xref: /llvm-project/clang/lib/CodeGen/ModuleBuilder.cpp (revision fb8d02b179732b17897f1d4024583949a56b0bb5)
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     llvm::Module* GetModule() override {
49       return M.get();
50     }
51 
52     const Decl *GetDeclForMangledName(StringRef MangledName) override {
53       GlobalDecl Result;
54       if (!Builder->lookupRepresentativeDecl(MangledName, Result))
55         return nullptr;
56       const Decl *D = Result.getCanonicalDecl().getDecl();
57       if (auto FD = dyn_cast<FunctionDecl>(D)) {
58         if (FD->hasBody(FD))
59           return FD;
60       } else if (auto TD = dyn_cast<TagDecl>(D)) {
61         if (auto Def = TD->getDefinition())
62           return Def;
63       }
64       return D;
65     }
66 
67     llvm::Module *ReleaseModule() override { return M.release(); }
68 
69     void Initialize(ASTContext &Context) override {
70       Ctx = &Context;
71 
72       M->setTargetTriple(Ctx->getTargetInfo().getTriple().getTriple());
73       M->setDataLayout(Ctx->getTargetInfo().getTargetDescription());
74       TD.reset(new llvm::DataLayout(Ctx->getTargetInfo().getTargetDescription()));
75       Builder.reset(new CodeGen::CodeGenModule(Context, CodeGenOpts, *M, *TD,
76                                                Diags));
77 
78       for (size_t i = 0, e = CodeGenOpts.DependentLibraries.size(); i < e; ++i)
79         HandleDependentLibrary(CodeGenOpts.DependentLibraries[i]);
80     }
81 
82     void HandleCXXStaticMemberVarInstantiation(VarDecl *VD) override {
83       if (Diags.hasErrorOccurred())
84         return;
85 
86       Builder->HandleCXXStaticMemberVarInstantiation(VD);
87     }
88 
89     bool HandleTopLevelDecl(DeclGroupRef DG) override {
90       if (Diags.hasErrorOccurred())
91         return true;
92 
93       // Make sure to emit all elements of a Decl.
94       for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
95         Builder->EmitTopLevelDecl(*I);
96       return true;
97     }
98 
99     void HandleInlineMethodDefinition(CXXMethodDecl *D) override {
100       if (Diags.hasErrorOccurred())
101         return;
102 
103       assert(D->doesThisDeclarationHaveABody());
104 
105       // We may have member functions that need to be emitted at this point.
106       if (!D->isDependentContext() &&
107           (D->hasAttr<UsedAttr>() || D->hasAttr<ConstructorAttr>() ||
108            D->hasAttr<DLLExportAttr>())) {
109         Builder->EmitTopLevelDecl(D);
110       }
111     }
112 
113     /// HandleTagDeclDefinition - This callback is invoked each time a TagDecl
114     /// to (e.g. struct, union, enum, class) is completed. This allows the
115     /// client hack on the type, which can occur at any point in the file
116     /// (because these can be defined in declspecs).
117     void HandleTagDeclDefinition(TagDecl *D) override {
118       if (Diags.hasErrorOccurred())
119         return;
120 
121       Builder->UpdateCompletedType(D);
122     }
123 
124     void HandleTagDeclRequiredDefinition(const TagDecl *D) override {
125       if (Diags.hasErrorOccurred())
126         return;
127 
128       if (CodeGen::CGDebugInfo *DI = Builder->getModuleDebugInfo())
129         if (const RecordDecl *RD = dyn_cast<RecordDecl>(D))
130           DI->completeRequiredType(RD);
131     }
132 
133     void HandleTranslationUnit(ASTContext &Ctx) override {
134       if (Diags.hasErrorOccurred()) {
135         if (Builder)
136           Builder->clear();
137         M.reset();
138         return;
139       }
140 
141       if (Builder)
142         Builder->Release();
143     }
144 
145     void CompleteTentativeDefinition(VarDecl *D) override {
146       if (Diags.hasErrorOccurred())
147         return;
148 
149       Builder->EmitTentativeDefinition(D);
150     }
151 
152     void HandleVTable(CXXRecordDecl *RD, bool DefinitionRequired) override {
153       if (Diags.hasErrorOccurred())
154         return;
155 
156       Builder->EmitVTable(RD, DefinitionRequired);
157     }
158 
159     void HandleLinkerOptionPragma(llvm::StringRef Opts) override {
160       Builder->AppendLinkerOptions(Opts);
161     }
162 
163     void HandleDetectMismatch(llvm::StringRef Name,
164                               llvm::StringRef Value) override {
165       Builder->AddDetectMismatch(Name, Value);
166     }
167 
168     void HandleDependentLibrary(llvm::StringRef Lib) override {
169       Builder->AddDependentLib(Lib);
170     }
171   };
172 }
173 
174 void CodeGenerator::anchor() { }
175 
176 CodeGenerator *clang::CreateLLVMCodeGen(DiagnosticsEngine &Diags,
177                                         const std::string& ModuleName,
178                                         const CodeGenOptions &CGO,
179                                         const TargetOptions &/*TO*/,
180                                         llvm::LLVMContext& C) {
181   return new CodeGeneratorImpl(Diags, ModuleName, CGO, C);
182 }
183