Coverage Report

Created: 2022-07-16 07:03

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/AST/APValue.cpp
Line
Count
Source (jump to first uncovered line)
1
//===--- APValue.cpp - Union class for APFloat/APSInt/Complex -------------===//
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 file implements the APValue class.
10
//
11
//===----------------------------------------------------------------------===//
12
13
#include "clang/AST/APValue.h"
14
#include "Linkage.h"
15
#include "clang/AST/ASTContext.h"
16
#include "clang/AST/CharUnits.h"
17
#include "clang/AST/DeclCXX.h"
18
#include "clang/AST/Expr.h"
19
#include "clang/AST/ExprCXX.h"
20
#include "clang/AST/Type.h"
21
#include "llvm/Support/ErrorHandling.h"
22
#include "llvm/Support/raw_ostream.h"
23
using namespace clang;
24
25
/// The identity of a type_info object depends on the canonical unqualified
26
/// type only.
27
TypeInfoLValue::TypeInfoLValue(const Type *T)
28
1.27k
    : T(T->getCanonicalTypeUnqualified().getTypePtr()) {}
29
30
void TypeInfoLValue::print(llvm::raw_ostream &Out,
31
36
                           const PrintingPolicy &Policy) const {
32
36
  Out << "typeid(";
33
36
  QualType(getType(), 0).print(Out, Policy);
34
36
  Out << ")";
35
36
}
36
37
static_assert(
38
    1 << llvm::PointerLikeTypeTraits<TypeInfoLValue>::NumLowBitsAvailable <=
39
        alignof(Type),
40
    "Type is insufficiently aligned");
41
42
APValue::LValueBase::LValueBase(const ValueDecl *P, unsigned I, unsigned V)
43
35.6M
    : Ptr(P ? cast<ValueDecl>(P->getCanonicalDecl()) : nullptr), Local{I, V} {}
44
APValue::LValueBase::LValueBase(const Expr *P, unsigned I, unsigned V)
45
2.37M
    : Ptr(P), Local{I, V} {}
46
47
APValue::LValueBase APValue::LValueBase::getDynamicAlloc(DynamicAllocLValue LV,
48
1.13k
                                                         QualType Type) {
49
1.13k
  LValueBase Base;
50
1.13k
  Base.Ptr = LV;
51
1.13k
  Base.DynamicAllocType = Type.getAsOpaquePtr();
52
1.13k
  return Base;
53
1.13k
}
54
55
APValue::LValueBase APValue::LValueBase::getTypeInfo(TypeInfoLValue LV,
56
1.27k
                                                     QualType TypeInfo) {
57
1.27k
  LValueBase Base;
58
1.27k
  Base.Ptr = LV;
59
1.27k
  Base.TypeInfoType = TypeInfo.getAsOpaquePtr();
60
1.27k
  return Base;
61
1.27k
}
62
63
21.0M
QualType APValue::LValueBase::getType() const {
64
21.0M
  if (!*this) 
return QualType()21
;
65
21.0M
  if (const ValueDecl *D = dyn_cast<const ValueDecl*>()) {
66
    // FIXME: It's unclear where we're supposed to take the type from, and
67
    // this actually matters for arrays of unknown bound. Eg:
68
    //
69
    // extern int arr[]; void f() { extern int arr[3]; };
70
    // constexpr int *p = &arr[1]; // valid?
71
    //
72
    // For now, we take the most complete type we can find.
73
18.5M
    for (auto *Redecl = cast<ValueDecl>(D->getMostRecentDecl()); Redecl;
74
18.5M
         
Redecl = cast_or_null<ValueDecl>(Redecl->getPreviousDecl())2.46k
) {
75
18.5M
      QualType T = Redecl->getType();
76
18.5M
      if (!T->isIncompleteArrayType())
77
18.5M
        return T;
78
18.5M
    }
79
2.01k
    return D->getType();
80
18.5M
  }
81
82
2.52M
  if (is<TypeInfoLValue>())
83
2.38k
    return getTypeInfoType();
84
85
2.51M
  if (is<DynamicAllocLValue>())
86
51.1k
    return getDynamicAllocType();
87
88
2.46M
  const Expr *Base = get<const Expr*>();
89
90
  // For a materialized temporary, the type of the temporary we materialized
91
  // may not be the type of the expression.
92
2.46M
  if (const MaterializeTemporaryExpr *MTE =
93
2.46M
          clang::dyn_cast<MaterializeTemporaryExpr>(Base)) {
94
2.32M
    SmallVector<const Expr *, 2> CommaLHSs;
95
2.32M
    SmallVector<SubobjectAdjustment, 2> Adjustments;
96
2.32M
    const Expr *Temp = MTE->getSubExpr();
97
2.32M
    const Expr *Inner = Temp->skipRValueSubobjectAdjustments(CommaLHSs,
98
2.32M
                                                             Adjustments);
99
    // Keep any cv-qualifiers from the reference if we generated a temporary
100
    // for it directly. Otherwise use the type after adjustment.
101
2.32M
    if (!Adjustments.empty())
102
3.23k
      return Inner->getType();
103
2.32M
  }
104
105
2.46M
  return Base->getType();
106
2.46M
}
107
108
11.5M
unsigned APValue::LValueBase::getCallIndex() const {
109
11.5M
  return (is<TypeInfoLValue>() || 
is<DynamicAllocLValue>()11.5M
) ?
046.2k
110
11.5M
                                                            : 
Local.CallIndex11.5M
;
111
11.5M
}
112
113
5.64M
unsigned APValue::LValueBase::getVersion() const {
114
5.64M
  return (is<TypeInfoLValue>() || 
is<DynamicAllocLValue>()5.64M
) ?
052
:
Local.Version5.64M
;
115
5.64M
}
116
117
2.53k
QualType APValue::LValueBase::getTypeInfoType() const {
118
2.53k
  assert(is<TypeInfoLValue>() && "not a type_info lvalue");
119
0
  return QualType::getFromOpaquePtr(TypeInfoType);
120
2.53k
}
121
122
96.9k
QualType APValue::LValueBase::getDynamicAllocType() const {
123
96.9k
  assert(is<DynamicAllocLValue>() && "not a dynamic allocation lvalue");
124
0
  return QualType::getFromOpaquePtr(DynamicAllocType);
125
96.9k
}
126
127
472
void APValue::LValueBase::Profile(llvm::FoldingSetNodeID &ID) const {
128
472
  ID.AddPointer(Ptr.getOpaqueValue());
129
472
  if (is<TypeInfoLValue>() || is<DynamicAllocLValue>())
130
0
    return;
131
472
  ID.AddInteger(Local.CallIndex);
132
472
  ID.AddInteger(Local.Version);
133
472
}
134
135
namespace clang {
136
bool operator==(const APValue::LValueBase &LHS,
137
8.31M
                const APValue::LValueBase &RHS) {
138
8.31M
  if (LHS.Ptr != RHS.Ptr)
139
6.67M
    return false;
140
1.64M
  if (LHS.is<TypeInfoLValue>() || LHS.is<DynamicAllocLValue>())
141
490
    return true;
142
1.64M
  return LHS.Local.CallIndex == RHS.Local.CallIndex &&
143
1.64M
         
LHS.Local.Version == RHS.Local.Version1.64M
;
144
1.64M
}
145
}
146
147
184k
APValue::LValuePathEntry::LValuePathEntry(BaseOrMemberType BaseOrMember) {
148
184k
  if (const Decl *D = BaseOrMember.getPointer())
149
184k
    BaseOrMember.setPointer(D->getCanonicalDecl());
150
184k
  Value = reinterpret_cast<uintptr_t>(BaseOrMember.getOpaqueValue());
151
184k
}
152
153
453
void APValue::LValuePathEntry::Profile(llvm::FoldingSetNodeID &ID) const {
154
453
  ID.AddInteger(Value);
155
453
}
156
157
APValue::LValuePathSerializationHelper::LValuePathSerializationHelper(
158
    ArrayRef<LValuePathEntry> Path, QualType ElemTy)
