]> granicus.if.org Git - clang/blob - lib/CodeGen/ModuleBuilder.cpp
Create a TargetMachine whenever we create a CodeGenAction. The codegen of
[clang] / lib / CodeGen / ModuleBuilder.cpp
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/CodeGen/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"
26 using namespace clang;
27
28 namespace {
29   class CodeGeneratorImpl : public CodeGenerator {
30     Diagnostic &Diags;
31     llvm::OwningPtr<const llvm::TargetData> TD;
32     const llvm::TargetMachine &TM;
33     ASTContext *Ctx;
34     const CodeGenOptions CodeGenOpts;  // Intentionally copied in.
35   protected:
36     llvm::OwningPtr<llvm::Module> M;
37     llvm::OwningPtr<CodeGen::CodeGenModule> Builder;
38   public:
39     CodeGeneratorImpl(Diagnostic &diags, const std::string& ModuleName,
40                       const CodeGenOptions &CGO,
41                       const llvm::TargetMachine &TM,
42                       llvm::LLVMContext& C)
43       : Diags(diags), TM(TM), CodeGenOpts(CGO),
44         M(new llvm::Module(ModuleName, C)) {}
45
46     virtual ~CodeGeneratorImpl() {}
47
48     virtual llvm::Module* GetModule() {
49       return M.get();
50     }
51
52     virtual llvm::Module* ReleaseModule() {
53       return M.take();
54     }
55
56     virtual void Initialize(ASTContext &Context) {
57       Ctx = &Context;
58
59       M->setTargetTriple(Ctx->Target.getTriple().getTriple());
60       M->setDataLayout(Ctx->Target.getTargetDescription());
61       TD.reset(new llvm::TargetData(Ctx->Target.getTargetDescription()));
62       Builder.reset(new CodeGen::CodeGenModule(Context, CodeGenOpts,
63                                                *M, TM, *TD, Diags));
64     }
65
66     virtual void 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     }
71
72     /// HandleTagDeclDefinition - This callback is invoked each time a TagDecl
73     /// to (e.g. struct, union, enum, class) is completed. This allows the
74     /// client hack on the type, which can occur at any point in the file
75     /// (because these can be defined in declspecs).
76     virtual void HandleTagDeclDefinition(TagDecl *D) {
77       Builder->UpdateCompletedType(D);
78     }
79
80     virtual void HandleTranslationUnit(ASTContext &Ctx) {
81       if (Diags.hasErrorOccurred()) {
82         M.reset();
83         return;
84       }
85
86       if (Builder)
87         Builder->Release();
88     }
89
90     virtual void CompleteTentativeDefinition(VarDecl *D) {
91       if (Diags.hasErrorOccurred())
92         return;
93
94       Builder->EmitTentativeDefinition(D);
95     }
96   };
97 }
98
99 CodeGenerator *clang::CreateLLVMCodeGen(Diagnostic &Diags,
100                                         const std::string& ModuleName,
101                                         const CodeGenOptions &CGO,
102                                         const llvm::TargetMachine &Machine,
103                                         llvm::LLVMContext& C) {
104   return new CodeGeneratorImpl(Diags, ModuleName, CGO, Machine, C);
105 }