From: Chris Lattner Date: Wed, 6 Feb 2008 02:01:47 +0000 (+0000) Subject: move the codegen ASTConsumer out of the driver into libcodegen, X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=8ee3c0340ff10f0d98e2e138813ada3b36239038;p=clang move the codegen ASTConsumer out of the driver into libcodegen, eliminating a bunch of forwarding methods and generally simplifying things. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@46792 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/AST/ASTConsumer.cpp b/AST/ASTConsumer.cpp index dd839752ee..b3d1271092 100644 --- a/AST/ASTConsumer.cpp +++ b/AST/ASTConsumer.cpp @@ -13,9 +13,10 @@ #include "clang/AST/ASTConsumer.h" #include "clang/AST/Decl.h" - using namespace clang; +ASTConsumer::~ASTConsumer() {} + void ASTConsumer::HandleTopLevelDeclaration(Decl* d) { if (ScopedDecl* sd = dyn_cast(d)) while (sd) { diff --git a/CodeGen/CodeGenModule.h b/CodeGen/CodeGenModule.h index 5135c1b598..900ddf46bb 100644 --- a/CodeGen/CodeGenModule.h +++ b/CodeGen/CodeGenModule.h @@ -92,8 +92,6 @@ public: llvm::Constant *EmitGlobalInit(const Expr *E); llvm::Constant *EmitConstantExpr(const Expr *E); - void PrintStats() {} - /// WarnUnsupported - Print out a warning that codegen doesn't support the /// specified stmt yet. diff --git a/CodeGen/ModuleBuilder.cpp b/CodeGen/ModuleBuilder.cpp index b626c45272..c59ef447e6 100644 --- a/CodeGen/ModuleBuilder.cpp +++ b/CodeGen/ModuleBuilder.cpp @@ -13,51 +13,73 @@ #include "clang/CodeGen/ModuleBuilder.h" #include "CodeGenModule.h" +#include "clang/AST/ASTConsumer.h" +#include "clang/AST/ASTContext.h" #include "clang/AST/Decl.h" using namespace clang; +//===----------------------------------------------------------------------===// +// LLVM Emitter -/// Init - Create an ModuleBuilder with the specified ASTContext. -clang::CodeGen::CodeGenModule * -clang::CodeGen::Init(ASTContext &Context, const LangOptions &Features, - llvm::Module &M, const llvm::TargetData &TD, - Diagnostic &Diags) { - return new CodeGenModule(Context, Features, M, TD, Diags); -} - -void clang::CodeGen::Terminate(CodeGenModule *B) { - delete B; -} - -/// CodeGenFunction - Convert the AST node for a FunctionDecl into LLVM. -/// -void clang::CodeGen::CodeGenFunction(CodeGenModule *B, FunctionDecl *D) { - B->EmitFunction(D); -} - -/// CodeGenLinkageSpec - Emit the specified linkage space to LLVM. -void clang::CodeGen::CodeGenLinkageSpec(CodeGenModule *Builder, - LinkageSpecDecl *LS) { - if (LS->getLanguage() == LinkageSpecDecl::lang_cxx) - Builder->WarnUnsupported(LS, "linkage spec"); - - // FIXME: implement C++ linkage, C linkage works mostly by C - // language reuse already. -} +#include "clang/Basic/Diagnostic.h" +#include "clang/Basic/TargetInfo.h" +#include "clang/CodeGen/ModuleBuilder.h" +#include "llvm/Module.h" +#include "llvm/Target/TargetData.h" +#include "llvm/Target/TargetMachine.h" -/// CodeGenGlobalVar - Emit the specified global variable to LLVM. -void clang::CodeGen::CodeGenGlobalVar(CodeGenModule *Builder, FileVarDecl *D) { - Builder->EmitGlobalVarDeclarator(D); +namespace { + class CodeGenerator : public ASTConsumer { + Diagnostic &Diags; + const llvm::TargetData *TD; + ASTContext *Ctx; + const LangOptions &Features; + protected: + llvm::Module *&M; + CodeGen::CodeGenModule *Builder; + public: + CodeGenerator(Diagnostic &diags, const LangOptions &LO, + llvm::Module *&DestModule) + : Diags(diags), Features(LO), M(DestModule) {} + + ~CodeGenerator() { + delete Builder; + } + + virtual void Initialize(ASTContext &Context) { + Ctx = &Context; + + M->setTargetTriple(Ctx->Target.getTargetTriple()); + M->setDataLayout(Ctx->Target.getTargetDescription()); + TD = new llvm::TargetData(Ctx->Target.getTargetDescription()); + Builder = new CodeGen::CodeGenModule(Context, Features, *M, *TD, Diags); + } + + virtual void HandleTopLevelDecl(Decl *D) { + // If an error occurred, stop code generation, but continue parsing and + // semantic analysis (to ensure all warnings and errors are emitted). + if (Diags.hasErrorOccurred()) + return; + + if (FunctionDecl *FD = dyn_cast(D)) { + Builder->EmitFunction(FD); + } else if (FileVarDecl *FVD = dyn_cast(D)) { + Builder->EmitGlobalVarDeclarator(FVD); + } else if (LinkageSpecDecl *LSD = dyn_cast(D)) { + if (LSD->getLanguage() == LinkageSpecDecl::lang_cxx) + Builder->WarnUnsupported(LSD, "linkage spec"); + // FIXME: implement C++ linkage, C linkage works mostly by C + // language reuse already. + } else { + Builder->EmitType(cast(D)); + } + } + }; } -/// CodeGenTypeDecl - Compile a type. -void clang::CodeGen::CodeGenTypeDecl(CodeGenModule *Builder, TypeDecl *D) { - Builder->EmitType(D); +ASTConsumer *clang::CreateLLVMCodeGen(Diagnostic &Diags, + const LangOptions &Features, + llvm::Module *&DestModule) { + return new CodeGenerator(Diags, Features, DestModule); } - -/// PrintStats - Emit statistic information to stderr. -/// -void clang::CodeGen::PrintStats(CodeGenModule *B) { - B->PrintStats(); -} diff --git a/Driver/ASTConsumers.cpp b/Driver/ASTConsumers.cpp index 4dba24bdce..7b96a6c138 100644 --- a/Driver/ASTConsumers.cpp +++ b/Driver/ASTConsumers.cpp @@ -13,6 +13,7 @@ #include "ASTConsumers.h" #include "clang/AST/TranslationUnit.h" +#include "clang/Basic/Diagnostic.h" #include "clang/Basic/SourceManager.h" #include "clang/Basic/FileManager.h" #include "clang/AST/AST.h" @@ -587,69 +588,6 @@ void GRConstantsVisitor::VisitCFG(CFG& C, FunctionDecl& FD) { RunGRConstants(C, FD, *Ctx); } -//===----------------------------------------------------------------------===// -// LLVM Emitter - -#include "clang/Basic/Diagnostic.h" -#include "clang/Basic/TargetInfo.h" -#include "clang/CodeGen/ModuleBuilder.h" -#include "llvm/Module.h" -#include "llvm/Target/TargetData.h" -#include "llvm/Target/TargetMachine.h" -#include "llvm/Bitcode/ReaderWriter.h" - -namespace { - class CodeGenerator : public ASTConsumer { - Diagnostic &Diags; - const llvm::TargetData *TD; - ASTContext *Ctx; - const LangOptions &Features; - protected: - llvm::Module *&M; - CodeGen::CodeGenModule *Builder; - public: - CodeGenerator(Diagnostic &diags, const LangOptions &LO, - llvm::Module *&DestModule) - : Diags(diags), Features(LO), M(DestModule) {} - - ~CodeGenerator() { - CodeGen::Terminate(Builder); - } - - virtual void Initialize(ASTContext &Context) { - Ctx = &Context; - - M->setTargetTriple(Ctx->Target.getTargetTriple()); - M->setDataLayout(Ctx->Target.getTargetDescription()); - TD = new llvm::TargetData(Ctx->Target.getTargetDescription()); - Builder = CodeGen::Init(Context, Features, *M, *TD, Diags); - } - - virtual void HandleTopLevelDecl(Decl *D) { - // If an error occurred, stop code generation, but continue parsing and - // semantic analysis (to ensure all warnings and errors are emitted). - if (Diags.hasErrorOccurred()) - return; - - if (FunctionDecl *FD = dyn_cast(D)) { - CodeGen::CodeGenFunction(Builder, FD); - } else if (FileVarDecl *FVD = dyn_cast(D)) { - CodeGen::CodeGenGlobalVar(Builder, FVD); - } else if (LinkageSpecDecl *LSD = dyn_cast(D)) { - CodeGen::CodeGenLinkageSpec(Builder, LSD); - } else { - CodeGen::CodeGenTypeDecl(Builder, cast(D)); - } - } - }; -} - -ASTConsumer *clang::CreateLLVMCodeGen(Diagnostic &Diags, - const LangOptions &Features, - llvm::Module *&DestModule) { - return new CodeGenerator(Diags, Features, DestModule); -} - //===----------------------------------------------------------------------===// // AST Serializer diff --git a/Driver/ASTConsumers.h b/Driver/ASTConsumers.h index 3c7589b4fd..10ab1eb83c 100644 --- a/Driver/ASTConsumers.h +++ b/Driver/ASTConsumers.h @@ -43,10 +43,6 @@ ASTConsumer *CreateUnitValsChecker(Diagnostic &Diags); ASTConsumer *CreateGRConstants(); - -ASTConsumer *CreateLLVMCodeGen(Diagnostic &Diags, const LangOptions &Features, - llvm::Module *&DestModule); - ASTConsumer *CreateCodeRewriterTest(const std::string& InFile, Diagnostic &Diags); diff --git a/Driver/clang.cpp b/Driver/clang.cpp index 6e76a4a0cd..01ef28ff38 100644 --- a/Driver/clang.cpp +++ b/Driver/clang.cpp @@ -27,6 +27,7 @@ #include "TextDiagnosticBuffer.h" #include "TextDiagnosticPrinter.h" #include "clang/AST/TranslationUnit.h" +#include "clang/CodeGen/ModuleBuilder.h" #include "clang/Sema/ParseAST.h" #include "clang/AST/ASTConsumer.h" #include "clang/Parse/Parser.h" diff --git a/Sema/ParseAST.cpp b/Sema/ParseAST.cpp index 8fd14d17d1..364b072910 100644 --- a/Sema/ParseAST.cpp +++ b/Sema/ParseAST.cpp @@ -19,8 +19,6 @@ #include "clang/Parse/Parser.h" using namespace clang; -ASTConsumer::~ASTConsumer() {} - //===----------------------------------------------------------------------===// // Public interface to the file //===----------------------------------------------------------------------===// diff --git a/include/clang/CodeGen/ModuleBuilder.h b/include/clang/CodeGen/ModuleBuilder.h index ab08c4abc6..8abb377f80 100644 --- a/include/clang/CodeGen/ModuleBuilder.h +++ b/include/clang/CodeGen/ModuleBuilder.h @@ -16,46 +16,15 @@ namespace llvm { class Module; - class TargetData; } namespace clang { - class ASTContext; - class FunctionDecl; - class LinkageSpecDecl; - class FileVarDecl; - class TypeDecl; - struct LangOptions; class Diagnostic; - -namespace CodeGen { - class CodeGenModule; - - /// Init - Create an ModuleBuilder with the specified ASTContext. - CodeGenModule *Init(ASTContext &Context, const LangOptions &Features, - llvm::Module &M, const llvm::TargetData &TD, - Diagnostic &Diags); - - /// CodeGenFunction - Convert the AST node for a FunctionDecl into LLVM. - /// - void CodeGenFunction(CodeGenModule *Builder, FunctionDecl *D); - - void CodeGenLinkageSpec(CodeGenModule *Builder, LinkageSpecDecl *LS); - - /// CodeGenGlobalVar - Emit the specified global variable to LLVM. - void CodeGenGlobalVar(CodeGenModule *Builder, FileVarDecl *D); - - /// CodeGenTypeDecl - Compile a type. - void CodeGenTypeDecl(CodeGenModule *Builder, TypeDecl *D); - - /// PrintStats - Emit statistic information to stderr. - /// - void PrintStats(CodeGenModule *Builder); + struct LangOptions; + class ASTConsumer; - /// Terminate - Gracefully shut down the builder. - /// - void Terminate(CodeGenModule *Builder); -} // end namespace CodeGen -} // end namespace clang + ASTConsumer *CreateLLVMCodeGen(Diagnostic &Diags, const LangOptions &Features, + llvm::Module *&DestModule); +} #endif