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/DeclObjC.h"
18 #include "clang/AST/Expr.h"
19 #include "clang/Basic/Diagnostic.h"
20 #include "clang/Basic/TargetInfo.h"
21 #include "clang/Frontend/CodeGenOptions.h"
22 #include "llvm/ADT/OwningPtr.h"
23 #include "llvm/IR/DataLayout.h"
24 #include "llvm/IR/LLVMContext.h"
25 #include "llvm/IR/Module.h"
26 using namespace clang;
29 class CodeGeneratorImpl : public CodeGenerator {
30 DiagnosticsEngine &Diags;
31 OwningPtr<const llvm::DataLayout> TD;
33 const CodeGenOptions CodeGenOpts; // Intentionally copied in.
35 OwningPtr<llvm::Module> M;
36 OwningPtr<CodeGen::CodeGenModule> Builder;
38 CodeGeneratorImpl(DiagnosticsEngine &diags, const std::string& ModuleName,
39 const CodeGenOptions &CGO, llvm::LLVMContext& C)
40 : Diags(diags), CodeGenOpts(CGO),
41 M(new llvm::Module(ModuleName, C)) {}
43 virtual ~CodeGeneratorImpl() {}
45 virtual llvm::Module* GetModule() {
49 virtual llvm::Module* ReleaseModule() {
53 virtual void Initialize(ASTContext &Context) {
56 M->setTargetTriple(Ctx->getTargetInfo().getTriple().getTriple());
57 M->setDataLayout(Ctx->getTargetInfo().getTargetDescription());
58 TD.reset(new llvm::DataLayout(Ctx->getTargetInfo().getTargetDescription()));
59 Builder.reset(new CodeGen::CodeGenModule(Context, CodeGenOpts, *M, *TD,
63 virtual void HandleCXXStaticMemberVarInstantiation(VarDecl *VD) {
64 Builder->HandleCXXStaticMemberVarInstantiation(VD);
67 virtual bool HandleTopLevelDecl(DeclGroupRef DG) {
68 // Make sure to emit all elements of a Decl.
69 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
70 Builder->EmitTopLevelDecl(*I);
74 /// HandleTagDeclDefinition - This callback is invoked each time a TagDecl
75 /// to (e.g. struct, union, enum, class) is completed. This allows the
76 /// client hack on the type, which can occur at any point in the file
77 /// (because these can be defined in declspecs).
78 virtual void HandleTagDeclDefinition(TagDecl *D) {
79 Builder->UpdateCompletedType(D);
81 // In C++, we may have member functions that need to be emitted at this
83 if (Ctx->getLangOpts().CPlusPlus && !D->isDependentContext()) {
84 for (DeclContext::decl_iterator M = D->decls_begin(),
85 MEnd = D->decls_end();
87 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(*M))
88 if (Method->doesThisDeclarationHaveABody() &&
89 (Method->hasAttr<UsedAttr>() ||
90 Method->hasAttr<ConstructorAttr>()))
91 Builder->EmitTopLevelDecl(Method);
95 virtual void HandleTranslationUnit(ASTContext &Ctx) {
96 if (Diags.hasErrorOccurred()) {
105 virtual void CompleteTentativeDefinition(VarDecl *D) {
106 if (Diags.hasErrorOccurred())
109 Builder->EmitTentativeDefinition(D);
112 virtual void HandleVTable(CXXRecordDecl *RD, bool DefinitionRequired) {
113 if (Diags.hasErrorOccurred())
116 Builder->EmitVTable(RD, DefinitionRequired);
121 void CodeGenerator::anchor() { }
123 CodeGenerator *clang::CreateLLVMCodeGen(DiagnosticsEngine &Diags,
124 const std::string& ModuleName,
125 const CodeGenOptions &CGO,
126 const TargetOptions &/*TO*/,
127 llvm::LLVMContext& C) {
128 return new CodeGeneratorImpl(Diags, ModuleName, CGO, C);