clang API Documentation

ModuleBuilder.cpp
Go to the documentation of this file.
00001 //===--- ModuleBuilder.cpp - Emit LLVM Code from ASTs ---------------------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This builds an AST and converts it to LLVM Code.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #include "clang/CodeGen/ModuleBuilder.h"
00015 #include "CodeGenModule.h"
00016 #include "clang/Frontend/CodeGenOptions.h"
00017 #include "clang/AST/ASTContext.h"
00018 #include "clang/AST/DeclObjC.h"
00019 #include "clang/AST/Expr.h"
00020 #include "clang/Basic/Diagnostic.h"
00021 #include "clang/Basic/TargetInfo.h"
00022 #include "llvm/LLVMContext.h"
00023 #include "llvm/Module.h"
00024 #include "llvm/Target/TargetData.h"
00025 #include "llvm/ADT/OwningPtr.h"
00026 using namespace clang;
00027 
00028 namespace {
00029   class CodeGeneratorImpl : public CodeGenerator {
00030     DiagnosticsEngine &Diags;
00031     OwningPtr<const llvm::TargetData> TD;
00032     ASTContext *Ctx;
00033     const CodeGenOptions CodeGenOpts;  // Intentionally copied in.
00034   protected:
00035     OwningPtr<llvm::Module> M;
00036     OwningPtr<CodeGen::CodeGenModule> Builder;
00037   public:
00038     CodeGeneratorImpl(DiagnosticsEngine &diags, const std::string& ModuleName,
00039                       const CodeGenOptions &CGO, llvm::LLVMContext& C)
00040       : Diags(diags), CodeGenOpts(CGO), M(new llvm::Module(ModuleName, C)) {}
00041 
00042     virtual ~CodeGeneratorImpl() {}
00043 
00044     virtual llvm::Module* GetModule() {
00045       return M.get();
00046     }
00047 
00048     virtual llvm::Module* ReleaseModule() {
00049       return M.take();
00050     }
00051 
00052     virtual void Initialize(ASTContext &Context) {
00053       Ctx = &Context;
00054 
00055       M->setTargetTriple(Ctx->getTargetInfo().getTriple().getTriple());
00056       M->setDataLayout(Ctx->getTargetInfo().getTargetDescription());
00057       TD.reset(new llvm::TargetData(Ctx->getTargetInfo().getTargetDescription()));
00058       Builder.reset(new CodeGen::CodeGenModule(Context, CodeGenOpts,
00059                                                *M, *TD, Diags));
00060     }
00061 
00062     virtual void HandleCXXStaticMemberVarInstantiation(VarDecl *VD) {
00063       Builder->HandleCXXStaticMemberVarInstantiation(VD);
00064     }
00065 
00066     virtual bool HandleTopLevelDecl(DeclGroupRef DG) {
00067       // Make sure to emit all elements of a Decl.
00068       for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
00069         Builder->EmitTopLevelDecl(*I);
00070       return true;
00071     }
00072 
00073     /// HandleTagDeclDefinition - This callback is invoked each time a TagDecl
00074     /// to (e.g. struct, union, enum, class) is completed. This allows the
00075     /// client hack on the type, which can occur at any point in the file
00076     /// (because these can be defined in declspecs).
00077     virtual void HandleTagDeclDefinition(TagDecl *D) {
00078       Builder->UpdateCompletedType(D);
00079       
00080       // In C++, we may have member functions that need to be emitted at this 
00081       // point.
00082       if (Ctx->getLangOpts().CPlusPlus && !D->isDependentContext()) {
00083         for (DeclContext::decl_iterator M = D->decls_begin(), 
00084                                      MEnd = D->decls_end();
00085              M != MEnd; ++M)
00086           if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(*M))
00087             if (Method->doesThisDeclarationHaveABody() &&
00088                 (Method->hasAttr<UsedAttr>() || 
00089                  Method->hasAttr<ConstructorAttr>()))
00090               Builder->EmitTopLevelDecl(Method);
00091       }
00092     }
00093 
00094     virtual void HandleTranslationUnit(ASTContext &Ctx) {
00095       if (Diags.hasErrorOccurred()) {
00096         M.reset();
00097         return;
00098       }
00099 
00100       if (Builder)
00101         Builder->Release();
00102     }
00103 
00104     virtual void CompleteTentativeDefinition(VarDecl *D) {
00105       if (Diags.hasErrorOccurred())
00106         return;
00107 
00108       Builder->EmitTentativeDefinition(D);
00109     }
00110 
00111     virtual void HandleVTable(CXXRecordDecl *RD, bool DefinitionRequired) {
00112       if (Diags.hasErrorOccurred())
00113         return;
00114 
00115       Builder->EmitVTable(RD, DefinitionRequired);
00116     }
00117   };
00118 }
00119 
00120 void CodeGenerator::anchor() { }
00121 
00122 CodeGenerator *clang::CreateLLVMCodeGen(DiagnosticsEngine &Diags,
00123                                         const std::string& ModuleName,
00124                                         const CodeGenOptions &CGO,
00125                                         llvm::LLVMContext& C) {
00126   return new CodeGeneratorImpl(Diags, ModuleName, CGO, C);
00127 }