blob: 7937e9296747486e4bf30e4a9570cd6da53804a7 [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 Lattnerf04a7562009-03-26 05:00:52 +000016#include "clang/Frontend/CompileOptions.h"
Chris Lattnerf5e9db02008-02-06 02:01:47 +000017#include "clang/AST/ASTContext.h"
Daniel Dunbarde300732008-08-11 04:54:23 +000018#include "clang/AST/DeclObjC.h"
19#include "clang/AST/Expr.h"
Chris Lattnerf5e9db02008-02-06 02:01:47 +000020#include "clang/Basic/Diagnostic.h"
21#include "clang/Basic/TargetInfo.h"
Owen Andersonab960ba2009-07-01 17:00:06 +000022#include "llvm/LLVMContext.h"
Chris Lattnerf5e9db02008-02-06 02:01:47 +000023#include "llvm/Module.h"
24#include "llvm/Target/TargetData.h"
Ted Kremenek7c65b6c2008-08-05 18:50:11 +000025#include "llvm/Support/Compiler.h"
26#include "llvm/ADT/OwningPtr.h"
Chris Lattnerf04a7562009-03-26 05:00:52 +000027using namespace clang;
Ted Kremenek7c65b6c2008-08-05 18:50:11 +000028
Chris Lattnerf5e9db02008-02-06 02:01:47 +000029
30namespace {
Ted Kremenek7c65b6c2008-08-05 18:50:11 +000031 class VISIBILITY_HIDDEN CodeGeneratorImpl : public CodeGenerator {
Chris Lattnerf5e9db02008-02-06 02:01:47 +000032 Diagnostic &Diags;
Ted Kremenek7c65b6c2008-08-05 18:50:11 +000033 llvm::OwningPtr<const llvm::TargetData> TD;
Chris Lattnerf5e9db02008-02-06 02:01:47 +000034 ASTContext *Ctx;
Chris Lattnerf04a7562009-03-26 05:00:52 +000035 const CompileOptions CompileOpts; // Intentionally copied in.
Chris Lattnerf5e9db02008-02-06 02:01:47 +000036 protected:
Ted Kremenek7c65b6c2008-08-05 18:50:11 +000037 llvm::OwningPtr<llvm::Module> M;
38 llvm::OwningPtr<CodeGen::CodeGenModule> Builder;
Chris Lattnerf5e9db02008-02-06 02:01:47 +000039 public:
Chris Lattnerf04a7562009-03-26 05:00:52 +000040 CodeGeneratorImpl(Diagnostic &diags, const std::string& ModuleName,
Owen Andersonab960ba2009-07-01 17:00:06 +000041 const CompileOptions &CO, llvm::LLVMContext* C)
42 : Diags(diags), CompileOpts(CO), M(new llvm::Module(ModuleName, C)) {}
Chris Lattnerf5e9db02008-02-06 02:01:47 +000043
Ted Kremenek7c65b6c2008-08-05 18:50:11 +000044 virtual ~CodeGeneratorImpl() {}
45
Daniel Dunbar984c2402008-10-21 19:55:09 +000046 virtual llvm::Module* GetModule() {
47 return M.get();
48 }
49
Ted Kremenek2866ea42008-08-07 19:47:41 +000050 virtual llvm::Module* ReleaseModule() {
Ted Kremenek7c65b6c2008-08-05 18:50:11 +000051 return M.take();
Chris Lattnerf5e9db02008-02-06 02:01:47 +000052 }
53
54 virtual void Initialize(ASTContext &Context) {
55 Ctx = &Context;
56
57 M->setTargetTriple(Ctx->Target.getTargetTriple());
58 M->setDataLayout(Ctx->Target.getTargetDescription());
Ted Kremenek7c65b6c2008-08-05 18:50:11 +000059 TD.reset(new llvm::TargetData(Ctx->Target.getTargetDescription()));
Chris Lattnerf04a7562009-03-26 05:00:52 +000060 Builder.reset(new CodeGen::CodeGenModule(Context, CompileOpts,
61 *M, *TD, Diags));
Chris Lattnerf5e9db02008-02-06 02:01:47 +000062 }
63
Chris Lattnera17991f2009-03-29 16:50:03 +000064 virtual void HandleTopLevelDecl(DeclGroupRef DG) {
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +000065 // Make sure to emit all elements of a Decl.
Chris Lattnera17991f2009-03-29 16:50:03 +000066 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
67 Builder->EmitTopLevelDecl(*I);
Chris Lattnerf5e9db02008-02-06 02:01:47 +000068 }
Daniel Dunbar27250d32008-08-15 23:26:23 +000069
Chris Lattner08994a52008-02-06 04:51:19 +000070 /// HandleTagDeclDefinition - This callback is invoked each time a TagDecl
Chris Lattnera17991f2009-03-29 16:50:03 +000071 /// to (e.g. struct, union, enum, class) is completed. This allows the
72 /// client hack on the type, which can occur at any point in the file
73 /// (because these can be defined in declspecs).
Chris Lattner08994a52008-02-06 04:51:19 +000074 virtual void HandleTagDeclDefinition(TagDecl *D) {
Chris Lattner9ec3ca22008-02-06 05:08:19 +000075 Builder->UpdateCompletedType(D);
Chris Lattner08994a52008-02-06 04:51:19 +000076 }
Ted Kremenek2866ea42008-08-07 19:47:41 +000077
Chris Lattner2a594d02009-03-28 04:11:33 +000078 virtual void HandleTranslationUnit(ASTContext &Ctx) {
Ted Kremenek2866ea42008-08-07 19:47:41 +000079 if (Diags.hasErrorOccurred()) {
80 M.reset();
81 return;
82 }
83
84 if (Builder)
85 Builder->Release();
86 };
Douglas Gregor9cdb4a12009-04-21 17:11:58 +000087
88 virtual void CompleteTentativeDefinition(VarDecl *D) {
89 if (Diags.hasErrorOccurred())
90 return;
91
92 Builder->EmitTentativeDefinition(D);
93 }
Chris Lattnerf5e9db02008-02-06 02:01:47 +000094 };
Chris Lattner4b009652007-07-25 00:24:17 +000095}
96
Ted Kremenek7c65b6c2008-08-05 18:50:11 +000097CodeGenerator *clang::CreateLLVMCodeGen(Diagnostic &Diags,
Ted Kremenek7c65b6c2008-08-05 18:50:11 +000098 const std::string& ModuleName,
Owen Andersonab960ba2009-07-01 17:00:06 +000099 const CompileOptions &CO,
100 llvm::LLVMContext* C) {
101 return new CodeGeneratorImpl(Diags, ModuleName, CO, C);
Chris Lattner4b009652007-07-25 00:24:17 +0000102}