159
37
    : ElemTy((const void *)ElemTy.getTypePtrOrNull()), Path(Path) {}
160
161
37
QualType APValue::LValuePathSerializationHelper::getType() {
162
37
  return QualType::getFromOpaquePtr(ElemTy);
163
37
}
164
165
namespace {
166
  struct LVBase {
167
    APValue::LValueBase Base;
168
    CharUnits Offset;
169
    unsigned PathLength;
170
    bool IsNullPtr : 1;
171
    bool IsOnePastTheEnd : 1;
172
  };
173
}
174
175
125k
void *APValue::LValueBase::getOpaqueValue() const {
176
125k
  return Ptr.getOpaqueValue();
177
125k
}
178
179
10.1k
bool APValue::LValueBase::isNull() const {
180
10.1k
  return Ptr.isNull();
181
10.1k
}
182
183
30.9M
APValue::LValueBase::operator bool () const {
184
30.9M
  return static_cast<bool>(Ptr);
185
30.9M
}
186
187
clang::APValue::LValueBase
188
191k
llvm::DenseMapInfo<clang::APValue::LValueBase>::getEmptyKey() {
189
191k
  clang::APValue::LValueBase B;
190
191k
  B.Ptr = DenseMapInfo<const ValueDecl*>::getEmptyKey();
191
191k
  return B;
192
191k
}
193
194
clang::APValue::LValueBase
195
168k
llvm::DenseMapInfo<clang::APValue::LValueBase>::getTombstoneKey() {
196
168k
  clang::APValue::LValueBase B;
197
168k
  B.Ptr = DenseMapInfo<const ValueDecl*>::getTombstoneKey();
198
168k
  return B;
199
168k
}
200
201
namespace clang {
202
115k
llvm::hash_code hash_value(const APValue::LValueBase &Base) {
203
115k
  if (Base.is<TypeInfoLValue>() || Base.is<DynamicAllocLValue>())
204
750
    return llvm::hash_value(Base.getOpaqueValue());
205
114k
  return llvm::hash_combine(Base.getOpaqueValue(), Base.getCallIndex(),
206
114k
                            Base.getVersion());
207
115k
}
208
}
209
210
unsigned llvm::DenseMapInfo<clang::APValue::LValueBase>::getHashValue(
211
0
    const clang::APValue::LValueBase &Base) {
212
0
  return hash_value(Base);
213
0
}
214
215
bool llvm::DenseMapInfo<clang::APValue::LValueBase>::isEqual(
216
    const clang::APValue::LValueBase &LHS,
217
0
    const clang::APValue::LValueBase &RHS) {
218
0
  return LHS == RHS;
219
0
}
220
221
struct APValue::LV : LVBase {
222
  static const unsigned InlinePathSpace =
223
      (DataSize - sizeof(LVBase)) / sizeof(LValuePathEntry);
224
225
  /// Path - The sequence of base classes, fields and array indices to follow to
226
  /// walk from Base to the subobject. When performing GCC-style folding, there
227
  /// may not be such a path.
228
  union {
229
    LValuePathEntry Path[InlinePathSpace];
230
    LValuePathEntry *PathPtr;
231
  };
232
233
1.39M
  LV() { PathLength = (unsigned)-1; }
234
1.38M
  ~LV() { resizePath(0); }
235
236
2.77M
  void resizePath(unsigned Length) {
237
2.77M
    if (Length == PathLength)
238
1.29M
      return;
239
1.47M
    if (hasPathPtr())
240
202
      delete [] PathPtr;
241
1.47M
    PathLength = Length;
242
1.47M
    if (hasPathPtr())
243
202
      PathPtr = new LValuePathEntry[Length];
244
1.47M
  }
245
246
8.46M
  bool hasPath() const { return PathLength != (unsigned)-1; }
247
5.97M
  bool hasPathPtr() const { return hasPath() && 
PathLength > InlinePathSpace4.58M
; }
248
249
1.38M
  LValuePathEntry *getPath() { return hasPathPtr() ? 
PathPtr202
:
Path1.38M
; }
250
1.62M
  const LValuePathEntry *getPath() const {
251
1.62M
    return hasPathPtr() ? 
PathPtr413
:
Path1.62M
;
252
1.62M
  }
253
};
254
255
namespace {
256
  struct MemberPointerBase {
257
    llvm::PointerIntPair<const ValueDecl*, 1, bool> MemberAndIsDerivedMember;
258
    unsigned PathLength;
259
  };
260
}
261
262
struct APValue::MemberPointerData : MemberPointerBase {
263
  static const unsigned InlinePathSpace =
264
      (DataSize - sizeof(MemberPointerBase)) / sizeof(const CXXRecordDecl*);
265
  typedef const CXXRecordDecl *PathElem;
266
  union {
267
    PathElem Path[InlinePathSpace];
268
    PathElem *PathPtr;
269
  };
270
271
2.24k
  MemberPointerData() { PathLength = 0; }
272
1.70k
  ~MemberPointerData() { resizePath(0); }
273
274
3.95k
  void resizePath(unsigned Length) {
275
3.95k
    if (Length == PathLength)
276
3.32k
      return;
277
637
    if (hasPathPtr())
278
91
      delete [] PathPtr;
279
637
    PathLength = Length;
280
637
    if (hasPathPtr())
281
91
      PathPtr = new PathElem[Length];
282
637
  }
283
284
5.21k
  bool hasPathPtr() const { return PathLength > InlinePathSpace; }
285
286
2.24k
  PathElem *getPath() { return hasPathPtr() ? 
PathPtr91
:
Path2.15k
; }
287
1.14k
  const PathElem *getPath() const {
288
1.14k
    return hasPathPtr() ? 
PathPtr168
:
Path974
;
289
1.14k
  }
290
};
291
292
// FIXME: Reduce the malloc traffic here.
293
294
APValue::Arr::Arr(unsigned NumElts, unsigned Size) :
295
  Elts(new APValue[NumElts + (NumElts != Size ? 1 : 0)]),
296
19.9k
  NumElts(NumElts), ArrSize(Size) {}
297
19.9k
APValue::Arr::~Arr() { delete [] Elts; }
298
299
APValue::StructData::StructData(unsigned NumBases, unsigned NumFields) :
300
  Elts(new APValue[NumBases+NumFields]),
301
55.0k
  NumBases(NumBases), NumFields(NumFields) {}
