clang 22.0.0git
CIRGenCXXABI.h
Go to the documentation of this file.
1//===----------------------------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This provides an abstract class for C++ code generation. Concrete subclasses
10// of this implement code generation for specific C++ ABIs.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_CLANG_LIB_CIR_CIRGENCXXABI_H
15#define LLVM_CLANG_LIB_CIR_CIRGENCXXABI_H
16
17#include "CIRGenCall.h"
18#include "CIRGenCleanup.h"
19#include "CIRGenFunction.h"
20#include "CIRGenModule.h"
21
22#include "clang/AST/Mangle.h"
23
24namespace clang::CIRGen {
25
26/// Implements C++ ABI-specific code generation functions.
28protected:
30 std::unique_ptr<clang::MangleContext> mangleContext;
31
32 virtual bool requiresArrayCookie(const CXXNewExpr *e);
33
34public:
35 // TODO(cir): make this protected when target-specific CIRGenCXXABIs are
36 // implemented.
38 : cgm(cgm), mangleContext(cgm.getASTContext().createMangleContext()) {}
39 virtual ~CIRGenCXXABI();
40
41 void setCXXABIThisValue(CIRGenFunction &cgf, mlir::Value thisPtr);
42
43 /// Emit the code to initialize hidden members required to handle virtual
44 /// inheritance, if needed by the ABI.
45 virtual void
48
49 /// Emit a single constructor/destructor with the gen type from a C++
50 /// constructor/destructor Decl.
51 virtual void emitCXXStructor(clang::GlobalDecl gd) = 0;
52
53 virtual mlir::Value
54 getVirtualBaseClassOffset(mlir::Location loc, CIRGenFunction &cgf,
55 Address thisAddr, const CXXRecordDecl *classDecl,
56 const CXXRecordDecl *baseClassDecl) = 0;
57
58 virtual mlir::Value emitDynamicCast(CIRGenFunction &cgf, mlir::Location loc,
59 QualType srcRecordTy,
60 QualType destRecordTy,
61 cir::PointerType destCIRTy,
62 bool isRefCast, Address src) = 0;
63
64public:
65 /// Similar to AddedStructorArgs, but only notes the number of additional
66 /// arguments.
68 unsigned prefix = 0;
69 unsigned suffix = 0;
71 AddedStructorArgCounts(unsigned p, unsigned s) : prefix(p), suffix(s) {}
72 static AddedStructorArgCounts withPrefix(unsigned n) { return {n, 0}; }
73 static AddedStructorArgCounts withSuffix(unsigned n) { return {0, n}; }
74 };
75
76 /// Additional implicit arguments to add to the beginning (Prefix) and end
77 /// (Suffix) of a constructor / destructor arg list.
78 ///
79 /// Note that Prefix should actually be inserted *after* the first existing
80 /// arg; `this` arguments always come first.
98
99 /// Build the signature of the given constructor or destructor vairant by
100 /// adding any required parameters. For convenience, ArgTys has been
101 /// initialized with the type of 'this'.
102 virtual AddedStructorArgCounts
105
108 CXXCtorType type, bool forVirtualBase,
109 bool delegating, CallArgList &args);
110
114
115 virtual AddedStructorArgs
117 CXXCtorType type, bool forVirtualBase,
118 bool delegating) = 0;
119
120 /// Emit the ABI-specific prolog for the function
122 CIRGenFunction &cgf) = 0;
123
124 virtual void emitRethrow(CIRGenFunction &cgf, bool isNoReturn) = 0;
125 virtual void emitThrow(CIRGenFunction &cgf, const CXXThrowExpr *e) = 0;
126
127 virtual void emitBadCastCall(CIRGenFunction &cgf, mlir::Location loc) = 0;
128
129 virtual mlir::Attribute getAddrOfRTTIDescriptor(mlir::Location loc,
130 QualType ty) = 0;
131
132 /// Get the type of the implicit "this" parameter used by a method. May return
133 /// zero if no specific type is applicable, e.g. if the ABI expects the "this"
134 /// parameter to point to some artificial offset in a complete object due to
135 /// vbases being reordered.
136 virtual const clang::CXXRecordDecl *
138 return md->getParent();
139 }
140
141 /// Return whether the given global decl needs a VTT (virtual table table)
142 /// parameter.
143 virtual bool needsVTTParameter(clang::GlobalDecl gd) { return false; }
144
145 /// Perform ABI-specific "this" argument adjustment required prior to
146 /// a call of a virtual function.
147 /// The "VirtualCall" argument is true iff the call itself is virtual.
150 Address thisPtr,
151 bool virtualCall) {
152 return thisPtr;
153 }
154
155 /// Build a parameter variable suitable for 'this'.
157
158 /// Loads the incoming C++ this pointer as it was passed by the caller.
159 mlir::Value loadIncomingCXXThis(CIRGenFunction &cgf);
160
162
163 /// Get the implicit (second) parameter that comes after the "this" pointer,
164 /// or nullptr if there is isn't one.
166 const CXXDestructorDecl *dd,
168 bool forVirtualBase,
169 bool delegating) = 0;
170
171 /// Emit constructor variants required by this ABI.
173
174 /// Emit dtor variants required by this ABI.
176
179 bool forVirtualBase, bool delegating,
180 Address thisAddr, QualType thisTy) = 0;
181
182 /// Emit code to force the execution of a destructor during global
183 /// teardown. The default implementation of this uses atexit.
184 ///
185 /// \param dtor - a function taking a single pointer argument
186 /// \param addr - a pointer to pass to the destructor function.
187 virtual void registerGlobalDtor(const VarDecl *vd, cir::FuncOp dtor,
188 mlir::Value addr) = 0;
189
191 const CXXDeleteExpr *de, Address ptr,
192 QualType elementType,
193 const CXXDestructorDecl *dtor) = 0;
194
195 /// Checks if ABI requires extra virtual offset for vtable field.
196 virtual bool
198 CIRGenFunction::VPtr vptr) = 0;
199
200 /// Emits the VTable definitions required for the given record type.
202 const CXXRecordDecl *rd) = 0;
203
205 llvm::PointerUnion<const CXXDeleteExpr *, const CXXMemberCallExpr *>;
206
208 const CXXDestructorDecl *dtor,
209 CXXDtorType dtorType,
210 Address thisAddr,
212
213 /// Emit any tables needed to implement virtual inheritance. For Itanium,
214 /// this emits virtual table tables.
215 virtual void emitVirtualInheritanceTables(const CXXRecordDecl *rd) = 0;
216
217 /// Returns true if the given destructor type should be emitted as a linkonce
218 /// delegating thunk, regardless of whether the dtor is defined in this TU or
219 /// not.
221 CXXDtorType dt) const = 0;
222
223 virtual cir::GlobalLinkageKind
225 CXXDtorType dt) const;
226
227 /// Get the address of the vtable for the given record decl which should be
228 /// used for the vptr at the given offset in RD.
229 virtual cir::GlobalOp getAddrOfVTable(const CXXRecordDecl *rd,
230 CharUnits vptrOffset) = 0;
231
232 /// Build a virtual function pointer in the ABI-specific way.
235 Address thisAddr,
236 mlir::Type ty,
237 SourceLocation loc) = 0;
238
239 /// Get the address point of the vtable for the given base subobject.
240 virtual mlir::Value
242 const CXXRecordDecl *vtableClass) = 0;
243
244 /// Get the address point of the vtable for the given base subobject while
245 /// building a constructor or a destructor.
247 CIRGenFunction &cgf, const CXXRecordDecl *vtableClass, BaseSubobject base,
248 const CXXRecordDecl *nearestVBase) = 0;
249
250 /// Insert any ABI-specific implicit parameters into the parameter list for a
251 /// function. This generally involves extra data for constructors and
252 /// destructors.
253 ///
254 /// ABIs may also choose to override the return type, which has been
255 /// initialized with the type of 'this' if HasThisReturn(CGF.CurGD) is true or
256 /// the formal return type of the function otherwise.
258 clang::QualType &resTy,
259 FunctionArgList &params) = 0;
260
261 /// Checks if ABI requires to initialize vptrs for given dynamic class.
262 virtual bool
264
265 /// Returns true if the given constructor or destructor is one of the kinds
266 /// that the ABI says returns 'this' (only applies when called non-virtually
267 /// for destructors).
268 ///
269 /// There currently is no way to indicate if a destructor returns 'this' when
270 /// called virtually, and CIR generation does not support this case.
271 virtual bool hasThisReturn(clang::GlobalDecl gd) const { return false; }
272
274 return false;
275 }
276
277 /// Returns true if the target allows calling a function through a pointer
278 /// with a different signature than the actual function (or equivalently,
279 /// bitcasting a function or function pointer to a different function type).
280 /// In principle in the most general case this could depend on the target, the
281 /// calling convention, and the actual types of the arguments and return
282 /// value. Here it just means whether the signature mismatch could *ever* be
283 /// allowed; in other words, does the target do strict checking of signatures
284 /// for all calls.
285 virtual bool canCallMismatchedFunctionType() const { return true; }
286
287 /// Gets the mangle context.
289
293
297
298 void setStructorImplicitParamValue(CIRGenFunction &cgf, mlir::Value val) {
300 }
301
302 /**************************** Array cookies ******************************/
303
304 /// Returns the extra size required in order to store the array
305 /// cookie for the given new-expression. May return 0 to indicate that no
306 /// array cookie is required.
307 ///
308 /// Several cases are filtered out before this method is called:
309 /// - non-array allocations never need a cookie
310 /// - calls to \::operator new(size_t, void*) never need a cookie
311 ///
312 /// \param e - the new-expression being allocated.
313 virtual CharUnits getArrayCookieSize(const CXXNewExpr *e);
314
315 /// Initialize the array cookie for the given allocation.
316 ///
317 /// \param newPtr - a char* which is the presumed-non-null
318 /// return value of the allocation function
319 /// \param numElements - the computed number of elements,
320 /// potentially collapsed from the multidimensional array case;
321 /// always a size_t
322 /// \param elementType - the base element allocated type,
323 /// i.e. the allocated type after stripping all array types
325 mlir::Value numElements,
326 const CXXNewExpr *e,
327 QualType elementType) = 0;
328
329protected:
330 /// Returns the extra size required in order to store the array
331 /// cookie for the given type. Assumes that an array cookie is
332 /// required.
333 virtual CharUnits getArrayCookieSizeImpl(QualType elementType) = 0;
334};
335
336/// Creates and Itanium-family ABI
338
339} // namespace clang::CIRGen
340
341#endif
__device__ __2f16 float __ockl_bool s
Implements C++ ABI-specific code generation functions.
virtual bool requiresArrayCookie(const CXXNewExpr *e)
virtual mlir::Value getVirtualBaseClassOffset(mlir::Location loc, CIRGenFunction &cgf, Address thisAddr, const CXXRecordDecl *classDecl, const CXXRecordDecl *baseClassDecl)=0
virtual mlir::Attribute getAddrOfRTTIDescriptor(mlir::Location loc, QualType ty)=0
virtual Address initializeArrayCookie(CIRGenFunction &cgf, Address newPtr, mlir::Value numElements, const CXXNewExpr *e, QualType elementType)=0
Initialize the array cookie for the given allocation.
virtual CharUnits getArrayCookieSizeImpl(QualType elementType)=0
Returns the extra size required in order to store the array cookie for the given type.
virtual mlir::Value emitVirtualDestructorCall(CIRGenFunction &cgf, const CXXDestructorDecl *dtor, CXXDtorType dtorType, Address thisAddr, DeleteOrMemberCallExpr e)=0
virtual void registerGlobalDtor(const VarDecl *vd, cir::FuncOp dtor, mlir::Value addr)=0
Emit code to force the execution of a destructor during global teardown.
CIRGenCXXABI(CIRGenModule &cgm)
void setStructorImplicitParamValue(CIRGenFunction &cgf, mlir::Value val)
virtual void emitInstanceFunctionProlog(SourceLocation loc, CIRGenFunction &cgf)=0
Emit the ABI-specific prolog for the function.
virtual bool canCallMismatchedFunctionType() const
Returns true if the target allows calling a function through a pointer with a different signature tha...
virtual void emitCXXConstructors(const clang::CXXConstructorDecl *d)=0
Emit constructor variants required by this ABI.
virtual Address adjustThisArgumentForVirtualFunctionCall(CIRGenFunction &cgf, clang::GlobalDecl gd, Address thisPtr, bool virtualCall)
Perform ABI-specific "this" argument adjustment required prior to a call of a virtual function.
virtual void emitVirtualObjectDelete(CIRGenFunction &cgf, const CXXDeleteExpr *de, Address ptr, QualType elementType, const CXXDestructorDecl *dtor)=0
AddedStructorArgCounts addImplicitConstructorArgs(CIRGenFunction &cgf, const CXXConstructorDecl *d, CXXCtorType type, bool forVirtualBase, bool delegating, CallArgList &args)
virtual void emitVirtualInheritanceTables(const CXXRecordDecl *rd)=0
Emit any tables needed to implement virtual inheritance.
mlir::Value getStructorImplicitParamValue(CIRGenFunction &cgf)
clang::ImplicitParamDecl * getThisDecl(CIRGenFunction &cgf)
virtual const clang::CXXRecordDecl * getThisArgumentTypeForMethod(const clang::CXXMethodDecl *md)
Get the type of the implicit "this" parameter used by a method.
virtual AddedStructorArgs getImplicitConstructorArgs(CIRGenFunction &cgf, const CXXConstructorDecl *d, CXXCtorType type, bool forVirtualBase, bool delegating)=0
virtual void emitCXXDestructors(const clang::CXXDestructorDecl *d)=0
Emit dtor variants required by this ABI.
virtual cir::GlobalOp getAddrOfVTable(const CXXRecordDecl *rd, CharUnits vptrOffset)=0
Get the address of the vtable for the given record decl which should be used for the vptr at the give...
virtual CIRGenCallee getVirtualFunctionPointer(CIRGenFunction &cgf, clang::GlobalDecl gd, Address thisAddr, mlir::Type ty, SourceLocation loc)=0
Build a virtual function pointer in the ABI-specific way.
virtual void emitThrow(CIRGenFunction &cgf, const CXXThrowExpr *e)=0
virtual bool hasThisReturn(clang::GlobalDecl gd) const
Returns true if the given constructor or destructor is one of the kinds that the ABI says returns 'th...
clang::MangleContext & getMangleContext()
Gets the mangle context.
virtual mlir::Value getCXXDestructorImplicitParam(CIRGenFunction &cgf, const CXXDestructorDecl *dd, CXXDtorType type, bool forVirtualBase, bool delegating)=0
Get the implicit (second) parameter that comes after the "this" pointer, or nullptr if there is isn't...
virtual void emitVTableDefinitions(CIRGenVTables &cgvt, const CXXRecordDecl *rd)=0
Emits the VTable definitions required for the given record type.
virtual bool useThunkForDtorVariant(const CXXDestructorDecl *dtor, CXXDtorType dt) const =0
Returns true if the given destructor type should be emitted as a linkonce delegating thunk,...
virtual AddedStructorArgCounts buildStructorSignature(GlobalDecl gd, llvm::SmallVectorImpl< CanQualType > &argTys)=0
Build the signature of the given constructor or destructor vairant by adding any required parameters.
virtual bool needsVTTParameter(clang::GlobalDecl gd)
Return whether the given global decl needs a VTT (virtual table table) parameter.
virtual mlir::Value getVTableAddressPointInStructor(CIRGenFunction &cgf, const CXXRecordDecl *vtableClass, BaseSubobject base, const CXXRecordDecl *nearestVBase)=0
Get the address point of the vtable for the given base subobject while building a constructor or a de...
void setCXXABIThisValue(CIRGenFunction &cgf, mlir::Value thisPtr)
virtual mlir::Value getVTableAddressPoint(BaseSubobject base, const CXXRecordDecl *vtableClass)=0
Get the address point of the vtable for the given base subobject.
std::unique_ptr< clang::MangleContext > mangleContext
virtual void emitDestructorCall(CIRGenFunction &cgf, const CXXDestructorDecl *dd, CXXDtorType type, bool forVirtualBase, bool delegating, Address thisAddr, QualType thisTy)=0
virtual void emitCXXStructor(clang::GlobalDecl gd)=0
Emit a single constructor/destructor with the gen type from a C++ constructor/destructor Decl.
mlir::Value loadIncomingCXXThis(CIRGenFunction &cgf)
Loads the incoming C++ this pointer as it was passed by the caller.
virtual bool hasMostDerivedReturn(clang::GlobalDecl gd) const
virtual void initializeHiddenVirtualInheritanceMembers(CIRGenFunction &cgf, const CXXRecordDecl *rd)
Emit the code to initialize hidden members required to handle virtual inheritance,...
virtual void emitBadCastCall(CIRGenFunction &cgf, mlir::Location loc)=0
virtual bool isVirtualOffsetNeededForVTableField(CIRGenFunction &cgf, CIRGenFunction::VPtr vptr)=0
Checks if ABI requires extra virtual offset for vtable field.
llvm::PointerUnion< const CXXDeleteExpr *, const CXXMemberCallExpr * > DeleteOrMemberCallExpr
virtual mlir::Value emitDynamicCast(CIRGenFunction &cgf, mlir::Location loc, QualType srcRecordTy, QualType destRecordTy, cir::PointerType destCIRTy, bool isRefCast, Address src)=0
virtual void emitRethrow(CIRGenFunction &cgf, bool isNoReturn)=0
virtual bool doStructorsInitializeVPtrs(const clang::CXXRecordDecl *vtableClass)=0
Checks if ABI requires to initialize vptrs for given dynamic class.
virtual CatchTypeInfo getCatchAllTypeInfo()
virtual void addImplicitStructorParams(CIRGenFunction &cgf, clang::QualType &resTy, FunctionArgList &params)=0
Insert any ABI-specific implicit parameters into the parameter list for a function.
virtual CharUnits getArrayCookieSize(const CXXNewExpr *e)
Returns the extra size required in order to store the array cookie for the given new-expression.
clang::ImplicitParamDecl *& getStructorImplicitParamDecl(CIRGenFunction &cgf)
virtual cir::GlobalLinkageKind getCXXDestructorLinkage(GVALinkage linkage, const CXXDestructorDecl *dtor, CXXDtorType dt) const
void buildThisParam(CIRGenFunction &cgf, FunctionArgList &params)
Build a parameter variable suitable for 'this'.
ImplicitParamDecl * cxxabiThisDecl
CXXThisDecl - When generating code for a C++ member function, this will hold the implicit 'this' decl...
ImplicitParamDecl * cxxStructorImplicitParamDecl
When generating code for a constructor or destructor, this will hold the implicit argument (e....
This class organizes the cross-function state that is used while generating CIR code.
Type for representing both the decl and type of parameters to a function.
Definition CIRGenCall.h:191
Represents a C++ constructor within a class.
Definition DeclCXX.h:2604
Represents a delete expression for memory deallocation and destructor calls, e.g.
Definition ExprCXX.h:2628
Represents a C++ destructor within a class.
Definition DeclCXX.h:2869
Represents a static or instance method of a struct/union/class.
Definition DeclCXX.h:2129
const CXXRecordDecl * getParent() const
Return the parent of this method declaration, which is the class in which this method is defined.
Definition DeclCXX.h:2255
Represents a new-expression for memory allocation and constructor calls, e.g: "new CXXNewExpr(foo)".
Definition ExprCXX.h:2357
Represents a C++ struct/union/class.
Definition DeclCXX.h:258
A C++ throw-expression (C++ [except.throw]).
Definition ExprCXX.h:1209
CharUnits - This is an opaque type for sizes expressed in character units.
Definition CharUnits.h:38
GlobalDecl - represents a global declaration.
Definition GlobalDecl.h:57
MangleContext - Context for tracking state which persists across multiple calls to the C++ name mangl...
Definition Mangle.h:52
A (possibly-)qualified type.
Definition TypeBase.h:937
Encodes a location in the source.
Represents a variable declaration or definition.
Definition Decl.h:926
CIRGenCXXABI * CreateCIRGenItaniumCXXABI(CIRGenModule &cgm)
Creates and Itanium-family ABI.
const internal::VariadicAllOfMatcher< Type > type
Matches Types in the clang AST.
CXXCtorType
C++ constructor types.
Definition ABI.h:24
GVALinkage
A more specific kind of linkage than enum Linkage.
Definition Linkage.h:72
CXXDtorType
C++ destructor types.
Definition ABI.h:34
Similar to AddedStructorArgs, but only notes the number of additional arguments.
static AddedStructorArgCounts withSuffix(unsigned n)
static AddedStructorArgCounts withPrefix(unsigned n)
static AddedStructorArgs withPrefix(llvm::SmallVector< Arg, 1 > args)
static AddedStructorArgs withSuffix(llvm::SmallVector< Arg, 1 > args)
AddedStructorArgs(llvm::SmallVector< Arg, 1 > p, llvm::SmallVector< Arg, 1 > s)
The MS C++ ABI needs a pointer to RTTI data plus some flags to describe the type of a catch handler,...