1 //===--- ModuleBuilder.cpp - Emit LLVM Code from ASTs ---------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This builds an AST and converts it to LLVM Code.
12 //===----------------------------------------------------------------------===//
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/StringRef.h"
24 #include "llvm/IR/DataLayout.h"
25 #include "llvm/IR/LLVMContext.h"
26 #include "llvm/IR/Module.h"
28 using namespace clang;
31 class CodeGeneratorImpl : public CodeGenerator {
32 DiagnosticsEngine &Diags;
33 std::unique_ptr<const llvm::DataLayout> TD;
35 const CodeGenOptions CodeGenOpts; // Intentionally copied in.
37 std::unique_ptr<llvm::Module> M;
38 std::unique_ptr<CodeGen::CodeGenModule> Builder;
41 CodeGeneratorImpl(DiagnosticsEngine &diags, const std::string& ModuleName,
42 const CodeGenOptions &CGO, llvm::LLVMContext& C)
43 : Diags(diags), CodeGenOpts(CGO),
44 M(new llvm::Module(ModuleName, C)) {}
46 virtual ~CodeGeneratorImpl() {}
48 llvm::Module* GetModule() override {
52 llvm::Module *ReleaseModule() override { return M.release(); }
54 void Initialize(ASTContext &Context) override {
57 M->setTargetTriple(Ctx->getTargetInfo().getTriple().getTriple());
58 M->setDataLayout(Ctx->getTargetInfo().getTargetDescription());
59 TD.reset(new llvm::DataLayout(Ctx->getTargetInfo().getTargetDescription()));
60 Builder.reset(new CodeGen::CodeGenModule(Context, CodeGenOpts, *M, *TD,
63 for (size_t i = 0, e = CodeGenOpts.DependentLibraries.size(); i < e; ++i)
64 HandleDependentLibrary(CodeGenOpts.DependentLibraries[i]);
67 void HandleCXXStaticMemberVarInstantiation(VarDecl *VD) override {
68 if (Diags.hasErrorOccurred())
71 Builder->HandleCXXStaticMemberVarInstantiation(VD);
74 bool HandleTopLevelDecl(DeclGroupRef DG) override {
75 if (Diags.hasErrorOccurred())
78 // Make sure to emit all elements of a Decl.
79 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
80 Builder->EmitTopLevelDecl(*I);
84 /// HandleTagDeclDefinition - This callback is invoked each time a TagDecl
85 /// to (e.g. struct, union, enum, class) is completed. This allows the
86 /// client hack on the type, which can occur at any point in the file
87 /// (because these can be defined in declspecs).
88 void HandleTagDeclDefinition(TagDecl *D) override {
89 if (Diags.hasErrorOccurred())
92 Builder->UpdateCompletedType(D);
94 // In C++, we may have member functions that need to be emitted at this
96 if (Ctx->getLangOpts().CPlusPlus && !D->isDependentContext()) {
97 for (auto *M : D->decls())
98 if (auto *Method = dyn_cast<CXXMethodDecl>(M))
99 if (Method->doesThisDeclarationHaveABody() &&
100 (Method->hasAttr<UsedAttr>() ||
101 Method->hasAttr<ConstructorAttr>()))
102 Builder->EmitTopLevelDecl(Method);
106 void HandleTagDeclRequiredDefinition(const TagDecl *D) override {
107 if (Diags.hasErrorOccurred())
110 if (CodeGen::CGDebugInfo *DI = Builder->getModuleDebugInfo())
111 if (const RecordDecl *RD = dyn_cast<RecordDecl>(D))
112 DI->completeRequiredType(RD);
115 void HandleTranslationUnit(ASTContext &Ctx) override {
116 if (Diags.hasErrorOccurred()) {
127 void CompleteTentativeDefinition(VarDecl *D) override {
128 if (Diags.hasErrorOccurred())
131 Builder->EmitTentativeDefinition(D);
134 void HandleVTable(CXXRecordDecl *RD, bool DefinitionRequired) override {
135 if (Diags.hasErrorOccurred())
138 Builder->EmitVTable(RD, DefinitionRequired);
141 void HandleLinkerOptionPragma(llvm::StringRef Opts) override {
142 Builder->AppendLinkerOptions(Opts);
145 void HandleDetectMismatch(llvm::StringRef Name,
146 llvm::StringRef Value) override {
147 Builder->AddDetectMismatch(Name, Value);
150 void HandleDependentLibrary(llvm::StringRef Lib) override {
151 Builder->AddDependentLib(Lib);
156 void CodeGenerator::anchor() { }
158 CodeGenerator *clang::CreateLLVMCodeGen(DiagnosticsEngine &Diags,
159 const std::string& ModuleName,
160 const CodeGenOptions &CGO,
161 const TargetOptions &/*TO*/,
162 llvm::LLVMContext& C) {
163 return new CodeGeneratorImpl(Diags, ModuleName, CGO, C);