Coverage Report

Created: 2024-01-17 10:31

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