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 "CGDebugInfo.h"
16 #include "CodeGenModule.h"
17 #include "clang/AST/ASTContext.h"
18 #include "clang/AST/DeclObjC.h"
19 #include "clang/AST/Expr.h"
20 #include "clang/Basic/Diagnostic.h"
21 #include "clang/Basic/TargetInfo.h"
22 #include "clang/Frontend/CodeGenOptions.h"
23 #include "llvm/ADT/OwningPtr.h"
24 #include "llvm/ADT/StringRef.h"
25 #include "llvm/IR/DataLayout.h"
26 #include "llvm/IR/LLVMContext.h"
27 #include "llvm/IR/Module.h"
28 using namespace clang;
31 class CodeGeneratorImpl : public CodeGenerator {
32 DiagnosticsEngine &Diags;
33 OwningPtr<const llvm::DataLayout> TD;
35 const CodeGenOptions CodeGenOpts; // Intentionally copied in.
37 OwningPtr<llvm::Module> M;
38 OwningPtr<CodeGen::CodeGenModule> Builder;
40 CodeGeneratorImpl(DiagnosticsEngine &diags, const std::string& ModuleName,
41 const CodeGenOptions &CGO, llvm::LLVMContext& C)
42 : Diags(diags), CodeGenOpts(CGO),
43 M(new llvm::Module(ModuleName, C)) {}
45 virtual ~CodeGeneratorImpl() {}
47 virtual llvm::Module* GetModule() {
51 virtual llvm::Module* ReleaseModule() {
55 virtual void Initialize(ASTContext &Context) {
58 M->setTargetTriple(Ctx->getTargetInfo().getTriple().getTriple());
59 M->setDataLayout(Ctx->getTargetInfo().getTargetDescription());
60 TD.reset(new llvm::DataLayout(Ctx->getTargetInfo().getTargetDescription()));
61 Builder.reset(new CodeGen::CodeGenModule(Context, CodeGenOpts, *M, *TD,
64 for (size_t i = 0, e = CodeGenOpts.DependentLibraries.size(); i < e; ++i)
65 HandleDependentLibrary(CodeGenOpts.DependentLibraries[i]);
68 virtual void HandleCXXStaticMemberVarInstantiation(VarDecl *VD) {
69 if (Diags.hasErrorOccurred())
72 Builder->HandleCXXStaticMemberVarInstantiation(VD);
75 virtual bool HandleTopLevelDecl(DeclGroupRef DG) {
76 if (Diags.hasErrorOccurred())
79 // Make sure to emit all elements of a Decl.
80 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
81 Builder->EmitTopLevelDecl(*I);
85 /// HandleTagDeclDefinition - This callback is invoked each time a TagDecl
86 /// to (e.g. struct, union, enum, class) is completed. This allows the
87 /// client hack on the type, which can occur at any point in the file
88 /// (because these can be defined in declspecs).
89 virtual void HandleTagDeclDefinition(TagDecl *D) {
90 if (Diags.hasErrorOccurred())
93 Builder->UpdateCompletedType(D);
95 // In C++, we may have member functions that need to be emitted at this
97 if (Ctx->getLangOpts().CPlusPlus && !D->isDependentContext()) {
98 for (DeclContext::decl_iterator M = D->decls_begin(),
99 MEnd = D->decls_end();
101 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(*M))
102 if (Method->doesThisDeclarationHaveABody() &&
103 (Method->hasAttr<UsedAttr>() ||
104 Method->hasAttr<ConstructorAttr>()))
105 Builder->EmitTopLevelDecl(Method);
109 virtual void HandleTagDeclRequiredDefinition(const TagDecl *D) LLVM_OVERRIDE {
110 if (Diags.hasErrorOccurred())
113 if (CodeGen::CGDebugInfo *DI = Builder->getModuleDebugInfo())
114 if (const RecordDecl *RD = dyn_cast<RecordDecl>(D))
115 DI->completeRequiredType(RD);
118 virtual void HandleTranslationUnit(ASTContext &Ctx) {
119 if (Diags.hasErrorOccurred()) {
130 virtual void CompleteTentativeDefinition(VarDecl *D) {
131 if (Diags.hasErrorOccurred())
134 Builder->EmitTentativeDefinition(D);
137 virtual void HandleVTable(CXXRecordDecl *RD, bool DefinitionRequired) {
138 if (Diags.hasErrorOccurred())
141 Builder->EmitVTable(RD, DefinitionRequired);
144 virtual void HandleLinkerOptionPragma(llvm::StringRef Opts) {
145 Builder->AppendLinkerOptions(Opts);
148 virtual void HandleDetectMismatch(llvm::StringRef Name,
149 llvm::StringRef Value) {
150 Builder->AddDetectMismatch(Name, Value);
153 virtual void HandleDependentLibrary(llvm::StringRef Lib) {
154 Builder->AddDependentLib(Lib);
159 void CodeGenerator::anchor() { }
161 CodeGenerator *clang::CreateLLVMCodeGen(DiagnosticsEngine &Diags,
162 const std::string& ModuleName,
163 const CodeGenOptions &CGO,
164 const TargetOptions &/*TO*/,
165 llvm::LLVMContext& C) {
166 return new CodeGeneratorImpl(Diags, ModuleName, CGO, C);