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.
34 const TargetOptions TargetOpts; // Intentionally copied in.
36 OwningPtr<llvm::Module> M;
37 OwningPtr<CodeGen::CodeGenModule> Builder;
39 CodeGeneratorImpl(DiagnosticsEngine &diags, const std::string& ModuleName,
40 const CodeGenOptions &CGO, const TargetOptions &TO,
42 : Diags(diags), CodeGenOpts(CGO), TargetOpts(TO),
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, TargetOpts,
65 virtual void HandleCXXStaticMemberVarInstantiation(VarDecl *VD) {
66 Builder->HandleCXXStaticMemberVarInstantiation(VD);
69 virtual bool HandleTopLevelDecl(DeclGroupRef DG) {
70 // Make sure to emit all elements of a Decl.
71 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
72 Builder->EmitTopLevelDecl(*I);
76 /// HandleTagDeclDefinition - This callback is invoked each time a TagDecl
77 /// to (e.g. struct, union, enum, class) is completed. This allows the
78 /// client hack on the type, which can occur at any point in the file
79 /// (because these can be defined in declspecs).
80 virtual void HandleTagDeclDefinition(TagDecl *D) {
81 Builder->UpdateCompletedType(D);
83 // In C++, we may have member functions that need to be emitted at this
85 if (Ctx->getLangOpts().CPlusPlus && !D->isDependentContext()) {
86 for (DeclContext::decl_iterator M = D->decls_begin(),
87 MEnd = D->decls_end();
89 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(*M))
90 if (Method->doesThisDeclarationHaveABody() &&
91 (Method->hasAttr<UsedAttr>() ||
92 Method->hasAttr<ConstructorAttr>()))
93 Builder->EmitTopLevelDecl(Method);
97 virtual void HandleTranslationUnit(ASTContext &Ctx) {
98 if (Diags.hasErrorOccurred()) {
107 virtual void CompleteTentativeDefinition(VarDecl *D) {
108 if (Diags.hasErrorOccurred())
111 Builder->EmitTentativeDefinition(D);
114 virtual void HandleVTable(CXXRecordDecl *RD, bool DefinitionRequired) {
115 if (Diags.hasErrorOccurred())
118 Builder->EmitVTable(RD, DefinitionRequired);
123 void CodeGenerator::anchor() { }
125 CodeGenerator *clang::CreateLLVMCodeGen(DiagnosticsEngine &Diags,
126 const std::string& ModuleName,
127 const CodeGenOptions &CGO,
128 const TargetOptions &TO,
129 llvm::LLVMContext& C) {
130 return new CodeGeneratorImpl(Diags, ModuleName, CGO, TO, C);