ModuleBuilder.cpp revision 199482
1193326Sed//===--- ModuleBuilder.cpp - Emit LLVM Code from ASTs ---------------------===//
2193326Sed//
3193326Sed//                     The LLVM Compiler Infrastructure
4193326Sed//
5193326Sed// This file is distributed under the University of Illinois Open Source
6193326Sed// License. See LICENSE.TXT for details.
7193326Sed//
8193326Sed//===----------------------------------------------------------------------===//
9193326Sed//
10193326Sed// This builds an AST and converts it to LLVM Code.
11193326Sed//
12193326Sed//===----------------------------------------------------------------------===//
13193326Sed
14193326Sed#include "clang/CodeGen/ModuleBuilder.h"
15193326Sed#include "CodeGenModule.h"
16199482Srdivacky#include "clang/CodeGen/CodeGenOptions.h"
17193326Sed#include "clang/AST/ASTContext.h"
18193326Sed#include "clang/AST/DeclObjC.h"
19193326Sed#include "clang/AST/Expr.h"
20193326Sed#include "clang/Basic/Diagnostic.h"
21193326Sed#include "clang/Basic/TargetInfo.h"
22195341Sed#include "llvm/LLVMContext.h"
23193326Sed#include "llvm/Module.h"
24193326Sed#include "llvm/Target/TargetData.h"
25193326Sed#include "llvm/Support/Compiler.h"
26193326Sed#include "llvm/ADT/OwningPtr.h"
27193326Sedusing namespace clang;
28193326Sed
29193326Sed
30193326Sednamespace {
31193326Sed  class VISIBILITY_HIDDEN CodeGeneratorImpl : public CodeGenerator {
32193326Sed    Diagnostic &Diags;
33193326Sed    llvm::OwningPtr<const llvm::TargetData> TD;
34193326Sed    ASTContext *Ctx;
35199482Srdivacky    const CodeGenOptions CodeGenOpts;  // Intentionally copied in.
36193326Sed  protected:
37193326Sed    llvm::OwningPtr<llvm::Module> M;
38193326Sed    llvm::OwningPtr<CodeGen::CodeGenModule> Builder;
39193326Sed  public:
40193326Sed    CodeGeneratorImpl(Diagnostic &diags, const std::string& ModuleName,
41199482Srdivacky                      const CodeGenOptions &CGO, llvm::LLVMContext& C)
42199482Srdivacky      : Diags(diags), CodeGenOpts(CGO), M(new llvm::Module(ModuleName, C)) {}
43198092Srdivacky
44193326Sed    virtual ~CodeGeneratorImpl() {}
45198092Srdivacky
46193326Sed    virtual llvm::Module* GetModule() {
47193326Sed      return M.get();
48193326Sed    }
49198092Srdivacky
50193326Sed    virtual llvm::Module* ReleaseModule() {
51193326Sed      return M.take();
52193326Sed    }
53198092Srdivacky
54193326Sed    virtual void Initialize(ASTContext &Context) {
55193326Sed      Ctx = &Context;
56198092Srdivacky
57198092Srdivacky      M->setTargetTriple(Ctx->Target.getTriple().getTriple());
58193326Sed      M->setDataLayout(Ctx->Target.getTargetDescription());
59193326Sed      TD.reset(new llvm::TargetData(Ctx->Target.getTargetDescription()));
60199482Srdivacky      Builder.reset(new CodeGen::CodeGenModule(Context, CodeGenOpts,
61193326Sed                                               *M, *TD, Diags));
62193326Sed    }
63198092Srdivacky
64193326Sed    virtual void HandleTopLevelDecl(DeclGroupRef DG) {
65193326Sed      // Make sure to emit all elements of a Decl.
66193326Sed      for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
67193326Sed        Builder->EmitTopLevelDecl(*I);
68193326Sed    }
69193326Sed
70193326Sed    /// HandleTagDeclDefinition - This callback is invoked each time a TagDecl
71193326Sed    /// to (e.g. struct, union, enum, class) is completed. This allows the
72193326Sed    /// client hack on the type, which can occur at any point in the file
73193326Sed    /// (because these can be defined in declspecs).
74193326Sed    virtual void HandleTagDeclDefinition(TagDecl *D) {
75193326Sed      Builder->UpdateCompletedType(D);
76193326Sed    }
77193326Sed
78193326Sed    virtual void HandleTranslationUnit(ASTContext &Ctx) {
79193326Sed      if (Diags.hasErrorOccurred()) {
80193326Sed        M.reset();
81193326Sed        return;
82193326Sed      }
83193326Sed
84193326Sed      if (Builder)
85193326Sed        Builder->Release();
86193326Sed    };
87193326Sed
88193326Sed    virtual void CompleteTentativeDefinition(VarDecl *D) {
89193326Sed      if (Diags.hasErrorOccurred())
90193326Sed        return;
91193326Sed
92193326Sed      Builder->EmitTentativeDefinition(D);
93193326Sed    }
94193326Sed  };
95193326Sed}
96193326Sed
97198092SrdivackyCodeGenerator *clang::CreateLLVMCodeGen(Diagnostic &Diags,
98193326Sed                                        const std::string& ModuleName,
99199482Srdivacky                                        const CodeGenOptions &CGO,
100195341Sed                                        llvm::LLVMContext& C) {
101199482Srdivacky  return new CodeGeneratorImpl(Diags, ModuleName, CGO, C);
102193326Sed}
103