blob: 3a39eb3f79d320243161693fc767247d39a89146 [file] [log] [blame]
Daniel Dunbarbd012ff2008-07-29 23:18:29 +00001//===--- CodeGenModule.h - Per-Module state for LLVM CodeGen ----*- C++ -*-===//
Reid Spencer5f016e22007-07-11 17:01:13 +00002//
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 is the internal per-translation-unit state used for llvm translation.
11//
12//===----------------------------------------------------------------------===//
13
Chris Lattneref52a2f2008-02-29 17:10:38 +000014#ifndef CLANG_CODEGEN_CODEGENMODULE_H
15#define CLANG_CODEGEN_CODEGENMODULE_H
Reid Spencer5f016e22007-07-11 17:01:13 +000016
17#include "CodeGenTypes.h"
Chris Lattner2b94fe32008-03-01 08:45:05 +000018#include "CGObjCRuntime.h"
Dan Gohman4f8d1232008-05-22 00:50:06 +000019#include "clang/AST/Attr.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000020#include "llvm/ADT/DenseMap.h"
Anders Carlssonc9e20912007-08-21 00:21:21 +000021#include "llvm/ADT/StringMap.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000022
23namespace llvm {
24 class Module;
25 class Constant;
26 class Function;
Nate Begeman8bd4afe2008-04-19 04:17:09 +000027 class GlobalValue;
Devang Patel7a4718e2007-10-31 20:01:01 +000028 class TargetData;
Eli Friedmanff4a2d92008-06-01 15:54:49 +000029 class FunctionType;
Reid Spencer5f016e22007-07-11 17:01:13 +000030}
31
32namespace clang {
33 class ASTContext;
34 class FunctionDecl;
Chris Lattner391d77a2008-03-30 23:03:07 +000035 class ObjCMethodDecl;
Anton Korobeynikov20ff3102008-06-01 14:13:53 +000036 class ObjCImplementationDecl;
37 class ObjCCategoryImplDecl;
38 class ObjCProtocolDecl;
Reid Spencer5f016e22007-07-11 17:01:13 +000039 class Decl;
Oliver Hunt28247232007-12-02 00:11:25 +000040 class Expr;
Chris Lattner2c8569d2007-12-02 07:19:18 +000041 class Stmt;
Nate Begeman1a1d92a2008-04-20 20:38:08 +000042 class NamedDecl;
Daniel Dunbarbd012ff2008-07-29 23:18:29 +000043 class ValueDecl;
Chris Lattner2b9d2ca2007-12-18 08:16:44 +000044 class VarDecl;
Chris Lattner45e8cbd2007-11-28 05:34:05 +000045 struct LangOptions;
Chris Lattnerfb97b032007-12-02 01:40:18 +000046 class Diagnostic;
Nate Begeman8bd4afe2008-04-19 04:17:09 +000047 class AnnotateAttr;
Reid Spencer5f016e22007-07-11 17:01:13 +000048
49namespace CodeGen {
50
Lauro Ramos Venancio81373352008-02-26 21:41:45 +000051 class CodeGenFunction;
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000052 class CGDebugInfo;
53
Reid Spencer5f016e22007-07-11 17:01:13 +000054/// CodeGenModule - This class organizes the cross-module state that is used
55/// while generating LLVM code.
56class CodeGenModule {
57 ASTContext &Context;
Chris Lattner45e8cbd2007-11-28 05:34:05 +000058 const LangOptions &Features;
Reid Spencer5f016e22007-07-11 17:01:13 +000059 llvm::Module &TheModule;
Devang Patel7a4718e2007-10-31 20:01:01 +000060 const llvm::TargetData &TheTargetData;
Chris Lattnerfb97b032007-12-02 01:40:18 +000061 Diagnostic &Diags;
Reid Spencer5f016e22007-07-11 17:01:13 +000062 CodeGenTypes Types;
Chris Lattner2b94fe32008-03-01 08:45:05 +000063 CGObjCRuntime *Runtime;
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000064 CGDebugInfo *DebugInfo;
Reid Spencer5f016e22007-07-11 17:01:13 +000065
66 llvm::Function *MemCpyFn;
Eli Friedman0c995092008-05-26 12:59:39 +000067 llvm::Function *MemMoveFn;
Lauro Ramos Venancio41ef30e2008-02-19 22:01:01 +000068 llvm::Function *MemSetFn;
Daniel Dunbar9986eab2008-07-30 16:32:24 +000069
70 /// GlobalDeclMap - Mapping of decls to global variables we have
71 /// already emitted. Note that the entries in this map are the
72 /// actual global and therefore may not be of the same type as the
73 /// decl, they should be bitcasted on retrieval.
74 llvm::DenseMap<const Decl*, llvm::GlobalValue*> GlobalDeclMap;
Daniel Dunbarbd012ff2008-07-29 23:18:29 +000075
76 /// List of static global for which code generation is delayed. When
77 /// the translation unit has been fully processed we will lazily
78 /// emit definitions for only the decls that were actually used.
79 /// This should contain only Function and Var decls, and only those
80 /// which actually define something.
81 std::vector<const ValueDecl*> StaticDecls;
Nate Begeman4c13b7a2008-04-20 06:29:50 +000082
Chris Lattner6d397602008-03-14 17:18:18 +000083 std::vector<llvm::Constant*> GlobalCtors;
Nate Begeman532485c2008-04-18 23:43:57 +000084 std::vector<llvm::Constant*> Annotations;
Chris Lattner45e8cbd2007-11-28 05:34:05 +000085
Anders Carlssonc9e20912007-08-21 00:21:21 +000086 llvm::StringMap<llvm::Constant*> CFConstantStringMap;
Chris Lattner45e8cbd2007-11-28 05:34:05 +000087 llvm::StringMap<llvm::Constant*> ConstantStringMap;
Anders Carlssonc9e20912007-08-21 00:21:21 +000088 llvm::Constant *CFConstantStringClassRef;
Chris Lattnerbef20ac2007-08-31 04:31:45 +000089
90 std::vector<llvm::Function *> BuiltinFunctions;
Reid Spencer5f016e22007-07-11 17:01:13 +000091public:
Chris Lattner45e8cbd2007-11-28 05:34:05 +000092 CodeGenModule(ASTContext &C, const LangOptions &Features, llvm::Module &M,
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000093 const llvm::TargetData &TD, Diagnostic &Diags,
94 bool GenerateDebugInfo);
Chris Lattner2b94fe32008-03-01 08:45:05 +000095 ~CodeGenModule();
Reid Spencer5f016e22007-07-11 17:01:13 +000096
Chris Lattner2b94fe32008-03-01 08:45:05 +000097 CGObjCRuntime *getObjCRuntime() { return Runtime; }
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000098 CGDebugInfo *getDebugInfo() { return DebugInfo; }
Reid Spencer5f016e22007-07-11 17:01:13 +000099 ASTContext &getContext() const { return Context; }
Chris Lattner45e8cbd2007-11-28 05:34:05 +0000100 const LangOptions &getLangOptions() const { return Features; }
Reid Spencer5f016e22007-07-11 17:01:13 +0000101 llvm::Module &getModule() const { return TheModule; }
102 CodeGenTypes &getTypes() { return Types; }
Chris Lattnerfb97b032007-12-02 01:40:18 +0000103 Diagnostic &getDiags() const { return Diags; }
Chris Lattner8f925282008-01-03 06:36:51 +0000104 const llvm::TargetData &getTargetData() const { return TheTargetData; }
Daniel Dunbarbd012ff2008-07-29 23:18:29 +0000105
106 /// GetAddrOfGlobalVar - Return the llvm::Constant for the address
107 /// of the given global variable.
108 llvm::Constant *GetAddrOfGlobalVar(const VarDecl *D);
109
110 /// GetAddrOfFunction - Return the llvm::Constant for the address
111 /// of the given function.
112 llvm::Constant *GetAddrOfFunction(const FunctionDecl *D);
Chris Lattner58c3f9e2007-12-02 06:27:33 +0000113
Chris Lattnerbef20ac2007-08-31 04:31:45 +0000114 /// getBuiltinLibFunction - Given a builtin id for a function like
115 /// "__builtin_fabsf", return a Function* for "fabsf".
116 ///
117 llvm::Function *getBuiltinLibFunction(unsigned BuiltinID);
Anders Carlssonc9e20912007-08-21 00:21:21 +0000118 llvm::Constant *GetAddrOfConstantCFString(const std::string& str);
Chris Lattnera7ad98f2008-02-11 00:02:17 +0000119
120 /// GetAddrOfConstantString -- returns a pointer to the character
121 /// array containing the literal. The result is pointer to array type.
Chris Lattner45e8cbd2007-11-28 05:34:05 +0000122 llvm::Constant *GetAddrOfConstantString(const std::string& str);
Reid Spencer5f016e22007-07-11 17:01:13 +0000123 llvm::Function *getMemCpyFn();
Eli Friedman0c995092008-05-26 12:59:39 +0000124 llvm::Function *getMemMoveFn();
Lauro Ramos Venancio41ef30e2008-02-19 22:01:01 +0000125 llvm::Function *getMemSetFn();
Chris Lattner7acda7c2007-12-18 00:25:38 +0000126 llvm::Function *getIntrinsic(unsigned IID, const llvm::Type **Tys = 0,
127 unsigned NumTys = 0);
Chris Lattnerbef20ac2007-08-31 04:31:45 +0000128
Chris Lattner391d77a2008-03-30 23:03:07 +0000129 void EmitObjCMethod(const ObjCMethodDecl *OMD);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000130 void EmitObjCCategoryImpl(const ObjCCategoryImplDecl *OCD);
131 void EmitObjCClassImplementation(const ObjCImplementationDecl *OID);
132 void EmitObjCProtocolImplementation(const ObjCProtocolDecl *PD);
Daniel Dunbarbd012ff2008-07-29 23:18:29 +0000133
134 /// EmitGlobal - Emit code for a singal global function or var
135 /// decl. Forward declarations are emitted lazily.
136 void EmitGlobal(const ValueDecl *D);
137
138 void AddAnnotation(llvm::Constant *C) { Annotations.push_back(C); }
139
Chris Lattnerc5b88062008-02-06 05:08:19 +0000140 void UpdateCompletedType(const TagDecl *D);
Lauro Ramos Venancio81373352008-02-26 21:41:45 +0000141 llvm::Constant *EmitConstantExpr(const Expr *E, CodeGenFunction *CGF = 0);
Nate Begeman8bd4afe2008-04-19 04:17:09 +0000142 llvm::Constant *EmitAnnotateAttr(llvm::GlobalValue *GV,
143 const AnnotateAttr *AA, unsigned LineNo);
Anders Carlsson3b1d57b2008-01-26 01:36:00 +0000144
Chris Lattner2c8569d2007-12-02 07:19:18 +0000145 /// WarnUnsupported - Print out a warning that codegen doesn't support the
146 /// specified stmt yet.
Anders Carlsson3b1d57b2008-01-26 01:36:00 +0000147
Chris Lattner2c8569d2007-12-02 07:19:18 +0000148 void WarnUnsupported(const Stmt *S, const char *Type);
149
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000150 /// WarnUnsupported - Print out a warning that codegen doesn't support the
151 /// specified decl yet.
152 void WarnUnsupported(const Decl *D, const char *Type);
153
Dan Gohman4f8d1232008-05-22 00:50:06 +0000154 /// setVisibility - Set the visibility for the given LLVM GlobalValue
155 /// according to the given clang AST visibility value.
156 static void setVisibility(llvm::GlobalValue *GV,
157 VisibilityAttr::VisibilityTypes);
158
Chris Lattner9cd4fe42007-12-02 07:09:19 +0000159private:
160 /// ReplaceMapValuesWith - This is a really slow and bad function that
161 /// searches for any entries in GlobalDeclMap that point to OldVal, changing
162 /// them to point to NewVal. This is badbadbad, FIXME!
Daniel Dunbar9986eab2008-07-30 16:32:24 +0000163 void ReplaceMapValuesWith(llvm::GlobalValue *OldVal, llvm::GlobalValue *NewVal);
Eli Friedmanff4a2d92008-06-01 15:54:49 +0000164
165 void SetFunctionAttributes(const FunctionDecl *FD,
166 llvm::Function *F,
167 const llvm::FunctionType *FTy);
Nuno Lopesd4cbda62008-06-08 15:45:52 +0000168
169 void SetGlobalValueAttributes(const FunctionDecl *FD,
170 llvm::GlobalValue *GV);
Chris Lattner9cd4fe42007-12-02 07:09:19 +0000171
Daniel Dunbarbd012ff2008-07-29 23:18:29 +0000172 void EmitGlobalDefinition(const ValueDecl *D);
173 llvm::GlobalValue *EmitForwardFunctionDefinition(const FunctionDecl *D);
174 void EmitGlobalFunctionDefinition(const FunctionDecl *D);
175 void EmitGlobalVarDefinition(const VarDecl *D);
176
177 void AddGlobalCtor(llvm::Function * Ctor);
178 void EmitGlobalCtors(void);
179 void EmitAnnotations(void);
180 void EmitStatics(void);
181
Reid Spencer5f016e22007-07-11 17:01:13 +0000182};
183} // end namespace CodeGen
184} // end namespace clang
185
186#endif