blob: b654df744c50b95d67e30857486d807f45259876 [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"
Chris Lattner4b009652007-07-25 00:24:17 +000018using namespace clang;
19
Chris Lattnerf5e9db02008-02-06 02:01:47 +000020//===----------------------------------------------------------------------===//
21// LLVM Emitter
Chris Lattner4b009652007-07-25 00:24:17 +000022
Chris Lattnerf5e9db02008-02-06 02:01:47 +000023#include "clang/Basic/Diagnostic.h"
24#include "clang/Basic/TargetInfo.h"
25#include "clang/CodeGen/ModuleBuilder.h"
26#include "llvm/Module.h"
27#include "llvm/Target/TargetData.h"
28#include "llvm/Target/TargetMachine.h"
Ted Kremenek7c65b6c2008-08-05 18:50:11 +000029#include "llvm/Support/Compiler.h"
30#include "llvm/ADT/OwningPtr.h"
31
Chris Lattnerf5e9db02008-02-06 02:01:47 +000032
33namespace {
Ted Kremenek7c65b6c2008-08-05 18:50:11 +000034 class VISIBILITY_HIDDEN CodeGeneratorImpl : public CodeGenerator {
Chris Lattnerf5e9db02008-02-06 02:01:47 +000035 Diagnostic &Diags;
Ted Kremenek7c65b6c2008-08-05 18:50:11 +000036 llvm::OwningPtr<const llvm::TargetData> TD;
Chris Lattnerf5e9db02008-02-06 02:01:47 +000037 ASTContext *Ctx;
38 const LangOptions &Features;
Sanjiv Gupta40e56a12008-05-08 08:54:20 +000039 bool GenerateDebugInfo;
Chris Lattnerf5e9db02008-02-06 02:01:47 +000040 protected:
Ted Kremenek7c65b6c2008-08-05 18:50:11 +000041 llvm::OwningPtr<llvm::Module> M;
42 llvm::OwningPtr<CodeGen::CodeGenModule> Builder;
Chris Lattnerf5e9db02008-02-06 02:01:47 +000043 public:
Ted Kremenek7c65b6c2008-08-05 18:50:11 +000044 CodeGeneratorImpl(Diagnostic &diags, const LangOptions &LO,
45 const std::string& ModuleName,
46 bool DebugInfoFlag)
Sanjiv Gupta40e56a12008-05-08 08:54:20 +000047 : Diags(diags), Features(LO), GenerateDebugInfo(DebugInfoFlag),
Ted Kremenek7c65b6c2008-08-05 18:50:11 +000048 M(new llvm::Module(ModuleName)) {}
Chris Lattnerf5e9db02008-02-06 02:01:47 +000049
Ted Kremenek7c65b6c2008-08-05 18:50:11 +000050 virtual ~CodeGeneratorImpl() {}
51
Ted Kremenek2866ea42008-08-07 19:47:41 +000052 virtual llvm::Module* ReleaseModule() {
Ted Kremenek7c65b6c2008-08-05 18:50:11 +000053 return M.take();
Chris Lattnerf5e9db02008-02-06 02:01:47 +000054 }
55
56 virtual void Initialize(ASTContext &Context) {
57 Ctx = &Context;
58
59 M->setTargetTriple(Ctx->Target.getTargetTriple());
60 M->setDataLayout(Ctx->Target.getTargetDescription());
Ted Kremenek7c65b6c2008-08-05 18:50:11 +000061 TD.reset(new llvm::TargetData(Ctx->Target.getTargetDescription()));
62 Builder.reset(new CodeGen::CodeGenModule(Context, Features, *M, *TD,
Daniel Dunbar8c85fac2008-08-11 02:45:11 +000063 Diags, GenerateDebugInfo,
64 false));
Chris Lattnerf5e9db02008-02-06 02:01:47 +000065 }
66
67 virtual void HandleTopLevelDecl(Decl *D) {
68 // If an error occurred, stop code generation, but continue parsing and
69 // semantic analysis (to ensure all warnings and errors are emitted).
70 if (Diags.hasErrorOccurred())
71 return;
Daniel Dunbarf7bacd32008-07-29 17:47:36 +000072
Chris Lattnerf5e9db02008-02-06 02:01:47 +000073 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Daniel Dunbar7bf5b3d2008-07-29 23:18:29 +000074 Builder->EmitGlobal(FD);
75 } else if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
76 Builder->EmitGlobal(VD);
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +000077 } else if (isa<ObjCClassDecl>(D)){
78 //Forward declaration. Only used for type checking.
79 } else if (ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(D)){
80 // Generate Protocol object.
81 Builder->EmitObjCProtocolImplementation(PD);
82 } else if (isa<ObjCCategoryDecl>(D)){
83 //Only used for typechecking.
84 } else if (ObjCCategoryImplDecl *OCD = dyn_cast<ObjCCategoryImplDecl>(D)){
85 // Generate methods, attach to category structure
86 Builder->EmitObjCCategoryImpl(OCD);
87 } else if (ObjCImplementationDecl * OID =
88 dyn_cast<ObjCImplementationDecl>(D)){
89 // Generate methods, attach to class structure
90 Builder->EmitObjCClassImplementation(OID);
91 } else if (isa<ObjCInterfaceDecl>(D)){
92 // Ignore - generated when the implementation decl is CodeGen'd
93 } else if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(D)){
94 Builder->EmitObjCMethod(OMD);
Chris Lattnerb326b172008-03-30 23:03:07 +000095 } else if (isa<ObjCClassDecl>(D) || isa<ObjCCategoryDecl>(D)) {
96 // Forward declaration. Only used for type checking.
97 } else if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(D)){
98 Builder->EmitObjCMethod(OMD);
Chris Lattnerf5e9db02008-02-06 02:01:47 +000099 } else if (LinkageSpecDecl *LSD = dyn_cast<LinkageSpecDecl>(D)) {
100 if (LSD->getLanguage() == LinkageSpecDecl::lang_cxx)
101 Builder->WarnUnsupported(LSD, "linkage spec");
102 // FIXME: implement C++ linkage, C linkage works mostly by C
103 // language reuse already.
Anders Carlsson4f7f4412008-02-08 00:33:21 +0000104 } else if (FileScopeAsmDecl *AD = dyn_cast<FileScopeAsmDecl>(D)) {
105 std::string AsmString(AD->getAsmString()->getStrData(),
106 AD->getAsmString()->getByteLength());
107
108 const std::string &S = Builder->getModule().getModuleInlineAsm();
109 if (S.empty())
110 Builder->getModule().setModuleInlineAsm(AsmString);
111 else
112 Builder->getModule().setModuleInlineAsm(S + '\n' + AsmString);
Chris Lattnerf5e9db02008-02-06 02:01:47 +0000113 } else {
Chris Lattner08994a52008-02-06 04:51:19 +0000114 assert(isa<TypeDecl>(D) && "Unknown top level decl");
115 // TODO: handle debug info?
Chris Lattnerf5e9db02008-02-06 02:01:47 +0000116 }
Daniel Dunbarf7bacd32008-07-29 17:47:36 +0000117
118 if (ScopedDecl *SD = dyn_cast<ScopedDecl>(D)) {
119 SD = SD->getNextDeclarator();
120 if (SD)
121 HandleTopLevelDecl(SD);
122 }
Chris Lattnerf5e9db02008-02-06 02:01:47 +0000123 }
Chris Lattner08994a52008-02-06 04:51:19 +0000124
125 /// HandleTagDeclDefinition - This callback is invoked each time a TagDecl
Nico Weber91e5f892008-08-09 23:08:17 +0000126 /// (e.g. struct, union, enum, class) is completed. This allows the client to
Chris Lattner08994a52008-02-06 04:51:19 +0000127 /// hack on the type, which can occur at any point in the file (because these
128 /// can be defined in declspecs).
129 virtual void HandleTagDeclDefinition(TagDecl *D) {
Chris Lattner9ec3ca22008-02-06 05:08:19 +0000130 Builder->UpdateCompletedType(D);
Chris Lattner08994a52008-02-06 04:51:19 +0000131 }
Ted Kremenek2866ea42008-08-07 19:47:41 +0000132
133 virtual void HandleTranslationUnit(TranslationUnit& TU) {
134 if (Diags.hasErrorOccurred()) {
135 M.reset();
136 return;
137 }
138
139 if (Builder)
140 Builder->Release();
141 };
Chris Lattnerf5e9db02008-02-06 02:01:47 +0000142 };
Chris Lattner4b009652007-07-25 00:24:17 +0000143}
144
Ted Kremenek7c65b6c2008-08-05 18:50:11 +0000145CodeGenerator *clang::CreateLLVMCodeGen(Diagnostic &Diags,
146 const LangOptions &Features,
147 const std::string& ModuleName,
148 bool GenerateDebugInfo) {
149 return new CodeGeneratorImpl(Diags, Features, ModuleName, GenerateDebugInfo);
Chris Lattner4b009652007-07-25 00:24:17 +0000150}