1 //===----- CGCXXABI.h - Interface to C++ ABIs -------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This provides an abstract class for C++ code generation. Concrete subclasses
11 // of this implement code generation for specific C++ ABIs.
13 //===----------------------------------------------------------------------===//
15 #ifndef CLANG_CODEGEN_CXXABI_H
16 #define CLANG_CODEGEN_CXXABI_H
18 #include "CodeGenFunction.h"
19 #include "clang/Basic/LLVM.h"
29 class CXXConstructorDecl;
30 class CXXDestructorDecl;
37 class CodeGenFunction;
40 /// \brief Implements C++ ABI-specific code generation functions.
44 OwningPtr<MangleContext> MangleCtx;
46 CGCXXABI(CodeGenModule &CGM)
47 : CGM(CGM), MangleCtx(CGM.getContext().createMangleContext()) {}
50 ImplicitParamDecl *&getThisDecl(CodeGenFunction &CGF) {
51 return CGF.CXXABIThisDecl;
53 llvm::Value *&getThisValue(CodeGenFunction &CGF) {
54 return CGF.CXXABIThisValue;
57 /// Issue a diagnostic about unsupported features in the ABI.
58 void ErrorUnsupportedABI(CodeGenFunction &CGF, StringRef S);
60 /// Get a null value for unsupported member pointers.
61 llvm::Constant *GetBogusMemberPointer(QualType T);
63 // FIXME: Every place that calls getVTT{Decl,Value} is something
64 // that needs to be abstracted properly.
65 ImplicitParamDecl *&getVTTDecl(CodeGenFunction &CGF) {
66 return CGF.CXXStructorImplicitParamDecl;
68 llvm::Value *&getVTTValue(CodeGenFunction &CGF) {
69 return CGF.CXXStructorImplicitParamValue;
72 ImplicitParamDecl *&getStructorImplicitParamDecl(CodeGenFunction &CGF) {
73 return CGF.CXXStructorImplicitParamDecl;
75 llvm::Value *&getStructorImplicitParamValue(CodeGenFunction &CGF) {
76 return CGF.CXXStructorImplicitParamValue;
79 /// Build a parameter variable suitable for 'this'.
80 void BuildThisParam(CodeGenFunction &CGF, FunctionArgList &Params);
82 /// Perform prolog initialization of the parameter variable suitable
83 /// for 'this' emitted by BuildThisParam.
84 void EmitThisParam(CodeGenFunction &CGF);
86 ASTContext &getContext() const { return CGM.getContext(); }
88 virtual bool requiresArrayCookie(const CXXDeleteExpr *E, QualType eltType);
89 virtual bool requiresArrayCookie(const CXXNewExpr *E);
95 /// Gets the mangle context.
96 MangleContext &getMangleContext() {
100 /// Returns true if the given instance method is one of the
101 /// kinds that the ABI says returns 'this'.
102 virtual bool HasThisReturn(GlobalDecl GD) const { return false; }
104 /// Returns true if the given record type should be returned indirectly.
105 virtual bool isReturnTypeIndirect(const CXXRecordDecl *RD) const = 0;
107 /// Specify how one should pass an argument of a record type.
109 /// Pass it using the normal C aggregate rules for the ABI, potentially
110 /// introducing extra copies and passing some or all of it in registers.
113 /// Pass it on the stack using its defined layout. The argument must be
114 /// evaluated directly into the correct stack position in the arguments area,
115 /// and the call machinery must not move it or introduce extra copies.
118 /// Pass it as a pointer to temporary memory.
122 /// Returns how an argument of the given record type should be passed.
123 virtual RecordArgABI getRecordArgABI(const CXXRecordDecl *RD) const = 0;
125 /// Find the LLVM type used to represent the given member pointer
128 ConvertMemberPointerType(const MemberPointerType *MPT);
130 /// Load a member function from an object and a member function
131 /// pointer. Apply the this-adjustment and set 'This' to the
133 virtual llvm::Value *
134 EmitLoadOfMemberFunctionPointer(CodeGenFunction &CGF,
137 const MemberPointerType *MPT);
139 /// Calculate an l-value from an object and a data member pointer.
140 virtual llvm::Value *EmitMemberDataPointerAddress(CodeGenFunction &CGF,
143 const MemberPointerType *MPT);
145 /// Perform a derived-to-base, base-to-derived, or bitcast member
146 /// pointer conversion.
147 virtual llvm::Value *EmitMemberPointerConversion(CodeGenFunction &CGF,
151 /// Perform a derived-to-base, base-to-derived, or bitcast member
152 /// pointer conversion on a constant value.
153 virtual llvm::Constant *EmitMemberPointerConversion(const CastExpr *E,
154 llvm::Constant *Src);
156 /// Return true if the given member pointer can be zero-initialized
157 /// (in the C++ sense) with an LLVM zeroinitializer.
158 virtual bool isZeroInitializable(const MemberPointerType *MPT);
160 /// Create a null member pointer of the given type.
161 virtual llvm::Constant *EmitNullMemberPointer(const MemberPointerType *MPT);
163 /// Create a member pointer for the given method.
164 virtual llvm::Constant *EmitMemberPointer(const CXXMethodDecl *MD);
166 /// Create a member pointer for the given field.
167 virtual llvm::Constant *EmitMemberDataPointer(const MemberPointerType *MPT,
170 /// Create a member pointer for the given member pointer constant.
171 virtual llvm::Constant *EmitMemberPointer(const APValue &MP, QualType MPT);
173 /// Emit a comparison between two member pointers. Returns an i1.
174 virtual llvm::Value *
175 EmitMemberPointerComparison(CodeGenFunction &CGF,
178 const MemberPointerType *MPT,
181 /// Determine if a member pointer is non-null. Returns an i1.
182 virtual llvm::Value *
183 EmitMemberPointerIsNotNull(CodeGenFunction &CGF,
185 const MemberPointerType *MPT);
188 /// A utility method for computing the offset required for the given
189 /// base-to-derived or derived-to-base member-pointer conversion.
190 /// Does not handle virtual conversions (in case we ever fully
191 /// support an ABI that allows this). Returns null if no adjustment
193 llvm::Constant *getMemberPointerAdjustment(const CastExpr *E);
195 /// \brief Computes the non-virtual adjustment needed for a member pointer
196 /// conversion along an inheritance path stored in an APValue. Unlike
197 /// getMemberPointerAdjustment(), the adjustment can be negative if the path
198 /// is from a derived type to a base type.
199 CharUnits getMemberPointerPathAdjustment(const APValue &MP);
202 /// Adjust the given non-null pointer to an object of polymorphic
203 /// type to point to the complete object.
205 /// The IR type of the result should be a pointer but is otherwise
207 virtual llvm::Value *adjustToCompleteObject(CodeGenFunction &CGF,
211 virtual llvm::Value *GetVirtualBaseClassOffset(CodeGenFunction &CGF,
213 const CXXRecordDecl *ClassDecl,
214 const CXXRecordDecl *BaseClassDecl) = 0;
216 /// Build the signature of the given constructor variant by adding
217 /// any required parameters. For convenience, ResTy has been
218 /// initialized to 'void', and ArgTys has been initialized with the
219 /// type of 'this' (although this may be changed by the ABI) and
220 /// will have the formal parameters added to it afterwards.
222 /// If there are ever any ABIs where the implicit parameters are
223 /// intermixed with the formal parameters, we can address those
225 virtual void BuildConstructorSignature(const CXXConstructorDecl *Ctor,
228 SmallVectorImpl<CanQualType> &ArgTys) = 0;
230 virtual llvm::BasicBlock *EmitCtorCompleteObjectHandler(CodeGenFunction &CGF);
232 /// Build the signature of the given destructor variant by adding
233 /// any required parameters. For convenience, ResTy has been
234 /// initialized to 'void' and ArgTys has been initialized with the
235 /// type of 'this' (although this may be changed by the ABI).
236 virtual void BuildDestructorSignature(const CXXDestructorDecl *Dtor,
239 SmallVectorImpl<CanQualType> &ArgTys) = 0;
241 /// Build the ABI-specific portion of the parameter list for a
242 /// function. This generally involves a 'this' parameter and
243 /// possibly some extra data for constructors and destructors.
245 /// ABIs may also choose to override the return type, which has been
246 /// initialized with the formal return type of the function.
247 virtual void BuildInstanceFunctionParams(CodeGenFunction &CGF,
249 FunctionArgList &Params) = 0;
251 /// Emit the ABI-specific prolog for the function.
252 virtual void EmitInstanceFunctionProlog(CodeGenFunction &CGF) = 0;
254 /// Emit the constructor call. Return the function that is called.
255 virtual llvm::Value *EmitConstructorCall(CodeGenFunction &CGF,
256 const CXXConstructorDecl *D,
257 CXXCtorType Type, bool ForVirtualBase,
260 CallExpr::const_arg_iterator ArgBeg,
261 CallExpr::const_arg_iterator ArgEnd) = 0;
263 /// Emit the ABI-specific virtual destructor call.
264 virtual RValue EmitVirtualDestructorCall(CodeGenFunction &CGF,
265 const CXXDestructorDecl *Dtor,
266 CXXDtorType DtorType,
267 SourceLocation CallLoc,
268 ReturnValueSlot ReturnValue,
269 llvm::Value *This) = 0;
271 virtual void EmitReturnFromThunk(CodeGenFunction &CGF,
272 RValue RV, QualType ResultType);
274 /// Gets the pure virtual member call function.
275 virtual StringRef GetPureVirtualCallName() = 0;
277 /// Gets the deleted virtual member call name.
278 virtual StringRef GetDeletedVirtualCallName() = 0;
280 /**************************** Array cookies ******************************/
282 /// Returns the extra size required in order to store the array
283 /// cookie for the given new-expression. May return 0 to indicate that no
284 /// array cookie is required.
286 /// Several cases are filtered out before this method is called:
287 /// - non-array allocations never need a cookie
288 /// - calls to \::operator new(size_t, void*) never need a cookie
290 /// \param expr - the new-expression being allocated.
291 virtual CharUnits GetArrayCookieSize(const CXXNewExpr *expr);
293 /// Initialize the array cookie for the given allocation.
295 /// \param NewPtr - a char* which is the presumed-non-null
296 /// return value of the allocation function
297 /// \param NumElements - the computed number of elements,
298 /// potentially collapsed from the multidimensional array case;
300 /// \param ElementType - the base element allocated type,
301 /// i.e. the allocated type after stripping all array types
302 virtual llvm::Value *InitializeArrayCookie(CodeGenFunction &CGF,
304 llvm::Value *NumElements,
305 const CXXNewExpr *expr,
306 QualType ElementType);
308 /// Reads the array cookie associated with the given pointer,
311 /// \param Ptr - a pointer to the first element in the array
312 /// \param ElementType - the base element type of elements of the array
313 /// \param NumElements - an out parameter which will be initialized
314 /// with the number of elements allocated, or zero if there is no
316 /// \param AllocPtr - an out parameter which will be initialized
317 /// with a char* pointing to the address returned by the allocation
319 /// \param CookieSize - an out parameter which will be initialized
320 /// with the size of the cookie, or zero if there is no cookie
321 virtual void ReadArrayCookie(CodeGenFunction &CGF, llvm::Value *Ptr,
322 const CXXDeleteExpr *expr,
323 QualType ElementType, llvm::Value *&NumElements,
324 llvm::Value *&AllocPtr, CharUnits &CookieSize);
327 /// Returns the extra size required in order to store the array
328 /// cookie for the given type. Assumes that an array cookie is
330 virtual CharUnits getArrayCookieSizeImpl(QualType elementType);
332 /// Reads the array cookie for an allocation which is known to have one.
333 /// This is called by the standard implementation of ReadArrayCookie.
335 /// \param ptr - a pointer to the allocation made for an array, as a char*
336 /// \param cookieSize - the computed cookie size of an array
338 /// Other parameters are as above.
341 virtual llvm::Value *readArrayCookieImpl(CodeGenFunction &IGF,
343 CharUnits cookieSize);
347 /*************************** Static local guards ****************************/
349 /// Emits the guarded initializer and destructor setup for the given
350 /// variable, given that it couldn't be emitted as a constant.
351 /// If \p PerformInit is false, the initialization has been folded to a
352 /// constant and should not be performed.
354 /// The variable may be:
355 /// - a static local variable
356 /// - a static data member of a class template instantiation
357 virtual void EmitGuardedInit(CodeGenFunction &CGF, const VarDecl &D,
358 llvm::GlobalVariable *DeclPtr, bool PerformInit);
360 /// Emit code to force the execution of a destructor during global
361 /// teardown. The default implementation of this uses atexit.
363 /// \param dtor - a function taking a single pointer argument
364 /// \param addr - a pointer to pass to the destructor function.
365 virtual void registerGlobalDtor(CodeGenFunction &CGF, const VarDecl &D,
366 llvm::Constant *dtor, llvm::Constant *addr);
368 /*************************** thread_local initialization ********************/
370 /// Emits ABI-required functions necessary to initialize thread_local
371 /// variables in this translation unit.
373 /// \param Decls The thread_local declarations in this translation unit.
374 /// \param InitFunc If this translation unit contains any non-constant
375 /// initialization or non-trivial destruction for thread_local
376 /// variables, a function to perform the initialization. Otherwise, 0.
377 virtual void EmitThreadLocalInitFuncs(
378 llvm::ArrayRef<std::pair<const VarDecl *, llvm::GlobalVariable *> > Decls,
379 llvm::Function *InitFunc);
381 /// Emit a reference to a non-local thread_local variable (including
382 /// triggering the initialization of all thread_local variables in its
383 /// translation unit).
384 virtual LValue EmitThreadLocalDeclRefExpr(CodeGenFunction &CGF,
385 const DeclRefExpr *DRE);
388 // Create an instance of a C++ ABI class:
390 /// Creates an Itanium-family ABI.
391 CGCXXABI *CreateItaniumCXXABI(CodeGenModule &CGM);
393 /// Creates a Microsoft-family ABI.
394 CGCXXABI *CreateMicrosoftCXXABI(CodeGenModule &CGM);