blob: 7e0e3aac072526abfecfe8f9986909d3665e7d0e [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- ModuleBuilder.cpp - Emit LLVM Code from ASTs ---------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This builds an AST and converts it to LLVM Code.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/CodeGen/ModuleBuilder.h"
15#include "CodeGenModule.h"
David Blaikie658cd2c2013-07-13 21:08:14 +000016#include "CGDebugInfo.h"
Chris Lattner8ee3c032008-02-06 02:01:47 +000017#include "clang/AST/ASTContext.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000018#include "clang/AST/DeclObjC.h"
19#include "clang/AST/Expr.h"
Chris Lattner8ee3c032008-02-06 02:01:47 +000020#include "clang/Basic/Diagnostic.h"
21#include "clang/Basic/TargetInfo.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000022#include "clang/Frontend/CodeGenOptions.h"
23#include "llvm/ADT/OwningPtr.h"
Reid Kleckner3190ca92013-05-08 13:44:39 +000024#include "llvm/ADT/StringRef.h"
Chandler Carruth3b844ba2013-01-02 11:45:17 +000025#include "llvm/IR/DataLayout.h"
26#include "llvm/IR/LLVMContext.h"
27#include "llvm/IR/Module.h"
Chris Lattnerbd360642009-03-26 05:00:52 +000028using namespace clang;
Ted Kremenek815c78f2008-08-05 18:50:11 +000029
Chris Lattner8ee3c032008-02-06 02:01:47 +000030namespace {
Benjamin Kramer85b45212009-11-28 19:45:26 +000031 class CodeGeneratorImpl : public CodeGenerator {
David Blaikied6471f72011-09-25 23:23:43 +000032 DiagnosticsEngine &Diags;
Micah Villmow25a6a842012-10-08 16:25:52 +000033 OwningPtr<const llvm::DataLayout> TD;
Chris Lattner8ee3c032008-02-06 02:01:47 +000034 ASTContext *Ctx;
Chandler Carruth2811ccf2009-11-12 17:24:48 +000035 const CodeGenOptions CodeGenOpts; // Intentionally copied in.
Chris Lattner8ee3c032008-02-06 02:01:47 +000036 protected:
Dylan Noblesmith6f42b622012-02-05 02:12:40 +000037 OwningPtr<llvm::Module> M;
38 OwningPtr<CodeGen::CodeGenModule> Builder;
Chris Lattner8ee3c032008-02-06 02:01:47 +000039 public:
David Blaikied6471f72011-09-25 23:23:43 +000040 CodeGeneratorImpl(DiagnosticsEngine &diags, const std::string& ModuleName,
John McCall3abae092013-04-16 22:48:20 +000041 const CodeGenOptions &CGO, llvm::LLVMContext& C)
42 : Diags(diags), CodeGenOpts(CGO),
Bill Wendlinge1092df2013-02-14 08:09:20 +000043 M(new llvm::Module(ModuleName, C)) {}
Mike Stump1eb44332009-09-09 15:08:12 +000044
Ted Kremenek815c78f2008-08-05 18:50:11 +000045 virtual ~CodeGeneratorImpl() {}
Mike Stump1eb44332009-09-09 15:08:12 +000046
Daniel Dunbard8c0ea12008-10-21 19:55:09 +000047 virtual llvm::Module* GetModule() {
48 return M.get();
49 }
Mike Stump1eb44332009-09-09 15:08:12 +000050
Ted Kremenek159346a2008-08-07 19:47:41 +000051 virtual llvm::Module* ReleaseModule() {
Ted Kremenek815c78f2008-08-05 18:50:11 +000052 return M.take();
Chris Lattner8ee3c032008-02-06 02:01:47 +000053 }
Mike Stump1eb44332009-09-09 15:08:12 +000054
Chris Lattner8ee3c032008-02-06 02:01:47 +000055 virtual void Initialize(ASTContext &Context) {
56 Ctx = &Context;
Mike Stump1eb44332009-09-09 15:08:12 +000057
Douglas Gregorbcfd1f52011-09-02 00:18:52 +000058 M->setTargetTriple(Ctx->getTargetInfo().getTriple().getTriple());
59 M->setDataLayout(Ctx->getTargetInfo().getTargetDescription());
Micah Villmow25a6a842012-10-08 16:25:52 +000060 TD.reset(new llvm::DataLayout(Ctx->getTargetInfo().getTargetDescription()));
John McCall3abae092013-04-16 22:48:20 +000061 Builder.reset(new CodeGen::CodeGenModule(Context, CodeGenOpts, *M, *TD,
62 Diags));
Chris Lattner8ee3c032008-02-06 02:01:47 +000063 }
Mike Stump1eb44332009-09-09 15:08:12 +000064
Rafael Espindola02503932012-03-08 15:51:03 +000065 virtual void HandleCXXStaticMemberVarInstantiation(VarDecl *VD) {
66 Builder->HandleCXXStaticMemberVarInstantiation(VD);
Rafael Espindola234fe652012-03-05 10:54:55 +000067 }
68
Argyrios Kyrtzidis88c25962011-11-18 00:26:59 +000069 virtual bool HandleTopLevelDecl(DeclGroupRef DG) {
Douglas Gregor4afa39d2009-01-20 01:17:11 +000070 // Make sure to emit all elements of a Decl.
Chris Lattner682bf922009-03-29 16:50:03 +000071 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
72 Builder->EmitTopLevelDecl(*I);
Argyrios Kyrtzidis88c25962011-11-18 00:26:59 +000073 return true;
Chris Lattner8ee3c032008-02-06 02:01:47 +000074 }
Daniel Dunbar41071de2008-08-15 23:26:23 +000075
Chris Lattner9eee0f82008-02-06 04:51:19 +000076 /// HandleTagDeclDefinition - This callback is invoked each time a TagDecl
Chris Lattner682bf922009-03-29 16:50:03 +000077 /// to (e.g. struct, union, enum, class) is completed. This allows the
78 /// client hack on the type, which can occur at any point in the file
79 /// (because these can be defined in declspecs).
Chris Lattner9eee0f82008-02-06 04:51:19 +000080 virtual void HandleTagDeclDefinition(TagDecl *D) {
Chris Lattnerc5b88062008-02-06 05:08:19 +000081 Builder->UpdateCompletedType(D);
Douglas Gregorf552c202011-02-15 18:11:42 +000082
83 // In C++, we may have member functions that need to be emitted at this
84 // point.
David Blaikie4e4d0842012-03-11 07:00:24 +000085 if (Ctx->getLangOpts().CPlusPlus && !D->isDependentContext()) {
Douglas Gregorf552c202011-02-15 18:11:42 +000086 for (DeclContext::decl_iterator M = D->decls_begin(),
87 MEnd = D->decls_end();
88 M != MEnd; ++M)
89 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(*M))
Sean Hunt10620eb2011-05-06 20:44:56 +000090 if (Method->doesThisDeclarationHaveABody() &&
Douglas Gregor944ed3b2011-02-19 21:54:50 +000091 (Method->hasAttr<UsedAttr>() ||
92 Method->hasAttr<ConstructorAttr>()))
Douglas Gregorf552c202011-02-15 18:11:42 +000093 Builder->EmitTopLevelDecl(Method);
94 }
Chris Lattner9eee0f82008-02-06 04:51:19 +000095 }
Ted Kremenek159346a2008-08-07 19:47:41 +000096
David Blaikie658cd2c2013-07-13 21:08:14 +000097 virtual void HandleTagDeclRequiredDefinition(const TagDecl *D) LLVM_OVERRIDE {
98 if (CodeGen::CGDebugInfo *DI = Builder->getModuleDebugInfo())
99 if (const RecordDecl *RD = dyn_cast<RecordDecl>(D))
100 DI->completeFwdDecl(*RD);
101 }
102
Chris Lattnerdacbc5d2009-03-28 04:11:33 +0000103 virtual void HandleTranslationUnit(ASTContext &Ctx) {
Ted Kremenek159346a2008-08-07 19:47:41 +0000104 if (Diags.hasErrorOccurred()) {
105 M.reset();
106 return;
107 }
108
109 if (Builder)
110 Builder->Release();
Daniel Dunbar7177dee2009-12-19 17:50:07 +0000111 }
Douglas Gregorb6c8c8b2009-04-21 17:11:58 +0000112
113 virtual void CompleteTentativeDefinition(VarDecl *D) {
114 if (Diags.hasErrorOccurred())
115 return;
116
117 Builder->EmitTentativeDefinition(D);
118 }
Douglas Gregor6fb745b2010-05-13 16:44:06 +0000119
120 virtual void HandleVTable(CXXRecordDecl *RD, bool DefinitionRequired) {
121 if (Diags.hasErrorOccurred())
122 return;
123
124 Builder->EmitVTable(RD, DefinitionRequired);
125 }
Reid Kleckner3190ca92013-05-08 13:44:39 +0000126
127 virtual void HandleLinkerOptionPragma(llvm::StringRef Opts) {
128 Builder->AppendLinkerOptions(Opts);
129 }
130
Aaron Ballmana7ff62f2013-06-04 02:07:14 +0000131 virtual void HandleDetectMismatch(llvm::StringRef Name,
132 llvm::StringRef Value) {
133 Builder->AddDetectMismatch(Name, Value);
134 }
135
Reid Kleckner3190ca92013-05-08 13:44:39 +0000136 virtual void HandleDependentLibrary(llvm::StringRef Lib) {
137 Builder->AddDependentLib(Lib);
138 }
Chris Lattner8ee3c032008-02-06 02:01:47 +0000139 };
Reid Spencer5f016e22007-07-11 17:01:13 +0000140}
141
David Blaikie99ba9e32011-12-20 02:48:34 +0000142void CodeGenerator::anchor() { }
143
David Blaikied6471f72011-09-25 23:23:43 +0000144CodeGenerator *clang::CreateLLVMCodeGen(DiagnosticsEngine &Diags,
Ted Kremenek815c78f2008-08-05 18:50:11 +0000145 const std::string& ModuleName,
Chandler Carruth2811ccf2009-11-12 17:24:48 +0000146 const CodeGenOptions &CGO,
John McCall3abae092013-04-16 22:48:20 +0000147 const TargetOptions &/*TO*/,
Owen Anderson8f1ca782009-07-01 23:14:14 +0000148 llvm::LLVMContext& C) {
John McCall3abae092013-04-16 22:48:20 +0000149 return new CodeGeneratorImpl(Diags, ModuleName, CGO, C);
Reid Spencer5f016e22007-07-11 17:01:13 +0000150}