302
55.0k
APValue::StructData::~StructData() {
303
55.0k
  delete [] Elts;
304
55.0k
}
305
306
3.18k
APValue::UnionData::UnionData() : Field(nullptr), Value(new APValue) {}
307
3.18k
APValue::UnionData::~UnionData () {
308
3.18k
  delete Value;
309
3.18k
}
310
311
10.5M
APValue::APValue(const APValue &RHS) : Kind(None) {
312
10.5M
  switch (RHS.getKind()) {
313
9.86k
  case None:
314
26.2k
  case Indeterminate:
315
26.2k
    Kind = RHS.getKind();
316
26.2k
    break;
317
10.4M
  case Int:
318
10.4M
    MakeInt();
319
10.4M
    setInt(RHS.getInt());
320
10.4M
    break;
321
18.2k
  case Float:
322
18.2k
    MakeFloat();
323
18.2k
    setFloat(RHS.getFloat());
324
18.2k
    break;
325
6
  case FixedPoint: {
326
6
    APFixedPoint FXCopy = RHS.getFixedPoint();
327
6
    MakeFixedPoint(std::move(FXCopy));
328
6
    break;
329
9.86k
  }
330
1.83k
  case Vector:
331
1.83k
    MakeVector();
332
1.83k
    setVector(((const Vec *)(const char *)&RHS.Data)->Elts,
333
1.83k
              RHS.getVectorLength());
334
1.83k
    break;
335
171
  case ComplexInt:
336
171
    MakeComplexInt();
337
171
    setComplexInt(RHS.getComplexIntReal(), RHS.getComplexIntImag());
338
171
    break;
339
174
  case ComplexFloat:
340
174
    MakeComplexFloat();
341
174
    setComplexFloat(RHS.getComplexFloatReal(), RHS.getComplexFloatImag());
342
174
    break;
343
36.7k
  case LValue:
344
36.7k
    MakeLValue();
345
36.7k
    if (RHS.hasLValuePath())
346
36.3k
      setLValue(RHS.getLValueBase(), RHS.getLValueOffset(), RHS.getLValuePath(),
347
36.3k
                RHS.isLValueOnePastTheEnd(), RHS.isNullPointer());
348
412
    else
349
412
      setLValue(RHS.getLValueBase(), RHS.getLValueOffset(), NoLValuePath(),
350
412
                RHS.isNullPointer());
351
36.7k
    break;
352
1.56k
  case Array:
353
1.56k
    MakeArray(RHS.getArrayInitializedElts(), RHS.getArraySize());
354
11.1k
    for (unsigned I = 0, N = RHS.getArrayInitializedElts(); I != N; 
++I9.57k
)
355
9.57k
      getArrayInitializedElt(I) = RHS.getArrayInitializedElt(I);
356
1.56k
    if (RHS.hasArrayFiller())
357
419
      getArrayFiller() = RHS.getArrayFiller();
358
1.56k
    break;
359
5.52k
  case Struct:
360
5.52k
    MakeStruct(RHS.getStructNumBases(), RHS.getStructNumFields());
361
6.08k
    for (unsigned I = 0, N = RHS.getStructNumBases(); I != N; 
++I552
)
362
552
      getStructBase(I) = RHS.getStructBase(I);
363
13.4k
    for (unsigned I = 0, N = RHS.getStructNumFields(); I != N; 
++I7.88k
)
364
7.88k
      getStructField(I) = RHS.getStructField(I);
365
5.52k
    break;
366
712
  case Union:
367
712
    MakeUnion();
368
712
    setUnion(RHS.getUnionField(), RHS.getUnionValue());
369
712
    break;
370
481
  case MemberPointer:
371
481
    MakeMemberPointer(RHS.getMemberPointerDecl(),
372
481
                      RHS.isMemberPointerToDerivedMember(),
373
481
                      RHS.getMemberPointerPath());
374
481
    break;
375
19
  case AddrLabelDiff:
376
19
    MakeAddrLabelDiff();
377
19
    setAddrLabelDiff(RHS.getAddrLabelDiffLHS(), RHS.getAddrLabelDiffRHS());
378
19
    break;
379
10.5M
  }
380
10.5M
}
381
382
110k
APValue::APValue(APValue &&RHS) : Kind(RHS.Kind), Data(RHS.Data) {
383
110k
  RHS.Kind = None;
384
110k
}
385
386
4.25M
APValue &APValue::operator=(const APValue &RHS) {
387
4.25M
  if (this != &RHS)
388
4.25M
    *this = APValue(RHS);
389
4.25M
  return *this;
390
4.25M
}
391
392
39.5M
APValue &APValue::operator=(APValue &&RHS) {
393
39.5M
  if (Kind != None && 
Kind != Indeterminate2.47M
)
394
2.46M
    DestroyDataAndMakeUninit();
395
39.5M
  Kind = RHS.Kind;
396
39.5M
  Data = RHS.Data;
397
39.5M
  RHS.Kind = None;
398
39.5M
  return *this;
399
39.5M
}
400
401
45.1M
void APValue::DestroyDataAndMakeUninit() {
402
45.1M
  if (Kind == Int)
403
43.6M
    ((APSInt *)(char *)&Data)->~APSInt();
404
1.53M
  else if (Kind == Float)
405
64.1k
    ((APFloat *)(char *)&Data)->~APFloat();
406
1.47M
  else if (Kind == FixedPoint)
407
2.90k
    ((APFixedPoint *)(char *)&Data)->~APFixedPoint();
408
1.46M
  else if (Kind == Vector)
409
5.10k
    ((Vec *)(char *)&Data)->~Vec();
410
1.46M
  else if (Kind == ComplexInt)
411
258
    ((ComplexAPSInt *)(char *)&Data)->~ComplexAPSInt();
412
1.46M
  else if (Kind == ComplexFloat)
413
366
    ((ComplexAPFloat *)(char *)&Data)->~ComplexAPFloat();
414
1.46M
  else if (Kind == LValue)
415
1.38M
    ((LV *)(char *)&Data)->~LV();
416
79.9k
  else if (Kind == Array)
417
19.9k
    ((Arr *)(char *)&Data)->~Arr();
418
59.9k
  else if (Kind == Struct)
419
55.0k
    ((StructData *)(char *)&Data)->~StructData();
420
4.94k
  else if (Kind == Union)
421
3.18k
    ((UnionData *)(char *)&Data)->~UnionData();
422
1.76k
  else if (Kind == MemberPointer)
423
1.70k
    ((MemberPointerData *)(char *)&Data)->~MemberPointerData();
424
56
  else if (Kind == AddrLabelDiff)
425
56
    ((AddrLabelDiffData *)(char *)&Data)->~AddrLabelDiffData();
426
45.1M
  Kind = None;
427
45.1M
}
428
429
376k
bool APValue::needsCleanup() const {
430
376k
  switch (getKind()) {
431
4
  case None:
432
4
  case Indeterminate:
433
12
  case AddrLabelDiff:
434
12
    return false;
435
17.2k
  case Struct:
436
17.5k
  case Union:
437
22.4k
  case Array:
438
22.8k
  case Vector:
439
22.8k
    return true;
440
341k
  case Int:
441
341k
    return getInt().needsCleanup();
442
2.18k
  case Float:
443
2.18k
    return getFloat().needsCleanup();
444
350
  case FixedPoint:
445
350
    return getFixedPoint().getValue().needsCleanup();
446
55
  case ComplexFloat:
447
55
    assert(getComplexFloatImag().needsCleanup() ==
448
55
               getComplexFloatReal().needsCleanup() &&
449
55
           "In _Complex float types, real and imaginary values always have the "
450
55
           "same size.");
451
0
    return getComplexFloatReal().needsCleanup();
452
31
  case ComplexInt:
453
31
    assert(getComplexIntImag().needsCleanup() ==
454
31
               getComplexIntReal().needsCleanup() &&
455
31
           "In _Complex int types, real and imaginary values must have the "
456
31
           "same size.");
457
0
    return getComplexIntReal().needsCleanup();
458
9.17k
  case LValue:
459
9.17k
    return reinterpret_cast<const LV *>(&Data)->hasPathPtr();
460
548
  case MemberPointer:
461
548
    return reinterpret_cast<const MemberPointerData *>(&Data)->hasPathPtr();
462
376k
  }
463
0
  llvm_unreachable("Unknown APValue kind!");
464
0
}
465
466
14.1M
void APValue::swap(APValue &RHS) {
467
14.1M
  std::swap(Kind, RHS.Kind);
468
14.1M
  std::swap(Data, RHS.Data);
469
14.1M
}
470
471
/// Profile the value of an APInt, excluding its bit-width.
472
2.70k
static void profileIntValue(llvm::FoldingSetNodeID &ID, const llvm::APInt &V) {
473
5.48k
  for (unsigned I = 0, N = V.getBitWidth(); I < N; 
I += 322.78k
)
474
2.78k
    ID.AddInteger((uint32_t)V.extractBitsAsZExtValue(std::min(32u, N - I), I));
475
2.70k
}
476
477
4.29k
void APValue::Profile(llvm::FoldingSetNodeID &ID) const {
478
  // Note that our profiling assumes that only APValues of the same type are
479
  // ever compared. As a result, we don't consider collisions that could only
480
  // happen if the types are different. (For example, structs with different
481
  // numbers of members could profile the same.)
482
483
4.29k
  ID.AddInteger(Kind);
484
485
4.29k
  switch (Kind) {
486
35
  case None:
487
35
  case Indeterminate:
488
35
    return;
489
490
0
  case AddrLabelDiff:
491
0
    ID.AddPointer(getAddrLabelDiffLHS()->getLabel()->getCanonicalDecl());
492
0
    ID.AddPointer(getAddrLabelDiffRHS()->getLabel()->getCanonicalDecl());
493
0
    return;
494
495
786
  case Struct:
496
822
    for (unsigned I = 0, N = getStructNumBases(); I != N; 
++I36
)
497
36
      getStructBase(I).Profile(ID);
498
2.36k
    for (unsigned I = 0, N = getStructNumFields(); I != N; 
++I1.57k
)
499
1.57k
      getStructField(I).Profile(ID);
500
786
    return;
501
502
84
  case Union:
503
84
    if (!getUnionField()) {
504
12
      ID.AddInteger(0);
505
12
      return;
506
12
    }
507
72
    ID.AddInteger(getUnionField()->getFieldIndex() + 1);
508
72
    getUnionValue().Profile(ID);
509
72
    return;
510
511
191
  case Array: {
512
191
    if (getArraySize() == 0)
513
0
      return;
514
515
    // The profile should not depend on whether the array is expanded or
516
    // not, but we don't want to profile the array filler many times for
517
    // a large array. So treat all equal trailing elements as the filler.
518
    // Elements are profiled in reverse order to support this, and the
519
    // first profiled element is followed by a count. For example:
520
    //
521
    //   ['a', 'c', 'x', 'x', 'x'] is profiled as
522
    //   [5, 'x', 3, 'c', 'a']
523
191
    llvm::FoldingSetNodeID FillerID;
524
191
    (hasArrayFiller() ? 
getArrayFiller()87
525
191
                      : 
getArrayInitializedElt(getArrayInitializedElts() - 1)104
)
526
191
        .Profile(FillerID);
527
191
    ID.AddNodeID(FillerID);
528
191
    unsigned NumFillers = getArraySize() - getArrayInitializedElts();
529
191
    unsigned N = getArrayInitializedElts();
530
531
    // Count the number of elements equal to the last one. This loop ends
532
    // by adding an integer indicating the number of such elements, with
533
    // N set to the number of elements left to profile.
534
326
    while (true) {
535
326
      if (N == 0) {
536
        // All elements are fillers.
537
26
        assert(NumFillers == getArraySize());
538
0
        ID.AddInteger(NumFillers);
539
26
        break;
540
26
      }
541
542
      // No need to check if the last element is equal to the last
543
      // element.
544
300
      if (N != getArraySize()) {
545
196
        llvm::FoldingSetNodeID ElemID;
546
196
        getArrayInitializedElt(N - 1).Profile(ElemID);
547
196
        if (ElemID != FillerID) {
548
165
          ID.AddInteger(NumFillers);
549
165
          ID.AddNodeID(ElemID);
550
165
          --N;
551
165
          break;
552
165
        }
553
196
      }
554
555
      // This is a filler.
556
135
      ++NumFillers;
557
135
      --N;
558
135
    }
559
560
    // Emit the remaining elements.
561
1.57k
    for (; N != 0; 
--N1.38k
)
562
1.38k
      getArrayInitializedElt(N - 1).Profile(ID);
563
191
    return;
564
191
  }
565
566
32
  case Vector:
567
121
    for (unsigned I = 0, N = getVectorLength(); I != N; 
++I89
)
568
89
      getVectorElt(I).Profile(ID);
569
32
    return;
570
571
2.53k
  case Int:
572
2.53k
    profileIntValue(ID, getInt());
573
2.53k
    return;
574
575
80
  case Float:
576
80
    profileIntValue(ID, getFloat().bitcastToAPInt());
577
80
    return;
578
579
0
  case FixedPoint:
580
0
    profileIntValue(ID, getFixedPoint().getValue());
581
0
    return;
582
583
21
  case ComplexFloat:
584
21
    profileIntValue(ID, getComplexFloatReal().bitcastToAPInt());
585
21
    profileIntValue(ID, getComplexFloatImag().bitcastToAPInt());
586
21
    return;
587
588
21
  case ComplexInt:
589
21
    profileIntValue(ID, getComplexIntReal());
590
21
    profileIntValue(ID, getComplexIntImag());
591
21
    return;
592
593
472
  case LValue:
594
472
    getLValueBase().Profile(ID);
595
472
    ID.AddInteger(getLValueOffset().getQuantity());
596
472
    ID.AddInteger((isNullPointer() ? 
122
:
0450
) |
597
472
                  (isLValueOnePastTheEnd() ? 
22
:
0470
) |
598
472
                  (hasLValuePath() ? 
4460
:
012
));
599
472
    if (hasLValuePath()) {
600
460
      ID.AddInteger(getLValuePath().size());
601
      // For uniqueness, we only need to profile the entries corresponding
602
      // to union members, but we don't have the type here so we don't know
603
      // how to interpret the entries.
604
460
      for (LValuePathEntry E : getLValuePath())
605
453
        E.Profile(ID);
606
460
    }
607
472
    return;
608
609
40
  case MemberPointer:
610
40
    ID.AddPointer(getMemberPointerDecl());
611
40
    ID.AddInteger(isMemberPointerToDerivedMember());
612
40
    for (const CXXRecordDecl *D : getMemberPointerPath())
613
8
      ID.AddPointer(D);
614
40
    return;
615
4.29k
  }
616
617
0
  llvm_unreachable("Unknown APValue kind!");
618
0
}
619
620
24
static double GetApproxValue(const llvm::APFloat &F) {
621
24
  llvm::APFloat V = F;
622
24
  bool ignored;
623
24
  V.convert(llvm::APFloat::IEEEdouble(), llvm::APFloat::rmNearestTiesToEven,
624
24
            &ignored);
625
24
  return V.convertToDouble();
626
24
}
627
628
static bool TryPrintAsStringLiteral(raw_ostream &Out,
629
                                    const PrintingPolicy &Policy,
630
                                    const ArrayType *ATy,
631
72
                                    ArrayRef<APValue> Inits) {
632
72
  if (Inits.empty())
633
0
    return false;
634
635
72
  QualType Ty = ATy->getElementType();
636
72
  if (!Ty->isAnyCharacterType())
637
14
    return false;
638
639
  // Nothing we can do about a sequence that is not null-terminated
640
58
  if (!Inits.back().getInt().isZero())
641
0
    return false;
642
58
  else
643
58
    Inits = Inits.drop_back();
644
645
58
  llvm::SmallString<40> Buf;
646
58
  Buf.push_back('"');
647
648
  // Better than printing a two-digit sequence of 10 integers.
649
58
  constexpr size_t MaxN = 36;
650
58
  StringRef Ellipsis;
651
58
  if (Inits.size() > MaxN && 
!Policy.EntireContentsOfLargeArray11
) {
652
3
    Ellipsis = "[...]";
653
3
    Inits =
654
3
        Inits.take_front(std::min(MaxN - Ellipsis.size() / 2, Inits.size()));
655
3
  }
656
657
1.00k
  for (auto &Val : Inits) {
658
1.00k
    int64_t Char64 = Val.getInt().getExtValue();
659
1.00k
    if (!isASCII(Char64))
660
3
      return false; // Bye bye, see you in integers.
661
998
    auto Ch = static_cast<unsigned char>(Char64);
662
    // The diagnostic message is 'quoted'
663
998
    StringRef Escaped = escapeCStyle<EscapeChar::SingleAndDouble>(Ch);
664
998
    if (Escaped.empty()) {
665
961
      if (!isPrintable(Ch))
666
14
        return false;
667
947
      Buf.emplace_back(Ch);
668
947
    } else {
669
37
      Buf.append(Escaped);
670
37
    }
671
998
  }
672
673
41
  Buf.append(Ellipsis);
674
41
  Buf.push_back('"');
675
676
41
  if (Ty->isWideCharType())
677
9
    Out << 'L';
678
32
  else if (Ty->isChar8Type())
679
4
    Out << "u8";
680
28
  else if (Ty->isChar16Type())
681
4
    Out << 'u';
682
24
  else if (Ty->isChar32Type())
683
4
    Out << 'U';
684
685
41
  Out << Buf;
686
41
  return true;
687
58
}
688
689
void APValue::printPretty(raw_ostream &Out, const ASTContext &Ctx,
690
3.57k
                          QualType Ty) const {
691
3.57k
  printPretty(Out, Ctx.getPrintingPolicy(), Ty, &Ctx);
692
3.57k
}
693
694
void APValue::printPretty(raw_ostream &Out, const PrintingPolicy &Policy,
695
3.97k
                          QualType Ty, const ASTContext *Ctx) const {
696
  // There are no objects of type 'void', but values of this type can be
697
  // returned from functions.
698
3.97k
  if (Ty->isVoidType()) {
699
0
    Out << "void()";
700
0
    return;
701
0
  }
702
703
3.97k
  switch (getKind()) {
704
0
  case APValue::None:
705
0
    Out << "<out of lifetime>";
706
0
    return;
707
4
  case APValue::Indeterminate:
708
4
    Out << "<uninitialized>";
709
4
    return;
710
1.59k
  case APValue::Int:
711
1.59k
    if (Ty->isBooleanType())
712
46
      Out << (getInt().getBoolValue() ? 
"true"30
:
"false"16
);
713
1.54k
    else
714
1.54k
      Out << getInt();
715
1.59k
    return;
716
20
  case APValue::Float:
717
20
    Out << GetApproxValue(getFloat());
718
20
    return;
719
0
  case APValue::FixedPoint:
720
0
    Out << getFixedPoint();
721
0
    return;
722
2
  case APValue::Vector: {
723
2
    Out << '{';
724
2
    QualType ElemTy = Ty->castAs<VectorType>()->getElementType();
725
2
    getVectorElt(0).printPretty(Out, Policy, ElemTy, Ctx);
726
8
    for (unsigned i = 1; i != getVectorLength(); 
++i6
) {
727
6
      Out << ", ";
728
6
      getVectorElt(i).printPretty(Out, Policy, ElemTy, Ctx);
729
6
    }
730
2
    Out << '}';
731
2
    return;
732
0
  }
733
2
  case APValue::ComplexInt:
734
2
    Out << getComplexIntReal() << "+" << getComplexIntImag() << "i";
735
2
    return;
736
2
  case APValue::ComplexFloat:
737
2
    Out << GetApproxValue(getComplexFloatReal()) << "+"
738
2
        << GetApproxValue(getComplexFloatImag()) << "i";
739
2
    return;
740
2.15k
  case APValue::LValue: {
741
2.15k
    bool IsReference = Ty->isReferenceType();
742
2.15k
    QualType InnerTy
743
2.15k
      = IsReference ? 
Ty.getNonReferenceType()797
:
Ty->getPointeeType()1.35k
;
744
2.15k
    if (InnerTy.isNull())
745
1.07k
      InnerTy = Ty;
746
747
2.15k
    LValueBase Base = getLValueBase();
748
2.15k
    if (!Base) {
749
103
      if (isNullPointer()) {
750
87
        Out << (Policy.Nullptr ? "nullptr" : 
"0"0
);
751
87
      } else 
if (16
IsReference16
) {
752
0
        Out << "*(" << InnerTy.stream(Policy) << "*)"
753
0
            << getLValueOffset().getQuantity();
754
16
      } else {
755
16
        Out << "(" << Ty.stream(Policy) << ")"
756
16
            << getLValueOffset().getQuantity();
757
16
      }
758
103
      return;
759
103
    }
760
761
2.04k
    if (!hasLValuePath()) {
762
      // No lvalue path: just print the offset.
763
2
      CharUnits O = getLValueOffset();
764
2
      CharUnits S = Ctx ? Ctx->getTypeSizeInCharsIfKnown(InnerTy).value_or(
765
2
                              CharUnits::Zero())
766
2
                        : 
CharUnits::Zero()0
;
767
2
      if (!O.isZero()) {
768
2
        if (IsReference)
769
0
          Out << "*(";
770
2
        if (S.isZero() || O % S) {
771
2
          Out << "(char*)";
772
2
          S = CharUnits::One();
773
2
        }
774
2
        Out << '&';
775
2
      } else 
if (0
!IsReference0
) {
776
0
        Out << '&';
777
0
      }
778
779
2
      if (const ValueDecl *VD = Base.dyn_cast<const ValueDecl*>())
780
2
        Out << *VD;
781
0
      else if (TypeInfoLValue TI = Base.dyn_cast<TypeInfoLValue>()) {
782
0
        TI.print(Out, Policy);
783
0
      } else if (DynamicAllocLValue DA = Base.dyn_cast<DynamicAllocLValue>()) {
784
0
        Out << "{*new "
785
0
            << Base.getDynamicAllocType().stream(Policy) << "#"
786
0
            << DA.getIndex() << "}";
787
0
      } else {
788
0
        assert(Base.get<const Expr *>() != nullptr &&
789
0
               "Expecting non-null Expr");
790
0
        Base.get<const Expr*>()->printPretty(Out, nullptr, Policy);
791
0
      }
792
793
2
      if (!O.isZero()) {
794
2
        Out << " + " << (O / S);
795
2
        if (IsReference)
796
0
          Out << ')';
797
2
      }
798
2
      return;
799
2
    }
800
801
    // We have an lvalue path. Print it out nicely.
802
2.04k
    if (!IsReference)
803
1.25k
      Out << '&';
804
797
    else if (isLValueOnePastTheEnd())
805
0
      Out << "*(&";
806
807
2.04k
    QualType ElemTy = Base.getType();
808
2.04k
    if (const ValueDecl *VD = Base.dyn_cast<const ValueDecl*>()) {
809
1.71k
      Out << *VD;
810
1.71k
    } else 
if (TypeInfoLValue 328
TI328
= Base.dyn_cast<TypeInfoLValue>()) {
811
36
      TI.print(Out, Policy);
812
292
    } else if (DynamicAllocLValue DA = Base.dyn_cast<DynamicAllocLValue>()) {
813
17
      Out << "{*new " << Base.getDynamicAllocType().stream(Policy) << "#"
814
17
          << DA.getIndex() << "}";
815
275
    } else {
816
275
      const Expr *E = Base.get<const Expr*>();
817
275
      assert(E != nullptr && "Expecting non-null Expr");
818
0
      E->printPretty(Out, nullptr, Policy);
819
275
    }
820
821
0
    ArrayRef<LValuePathEntry> Path = getLValuePath();
822
2.04k
    const CXXRecordDecl *CastToBase = nullptr;
823
2.33k
    for (unsigned I = 0, N = Path.size(); I != N; 
++I288
) {
824
288
      if (ElemTy->isRecordType()) {
825
        // The lvalue refers to a class type, so the next path entry is a base
826
        // or member.
827
117
        const Decl *BaseOrMember = Path[I].getAsBaseOrMember().getPointer();
828
117
        if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(BaseOrMember)) {
829
54
          CastToBase = RD;
830
          // Leave ElemTy referring to the most-derived class. The actual type
831
          // doesn't matter except for array types.
832
63
        } else {
833
63
          const ValueDecl *VD = cast<ValueDecl>(BaseOrMember);
834
63
          Out << ".";
835
63
          if (CastToBase)
836
4
            Out << *CastToBase << "::";
837
63
          Out << *VD;
838
63
          ElemTy = VD->getType();
839
63
        }
840
171
      } else {
841
        // The lvalue must refer to an array.
842
171
        Out << '[' << Path[I].getAsArrayIndex() << ']';
843
171
        ElemTy = ElemTy->castAsArrayTypeUnsafe()->getElementType();
844
171
      }
845
288
    }
846
847
    // Handle formatting of one-past-the-end lvalues.
848
2.04k
    if (isLValueOnePastTheEnd()) {
849
      // FIXME: If CastToBase is non-0, we should prefix the output with
850
      // "(CastToBase*)".
851
4
      Out << " + 1";
852
4
      if (IsReference)
853
0
        Out << ')';
854
4
    }
855
2.04k
    return;
856
2.04k
  }
857
76
  case APValue::Array: {
858
76
    const ArrayType *AT = Ty->castAsArrayTypeUnsafe();
859
76
    unsigned N = getArrayInitializedElts();
860
76
    if (N != 0 && TryPrintAsStringLiteral(Out, Policy, AT,
861
72
                                          {&getArrayInitializedElt(0), N}))
862
41
      return;
863
35
    QualType ElemTy = AT->getElementType();
864
35
    Out << '{';
865
35
    unsigned I = 0;
866
35
    switch (N) {
867
4
    case 0:
868
195
      for (; I != N; 
++I191
) {
869
164
        Out << ", ";
870
164
        if (I == 10 && 
!Policy.EntireContentsOfLargeArray7
) {
871
4
          Out << "...}";
872
4
          return;
873
4
        }
874
164
        
LLVM_FALLTHROUGH160
;160
875
191
      default:
876
191
        getArrayInitializedElt(I).printPretty(Out, Policy, ElemTy, Ctx);
877
191
      }
878
35
    }
879
31
    Out << '}';
880
31
    return;
881
35
  }
882
116
  case APValue::Struct: {
883
116
    Out << '{';
884
116
    const RecordDecl *RD = Ty->castAs<RecordType>()->getDecl();
885
116
    bool First = true;
886
116
    if (unsigned N = getStructNumBases()) {
887
2
      const CXXRecordDecl *CD = cast<CXXRecordDecl>(RD);
888
2
      CXXRecordDecl::base_class_const_iterator BI = CD->bases_begin();
889
4
      for (unsigned I = 0; I != N; 
++I, ++BI2
) {
890
2
        assert(BI != CD->bases_end());
891
2
        if (!First)
892
0
          Out << ", ";
893
2
        getStructBase(I).printPretty(Out, Policy, BI->getType(), Ctx);
894
2
        First = false;
895
2
      }
896
2
    }
897
116
    for (const auto *FI : RD->fields()) {
898
106
      if (!First)
899
13
        Out << ", ";
900
106
      if (FI->isUnnamedBitfield()) 
continue0
;
901
106
      getStructField(FI->getFieldIndex()).
902
106
        printPretty(Out, Policy, FI->getType(), Ctx);
903
106
      First = false;
904
106
    }
905
116
    Out << '}';
906
116
    return;
907
35
  }
908
11
  case APValue::Union:
909
11
    Out << '{';
910
11
    if (const FieldDecl *FD = getUnionField()) {
911
11
      Out << "." << *FD << " = ";
912
11
      getUnionValue().printPretty(Out, Policy, FD->getType(), Ctx);
913
11
    }
914
11
    Out << '}';
915
11
    return;
916
2
  case APValue::MemberPointer:
917
    // FIXME: This is not enough to unambiguously identify the member in a
918
    // multiple-inheritance scenario.
919
2
    if (const ValueDecl *VD = getMemberPointerDecl()) {
920
2
      Out << '&' << *cast<CXXRecordDecl>(VD->getDeclContext()) << "::" << *VD;
921
2
      return;
922
2
    }
923
0
    Out << "0";
924
0
    return;
925
2
  case APValue::AddrLabelDiff:
926
2
    Out << "&&" << getAddrLabelDiffLHS()->getLabel()->getName();
927
2
    Out << " - ";
928
2
    Out << "&&" << getAddrLabelDiffRHS()->getLabel()->getName();
929
2
    return;
930
3.97k
  }
