blob: dafc0f5e25860975c9cd1dbca619896413d95622 [file] [log] [blame]
Chris Lattnerf97fe382007-05-24 06:29:05 +00001//===--- ModuleBuilder.cpp - Emit LLVM Code from ASTs ---------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-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 Lattnerf97fe382007-05-24 06:29:05 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This builds an AST and converts it to LLVM Code.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/CodeGen/ModuleBuilder.h"
Chris Lattnerbed31442007-05-28 01:07:47 +000015#include "CodeGenModule.h"
Chris Lattneradf1f512008-02-06 02:01:47 +000016#include "clang/AST/ASTContext.h"
Daniel Dunbar221fa942008-08-11 04:54:23 +000017#include "clang/AST/DeclObjC.h"
18#include "clang/AST/Expr.h"
Chris Lattnerf97fe382007-05-24 06:29:05 +000019using namespace clang;
20
Chris Lattneradf1f512008-02-06 02:01:47 +000021//===----------------------------------------------------------------------===//
22// LLVM Emitter
Chris Lattnerf97fe382007-05-24 06:29:05 +000023
Chris Lattneradf1f512008-02-06 02:01:47 +000024#include "clang/Basic/Diagnostic.h"
25#include "clang/Basic/TargetInfo.h"
Chris Lattneradf1f512008-02-06 02:01:47 +000026#include "llvm/Module.h"
27#include "llvm/Target/TargetData.h"
Ted Kremenek2c674f62008-08-05 18:50:11 +000028#include "llvm/Support/Compiler.h"
29#include "llvm/ADT/OwningPtr.h"
30
Chris Lattneradf1f512008-02-06 02:01:47 +000031
32namespace {
Ted Kremenek2c674f62008-08-05 18:50:11 +000033 class VISIBILITY_HIDDEN CodeGeneratorImpl : public CodeGenerator {
Chris Lattneradf1f512008-02-06 02:01:47 +000034 Diagnostic &Diags;
Ted Kremenek2c674f62008-08-05 18:50:11 +000035 llvm::OwningPtr<const llvm::TargetData> TD;
Chris Lattneradf1f512008-02-06 02:01:47 +000036 ASTContext *Ctx;
37 const LangOptions &Features;
Sanjiv Gupta15cb6692008-05-08 08:54:20 +000038 bool GenerateDebugInfo;
Chris Lattneradf1f512008-02-06 02:01:47 +000039 protected:
Ted Kremenek2c674f62008-08-05 18:50:11 +000040 llvm::OwningPtr<llvm::Module> M;
41 llvm::OwningPtr<CodeGen::CodeGenModule> Builder;
Chris Lattneradf1f512008-02-06 02:01:47 +000042 public:
Ted Kremenek2c674f62008-08-05 18:50:11 +000043 CodeGeneratorImpl(Diagnostic &diags, const LangOptions &LO,
44 const std::string& ModuleName,
45 bool DebugInfoFlag)
Sanjiv Gupta15cb6692008-05-08 08:54:20 +000046 : Diags(diags), Features(LO), GenerateDebugInfo(DebugInfoFlag),
Ted Kremenek2c674f62008-08-05 18:50:11 +000047 M(new llvm::Module(ModuleName)) {}
Chris Lattneradf1f512008-02-06 02:01:47 +000048
Ted Kremenek2c674f62008-08-05 18:50:11 +000049 virtual ~CodeGeneratorImpl() {}
50
Ted Kremenek7db4f602008-08-07 19:47:41 +000051 virtual llvm::Module* ReleaseModule() {
Ted Kremenek2c674f62008-08-05 18:50:11 +000052 return M.take();
Chris Lattneradf1f512008-02-06 02:01:47 +000053 }
54
55 virtual void Initialize(ASTContext &Context) {
56 Ctx = &Context;
57
58 M->setTargetTriple(Ctx->Target.getTargetTriple());
59 M->setDataLayout(Ctx->Target.getTargetDescription());
Ted Kremenek2c674f62008-08-05 18:50:11 +000060 TD.reset(new llvm::TargetData(Ctx->Target.getTargetDescription()));
61 Builder.reset(new CodeGen::CodeGenModule(Context, Features, *M, *TD,
Daniel Dunbar3ad53482008-08-11 21:35:06 +000062 Diags, GenerateDebugInfo));
Chris Lattneradf1f512008-02-06 02:01:47 +000063 }
64
65 virtual void HandleTopLevelDecl(Decl *D) {
Daniel Dunbarfce4be82008-08-15 23:26:23 +000066 // Make sure to emit all elements of a ScopedDecl.
Daniel Dunbar837fd272008-07-29 17:47:36 +000067 if (ScopedDecl *SD = dyn_cast<ScopedDecl>(D)) {
Daniel Dunbarfce4be82008-08-15 23:26:23 +000068 for (; SD; SD = SD->getNextDeclarator())
69 Builder->EmitTopLevelDecl(SD);
70 } else {
71 Builder->EmitTopLevelDecl(D);
Daniel Dunbar837fd272008-07-29 17:47:36 +000072 }
Chris Lattneradf1f512008-02-06 02:01:47 +000073 }
Daniel Dunbarfce4be82008-08-15 23:26:23 +000074
Chris Lattnera5e4d302008-02-06 04:51:19 +000075 /// HandleTagDeclDefinition - This callback is invoked each time a TagDecl
Nico Weber53c362e2008-08-09 23:08:17 +000076 /// (e.g. struct, union, enum, class) is completed. This allows the client to
Chris Lattnera5e4d302008-02-06 04:51:19 +000077 /// hack on the type, which can occur at any point in the file (because these
78 /// can be defined in declspecs).
79 virtual void HandleTagDeclDefinition(TagDecl *D) {
Chris Lattner68be6062008-02-06 05:08:19 +000080 Builder->UpdateCompletedType(D);
Chris Lattnera5e4d302008-02-06 04:51:19 +000081 }
Ted Kremenek7db4f602008-08-07 19:47:41 +000082
83 virtual void HandleTranslationUnit(TranslationUnit& TU) {
84 if (Diags.hasErrorOccurred()) {
85 M.reset();
86 return;
87 }
88
89 if (Builder)
90 Builder->Release();
91 };
Chris Lattneradf1f512008-02-06 02:01:47 +000092 };
Chris Lattnerf97fe382007-05-24 06:29:05 +000093}
94
Ted Kremenek2c674f62008-08-05 18:50:11 +000095CodeGenerator *clang::CreateLLVMCodeGen(Diagnostic &Diags,
96 const LangOptions &Features,
97 const std::string& ModuleName,
98 bool GenerateDebugInfo) {
99 return new CodeGeneratorImpl(Diags, Features, ModuleName, GenerateDebugInfo);
Chris Lattnerf97fe382007-05-24 06:29:05 +0000100}