Coverage Report

Created: 2022-05-17 06:19

/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
617
    : 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
32.6M
    : Ptr(P ? cast<ValueDecl>(P->getCanonicalDecl()) : nullptr), Local{I, V} {}
44
APValue::LValueBase::LValueBase(const Expr *P, unsigned I, unsigned V)
45
2.02M
    : 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
617
                                                     QualType TypeInfo) {
57
617
  LValueBase Base;
58
617
  Base.Ptr = LV;
59
617
  Base.TypeInfoType = TypeInfo.getAsOpaquePtr();
60
617
  return Base;
61
617
}
62
63
18.8M
QualType APValue::LValueBase::getType() const {
64
18.8M
  if (!*this) 
return QualType()21
;
65
18.8M
  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
16.6M
    for (auto *Redecl = cast<ValueDecl>(D->getMostRecentDecl()); Redecl;
74
16.6M
         
Redecl = cast_or_null<ValueDecl>(Redecl->getPreviousDecl())2.46k
) {
75
16.6M
      QualType T = Redecl->getType();
76
16.6M
      if (!T->isIncompleteArrayType())
77
16.6M
        return T;
78
16.6M
    }
79
2.01k
    return D->getType();
80
16.6M
  }
81
82
2.17M
  if (is<TypeInfoLValue>())
83
1.07k
    return getTypeInfoType();
84
85
2.17M
  if (is<DynamicAllocLValue>())
86
51.1k
    return getDynamicAllocType();
87
88
2.11M
  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.11M
  if (const MaterializeTemporaryExpr *MTE =
93
2.11M
          clang::dyn_cast<MaterializeTemporaryExpr>(Base)) {
94
1.99M
    SmallVector<const Expr *, 2> CommaLHSs;
95
1.99M
    SmallVector<SubobjectAdjustment, 2> Adjustments;
96
1.99M
    const Expr *Temp = MTE->getSubExpr();
97
1.99M
    const Expr *Inner = Temp->skipRValueSubobjectAdjustments(CommaLHSs,
98
1.99M
                                                             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
1.99M
    if (!Adjustments.empty())
102
3.21k
      return Inner->getType();
103
1.99M
  }
104
105
2.11M
  return Base->getType();
106
2.11M
}
107
108
10.4M
unsigned APValue::LValueBase::getCallIndex() const {
109
10.4M
  return (is<TypeInfoLValue>() || 
is<DynamicAllocLValue>()10.4M
) ?
045.5k
110
10.4M
                                                            : 
Local.CallIndex10.3M
;
111
10.4M
}
112
113
5.07M
unsigned APValue::LValueBase::getVersion() const {
114
5.07M
  return (is<TypeInfoLValue>() || 
is<DynamicAllocLValue>()5.07M
) ?
052
:
Local.Version5.07M
;
115
5.07M
}
116
117
1.22k
QualType APValue::LValueBase::getTypeInfoType() const {
118
1.22k
  assert(is<TypeInfoLValue>() && "not a type_info lvalue");
119
0
  return QualType::getFromOpaquePtr(TypeInfoType);
120
1.22k
}
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
446
void APValue::LValueBase::Profile(llvm::FoldingSetNodeID &ID) const {
128
446
  ID.AddPointer(Ptr.getOpaqueValue());
129
446
  if (is<TypeInfoLValue>() || is<DynamicAllocLValue>())
130
0
    return;
131
446
  ID.AddInteger(Local.CallIndex);
132
446
  ID.AddInteger(Local.Version);
133
446
}
134
135
namespace clang {
136
bool operator==(const APValue::LValueBase &LHS,
137
7.11M
                const APValue::LValueBase &RHS) {
138
7.11M
  if (LHS.Ptr != RHS.Ptr)
139
5.84M
    return false;
140
1.26M
  if (LHS.is<TypeInfoLValue>() || LHS.is<DynamicAllocLValue>())
141
490
    return true;
142
1.26M
  return LHS.Local.CallIndex == RHS.Local.CallIndex &&
143
1.26M
         
LHS.Local.Version == RHS.Local.Version1.26M
;
144
1.26M
}
145
}
146
147
174k
APValue::LValuePathEntry::LValuePathEntry(BaseOrMemberType BaseOrMember) {
148
174k
  if (const Decl *D = BaseOrMember.getPointer())
149
174k
    BaseOrMember.setPointer(D->getCanonicalDecl());
150
174k
  Value = reinterpret_cast<uintptr_t>(BaseOrMember.getOpaqueValue());
151
174k
}
152
153
422
void APValue::LValuePathEntry::Profile(llvm::FoldingSetNodeID &ID) const {
154
422
  ID.AddInteger(Value);
155
422
}
156
157
APValue::LValuePathSerializationHelper::LValuePathSerializationHelper(
158
    ArrayRef<LValuePathEntry> Path, QualType ElemTy)
