blob: 56067a47bbdc6f55f14c826fa6512902cf9cf4ac [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"
Chris Lattnerbed31442007-05-28 01:07:47 +000015#include "CodeGenModule.h"
Chris Lattneradf1f512008-02-06 02:01:47 +000016#include "clang/AST/ASTContext.h"
Daniel Dunbar221fa942008-08-11 04:54:23 +000017#include "clang/AST/DeclObjC.h"
18#include "clang/AST/Expr.h"
Chris Lattneradf1f512008-02-06 02:01:47 +000019#include "clang/Basic/Diagnostic.h"
20#include "clang/Basic/TargetInfo.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000021#include "clang/Frontend/CodeGenOptions.h"
22#include "llvm/ADT/OwningPtr.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"
Chris Lattner984fac52009-03-26 05:00:52 +000027using namespace clang;
Ted Kremenek2c674f62008-08-05 18:50:11 +000028
Chris Lattneradf1f512008-02-06 02:01:47 +000029namespace {
Benjamin Kramer337e3a52009-11-28 19:45:26 +000030 class CodeGeneratorImpl : public CodeGenerator {
David Blaikie9c902b52011-09-25 23:23:43 +000031 DiagnosticsEngine &Diags;
Micah Villmowdd31ca12012-10-08 16:25:52 +000032 OwningPtr<const llvm::DataLayout> TD;
Chris Lattneradf1f512008-02-06 02:01:47 +000033 ASTContext *Ctx;
Chandler Carruthbc55fe22009-11-12 17:24:48 +000034 const CodeGenOptions CodeGenOpts; // Intentionally copied in.
Chris Lattneradf1f512008-02-06 02:01:47 +000035 protected:
Dylan Noblesmithe2778992012-02-05 02:12:40 +000036 OwningPtr<llvm::Module> M;
37 OwningPtr<CodeGen::CodeGenModule> Builder;
Chris Lattneradf1f512008-02-06 02:01:47 +000038 public:
David Blaikie9c902b52011-09-25 23:23:43 +000039 CodeGeneratorImpl(DiagnosticsEngine &diags, const std::string& ModuleName,
John McCall568d4102013-04-16 22:48:20 +000040 const CodeGenOptions &CGO, llvm::LLVMContext& C)
41 : Diags(diags), CodeGenOpts(CGO),
Bill Wendlingc86a2f32013-02-14 08:09:20 +000042 M(new llvm::Module(ModuleName, C)) {}
Mike Stump11289f42009-09-09 15:08:12 +000043
Ted Kremenek2c674f62008-08-05 18:50:11 +000044 virtual ~CodeGeneratorImpl() {}
Mike Stump11289f42009-09-09 15:08:12 +000045
Daniel Dunbar30c514e2008-10-21 19:55:09 +000046 virtual llvm::Module* GetModule() {
47 return M.get();
48 }
Mike Stump11289f42009-09-09 15:08:12 +000049
Ted Kremenek7db4f602008-08-07 19:47:41 +000050 virtual llvm::Module* ReleaseModule() {
Ted Kremenek2c674f62008-08-05 18:50:11 +000051 return M.take();
Chris Lattneradf1f512008-02-06 02:01:47 +000052 }
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));
Chris Lattneradf1f512008-02-06 02:01:47 +000062 }
Mike Stump11289f42009-09-09 15:08:12 +000063
Rafael Espindoladf88f6f2012-03-08 15:51:03 +000064 virtual void HandleCXXStaticMemberVarInstantiation(VarDecl *VD) {
65 Builder->HandleCXXStaticMemberVarInstantiation(VD);
Rafael Espindola189fa742012-03-05 10:54:55 +000066 }
67
Argyrios Kyrtzidis841dd882011-11-18 00:26:59 +000068 virtual bool HandleTopLevelDecl(DeclGroupRef DG) {
Douglas Gregor6e6ad602009-01-20 01:17:11 +000069 // Make sure to emit all elements of a Decl.
Chris Lattner5bbb3c82009-03-29 16:50:03 +000070 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
71 Builder->EmitTopLevelDecl(*I);
Argyrios Kyrtzidis841dd882011-11-18 00:26:59 +000072 return true;
Chris Lattneradf1f512008-02-06 02:01:47 +000073 }
Daniel Dunbarfce4be82008-08-15 23:26:23 +000074
Chris Lattnera5e4d302008-02-06 04:51:19 +000075 /// HandleTagDeclDefinition - This callback is invoked each time a TagDecl
Chris Lattner5bbb3c82009-03-29 16:50:03 +000076 /// to (e.g. struct, union, enum, class) is completed. This allows the
77 /// client hack on the type, which can occur at any point in the file
78 /// (because these can be defined in declspecs).
Chris Lattnera5e4d302008-02-06 04:51:19 +000079 virtual void HandleTagDeclDefinition(TagDecl *D) {
Chris Lattner68be6062008-02-06 05:08:19 +000080 Builder->UpdateCompletedType(D);
Douglas Gregore0610152011-02-15 18:11:42 +000081
82 // In C++, we may have member functions that need to be emitted at this
83 // point.
David Blaikiebbafb8a2012-03-11 07:00:24 +000084 if (Ctx->getLangOpts().CPlusPlus && !D->isDependentContext()) {
Douglas Gregore0610152011-02-15 18:11:42 +000085 for (DeclContext::decl_iterator M = D->decls_begin(),
86 MEnd = D->decls_end();
87 M != MEnd; ++M)
88 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(*M))
Alexis Hunt4a8ea102011-05-06 20:44:56 +000089 if (Method->doesThisDeclarationHaveABody() &&
Douglas Gregord66828d2011-02-19 21:54:50 +000090 (Method->hasAttr<UsedAttr>() ||
91 Method->hasAttr<ConstructorAttr>()))
Douglas Gregore0610152011-02-15 18:11:42 +000092 Builder->EmitTopLevelDecl(Method);
93 }
Chris Lattnera5e4d302008-02-06 04:51:19 +000094 }
Ted Kremenek7db4f602008-08-07 19:47:41 +000095
Chris Lattnercf169832009-03-28 04:11:33 +000096 virtual void HandleTranslationUnit(ASTContext &Ctx) {
Ted Kremenek7db4f602008-08-07 19:47:41 +000097 if (Diags.hasErrorOccurred()) {
98 M.reset();
99 return;
100 }
101
102 if (Builder)
103 Builder->Release();
Daniel Dunbare017ecc2009-12-19 17:50:07 +0000104 }
Douglas Gregorbeecd582009-04-21 17:11:58 +0000105
106 virtual void CompleteTentativeDefinition(VarDecl *D) {
107 if (Diags.hasErrorOccurred())
108 return;
109
110 Builder->EmitTentativeDefinition(D);
111 }
Douglas Gregor88d292c2010-05-13 16:44:06 +0000112
113 virtual void HandleVTable(CXXRecordDecl *RD, bool DefinitionRequired) {
114 if (Diags.hasErrorOccurred())
115 return;
116
117 Builder->EmitVTable(RD, DefinitionRequired);
118 }
Reid Klecknere43f0fe2013-05-08 13:44:39 +0000119
120 virtual void HandleLinkerOptionPragma(llvm::StringRef Opts) {
121 Builder->AppendLinkerOptions(Opts);
122 }
123
124 virtual void HandleDependentLibrary(llvm::StringRef Lib) {
125 Builder->AddDependentLib(Lib);
126 }
Chris Lattneradf1f512008-02-06 02:01:47 +0000127 };
Chris Lattnerf97fe382007-05-24 06:29:05 +0000128}
129
David Blaikie68e081d2011-12-20 02:48:34 +0000130void CodeGenerator::anchor() { }
131
David Blaikie9c902b52011-09-25 23:23:43 +0000132CodeGenerator *clang::CreateLLVMCodeGen(DiagnosticsEngine &Diags,
Ted Kremenek2c674f62008-08-05 18:50:11 +0000133 const std::string& ModuleName,
Chandler Carruthbc55fe22009-11-12 17:24:48 +0000134 const CodeGenOptions &CGO,
John McCall568d4102013-04-16 22:48:20 +0000135 const TargetOptions &/*TO*/,
Owen Andersonecaeaa82009-07-01 23:14:14 +0000136 llvm::LLVMContext& C) {
John McCall568d4102013-04-16 22:48:20 +0000137 return new CodeGeneratorImpl(Diags, ModuleName, CGO, C);
Chris Lattnerf97fe382007-05-24 06:29:05 +0000138}