Coverage Report

Created: 2022-07-16 07:03

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/CodeGen/CGCXXABI.cpp
Line
Count
Source (jump to first uncovered line)
1
//===----- CGCXXABI.cpp - Interface to C++ ABIs ---------------------------===//
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
#include "CGCXXABI.h"
15
#include "CGCleanup.h"
16
#include "clang/AST/Attr.h"
17
18
using namespace clang;
19
using namespace CodeGen;
20
21
36.3k
CGCXXABI::~CGCXXABI() { }
22
23
0
void CGCXXABI::ErrorUnsupportedABI(CodeGenFunction &CGF, StringRef S) {
24
0
  DiagnosticsEngine &Diags = CGF.CGM.getDiags();
25
0
  unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
26
0
                                          "cannot yet compile %0 in this ABI");
27
0
  Diags.Report(CGF.getContext().getFullLoc(CGF.CurCodeDecl->getLocation()),
28
0
               DiagID)
29
0
    << S;
30
0
}
31
32
0
llvm::Constant *CGCXXABI::GetBogusMemberPointer(QualType T) {
33
0
  return llvm::Constant::getNullValue(CGM.getTypes().ConvertType(T));
34
0
}
35
36
llvm::Type *
37
0
CGCXXABI::ConvertMemberPointerType(const MemberPointerType *MPT) {
38
0
  return CGM.getTypes().ConvertType(CGM.getContext().getPointerDiffType());
39
0
}
40
41
CGCallee CGCXXABI::EmitLoadOfMemberFunctionPointer(
42
    CodeGenFunction &CGF, const Expr *E, Address This,
43
    llvm::Value *&ThisPtrForCall,
44
0
    llvm::Value *MemPtr, const MemberPointerType *MPT) {
45
0
  ErrorUnsupportedABI(CGF, "calls through member pointers");
46
47
0
  ThisPtrForCall = This.getPointer();
48
0
  const auto *FPT = MPT->getPointeeType()->castAs<FunctionProtoType>();
49
0
  const auto *RD =
50
0
      cast<CXXRecordDecl>(MPT->getClass()->castAs<RecordType>()->getDecl());
51
0
  llvm::FunctionType *FTy = CGM.getTypes().GetFunctionType(
52
0
      CGM.getTypes().arrangeCXXMethodType(RD, FPT, /*FD=*/nullptr));
53
0
  llvm::Constant *FnPtr = llvm::Constant::getNullValue(FTy->getPointerTo());
54
0
  return CGCallee::forDirect(FnPtr, FPT);
55
0
}
56
57
llvm::Value *
58
CGCXXABI::EmitMemberDataPointerAddress(CodeGenFunction &CGF, const Expr *E,
59
                                       Address Base, llvm::Value *MemPtr,
60
0
                                       const MemberPointerType *MPT) {
61
0
  ErrorUnsupportedABI(CGF, "loads of member pointers");
62
0
  llvm::Type *Ty = CGF.ConvertType(MPT->getPointeeType())
63
0
                         ->getPointerTo(Base.getAddressSpace());
64
0
  return llvm::Constant::getNullValue(Ty);
65
0
}
66
67
llvm::Value *CGCXXABI::EmitMemberPointerConversion(CodeGenFunction &CGF,
68
                                                   const CastExpr *E,
69
0
                                                   llvm::Value *Src) {
70
0
  ErrorUnsupportedABI(CGF, "member function pointer conversions");
71
0
  return GetBogusMemberPointer(E->getType());
72
0
}
73
74
llvm::Constant *CGCXXABI::EmitMemberPointerConversion(const CastExpr *E,
75
0
                                                      llvm::Constant *Src) {
76
0
  return GetBogusMemberPointer(E->getType());
77
0
}
78
79
llvm::Value *
80
CGCXXABI::EmitMemberPointerComparison(CodeGenFunction &CGF,
81
                                      llvm::Value *L,
82
                                      llvm::Value *R,
83
                                      const MemberPointerType *MPT,
84
0
                                      bool Inequality) {
85
0
  ErrorUnsupportedABI(CGF, "member function pointer comparison");
86
0
  return CGF.Builder.getFalse();
87
0
}
88
89
llvm::Value *
90
CGCXXABI::EmitMemberPointerIsNotNull(CodeGenFunction &CGF,
91
                                     llvm::Value *MemPtr,
92
0
                                     const MemberPointerType *MPT) {
93
0
  ErrorUnsupportedABI(CGF, "member function pointer null testing");
94
0
  return CGF.Builder.getFalse();
95
0
}
96
97
llvm::Constant *
98
0
CGCXXABI::EmitNullMemberPointer(const MemberPointerType *MPT) {
99
0
  return GetBogusMemberPointer(QualType(MPT, 0));
100
0
}
101
102
0
llvm::Constant *CGCXXABI::EmitMemberFunctionPointer(const CXXMethodDecl *MD) {
103
0
  return GetBogusMemberPointer(CGM.getContext().getMemberPointerType(
104
0
      MD->getType(), MD->getParent()->getTypeForDecl()));
105
0
}
106
107
llvm::Constant *CGCXXABI::EmitMemberDataPointer(const MemberPointerType *MPT,
108
0
                                                CharUnits offset) {
109
0
  return GetBogusMemberPointer(QualType(MPT, 0));
110
0
}
111
112
0
llvm::Constant *CGCXXABI::EmitMemberPointer(const APValue &MP, QualType MPT) {
113
0
  return GetBogusMemberPointer(MPT);
114
0
}
115
116
0
bool CGCXXABI::isZeroInitializable(const MemberPointerType *MPT) {
117
  // Fake answer.
118
0
  return true;
119
0
}
120
121
104k
void CGCXXABI::buildThisParam(CodeGenFunction &CGF, FunctionArgList &params) {
122
104k
  const CXXMethodDecl *MD = cast<CXXMethodDecl>(CGF.CurGD.getDecl());
123
124
  // FIXME: I'm not entirely sure I like using a fake decl just for code
125
  // generation. Maybe we can come up with a better way?
126
104k
  auto *ThisDecl = ImplicitParamDecl::Create(
127
104k
      CGM.getContext(), nullptr, MD->getLocation(),
128
104k
      &CGM.getContext().Idents.get("this"), MD->getThisType(),
129
104k
      ImplicitParamDecl::CXXThis);
130
104k
  params.push_back(ThisDecl);
131
104k
  CGF.CXXABIThisDecl = ThisDecl;
132
133
  // Compute the presumed alignment of 'this', which basically comes
134
  // down to whether we know it's a complete object or not.
135
104k
  auto &Layout = CGF.getContext().getASTRecordLayout(MD->getParent());
136
104k
  if (MD->getParent()->getNumVBases() == 0 || // avoid vcall in common case
137
104k
      
MD->getParent()->isEffectivelyFinal()1.87k
||
138
104k
      
isThisCompleteObject(CGF.CurGD)1.87k
) {
139
103k
    CGF.CXXABIThisAlignment = Layout.getAlignment();
140
103k
  } else {
141
1.14k
    CGF.CXXABIThisAlignment = Layout.getNonVirtualAlignment();
142
1.14k
  }
143
104k
}
144
145
104k
llvm::Value *CGCXXABI::loadIncomingCXXThis(CodeGenFunction &CGF) {
146
104k
  return CGF.Builder.CreateLoad(CGF.GetAddrOfLocalVar(getThisDecl(CGF)),
147
104k
                                "this");
148
104k
}
149
150
104k
void CGCXXABI::setCXXABIThisValue(CodeGenFunction &CGF, llvm::Value *ThisPtr) {
151
  /// Initialize the 'this' slot.
152
104k
  assert(getThisDecl(CGF) && "no 'this' variable for function");
153
0
  CGF.CXXABIThisValue = ThisPtr;
154
104k
}
155
156
334
bool CGCXXABI::mayNeedDestruction(const VarDecl *VD) const {
157
334
  if (VD->needsDestruction(getContext()))
158
90
    return true;
159
160
  // If the variable has an incomplete class type (or array thereof), it
161
  // might need destruction.
162
244
  const Type *T = VD->getType()->getBaseElementTypeUnsafe();
163
244
  if (T->getAs<RecordType>() && 
T->isIncompleteType()38
)
164
3
    return true;
165
166
241
  return false;
167
244
}
168
169
bool CGCXXABI::isEmittedWithConstantInitializer(
170
483
    const VarDecl *VD, bool InspectInitForWeakDef) const {
171
483
  VD = VD->getMostRecentDecl();
172
483
  if (VD->hasAttr<ConstInitAttr>())
173
14
    return true;
174
175
  // All later checks examine the initializer specified on the variable. If
176
  // the variable is weak, such examination would not be correct.
177
469
  if (!InspectInitForWeakDef && 
(457
VD->isWeak()457
||
VD->hasAttr<SelectAnyAttr>()457
))
178
0
    return false;
179
180
469
  const VarDecl *InitDecl = VD->getInitializingDeclaration();
181
469
  if (!InitDecl)
182
166
    return false;
183
184
  // If there's no initializer to run, this is constant initialization.
185
303
  if (!InitDecl->hasInit())
186
29
    return true;
187
188
  // If we have the only definition, we don't need a thread wrapper if we
189
  // will emit the value as a constant.
190
274
  if (isUniqueGVALinkage(getContext().GetGVALinkageForVariable(VD)))
191
185
    return !mayNeedDestruction(VD) && 
InitDecl->evaluateValue()120
;
192
193
  // Otherwise, we need a thread wrapper unless we know that every
194
  // translation unit will emit the value as a constant. We rely on the
195
  // variable being constant-initialized in every translation unit if it's
196
  // constant-initialized in any translation unit, which isn't actually
197
  // guaranteed by the standard but is necessary for sanity.
198
89
  return InitDecl->hasConstantInitialization();
199
274
}
200
201
void CGCXXABI::EmitReturnFromThunk(CodeGenFunction &CGF,
202
196
                                   RValue RV, QualType ResultType) {
203
196
  assert(!CGF.hasAggregateEvaluationKind(ResultType) &&
204
196
         "cannot handle aggregates");
205
0
  CGF.EmitReturnOfRValue(RV, ResultType);
206
196
}
207
208
684
CharUnits CGCXXABI::GetArrayCookieSize(const CXXNewExpr *expr) {
209
684
  if (!requiresArrayCookie(expr))
210
560
    return CharUnits::Zero();
211
124
  return getArrayCookieSizeImpl(expr->getAllocatedType());
212
684
}
213
214
0
CharUnits CGCXXABI::getArrayCookieSizeImpl(QualType elementType) {
215
  // BOGUS
216
0
  return CharUnits::Zero();
217
0
}
218
219
Address CGCXXABI::InitializeArrayCookie(CodeGenFunction &CGF,
220
                                        Address NewPtr,
221
                                        llvm::Value *NumElements,
222
                                        const CXXNewExpr *expr,
223
0
                                        QualType ElementType) {
224
  // Should never be called.
225
0
  ErrorUnsupportedABI(CGF, "array cookie initialization");
226
0
  return Address::invalid();
227
0
}
228
229
bool CGCXXABI::requiresArrayCookie(const CXXDeleteExpr *expr,
230
251
                                   QualType elementType) {
231
  // If the class's usual deallocation function takes two arguments,
232
  // it needs a cookie.
233
251
  if (expr->doesUsualArrayDeleteWantSize())
234
18
    return true;
235
236
233
  return elementType.isDestructedType();
237
251
}
238
239
710
bool CGCXXABI::requiresArrayCookie(const CXXNewExpr *expr) {
240
  // If the class's usual deallocation function takes two arguments,
241
  // it needs a cookie.
242
710
  if (expr->doesUsualArrayDeleteWantSize())
243
45
    return true;
244
245
665
  return expr->getAllocatedType().isDestructedType();
246
710
}
247
248
void CGCXXABI::ReadArrayCookie(CodeGenFunction &CGF, Address ptr,
249
                               const CXXDeleteExpr *expr, QualType eltTy,
250
                               llvm::Value *&numElements,
251
265
                               llvm::Value *&allocPtr, CharUnits &cookieSize) {
252
  // Derive a char* in the same address space as the pointer.
253
265
  ptr = CGF.Builder.CreateElementBitCast(ptr, CGF.Int8Ty);
254
255
  // If we don't need an array cookie, bail out early.
256
265
  if (!requiresArrayCookie(expr, eltTy)) {
257
206
    allocPtr = ptr.getPointer();
258
206
    numElements = nullptr;
259
206
    cookieSize = CharUnits::Zero();
260
206
    return;
261
206
  }
262
263
59
  cookieSize = getArrayCookieSizeImpl(eltTy);
264
59
  Address allocAddr =
265
59
    CGF.Builder.CreateConstInBoundsByteGEP(ptr, -cookieSize);
266
59
  allocPtr = allocAddr.getPointer();
267
59
  numElements = readArrayCookieImpl(CGF, allocAddr, cookieSize);
268
59
}
269
270
llvm::Value *CGCXXABI::readArrayCookieImpl(CodeGenFunction &CGF,
271
                                           Address ptr,
272
0
                                           CharUnits cookieSize) {
273
0
  ErrorUnsupportedABI(CGF, "reading a new[] cookie");
274
0
  return llvm::ConstantInt::get(CGF.SizeTy, 0);
275
0
}
276
277
/// Returns the adjustment, in bytes, required for the given
278
/// member-pointer operation.  Returns null if no adjustment is
279
/// required.
280
56
llvm::Constant *CGCXXABI::getMemberPointerAdjustment(const CastExpr *E) {
281
56
  assert(E->getCastKind() == CK_DerivedToBaseMemberPointer ||
282
56
         E->getCastKind() == CK_BaseToDerivedMemberPointer);
283
284
0
  QualType derivedType;
285
56
  if (E->getCastKind() == CK_DerivedToBaseMemberPointer)
286
8
    derivedType = E->getSubExpr()->getType();
287
48
  else
288
48
    derivedType = E->getType();
289
290
56
  const CXXRecordDecl *derivedClass =
291
56
    derivedType->castAs<MemberPointerType>()->getClass()->getAsCXXRecordDecl();
292
293
56
  return CGM.GetNonVirtualBaseClassOffset(derivedClass,
294
56
                                          E->path_begin(),
295
56
                                          E->path_end());
296
56
}
297
298
llvm::BasicBlock *
299
CGCXXABI::EmitCtorCompleteObjectHandler(CodeGenFunction &CGF,
300
0
                                        const CXXRecordDecl *RD) {
301
0
  if (CGM.getTarget().getCXXABI().hasConstructorVariants())
302
0
    llvm_unreachable("shouldn't be called in this ABI");
303
304
0
  ErrorUnsupportedABI(CGF, "complete object detection in ctor");
305
0
  return nullptr;
306
0
}
307
308
void CGCXXABI::setCXXDestructorDLLStorage(llvm::GlobalValue *GV,
309
                                          const CXXDestructorDecl *Dtor,
310
16.1k
                                          CXXDtorType DT) const {
311
  // Assume the base C++ ABI has no special rules for destructor variants.
312
16.1k
  CGM.setDLLImportDLLExport(GV, Dtor);
313
16.1k
}
314
315
llvm::GlobalValue::LinkageTypes CGCXXABI::getCXXDestructorLinkage(
316
48.3k
    GVALinkage Linkage, const CXXDestructorDecl *Dtor, CXXDtorType DT) const {
317
  // Delegate back to CGM by default.
318
48.3k
  return CGM.getLLVMLinkageForDeclarator(Dtor, Linkage,
319
48.3k
                                         /*IsConstantVariable=*/false);
320
48.3k
}
321
322
0
bool CGCXXABI::NeedsVTTParameter(GlobalDecl GD) {
323
0
  return false;
324
0
}
325
326
llvm::CallInst *
327
CGCXXABI::emitTerminateForUnexpectedException(CodeGenFunction &CGF,
328
17
                                              llvm::Value *Exn) {
329
  // Just call std::terminate and ignore the violating exception.
330
17
  return CGF.EmitNounwindRuntimeCall(CGF.CGM.getTerminateFn());
331
17
}
332
333
220
CatchTypeInfo CGCXXABI::getCatchAllTypeInfo() {
334
220
  return CatchTypeInfo{nullptr, 0};
335
220
}
336
337
15
std::vector<CharUnits> CGCXXABI::getVBPtrOffsets(const CXXRecordDecl *RD) {
338
15
  return std::vector<CharUnits>();
339
15
}
340
341
CGCXXABI::AddedStructorArgCounts CGCXXABI::addImplicitConstructorArgs(
342
    CodeGenFunction &CGF, const CXXConstructorDecl *D, CXXCtorType Type,
343
55.3k
    bool ForVirtualBase, bool Delegating, CallArgList &Args) {
344
55.3k
  AddedStructorArgs AddedArgs =
345
55.3k
      getImplicitConstructorArgs(CGF, D, Type, ForVirtualBase, Delegating);
346
55.5k
  for (size_t i = 0; i < AddedArgs.Prefix.size(); 
++i142
) {
347
142
    Args.insert(Args.begin() + 1 + i,
348
142
                CallArg(RValue::get(AddedArgs.Prefix[i].Value),
349
142
                        AddedArgs.Prefix[i].Type));
350
142
  }
351
55.3k
  for (const auto &arg : AddedArgs.Suffix) {
352
470
    Args.add(RValue::get(arg.Value), arg.Type);
353
470
  }
354
55.3k
  return AddedStructorArgCounts(AddedArgs.Prefix.size(),
355
55.3k
                                AddedArgs.Suffix.size());
356
55.3k
}