blob: 1621016baa46880a6ba84ed2ccf98cc9539772d8 [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
Chris Lattnerd47d3b02011-07-23 10:35:09 +000018#include "clang/Basic/LLVM.h"
19
John McCall4c40d982010-08-31 07:33:07 +000020#include "CodeGenFunction.h"
21
John McCall93d557b2010-08-22 00:05:51 +000022namespace llvm {
John McCallcf2c85e2010-08-22 04:16:24 +000023 class Constant;
John McCall0bab0cd2010-08-23 01:21:21 +000024 class Type;
John McCall93d557b2010-08-22 00:05:51 +000025 class Value;
26}
27
Charles Davis3a811f12010-05-25 19:52:27 +000028namespace clang {
John McCall3023def2010-08-22 03:04:22 +000029 class CastExpr;
John McCall4c40d982010-08-31 07:33:07 +000030 class CXXConstructorDecl;
31 class CXXDestructorDecl;
John McCall875ab102010-08-22 06:43:33 +000032 class CXXMethodDecl;
John McCallcf2c85e2010-08-22 04:16:24 +000033 class CXXRecordDecl;
John McCall0bab0cd2010-08-23 01:21:21 +000034 class FieldDecl;
Peter Collingbourne14110472011-01-13 18:57:25 +000035 class MangleContext;
John McCall93d557b2010-08-22 00:05:51 +000036
Charles Davis3a811f12010-05-25 19:52:27 +000037namespace CodeGen {
John McCall93d557b2010-08-22 00:05:51 +000038 class CodeGenFunction;
Charles Davis3a811f12010-05-25 19:52:27 +000039 class CodeGenModule;
Charles Davis3a811f12010-05-25 19:52:27 +000040
41/// Implements C++ ABI-specific code generation functions.
Charles Davis071cc7d2010-08-16 03:33:14 +000042class CGCXXABI {
John McCalld608cdb2010-08-22 10:59:02 +000043protected:
44 CodeGenModule &CGM;
Dylan Noblesmith6f42b622012-02-05 02:12:40 +000045 OwningPtr<MangleContext> MangleCtx;
John McCalld608cdb2010-08-22 10:59:02 +000046
Peter Collingbourne14110472011-01-13 18:57:25 +000047 CGCXXABI(CodeGenModule &CGM)
48 : CGM(CGM), MangleCtx(CGM.getContext().createMangleContext()) {}
John McCalld608cdb2010-08-22 10:59:02 +000049
John McCall4c40d982010-08-31 07:33:07 +000050protected:
51 ImplicitParamDecl *&getThisDecl(CodeGenFunction &CGF) {
Eli Friedmancec5ebd2012-02-11 02:57:39 +000052 return CGF.CXXABIThisDecl;
John McCall4c40d982010-08-31 07:33:07 +000053 }
54 llvm::Value *&getThisValue(CodeGenFunction &CGF) {
Eli Friedmancec5ebd2012-02-11 02:57:39 +000055 return CGF.CXXABIThisValue;
John McCall4c40d982010-08-31 07:33:07 +000056 }
57
58 ImplicitParamDecl *&getVTTDecl(CodeGenFunction &CGF) {
59 return CGF.CXXVTTDecl;
60 }
61 llvm::Value *&getVTTValue(CodeGenFunction &CGF) {
62 return CGF.CXXVTTValue;
63 }
64
65 /// Build a parameter variable suitable for 'this'.
66 void BuildThisParam(CodeGenFunction &CGF, FunctionArgList &Params);
67
68 /// Perform prolog initialization of the parameter variable suitable
69 /// for 'this' emitted by BuildThisParam.
70 void EmitThisParam(CodeGenFunction &CGF);
71
John McCall1e7fe752010-09-02 09:58:18 +000072 ASTContext &getContext() const { return CGM.getContext(); }
73
John McCalle2b45e22012-05-01 05:23:51 +000074 virtual bool requiresArrayCookie(const CXXDeleteExpr *E, QualType eltType);
75 virtual bool requiresArrayCookie(const CXXNewExpr *E);
76
Charles Davis3a811f12010-05-25 19:52:27 +000077public:
John McCalld608cdb2010-08-22 10:59:02 +000078
Anders Carlsson1af610f2010-11-28 17:50:09 +000079 virtual ~CGCXXABI();
Charles Davis3a811f12010-05-25 19:52:27 +000080
81 /// Gets the mangle context.
Peter Collingbourne14110472011-01-13 18:57:25 +000082 MangleContext &getMangleContext() {
83 return *MangleCtx;
84 }
John McCall93d557b2010-08-22 00:05:51 +000085
John McCall0bab0cd2010-08-23 01:21:21 +000086 /// Find the LLVM type used to represent the given member pointer
87 /// type.
Chris Lattner9cbe4f02011-07-09 17:41:47 +000088 virtual llvm::Type *
John McCall0bab0cd2010-08-23 01:21:21 +000089 ConvertMemberPointerType(const MemberPointerType *MPT);
90
91 /// Load a member function from an object and a member function
92 /// pointer. Apply the this-adjustment and set 'This' to the
93 /// adjusted value.
John McCall93d557b2010-08-22 00:05:51 +000094 virtual llvm::Value *
95 EmitLoadOfMemberFunctionPointer(CodeGenFunction &CGF,
96 llvm::Value *&This,
97 llvm::Value *MemPtr,
98 const MemberPointerType *MPT);
John McCall3023def2010-08-22 03:04:22 +000099
John McCall6c2ab1d2010-08-31 21:07:20 +0000100 /// Calculate an l-value from an object and a data member pointer.
101 virtual llvm::Value *EmitMemberDataPointerAddress(CodeGenFunction &CGF,
102 llvm::Value *Base,
103 llvm::Value *MemPtr,
104 const MemberPointerType *MPT);
105
John McCall4d4e5c12012-02-15 01:22:51 +0000106 /// Perform a derived-to-base, base-to-derived, or bitcast member
107 /// pointer conversion.
John McCall0bab0cd2010-08-23 01:21:21 +0000108 virtual llvm::Value *EmitMemberPointerConversion(CodeGenFunction &CGF,
109 const CastExpr *E,
110 llvm::Value *Src);
John McCallcf2c85e2010-08-22 04:16:24 +0000111
John McCall4d4e5c12012-02-15 01:22:51 +0000112 /// Perform a derived-to-base, base-to-derived, or bitcast member
113 /// pointer conversion on a constant value.
114 virtual llvm::Constant *EmitMemberPointerConversion(const CastExpr *E,
115 llvm::Constant *Src);
116
John McCall0bab0cd2010-08-23 01:21:21 +0000117 /// Return true if the given member pointer can be zero-initialized
118 /// (in the C++ sense) with an LLVM zeroinitializer.
John McCallf16aa102010-08-22 21:01:12 +0000119 virtual bool isZeroInitializable(const MemberPointerType *MPT);
John McCallcf2c85e2010-08-22 04:16:24 +0000120
John McCall0bab0cd2010-08-23 01:21:21 +0000121 /// Create a null member pointer of the given type.
122 virtual llvm::Constant *EmitNullMemberPointer(const MemberPointerType *MPT);
John McCallcf2c85e2010-08-22 04:16:24 +0000123
John McCall0bab0cd2010-08-23 01:21:21 +0000124 /// Create a member pointer for the given method.
John McCall755d8492011-04-12 00:42:48 +0000125 virtual llvm::Constant *EmitMemberPointer(const CXXMethodDecl *MD);
John McCall875ab102010-08-22 06:43:33 +0000126
John McCall0bab0cd2010-08-23 01:21:21 +0000127 /// Create a member pointer for the given field.
John McCall5808ce42011-02-03 08:15:49 +0000128 virtual llvm::Constant *EmitMemberDataPointer(const MemberPointerType *MPT,
129 CharUnits offset);
John McCalle9fd7eb2010-08-22 08:30:07 +0000130
Richard Smith2d6a5672012-01-14 04:30:29 +0000131 /// Create a member pointer for the given member pointer constant.
132 virtual llvm::Constant *EmitMemberPointer(const APValue &MP, QualType MPT);
133
John McCall0bab0cd2010-08-23 01:21:21 +0000134 /// Emit a comparison between two member pointers. Returns an i1.
John McCalle9fd7eb2010-08-22 08:30:07 +0000135 virtual llvm::Value *
John McCall0bab0cd2010-08-23 01:21:21 +0000136 EmitMemberPointerComparison(CodeGenFunction &CGF,
137 llvm::Value *L,
138 llvm::Value *R,
139 const MemberPointerType *MPT,
140 bool Inequality);
John McCalle9fd7eb2010-08-22 08:30:07 +0000141
John McCall0bab0cd2010-08-23 01:21:21 +0000142 /// Determine if a member pointer is non-null. Returns an i1.
John McCalle9fd7eb2010-08-22 08:30:07 +0000143 virtual llvm::Value *
John McCall0bab0cd2010-08-23 01:21:21 +0000144 EmitMemberPointerIsNotNull(CodeGenFunction &CGF,
145 llvm::Value *MemPtr,
146 const MemberPointerType *MPT);
John McCall4c40d982010-08-31 07:33:07 +0000147
John McCall4d4e5c12012-02-15 01:22:51 +0000148protected:
149 /// A utility method for computing the offset required for the given
150 /// base-to-derived or derived-to-base member-pointer conversion.
151 /// Does not handle virtual conversions (in case we ever fully
152 /// support an ABI that allows this). Returns null if no adjustment
153 /// is required.
154 llvm::Constant *getMemberPointerAdjustment(const CastExpr *E);
155
156public:
John McCall4c40d982010-08-31 07:33:07 +0000157 /// Build the signature of the given constructor variant by adding
158 /// any required parameters. For convenience, ResTy has been
159 /// initialized to 'void', and ArgTys has been initialized with the
160 /// type of 'this' (although this may be changed by the ABI) and
161 /// will have the formal parameters added to it afterwards.
162 ///
163 /// If there are ever any ABIs where the implicit parameters are
164 /// intermixed with the formal parameters, we can address those
165 /// then.
166 virtual void BuildConstructorSignature(const CXXConstructorDecl *Ctor,
167 CXXCtorType T,
168 CanQualType &ResTy,
Chris Lattner686775d2011-07-20 06:58:45 +0000169 SmallVectorImpl<CanQualType> &ArgTys) = 0;
John McCall4c40d982010-08-31 07:33:07 +0000170
171 /// Build the signature of the given destructor variant by adding
172 /// any required parameters. For convenience, ResTy has been
173 /// initialized to 'void' and ArgTys has been initialized with the
174 /// type of 'this' (although this may be changed by the ABI).
175 virtual void BuildDestructorSignature(const CXXDestructorDecl *Dtor,
176 CXXDtorType T,
177 CanQualType &ResTy,
Chris Lattner686775d2011-07-20 06:58:45 +0000178 SmallVectorImpl<CanQualType> &ArgTys) = 0;
John McCall4c40d982010-08-31 07:33:07 +0000179
180 /// Build the ABI-specific portion of the parameter list for a
181 /// function. This generally involves a 'this' parameter and
182 /// possibly some extra data for constructors and destructors.
183 ///
184 /// ABIs may also choose to override the return type, which has been
185 /// initialized with the formal return type of the function.
186 virtual void BuildInstanceFunctionParams(CodeGenFunction &CGF,
187 QualType &ResTy,
188 FunctionArgList &Params) = 0;
189
190 /// Emit the ABI-specific prolog for the function.
191 virtual void EmitInstanceFunctionProlog(CodeGenFunction &CGF) = 0;
192
193 virtual void EmitReturnFromThunk(CodeGenFunction &CGF,
194 RValue RV, QualType ResultType);
John McCall1e7fe752010-09-02 09:58:18 +0000195
196 /**************************** Array cookies ******************************/
197
198 /// Returns the extra size required in order to store the array
199 /// cookie for the given type. May return 0 to indicate that no
200 /// array cookie is required.
201 ///
202 /// Several cases are filtered out before this method is called:
203 /// - non-array allocations never need a cookie
204 /// - calls to ::operator new(size_t, void*) never need a cookie
205 ///
206 /// \param ElementType - the allocated type of the expression,
207 /// i.e. the pointee type of the expression result type
John McCall6ec278d2011-01-27 09:37:56 +0000208 virtual CharUnits GetArrayCookieSize(const CXXNewExpr *expr);
John McCall1e7fe752010-09-02 09:58:18 +0000209
210 /// Initialize the array cookie for the given allocation.
211 ///
212 /// \param NewPtr - a char* which is the presumed-non-null
213 /// return value of the allocation function
214 /// \param NumElements - the computed number of elements,
John McCalle2b45e22012-05-01 05:23:51 +0000215 /// potentially collapsed from the multidimensional array case;
216 /// always a size_t
John McCall1e7fe752010-09-02 09:58:18 +0000217 /// \param ElementType - the base element allocated type,
218 /// i.e. the allocated type after stripping all array types
219 virtual llvm::Value *InitializeArrayCookie(CodeGenFunction &CGF,
220 llvm::Value *NewPtr,
221 llvm::Value *NumElements,
John McCall6ec278d2011-01-27 09:37:56 +0000222 const CXXNewExpr *expr,
John McCall1e7fe752010-09-02 09:58:18 +0000223 QualType ElementType);
224
225 /// Reads the array cookie associated with the given pointer,
226 /// if it has one.
227 ///
228 /// \param Ptr - a pointer to the first element in the array
229 /// \param ElementType - the base element type of elements of the array
230 /// \param NumElements - an out parameter which will be initialized
231 /// with the number of elements allocated, or zero if there is no
232 /// cookie
233 /// \param AllocPtr - an out parameter which will be initialized
234 /// with a char* pointing to the address returned by the allocation
235 /// function
236 /// \param CookieSize - an out parameter which will be initialized
237 /// with the size of the cookie, or zero if there is no cookie
238 virtual void ReadArrayCookie(CodeGenFunction &CGF, llvm::Value *Ptr,
John McCall6ec278d2011-01-27 09:37:56 +0000239 const CXXDeleteExpr *expr,
John McCall1e7fe752010-09-02 09:58:18 +0000240 QualType ElementType, llvm::Value *&NumElements,
241 llvm::Value *&AllocPtr, CharUnits &CookieSize);
242
John McCalle2b45e22012-05-01 05:23:51 +0000243protected:
244 /// Returns the extra size required in order to store the array
245 /// cookie for the given type. Assumes that an array cookie is
246 /// required.
247 virtual CharUnits getArrayCookieSizeImpl(QualType elementType);
248
249 /// Reads the array cookie for an allocation which is known to have one.
250 /// This is called by the standard implementation of ReadArrayCookie.
251 ///
252 /// \param ptr - a pointer to the allocation made for an array, as a char*
253 /// \param cookieSize - the computed cookie size of an array
254 /// Other parameters are as above.
255 /// \return a size_t
256 virtual llvm::Value *readArrayCookieImpl(CodeGenFunction &IGF,
257 llvm::Value *ptr,
258 CharUnits cookieSize);
259
260public:
261
John McCall5cd91b52010-09-08 01:44:27 +0000262 /*************************** Static local guards ****************************/
263
John McCall3030eb82010-11-06 09:44:32 +0000264 /// Emits the guarded initializer and destructor setup for the given
265 /// variable, given that it couldn't be emitted as a constant.
Richard Smith7ca48502012-02-13 22:16:19 +0000266 /// If \p PerformInit is false, the initialization has been folded to a
267 /// constant and should not be performed.
John McCall3030eb82010-11-06 09:44:32 +0000268 ///
269 /// The variable may be:
270 /// - a static local variable
271 /// - a static data member of a class template instantiation
272 virtual void EmitGuardedInit(CodeGenFunction &CGF, const VarDecl &D,
Chandler Carruth0f30a122012-03-30 19:44:53 +0000273 llvm::GlobalVariable *DeclPtr, bool PerformInit);
John McCall5cd91b52010-09-08 01:44:27 +0000274
Charles Davis3a811f12010-05-25 19:52:27 +0000275};
276
277/// Creates an instance of a C++ ABI class.
John McCallee79a4c2010-08-21 22:46:04 +0000278CGCXXABI *CreateARMCXXABI(CodeGenModule &CGM);
Charles Davis071cc7d2010-08-16 03:33:14 +0000279CGCXXABI *CreateItaniumCXXABI(CodeGenModule &CGM);
280CGCXXABI *CreateMicrosoftCXXABI(CodeGenModule &CGM);
John McCall93d557b2010-08-22 00:05:51 +0000281
Charles Davis3a811f12010-05-25 19:52:27 +0000282}
283}
284
285#endif