xref: /llvm-project/clang/lib/CodeGen/ModuleBuilder.cpp (revision dfca6f97bcabeb67685f02b721d276fec93b4ea9)
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/StringRef.h"
24 #include "llvm/IR/DataLayout.h"
25 #include "llvm/IR/LLVMContext.h"
26 #include "llvm/IR/Module.h"
27 #include <memory>
28 using namespace clang;
29 
30 namespace {
31   class CodeGeneratorImpl : public CodeGenerator {
32     DiagnosticsEngine &Diags;
33     std::unique_ptr<const llvm::DataLayout> TD;
34     ASTContext *Ctx;
35     const CodeGenOptions CodeGenOpts;  // Intentionally copied in.
36   protected:
37     std::unique_ptr<llvm::Module> M;
38     std::unique_ptr<CodeGen::CodeGenModule> Builder;
39 
40   public:
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)) {}
45 
46     virtual ~CodeGeneratorImpl() {}
47 
48     virtual llvm::Module* GetModule() {
49       return M.get();
50     }
51 
52     virtual llvm::Module *ReleaseModule() { return M.release(); }
53 
54     virtual void Initialize(ASTContext &Context) {
55       Ctx = &Context;
56 
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,
61                                                Diags));
62 
63       for (size_t i = 0, e = CodeGenOpts.DependentLibraries.size(); i < e; ++i)
64         HandleDependentLibrary(CodeGenOpts.DependentLibraries[i]);
65     }
66 
67     virtual void HandleCXXStaticMemberVarInstantiation(VarDecl *VD) {
68       if (Diags.hasErrorOccurred())
69         return;
70 
71       Builder->HandleCXXStaticMemberVarInstantiation(VD);
72     }
73 
74     virtual bool HandleTopLevelDecl(DeclGroupRef DG) {
75       if (Diags.hasErrorOccurred())
76         return true;
77 
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);
81       return true;
82     }
83 
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     virtual void HandleTagDeclDefinition(TagDecl *D) {
89       if (Diags.hasErrorOccurred())
90         return;
91 
92       Builder->UpdateCompletedType(D);
93 
94       // In C++, we may have member functions that need to be emitted at this
95       // point.
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);
103       }
104     }
105 
106     virtual void HandleTagDeclRequiredDefinition(const TagDecl *D) override {
107       if (Diags.hasErrorOccurred())
108         return;
109 
110       if (CodeGen::CGDebugInfo *DI = Builder->getModuleDebugInfo())
111         if (const RecordDecl *RD = dyn_cast<RecordDecl>(D))
112           DI->completeRequiredType(RD);
113     }
114 
115     virtual void HandleTranslationUnit(ASTContext &Ctx) {
116       if (Diags.hasErrorOccurred()) {
117         if (Builder)
118           Builder->clear();
119         M.reset();
120         return;
121       }
122 
123       if (Builder)
124         Builder->Release();
125     }
126 
127     virtual void CompleteTentativeDefinition(VarDecl *D) {
128       if (Diags.hasErrorOccurred())
129         return;
130 
131       Builder->EmitTentativeDefinition(D);
132     }
133 
134     virtual void HandleVTable(CXXRecordDecl *RD, bool DefinitionRequired) {
135       if (Diags.hasErrorOccurred())
136         return;
137 
138       Builder->EmitVTable(RD, DefinitionRequired);
139     }
140 
141     virtual void HandleLinkerOptionPragma(llvm::StringRef Opts) {
142       Builder->AppendLinkerOptions(Opts);
143     }
144 
145     virtual void HandleDetectMismatch(llvm::StringRef Name,
146                                       llvm::StringRef Value) {
147       Builder->AddDetectMismatch(Name, Value);
148     }
149 
150     virtual void HandleDependentLibrary(llvm::StringRef Lib) {
151       Builder->AddDependentLib(Lib);
152     }
153   };
154 }
155 
156 void CodeGenerator::anchor() { }
157 
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);
164 }
165