blob: def56a96312639d722d33fcc6016031b05249ac3 [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;
Ahmed Charlesb8984322014-03-07 20:03:18 +000033 std::unique_ptr<const llvm::DataLayout> TD;
Chris Lattneradf1f512008-02-06 02:01:47 +000034 ASTContext *Ctx;
Adrian Prantle74f5252015-06-30 02:26:03 +000035 const HeaderSearchOptions &HeaderSearchOpts; // Only used for debug info.
36 const PreprocessorOptions &PreprocessorOpts; // Only used for debug info.
Chandler Carruthbc55fe22009-11-12 17:24:48 +000037 const CodeGenOptions CodeGenOpts; // Intentionally copied in.
Richard Smith7f5755c2014-08-01 22:42:16 +000038
39 unsigned HandlingTopLevelDecls;
40 struct HandlingTopLevelDeclRAII {
41 CodeGeneratorImpl &Self;
42 HandlingTopLevelDeclRAII(CodeGeneratorImpl &Self) : Self(Self) {
43 ++Self.HandlingTopLevelDecls;
44 }
45 ~HandlingTopLevelDeclRAII() {
46 if (--Self.HandlingTopLevelDecls == 0)
47 Self.EmitDeferredDecls();
48 }
49 };
50
Alex Lorenzee024992014-08-04 18:41:51 +000051 CoverageSourceInfo *CoverageInfo;
52
Chris Lattneradf1f512008-02-06 02:01:47 +000053 protected:
Ahmed Charlesb8984322014-03-07 20:03:18 +000054 std::unique_ptr<llvm::Module> M;
55 std::unique_ptr<CodeGen::CodeGenModule> Builder;
56
Hans Wennborg2b0d0142014-12-18 19:19:00 +000057 private:
58 SmallVector<CXXMethodDecl *, 8> DeferredInlineMethodDefinitions;
59
Chris Lattneradf1f512008-02-06 02:01:47 +000060 public:
Adrian Prantle74f5252015-06-30 02:26:03 +000061 CodeGeneratorImpl(DiagnosticsEngine &diags, const std::string &ModuleName,
62 const HeaderSearchOptions &HSO,
63 const PreprocessorOptions &PPO, const CodeGenOptions &CGO,
64 llvm::LLVMContext &C,
Alex Lorenzee024992014-08-04 18:41:51 +000065 CoverageSourceInfo *CoverageInfo = nullptr)
Adrian Prantle74f5252015-06-30 02:26:03 +000066 : Diags(diags), Ctx(nullptr), HeaderSearchOpts(HSO),
67 PreprocessorOpts(PPO), CodeGenOpts(CGO), HandlingTopLevelDecls(0),
68 CoverageInfo(CoverageInfo),
69 M(new llvm::Module(ModuleName, C)) {}
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());
102 M->setDataLayout(Ctx->getTargetInfo().getTargetDescription());
Nico Weberb6cb6952015-01-17 02:27:54 +0000103 TD.reset(
104 new llvm::DataLayout(Ctx->getTargetInfo().getTargetDescription()));
Adrian Prantle74f5252015-06-30 02:26:03 +0000105 Builder.reset(new CodeGen::CodeGenModule(Context,
106 HeaderSearchOpts,
107 PreprocessorOpts,
108 CodeGenOpts, *M, *TD,
Alex Lorenzee024992014-08-04 18:41:51 +0000109 Diags, CoverageInfo));
Hans Wennborg75958c42013-08-08 00:17:41 +0000110
111 for (size_t i = 0, e = CodeGenOpts.DependentLibraries.size(); i < e; ++i)
112 HandleDependentLibrary(CodeGenOpts.DependentLibraries[i]);
Chris Lattneradf1f512008-02-06 02:01:47 +0000113 }
Mike Stump11289f42009-09-09 15:08:12 +0000114
Craig Topper4f12f102014-03-12 06:41:41 +0000115 void HandleCXXStaticMemberVarInstantiation(VarDecl *VD) override {
David Blaikie4a9ec7b2013-08-19 21:02:26 +0000116 if (Diags.hasErrorOccurred())
117 return;
118
Rafael Espindoladf88f6f2012-03-08 15:51:03 +0000119 Builder->HandleCXXStaticMemberVarInstantiation(VD);
Rafael Espindola189fa742012-03-05 10:54:55 +0000120 }
121
Craig Topper4f12f102014-03-12 06:41:41 +0000122 bool HandleTopLevelDecl(DeclGroupRef DG) override {
David Blaikie4a9ec7b2013-08-19 21:02:26 +0000123 if (Diags.hasErrorOccurred())
124 return true;
125
Richard Smith7f5755c2014-08-01 22:42:16 +0000126 HandlingTopLevelDeclRAII HandlingDecl(*this);
127
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000128 // Make sure to emit all elements of a Decl.
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000129 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
130 Builder->EmitTopLevelDecl(*I);
Hans Wennborgdfcb7d62014-06-06 17:36:17 +0000131
Richard Smith7f5755c2014-08-01 22:42:16 +0000132 return true;
133 }
134
135 void EmitDeferredDecls() {
Richard Smithc9cbde72014-08-13 21:15:09 +0000136 if (DeferredInlineMethodDefinitions.empty())
137 return;
138
Richard Smith1ba0a072014-08-01 20:39:36 +0000139 // Emit any deferred inline method definitions. Note that more deferred
140 // methods may be added during this loop, since ASTConsumer callbacks
141 // can be invoked if AST inspection results in declarations being added.
Richard Smithc9cbde72014-08-13 21:15:09 +0000142 HandlingTopLevelDeclRAII HandlingDecl(*this);
143 for (unsigned I = 0; I != DeferredInlineMethodDefinitions.size(); ++I)
Richard Smith1ba0a072014-08-01 20:39:36 +0000144 Builder->EmitTopLevelDecl(DeferredInlineMethodDefinitions[I]);
Richard Smith455768e2014-08-01 20:09:39 +0000145 DeferredInlineMethodDefinitions.clear();
Chris Lattneradf1f512008-02-06 02:01:47 +0000146 }
Daniel Dunbarfce4be82008-08-15 23:26:23 +0000147
Hans Wennborga926d842014-05-23 20:37:38 +0000148 void HandleInlineMethodDefinition(CXXMethodDecl *D) override {
149 if (Diags.hasErrorOccurred())
150 return;
151
152 assert(D->doesThisDeclarationHaveABody());
153
Hans Wennborgdfcb7d62014-06-06 17:36:17 +0000154 // We may want to emit this definition. However, that decision might be
155 // based on computing the linkage, and we have to defer that in case we
156 // are inside of something that will change the method's final linkage,
157 // e.g.
158 // typedef struct {
159 // void bar();
160 // void foo() { bar(); }
161 // } A;
162 DeferredInlineMethodDefinitions.push_back(D);
Alex Lorenzee024992014-08-04 18:41:51 +0000163
Justin Bogner94d384e2014-11-18 00:34:46 +0000164 // Provide some coverage mapping even for methods that aren't emitted.
165 // Don't do this for templated classes though, as they may not be
166 // instantiable.
167 if (!D->getParent()->getDescribedClassTemplate())
168 Builder->AddDeferredUnusedCoverageMapping(D);
Hans Wennborga926d842014-05-23 20:37:38 +0000169 }
170
Chris Lattnera5e4d302008-02-06 04:51:19 +0000171 /// HandleTagDeclDefinition - This callback is invoked each time a TagDecl
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000172 /// to (e.g. struct, union, enum, class) is completed. This allows the
173 /// client hack on the type, which can occur at any point in the file
174 /// (because these can be defined in declspecs).
Craig Topper4f12f102014-03-12 06:41:41 +0000175 void HandleTagDeclDefinition(TagDecl *D) override {
David Blaikie4a9ec7b2013-08-19 21:02:26 +0000176 if (Diags.hasErrorOccurred())
177 return;
178
Chris Lattner68be6062008-02-06 05:08:19 +0000179 Builder->UpdateCompletedType(D);
Hans Wennborg56fc62b2014-07-17 20:25:23 +0000180
181 // For MSVC compatibility, treat declarations of static data members with
182 // inline initializers as definitions.
183 if (Ctx->getLangOpts().MSVCCompat) {
184 for (Decl *Member : D->decls()) {
185 if (VarDecl *VD = dyn_cast<VarDecl>(Member)) {
186 if (Ctx->isMSStaticDataMemberInlineDefinition(VD) &&
187 Ctx->DeclMustBeEmitted(VD)) {
188 Builder->EmitGlobal(VD);
189 }
190 }
191 }
192 }
Chris Lattnera5e4d302008-02-06 04:51:19 +0000193 }
Ted Kremenek7db4f602008-08-07 19:47:41 +0000194
Craig Topper4f12f102014-03-12 06:41:41 +0000195 void HandleTagDeclRequiredDefinition(const TagDecl *D) override {
David Blaikie4a9ec7b2013-08-19 21:02:26 +0000196 if (Diags.hasErrorOccurred())
197 return;
198
David Blaikie48ad6dc2013-07-13 21:08:14 +0000199 if (CodeGen::CGDebugInfo *DI = Builder->getModuleDebugInfo())
200 if (const RecordDecl *RD = dyn_cast<RecordDecl>(D))
David Blaikieb2e86eb2013-08-15 20:49:17 +0000201 DI->completeRequiredType(RD);
David Blaikie48ad6dc2013-07-13 21:08:14 +0000202 }
203
Craig Topper4f12f102014-03-12 06:41:41 +0000204 void HandleTranslationUnit(ASTContext &Ctx) override {
Ted Kremenek7db4f602008-08-07 19:47:41 +0000205 if (Diags.hasErrorOccurred()) {
Rafael Espindolac0ff7442013-12-09 14:59:08 +0000206 if (Builder)
207 Builder->clear();
Ted Kremenek7db4f602008-08-07 19:47:41 +0000208 M.reset();
209 return;
210 }
211
212 if (Builder)
213 Builder->Release();
Daniel Dunbare017ecc2009-12-19 17:50:07 +0000214 }
Douglas Gregorbeecd582009-04-21 17:11:58 +0000215
Craig Topper4f12f102014-03-12 06:41:41 +0000216 void CompleteTentativeDefinition(VarDecl *D) override {
Douglas Gregorbeecd582009-04-21 17:11:58 +0000217 if (Diags.hasErrorOccurred())
218 return;
219
220 Builder->EmitTentativeDefinition(D);
221 }
Douglas Gregor88d292c2010-05-13 16:44:06 +0000222
Nico Weberb6a5d052015-01-15 04:07:35 +0000223 void HandleVTable(CXXRecordDecl *RD) override {
Douglas Gregor88d292c2010-05-13 16:44:06 +0000224 if (Diags.hasErrorOccurred())
225 return;
226
Nico Weberb6a5d052015-01-15 04:07:35 +0000227 Builder->EmitVTable(RD);
Douglas Gregor88d292c2010-05-13 16:44:06 +0000228 }
Reid Klecknere43f0fe2013-05-08 13:44:39 +0000229
Craig Topper4f12f102014-03-12 06:41:41 +0000230 void HandleLinkerOptionPragma(llvm::StringRef Opts) override {
Reid Klecknere43f0fe2013-05-08 13:44:39 +0000231 Builder->AppendLinkerOptions(Opts);
232 }
233
Craig Topper4f12f102014-03-12 06:41:41 +0000234 void HandleDetectMismatch(llvm::StringRef Name,
235 llvm::StringRef Value) override {
Aaron Ballman5d041be2013-06-04 02:07:14 +0000236 Builder->AddDetectMismatch(Name, Value);
237 }
238
Craig Topper4f12f102014-03-12 06:41:41 +0000239 void HandleDependentLibrary(llvm::StringRef Lib) override {
Reid Klecknere43f0fe2013-05-08 13:44:39 +0000240 Builder->AddDependentLib(Lib);
241 }
Chris Lattneradf1f512008-02-06 02:01:47 +0000242 };
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000243}
Chris Lattnerf97fe382007-05-24 06:29:05 +0000244
David Blaikie68e081d2011-12-20 02:48:34 +0000245void CodeGenerator::anchor() { }
246
Adrian Prantle74f5252015-06-30 02:26:03 +0000247CodeGenerator *clang::CreateLLVMCodeGen(
248 DiagnosticsEngine &Diags, const std::string &ModuleName,
249 const HeaderSearchOptions &HeaderSearchOpts,
250 const PreprocessorOptions &PreprocessorOpts, const CodeGenOptions &CGO,
251 llvm::LLVMContext &C, CoverageSourceInfo *CoverageInfo) {
252 return new CodeGeneratorImpl(Diags, ModuleName, HeaderSearchOpts,
253 PreprocessorOpts, CGO, C, CoverageInfo);
Chris Lattnerf97fe382007-05-24 06:29:05 +0000254}