1 //===--- ModuleBuilder.cpp - Emit LLVM Code from ASTs ---------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This builds an AST and converts it to LLVM Code. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "clang/CodeGen/ModuleBuilder.h" 15 #include "CGDebugInfo.h" 16 #include "CodeGenModule.h" 17 #include "clang/AST/ASTContext.h" 18 #include "clang/AST/DeclObjC.h" 19 #include "clang/AST/Expr.h" 20 #include "clang/Basic/Diagnostic.h" 21 #include "clang/Basic/TargetInfo.h" 22 #include "clang/Frontend/CodeGenOptions.h" 23 #include "llvm/ADT/OwningPtr.h" 24 #include "llvm/ADT/StringRef.h" 25 #include "llvm/IR/DataLayout.h" 26 #include "llvm/IR/LLVMContext.h" 27 #include "llvm/IR/Module.h" 28 using namespace clang; 29 30 namespace { 31 class CodeGeneratorImpl : public CodeGenerator { 32 DiagnosticsEngine &Diags; 33 OwningPtr<const llvm::DataLayout> TD; 34 ASTContext *Ctx; 35 const CodeGenOptions CodeGenOpts; // Intentionally copied in. 36 protected: 37 OwningPtr<llvm::Module> M; 38 OwningPtr<CodeGen::CodeGenModule> Builder; 39 public: 40 CodeGeneratorImpl(DiagnosticsEngine &diags, const std::string& ModuleName, 41 const CodeGenOptions &CGO, llvm::LLVMContext& C) 42 : Diags(diags), CodeGenOpts(CGO), 43 M(new llvm::Module(ModuleName, C)) {} 44 45 virtual ~CodeGeneratorImpl() {} 46 47 virtual llvm::Module* GetModule() { 48 return M.get(); 49 } 50 51 virtual llvm::Module *ReleaseModule() { return M.release(); } 52 53 virtual void Initialize(ASTContext &Context) { 54 Ctx = &Context; 55 56 M->setTargetTriple(Ctx->getTargetInfo().getTriple().getTriple()); 57 M->setDataLayout(Ctx->getTargetInfo().getTargetDescription()); 58 TD.reset(new llvm::DataLayout(Ctx->getTargetInfo().getTargetDescription())); 59 Builder.reset(new CodeGen::CodeGenModule(Context, CodeGenOpts, *M, *TD, 60 Diags)); 61 62 for (size_t i = 0, e = CodeGenOpts.DependentLibraries.size(); i < e; ++i) 63 HandleDependentLibrary(CodeGenOpts.DependentLibraries[i]); 64 } 65 66 virtual void HandleCXXStaticMemberVarInstantiation(VarDecl *VD) { 67 if (Diags.hasErrorOccurred()) 68 return; 69 70 Builder->HandleCXXStaticMemberVarInstantiation(VD); 71 } 72 73 virtual bool HandleTopLevelDecl(DeclGroupRef DG) { 74 if (Diags.hasErrorOccurred()) 75 return true; 76 77 // Make sure to emit all elements of a Decl. 78 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I) 79 Builder->EmitTopLevelDecl(*I); 80 return true; 81 } 82 83 /// HandleTagDeclDefinition - This callback is invoked each time a TagDecl 84 /// to (e.g. struct, union, enum, class) is completed. This allows the 85 /// client hack on the type, which can occur at any point in the file 86 /// (because these can be defined in declspecs). 87 virtual void HandleTagDeclDefinition(TagDecl *D) { 88 if (Diags.hasErrorOccurred()) 89 return; 90 91 Builder->UpdateCompletedType(D); 92 93 // In C++, we may have member functions that need to be emitted at this 94 // point. 95 if (Ctx->getLangOpts().CPlusPlus && !D->isDependentContext()) { 96 for (auto *M : D->decls()) 97 if (auto *Method = dyn_cast<CXXMethodDecl>(M)) 98 if (Method->doesThisDeclarationHaveABody() && 99 (Method->hasAttr<UsedAttr>() || 100 Method->hasAttr<ConstructorAttr>())) 101 Builder->EmitTopLevelDecl(Method); 102 } 103 } 104 105 virtual void HandleTagDeclRequiredDefinition(const TagDecl *D) override { 106 if (Diags.hasErrorOccurred()) 107 return; 108 109 if (CodeGen::CGDebugInfo *DI = Builder->getModuleDebugInfo()) 110 if (const RecordDecl *RD = dyn_cast<RecordDecl>(D)) 111 DI->completeRequiredType(RD); 112 } 113 114 virtual void HandleTranslationUnit(ASTContext &Ctx) { 115 if (Diags.hasErrorOccurred()) { 116 if (Builder) 117 Builder->clear(); 118 M.reset(); 119 return; 120 } 121 122 if (Builder) 123 Builder->Release(); 124 } 125 126 virtual void CompleteTentativeDefinition(VarDecl *D) { 127 if (Diags.hasErrorOccurred()) 128 return; 129 130 Builder->EmitTentativeDefinition(D); 131 } 132 133 virtual void HandleVTable(CXXRecordDecl *RD, bool DefinitionRequired) { 134 if (Diags.hasErrorOccurred()) 135 return; 136 137 Builder->EmitVTable(RD, DefinitionRequired); 138 } 139 140 virtual void HandleLinkerOptionPragma(llvm::StringRef Opts) { 141 Builder->AppendLinkerOptions(Opts); 142 } 143 144 virtual void HandleDetectMismatch(llvm::StringRef Name, 145 llvm::StringRef Value) { 146 Builder->AddDetectMismatch(Name, Value); 147 } 148 149 virtual void HandleDependentLibrary(llvm::StringRef Lib) { 150 Builder->AddDependentLib(Lib); 151 } 152 }; 153 } 154 155 void CodeGenerator::anchor() { } 156 157 CodeGenerator *clang::CreateLLVMCodeGen(DiagnosticsEngine &Diags, 158 const std::string& ModuleName, 159 const CodeGenOptions &CGO, 160 const TargetOptions &/*TO*/, 161 llvm::LLVMContext& C) { 162 return new CodeGeneratorImpl(Diags, ModuleName, CGO, C); 163 } 164