blob: ffe70be2ca5424db3e3e3429e979b40ad9969651 [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
James Dennett3b2adf22012-06-15 07:35:42 +000041/// \brief 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 McCallecd03b42012-09-25 10:10:39 +0000157 /// Adjust the given non-null pointer to an object of polymorphic
158 /// type to point to the complete object.
159 ///
160 /// The IR type of the result should be a pointer but is otherwise
161 /// irrelevant.
162 virtual llvm::Value *adjustToCompleteObject(CodeGenFunction &CGF,
163 llvm::Value *ptr,
164 QualType type) = 0;
165
John McCall4c40d982010-08-31 07:33:07 +0000166 /// Build the signature of the given constructor variant by adding
167 /// any required parameters. For convenience, ResTy has been
168 /// initialized to 'void', and ArgTys has been initialized with the
169 /// type of 'this' (although this may be changed by the ABI) and
170 /// will have the formal parameters added to it afterwards.
171 ///
172 /// If there are ever any ABIs where the implicit parameters are
173 /// intermixed with the formal parameters, we can address those
174 /// then.
175 virtual void BuildConstructorSignature(const CXXConstructorDecl *Ctor,
176 CXXCtorType 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 signature of the given destructor variant by adding
181 /// any required parameters. For convenience, ResTy has been
182 /// initialized to 'void' and ArgTys has been initialized with the
183 /// type of 'this' (although this may be changed by the ABI).
184 virtual void BuildDestructorSignature(const CXXDestructorDecl *Dtor,
185 CXXDtorType T,
186 CanQualType &ResTy,
Chris Lattner686775d2011-07-20 06:58:45 +0000187 SmallVectorImpl<CanQualType> &ArgTys) = 0;
John McCall4c40d982010-08-31 07:33:07 +0000188
189 /// Build the ABI-specific portion of the parameter list for a
190 /// function. This generally involves a 'this' parameter and
191 /// possibly some extra data for constructors and destructors.
192 ///
193 /// ABIs may also choose to override the return type, which has been
194 /// initialized with the formal return type of the function.
195 virtual void BuildInstanceFunctionParams(CodeGenFunction &CGF,
196 QualType &ResTy,
197 FunctionArgList &Params) = 0;
198
199 /// Emit the ABI-specific prolog for the function.
200 virtual void EmitInstanceFunctionProlog(CodeGenFunction &CGF) = 0;
201
202 virtual void EmitReturnFromThunk(CodeGenFunction &CGF,
203 RValue RV, QualType ResultType);
John McCall1e7fe752010-09-02 09:58:18 +0000204
Joao Matos285baac2012-07-17 17:10:11 +0000205 /// Gets the pure virtual member call function.
206 virtual StringRef GetPureVirtualCallName() = 0;
207
John McCall1e7fe752010-09-02 09:58:18 +0000208 /**************************** Array cookies ******************************/
209
210 /// Returns the extra size required in order to store the array
James Dennett16ae9de2012-06-22 10:16:05 +0000211 /// cookie for the given new-expression. May return 0 to indicate that no
John McCall1e7fe752010-09-02 09:58:18 +0000212 /// array cookie is required.
213 ///
214 /// Several cases are filtered out before this method is called:
215 /// - non-array allocations never need a cookie
James Dennett59001032012-06-20 00:57:15 +0000216 /// - calls to \::operator new(size_t, void*) never need a cookie
John McCall1e7fe752010-09-02 09:58:18 +0000217 ///
James Dennett16ae9de2012-06-22 10:16:05 +0000218 /// \param expr - the new-expression being allocated.
John McCall6ec278d2011-01-27 09:37:56 +0000219 virtual CharUnits GetArrayCookieSize(const CXXNewExpr *expr);
John McCall1e7fe752010-09-02 09:58:18 +0000220
221 /// Initialize the array cookie for the given allocation.
222 ///
223 /// \param NewPtr - a char* which is the presumed-non-null
224 /// return value of the allocation function
225 /// \param NumElements - the computed number of elements,
John McCalle2b45e22012-05-01 05:23:51 +0000226 /// potentially collapsed from the multidimensional array case;
227 /// always a size_t
John McCall1e7fe752010-09-02 09:58:18 +0000228 /// \param ElementType - the base element allocated type,
229 /// i.e. the allocated type after stripping all array types
230 virtual llvm::Value *InitializeArrayCookie(CodeGenFunction &CGF,
231 llvm::Value *NewPtr,
232 llvm::Value *NumElements,
John McCall6ec278d2011-01-27 09:37:56 +0000233 const CXXNewExpr *expr,
John McCall1e7fe752010-09-02 09:58:18 +0000234 QualType ElementType);
235
236 /// Reads the array cookie associated with the given pointer,
237 /// if it has one.
238 ///
239 /// \param Ptr - a pointer to the first element in the array
240 /// \param ElementType - the base element type of elements of the array
241 /// \param NumElements - an out parameter which will be initialized
242 /// with the number of elements allocated, or zero if there is no
243 /// cookie
244 /// \param AllocPtr - an out parameter which will be initialized
245 /// with a char* pointing to the address returned by the allocation
246 /// function
247 /// \param CookieSize - an out parameter which will be initialized
248 /// with the size of the cookie, or zero if there is no cookie
249 virtual void ReadArrayCookie(CodeGenFunction &CGF, llvm::Value *Ptr,
John McCall6ec278d2011-01-27 09:37:56 +0000250 const CXXDeleteExpr *expr,
John McCall1e7fe752010-09-02 09:58:18 +0000251 QualType ElementType, llvm::Value *&NumElements,
252 llvm::Value *&AllocPtr, CharUnits &CookieSize);
253
John McCalle2b45e22012-05-01 05:23:51 +0000254protected:
255 /// Returns the extra size required in order to store the array
256 /// cookie for the given type. Assumes that an array cookie is
257 /// required.
258 virtual CharUnits getArrayCookieSizeImpl(QualType elementType);
259
260 /// Reads the array cookie for an allocation which is known to have one.
261 /// This is called by the standard implementation of ReadArrayCookie.
262 ///
263 /// \param ptr - a pointer to the allocation made for an array, as a char*
264 /// \param cookieSize - the computed cookie size of an array
James Dennett3b2adf22012-06-15 07:35:42 +0000265 ///
John McCalle2b45e22012-05-01 05:23:51 +0000266 /// Other parameters are as above.
James Dennett3b2adf22012-06-15 07:35:42 +0000267 ///
John McCalle2b45e22012-05-01 05:23:51 +0000268 /// \return a size_t
269 virtual llvm::Value *readArrayCookieImpl(CodeGenFunction &IGF,
270 llvm::Value *ptr,
271 CharUnits cookieSize);
272
273public:
274
John McCall5cd91b52010-09-08 01:44:27 +0000275 /*************************** Static local guards ****************************/
276
John McCall3030eb82010-11-06 09:44:32 +0000277 /// Emits the guarded initializer and destructor setup for the given
278 /// variable, given that it couldn't be emitted as a constant.
Richard Smith7ca48502012-02-13 22:16:19 +0000279 /// If \p PerformInit is false, the initialization has been folded to a
280 /// constant and should not be performed.
John McCall3030eb82010-11-06 09:44:32 +0000281 ///
282 /// The variable may be:
283 /// - a static local variable
284 /// - a static data member of a class template instantiation
285 virtual void EmitGuardedInit(CodeGenFunction &CGF, const VarDecl &D,
Chandler Carruth0f30a122012-03-30 19:44:53 +0000286 llvm::GlobalVariable *DeclPtr, bool PerformInit);
John McCall5cd91b52010-09-08 01:44:27 +0000287
John McCall20bb1752012-05-01 06:13:13 +0000288 /// Emit code to force the execution of a destructor during global
289 /// teardown. The default implementation of this uses atexit.
290 ///
291 /// \param dtor - a function taking a single pointer argument
292 /// \param addr - a pointer to pass to the destructor function.
293 virtual void registerGlobalDtor(CodeGenFunction &CGF, llvm::Constant *dtor,
294 llvm::Constant *addr);
Charles Davis9ee494f2012-06-23 23:44:00 +0000295
296 /***************************** Virtual Tables *******************************/
297
298 /// Generates and emits the virtual tables for a class.
299 virtual void EmitVTables(const CXXRecordDecl *Class) = 0;
Charles Davis3a811f12010-05-25 19:52:27 +0000300};
301
302/// Creates an instance of a C++ ABI class.
John McCallee79a4c2010-08-21 22:46:04 +0000303CGCXXABI *CreateARMCXXABI(CodeGenModule &CGM);
Charles Davis071cc7d2010-08-16 03:33:14 +0000304CGCXXABI *CreateItaniumCXXABI(CodeGenModule &CGM);
305CGCXXABI *CreateMicrosoftCXXABI(CodeGenModule &CGM);
John McCall93d557b2010-08-22 00:05:51 +0000306
Charles Davis3a811f12010-05-25 19:52:27 +0000307}
308}
309
310#endif