159
29
    : ElemTy((const void *)ElemTy.getTypePtrOrNull()), Path(Path) {}
160
161
29
QualType APValue::LValuePathSerializationHelper::getType() {
162
29
  return QualType::getFromOpaquePtr(ElemTy);
163
29
}
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
108k
void *APValue::LValueBase::getOpaqueValue() const {
176
108k
  return Ptr.getOpaqueValue();
177
108k
}
178
179
7.91k
bool APValue::LValueBase::isNull() const {
180
7.91k
  return Ptr.isNull();
181
7.91k
}
182
183
27.7M
APValue::LValueBase::operator bool () const {
184
27.7M
  return static_cast<bool>(Ptr);
185
27.7M
}
186
187
clang::APValue::LValueBase
188
157k
llvm::DenseMapInfo<clang::APValue::LValueBase>::getEmptyKey() {
189
157k
  clang::APValue::LValueBase B;
190
157k
  B.Ptr = DenseMapInfo<const ValueDecl*>::getEmptyKey();
191
157k
  return B;
192
157k
}
193
194
clang::APValue::LValueBase
195
140k
llvm::DenseMapInfo<clang::APValue::LValueBase>::getTombstoneKey() {
196
140k
  clang::APValue::LValueBase B;
197
140k
  B.Ptr = DenseMapInfo<const ValueDecl*>::getTombstoneKey();
198
140k
  return B;
199
140k
}
200
201
namespace clang {
202
98.3k
llvm::hash_code hash_value(const APValue::LValueBase &Base) {
203
98.3k
  if (Base.is<TypeInfoLValue>() || Base.is<DynamicAllocLValue>())
204
750
    return llvm::hash_value(Base.getOpaqueValue());
205
97.6k
  return llvm::hash_combine(Base.getOpaqueValue(), Base.getCallIndex(),
206
97.6k
                            Base.getVersion());
207
98.3k
}
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.28M
  LV() { PathLength = (unsigned)-1; }
234
1.27M
  ~LV() { resizePath(0); }
235
236
2.55M
  void resizePath(unsigned Length) {
237
2.55M
    if (Length == PathLength)
238
1.19M
      return;
239
1.36M
    if (hasPathPtr())
240
148
      delete [] PathPtr;
241
1.36M
    PathLength = Length;
242
1.36M
    if (hasPathPtr())
243
148
      PathPtr = new LValuePathEntry[Length];
244
1.36M
  }
245
246
7.68M
  bool hasPath() const { return PathLength != (unsigned)-1; }
247
5.46M
  bool hasPathPtr() const { return hasPath() && 
PathLength > InlinePathSpace4.17M
; }
248
249
1.27M
  LValuePathEntry *getPath() { return hasPathPtr() ? 
PathPtr148
:
Path1.27M
; }
250
1.45M
  const LValuePathEntry *getPath() const {
251
1.45M
    return hasPathPtr() ? 
PathPtr343
:
Path1.45M
;
252
1.45M
  }
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.15k
  MemberPointerData() { PathLength = 0; }
272
1.61k
  ~MemberPointerData() { resizePath(0); }
273
274
3.77k
  void resizePath(unsigned Length) {
275
3.77k
    if (Length == PathLength)
276
3.13k
      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.08k
  bool hasPathPtr() const { return PathLength > InlinePathSpace; }
285
286
2.15k
  PathElem *getPath() { return hasPathPtr() ? 
PathPtr91
:
Path2.06k
; }
287
1.11k
  const PathElem *getPath() const {
288
1.11k
    return hasPathPtr() ? 
PathPtr168
:
Path947
;
289
1.11k
  }
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.1k
  NumElts(NumElts), ArrSize(Size) {}
297
19.1k
APValue::Arr::~Arr() { delete [] Elts; }
298
299
APValue::StructData::StructData(unsigned NumBases, unsigned NumFields) :
300
  Elts(new APValue[NumBases+NumFields]),
301
48.2k
  NumBases(NumBases), NumFields(NumFields) {}
302
48.2k
APValue::StructData::~StructData() {
303
48.2k
  delete [] Elts;
304
48.2k
}
305
306
3.08k
APValue::UnionData::UnionData() : Field(nullptr), Value(new APValue) {}
307
3.08k
APValue::UnionData::~UnionData () {
308
3.08k
  delete Value;
309
3.08k
}
310
311
9.51M
APValue::APValue(const APValue &RHS) : Kind(None) {
312
9.51M
  switch (RHS.getKind()) {
313
9.49k
  case None:
314
25.8k
  case Indeterminate:
315
25.8k
    Kind = RHS.getKind();
316
25.8k
    break;
317
9.42M
  case Int:
318
9.42M
    MakeInt();
319
9.42M
    setInt(RHS.getInt());
320
9.42M
    break;
321
16.9k
  case Float:
322
16.9k
    MakeFloat();
323
16.9k
    setFloat(RHS.getFloat());
324
16.9k
    break;
325
6
  case FixedPoint: {
326
6
    APFixedPoint FXCopy = RHS.getFixedPoint();
327
6
    MakeFixedPoint(std::move(FXCopy));
328
6
    break;
329
9.49k
  }
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.2k
  case LValue:
344
36.2k
    MakeLValue();
345
36.2k
    if (RHS.hasLValuePath())
346
35.8k
      setLValue(RHS.getLValueBase(), RHS.getLValueOffset(), RHS.getLValuePath(),
347
35.8k
                RHS.isLValueOnePastTheEnd(), RHS.isNullPointer());
348
412
    else
349
412
      setLValue(RHS.getLValueBase(), RHS.getLValueOffset(), NoLValuePath(),
350
412
                RHS.isNullPointer());
351
36.2k
    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.29k
  case Struct:
360
5.29k
    MakeStruct(RHS.getStructNumBases(), RHS.getStructNumFields());
361
5.77k
    for (unsigned I = 0, N = RHS.getStructNumBases(); I != N; 
++I484
)
362
484
      getStructBase(I) = RHS.getStructBase(I);
363
12.9k
    for (unsigned I = 0, N = RHS.getStructNumFields(); I != N; 
++I7.65k
)
364
7.65k
      getStructField(I) = RHS.getStructField(I);
365
5.29k
    break;
366
680
  case Union:
367
680
    MakeUnion();
368
680
    setUnion(RHS.getUnionField(), RHS.getUnionValue());
369
680
    break;
370
469
  case MemberPointer:
371
469
    MakeMemberPointer(RHS.getMemberPointerDecl(),
372
469
                      RHS.isMemberPointerToDerivedMember(),
373
469
                      RHS.getMemberPointerPath());
374
469
    break;
375
19
  case AddrLabelDiff:
376
19
    MakeAddrLabelDiff();
377
19
    setAddrLabelDiff(RHS.getAddrLabelDiffLHS(), RHS.getAddrLabelDiffRHS());
378
19
    break;
379
9.51M
  }
380
9.51M
}
381
382
107k
APValue::APValue(APValue &&RHS) : Kind(RHS.Kind), Data(RHS.Data) {
383
107k
  RHS.Kind = None;
384
107k
}
385
386
3.57M
APValue &APValue::operator=(const APValue &RHS) {
387
3.57M
  if (this != &RHS)
388
3.57M
    *this = APValue(RHS);
389
3.57M
  return *this;
390
3.57M
}
391
392
36.0M
APValue &APValue::operator=(APValue &&RHS) {
393
36.0M
  if (Kind != None && 
Kind != Indeterminate2.33M
)
394
2.33M
    DestroyDataAndMakeUninit();
395
36.0M
  Kind = RHS.Kind;
396
36.0M
  Data = RHS.Data;
397
36.0M
  RHS.Kind = None;
398
36.0M
  return *this;
399
36.0M
}
400
401
41.7M
void APValue::DestroyDataAndMakeUninit() {
402
41.7M
  if (Kind == Int)
403
40.3M
    ((APSInt *)(char *)&Data)->~APSInt();
404
1.41M
  else if (Kind == Float)
405
61.6k
    ((APFloat *)(char *)&Data)->~APFloat();
406
1.35M
  else if (Kind == FixedPoint)
407
2.90k
    ((APFixedPoint *)(char *)&Data)->~APFixedPoint();
408
1.35M
  else if (Kind == Vector)
409
4.89k
    ((Vec *)(char *)&Data)->~Vec();
410
1.34M
  else if (Kind == ComplexInt)
411
258
    ((ComplexAPSInt *)(char *)&Data)->~ComplexAPSInt();
412
1.34M
  else if (Kind == ComplexFloat)
413
362
    ((ComplexAPFloat *)(char *)&Data)->~ComplexAPFloat();
414
1.34M
  else if (Kind == LValue)
415
1.27M
    ((LV *)(char *)&Data)->~LV();
416
72.1k
  else if (Kind == Array)
417
19.1k
    ((Arr *)(char *)&Data)->~Arr();
418
52.9k
  else if (Kind == Struct)
419
48.2k
    ((StructData *)(char *)&Data)->~StructData();
420
4.75k
  else if (Kind == Union)
421
3.08k
    ((UnionData *)(char *)&Data)->~UnionData();
422
1.67k
  else if (Kind == MemberPointer)
423
1.61k
    ((MemberPointerData *)(char *)&Data)->~MemberPointerData();
424
56
  else if (Kind == AddrLabelDiff)
425
56
    ((AddrLabelDiffData *)(char *)&Data)->~AddrLabelDiffData();
426
41.7M
  Kind = None;
427
41.7M
}
428
429
292k
bool APValue::needsCleanup() const {
430
292k
  switch (getKind()) {
431
4
  case None:
432
4
  case Indeterminate:
433
12
  case AddrLabelDiff:
434
12
    return false;
435
12.2k
  case Struct:
436
12.5k
  case Union:
437
17.3k
  case Array:
438
17.7k
  case Vector:
439
17.7k
    return true;
440
262k
  case Int:
441
262k
    return getInt().needsCleanup();
442
2.07k
  case Float:
443
2.07k
    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
8.73k
  case LValue:
459
8.73k
    return reinterpret_cast<const LV *>(&Data)->hasPathPtr();
460
543
  case MemberPointer:
461
543
    return reinterpret_cast<const MemberPointerData *>(&Data)->hasPathPtr();
462
292k
  }
463
0
  llvm_unreachable("Unknown APValue kind!");
464
0
}
465
466
12.6M
void APValue::swap(APValue &RHS) {
467
12.6M
  std::swap(Kind, RHS.Kind);
468
12.6M
  std::swap(Data, RHS.Data);
469
12.6M
}
470
471
/// Profile the value of an APInt, excluding its bit-width.
472
3.02k
static void profileIntValue(llvm::FoldingSetNodeID &ID, const llvm::APInt &V) {
473
6.12k
  for (unsigned I = 0, N = V.getBitWidth(); I < N; 
I += 323.09k
)
474
3.09k
    ID.AddInteger((uint32_t)V.extractBitsAsZExtValue(std::min(32u, N - I), I));
475
3.02k
}
476
477
4.58k
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.58k
  ID.AddInteger(Kind);
484
485
4.58k
  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
765
  case Struct:
496
792
    for (unsigned I = 0, N = getStructNumBases(); I != N; 
++I27
)
497
27
      getStructBase(I).Profile(ID);
498
2.27k
    for (unsigned I = 0, N = getStructNumFields(); I != N; 
++I1.50k
)
499
1.50k
      getStructField(I).Profile(ID);
500
765
    return;
501
502
85
  case Union:
503
85
    if (!getUnionField()) {
504
10
      ID.AddInteger(0);
505
10
      return;
506
10
    }
507
75
    ID.AddInteger(getUnionField()->getFieldIndex() + 1);
508
75
    getUnionValue().Profile(ID);
509
75
    return;
510
511
204
  case Array: {
512
204
    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
204
    llvm::FoldingSetNodeID FillerID;
524
204
    (hasArrayFiller() ? 
getArrayFiller()81
525
204
                      : 
getArrayInitializedElt(getArrayInitializedElts() - 1)123
)
526
204
        .Profile(FillerID);
527
204
    ID.AddNodeID(FillerID);
528
204
    unsigned NumFillers = getArraySize() - getArrayInitializedElts();
529
204
    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
362
    while (true) {
535
362
      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
336
      if (N != getArraySize()) {
545
213
        llvm::FoldingSetNodeID ElemID;
546
213
        getArrayInitializedElt(N - 1).Profile(ElemID);
547
213
        if (ElemID != FillerID) {
548
178
          ID.AddInteger(NumFillers);
549
178
          ID.AddNodeID(ElemID);
550
178
          --N;
551
178
          break;
552
178
        }
553
213
      }
554
555
      // This is a filler.
556
158
      ++NumFillers;
557
158
      --N;
558
158
    }
559
560
    // Emit the remaining elements.
561
1.92k
    for (; N != 0; 
--N1.72k
)
562
1.72k
      getArrayInitializedElt(N - 1).Profile(ID);
563
204
    return;
564
204
  }
565
566
32
  case Vector:
567
129
    for (unsigned I = 0, N = getVectorLength(); I != N; 
++I97
)
568
97
      getVectorElt(I).Profile(ID);
569
32
    return;
570
571
2.85k
  case Int:
572
2.85k
    profileIntValue(ID, getInt());
573
2.85k
    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
23
  case ComplexFloat:
584
23
    profileIntValue(ID, getComplexFloatReal().bitcastToAPInt());
585
23
    profileIntValue(ID, getComplexFloatImag().bitcastToAPInt());
586
23
    return;
587
588
23
  case ComplexInt:
589
23
    profileIntValue(ID, getComplexIntReal());
590
23
    profileIntValue(ID, getComplexIntImag());
591
23
    return;
592
593
446
  case LValue:
594
446
    getLValueBase().Profile(ID);
595
446
    ID.AddInteger(getLValueOffset().getQuantity());
596
446
    ID.AddInteger((isNullPointer() ? 
124
:
0422
) |
597
446
                  (isLValueOnePastTheEnd() ? 
22
:
0444
) |
598
446
                  (hasLValuePath() ? 
4438
:
08
));
599
446
    if (hasLValuePath()) {
600
438
      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
438
      for (LValuePathEntry E : getLValuePath())
605
422
        E.Profile(ID);
606
438
    }
607
446
    return;
608
609
37
  case MemberPointer:
610
37
    ID.AddPointer(getMemberPointerDecl());
611
37
    ID.AddInteger(isMemberPointerToDerivedMember());
612
37
    for (const CXXRecordDecl *D : getMemberPointerPath())
613
6
      ID.AddPointer(D);
614
37
    return;
615
4.58k
  }
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.31k
                          QualType Ty) const {
691
3.31k
  printPretty(Out, Ctx.getPrintingPolicy(), Ty, &Ctx);
692
3.31k
}
693
694
void APValue::printPretty(raw_ostream &Out, const PrintingPolicy &Policy,
695
3.71k
                          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.71k
  if (Ty->isVoidType()) {
699
0
    Out << "void()";
700
0
    return;
701
0
  }
702
703
3.71k
  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.56k
  case APValue::Int:
711
1.56k
    if (Ty->isBooleanType())
712
46
      Out << (getInt().getBoolValue() ? 
"true"30
:
"false"16
);
713
1.52k
    else
714
1.52k
      Out << getInt();
715
1.56k
    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
1.91k
  case APValue::LValue: {
741
1.91k
    bool IsReference = Ty->isReferenceType();
742
1.91k
    QualType InnerTy
743
1.91k
      = IsReference ? 
Ty.getNonReferenceType()794
:
Ty->getPointeeType()1.11k
;
744
1.91k
    if (InnerTy.isNull())
745
840
      InnerTy = Ty;
746
747
1.91k
    LValueBase Base = getLValueBase();
748
1.91k
    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
1.80k
    if (!hasLValuePath()) {
762
      // No lvalue path: just print the offset.
763
2
      CharUnits O = getLValueOffset();
764
2
      CharUnits S = Ctx ? Ctx->getTypeSizeInCharsIfKnown(InnerTy).getValueOr(
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
1.80k
    if (!IsReference)
803
1.01k
      Out << '&';
804
794
    else if (isLValueOnePastTheEnd())
805
0
      Out << "*(&";
806
807
1.80k
    QualType ElemTy = Base.getType();
808
1.80k
    if (const ValueDecl *VD = Base.dyn_cast<const ValueDecl*>()) {
809
1.47k
      Out << *VD;
810
1.47k
    } 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
1.80k
    const CXXRecordDecl *CastToBase = nullptr;
823
2.07k
    for (unsigned I = 0, N = Path.size(); I != N; 
++I266
) {
824
266
      if (ElemTy->isRecordType()) {
825
        // The lvalue refers to a class type, so the next path entry is a base
826
        // or member.
827
95
        const Decl *BaseOrMember = Path[I].getAsBaseOrMember().getPointer();
828
95
        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
54
        } else {
833
41
          const ValueDecl *VD = cast<ValueDecl>(BaseOrMember);
834
41
          Out << ".";
835
41
          if (CastToBase)
836
4
            Out << *CastToBase << "::";
837
41
          Out << *VD;
838
41
          ElemTy = VD->getType();
839
41
        }
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
266
    }
846
847
    // Handle formatting of one-past-the-end lvalues.
848
1.80k
    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
1.80k
    return;
856
1.80k
  }
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.71k
  }
931
0
  llvm_unreachable("Unknown APValue kind!");
932
0
}
933
934
554
std::string APValue::getAsString(const ASTContext &Ctx, QualType Ty) const {
935
554
  std::string Result;
936
554
  llvm::raw_string_ostream Out(Result);
937
554
  printPretty(Out, Ctx, Ty);
938
554
  Out.flush();
939
554
  return Result;
940
554
}
941
942
bool APValue::toIntegralConstant(APSInt &Result, QualType SrcTy,
943
1.06k
                                 const ASTContext &Ctx) const {
944
1.06k
  if (isInt()) {
945
249
    Result = getInt();
946
249
    return true;
947
249
  }
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.21M
const APValue::LValueBase APValue::getLValueBase() const {
963
2.21M
  assert(isLValue() && "Invalid accessor");
964
0
  return ((const LV *)(const void *)&Data)->Base;
965
2.21M
}
966
967
758k
bool APValue::isLValueOnePastTheEnd() const {
968
758k
  assert(isLValue() && "Invalid accessor");
969
0
  return ((const LV *)(const void *)&Data)->IsOnePastTheEnd;
970
758k
}
971
972
769k
CharUnits &APValue::getLValueOffset() {
973
769k
  assert(isLValue() && "Invalid accessor");
974
0
  return ((LV *)(void *)&Data)->Offset;
975
769k
}
976
977
2.22M
bool APValue::hasLValuePath() const {
978
2.22M
  assert(isLValue() && "Invalid accessor");
979
0
  return ((const LV *)(const char *)&Data)->hasPath();
980
2.22M
}
981
982
1.45M
ArrayRef<APValue::LValuePathEntry> APValue::getLValuePath() const {
983
1.45M
  assert(isLValue() && hasLValuePath() && "Invalid accessor");
984
0
  const LV &LVal = *((const LV *)(const char *)&Data);
985
1.45M
  return llvm::makeArrayRef(LVal.getPath(), LVal.PathLength);
986
1.45M
}
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
763k
bool APValue::isNullPointer() const {
999
763k
  assert(isLValue() && "Invalid usage");
1000
0
  return ((const LV *)(const char *)&Data)->IsNullPtr;
1001
763k
}
1002
1003
void APValue::setLValue(LValueBase B, const CharUnits &O, NoLValuePath,
1004
6.73k
                        bool IsNullPtr) {
1005
6.73k
  assert(isLValue() && "Invalid accessor");
1006
0
  LV &LVal = *((LV *)(char *)&Data);
1007
6.73k
  LVal.Base = B;
1008
6.73k
  LVal.IsOnePastTheEnd = false;
1009
6.73k
  LVal.Offset = O;
1010
6.73k
  LVal.resizePath((unsigned)-1);
1011
6.73k
  LVal.IsNullPtr = IsNullPtr;
1012
6.73k
}
1013
1014
MutableArrayRef<APValue::LValuePathEntry>
1015
APValue::setLValueUninit(LValueBase B, const CharUnits &O, unsigned Size,
1016
1.27M
                         bool IsOnePastTheEnd, bool IsNullPtr) {
1017
1.27M
  assert(isLValue() && "Invalid accessor");
1018
0
  LV &LVal = *((LV *)(char *)&Data);
1019
1.27M
  LVal.Base = B;
1020
1.27M
  LVal.IsOnePastTheEnd = IsOnePastTheEnd;
1021
1.27M
  LVal.Offset = O;
1022
1.27M
  LVal.IsNullPtr = IsNullPtr;
1023
1.27M
  LVal.resizePath(Size);
1024
1.27M
  return {LVal.getPath(), Size};
1025
1.27M
}
1026
1027
void APValue::setLValue(LValueBase B, const CharUnits &O,
1028
                        ArrayRef<LValuePathEntry> Path, bool IsOnePastTheEnd,
1029
1.27M
                        bool IsNullPtr) {
1030
1.27M
  MutableArrayRef<APValue::LValuePathEntry> InternalPath =
1031
1.27M
      setLValueUninit(B, O, Path.size(), IsOnePastTheEnd, IsNullPtr);
1032
1.27M
  if (Path.size()) {
1033
81.9k
    memcpy(InternalPath.data(), Path.data(),
1034
81.9k
           Path.size() * sizeof(LValuePathEntry));
1035
81.9k
  }
1036
1.27M
}
1037
1038
3.15k
void APValue::setUnion(const FieldDecl *Field, const APValue &Value) {
1039
3.15k
  assert(isUnion() && "Invalid accessor");
1040
0
  ((UnionData *)(char *)&Data)->Field =
1041
3.15k
      Field ? 
Field->getCanonicalDecl()2.41k
:
nullptr734
;
1042
3.15k
  *((UnionData *)(char *)&Data)->Value = Value;
1043
3.15k
}
1044
1045
3.00k
const ValueDecl *APValue::getMemberPointerDecl() const {
1046
3.00k
  assert(isMemberPointer() && "Invalid accessor");
1047
0
  const MemberPointerData &MPD =
1048
3.00k
      *((const MemberPointerData *)(const char *)&Data);
1049
3.00k
  return MPD.MemberAndIsDerivedMember.getPointer();
1050
3.00k
}
1051
1052
978
bool APValue::isMemberPointerToDerivedMember() const {
1053
978
  assert(isMemberPointer() && "Invalid accessor");
1054
0
  const MemberPointerData &MPD =
1055
978
      *((const MemberPointerData *)(const char *)&Data);
1056
978
  return MPD.MemberAndIsDerivedMember.getInt();
1057
978
}
1058
1059
1.11k
ArrayRef<const CXXRecordDecl*> APValue::getMemberPointerPath() const {
1060
1.11k
  assert(isMemberPointer() && "Invalid accessor");
1061
0
  const MemberPointerData &MPD =
1062
1.11k
      *((const MemberPointerData *)(const char *)&Data);
1063
1.11k
  return llvm::makeArrayRef(MPD.getPath(), MPD.PathLength);
1064
1.11k
}
1065
1066
1.28M
void APValue::MakeLValue() {
1067
1.28M
  assert(isAbsent() && "Bad state change");
1068
0
  static_assert(sizeof(LV) <= DataSize, "LV too big");
1069
1.28M
  new ((void *)(char *)&Data) LV();
1070
1.28M
  Kind = LValue;
1071
1.28M
}
1072
1073
19.1k
void APValue::MakeArray(unsigned InitElts, unsigned Size) {
1074
19.1k
  assert(isAbsent() && "Bad state change");
1075
0
  new ((void *)(char *)&Data) Arr(InitElts, Size);
1076
19.1k
  Kind = Array;
1077
19.1k
}
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.15k
                                unsigned Size) {
1086
2.15k
  assert(isAbsent() && "Bad state change");
1087
0
  MemberPointerData *MPD = new ((void *)(char *)&Data) MemberPointerData;
1088
2.15k
  Kind = MemberPointer;
1089
2.15k
  MPD->MemberAndIsDerivedMember.setPointer(
1090
2.15k
      Member ? 
cast<ValueDecl>(Member->getCanonicalDecl())1.85k
:
nullptr302
);
1091
2.15k
  MPD->MemberAndIsDerivedMember.setInt(IsDerivedMember);
1092
2.15k
  MPD->resizePath(Size);
1093
2.15k
  return {MPD->getPath(), MPD->PathLength};
1094
2.15k
}
1095
1096
void APValue::MakeMemberPointer(const ValueDecl *Member, bool IsDerivedMember,
1097
2.15k
                                ArrayRef<const CXXRecordDecl *> Path) {
1098
2.15k
  MutableArrayRef<const CXXRecordDecl *> InternalPath =
1099
2.15k
      setMemberPointerUninit(Member, IsDerivedMember, Path.size());
1100
3.65k
  for (unsigned I = 0; I != Path.size(); 
++I1.49k
)
1101
1.49k
    InternalPath[I] = Path[I]->getCanonicalDecl();
1102
2.15k
}
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
}