ModuleBuilder.cpp revision 278048971c8b2e153d305ee8917618c9bf38b1c3
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 "CGDebugInfo.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"
28using namespace clang;
29
30namespace {
31  class CodeGeneratorImpl : public CodeGenerator {
32    DiagnosticsEngine &Diags;
33    OwningPtr<const llvm::DataLayout> TD;
34    ASTContext *Ctx;
35    const CodeGenOptions CodeGenOpts;  // Intentionally copied in.
36  protected:
37    OwningPtr<llvm::Module> M;
38    OwningPtr<CodeGen::CodeGenModule> Builder;
39  public:
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)) {}
44
45    virtual ~CodeGeneratorImpl() {}
46
47    virtual llvm::Module* GetModule() {
48      return M.get();
49    }
50
51    virtual llvm::Module* ReleaseModule() {
52      return M.take();
53    }
54
55    virtual void Initialize(ASTContext &Context) {
56      Ctx = &Context;
57
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,
62                                               Diags));
63
64      for (size_t i = 0, e = CodeGenOpts.DependentLibraries.size(); i < e; ++i)
65        HandleDependentLibrary(CodeGenOpts.DependentLibraries[i]);
66    }
67
68    virtual void HandleCXXStaticMemberVarInstantiation(VarDecl *VD) {
69      Builder->HandleCXXStaticMemberVarInstantiation(VD);
70    }
71
72    virtual bool HandleTopLevelDecl(DeclGroupRef DG) {
73      // Make sure to emit all elements of a Decl.
74      for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
75        Builder->EmitTopLevelDecl(*I);
76      return true;
77    }
78
79    /// HandleTagDeclDefinition - This callback is invoked each time a TagDecl
80    /// to (e.g. struct, union, enum, class) is completed. This allows the
81    /// client hack on the type, which can occur at any point in the file
82    /// (because these can be defined in declspecs).
83    virtual void HandleTagDeclDefinition(TagDecl *D) {
84      Builder->UpdateCompletedType(D);
85
86      // In C++, we may have member functions that need to be emitted at this
87      // point.
88      if (Ctx->getLangOpts().CPlusPlus && !D->isDependentContext()) {
89        for (DeclContext::decl_iterator M = D->decls_begin(),
90                                     MEnd = D->decls_end();
91             M != MEnd; ++M)
92          if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(*M))
93            if (Method->doesThisDeclarationHaveABody() &&
94                (Method->hasAttr<UsedAttr>() ||
95                 Method->hasAttr<ConstructorAttr>()))
96              Builder->EmitTopLevelDecl(Method);
97      }
98    }
99
100    virtual void HandleTagDeclRequiredDefinition(const TagDecl *D) LLVM_OVERRIDE {
101      if (CodeGen::CGDebugInfo *DI = Builder->getModuleDebugInfo())
102        if (const RecordDecl *RD = dyn_cast<RecordDecl>(D))
103          DI->completeRequiredType(RD);
104    }
105
106    virtual void HandleTranslationUnit(ASTContext &Ctx) {
107      if (Diags.hasErrorOccurred()) {
108        M.reset();
109        return;
110      }
111
112      if (Builder)
113        Builder->Release();
114    }
115
116    virtual void CompleteTentativeDefinition(VarDecl *D) {
117      if (Diags.hasErrorOccurred())
118        return;
119
120      Builder->EmitTentativeDefinition(D);
121    }
122
123    virtual void HandleVTable(CXXRecordDecl *RD, bool DefinitionRequired) {
124      if (Diags.hasErrorOccurred())
125        return;
126
127      Builder->EmitVTable(RD, DefinitionRequired);
128    }
129
130    virtual void HandleLinkerOptionPragma(llvm::StringRef Opts) {
131      Builder->AppendLinkerOptions(Opts);
132    }
133
134    virtual void HandleDetectMismatch(llvm::StringRef Name,
135                                      llvm::StringRef Value) {
136      Builder->AddDetectMismatch(Name, Value);
137    }
138
139    virtual void HandleDependentLibrary(llvm::StringRef Lib) {
140      Builder->AddDependentLib(Lib);
141    }
142  };
143}
144
145void CodeGenerator::anchor() { }
146
147CodeGenerator *clang::CreateLLVMCodeGen(DiagnosticsEngine &Diags,
148                                        const std::string& ModuleName,
149                                        const CodeGenOptions &CGO,
150                                        const TargetOptions &/*TO*/,
151                                        llvm::LLVMContext& C) {
152  return new CodeGeneratorImpl(Diags, ModuleName, CGO, C);
153}
154