blob: 176d39e85d2af12f812099a1baba101931d40805 [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +00001//===--- ModuleBuilder.cpp - Emit LLVM Code from ASTs ---------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-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 Lattner4b009652007-07-25 00:24:17 +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"
Chris Lattnerf5e9db02008-02-06 02:01:47 +000016#include "clang/AST/ASTContext.h"
Chris Lattner806a5f52008-01-12 07:05:38 +000017#include "clang/AST/Decl.h"
Daniel Dunbarde300732008-08-11 04:54:23 +000018#include "clang/AST/DeclObjC.h"
19#include "clang/AST/Expr.h"
Chris Lattner4b009652007-07-25 00:24:17 +000020using namespace clang;
21
Chris Lattnerf5e9db02008-02-06 02:01:47 +000022//===----------------------------------------------------------------------===//
23// LLVM Emitter
Chris Lattner4b009652007-07-25 00:24:17 +000024
Chris Lattnerf5e9db02008-02-06 02:01:47 +000025#include "clang/Basic/Diagnostic.h"
26#include "clang/Basic/TargetInfo.h"
27#include "clang/CodeGen/ModuleBuilder.h"
28#include "llvm/Module.h"
29#include "llvm/Target/TargetData.h"
30#include "llvm/Target/TargetMachine.h"
Ted Kremenek7c65b6c2008-08-05 18:50:11 +000031#include "llvm/Support/Compiler.h"
32#include "llvm/ADT/OwningPtr.h"
33
Chris Lattnerf5e9db02008-02-06 02:01:47 +000034
35namespace {
Ted Kremenek7c65b6c2008-08-05 18:50:11 +000036 class VISIBILITY_HIDDEN CodeGeneratorImpl : public CodeGenerator {
Chris Lattnerf5e9db02008-02-06 02:01:47 +000037 Diagnostic &Diags;
Ted Kremenek7c65b6c2008-08-05 18:50:11 +000038 llvm::OwningPtr<const llvm::TargetData> TD;
Chris Lattnerf5e9db02008-02-06 02:01:47 +000039 ASTContext *Ctx;
40 const LangOptions &Features;
Sanjiv Gupta40e56a12008-05-08 08:54:20 +000041 bool GenerateDebugInfo;
Chris Lattnerf5e9db02008-02-06 02:01:47 +000042 protected:
Ted Kremenek7c65b6c2008-08-05 18:50:11 +000043 llvm::OwningPtr<llvm::Module> M;
44 llvm::OwningPtr<CodeGen::CodeGenModule> Builder;
Chris Lattnerf5e9db02008-02-06 02:01:47 +000045 public:
Ted Kremenek7c65b6c2008-08-05 18:50:11 +000046 CodeGeneratorImpl(Diagnostic &diags, const LangOptions &LO,
47 const std::string& ModuleName,
48 bool DebugInfoFlag)
Sanjiv Gupta40e56a12008-05-08 08:54:20 +000049 : Diags(diags), Features(LO), GenerateDebugInfo(DebugInfoFlag),
Ted Kremenek7c65b6c2008-08-05 18:50:11 +000050 M(new llvm::Module(ModuleName)) {}
Chris Lattnerf5e9db02008-02-06 02:01:47 +000051
Ted Kremenek7c65b6c2008-08-05 18:50:11 +000052 virtual ~CodeGeneratorImpl() {}
53
Ted Kremenek2866ea42008-08-07 19:47:41 +000054 virtual llvm::Module* ReleaseModule() {
Ted Kremenek7c65b6c2008-08-05 18:50:11 +000055 return M.take();
Chris Lattnerf5e9db02008-02-06 02:01:47 +000056 }
57
58 virtual void Initialize(ASTContext &Context) {
59 Ctx = &Context;
60
61 M->setTargetTriple(Ctx->Target.getTargetTriple());
62 M->setDataLayout(Ctx->Target.getTargetDescription());
Ted Kremenek7c65b6c2008-08-05 18:50:11 +000063 TD.reset(new llvm::TargetData(Ctx->Target.getTargetDescription()));
64 Builder.reset(new CodeGen::CodeGenModule(Context, Features, *M, *TD,
Daniel Dunbar8c85fac2008-08-11 02:45:11 +000065 Diags, GenerateDebugInfo,
66 false));
Chris Lattnerf5e9db02008-02-06 02:01:47 +000067 }
68
69 virtual void HandleTopLevelDecl(Decl *D) {
70 // If an error occurred, stop code generation, but continue parsing and
71 // semantic analysis (to ensure all warnings and errors are emitted).
72 if (Diags.hasErrorOccurred())
73 return;
Daniel Dunbarf7bacd32008-07-29 17:47:36 +000074
Chris Lattnerf5e9db02008-02-06 02:01:47 +000075 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Daniel Dunbar7bf5b3d2008-07-29 23:18:29 +000076 Builder->EmitGlobal(FD);
77 } else if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
78 Builder->EmitGlobal(VD);
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +000079 } else if (isa<ObjCClassDecl>(D)){
80 //Forward declaration. Only used for type checking.
81 } else if (ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(D)){
82 // Generate Protocol object.
83 Builder->EmitObjCProtocolImplementation(PD);
84 } else if (isa<ObjCCategoryDecl>(D)){
85 //Only used for typechecking.
86 } else if (ObjCCategoryImplDecl *OCD = dyn_cast<ObjCCategoryImplDecl>(D)){
87 // Generate methods, attach to category structure
88 Builder->EmitObjCCategoryImpl(OCD);
89 } else if (ObjCImplementationDecl * OID =
90 dyn_cast<ObjCImplementationDecl>(D)){
91 // Generate methods, attach to class structure
92 Builder->EmitObjCClassImplementation(OID);
93 } else if (isa<ObjCInterfaceDecl>(D)){
94 // Ignore - generated when the implementation decl is CodeGen'd
95 } else if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(D)){
96 Builder->EmitObjCMethod(OMD);
Chris Lattnerb326b172008-03-30 23:03:07 +000097 } else if (isa<ObjCClassDecl>(D) || isa<ObjCCategoryDecl>(D)) {
98 // Forward declaration. Only used for type checking.
99 } else if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(D)){
100 Builder->EmitObjCMethod(OMD);
Chris Lattnerf5e9db02008-02-06 02:01:47 +0000101 } else if (LinkageSpecDecl *LSD = dyn_cast<LinkageSpecDecl>(D)) {
102 if (LSD->getLanguage() == LinkageSpecDecl::lang_cxx)
103 Builder->WarnUnsupported(LSD, "linkage spec");
104 // FIXME: implement C++ linkage, C linkage works mostly by C
105 // language reuse already.
Anders Carlsson4f7f4412008-02-08 00:33:21 +0000106 } else if (FileScopeAsmDecl *AD = dyn_cast<FileScopeAsmDecl>(D)) {
107 std::string AsmString(AD->getAsmString()->getStrData(),
108 AD->getAsmString()->getByteLength());
109
110 const std::string &S = Builder->getModule().getModuleInlineAsm();
111 if (S.empty())
112 Builder->getModule().setModuleInlineAsm(AsmString);
113 else
114 Builder->getModule().setModuleInlineAsm(S + '\n' + AsmString);
Chris Lattnerf5e9db02008-02-06 02:01:47 +0000115 } else {
Chris Lattner08994a52008-02-06 04:51:19 +0000116 assert(isa<TypeDecl>(D) && "Unknown top level decl");
117 // TODO: handle debug info?
Chris Lattnerf5e9db02008-02-06 02:01:47 +0000118 }
Daniel Dunbarf7bacd32008-07-29 17:47:36 +0000119
120 if (ScopedDecl *SD = dyn_cast<ScopedDecl>(D)) {
121 SD = SD->getNextDeclarator();
122 if (SD)
123 HandleTopLevelDecl(SD);
124 }
Chris Lattnerf5e9db02008-02-06 02:01:47 +0000125 }
Chris Lattner08994a52008-02-06 04:51:19 +0000126
127 /// HandleTagDeclDefinition - This callback is invoked each time a TagDecl
Nico Weber91e5f892008-08-09 23:08:17 +0000128 /// (e.g. struct, union, enum, class) is completed. This allows the client to
Chris Lattner08994a52008-02-06 04:51:19 +0000129 /// hack on the type, which can occur at any point in the file (because these
130 /// can be defined in declspecs).
131 virtual void HandleTagDeclDefinition(TagDecl *D) {
Chris Lattner9ec3ca22008-02-06 05:08:19 +0000132 Builder->UpdateCompletedType(D);
Chris Lattner08994a52008-02-06 04:51:19 +0000133 }
Ted Kremenek2866ea42008-08-07 19:47:41 +0000134
135 virtual void HandleTranslationUnit(TranslationUnit& TU) {
136 if (Diags.hasErrorOccurred()) {
137 M.reset();
138 return;
139 }
140
141 if (Builder)
142 Builder->Release();
143 };
Chris Lattnerf5e9db02008-02-06 02:01:47 +0000144 };
Chris Lattner4b009652007-07-25 00:24:17 +0000145}
146
Ted Kremenek7c65b6c2008-08-05 18:50:11 +0000147CodeGenerator *clang::CreateLLVMCodeGen(Diagnostic &Diags,
148 const LangOptions &Features,
149 const std::string& ModuleName,
150 bool GenerateDebugInfo) {
151 return new CodeGeneratorImpl(Diags, Features, ModuleName, GenerateDebugInfo);
Chris Lattner4b009652007-07-25 00:24:17 +0000152}