Debian Codegraph @

clang/lib/CIR/CodeGen/CIRGenCXXABI.h

raw
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
24 namespace clang::CIRGen {
25
26 /// Implements C++ ABI-specific code generation functions.
27 class CIRGenCXXABI {
28 protected:
29 CIRGenModule &cgm;
30 std::unique_ptr<clang::MangleContext> mangleContext;
31
32 virtual bool requiresArrayCookie(const CXXNewExpr *e);
33
34 public:
35 // TODO(cir): make this protected when target-specific CIRGenCXXABIs are
36 // implemented.
37 CIRGenCXXABI(CIRGenModule &cgm)
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
46 initializeHiddenVirtualInheritanceMembers(CIRGenFunction &cgf,
47 const CXXRecordDecl *rd) {}
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
64 public:
65 /// Similar to AddedStructorArgs, but only notes the number of additional
66 /// arguments.
67 struct AddedStructorArgCounts {
68 unsigned prefix = 0;
69 unsigned suffix = 0;
70 AddedStructorArgCounts() = default;
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.
81 struct AddedStructorArgs {
82 struct Arg {
83 mlir::Value value;
84 QualType type;
85 };
86 llvm::SmallVector<Arg, 1> prefix;
87 llvm::SmallVector<Arg, 1> suffix;
88 AddedStructorArgs() = default;
89 AddedStructorArgs(llvm::SmallVector<Arg, 1> p, llvm::SmallVector<Arg, 1> s)
90 : prefix(std::move(p)), suffix(std::move(s)) {}
91 static AddedStructorArgs withPrefix(llvm::SmallVector<Arg, 1> args) {
92 return {std::move(args), {}};
93 }
94 static AddedStructorArgs withSuffix(llvm::SmallVector<Arg, 1> args) {
95 return {{}, std::move(args)};
96 }
97 };
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
103 buildStructorSignature(GlobalDecl gd,
104 llvm::SmallVectorImpl<CanQualType> &argTys) = 0;
105
106 AddedStructorArgCounts
107 addImplicitConstructorArgs(CIRGenFunction &cgf, const CXXConstructorDecl *d,
108 CXXCtorType type, bool forVirtualBase,
109 bool delegating, CallArgList &args);
110
111 clang::ImplicitParamDecl *getThisDecl(CIRGenFunction &cgf) {
112 return cgf.cxxabiThisDecl;
113 }
114
115 virtual AddedStructorArgs
116 getImplicitConstructorArgs(CIRGenFunction &cgf, const CXXConstructorDecl *d,
117 CXXCtorType type, bool forVirtualBase,
118 bool delegating) = 0;
119
120 /// Emit the ABI-specific prolog for the function
121 virtual void emitInstanceFunctionProlog(SourceLocation loc,
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 void emitBeginCatch(CIRGenFunction &cgf,
130 const CXXCatchStmt *catchStmt) = 0;
131
132 virtual mlir::Attribute getAddrOfRTTIDescriptor(mlir::Location loc,
133 QualType ty) = 0;
134
135 /// Get the type of the implicit "this" parameter used by a method. May return
136 /// zero if no specific type is applicable, e.g. if the ABI expects the "this"
137 /// parameter to point to some artificial offset in a complete object due to
138 /// vbases being reordered.
139 virtual const clang::CXXRecordDecl *
140 getThisArgumentTypeForMethod(const clang::CXXMethodDecl *md) {
141 return md->getParent();
142 }
143
144 /// Return whether the given global decl needs a VTT (virtual table table)
145 /// parameter.
146 virtual bool needsVTTParameter(clang::GlobalDecl gd) { return false; }
147
148 /// Perform ABI-specific "this" argument adjustment required prior to
149 /// a call of a virtual function.
150 /// The "VirtualCall" argument is true iff the call itself is virtual.
151 virtual Address adjustThisArgumentForVirtualFunctionCall(CIRGenFunction &cgf,
152 clang::GlobalDecl gd,
153 Address thisPtr,
154 bool virtualCall) {
155 return thisPtr;
156 }
157
158 /// Build a parameter variable suitable for 'this'.
159 void buildThisParam(CIRGenFunction &cgf, FunctionArgList &params);
160
161 /// Loads the incoming C++ this pointer as it was passed by the caller.
162 mlir::Value loadIncomingCXXThis(CIRGenFunction &cgf);
163
164 virtual CatchTypeInfo
165 getAddrOfCXXCatchHandlerType(mlir::Location loc, QualType ty,
166 QualType catchHandlerType) = 0;
167 virtual CatchTypeInfo getCatchAllTypeInfo();
168
169 /// Get the implicit (second) parameter that comes after the "this" pointer,
170 /// or nullptr if there is isn't one.
171 virtual mlir::Value getCXXDestructorImplicitParam(CIRGenFunction &cgf,
172 const CXXDestructorDecl *dd,
173 CXXDtorType type,
174 bool forVirtualBase,
175 bool delegating) = 0;
176
177 /// Emit constructor variants required by this ABI.
178 virtual void emitCXXConstructors(const clang::CXXConstructorDecl *d) = 0;
179
180 /// Emit dtor variants required by this ABI.
181 virtual void emitCXXDestructors(const clang::CXXDestructorDecl *d) = 0;
182
183 virtual void emitDestructorCall(CIRGenFunction &cgf,
184 const CXXDestructorDecl *dd, CXXDtorType type,
185 bool forVirtualBase, bool delegating,
186 Address thisAddr, QualType thisTy) = 0;
187
188 /// Emit code to force the execution of a destructor during global
189 /// teardown. The default implementation of this uses atexit.
190 ///
191 /// \param dtor - a function taking a single pointer argument
192 /// \param addr - a pointer to pass to the destructor function.
193 virtual void registerGlobalDtor(const VarDecl *vd, cir::FuncOp dtor,
194 mlir::Value addr) = 0;
195
196 virtual void emitVirtualObjectDelete(CIRGenFunction &cgf,
197 const CXXDeleteExpr *de, Address ptr,
198 QualType elementType,
199 const CXXDestructorDecl *dtor) = 0;
200
201 virtual size_t getSrcArgforCopyCtor(const CXXConstructorDecl *,
202 FunctionArgList &args) const = 0;
203
204 /// Checks if ABI requires extra virtual offset for vtable field.
205 virtual bool
206 isVirtualOffsetNeededForVTableField(CIRGenFunction &cgf,
207 CIRGenFunction::VPtr vptr) = 0;
208
209 /// Emits the VTable definitions required for the given record type.
210 virtual void emitVTableDefinitions(CIRGenVTables &cgvt,
211 const CXXRecordDecl *rd) = 0;
212
213 using DeleteOrMemberCallExpr =
214 llvm::PointerUnion<const CXXDeleteExpr *, const CXXMemberCallExpr *>;
215
216 virtual mlir::Value emitVirtualDestructorCall(CIRGenFunction &cgf,
217 const CXXDestructorDecl *dtor,
218 CXXDtorType dtorType,
219 Address thisAddr,
220 DeleteOrMemberCallExpr e) = 0;
221
222 /// Emit any tables needed to implement virtual inheritance. For Itanium,
223 /// this emits virtual table tables.
224 virtual void emitVirtualInheritanceTables(const CXXRecordDecl *rd) = 0;
225
226 /// Returns true if the given destructor type should be emitted as a linkonce
227 /// delegating thunk, regardless of whether the dtor is defined in this TU or
228 /// not.
229 virtual bool useThunkForDtorVariant(const CXXDestructorDecl *dtor,
230 CXXDtorType dt) const = 0;
231
232 virtual cir::GlobalLinkageKind
233 getCXXDestructorLinkage(GVALinkage linkage, const CXXDestructorDecl *dtor,
234 CXXDtorType dt) const;
235
236 /// Get the address of the vtable for the given record decl which should be
237 /// used for the vptr at the given offset in RD.
238 virtual cir::GlobalOp getAddrOfVTable(const CXXRecordDecl *rd,
239 CharUnits vptrOffset) = 0;
240
241 /// Build a virtual function pointer in the ABI-specific way.
242 virtual CIRGenCallee getVirtualFunctionPointer(CIRGenFunction &cgf,
243 clang::GlobalDecl gd,
244 Address thisAddr,
245 mlir::Type ty,
246 SourceLocation loc) = 0;
247
248 /// Get the address point of the vtable for the given base subobject.
249 virtual mlir::Value
250 getVTableAddressPoint(BaseSubobject base,
251 const CXXRecordDecl *vtableClass) = 0;
252
253 /// Get the address point of the vtable for the given base subobject while
254 /// building a constructor or a destructor.
255 virtual mlir::Value getVTableAddressPointInStructor(
256 CIRGenFunction &cgf, const CXXRecordDecl *vtableClass, BaseSubobject base,
257 const CXXRecordDecl *nearestVBase) = 0;
258
259 /// Insert any ABI-specific implicit parameters into the parameter list for a
260 /// function. This generally involves extra data for constructors and
261 /// destructors.
262 ///
263 /// ABIs may also choose to override the return type, which has been
264 /// initialized with the type of 'this' if HasThisReturn(CGF.CurGD) is true or
265 /// the formal return type of the function otherwise.
266 virtual void addImplicitStructorParams(CIRGenFunction &cgf,
267 clang::QualType &resTy,
268 FunctionArgList &params) = 0;
269
270 /// Checks if ABI requires to initialize vptrs for given dynamic class.
271 virtual bool
272 doStructorsInitializeVPtrs(const clang::CXXRecordDecl *vtableClass) = 0;
273
274 /// Returns true if the given constructor or destructor is one of the kinds
275 /// that the ABI says returns 'this' (only applies when called non-virtually
276 /// for destructors).
277 ///
278 /// There currently is no way to indicate if a destructor returns 'this' when
279 /// called virtually, and CIR generation does not support this case.
280 virtual bool hasThisReturn(clang::GlobalDecl gd) const { return false; }
281
282 virtual bool hasMostDerivedReturn(clang::GlobalDecl gd) const {
283 return false;
284 }
285
286 /// Returns true if the target allows calling a function through a pointer
287 /// with a different signature than the actual function (or equivalently,
288 /// bitcasting a function or function pointer to a different function type).
289 /// In principle in the most general case this could depend on the target, the
290 /// calling convention, and the actual types of the arguments and return
291 /// value. Here it just means whether the signature mismatch could *ever* be
292 /// allowed; in other words, does the target do strict checking of signatures
293 /// for all calls.
294 virtual bool canCallMismatchedFunctionType() const { return true; }
295
296 /// Gets the mangle context.
297 clang::MangleContext &getMangleContext() { return *mangleContext; }
298
299 clang::ImplicitParamDecl *&getStructorImplicitParamDecl(CIRGenFunction &cgf) {
300 return cgf.cxxStructorImplicitParamDecl;
301 }
302
303 mlir::Value getStructorImplicitParamValue(CIRGenFunction &cgf) {
304 return cgf.cxxStructorImplicitParamValue;
305 }
306
307 void setStructorImplicitParamValue(CIRGenFunction &cgf, mlir::Value val) {
308 cgf.cxxStructorImplicitParamValue = val;
309 }
310
311 /**************************** Array cookies ******************************/
312
313 /// Returns the extra size required in order to store the array
314 /// cookie for the given new-expression. May return 0 to indicate that no
315 /// array cookie is required.
316 ///
317 /// Several cases are filtered out before this method is called:
318 /// - non-array allocations never need a cookie
319 /// - calls to \::operator new(size_t, void*) never need a cookie
320 ///
321 /// \param e - the new-expression being allocated.
322 virtual CharUnits getArrayCookieSize(const CXXNewExpr *e);
323
324 /// Initialize the array cookie for the given allocation.
325 ///
326 /// \param newPtr - a char* which is the presumed-non-null
327 /// return value of the allocation function
328 /// \param numElements - the computed number of elements,
329 /// potentially collapsed from the multidimensional array case;
330 /// always a size_t
331 /// \param elementType - the base element allocated type,
332 /// i.e. the allocated type after stripping all array types
333 virtual Address initializeArrayCookie(CIRGenFunction &cgf, Address newPtr,
334 mlir::Value numElements,
335 const CXXNewExpr *e,
336 QualType elementType) = 0;
337
338 protected:
339 /// Returns the extra size required in order to store the array
340 /// cookie for the given type. Assumes that an array cookie is
341 /// required.
342 virtual CharUnits getArrayCookieSizeImpl(QualType elementType) = 0;
343 };
344
345 /// Creates and Itanium-family ABI
346 CIRGenCXXABI *CreateCIRGenItaniumCXXABI(CIRGenModule &cgm);
347
348 } // namespace clang::CIRGen
349
350 #endif