1 //===--- ModuleBuilder.cpp - Emit LLVM Code from ASTs ---------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This builds an AST and converts it to LLVM Code.
12 //===----------------------------------------------------------------------===//
14 #include "clang/CodeGen/ModuleBuilder.h"
15 #include "CodeGenModule.h"
16 #include "clang/AST/ASTContext.h"
17 #include "clang/AST/Decl.h"
18 using namespace clang;
20 //===----------------------------------------------------------------------===//
23 #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"
29 #include "llvm/Support/Compiler.h"
30 #include "llvm/ADT/OwningPtr.h"
34 class VISIBILITY_HIDDEN CodeGeneratorImpl : public CodeGenerator {
36 llvm::OwningPtr<const llvm::TargetData> TD;
38 const LangOptions &Features;
39 bool GenerateDebugInfo;
41 llvm::OwningPtr<llvm::Module> M;
42 llvm::OwningPtr<CodeGen::CodeGenModule> Builder;
44 CodeGeneratorImpl(Diagnostic &diags, const LangOptions &LO,
45 const std::string& ModuleName,
47 : Diags(diags), Features(LO), GenerateDebugInfo(DebugInfoFlag),
48 M(new llvm::Module(ModuleName)) {}
50 virtual ~CodeGeneratorImpl() {}
52 virtual llvm::Module* ReleaseModule() {
56 virtual void Initialize(ASTContext &Context) {
59 M->setTargetTriple(Ctx->Target.getTargetTriple());
60 M->setDataLayout(Ctx->Target.getTargetDescription());
61 TD.reset(new llvm::TargetData(Ctx->Target.getTargetDescription()));
62 Builder.reset(new CodeGen::CodeGenModule(Context, Features, *M, *TD,
63 Diags, GenerateDebugInfo,
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())
73 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
74 Builder->EmitGlobal(FD);
75 } else if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
76 Builder->EmitGlobal(VD);
77 } 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);
95 } 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);
99 } 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.
104 } else if (FileScopeAsmDecl *AD = dyn_cast<FileScopeAsmDecl>(D)) {
105 std::string AsmString(AD->getAsmString()->getStrData(),
106 AD->getAsmString()->getByteLength());
108 const std::string &S = Builder->getModule().getModuleInlineAsm();
110 Builder->getModule().setModuleInlineAsm(AsmString);
112 Builder->getModule().setModuleInlineAsm(S + '\n' + AsmString);
114 assert(isa<TypeDecl>(D) && "Unknown top level decl");
115 // TODO: handle debug info?
118 if (ScopedDecl *SD = dyn_cast<ScopedDecl>(D)) {
119 SD = SD->getNextDeclarator();
121 HandleTopLevelDecl(SD);
125 /// HandleTagDeclDefinition - This callback is invoked each time a TagDecl
126 /// (e.g. struct, union, enum, class) is completed. This allows the client to
127 /// 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) {
130 Builder->UpdateCompletedType(D);
133 virtual void HandleTranslationUnit(TranslationUnit& TU) {
134 if (Diags.hasErrorOccurred()) {
145 CodeGenerator *clang::CreateLLVMCodeGen(Diagnostic &Diags,
146 const LangOptions &Features,
147 const std::string& ModuleName,
148 bool GenerateDebugInfo) {
149 return new CodeGeneratorImpl(Diags, Features, ModuleName, GenerateDebugInfo);