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 constructor or destructor is one of the
101 /// kinds that the ABI says returns 'this' (only applies when called
102 /// non-virtually for destructors).
104 /// There currently is no way to indicate if a destructor returns 'this'
105 /// when called virtually, and code generation does not support the case.
106 virtual bool HasThisReturn(GlobalDecl GD) const { return false; }
108 /// Returns true if the given record type should be returned indirectly.
109 virtual bool isReturnTypeIndirect(const CXXRecordDecl *RD) const = 0;
111 /// Specify how one should pass an argument of a record type.
113 /// Pass it using the normal C aggregate rules for the ABI, potentially
114 /// introducing extra copies and passing some or all of it in registers.
117 /// Pass it on the stack using its defined layout. The argument must be
118 /// evaluated directly into the correct stack position in the arguments area,
119 /// and the call machinery must not move it or introduce extra copies.
122 /// Pass it as a pointer to temporary memory.
126 /// Returns how an argument of the given record type should be passed.
127 virtual RecordArgABI getRecordArgABI(const CXXRecordDecl *RD) const = 0;
129 /// Find the LLVM type used to represent the given member pointer
132 ConvertMemberPointerType(const MemberPointerType *MPT);
134 /// Load a member function from an object and a member function
135 /// pointer. Apply the this-adjustment and set 'This' to the
137 virtual llvm::Value *
138 EmitLoadOfMemberFunctionPointer(CodeGenFunction &CGF,
141 const MemberPointerType *MPT);
143 /// Calculate an l-value from an object and a data member pointer.
144 virtual llvm::Value *EmitMemberDataPointerAddress(CodeGenFunction &CGF,
147 const MemberPointerType *MPT);
149 /// Perform a derived-to-base, base-to-derived, or bitcast member
150 /// pointer conversion.
151 virtual llvm::Value *EmitMemberPointerConversion(CodeGenFunction &CGF,
155 /// Perform a derived-to-base, base-to-derived, or bitcast member
156 /// pointer conversion on a constant value.
157 virtual llvm::Constant *EmitMemberPointerConversion(const CastExpr *E,
158 llvm::Constant *Src);
160 /// Return true if the given member pointer can be zero-initialized
161 /// (in the C++ sense) with an LLVM zeroinitializer.
162 virtual bool isZeroInitializable(const MemberPointerType *MPT);
164 /// Create a null member pointer of the given type.
165 virtual llvm::Constant *EmitNullMemberPointer(const MemberPointerType *MPT);
167 /// Create a member pointer for the given method.
168 virtual llvm::Constant *EmitMemberPointer(const CXXMethodDecl *MD);
170 /// Create a member pointer for the given field.
171 virtual llvm::Constant *EmitMemberDataPointer(const MemberPointerType *MPT,
174 /// Create a member pointer for the given member pointer constant.
175 virtual llvm::Constant *EmitMemberPointer(const APValue &MP, QualType MPT);
177 /// Emit a comparison between two member pointers. Returns an i1.
178 virtual llvm::Value *
179 EmitMemberPointerComparison(CodeGenFunction &CGF,
182 const MemberPointerType *MPT,
185 /// Determine if a member pointer is non-null. Returns an i1.
186 virtual llvm::Value *
187 EmitMemberPointerIsNotNull(CodeGenFunction &CGF,
189 const MemberPointerType *MPT);
192 /// A utility method for computing the offset required for the given
193 /// base-to-derived or derived-to-base member-pointer conversion.
194 /// Does not handle virtual conversions (in case we ever fully
195 /// support an ABI that allows this). Returns null if no adjustment
197 llvm::Constant *getMemberPointerAdjustment(const CastExpr *E);
199 /// \brief Computes the non-virtual adjustment needed for a member pointer
200 /// conversion along an inheritance path stored in an APValue. Unlike
201 /// getMemberPointerAdjustment(), the adjustment can be negative if the path
202 /// is from a derived type to a base type.
203 CharUnits getMemberPointerPathAdjustment(const APValue &MP);
206 /// Adjust the given non-null pointer to an object of polymorphic
207 /// type to point to the complete object.
209 /// The IR type of the result should be a pointer but is otherwise
211 virtual llvm::Value *adjustToCompleteObject(CodeGenFunction &CGF,
215 virtual llvm::Value *GetVirtualBaseClassOffset(CodeGenFunction &CGF,
217 const CXXRecordDecl *ClassDecl,
218 const CXXRecordDecl *BaseClassDecl) = 0;
220 /// Build the signature of the given constructor variant by adding
221 /// any required parameters. For convenience, ArgTys has been initialized
222 /// with the type of 'this' and ResTy has been initialized with the type of
223 /// 'this' if HasThisReturn(GlobalDecl(Ctor, T)) is true or 'void' otherwise
224 /// (although both may be changed by the ABI).
226 /// If there are ever any ABIs where the implicit parameters are
227 /// intermixed with the formal parameters, we can address those
229 virtual void BuildConstructorSignature(const CXXConstructorDecl *Ctor,
232 SmallVectorImpl<CanQualType> &ArgTys) = 0;
234 virtual llvm::BasicBlock *EmitCtorCompleteObjectHandler(CodeGenFunction &CGF,
235 const CXXRecordDecl *RD);
237 /// Build the signature of the given destructor variant by adding
238 /// any required parameters. For convenience, ArgTys has been initialized
239 /// with the type of 'this' and ResTy has been initialized with the type of
240 /// 'this' if HasThisReturn(GlobalDecl(Dtor, T)) is true or 'void' otherwise
241 /// (although both may be changed by the ABI).
242 virtual void BuildDestructorSignature(const CXXDestructorDecl *Dtor,
245 SmallVectorImpl<CanQualType> &ArgTys) = 0;
247 /// Build the ABI-specific portion of the parameter list for a
248 /// function. This generally involves a 'this' parameter and
249 /// possibly some extra data for constructors and destructors.
251 /// ABIs may also choose to override the return type, which has been
252 /// initialized with the type of 'this' if HasThisReturn(CGF.CurGD) is true or
253 /// the formal return type of the function otherwise.
254 virtual void BuildInstanceFunctionParams(CodeGenFunction &CGF,
256 FunctionArgList &Params) = 0;
258 /// Emit the ABI-specific prolog for the function.
259 virtual void EmitInstanceFunctionProlog(CodeGenFunction &CGF) = 0;
261 /// Emit the constructor call. Return the function that is called.
262 virtual void EmitConstructorCall(CodeGenFunction &CGF,
263 const CXXConstructorDecl *D,
265 bool ForVirtualBase, bool Delegating,
267 CallExpr::const_arg_iterator ArgBeg,
268 CallExpr::const_arg_iterator ArgEnd) = 0;
270 /// Emit the ABI-specific virtual destructor call.
271 virtual void EmitVirtualDestructorCall(CodeGenFunction &CGF,
272 const CXXDestructorDecl *Dtor,
273 CXXDtorType DtorType,
274 SourceLocation CallLoc,
275 llvm::Value *This) = 0;
277 /// Emit any tables needed to implement virtual inheritance. For Itanium,
278 /// this emits virtual table tables. For the MSVC++ ABI, this emits virtual
281 EmitVirtualInheritanceTables(llvm::GlobalVariable::LinkageTypes Linkage,
282 const CXXRecordDecl *RD) = 0;
284 virtual void EmitReturnFromThunk(CodeGenFunction &CGF,
285 RValue RV, QualType ResultType);
287 /// Gets the pure virtual member call function.
288 virtual StringRef GetPureVirtualCallName() = 0;
290 /// Gets the deleted virtual member call name.
291 virtual StringRef GetDeletedVirtualCallName() = 0;
293 /**************************** Array cookies ******************************/
295 /// Returns the extra size required in order to store the array
296 /// cookie for the given new-expression. May return 0 to indicate that no
297 /// array cookie is required.
299 /// Several cases are filtered out before this method is called:
300 /// - non-array allocations never need a cookie
301 /// - calls to \::operator new(size_t, void*) never need a cookie
303 /// \param expr - the new-expression being allocated.
304 virtual CharUnits GetArrayCookieSize(const CXXNewExpr *expr);
306 /// Initialize the array cookie for the given allocation.
308 /// \param NewPtr - a char* which is the presumed-non-null
309 /// return value of the allocation function
310 /// \param NumElements - the computed number of elements,
311 /// potentially collapsed from the multidimensional array case;
313 /// \param ElementType - the base element allocated type,
314 /// i.e. the allocated type after stripping all array types
315 virtual llvm::Value *InitializeArrayCookie(CodeGenFunction &CGF,
317 llvm::Value *NumElements,
318 const CXXNewExpr *expr,
319 QualType ElementType);
321 /// Reads the array cookie associated with the given pointer,
324 /// \param Ptr - a pointer to the first element in the array
325 /// \param ElementType - the base element type of elements of the array
326 /// \param NumElements - an out parameter which will be initialized
327 /// with the number of elements allocated, or zero if there is no
329 /// \param AllocPtr - an out parameter which will be initialized
330 /// with a char* pointing to the address returned by the allocation
332 /// \param CookieSize - an out parameter which will be initialized
333 /// with the size of the cookie, or zero if there is no cookie
334 virtual void ReadArrayCookie(CodeGenFunction &CGF, llvm::Value *Ptr,
335 const CXXDeleteExpr *expr,
336 QualType ElementType, llvm::Value *&NumElements,
337 llvm::Value *&AllocPtr, CharUnits &CookieSize);
339 /// Return whether the given global decl needs a VTT parameter.
340 virtual bool NeedsVTTParameter(GlobalDecl GD);
343 /// Returns the extra size required in order to store the array
344 /// cookie for the given type. Assumes that an array cookie is
346 virtual CharUnits getArrayCookieSizeImpl(QualType elementType);
348 /// Reads the array cookie for an allocation which is known to have one.
349 /// This is called by the standard implementation of ReadArrayCookie.
351 /// \param ptr - a pointer to the allocation made for an array, as a char*
352 /// \param cookieSize - the computed cookie size of an array
354 /// Other parameters are as above.
357 virtual llvm::Value *readArrayCookieImpl(CodeGenFunction &IGF,
359 CharUnits cookieSize);
363 /*************************** Static local guards ****************************/
365 /// Emits the guarded initializer and destructor setup for the given
366 /// variable, given that it couldn't be emitted as a constant.
367 /// If \p PerformInit is false, the initialization has been folded to a
368 /// constant and should not be performed.
370 /// The variable may be:
371 /// - a static local variable
372 /// - a static data member of a class template instantiation
373 virtual void EmitGuardedInit(CodeGenFunction &CGF, const VarDecl &D,
374 llvm::GlobalVariable *DeclPtr, bool PerformInit);
376 /// Emit code to force the execution of a destructor during global
377 /// teardown. The default implementation of this uses atexit.
379 /// \param dtor - a function taking a single pointer argument
380 /// \param addr - a pointer to pass to the destructor function.
381 virtual void registerGlobalDtor(CodeGenFunction &CGF, const VarDecl &D,
382 llvm::Constant *dtor, llvm::Constant *addr);
384 /*************************** thread_local initialization ********************/
386 /// Emits ABI-required functions necessary to initialize thread_local
387 /// variables in this translation unit.
389 /// \param Decls The thread_local declarations in this translation unit.
390 /// \param InitFunc If this translation unit contains any non-constant
391 /// initialization or non-trivial destruction for thread_local
392 /// variables, a function to perform the initialization. Otherwise, 0.
393 virtual void EmitThreadLocalInitFuncs(
394 llvm::ArrayRef<std::pair<const VarDecl *, llvm::GlobalVariable *> > Decls,
395 llvm::Function *InitFunc);
397 /// Emit a reference to a non-local thread_local variable (including
398 /// triggering the initialization of all thread_local variables in its
399 /// translation unit).
400 virtual LValue EmitThreadLocalDeclRefExpr(CodeGenFunction &CGF,
401 const DeclRefExpr *DRE);
404 // Create an instance of a C++ ABI class:
406 /// Creates an Itanium-family ABI.
407 CGCXXABI *CreateItaniumCXXABI(CodeGenModule &CGM);
409 /// Creates a Microsoft-family ABI.
410 CGCXXABI *CreateMicrosoftCXXABI(CodeGenModule &CGM);