Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1 | //===--- ModuleBuilder.cpp - Emit LLVM Code from ASTs ---------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 959e5be | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 7 | // |
| 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 Lattner | f5e9db0 | 2008-02-06 02:01:47 +0000 | [diff] [blame] | 16 | #include "clang/AST/ASTContext.h" |
Chris Lattner | 806a5f5 | 2008-01-12 07:05:38 +0000 | [diff] [blame] | 17 | #include "clang/AST/Decl.h" |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 18 | using namespace clang; |
| 19 | |
Chris Lattner | f5e9db0 | 2008-02-06 02:01:47 +0000 | [diff] [blame] | 20 | //===----------------------------------------------------------------------===// |
| 21 | // LLVM Emitter |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 22 | |
Chris Lattner | f5e9db0 | 2008-02-06 02:01:47 +0000 | [diff] [blame] | 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" |
Ted Kremenek | 7c65b6c | 2008-08-05 18:50:11 +0000 | [diff] [blame] | 29 | #include "llvm/Support/Compiler.h" |
| 30 | #include "llvm/ADT/OwningPtr.h" |
| 31 | |
Chris Lattner | f5e9db0 | 2008-02-06 02:01:47 +0000 | [diff] [blame] | 32 | |
| 33 | namespace { |
Ted Kremenek | 7c65b6c | 2008-08-05 18:50:11 +0000 | [diff] [blame] | 34 | class VISIBILITY_HIDDEN CodeGeneratorImpl : public CodeGenerator { |
Chris Lattner | f5e9db0 | 2008-02-06 02:01:47 +0000 | [diff] [blame] | 35 | Diagnostic &Diags; |
Ted Kremenek | 7c65b6c | 2008-08-05 18:50:11 +0000 | [diff] [blame] | 36 | llvm::OwningPtr<const llvm::TargetData> TD; |
Chris Lattner | f5e9db0 | 2008-02-06 02:01:47 +0000 | [diff] [blame] | 37 | ASTContext *Ctx; |
| 38 | const LangOptions &Features; |
Sanjiv Gupta | 40e56a1 | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 39 | bool GenerateDebugInfo; |
Chris Lattner | f5e9db0 | 2008-02-06 02:01:47 +0000 | [diff] [blame] | 40 | protected: |
Ted Kremenek | 7c65b6c | 2008-08-05 18:50:11 +0000 | [diff] [blame] | 41 | llvm::OwningPtr<llvm::Module> M; |
| 42 | llvm::OwningPtr<CodeGen::CodeGenModule> Builder; |
Chris Lattner | f5e9db0 | 2008-02-06 02:01:47 +0000 | [diff] [blame] | 43 | public: |
Ted Kremenek | 7c65b6c | 2008-08-05 18:50:11 +0000 | [diff] [blame] | 44 | CodeGeneratorImpl(Diagnostic &diags, const LangOptions &LO, |
| 45 | const std::string& ModuleName, |
| 46 | bool DebugInfoFlag) |
Sanjiv Gupta | 40e56a1 | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 47 | : Diags(diags), Features(LO), GenerateDebugInfo(DebugInfoFlag), |
Ted Kremenek | 7c65b6c | 2008-08-05 18:50:11 +0000 | [diff] [blame] | 48 | M(new llvm::Module(ModuleName)) {} |
Chris Lattner | f5e9db0 | 2008-02-06 02:01:47 +0000 | [diff] [blame] | 49 | |
Ted Kremenek | 7c65b6c | 2008-08-05 18:50:11 +0000 | [diff] [blame] | 50 | virtual ~CodeGeneratorImpl() {} |
| 51 | |
Ted Kremenek | 2866ea4 | 2008-08-07 19:47:41 +0000 | [diff] [blame] | 52 | virtual llvm::Module* ReleaseModule() { |
Ted Kremenek | 7c65b6c | 2008-08-05 18:50:11 +0000 | [diff] [blame] | 53 | return M.take(); |
Chris Lattner | f5e9db0 | 2008-02-06 02:01:47 +0000 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | virtual void Initialize(ASTContext &Context) { |
| 57 | Ctx = &Context; |
| 58 | |
| 59 | M->setTargetTriple(Ctx->Target.getTargetTriple()); |
| 60 | M->setDataLayout(Ctx->Target.getTargetDescription()); |
Ted Kremenek | 7c65b6c | 2008-08-05 18:50:11 +0000 | [diff] [blame] | 61 | TD.reset(new llvm::TargetData(Ctx->Target.getTargetDescription())); |
| 62 | Builder.reset(new CodeGen::CodeGenModule(Context, Features, *M, *TD, |
| 63 | Diags, GenerateDebugInfo)); |
Chris Lattner | f5e9db0 | 2008-02-06 02:01:47 +0000 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | virtual void HandleTopLevelDecl(Decl *D) { |
| 67 | // If an error occurred, stop code generation, but continue parsing and |
| 68 | // semantic analysis (to ensure all warnings and errors are emitted). |
| 69 | if (Diags.hasErrorOccurred()) |
| 70 | return; |
Daniel Dunbar | f7bacd3 | 2008-07-29 17:47:36 +0000 | [diff] [blame] | 71 | |
Chris Lattner | f5e9db0 | 2008-02-06 02:01:47 +0000 | [diff] [blame] | 72 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Daniel Dunbar | 7bf5b3d | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 73 | Builder->EmitGlobal(FD); |
| 74 | } else if (VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 75 | Builder->EmitGlobal(VD); |
Anton Korobeynikov | cd5d08d | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 76 | } else if (isa<ObjCClassDecl>(D)){ |
| 77 | //Forward declaration. Only used for type checking. |
| 78 | } else if (ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(D)){ |
| 79 | // Generate Protocol object. |
| 80 | Builder->EmitObjCProtocolImplementation(PD); |
| 81 | } else if (isa<ObjCCategoryDecl>(D)){ |
| 82 | //Only used for typechecking. |
| 83 | } else if (ObjCCategoryImplDecl *OCD = dyn_cast<ObjCCategoryImplDecl>(D)){ |
| 84 | // Generate methods, attach to category structure |
| 85 | Builder->EmitObjCCategoryImpl(OCD); |
| 86 | } else if (ObjCImplementationDecl * OID = |
| 87 | dyn_cast<ObjCImplementationDecl>(D)){ |
| 88 | // Generate methods, attach to class structure |
| 89 | Builder->EmitObjCClassImplementation(OID); |
| 90 | } else if (isa<ObjCInterfaceDecl>(D)){ |
| 91 | // Ignore - generated when the implementation decl is CodeGen'd |
| 92 | } else if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(D)){ |
| 93 | Builder->EmitObjCMethod(OMD); |
Chris Lattner | b326b17 | 2008-03-30 23:03:07 +0000 | [diff] [blame] | 94 | } else if (isa<ObjCClassDecl>(D) || isa<ObjCCategoryDecl>(D)) { |
| 95 | // Forward declaration. Only used for type checking. |
| 96 | } else if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(D)){ |
| 97 | Builder->EmitObjCMethod(OMD); |
Chris Lattner | f5e9db0 | 2008-02-06 02:01:47 +0000 | [diff] [blame] | 98 | } else if (LinkageSpecDecl *LSD = dyn_cast<LinkageSpecDecl>(D)) { |
| 99 | if (LSD->getLanguage() == LinkageSpecDecl::lang_cxx) |
| 100 | Builder->WarnUnsupported(LSD, "linkage spec"); |
| 101 | // FIXME: implement C++ linkage, C linkage works mostly by C |
| 102 | // language reuse already. |
Anders Carlsson | 4f7f441 | 2008-02-08 00:33:21 +0000 | [diff] [blame] | 103 | } else if (FileScopeAsmDecl *AD = dyn_cast<FileScopeAsmDecl>(D)) { |
| 104 | std::string AsmString(AD->getAsmString()->getStrData(), |
| 105 | AD->getAsmString()->getByteLength()); |
| 106 | |
| 107 | const std::string &S = Builder->getModule().getModuleInlineAsm(); |
| 108 | if (S.empty()) |
| 109 | Builder->getModule().setModuleInlineAsm(AsmString); |
| 110 | else |
| 111 | Builder->getModule().setModuleInlineAsm(S + '\n' + AsmString); |
Chris Lattner | f5e9db0 | 2008-02-06 02:01:47 +0000 | [diff] [blame] | 112 | } else { |
Chris Lattner | 08994a5 | 2008-02-06 04:51:19 +0000 | [diff] [blame] | 113 | assert(isa<TypeDecl>(D) && "Unknown top level decl"); |
| 114 | // TODO: handle debug info? |
Chris Lattner | f5e9db0 | 2008-02-06 02:01:47 +0000 | [diff] [blame] | 115 | } |
Daniel Dunbar | f7bacd3 | 2008-07-29 17:47:36 +0000 | [diff] [blame] | 116 | |
| 117 | if (ScopedDecl *SD = dyn_cast<ScopedDecl>(D)) { |
| 118 | SD = SD->getNextDeclarator(); |
| 119 | if (SD) |
| 120 | HandleTopLevelDecl(SD); |
| 121 | } |
Chris Lattner | f5e9db0 | 2008-02-06 02:01:47 +0000 | [diff] [blame] | 122 | } |
Chris Lattner | 08994a5 | 2008-02-06 04:51:19 +0000 | [diff] [blame] | 123 | |
| 124 | /// HandleTagDeclDefinition - This callback is invoked each time a TagDecl |
Nico Weber | 91e5f89 | 2008-08-09 23:08:17 +0000 | [diff] [blame] | 125 | /// (e.g. struct, union, enum, class) is completed. This allows the client to |
Chris Lattner | 08994a5 | 2008-02-06 04:51:19 +0000 | [diff] [blame] | 126 | /// hack on the type, which can occur at any point in the file (because these |
| 127 | /// can be defined in declspecs). |
| 128 | virtual void HandleTagDeclDefinition(TagDecl *D) { |
Chris Lattner | 9ec3ca2 | 2008-02-06 05:08:19 +0000 | [diff] [blame] | 129 | Builder->UpdateCompletedType(D); |
Chris Lattner | 08994a5 | 2008-02-06 04:51:19 +0000 | [diff] [blame] | 130 | } |
Ted Kremenek | 2866ea4 | 2008-08-07 19:47:41 +0000 | [diff] [blame] | 131 | |
| 132 | virtual void HandleTranslationUnit(TranslationUnit& TU) { |
| 133 | if (Diags.hasErrorOccurred()) { |
| 134 | M.reset(); |
| 135 | return; |
| 136 | } |
| 137 | |
| 138 | if (Builder) |
| 139 | Builder->Release(); |
| 140 | }; |
Chris Lattner | f5e9db0 | 2008-02-06 02:01:47 +0000 | [diff] [blame] | 141 | }; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 142 | } |
| 143 | |
Ted Kremenek | 7c65b6c | 2008-08-05 18:50:11 +0000 | [diff] [blame] | 144 | CodeGenerator *clang::CreateLLVMCodeGen(Diagnostic &Diags, |
| 145 | const LangOptions &Features, |
| 146 | const std::string& ModuleName, |
| 147 | bool GenerateDebugInfo) { |
| 148 | return new CodeGeneratorImpl(Diags, Features, ModuleName, GenerateDebugInfo); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 149 | } |