1 #ifndef _LLVM_NDK_CC_BACKEND_H 2 #define _LLVM_NDK_CC_BACKEND_H 3 4 #include "clang/AST/ASTConsumer.h" 5 6 #include "llvm/PassManager.h" 7 8 #include "llvm/Support/FormattedStream.h" 9 #include "llvm/Support/PassManagerBuilder.h" 10 11 #include "Compiler.h" 12 13 namespace llvm { 14 class formatted_raw_ostream; 15 class LLVMContext; 16 class NamedMDNode; 17 class Module; 18 class PassManager; 19 class FunctionPassManager; 20 } 21 22 namespace clang { 23 class CodeGenOptions; 24 class CodeGenerator; 25 class DeclGroupRef; 26 class TagDecl; 27 class VarDecl; 28 } 29 30 namespace ndkpc { 31 32 class Backend : public clang::ASTConsumer { 33 public: 34 Backend(const clang::CodeGenOptions &CodeGenOpts, 35 const clang::TargetOptions &TargetOpts, 36 clang::Diagnostic *Diags, 37 llvm::raw_ostream *OS, 38 Compiler::OutputType OT); 39 40 virtual ~Backend(); 41 42 // Initialize - This is called to initialize the consumer, providing the 43 // ASTContext. 44 virtual void Initialize(clang::ASTContext &Ctx); 45 46 // HandleTopLevelDecl - Handle the specified top-level declaration. This is 47 // called by the parser to process every top-level Decl*. Note that D can be 48 // the head of a chain of Decls (e.g. for `int a, b` the chain will have two 49 // elements). Use Decl::getNextDeclarator() to walk the chain. 50 virtual void HandleTopLevelDecl(clang::DeclGroupRef D); 51 52 // HandleTranslationUnit - This method is called when the ASTs for entire 53 // translation unit have been parsed. 54 virtual void HandleTranslationUnit(clang::ASTContext &Ctx); 55 56 // HandleTagDeclDefinition - This callback is invoked each time a TagDecl 57 // (e.g. struct, union, enum, class) is completed. This allows the client to 58 // hack on the type, which can occur at any point in the file (because these 59 // can be defined in declspecs). 60 virtual void HandleTagDeclDefinition(clang::TagDecl *D); 61 62 // CompleteTentativeDefinition - Callback invoked at the end of a translation 63 // unit to notify the consumer that the given tentative definition should be 64 // completed. 65 virtual void CompleteTentativeDefinition(clang::VarDecl *D); 66 67 private: 68 const clang::CodeGenOptions &mCodeGenOpts; 69 const clang::TargetOptions &mTargetOpts; 70 71 llvm::LLVMContext &mLLVMContext; 72 clang::Diagnostic &mDiags; 73 74 llvm::Module *mpModule; 75 76 // Output stream 77 llvm::raw_ostream *mpOS; 78 Compiler::OutputType mOT; 79 80 // This helps us translate Clang AST using into LLVM IR 81 clang::CodeGenerator *mpGen; 82 83 // Passes apply on function scope in a translation unit 84 llvm::FunctionPassManager *mpPerFunctionPasses; 85 void CreateFunctionPasses(); 86 87 // Passes apply on module scope 88 llvm::PassManager *mpPerModulePasses; 89 void CreateModulePasses(); 90 91 // Passes for code emission 92 llvm::FunctionPassManager *mpCodeGenPasses; 93 bool CreateCodeGenPasses(); 94 95 llvm::formatted_raw_ostream FormattedOutStream; 96 }; 97 98 } 99 100 #endif // _LLVM_NDK_CC_BACKEND_H 101