blob: d0384ecc128250f1f40f9470a17ceda5891d2206 [file] [log] [blame]
Charles Davis3a811f12010-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 McCall4c40d982010-08-31 07:33:07 +000018#include "CodeGenFunction.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000019#include "clang/Basic/LLVM.h"
John McCall4c40d982010-08-31 07:33:07 +000020
John McCall93d557b2010-08-22 00:05:51 +000021namespace llvm {
John McCallcf2c85e2010-08-22 04:16:24 +000022 class Constant;
John McCall0bab0cd2010-08-23 01:21:21 +000023 class Type;
John McCall93d557b2010-08-22 00:05:51 +000024 class Value;
25}
26
Charles Davis3a811f12010-05-25 19:52:27 +000027namespace clang {
John McCall3023def2010-08-22 03:04:22 +000028 class CastExpr;
John McCall4c40d982010-08-31 07:33:07 +000029 class CXXConstructorDecl;
30 class CXXDestructorDecl;
John McCall875ab102010-08-22 06:43:33 +000031 class CXXMethodDecl;
John McCallcf2c85e2010-08-22 04:16:24 +000032 class CXXRecordDecl;
John McCall0bab0cd2010-08-23 01:21:21 +000033 class FieldDecl;
Peter Collingbourne14110472011-01-13 18:57:25 +000034 class MangleContext;
John McCall93d557b2010-08-22 00:05:51 +000035
Charles Davis3a811f12010-05-25 19:52:27 +000036namespace CodeGen {
John McCall93d557b2010-08-22 00:05:51 +000037 class CodeGenFunction;
Charles Davis3a811f12010-05-25 19:52:27 +000038 class CodeGenModule;
Charles Davis3a811f12010-05-25 19:52:27 +000039
James Dennett3b2adf22012-06-15 07:35:42 +000040/// \brief Implements C++ ABI-specific code generation functions.
Charles Davis071cc7d2010-08-16 03:33:14 +000041class CGCXXABI {
John McCalld608cdb2010-08-22 10:59:02 +000042protected:
43 CodeGenModule &CGM;
Dylan Noblesmith6f42b622012-02-05 02:12:40 +000044 OwningPtr<MangleContext> MangleCtx;
John McCalld608cdb2010-08-22 10:59:02 +000045
Peter Collingbourne14110472011-01-13 18:57:25 +000046 CGCXXABI(CodeGenModule &CGM)
47 : CGM(CGM), MangleCtx(CGM.getContext().createMangleContext()) {}
John McCalld608cdb2010-08-22 10:59:02 +000048
John McCall4c40d982010-08-31 07:33:07 +000049protected:
50 ImplicitParamDecl *&getThisDecl(CodeGenFunction &CGF) {
Eli Friedmancec5ebd2012-02-11 02:57:39 +000051 return CGF.CXXABIThisDecl;
John McCall4c40d982010-08-31 07:33:07 +000052 }
53 llvm::Value *&getThisValue(CodeGenFunction &CGF) {
Eli Friedmancec5ebd2012-02-11 02:57:39 +000054 return CGF.CXXABIThisValue;
John McCall4c40d982010-08-31 07:33:07 +000055 }
56
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +000057 // FIXME: Every place that calls getVTT{Decl,Value} is something
58 // that needs to be abstracted properly.
John McCall4c40d982010-08-31 07:33:07 +000059 ImplicitParamDecl *&getVTTDecl(CodeGenFunction &CGF) {
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +000060 return CGF.CXXStructorImplicitParamDecl;
John McCall4c40d982010-08-31 07:33:07 +000061 }
62 llvm::Value *&getVTTValue(CodeGenFunction &CGF) {
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +000063 return CGF.CXXStructorImplicitParamValue;
64 }
65
66 ImplicitParamDecl *&getStructorImplicitParamDecl(CodeGenFunction &CGF) {
67 return CGF.CXXStructorImplicitParamDecl;
68 }
69 llvm::Value *&getStructorImplicitParamValue(CodeGenFunction &CGF) {
70 return CGF.CXXStructorImplicitParamValue;
John McCall4c40d982010-08-31 07:33:07 +000071 }
72
73 /// Build a parameter variable suitable for 'this'.
74 void BuildThisParam(CodeGenFunction &CGF, FunctionArgList &Params);
75
76 /// Perform prolog initialization of the parameter variable suitable
77 /// for 'this' emitted by BuildThisParam.
78 void EmitThisParam(CodeGenFunction &CGF);
79
John McCall1e7fe752010-09-02 09:58:18 +000080 ASTContext &getContext() const { return CGM.getContext(); }
81
John McCalle2b45e22012-05-01 05:23:51 +000082 virtual bool requiresArrayCookie(const CXXDeleteExpr *E, QualType eltType);
83 virtual bool requiresArrayCookie(const CXXNewExpr *E);
84
Charles Davis3a811f12010-05-25 19:52:27 +000085public:
John McCalld608cdb2010-08-22 10:59:02 +000086
Anders Carlsson1af610f2010-11-28 17:50:09 +000087 virtual ~CGCXXABI();
Charles Davis3a811f12010-05-25 19:52:27 +000088
89 /// Gets the mangle context.
Peter Collingbourne14110472011-01-13 18:57:25 +000090 MangleContext &getMangleContext() {
91 return *MangleCtx;
92 }
John McCall93d557b2010-08-22 00:05:51 +000093
Manman Ren63fd4082013-03-20 16:59:38 +000094 /// Returns true if the given instance method is one of the
95 /// kinds that the ABI says returns 'this'.
96 virtual bool HasThisReturn(GlobalDecl GD) const { return false; }
97
John McCall0bab0cd2010-08-23 01:21:21 +000098 /// Find the LLVM type used to represent the given member pointer
99 /// type.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000100 virtual llvm::Type *
John McCall0bab0cd2010-08-23 01:21:21 +0000101 ConvertMemberPointerType(const MemberPointerType *MPT);
102
103 /// Load a member function from an object and a member function
104 /// pointer. Apply the this-adjustment and set 'This' to the
105 /// adjusted value.
John McCall93d557b2010-08-22 00:05:51 +0000106 virtual llvm::Value *
107 EmitLoadOfMemberFunctionPointer(CodeGenFunction &CGF,
108 llvm::Value *&This,
109 llvm::Value *MemPtr,
110 const MemberPointerType *MPT);
John McCall3023def2010-08-22 03:04:22 +0000111
John McCall6c2ab1d2010-08-31 21:07:20 +0000112 /// Calculate an l-value from an object and a data member pointer.
113 virtual llvm::Value *EmitMemberDataPointerAddress(CodeGenFunction &CGF,
114 llvm::Value *Base,
115 llvm::Value *MemPtr,
116 const MemberPointerType *MPT);
117
John McCall4d4e5c12012-02-15 01:22:51 +0000118 /// Perform a derived-to-base, base-to-derived, or bitcast member
119 /// pointer conversion.
John McCall0bab0cd2010-08-23 01:21:21 +0000120 virtual llvm::Value *EmitMemberPointerConversion(CodeGenFunction &CGF,
121 const CastExpr *E,
122 llvm::Value *Src);
John McCallcf2c85e2010-08-22 04:16:24 +0000123
John McCall4d4e5c12012-02-15 01:22:51 +0000124 /// Perform a derived-to-base, base-to-derived, or bitcast member
125 /// pointer conversion on a constant value.
126 virtual llvm::Constant *EmitMemberPointerConversion(const CastExpr *E,
127 llvm::Constant *Src);
128
John McCall0bab0cd2010-08-23 01:21:21 +0000129 /// Return true if the given member pointer can be zero-initialized
130 /// (in the C++ sense) with an LLVM zeroinitializer.
John McCallf16aa102010-08-22 21:01:12 +0000131 virtual bool isZeroInitializable(const MemberPointerType *MPT);
John McCallcf2c85e2010-08-22 04:16:24 +0000132
John McCall0bab0cd2010-08-23 01:21:21 +0000133 /// Create a null member pointer of the given type.
134 virtual llvm::Constant *EmitNullMemberPointer(const MemberPointerType *MPT);
John McCallcf2c85e2010-08-22 04:16:24 +0000135
John McCall0bab0cd2010-08-23 01:21:21 +0000136 /// Create a member pointer for the given method.
John McCall755d8492011-04-12 00:42:48 +0000137 virtual llvm::Constant *EmitMemberPointer(const CXXMethodDecl *MD);
John McCall875ab102010-08-22 06:43:33 +0000138
John McCall0bab0cd2010-08-23 01:21:21 +0000139 /// Create a member pointer for the given field.
John McCall5808ce42011-02-03 08:15:49 +0000140 virtual llvm::Constant *EmitMemberDataPointer(const MemberPointerType *MPT,
141 CharUnits offset);
John McCalle9fd7eb2010-08-22 08:30:07 +0000142
Richard Smith2d6a5672012-01-14 04:30:29 +0000143 /// Create a member pointer for the given member pointer constant.
144 virtual llvm::Constant *EmitMemberPointer(const APValue &MP, QualType MPT);
145
John McCall0bab0cd2010-08-23 01:21:21 +0000146 /// Emit a comparison between two member pointers. Returns an i1.
John McCalle9fd7eb2010-08-22 08:30:07 +0000147 virtual llvm::Value *
John McCall0bab0cd2010-08-23 01:21:21 +0000148 EmitMemberPointerComparison(CodeGenFunction &CGF,
149 llvm::Value *L,
150 llvm::Value *R,
151 const MemberPointerType *MPT,
152 bool Inequality);
John McCalle9fd7eb2010-08-22 08:30:07 +0000153
John McCall0bab0cd2010-08-23 01:21:21 +0000154 /// Determine if a member pointer is non-null. Returns an i1.
John McCalle9fd7eb2010-08-22 08:30:07 +0000155 virtual llvm::Value *
John McCall0bab0cd2010-08-23 01:21:21 +0000156 EmitMemberPointerIsNotNull(CodeGenFunction &CGF,
157 llvm::Value *MemPtr,
158 const MemberPointerType *MPT);
John McCall4c40d982010-08-31 07:33:07 +0000159
John McCall4d4e5c12012-02-15 01:22:51 +0000160protected:
161 /// A utility method for computing the offset required for the given
162 /// base-to-derived or derived-to-base member-pointer conversion.
163 /// Does not handle virtual conversions (in case we ever fully
164 /// support an ABI that allows this). Returns null if no adjustment
165 /// is required.
166 llvm::Constant *getMemberPointerAdjustment(const CastExpr *E);
167
168public:
John McCallecd03b42012-09-25 10:10:39 +0000169 /// Adjust the given non-null pointer to an object of polymorphic
170 /// type to point to the complete object.
171 ///
172 /// The IR type of the result should be a pointer but is otherwise
173 /// irrelevant.
174 virtual llvm::Value *adjustToCompleteObject(CodeGenFunction &CGF,
175 llvm::Value *ptr,
176 QualType type) = 0;
177
John McCall4c40d982010-08-31 07:33:07 +0000178 /// Build the signature of the given constructor variant by adding
179 /// any required parameters. For convenience, ResTy has been
180 /// initialized to 'void', and ArgTys has been initialized with the
181 /// type of 'this' (although this may be changed by the ABI) and
182 /// will have the formal parameters added to it afterwards.
183 ///
184 /// If there are ever any ABIs where the implicit parameters are
185 /// intermixed with the formal parameters, we can address those
186 /// then.
187 virtual void BuildConstructorSignature(const CXXConstructorDecl *Ctor,
188 CXXCtorType T,
189 CanQualType &ResTy,
Chris Lattner686775d2011-07-20 06:58:45 +0000190 SmallVectorImpl<CanQualType> &ArgTys) = 0;
John McCall4c40d982010-08-31 07:33:07 +0000191
Timur Iskhodzhanov1d4fff52013-02-27 13:46:31 +0000192 virtual llvm::BasicBlock *EmitCtorCompleteObjectHandler(CodeGenFunction &CGF);
193
John McCall4c40d982010-08-31 07:33:07 +0000194 /// Build the signature of the given destructor variant by adding
195 /// any required parameters. For convenience, ResTy has been
196 /// initialized to 'void' and ArgTys has been initialized with the
197 /// type of 'this' (although this may be changed by the ABI).
198 virtual void BuildDestructorSignature(const CXXDestructorDecl *Dtor,
199 CXXDtorType T,
200 CanQualType &ResTy,
Chris Lattner686775d2011-07-20 06:58:45 +0000201 SmallVectorImpl<CanQualType> &ArgTys) = 0;
John McCall4c40d982010-08-31 07:33:07 +0000202
203 /// Build the ABI-specific portion of the parameter list for a
204 /// function. This generally involves a 'this' parameter and
205 /// possibly some extra data for constructors and destructors.
206 ///
207 /// ABIs may also choose to override the return type, which has been
208 /// initialized with the formal return type of the function.
209 virtual void BuildInstanceFunctionParams(CodeGenFunction &CGF,
210 QualType &ResTy,
211 FunctionArgList &Params) = 0;
212
213 /// Emit the ABI-specific prolog for the function.
214 virtual void EmitInstanceFunctionProlog(CodeGenFunction &CGF) = 0;
215
Manman Ren63fd4082013-03-20 16:59:38 +0000216 /// Emit the constructor call. Return the function that is called.
217 virtual llvm::Value *EmitConstructorCall(CodeGenFunction &CGF,
Timur Iskhodzhanov1d4fff52013-02-27 13:46:31 +0000218 const CXXConstructorDecl *D,
219 CXXCtorType Type, bool ForVirtualBase,
220 bool Delegating,
221 llvm::Value *This,
222 CallExpr::const_arg_iterator ArgBeg,
223 CallExpr::const_arg_iterator ArgEnd) = 0;
224
Timur Iskhodzhanov0f9827f2013-02-15 14:45:22 +0000225 /// Emit the ABI-specific virtual destructor call.
226 virtual RValue EmitVirtualDestructorCall(CodeGenFunction &CGF,
227 const CXXDestructorDecl *Dtor,
228 CXXDtorType DtorType,
229 SourceLocation CallLoc,
230 ReturnValueSlot ReturnValue,
231 llvm::Value *This) = 0;
232
John McCall4c40d982010-08-31 07:33:07 +0000233 virtual void EmitReturnFromThunk(CodeGenFunction &CGF,
234 RValue RV, QualType ResultType);
John McCall1e7fe752010-09-02 09:58:18 +0000235
Joao Matos285baac2012-07-17 17:10:11 +0000236 /// Gets the pure virtual member call function.
237 virtual StringRef GetPureVirtualCallName() = 0;
238
David Blaikie2eb9a952012-10-16 22:56:05 +0000239 /// Gets the deleted virtual member call name.
240 virtual StringRef GetDeletedVirtualCallName() = 0;
241
John McCall1e7fe752010-09-02 09:58:18 +0000242 /**************************** Array cookies ******************************/
243
244 /// Returns the extra size required in order to store the array
James Dennett16ae9de2012-06-22 10:16:05 +0000245 /// cookie for the given new-expression. May return 0 to indicate that no
John McCall1e7fe752010-09-02 09:58:18 +0000246 /// array cookie is required.
247 ///
248 /// Several cases are filtered out before this method is called:
249 /// - non-array allocations never need a cookie
James Dennett59001032012-06-20 00:57:15 +0000250 /// - calls to \::operator new(size_t, void*) never need a cookie
John McCall1e7fe752010-09-02 09:58:18 +0000251 ///
James Dennett16ae9de2012-06-22 10:16:05 +0000252 /// \param expr - the new-expression being allocated.
John McCall6ec278d2011-01-27 09:37:56 +0000253 virtual CharUnits GetArrayCookieSize(const CXXNewExpr *expr);
John McCall1e7fe752010-09-02 09:58:18 +0000254
255 /// Initialize the array cookie for the given allocation.
256 ///
257 /// \param NewPtr - a char* which is the presumed-non-null
258 /// return value of the allocation function
259 /// \param NumElements - the computed number of elements,
John McCalle2b45e22012-05-01 05:23:51 +0000260 /// potentially collapsed from the multidimensional array case;
261 /// always a size_t
John McCall1e7fe752010-09-02 09:58:18 +0000262 /// \param ElementType - the base element allocated type,
263 /// i.e. the allocated type after stripping all array types
264 virtual llvm::Value *InitializeArrayCookie(CodeGenFunction &CGF,
265 llvm::Value *NewPtr,
266 llvm::Value *NumElements,
John McCall6ec278d2011-01-27 09:37:56 +0000267 const CXXNewExpr *expr,
John McCall1e7fe752010-09-02 09:58:18 +0000268 QualType ElementType);
269
270 /// Reads the array cookie associated with the given pointer,
271 /// if it has one.
272 ///
273 /// \param Ptr - a pointer to the first element in the array
274 /// \param ElementType - the base element type of elements of the array
275 /// \param NumElements - an out parameter which will be initialized
276 /// with the number of elements allocated, or zero if there is no
277 /// cookie
278 /// \param AllocPtr - an out parameter which will be initialized
279 /// with a char* pointing to the address returned by the allocation
280 /// function
281 /// \param CookieSize - an out parameter which will be initialized
282 /// with the size of the cookie, or zero if there is no cookie
283 virtual void ReadArrayCookie(CodeGenFunction &CGF, llvm::Value *Ptr,
John McCall6ec278d2011-01-27 09:37:56 +0000284 const CXXDeleteExpr *expr,
John McCall1e7fe752010-09-02 09:58:18 +0000285 QualType ElementType, llvm::Value *&NumElements,
286 llvm::Value *&AllocPtr, CharUnits &CookieSize);
287
John McCalle2b45e22012-05-01 05:23:51 +0000288protected:
289 /// Returns the extra size required in order to store the array
290 /// cookie for the given type. Assumes that an array cookie is
291 /// required.
292 virtual CharUnits getArrayCookieSizeImpl(QualType elementType);
293
294 /// Reads the array cookie for an allocation which is known to have one.
295 /// This is called by the standard implementation of ReadArrayCookie.
296 ///
297 /// \param ptr - a pointer to the allocation made for an array, as a char*
298 /// \param cookieSize - the computed cookie size of an array
James Dennett3b2adf22012-06-15 07:35:42 +0000299 ///
John McCalle2b45e22012-05-01 05:23:51 +0000300 /// Other parameters are as above.
James Dennett3b2adf22012-06-15 07:35:42 +0000301 ///
John McCalle2b45e22012-05-01 05:23:51 +0000302 /// \return a size_t
303 virtual llvm::Value *readArrayCookieImpl(CodeGenFunction &IGF,
304 llvm::Value *ptr,
305 CharUnits cookieSize);
306
307public:
308
John McCall5cd91b52010-09-08 01:44:27 +0000309 /*************************** Static local guards ****************************/
310
John McCall3030eb82010-11-06 09:44:32 +0000311 /// Emits the guarded initializer and destructor setup for the given
312 /// variable, given that it couldn't be emitted as a constant.
Richard Smith7ca48502012-02-13 22:16:19 +0000313 /// If \p PerformInit is false, the initialization has been folded to a
314 /// constant and should not be performed.
John McCall3030eb82010-11-06 09:44:32 +0000315 ///
316 /// The variable may be:
317 /// - a static local variable
318 /// - a static data member of a class template instantiation
319 virtual void EmitGuardedInit(CodeGenFunction &CGF, const VarDecl &D,
Chandler Carruth0f30a122012-03-30 19:44:53 +0000320 llvm::GlobalVariable *DeclPtr, bool PerformInit);
John McCall5cd91b52010-09-08 01:44:27 +0000321
John McCall20bb1752012-05-01 06:13:13 +0000322 /// Emit code to force the execution of a destructor during global
323 /// teardown. The default implementation of this uses atexit.
324 ///
325 /// \param dtor - a function taking a single pointer argument
326 /// \param addr - a pointer to pass to the destructor function.
327 virtual void registerGlobalDtor(CodeGenFunction &CGF, llvm::Constant *dtor,
328 llvm::Constant *addr);
Charles Davis3a811f12010-05-25 19:52:27 +0000329};
330
John McCall96fcde02013-01-25 23:36:14 +0000331// Create an instance of a C++ ABI class:
332
333/// Creates an Itanium-family ABI.
Charles Davis071cc7d2010-08-16 03:33:14 +0000334CGCXXABI *CreateItaniumCXXABI(CodeGenModule &CGM);
John McCall96fcde02013-01-25 23:36:14 +0000335
336/// Creates a Microsoft-family ABI.
Charles Davis071cc7d2010-08-16 03:33:14 +0000337CGCXXABI *CreateMicrosoftCXXABI(CodeGenModule &CGM);
John McCall93d557b2010-08-22 00:05:51 +0000338
Charles Davis3a811f12010-05-25 19:52:27 +0000339}
340}
341
342#endif