blob: 57363cddee0bf212a8c92c881e1c23510488f2bd [file] [log] [blame]
Daniel Dunbar7bf5b3d2008-07-29 23:18:29 +00001//===--- CodeGenModule.h - Per-Module state for LLVM CodeGen ----*- C++ -*-===//
Chris Lattner4b009652007-07-25 00:24:17 +00002//
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 is the internal per-translation-unit state used for llvm translation.
11//
12//===----------------------------------------------------------------------===//
13
Chris Lattner2629b882008-02-29 17:10:38 +000014#ifndef CLANG_CODEGEN_CODEGENMODULE_H
15#define CLANG_CODEGEN_CODEGENMODULE_H
Chris Lattner4b009652007-07-25 00:24:17 +000016
17#include "CodeGenTypes.h"
Chris Lattnercbfb5512008-03-01 08:45:05 +000018#include "CGObjCRuntime.h"
Dan Gohman4751a3a2008-05-22 00:50:06 +000019#include "clang/AST/Attr.h"
Chris Lattner4b009652007-07-25 00:24:17 +000020#include "llvm/ADT/DenseMap.h"
Anders Carlsson36a04872007-08-21 00:21:21 +000021#include "llvm/ADT/StringMap.h"
Chris Lattner4b009652007-07-25 00:24:17 +000022
23namespace llvm {
24 class Module;
25 class Constant;
26 class Function;
Nate Begeman8a704172008-04-19 04:17:09 +000027 class GlobalValue;
Devang Patela8fccb82007-10-31 20:01:01 +000028 class TargetData;
Eli Friedman9be42212008-06-01 15:54:49 +000029 class FunctionType;
Chris Lattner4b009652007-07-25 00:24:17 +000030}
31
32namespace clang {
33 class ASTContext;
34 class FunctionDecl;
Chris Lattnerb326b172008-03-30 23:03:07 +000035 class ObjCMethodDecl;
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +000036 class ObjCImplementationDecl;
37 class ObjCCategoryImplDecl;
38 class ObjCProtocolDecl;
Chris Lattner4b009652007-07-25 00:24:17 +000039 class Decl;
Oliver Hunt253e0a72007-12-02 00:11:25 +000040 class Expr;
Chris Lattnercf9c9d02007-12-02 07:19:18 +000041 class Stmt;
Nate Begeman91f52012008-04-20 20:38:08 +000042 class NamedDecl;
Daniel Dunbar7bf5b3d2008-07-29 23:18:29 +000043 class ValueDecl;
Chris Lattnerd2df2b52007-12-18 08:16:44 +000044 class VarDecl;
Chris Lattnerdb6be562007-11-28 05:34:05 +000045 struct LangOptions;
Chris Lattner22595b82007-12-02 01:40:18 +000046 class Diagnostic;
Nate Begeman8a704172008-04-19 04:17:09 +000047 class AnnotateAttr;
Chris Lattner4b009652007-07-25 00:24:17 +000048
49namespace CodeGen {
50
Lauro Ramos Venancio934fb022008-02-26 21:41:45 +000051 class CodeGenFunction;
Sanjiv Gupta40e56a12008-05-08 08:54:20 +000052 class CGDebugInfo;
53
Chris Lattner4b009652007-07-25 00:24:17 +000054/// CodeGenModule - This class organizes the cross-module state that is used
55/// while generating LLVM code.
56class CodeGenModule {
57 ASTContext &Context;
Chris Lattnerdb6be562007-11-28 05:34:05 +000058 const LangOptions &Features;
Chris Lattner4b009652007-07-25 00:24:17 +000059 llvm::Module &TheModule;
Devang Patela8fccb82007-10-31 20:01:01 +000060 const llvm::TargetData &TheTargetData;
Chris Lattner22595b82007-12-02 01:40:18 +000061 Diagnostic &Diags;
Chris Lattner4b009652007-07-25 00:24:17 +000062 CodeGenTypes Types;
Chris Lattnercbfb5512008-03-01 08:45:05 +000063 CGObjCRuntime *Runtime;
Sanjiv Gupta40e56a12008-05-08 08:54:20 +000064 CGDebugInfo *DebugInfo;
Chris Lattner4b009652007-07-25 00:24:17 +000065
66 llvm::Function *MemCpyFn;
Eli Friedman8f08a252008-05-26 12:59:39 +000067 llvm::Function *MemMoveFn;
Lauro Ramos Venancioe5bef732008-02-19 22:01:01 +000068 llvm::Function *MemSetFn;
Chris Lattner4b009652007-07-25 00:24:17 +000069 llvm::DenseMap<const Decl*, llvm::Constant*> GlobalDeclMap;
Daniel Dunbar7bf5b3d2008-07-29 23:18:29 +000070
71 /// List of static global for which code generation is delayed. When
72 /// the translation unit has been fully processed we will lazily
73 /// emit definitions for only the decls that were actually used.
74 /// This should contain only Function and Var decls, and only those
75 /// which actually define something.
76 std::vector<const ValueDecl*> StaticDecls;
Nate Begemanad320b62008-04-20 06:29:50 +000077
Chris Lattner753d2592008-03-14 17:18:18 +000078 std::vector<llvm::Constant*> GlobalCtors;
Nate Begeman52da5c72008-04-18 23:43:57 +000079 std::vector<llvm::Constant*> Annotations;
Chris Lattnerdb6be562007-11-28 05:34:05 +000080
Anders Carlsson36a04872007-08-21 00:21:21 +000081 llvm::StringMap<llvm::Constant*> CFConstantStringMap;
Chris Lattnerdb6be562007-11-28 05:34:05 +000082 llvm::StringMap<llvm::Constant*> ConstantStringMap;
Anders Carlsson36a04872007-08-21 00:21:21 +000083 llvm::Constant *CFConstantStringClassRef;
Chris Lattnerab862cc2007-08-31 04:31:45 +000084
85 std::vector<llvm::Function *> BuiltinFunctions;
Chris Lattner4b009652007-07-25 00:24:17 +000086public:
Chris Lattnerdb6be562007-11-28 05:34:05 +000087 CodeGenModule(ASTContext &C, const LangOptions &Features, llvm::Module &M,
Sanjiv Gupta40e56a12008-05-08 08:54:20 +000088 const llvm::TargetData &TD, Diagnostic &Diags,
89 bool GenerateDebugInfo);
Chris Lattnercbfb5512008-03-01 08:45:05 +000090 ~CodeGenModule();
Chris Lattner4b009652007-07-25 00:24:17 +000091
Chris Lattnercbfb5512008-03-01 08:45:05 +000092 CGObjCRuntime *getObjCRuntime() { return Runtime; }
Sanjiv Gupta40e56a12008-05-08 08:54:20 +000093 CGDebugInfo *getDebugInfo() { return DebugInfo; }
Chris Lattner4b009652007-07-25 00:24:17 +000094 ASTContext &getContext() const { return Context; }
Chris Lattnerdb6be562007-11-28 05:34:05 +000095 const LangOptions &getLangOptions() const { return Features; }
Chris Lattner4b009652007-07-25 00:24:17 +000096 llvm::Module &getModule() const { return TheModule; }
97 CodeGenTypes &getTypes() { return Types; }
Chris Lattner22595b82007-12-02 01:40:18 +000098 Diagnostic &getDiags() const { return Diags; }
Chris Lattner17c0cb02008-01-03 06:36:51 +000099 const llvm::TargetData &getTargetData() const { return TheTargetData; }
Daniel Dunbar7bf5b3d2008-07-29 23:18:29 +0000100
101 /// GetAddrOfGlobalVar - Return the llvm::Constant for the address
102 /// of the given global variable.
103 llvm::Constant *GetAddrOfGlobalVar(const VarDecl *D);
104
105 /// GetAddrOfFunction - Return the llvm::Constant for the address
106 /// of the given function.
107 llvm::Constant *GetAddrOfFunction(const FunctionDecl *D);
Chris Lattner0e4755d2007-12-02 06:27:33 +0000108
Chris Lattnerab862cc2007-08-31 04:31:45 +0000109 /// getBuiltinLibFunction - Given a builtin id for a function like
110 /// "__builtin_fabsf", return a Function* for "fabsf".
111 ///
112 llvm::Function *getBuiltinLibFunction(unsigned BuiltinID);
Anders Carlsson36a04872007-08-21 00:21:21 +0000113 llvm::Constant *GetAddrOfConstantCFString(const std::string& str);
Chris Lattnera6dcce32008-02-11 00:02:17 +0000114
115 /// GetAddrOfConstantString -- returns a pointer to the character
116 /// array containing the literal. The result is pointer to array type.
Chris Lattnerdb6be562007-11-28 05:34:05 +0000117 llvm::Constant *GetAddrOfConstantString(const std::string& str);
Chris Lattner4b009652007-07-25 00:24:17 +0000118 llvm::Function *getMemCpyFn();
Eli Friedman8f08a252008-05-26 12:59:39 +0000119 llvm::Function *getMemMoveFn();
Lauro Ramos Venancioe5bef732008-02-19 22:01:01 +0000120 llvm::Function *getMemSetFn();
Chris Lattner4b23f942007-12-18 00:25:38 +0000121 llvm::Function *getIntrinsic(unsigned IID, const llvm::Type **Tys = 0,
122 unsigned NumTys = 0);
Chris Lattnerab862cc2007-08-31 04:31:45 +0000123
Chris Lattnerb326b172008-03-30 23:03:07 +0000124 void EmitObjCMethod(const ObjCMethodDecl *OMD);
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000125 void EmitObjCCategoryImpl(const ObjCCategoryImplDecl *OCD);
126 void EmitObjCClassImplementation(const ObjCImplementationDecl *OID);
127 void EmitObjCProtocolImplementation(const ObjCProtocolDecl *PD);
Daniel Dunbar7bf5b3d2008-07-29 23:18:29 +0000128
129 /// EmitGlobal - Emit code for a singal global function or var
130 /// decl. Forward declarations are emitted lazily.
131 void EmitGlobal(const ValueDecl *D);
132
133 void AddAnnotation(llvm::Constant *C) { Annotations.push_back(C); }
134
Chris Lattner9ec3ca22008-02-06 05:08:19 +0000135 void UpdateCompletedType(const TagDecl *D);
Lauro Ramos Venancio934fb022008-02-26 21:41:45 +0000136 llvm::Constant *EmitConstantExpr(const Expr *E, CodeGenFunction *CGF = 0);
Nate Begeman8a704172008-04-19 04:17:09 +0000137 llvm::Constant *EmitAnnotateAttr(llvm::GlobalValue *GV,
138 const AnnotateAttr *AA, unsigned LineNo);
Anders Carlssond76cead2008-01-26 01:36:00 +0000139
Chris Lattnercf9c9d02007-12-02 07:19:18 +0000140 /// WarnUnsupported - Print out a warning that codegen doesn't support the
141 /// specified stmt yet.
Anders Carlssond76cead2008-01-26 01:36:00 +0000142
Chris Lattnercf9c9d02007-12-02 07:19:18 +0000143 void WarnUnsupported(const Stmt *S, const char *Type);
144
Chris Lattner806a5f52008-01-12 07:05:38 +0000145 /// WarnUnsupported - Print out a warning that codegen doesn't support the
146 /// specified decl yet.
147 void WarnUnsupported(const Decl *D, const char *Type);
148
Dan Gohman4751a3a2008-05-22 00:50:06 +0000149 /// setVisibility - Set the visibility for the given LLVM GlobalValue
150 /// according to the given clang AST visibility value.
151 static void setVisibility(llvm::GlobalValue *GV,
152 VisibilityAttr::VisibilityTypes);
153
Chris Lattner1a3c1e22007-12-02 07:09:19 +0000154private:
155 /// ReplaceMapValuesWith - This is a really slow and bad function that
156 /// searches for any entries in GlobalDeclMap that point to OldVal, changing
157 /// them to point to NewVal. This is badbadbad, FIXME!
158 void ReplaceMapValuesWith(llvm::Constant *OldVal, llvm::Constant *NewVal);
Eli Friedman9be42212008-06-01 15:54:49 +0000159
160 void SetFunctionAttributes(const FunctionDecl *FD,
161 llvm::Function *F,
162 const llvm::FunctionType *FTy);
Nuno Lopes78534382008-06-08 15:45:52 +0000163
164 void SetGlobalValueAttributes(const FunctionDecl *FD,
165 llvm::GlobalValue *GV);
Chris Lattner1a3c1e22007-12-02 07:09:19 +0000166
Daniel Dunbar7bf5b3d2008-07-29 23:18:29 +0000167 void EmitGlobalDefinition(const ValueDecl *D);
168 llvm::GlobalValue *EmitForwardFunctionDefinition(const FunctionDecl *D);
169 void EmitGlobalFunctionDefinition(const FunctionDecl *D);
170 void EmitGlobalVarDefinition(const VarDecl *D);
171
172 void AddGlobalCtor(llvm::Function * Ctor);
173 void EmitGlobalCtors(void);
174 void EmitAnnotations(void);
175 void EmitStatics(void);
176
Chris Lattner4b009652007-07-25 00:24:17 +0000177};
178} // end namespace CodeGen
179} // end namespace clang
180
181#endif