blob: ee6f6f94c715816062568672bb316baac7dca520 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- ModuleBuilder.cpp - Emit LLVM Code from ASTs ---------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This builds an AST and converts it to LLVM Code.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/CodeGen/ModuleBuilder.h"
David Blaikie658cd2c2013-07-13 21:08:14 +000015#include "CGDebugInfo.h"
Stephen Hines651f13c2014-04-23 16:59:28 -070016#include "CodeGenModule.h"
Chris Lattner8ee3c032008-02-06 02:01:47 +000017#include "clang/AST/ASTContext.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000018#include "clang/AST/DeclObjC.h"
19#include "clang/AST/Expr.h"
Chris Lattner8ee3c032008-02-06 02:01:47 +000020#include "clang/Basic/Diagnostic.h"
21#include "clang/Basic/TargetInfo.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000022#include "clang/Frontend/CodeGenOptions.h"
Reid Kleckner3190ca92013-05-08 13:44:39 +000023#include "llvm/ADT/StringRef.h"
Chandler Carruth3b844ba2013-01-02 11:45:17 +000024#include "llvm/IR/DataLayout.h"
25#include "llvm/IR/LLVMContext.h"
26#include "llvm/IR/Module.h"
Stephen Hines651f13c2014-04-23 16:59:28 -070027#include <memory>
Chris Lattnerbd360642009-03-26 05:00:52 +000028using namespace clang;
Ted Kremenek815c78f2008-08-05 18:50:11 +000029
Chris Lattner8ee3c032008-02-06 02:01:47 +000030namespace {
Benjamin Kramer85b45212009-11-28 19:45:26 +000031 class CodeGeneratorImpl : public CodeGenerator {
David Blaikied6471f72011-09-25 23:23:43 +000032 DiagnosticsEngine &Diags;
Stephen Hines651f13c2014-04-23 16:59:28 -070033 std::unique_ptr<const llvm::DataLayout> TD;
Chris Lattner8ee3c032008-02-06 02:01:47 +000034 ASTContext *Ctx;
Chandler Carruth2811ccf2009-11-12 17:24:48 +000035 const CodeGenOptions CodeGenOpts; // Intentionally copied in.
Stephen Hines176edba2014-12-01 14:53:08 -080036
37 unsigned HandlingTopLevelDecls;
38 struct HandlingTopLevelDeclRAII {
39 CodeGeneratorImpl &Self;
40 HandlingTopLevelDeclRAII(CodeGeneratorImpl &Self) : Self(Self) {
41 ++Self.HandlingTopLevelDecls;
42 }
43 ~HandlingTopLevelDeclRAII() {
44 if (--Self.HandlingTopLevelDecls == 0)
45 Self.EmitDeferredDecls();
46 }
47 };
48
49 CoverageSourceInfo *CoverageInfo;
50
Chris Lattner8ee3c032008-02-06 02:01:47 +000051 protected:
Stephen Hines651f13c2014-04-23 16:59:28 -070052 std::unique_ptr<llvm::Module> M;
53 std::unique_ptr<CodeGen::CodeGenModule> Builder;
54
Chris Lattner8ee3c032008-02-06 02:01:47 +000055 public:
David Blaikied6471f72011-09-25 23:23:43 +000056 CodeGeneratorImpl(DiagnosticsEngine &diags, const std::string& ModuleName,
Stephen Hines176edba2014-12-01 14:53:08 -080057 const CodeGenOptions &CGO, llvm::LLVMContext& C,
58 CoverageSourceInfo *CoverageInfo = nullptr)
59 : Diags(diags), CodeGenOpts(CGO), HandlingTopLevelDecls(0),
60 CoverageInfo(CoverageInfo),
Bill Wendlinge1092df2013-02-14 08:09:20 +000061 M(new llvm::Module(ModuleName, C)) {}
Mike Stump1eb44332009-09-09 15:08:12 +000062
Ted Kremenek815c78f2008-08-05 18:50:11 +000063 virtual ~CodeGeneratorImpl() {}
Mike Stump1eb44332009-09-09 15:08:12 +000064
Stephen Hines651f13c2014-04-23 16:59:28 -070065 llvm::Module* GetModule() override {
Daniel Dunbard8c0ea12008-10-21 19:55:09 +000066 return M.get();
67 }
Mike Stump1eb44332009-09-09 15:08:12 +000068
Stephen Hinesc568f1e2014-07-21 00:47:37 -070069 const Decl *GetDeclForMangledName(StringRef MangledName) override {
70 GlobalDecl Result;
71 if (!Builder->lookupRepresentativeDecl(MangledName, Result))
72 return nullptr;
73 const Decl *D = Result.getCanonicalDecl().getDecl();
74 if (auto FD = dyn_cast<FunctionDecl>(D)) {
75 if (FD->hasBody(FD))
76 return FD;
77 } else if (auto TD = dyn_cast<TagDecl>(D)) {
78 if (auto Def = TD->getDefinition())
79 return Def;
80 }
81 return D;
82 }
83
Stephen Hines651f13c2014-04-23 16:59:28 -070084 llvm::Module *ReleaseModule() override { return M.release(); }
Mike Stump1eb44332009-09-09 15:08:12 +000085
Stephen Hines651f13c2014-04-23 16:59:28 -070086 void Initialize(ASTContext &Context) override {
Chris Lattner8ee3c032008-02-06 02:01:47 +000087 Ctx = &Context;
Mike Stump1eb44332009-09-09 15:08:12 +000088
Douglas Gregorbcfd1f52011-09-02 00:18:52 +000089 M->setTargetTriple(Ctx->getTargetInfo().getTriple().getTriple());
90 M->setDataLayout(Ctx->getTargetInfo().getTargetDescription());
Micah Villmow25a6a842012-10-08 16:25:52 +000091 TD.reset(new llvm::DataLayout(Ctx->getTargetInfo().getTargetDescription()));
John McCall3abae092013-04-16 22:48:20 +000092 Builder.reset(new CodeGen::CodeGenModule(Context, CodeGenOpts, *M, *TD,
Stephen Hines176edba2014-12-01 14:53:08 -080093 Diags, CoverageInfo));
Hans Wennborgb3574792013-08-08 00:17:41 +000094
95 for (size_t i = 0, e = CodeGenOpts.DependentLibraries.size(); i < e; ++i)
96 HandleDependentLibrary(CodeGenOpts.DependentLibraries[i]);
Chris Lattner8ee3c032008-02-06 02:01:47 +000097 }
Mike Stump1eb44332009-09-09 15:08:12 +000098
Stephen Hines651f13c2014-04-23 16:59:28 -070099 void HandleCXXStaticMemberVarInstantiation(VarDecl *VD) override {
David Blaikie0a1c8622013-08-19 21:02:26 +0000100 if (Diags.hasErrorOccurred())
101 return;
102
Rafael Espindola02503932012-03-08 15:51:03 +0000103 Builder->HandleCXXStaticMemberVarInstantiation(VD);
Rafael Espindola234fe652012-03-05 10:54:55 +0000104 }
105
Stephen Hines651f13c2014-04-23 16:59:28 -0700106 bool HandleTopLevelDecl(DeclGroupRef DG) override {
David Blaikie0a1c8622013-08-19 21:02:26 +0000107 if (Diags.hasErrorOccurred())
108 return true;
109
Stephen Hines176edba2014-12-01 14:53:08 -0800110 HandlingTopLevelDeclRAII HandlingDecl(*this);
111
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000112 // Make sure to emit all elements of a Decl.
Chris Lattner682bf922009-03-29 16:50:03 +0000113 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
114 Builder->EmitTopLevelDecl(*I);
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700115
Argyrios Kyrtzidis88c25962011-11-18 00:26:59 +0000116 return true;
Chris Lattner8ee3c032008-02-06 02:01:47 +0000117 }
Daniel Dunbar41071de2008-08-15 23:26:23 +0000118
Stephen Hines176edba2014-12-01 14:53:08 -0800119 void EmitDeferredDecls() {
120 if (DeferredInlineMethodDefinitions.empty())
121 return;
122
123 // Emit any deferred inline method definitions. Note that more deferred
124 // methods may be added during this loop, since ASTConsumer callbacks
125 // can be invoked if AST inspection results in declarations being added.
126 HandlingTopLevelDeclRAII HandlingDecl(*this);
127 for (unsigned I = 0; I != DeferredInlineMethodDefinitions.size(); ++I)
128 Builder->EmitTopLevelDecl(DeferredInlineMethodDefinitions[I]);
129 DeferredInlineMethodDefinitions.clear();
130 }
131
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700132 void HandleInlineMethodDefinition(CXXMethodDecl *D) override {
133 if (Diags.hasErrorOccurred())
134 return;
135
136 assert(D->doesThisDeclarationHaveABody());
137
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700138 // We may want to emit this definition. However, that decision might be
139 // based on computing the linkage, and we have to defer that in case we
140 // are inside of something that will change the method's final linkage,
141 // e.g.
142 // typedef struct {
143 // void bar();
144 // void foo() { bar(); }
145 // } A;
146 DeferredInlineMethodDefinitions.push_back(D);
Stephen Hines176edba2014-12-01 14:53:08 -0800147
148 // Provide some coverage mapping even for methods that aren't emitted.
149 // Don't do this for templated classes though, as they may not be
150 // instantiable.
151 if (!D->getParent()->getDescribedClassTemplate())
152 Builder->AddDeferredUnusedCoverageMapping(D);
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700153 }
154
Chris Lattner9eee0f82008-02-06 04:51:19 +0000155 /// HandleTagDeclDefinition - This callback is invoked each time a TagDecl
Chris Lattner682bf922009-03-29 16:50:03 +0000156 /// to (e.g. struct, union, enum, class) is completed. This allows the
157 /// client hack on the type, which can occur at any point in the file
158 /// (because these can be defined in declspecs).
Stephen Hines651f13c2014-04-23 16:59:28 -0700159 void HandleTagDeclDefinition(TagDecl *D) override {
David Blaikie0a1c8622013-08-19 21:02:26 +0000160 if (Diags.hasErrorOccurred())
161 return;
162
Chris Lattnerc5b88062008-02-06 05:08:19 +0000163 Builder->UpdateCompletedType(D);
Stephen Hines176edba2014-12-01 14:53:08 -0800164
165 // For MSVC compatibility, treat declarations of static data members with
166 // inline initializers as definitions.
167 if (Ctx->getLangOpts().MSVCCompat) {
168 for (Decl *Member : D->decls()) {
169 if (VarDecl *VD = dyn_cast<VarDecl>(Member)) {
170 if (Ctx->isMSStaticDataMemberInlineDefinition(VD) &&
171 Ctx->DeclMustBeEmitted(VD)) {
172 Builder->EmitGlobal(VD);
173 }
174 }
175 }
176 }
Chris Lattner9eee0f82008-02-06 04:51:19 +0000177 }
Ted Kremenek159346a2008-08-07 19:47:41 +0000178
Stephen Hines651f13c2014-04-23 16:59:28 -0700179 void HandleTagDeclRequiredDefinition(const TagDecl *D) override {
David Blaikie0a1c8622013-08-19 21:02:26 +0000180 if (Diags.hasErrorOccurred())
181 return;
182
David Blaikie658cd2c2013-07-13 21:08:14 +0000183 if (CodeGen::CGDebugInfo *DI = Builder->getModuleDebugInfo())
184 if (const RecordDecl *RD = dyn_cast<RecordDecl>(D))
David Blaikie27804892013-08-15 20:49:17 +0000185 DI->completeRequiredType(RD);
David Blaikie658cd2c2013-07-13 21:08:14 +0000186 }
187
Stephen Hines651f13c2014-04-23 16:59:28 -0700188 void HandleTranslationUnit(ASTContext &Ctx) override {
Ted Kremenek159346a2008-08-07 19:47:41 +0000189 if (Diags.hasErrorOccurred()) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700190 if (Builder)
191 Builder->clear();
Ted Kremenek159346a2008-08-07 19:47:41 +0000192 M.reset();
193 return;
194 }
195
196 if (Builder)
197 Builder->Release();
Daniel Dunbar7177dee2009-12-19 17:50:07 +0000198 }
Douglas Gregorb6c8c8b2009-04-21 17:11:58 +0000199
Stephen Hines651f13c2014-04-23 16:59:28 -0700200 void CompleteTentativeDefinition(VarDecl *D) override {
Douglas Gregorb6c8c8b2009-04-21 17:11:58 +0000201 if (Diags.hasErrorOccurred())
202 return;
203
204 Builder->EmitTentativeDefinition(D);
205 }
Douglas Gregor6fb745b2010-05-13 16:44:06 +0000206
Stephen Hines651f13c2014-04-23 16:59:28 -0700207 void HandleVTable(CXXRecordDecl *RD, bool DefinitionRequired) override {
Douglas Gregor6fb745b2010-05-13 16:44:06 +0000208 if (Diags.hasErrorOccurred())
209 return;
210
211 Builder->EmitVTable(RD, DefinitionRequired);
212 }
Reid Kleckner3190ca92013-05-08 13:44:39 +0000213
Stephen Hines651f13c2014-04-23 16:59:28 -0700214 void HandleLinkerOptionPragma(llvm::StringRef Opts) override {
Reid Kleckner3190ca92013-05-08 13:44:39 +0000215 Builder->AppendLinkerOptions(Opts);
216 }
217
Stephen Hines651f13c2014-04-23 16:59:28 -0700218 void HandleDetectMismatch(llvm::StringRef Name,
219 llvm::StringRef Value) override {
Aaron Ballmana7ff62f2013-06-04 02:07:14 +0000220 Builder->AddDetectMismatch(Name, Value);
221 }
222
Stephen Hines651f13c2014-04-23 16:59:28 -0700223 void HandleDependentLibrary(llvm::StringRef Lib) override {
Reid Kleckner3190ca92013-05-08 13:44:39 +0000224 Builder->AddDependentLib(Lib);
225 }
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700226
227 private:
228 std::vector<CXXMethodDecl *> DeferredInlineMethodDefinitions;
Chris Lattner8ee3c032008-02-06 02:01:47 +0000229 };
Reid Spencer5f016e22007-07-11 17:01:13 +0000230}
231
David Blaikie99ba9e32011-12-20 02:48:34 +0000232void CodeGenerator::anchor() { }
233
David Blaikied6471f72011-09-25 23:23:43 +0000234CodeGenerator *clang::CreateLLVMCodeGen(DiagnosticsEngine &Diags,
Ted Kremenek815c78f2008-08-05 18:50:11 +0000235 const std::string& ModuleName,
Chandler Carruth2811ccf2009-11-12 17:24:48 +0000236 const CodeGenOptions &CGO,
John McCall3abae092013-04-16 22:48:20 +0000237 const TargetOptions &/*TO*/,
Stephen Hines176edba2014-12-01 14:53:08 -0800238 llvm::LLVMContext& C,
239 CoverageSourceInfo *CoverageInfo) {
240 return new CodeGeneratorImpl(Diags, ModuleName, CGO, C, CoverageInfo);
Reid Spencer5f016e22007-07-11 17:01:13 +0000241}