blob: 03aecce761d753307f873385c1ef05e5c7c83e33 [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.
Chris Lattneradf1f512008-02-06 02:01:47 +000036 protected:
Ahmed Charlesb8984322014-03-07 20:03:18 +000037 std::unique_ptr<llvm::Module> M;
38 std::unique_ptr<CodeGen::CodeGenModule> Builder;
39
Chris Lattneradf1f512008-02-06 02:01:47 +000040 public:
David Blaikie9c902b52011-09-25 23:23:43 +000041 CodeGeneratorImpl(DiagnosticsEngine &diags, const std::string& ModuleName,
John McCall568d4102013-04-16 22:48:20 +000042 const CodeGenOptions &CGO, llvm::LLVMContext& C)
43 : Diags(diags), CodeGenOpts(CGO),
Bill Wendlingc86a2f32013-02-14 08:09:20 +000044 M(new llvm::Module(ModuleName, C)) {}
Mike Stump11289f42009-09-09 15:08:12 +000045
Ted Kremenek2c674f62008-08-05 18:50:11 +000046 virtual ~CodeGeneratorImpl() {}
Mike Stump11289f42009-09-09 15:08:12 +000047
Daniel Dunbar30c514e2008-10-21 19:55:09 +000048 virtual llvm::Module* GetModule() {
49 return M.get();
50 }
Mike Stump11289f42009-09-09 15:08:12 +000051
Ahmed Charles9a16beb2014-03-07 19:33:25 +000052 virtual llvm::Module *ReleaseModule() { return M.release(); }
Mike Stump11289f42009-09-09 15:08:12 +000053
Chris Lattneradf1f512008-02-06 02:01:47 +000054 virtual void Initialize(ASTContext &Context) {
55 Ctx = &Context;
Mike Stump11289f42009-09-09 15:08:12 +000056
Douglas Gregore8bbc122011-09-02 00:18:52 +000057 M->setTargetTriple(Ctx->getTargetInfo().getTriple().getTriple());
58 M->setDataLayout(Ctx->getTargetInfo().getTargetDescription());
Micah Villmowdd31ca12012-10-08 16:25:52 +000059 TD.reset(new llvm::DataLayout(Ctx->getTargetInfo().getTargetDescription()));
John McCall568d4102013-04-16 22:48:20 +000060 Builder.reset(new CodeGen::CodeGenModule(Context, CodeGenOpts, *M, *TD,
61 Diags));
Hans Wennborg75958c42013-08-08 00:17:41 +000062
63 for (size_t i = 0, e = CodeGenOpts.DependentLibraries.size(); i < e; ++i)
64 HandleDependentLibrary(CodeGenOpts.DependentLibraries[i]);
Chris Lattneradf1f512008-02-06 02:01:47 +000065 }
Mike Stump11289f42009-09-09 15:08:12 +000066
Rafael Espindoladf88f6f2012-03-08 15:51:03 +000067 virtual void HandleCXXStaticMemberVarInstantiation(VarDecl *VD) {
David Blaikie4a9ec7b2013-08-19 21:02:26 +000068 if (Diags.hasErrorOccurred())
69 return;
70
Rafael Espindoladf88f6f2012-03-08 15:51:03 +000071 Builder->HandleCXXStaticMemberVarInstantiation(VD);
Rafael Espindola189fa742012-03-05 10:54:55 +000072 }
73
Argyrios Kyrtzidis841dd882011-11-18 00:26:59 +000074 virtual bool HandleTopLevelDecl(DeclGroupRef DG) {
David Blaikie4a9ec7b2013-08-19 21:02:26 +000075 if (Diags.hasErrorOccurred())
76 return true;
77
Douglas Gregor6e6ad602009-01-20 01:17:11 +000078 // Make sure to emit all elements of a Decl.
Chris Lattner5bbb3c82009-03-29 16:50:03 +000079 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
80 Builder->EmitTopLevelDecl(*I);
Argyrios Kyrtzidis841dd882011-11-18 00:26:59 +000081 return true;
Chris Lattneradf1f512008-02-06 02:01:47 +000082 }
Daniel Dunbarfce4be82008-08-15 23:26:23 +000083
Chris Lattnera5e4d302008-02-06 04:51:19 +000084 /// HandleTagDeclDefinition - This callback is invoked each time a TagDecl
Chris Lattner5bbb3c82009-03-29 16:50:03 +000085 /// to (e.g. struct, union, enum, class) is completed. This allows the
86 /// client hack on the type, which can occur at any point in the file
87 /// (because these can be defined in declspecs).
Chris Lattnera5e4d302008-02-06 04:51:19 +000088 virtual void HandleTagDeclDefinition(TagDecl *D) {
David Blaikie4a9ec7b2013-08-19 21:02:26 +000089 if (Diags.hasErrorOccurred())
90 return;
91
Chris Lattner68be6062008-02-06 05:08:19 +000092 Builder->UpdateCompletedType(D);
Douglas Gregore0610152011-02-15 18:11:42 +000093
94 // In C++, we may have member functions that need to be emitted at this
95 // point.
David Blaikiebbafb8a2012-03-11 07:00:24 +000096 if (Ctx->getLangOpts().CPlusPlus && !D->isDependentContext()) {
Aaron Ballman629afae2014-03-07 19:56:05 +000097 for (auto *M : D->decls())
98 if (auto *Method = dyn_cast<CXXMethodDecl>(M))
Alexis Hunt4a8ea102011-05-06 20:44:56 +000099 if (Method->doesThisDeclarationHaveABody() &&
Douglas Gregord66828d2011-02-19 21:54:50 +0000100 (Method->hasAttr<UsedAttr>() ||
101 Method->hasAttr<ConstructorAttr>()))
Douglas Gregore0610152011-02-15 18:11:42 +0000102 Builder->EmitTopLevelDecl(Method);
103 }
Chris Lattnera5e4d302008-02-06 04:51:19 +0000104 }
Ted Kremenek7db4f602008-08-07 19:47:41 +0000105
Craig Toppera798a9d2014-03-02 09:32:10 +0000106 virtual void HandleTagDeclRequiredDefinition(const TagDecl *D) override {
David Blaikie4a9ec7b2013-08-19 21:02:26 +0000107 if (Diags.hasErrorOccurred())
108 return;
109
David Blaikie48ad6dc2013-07-13 21:08:14 +0000110 if (CodeGen::CGDebugInfo *DI = Builder->getModuleDebugInfo())
111 if (const RecordDecl *RD = dyn_cast<RecordDecl>(D))
David Blaikieb2e86eb2013-08-15 20:49:17 +0000112 DI->completeRequiredType(RD);
David Blaikie48ad6dc2013-07-13 21:08:14 +0000113 }
114
Chris Lattnercf169832009-03-28 04:11:33 +0000115 virtual void HandleTranslationUnit(ASTContext &Ctx) {
Ted Kremenek7db4f602008-08-07 19:47:41 +0000116 if (Diags.hasErrorOccurred()) {
Rafael Espindolac0ff7442013-12-09 14:59:08 +0000117 if (Builder)
118 Builder->clear();
Ted Kremenek7db4f602008-08-07 19:47:41 +0000119 M.reset();
120 return;
121 }
122
123 if (Builder)
124 Builder->Release();
Daniel Dunbare017ecc2009-12-19 17:50:07 +0000125 }
Douglas Gregorbeecd582009-04-21 17:11:58 +0000126
127 virtual void CompleteTentativeDefinition(VarDecl *D) {
128 if (Diags.hasErrorOccurred())
129 return;
130
131 Builder->EmitTentativeDefinition(D);
132 }
Douglas Gregor88d292c2010-05-13 16:44:06 +0000133
134 virtual void HandleVTable(CXXRecordDecl *RD, bool DefinitionRequired) {
135 if (Diags.hasErrorOccurred())
136 return;
137
138 Builder->EmitVTable(RD, DefinitionRequired);
139 }
Reid Klecknere43f0fe2013-05-08 13:44:39 +0000140
141 virtual void HandleLinkerOptionPragma(llvm::StringRef Opts) {
142 Builder->AppendLinkerOptions(Opts);
143 }
144
Aaron Ballman5d041be2013-06-04 02:07:14 +0000145 virtual void HandleDetectMismatch(llvm::StringRef Name,
146 llvm::StringRef Value) {
147 Builder->AddDetectMismatch(Name, Value);
148 }
149
Reid Klecknere43f0fe2013-05-08 13:44:39 +0000150 virtual void HandleDependentLibrary(llvm::StringRef Lib) {
151 Builder->AddDependentLib(Lib);
152 }
Chris Lattneradf1f512008-02-06 02:01:47 +0000153 };
Chris Lattnerf97fe382007-05-24 06:29:05 +0000154}
155
David Blaikie68e081d2011-12-20 02:48:34 +0000156void CodeGenerator::anchor() { }
157
David Blaikie9c902b52011-09-25 23:23:43 +0000158CodeGenerator *clang::CreateLLVMCodeGen(DiagnosticsEngine &Diags,
Ted Kremenek2c674f62008-08-05 18:50:11 +0000159 const std::string& ModuleName,
Chandler Carruthbc55fe22009-11-12 17:24:48 +0000160 const CodeGenOptions &CGO,
John McCall568d4102013-04-16 22:48:20 +0000161 const TargetOptions &/*TO*/,
Owen Andersonecaeaa82009-07-01 23:14:14 +0000162 llvm::LLVMContext& C) {
John McCall568d4102013-04-16 22:48:20 +0000163 return new CodeGeneratorImpl(Diags, ModuleName, CGO, C);
Chris Lattnerf97fe382007-05-24 06:29:05 +0000164}