931
0
  llvm_unreachable("Unknown APValue kind!");
932
0
}
933
934
558
std::string APValue::getAsString(const ASTContext &Ctx, QualType Ty) const {
935
558
  std::string Result;
936
558
  llvm::raw_string_ostream Out(Result);
937
558
  printPretty(Out, Ctx, Ty);
938
558
  Out.flush();
939
558
  return Result;
940
558
}
941
942
bool APValue::toIntegralConstant(APSInt &Result, QualType SrcTy,
943
1.07k
                                 const ASTContext &Ctx) const {
944
1.07k
  if (isInt()) {
945
261
    Result = getInt();
946
261
    return true;
947
261
  }
948
949
818
  if (isLValue() && 
isNullPointer()817
) {
950
391
    Result = Ctx.MakeIntValue(Ctx.getTargetNullPointerValue(SrcTy), SrcTy);
951
391
    return true;
952
391
  }
953
954
427
  if (isLValue() && 
!getLValueBase()426
) {
955
424
    Result = Ctx.MakeIntValue(getLValueOffset().getQuantity(), SrcTy);
956
424
    return true;
957
424
  }
958
959
3
  return false;
960
427
}
961
962
2.47M
const APValue::LValueBase APValue::getLValueBase() const {
963
2.47M
  assert(isLValue() && "Invalid accessor");
964
0
  return ((const LV *)(const void *)&Data)->Base;
965
2.47M
}
966
967
851k
bool APValue::isLValueOnePastTheEnd() const {
968
851k
  assert(isLValue() && "Invalid accessor");
969
0
  return ((const LV *)(const void *)&Data)->IsOnePastTheEnd;
970
851k
}
971
972
862k
CharUnits &APValue::getLValueOffset() {
973
862k
  assert(isLValue() && "Invalid accessor");
974
0
  return ((LV *)(void *)&Data)->Offset;
975
862k
}
976
977
2.48M
bool APValue::hasLValuePath() const {
978
2.48M
  assert(isLValue() && "Invalid accessor");
979
0
  return ((const LV *)(const char *)&Data)->hasPath();
980
2.48M
}
981
982
1.62M
ArrayRef<APValue::LValuePathEntry> APValue::getLValuePath() const {
983
1.62M
  assert(isLValue() && hasLValuePath() && "Invalid accessor");
984
0
  const LV &LVal = *((const LV *)(const char *)&Data);
985
1.62M
  return llvm::makeArrayRef(LVal.getPath(), LVal.PathLength);
986
1.62M
}
987
988
0
unsigned APValue::getLValueCallIndex() const {
989
0
  assert(isLValue() && "Invalid accessor");
990
0
  return ((const LV *)(const char *)&Data)->Base.getCallIndex();
991
0
}
992
993
0
unsigned APValue::getLValueVersion() const {
994
0
  assert(isLValue() && "Invalid accessor");
995
0
  return ((const LV *)(const char *)&Data)->Base.getVersion();
996
0
}
997
998
855k
bool APValue::isNullPointer() const {
999
855k
  assert(isLValue() && "Invalid usage");
1000
0
  return ((const LV *)(const char *)&Data)->IsNullPtr;
1001
855k
}
1002
1003
void APValue::setLValue(LValueBase B, const CharUnits &O, NoLValuePath,
1004
7.45k
                        bool IsNullPtr) {
1005
7.45k
  assert(isLValue() && "Invalid accessor");
1006
0
  LV &LVal = *((LV *)(char *)&Data);
1007
7.45k
  LVal.Base = B;
1008
7.45k
  LVal.IsOnePastTheEnd = false;
1009
7.45k
  LVal.Offset = O;
1010
7.45k
  LVal.resizePath((unsigned)-1);
1011
7.45k
  LVal.IsNullPtr = IsNullPtr;
1012
7.45k
}
1013
1014
MutableArrayRef<APValue::LValuePathEntry>
1015
APValue::setLValueUninit(LValueBase B, const CharUnits &O, unsigned Size,
1016
1.38M
                         bool IsOnePastTheEnd, bool IsNullPtr) {
1017
1.38M
  assert(isLValue() && "Invalid accessor");
1018
0
  LV &LVal = *((LV *)(char *)&Data);
1019
1.38M
  LVal.Base = B;
1020
1.38M
  LVal.IsOnePastTheEnd = IsOnePastTheEnd;
1021
1.38M
  LVal.Offset = O;
1022
1.38M
  LVal.IsNullPtr = IsNullPtr;
1023
1.38M
  LVal.resizePath(Size);
1024
1.38M
  return {LVal.getPath(), Size};
1025
1.38M
}
1026
1027
void APValue::setLValue(LValueBase B, const CharUnits &O,
1028
                        ArrayRef<LValuePathEntry> Path, bool IsOnePastTheEnd,
1029
1.38M
                        bool IsNullPtr) {
1030
1.38M
  MutableArrayRef<APValue::LValuePathEntry> InternalPath =
1031
1.38M
      setLValueUninit(B, O, Path.size(), IsOnePastTheEnd, IsNullPtr);
1032
1.38M
  if (Path.size()) {
1033
88.2k
    memcpy(InternalPath.data(), Path.data(),
1034
88.2k
           Path.size() * sizeof(LValuePathEntry));
1035
88.2k
  }
1036
1.38M
}
1037
1038
3.25k
void APValue::setUnion(const FieldDecl *Field, const APValue &Value) {
1039
3.25k
  assert(isUnion() && "Invalid accessor");
1040
0
  ((UnionData *)(char *)&Data)->Field =
1041
3.25k
      Field ? 
Field->getCanonicalDecl()2.46k
:
nullptr787
;
1042
3.25k
  *((UnionData *)(char *)&Data)->Value = Value;
1043
3.25k
}
1044
1045
3.04k
const ValueDecl *APValue::getMemberPointerDecl() const {
1046
3.04k
  assert(isMemberPointer() && "Invalid accessor");
1047
0
  const MemberPointerData &MPD =
1048
3.04k
      *((const MemberPointerData *)(const char *)&Data);
1049
3.04k
  return MPD.MemberAndIsDerivedMember.getPointer();
1050
3.04k
}
1051
1052
1.00k
bool APValue::isMemberPointerToDerivedMember() const {
1053
1.00k
  assert(isMemberPointer() && "Invalid accessor");
1054
0
  const MemberPointerData &MPD =
1055
1.00k
      *((const MemberPointerData *)(const char *)&Data);
1056
1.00k
  return MPD.MemberAndIsDerivedMember.getInt();
1057
1.00k
}
1058
1059
1.14k
ArrayRef<const CXXRecordDecl*> APValue::getMemberPointerPath() const {
1060
1.14k
  assert(isMemberPointer() && "Invalid accessor");
1061
0
  const MemberPointerData &MPD =
1062
1.14k
      *((const MemberPointerData *)(const char *)&Data);
1063
1.14k
  return llvm::makeArrayRef(MPD.getPath(), MPD.PathLength);
1064
1.14k
}
1065
1066
1.39M
void APValue::MakeLValue() {
1067
1.39M
  assert(isAbsent() && "Bad state change");
1068
0
  static_assert(sizeof(LV) <= DataSize, "LV too big");
1069
1.39M
  new ((void *)(char *)&Data) LV();
1070
1.39M
  Kind = LValue;
1071
1.39M
}
1072
1073
19.9k
void APValue::MakeArray(unsigned InitElts, unsigned Size) {
1074
19.9k
  assert(isAbsent() && "Bad state change");
1075
0
  new ((void *)(char *)&Data) Arr(InitElts, Size);
1076
19.9k
  Kind = Array;
1077
19.9k
}
1078
1079
MutableArrayRef<APValue::LValuePathEntry>
1080
setLValueUninit(APValue::LValueBase B, const CharUnits &O, unsigned Size,
1081
                bool OnePastTheEnd, bool IsNullPtr);
