ModuleBuilder.cpp revision 4afa39deaa245592977136d367251ee2c173dd8d
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/AST/ASTContext.h"
17#include "clang/AST/DeclObjC.h"
18#include "clang/AST/Expr.h"
19using namespace clang;
20
21//===----------------------------------------------------------------------===//
22// LLVM Emitter
23
24#include "clang/Basic/Diagnostic.h"
25#include "clang/Basic/TargetInfo.h"
26#include "llvm/Module.h"
27#include "llvm/Target/TargetData.h"
28#include "llvm/Support/Compiler.h"
29#include "llvm/ADT/OwningPtr.h"
30
31
32namespace {
33  class VISIBILITY_HIDDEN CodeGeneratorImpl : public CodeGenerator {
34    Diagnostic &Diags;
35    llvm::OwningPtr<const llvm::TargetData> TD;
36    ASTContext *Ctx;
37    const LangOptions &Features;
38    bool GenerateDebugInfo;
39  protected:
40    llvm::OwningPtr<llvm::Module> M;
41    llvm::OwningPtr<CodeGen::CodeGenModule> Builder;
42  public:
43    CodeGeneratorImpl(Diagnostic &diags, const LangOptions &LO,
44                      const std::string& ModuleName,
45                      bool DebugInfoFlag)
46    : Diags(diags), Features(LO), GenerateDebugInfo(DebugInfoFlag),
47      M(new llvm::Module(ModuleName)) {}
48
49    virtual ~CodeGeneratorImpl() {}
50
51    virtual llvm::Module* GetModule() {
52      return M.get();
53    }
54
55    virtual llvm::Module* ReleaseModule() {
56      return M.take();
57    }
58
59    virtual void Initialize(ASTContext &Context) {
60      Ctx = &Context;
61
62      M->setTargetTriple(Ctx->Target.getTargetTriple());
63      M->setDataLayout(Ctx->Target.getTargetDescription());
64      TD.reset(new llvm::TargetData(Ctx->Target.getTargetDescription()));
65      Builder.reset(new CodeGen::CodeGenModule(Context, Features, *M, *TD,
66                                               Diags, GenerateDebugInfo));
67    }
68
69    virtual void HandleTopLevelDecl(Decl *D) {
70      // Make sure to emit all elements of a Decl.
71      if (Decl *SD = dyn_cast<Decl>(D)) {
72        for (; SD; SD = SD->getNextDeclarator())
73          Builder->EmitTopLevelDecl(SD);
74      } else {
75        Builder->EmitTopLevelDecl(D);
76      }
77    }
78
79    /// HandleTagDeclDefinition - This callback is invoked each time a TagDecl
80    /// (e.g. struct, union, enum, class) is completed. This allows the client to
81    /// hack on the type, which can occur at any point in the file (because these
82    /// can be defined in declspecs).
83    virtual void HandleTagDeclDefinition(TagDecl *D) {
84      Builder->UpdateCompletedType(D);
85    }
86
87    virtual void HandleTranslationUnit(TranslationUnit& TU) {
88      if (Diags.hasErrorOccurred()) {
89        M.reset();
90        return;
91      }
92
93      if (Builder)
94        Builder->Release();
95    };
96  };
97}
98
99CodeGenerator *clang::CreateLLVMCodeGen(Diagnostic &Diags,
100                                        const LangOptions &Features,
101                                        const std::string& ModuleName,
102                                        bool GenerateDebugInfo) {
103  return new CodeGeneratorImpl(Diags, Features, ModuleName, GenerateDebugInfo);
104}
105