1 //===--- ModuleBuilder.cpp - Emit LLVM Code from ASTs ---------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This builds an AST and converts it to LLVM Code.
12 //===----------------------------------------------------------------------===//
14 #include "clang/CodeGen/ModuleBuilder.h"
15 #include "CodeGenModule.h"
16 #include "clang/AST/ASTConsumer.h"
17 #include "clang/AST/ASTContext.h"
18 #include "clang/AST/Decl.h"
19 using namespace clang;
21 //===----------------------------------------------------------------------===//
24 #include "clang/Basic/Diagnostic.h"
25 #include "clang/Basic/TargetInfo.h"
26 #include "clang/CodeGen/ModuleBuilder.h"
27 #include "llvm/Module.h"
28 #include "llvm/Target/TargetData.h"
29 #include "llvm/Target/TargetMachine.h"
32 class CodeGenerator : public ASTConsumer {
34 const llvm::TargetData *TD;
36 const LangOptions &Features;
39 CodeGen::CodeGenModule *Builder;
41 CodeGenerator(Diagnostic &diags, const LangOptions &LO,
42 llvm::Module *&DestModule)
43 : Diags(diags), Features(LO), M(DestModule) {}
49 virtual void Initialize(ASTContext &Context) {
52 M->setTargetTriple(Ctx->Target.getTargetTriple());
53 M->setDataLayout(Ctx->Target.getTargetDescription());
54 TD = new llvm::TargetData(Ctx->Target.getTargetDescription());
55 Builder = new CodeGen::CodeGenModule(Context, Features, *M, *TD, Diags);
58 virtual void HandleTopLevelDecl(Decl *D) {
59 // If an error occurred, stop code generation, but continue parsing and
60 // semantic analysis (to ensure all warnings and errors are emitted).
61 if (Diags.hasErrorOccurred())
64 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
65 Builder->EmitFunction(FD);
66 } else if (FileVarDecl *FVD = dyn_cast<FileVarDecl>(D)) {
67 Builder->EmitGlobalVarDeclarator(FVD);
68 } else if (LinkageSpecDecl *LSD = dyn_cast<LinkageSpecDecl>(D)) {
69 if (LSD->getLanguage() == LinkageSpecDecl::lang_cxx)
70 Builder->WarnUnsupported(LSD, "linkage spec");
71 // FIXME: implement C++ linkage, C linkage works mostly by C
72 // language reuse already.
74 Builder->EmitType(cast<TypeDecl>(D));
80 ASTConsumer *clang::CreateLLVMCodeGen(Diagnostic &Diags,
81 const LangOptions &Features,
82 llvm::Module *&DestModule) {
83 return new CodeGenerator(Diags, Features, DestModule);