blob: be6e171bc453675982ff58b05370c74ea8b7c014 [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
Daniel Dunbar30c514e2008-10-21 19:55:09 +000051 virtual llvm::Module* GetModule() {
52 return M.get();
53 }
54
Ted Kremenek7db4f602008-08-07 19:47:41 +000055 virtual llvm::Module* ReleaseModule() {
Ted Kremenek2c674f62008-08-05 18:50:11 +000056 return M.take();
Chris Lattneradf1f512008-02-06 02:01:47 +000057 }
58
59 virtual void Initialize(ASTContext &Context) {
60 Ctx = &Context;
61
62 M->setTargetTriple(Ctx->Target.getTargetTriple());
63 M->setDataLayout(Ctx->Target.getTargetDescription());
Ted Kremenek2c674f62008-08-05 18:50:11 +000064 TD.reset(new llvm::TargetData(Ctx->Target.getTargetDescription()));
65 Builder.reset(new CodeGen::CodeGenModule(Context, Features, *M, *TD,
Daniel Dunbar3ad53482008-08-11 21:35:06 +000066 Diags, GenerateDebugInfo));
Chris Lattneradf1f512008-02-06 02:01:47 +000067 }
68
69 virtual void HandleTopLevelDecl(Decl *D) {
Daniel Dunbarfce4be82008-08-15 23:26:23 +000070 // Make sure to emit all elements of a ScopedDecl.
Daniel Dunbar837fd272008-07-29 17:47:36 +000071 if (ScopedDecl *SD = dyn_cast<ScopedDecl>(D)) {
Daniel Dunbarfce4be82008-08-15 23:26:23 +000072 for (; SD; SD = SD->getNextDeclarator())
73 Builder->EmitTopLevelDecl(SD);
74 } else {
75 Builder->EmitTopLevelDecl(D);
Daniel Dunbar837fd272008-07-29 17:47:36 +000076 }
Chris Lattneradf1f512008-02-06 02:01:47 +000077 }
Daniel Dunbarfce4be82008-08-15 23:26:23 +000078
Chris Lattnera5e4d302008-02-06 04:51:19 +000079 /// HandleTagDeclDefinition - This callback is invoked each time a TagDecl
Nico Weber53c362e2008-08-09 23:08:17 +000080 /// (e.g. struct, union, enum, class) is completed. This allows the client to
Chris Lattnera5e4d302008-02-06 04:51:19 +000081 /// hack on the type, which can occur at any point in the file (because these
82 /// can be defined in declspecs).
83 virtual void HandleTagDeclDefinition(TagDecl *D) {
Chris Lattner68be6062008-02-06 05:08:19 +000084 Builder->UpdateCompletedType(D);
Chris Lattnera5e4d302008-02-06 04:51:19 +000085 }
Ted Kremenek7db4f602008-08-07 19:47:41 +000086
87 virtual void HandleTranslationUnit(TranslationUnit& TU) {
88 if (Diags.hasErrorOccurred()) {
89 M.reset();
90 return;
91 }
92
93 if (Builder)
94 Builder->Release();
95 };
Chris Lattneradf1f512008-02-06 02:01:47 +000096 };
Chris Lattnerf97fe382007-05-24 06:29:05 +000097}
98
Ted Kremenek2c674f62008-08-05 18:50:11 +000099CodeGenerator *clang::CreateLLVMCodeGen(Diagnostic &Diags,
100 const LangOptions &Features,
101 const std::string& ModuleName,
102 bool GenerateDebugInfo) {
103 return new CodeGeneratorImpl(Diags, Features, ModuleName, GenerateDebugInfo);
Chris Lattnerf97fe382007-05-24 06:29:05 +0000104}