blob: a3e49d80350349bd05fffc4a8965620151c0883b [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
57 ImplicitParamDecl *&getVTTDecl(CodeGenFunction &CGF) {
58 return CGF.CXXVTTDecl;
59 }
60 llvm::Value *&getVTTValue(CodeGenFunction &CGF) {
61 return CGF.CXXVTTValue;
62 }
63
64 /// Build a parameter variable suitable for 'this'.
65 void BuildThisParam(CodeGenFunction &CGF, FunctionArgList &Params);
66
67 /// Perform prolog initialization of the parameter variable suitable
68 /// for 'this' emitted by BuildThisParam.
69 void EmitThisParam(CodeGenFunction &CGF);
70
John McCall1e7fe752010-09-02 09:58:18 +000071 ASTContext &getContext() const { return CGM.getContext(); }
72
John McCalle2b45e22012-05-01 05:23:51 +000073 virtual bool requiresArrayCookie(const CXXDeleteExpr *E, QualType eltType);
74 virtual bool requiresArrayCookie(const CXXNewExpr *E);
75
Charles Davis3a811f12010-05-25 19:52:27 +000076public:
John McCalld608cdb2010-08-22 10:59:02 +000077
Anders Carlsson1af610f2010-11-28 17:50:09 +000078 virtual ~CGCXXABI();
Charles Davis3a811f12010-05-25 19:52:27 +000079
80 /// Gets the mangle context.
Peter Collingbourne14110472011-01-13 18:57:25 +000081 MangleContext &getMangleContext() {
82 return *MangleCtx;
83 }
John McCall93d557b2010-08-22 00:05:51 +000084
John McCall0bab0cd2010-08-23 01:21:21 +000085 /// Find the LLVM type used to represent the given member pointer
86 /// type.
Chris Lattner9cbe4f02011-07-09 17:41:47 +000087 virtual llvm::Type *
John McCall0bab0cd2010-08-23 01:21:21 +000088 ConvertMemberPointerType(const MemberPointerType *MPT);
89
90 /// Load a member function from an object and a member function
91 /// pointer. Apply the this-adjustment and set 'This' to the
92 /// adjusted value.
John McCall93d557b2010-08-22 00:05:51 +000093 virtual llvm::Value *
94 EmitLoadOfMemberFunctionPointer(CodeGenFunction &CGF,
95 llvm::Value *&This,
96 llvm::Value *MemPtr,
97 const MemberPointerType *MPT);
John McCall3023def2010-08-22 03:04:22 +000098
John McCall6c2ab1d2010-08-31 21:07:20 +000099 /// Calculate an l-value from an object and a data member pointer.
100 virtual llvm::Value *EmitMemberDataPointerAddress(CodeGenFunction &CGF,
101 llvm::Value *Base,
102 llvm::Value *MemPtr,
103 const MemberPointerType *MPT);
104
John McCall4d4e5c12012-02-15 01:22:51 +0000105 /// Perform a derived-to-base, base-to-derived, or bitcast member
106 /// pointer conversion.
John McCall0bab0cd2010-08-23 01:21:21 +0000107 virtual llvm::Value *EmitMemberPointerConversion(CodeGenFunction &CGF,
108 const CastExpr *E,
109 llvm::Value *Src);
John McCallcf2c85e2010-08-22 04:16:24 +0000110
John McCall4d4e5c12012-02-15 01:22:51 +0000111 /// Perform a derived-to-base, base-to-derived, or bitcast member
112 /// pointer conversion on a constant value.
113 virtual llvm::Constant *EmitMemberPointerConversion(const CastExpr *E,
114 llvm::Constant *Src);
115
John McCall0bab0cd2010-08-23 01:21:21 +0000116 /// Return true if the given member pointer can be zero-initialized
117 /// (in the C++ sense) with an LLVM zeroinitializer.
John McCallf16aa102010-08-22 21:01:12 +0000118 virtual bool isZeroInitializable(const MemberPointerType *MPT);
John McCallcf2c85e2010-08-22 04:16:24 +0000119
John McCall0bab0cd2010-08-23 01:21:21 +0000120 /// Create a null member pointer of the given type.
121 virtual llvm::Constant *EmitNullMemberPointer(const MemberPointerType *MPT);
John McCallcf2c85e2010-08-22 04:16:24 +0000122
John McCall0bab0cd2010-08-23 01:21:21 +0000123 /// Create a member pointer for the given method.
John McCall755d8492011-04-12 00:42:48 +0000124 virtual llvm::Constant *EmitMemberPointer(const CXXMethodDecl *MD);
John McCall875ab102010-08-22 06:43:33 +0000125
John McCall0bab0cd2010-08-23 01:21:21 +0000126 /// Create a member pointer for the given field.
John McCall5808ce42011-02-03 08:15:49 +0000127 virtual llvm::Constant *EmitMemberDataPointer(const MemberPointerType *MPT,
128 CharUnits offset);
John McCalle9fd7eb2010-08-22 08:30:07 +0000129
Richard Smith2d6a5672012-01-14 04:30:29 +0000130 /// Create a member pointer for the given member pointer constant.
131 virtual llvm::Constant *EmitMemberPointer(const APValue &MP, QualType MPT);
132
John McCall0bab0cd2010-08-23 01:21:21 +0000133 /// Emit a comparison between two member pointers. Returns an i1.
John McCalle9fd7eb2010-08-22 08:30:07 +0000134 virtual llvm::Value *
John McCall0bab0cd2010-08-23 01:21:21 +0000135 EmitMemberPointerComparison(CodeGenFunction &CGF,
136 llvm::Value *L,
137 llvm::Value *R,
138 const MemberPointerType *MPT,
139 bool Inequality);
John McCalle9fd7eb2010-08-22 08:30:07 +0000140
John McCall0bab0cd2010-08-23 01:21:21 +0000141 /// Determine if a member pointer is non-null. Returns an i1.
John McCalle9fd7eb2010-08-22 08:30:07 +0000142 virtual llvm::Value *
John McCall0bab0cd2010-08-23 01:21:21 +0000143 EmitMemberPointerIsNotNull(CodeGenFunction &CGF,
144 llvm::Value *MemPtr,
145 const MemberPointerType *MPT);
John McCall4c40d982010-08-31 07:33:07 +0000146
John McCall4d4e5c12012-02-15 01:22:51 +0000147protected:
148 /// A utility method for computing the offset required for the given
149 /// base-to-derived or derived-to-base member-pointer conversion.
150 /// Does not handle virtual conversions (in case we ever fully
151 /// support an ABI that allows this). Returns null if no adjustment
152 /// is required.
153 llvm::Constant *getMemberPointerAdjustment(const CastExpr *E);
154
155public:
John McCallecd03b42012-09-25 10:10:39 +0000156 /// Adjust the given non-null pointer to an object of polymorphic
157 /// type to point to the complete object.
158 ///
159 /// The IR type of the result should be a pointer but is otherwise
160 /// irrelevant.
161 virtual llvm::Value *adjustToCompleteObject(CodeGenFunction &CGF,
162 llvm::Value *ptr,
163 QualType type) = 0;
164
John McCall4c40d982010-08-31 07:33:07 +0000165 /// Build the signature of the given constructor variant by adding
166 /// any required parameters. For convenience, ResTy has been
167 /// initialized to 'void', and ArgTys has been initialized with the
168 /// type of 'this' (although this may be changed by the ABI) and
169 /// will have the formal parameters added to it afterwards.
170 ///
171 /// If there are ever any ABIs where the implicit parameters are
172 /// intermixed with the formal parameters, we can address those
173 /// then.
174 virtual void BuildConstructorSignature(const CXXConstructorDecl *Ctor,
175 CXXCtorType T,
176 CanQualType &ResTy,
Chris Lattner686775d2011-07-20 06:58:45 +0000177 SmallVectorImpl<CanQualType> &ArgTys) = 0;
John McCall4c40d982010-08-31 07:33:07 +0000178
179 /// Build the signature of the given destructor variant by adding
180 /// any required parameters. For convenience, ResTy has been
181 /// initialized to 'void' and ArgTys has been initialized with the
182 /// type of 'this' (although this may be changed by the ABI).
183 virtual void BuildDestructorSignature(const CXXDestructorDecl *Dtor,
184 CXXDtorType T,
185 CanQualType &ResTy,
Chris Lattner686775d2011-07-20 06:58:45 +0000186 SmallVectorImpl<CanQualType> &ArgTys) = 0;
John McCall4c40d982010-08-31 07:33:07 +0000187
188 /// Build the ABI-specific portion of the parameter list for a
189 /// function. This generally involves a 'this' parameter and
190 /// possibly some extra data for constructors and destructors.
191 ///
192 /// ABIs may also choose to override the return type, which has been
193 /// initialized with the formal return type of the function.
194 virtual void BuildInstanceFunctionParams(CodeGenFunction &CGF,
195 QualType &ResTy,
196 FunctionArgList &Params) = 0;
197
198 /// Emit the ABI-specific prolog for the function.
199 virtual void EmitInstanceFunctionProlog(CodeGenFunction &CGF) = 0;
200
201 virtual void EmitReturnFromThunk(CodeGenFunction &CGF,
202 RValue RV, QualType ResultType);
John McCall1e7fe752010-09-02 09:58:18 +0000203
Joao Matos285baac2012-07-17 17:10:11 +0000204 /// Gets the pure virtual member call function.
205 virtual StringRef GetPureVirtualCallName() = 0;
206
David Blaikie2eb9a952012-10-16 22:56:05 +0000207 /// Gets the deleted virtual member call name.
208 virtual StringRef GetDeletedVirtualCallName() = 0;
209
John McCall1e7fe752010-09-02 09:58:18 +0000210 /**************************** Array cookies ******************************/
211
212 /// Returns the extra size required in order to store the array
James Dennett16ae9de2012-06-22 10:16:05 +0000213 /// cookie for the given new-expression. May return 0 to indicate that no
John McCall1e7fe752010-09-02 09:58:18 +0000214 /// array cookie is required.
215 ///
216 /// Several cases are filtered out before this method is called:
217 /// - non-array allocations never need a cookie
James Dennett59001032012-06-20 00:57:15 +0000218 /// - calls to \::operator new(size_t, void*) never need a cookie
John McCall1e7fe752010-09-02 09:58:18 +0000219 ///
James Dennett16ae9de2012-06-22 10:16:05 +0000220 /// \param expr - the new-expression being allocated.
John McCall6ec278d2011-01-27 09:37:56 +0000221 virtual CharUnits GetArrayCookieSize(const CXXNewExpr *expr);
John McCall1e7fe752010-09-02 09:58:18 +0000222
223 /// Initialize the array cookie for the given allocation.
224 ///
225 /// \param NewPtr - a char* which is the presumed-non-null
226 /// return value of the allocation function
227 /// \param NumElements - the computed number of elements,
John McCalle2b45e22012-05-01 05:23:51 +0000228 /// potentially collapsed from the multidimensional array case;
229 /// always a size_t
John McCall1e7fe752010-09-02 09:58:18 +0000230 /// \param ElementType - the base element allocated type,
231 /// i.e. the allocated type after stripping all array types
232 virtual llvm::Value *InitializeArrayCookie(CodeGenFunction &CGF,
233 llvm::Value *NewPtr,
234 llvm::Value *NumElements,
John McCall6ec278d2011-01-27 09:37:56 +0000235 const CXXNewExpr *expr,
John McCall1e7fe752010-09-02 09:58:18 +0000236 QualType ElementType);
237
238 /// Reads the array cookie associated with the given pointer,
239 /// if it has one.
240 ///
241 /// \param Ptr - a pointer to the first element in the array
242 /// \param ElementType - the base element type of elements of the array
243 /// \param NumElements - an out parameter which will be initialized
244 /// with the number of elements allocated, or zero if there is no
245 /// cookie
246 /// \param AllocPtr - an out parameter which will be initialized
247 /// with a char* pointing to the address returned by the allocation
248 /// function
249 /// \param CookieSize - an out parameter which will be initialized
250 /// with the size of the cookie, or zero if there is no cookie
251 virtual void ReadArrayCookie(CodeGenFunction &CGF, llvm::Value *Ptr,
John McCall6ec278d2011-01-27 09:37:56 +0000252 const CXXDeleteExpr *expr,
John McCall1e7fe752010-09-02 09:58:18 +0000253 QualType ElementType, llvm::Value *&NumElements,
254 llvm::Value *&AllocPtr, CharUnits &CookieSize);
255
John McCalle2b45e22012-05-01 05:23:51 +0000256protected:
257 /// Returns the extra size required in order to store the array
258 /// cookie for the given type. Assumes that an array cookie is
259 /// required.
260 virtual CharUnits getArrayCookieSizeImpl(QualType elementType);
261
262 /// Reads the array cookie for an allocation which is known to have one.
263 /// This is called by the standard implementation of ReadArrayCookie.
264 ///
265 /// \param ptr - a pointer to the allocation made for an array, as a char*
266 /// \param cookieSize - the computed cookie size of an array
James Dennett3b2adf22012-06-15 07:35:42 +0000267 ///
John McCalle2b45e22012-05-01 05:23:51 +0000268 /// Other parameters are as above.
James Dennett3b2adf22012-06-15 07:35:42 +0000269 ///
John McCalle2b45e22012-05-01 05:23:51 +0000270 /// \return a size_t
271 virtual llvm::Value *readArrayCookieImpl(CodeGenFunction &IGF,
272 llvm::Value *ptr,
273 CharUnits cookieSize);
274
275public:
276
John McCall5cd91b52010-09-08 01:44:27 +0000277 /*************************** Static local guards ****************************/
278
John McCall3030eb82010-11-06 09:44:32 +0000279 /// Emits the guarded initializer and destructor setup for the given
280 /// variable, given that it couldn't be emitted as a constant.
Richard Smith7ca48502012-02-13 22:16:19 +0000281 /// If \p PerformInit is false, the initialization has been folded to a
282 /// constant and should not be performed.
John McCall3030eb82010-11-06 09:44:32 +0000283 ///
284 /// The variable may be:
285 /// - a static local variable
286 /// - a static data member of a class template instantiation
287 virtual void EmitGuardedInit(CodeGenFunction &CGF, const VarDecl &D,
Chandler Carruth0f30a122012-03-30 19:44:53 +0000288 llvm::GlobalVariable *DeclPtr, bool PerformInit);
John McCall5cd91b52010-09-08 01:44:27 +0000289
John McCall20bb1752012-05-01 06:13:13 +0000290 /// Emit code to force the execution of a destructor during global
291 /// teardown. The default implementation of this uses atexit.
292 ///
293 /// \param dtor - a function taking a single pointer argument
294 /// \param addr - a pointer to pass to the destructor function.
295 virtual void registerGlobalDtor(CodeGenFunction &CGF, llvm::Constant *dtor,
296 llvm::Constant *addr);
Charles Davis9ee494f2012-06-23 23:44:00 +0000297
298 /***************************** Virtual Tables *******************************/
299
300 /// Generates and emits the virtual tables for a class.
301 virtual void EmitVTables(const CXXRecordDecl *Class) = 0;
Charles Davis3a811f12010-05-25 19:52:27 +0000302};
303
304/// Creates an instance of a C++ ABI class.
John McCallee79a4c2010-08-21 22:46:04 +0000305CGCXXABI *CreateARMCXXABI(CodeGenModule &CGM);
Charles Davis071cc7d2010-08-16 03:33:14 +0000306CGCXXABI *CreateItaniumCXXABI(CodeGenModule &CGM);
307CGCXXABI *CreateMicrosoftCXXABI(CodeGenModule &CGM);
John McCall93d557b2010-08-22 00:05:51 +0000308
Charles Davis3a811f12010-05-25 19:52:27 +0000309}
310}
311
312#endif