blob: 7329ed1d90d067a69bda1c7f4a8d0eafd56d48ec [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"
Chris Lattner8ee3c032008-02-06 02:01:47 +000016#include "clang/AST/ASTContext.h"
Chris Lattnerc6fdc342008-01-12 07:05:38 +000017#include "clang/AST/Decl.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000018using namespace clang;
19
Chris Lattner8ee3c032008-02-06 02:01:47 +000020//===----------------------------------------------------------------------===//
21// LLVM Emitter
Reid Spencer5f016e22007-07-11 17:01:13 +000022
Chris Lattner8ee3c032008-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 Kremenek815c78f2008-08-05 18:50:11 +000029#include "llvm/Support/Compiler.h"
30#include "llvm/ADT/OwningPtr.h"
31
Chris Lattner8ee3c032008-02-06 02:01:47 +000032
33namespace {
Ted Kremenek815c78f2008-08-05 18:50:11 +000034 class VISIBILITY_HIDDEN CodeGeneratorImpl : public CodeGenerator {
Chris Lattner8ee3c032008-02-06 02:01:47 +000035 Diagnostic &Diags;
Ted Kremenek815c78f2008-08-05 18:50:11 +000036 llvm::OwningPtr<const llvm::TargetData> TD;
Chris Lattner8ee3c032008-02-06 02:01:47 +000037 ASTContext *Ctx;
38 const LangOptions &Features;
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000039 bool GenerateDebugInfo;
Chris Lattner8ee3c032008-02-06 02:01:47 +000040 protected:
Ted Kremenek815c78f2008-08-05 18:50:11 +000041 llvm::OwningPtr<llvm::Module> M;
42 llvm::OwningPtr<CodeGen::CodeGenModule> Builder;
Chris Lattner8ee3c032008-02-06 02:01:47 +000043 public:
Ted Kremenek815c78f2008-08-05 18:50:11 +000044 CodeGeneratorImpl(Diagnostic &diags, const LangOptions &LO,
45 const std::string& ModuleName,
46 bool DebugInfoFlag)
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000047 : Diags(diags), Features(LO), GenerateDebugInfo(DebugInfoFlag),
Ted Kremenek815c78f2008-08-05 18:50:11 +000048 M(new llvm::Module(ModuleName)) {}
Chris Lattner8ee3c032008-02-06 02:01:47 +000049
Ted Kremenek815c78f2008-08-05 18:50:11 +000050 virtual ~CodeGeneratorImpl() {}
51
52 virtual llvm::Module* ReleaseModule() {
53 if (Diags.hasErrorOccurred())
54 return 0;
55
56 if (Builder)
57 Builder->Release();
58
59 return M.take();
Chris Lattner8ee3c032008-02-06 02:01:47 +000060 }
61
62 virtual void Initialize(ASTContext &Context) {
63 Ctx = &Context;
64
65 M->setTargetTriple(Ctx->Target.getTargetTriple());
66 M->setDataLayout(Ctx->Target.getTargetDescription());
Ted Kremenek815c78f2008-08-05 18:50:11 +000067 TD.reset(new llvm::TargetData(Ctx->Target.getTargetDescription()));
68 Builder.reset(new CodeGen::CodeGenModule(Context, Features, *M, *TD,
69 Diags, GenerateDebugInfo));
Chris Lattner8ee3c032008-02-06 02:01:47 +000070 }
71
72 virtual void HandleTopLevelDecl(Decl *D) {
73 // If an error occurred, stop code generation, but continue parsing and
74 // semantic analysis (to ensure all warnings and errors are emitted).
75 if (Diags.hasErrorOccurred())
76 return;
Daniel Dunbar58a7a262008-07-29 17:47:36 +000077
Chris Lattner8ee3c032008-02-06 02:01:47 +000078 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Daniel Dunbarbd012ff2008-07-29 23:18:29 +000079 Builder->EmitGlobal(FD);
80 } else if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
81 Builder->EmitGlobal(VD);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +000082 } else if (isa<ObjCClassDecl>(D)){
83 //Forward declaration. Only used for type checking.
84 } else if (ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(D)){
85 // Generate Protocol object.
86 Builder->EmitObjCProtocolImplementation(PD);
87 } else if (isa<ObjCCategoryDecl>(D)){
88 //Only used for typechecking.
89 } else if (ObjCCategoryImplDecl *OCD = dyn_cast<ObjCCategoryImplDecl>(D)){
90 // Generate methods, attach to category structure
91 Builder->EmitObjCCategoryImpl(OCD);
92 } else if (ObjCImplementationDecl * OID =
93 dyn_cast<ObjCImplementationDecl>(D)){
94 // Generate methods, attach to class structure
95 Builder->EmitObjCClassImplementation(OID);
96 } else if (isa<ObjCInterfaceDecl>(D)){
97 // Ignore - generated when the implementation decl is CodeGen'd
98 } else if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(D)){
99 Builder->EmitObjCMethod(OMD);
Chris Lattner391d77a2008-03-30 23:03:07 +0000100 } else if (isa<ObjCClassDecl>(D) || isa<ObjCCategoryDecl>(D)) {
101 // Forward declaration. Only used for type checking.
102 } else if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(D)){
103 Builder->EmitObjCMethod(OMD);
Chris Lattner8ee3c032008-02-06 02:01:47 +0000104 } else if (LinkageSpecDecl *LSD = dyn_cast<LinkageSpecDecl>(D)) {
105 if (LSD->getLanguage() == LinkageSpecDecl::lang_cxx)
106 Builder->WarnUnsupported(LSD, "linkage spec");
107 // FIXME: implement C++ linkage, C linkage works mostly by C
108 // language reuse already.
Anders Carlssondfab6cb2008-02-08 00:33:21 +0000109 } else if (FileScopeAsmDecl *AD = dyn_cast<FileScopeAsmDecl>(D)) {
110 std::string AsmString(AD->getAsmString()->getStrData(),
111 AD->getAsmString()->getByteLength());
112
113 const std::string &S = Builder->getModule().getModuleInlineAsm();
114 if (S.empty())
115 Builder->getModule().setModuleInlineAsm(AsmString);
116 else
117 Builder->getModule().setModuleInlineAsm(S + '\n' + AsmString);
Chris Lattner8ee3c032008-02-06 02:01:47 +0000118 } else {
Chris Lattner9eee0f82008-02-06 04:51:19 +0000119 assert(isa<TypeDecl>(D) && "Unknown top level decl");
120 // TODO: handle debug info?
Chris Lattner8ee3c032008-02-06 02:01:47 +0000121 }
Daniel Dunbar58a7a262008-07-29 17:47:36 +0000122
123 if (ScopedDecl *SD = dyn_cast<ScopedDecl>(D)) {
124 SD = SD->getNextDeclarator();
125 if (SD)
126 HandleTopLevelDecl(SD);
127 }
Chris Lattner8ee3c032008-02-06 02:01:47 +0000128 }
Chris Lattner9eee0f82008-02-06 04:51:19 +0000129
130 /// HandleTagDeclDefinition - This callback is invoked each time a TagDecl
131 /// (e.g. struct, union, enum, class) is completed. This allows the client to
132 /// hack on the type, which can occur at any point in the file (because these
133 /// can be defined in declspecs).
134 virtual void HandleTagDeclDefinition(TagDecl *D) {
Chris Lattnerc5b88062008-02-06 05:08:19 +0000135 Builder->UpdateCompletedType(D);
Chris Lattner9eee0f82008-02-06 04:51:19 +0000136 }
137
Chris Lattner8ee3c032008-02-06 02:01:47 +0000138 };
Reid Spencer5f016e22007-07-11 17:01:13 +0000139}
140
Ted Kremenek815c78f2008-08-05 18:50:11 +0000141CodeGenerator *clang::CreateLLVMCodeGen(Diagnostic &Diags,
142 const LangOptions &Features,
143 const std::string& ModuleName,
144 bool GenerateDebugInfo) {
145 return new CodeGeneratorImpl(Diags, Features, ModuleName, GenerateDebugInfo);
Reid Spencer5f016e22007-07-11 17:01:13 +0000146}