]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/clang/lib/CodeGen/ModuleBuilder.cpp
MFV: tcpdump 4.4.0.
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / 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/AST/ASTContext.h"
17 #include "clang/AST/DeclObjC.h"
18 #include "clang/AST/Expr.h"
19 #include "clang/Basic/Diagnostic.h"
20 #include "clang/Basic/TargetInfo.h"
21 #include "clang/Frontend/CodeGenOptions.h"
22 #include "llvm/ADT/OwningPtr.h"
23 #include "llvm/IR/DataLayout.h"
24 #include "llvm/IR/LLVMContext.h"
25 #include "llvm/IR/Module.h"
26 using namespace clang;
27
28 namespace {
29   class CodeGeneratorImpl : public CodeGenerator {
30     DiagnosticsEngine &Diags;
31     OwningPtr<const llvm::DataLayout> TD;
32     ASTContext *Ctx;
33     const CodeGenOptions CodeGenOpts;  // Intentionally copied in.
34     const TargetOptions TargetOpts;    // Intentionally copied in.
35   protected:
36     OwningPtr<llvm::Module> M;
37     OwningPtr<CodeGen::CodeGenModule> Builder;
38   public:
39     CodeGeneratorImpl(DiagnosticsEngine &diags, const std::string& ModuleName,
40                       const CodeGenOptions &CGO, const TargetOptions &TO,
41                       llvm::LLVMContext& C)
42       : Diags(diags), CodeGenOpts(CGO), TargetOpts(TO),
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, TargetOpts,
62                                                *M, *TD, Diags));
63     }
64
65     virtual void HandleCXXStaticMemberVarInstantiation(VarDecl *VD) {
66       Builder->HandleCXXStaticMemberVarInstantiation(VD);
67     }
68
69     virtual bool HandleTopLevelDecl(DeclGroupRef DG) {
70       // Make sure to emit all elements of a Decl.
71       for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
72         Builder->EmitTopLevelDecl(*I);
73       return true;
74     }
75
76     /// HandleTagDeclDefinition - This callback is invoked each time a TagDecl
77     /// to (e.g. struct, union, enum, class) is completed. This allows the
78     /// client hack on the type, which can occur at any point in the file
79     /// (because these can be defined in declspecs).
80     virtual void HandleTagDeclDefinition(TagDecl *D) {
81       Builder->UpdateCompletedType(D);
82       
83       // In C++, we may have member functions that need to be emitted at this 
84       // point.
85       if (Ctx->getLangOpts().CPlusPlus && !D->isDependentContext()) {
86         for (DeclContext::decl_iterator M = D->decls_begin(), 
87                                      MEnd = D->decls_end();
88              M != MEnd; ++M)
89           if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(*M))
90             if (Method->doesThisDeclarationHaveABody() &&
91                 (Method->hasAttr<UsedAttr>() || 
92                  Method->hasAttr<ConstructorAttr>()))
93               Builder->EmitTopLevelDecl(Method);
94       }
95     }
96
97     virtual void HandleTranslationUnit(ASTContext &Ctx) {
98       if (Diags.hasErrorOccurred()) {
99         M.reset();
100         return;
101       }
102
103       if (Builder)
104         Builder->Release();
105     }
106
107     virtual void CompleteTentativeDefinition(VarDecl *D) {
108       if (Diags.hasErrorOccurred())
109         return;
110
111       Builder->EmitTentativeDefinition(D);
112     }
113
114     virtual void HandleVTable(CXXRecordDecl *RD, bool DefinitionRequired) {
115       if (Diags.hasErrorOccurred())
116         return;
117
118       Builder->EmitVTable(RD, DefinitionRequired);
119     }
120   };
121 }
122
123 void CodeGenerator::anchor() { }
124
125 CodeGenerator *clang::CreateLLVMCodeGen(DiagnosticsEngine &Diags,
126                                         const std::string& ModuleName,
127                                         const CodeGenOptions &CGO,
128                                         const TargetOptions &TO,
129                                         llvm::LLVMContext& C) {
130   return new CodeGeneratorImpl(Diags, ModuleName, CGO, TO, C);
131 }