blob: 615ec23aa7d8efff71a634231df812b6f342cc7d [file] [log] [blame]
Charles Davis4e786dd2010-05-25 19:52:27 +00001//===----- CGCXXABI.h - Interface to C++ ABIs -------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This provides an abstract class for C++ code generation. Concrete subclasses
11// of this implement code generation for specific C++ ABIs.
12//
13//===----------------------------------------------------------------------===//
14
15#ifndef CLANG_CODEGEN_CXXABI_H
16#define CLANG_CODEGEN_CXXABI_H
17
John McCall5d865c322010-08-31 07:33:07 +000018#include "CodeGenFunction.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000019#include "clang/Basic/LLVM.h"
John McCall5d865c322010-08-31 07:33:07 +000020
John McCall475999d2010-08-22 00:05:51 +000021namespace llvm {
Rafael Espindola14048092014-05-09 00:26:20 +000022class Constant;
23class Type;
24class Value;
John McCall475999d2010-08-22 00:05:51 +000025}
26
Charles Davis4e786dd2010-05-25 19:52:27 +000027namespace clang {
Rafael Espindola14048092014-05-09 00:26:20 +000028class CastExpr;
29class CXXConstructorDecl;
30class CXXDestructorDecl;
31class CXXMethodDecl;
32class CXXRecordDecl;
33class FieldDecl;
34class MangleContext;
John McCall475999d2010-08-22 00:05:51 +000035
Charles Davis4e786dd2010-05-25 19:52:27 +000036namespace CodeGen {
Rafael Espindola14048092014-05-09 00:26:20 +000037class CodeGenFunction;
38class CodeGenModule;
Charles Davis4e786dd2010-05-25 19:52:27 +000039
James Dennett6e5cffb2012-06-15 07:35:42 +000040/// \brief Implements C++ ABI-specific code generation functions.
Charles Davis53c59df2010-08-16 03:33:14 +000041class CGCXXABI {
John McCalla1dee5302010-08-22 10:59:02 +000042protected:
43 CodeGenModule &CGM;
Ahmed Charlesb8984322014-03-07 20:03:18 +000044 std::unique_ptr<MangleContext> MangleCtx;
John McCalla1dee5302010-08-22 10:59:02 +000045
Peter Collingbourne0ff0b372011-01-13 18:57:25 +000046 CGCXXABI(CodeGenModule &CGM)
47 : CGM(CGM), MangleCtx(CGM.getContext().createMangleContext()) {}
John McCalla1dee5302010-08-22 10:59:02 +000048
John McCall5d865c322010-08-31 07:33:07 +000049protected:
50 ImplicitParamDecl *&getThisDecl(CodeGenFunction &CGF) {
Eli Friedman9fbeba02012-02-11 02:57:39 +000051 return CGF.CXXABIThisDecl;
John McCall5d865c322010-08-31 07:33:07 +000052 }
53 llvm::Value *&getThisValue(CodeGenFunction &CGF) {
Eli Friedman9fbeba02012-02-11 02:57:39 +000054 return CGF.CXXABIThisValue;
John McCall5d865c322010-08-31 07:33:07 +000055 }
56
Reid Kleckner407e8b62013-03-22 19:02:54 +000057 /// Issue a diagnostic about unsupported features in the ABI.
58 void ErrorUnsupportedABI(CodeGenFunction &CGF, StringRef S);
59
60 /// Get a null value for unsupported member pointers.
61 llvm::Constant *GetBogusMemberPointer(QualType T);
62
Timur Iskhodzhanovee6bc532013-02-13 08:37:51 +000063 ImplicitParamDecl *&getStructorImplicitParamDecl(CodeGenFunction &CGF) {
64 return CGF.CXXStructorImplicitParamDecl;
65 }
66 llvm::Value *&getStructorImplicitParamValue(CodeGenFunction &CGF) {
67 return CGF.CXXStructorImplicitParamValue;
John McCall5d865c322010-08-31 07:33:07 +000068 }
69
John McCall5d865c322010-08-31 07:33:07 +000070 /// Perform prolog initialization of the parameter variable suitable
Reid Kleckner89077a12013-12-17 19:46:40 +000071 /// for 'this' emitted by buildThisParam.
John McCall5d865c322010-08-31 07:33:07 +000072 void EmitThisParam(CodeGenFunction &CGF);
73
John McCall8ed55a52010-09-02 09:58:18 +000074 ASTContext &getContext() const { return CGM.getContext(); }
75
John McCallb91cd662012-05-01 05:23:51 +000076 virtual bool requiresArrayCookie(const CXXDeleteExpr *E, QualType eltType);
77 virtual bool requiresArrayCookie(const CXXNewExpr *E);
78
Charles Davis4e786dd2010-05-25 19:52:27 +000079public:
John McCalla1dee5302010-08-22 10:59:02 +000080
Anders Carlssone8ba4732010-11-28 17:50:09 +000081 virtual ~CGCXXABI();
Charles Davis4e786dd2010-05-25 19:52:27 +000082
83 /// Gets the mangle context.
Peter Collingbourne0ff0b372011-01-13 18:57:25 +000084 MangleContext &getMangleContext() {
85 return *MangleCtx;
86 }
John McCall475999d2010-08-22 00:05:51 +000087
Stephen Lin9dc6eef2013-06-30 20:40:16 +000088 /// Returns true if the given constructor or destructor is one of the
89 /// kinds that the ABI says returns 'this' (only applies when called
90 /// non-virtually for destructors).
91 ///
92 /// There currently is no way to indicate if a destructor returns 'this'
93 /// when called virtually, and code generation does not support the case.
Manman Ren01754612013-03-20 16:59:38 +000094 virtual bool HasThisReturn(GlobalDecl GD) const { return false; }
95
Reid Kleckner40ca9132014-05-13 22:05:45 +000096 /// If the C++ ABI requires the given type be returned in a particular way,
97 /// this method sets RetAI and returns true.
98 virtual bool classifyReturnType(CGFunctionInfo &FI) const = 0;
Timur Iskhodzhanov8fe501d2013-04-17 12:54:10 +000099
100 /// Specify how one should pass an argument of a record type.
101 enum RecordArgABI {
102 /// Pass it using the normal C aggregate rules for the ABI, potentially
103 /// introducing extra copies and passing some or all of it in registers.
104 RAA_Default = 0,
105
106 /// Pass it on the stack using its defined layout. The argument must be
107 /// evaluated directly into the correct stack position in the arguments area,
108 /// and the call machinery must not move it or introduce extra copies.
109 RAA_DirectInMemory,
110
111 /// Pass it as a pointer to temporary memory.
112 RAA_Indirect
113 };
114
Reid Klecknercf87e102014-05-14 16:02:09 +0000115 /// Returns true if C++ allows us to copy the memory of an object of type RD
116 /// when it is passed as an argument.
117 bool canCopyArgument(const CXXRecordDecl *RD) const;
118
Timur Iskhodzhanov8fe501d2013-04-17 12:54:10 +0000119 /// Returns how an argument of the given record type should be passed.
120 virtual RecordArgABI getRecordArgABI(const CXXRecordDecl *RD) const = 0;
121
Reid Kleckner37abaca2014-05-09 22:46:15 +0000122 /// Returns true if the implicit 'sret' parameter comes after the implicit
123 /// 'this' parameter of C++ instance methods.
124 virtual bool isSRetParameterAfterThis() const { return false; }
125
John McCall7a9aac22010-08-23 01:21:21 +0000126 /// Find the LLVM type used to represent the given member pointer
127 /// type.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000128 virtual llvm::Type *
John McCall7a9aac22010-08-23 01:21:21 +0000129 ConvertMemberPointerType(const MemberPointerType *MPT);
130
131 /// Load a member function from an object and a member function
132 /// pointer. Apply the this-adjustment and set 'This' to the
133 /// adjusted value.
David Majnemer2b0d66d2014-02-20 23:22:07 +0000134 virtual llvm::Value *EmitLoadOfMemberFunctionPointer(
135 CodeGenFunction &CGF, const Expr *E, llvm::Value *&This,
136 llvm::Value *MemPtr, const MemberPointerType *MPT);
John McCalla8bbb822010-08-22 03:04:22 +0000137
John McCallc134eb52010-08-31 21:07:20 +0000138 /// Calculate an l-value from an object and a data member pointer.
David Majnemer2b0d66d2014-02-20 23:22:07 +0000139 virtual llvm::Value *
140 EmitMemberDataPointerAddress(CodeGenFunction &CGF, const Expr *E,
141 llvm::Value *Base, llvm::Value *MemPtr,
142 const MemberPointerType *MPT);
John McCallc134eb52010-08-31 21:07:20 +0000143
John McCallc62bb392012-02-15 01:22:51 +0000144 /// Perform a derived-to-base, base-to-derived, or bitcast member
145 /// pointer conversion.
John McCall7a9aac22010-08-23 01:21:21 +0000146 virtual llvm::Value *EmitMemberPointerConversion(CodeGenFunction &CGF,
147 const CastExpr *E,
148 llvm::Value *Src);
John McCall84fa5102010-08-22 04:16:24 +0000149
John McCallc62bb392012-02-15 01:22:51 +0000150 /// Perform a derived-to-base, base-to-derived, or bitcast member
151 /// pointer conversion on a constant value.
152 virtual llvm::Constant *EmitMemberPointerConversion(const CastExpr *E,
153 llvm::Constant *Src);
154
John McCall7a9aac22010-08-23 01:21:21 +0000155 /// Return true if the given member pointer can be zero-initialized
156 /// (in the C++ sense) with an LLVM zeroinitializer.
John McCall614dbdc2010-08-22 21:01:12 +0000157 virtual bool isZeroInitializable(const MemberPointerType *MPT);
John McCall84fa5102010-08-22 04:16:24 +0000158
John McCall7a9aac22010-08-23 01:21:21 +0000159 /// Create a null member pointer of the given type.
160 virtual llvm::Constant *EmitNullMemberPointer(const MemberPointerType *MPT);
John McCall84fa5102010-08-22 04:16:24 +0000161
John McCall7a9aac22010-08-23 01:21:21 +0000162 /// Create a member pointer for the given method.
John McCall2979fe02011-04-12 00:42:48 +0000163 virtual llvm::Constant *EmitMemberPointer(const CXXMethodDecl *MD);
John McCall1c456c82010-08-22 06:43:33 +0000164
John McCall7a9aac22010-08-23 01:21:21 +0000165 /// Create a member pointer for the given field.
John McCallf3a88602011-02-03 08:15:49 +0000166 virtual llvm::Constant *EmitMemberDataPointer(const MemberPointerType *MPT,
167 CharUnits offset);
John McCall131d97d2010-08-22 08:30:07 +0000168
Richard Smithdafff942012-01-14 04:30:29 +0000169 /// Create a member pointer for the given member pointer constant.
170 virtual llvm::Constant *EmitMemberPointer(const APValue &MP, QualType MPT);
171
John McCall7a9aac22010-08-23 01:21:21 +0000172 /// Emit a comparison between two member pointers. Returns an i1.
John McCall131d97d2010-08-22 08:30:07 +0000173 virtual llvm::Value *
John McCall7a9aac22010-08-23 01:21:21 +0000174 EmitMemberPointerComparison(CodeGenFunction &CGF,
175 llvm::Value *L,
176 llvm::Value *R,
177 const MemberPointerType *MPT,
178 bool Inequality);
John McCall131d97d2010-08-22 08:30:07 +0000179
John McCall7a9aac22010-08-23 01:21:21 +0000180 /// Determine if a member pointer is non-null. Returns an i1.
John McCall131d97d2010-08-22 08:30:07 +0000181 virtual llvm::Value *
John McCall7a9aac22010-08-23 01:21:21 +0000182 EmitMemberPointerIsNotNull(CodeGenFunction &CGF,
183 llvm::Value *MemPtr,
184 const MemberPointerType *MPT);
John McCall5d865c322010-08-31 07:33:07 +0000185
John McCallc62bb392012-02-15 01:22:51 +0000186protected:
187 /// A utility method for computing the offset required for the given
188 /// base-to-derived or derived-to-base member-pointer conversion.
189 /// Does not handle virtual conversions (in case we ever fully
190 /// support an ABI that allows this). Returns null if no adjustment
191 /// is required.
192 llvm::Constant *getMemberPointerAdjustment(const CastExpr *E);
193
Reid Kleckner452abac2013-05-09 21:01:17 +0000194 /// \brief Computes the non-virtual adjustment needed for a member pointer
195 /// conversion along an inheritance path stored in an APValue. Unlike
196 /// getMemberPointerAdjustment(), the adjustment can be negative if the path
197 /// is from a derived type to a base type.
198 CharUnits getMemberPointerPathAdjustment(const APValue &MP);
199
John McCallc62bb392012-02-15 01:22:51 +0000200public:
John McCall82fb8922012-09-25 10:10:39 +0000201 /// Adjust the given non-null pointer to an object of polymorphic
202 /// type to point to the complete object.
203 ///
204 /// The IR type of the result should be a pointer but is otherwise
205 /// irrelevant.
206 virtual llvm::Value *adjustToCompleteObject(CodeGenFunction &CGF,
207 llvm::Value *ptr,
208 QualType type) = 0;
209
Reid Klecknerd8cbeec2013-05-29 18:02:47 +0000210 virtual llvm::Value *GetVirtualBaseClassOffset(CodeGenFunction &CGF,
211 llvm::Value *This,
212 const CXXRecordDecl *ClassDecl,
213 const CXXRecordDecl *BaseClassDecl) = 0;
214
John McCall5d865c322010-08-31 07:33:07 +0000215 /// Build the signature of the given constructor variant by adding
Stephen Lin9dc6eef2013-06-30 20:40:16 +0000216 /// any required parameters. For convenience, ArgTys has been initialized
217 /// with the type of 'this' and ResTy has been initialized with the type of
218 /// 'this' if HasThisReturn(GlobalDecl(Ctor, T)) is true or 'void' otherwise
219 /// (although both may be changed by the ABI).
John McCall5d865c322010-08-31 07:33:07 +0000220 ///
221 /// If there are ever any ABIs where the implicit parameters are
222 /// intermixed with the formal parameters, we can address those
223 /// then.
224 virtual void BuildConstructorSignature(const CXXConstructorDecl *Ctor,
225 CXXCtorType T,
226 CanQualType &ResTy,
Chris Lattner01cf8db2011-07-20 06:58:45 +0000227 SmallVectorImpl<CanQualType> &ArgTys) = 0;
John McCall5d865c322010-08-31 07:33:07 +0000228
Reid Kleckner7810af02013-06-19 15:20:38 +0000229 virtual llvm::BasicBlock *EmitCtorCompleteObjectHandler(CodeGenFunction &CGF,
230 const CXXRecordDecl *RD);
Timur Iskhodzhanov57cbe5c2013-02-27 13:46:31 +0000231
Timur Iskhodzhanovb6487322013-10-09 18:16:58 +0000232 /// Emit the code to initialize hidden members required
233 /// to handle virtual inheritance, if needed by the ABI.
234 virtual void
235 initializeHiddenVirtualInheritanceMembers(CodeGenFunction &CGF,
236 const CXXRecordDecl *RD) {}
237
Timur Iskhodzhanov40f2fa92013-08-04 17:30:04 +0000238 /// Emit constructor variants required by this ABI.
239 virtual void EmitCXXConstructors(const CXXConstructorDecl *D) = 0;
240
John McCall5d865c322010-08-31 07:33:07 +0000241 /// Build the signature of the given destructor variant by adding
Stephen Lin9dc6eef2013-06-30 20:40:16 +0000242 /// any required parameters. For convenience, ArgTys has been initialized
243 /// with the type of 'this' and ResTy has been initialized with the type of
244 /// 'this' if HasThisReturn(GlobalDecl(Dtor, T)) is true or 'void' otherwise
245 /// (although both may be changed by the ABI).
John McCall5d865c322010-08-31 07:33:07 +0000246 virtual void BuildDestructorSignature(const CXXDestructorDecl *Dtor,
247 CXXDtorType T,
248 CanQualType &ResTy,
Chris Lattner01cf8db2011-07-20 06:58:45 +0000249 SmallVectorImpl<CanQualType> &ArgTys) = 0;
John McCall5d865c322010-08-31 07:33:07 +0000250
Reid Klecknere7de47e2013-07-22 13:51:44 +0000251 /// Returns true if the given destructor type should be emitted as a linkonce
252 /// delegating thunk, regardless of whether the dtor is defined in this TU or
253 /// not.
254 virtual bool useThunkForDtorVariant(const CXXDestructorDecl *Dtor,
255 CXXDtorType DT) const = 0;
256
257 /// Emit destructor variants required by this ABI.
258 virtual void EmitCXXDestructors(const CXXDestructorDecl *D) = 0;
259
Timur Iskhodzhanov88fd4392013-08-21 06:25:03 +0000260 /// Get the type of the implicit "this" parameter used by a method. May return
261 /// zero if no specific type is applicable, e.g. if the ABI expects the "this"
262 /// parameter to point to some artificial offset in a complete object due to
263 /// vbases being reordered.
264 virtual const CXXRecordDecl *
265 getThisArgumentTypeForMethod(const CXXMethodDecl *MD) {
266 return MD->getParent();
267 }
268
269 /// Perform ABI-specific "this" argument adjustment required prior to
Timur Iskhodzhanovf1749422014-03-14 17:43:37 +0000270 /// a call of a virtual function.
271 /// The "VirtualCall" argument is true iff the call itself is virtual.
272 virtual llvm::Value *
273 adjustThisArgumentForVirtualFunctionCall(CodeGenFunction &CGF, GlobalDecl GD,
274 llvm::Value *This,
275 bool VirtualCall) {
Timur Iskhodzhanov88fd4392013-08-21 06:25:03 +0000276 return This;
277 }
278
Reid Kleckner89077a12013-12-17 19:46:40 +0000279 /// Build a parameter variable suitable for 'this'.
280 void buildThisParam(CodeGenFunction &CGF, FunctionArgList &Params);
281
282 /// Insert any ABI-specific implicit parameters into the parameter list for a
283 /// function. This generally involves extra data for constructors and
284 /// destructors.
John McCall5d865c322010-08-31 07:33:07 +0000285 ///
286 /// ABIs may also choose to override the return type, which has been
Stephen Lin9dc6eef2013-06-30 20:40:16 +0000287 /// initialized with the type of 'this' if HasThisReturn(CGF.CurGD) is true or
288 /// the formal return type of the function otherwise.
Reid Kleckner89077a12013-12-17 19:46:40 +0000289 virtual void addImplicitStructorParams(CodeGenFunction &CGF, QualType &ResTy,
290 FunctionArgList &Params) = 0;
John McCall5d865c322010-08-31 07:33:07 +0000291
Timur Iskhodzhanov88fd4392013-08-21 06:25:03 +0000292 /// Perform ABI-specific "this" parameter adjustment in a virtual function
293 /// prologue.
294 virtual llvm::Value *adjustThisParameterInVirtualFunctionPrologue(
295 CodeGenFunction &CGF, GlobalDecl GD, llvm::Value *This) {
296 return This;
297 }
298
John McCall5d865c322010-08-31 07:33:07 +0000299 /// Emit the ABI-specific prolog for the function.
300 virtual void EmitInstanceFunctionProlog(CodeGenFunction &CGF) = 0;
301
Reid Kleckner89077a12013-12-17 19:46:40 +0000302 /// Add any ABI-specific implicit arguments needed to call a constructor.
303 ///
304 /// \return The number of args added to the call, which is typically zero or
305 /// one.
306 virtual unsigned
307 addImplicitConstructorArgs(CodeGenFunction &CGF, const CXXConstructorDecl *D,
308 CXXCtorType Type, bool ForVirtualBase,
309 bool Delegating, CallArgList &Args) = 0;
Timur Iskhodzhanov57cbe5c2013-02-27 13:46:31 +0000310
Reid Kleckner6fe771a2013-12-13 00:53:54 +0000311 /// Emit the destructor call.
312 virtual void EmitDestructorCall(CodeGenFunction &CGF,
313 const CXXDestructorDecl *DD, CXXDtorType Type,
314 bool ForVirtualBase, bool Delegating,
315 llvm::Value *This) = 0;
316
Timur Iskhodzhanov8b5987e2013-09-27 14:48:01 +0000317 /// Emits the VTable definitions required for the given record type.
318 virtual void emitVTableDefinitions(CodeGenVTables &CGVT,
319 const CXXRecordDecl *RD) = 0;
320
321 /// Get the address point of the vtable for the given base subobject while
322 /// building a constructor or a destructor. On return, NeedsVirtualOffset
323 /// tells if a virtual base adjustment is needed in order to get the offset
324 /// of the base subobject.
325 virtual llvm::Value *getVTableAddressPointInStructor(
326 CodeGenFunction &CGF, const CXXRecordDecl *RD, BaseSubobject Base,
327 const CXXRecordDecl *NearestVBase, bool &NeedsVirtualOffset) = 0;
328
329 /// Get the address point of the vtable for the given base subobject while
330 /// building a constexpr.
331 virtual llvm::Constant *
332 getVTableAddressPointForConstExpr(BaseSubobject Base,
333 const CXXRecordDecl *VTableClass) = 0;
334
335 /// Get the address of the vtable for the given record decl which should be
336 /// used for the vptr at the given offset in RD.
337 virtual llvm::GlobalVariable *getAddrOfVTable(const CXXRecordDecl *RD,
338 CharUnits VPtrOffset) = 0;
339
Timur Iskhodzhanov88fd4392013-08-21 06:25:03 +0000340 /// Build a virtual function pointer in the ABI-specific way.
341 virtual llvm::Value *getVirtualFunctionPointer(CodeGenFunction &CGF,
342 GlobalDecl GD,
343 llvm::Value *This,
344 llvm::Type *Ty) = 0;
345
Timur Iskhodzhanovd6197112013-02-15 14:45:22 +0000346 /// Emit the ABI-specific virtual destructor call.
Stephen Lin9dc6eef2013-06-30 20:40:16 +0000347 virtual void EmitVirtualDestructorCall(CodeGenFunction &CGF,
348 const CXXDestructorDecl *Dtor,
349 CXXDtorType DtorType,
350 SourceLocation CallLoc,
351 llvm::Value *This) = 0;
Timur Iskhodzhanovd6197112013-02-15 14:45:22 +0000352
Timur Iskhodzhanovad9d3b82013-10-09 09:23:58 +0000353 virtual void adjustCallArgsForDestructorThunk(CodeGenFunction &CGF,
354 GlobalDecl GD,
355 CallArgList &CallArgs) {}
356
Reid Kleckner7810af02013-06-19 15:20:38 +0000357 /// Emit any tables needed to implement virtual inheritance. For Itanium,
358 /// this emits virtual table tables. For the MSVC++ ABI, this emits virtual
359 /// base tables.
Timur Iskhodzhanov8b5987e2013-09-27 14:48:01 +0000360 virtual void emitVirtualInheritanceTables(const CXXRecordDecl *RD) = 0;
Reid Kleckner7810af02013-06-19 15:20:38 +0000361
Timur Iskhodzhanovad9d3b82013-10-09 09:23:58 +0000362 virtual void setThunkLinkage(llvm::Function *Thunk, bool ForVTable) = 0;
363
Timur Iskhodzhanov02014322013-10-30 11:55:43 +0000364 virtual llvm::Value *performThisAdjustment(CodeGenFunction &CGF,
365 llvm::Value *This,
366 const ThisAdjustment &TA) = 0;
367
368 virtual llvm::Value *performReturnAdjustment(CodeGenFunction &CGF,
369 llvm::Value *Ret,
370 const ReturnAdjustment &RA) = 0;
371
John McCall5d865c322010-08-31 07:33:07 +0000372 virtual void EmitReturnFromThunk(CodeGenFunction &CGF,
373 RValue RV, QualType ResultType);
John McCall8ed55a52010-09-02 09:58:18 +0000374
Joao Matos2ce88ef2012-07-17 17:10:11 +0000375 /// Gets the pure virtual member call function.
376 virtual StringRef GetPureVirtualCallName() = 0;
377
David Blaikieeb7d5982012-10-16 22:56:05 +0000378 /// Gets the deleted virtual member call name.
379 virtual StringRef GetDeletedVirtualCallName() = 0;
380
Hans Wennborgfeedf852013-11-21 00:15:56 +0000381 /// \brief Returns true iff static data members that are initialized in the
382 /// class definition should have linkonce linkage.
383 virtual bool isInlineInitializedStaticDataMemberLinkOnce() { return false; }
384
John McCall8ed55a52010-09-02 09:58:18 +0000385 /**************************** Array cookies ******************************/
386
387 /// Returns the extra size required in order to store the array
James Dennett41725122012-06-22 10:16:05 +0000388 /// cookie for the given new-expression. May return 0 to indicate that no
John McCall8ed55a52010-09-02 09:58:18 +0000389 /// array cookie is required.
390 ///
391 /// Several cases are filtered out before this method is called:
392 /// - non-array allocations never need a cookie
James Dennetta02e11f2012-06-20 00:57:15 +0000393 /// - calls to \::operator new(size_t, void*) never need a cookie
John McCall8ed55a52010-09-02 09:58:18 +0000394 ///
James Dennett41725122012-06-22 10:16:05 +0000395 /// \param expr - the new-expression being allocated.
John McCall284c48f2011-01-27 09:37:56 +0000396 virtual CharUnits GetArrayCookieSize(const CXXNewExpr *expr);
John McCall8ed55a52010-09-02 09:58:18 +0000397
398 /// Initialize the array cookie for the given allocation.
399 ///
400 /// \param NewPtr - a char* which is the presumed-non-null
401 /// return value of the allocation function
402 /// \param NumElements - the computed number of elements,
John McCallb91cd662012-05-01 05:23:51 +0000403 /// potentially collapsed from the multidimensional array case;
404 /// always a size_t
John McCall8ed55a52010-09-02 09:58:18 +0000405 /// \param ElementType - the base element allocated type,
406 /// i.e. the allocated type after stripping all array types
407 virtual llvm::Value *InitializeArrayCookie(CodeGenFunction &CGF,
408 llvm::Value *NewPtr,
409 llvm::Value *NumElements,
John McCall284c48f2011-01-27 09:37:56 +0000410 const CXXNewExpr *expr,
John McCall8ed55a52010-09-02 09:58:18 +0000411 QualType ElementType);
412
413 /// Reads the array cookie associated with the given pointer,
414 /// if it has one.
415 ///
416 /// \param Ptr - a pointer to the first element in the array
417 /// \param ElementType - the base element type of elements of the array
418 /// \param NumElements - an out parameter which will be initialized
419 /// with the number of elements allocated, or zero if there is no
420 /// cookie
421 /// \param AllocPtr - an out parameter which will be initialized
422 /// with a char* pointing to the address returned by the allocation
423 /// function
424 /// \param CookieSize - an out parameter which will be initialized
425 /// with the size of the cookie, or zero if there is no cookie
426 virtual void ReadArrayCookie(CodeGenFunction &CGF, llvm::Value *Ptr,
John McCall284c48f2011-01-27 09:37:56 +0000427 const CXXDeleteExpr *expr,
John McCall8ed55a52010-09-02 09:58:18 +0000428 QualType ElementType, llvm::Value *&NumElements,
429 llvm::Value *&AllocPtr, CharUnits &CookieSize);
430
Peter Collingbourne66f82e62013-06-28 20:45:28 +0000431 /// Return whether the given global decl needs a VTT parameter.
432 virtual bool NeedsVTTParameter(GlobalDecl GD);
433
John McCallb91cd662012-05-01 05:23:51 +0000434protected:
435 /// Returns the extra size required in order to store the array
436 /// cookie for the given type. Assumes that an array cookie is
437 /// required.
438 virtual CharUnits getArrayCookieSizeImpl(QualType elementType);
439
440 /// Reads the array cookie for an allocation which is known to have one.
441 /// This is called by the standard implementation of ReadArrayCookie.
442 ///
443 /// \param ptr - a pointer to the allocation made for an array, as a char*
444 /// \param cookieSize - the computed cookie size of an array
James Dennett6e5cffb2012-06-15 07:35:42 +0000445 ///
John McCallb91cd662012-05-01 05:23:51 +0000446 /// Other parameters are as above.
James Dennett6e5cffb2012-06-15 07:35:42 +0000447 ///
John McCallb91cd662012-05-01 05:23:51 +0000448 /// \return a size_t
449 virtual llvm::Value *readArrayCookieImpl(CodeGenFunction &IGF,
450 llvm::Value *ptr,
451 CharUnits cookieSize);
452
453public:
454
John McCall68ff0372010-09-08 01:44:27 +0000455 /*************************** Static local guards ****************************/
456
John McCallcdf7ef52010-11-06 09:44:32 +0000457 /// Emits the guarded initializer and destructor setup for the given
458 /// variable, given that it couldn't be emitted as a constant.
Richard Smith6331c402012-02-13 22:16:19 +0000459 /// If \p PerformInit is false, the initialization has been folded to a
460 /// constant and should not be performed.
John McCallcdf7ef52010-11-06 09:44:32 +0000461 ///
462 /// The variable may be:
463 /// - a static local variable
464 /// - a static data member of a class template instantiation
465 virtual void EmitGuardedInit(CodeGenFunction &CGF, const VarDecl &D,
Reid Klecknerd8110b62013-09-10 20:14:30 +0000466 llvm::GlobalVariable *DeclPtr,
467 bool PerformInit) = 0;
John McCall68ff0372010-09-08 01:44:27 +0000468
John McCallc84ed6a2012-05-01 06:13:13 +0000469 /// Emit code to force the execution of a destructor during global
470 /// teardown. The default implementation of this uses atexit.
471 ///
472 /// \param dtor - a function taking a single pointer argument
473 /// \param addr - a pointer to pass to the destructor function.
Richard Smithdbf74ba2013-04-14 23:01:42 +0000474 virtual void registerGlobalDtor(CodeGenFunction &CGF, const VarDecl &D,
475 llvm::Constant *dtor, llvm::Constant *addr);
Richard Smith2fd1d7a2013-04-19 16:42:07 +0000476
477 /*************************** thread_local initialization ********************/
478
479 /// Emits ABI-required functions necessary to initialize thread_local
480 /// variables in this translation unit.
481 ///
482 /// \param Decls The thread_local declarations in this translation unit.
483 /// \param InitFunc If this translation unit contains any non-constant
484 /// initialization or non-trivial destruction for thread_local
485 /// variables, a function to perform the initialization. Otherwise, 0.
486 virtual void EmitThreadLocalInitFuncs(
487 llvm::ArrayRef<std::pair<const VarDecl *, llvm::GlobalVariable *> > Decls,
488 llvm::Function *InitFunc);
489
490 /// Emit a reference to a non-local thread_local variable (including
491 /// triggering the initialization of all thread_local variables in its
492 /// translation unit).
Richard Smith0f383742014-03-26 22:48:22 +0000493 virtual LValue EmitThreadLocalVarDeclLValue(CodeGenFunction &CGF,
494 const VarDecl *VD,
495 QualType LValType);
Tim Northover65f582f2014-03-30 17:32:48 +0000496
497 /**************************** RTTI Uniqueness ******************************/
498
499protected:
500 /// Returns true if the ABI requires RTTI type_info objects to be unique
501 /// across a program.
502 virtual bool shouldRTTIBeUnique() { return true; }
503
504public:
505 /// What sort of unique-RTTI behavior should we use?
506 enum RTTIUniquenessKind {
507 /// We are guaranteeing, or need to guarantee, that the RTTI string
508 /// is unique.
509 RUK_Unique,
510
511 /// We are not guaranteeing uniqueness for the RTTI string, so we
512 /// can demote to hidden visibility but must use string comparisons.
513 RUK_NonUniqueHidden,
514
515 /// We are not guaranteeing uniqueness for the RTTI string, so we
516 /// have to use string comparisons, but we also have to emit it with
517 /// non-hidden visibility.
518 RUK_NonUniqueVisible
519 };
520
521 /// Return the required visibility status for the given type and linkage in
522 /// the current ABI.
523 RTTIUniquenessKind
524 classifyRTTIUniqueness(QualType CanTy,
525 llvm::GlobalValue::LinkageTypes Linkage);
Charles Davis4e786dd2010-05-25 19:52:27 +0000526};
527
John McCall57625922013-01-25 23:36:14 +0000528// Create an instance of a C++ ABI class:
529
530/// Creates an Itanium-family ABI.
Charles Davis53c59df2010-08-16 03:33:14 +0000531CGCXXABI *CreateItaniumCXXABI(CodeGenModule &CGM);
John McCall57625922013-01-25 23:36:14 +0000532
533/// Creates a Microsoft-family ABI.
Charles Davis53c59df2010-08-16 03:33:14 +0000534CGCXXABI *CreateMicrosoftCXXABI(CodeGenModule &CGM);
John McCall475999d2010-08-22 00:05:51 +0000535
Charles Davis4e786dd2010-05-25 19:52:27 +0000536}
537}
538
539#endif