• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "CGDebugInfo.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 "clang/Frontend/CodeGenOptions.h"
23 #include "llvm/ADT/OwningPtr.h"
24 #include "llvm/ADT/StringRef.h"
25 #include "llvm/IR/DataLayout.h"
26 #include "llvm/IR/LLVMContext.h"
27 #include "llvm/IR/Module.h"
28 using namespace clang;
29 
30 namespace {
31   class CodeGeneratorImpl : public CodeGenerator {
32     DiagnosticsEngine &Diags;
33     OwningPtr<const llvm::DataLayout> TD;
34     ASTContext *Ctx;
35     const CodeGenOptions CodeGenOpts;  // Intentionally copied in.
36   protected:
37     OwningPtr<llvm::Module> M;
38     OwningPtr<CodeGen::CodeGenModule> Builder;
39   public:
CodeGeneratorImpl(DiagnosticsEngine & diags,const std::string & ModuleName,const CodeGenOptions & CGO,llvm::LLVMContext & C)40     CodeGeneratorImpl(DiagnosticsEngine &diags, const std::string& ModuleName,
41                       const CodeGenOptions &CGO, llvm::LLVMContext& C)
42       : Diags(diags), CodeGenOpts(CGO),
43         M(new llvm::Module(ModuleName, C)) {}
44 
~CodeGeneratorImpl()45     virtual ~CodeGeneratorImpl() {}
46 
GetModule()47     virtual llvm::Module* GetModule() {
48       return M.get();
49     }
50 
ReleaseModule()51     virtual llvm::Module* ReleaseModule() {
52       return M.take();
53     }
54 
Initialize(ASTContext & Context)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, *M, *TD,
62                                                Diags));
63     }
64 
HandleCXXStaticMemberVarInstantiation(VarDecl * VD)65     virtual void HandleCXXStaticMemberVarInstantiation(VarDecl *VD) {
66       Builder->HandleCXXStaticMemberVarInstantiation(VD);
67     }
68 
HandleTopLevelDecl(DeclGroupRef DG)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).
HandleTagDeclDefinition(TagDecl * D)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 
HandleTagDeclRequiredDefinition(const TagDecl * D)97     virtual void HandleTagDeclRequiredDefinition(const TagDecl *D) LLVM_OVERRIDE {
98       if (CodeGen::CGDebugInfo *DI = Builder->getModuleDebugInfo())
99         if (const RecordDecl *RD = dyn_cast<RecordDecl>(D))
100           DI->completeFwdDecl(*RD);
101     }
102 
HandleTranslationUnit(ASTContext & Ctx)103     virtual void HandleTranslationUnit(ASTContext &Ctx) {
104       if (Diags.hasErrorOccurred()) {
105         M.reset();
106         return;
107       }
108 
109       if (Builder)
110         Builder->Release();
111     }
112 
CompleteTentativeDefinition(VarDecl * D)113     virtual void CompleteTentativeDefinition(VarDecl *D) {
114       if (Diags.hasErrorOccurred())
115         return;
116 
117       Builder->EmitTentativeDefinition(D);
118     }
119 
HandleVTable(CXXRecordDecl * RD,bool DefinitionRequired)120     virtual void HandleVTable(CXXRecordDecl *RD, bool DefinitionRequired) {
121       if (Diags.hasErrorOccurred())
122         return;
123 
124       Builder->EmitVTable(RD, DefinitionRequired);
125     }
126 
HandleLinkerOptionPragma(llvm::StringRef Opts)127     virtual void HandleLinkerOptionPragma(llvm::StringRef Opts) {
128       Builder->AppendLinkerOptions(Opts);
129     }
130 
HandleDetectMismatch(llvm::StringRef Name,llvm::StringRef Value)131     virtual void HandleDetectMismatch(llvm::StringRef Name,
132                                       llvm::StringRef Value) {
133       Builder->AddDetectMismatch(Name, Value);
134     }
135 
HandleDependentLibrary(llvm::StringRef Lib)136     virtual void HandleDependentLibrary(llvm::StringRef Lib) {
137       Builder->AddDependentLib(Lib);
138     }
139   };
140 }
141 
anchor()142 void CodeGenerator::anchor() { }
143 
CreateLLVMCodeGen(DiagnosticsEngine & Diags,const std::string & ModuleName,const CodeGenOptions & CGO,const TargetOptions &,llvm::LLVMContext & C)144 CodeGenerator *clang::CreateLLVMCodeGen(DiagnosticsEngine &Diags,
145                                         const std::string& ModuleName,
146                                         const CodeGenOptions &CGO,
147                                         const TargetOptions &/*TO*/,
148                                         llvm::LLVMContext& C) {
149   return new CodeGeneratorImpl(Diags, ModuleName, CGO, C);
150 }
151