xref: /minix3/external/bsd/llvm/dist/clang/lib/CodeGen/ModuleBuilder.cpp (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1f4a2713aSLionel Sambuc //===--- ModuleBuilder.cpp - Emit LLVM Code from ASTs ---------------------===//
2f4a2713aSLionel Sambuc //
3f4a2713aSLionel Sambuc //                     The LLVM Compiler Infrastructure
4f4a2713aSLionel Sambuc //
5f4a2713aSLionel Sambuc // This file is distributed under the University of Illinois Open Source
6f4a2713aSLionel Sambuc // License. See LICENSE.TXT for details.
7f4a2713aSLionel Sambuc //
8f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
9f4a2713aSLionel Sambuc //
10f4a2713aSLionel Sambuc // This builds an AST and converts it to LLVM Code.
11f4a2713aSLionel Sambuc //
12f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
13f4a2713aSLionel Sambuc 
14f4a2713aSLionel Sambuc #include "clang/CodeGen/ModuleBuilder.h"
15f4a2713aSLionel Sambuc #include "CGDebugInfo.h"
16*0a6a1f1dSLionel Sambuc #include "CodeGenModule.h"
17f4a2713aSLionel Sambuc #include "clang/AST/ASTContext.h"
18f4a2713aSLionel Sambuc #include "clang/AST/DeclObjC.h"
19f4a2713aSLionel Sambuc #include "clang/AST/Expr.h"
20f4a2713aSLionel Sambuc #include "clang/Basic/Diagnostic.h"
21f4a2713aSLionel Sambuc #include "clang/Basic/TargetInfo.h"
22f4a2713aSLionel Sambuc #include "clang/Frontend/CodeGenOptions.h"
23f4a2713aSLionel Sambuc #include "llvm/ADT/StringRef.h"
24f4a2713aSLionel Sambuc #include "llvm/IR/DataLayout.h"
25f4a2713aSLionel Sambuc #include "llvm/IR/LLVMContext.h"
26f4a2713aSLionel Sambuc #include "llvm/IR/Module.h"
27*0a6a1f1dSLionel Sambuc #include <memory>
28f4a2713aSLionel Sambuc using namespace clang;
29f4a2713aSLionel Sambuc 
30f4a2713aSLionel Sambuc namespace {
31f4a2713aSLionel Sambuc   class CodeGeneratorImpl : public CodeGenerator {
32f4a2713aSLionel Sambuc     DiagnosticsEngine &Diags;
33*0a6a1f1dSLionel Sambuc     std::unique_ptr<const llvm::DataLayout> TD;
34f4a2713aSLionel Sambuc     ASTContext *Ctx;
35f4a2713aSLionel Sambuc     const CodeGenOptions CodeGenOpts;  // Intentionally copied in.
36*0a6a1f1dSLionel Sambuc 
37*0a6a1f1dSLionel Sambuc     unsigned HandlingTopLevelDecls;
38*0a6a1f1dSLionel Sambuc     struct HandlingTopLevelDeclRAII {
39*0a6a1f1dSLionel Sambuc       CodeGeneratorImpl &Self;
HandlingTopLevelDeclRAII__anoncaca9f7e0111::CodeGeneratorImpl::HandlingTopLevelDeclRAII40*0a6a1f1dSLionel Sambuc       HandlingTopLevelDeclRAII(CodeGeneratorImpl &Self) : Self(Self) {
41*0a6a1f1dSLionel Sambuc         ++Self.HandlingTopLevelDecls;
42*0a6a1f1dSLionel Sambuc       }
~HandlingTopLevelDeclRAII__anoncaca9f7e0111::CodeGeneratorImpl::HandlingTopLevelDeclRAII43*0a6a1f1dSLionel Sambuc       ~HandlingTopLevelDeclRAII() {
44*0a6a1f1dSLionel Sambuc         if (--Self.HandlingTopLevelDecls == 0)
45*0a6a1f1dSLionel Sambuc           Self.EmitDeferredDecls();
46*0a6a1f1dSLionel Sambuc       }
47*0a6a1f1dSLionel Sambuc     };
48*0a6a1f1dSLionel Sambuc 
49*0a6a1f1dSLionel Sambuc     CoverageSourceInfo *CoverageInfo;
50*0a6a1f1dSLionel Sambuc 
51f4a2713aSLionel Sambuc   protected:
52*0a6a1f1dSLionel Sambuc     std::unique_ptr<llvm::Module> M;
53*0a6a1f1dSLionel Sambuc     std::unique_ptr<CodeGen::CodeGenModule> Builder;
54*0a6a1f1dSLionel Sambuc 
55*0a6a1f1dSLionel Sambuc   private:
56*0a6a1f1dSLionel Sambuc     SmallVector<CXXMethodDecl *, 8> DeferredInlineMethodDefinitions;
57*0a6a1f1dSLionel Sambuc 
58f4a2713aSLionel Sambuc   public:
CodeGeneratorImpl(DiagnosticsEngine & diags,const std::string & ModuleName,const CodeGenOptions & CGO,llvm::LLVMContext & C,CoverageSourceInfo * CoverageInfo=nullptr)59f4a2713aSLionel Sambuc     CodeGeneratorImpl(DiagnosticsEngine &diags, const std::string& ModuleName,
60*0a6a1f1dSLionel Sambuc                       const CodeGenOptions &CGO, llvm::LLVMContext& C,
61*0a6a1f1dSLionel Sambuc                       CoverageSourceInfo *CoverageInfo = nullptr)
62*0a6a1f1dSLionel Sambuc       : Diags(diags), Ctx(nullptr), CodeGenOpts(CGO), HandlingTopLevelDecls(0),
63*0a6a1f1dSLionel Sambuc         CoverageInfo(CoverageInfo),
64f4a2713aSLionel Sambuc         M(new llvm::Module(ModuleName, C)) {}
65f4a2713aSLionel Sambuc 
~CodeGeneratorImpl()66*0a6a1f1dSLionel Sambuc     virtual ~CodeGeneratorImpl() {
67*0a6a1f1dSLionel Sambuc       // There should normally not be any leftover inline method definitions.
68*0a6a1f1dSLionel Sambuc       assert(DeferredInlineMethodDefinitions.empty() ||
69*0a6a1f1dSLionel Sambuc              Diags.hasErrorOccurred());
70*0a6a1f1dSLionel Sambuc     }
71f4a2713aSLionel Sambuc 
GetModule()72*0a6a1f1dSLionel Sambuc     llvm::Module* GetModule() override {
73f4a2713aSLionel Sambuc       return M.get();
74f4a2713aSLionel Sambuc     }
75f4a2713aSLionel Sambuc 
GetDeclForMangledName(StringRef MangledName)76*0a6a1f1dSLionel Sambuc     const Decl *GetDeclForMangledName(StringRef MangledName) override {
77*0a6a1f1dSLionel Sambuc       GlobalDecl Result;
78*0a6a1f1dSLionel Sambuc       if (!Builder->lookupRepresentativeDecl(MangledName, Result))
79*0a6a1f1dSLionel Sambuc         return nullptr;
80*0a6a1f1dSLionel Sambuc       const Decl *D = Result.getCanonicalDecl().getDecl();
81*0a6a1f1dSLionel Sambuc       if (auto FD = dyn_cast<FunctionDecl>(D)) {
82*0a6a1f1dSLionel Sambuc         if (FD->hasBody(FD))
83*0a6a1f1dSLionel Sambuc           return FD;
84*0a6a1f1dSLionel Sambuc       } else if (auto TD = dyn_cast<TagDecl>(D)) {
85*0a6a1f1dSLionel Sambuc         if (auto Def = TD->getDefinition())
86*0a6a1f1dSLionel Sambuc           return Def;
87*0a6a1f1dSLionel Sambuc       }
88*0a6a1f1dSLionel Sambuc       return D;
89f4a2713aSLionel Sambuc     }
90f4a2713aSLionel Sambuc 
ReleaseModule()91*0a6a1f1dSLionel Sambuc     llvm::Module *ReleaseModule() override { return M.release(); }
92*0a6a1f1dSLionel Sambuc 
Initialize(ASTContext & Context)93*0a6a1f1dSLionel Sambuc     void Initialize(ASTContext &Context) override {
94f4a2713aSLionel Sambuc       Ctx = &Context;
95f4a2713aSLionel Sambuc 
96f4a2713aSLionel Sambuc       M->setTargetTriple(Ctx->getTargetInfo().getTriple().getTriple());
97f4a2713aSLionel Sambuc       M->setDataLayout(Ctx->getTargetInfo().getTargetDescription());
98f4a2713aSLionel Sambuc       TD.reset(new llvm::DataLayout(Ctx->getTargetInfo().getTargetDescription()));
99f4a2713aSLionel Sambuc       Builder.reset(new CodeGen::CodeGenModule(Context, CodeGenOpts, *M, *TD,
100*0a6a1f1dSLionel Sambuc                                                Diags, CoverageInfo));
101f4a2713aSLionel Sambuc 
102f4a2713aSLionel Sambuc       for (size_t i = 0, e = CodeGenOpts.DependentLibraries.size(); i < e; ++i)
103f4a2713aSLionel Sambuc         HandleDependentLibrary(CodeGenOpts.DependentLibraries[i]);
104f4a2713aSLionel Sambuc     }
105f4a2713aSLionel Sambuc 
HandleCXXStaticMemberVarInstantiation(VarDecl * VD)106*0a6a1f1dSLionel Sambuc     void HandleCXXStaticMemberVarInstantiation(VarDecl *VD) override {
107f4a2713aSLionel Sambuc       if (Diags.hasErrorOccurred())
108f4a2713aSLionel Sambuc         return;
109f4a2713aSLionel Sambuc 
110f4a2713aSLionel Sambuc       Builder->HandleCXXStaticMemberVarInstantiation(VD);
111f4a2713aSLionel Sambuc     }
112f4a2713aSLionel Sambuc 
HandleTopLevelDecl(DeclGroupRef DG)113*0a6a1f1dSLionel Sambuc     bool HandleTopLevelDecl(DeclGroupRef DG) override {
114f4a2713aSLionel Sambuc       if (Diags.hasErrorOccurred())
115f4a2713aSLionel Sambuc         return true;
116f4a2713aSLionel Sambuc 
117*0a6a1f1dSLionel Sambuc       HandlingTopLevelDeclRAII HandlingDecl(*this);
118*0a6a1f1dSLionel Sambuc 
119f4a2713aSLionel Sambuc       // Make sure to emit all elements of a Decl.
120f4a2713aSLionel Sambuc       for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
121f4a2713aSLionel Sambuc         Builder->EmitTopLevelDecl(*I);
122*0a6a1f1dSLionel Sambuc 
123f4a2713aSLionel Sambuc       return true;
124f4a2713aSLionel Sambuc     }
125f4a2713aSLionel Sambuc 
EmitDeferredDecls()126*0a6a1f1dSLionel Sambuc     void EmitDeferredDecls() {
127*0a6a1f1dSLionel Sambuc       if (DeferredInlineMethodDefinitions.empty())
128*0a6a1f1dSLionel Sambuc         return;
129*0a6a1f1dSLionel Sambuc 
130*0a6a1f1dSLionel Sambuc       // Emit any deferred inline method definitions. Note that more deferred
131*0a6a1f1dSLionel Sambuc       // methods may be added during this loop, since ASTConsumer callbacks
132*0a6a1f1dSLionel Sambuc       // can be invoked if AST inspection results in declarations being added.
133*0a6a1f1dSLionel Sambuc       HandlingTopLevelDeclRAII HandlingDecl(*this);
134*0a6a1f1dSLionel Sambuc       for (unsigned I = 0; I != DeferredInlineMethodDefinitions.size(); ++I)
135*0a6a1f1dSLionel Sambuc         Builder->EmitTopLevelDecl(DeferredInlineMethodDefinitions[I]);
136*0a6a1f1dSLionel Sambuc       DeferredInlineMethodDefinitions.clear();
137*0a6a1f1dSLionel Sambuc     }
138*0a6a1f1dSLionel Sambuc 
HandleInlineMethodDefinition(CXXMethodDecl * D)139*0a6a1f1dSLionel Sambuc     void HandleInlineMethodDefinition(CXXMethodDecl *D) override {
140*0a6a1f1dSLionel Sambuc       if (Diags.hasErrorOccurred())
141*0a6a1f1dSLionel Sambuc         return;
142*0a6a1f1dSLionel Sambuc 
143*0a6a1f1dSLionel Sambuc       assert(D->doesThisDeclarationHaveABody());
144*0a6a1f1dSLionel Sambuc 
145*0a6a1f1dSLionel Sambuc       // We may want to emit this definition. However, that decision might be
146*0a6a1f1dSLionel Sambuc       // based on computing the linkage, and we have to defer that in case we
147*0a6a1f1dSLionel Sambuc       // are inside of something that will change the method's final linkage,
148*0a6a1f1dSLionel Sambuc       // e.g.
149*0a6a1f1dSLionel Sambuc       //   typedef struct {
150*0a6a1f1dSLionel Sambuc       //     void bar();
151*0a6a1f1dSLionel Sambuc       //     void foo() { bar(); }
152*0a6a1f1dSLionel Sambuc       //   } A;
153*0a6a1f1dSLionel Sambuc       DeferredInlineMethodDefinitions.push_back(D);
154*0a6a1f1dSLionel Sambuc 
155*0a6a1f1dSLionel Sambuc       // Provide some coverage mapping even for methods that aren't emitted.
156*0a6a1f1dSLionel Sambuc       // Don't do this for templated classes though, as they may not be
157*0a6a1f1dSLionel Sambuc       // instantiable.
158*0a6a1f1dSLionel Sambuc       if (!D->getParent()->getDescribedClassTemplate())
159*0a6a1f1dSLionel Sambuc         Builder->AddDeferredUnusedCoverageMapping(D);
160*0a6a1f1dSLionel Sambuc     }
161*0a6a1f1dSLionel Sambuc 
162f4a2713aSLionel Sambuc     /// HandleTagDeclDefinition - This callback is invoked each time a TagDecl
163f4a2713aSLionel Sambuc     /// to (e.g. struct, union, enum, class) is completed. This allows the
164f4a2713aSLionel Sambuc     /// client hack on the type, which can occur at any point in the file
165f4a2713aSLionel Sambuc     /// (because these can be defined in declspecs).
HandleTagDeclDefinition(TagDecl * D)166*0a6a1f1dSLionel Sambuc     void HandleTagDeclDefinition(TagDecl *D) override {
167f4a2713aSLionel Sambuc       if (Diags.hasErrorOccurred())
168f4a2713aSLionel Sambuc         return;
169f4a2713aSLionel Sambuc 
170f4a2713aSLionel Sambuc       Builder->UpdateCompletedType(D);
171f4a2713aSLionel Sambuc 
172*0a6a1f1dSLionel Sambuc       // For MSVC compatibility, treat declarations of static data members with
173*0a6a1f1dSLionel Sambuc       // inline initializers as definitions.
174*0a6a1f1dSLionel Sambuc       if (Ctx->getLangOpts().MSVCCompat) {
175*0a6a1f1dSLionel Sambuc         for (Decl *Member : D->decls()) {
176*0a6a1f1dSLionel Sambuc           if (VarDecl *VD = dyn_cast<VarDecl>(Member)) {
177*0a6a1f1dSLionel Sambuc             if (Ctx->isMSStaticDataMemberInlineDefinition(VD) &&
178*0a6a1f1dSLionel Sambuc                 Ctx->DeclMustBeEmitted(VD)) {
179*0a6a1f1dSLionel Sambuc               Builder->EmitGlobal(VD);
180*0a6a1f1dSLionel Sambuc             }
181*0a6a1f1dSLionel Sambuc           }
182*0a6a1f1dSLionel Sambuc         }
183f4a2713aSLionel Sambuc       }
184f4a2713aSLionel Sambuc     }
185f4a2713aSLionel Sambuc 
HandleTagDeclRequiredDefinition(const TagDecl * D)186*0a6a1f1dSLionel Sambuc     void HandleTagDeclRequiredDefinition(const TagDecl *D) override {
187f4a2713aSLionel Sambuc       if (Diags.hasErrorOccurred())
188f4a2713aSLionel Sambuc         return;
189f4a2713aSLionel Sambuc 
190f4a2713aSLionel Sambuc       if (CodeGen::CGDebugInfo *DI = Builder->getModuleDebugInfo())
191f4a2713aSLionel Sambuc         if (const RecordDecl *RD = dyn_cast<RecordDecl>(D))
192f4a2713aSLionel Sambuc           DI->completeRequiredType(RD);
193f4a2713aSLionel Sambuc     }
194f4a2713aSLionel Sambuc 
HandleTranslationUnit(ASTContext & Ctx)195*0a6a1f1dSLionel Sambuc     void HandleTranslationUnit(ASTContext &Ctx) override {
196f4a2713aSLionel Sambuc       if (Diags.hasErrorOccurred()) {
197*0a6a1f1dSLionel Sambuc         if (Builder)
198*0a6a1f1dSLionel Sambuc           Builder->clear();
199f4a2713aSLionel Sambuc         M.reset();
200f4a2713aSLionel Sambuc         return;
201f4a2713aSLionel Sambuc       }
202f4a2713aSLionel Sambuc 
203f4a2713aSLionel Sambuc       if (Builder)
204f4a2713aSLionel Sambuc         Builder->Release();
205f4a2713aSLionel Sambuc     }
206f4a2713aSLionel Sambuc 
CompleteTentativeDefinition(VarDecl * D)207*0a6a1f1dSLionel Sambuc     void CompleteTentativeDefinition(VarDecl *D) override {
208f4a2713aSLionel Sambuc       if (Diags.hasErrorOccurred())
209f4a2713aSLionel Sambuc         return;
210f4a2713aSLionel Sambuc 
211f4a2713aSLionel Sambuc       Builder->EmitTentativeDefinition(D);
212f4a2713aSLionel Sambuc     }
213f4a2713aSLionel Sambuc 
HandleVTable(CXXRecordDecl * RD,bool DefinitionRequired)214*0a6a1f1dSLionel Sambuc     void HandleVTable(CXXRecordDecl *RD, bool DefinitionRequired) override {
215f4a2713aSLionel Sambuc       if (Diags.hasErrorOccurred())
216f4a2713aSLionel Sambuc         return;
217f4a2713aSLionel Sambuc 
218f4a2713aSLionel Sambuc       Builder->EmitVTable(RD, DefinitionRequired);
219f4a2713aSLionel Sambuc     }
220f4a2713aSLionel Sambuc 
HandleLinkerOptionPragma(llvm::StringRef Opts)221*0a6a1f1dSLionel Sambuc     void HandleLinkerOptionPragma(llvm::StringRef Opts) override {
222f4a2713aSLionel Sambuc       Builder->AppendLinkerOptions(Opts);
223f4a2713aSLionel Sambuc     }
224f4a2713aSLionel Sambuc 
HandleDetectMismatch(llvm::StringRef Name,llvm::StringRef Value)225*0a6a1f1dSLionel Sambuc     void HandleDetectMismatch(llvm::StringRef Name,
226*0a6a1f1dSLionel Sambuc                               llvm::StringRef Value) override {
227f4a2713aSLionel Sambuc       Builder->AddDetectMismatch(Name, Value);
228f4a2713aSLionel Sambuc     }
229f4a2713aSLionel Sambuc 
HandleDependentLibrary(llvm::StringRef Lib)230*0a6a1f1dSLionel Sambuc     void HandleDependentLibrary(llvm::StringRef Lib) override {
231f4a2713aSLionel Sambuc       Builder->AddDependentLib(Lib);
232f4a2713aSLionel Sambuc     }
233f4a2713aSLionel Sambuc   };
234f4a2713aSLionel Sambuc }
235f4a2713aSLionel Sambuc 
anchor()236f4a2713aSLionel Sambuc void CodeGenerator::anchor() { }
237f4a2713aSLionel Sambuc 
CreateLLVMCodeGen(DiagnosticsEngine & Diags,const std::string & ModuleName,const CodeGenOptions & CGO,const TargetOptions &,llvm::LLVMContext & C,CoverageSourceInfo * CoverageInfo)238f4a2713aSLionel Sambuc CodeGenerator *clang::CreateLLVMCodeGen(DiagnosticsEngine &Diags,
239f4a2713aSLionel Sambuc                                         const std::string& ModuleName,
240f4a2713aSLionel Sambuc                                         const CodeGenOptions &CGO,
241f4a2713aSLionel Sambuc                                         const TargetOptions &/*TO*/,
242*0a6a1f1dSLionel Sambuc                                         llvm::LLVMContext& C,
243*0a6a1f1dSLionel Sambuc                                         CoverageSourceInfo *CoverageInfo) {
244*0a6a1f1dSLionel Sambuc   return new CodeGeneratorImpl(Diags, ModuleName, CGO, C, CoverageInfo);
245f4a2713aSLionel Sambuc }
246