1 //===- FrontendAction.h -----------------------------------------*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 /// 9 /// \file 10 /// Defines the flang::FrontendAction interface. 11 /// 12 //===----------------------------------------------------------------------===// 13 14 #ifndef LLVM_FLANG_FRONTEND_FRONTENDACTION_H 15 #define LLVM_FLANG_FRONTEND_FRONTENDACTION_H 16 17 #include "flang/Frontend/FrontendOptions.h" 18 #include "llvm/Support/Error.h" 19 20 namespace Fortran::frontend { 21 class CompilerInstance; 22 23 /// Abstract base class for actions which can be performed by the frontend. 24 class FrontendAction { 25 FrontendInputFile currentInput_; 26 CompilerInstance *instance_; 27 28 protected: 29 /// @name Implementation Action Interface 30 /// @{ 31 32 /// Callback to run the program action, using the initialized 33 /// compiler instance. 34 virtual void ExecuteAction() = 0; 35 36 /// Callback at the end of processing a single input, to determine 37 /// if the output files should be erased or not. 38 /// 39 /// By default it returns true if a compiler error occurred. 40 virtual bool ShouldEraseOutputFiles(); 41 42 /// @} 43 44 public: FrontendAction()45 FrontendAction() : instance_(nullptr) {} 46 virtual ~FrontendAction() = default; 47 48 /// @name Compiler Instance Access 49 /// @{ 50 instance()51 CompilerInstance &instance() const { 52 assert(instance_ && "Compiler instance not registered!"); 53 return *instance_; 54 } 55 set_instance(CompilerInstance * value)56 void set_instance(CompilerInstance *value) { instance_ = value; } 57 58 /// @} 59 /// @name Current File Information 60 /// @{ 61 currentInput()62 const FrontendInputFile ¤tInput() const { return currentInput_; } 63 GetCurrentFile()64 llvm::StringRef GetCurrentFile() const { 65 assert(!currentInput_.IsEmpty() && "No current file!"); 66 return currentInput_.file(); 67 } 68 GetCurrentFileOrBufferName()69 llvm::StringRef GetCurrentFileOrBufferName() const { 70 assert(!currentInput_.IsEmpty() && "No current file!"); 71 return currentInput_.IsFile() 72 ? currentInput_.file() 73 : currentInput_.buffer()->getBufferIdentifier(); 74 } 75 void set_currentInput(const FrontendInputFile ¤tInput); 76 77 /// @} 78 /// @name Public Action Interface 79 /// @} 80 81 /// Prepare the action for processing the input file \p input. 82 /// 83 /// This is run after the options and frontend have been initialized, 84 /// but prior to executing any per-file processing. 85 /// \param ci - The compiler instance this action is being run from. The 86 /// action may store and use this object. 87 /// \param input - The input filename and kind. 88 /// \return True on success; on failure the compilation of this file should 89 bool BeginSourceFile(CompilerInstance &ci, const FrontendInputFile &input); 90 91 /// Run the action. 92 llvm::Error Execute(); 93 94 /// Perform any per-file post processing, deallocate per-file 95 /// objects, and run statistics and output file cleanup code. 96 void EndSourceFile(); 97 }; 98 99 } // namespace Fortran::frontend 100 101 #endif // LLVM_FLANG_FRONTEND_FRONTENDACTION_H 102