blob: bd59332a274a0c83d4ce1edb9321a1ee8b2652fb [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),
Mehdi Amini557c20a2016-03-13 21:05:23 +000067 CoverageInfo(CoverageInfo), M(new llvm::Module(ModuleName, C)) {
68 C.setDiscardValueNames(CGO.DiscardValueNames);
69 }
Mike Stump11289f42009-09-09 15:08:12 +000070
Alexander Kornienko34eb2072015-04-11 02:00:23 +000071 ~CodeGeneratorImpl() override {
Hans Wennborg0c0a8c82014-12-19 23:35:11 +000072 // There should normally not be any leftover inline method definitions.
73 assert(DeferredInlineMethodDefinitions.empty() ||
74 Diags.hasErrorOccurred());
Hans Wennborg2b0d0142014-12-18 19:19:00 +000075 }
Mike Stump11289f42009-09-09 15:08:12 +000076
Craig Topper4f12f102014-03-12 06:41:41 +000077 llvm::Module* GetModule() override {
Daniel Dunbar30c514e2008-10-21 19:55:09 +000078 return M.get();
79 }
Mike Stump11289f42009-09-09 15:08:12 +000080
Alp Tokerfb8d02b2014-06-05 22:10:59 +000081 const Decl *GetDeclForMangledName(StringRef MangledName) override {
82 GlobalDecl Result;
83 if (!Builder->lookupRepresentativeDecl(MangledName, Result))
84 return nullptr;
85 const Decl *D = Result.getCanonicalDecl().getDecl();
86 if (auto FD = dyn_cast<FunctionDecl>(D)) {
87 if (FD->hasBody(FD))
88 return FD;
89 } else if (auto TD = dyn_cast<TagDecl>(D)) {
90 if (auto Def = TD->getDefinition())
91 return Def;
92 }
93 return D;
94 }
95
Craig Topper4f12f102014-03-12 06:41:41 +000096 llvm::Module *ReleaseModule() override { return M.release(); }
Mike Stump11289f42009-09-09 15:08:12 +000097
Craig Topper4f12f102014-03-12 06:41:41 +000098 void Initialize(ASTContext &Context) override {
Chris Lattneradf1f512008-02-06 02:01:47 +000099 Ctx = &Context;
Mike Stump11289f42009-09-09 15:08:12 +0000100
Douglas Gregore8bbc122011-09-02 00:18:52 +0000101 M->setTargetTriple(Ctx->getTargetInfo().getTriple().getTriple());
James Y Knightb214cbc2016-03-04 19:00:41 +0000102 M->setDataLayout(Ctx->getTargetInfo().getDataLayout());
Mehdi Aminica3cf9e2015-07-24 16:04:29 +0000103 Builder.reset(new CodeGen::CodeGenModule(Context, HeaderSearchOpts,
104 PreprocessorOpts, CodeGenOpts,
105 *M, Diags, CoverageInfo));
Hans Wennborg75958c42013-08-08 00:17:41 +0000106
Peter Collingbournedc134532016-01-16 00:31:22 +0000107 for (auto &&Lib : CodeGenOpts.DependentLibraries)
Nico Weber66220292016-03-02 17:28:48 +0000108 Builder->AddDependentLib(Lib);
Peter Collingbournedc134532016-01-16 00:31:22 +0000109 for (auto &&Opt : CodeGenOpts.LinkerOptions)
Nico Weber66220292016-03-02 17:28:48 +0000110 Builder->AppendLinkerOptions(Opt);
Chris Lattneradf1f512008-02-06 02:01:47 +0000111 }
Mike Stump11289f42009-09-09 15:08:12 +0000112
Craig Topper4f12f102014-03-12 06:41:41 +0000113 void HandleCXXStaticMemberVarInstantiation(VarDecl *VD) override {
David Blaikie4a9ec7b2013-08-19 21:02:26 +0000114 if (Diags.hasErrorOccurred())
115 return;
116
Rafael Espindoladf88f6f2012-03-08 15:51:03 +0000117 Builder->HandleCXXStaticMemberVarInstantiation(VD);
Rafael Espindola189fa742012-03-05 10:54:55 +0000118 }
119
Craig Topper4f12f102014-03-12 06:41:41 +0000120 bool HandleTopLevelDecl(DeclGroupRef DG) override {
David Blaikie4a9ec7b2013-08-19 21:02:26 +0000121 if (Diags.hasErrorOccurred())
122 return true;
123
Richard Smith7f5755c2014-08-01 22:42:16 +0000124 HandlingTopLevelDeclRAII HandlingDecl(*this);
125
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000126 // Make sure to emit all elements of a Decl.
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000127 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
128 Builder->EmitTopLevelDecl(*I);
Hans Wennborgdfcb7d62014-06-06 17:36:17 +0000129
Richard Smith7f5755c2014-08-01 22:42:16 +0000130 return true;
131 }
132
133 void EmitDeferredDecls() {
Richard Smithc9cbde72014-08-13 21:15:09 +0000134 if (DeferredInlineMethodDefinitions.empty())
135 return;
136
Richard Smith1ba0a072014-08-01 20:39:36 +0000137 // Emit any deferred inline method definitions. Note that more deferred
138 // methods may be added during this loop, since ASTConsumer callbacks
139 // can be invoked if AST inspection results in declarations being added.
Richard Smithc9cbde72014-08-13 21:15:09 +0000140 HandlingTopLevelDeclRAII HandlingDecl(*this);
141 for (unsigned I = 0; I != DeferredInlineMethodDefinitions.size(); ++I)
Richard Smith1ba0a072014-08-01 20:39:36 +0000142 Builder->EmitTopLevelDecl(DeferredInlineMethodDefinitions[I]);
Richard Smith455768e2014-08-01 20:09:39 +0000143 DeferredInlineMethodDefinitions.clear();
Chris Lattneradf1f512008-02-06 02:01:47 +0000144 }
Daniel Dunbarfce4be82008-08-15 23:26:23 +0000145
Hans Wennborga926d842014-05-23 20:37:38 +0000146 void HandleInlineMethodDefinition(CXXMethodDecl *D) override {
147 if (Diags.hasErrorOccurred())
148 return;
149
150 assert(D->doesThisDeclarationHaveABody());
151
Hans Wennborgdfcb7d62014-06-06 17:36:17 +0000152 // We may want to emit this definition. However, that decision might be
153 // based on computing the linkage, and we have to defer that in case we
154 // are inside of something that will change the method's final linkage,
155 // e.g.
156 // typedef struct {
157 // void bar();
158 // void foo() { bar(); }
159 // } A;
160 DeferredInlineMethodDefinitions.push_back(D);
Alex Lorenzee024992014-08-04 18:41:51 +0000161
Justin Bogner94d384e2014-11-18 00:34:46 +0000162 // Provide some coverage mapping even for methods that aren't emitted.
163 // Don't do this for templated classes though, as they may not be
164 // instantiable.
165 if (!D->getParent()->getDescribedClassTemplate())
166 Builder->AddDeferredUnusedCoverageMapping(D);
Hans Wennborga926d842014-05-23 20:37:38 +0000167 }
168
Chris Lattnera5e4d302008-02-06 04:51:19 +0000169 /// HandleTagDeclDefinition - This callback is invoked each time a TagDecl
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000170 /// to (e.g. struct, union, enum, class) is completed. This allows the
171 /// client hack on the type, which can occur at any point in the file
172 /// (because these can be defined in declspecs).
Craig Topper4f12f102014-03-12 06:41:41 +0000173 void HandleTagDeclDefinition(TagDecl *D) override {
David Blaikie4a9ec7b2013-08-19 21:02:26 +0000174 if (Diags.hasErrorOccurred())
175 return;
176
Chris Lattner68be6062008-02-06 05:08:19 +0000177 Builder->UpdateCompletedType(D);
Hans Wennborg56fc62b2014-07-17 20:25:23 +0000178
179 // For MSVC compatibility, treat declarations of static data members with
180 // inline initializers as definitions.
David Majnemer3f021502015-10-08 04:53:31 +0000181 if (Ctx->getTargetInfo().getCXXABI().isMicrosoft()) {
Hans Wennborg56fc62b2014-07-17 20:25:23 +0000182 for (Decl *Member : D->decls()) {
183 if (VarDecl *VD = dyn_cast<VarDecl>(Member)) {
184 if (Ctx->isMSStaticDataMemberInlineDefinition(VD) &&
185 Ctx->DeclMustBeEmitted(VD)) {
186 Builder->EmitGlobal(VD);
187 }
188 }
189 }
190 }
Alexey Bataevc5b1d322016-03-04 09:22:22 +0000191 // For OpenMP emit declare reduction functions, if required.
192 if (Ctx->getLangOpts().OpenMP) {
193 for (Decl *Member : D->decls()) {
194 if (auto *DRD = dyn_cast<OMPDeclareReductionDecl>(Member)) {
195 if (Ctx->DeclMustBeEmitted(DRD))
196 Builder->EmitGlobal(DRD);
197 }
198 }
199 }
Chris Lattnera5e4d302008-02-06 04:51:19 +0000200 }
Ted Kremenek7db4f602008-08-07 19:47:41 +0000201
Craig Topper4f12f102014-03-12 06:41:41 +0000202 void HandleTagDeclRequiredDefinition(const TagDecl *D) override {
David Blaikie4a9ec7b2013-08-19 21:02:26 +0000203 if (Diags.hasErrorOccurred())
204 return;
205
David Blaikie48ad6dc2013-07-13 21:08:14 +0000206 if (CodeGen::CGDebugInfo *DI = Builder->getModuleDebugInfo())
207 if (const RecordDecl *RD = dyn_cast<RecordDecl>(D))
David Blaikieb2e86eb2013-08-15 20:49:17 +0000208 DI->completeRequiredType(RD);
David Blaikie48ad6dc2013-07-13 21:08:14 +0000209 }
210
Craig Topper4f12f102014-03-12 06:41:41 +0000211 void HandleTranslationUnit(ASTContext &Ctx) override {
Manman Ren581c2b92016-01-28 23:29:02 +0000212 // Release the Builder when there is no error.
213 if (!Diags.hasErrorOccurred() && Builder)
214 Builder->Release();
215
216 // If there are errors before or when releasing the Builder, reset
217 // the module to stop here before invoking the backend.
Ted Kremenek7db4f602008-08-07 19:47:41 +0000218 if (Diags.hasErrorOccurred()) {
Rafael Espindolac0ff7442013-12-09 14:59:08 +0000219 if (Builder)
220 Builder->clear();
Ted Kremenek7db4f602008-08-07 19:47:41 +0000221 M.reset();
222 return;
223 }
Daniel Dunbare017ecc2009-12-19 17:50:07 +0000224 }
Douglas Gregorbeecd582009-04-21 17:11:58 +0000225
David Majnemer929025d2016-01-26 19:30:26 +0000226 void AssignInheritanceModel(CXXRecordDecl *RD) override {
227 if (Diags.hasErrorOccurred())
228 return;
229
230 Builder->RefreshTypeCacheForClass(RD);
231 }
232
Craig Topper4f12f102014-03-12 06:41:41 +0000233 void CompleteTentativeDefinition(VarDecl *D) override {
Douglas Gregorbeecd582009-04-21 17:11:58 +0000234 if (Diags.hasErrorOccurred())
235 return;
236
237 Builder->EmitTentativeDefinition(D);
238 }
Douglas Gregor88d292c2010-05-13 16:44:06 +0000239
Nico Weberb6a5d052015-01-15 04:07:35 +0000240 void HandleVTable(CXXRecordDecl *RD) override {
Douglas Gregor88d292c2010-05-13 16:44:06 +0000241 if (Diags.hasErrorOccurred())
242 return;
243
Nico Weberb6a5d052015-01-15 04:07:35 +0000244 Builder->EmitVTable(RD);
Douglas Gregor88d292c2010-05-13 16:44:06 +0000245 }
Chris Lattneradf1f512008-02-06 02:01:47 +0000246 };
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000247}
Chris Lattnerf97fe382007-05-24 06:29:05 +0000248
David Blaikie68e081d2011-12-20 02:48:34 +0000249void CodeGenerator::anchor() { }
250
Adrian Prantle74f5252015-06-30 02:26:03 +0000251CodeGenerator *clang::CreateLLVMCodeGen(
252 DiagnosticsEngine &Diags, const std::string &ModuleName,
253 const HeaderSearchOptions &HeaderSearchOpts,
254 const PreprocessorOptions &PreprocessorOpts, const CodeGenOptions &CGO,
255 llvm::LLVMContext &C, CoverageSourceInfo *CoverageInfo) {
256 return new CodeGeneratorImpl(Diags, ModuleName, HeaderSearchOpts,
257 PreprocessorOpts, CGO, C, CoverageInfo);
Chris Lattnerf97fe382007-05-24 06:29:05 +0000258}