1082
1083
MutableArrayRef<const CXXRecordDecl *>
1084
APValue::setMemberPointerUninit(const ValueDecl *Member, bool IsDerivedMember,
1085
2.24k
                                unsigned Size) {
1086
2.24k
  assert(isAbsent() && "Bad state change");
1087
0
  MemberPointerData *MPD = new ((void *)(char *)&Data) MemberPointerData;
1088
2.24k
  Kind = MemberPointer;
1089
2.24k
  MPD->MemberAndIsDerivedMember.setPointer(
1090
2.24k
      Member ? 
cast<ValueDecl>(Member->getCanonicalDecl())1.87k
:
nullptr377
);
1091
2.24k
  MPD->MemberAndIsDerivedMember.setInt(IsDerivedMember);
1092
2.24k
  MPD->resizePath(Size);
1093
2.24k
  return {MPD->getPath(), MPD->PathLength};
1094
2.24k
}
1095
1096
void APValue::MakeMemberPointer(const ValueDecl *Member, bool IsDerivedMember,
1097
2.24k
                                ArrayRef<const CXXRecordDecl *> Path) {
1098
2.24k
  MutableArrayRef<const CXXRecordDecl *> InternalPath =
1099
2.24k
      setMemberPointerUninit(Member, IsDerivedMember, Path.size());
1100
3.74k
  for (unsigned I = 0; I != Path.size(); 
++I1.49k
)
1101
1.49k
    InternalPath[I] = Path[I]->getCanonicalDecl();
1102
2.24k
}
1103
1104
LinkageInfo LinkageComputer::getLVForValue(const APValue &V,
1105
3.40k
                                           LVComputationKind computation) {
1106
3.40k
  LinkageInfo LV = LinkageInfo::external();
1107
1108
3.40k
  auto MergeLV = [&](LinkageInfo MergeLV) {
1109
2.73k
    LV.merge(MergeLV);
1110
2.73k
    return LV.getLinkage() == InternalLinkage;
1111
2.73k
  };
1112
3.40k
  auto Merge = [&](const APValue &V) {
1113
2.61k
    return MergeLV(getLVForValue(V, computation));
1114
2.61k
  };
1115
1116
3.40k
  switch (V.getKind()) {
1117
120
  case APValue::None:
1118
120
  case APValue::Indeterminate:
1119
1.36k
  case APValue::Int:
1120
1.60k
  case APValue::Float:
1121
1.60k
  case APValue::FixedPoint:
1122
1.68k
  case APValue::ComplexInt:
1123
1.76k
  case APValue::ComplexFloat:
1124
1.87k
  case APValue::Vector:
1125
1.87k
    break;
1126
1127
0
  case APValue::AddrLabelDiff:
1128
    // Even for an inline function, it's not reasonable to treat a difference
1129
    // between the addresses of labels as an external value.
1130
0
    return LinkageInfo::internal();
1131
1132
966
  case APValue::Struct: {
1133
1.08k
    for (unsigned I = 0, N = V.getStructNumBases(); I != N; 
++I120
)
1134
120
      if (Merge(V.getStructBase(I)))
1135
0
        break;
1136
2.70k
    for (unsigned I = 0, N = V.getStructNumFields(); I != N; 
++I1.73k
)
1137
1.73k
      if (Merge(V.getStructField(I)))
1138
0
        break;
1139
966
    break;
1140
1.76k
  }
1141
1142
248
  case APValue::Union:
1143
248
    if (V.getUnionField())
1144
208
      Merge(V.getUnionValue());
1145
248
    break;
1146
1147
119
  case APValue::Array: {
1148
609
    for (unsigned I = 0, N = V.getArrayInitializedElts(); I != N; 
++I490
)
1149
490
      if (Merge(V.getArrayInitializedElt(I)))
1150
0
        break;
1151
119
    if (V.hasArrayFiller())
1152
60
      Merge(V.getArrayFiller());
1153
119
    break;
1154
1.76k
  }
1155
1156
138
  case APValue::LValue: {
1157
138
    if (!V.getLValueBase()) {
1158
      // Null or absolute address: this is external.
1159
88
    } else if (const auto *VD =
1160
88
                   V.getLValueBase().dyn_cast<const ValueDecl *>()) {
1161
88
      if (VD && MergeLV(getLVForDecl(VD, computation)))
1162
0
        break;
1163
88
    } else 
if (const auto 0
TI0
= V.getLValueBase().dyn_cast<TypeInfoLValue>()) {
1164
0
      if (MergeLV(getLVForType(*TI.getType(), computation)))
1165
0
        break;
1166
0
    } else if (const Expr *E = V.getLValueBase().dyn_cast<const Expr *>()) {
1167
      // Almost all expression bases are internal. The exception is
1168
      // lifetime-extended temporaries.
1169
      // FIXME: These should be modeled as having the
1170
      // LifetimeExtendedTemporaryDecl itself as the base.
1171
      // FIXME: If we permit Objective-C object literals in template arguments,
1172
      // they should not imply internal linkage.
1173
0
      auto *MTE = dyn_cast<MaterializeTemporaryExpr>(E);
1174
0
      if (!MTE || MTE->getStorageDuration() == SD_FullExpression)
1175
0
        return LinkageInfo::internal();
1176
0
      if (MergeLV(getLVForDecl(MTE->getExtendingDecl(), computation)))
1177
0
        break;
1178
0
    } else {
1179
0
      assert(V.getLValueBase().is<DynamicAllocLValue>() &&
1180
0
             "unexpected LValueBase kind");
1181
0
      return LinkageInfo::internal();
1182
0
    }
1183
    // The lvalue path doesn't matter: pointers to all subobjects always have
1184
    // the same visibility as pointers to the complete object.
1185
138
    break;
1186
138
  }
1187
1188
138
  case APValue::MemberPointer:
1189
58
    if (const NamedDecl *D = V.getMemberPointerDecl())
1190
33
      MergeLV(getLVForDecl(D, computation));
1191
    // Note that we could have a base-to-derived conversion here to a member of
1192
    // a derived class with less linkage/visibility. That's covered by the
1193
    // linkage and visibility of the value's type.
1194
58
    break;
1195
3.40k
  }
1196
1197
3.40k
  return LV;
1198
3.40k
}