blob: 06467488a5e945aec3974210c8ba72fba1a13105 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- ModuleBuilder.cpp - Emit LLVM Code from ASTs ---------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
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"
Chris Lattner8ee3c032008-02-06 02:01:47 +000016#include "clang/AST/ASTConsumer.h"
17#include "clang/AST/ASTContext.h"
Chris Lattnerc6fdc342008-01-12 07:05:38 +000018#include "clang/AST/Decl.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000019using namespace clang;
20
Chris Lattner8ee3c032008-02-06 02:01:47 +000021//===----------------------------------------------------------------------===//
22// LLVM Emitter
Reid Spencer5f016e22007-07-11 17:01:13 +000023
Chris Lattner8ee3c032008-02-06 02:01:47 +000024#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"
30
31namespace {
32 class CodeGenerator : public ASTConsumer {
33 Diagnostic &Diags;
34 const llvm::TargetData *TD;
35 ASTContext *Ctx;
36 const LangOptions &Features;
37 protected:
38 llvm::Module *&M;
39 CodeGen::CodeGenModule *Builder;
40 public:
41 CodeGenerator(Diagnostic &diags, const LangOptions &LO,
42 llvm::Module *&DestModule)
43 : Diags(diags), Features(LO), M(DestModule) {}
44
45 ~CodeGenerator() {
46 delete Builder;
47 }
48
49 virtual void Initialize(ASTContext &Context) {
50 Ctx = &Context;
51
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);
56 }
57
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())
62 return;
63
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.
Anders Carlssondfab6cb2008-02-08 00:33:21 +000073 } else if (FileScopeAsmDecl *AD = dyn_cast<FileScopeAsmDecl>(D)) {
74 std::string AsmString(AD->getAsmString()->getStrData(),
75 AD->getAsmString()->getByteLength());
76
77 const std::string &S = Builder->getModule().getModuleInlineAsm();
78 if (S.empty())
79 Builder->getModule().setModuleInlineAsm(AsmString);
80 else
81 Builder->getModule().setModuleInlineAsm(S + '\n' + AsmString);
Chris Lattner8ee3c032008-02-06 02:01:47 +000082 } else {
Chris Lattner9eee0f82008-02-06 04:51:19 +000083 assert(isa<TypeDecl>(D) && "Unknown top level decl");
84 // TODO: handle debug info?
Chris Lattner8ee3c032008-02-06 02:01:47 +000085 }
86 }
Chris Lattner9eee0f82008-02-06 04:51:19 +000087
88 /// HandleTagDeclDefinition - This callback is invoked each time a TagDecl
89 /// (e.g. struct, union, enum, class) is completed. This allows the client to
90 /// hack on the type, which can occur at any point in the file (because these
91 /// can be defined in declspecs).
92 virtual void HandleTagDeclDefinition(TagDecl *D) {
Chris Lattnerc5b88062008-02-06 05:08:19 +000093 Builder->UpdateCompletedType(D);
Chris Lattner9eee0f82008-02-06 04:51:19 +000094 }
95
Chris Lattner8ee3c032008-02-06 02:01:47 +000096 };
Reid Spencer5f016e22007-07-11 17:01:13 +000097}
98
Chris Lattner8ee3c032008-02-06 02:01:47 +000099ASTConsumer *clang::CreateLLVMCodeGen(Diagnostic &Diags,
100 const LangOptions &Features,
101 llvm::Module *&DestModule) {
102 return new CodeGenerator(Diags, Features, DestModule);
Reid Spencer5f016e22007-07-11 17:01:13 +0000103}
104