ModuleBuilder.cpp revision 201361
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/ADT/OwningPtr.h"
26193326Sedusing namespace clang;
27193326Sed
28193326Sednamespace {
29199990Srdivacky  class CodeGeneratorImpl : public CodeGenerator {
30193326Sed    Diagnostic &Diags;
31193326Sed    llvm::OwningPtr<const llvm::TargetData> TD;
32193326Sed    ASTContext *Ctx;
33199482Srdivacky    const CodeGenOptions CodeGenOpts;  // Intentionally copied in.
34193326Sed  protected:
35193326Sed    llvm::OwningPtr<llvm::Module> M;
36193326Sed    llvm::OwningPtr<CodeGen::CodeGenModule> Builder;
37193326Sed  public:
38193326Sed    CodeGeneratorImpl(Diagnostic &diags, const std::string& ModuleName,
39199482Srdivacky                      const CodeGenOptions &CGO, llvm::LLVMContext& C)
40199482Srdivacky      : Diags(diags), CodeGenOpts(CGO), M(new llvm::Module(ModuleName, C)) {}
41198092Srdivacky
42193326Sed    virtual ~CodeGeneratorImpl() {}
43198092Srdivacky
44193326Sed    virtual llvm::Module* GetModule() {
45193326Sed      return M.get();
46193326Sed    }
47198092Srdivacky
48193326Sed    virtual llvm::Module* ReleaseModule() {
49193326Sed      return M.take();
50193326Sed    }
51198092Srdivacky
52193326Sed    virtual void Initialize(ASTContext &Context) {
53193326Sed      Ctx = &Context;
54198092Srdivacky
55198092Srdivacky      M->setTargetTriple(Ctx->Target.getTriple().getTriple());
56193326Sed      M->setDataLayout(Ctx->Target.getTargetDescription());
57193326Sed      TD.reset(new llvm::TargetData(Ctx->Target.getTargetDescription()));
58199482Srdivacky      Builder.reset(new CodeGen::CodeGenModule(Context, CodeGenOpts,
59193326Sed                                               *M, *TD, Diags));
60193326Sed    }
61198092Srdivacky
62193326Sed    virtual void HandleTopLevelDecl(DeclGroupRef DG) {
63193326Sed      // Make sure to emit all elements of a Decl.
64193326Sed      for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
65193326Sed        Builder->EmitTopLevelDecl(*I);
66193326Sed    }
67193326Sed
68193326Sed    /// HandleTagDeclDefinition - This callback is invoked each time a TagDecl
69193326Sed    /// to (e.g. struct, union, enum, class) is completed. This allows the
70193326Sed    /// client hack on the type, which can occur at any point in the file
71193326Sed    /// (because these can be defined in declspecs).
72193326Sed    virtual void HandleTagDeclDefinition(TagDecl *D) {
73193326Sed      Builder->UpdateCompletedType(D);
74193326Sed    }
75193326Sed
76193326Sed    virtual void HandleTranslationUnit(ASTContext &Ctx) {
77193326Sed      if (Diags.hasErrorOccurred()) {
78193326Sed        M.reset();
79193326Sed        return;
80193326Sed      }
81193326Sed
82193326Sed      if (Builder)
83193326Sed        Builder->Release();
84201361Srdivacky    }
85193326Sed
86193326Sed    virtual void CompleteTentativeDefinition(VarDecl *D) {
87193326Sed      if (Diags.hasErrorOccurred())
88193326Sed        return;
89193326Sed
90193326Sed      Builder->EmitTentativeDefinition(D);
91193326Sed    }
92193326Sed  };
93193326Sed}
94193326Sed
95198092SrdivackyCodeGenerator *clang::CreateLLVMCodeGen(Diagnostic &Diags,
96193326Sed                                        const std::string& ModuleName,
97199482Srdivacky                                        const CodeGenOptions &CGO,
98195341Sed                                        llvm::LLVMContext& C) {
99199482Srdivacky  return new CodeGeneratorImpl(Diags, ModuleName, CGO, C);
100193326Sed}
101