Coverage Report

Created: 2022-05-17 06:19

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/CodeGen/CGCXXABI.h
Line
Count
Source (jump to first uncovered line)
1
//===----- CGCXXABI.h - Interface to C++ ABIs -------------------*- C++ -*-===//
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_CODEGEN_CGCXXABI_H
15
#define LLVM_CLANG_LIB_CODEGEN_CGCXXABI_H
16
17
#include "CodeGenFunction.h"
18
#include "clang/Basic/LLVM.h"
19
#include "clang/CodeGen/CodeGenABITypes.h"
20
21
namespace llvm {
22
class Constant;
23
class Type;
24
class Value;
25
class CallInst;
26
}
27
28
namespace clang {
29
class CastExpr;
30
class CXXConstructorDecl;
31
class CXXDestructorDecl;
32
class CXXMethodDecl;
33
class CXXRecordDecl;
34
class MangleContext;
35
36
namespace CodeGen {
37
class CGCallee;
38
class CodeGenFunction;
39
class CodeGenModule;
40
struct CatchTypeInfo;
41
42
/// Implements C++ ABI-specific code generation functions.
43
class CGCXXABI {
44
protected:
45
  CodeGenModule &CGM;
46
  std::unique_ptr<MangleContext> MangleCtx;
47
48
  CGCXXABI(CodeGenModule &CGM)
49
35.6k
    : CGM(CGM), MangleCtx(CGM.getContext().createMangleContext()) {}
50
51
protected:
52
205k
  ImplicitParamDecl *getThisDecl(CodeGenFunction &CGF) {
53
205k
    return CGF.CXXABIThisDecl;
54
205k
  }
55
2.13k
  llvm::Value *getThisValue(CodeGenFunction &CGF) {
56
2.13k
    return CGF.CXXABIThisValue;
57
2.13k
  }
58
899
  Address getThisAddress(CodeGenFunction &CGF) {
59
899
    return Address(
60
899
        CGF.CXXABIThisValue,
61
899
        CGF.ConvertTypeForMem(CGF.CXXABIThisDecl->getType()->getPointeeType()),
62
899
        CGF.CXXABIThisAlignment);
63
899
  }
64
65
  /// Issue a diagnostic about unsupported features in the ABI.
66
  void ErrorUnsupportedABI(CodeGenFunction &CGF, StringRef S);
67
68
  /// Get a null value for unsupported member pointers.
69
  llvm::Constant *GetBogusMemberPointer(QualType T);
70
71
101k
  ImplicitParamDecl *&getStructorImplicitParamDecl(CodeGenFunction &CGF) {
72
101k
    return CGF.CXXStructorImplicitParamDecl;
73
101k
  }
74
1.43k
  llvm::Value *&getStructorImplicitParamValue(CodeGenFunction &CGF) {
75
1.43k
    return CGF.CXXStructorImplicitParamValue;
76
1.43k
  }
77
78
  /// Loads the incoming C++ this pointer as it was passed by the caller.
79
  llvm::Value *loadIncomingCXXThis(CodeGenFunction &CGF);
80
81
  void setCXXABIThisValue(CodeGenFunction &CGF, llvm::Value *ThisPtr);
82
83
397k
  ASTContext &getContext() const { return CGM.getContext(); }
84
85
  bool mayNeedDestruction(const VarDecl *VD) const;
86
87
  /// Determine whether we will definitely emit this variable with a constant
88
  /// initializer, either because the language semantics demand it or because
89
  /// we know that the initializer is a constant.
90
  // For weak definitions, any initializer available in the current translation
91
  // is not necessarily reflective of the initializer used; such initializers
92
  // are ignored unless if InspectInitForWeakDef is true.
93
  bool
94
  isEmittedWithConstantInitializer(const VarDecl *VD,
95
                                   bool InspectInitForWeakDef = false) const;
96
97
  virtual bool requiresArrayCookie(const CXXDeleteExpr *E, QualType eltType);
98
  virtual bool requiresArrayCookie(const CXXNewExpr *E);
99
100
  /// Determine whether there's something special about the rules of
101
  /// the ABI tell us that 'this' is a complete object within the
102
  /// given function.  Obvious common logic like being defined on a
103
  /// final class will have been taken care of by the caller.
104
  virtual bool isThisCompleteObject(GlobalDecl GD) const = 0;
105
106
public:
107
108
  virtual ~CGCXXABI();
109
110
  /// Gets the mangle context.
111
3.56M
  MangleContext &getMangleContext() {
112
3.56M
    return *MangleCtx;
113
3.56M
  }
114
115
  /// Returns true if the given constructor or destructor is one of the
116
  /// kinds that the ABI says returns 'this' (only applies when called
117
  /// non-virtually for destructors).
118
  ///
119
  /// There currently is no way to indicate if a destructor returns 'this'
120
  /// when called virtually, and code generation does not support the case.
121
812k
  virtual bool HasThisReturn(GlobalDecl GD) const { return false; }
122
123
433k
  virtual bool hasMostDerivedReturn(GlobalDecl GD) const { return false; }
124
125
36.3k
  virtual bool useSinitAndSterm() const { return false; }
126
127
  /// Returns true if the target allows calling a function through a pointer
128
  /// with a different signature than the actual function (or equivalently,
129
  /// bitcasting a function or function pointer to a different function type).
130
  /// In principle in the most general case this could depend on the target, the
131
  /// calling convention, and the actual types of the arguments and return
132
  /// value. Here it just means whether the signature mismatch could *ever* be
133
  /// allowed; in other words, does the target do strict checking of signatures
134
  /// for all calls.
135
7
  virtual bool canCallMismatchedFunctionType() const { return true; }
136
137
  /// If the C++ ABI requires the given type be returned in a particular way,
138
  /// this method sets RetAI and returns true.
139
  virtual bool classifyReturnType(CGFunctionInfo &FI) const = 0;
140
141
  /// Specify how one should pass an argument of a record type.
142
  enum RecordArgABI {
143
    /// Pass it using the normal C aggregate rules for the ABI, potentially
144
    /// introducing extra copies and passing some or all of it in registers.
145
    RAA_Default = 0,
146
147
    /// Pass it on the stack using its defined layout.  The argument must be
148
    /// evaluated directly into the correct stack position in the arguments area,
149
    /// and the call machinery must not move it or introduce extra copies.
150
    RAA_DirectInMemory,
151
152
    /// Pass it as a pointer to temporary memory.
153
    RAA_Indirect
154
  };
155
156
  /// Returns how an argument of the given record type should be passed.
157
  virtual RecordArgABI getRecordArgABI(const CXXRecordDecl *RD) const = 0;
158
159
  /// Returns true if the implicit 'sret' parameter comes after the implicit
160
  /// 'this' parameter of C++ instance methods.
161
0
  virtual bool isSRetParameterAfterThis() const { return false; }
162
163
  /// Returns true if the ABI permits the argument to be a homogeneous
164
  /// aggregate.
165
  virtual bool
166
433
  isPermittedToBeHomogeneousAggregate(const CXXRecordDecl *RD) const {
167
433
    return true;
168
433
  };
169
170
  /// Find the LLVM type used to represent the given member pointer
171
  /// type.
172
  virtual llvm::Type *
173
  ConvertMemberPointerType(const MemberPointerType *MPT);
174
175
  /// Load a member function from an object and a member function
176
  /// pointer.  Apply the this-adjustment and set 'This' to the
177
  /// adjusted value.
178
  virtual CGCallee EmitLoadOfMemberFunctionPointer(
179
      CodeGenFunction &CGF, const Expr *E, Address This,
180
      llvm::Value *&ThisPtrForCall, llvm::Value *MemPtr,
181
      const MemberPointerType *MPT);
182
183
  /// Calculate an l-value from an object and a data member pointer.
184
  virtual llvm::Value *
185
  EmitMemberDataPointerAddress(CodeGenFunction &CGF, const Expr *E,
186
                               Address Base, llvm::Value *MemPtr,
187
                               const MemberPointerType *MPT);
188
189
  /// Perform a derived-to-base, base-to-derived, or bitcast member
190
  /// pointer conversion.
191
  virtual llvm::Value *EmitMemberPointerConversion(CodeGenFunction &CGF,
192
                                                   const CastExpr *E,
193
                                                   llvm::Value *Src);
194
195
  /// Perform a derived-to-base, base-to-derived, or bitcast member
196
  /// pointer conversion on a constant value.
197
  virtual llvm::Constant *EmitMemberPointerConversion(const CastExpr *E,
198
                                                      llvm::Constant *Src);
199
200
  /// Return true if the given member pointer can be zero-initialized
201
  /// (in the C++ sense) with an LLVM zeroinitializer.
202
  virtual bool isZeroInitializable(const MemberPointerType *MPT);
203
204
  /// Return whether or not a member pointers type is convertible to an IR type.
205
1.08k
  virtual bool isMemberPointerConvertible(const MemberPointerType *MPT) const {
206
1.08k
    return true;
207
1.08k
  }
208
209
  /// Create a null member pointer of the given type.
210
  virtual llvm::Constant *EmitNullMemberPointer(const MemberPointerType *MPT);
211
212
  /// Create a member pointer for the given method.
213
  virtual llvm::Constant *EmitMemberFunctionPointer(const CXXMethodDecl *MD);
214
215
  /// Create a member pointer for the given field.
216
  virtual llvm::Constant *EmitMemberDataPointer(const MemberPointerType *MPT,
217
                                                CharUnits offset);
218
219
  /// Create a member pointer for the given member pointer constant.
220
  virtual llvm::Constant *EmitMemberPointer(const APValue &MP, QualType MPT);
221
222
  /// Emit a comparison between two member pointers.  Returns an i1.
223
  virtual llvm::Value *
224
  EmitMemberPointerComparison(CodeGenFunction &CGF,
225
                              llvm::Value *L,
226
                              llvm::Value *R,
227
                              const MemberPointerType *MPT,
228
                              bool Inequality);
229
230
  /// Determine if a member pointer is non-null.  Returns an i1.
231
  virtual llvm::Value *
232
  EmitMemberPointerIsNotNull(CodeGenFunction &CGF,
233
                             llvm::Value *MemPtr,
234
                             const MemberPointerType *MPT);
235
236
protected:
237
  /// A utility method for computing the offset required for the given
238
  /// base-to-derived or derived-to-base member-pointer conversion.
239
  /// Does not handle virtual conversions (in case we ever fully
240
  /// support an ABI that allows this).  Returns null if no adjustment
241
  /// is required.
242
  llvm::Constant *getMemberPointerAdjustment(const CastExpr *E);
243
244
public:
245
  virtual void emitVirtualObjectDelete(CodeGenFunction &CGF,
246
                                       const CXXDeleteExpr *DE,
247
                                       Address Ptr, QualType ElementType,
248
                                       const CXXDestructorDecl *Dtor) = 0;
249
  virtual void emitRethrow(CodeGenFunction &CGF, bool isNoReturn) = 0;
250
  virtual void emitThrow(CodeGenFunction &CGF, const CXXThrowExpr *E) = 0;
251
0
  virtual llvm::GlobalVariable *getThrowInfo(QualType T) { return nullptr; }
252
253
  /// Determine whether it's possible to emit a vtable for \p RD, even
254
  /// though we do not know that the vtable has been marked as used by semantic
255
  /// analysis.
256
  virtual bool canSpeculativelyEmitVTable(const CXXRecordDecl *RD) const = 0;
257
258
  virtual void emitBeginCatch(CodeGenFunction &CGF, const CXXCatchStmt *C) = 0;
259
260
  virtual llvm::CallInst *
261
  emitTerminateForUnexpectedException(CodeGenFunction &CGF,
262
                                      llvm::Value *Exn);
263
264
  virtual llvm::Constant *getAddrOfRTTIDescriptor(QualType Ty) = 0;
265
  virtual CatchTypeInfo
266
  getAddrOfCXXCatchHandlerType(QualType Ty, QualType CatchHandlerType) = 0;
267
  virtual CatchTypeInfo getCatchAllTypeInfo();
268
269
  virtual bool shouldTypeidBeNullChecked(bool IsDeref,
270
                                         QualType SrcRecordTy) = 0;
271
  virtual void EmitBadTypeidCall(CodeGenFunction &CGF) = 0;
272
  virtual llvm::Value *EmitTypeid(CodeGenFunction &CGF, QualType SrcRecordTy,
273
                                  Address ThisPtr,
274
                                  llvm::Type *StdTypeInfoPtrTy) = 0;
275
276
  virtual bool shouldDynamicCastCallBeNullChecked(bool SrcIsPtr,
277
                                                  QualType SrcRecordTy) = 0;
278
279
  virtual llvm::Value *
280
  EmitDynamicCastCall(CodeGenFunction &CGF, Address Value,
281
                      QualType SrcRecordTy, QualType DestTy,
282
                      QualType DestRecordTy, llvm::BasicBlock *CastEnd) = 0;
283
284
  virtual llvm::Value *EmitDynamicCastToVoid(CodeGenFunction &CGF,
285
                                             Address Value,
286
                                             QualType SrcRecordTy,
287
                                             QualType DestTy) = 0;
288
289
  virtual bool EmitBadCastCall(CodeGenFunction &CGF) = 0;
290
291
  virtual llvm::Value *GetVirtualBaseClassOffset(CodeGenFunction &CGF,
292
                                                 Address This,
293
                                                 const CXXRecordDecl *ClassDecl,
294
                                        const CXXRecordDecl *BaseClassDecl) = 0;
295
296
  virtual llvm::BasicBlock *EmitCtorCompleteObjectHandler(CodeGenFunction &CGF,
297
                                                          const CXXRecordDecl *RD);
298
299
  /// Emit the code to initialize hidden members required
300
  /// to handle virtual inheritance, if needed by the ABI.
301
  virtual void
302
  initializeHiddenVirtualInheritanceMembers(CodeGenFunction &CGF,
303
408
                                            const CXXRecordDecl *RD) {}
304
305
  /// Emit constructor variants required by this ABI.
306
  virtual void EmitCXXConstructors(const CXXConstructorDecl *D) = 0;
307
308
  /// Additional implicit arguments to add to the beginning (Prefix) and end
309
  /// (Suffix) of a constructor / destructor arg list.
310
  ///
311
  /// Note that Prefix should actually be inserted *after* the first existing
312
  /// arg; `this` arguments always come first.
313
  struct AddedStructorArgs {
314
    struct Arg {
315
      llvm::Value *Value;
316
      QualType Type;
317
    };
318
    SmallVector<Arg, 1> Prefix;
319
    SmallVector<Arg, 1> Suffix;
320
53.7k
    AddedStructorArgs() = default;
321
    AddedStructorArgs(SmallVector<Arg, 1> P, SmallVector<Arg, 1> S)
322
606
        : Prefix(std::move(P)), Suffix(std::move(S)) {}
323
136
    static AddedStructorArgs prefix(SmallVector<Arg, 1> Args) {
324
136
      return {std::move(Args), {}};
325
136
    }
326
470
    static AddedStructorArgs suffix(SmallVector<Arg, 1> Args) {
327
470
      return {{}, std::move(Args)};
328
470
    }
329
  };
330
331
  /// Similar to AddedStructorArgs, but only notes the number of additional
332
  /// arguments.
333
  struct AddedStructorArgCounts {
334
    unsigned Prefix = 0;
335
    unsigned Suffix = 0;
336
293k
    AddedStructorArgCounts() = default;
337
55.4k
    AddedStructorArgCounts(unsigned P, unsigned S) : Prefix(P), Suffix(S) {}
338
1.09k
    static AddedStructorArgCounts prefix(unsigned N) { return {N, 0}; }
339
0
    static AddedStructorArgCounts suffix(unsigned N) { return {0, N}; }
340
  };
341
342
  /// Build the signature of the given constructor or destructor variant by
343
  /// adding any required parameters.  For convenience, ArgTys has been
344
  /// initialized with the type of 'this'.
345
  virtual AddedStructorArgCounts
346
  buildStructorSignature(GlobalDecl GD,
347
                         SmallVectorImpl<CanQualType> &ArgTys) = 0;
348
349
  /// Returns true if the given destructor type should be emitted as a linkonce
350
  /// delegating thunk, regardless of whether the dtor is defined in this TU or
351
  /// not.
352
  virtual bool useThunkForDtorVariant(const CXXDestructorDecl *Dtor,
353
                                      CXXDtorType DT) const = 0;
354
355
  virtual void setCXXDestructorDLLStorage(llvm::GlobalValue *GV,
356
                                          const CXXDestructorDecl *Dtor,
357
                                          CXXDtorType DT) const;
358
359
  virtual llvm::GlobalValue::LinkageTypes
360
  getCXXDestructorLinkage(GVALinkage Linkage, const CXXDestructorDecl *Dtor,
361
                          CXXDtorType DT) const;
362
363
  /// Emit destructor variants required by this ABI.
364
  virtual void EmitCXXDestructors(const CXXDestructorDecl *D) = 0;
365
366
  /// Get the type of the implicit "this" parameter used by a method. May return
367
  /// zero if no specific type is applicable, e.g. if the ABI expects the "this"
368
  /// parameter to point to some artificial offset in a complete object due to
369
  /// vbases being reordered.
370
  virtual const CXXRecordDecl *
371
318k
  getThisArgumentTypeForMethod(const CXXMethodDecl *MD) {
372
318k
    return MD->getParent();
373
318k
  }
374
375
  /// Perform ABI-specific "this" argument adjustment required prior to
376
  /// a call of a virtual function.
377
  /// The "VirtualCall" argument is true iff the call itself is virtual.
378
  virtual Address
379
  adjustThisArgumentForVirtualFunctionCall(CodeGenFunction &CGF, GlobalDecl GD,
380
1.13k
                                           Address This, bool VirtualCall) {
381
1.13k
    return This;
382
1.13k
  }
383
384
  /// Build a parameter variable suitable for 'this'.
385
  void buildThisParam(CodeGenFunction &CGF, FunctionArgList &Params);
386
387
  /// Insert any ABI-specific implicit parameters into the parameter list for a
388
  /// function.  This generally involves extra data for constructors and
389
  /// destructors.
390
  ///
391
  /// ABIs may also choose to override the return type, which has been
392
  /// initialized with the type of 'this' if HasThisReturn(CGF.CurGD) is true or
393
  /// the formal return type of the function otherwise.
394
  virtual void addImplicitStructorParams(CodeGenFunction &CGF, QualType &ResTy,
395
                                         FunctionArgList &Params) = 0;
396
397
  /// Get the ABI-specific "this" parameter adjustment to apply in the prologue
398
  /// of a virtual function.
399
0
  virtual CharUnits getVirtualFunctionPrologueThisAdjustment(GlobalDecl GD) {
400
0
    return CharUnits::Zero();
401
0
  }
402
403
  /// Emit the ABI-specific prolog for the function.
404
  virtual void EmitInstanceFunctionProlog(CodeGenFunction &CGF) = 0;
405
406
  virtual AddedStructorArgs
407
  getImplicitConstructorArgs(CodeGenFunction &CGF, const CXXConstructorDecl *D,
408
                             CXXCtorType Type, bool ForVirtualBase,
409
                             bool Delegating) = 0;
410
411
  /// Add any ABI-specific implicit arguments needed to call a constructor.
412
  ///
413
  /// \return The number of arguments added at the beginning and end of the
414
  /// call, which is typically zero or one.
415
  AddedStructorArgCounts
416
  addImplicitConstructorArgs(CodeGenFunction &CGF, const CXXConstructorDecl *D,
417
                             CXXCtorType Type, bool ForVirtualBase,
418
                             bool Delegating, CallArgList &Args);
419
420
  /// Get the implicit (second) parameter that comes after the "this" pointer,
421
  /// or nullptr if there is isn't one.
422
  virtual llvm::Value *
423
  getCXXDestructorImplicitParam(CodeGenFunction &CGF,
424
                                const CXXDestructorDecl *DD, CXXDtorType Type,
425
                                bool ForVirtualBase, bool Delegating) = 0;
426
427
  /// Emit the destructor call.
428
  virtual void EmitDestructorCall(CodeGenFunction &CGF,
429
                                  const CXXDestructorDecl *DD, CXXDtorType Type,
430
                                  bool ForVirtualBase, bool Delegating,
431
                                  Address This, QualType ThisTy) = 0;
432
433
  /// Emits the VTable definitions required for the given record type.
434
  virtual void emitVTableDefinitions(CodeGenVTables &CGVT,
435
                                     const CXXRecordDecl *RD) = 0;
436
437
  /// Checks if ABI requires extra virtual offset for vtable field.
438
  virtual bool
439
  isVirtualOffsetNeededForVTableField(CodeGenFunction &CGF,
440
                                      CodeGenFunction::VPtr Vptr) = 0;
441
442
  /// Checks if ABI requires to initialize vptrs for given dynamic class.
443
  virtual bool doStructorsInitializeVPtrs(const CXXRecordDecl *VTableClass) = 0;
444
445
  /// Get the address point of the vtable for the given base subobject.
446
  virtual llvm::Constant *
447
  getVTableAddressPoint(BaseSubobject Base,
448
                        const CXXRecordDecl *VTableClass) = 0;
449
450
  /// Get the address point of the vtable for the given base subobject while
451
  /// building a constructor or a destructor.
452
  virtual llvm::Value *
453
  getVTableAddressPointInStructor(CodeGenFunction &CGF, const CXXRecordDecl *RD,
454
                                  BaseSubobject Base,
455
                                  const CXXRecordDecl *NearestVBase) = 0;
456
457
  /// Get the address point of the vtable for the given base subobject while
458
  /// building a constexpr.
459
  virtual llvm::Constant *
460
  getVTableAddressPointForConstExpr(BaseSubobject Base,
461
                                    const CXXRecordDecl *VTableClass) = 0;
462
463
  /// Get the address of the vtable for the given record decl which should be
464
  /// used for the vptr at the given offset in RD.
465
  virtual llvm::GlobalVariable *getAddrOfVTable(const CXXRecordDecl *RD,
466
                                                CharUnits VPtrOffset) = 0;
467
468
  /// Build a virtual function pointer in the ABI-specific way.
469
  virtual CGCallee getVirtualFunctionPointer(CodeGenFunction &CGF,
470
                                             GlobalDecl GD, Address This,
471
                                             llvm::Type *Ty,
472
                                             SourceLocation Loc) = 0;
473
474
  using DeleteOrMemberCallExpr =
475
      llvm::PointerUnion<const CXXDeleteExpr *, const CXXMemberCallExpr *>;
476
477
  /// Emit the ABI-specific virtual destructor call.
478
  virtual llvm::Value *EmitVirtualDestructorCall(CodeGenFunction &CGF,
479
                                                 const CXXDestructorDecl *Dtor,
480
                                                 CXXDtorType DtorType,
481
                                                 Address This,
482
                                                 DeleteOrMemberCallExpr E) = 0;
483
484
  virtual void adjustCallArgsForDestructorThunk(CodeGenFunction &CGF,
485
                                                GlobalDecl GD,
486
139
                                                CallArgList &CallArgs) {}
487
488
  /// Emit any tables needed to implement virtual inheritance.  For Itanium,
489
  /// this emits virtual table tables.  For the MSVC++ ABI, this emits virtual
490
  /// base tables.
491
  virtual void emitVirtualInheritanceTables(const CXXRecordDecl *RD) = 0;
492
493
  virtual bool exportThunk() = 0;
494
  virtual void setThunkLinkage(llvm::Function *Thunk, bool ForVTable,
495
                               GlobalDecl GD, bool ReturnAdjustment) = 0;
496
497
  virtual llvm::Value *performThisAdjustment(CodeGenFunction &CGF,
498
                                             Address This,
499
                                             const ThisAdjustment &TA) = 0;
500
501
  virtual llvm::Value *performReturnAdjustment(CodeGenFunction &CGF,
502
                                               Address Ret,
503
                                               const ReturnAdjustment &RA) = 0;
504
505
  virtual void EmitReturnFromThunk(CodeGenFunction &CGF,
506
                                   RValue RV, QualType ResultType);
507
508
  virtual size_t getSrcArgforCopyCtor(const CXXConstructorDecl *,
509
                                      FunctionArgList &Args) const = 0;
510
511
  /// Gets the offsets of all the virtual base pointers in a given class.
512
  virtual std::vector<CharUnits> getVBPtrOffsets(const CXXRecordDecl *RD);
513
514
  /// Gets the pure virtual member call function.
515
  virtual StringRef GetPureVirtualCallName() = 0;
516
517
  /// Gets the deleted virtual member call name.
518
  virtual StringRef GetDeletedVirtualCallName() = 0;
519
520
  /**************************** Array cookies ******************************/
521
522
  /// Returns the extra size required in order to store the array
523
  /// cookie for the given new-expression.  May return 0 to indicate that no
524
  /// array cookie is required.
525
  ///
526
  /// Several cases are filtered out before this method is called:
527
  ///   - non-array allocations never need a cookie
528
  ///   - calls to \::operator new(size_t, void*) never need a cookie
529
  ///
530
  /// \param expr - the new-expression being allocated.
531
  virtual CharUnits GetArrayCookieSize(const CXXNewExpr *expr);
532
533
  /// Initialize the array cookie for the given allocation.
534
  ///
535
  /// \param NewPtr - a char* which is the presumed-non-null
536
  ///   return value of the allocation function
537
  /// \param NumElements - the computed number of elements,
538
  ///   potentially collapsed from the multidimensional array case;
539
  ///   always a size_t
540
  /// \param ElementType - the base element allocated type,
541
  ///   i.e. the allocated type after stripping all array types
542
  virtual Address InitializeArrayCookie(CodeGenFunction &CGF,
543
                                        Address NewPtr,
544
                                        llvm::Value *NumElements,
545
                                        const CXXNewExpr *expr,
546
                                        QualType ElementType);
547
548
  /// Reads the array cookie associated with the given pointer,
549
  /// if it has one.
550
  ///
551
  /// \param Ptr - a pointer to the first element in the array
552
  /// \param ElementType - the base element type of elements of the array
553
  /// \param NumElements - an out parameter which will be initialized
554
  ///   with the number of elements allocated, or zero if there is no
555
  ///   cookie
556
  /// \param AllocPtr - an out parameter which will be initialized
557
  ///   with a char* pointing to the address returned by the allocation
558
  ///   function
559
  /// \param CookieSize - an out parameter which will be initialized
560
  ///   with the size of the cookie, or zero if there is no cookie
561
  virtual void ReadArrayCookie(CodeGenFunction &CGF, Address Ptr,
562
                               const CXXDeleteExpr *expr,
563
                               QualType ElementType, llvm::Value *&NumElements,
564
                               llvm::Value *&AllocPtr, CharUnits &CookieSize);
565
566
  /// Return whether the given global decl needs a VTT parameter.
567
  virtual bool NeedsVTTParameter(GlobalDecl GD);
568
569
protected:
570
  /// Returns the extra size required in order to store the array
571
  /// cookie for the given type.  Assumes that an array cookie is
572
  /// required.
573
  virtual CharUnits getArrayCookieSizeImpl(QualType elementType);
574
575
  /// Reads the array cookie for an allocation which is known to have one.
576
  /// This is called by the standard implementation of ReadArrayCookie.
577
  ///
578
  /// \param ptr - a pointer to the allocation made for an array, as a char*
579
  /// \param cookieSize - the computed cookie size of an array
580
  ///
581
  /// Other parameters are as above.
582
  ///
583
  /// \return a size_t
584
  virtual llvm::Value *readArrayCookieImpl(CodeGenFunction &IGF, Address ptr,
585
                                           CharUnits cookieSize);
586
587
public:
588
589
  /*************************** Static local guards ****************************/
590
591
  /// Emits the guarded initializer and destructor setup for the given
592
  /// variable, given that it couldn't be emitted as a constant.
593
  /// If \p PerformInit is false, the initialization has been folded to a
594
  /// constant and should not be performed.
595
  ///
596
  /// The variable may be:
597
  ///   - a static local variable
598
  ///   - a static data member of a class template instantiation
599
  virtual void EmitGuardedInit(CodeGenFunction &CGF, const VarDecl &D,
600
                               llvm::GlobalVariable *DeclPtr,
601
                               bool PerformInit) = 0;
602
603
  /// Emit code to force the execution of a destructor during global
604
  /// teardown.  The default implementation of this uses atexit.
605
  ///
606
  /// \param Dtor - a function taking a single pointer argument
607
  /// \param Addr - a pointer to pass to the destructor function.
608
  virtual void registerGlobalDtor(CodeGenFunction &CGF, const VarDecl &D,
609
                                  llvm::FunctionCallee Dtor,
610
                                  llvm::Constant *Addr) = 0;
611
612
  /*************************** thread_local initialization ********************/
613
614
  /// Emits ABI-required functions necessary to initialize thread_local
615
  /// variables in this translation unit.
616
  ///
617
  /// \param CXXThreadLocals - The thread_local declarations in this translation
618
  ///        unit.
619
  /// \param CXXThreadLocalInits - If this translation unit contains any
620
  ///        non-constant initialization or non-trivial destruction for
621
  ///        thread_local variables, a list of functions to perform the
622
  ///        initialization.
623
  virtual void EmitThreadLocalInitFuncs(
624
      CodeGenModule &CGM, ArrayRef<const VarDecl *> CXXThreadLocals,
625
      ArrayRef<llvm::Function *> CXXThreadLocalInits,
626
      ArrayRef<const VarDecl *> CXXThreadLocalInitVars) = 0;
627
628
  // Determine if references to thread_local global variables can be made
629
  // directly or require access through a thread wrapper function.
630
  virtual bool usesThreadWrapperFunction(const VarDecl *VD) const = 0;
631
632
  /// Emit a reference to a non-local thread_local variable (including
633
  /// triggering the initialization of all thread_local variables in its
634
  /// translation unit).
635
  virtual LValue EmitThreadLocalVarDeclLValue(CodeGenFunction &CGF,
636
                                              const VarDecl *VD,
637
                                              QualType LValType) = 0;
638
639
  /// Emit a single constructor/destructor with the given type from a C++
640
  /// constructor Decl.
641
  virtual void emitCXXStructor(GlobalDecl GD) = 0;
642
643
  /// Load a vtable from This, an object of polymorphic type RD, or from one of
644
  /// its virtual bases if it does not have its own vtable. Returns the vtable
645
  /// and the class from which the vtable was loaded.
646
  virtual std::pair<llvm::Value *, const CXXRecordDecl *>
647
  LoadVTablePtr(CodeGenFunction &CGF, Address This,
648
                const CXXRecordDecl *RD) = 0;
649
};
650
651
// Create an instance of a C++ ABI class:
652
653
/// Creates an Itanium-family ABI.
654
CGCXXABI *CreateItaniumCXXABI(CodeGenModule &CGM);
655
656
/// Creates a Microsoft-family ABI.
657
CGCXXABI *CreateMicrosoftCXXABI(CodeGenModule &CGM);
658
659
struct CatchRetScope final : EHScopeStack::Cleanup {
660
  llvm::CatchPadInst *CPI;
661
662
44
  CatchRetScope(llvm::CatchPadInst *CPI) : CPI(CPI) {}
663
664
42
  void Emit(CodeGenFunction &CGF, Flags flags) override {
665
42
    llvm::BasicBlock *BB = CGF.createBasicBlock("catchret.dest");
666
42
    CGF.Builder.CreateCatchRet(CPI, BB);
667
42
    CGF.EmitBlock(BB);
668
42
  }
669
};
670
}
671
}
672
673
#endif