blob: 234a7736065b4dfbbf8e607a3c05e67ee47c4cc3 [file] [log] [blame]
Chris Lattnerf97fe382007-05-24 06:29:05 +00001//===--- ModuleBuilder.cpp - Emit LLVM Code from ASTs ---------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerf97fe382007-05-24 06:29:05 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This builds an AST and converts it to LLVM Code.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/CodeGen/ModuleBuilder.h"
David Blaikie48ad6dc2013-07-13 21:08:14 +000015#include "CGDebugInfo.h"
Chandler Carruth5553d0d2014-01-07 11:51:46 +000016#include "CodeGenModule.h"
Chris Lattneradf1f512008-02-06 02:01:47 +000017#include "clang/AST/ASTContext.h"
Daniel Dunbar221fa942008-08-11 04:54:23 +000018#include "clang/AST/DeclObjC.h"
19#include "clang/AST/Expr.h"
Chris Lattneradf1f512008-02-06 02:01:47 +000020#include "clang/Basic/Diagnostic.h"
21#include "clang/Basic/TargetInfo.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000022#include "clang/Frontend/CodeGenOptions.h"
Reid Klecknere43f0fe2013-05-08 13:44:39 +000023#include "llvm/ADT/StringRef.h"
Chandler Carruthffd55512013-01-02 11:45:17 +000024#include "llvm/IR/DataLayout.h"
25#include "llvm/IR/LLVMContext.h"
26#include "llvm/IR/Module.h"
Ahmed Charlesdfca6f92014-03-09 11:36:40 +000027#include <memory>
Chris Lattner984fac52009-03-26 05:00:52 +000028using namespace clang;
Ted Kremenek2c674f62008-08-05 18:50:11 +000029
Chris Lattneradf1f512008-02-06 02:01:47 +000030namespace {
Benjamin Kramer337e3a52009-11-28 19:45:26 +000031 class CodeGeneratorImpl : public CodeGenerator {
David Blaikie9c902b52011-09-25 23:23:43 +000032 DiagnosticsEngine &Diags;
Chris Lattneradf1f512008-02-06 02:01:47 +000033 ASTContext *Ctx;
Adrian Prantle74f5252015-06-30 02:26:03 +000034 const HeaderSearchOptions &HeaderSearchOpts; // Only used for debug info.
35 const PreprocessorOptions &PreprocessorOpts; // Only used for debug info.
Chandler Carruthbc55fe22009-11-12 17:24:48 +000036 const CodeGenOptions CodeGenOpts; // Intentionally copied in.
Richard Smith7f5755c2014-08-01 22:42:16 +000037
38 unsigned HandlingTopLevelDecls;
39 struct HandlingTopLevelDeclRAII {
40 CodeGeneratorImpl &Self;
41 HandlingTopLevelDeclRAII(CodeGeneratorImpl &Self) : Self(Self) {
42 ++Self.HandlingTopLevelDecls;
43 }
44 ~HandlingTopLevelDeclRAII() {
45 if (--Self.HandlingTopLevelDecls == 0)
46 Self.EmitDeferredDecls();
47 }
48 };
49
Alex Lorenzee024992014-08-04 18:41:51 +000050 CoverageSourceInfo *CoverageInfo;
51
Chris Lattneradf1f512008-02-06 02:01:47 +000052 protected:
Ahmed Charlesb8984322014-03-07 20:03:18 +000053 std::unique_ptr<llvm::Module> M;
54 std::unique_ptr<CodeGen::CodeGenModule> Builder;
55
Hans Wennborg2b0d0142014-12-18 19:19:00 +000056 private:
57 SmallVector<CXXMethodDecl *, 8> DeferredInlineMethodDefinitions;
58
Chris Lattneradf1f512008-02-06 02:01:47 +000059 public:
Adrian Prantle74f5252015-06-30 02:26:03 +000060 CodeGeneratorImpl(DiagnosticsEngine &diags, const std::string &ModuleName,
61 const HeaderSearchOptions &HSO,
62 const PreprocessorOptions &PPO, const CodeGenOptions &CGO,
63 llvm::LLVMContext &C,
Alex Lorenzee024992014-08-04 18:41:51 +000064 CoverageSourceInfo *CoverageInfo = nullptr)
Adrian Prantle74f5252015-06-30 02:26:03 +000065 : Diags(diags), Ctx(nullptr), HeaderSearchOpts(HSO),
66 PreprocessorOpts(PPO), CodeGenOpts(CGO), HandlingTopLevelDecls(0),
67 CoverageInfo(CoverageInfo),
68 M(new llvm::Module(ModuleName, C)) {}
Mike Stump11289f42009-09-09 15:08:12 +000069
Alexander Kornienko34eb2072015-04-11 02:00:23 +000070 ~CodeGeneratorImpl() override {
Hans Wennborg0c0a8c82014-12-19 23:35:11 +000071 // There should normally not be any leftover inline method definitions.
72 assert(DeferredInlineMethodDefinitions.empty() ||
73 Diags.hasErrorOccurred());
Hans Wennborg2b0d0142014-12-18 19:19:00 +000074 }
Mike Stump11289f42009-09-09 15:08:12 +000075
Craig Topper4f12f102014-03-12 06:41:41 +000076 llvm::Module* GetModule() override {
Daniel Dunbar30c514e2008-10-21 19:55:09 +000077 return M.get();
78 }
Mike Stump11289f42009-09-09 15:08:12 +000079
Alp Tokerfb8d02b2014-06-05 22:10:59 +000080 const Decl *GetDeclForMangledName(StringRef MangledName) override {
81 GlobalDecl Result;
82 if (!Builder->lookupRepresentativeDecl(MangledName, Result))
83 return nullptr;
84 const Decl *D = Result.getCanonicalDecl().getDecl();
85 if (auto FD = dyn_cast<FunctionDecl>(D)) {
86 if (FD->hasBody(FD))
87 return FD;
88 } else if (auto TD = dyn_cast<TagDecl>(D)) {
89 if (auto Def = TD->getDefinition())
90 return Def;
91 }
92 return D;
93 }
94
Craig Topper4f12f102014-03-12 06:41:41 +000095 llvm::Module *ReleaseModule() override { return M.release(); }
Mike Stump11289f42009-09-09 15:08:12 +000096
Craig Topper4f12f102014-03-12 06:41:41 +000097 void Initialize(ASTContext &Context) override {
Chris Lattneradf1f512008-02-06 02:01:47 +000098 Ctx = &Context;
Mike Stump11289f42009-09-09 15:08:12 +000099
Douglas Gregore8bbc122011-09-02 00:18:52 +0000100 M->setTargetTriple(Ctx->getTargetInfo().getTriple().getTriple());
Eric Christopher964a5f32015-08-05 23:48:05 +0000101 M->setDataLayout(Ctx->getTargetInfo().getDataLayoutString());
Mehdi Aminica3cf9e2015-07-24 16:04:29 +0000102 Builder.reset(new CodeGen::CodeGenModule(Context, HeaderSearchOpts,
103 PreprocessorOpts, CodeGenOpts,
104 *M, Diags, CoverageInfo));
Hans Wennborg75958c42013-08-08 00:17:41 +0000105
Peter Collingbournedc134532016-01-16 00:31:22 +0000106 for (auto &&Lib : CodeGenOpts.DependentLibraries)
107 HandleDependentLibrary(Lib);
108 for (auto &&Opt : CodeGenOpts.LinkerOptions)
109 HandleLinkerOption(Opt);
Chris Lattneradf1f512008-02-06 02:01:47 +0000110 }
Mike Stump11289f42009-09-09 15:08:12 +0000111
Craig Topper4f12f102014-03-12 06:41:41 +0000112 void HandleCXXStaticMemberVarInstantiation(VarDecl *VD) override {
David Blaikie4a9ec7b2013-08-19 21:02:26 +0000113 if (Diags.hasErrorOccurred())
114 return;
115
Rafael Espindoladf88f6f2012-03-08 15:51:03 +0000116 Builder->HandleCXXStaticMemberVarInstantiation(VD);
Rafael Espindola189fa742012-03-05 10:54:55 +0000117 }
118
Craig Topper4f12f102014-03-12 06:41:41 +0000119 bool HandleTopLevelDecl(DeclGroupRef DG) override {
David Blaikie4a9ec7b2013-08-19 21:02:26 +0000120 if (Diags.hasErrorOccurred())
121 return true;
122
Richard Smith7f5755c2014-08-01 22:42:16 +0000123 HandlingTopLevelDeclRAII HandlingDecl(*this);
124
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000125 // Make sure to emit all elements of a Decl.
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000126 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
127 Builder->EmitTopLevelDecl(*I);
Hans Wennborgdfcb7d62014-06-06 17:36:17 +0000128
Richard Smith7f5755c2014-08-01 22:42:16 +0000129 return true;
130 }
131
132 void EmitDeferredDecls() {
Richard Smithc9cbde72014-08-13 21:15:09 +0000133 if (DeferredInlineMethodDefinitions.empty())
134 return;
135
Richard Smith1ba0a072014-08-01 20:39:36 +0000136 // Emit any deferred inline method definitions. Note that more deferred
137 // methods may be added during this loop, since ASTConsumer callbacks
138 // can be invoked if AST inspection results in declarations being added.
Richard Smithc9cbde72014-08-13 21:15:09 +0000139 HandlingTopLevelDeclRAII HandlingDecl(*this);
140 for (unsigned I = 0; I != DeferredInlineMethodDefinitions.size(); ++I)
Richard Smith1ba0a072014-08-01 20:39:36 +0000141 Builder->EmitTopLevelDecl(DeferredInlineMethodDefinitions[I]);
Richard Smith455768e2014-08-01 20:09:39 +0000142 DeferredInlineMethodDefinitions.clear();
Chris Lattneradf1f512008-02-06 02:01:47 +0000143 }
Daniel Dunbarfce4be82008-08-15 23:26:23 +0000144
Hans Wennborga926d842014-05-23 20:37:38 +0000145 void HandleInlineMethodDefinition(CXXMethodDecl *D) override {
146 if (Diags.hasErrorOccurred())
147 return;
148
149 assert(D->doesThisDeclarationHaveABody());
150
Hans Wennborgdfcb7d62014-06-06 17:36:17 +0000151 // We may want to emit this definition. However, that decision might be
152 // based on computing the linkage, and we have to defer that in case we
153 // are inside of something that will change the method's final linkage,
154 // e.g.
155 // typedef struct {
156 // void bar();
157 // void foo() { bar(); }
158 // } A;
159 DeferredInlineMethodDefinitions.push_back(D);
Alex Lorenzee024992014-08-04 18:41:51 +0000160
Justin Bogner94d384e2014-11-18 00:34:46 +0000161 // Provide some coverage mapping even for methods that aren't emitted.
162 // Don't do this for templated classes though, as they may not be
163 // instantiable.
164 if (!D->getParent()->getDescribedClassTemplate())
165 Builder->AddDeferredUnusedCoverageMapping(D);
Hans Wennborga926d842014-05-23 20:37:38 +0000166 }
167
Chris Lattnera5e4d302008-02-06 04:51:19 +0000168 /// HandleTagDeclDefinition - This callback is invoked each time a TagDecl
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000169 /// to (e.g. struct, union, enum, class) is completed. This allows the
170 /// client hack on the type, which can occur at any point in the file
171 /// (because these can be defined in declspecs).
Craig Topper4f12f102014-03-12 06:41:41 +0000172 void HandleTagDeclDefinition(TagDecl *D) override {
David Blaikie4a9ec7b2013-08-19 21:02:26 +0000173 if (Diags.hasErrorOccurred())
174 return;
175
Chris Lattner68be6062008-02-06 05:08:19 +0000176 Builder->UpdateCompletedType(D);
Hans Wennborg56fc62b2014-07-17 20:25:23 +0000177
178 // For MSVC compatibility, treat declarations of static data members with
179 // inline initializers as definitions.
David Majnemer3f021502015-10-08 04:53:31 +0000180 if (Ctx->getTargetInfo().getCXXABI().isMicrosoft()) {
Hans Wennborg56fc62b2014-07-17 20:25:23 +0000181 for (Decl *Member : D->decls()) {
182 if (VarDecl *VD = dyn_cast<VarDecl>(Member)) {
183 if (Ctx->isMSStaticDataMemberInlineDefinition(VD) &&
184 Ctx->DeclMustBeEmitted(VD)) {
185 Builder->EmitGlobal(VD);
186 }
187 }
188 }
189 }
Chris Lattnera5e4d302008-02-06 04:51:19 +0000190 }
Ted Kremenek7db4f602008-08-07 19:47:41 +0000191
Craig Topper4f12f102014-03-12 06:41:41 +0000192 void HandleTagDeclRequiredDefinition(const TagDecl *D) override {
David Blaikie4a9ec7b2013-08-19 21:02:26 +0000193 if (Diags.hasErrorOccurred())
194 return;
195
David Blaikie48ad6dc2013-07-13 21:08:14 +0000196 if (CodeGen::CGDebugInfo *DI = Builder->getModuleDebugInfo())
197 if (const RecordDecl *RD = dyn_cast<RecordDecl>(D))
David Blaikieb2e86eb2013-08-15 20:49:17 +0000198 DI->completeRequiredType(RD);
David Blaikie48ad6dc2013-07-13 21:08:14 +0000199 }
200
Craig Topper4f12f102014-03-12 06:41:41 +0000201 void HandleTranslationUnit(ASTContext &Ctx) override {
Manman Ren581c2b92016-01-28 23:29:02 +0000202 // Release the Builder when there is no error.
203 if (!Diags.hasErrorOccurred() && Builder)
204 Builder->Release();
205
206 // If there are errors before or when releasing the Builder, reset
207 // the module to stop here before invoking the backend.
Ted Kremenek7db4f602008-08-07 19:47:41 +0000208 if (Diags.hasErrorOccurred()) {
Rafael Espindolac0ff7442013-12-09 14:59:08 +0000209 if (Builder)
210 Builder->clear();
Ted Kremenek7db4f602008-08-07 19:47:41 +0000211 M.reset();
212 return;
213 }
Daniel Dunbare017ecc2009-12-19 17:50:07 +0000214 }
Douglas Gregorbeecd582009-04-21 17:11:58 +0000215
David Majnemer929025d2016-01-26 19:30:26 +0000216 void AssignInheritanceModel(CXXRecordDecl *RD) override {
217 if (Diags.hasErrorOccurred())
218 return;
219
220 Builder->RefreshTypeCacheForClass(RD);
221 }
222
Craig Topper4f12f102014-03-12 06:41:41 +0000223 void CompleteTentativeDefinition(VarDecl *D) override {
Douglas Gregorbeecd582009-04-21 17:11:58 +0000224 if (Diags.hasErrorOccurred())
225 return;
226
227 Builder->EmitTentativeDefinition(D);
228 }
Douglas Gregor88d292c2010-05-13 16:44:06 +0000229
Nico Weberb6a5d052015-01-15 04:07:35 +0000230 void HandleVTable(CXXRecordDecl *RD) override {
Douglas Gregor88d292c2010-05-13 16:44:06 +0000231 if (Diags.hasErrorOccurred())
232 return;
233
Nico Weberb6a5d052015-01-15 04:07:35 +0000234 Builder->EmitVTable(RD);
Douglas Gregor88d292c2010-05-13 16:44:06 +0000235 }
Reid Klecknere43f0fe2013-05-08 13:44:39 +0000236
Peter Collingbournedc134532016-01-16 00:31:22 +0000237 void HandleLinkerOption(llvm::StringRef Opts) override {
Reid Klecknere43f0fe2013-05-08 13:44:39 +0000238 Builder->AppendLinkerOptions(Opts);
239 }
240
Craig Topper4f12f102014-03-12 06:41:41 +0000241 void HandleDetectMismatch(llvm::StringRef Name,
242 llvm::StringRef Value) override {
Aaron Ballman5d041be2013-06-04 02:07:14 +0000243 Builder->AddDetectMismatch(Name, Value);
244 }
245
Craig Topper4f12f102014-03-12 06:41:41 +0000246 void HandleDependentLibrary(llvm::StringRef Lib) override {
Reid Klecknere43f0fe2013-05-08 13:44:39 +0000247 Builder->AddDependentLib(Lib);
248 }
Chris Lattneradf1f512008-02-06 02:01:47 +0000249 };
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000250}
Chris Lattnerf97fe382007-05-24 06:29:05 +0000251
David Blaikie68e081d2011-12-20 02:48:34 +0000252void CodeGenerator::anchor() { }
253
Adrian Prantle74f5252015-06-30 02:26:03 +0000254CodeGenerator *clang::CreateLLVMCodeGen(
255 DiagnosticsEngine &Diags, const std::string &ModuleName,
256 const HeaderSearchOptions &HeaderSearchOpts,
257 const PreprocessorOptions &PreprocessorOpts, const CodeGenOptions &CGO,
258 llvm::LLVMContext &C, CoverageSourceInfo *CoverageInfo) {
259 return new CodeGeneratorImpl(Diags, ModuleName, HeaderSearchOpts,
260 PreprocessorOpts, CGO, C, CoverageInfo);
Chris Lattnerf97fe382007-05-24 06:29:05 +0000261}