blob: 7b467291e42266f2b21541fa3e9d37582bcb6bfa [file] [log] [blame]
Daniel Dunbar9c426522008-07-29 23:18:29 +00001//===--- CodeGenModule.h - Per-Module state for LLVM CodeGen ----*- C++ -*-===//
Chris Lattnerf97fe382007-05-24 06:29:05 +00002//
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//
Mike Stump1676c5b2009-02-13 19:12:34 +000010// This is the internal per-translation-unit state used for llvm translation.
Chris Lattnerf97fe382007-05-24 06:29:05 +000011//
12//===----------------------------------------------------------------------===//
13
Chris Lattnerc18bfbb2008-02-29 17:10:38 +000014#ifndef CLANG_CODEGEN_CODEGENMODULE_H
15#define CLANG_CODEGEN_CODEGENMODULE_H
Chris Lattnerf97fe382007-05-24 06:29:05 +000016
Chandler Carruth3a022472012-12-04 09:13:33 +000017#include "CGVTables.h"
18#include "CodeGenTypes.h"
Dan Gohman75d69da2008-05-22 00:50:06 +000019#include "clang/AST/Attr.h"
Anders Carlssondae1abc2009-05-05 04:44:02 +000020#include "clang/AST/DeclCXX.h"
Anders Carlsson73fcc952009-09-11 00:07:24 +000021#include "clang/AST/DeclObjC.h"
Peter Collingbourneee781d52011-06-14 04:02:39 +000022#include "clang/AST/GlobalDecl.h"
Peter Collingbourne0ff0b372011-01-13 18:57:25 +000023#include "clang/AST/Mangle.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000024#include "clang/Basic/ABI.h"
25#include "clang/Basic/LangOptions.h"
Douglas Gregor6ddfca92013-01-14 17:21:00 +000026#include "clang/Basic/Module.h"
Chris Lattnerb6984c42007-06-20 04:44:43 +000027#include "llvm/ADT/DenseMap.h"
Douglas Gregor6ddfca92013-01-14 17:21:00 +000028#include "llvm/ADT/SetVector.h"
Rafael Espindola2e42fec2010-03-04 18:17:24 +000029#include "llvm/ADT/SmallPtrSet.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000030#include "llvm/ADT/StringMap.h"
John McCall882987f2013-02-28 19:01:20 +000031#include "llvm/IR/CallingConv.h"
Chandler Carruthffd55512013-01-02 11:45:17 +000032#include "llvm/IR/Module.h"
Chandler Carruth61743af2014-03-04 11:18:19 +000033#include "llvm/IR/ValueHandle.h"
Peter Collingbourne41148a82013-07-09 22:03:30 +000034#include "llvm/Transforms/Utils/SpecialCaseList.h"
Anders Carlsson762e1622009-01-04 02:08:04 +000035
Chris Lattnerf97fe382007-05-24 06:29:05 +000036namespace llvm {
Rafael Espindola42ae7452014-05-09 00:57:59 +000037class Module;
38class Constant;
39class ConstantInt;
40class Function;
41class GlobalValue;
42class DataLayout;
43class FunctionType;
44class LLVMContext;
45class IndexedInstrProfReader;
Chris Lattner23b7eb62007-06-15 23:05:46 +000046}
47
Chris Lattnerf97fe382007-05-24 06:29:05 +000048namespace clang {
Rafael Espindola42ae7452014-05-09 00:57:59 +000049class TargetCodeGenInfo;
50class ASTContext;
51class AtomicType;
52class FunctionDecl;
53class IdentifierInfo;
54class ObjCMethodDecl;
55class ObjCImplementationDecl;
56class ObjCCategoryImplDecl;
57class ObjCProtocolDecl;
58class ObjCEncodeExpr;
59class BlockExpr;
60class CharUnits;
61class Decl;
62class Expr;
63class Stmt;
64class InitListExpr;
65class StringLiteral;
66class NamedDecl;
67class ValueDecl;
68class VarDecl;
69class LangOptions;
70class CodeGenOptions;
71class DiagnosticsEngine;
72class AnnotateAttr;
73class CXXDestructorDecl;
74class MangleBuffer;
75class Module;
Mike Stump1676c5b2009-02-13 19:12:34 +000076
Chris Lattnerf97fe382007-05-24 06:29:05 +000077namespace CodeGen {
78
Rafael Espindola42ae7452014-05-09 00:57:59 +000079class CallArgList;
80class CodeGenFunction;
81class CodeGenTBAA;
82class CGCXXABI;
83class CGDebugInfo;
84class CGObjCRuntime;
85class CGOpenCLRuntime;
86class CGOpenMPRuntime;
87class CGCUDARuntime;
88class BlockFieldFlags;
89class FunctionArgList;
Justin Bogneref512b92014-01-06 22:27:43 +000090
Rafael Espindola42ae7452014-05-09 00:57:59 +000091struct OrderGlobalInits {
92 unsigned int priority;
93 unsigned int lex_order;
94 OrderGlobalInits(unsigned int p, unsigned int l)
Chris Lattnere0009072010-06-27 06:32:58 +000095 : priority(p), lex_order(l) {}
Rafael Espindola42ae7452014-05-09 00:57:59 +000096
97 bool operator==(const OrderGlobalInits &RHS) const {
98 return priority == RHS.priority && lex_order == RHS.lex_order;
99 }
100
101 bool operator<(const OrderGlobalInits &RHS) const {
102 return std::tie(priority, lex_order) <
103 std::tie(RHS.priority, RHS.lex_order);
104 }
105};
106
107struct CodeGenTypeCache {
108 /// void
109 llvm::Type *VoidTy;
110
111 /// i8, i16, i32, and i64
112 llvm::IntegerType *Int8Ty, *Int16Ty, *Int32Ty, *Int64Ty;
113 /// float, double
114 llvm::Type *FloatTy, *DoubleTy;
115
116 /// int
117 llvm::IntegerType *IntTy;
118
119 /// intptr_t, size_t, and ptrdiff_t, which we assume are the same size.
120 union {
121 llvm::IntegerType *IntPtrTy;
122 llvm::IntegerType *SizeTy;
123 llvm::IntegerType *PtrDiffTy;
Chris Lattnere0009072010-06-27 06:32:58 +0000124 };
John McCalle3dc1702011-02-15 09:22:45 +0000125
Rafael Espindola42ae7452014-05-09 00:57:59 +0000126 /// void* in address space 0
127 union {
128 llvm::PointerType *VoidPtrTy;
129 llvm::PointerType *Int8PtrTy;
John McCalle3dc1702011-02-15 09:22:45 +0000130 };
John McCall31168b02011-06-15 23:02:42 +0000131
Rafael Espindola42ae7452014-05-09 00:57:59 +0000132 /// void** in address space 0
133 union {
134 llvm::PointerType *VoidPtrPtrTy;
135 llvm::PointerType *Int8PtrPtrTy;
136 };
137
138 /// The width of a pointer into the generic address space.
139 unsigned char PointerWidthInBits;
140
141 /// The size and alignment of a pointer into the generic address
142 /// space.
143 union {
144 unsigned char PointerAlignInBytes;
145 unsigned char PointerSizeInBytes;
146 unsigned char SizeSizeInBytes; // sizeof(size_t)
147 };
148
149 llvm::CallingConv::ID RuntimeCC;
150 llvm::CallingConv::ID getRuntimeCC() const { return RuntimeCC; }
151};
152
John McCall31168b02011-06-15 23:02:42 +0000153struct RREntrypoints {
154 RREntrypoints() { memset(this, 0, sizeof(*this)); }
155 /// void objc_autoreleasePoolPop(void*);
156 llvm::Constant *objc_autoreleasePoolPop;
157
158 /// void *objc_autoreleasePoolPush(void);
159 llvm::Constant *objc_autoreleasePoolPush;
160};
161
162struct ARCEntrypoints {
163 ARCEntrypoints() { memset(this, 0, sizeof(*this)); }
164
165 /// id objc_autorelease(id);
166 llvm::Constant *objc_autorelease;
167
168 /// id objc_autoreleaseReturnValue(id);
169 llvm::Constant *objc_autoreleaseReturnValue;
170
171 /// void objc_copyWeak(id *dest, id *src);
172 llvm::Constant *objc_copyWeak;
173
174 /// void objc_destroyWeak(id*);
175 llvm::Constant *objc_destroyWeak;
176
177 /// id objc_initWeak(id*, id);
178 llvm::Constant *objc_initWeak;
179
180 /// id objc_loadWeak(id*);
181 llvm::Constant *objc_loadWeak;
182
183 /// id objc_loadWeakRetained(id*);
184 llvm::Constant *objc_loadWeakRetained;
185
186 /// void objc_moveWeak(id *dest, id *src);
187 llvm::Constant *objc_moveWeak;
188
189 /// id objc_retain(id);
190 llvm::Constant *objc_retain;
191
192 /// id objc_retainAutorelease(id);
193 llvm::Constant *objc_retainAutorelease;
194
195 /// id objc_retainAutoreleaseReturnValue(id);
196 llvm::Constant *objc_retainAutoreleaseReturnValue;
197
198 /// id objc_retainAutoreleasedReturnValue(id);
199 llvm::Constant *objc_retainAutoreleasedReturnValue;
200
201 /// id objc_retainBlock(id);
202 llvm::Constant *objc_retainBlock;
203
204 /// void objc_release(id);
205 llvm::Constant *objc_release;
206
207 /// id objc_storeStrong(id*, id);
208 llvm::Constant *objc_storeStrong;
209
210 /// id objc_storeWeak(id*, id);
211 llvm::Constant *objc_storeWeak;
212
213 /// A void(void) inline asm to use to mark that the return value of
214 /// a call will be immediately retain.
215 llvm::InlineAsm *retainAutoreleasedReturnValueMarker;
John McCalleff18842013-03-23 02:35:54 +0000216
217 /// void clang.arc.use(...);
218 llvm::Constant *clang_arc_use;
John McCall31168b02011-06-15 23:02:42 +0000219};
Will Dietzf54319c2013-01-18 11:30:38 +0000220
Justin Bognere2ef2a02014-04-15 21:22:35 +0000221/// This class records statistics on instrumentation based profiling.
222struct InstrProfStats {
223 InstrProfStats() : Visited(0), Missing(0), Mismatched(0) {}
224 bool isOutOfDate() { return Missing || Mismatched; }
225 uint32_t Visited;
226 uint32_t Missing;
227 uint32_t Mismatched;
228};
229
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000230/// This class organizes the cross-function state that is used while generating
231/// LLVM code.
John McCalle3dc1702011-02-15 09:22:45 +0000232class CodeGenModule : public CodeGenTypeCache {
Dmitri Gribenkoa664e5b2012-09-15 20:20:27 +0000233 CodeGenModule(const CodeGenModule &) LLVM_DELETED_FUNCTION;
234 void operator=(const CodeGenModule &) LLVM_DELETED_FUNCTION;
Anders Carlsson729a8202009-02-24 04:21:31 +0000235
Chris Lattneree7bd3b2009-09-12 22:45:21 +0000236 typedef std::vector<std::pair<llvm::Constant*, int> > CtorList;
Daniel Dunbar74aa7e12008-08-01 00:01:51 +0000237
Chris Lattnerf97fe382007-05-24 06:29:05 +0000238 ASTContext &Context;
David Blaikiebbafb8a2012-03-11 07:00:24 +0000239 const LangOptions &LangOpts;
Chandler Carruthbc55fe22009-11-12 17:24:48 +0000240 const CodeGenOptions &CodeGenOpts;
Chris Lattner23b7eb62007-06-15 23:05:46 +0000241 llvm::Module &TheModule;
David Blaikie9c902b52011-09-25 23:23:43 +0000242 DiagnosticsEngine &Diags;
John McCallc8e01702013-04-16 22:48:15 +0000243 const llvm::DataLayout &TheDataLayout;
244 const TargetInfo &Target;
Ahmed Charlesb8984322014-03-07 20:03:18 +0000245 std::unique_ptr<CGCXXABI> ABI;
John McCallc8e01702013-04-16 22:48:15 +0000246 llvm::LLVMContext &VMContext;
Anders Carlssonff971e82009-10-07 01:06:45 +0000247
John McCallc8e01702013-04-16 22:48:15 +0000248 CodeGenTBAA *TBAA;
249
250 mutable const TargetCodeGenInfo *TheTargetCodeGenInfo;
251
252 // This should not be moved earlier, since its initialization depends on some
253 // of the previous reference members being already initialized and also checks
254 // if TheTargetCodeGenInfo is NULL
255 CodeGenTypes Types;
256
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000257 /// Holds information about C++ vtables.
Anders Carlssona864caf2010-03-23 04:11:45 +0000258 CodeGenVTables VTables;
Douglas Gregorbc6a4342010-04-08 16:07:47 +0000259
Peter Collingbournee1d20992011-07-27 20:29:46 +0000260 CGObjCRuntime* ObjCRuntime;
Peter Collingbourne2dbb7082011-09-19 21:14:35 +0000261 CGOpenCLRuntime* OpenCLRuntime;
Alexey Bataev9959db52014-05-06 10:08:46 +0000262 CGOpenMPRuntime* OpenMPRuntime;
Peter Collingbournefe883422011-10-06 18:29:37 +0000263 CGCUDARuntime* CUDARuntime;
Ted Kremenek2c674f62008-08-05 18:50:11 +0000264 CGDebugInfo* DebugInfo;
John McCall31168b02011-06-15 23:02:42 +0000265 ARCEntrypoints *ARCData;
Dan Gohman515a60d2012-02-16 00:57:37 +0000266 llvm::MDNode *NoObjCARCExceptionsMetadata;
John McCall31168b02011-06-15 23:02:42 +0000267 RREntrypoints *RRData;
Justin Bogner837a6f62014-04-18 21:52:00 +0000268 std::unique_ptr<llvm::IndexedInstrProfReader> PGOReader;
Justin Bognere2ef2a02014-04-15 21:22:35 +0000269 InstrProfStats PGOStats;
Daniel Dunbare49df9b52008-07-30 16:32:24 +0000270
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000271 // A set of references that have only been seen via a weakref so far. This is
272 // used to remove the weak of the reference if we ever see a direct reference
273 // or a definition.
Rafael Espindola2e42fec2010-03-04 18:17:24 +0000274 llvm::SmallPtrSet<llvm::GlobalValue*, 10> WeakRefReferences;
275
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000276 /// This contains all the decls which have definitions but/ which are deferred
277 /// for emission and therefore should only be output if they are actually
278 /// used. If a decl is in this, then it is known to have not been referenced
279 /// yet.
John McCall7ec50432010-03-19 23:29:14 +0000280 llvm::StringMap<GlobalDecl> DeferredDecls;
Daniel Dunbar08b26a02009-02-13 20:29:50 +0000281
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000282 /// This is a list of deferred decls which we have seen that *are* actually
283 /// referenced. These get code generated when the module is done.
Rafael Espindolac0ff7442013-12-09 14:59:08 +0000284 struct DeferredGlobal {
285 DeferredGlobal(llvm::GlobalValue *GV, GlobalDecl GD) : GV(GV), GD(GD) {}
286 llvm::AssertingVH<llvm::GlobalValue> GV;
287 GlobalDecl GD;
288 };
289 std::vector<DeferredGlobal> DeferredDeclsToEmit;
290 void addDeferredDeclToEmit(llvm::GlobalValue *GV, GlobalDecl GD) {
291 DeferredDeclsToEmit.push_back(DeferredGlobal(GV, GD));
292 }
Daniel Dunbar7dd749e2009-04-15 22:08:45 +0000293
Rafael Espindola208b5c02013-10-22 19:26:13 +0000294 /// List of alias we have emitted. Used to make sure that what they point to
295 /// is defined once we get to the end of the of the translation unit.
296 std::vector<GlobalDecl> Aliases;
297
Rafael Espindola2e2995b2013-11-05 21:37:29 +0000298 typedef llvm::StringMap<llvm::TrackingVH<llvm::Constant> > ReplacementsTy;
299 ReplacementsTy Replacements;
300
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000301 /// A queue of (optional) vtables to consider emitting.
John McCall6bd2a892013-01-25 22:31:03 +0000302 std::vector<const CXXRecordDecl*> DeferredVTables;
303
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000304 /// List of global values which are required to be present in the object file;
305 /// bitcast to i8*. This is used for forcing visibility of symbols which may
306 /// otherwise be optimized out.
Chris Lattnerf41e87f2009-03-31 22:37:52 +0000307 std::vector<llvm::WeakVH> LLVMUsed;
Rafael Espindola060062a2014-03-06 22:15:10 +0000308 std::vector<llvm::WeakVH> LLVMCompilerUsed;
Mike Stump1676c5b2009-02-13 19:12:34 +0000309
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000310 /// Store the list of global constructors and their respective priorities to
311 /// be emitted when the translation unit is complete.
Daniel Dunbar74aa7e12008-08-01 00:01:51 +0000312 CtorList GlobalCtors;
313
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000314 /// Store the list of global destructors and their respective priorities to be
315 /// emitted when the translation unit is complete.
Daniel Dunbar74aa7e12008-08-01 00:01:51 +0000316 CtorList GlobalDtors;
317
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000318 /// A map of canonical GlobalDecls to their mangled names.
Chris Lattner01cf8db2011-07-20 06:58:45 +0000319 llvm::DenseMap<GlobalDecl, StringRef> MangledDeclNames;
Anders Carlsson2e2f4d22010-06-22 16:05:32 +0000320 llvm::BumpPtrAllocator MangledNamesAllocator;
321
Julien Lerouge5a6b6982011-09-09 22:41:49 +0000322 /// Global annotations.
Nate Begeman7fab5782008-04-18 23:43:57 +0000323 std::vector<llvm::Constant*> Annotations;
Mike Stump1676c5b2009-02-13 19:12:34 +0000324
Julien Lerouge5a6b6982011-09-09 22:41:49 +0000325 /// Map used to get unique annotation strings.
326 llvm::StringMap<llvm::Constant*> AnnotationStrings;
327
Anders Carlssonb04ea612007-08-21 00:21:21 +0000328 llvm::StringMap<llvm::Constant*> CFConstantStringMap;
David Majnemer58e5bee2014-03-24 21:43:36 +0000329
330 llvm::StringMap<llvm::GlobalVariable *> Constant1ByteStringMap;
331 llvm::StringMap<llvm::GlobalVariable *> Constant2ByteStringMap;
332 llvm::StringMap<llvm::GlobalVariable *> Constant4ByteStringMap;
Eli Friedman1c8d2ca2012-03-09 03:27:46 +0000333 llvm::DenseMap<const Decl*, llvm::Constant *> StaticLocalDeclMap;
John McCallb88a5662012-03-30 21:00:39 +0000334 llvm::DenseMap<const Decl*, llvm::GlobalVariable*> StaticLocalDeclGuardMap;
Richard Smithe6c01442013-06-05 00:46:14 +0000335 llvm::DenseMap<const Expr*, llvm::Constant *> MaterializedGlobalTemporaryMap;
336
Fariborz Jahanian1bed4132012-01-08 19:13:23 +0000337 llvm::DenseMap<QualType, llvm::Constant *> AtomicSetterHelperFnMap;
338 llvm::DenseMap<QualType, llvm::Constant *> AtomicGetterHelperFnMap;
Daniel Dunbard644ad62008-08-23 18:37:06 +0000339
Will Dietz949ec542013-11-08 01:09:22 +0000340 /// Map used to get unique type descriptor constants for sanitizers.
341 llvm::DenseMap<QualType, llvm::Constant *> TypeDescriptorMap;
342
Richard Smithdf931ce2013-04-06 05:00:46 +0000343 /// Map used to track internal linkage functions declared within
344 /// extern "C" regions.
Richard Smithf21c9612013-04-13 01:28:18 +0000345 typedef llvm::MapVector<IdentifierInfo *,
346 llvm::GlobalValue *> StaticExternCMap;
Richard Smithdf931ce2013-04-06 05:00:46 +0000347 StaticExternCMap StaticExternCValues;
348
Richard Smith2fd1d7a2013-04-19 16:42:07 +0000349 /// \brief thread_local variables defined or used in this TU.
350 std::vector<std::pair<const VarDecl *, llvm::GlobalVariable *> >
351 CXXThreadLocals;
352
353 /// \brief thread_local variables with initializers that need to run
354 /// before any thread_local variable in this TU is odr-used.
355 std::vector<llvm::Constant*> CXXThreadLocalInits;
356
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000357 /// Global variables with initializers that need to run before main.
Eli Friedman5866fe32010-01-08 00:50:11 +0000358 std::vector<llvm::Constant*> CXXGlobalInits;
John McCall70013b62010-07-15 23:40:35 +0000359
360 /// When a C++ decl with an initializer is deferred, null is
361 /// appended to CXXGlobalInits, and the index of that null is placed
362 /// here so that the initializer will be performed in the correct
363 /// order.
364 llvm::DenseMap<const Decl*, unsigned> DelayedCXXInitPosition;
Fariborz Jahanian9f2a4ee2010-06-21 18:45:05 +0000365
Anton Korobeynikovabed7492012-11-06 22:44:45 +0000366 typedef std::pair<OrderGlobalInits, llvm::Function*> GlobalInitData;
367
368 struct GlobalInitPriorityCmp {
369 bool operator()(const GlobalInitData &LHS,
370 const GlobalInitData &RHS) const {
371 return LHS.first.priority < RHS.first.priority;
372 }
373 };
374
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000375 /// Global variables with initializers whose order of initialization is set by
376 /// init_priority attribute.
Anton Korobeynikovabed7492012-11-06 22:44:45 +0000377 SmallVector<GlobalInitData, 8> PrioritizedCXXGlobalInits;
Mike Stump11289f42009-09-09 15:08:12 +0000378
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000379 /// Global destructor functions and arguments that need to run on termination.
Chris Lattner87233f72010-06-19 05:52:45 +0000380 std::vector<std::pair<llvm::WeakVH,llvm::Constant*> > CXXGlobalDtors;
Daniel Dunbarfe06df42010-03-20 04:15:41 +0000381
Douglas Gregor6ddfca92013-01-14 17:21:00 +0000382 /// \brief The complete set of modules that has been imported.
383 llvm::SetVector<clang::Module *> ImportedModules;
384
Reid Klecknere43f0fe2013-05-08 13:44:39 +0000385 /// \brief A vector of metadata strings.
386 SmallVector<llvm::Value *, 16> LinkerOptionsMetadata;
387
Douglas Gregorabf4e0d2011-08-09 15:54:21 +0000388 /// @name Cache for Objective-C runtime types
389 /// @{
390
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000391 /// Cached reference to the class for constant strings. This value has type
392 /// int * but is actually an Obj-C class pointer.
Fariborz Jahaniand1b67782013-04-16 15:25:39 +0000393 llvm::WeakVH CFConstantStringClassRef;
Mike Stump11289f42009-09-09 15:08:12 +0000394
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000395 /// Cached reference to the class for constant strings. This value has type
396 /// int * but is actually an Obj-C class pointer.
Fariborz Jahaniand1b67782013-04-16 15:25:39 +0000397 llvm::WeakVH ConstantStringClassRef;
Fariborz Jahaniane804c282010-04-23 17:41:07 +0000398
Douglas Gregorabf4e0d2011-08-09 15:54:21 +0000399 /// \brief The LLVM type corresponding to NSConstantString.
400 llvm::StructType *NSConstantStringType;
401
Douglas Gregor636e2002011-08-09 17:23:49 +0000402 /// \brief The type used to describe the state of a fast enumeration in
403 /// Objective-C's for..in loop.
404 QualType ObjCFastEnumerationStateType;
405
Douglas Gregorabf4e0d2011-08-09 15:54:21 +0000406 /// @}
407
David Chisnall481e3a82010-01-23 02:40:42 +0000408 /// Lazily create the Objective-C runtime
409 void createObjCRuntime();
410
Peter Collingbourne2dbb7082011-09-19 21:14:35 +0000411 void createOpenCLRuntime();
Alexey Bataev9959db52014-05-06 10:08:46 +0000412 void createOpenMPRuntime();
Peter Collingbournefe883422011-10-06 18:29:37 +0000413 void createCUDARuntime();
Peter Collingbourne2dbb7082011-09-19 21:14:35 +0000414
Rafael Espindola4fdc1752011-12-19 14:41:01 +0000415 bool isTriviallyRecursive(const FunctionDecl *F);
Peter Collingbourne4d90dba2013-06-05 17:49:37 +0000416 bool shouldEmitFunction(GlobalDecl GD);
Daniel Dunbar900546d2010-07-16 00:00:15 +0000417
418 /// @name Cache for Blocks Runtime Globals
419 /// @{
420
421 llvm::Constant *NSConcreteGlobalBlock;
422 llvm::Constant *NSConcreteStackBlock;
Daniel Dunbar3348e2d2010-07-16 00:00:19 +0000423
Daniel Dunbar900546d2010-07-16 00:00:15 +0000424 llvm::Constant *BlockObjectAssign;
425 llvm::Constant *BlockObjectDispose;
426
Chris Lattnera5f58b02011-07-09 17:41:47 +0000427 llvm::Type *BlockDescriptorType;
428 llvm::Type *GenericBlockLiteralType;
John McCallad7c5c12011-02-08 08:22:06 +0000429
430 struct {
431 int GlobalUniqueCount;
432 } Block;
Will Dietzf54319c2013-01-18 11:30:38 +0000433
Nadav Rotem1da30942013-03-23 06:43:35 +0000434 /// void @llvm.lifetime.start(i64 %size, i8* nocapture <ptr>)
435 llvm::Constant *LifetimeStartFn;
436
437 /// void @llvm.lifetime.end(i64 %size, i8* nocapture <ptr>)
438 llvm::Constant *LifetimeEndFn;
439
Fariborz Jahanian63628032012-06-26 16:06:38 +0000440 GlobalDecl initializedGlobalDecl;
John McCallad7c5c12011-02-08 08:22:06 +0000441
Ahmed Charlesb8984322014-03-07 20:03:18 +0000442 std::unique_ptr<llvm::SpecialCaseList> SanitizerBlacklist;
Will Dietzf54319c2013-01-18 11:30:38 +0000443
444 const SanitizerOptions &SanOpts;
445
Daniel Dunbar900546d2010-07-16 00:00:15 +0000446 /// @}
Chris Lattnerf97fe382007-05-24 06:29:05 +0000447public:
Chandler Carruthbc55fe22009-11-12 17:24:48 +0000448 CodeGenModule(ASTContext &C, const CodeGenOptions &CodeGenOpts,
John McCall568d4102013-04-16 22:48:20 +0000449 llvm::Module &M, const llvm::DataLayout &TD,
450 DiagnosticsEngine &Diags);
Ted Kremenek2c674f62008-08-05 18:50:11 +0000451
Chris Lattnera087ff92008-03-01 08:45:05 +0000452 ~CodeGenModule();
Mike Stump1676c5b2009-02-13 19:12:34 +0000453
Rafael Espindolac0ff7442013-12-09 14:59:08 +0000454 void clear();
455
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000456 /// Finalize LLVM code generation.
Ted Kremenek2c674f62008-08-05 18:50:11 +0000457 void Release();
Daniel Dunbar8d480592008-08-11 18:12:00 +0000458
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000459 /// Return a reference to the configured Objective-C runtime.
Mike Stump1676c5b2009-02-13 19:12:34 +0000460 CGObjCRuntime &getObjCRuntime() {
Peter Collingbournee1d20992011-07-27 20:29:46 +0000461 if (!ObjCRuntime) createObjCRuntime();
462 return *ObjCRuntime;
Daniel Dunbar8d480592008-08-11 18:12:00 +0000463 }
Mike Stump1676c5b2009-02-13 19:12:34 +0000464
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000465 /// Return true iff an Objective-C runtime has been configured.
Peter Collingbournee1d20992011-07-27 20:29:46 +0000466 bool hasObjCRuntime() { return !!ObjCRuntime; }
Daniel Dunbar8d480592008-08-11 18:12:00 +0000467
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000468 /// Return a reference to the configured OpenCL runtime.
Peter Collingbourne2dbb7082011-09-19 21:14:35 +0000469 CGOpenCLRuntime &getOpenCLRuntime() {
Craig Topper8a13c412014-05-21 05:09:00 +0000470 assert(OpenCLRuntime != nullptr);
Peter Collingbourne2dbb7082011-09-19 21:14:35 +0000471 return *OpenCLRuntime;
472 }
473
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000474 /// Return a reference to the configured OpenMP runtime.
Alexey Bataev9959db52014-05-06 10:08:46 +0000475 CGOpenMPRuntime &getOpenMPRuntime() {
476 assert(OpenMPRuntime != nullptr);
477 return *OpenMPRuntime;
478 }
479
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000480 /// Return a reference to the configured CUDA runtime.
Peter Collingbournefe883422011-10-06 18:29:37 +0000481 CGCUDARuntime &getCUDARuntime() {
Craig Topper8a13c412014-05-21 05:09:00 +0000482 assert(CUDARuntime != nullptr);
Peter Collingbournefe883422011-10-06 18:29:37 +0000483 return *CUDARuntime;
484 }
485
John McCall31168b02011-06-15 23:02:42 +0000486 ARCEntrypoints &getARCEntrypoints() const {
Craig Topper8a13c412014-05-21 05:09:00 +0000487 assert(getLangOpts().ObjCAutoRefCount && ARCData != nullptr);
John McCall31168b02011-06-15 23:02:42 +0000488 return *ARCData;
489 }
490
491 RREntrypoints &getRREntrypoints() const {
Craig Topper8a13c412014-05-21 05:09:00 +0000492 assert(RRData != nullptr);
John McCall31168b02011-06-15 23:02:42 +0000493 return *RRData;
494 }
495
Justin Bogner837a6f62014-04-18 21:52:00 +0000496 InstrProfStats &getPGOStats() { return PGOStats; }
497 llvm::IndexedInstrProfReader *getPGOReader() const { return PGOReader.get(); }
Justin Bogneref512b92014-01-06 22:27:43 +0000498
Eli Friedman1c8d2ca2012-03-09 03:27:46 +0000499 llvm::Constant *getStaticLocalDeclAddress(const VarDecl *D) {
500 return StaticLocalDeclMap[D];
Fariborz Jahanian3fef72f2010-04-18 21:01:23 +0000501 }
Fariborz Jahanian4d55b2d2010-04-19 18:15:02 +0000502 void setStaticLocalDeclAddress(const VarDecl *D,
Eli Friedman1c8d2ca2012-03-09 03:27:46 +0000503 llvm::Constant *C) {
504 StaticLocalDeclMap[D] = C;
Fariborz Jahanian3fef72f2010-04-18 21:01:23 +0000505 }
506
John McCallb88a5662012-03-30 21:00:39 +0000507 llvm::GlobalVariable *getStaticLocalDeclGuardAddress(const VarDecl *D) {
508 return StaticLocalDeclGuardMap[D];
509 }
510 void setStaticLocalDeclGuardAddress(const VarDecl *D,
511 llvm::GlobalVariable *C) {
512 StaticLocalDeclGuardMap[D] = C;
513 }
514
Fariborz Jahanian1bed4132012-01-08 19:13:23 +0000515 llvm::Constant *getAtomicSetterHelperFnMap(QualType Ty) {
516 return AtomicSetterHelperFnMap[Ty];
Fariborz Jahanian7ff610b2012-01-06 22:33:54 +0000517 }
Fariborz Jahanian1bed4132012-01-08 19:13:23 +0000518 void setAtomicSetterHelperFnMap(QualType Ty,
Fariborz Jahanian7ff610b2012-01-06 22:33:54 +0000519 llvm::Constant *Fn) {
Fariborz Jahanian1bed4132012-01-08 19:13:23 +0000520 AtomicSetterHelperFnMap[Ty] = Fn;
521 }
522
523 llvm::Constant *getAtomicGetterHelperFnMap(QualType Ty) {
524 return AtomicGetterHelperFnMap[Ty];
525 }
526 void setAtomicGetterHelperFnMap(QualType Ty,
527 llvm::Constant *Fn) {
528 AtomicGetterHelperFnMap[Ty] = Fn;
Fariborz Jahanian7ff610b2012-01-06 22:33:54 +0000529 }
530
Will Dietz949ec542013-11-08 01:09:22 +0000531 llvm::Constant *getTypeDescriptor(QualType Ty) {
532 return TypeDescriptorMap[Ty];
533 }
534 void setTypeDescriptor(QualType Ty, llvm::Constant *C) {
535 TypeDescriptorMap[Ty] = C;
536 }
537
Devang Pateld6ffebb2011-03-07 18:45:56 +0000538 CGDebugInfo *getModuleDebugInfo() { return DebugInfo; }
Devang Patele65982c2011-03-07 18:29:53 +0000539
Dan Gohman515a60d2012-02-16 00:57:37 +0000540 llvm::MDNode *getNoObjCARCExceptionsMetadata() {
541 if (!NoObjCARCExceptionsMetadata)
542 NoObjCARCExceptionsMetadata =
543 llvm::MDNode::get(getLLVMContext(),
544 SmallVector<llvm::Value*,1>());
545 return NoObjCARCExceptionsMetadata;
546 }
547
Chris Lattnerd1af2d22007-05-29 23:17:50 +0000548 ASTContext &getContext() const { return Context; }
David Blaikiebbafb8a2012-03-11 07:00:24 +0000549 const LangOptions &getLangOpts() const { return LangOpts; }
John McCallc8e01702013-04-16 22:48:15 +0000550 const CodeGenOptions &getCodeGenOpts() const { return CodeGenOpts; }
Chris Lattner23b7eb62007-06-15 23:05:46 +0000551 llvm::Module &getModule() const { return TheModule; }
David Blaikie9c902b52011-09-25 23:23:43 +0000552 DiagnosticsEngine &getDiags() const { return Diags; }
Micah Villmowdd31ca12012-10-08 16:25:52 +0000553 const llvm::DataLayout &getDataLayout() const { return TheDataLayout; }
John McCallc8e01702013-04-16 22:48:15 +0000554 const TargetInfo &getTarget() const { return Target; }
Alp Toker82862252013-12-28 21:58:40 +0000555 CGCXXABI &getCXXABI() const { return *ABI; }
Owen Anderson170229f2009-07-14 23:10:40 +0000556 llvm::LLVMContext &getLLVMContext() { return VMContext; }
Craig Topper8a13c412014-05-21 05:09:00 +0000557
558 bool shouldUseTBAA() const { return TBAA != nullptr; }
John McCall53fcbd22011-02-26 08:07:02 +0000559
John McCallc8e01702013-04-16 22:48:15 +0000560 const TargetCodeGenInfo &getTargetCodeGenInfo();
561
562 CodeGenTypes &getTypes() { return Types; }
563
564 CodeGenVTables &getVTables() { return VTables; }
Timur Iskhodzhanov88fd4392013-08-21 06:25:03 +0000565
Timur Iskhodzhanov58776632013-11-05 15:54:58 +0000566 ItaniumVTableContext &getItaniumVTableContext() {
567 return VTables.getItaniumVTableContext();
Timur Iskhodzhanove1ebc5f2013-10-09 11:33:51 +0000568 }
John McCallc8e01702013-04-16 22:48:15 +0000569
Timur Iskhodzhanov58776632013-11-05 15:54:58 +0000570 MicrosoftVTableContext &getMicrosoftVTableContext() {
571 return VTables.getMicrosoftVTableContext();
Timur Iskhodzhanov88fd4392013-08-21 06:25:03 +0000572 }
573
Dan Gohman947c9af2010-10-14 23:06:10 +0000574 llvm::MDNode *getTBAAInfo(QualType QTy);
Kostya Serebryany141e46f2012-03-26 17:03:51 +0000575 llvm::MDNode *getTBAAInfoForVTablePtr();
Dan Gohman22695fc2012-09-28 21:58:29 +0000576 llvm::MDNode *getTBAAStructInfo(QualType QTy);
Manman Renc451e572013-04-04 21:53:22 +0000577 /// Return the MDNode in the type DAG for the given struct type.
578 llvm::MDNode *getTBAAStructTypeInfo(QualType QTy);
579 /// Return the path-aware tag for given base type, access node and offset.
580 llvm::MDNode *getTBAAStructTagInfo(QualType BaseTy, llvm::MDNode *AccessN,
581 uint64_t O);
Dan Gohman947c9af2010-10-14 23:06:10 +0000582
Richard Smithe070fd22012-02-17 06:48:11 +0000583 bool isTypeConstant(QualType QTy, bool ExcludeCtorDtor);
584
John McCalla8ec7eb2013-03-07 21:37:17 +0000585 bool isPaddedAtomicType(QualType type);
586 bool isPaddedAtomicType(const AtomicType *type);
587
Manman Rene1ad74e2013-04-11 23:02:56 +0000588 /// Decorate the instruction with a TBAA tag. For scalar TBAA, the tag
589 /// is the same as the type. For struct-path aware TBAA, the tag
590 /// is different from the type: base type, access type and offset.
591 /// When ConvertTypeToTag is true, we create a tag based on the scalar type.
592 void DecorateInstruction(llvm::Instruction *Inst,
593 llvm::MDNode *TBAAInfo,
594 bool ConvertTypeToTag = true);
Dan Gohman947c9af2010-10-14 23:06:10 +0000595
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000596 /// Emit the given number of characters as a value of type size_t.
John McCall23c29fe2011-06-24 21:55:10 +0000597 llvm::ConstantInt *getSize(CharUnits numChars);
598
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000599 /// Set the visibility for the given LLVM GlobalValue.
Anders Carlssonc6a47892011-01-29 19:39:23 +0000600 void setGlobalVisibility(llvm::GlobalValue *GV, const NamedDecl *D) const;
Daniel Dunbarf5f359f2009-04-14 06:00:08 +0000601
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000602 /// Set the TLS mode for the given LLVM GlobalVariable for the thread-local
603 /// variable declaration D.
Hans Wennborgf60f6af2012-06-28 08:01:44 +0000604 void setTLSMode(llvm::GlobalVariable *GV, const VarDecl &D) const;
605
Anders Carlsson537fdce2011-01-29 20:59:35 +0000606 static llvm::GlobalValue::VisibilityTypes GetLLVMVisibility(Visibility V) {
607 switch (V) {
608 case DefaultVisibility: return llvm::GlobalValue::DefaultVisibility;
609 case HiddenVisibility: return llvm::GlobalValue::HiddenVisibility;
610 case ProtectedVisibility: return llvm::GlobalValue::ProtectedVisibility;
611 }
612 llvm_unreachable("unknown visibility!");
Anders Carlsson537fdce2011-01-29 20:59:35 +0000613 }
614
John McCalld4324142010-02-19 01:32:20 +0000615 llvm::Constant *GetAddrOfGlobal(GlobalDecl GD) {
616 if (isa<CXXConstructorDecl>(GD.getDecl()))
617 return GetAddrOfCXXConstructor(cast<CXXConstructorDecl>(GD.getDecl()),
618 GD.getCtorType());
619 else if (isa<CXXDestructorDecl>(GD.getDecl()))
620 return GetAddrOfCXXDestructor(cast<CXXDestructorDecl>(GD.getDecl()),
621 GD.getDtorType());
622 else if (isa<FunctionDecl>(GD.getDecl()))
623 return GetAddrOfFunction(GD);
624 else
625 return GetAddrOfGlobalVar(cast<VarDecl>(GD.getDecl()));
626 }
627
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000628 /// Will return a global variable of the given type. If a variable with a
629 /// different type already exists then a new variable with the right type
630 /// will be created and all uses of the old variable will be replaced with a
631 /// bitcast to the new variable.
Anders Carlssonda80af32011-01-29 18:20:20 +0000632 llvm::GlobalVariable *
Chris Lattner01cf8db2011-07-20 06:58:45 +0000633 CreateOrReplaceCXXRuntimeVariable(StringRef Name, llvm::Type *Ty,
Anders Carlssonda80af32011-01-29 18:20:20 +0000634 llvm::GlobalValue::LinkageTypes Linkage);
635
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000636 /// Return the address space of the underlying global variable for D, as
637 /// determined by its declaration. Normally this is the same as the address
638 /// space of D's type, but in CUDA, address spaces are associated with
639 /// declarations, not types.
Peter Collingbournef44bdf92012-05-20 21:08:35 +0000640 unsigned GetGlobalVarAddressSpace(const VarDecl *D, unsigned AddrSpace);
641
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000642 /// Return the llvm::Constant for the address of the given global variable.
643 /// If Ty is non-null and if the global doesn't exist, then it will be greated
644 /// with the specified type instead of whatever the normal requested type
645 /// would be.
Chris Lattner149927c2009-03-21 09:16:30 +0000646 llvm::Constant *GetAddrOfGlobalVar(const VarDecl *D,
Craig Topper8a13c412014-05-21 05:09:00 +0000647 llvm::Type *Ty = nullptr);
Daniel Dunbar9c426522008-07-29 23:18:29 +0000648
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000649 /// Return the address of the given function. If Ty is non-null, then this
650 /// function will use the specified type if it has to create it.
Rafael Espindola94abb8f2013-12-09 04:29:47 +0000651 llvm::Constant *GetAddrOfFunction(GlobalDecl GD, llvm::Type *Ty = 0,
652 bool ForVTable = false,
653 bool DontDefer = false);
Daniel Dunbarc4baa062008-08-13 23:20:05 +0000654
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000655 /// Get the address of the RTTI descriptor for the given type.
John McCall48bf3492010-04-30 01:15:21 +0000656 llvm::Constant *GetAddrOfRTTIDescriptor(QualType Ty, bool ForEH = false);
Anders Carlssonfd7dfeb2009-12-11 02:46:30 +0000657
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000658 /// Get the address of a uuid descriptor .
Nico Webercf4ff5862012-10-11 10:13:44 +0000659 llvm::Constant *GetAddrOfUuidDescriptor(const CXXUuidofExpr* E);
660
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000661 /// Get the address of the thunk for the given global decl.
Anders Carlssonfe8a9932011-02-06 17:15:43 +0000662 llvm::Constant *GetAddrOfThunk(GlobalDecl GD, const ThunkInfo &Thunk);
Anders Carlssoncd836f02010-03-23 17:17:29 +0000663
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000664 /// Get a reference to the target of VD.
Rafael Espindola2e42fec2010-03-04 18:17:24 +0000665 llvm::Constant *GetWeakRefReference(const ValueDecl *VD);
666
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000667 /// Returns the offset from a derived class to a class. Returns null if the
668 /// offset is 0.
Anders Carlsson8a64c1c2010-04-24 21:23:59 +0000669 llvm::Constant *
670 GetNonVirtualBaseClassOffset(const CXXRecordDecl *ClassDecl,
John McCallcf142162010-08-07 06:22:56 +0000671 CastExpr::path_const_iterator PathBegin,
672 CastExpr::path_const_iterator PathEnd);
John McCallad7c5c12011-02-08 08:22:06 +0000673
John McCallf9b056b2011-03-31 08:03:29 +0000674 /// A pair of helper functions for a __block variable.
675 class ByrefHelpers : public llvm::FoldingSetNode {
676 public:
677 llvm::Constant *CopyHelper;
678 llvm::Constant *DisposeHelper;
679
680 /// The alignment of the field. This is important because
681 /// different offsets to the field within the byref struct need to
682 /// have different helper functions.
683 CharUnits Alignment;
684
685 ByrefHelpers(CharUnits alignment) : Alignment(alignment) {}
686 virtual ~ByrefHelpers();
687
688 void Profile(llvm::FoldingSetNodeID &id) const {
689 id.AddInteger(Alignment.getQuantity());
690 profileImpl(id);
691 }
692 virtual void profileImpl(llvm::FoldingSetNodeID &id) const = 0;
693
694 virtual bool needsCopy() const { return true; }
695 virtual void emitCopy(CodeGenFunction &CGF,
696 llvm::Value *dest, llvm::Value *src) = 0;
697
698 virtual bool needsDispose() const { return true; }
699 virtual void emitDispose(CodeGenFunction &CGF, llvm::Value *field) = 0;
700 };
701
702 llvm::FoldingSet<ByrefHelpers> ByrefHelpersCache;
John McCallad7c5c12011-02-08 08:22:06 +0000703
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000704 /// Fetches the global unique block count.
John McCall147d0212011-02-22 22:38:33 +0000705 int getUniqueBlockCount() { return ++Block.GlobalUniqueCount; }
Fariborz Jahanian63628032012-06-26 16:06:38 +0000706
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000707 /// Fetches the type of a generic block descriptor.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000708 llvm::Type *getBlockDescriptorType();
John McCallad7c5c12011-02-08 08:22:06 +0000709
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000710 /// The type of a generic block literal.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000711 llvm::Type *getGenericBlockLiteralType();
John McCallad7c5c12011-02-08 08:22:06 +0000712
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000713 /// Gets the address of a block which requires no captures.
John McCallad7c5c12011-02-08 08:22:06 +0000714 llvm::Constant *GetAddrOfGlobalBlock(const BlockExpr *BE, const char *);
Anders Carlsson8a64c1c2010-04-24 21:23:59 +0000715
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000716 /// Return a pointer to a constant CFString object for the given string.
Steve Naroffe14b3682009-04-01 13:55:36 +0000717 llvm::Constant *GetAddrOfConstantCFString(const StringLiteral *Literal);
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000718
719 /// Return a pointer to a constant NSString object for the given string. Or a
720 /// user defined String object as defined via
Fariborz Jahanian50c925f2010-10-19 17:19:29 +0000721 /// -fconstant-string-class=class_name option.
722 llvm::Constant *GetAddrOfConstantString(const StringLiteral *Literal);
Chris Lattner36fc8792008-02-11 00:02:17 +0000723
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000724 /// Return a constant array for the given string.
Eli Friedmanfcec6302011-11-01 02:23:42 +0000725 llvm::Constant *GetConstantArrayFromStringLiteral(const StringLiteral *E);
726
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000727 /// Return a pointer to a constant array for the given string literal.
Daniel Dunbarc4baa062008-08-13 23:20:05 +0000728 llvm::Constant *GetAddrOfConstantStringFromLiteral(const StringLiteral *S);
Daniel Dunbar6dfdf8c2008-08-10 20:25:57 +0000729
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000730 /// Return a pointer to a constant array for the given ObjCEncodeExpr node.
Chris Lattnerd7e7b8e2009-02-24 22:18:39 +0000731 llvm::Constant *GetAddrOfConstantStringFromObjCEncode(const ObjCEncodeExpr *);
Mike Stump11289f42009-09-09 15:08:12 +0000732
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000733 /// Returns a pointer to a character array containing the literal. This
734 /// contents are exactly that of the given string, i.e. it will not be null
735 /// terminated automatically; see GetAddrOfConstantCString. Note that whether
736 /// the result is actually a pointer to an LLVM constant depends on
737 /// Feature.WriteableStrings.
Daniel Dunbarc4baa062008-08-13 23:20:05 +0000738 ///
739 /// The result has pointer to array type.
Daniel Dunbardfcf5992008-10-17 21:56:50 +0000740 ///
741 /// \param GlobalName If provided, the name to use for the global
742 /// (if one is created).
Chris Lattner01cf8db2011-07-20 06:58:45 +0000743 llvm::Constant *GetAddrOfConstantString(StringRef Str,
Craig Topper8a13c412014-05-21 05:09:00 +0000744 const char *GlobalName=nullptr,
Ulrich Weigandfa806422013-05-06 16:23:57 +0000745 unsigned Alignment=0);
Daniel Dunbarc4baa062008-08-13 23:20:05 +0000746
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000747 /// Returns a pointer to a character array containing the literal and a
748 /// terminating '\0' character. The result has pointer to array type.
Daniel Dunbardfcf5992008-10-17 21:56:50 +0000749 ///
Mike Stump1676c5b2009-02-13 19:12:34 +0000750 /// \param GlobalName If provided, the name to use for the global (if one is
751 /// created).
Daniel Dunbardfcf5992008-10-17 21:56:50 +0000752 llvm::Constant *GetAddrOfConstantCString(const std::string &str,
Craig Topper8a13c412014-05-21 05:09:00 +0000753 const char *GlobalName=nullptr,
Ulrich Weigandfa806422013-05-06 16:23:57 +0000754 unsigned Alignment=0);
Richard Smith2d988f02011-11-22 22:48:32 +0000755
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000756 /// Returns a pointer to a constant global variable for the given file-scope
757 /// compound literal expression.
Richard Smith2d988f02011-11-22 22:48:32 +0000758 llvm::Constant *GetAddrOfConstantCompoundLiteral(const CompoundLiteralExpr*E);
Richard Smithe6c01442013-06-05 00:46:14 +0000759
760 /// \brief Returns a pointer to a global variable representing a temporary
761 /// with static or thread storage duration.
762 llvm::Constant *GetAddrOfGlobalTemporary(const MaterializeTemporaryExpr *E,
763 const Expr *Inner);
764
Douglas Gregor636e2002011-08-09 17:23:49 +0000765 /// \brief Retrieve the record type that describes the state of an
766 /// Objective-C fast enumeration loop (for..in).
767 QualType getObjCFastEnumerationStateType();
768
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000769 /// Return the address of the constructor of the given type.
Craig Topper8a13c412014-05-21 05:09:00 +0000770 llvm::GlobalValue *
771 GetAddrOfCXXConstructor(const CXXConstructorDecl *ctor, CXXCtorType ctorType,
772 const CGFunctionInfo *fnInfo = nullptr,
773 bool DontDefer = false);
Anders Carlssoneaa28f72009-04-17 01:58:57 +0000774
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000775 /// Return the address of the constructor of the given type.
Craig Topper8a13c412014-05-21 05:09:00 +0000776 llvm::GlobalValue *
777 GetAddrOfCXXDestructor(const CXXDestructorDecl *dtor,
778 CXXDtorType dtorType,
779 const CGFunctionInfo *fnInfo = nullptr,
780 llvm::FunctionType *fnType = nullptr,
781 bool DontDefer = false);
Mike Stump11289f42009-09-09 15:08:12 +0000782
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000783 /// Given a builtin id for a function like "__builtin_fabsf", return a
784 /// Function* for "fabsf".
Daniel Dunbarff0553e2009-09-14 04:33:21 +0000785 llvm::Value *getBuiltinLibFunction(const FunctionDecl *FD,
786 unsigned BuiltinID);
Daniel Dunbarc4baa062008-08-13 23:20:05 +0000787
Dmitri Gribenko44ebbd52013-05-05 00:41:58 +0000788 llvm::Function *getIntrinsic(unsigned IID, ArrayRef<llvm::Type*> Tys = None);
Daniel Dunbar9c426522008-07-29 23:18:29 +0000789
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000790 /// Emit code for a single top level declaration.
Daniel Dunbarfce4be82008-08-15 23:26:23 +0000791 void EmitTopLevelDecl(Decl *D);
Daniel Dunbar9c426522008-07-29 23:18:29 +0000792
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000793 /// Tell the consumer that this variable has been instantiated.
Rafael Espindoladf88f6f2012-03-08 15:51:03 +0000794 void HandleCXXStaticMemberVarInstantiation(VarDecl *VD);
Rafael Espindola189fa742012-03-05 10:54:55 +0000795
Richard Smithdf931ce2013-04-06 05:00:46 +0000796 /// \brief If the declaration has internal linkage but is inside an
797 /// extern "C" linkage specification, prepare to emit an alias for it
798 /// to the expected name.
799 template<typename SomeDecl>
800 void MaybeHandleStaticInExternC(const SomeDecl *D, llvm::GlobalValue *GV);
801
Rafael Espindola060062a2014-03-06 22:15:10 +0000802 /// Add a global to a list to be added to the llvm.used metadata.
803 void addUsedGlobal(llvm::GlobalValue *GV);
804
805 /// Add a global to a list to be added to the llvm.compiler.used metadata.
806 void addCompilerUsedGlobal(llvm::GlobalValue *GV);
Daniel Dunbar08b26a02009-02-13 20:29:50 +0000807
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000808 /// Add a destructor and object to add to the C++ global destructor function.
Chris Lattner87233f72010-06-19 05:52:45 +0000809 void AddCXXDtorEntry(llvm::Constant *DtorFn, llvm::Constant *Object) {
810 CXXGlobalDtors.push_back(std::make_pair(DtorFn, Object));
811 }
Daniel Dunbarfe06df42010-03-20 04:15:41 +0000812
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000813 /// Create a new runtime function with the specified type and name.
Chris Lattner2192fe52011-07-18 04:24:23 +0000814 llvm::Constant *CreateRuntimeFunction(llvm::FunctionType *Ty,
Chris Lattner01cf8db2011-07-20 06:58:45 +0000815 StringRef Name,
Bill Wendling8594fcb2013-01-31 00:30:05 +0000816 llvm::AttributeSet ExtraAttrs =
817 llvm::AttributeSet());
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000818 /// Create a new runtime global variable with the specified type and name.
Chris Lattner2192fe52011-07-18 04:24:23 +0000819 llvm::Constant *CreateRuntimeVariable(llvm::Type *Ty,
Chris Lattner01cf8db2011-07-20 06:58:45 +0000820 StringRef Name);
Daniel Dunbar23fd4622008-10-01 00:49:24 +0000821
Daniel Dunbar900546d2010-07-16 00:00:15 +0000822 ///@name Custom Blocks Runtime Interfaces
823 ///@{
824
825 llvm::Constant *getNSConcreteGlobalBlock();
826 llvm::Constant *getNSConcreteStackBlock();
827 llvm::Constant *getBlockObjectAssign();
828 llvm::Constant *getBlockObjectDispose();
829
830 ///@}
831
Nadav Rotem1da30942013-03-23 06:43:35 +0000832 llvm::Constant *getLLVMLifetimeStartFn();
833 llvm::Constant *getLLVMLifetimeEndFn();
834
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000835 // Make sure that this type is translated.
Devang Patel945b8ae2011-03-23 16:29:39 +0000836 void UpdateCompletedType(const TagDecl *TD);
Daniel Dunbar38ad1e62009-02-17 18:43:32 +0000837
John McCallf3a88602011-02-03 08:15:49 +0000838 llvm::Constant *getMemberPointerConstant(const UnaryOperator *e);
839
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000840 /// Try to emit the initializer for the given declaration as a constant;
841 /// returns 0 if the expression cannot be emitted as a constant.
Craig Topper8a13c412014-05-21 05:09:00 +0000842 llvm::Constant *EmitConstantInit(const VarDecl &D,
843 CodeGenFunction *CGF = nullptr);
Richard Smithdafff942012-01-14 04:30:29 +0000844
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000845 /// Try to emit the given expression as a constant; returns 0 if the
846 /// expression cannot be emitted as a constant.
Anders Carlsson80f97ab2009-04-08 04:48:15 +0000847 llvm::Constant *EmitConstantExpr(const Expr *E, QualType DestType,
Craig Topper8a13c412014-05-21 05:09:00 +0000848 CodeGenFunction *CGF = nullptr);
Daniel Dunbar38ad1e62009-02-17 18:43:32 +0000849
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000850 /// Emit the given constant value as a constant, in the type's scalar
851 /// representation.
Richard Smithdafff942012-01-14 04:30:29 +0000852 llvm::Constant *EmitConstantValue(const APValue &Value, QualType DestType,
Craig Topper8a13c412014-05-21 05:09:00 +0000853 CodeGenFunction *CGF = nullptr);
Richard Smithdafff942012-01-14 04:30:29 +0000854
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000855 /// Emit the given constant value as a constant, in the type's memory
856 /// representation.
Richard Smithbc6387672012-03-02 23:27:11 +0000857 llvm::Constant *EmitConstantValueForMemory(const APValue &Value,
858 QualType DestType,
Craig Topper8a13c412014-05-21 05:09:00 +0000859 CodeGenFunction *CGF = nullptr);
Richard Smithbc6387672012-03-02 23:27:11 +0000860
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000861 /// Return the result of value-initializing the given type, i.e. a null
862 /// expression of the given type. This is usually, but not always, an LLVM
863 /// null constant.
Eli Friedman20bb5e02009-04-13 21:47:26 +0000864 llvm::Constant *EmitNullConstant(QualType T);
865
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000866 /// Return a null constant appropriate for zero-initializing a base class with
867 /// the given type. This is usually, but not always, an LLVM null constant.
Eli Friedmanfde961d2011-10-14 02:27:24 +0000868 llvm::Constant *EmitNullConstantForBase(const CXXRecordDecl *Record);
869
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000870 /// Emit a general error that something can't be done.
Chandler Carruth84537952012-03-30 19:44:53 +0000871 void Error(SourceLocation loc, StringRef error);
John McCall7ef5cb32011-03-18 02:56:14 +0000872
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000873 /// Print out an error that codegen doesn't support the specified stmt yet.
David Blaikie4a9ec7b2013-08-19 21:02:26 +0000874 void ErrorUnsupported(const Stmt *S, const char *Type);
Mike Stump1676c5b2009-02-13 19:12:34 +0000875
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000876 /// Print out an error that codegen doesn't support the specified decl yet.
David Blaikie4a9ec7b2013-08-19 21:02:26 +0000877 void ErrorUnsupported(const Decl *D, const char *Type);
Dan Gohman75d69da2008-05-22 00:50:06 +0000878
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000879 /// Set the attributes on the LLVM function for the given decl and function
880 /// info. This applies attributes necessary for handling the ABI as well as
881 /// user specified attributes like section.
Daniel Dunbarc3e7cff2009-04-17 00:48:04 +0000882 void SetInternalFunctionAttributes(const Decl *D, llvm::Function *F,
883 const CGFunctionInfo &FI);
Daniel Dunbar449a3392008-09-04 23:41:35 +0000884
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000885 /// Set the LLVM function attributes (sext, zext, etc).
Daniel Dunbaraeddffc2009-04-14 07:08:30 +0000886 void SetLLVMFunctionAttributes(const Decl *D,
887 const CGFunctionInfo &Info,
888 llvm::Function *F);
Daniel Dunbarc68897d2008-09-10 00:41:16 +0000889
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000890 /// Set the LLVM function attributes which only apply to a function
891 /// definintion.
Daniel Dunbar38932572009-04-14 08:05:55 +0000892 void SetLLVMFunctionAttributesForDefinition(const Decl *D, llvm::Function *F);
893
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000894 /// Return true iff the given type uses 'sret' when used as a return type.
Daniel Dunbar6f2e8392010-07-14 23:39:36 +0000895 bool ReturnTypeUsesSRet(const CGFunctionInfo &FI);
896
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000897 /// Return true iff the given type uses an argument slot when 'sret' is used
898 /// as a return type.
Tim Northovere77cc392014-03-29 13:28:05 +0000899 bool ReturnSlotInterferesWithArgs(const CGFunctionInfo &FI);
900
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000901 /// Return true iff the given type uses 'fpret' when used as a return type.
Daniel Dunbar6f2e8392010-07-14 23:39:36 +0000902 bool ReturnTypeUsesFPRet(QualType ResultType);
Daniel Dunbarc68897d2008-09-10 00:41:16 +0000903
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000904 /// Return true iff the given type uses 'fp2ret' when used as a return type.
Anders Carlsson2f1a6c32011-10-31 16:27:11 +0000905 bool ReturnTypeUsesFP2Ret(QualType ResultType);
906
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000907 /// Get the LLVM attributes and calling convention to use for a particular
908 /// function type.
Daniel Dunbar0ef34792009-09-12 00:59:20 +0000909 ///
910 /// \param Info - The function type information.
911 /// \param TargetDecl - The decl these attributes are being constructed
912 /// for. If supplied the attributes applied to this decl may contribute to the
913 /// function attributes and calling convention.
914 /// \param PAL [out] - On return, the attribute list to use.
915 /// \param CallingConv [out] - On return, the LLVM calling convention to use.
Daniel Dunbard931a872009-02-02 22:03:45 +0000916 void ConstructAttributeList(const CGFunctionInfo &Info,
917 const Decl *TargetDecl,
Daniel Dunbar0ef34792009-09-12 00:59:20 +0000918 AttributeListType &PAL,
Bill Wendlingf4d64cb2013-02-22 00:13:35 +0000919 unsigned &CallingConv,
920 bool AttrOnCallSite);
Daniel Dunbarc68897d2008-09-10 00:41:16 +0000921
Chris Lattner01cf8db2011-07-20 06:58:45 +0000922 StringRef getMangledName(GlobalDecl GD);
Peter Collingbourne0ff0b372011-01-13 18:57:25 +0000923 void getBlockMangledName(GlobalDecl GD, MangleBuffer &Buffer,
924 const BlockDecl *BD);
Douglas Gregorbeecd582009-04-21 17:11:58 +0000925
926 void EmitTentativeDefinition(const VarDecl *D);
Mike Stumpbc78a722009-07-31 18:25:34 +0000927
Douglas Gregor88d292c2010-05-13 16:44:06 +0000928 void EmitVTable(CXXRecordDecl *Class, bool DefinitionRequired);
929
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000930 /// Emit the RTTI descriptors for the builtin types.
Timur Iskhodzhanov8b5987e2013-09-27 14:48:01 +0000931 void EmitFundamentalRTTIDescriptors();
932
Reid Klecknere43f0fe2013-05-08 13:44:39 +0000933 /// \brief Appends Opts to the "Linker Options" metadata value.
934 void AppendLinkerOptions(StringRef Opts);
935
Benjamin Kramer82dfc972013-06-04 09:13:21 +0000936 /// \brief Appends a detect mismatch command to the linker options.
Aaron Ballman5d041be2013-06-04 02:07:14 +0000937 void AddDetectMismatch(StringRef Name, StringRef Value);
938
Reid Klecknere43f0fe2013-05-08 13:44:39 +0000939 /// \brief Appends a dependent lib to the "Linker Options" metadata value.
940 void AddDependentLib(StringRef Lib);
941
Peter Collingbourne4d90dba2013-06-05 17:49:37 +0000942 llvm::GlobalVariable::LinkageTypes getFunctionLinkage(GlobalDecl GD);
John McCalld4324142010-02-19 01:32:20 +0000943
Rafael Espindola64926362014-05-08 14:46:46 +0000944 void setFunctionLinkage(GlobalDecl GD, llvm::Function *F) {
945 F->setLinkage(getFunctionLinkage(GD));
John McCall7cb02202010-05-25 04:30:21 +0000946 }
947
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000948 /// Return the appropriate linkage for the vtable, VTT, and type information
949 /// of the given class.
Anders Carlsson68d34242011-01-24 02:04:33 +0000950 llvm::GlobalVariable::LinkageTypes getVTableLinkage(const CXXRecordDecl *RD);
Ken Dyck98ca7942010-01-26 13:48:07 +0000951
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000952 /// Return the store size, in character units, of the given LLVM type.
Chris Lattner2192fe52011-07-18 04:24:23 +0000953 CharUnits GetTargetTypeStoreSize(llvm::Type *Ty) const;
Fariborz Jahanian1518a5ec2010-10-27 16:21:54 +0000954
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000955 /// Returns LLVM linkage for a declarator.
David Majnemer27d69db2014-04-28 22:17:59 +0000956 llvm::GlobalValue::LinkageTypes
Hans Wennborg1a3a0742014-05-14 19:54:53 +0000957 getLLVMLinkageForDeclarator(const DeclaratorDecl *D, GVALinkage Linkage,
David Majnemer27d69db2014-04-28 22:17:59 +0000958 bool IsConstantVariable,
959 bool UseThunkForDtorVariant);
960
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000961 /// Returns LLVM linkage for a declarator.
David Majnemer27d69db2014-04-28 22:17:59 +0000962 llvm::GlobalValue::LinkageTypes
963 getLLVMLinkageVarDefinition(const VarDecl *VD, bool IsConstant);
964
Julien Lerouge5a6b6982011-09-09 22:41:49 +0000965 /// Emit all the global annotations.
966 void EmitGlobalAnnotations();
967
968 /// Emit an annotation string.
Dmitri Gribenkof8579502013-01-12 19:30:44 +0000969 llvm::Constant *EmitAnnotationString(StringRef Str);
Julien Lerouge5a6b6982011-09-09 22:41:49 +0000970
971 /// Emit the annotation's translation unit.
972 llvm::Constant *EmitAnnotationUnit(SourceLocation Loc);
973
974 /// Emit the annotation line number.
975 llvm::Constant *EmitAnnotationLineNo(SourceLocation L);
976
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000977 /// Generate the llvm::ConstantStruct which contains the annotation
978 /// information for a given GlobalValue. The annotation struct is
Julien Lerouge5a6b6982011-09-09 22:41:49 +0000979 /// {i8 *, i8 *, i8 *, i32}. The first field is a constant expression, the
980 /// GlobalValue being annotated. The second field is the constant string
981 /// created from the AnnotateAttr's annotation. The third field is a constant
982 /// string containing the name of the translation unit. The fourth field is
983 /// the line number in the file of the annotated value declaration.
984 llvm::Constant *EmitAnnotateAttr(llvm::GlobalValue *GV,
985 const AnnotateAttr *AA,
986 SourceLocation L);
987
988 /// Add global annotations that are set on D, for the global GV. Those
989 /// annotations are emitted during finalization of the LLVM code.
990 void AddGlobalAnnotations(const ValueDecl *D, llvm::GlobalValue *GV);
991
Peter Collingbourne41148a82013-07-09 22:03:30 +0000992 const llvm::SpecialCaseList &getSanitizerBlacklist() const {
Alexey Samsonovd6e043b2013-08-12 11:48:05 +0000993 return *SanitizerBlacklist;
Will Dietzf54319c2013-01-18 11:30:38 +0000994 }
995
996 const SanitizerOptions &getSanOpts() const { return SanOpts; }
997
John McCall6bd2a892013-01-25 22:31:03 +0000998 void addDeferredVTable(const CXXRecordDecl *RD) {
999 DeferredVTables.push_back(RD);
1000 }
1001
Rafael Espindolae98ed2f2014-05-09 01:34:38 +00001002 /// Emit code for a singal global function or var decl. Forward declarations
1003 /// are emitted lazily.
Reid Klecknere7de47e2013-07-22 13:51:44 +00001004 void EmitGlobal(GlobalDecl D);
1005
Chris Lattner5bcdf242007-12-02 07:09:19 +00001006private:
Chris Lattner01cf8db2011-07-20 06:58:45 +00001007 llvm::GlobalValue *GetGlobalValue(StringRef Ref);
Mike Stump11289f42009-09-09 15:08:12 +00001008
Rafael Espindola94abb8f2013-12-09 04:29:47 +00001009 llvm::Constant *
1010 GetOrCreateLLVMFunction(StringRef MangledName, llvm::Type *Ty, GlobalDecl D,
1011 bool ForVTable, bool DontDefer = false,
1012 llvm::AttributeSet ExtraAttrs = llvm::AttributeSet());
1013
Chris Lattner01cf8db2011-07-20 06:58:45 +00001014 llvm::Constant *GetOrCreateLLVMGlobal(StringRef MangledName,
Chris Lattner2192fe52011-07-18 04:24:23 +00001015 llvm::PointerType *PTy,
Rafael Espindolab73c9732014-05-22 23:33:27 +00001016 const VarDecl *D);
Mike Stump11289f42009-09-09 15:08:12 +00001017
Rafael Espindolae98ed2f2014-05-09 01:34:38 +00001018 /// Set attributes which are common to any form of a global definition (alias,
1019 /// Objective-C method, function, global variable).
Daniel Dunbaraeddffc2009-04-14 07:08:30 +00001020 ///
Daniel Dunbar38932572009-04-14 08:05:55 +00001021 /// NOTE: This should only be called for definitions.
1022 void SetCommonAttributes(const Decl *D, llvm::GlobalValue *GV);
Daniel Dunbar449a3392008-09-04 23:41:35 +00001023
Rafael Espindolaee076a22014-05-13 18:45:53 +00001024 void setNonAliasAttributes(const Decl *D, llvm::GlobalObject *GO);
Rafael Espindola502f65a2014-05-05 20:21:03 +00001025
Rafael Espindolae033c8c2014-05-08 15:26:12 +00001026 /// Set attributes for a global definition.
1027 void setFunctionDefinitionAttributes(const FunctionDecl *D,
1028 llvm::Function *F);
Mike Stump11289f42009-09-09 15:08:12 +00001029
Rafael Espindolae98ed2f2014-05-09 01:34:38 +00001030 /// Set function attributes for a function declaration.
Anders Carlsson6710c532010-02-06 02:44:09 +00001031 void SetFunctionAttributes(GlobalDecl GD,
Eli Friedman895771a2009-05-26 01:22:57 +00001032 llvm::Function *F,
1033 bool IsIncompleteFunction);
Nuno Lopesb6f79532008-06-08 15:45:52 +00001034
Craig Topper8a13c412014-05-21 05:09:00 +00001035 void EmitGlobalDefinition(GlobalDecl D, llvm::GlobalValue *GV = nullptr);
Daniel Dunbarf0acf7b2009-02-19 07:15:39 +00001036
Rafael Espindolacd7743b2013-12-09 16:01:03 +00001037 void EmitGlobalFunctionDefinition(GlobalDecl GD, llvm::GlobalValue *GV);
Daniel Dunbar9c426522008-07-29 23:18:29 +00001038 void EmitGlobalVarDefinition(const VarDecl *D);
John McCall7ec50432010-03-19 23:29:14 +00001039 void EmitAliasDefinition(GlobalDecl GD);
Daniel Dunbar89654ee2008-08-26 08:29:31 +00001040 void EmitObjCPropertyImplementations(const ObjCImplementationDecl *D);
Fariborz Jahanian0dec1e02010-04-28 21:28:56 +00001041 void EmitObjCIvarInitializations(ObjCImplementationDecl *D);
Fariborz Jahanian1518a5ec2010-10-27 16:21:54 +00001042
Anders Carlssonf7475242009-04-15 15:55:24 +00001043 // C++ related functions.
Mike Stump11289f42009-09-09 15:08:12 +00001044
Rafael Espindola3f643bd2013-11-04 18:38:59 +00001045 bool TryEmitDefinitionAsAlias(GlobalDecl Alias, GlobalDecl Target,
1046 bool InEveryTU);
John McCallf8ff7b92010-02-23 00:48:20 +00001047 bool TryEmitBaseDestructorAsAlias(const CXXDestructorDecl *D);
John McCalld4324142010-02-19 01:32:20 +00001048
Anders Carlsson237f3492009-04-01 00:58:25 +00001049 void EmitNamespace(const NamespaceDecl *D);
Anders Carlssoncbaeb9e2009-04-02 05:55:18 +00001050 void EmitLinkageSpec(const LinkageSpecDecl *D);
Adrian Prantlffcf4ba2013-05-09 23:16:27 +00001051 void CompleteDIClassType(const CXXMethodDecl* D);
Anders Carlssonf7475242009-04-15 15:55:24 +00001052
Rafael Espindolae98ed2f2014-05-09 01:34:38 +00001053 /// Emit a single constructor with the given type from a C++ constructor Decl.
Anders Carlssonf7475242009-04-15 15:55:24 +00001054 void EmitCXXConstructor(const CXXConstructorDecl *D, CXXCtorType Type);
Mike Stump11289f42009-09-09 15:08:12 +00001055
Rafael Espindolae98ed2f2014-05-09 01:34:38 +00001056 /// Emit a single destructor with the given type from a C++ destructor Decl.
Anders Carlssoneaa28f72009-04-17 01:58:57 +00001057 void EmitCXXDestructor(const CXXDestructorDecl *D, CXXDtorType Type);
Mike Stump11289f42009-09-09 15:08:12 +00001058
Richard Smith2fd1d7a2013-04-19 16:42:07 +00001059 /// \brief Emit the function that initializes C++ thread_local variables.
1060 void EmitCXXThreadLocalInitFunc();
1061
Rafael Espindolae98ed2f2014-05-09 01:34:38 +00001062 /// Emit the function that initializes C++ globals.
Anders Carlssonb8be93f2009-08-08 23:24:23 +00001063 void EmitCXXGlobalInitFunc();
Eli Friedman5866fe32010-01-08 00:50:11 +00001064
Rafael Espindolae98ed2f2014-05-09 01:34:38 +00001065 /// Emit the function that destroys C++ globals.
Daniel Dunbarfe06df42010-03-20 04:15:41 +00001066 void EmitCXXGlobalDtorFunc();
1067
Rafael Espindolae98ed2f2014-05-09 01:34:38 +00001068 /// Emit the function that initializes the specified global (if PerformInit is
1069 /// true) and registers its destructor.
John McCallcdf7ef52010-11-06 09:44:32 +00001070 void EmitCXXGlobalVarDeclInitFunc(const VarDecl *D,
Richard Smith6331c402012-02-13 22:16:19 +00001071 llvm::GlobalVariable *Addr,
1072 bool PerformInit);
Eli Friedman5866fe32010-01-08 00:50:11 +00001073
Daniel Dunbar74aa7e12008-08-01 00:01:51 +00001074 // FIXME: Hardcoding priority here is gross.
Chris Lattnere0be0df2009-05-12 21:21:08 +00001075 void AddGlobalCtor(llvm::Function *Ctor, int Priority=65535);
1076 void AddGlobalDtor(llvm::Function *Dtor, int Priority=65535);
Daniel Dunbar9c426522008-07-29 23:18:29 +00001077
Rafael Espindolae98ed2f2014-05-09 01:34:38 +00001078 /// Generates a global array of functions and priorities using the given list
1079 /// and name. This array will have appending linkage and is suitable for use
1080 /// as a LLVM constructor or destructor array.
Daniel Dunbar74aa7e12008-08-01 00:01:51 +00001081 void EmitCtorList(const CtorList &Fns, const char *GlobalName);
1082
Rafael Espindolae98ed2f2014-05-09 01:34:38 +00001083 /// Emit the RTTI descriptors for the given type.
Rafael Espindolaadcc1d12010-03-27 02:52:14 +00001084 void EmitFundamentalRTTIDescriptor(QualType Type);
1085
Rafael Espindolae98ed2f2014-05-09 01:34:38 +00001086 /// Emit any needed decls for which code generation was deferred.
Dmitri Gribenkob2aa9232012-11-15 14:28:07 +00001087 void EmitDeferred();
Daniel Dunbar08b26a02009-02-13 20:29:50 +00001088
Rafael Espindola2e2995b2013-11-05 21:37:29 +00001089 /// Call replaceAllUsesWith on all pairs in Replacements.
1090 void applyReplacements();
1091
Rafael Espindola208b5c02013-10-22 19:26:13 +00001092 void checkAliases();
1093
Rafael Espindolae98ed2f2014-05-09 01:34:38 +00001094 /// Emit any vtables which we deferred and still have a use for.
John McCall6bd2a892013-01-25 22:31:03 +00001095 void EmitDeferredVTables();
1096
Rafael Espindola060062a2014-03-06 22:15:10 +00001097 /// Emit the llvm.used and llvm.compiler.used metadata.
1098 void emitLLVMUsed();
Daniel Dunbar23fd4622008-10-01 00:49:24 +00001099
Douglas Gregorbc25ff42013-01-14 20:53:57 +00001100 /// \brief Emit the link options introduced by imported modules.
1101 void EmitModuleLinkOptions();
1102
Richard Smithdf931ce2013-04-06 05:00:46 +00001103 /// \brief Emit aliases for internal-linkage declarations inside "C" language
1104 /// linkage specifications, giving them the "expected" name where possible.
1105 void EmitStaticExternCAliases();
1106
John McCall09ae0322010-07-06 23:57:41 +00001107 void EmitDeclMetadata();
1108
Rafael Espindola3bfa4682013-10-16 19:28:50 +00001109 /// \brief Emit the Clang version as llvm.ident metadata.
1110 void EmitVersionIdentMetadata();
1111
Rafael Espindolae98ed2f2014-05-09 01:34:38 +00001112 /// Emit the llvm.gcov metadata used to tell LLVM where to emit the .gcno and
1113 /// .gcda files in a way that persists in .bc files.
Nick Lewycky85c011d2011-05-05 00:08:20 +00001114 void EmitCoverageFile();
Nick Lewycky480cb992011-05-04 20:46:58 +00001115
Nico Webercf4ff5862012-10-11 10:13:44 +00001116 /// Emits the initializer for a uuidof string.
1117 llvm::Constant *EmitUuidofInitializer(StringRef uuidstr, QualType IIDType);
1118
Rafael Espindolae98ed2f2014-05-09 01:34:38 +00001119 /// Determine if the given decl can be emitted lazily; this is only relevant
1120 /// for definitions. The given decl must be either a function or var decl.
Daniel Dunbar6b8720e2009-02-13 21:18:01 +00001121 bool MayDeferGeneration(const ValueDecl *D);
John McCall0bdb1fd2010-09-16 06:16:50 +00001122
Rafael Espindolae98ed2f2014-05-09 01:34:38 +00001123 /// Check whether we can use a "simpler", more core exceptions personality
1124 /// function.
John McCall0bdb1fd2010-09-16 06:16:50 +00001125 void SimplifyPersonality();
Chris Lattnerf97fe382007-05-24 06:29:05 +00001126};
1127} // end namespace CodeGen
1128} // end namespace clang
Chris Lattnerf97fe382007-05-24 06:29:05 +00001129
1130#endif