ModuleBuilder.cpp revision 025039377d7247620750205dbd61ca1ba336f7e0
1//===--- ModuleBuilder.cpp - Emit LLVM Code from ASTs ---------------------===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
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"
16#include "clang/Frontend/CodeGenOptions.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 "llvm/LLVMContext.h"
23#include "llvm/Module.h"
24#include "llvm/Target/TargetData.h"
25#include "llvm/ADT/OwningPtr.h"
26using namespace clang;
27
28namespace {
29  class CodeGeneratorImpl : public CodeGenerator {
30    DiagnosticsEngine &Diags;
31    OwningPtr<const llvm::TargetData> TD;
32    ASTContext *Ctx;
33    const CodeGenOptions CodeGenOpts;  // Intentionally copied in.
34  protected:
35    OwningPtr<llvm::Module> M;
36    OwningPtr<CodeGen::CodeGenModule> Builder;
37  public:
38    CodeGeneratorImpl(DiagnosticsEngine &diags, const std::string& ModuleName,
39                      const CodeGenOptions &CGO, llvm::LLVMContext& C)
40      : Diags(diags), CodeGenOpts(CGO), M(new llvm::Module(ModuleName, C)) {}
41
42    virtual ~CodeGeneratorImpl() {}
43
44    virtual llvm::Module* GetModule() {
45      return M.get();
46    }
47
48    virtual llvm::Module* ReleaseModule() {
49      return M.take();
50    }
51
52    virtual void Initialize(ASTContext &Context) {
53      Ctx = &Context;
54
55      M->setTargetTriple(Ctx->getTargetInfo().getTriple().getTriple());
56      M->setDataLayout(Ctx->getTargetInfo().getTargetDescription());
57      TD.reset(new llvm::TargetData(Ctx->getTargetInfo().getTargetDescription()));
58      Builder.reset(new CodeGen::CodeGenModule(Context, CodeGenOpts,
59                                               *M, *TD, Diags));
60    }
61
62    virtual void HandleCXXStaticMemberVarInstantiation(VarDecl *VD) {
63      Builder->HandleCXXStaticMemberVarInstantiation(VD);
64    }
65
66    virtual bool HandleTopLevelDecl(DeclGroupRef DG) {
67      // Make sure to emit all elements of a Decl.
68      for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
69        Builder->EmitTopLevelDecl(*I);
70      return true;
71    }
72
73    /// HandleTagDeclDefinition - This callback is invoked each time a TagDecl
74    /// to (e.g. struct, union, enum, class) is completed. This allows the
75    /// client hack on the type, which can occur at any point in the file
76    /// (because these can be defined in declspecs).
77    virtual void HandleTagDeclDefinition(TagDecl *D) {
78      Builder->UpdateCompletedType(D);
79
80      // In C++, we may have member functions that need to be emitted at this
81      // point.
82      if (Ctx->getLangOptions().CPlusPlus && !D->isDependentContext()) {
83        for (DeclContext::decl_iterator M = D->decls_begin(),
84                                     MEnd = D->decls_end();
85             M != MEnd; ++M)
86          if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(*M))
87            if (Method->doesThisDeclarationHaveABody() &&
88                (Method->hasAttr<UsedAttr>() ||
89                 Method->hasAttr<ConstructorAttr>()))
90              Builder->EmitTopLevelDecl(Method);
91      }
92    }
93
94    virtual void HandleTranslationUnit(ASTContext &Ctx) {
95      if (Diags.hasErrorOccurred()) {
96        M.reset();
97        return;
98      }
99
100      if (Builder)
101        Builder->Release();
102    }
103
104    virtual void CompleteTentativeDefinition(VarDecl *D) {
105      if (Diags.hasErrorOccurred())
106        return;
107
108      Builder->EmitTentativeDefinition(D);
109    }
110
111    virtual void HandleVTable(CXXRecordDecl *RD, bool DefinitionRequired) {
112      if (Diags.hasErrorOccurred())
113        return;
114
115      Builder->EmitVTable(RD, DefinitionRequired);
116    }
117  };
118}
119
120void CodeGenerator::anchor() { }
121
122CodeGenerator *clang::CreateLLVMCodeGen(DiagnosticsEngine &Diags,
123                                        const std::string& ModuleName,
124                                        const CodeGenOptions &CGO,
125                                        llvm::LLVMContext& C) {
126  return new CodeGeneratorImpl(Diags, ModuleName, CGO, C);
127}
128