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 "CIRGenFunction.h"
19#include "CIRGenModule.h"
20
21#include "clang/AST/Mangle.h"
22
23namespace clang::CIRGen {
24
25/// Implements C++ ABI-specific code generation functions.
27protected:
29 std::unique_ptr<clang::MangleContext> mangleContext;
30
31 virtual bool requiresArrayCookie(const CXXNewExpr *e);
32
33public:
34 // TODO(cir): make this protected when target-specific CIRGenCXXABIs are
35 // implemented.
37 : cgm(cgm), mangleContext(cgm.getASTContext().createMangleContext()) {}
38 virtual ~CIRGenCXXABI();
39
40 void setCXXABIThisValue(CIRGenFunction &cgf, mlir::Value thisPtr);
41
42 /// Emit the code to initialize hidden members required to handle virtual
43 /// inheritance, if needed by the ABI.
44 virtual void
47
48 /// Emit a single constructor/destructor with the gen type from a C++
49 /// constructor/destructor Decl.
50 virtual void emitCXXStructor(clang::GlobalDecl gd) = 0;
51
52 virtual mlir::Value
53 getVirtualBaseClassOffset(mlir::Location loc, CIRGenFunction &cgf,
54 Address thisAddr, const CXXRecordDecl *classDecl,
55 const CXXRecordDecl *baseClassDecl) = 0;
56
57 virtual mlir::Value emitDynamicCast(CIRGenFunction &cgf, mlir::Location loc,
58 QualType srcRecordTy,
59 QualType destRecordTy,
60 cir::PointerType destCIRTy,
61 bool isRefCast, Address src) = 0;
62
63public:
64 /// Similar to AddedStructorArgs, but only notes the number of additional
65 /// arguments.
67 unsigned prefix = 0;
68 unsigned suffix = 0;
70 AddedStructorArgCounts(unsigned p, unsigned s) : prefix(p), suffix(s) {}
71 static AddedStructorArgCounts withPrefix(unsigned n) { return {n, 0}; }
72 static AddedStructorArgCounts withSuffix(unsigned n) { return {0, n}; }
73 };
74
75 /// Additional implicit arguments to add to the beginning (Prefix) and end
76 /// (Suffix) of a constructor / destructor arg list.
77 ///
78 /// Note that Prefix should actually be inserted *after* the first existing
79 /// arg; `this` arguments always come first.
97
98 /// Build the signature of the given constructor or destructor vairant by
99 /// adding any required parameters. For convenience, ArgTys has been
100 /// initialized with the type of 'this'.
101 virtual AddedStructorArgCounts
104
107 CXXCtorType type, bool forVirtualBase,
108 bool delegating, CallArgList &args);
109
113
114 virtual AddedStructorArgs
116 CXXCtorType type, bool forVirtualBase,
117 bool delegating) = 0;
118
119 /// Emit the ABI-specific prolog for the function
121 CIRGenFunction &cgf) = 0;
122
123 virtual void emitRethrow(CIRGenFunction &cgf, bool isNoReturn) = 0;
124 virtual void emitThrow(CIRGenFunction &cgf, const CXXThrowExpr *e) = 0;
125
126 virtual mlir::Attribute getAddrOfRTTIDescriptor(mlir::Location loc,
127 QualType ty) = 0;
128
129 /// Get the type of the implicit "this" parameter used by a method. May return
130 /// zero if no specific type is applicable, e.g. if the ABI expects the "this"
131 /// parameter to point to some artificial offset in a complete object due to
132 /// vbases being reordered.
133 virtual const clang::CXXRecordDecl *
135 return md->getParent();
136 }
137
138 /// Return whether the given global decl needs a VTT (virtual table table)
139 /// parameter.
140 virtual bool needsVTTParameter(clang::GlobalDecl gd) { return false; }
141
142 /// Perform ABI-specific "this" argument adjustment required prior to
143 /// a call of a virtual function.
144 /// The "VirtualCall" argument is true iff the call itself is virtual.
147 Address thisPtr,
148 bool virtualCall) {
149 return thisPtr;
150 }
151
152 /// Build a parameter variable suitable for 'this'.
154
155 /// Loads the incoming C++ this pointer as it was passed by the caller.
156 mlir::Value loadIncomingCXXThis(CIRGenFunction &cgf);
157
158 /// Get the implicit (second) parameter that comes after the "this" pointer,
159 /// or nullptr if there is isn't one.
161 const CXXDestructorDecl *dd,
163 bool forVirtualBase,
164 bool delegating) = 0;
165
166 /// Emit constructor variants required by this ABI.
168
169 /// Emit dtor variants required by this ABI.
171
174 bool forVirtualBase, bool delegating,
175 Address thisAddr, QualType thisTy) = 0;
176
177 /// Emit code to force the execution of a destructor during global
178 /// teardown. The default implementation of this uses atexit.
179 ///
180 /// \param dtor - a function taking a single pointer argument
181 /// \param addr - a pointer to pass to the destructor function.
182 virtual void registerGlobalDtor(const VarDecl *vd, cir::FuncOp dtor,
183 mlir::Value addr) = 0;
184
185 /// Checks if ABI requires extra virtual offset for vtable field.
186 virtual bool
188 CIRGenFunction::VPtr vptr) = 0;
189
190 /// Emits the VTable definitions required for the given record type.
192 const CXXRecordDecl *rd) = 0;
193
195 llvm::PointerUnion<const CXXDeleteExpr *, const CXXMemberCallExpr *>;
196
198 const CXXDestructorDecl *dtor,
199 CXXDtorType dtorType,
200 Address thisAddr,
202
203 /// Emit any tables needed to implement virtual inheritance. For Itanium,
204 /// this emits virtual table tables.
205 virtual void emitVirtualInheritanceTables(const CXXRecordDecl *rd) = 0;
206
207 /// Returns true if the given destructor type should be emitted as a linkonce
208 /// delegating thunk, regardless of whether the dtor is defined in this TU or
209 /// not.
211 CXXDtorType dt) const = 0;
212
213 virtual cir::GlobalLinkageKind
215 CXXDtorType dt) const;
216
217 /// Get the address of the vtable for the given record decl which should be
218 /// used for the vptr at the given offset in RD.
219 virtual cir::GlobalOp getAddrOfVTable(const CXXRecordDecl *rd,
220 CharUnits vptrOffset) = 0;
221
222 /// Build a virtual function pointer in the ABI-specific way.
225 Address thisAddr,
226 mlir::Type ty,
227 SourceLocation loc) = 0;
228
229 /// Get the address point of the vtable for the given base subobject.
230 virtual mlir::Value
232 const CXXRecordDecl *vtableClass) = 0;
233
234 /// Get the address point of the vtable for the given base subobject while
235 /// building a constructor or a destructor.
237 CIRGenFunction &cgf, const CXXRecordDecl *vtableClass, BaseSubobject base,
238 const CXXRecordDecl *nearestVBase) = 0;
239
240 /// Insert any ABI-specific implicit parameters into the parameter list for a
241 /// function. This generally involves extra data for constructors and
242 /// destructors.
243 ///
244 /// ABIs may also choose to override the return type, which has been
245 /// initialized with the type of 'this' if HasThisReturn(CGF.CurGD) is true or
246 /// the formal return type of the function otherwise.
248 clang::QualType &resTy,
249 FunctionArgList &params) = 0;
250
251 /// Checks if ABI requires to initialize vptrs for given dynamic class.
252 virtual bool
254
255 /// Returns true if the given constructor or destructor is one of the kinds
256 /// that the ABI says returns 'this' (only applies when called non-virtually
257 /// for destructors).
258 ///
259 /// There currently is no way to indicate if a destructor returns 'this' when
260 /// called virtually, and CIR generation does not support this case.
261 virtual bool hasThisReturn(clang::GlobalDecl gd) const { return false; }
262
264 return false;
265 }
266
267 /// Returns true if the target allows calling a function through a pointer
268 /// with a different signature than the actual function (or equivalently,
269 /// bitcasting a function or function pointer to a different function type).
270 /// In principle in the most general case this could depend on the target, the
271 /// calling convention, and the actual types of the arguments and return
272 /// value. Here it just means whether the signature mismatch could *ever* be
273 /// allowed; in other words, does the target do strict checking of signatures
274 /// for all calls.
275 virtual bool canCallMismatchedFunctionType() const { return true; }
276
277 /// Gets the mangle context.
279
283
287
288 void setStructorImplicitParamValue(CIRGenFunction &cgf, mlir::Value val) {
290 }
291
292 /**************************** Array cookies ******************************/
293
294 /// Returns the extra size required in order to store the array
295 /// cookie for the given new-expression. May return 0 to indicate that no
296 /// array cookie is required.
297 ///
298 /// Several cases are filtered out before this method is called:
299 /// - non-array allocations never need a cookie
300 /// - calls to \::operator new(size_t, void*) never need a cookie
301 ///
302 /// \param E - the new-expression being allocated.
303 virtual CharUnits getArrayCookieSize(const CXXNewExpr *e);
304};
305
306/// Creates and Itanium-family ABI
307CIRGenCXXABI *CreateCIRGenItaniumCXXABI(CIRGenModule &cgm);
308
309} // namespace clang::CIRGen
310
311#endif
__device__ __2f16 float __ockl_bool s
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 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.
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 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 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 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)