blob: 17ac0023f8dc665efafaef7968b4eace50849bd5 [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"
Daniel Dunbarde300732008-08-11 04:54:23 +000017#include "clang/AST/DeclObjC.h"
18#include "clang/AST/Expr.h"
Chris Lattner4b009652007-07-25 00:24:17 +000019using namespace clang;
20
Chris Lattnerf5e9db02008-02-06 02:01:47 +000021//===----------------------------------------------------------------------===//
22// LLVM Emitter
Chris Lattner4b009652007-07-25 00:24:17 +000023
Chris Lattnerf5e9db02008-02-06 02:01:47 +000024#include "clang/Basic/Diagnostic.h"
25#include "clang/Basic/TargetInfo.h"
Chris Lattnerf5e9db02008-02-06 02:01:47 +000026#include "llvm/Module.h"
27#include "llvm/Target/TargetData.h"
Ted Kremenek7c65b6c2008-08-05 18:50:11 +000028#include "llvm/Support/Compiler.h"
29#include "llvm/ADT/OwningPtr.h"
30
Chris Lattnerf5e9db02008-02-06 02:01:47 +000031
32namespace {
Ted Kremenek7c65b6c2008-08-05 18:50:11 +000033 class VISIBILITY_HIDDEN CodeGeneratorImpl : public CodeGenerator {
Chris Lattnerf5e9db02008-02-06 02:01:47 +000034 Diagnostic &Diags;
Ted Kremenek7c65b6c2008-08-05 18:50:11 +000035 llvm::OwningPtr<const llvm::TargetData> TD;
Chris Lattnerf5e9db02008-02-06 02:01:47 +000036 ASTContext *Ctx;
37 const LangOptions &Features;
Sanjiv Gupta40e56a12008-05-08 08:54:20 +000038 bool GenerateDebugInfo;
Chris Lattnerf5e9db02008-02-06 02:01:47 +000039 protected:
Ted Kremenek7c65b6c2008-08-05 18:50:11 +000040 llvm::OwningPtr<llvm::Module> M;
41 llvm::OwningPtr<CodeGen::CodeGenModule> Builder;
Chris Lattnerf5e9db02008-02-06 02:01:47 +000042 public:
Ted Kremenek7c65b6c2008-08-05 18:50:11 +000043 CodeGeneratorImpl(Diagnostic &diags, const LangOptions &LO,
44 const std::string& ModuleName,
45 bool DebugInfoFlag)
Sanjiv Gupta40e56a12008-05-08 08:54:20 +000046 : Diags(diags), Features(LO), GenerateDebugInfo(DebugInfoFlag),
Ted Kremenek7c65b6c2008-08-05 18:50:11 +000047 M(new llvm::Module(ModuleName)) {}
Chris Lattnerf5e9db02008-02-06 02:01:47 +000048
Ted Kremenek7c65b6c2008-08-05 18:50:11 +000049 virtual ~CodeGeneratorImpl() {}
50
Ted Kremenek2866ea42008-08-07 19:47:41 +000051 virtual llvm::Module* ReleaseModule() {
Ted Kremenek7c65b6c2008-08-05 18:50:11 +000052 return M.take();
Chris Lattnerf5e9db02008-02-06 02:01:47 +000053 }
54
55 virtual void Initialize(ASTContext &Context) {
56 Ctx = &Context;
57
58 M->setTargetTriple(Ctx->Target.getTargetTriple());
59 M->setDataLayout(Ctx->Target.getTargetDescription());
Ted Kremenek7c65b6c2008-08-05 18:50:11 +000060 TD.reset(new llvm::TargetData(Ctx->Target.getTargetDescription()));
61 Builder.reset(new CodeGen::CodeGenModule(Context, Features, *M, *TD,
Daniel Dunbar1be1df32008-08-11 21:35:06 +000062 Diags, GenerateDebugInfo));
Chris Lattnerf5e9db02008-02-06 02:01:47 +000063 }
64
65 virtual void HandleTopLevelDecl(Decl *D) {
66 // If an error occurred, stop code generation, but continue parsing and
67 // semantic analysis (to ensure all warnings and errors are emitted).
68 if (Diags.hasErrorOccurred())
69 return;
Daniel Dunbarf7bacd32008-07-29 17:47:36 +000070
Chris Lattnerf5e9db02008-02-06 02:01:47 +000071 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Daniel Dunbar7bf5b3d2008-07-29 23:18:29 +000072 Builder->EmitGlobal(FD);
73 } else if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
74 Builder->EmitGlobal(VD);
Daniel Dunbar84bb85f2008-08-13 00:59:25 +000075 } else if (isa<ObjCClassDecl>(D) || isa<ObjCForwardProtocolDecl>(D)) {
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +000076 //Forward declaration. Only used for type checking.
77 } else if (ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(D)){
78 // Generate Protocol object.
79 Builder->EmitObjCProtocolImplementation(PD);
80 } else if (isa<ObjCCategoryDecl>(D)){
81 //Only used for typechecking.
82 } else if (ObjCCategoryImplDecl *OCD = dyn_cast<ObjCCategoryImplDecl>(D)){
83 // Generate methods, attach to category structure
84 Builder->EmitObjCCategoryImpl(OCD);
85 } else if (ObjCImplementationDecl * OID =
86 dyn_cast<ObjCImplementationDecl>(D)){
87 // Generate methods, attach to class structure
88 Builder->EmitObjCClassImplementation(OID);
89 } else if (isa<ObjCInterfaceDecl>(D)){
90 // Ignore - generated when the implementation decl is CodeGen'd
91 } else if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(D)){
92 Builder->EmitObjCMethod(OMD);
Chris Lattnerb326b172008-03-30 23:03:07 +000093 } else if (isa<ObjCClassDecl>(D) || isa<ObjCCategoryDecl>(D)) {
94 // Forward declaration. Only used for type checking.
95 } else if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(D)){
96 Builder->EmitObjCMethod(OMD);
Chris Lattnerf5e9db02008-02-06 02:01:47 +000097 } else if (LinkageSpecDecl *LSD = dyn_cast<LinkageSpecDecl>(D)) {
98 if (LSD->getLanguage() == LinkageSpecDecl::lang_cxx)
99 Builder->WarnUnsupported(LSD, "linkage spec");
100 // FIXME: implement C++ linkage, C linkage works mostly by C
101 // language reuse already.
Anders Carlsson4f7f4412008-02-08 00:33:21 +0000102 } else if (FileScopeAsmDecl *AD = dyn_cast<FileScopeAsmDecl>(D)) {
103 std::string AsmString(AD->getAsmString()->getStrData(),
104 AD->getAsmString()->getByteLength());
105
106 const std::string &S = Builder->getModule().getModuleInlineAsm();
107 if (S.empty())
108 Builder->getModule().setModuleInlineAsm(AsmString);
109 else
110 Builder->getModule().setModuleInlineAsm(S + '\n' + AsmString);
Chris Lattnerf5e9db02008-02-06 02:01:47 +0000111 } else {
Chris Lattner08994a52008-02-06 04:51:19 +0000112 assert(isa<TypeDecl>(D) && "Unknown top level decl");
113 // TODO: handle debug info?
Chris Lattnerf5e9db02008-02-06 02:01:47 +0000114 }
Daniel Dunbarf7bacd32008-07-29 17:47:36 +0000115
116 if (ScopedDecl *SD = dyn_cast<ScopedDecl>(D)) {
117 SD = SD->getNextDeclarator();
118 if (SD)
119 HandleTopLevelDecl(SD);
120 }
Chris Lattnerf5e9db02008-02-06 02:01:47 +0000121 }
Chris Lattner08994a52008-02-06 04:51:19 +0000122
123 /// HandleTagDeclDefinition - This callback is invoked each time a TagDecl
Nico Weber91e5f892008-08-09 23:08:17 +0000124 /// (e.g. struct, union, enum, class) is completed. This allows the client to
Chris Lattner08994a52008-02-06 04:51:19 +0000125 /// hack on the type, which can occur at any point in the file (because these
126 /// can be defined in declspecs).
127 virtual void HandleTagDeclDefinition(TagDecl *D) {
Chris Lattner9ec3ca22008-02-06 05:08:19 +0000128 Builder->UpdateCompletedType(D);
Chris Lattner08994a52008-02-06 04:51:19 +0000129 }
Ted Kremenek2866ea42008-08-07 19:47:41 +0000130
131 virtual void HandleTranslationUnit(TranslationUnit& TU) {
132 if (Diags.hasErrorOccurred()) {
133 M.reset();
134 return;
135 }
136
137 if (Builder)
138 Builder->Release();
139 };
Chris Lattnerf5e9db02008-02-06 02:01:47 +0000140 };
Chris Lattner4b009652007-07-25 00:24:17 +0000141}
142
Ted Kremenek7c65b6c2008-08-05 18:50:11 +0000143CodeGenerator *clang::CreateLLVMCodeGen(Diagnostic &Diags,
144 const LangOptions &Features,
145 const std::string& ModuleName,
146 bool GenerateDebugInfo) {
147 return new CodeGeneratorImpl(Diags, Features, ModuleName, GenerateDebugInfo);
Chris Lattner4b009652007-07-25 00:24:17 +0000148}