blob: 53ee2e45517b026d5e5f3be1bc4bad71858f88b1 [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +00001//===--- ModuleBuilder.cpp - Emit LLVM Code from ASTs ---------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner4b009652007-07-25 00:24:17 +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 Lattnerf5e9db02008-02-06 02:01:47 +000016#include "clang/AST/ASTConsumer.h"
17#include "clang/AST/ASTContext.h"
Chris Lattner806a5f52008-01-12 07:05:38 +000018#include "clang/AST/Decl.h"
Chris Lattner4b009652007-07-25 00:24:17 +000019using namespace clang;
20
Chris Lattnerf5e9db02008-02-06 02:01:47 +000021//===----------------------------------------------------------------------===//
22// LLVM Emitter
Chris Lattner4b009652007-07-25 00:24:17 +000023
Chris Lattnerf5e9db02008-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);
Steve Naroff72a6ebc2008-04-15 22:42:06 +000066 } else if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
67 if (VD->isFileVarDecl())
68 Builder->EmitGlobalVarDeclarator(VD);
Chris Lattnerb326b172008-03-30 23:03:07 +000069 } else if (isa<ObjCClassDecl>(D) || isa<ObjCCategoryDecl>(D)) {
70 // Forward declaration. Only used for type checking.
71 } else if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(D)){
72 Builder->EmitObjCMethod(OMD);
Chris Lattnerf5e9db02008-02-06 02:01:47 +000073 } else if (LinkageSpecDecl *LSD = dyn_cast<LinkageSpecDecl>(D)) {
74 if (LSD->getLanguage() == LinkageSpecDecl::lang_cxx)
75 Builder->WarnUnsupported(LSD, "linkage spec");
76 // FIXME: implement C++ linkage, C linkage works mostly by C
77 // language reuse already.
Anders Carlsson4f7f4412008-02-08 00:33:21 +000078 } else if (FileScopeAsmDecl *AD = dyn_cast<FileScopeAsmDecl>(D)) {
79 std::string AsmString(AD->getAsmString()->getStrData(),
80 AD->getAsmString()->getByteLength());
81
82 const std::string &S = Builder->getModule().getModuleInlineAsm();
83 if (S.empty())
84 Builder->getModule().setModuleInlineAsm(AsmString);
85 else
86 Builder->getModule().setModuleInlineAsm(S + '\n' + AsmString);
Chris Lattnerf5e9db02008-02-06 02:01:47 +000087 } else {
Chris Lattner08994a52008-02-06 04:51:19 +000088 assert(isa<TypeDecl>(D) && "Unknown top level decl");
89 // TODO: handle debug info?
Chris Lattnerf5e9db02008-02-06 02:01:47 +000090 }
91 }
Chris Lattner08994a52008-02-06 04:51:19 +000092
93 /// HandleTagDeclDefinition - This callback is invoked each time a TagDecl
94 /// (e.g. struct, union, enum, class) is completed. This allows the client to
95 /// hack on the type, which can occur at any point in the file (because these
96 /// can be defined in declspecs).
97 virtual void HandleTagDeclDefinition(TagDecl *D) {
Chris Lattner9ec3ca22008-02-06 05:08:19 +000098 Builder->UpdateCompletedType(D);
Chris Lattner08994a52008-02-06 04:51:19 +000099 }
100
Chris Lattnerf5e9db02008-02-06 02:01:47 +0000101 };
Chris Lattner4b009652007-07-25 00:24:17 +0000102}
103
Chris Lattnerf5e9db02008-02-06 02:01:47 +0000104ASTConsumer *clang::CreateLLVMCodeGen(Diagnostic &Diags,
105 const LangOptions &Features,
106 llvm::Module *&DestModule) {
107 return new CodeGenerator(Diags, Features, DestModule);
Chris Lattner4b009652007-07-25 00:24:17 +0000108}
109