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
This commit is contained in:
Chris Lattner 2008-02-06 02:01:47 +00:00
Родитель e66b65c3a3
Коммит 8ee3c0340f
8 изменённых файлов: 71 добавлений и 148 удалений

Просмотреть файл

@ -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<ScopedDecl>(d))
while (sd) {

Просмотреть файл

@ -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.

Просмотреть файл

@ -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);
#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"
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<FunctionDecl>(D)) {
Builder->EmitFunction(FD);
} else if (FileVarDecl *FVD = dyn_cast<FileVarDecl>(D)) {
Builder->EmitGlobalVarDeclarator(FVD);
} else if (LinkageSpecDecl *LSD = dyn_cast<LinkageSpecDecl>(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<TypeDecl>(D));
}
}
};
}
void clang::CodeGen::Terminate(CodeGenModule *B) {
delete B;
ASTConsumer *clang::CreateLLVMCodeGen(Diagnostic &Diags,
const LangOptions &Features,
llvm::Module *&DestModule) {
return new CodeGenerator(Diags, Features, DestModule);
}
/// 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.
}
/// CodeGenGlobalVar - Emit the specified global variable to LLVM.
void clang::CodeGen::CodeGenGlobalVar(CodeGenModule *Builder, FileVarDecl *D) {
Builder->EmitGlobalVarDeclarator(D);
}
/// CodeGenTypeDecl - Compile a type.
void clang::CodeGen::CodeGenTypeDecl(CodeGenModule *Builder, TypeDecl *D) {
Builder->EmitType(D);
}
/// PrintStats - Emit statistic information to stderr.
///
void clang::CodeGen::PrintStats(CodeGenModule *B) {
B->PrintStats();
}

Просмотреть файл

@ -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<FunctionDecl>(D)) {
CodeGen::CodeGenFunction(Builder, FD);
} else if (FileVarDecl *FVD = dyn_cast<FileVarDecl>(D)) {
CodeGen::CodeGenGlobalVar(Builder, FVD);
} else if (LinkageSpecDecl *LSD = dyn_cast<LinkageSpecDecl>(D)) {
CodeGen::CodeGenLinkageSpec(Builder, LSD);
} else {
CodeGen::CodeGenTypeDecl(Builder, cast<TypeDecl>(D));
}
}
};
}
ASTConsumer *clang::CreateLLVMCodeGen(Diagnostic &Diags,
const LangOptions &Features,
llvm::Module *&DestModule) {
return new CodeGenerator(Diags, Features, DestModule);
}
//===----------------------------------------------------------------------===//
// AST Serializer

Просмотреть файл

@ -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);

Просмотреть файл

@ -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"

Просмотреть файл

@ -19,8 +19,6 @@
#include "clang/Parse/Parser.h"
using namespace clang;
ASTConsumer::~ASTConsumer() {}
//===----------------------------------------------------------------------===//
// Public interface to the file
//===----------------------------------------------------------------------===//

Просмотреть файл

@ -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;
struct LangOptions;
class ASTConsumer;
/// 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);
/// 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