blob: b99216b786b3170bee0bb2b6fc05d997bf9538be [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"
19
John McCall93d557b2010-08-22 00:05:51 +000020namespace llvm {
John McCallcf2c85e2010-08-22 04:16:24 +000021 class Constant;
John McCall0bab0cd2010-08-23 01:21:21 +000022 class Type;
John McCall93d557b2010-08-22 00:05:51 +000023 class Value;
John McCall4c40d982010-08-31 07:33:07 +000024
25 template <class T> class SmallVectorImpl;
John McCall93d557b2010-08-22 00:05:51 +000026}
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;
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;
39 class MangleContext;
40
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;
45
46 CGCXXABI(CodeGenModule &CGM) : CGM(CGM) {}
47
John McCall4c40d982010-08-31 07:33:07 +000048protected:
49 ImplicitParamDecl *&getThisDecl(CodeGenFunction &CGF) {
50 return CGF.CXXThisDecl;
51 }
52 llvm::Value *&getThisValue(CodeGenFunction &CGF) {
53 return CGF.CXXThisValue;
54 }
55
56 ImplicitParamDecl *&getVTTDecl(CodeGenFunction &CGF) {
57 return CGF.CXXVTTDecl;
58 }
59 llvm::Value *&getVTTValue(CodeGenFunction &CGF) {
60 return CGF.CXXVTTValue;
61 }
62
63 /// Build a parameter variable suitable for 'this'.
64 void BuildThisParam(CodeGenFunction &CGF, FunctionArgList &Params);
65
66 /// Perform prolog initialization of the parameter variable suitable
67 /// for 'this' emitted by BuildThisParam.
68 void EmitThisParam(CodeGenFunction &CGF);
69
John McCall1e7fe752010-09-02 09:58:18 +000070 ASTContext &getContext() const { return CGM.getContext(); }
71
John McCall5cd91b52010-09-08 01:44:27 +000072 virtual void _anchor();
73
Charles Davis3a811f12010-05-25 19:52:27 +000074public:
John McCalld608cdb2010-08-22 10:59:02 +000075
John McCall5cd91b52010-09-08 01:44:27 +000076 virtual ~CGCXXABI() {}
Charles Davis3a811f12010-05-25 19:52:27 +000077
78 /// Gets the mangle context.
79 virtual MangleContext &getMangleContext() = 0;
John McCall93d557b2010-08-22 00:05:51 +000080
John McCall0bab0cd2010-08-23 01:21:21 +000081 /// Find the LLVM type used to represent the given member pointer
82 /// type.
83 virtual const llvm::Type *
84 ConvertMemberPointerType(const MemberPointerType *MPT);
85
86 /// Load a member function from an object and a member function
87 /// pointer. Apply the this-adjustment and set 'This' to the
88 /// adjusted value.
John McCall93d557b2010-08-22 00:05:51 +000089 virtual llvm::Value *
90 EmitLoadOfMemberFunctionPointer(CodeGenFunction &CGF,
91 llvm::Value *&This,
92 llvm::Value *MemPtr,
93 const MemberPointerType *MPT);
John McCall3023def2010-08-22 03:04:22 +000094
John McCall6c2ab1d2010-08-31 21:07:20 +000095 /// Calculate an l-value from an object and a data member pointer.
96 virtual llvm::Value *EmitMemberDataPointerAddress(CodeGenFunction &CGF,
97 llvm::Value *Base,
98 llvm::Value *MemPtr,
99 const MemberPointerType *MPT);
100
John McCall0bab0cd2010-08-23 01:21:21 +0000101 /// Perform a derived-to-base or base-to-derived member pointer
102 /// conversion.
103 virtual llvm::Value *EmitMemberPointerConversion(CodeGenFunction &CGF,
104 const CastExpr *E,
105 llvm::Value *Src);
John McCallcf2c85e2010-08-22 04:16:24 +0000106
John McCall0bab0cd2010-08-23 01:21:21 +0000107 /// Perform a derived-to-base or base-to-derived member pointer
108 /// conversion on a constant member pointer.
109 virtual llvm::Constant *EmitMemberPointerConversion(llvm::Constant *C,
110 const CastExpr *E);
John McCallcf2c85e2010-08-22 04:16:24 +0000111
John McCall0bab0cd2010-08-23 01:21:21 +0000112 /// Return true if the given member pointer can be zero-initialized
113 /// (in the C++ sense) with an LLVM zeroinitializer.
John McCallf16aa102010-08-22 21:01:12 +0000114 virtual bool isZeroInitializable(const MemberPointerType *MPT);
John McCallcf2c85e2010-08-22 04:16:24 +0000115
John McCall0bab0cd2010-08-23 01:21:21 +0000116 /// Create a null member pointer of the given type.
117 virtual llvm::Constant *EmitNullMemberPointer(const MemberPointerType *MPT);
John McCallcf2c85e2010-08-22 04:16:24 +0000118
John McCall0bab0cd2010-08-23 01:21:21 +0000119 /// Create a member pointer for the given method.
120 virtual llvm::Constant *EmitMemberPointer(const CXXMethodDecl *MD);
John McCall875ab102010-08-22 06:43:33 +0000121
John McCall0bab0cd2010-08-23 01:21:21 +0000122 /// Create a member pointer for the given field.
123 virtual llvm::Constant *EmitMemberPointer(const FieldDecl *FD);
John McCalle9fd7eb2010-08-22 08:30:07 +0000124
John McCall0bab0cd2010-08-23 01:21:21 +0000125 /// Emit a comparison between two member pointers. Returns an i1.
John McCalle9fd7eb2010-08-22 08:30:07 +0000126 virtual llvm::Value *
John McCall0bab0cd2010-08-23 01:21:21 +0000127 EmitMemberPointerComparison(CodeGenFunction &CGF,
128 llvm::Value *L,
129 llvm::Value *R,
130 const MemberPointerType *MPT,
131 bool Inequality);
John McCalle9fd7eb2010-08-22 08:30:07 +0000132
John McCall0bab0cd2010-08-23 01:21:21 +0000133 /// Determine if a member pointer is non-null. Returns an i1.
John McCalle9fd7eb2010-08-22 08:30:07 +0000134 virtual llvm::Value *
John McCall0bab0cd2010-08-23 01:21:21 +0000135 EmitMemberPointerIsNotNull(CodeGenFunction &CGF,
136 llvm::Value *MemPtr,
137 const MemberPointerType *MPT);
John McCall4c40d982010-08-31 07:33:07 +0000138
139 /// Build the signature of the given constructor variant by adding
140 /// any required parameters. For convenience, ResTy has been
141 /// initialized to 'void', and ArgTys has been initialized with the
142 /// type of 'this' (although this may be changed by the ABI) and
143 /// will have the formal parameters added to it afterwards.
144 ///
145 /// If there are ever any ABIs where the implicit parameters are
146 /// intermixed with the formal parameters, we can address those
147 /// then.
148 virtual void BuildConstructorSignature(const CXXConstructorDecl *Ctor,
149 CXXCtorType T,
150 CanQualType &ResTy,
151 llvm::SmallVectorImpl<CanQualType> &ArgTys) = 0;
152
153 /// Build the signature of the given destructor variant by adding
154 /// any required parameters. For convenience, ResTy has been
155 /// initialized to 'void' and ArgTys has been initialized with the
156 /// type of 'this' (although this may be changed by the ABI).
157 virtual void BuildDestructorSignature(const CXXDestructorDecl *Dtor,
158 CXXDtorType T,
159 CanQualType &ResTy,
160 llvm::SmallVectorImpl<CanQualType> &ArgTys) = 0;
161
162 /// Build the ABI-specific portion of the parameter list for a
163 /// function. This generally involves a 'this' parameter and
164 /// possibly some extra data for constructors and destructors.
165 ///
166 /// ABIs may also choose to override the return type, which has been
167 /// initialized with the formal return type of the function.
168 virtual void BuildInstanceFunctionParams(CodeGenFunction &CGF,
169 QualType &ResTy,
170 FunctionArgList &Params) = 0;
171
172 /// Emit the ABI-specific prolog for the function.
173 virtual void EmitInstanceFunctionProlog(CodeGenFunction &CGF) = 0;
174
175 virtual void EmitReturnFromThunk(CodeGenFunction &CGF,
176 RValue RV, QualType ResultType);
John McCall1e7fe752010-09-02 09:58:18 +0000177
178 /**************************** Array cookies ******************************/
179
180 /// Returns the extra size required in order to store the array
181 /// cookie for the given type. May return 0 to indicate that no
182 /// array cookie is required.
183 ///
184 /// Several cases are filtered out before this method is called:
185 /// - non-array allocations never need a cookie
186 /// - calls to ::operator new(size_t, void*) never need a cookie
187 ///
188 /// \param ElementType - the allocated type of the expression,
189 /// i.e. the pointee type of the expression result type
190 virtual CharUnits GetArrayCookieSize(QualType ElementType);
191
192 /// Initialize the array cookie for the given allocation.
193 ///
194 /// \param NewPtr - a char* which is the presumed-non-null
195 /// return value of the allocation function
196 /// \param NumElements - the computed number of elements,
197 /// potentially collapsed from the multidimensional array case
198 /// \param ElementType - the base element allocated type,
199 /// i.e. the allocated type after stripping all array types
200 virtual llvm::Value *InitializeArrayCookie(CodeGenFunction &CGF,
201 llvm::Value *NewPtr,
202 llvm::Value *NumElements,
203 QualType ElementType);
204
205 /// Reads the array cookie associated with the given pointer,
206 /// if it has one.
207 ///
208 /// \param Ptr - a pointer to the first element in the array
209 /// \param ElementType - the base element type of elements of the array
210 /// \param NumElements - an out parameter which will be initialized
211 /// with the number of elements allocated, or zero if there is no
212 /// cookie
213 /// \param AllocPtr - an out parameter which will be initialized
214 /// with a char* pointing to the address returned by the allocation
215 /// function
216 /// \param CookieSize - an out parameter which will be initialized
217 /// with the size of the cookie, or zero if there is no cookie
218 virtual void ReadArrayCookie(CodeGenFunction &CGF, llvm::Value *Ptr,
219 QualType ElementType, llvm::Value *&NumElements,
220 llvm::Value *&AllocPtr, CharUnits &CookieSize);
221
John McCall5cd91b52010-09-08 01:44:27 +0000222 /*************************** Static local guards ****************************/
223
224 /// Emits the initializer and destructor setup for the given static
225 /// local variable, given that it's reachable and couldn't be
226 /// emitted as a constant.
227 virtual void EmitStaticLocalInit(CodeGenFunction &CGF, const VarDecl &D,
228 llvm::GlobalVariable *DeclPtr);
229
Charles Davis3a811f12010-05-25 19:52:27 +0000230};
231
232/// Creates an instance of a C++ ABI class.
John McCallee79a4c2010-08-21 22:46:04 +0000233CGCXXABI *CreateARMCXXABI(CodeGenModule &CGM);
Charles Davis071cc7d2010-08-16 03:33:14 +0000234CGCXXABI *CreateItaniumCXXABI(CodeGenModule &CGM);
235CGCXXABI *CreateMicrosoftCXXABI(CodeGenModule &CGM);
John McCall93d557b2010-08-22 00:05:51 +0000236
Charles Davis3a811f12010-05-25 19:52:27 +0000237}
238}
239
240#endif