blob: 25e57401fd57aa029a6cc1017e76c386f00eabb0 [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;
Chandler Carruthbc55fe22009-11-12 17:24:48 +000035 const CodeGenOptions CodeGenOpts; // Intentionally copied in.
Richard Smith7f5755c2014-08-01 22:42:16 +000036
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
Alex Lorenzee024992014-08-04 18:41:51 +000049 CoverageSourceInfo *CoverageInfo;
50
Chris Lattneradf1f512008-02-06 02:01:47 +000051 protected:
Ahmed Charlesb8984322014-03-07 20:03:18 +000052 std::unique_ptr<llvm::Module> M;
53 std::unique_ptr<CodeGen::CodeGenModule> Builder;
54
Hans Wennborg2b0d0142014-12-18 19:19:00 +000055 private:
56 SmallVector<CXXMethodDecl *, 8> DeferredInlineMethodDefinitions;
57
Chris Lattneradf1f512008-02-06 02:01:47 +000058 public:
David Blaikie9c902b52011-09-25 23:23:43 +000059 CodeGeneratorImpl(DiagnosticsEngine &diags, const std::string& ModuleName,
Alex Lorenzee024992014-08-04 18:41:51 +000060 const CodeGenOptions &CGO, llvm::LLVMContext& C,
61 CoverageSourceInfo *CoverageInfo = nullptr)
Yaron Keren5fb94242014-12-25 11:38:15 +000062 : Diags(diags), Ctx(nullptr), CodeGenOpts(CGO), HandlingTopLevelDecls(0),
Alex Lorenzee024992014-08-04 18:41:51 +000063 CoverageInfo(CoverageInfo),
Bill Wendlingc86a2f32013-02-14 08:09:20 +000064 M(new llvm::Module(ModuleName, C)) {}
Mike Stump11289f42009-09-09 15:08:12 +000065
Alexander Kornienko34eb2072015-04-11 02:00:23 +000066 ~CodeGeneratorImpl() override {
Hans Wennborg0c0a8c82014-12-19 23:35:11 +000067 // There should normally not be any leftover inline method definitions.
68 assert(DeferredInlineMethodDefinitions.empty() ||
69 Diags.hasErrorOccurred());
Hans Wennborg2b0d0142014-12-18 19:19:00 +000070 }
Mike Stump11289f42009-09-09 15:08:12 +000071
Craig Topper4f12f102014-03-12 06:41:41 +000072 llvm::Module* GetModule() override {
Daniel Dunbar30c514e2008-10-21 19:55:09 +000073 return M.get();
74 }
Mike Stump11289f42009-09-09 15:08:12 +000075
Alp Tokerfb8d02b2014-06-05 22:10:59 +000076 const Decl *GetDeclForMangledName(StringRef MangledName) override {
77 GlobalDecl Result;
78 if (!Builder->lookupRepresentativeDecl(MangledName, Result))
79 return nullptr;
80 const Decl *D = Result.getCanonicalDecl().getDecl();
81 if (auto FD = dyn_cast<FunctionDecl>(D)) {
82 if (FD->hasBody(FD))
83 return FD;
84 } else if (auto TD = dyn_cast<TagDecl>(D)) {
85 if (auto Def = TD->getDefinition())
86 return Def;
87 }
88 return D;
89 }
90
Craig Topper4f12f102014-03-12 06:41:41 +000091 llvm::Module *ReleaseModule() override { return M.release(); }
Mike Stump11289f42009-09-09 15:08:12 +000092
Craig Topper4f12f102014-03-12 06:41:41 +000093 void Initialize(ASTContext &Context) override {
Chris Lattneradf1f512008-02-06 02:01:47 +000094 Ctx = &Context;
Mike Stump11289f42009-09-09 15:08:12 +000095
Douglas Gregore8bbc122011-09-02 00:18:52 +000096 M->setTargetTriple(Ctx->getTargetInfo().getTriple().getTriple());
97 M->setDataLayout(Ctx->getTargetInfo().getTargetDescription());
Nico Weberb6cb6952015-01-17 02:27:54 +000098 TD.reset(
99 new llvm::DataLayout(Ctx->getTargetInfo().getTargetDescription()));
John McCall568d4102013-04-16 22:48:20 +0000100 Builder.reset(new CodeGen::CodeGenModule(Context, CodeGenOpts, *M, *TD,
Alex Lorenzee024992014-08-04 18:41:51 +0000101 Diags, CoverageInfo));
Hans Wennborg75958c42013-08-08 00:17:41 +0000102
103 for (size_t i = 0, e = CodeGenOpts.DependentLibraries.size(); i < e; ++i)
104 HandleDependentLibrary(CodeGenOpts.DependentLibraries[i]);
Chris Lattneradf1f512008-02-06 02:01:47 +0000105 }
Mike Stump11289f42009-09-09 15:08:12 +0000106
Craig Topper4f12f102014-03-12 06:41:41 +0000107 void HandleCXXStaticMemberVarInstantiation(VarDecl *VD) override {
David Blaikie4a9ec7b2013-08-19 21:02:26 +0000108 if (Diags.hasErrorOccurred())
109 return;
110
Rafael Espindoladf88f6f2012-03-08 15:51:03 +0000111 Builder->HandleCXXStaticMemberVarInstantiation(VD);
Rafael Espindola189fa742012-03-05 10:54:55 +0000112 }
113
Craig Topper4f12f102014-03-12 06:41:41 +0000114 bool HandleTopLevelDecl(DeclGroupRef DG) override {
David Blaikie4a9ec7b2013-08-19 21:02:26 +0000115 if (Diags.hasErrorOccurred())
116 return true;
117
Richard Smith7f5755c2014-08-01 22:42:16 +0000118 HandlingTopLevelDeclRAII HandlingDecl(*this);
119
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000120 // Make sure to emit all elements of a Decl.
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000121 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
122 Builder->EmitTopLevelDecl(*I);
Hans Wennborgdfcb7d62014-06-06 17:36:17 +0000123
Richard Smith7f5755c2014-08-01 22:42:16 +0000124 return true;
125 }
126
127 void EmitDeferredDecls() {
Richard Smithc9cbde72014-08-13 21:15:09 +0000128 if (DeferredInlineMethodDefinitions.empty())
129 return;
130
Richard Smith1ba0a072014-08-01 20:39:36 +0000131 // Emit any deferred inline method definitions. Note that more deferred
132 // methods may be added during this loop, since ASTConsumer callbacks
133 // can be invoked if AST inspection results in declarations being added.
Richard Smithc9cbde72014-08-13 21:15:09 +0000134 HandlingTopLevelDeclRAII HandlingDecl(*this);
135 for (unsigned I = 0; I != DeferredInlineMethodDefinitions.size(); ++I)
Richard Smith1ba0a072014-08-01 20:39:36 +0000136 Builder->EmitTopLevelDecl(DeferredInlineMethodDefinitions[I]);
Richard Smith455768e2014-08-01 20:09:39 +0000137 DeferredInlineMethodDefinitions.clear();
Chris Lattneradf1f512008-02-06 02:01:47 +0000138 }
Daniel Dunbarfce4be82008-08-15 23:26:23 +0000139
Hans Wennborga926d842014-05-23 20:37:38 +0000140 void HandleInlineMethodDefinition(CXXMethodDecl *D) override {
141 if (Diags.hasErrorOccurred())
142 return;
143
144 assert(D->doesThisDeclarationHaveABody());
145
Hans Wennborgdfcb7d62014-06-06 17:36:17 +0000146 // We may want to emit this definition. However, that decision might be
147 // based on computing the linkage, and we have to defer that in case we
148 // are inside of something that will change the method's final linkage,
149 // e.g.
150 // typedef struct {
151 // void bar();
152 // void foo() { bar(); }
153 // } A;
154 DeferredInlineMethodDefinitions.push_back(D);
Alex Lorenzee024992014-08-04 18:41:51 +0000155
Justin Bogner94d384e2014-11-18 00:34:46 +0000156 // Provide some coverage mapping even for methods that aren't emitted.
157 // Don't do this for templated classes though, as they may not be
158 // instantiable.
159 if (!D->getParent()->getDescribedClassTemplate())
160 Builder->AddDeferredUnusedCoverageMapping(D);
Hans Wennborga926d842014-05-23 20:37:38 +0000161 }
162
Chris Lattnera5e4d302008-02-06 04:51:19 +0000163 /// HandleTagDeclDefinition - This callback is invoked each time a TagDecl
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000164 /// to (e.g. struct, union, enum, class) is completed. This allows the
165 /// client hack on the type, which can occur at any point in the file
166 /// (because these can be defined in declspecs).
Craig Topper4f12f102014-03-12 06:41:41 +0000167 void HandleTagDeclDefinition(TagDecl *D) override {
David Blaikie4a9ec7b2013-08-19 21:02:26 +0000168 if (Diags.hasErrorOccurred())
169 return;
170
Chris Lattner68be6062008-02-06 05:08:19 +0000171 Builder->UpdateCompletedType(D);
Hans Wennborg56fc62b2014-07-17 20:25:23 +0000172
173 // For MSVC compatibility, treat declarations of static data members with
174 // inline initializers as definitions.
175 if (Ctx->getLangOpts().MSVCCompat) {
176 for (Decl *Member : D->decls()) {
177 if (VarDecl *VD = dyn_cast<VarDecl>(Member)) {
178 if (Ctx->isMSStaticDataMemberInlineDefinition(VD) &&
179 Ctx->DeclMustBeEmitted(VD)) {
180 Builder->EmitGlobal(VD);
181 }
182 }
183 }
184 }
Chris Lattnera5e4d302008-02-06 04:51:19 +0000185 }
Ted Kremenek7db4f602008-08-07 19:47:41 +0000186
Craig Topper4f12f102014-03-12 06:41:41 +0000187 void HandleTagDeclRequiredDefinition(const TagDecl *D) override {
David Blaikie4a9ec7b2013-08-19 21:02:26 +0000188 if (Diags.hasErrorOccurred())
189 return;
190
David Blaikie48ad6dc2013-07-13 21:08:14 +0000191 if (CodeGen::CGDebugInfo *DI = Builder->getModuleDebugInfo())
192 if (const RecordDecl *RD = dyn_cast<RecordDecl>(D))
David Blaikieb2e86eb2013-08-15 20:49:17 +0000193 DI->completeRequiredType(RD);
David Blaikie48ad6dc2013-07-13 21:08:14 +0000194 }
195
Craig Topper4f12f102014-03-12 06:41:41 +0000196 void HandleTranslationUnit(ASTContext &Ctx) override {
Ted Kremenek7db4f602008-08-07 19:47:41 +0000197 if (Diags.hasErrorOccurred()) {
Rafael Espindolac0ff7442013-12-09 14:59:08 +0000198 if (Builder)
199 Builder->clear();
Ted Kremenek7db4f602008-08-07 19:47:41 +0000200 M.reset();
201 return;
202 }
203
204 if (Builder)
205 Builder->Release();
Daniel Dunbare017ecc2009-12-19 17:50:07 +0000206 }
Douglas Gregorbeecd582009-04-21 17:11:58 +0000207
Craig Topper4f12f102014-03-12 06:41:41 +0000208 void CompleteTentativeDefinition(VarDecl *D) override {
Douglas Gregorbeecd582009-04-21 17:11:58 +0000209 if (Diags.hasErrorOccurred())
210 return;
211
212 Builder->EmitTentativeDefinition(D);
213 }
Douglas Gregor88d292c2010-05-13 16:44:06 +0000214
Nico Weberb6a5d052015-01-15 04:07:35 +0000215 void HandleVTable(CXXRecordDecl *RD) override {
Douglas Gregor88d292c2010-05-13 16:44:06 +0000216 if (Diags.hasErrorOccurred())
217 return;
218
Nico Weberb6a5d052015-01-15 04:07:35 +0000219 Builder->EmitVTable(RD);
Douglas Gregor88d292c2010-05-13 16:44:06 +0000220 }
Reid Klecknere43f0fe2013-05-08 13:44:39 +0000221
Craig Topper4f12f102014-03-12 06:41:41 +0000222 void HandleLinkerOptionPragma(llvm::StringRef Opts) override {
Reid Klecknere43f0fe2013-05-08 13:44:39 +0000223 Builder->AppendLinkerOptions(Opts);
224 }
225
Craig Topper4f12f102014-03-12 06:41:41 +0000226 void HandleDetectMismatch(llvm::StringRef Name,
227 llvm::StringRef Value) override {
Aaron Ballman5d041be2013-06-04 02:07:14 +0000228 Builder->AddDetectMismatch(Name, Value);
229 }
230
Craig Topper4f12f102014-03-12 06:41:41 +0000231 void HandleDependentLibrary(llvm::StringRef Lib) override {
Reid Klecknere43f0fe2013-05-08 13:44:39 +0000232 Builder->AddDependentLib(Lib);
233 }
Chris Lattneradf1f512008-02-06 02:01:47 +0000234 };
Chris Lattnerf97fe382007-05-24 06:29:05 +0000235}
236
David Blaikie68e081d2011-12-20 02:48:34 +0000237void CodeGenerator::anchor() { }
238
David Blaikie9c902b52011-09-25 23:23:43 +0000239CodeGenerator *clang::CreateLLVMCodeGen(DiagnosticsEngine &Diags,
Ted Kremenek2c674f62008-08-05 18:50:11 +0000240 const std::string& ModuleName,
Chandler Carruthbc55fe22009-11-12 17:24:48 +0000241 const CodeGenOptions &CGO,
Alex Lorenzee024992014-08-04 18:41:51 +0000242 llvm::LLVMContext& C,
243 CoverageSourceInfo *CoverageInfo) {
244 return new CodeGeneratorImpl(Diags, ModuleName, CGO, C, CoverageInfo);
Chris Lattnerf97fe382007-05-24 06:29:05 +0000245}