blob: e86e639627b8243af76eba853919619a1ca41887 [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;
Peter Collingbourne14110472011-01-13 18:57:25 +000045 llvm::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) {
52 return CGF.CXXThisDecl;
53 }
54 llvm::Value *&getThisValue(CodeGenFunction &CGF) {
55 return CGF.CXXThisValue;
56 }
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
Charles Davis3a811f12010-05-25 19:52:27 +000074public:
John McCalld608cdb2010-08-22 10:59:02 +000075
Anders Carlsson1af610f2010-11-28 17:50:09 +000076 virtual ~CGCXXABI();
Charles Davis3a811f12010-05-25 19:52:27 +000077
78 /// Gets the mangle context.
Peter Collingbourne14110472011-01-13 18:57:25 +000079 MangleContext &getMangleContext() {
80 return *MangleCtx;
81 }
John McCall93d557b2010-08-22 00:05:51 +000082
John McCall0bab0cd2010-08-23 01:21:21 +000083 /// Find the LLVM type used to represent the given member pointer
84 /// type.
Chris Lattner9cbe4f02011-07-09 17:41:47 +000085 virtual llvm::Type *
John McCall0bab0cd2010-08-23 01:21:21 +000086 ConvertMemberPointerType(const MemberPointerType *MPT);
87
88 /// Load a member function from an object and a member function
89 /// pointer. Apply the this-adjustment and set 'This' to the
90 /// adjusted value.
John McCall93d557b2010-08-22 00:05:51 +000091 virtual llvm::Value *
92 EmitLoadOfMemberFunctionPointer(CodeGenFunction &CGF,
93 llvm::Value *&This,
94 llvm::Value *MemPtr,
95 const MemberPointerType *MPT);
John McCall3023def2010-08-22 03:04:22 +000096
John McCall6c2ab1d2010-08-31 21:07:20 +000097 /// Calculate an l-value from an object and a data member pointer.
98 virtual llvm::Value *EmitMemberDataPointerAddress(CodeGenFunction &CGF,
99 llvm::Value *Base,
100 llvm::Value *MemPtr,
101 const MemberPointerType *MPT);
102
John McCall0bab0cd2010-08-23 01:21:21 +0000103 /// Perform a derived-to-base or base-to-derived member pointer
104 /// conversion.
105 virtual llvm::Value *EmitMemberPointerConversion(CodeGenFunction &CGF,
106 const CastExpr *E,
107 llvm::Value *Src);
John McCallcf2c85e2010-08-22 04:16:24 +0000108
John McCall0bab0cd2010-08-23 01:21:21 +0000109 /// Perform a derived-to-base or base-to-derived member pointer
110 /// conversion on a constant member pointer.
111 virtual llvm::Constant *EmitMemberPointerConversion(llvm::Constant *C,
112 const CastExpr *E);
John McCallcf2c85e2010-08-22 04:16:24 +0000113
John McCall0bab0cd2010-08-23 01:21:21 +0000114 /// Return true if the given member pointer can be zero-initialized
115 /// (in the C++ sense) with an LLVM zeroinitializer.
John McCallf16aa102010-08-22 21:01:12 +0000116 virtual bool isZeroInitializable(const MemberPointerType *MPT);
John McCallcf2c85e2010-08-22 04:16:24 +0000117
John McCall0bab0cd2010-08-23 01:21:21 +0000118 /// Create a null member pointer of the given type.
119 virtual llvm::Constant *EmitNullMemberPointer(const MemberPointerType *MPT);
John McCallcf2c85e2010-08-22 04:16:24 +0000120
John McCall0bab0cd2010-08-23 01:21:21 +0000121 /// Create a member pointer for the given method.
John McCall755d8492011-04-12 00:42:48 +0000122 virtual llvm::Constant *EmitMemberPointer(const CXXMethodDecl *MD);
John McCall875ab102010-08-22 06:43:33 +0000123
John McCall0bab0cd2010-08-23 01:21:21 +0000124 /// Create a member pointer for the given field.
John McCall5808ce42011-02-03 08:15:49 +0000125 virtual llvm::Constant *EmitMemberDataPointer(const MemberPointerType *MPT,
126 CharUnits offset);
John McCalle9fd7eb2010-08-22 08:30:07 +0000127
Richard Smith2d6a5672012-01-14 04:30:29 +0000128 /// Create a member pointer for the given member pointer constant.
129 virtual llvm::Constant *EmitMemberPointer(const APValue &MP, QualType MPT);
130
John McCall0bab0cd2010-08-23 01:21:21 +0000131 /// Emit a comparison between two member pointers. Returns an i1.
John McCalle9fd7eb2010-08-22 08:30:07 +0000132 virtual llvm::Value *
John McCall0bab0cd2010-08-23 01:21:21 +0000133 EmitMemberPointerComparison(CodeGenFunction &CGF,
134 llvm::Value *L,
135 llvm::Value *R,
136 const MemberPointerType *MPT,
137 bool Inequality);
John McCalle9fd7eb2010-08-22 08:30:07 +0000138
John McCall0bab0cd2010-08-23 01:21:21 +0000139 /// Determine if a member pointer is non-null. Returns an i1.
John McCalle9fd7eb2010-08-22 08:30:07 +0000140 virtual llvm::Value *
John McCall0bab0cd2010-08-23 01:21:21 +0000141 EmitMemberPointerIsNotNull(CodeGenFunction &CGF,
142 llvm::Value *MemPtr,
143 const MemberPointerType *MPT);
John McCall4c40d982010-08-31 07:33:07 +0000144
145 /// Build the signature of the given constructor variant by adding
146 /// any required parameters. For convenience, ResTy has been
147 /// initialized to 'void', and ArgTys has been initialized with the
148 /// type of 'this' (although this may be changed by the ABI) and
149 /// will have the formal parameters added to it afterwards.
150 ///
151 /// If there are ever any ABIs where the implicit parameters are
152 /// intermixed with the formal parameters, we can address those
153 /// then.
154 virtual void BuildConstructorSignature(const CXXConstructorDecl *Ctor,
155 CXXCtorType T,
156 CanQualType &ResTy,
Chris Lattner686775d2011-07-20 06:58:45 +0000157 SmallVectorImpl<CanQualType> &ArgTys) = 0;
John McCall4c40d982010-08-31 07:33:07 +0000158
159 /// Build the signature of the given destructor variant by adding
160 /// any required parameters. For convenience, ResTy has been
161 /// initialized to 'void' and ArgTys has been initialized with the
162 /// type of 'this' (although this may be changed by the ABI).
163 virtual void BuildDestructorSignature(const CXXDestructorDecl *Dtor,
164 CXXDtorType T,
165 CanQualType &ResTy,
Chris Lattner686775d2011-07-20 06:58:45 +0000166 SmallVectorImpl<CanQualType> &ArgTys) = 0;
John McCall4c40d982010-08-31 07:33:07 +0000167
168 /// Build the ABI-specific portion of the parameter list for a
169 /// function. This generally involves a 'this' parameter and
170 /// possibly some extra data for constructors and destructors.
171 ///
172 /// ABIs may also choose to override the return type, which has been
173 /// initialized with the formal return type of the function.
174 virtual void BuildInstanceFunctionParams(CodeGenFunction &CGF,
175 QualType &ResTy,
176 FunctionArgList &Params) = 0;
177
178 /// Emit the ABI-specific prolog for the function.
179 virtual void EmitInstanceFunctionProlog(CodeGenFunction &CGF) = 0;
180
181 virtual void EmitReturnFromThunk(CodeGenFunction &CGF,
182 RValue RV, QualType ResultType);
John McCall1e7fe752010-09-02 09:58:18 +0000183
184 /**************************** Array cookies ******************************/
185
186 /// Returns the extra size required in order to store the array
187 /// cookie for the given type. May return 0 to indicate that no
188 /// array cookie is required.
189 ///
190 /// Several cases are filtered out before this method is called:
191 /// - non-array allocations never need a cookie
192 /// - calls to ::operator new(size_t, void*) never need a cookie
193 ///
194 /// \param ElementType - the allocated type of the expression,
195 /// i.e. the pointee type of the expression result type
John McCall6ec278d2011-01-27 09:37:56 +0000196 virtual CharUnits GetArrayCookieSize(const CXXNewExpr *expr);
John McCall1e7fe752010-09-02 09:58:18 +0000197
198 /// Initialize the array cookie for the given allocation.
199 ///
200 /// \param NewPtr - a char* which is the presumed-non-null
201 /// return value of the allocation function
202 /// \param NumElements - the computed number of elements,
203 /// potentially collapsed from the multidimensional array case
204 /// \param ElementType - the base element allocated type,
205 /// i.e. the allocated type after stripping all array types
206 virtual llvm::Value *InitializeArrayCookie(CodeGenFunction &CGF,
207 llvm::Value *NewPtr,
208 llvm::Value *NumElements,
John McCall6ec278d2011-01-27 09:37:56 +0000209 const CXXNewExpr *expr,
John McCall1e7fe752010-09-02 09:58:18 +0000210 QualType ElementType);
211
212 /// Reads the array cookie associated with the given pointer,
213 /// if it has one.
214 ///
215 /// \param Ptr - a pointer to the first element in the array
216 /// \param ElementType - the base element type of elements of the array
217 /// \param NumElements - an out parameter which will be initialized
218 /// with the number of elements allocated, or zero if there is no
219 /// cookie
220 /// \param AllocPtr - an out parameter which will be initialized
221 /// with a char* pointing to the address returned by the allocation
222 /// function
223 /// \param CookieSize - an out parameter which will be initialized
224 /// with the size of the cookie, or zero if there is no cookie
225 virtual void ReadArrayCookie(CodeGenFunction &CGF, llvm::Value *Ptr,
John McCall6ec278d2011-01-27 09:37:56 +0000226 const CXXDeleteExpr *expr,
John McCall1e7fe752010-09-02 09:58:18 +0000227 QualType ElementType, llvm::Value *&NumElements,
228 llvm::Value *&AllocPtr, CharUnits &CookieSize);
229
John McCall5cd91b52010-09-08 01:44:27 +0000230 /*************************** Static local guards ****************************/
231
John McCall3030eb82010-11-06 09:44:32 +0000232 /// Emits the guarded initializer and destructor setup for the given
233 /// variable, given that it couldn't be emitted as a constant.
234 ///
235 /// The variable may be:
236 /// - a static local variable
237 /// - a static data member of a class template instantiation
238 virtual void EmitGuardedInit(CodeGenFunction &CGF, const VarDecl &D,
239 llvm::GlobalVariable *DeclPtr);
John McCall5cd91b52010-09-08 01:44:27 +0000240
Charles Davis3a811f12010-05-25 19:52:27 +0000241};
242
243/// Creates an instance of a C++ ABI class.
John McCallee79a4c2010-08-21 22:46:04 +0000244CGCXXABI *CreateARMCXXABI(CodeGenModule &CGM);
Charles Davis071cc7d2010-08-16 03:33:14 +0000245CGCXXABI *CreateItaniumCXXABI(CodeGenModule &CGM);
246CGCXXABI *CreateMicrosoftCXXABI(CodeGenModule &CGM);
John McCall93d557b2010-08-22 00:05:51 +0000247
Charles Davis3a811f12010-05-25 19:52:27 +0000248}
249}
250
251#endif