blob: 30572ed9af24c6366889ade447a81753237469c2 [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"
Chandler Carruth06057ce2010-06-15 23:19:56 +000016#include "clang/Frontend/CodeGenOptions.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"
Owen Anderson42253cc2009-07-01 17:00:06 +000022#include "llvm/LLVMContext.h"
Chris Lattner8ee3c032008-02-06 02:01:47 +000023#include "llvm/Module.h"
24#include "llvm/Target/TargetData.h"
Ted Kremenek815c78f2008-08-05 18:50:11 +000025#include "llvm/ADT/OwningPtr.h"
Chris Lattnerbd360642009-03-26 05:00:52 +000026using namespace clang;
Ted Kremenek815c78f2008-08-05 18:50:11 +000027
Chris Lattner8ee3c032008-02-06 02:01:47 +000028namespace {
Benjamin Kramer85b45212009-11-28 19:45:26 +000029 class CodeGeneratorImpl : public CodeGenerator {
David Blaikied6471f72011-09-25 23:23:43 +000030 DiagnosticsEngine &Diags;
Ted Kremenek815c78f2008-08-05 18:50:11 +000031 llvm::OwningPtr<const llvm::TargetData> TD;
Chris Lattner8ee3c032008-02-06 02:01:47 +000032 ASTContext *Ctx;
Chandler Carruth2811ccf2009-11-12 17:24:48 +000033 const CodeGenOptions CodeGenOpts; // Intentionally copied in.
Chris Lattner8ee3c032008-02-06 02:01:47 +000034 protected:
Ted Kremenek815c78f2008-08-05 18:50:11 +000035 llvm::OwningPtr<llvm::Module> M;
36 llvm::OwningPtr<CodeGen::CodeGenModule> Builder;
Chris Lattner8ee3c032008-02-06 02:01:47 +000037 public:
David Blaikied6471f72011-09-25 23:23:43 +000038 CodeGeneratorImpl(DiagnosticsEngine &diags, const std::string& ModuleName,
John McCall468ec6c2010-03-04 04:29:44 +000039 const CodeGenOptions &CGO, llvm::LLVMContext& C)
40 : Diags(diags), CodeGenOpts(CGO), M(new llvm::Module(ModuleName, C)) {}
Mike Stump1eb44332009-09-09 15:08:12 +000041
Ted Kremenek815c78f2008-08-05 18:50:11 +000042 virtual ~CodeGeneratorImpl() {}
Mike Stump1eb44332009-09-09 15:08:12 +000043
Daniel Dunbard8c0ea12008-10-21 19:55:09 +000044 virtual llvm::Module* GetModule() {
45 return M.get();
46 }
Mike Stump1eb44332009-09-09 15:08:12 +000047
Ted Kremenek159346a2008-08-07 19:47:41 +000048 virtual llvm::Module* ReleaseModule() {
Ted Kremenek815c78f2008-08-05 18:50:11 +000049 return M.take();
Chris Lattner8ee3c032008-02-06 02:01:47 +000050 }
Mike Stump1eb44332009-09-09 15:08:12 +000051
Chris Lattner8ee3c032008-02-06 02:01:47 +000052 virtual void Initialize(ASTContext &Context) {
53 Ctx = &Context;
Mike Stump1eb44332009-09-09 15:08:12 +000054
Douglas Gregorbcfd1f52011-09-02 00:18:52 +000055 M->setTargetTriple(Ctx->getTargetInfo().getTriple().getTriple());
56 M->setDataLayout(Ctx->getTargetInfo().getTargetDescription());
57 TD.reset(new llvm::TargetData(Ctx->getTargetInfo().getTargetDescription()));
Chandler Carruth2811ccf2009-11-12 17:24:48 +000058 Builder.reset(new CodeGen::CodeGenModule(Context, CodeGenOpts,
John McCall468ec6c2010-03-04 04:29:44 +000059 *M, *TD, Diags));
Chris Lattner8ee3c032008-02-06 02:01:47 +000060 }
Mike Stump1eb44332009-09-09 15:08:12 +000061
Argyrios Kyrtzidis88c25962011-11-18 00:26:59 +000062 virtual bool HandleTopLevelDecl(DeclGroupRef DG) {
Douglas Gregor4afa39d2009-01-20 01:17:11 +000063 // Make sure to emit all elements of a Decl.
Chris Lattner682bf922009-03-29 16:50:03 +000064 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
65 Builder->EmitTopLevelDecl(*I);
Argyrios Kyrtzidis88c25962011-11-18 00:26:59 +000066 return true;
Chris Lattner8ee3c032008-02-06 02:01:47 +000067 }
Daniel Dunbar41071de2008-08-15 23:26:23 +000068
Chris Lattner9eee0f82008-02-06 04:51:19 +000069 /// HandleTagDeclDefinition - This callback is invoked each time a TagDecl
Chris Lattner682bf922009-03-29 16:50:03 +000070 /// to (e.g. struct, union, enum, class) is completed. This allows the
71 /// client hack on the type, which can occur at any point in the file
72 /// (because these can be defined in declspecs).
Chris Lattner9eee0f82008-02-06 04:51:19 +000073 virtual void HandleTagDeclDefinition(TagDecl *D) {
Chris Lattnerc5b88062008-02-06 05:08:19 +000074 Builder->UpdateCompletedType(D);
Douglas Gregorf552c202011-02-15 18:11:42 +000075
76 // In C++, we may have member functions that need to be emitted at this
77 // point.
78 if (Ctx->getLangOptions().CPlusPlus && !D->isDependentContext()) {
79 for (DeclContext::decl_iterator M = D->decls_begin(),
80 MEnd = D->decls_end();
81 M != MEnd; ++M)
82 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(*M))
Sean Hunt10620eb2011-05-06 20:44:56 +000083 if (Method->doesThisDeclarationHaveABody() &&
Douglas Gregor944ed3b2011-02-19 21:54:50 +000084 (Method->hasAttr<UsedAttr>() ||
85 Method->hasAttr<ConstructorAttr>()))
Douglas Gregorf552c202011-02-15 18:11:42 +000086 Builder->EmitTopLevelDecl(Method);
87 }
Chris Lattner9eee0f82008-02-06 04:51:19 +000088 }
Ted Kremenek159346a2008-08-07 19:47:41 +000089
Chris Lattnerdacbc5d2009-03-28 04:11:33 +000090 virtual void HandleTranslationUnit(ASTContext &Ctx) {
Ted Kremenek159346a2008-08-07 19:47:41 +000091 if (Diags.hasErrorOccurred()) {
92 M.reset();
93 return;
94 }
95
96 if (Builder)
97 Builder->Release();
Daniel Dunbar7177dee2009-12-19 17:50:07 +000098 }
Douglas Gregorb6c8c8b2009-04-21 17:11:58 +000099
100 virtual void CompleteTentativeDefinition(VarDecl *D) {
101 if (Diags.hasErrorOccurred())
102 return;
103
104 Builder->EmitTentativeDefinition(D);
105 }
Douglas Gregor6fb745b2010-05-13 16:44:06 +0000106
107 virtual void HandleVTable(CXXRecordDecl *RD, bool DefinitionRequired) {
108 if (Diags.hasErrorOccurred())
109 return;
110
111 Builder->EmitVTable(RD, DefinitionRequired);
112 }
Chris Lattner8ee3c032008-02-06 02:01:47 +0000113 };
Reid Spencer5f016e22007-07-11 17:01:13 +0000114}
115
David Blaikied6471f72011-09-25 23:23:43 +0000116CodeGenerator *clang::CreateLLVMCodeGen(DiagnosticsEngine &Diags,
Ted Kremenek815c78f2008-08-05 18:50:11 +0000117 const std::string& ModuleName,
Chandler Carruth2811ccf2009-11-12 17:24:48 +0000118 const CodeGenOptions &CGO,
Owen Anderson8f1ca782009-07-01 23:14:14 +0000119 llvm::LLVMContext& C) {
John McCall468ec6c2010-03-04 04:29:44 +0000120 return new CodeGeneratorImpl(Diags, ModuleName, CGO, C);
Reid Spencer5f016e22007-07-11 17:01:13 +0000121}