blob: 9b85df61da0504cfe61e64df54d352a1b9fa178d [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 Lattnerbd360642009-03-26 05:00:52 +000016#include "clang/Frontend/CompileOptions.h"
Chris Lattner8ee3c032008-02-06 02:01:47 +000017#include "clang/AST/ASTContext.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000018#include "clang/AST/DeclObjC.h"
19#include "clang/AST/Expr.h"
Chris Lattner8ee3c032008-02-06 02:01:47 +000020#include "clang/Basic/Diagnostic.h"
21#include "clang/Basic/TargetInfo.h"
Chris Lattner8ee3c032008-02-06 02:01:47 +000022#include "llvm/Module.h"
23#include "llvm/Target/TargetData.h"
Ted Kremenek815c78f2008-08-05 18:50:11 +000024#include "llvm/Support/Compiler.h"
25#include "llvm/ADT/OwningPtr.h"
Chris Lattnerbd360642009-03-26 05:00:52 +000026using namespace clang;
Ted Kremenek815c78f2008-08-05 18:50:11 +000027
Chris Lattner8ee3c032008-02-06 02:01:47 +000028
29namespace {
Ted Kremenek815c78f2008-08-05 18:50:11 +000030 class VISIBILITY_HIDDEN CodeGeneratorImpl : public CodeGenerator {
Chris Lattner8ee3c032008-02-06 02:01:47 +000031 Diagnostic &Diags;
Ted Kremenek815c78f2008-08-05 18:50:11 +000032 llvm::OwningPtr<const llvm::TargetData> TD;
Chris Lattner8ee3c032008-02-06 02:01:47 +000033 ASTContext *Ctx;
Chris Lattnerbd360642009-03-26 05:00:52 +000034 const CompileOptions CompileOpts; // Intentionally copied in.
Chris Lattner8ee3c032008-02-06 02:01:47 +000035 protected:
Ted Kremenek815c78f2008-08-05 18:50:11 +000036 llvm::OwningPtr<llvm::Module> M;
37 llvm::OwningPtr<CodeGen::CodeGenModule> Builder;
Chris Lattner8ee3c032008-02-06 02:01:47 +000038 public:
Chris Lattnerbd360642009-03-26 05:00:52 +000039 CodeGeneratorImpl(Diagnostic &diags, const std::string& ModuleName,
40 const CompileOptions &CO)
41 : Diags(diags), CompileOpts(CO), M(new llvm::Module(ModuleName)) {}
Chris Lattner8ee3c032008-02-06 02:01:47 +000042
Ted Kremenek815c78f2008-08-05 18:50:11 +000043 virtual ~CodeGeneratorImpl() {}
44
Daniel Dunbard8c0ea12008-10-21 19:55:09 +000045 virtual llvm::Module* GetModule() {
46 return M.get();
47 }
48
Ted Kremenek159346a2008-08-07 19:47:41 +000049 virtual llvm::Module* ReleaseModule() {
Ted Kremenek815c78f2008-08-05 18:50:11 +000050 return M.take();
Chris Lattner8ee3c032008-02-06 02:01:47 +000051 }
52
53 virtual void Initialize(ASTContext &Context) {
54 Ctx = &Context;
55
56 M->setTargetTriple(Ctx->Target.getTargetTriple());
57 M->setDataLayout(Ctx->Target.getTargetDescription());
Ted Kremenek815c78f2008-08-05 18:50:11 +000058 TD.reset(new llvm::TargetData(Ctx->Target.getTargetDescription()));
Chris Lattnerbd360642009-03-26 05:00:52 +000059 Builder.reset(new CodeGen::CodeGenModule(Context, CompileOpts,
60 *M, *TD, Diags));
Chris Lattner8ee3c032008-02-06 02:01:47 +000061 }
62
Chris Lattner682bf922009-03-29 16:50:03 +000063 virtual void HandleTopLevelDecl(DeclGroupRef DG) {
Douglas Gregor4afa39d2009-01-20 01:17:11 +000064 // Make sure to emit all elements of a Decl.
Chris Lattner682bf922009-03-29 16:50:03 +000065 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
66 Builder->EmitTopLevelDecl(*I);
Chris Lattner8ee3c032008-02-06 02:01:47 +000067 }
Daniel Dunbar41071de2008-08-15 23:26:23 +000068
Chris Lattner9eee0f82008-02-06 04:51:19 +000069 /// HandleTagDeclDefinition - This callback is invoked each time a TagDecl
Chris Lattner682bf922009-03-29 16:50:03 +000070 /// to (e.g. struct, union, enum, class) is completed. This allows the
71 /// client hack on the type, which can occur at any point in the file
72 /// (because these can be defined in declspecs).
Chris Lattner9eee0f82008-02-06 04:51:19 +000073 virtual void HandleTagDeclDefinition(TagDecl *D) {
Chris Lattnerc5b88062008-02-06 05:08:19 +000074 Builder->UpdateCompletedType(D);
Chris Lattner9eee0f82008-02-06 04:51:19 +000075 }
Ted Kremenek159346a2008-08-07 19:47:41 +000076
Chris Lattnerdacbc5d2009-03-28 04:11:33 +000077 virtual void HandleTranslationUnit(ASTContext &Ctx) {
Ted Kremenek159346a2008-08-07 19:47:41 +000078 if (Diags.hasErrorOccurred()) {
79 M.reset();
80 return;
81 }
82
83 if (Builder)
84 Builder->Release();
85 };
Douglas Gregorb6c8c8b2009-04-21 17:11:58 +000086
87 virtual void CompleteTentativeDefinition(VarDecl *D) {
88 if (Diags.hasErrorOccurred())
89 return;
90
91 Builder->EmitTentativeDefinition(D);
92 }
Chris Lattner8ee3c032008-02-06 02:01:47 +000093 };
Reid Spencer5f016e22007-07-11 17:01:13 +000094}
95
Ted Kremenek815c78f2008-08-05 18:50:11 +000096CodeGenerator *clang::CreateLLVMCodeGen(Diagnostic &Diags,
Ted Kremenek815c78f2008-08-05 18:50:11 +000097 const std::string& ModuleName,
Chris Lattnerbd360642009-03-26 05:00:52 +000098 const CompileOptions &CO) {
99 return new CodeGeneratorImpl(Diags, ModuleName, CO);
Reid Spencer5f016e22007-07-11 17:01:13 +0000100}