blob: b37b8d3f01b8cc2f4d4862bb01ca18736981c834 [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/ASTConsumer.h"
17#include "clang/AST/ASTContext.h"
Chris Lattner806a5f52008-01-12 07:05:38 +000018#include "clang/AST/Decl.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"
26#include "clang/CodeGen/ModuleBuilder.h"
27#include "llvm/Module.h"
28#include "llvm/Target/TargetData.h"
29#include "llvm/Target/TargetMachine.h"
30
31namespace {
32 class CodeGenerator : public ASTConsumer {
33 Diagnostic &Diags;
34 const llvm::TargetData *TD;
35 ASTContext *Ctx;
36 const LangOptions &Features;
Sanjiv Gupta40e56a12008-05-08 08:54:20 +000037 bool GenerateDebugInfo;
Chris Lattnerf5e9db02008-02-06 02:01:47 +000038 protected:
39 llvm::Module *&M;
40 CodeGen::CodeGenModule *Builder;
41 public:
42 CodeGenerator(Diagnostic &diags, const LangOptions &LO,
Sanjiv Gupta40e56a12008-05-08 08:54:20 +000043 llvm::Module *&DestModule, bool DebugInfoFlag)
44 : Diags(diags), Features(LO), GenerateDebugInfo(DebugInfoFlag),
45 M(DestModule) {}
Chris Lattnerf5e9db02008-02-06 02:01:47 +000046
47 ~CodeGenerator() {
48 delete Builder;
Eli Friedman9f5a6a92008-05-27 04:22:24 +000049 delete TD;
Chris Lattnerf5e9db02008-02-06 02:01:47 +000050 }
51
52 virtual void Initialize(ASTContext &Context) {
53 Ctx = &Context;
54
55 M->setTargetTriple(Ctx->Target.getTargetTriple());
56 M->setDataLayout(Ctx->Target.getTargetDescription());
57 TD = new llvm::TargetData(Ctx->Target.getTargetDescription());
Sanjiv Gupta40e56a12008-05-08 08:54:20 +000058 Builder = new CodeGen::CodeGenModule(Context, Features, *M, *TD, Diags,
59 GenerateDebugInfo);
Chris Lattnerf5e9db02008-02-06 02:01:47 +000060 }
61
62 virtual void HandleTopLevelDecl(Decl *D) {
63 // If an error occurred, stop code generation, but continue parsing and
64 // semantic analysis (to ensure all warnings and errors are emitted).
65 if (Diags.hasErrorOccurred())
66 return;
67
68 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
69 Builder->EmitFunction(FD);
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +000070 } else if (isa<ObjCClassDecl>(D)){
71 //Forward declaration. Only used for type checking.
72 } else if (ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(D)){
73 // Generate Protocol object.
74 Builder->EmitObjCProtocolImplementation(PD);
75 } else if (isa<ObjCCategoryDecl>(D)){
76 //Only used for typechecking.
77 } else if (ObjCCategoryImplDecl *OCD = dyn_cast<ObjCCategoryImplDecl>(D)){
78 // Generate methods, attach to category structure
79 Builder->EmitObjCCategoryImpl(OCD);
80 } else if (ObjCImplementationDecl * OID =
81 dyn_cast<ObjCImplementationDecl>(D)){
82 // Generate methods, attach to class structure
83 Builder->EmitObjCClassImplementation(OID);
84 } else if (isa<ObjCInterfaceDecl>(D)){
85 // Ignore - generated when the implementation decl is CodeGen'd
86 } else if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(D)){
87 Builder->EmitObjCMethod(OMD);
Steve Naroff72a6ebc2008-04-15 22:42:06 +000088 } else if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
89 if (VD->isFileVarDecl())
90 Builder->EmitGlobalVarDeclarator(VD);
Chris Lattnerb326b172008-03-30 23:03:07 +000091 } else if (isa<ObjCClassDecl>(D) || isa<ObjCCategoryDecl>(D)) {
92 // Forward declaration. Only used for type checking.
93 } else if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(D)){
94 Builder->EmitObjCMethod(OMD);
Chris Lattnerf5e9db02008-02-06 02:01:47 +000095 } else if (LinkageSpecDecl *LSD = dyn_cast<LinkageSpecDecl>(D)) {
96 if (LSD->getLanguage() == LinkageSpecDecl::lang_cxx)
97 Builder->WarnUnsupported(LSD, "linkage spec");
98 // FIXME: implement C++ linkage, C linkage works mostly by C
99 // language reuse already.
Anders Carlsson4f7f4412008-02-08 00:33:21 +0000100 } else if (FileScopeAsmDecl *AD = dyn_cast<FileScopeAsmDecl>(D)) {
101 std::string AsmString(AD->getAsmString()->getStrData(),
102 AD->getAsmString()->getByteLength());
103
104 const std::string &S = Builder->getModule().getModuleInlineAsm();
105 if (S.empty())
106 Builder->getModule().setModuleInlineAsm(AsmString);
107 else
108 Builder->getModule().setModuleInlineAsm(S + '\n' + AsmString);
Chris Lattnerf5e9db02008-02-06 02:01:47 +0000109 } else {
Chris Lattner08994a52008-02-06 04:51:19 +0000110 assert(isa<TypeDecl>(D) && "Unknown top level decl");
111 // TODO: handle debug info?
Chris Lattnerf5e9db02008-02-06 02:01:47 +0000112 }
113 }
Chris Lattner08994a52008-02-06 04:51:19 +0000114
115 /// HandleTagDeclDefinition - This callback is invoked each time a TagDecl
116 /// (e.g. struct, union, enum, class) is completed. This allows the client to
117 /// hack on the type, which can occur at any point in the file (because these
118 /// can be defined in declspecs).
119 virtual void HandleTagDeclDefinition(TagDecl *D) {
Chris Lattner9ec3ca22008-02-06 05:08:19 +0000120 Builder->UpdateCompletedType(D);
Chris Lattner08994a52008-02-06 04:51:19 +0000121 }
122
Chris Lattnerf5e9db02008-02-06 02:01:47 +0000123 };
Chris Lattner4b009652007-07-25 00:24:17 +0000124}
125
Chris Lattnerf5e9db02008-02-06 02:01:47 +0000126ASTConsumer *clang::CreateLLVMCodeGen(Diagnostic &Diags,
127 const LangOptions &Features,
Sanjiv Gupta40e56a12008-05-08 08:54:20 +0000128 llvm::Module *&DestModule,
129 bool GenerateDebugInfo) {
130 return new CodeGenerator(Diags, Features, DestModule, GenerateDebugInfo);
Chris Lattner4b009652007-07-25 00:24:17 +0000131}
132