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 unsigned HandlingTopLevelDecls;
38 struct HandlingTopLevelDeclRAII {
39 CodeGeneratorImpl &Self;
40 HandlingTopLevelDeclRAII(CodeGeneratorImpl &Self) : Self(Self) {
41 ++Self.HandlingTopLevelDecls;
43 ~HandlingTopLevelDeclRAII() {
44 if (--Self.HandlingTopLevelDecls == 0)
45 Self.EmitDeferredDecls();
49 CoverageSourceInfo *CoverageInfo;
52 std::unique_ptr<llvm::Module> M;
53 std::unique_ptr<CodeGen::CodeGenModule> Builder;
56 CodeGeneratorImpl(DiagnosticsEngine &diags, const std::string& ModuleName,
57 const CodeGenOptions &CGO, llvm::LLVMContext& C,
58 CoverageSourceInfo *CoverageInfo = nullptr)
59 : Diags(diags), CodeGenOpts(CGO), HandlingTopLevelDecls(0),
60 CoverageInfo(CoverageInfo),
61 M(new llvm::Module(ModuleName, C)) {}
63 virtual ~CodeGeneratorImpl() {}
65 llvm::Module* GetModule() override {
69 const Decl *GetDeclForMangledName(StringRef MangledName) override {
71 if (!Builder->lookupRepresentativeDecl(MangledName, Result))
73 const Decl *D = Result.getCanonicalDecl().getDecl();
74 if (auto FD = dyn_cast<FunctionDecl>(D)) {
77 } else if (auto TD = dyn_cast<TagDecl>(D)) {
78 if (auto Def = TD->getDefinition())
84 llvm::Module *ReleaseModule() override { return M.release(); }
86 void Initialize(ASTContext &Context) override {
89 M->setTargetTriple(Ctx->getTargetInfo().getTriple().getTriple());
90 M->setDataLayout(Ctx->getTargetInfo().getTargetDescription());
91 TD.reset(new llvm::DataLayout(Ctx->getTargetInfo().getTargetDescription()));
92 Builder.reset(new CodeGen::CodeGenModule(Context, CodeGenOpts, *M, *TD,
93 Diags, CoverageInfo));
95 for (size_t i = 0, e = CodeGenOpts.DependentLibraries.size(); i < e; ++i)
96 HandleDependentLibrary(CodeGenOpts.DependentLibraries[i]);
99 void HandleCXXStaticMemberVarInstantiation(VarDecl *VD) override {
100 if (Diags.hasErrorOccurred())
103 Builder->HandleCXXStaticMemberVarInstantiation(VD);
106 bool HandleTopLevelDecl(DeclGroupRef DG) override {
107 if (Diags.hasErrorOccurred())
110 HandlingTopLevelDeclRAII HandlingDecl(*this);
112 // Make sure to emit all elements of a Decl.
113 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
114 Builder->EmitTopLevelDecl(*I);
119 void EmitDeferredDecls() {
120 // Emit any deferred inline method definitions. Note that more deferred
121 // methods may be added during this loop, since ASTConsumer callbacks
122 // can be invoked if AST inspection results in declarations being added.
123 for (unsigned I = 0; I < DeferredInlineMethodDefinitions.size(); ++I)
124 Builder->EmitTopLevelDecl(DeferredInlineMethodDefinitions[I]);
125 DeferredInlineMethodDefinitions.clear();
128 void HandleInlineMethodDefinition(CXXMethodDecl *D) override {
129 if (Diags.hasErrorOccurred())
132 assert(D->doesThisDeclarationHaveABody());
134 // We may want to emit this definition. However, that decision might be
135 // based on computing the linkage, and we have to defer that in case we
136 // are inside of something that will change the method's final linkage,
140 // void foo() { bar(); }
142 DeferredInlineMethodDefinitions.push_back(D);
144 // Always provide some coverage mapping
145 // even for the methods that aren't emitted.
146 Builder->AddDeferredUnusedCoverageMapping(D);
149 /// HandleTagDeclDefinition - This callback is invoked each time a TagDecl
150 /// to (e.g. struct, union, enum, class) is completed. This allows the
151 /// client hack on the type, which can occur at any point in the file
152 /// (because these can be defined in declspecs).
153 void HandleTagDeclDefinition(TagDecl *D) override {
154 if (Diags.hasErrorOccurred())
157 Builder->UpdateCompletedType(D);
159 // For MSVC compatibility, treat declarations of static data members with
160 // inline initializers as definitions.
161 if (Ctx->getLangOpts().MSVCCompat) {
162 for (Decl *Member : D->decls()) {
163 if (VarDecl *VD = dyn_cast<VarDecl>(Member)) {
164 if (Ctx->isMSStaticDataMemberInlineDefinition(VD) &&
165 Ctx->DeclMustBeEmitted(VD)) {
166 Builder->EmitGlobal(VD);
173 void HandleTagDeclRequiredDefinition(const TagDecl *D) override {
174 if (Diags.hasErrorOccurred())
177 if (CodeGen::CGDebugInfo *DI = Builder->getModuleDebugInfo())
178 if (const RecordDecl *RD = dyn_cast<RecordDecl>(D))
179 DI->completeRequiredType(RD);
182 void HandleTranslationUnit(ASTContext &Ctx) override {
183 if (Diags.hasErrorOccurred()) {
194 void CompleteTentativeDefinition(VarDecl *D) override {
195 if (Diags.hasErrorOccurred())
198 Builder->EmitTentativeDefinition(D);
201 void HandleVTable(CXXRecordDecl *RD, bool DefinitionRequired) override {
202 if (Diags.hasErrorOccurred())
205 Builder->EmitVTable(RD, DefinitionRequired);
208 void HandleLinkerOptionPragma(llvm::StringRef Opts) override {
209 Builder->AppendLinkerOptions(Opts);
212 void HandleDetectMismatch(llvm::StringRef Name,
213 llvm::StringRef Value) override {
214 Builder->AddDetectMismatch(Name, Value);
217 void HandleDependentLibrary(llvm::StringRef Lib) override {
218 Builder->AddDependentLib(Lib);
222 std::vector<CXXMethodDecl *> DeferredInlineMethodDefinitions;
226 void CodeGenerator::anchor() { }
228 CodeGenerator *clang::CreateLLVMCodeGen(DiagnosticsEngine &Diags,
229 const std::string& ModuleName,
230 const CodeGenOptions &CGO,
231 const TargetOptions &/*TO*/,
232 llvm::LLVMContext& C,
233 CoverageSourceInfo *CoverageInfo) {
234 return new CodeGeneratorImpl(Diags, ModuleName, CGO, C, CoverageInfo);