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 "clang/AST/ASTContext.h" 15 #include "clang/AST/CharUnits.h" 16 #include "clang/AST/DeclCXX.h" 17 #include "clang/AST/Expr.h" 18 #include "clang/AST/Type.h" 19 #include "llvm/Support/ErrorHandling.h" 20 #include "llvm/Support/raw_ostream.h" 21 using namespace clang; 22 23 /// The identity of a type_info object depends on the canonical unqualified 24 /// type only. 25 TypeInfoLValue::TypeInfoLValue(const Type *T) 26 : T(T->getCanonicalTypeUnqualified().getTypePtr()) {} 27 28 void TypeInfoLValue::print(llvm::raw_ostream &Out, 29 const PrintingPolicy &Policy) const { 30 Out << "typeid("; 31 QualType(getType(), 0).print(Out, Policy); 32 Out << ")"; 33 } 34 35 static_assert( 36 1 << llvm::PointerLikeTypeTraits<TypeInfoLValue>::NumLowBitsAvailable <= 37 alignof(Type), 38 "Type is insufficiently aligned"); 39 40 APValue::LValueBase::LValueBase(const ValueDecl *P, unsigned I, unsigned V) 41 : Ptr(P), Local{I, V} {} 42 APValue::LValueBase::LValueBase(const Expr *P, unsigned I, unsigned V) 43 : Ptr(P), Local{I, V} {} 44 45 APValue::LValueBase APValue::LValueBase::getDynamicAlloc(DynamicAllocLValue LV, 46 QualType Type) { 47 LValueBase Base; 48 Base.Ptr = LV; 49 Base.DynamicAllocType = Type.getAsOpaquePtr(); 50 return Base; 51 } 52 53 APValue::LValueBase APValue::LValueBase::getTypeInfo(TypeInfoLValue LV, 54 QualType TypeInfo) { 55 LValueBase Base; 56 Base.Ptr = LV; 57 Base.TypeInfoType = TypeInfo.getAsOpaquePtr(); 58 return Base; 59 } 60 61 unsigned APValue::LValueBase::getCallIndex() const { 62 return (is<TypeInfoLValue>() || is<DynamicAllocLValue>()) ? 0 63 : Local.CallIndex; 64 } 65 66 unsigned APValue::LValueBase::getVersion() const { 67 return (is<TypeInfoLValue>() || is<DynamicAllocLValue>()) ? 0 : Local.Version; 68 } 69 70 QualType APValue::LValueBase::getTypeInfoType() const { 71 assert(is<TypeInfoLValue>() && "not a type_info lvalue"); 72 return QualType::getFromOpaquePtr(TypeInfoType); 73 } 74 75 QualType APValue::LValueBase::getDynamicAllocType() const { 76 assert(is<DynamicAllocLValue>() && "not a dynamic allocation lvalue"); 77 return QualType::getFromOpaquePtr(DynamicAllocType); 78 } 79 80 namespace clang { 81 bool operator==(const APValue::LValueBase &LHS, 82 const APValue::LValueBase &RHS) { 83 if (LHS.Ptr != RHS.Ptr) 84 return false; 85 if (LHS.is<TypeInfoLValue>()) 86 return true; 87 return LHS.Local.CallIndex == RHS.Local.CallIndex && 88 LHS.Local.Version == RHS.Local.Version; 89 } 90 } 91 92 namespace { 93 struct LVBase { 94 APValue::LValueBase Base; 95 CharUnits Offset; 96 unsigned PathLength; 97 bool IsNullPtr : 1; 98 bool IsOnePastTheEnd : 1; 99 }; 100 } 101 102 void *APValue::LValueBase::getOpaqueValue() const { 103 return Ptr.getOpaqueValue(); 104 } 105 106 bool APValue::LValueBase::isNull() const { 107 return Ptr.isNull(); 108 } 109 110 APValue::LValueBase::operator bool () const { 111 return static_cast<bool>(Ptr); 112 } 113 114 clang::APValue::LValueBase 115 llvm::DenseMapInfo<clang::APValue::LValueBase>::getEmptyKey() { 116 return clang::APValue::LValueBase( 117 DenseMapInfo<const ValueDecl*>::getEmptyKey()); 118 } 119 120 clang::APValue::LValueBase 121 llvm::DenseMapInfo<clang::APValue::LValueBase>::getTombstoneKey() { 122 return clang::APValue::LValueBase( 123 DenseMapInfo<const ValueDecl*>::getTombstoneKey()); 124 } 125 126 namespace clang { 127 llvm::hash_code hash_value(const APValue::LValueBase &Base) { 128 if (Base.is<TypeInfoLValue>() || Base.is<DynamicAllocLValue>()) 129 return llvm::hash_value(Base.getOpaqueValue()); 130 return llvm::hash_combine(Base.getOpaqueValue(), Base.getCallIndex(), 131 Base.getVersion()); 132 } 133 } 134 135 unsigned llvm::DenseMapInfo<clang::APValue::LValueBase>::getHashValue( 136 const clang::APValue::LValueBase &Base) { 137 return hash_value(Base); 138 } 139 140 bool llvm::DenseMapInfo<clang::APValue::LValueBase>::isEqual( 141 const clang::APValue::LValueBase &LHS, 142 const clang::APValue::LValueBase &RHS) { 143 return LHS == RHS; 144 } 145 146 struct APValue::LV : LVBase { 147 static const unsigned InlinePathSpace = 148 (DataSize - sizeof(LVBase)) / sizeof(LValuePathEntry); 149 150 /// Path - The sequence of base classes, fields and array indices to follow to 151 /// walk from Base to the subobject. When performing GCC-style folding, there 152 /// may not be such a path. 153 union { 154 LValuePathEntry Path[InlinePathSpace]; 155 LValuePathEntry *PathPtr; 156 }; 157 158 LV() { PathLength = (unsigned)-1; } 159 ~LV() { resizePath(0); } 160 161 void resizePath(unsigned Length) { 162 if (Length == PathLength) 163 return; 164 if (hasPathPtr()) 165 delete [] PathPtr; 166 PathLength = Length; 167 if (hasPathPtr()) 168 PathPtr = new LValuePathEntry[Length]; 169 } 170 171 bool hasPath() const { return PathLength != (unsigned)-1; } 172 bool hasPathPtr() const { return hasPath() && PathLength > InlinePathSpace; } 173 174 LValuePathEntry *getPath() { return hasPathPtr() ? PathPtr : Path; } 175 const LValuePathEntry *getPath() const { 176 return hasPathPtr() ? PathPtr : Path; 177 } 178 }; 179 180 namespace { 181 struct MemberPointerBase { 182 llvm::PointerIntPair<const ValueDecl*, 1, bool> MemberAndIsDerivedMember; 183 unsigned PathLength; 184 }; 185 } 186 187 struct APValue::MemberPointerData : MemberPointerBase { 188 static const unsigned InlinePathSpace = 189 (DataSize - sizeof(MemberPointerBase)) / sizeof(const CXXRecordDecl*); 190 typedef const CXXRecordDecl *PathElem; 191 union { 192 PathElem Path[InlinePathSpace]; 193 PathElem *PathPtr; 194 }; 195 196 MemberPointerData() { PathLength = 0; } 197 ~MemberPointerData() { resizePath(0); } 198 199 void resizePath(unsigned Length) { 200 if (Length == PathLength) 201 return; 202 if (hasPathPtr()) 203 delete [] PathPtr; 204 PathLength = Length; 205 if (hasPathPtr()) 206 PathPtr = new PathElem[Length]; 207 } 208 209 bool hasPathPtr() const { return PathLength > InlinePathSpace; } 210 211 PathElem *getPath() { return hasPathPtr() ? PathPtr : Path; } 212 const PathElem *getPath() const { 213 return hasPathPtr() ? PathPtr : Path; 214 } 215 }; 216 217 // FIXME: Reduce the malloc traffic here. 218 219 APValue::Arr::Arr(unsigned NumElts, unsigned Size) : 220 Elts(new APValue[NumElts + (NumElts != Size ? 1 : 0)]), 221 NumElts(NumElts), ArrSize(Size) {} 222 APValue::Arr::~Arr() { delete [] Elts; } 223 224 APValue::StructData::StructData(unsigned NumBases, unsigned NumFields) : 225 Elts(new APValue[NumBases+NumFields]), 226 NumBases(NumBases), NumFields(NumFields) {} 227 APValue::StructData::~StructData() { 228 delete [] Elts; 229 } 230 231 APValue::UnionData::UnionData() : Field(nullptr), Value(new APValue) {} 232 APValue::UnionData::~UnionData () { 233 delete Value; 234 } 235 236 APValue::APValue(const APValue &RHS) : Kind(None) { 237 switch (RHS.getKind()) { 238 case None: 239 case Indeterminate: 240 Kind = RHS.getKind(); 241 break; 242 case Int: 243 MakeInt(); 244 setInt(RHS.getInt()); 245 break; 246 case Float: 247 MakeFloat(); 248 setFloat(RHS.getFloat()); 249 break; 250 case FixedPoint: { 251 APFixedPoint FXCopy = RHS.getFixedPoint(); 252 MakeFixedPoint(std::move(FXCopy)); 253 break; 254 } 255 case Vector: 256 MakeVector(); 257 setVector(((const Vec *)(const char *)RHS.Data.buffer)->Elts, 258 RHS.getVectorLength()); 259 break; 260 case ComplexInt: 261 MakeComplexInt(); 262 setComplexInt(RHS.getComplexIntReal(), RHS.getComplexIntImag()); 263 break; 264 case ComplexFloat: 265 MakeComplexFloat(); 266 setComplexFloat(RHS.getComplexFloatReal(), RHS.getComplexFloatImag()); 267 break; 268 case LValue: 269 MakeLValue(); 270 if (RHS.hasLValuePath()) 271 setLValue(RHS.getLValueBase(), RHS.getLValueOffset(), RHS.getLValuePath(), 272 RHS.isLValueOnePastTheEnd(), RHS.isNullPointer()); 273 else 274 setLValue(RHS.getLValueBase(), RHS.getLValueOffset(), NoLValuePath(), 275 RHS.isNullPointer()); 276 break; 277 case Array: 278 MakeArray(RHS.getArrayInitializedElts(), RHS.getArraySize()); 279 for (unsigned I = 0, N = RHS.getArrayInitializedElts(); I != N; ++I) 280 getArrayInitializedElt(I) = RHS.getArrayInitializedElt(I); 281 if (RHS.hasArrayFiller()) 282 getArrayFiller() = RHS.getArrayFiller(); 283 break; 284 case Struct: 285 MakeStruct(RHS.getStructNumBases(), RHS.getStructNumFields()); 286 for (unsigned I = 0, N = RHS.getStructNumBases(); I != N; ++I) 287 getStructBase(I) = RHS.getStructBase(I); 288 for (unsigned I = 0, N = RHS.getStructNumFields(); I != N; ++I) 289 getStructField(I) = RHS.getStructField(I); 290 break; 291 case Union: 292 MakeUnion(); 293 setUnion(RHS.getUnionField(), RHS.getUnionValue()); 294 break; 295 case MemberPointer: 296 MakeMemberPointer(RHS.getMemberPointerDecl(), 297 RHS.isMemberPointerToDerivedMember(), 298 RHS.getMemberPointerPath()); 299 break; 300 case AddrLabelDiff: 301 MakeAddrLabelDiff(); 302 setAddrLabelDiff(RHS.getAddrLabelDiffLHS(), RHS.getAddrLabelDiffRHS()); 303 break; 304 } 305 } 306 307 void APValue::DestroyDataAndMakeUninit() { 308 if (Kind == Int) 309 ((APSInt*)(char*)Data.buffer)->~APSInt(); 310 else if (Kind == Float) 311 ((APFloat*)(char*)Data.buffer)->~APFloat(); 312 else if (Kind == FixedPoint) 313 ((APFixedPoint *)(char *)Data.buffer)->~APFixedPoint(); 314 else if (Kind == Vector) 315 ((Vec*)(char*)Data.buffer)->~Vec(); 316 else if (Kind == ComplexInt) 317 ((ComplexAPSInt*)(char*)Data.buffer)->~ComplexAPSInt(); 318 else if (Kind == ComplexFloat) 319 ((ComplexAPFloat*)(char*)Data.buffer)->~ComplexAPFloat(); 320 else if (Kind == LValue) 321 ((LV*)(char*)Data.buffer)->~LV(); 322 else if (Kind == Array) 323 ((Arr*)(char*)Data.buffer)->~Arr(); 324 else if (Kind == Struct) 325 ((StructData*)(char*)Data.buffer)->~StructData(); 326 else if (Kind == Union) 327 ((UnionData*)(char*)Data.buffer)->~UnionData(); 328 else if (Kind == MemberPointer) 329 ((MemberPointerData*)(char*)Data.buffer)->~MemberPointerData(); 330 else if (Kind == AddrLabelDiff) 331 ((AddrLabelDiffData*)(char*)Data.buffer)->~AddrLabelDiffData(); 332 Kind = None; 333 } 334 335 bool APValue::needsCleanup() const { 336 switch (getKind()) { 337 case None: 338 case Indeterminate: 339 case AddrLabelDiff: 340 return false; 341 case Struct: 342 case Union: 343 case Array: 344 case Vector: 345 return true; 346 case Int: 347 return getInt().needsCleanup(); 348 case Float: 349 return getFloat().needsCleanup(); 350 case FixedPoint: 351 return getFixedPoint().getValue().needsCleanup(); 352 case ComplexFloat: 353 assert(getComplexFloatImag().needsCleanup() == 354 getComplexFloatReal().needsCleanup() && 355 "In _Complex float types, real and imaginary values always have the " 356 "same size."); 357 return getComplexFloatReal().needsCleanup(); 358 case ComplexInt: 359 assert(getComplexIntImag().needsCleanup() == 360 getComplexIntReal().needsCleanup() && 361 "In _Complex int types, real and imaginary values must have the " 362 "same size."); 363 return getComplexIntReal().needsCleanup(); 364 case LValue: 365 return reinterpret_cast<const LV *>(Data.buffer)->hasPathPtr(); 366 case MemberPointer: 367 return reinterpret_cast<const MemberPointerData *>(Data.buffer) 368 ->hasPathPtr(); 369 } 370 llvm_unreachable("Unknown APValue kind!"); 371 } 372 373 void APValue::swap(APValue &RHS) { 374 std::swap(Kind, RHS.Kind); 375 char TmpData[DataSize]; 376 memcpy(TmpData, Data.buffer, DataSize); 377 memcpy(Data.buffer, RHS.Data.buffer, DataSize); 378 memcpy(RHS.Data.buffer, TmpData, DataSize); 379 } 380 381 static double GetApproxValue(const llvm::APFloat &F) { 382 llvm::APFloat V = F; 383 bool ignored; 384 V.convert(llvm::APFloat::IEEEdouble(), llvm::APFloat::rmNearestTiesToEven, 385 &ignored); 386 return V.convertToDouble(); 387 } 388 389 LLVM_DUMP_METHOD void APValue::dump() const { 390 dump(llvm::errs(), /*Context=*/nullptr); 391 llvm::errs() << '\n'; 392 } 393 394 LLVM_DUMP_METHOD void APValue::dump(raw_ostream &OS, 395 const ASTContext *Context) const { 396 switch (getKind()) { 397 case None: 398 OS << "None"; 399 return; 400 case Indeterminate: 401 OS << "Indeterminate"; 402 return; 403 case Int: 404 OS << "Int: " << getInt(); 405 return; 406 case Float: 407 OS << "Float: " << GetApproxValue(getFloat()); 408 return; 409 case FixedPoint: 410 OS << "FixedPoint : " << getFixedPoint(); 411 return; 412 case Vector: 413 OS << "Vector: "; 414 getVectorElt(0).dump(OS, Context); 415 for (unsigned i = 1; i != getVectorLength(); ++i) { 416 OS << ", "; 417 getVectorElt(i).dump(OS, Context); 418 } 419 return; 420 case ComplexInt: 421 OS << "ComplexInt: " << getComplexIntReal() << ", " << getComplexIntImag(); 422 return; 423 case ComplexFloat: 424 OS << "ComplexFloat: " << GetApproxValue(getComplexFloatReal()) 425 << ", " << GetApproxValue(getComplexFloatImag()); 426 return; 427 case LValue: 428 OS << "LValue: <todo>"; 429 return; 430 case Array: 431 OS << "Array: "; 432 for (unsigned I = 0, N = getArrayInitializedElts(); I != N; ++I) { 433 getArrayInitializedElt(I).dump(OS, Context); 434 if (I != getArraySize() - 1) 435 OS << ", "; 436 } 437 if (hasArrayFiller()) { 438 OS << getArraySize() - getArrayInitializedElts() << " x "; 439 getArrayFiller().dump(OS, Context); 440 } 441 return; 442 case Struct: 443 OS << "Struct "; 444 if (unsigned N = getStructNumBases()) { 445 OS << " bases: "; 446 getStructBase(0).dump(OS, Context); 447 for (unsigned I = 1; I != N; ++I) { 448 OS << ", "; 449 getStructBase(I).dump(OS, Context); 450 } 451 } 452 if (unsigned N = getStructNumFields()) { 453 OS << " fields: "; 454 getStructField(0).dump(OS, Context); 455 for (unsigned I = 1; I != N; ++I) { 456 OS << ", "; 457 getStructField(I).dump(OS, Context); 458 } 459 } 460 return; 461 case Union: 462 OS << "Union: "; 463 getUnionValue().dump(OS, Context); 464 return; 465 case MemberPointer: 466 OS << "MemberPointer: <todo>"; 467 return; 468 case AddrLabelDiff: 469 OS << "AddrLabelDiff: <todo>"; 470 return; 471 } 472 llvm_unreachable("Unknown APValue kind!"); 473 } 474 475 void APValue::printPretty(raw_ostream &Out, const ASTContext &Ctx, 476 QualType Ty) const { 477 switch (getKind()) { 478 case APValue::None: 479 Out << "<out of lifetime>"; 480 return; 481 case APValue::Indeterminate: 482 Out << "<uninitialized>"; 483 return; 484 case APValue::Int: 485 if (Ty->isBooleanType()) 486 Out << (getInt().getBoolValue() ? "true" : "false"); 487 else 488 Out << getInt(); 489 return; 490 case APValue::Float: 491 Out << GetApproxValue(getFloat()); 492 return; 493 case APValue::FixedPoint: 494 Out << getFixedPoint(); 495 return; 496 case APValue::Vector: { 497 Out << '{'; 498 QualType ElemTy = Ty->castAs<VectorType>()->getElementType(); 499 getVectorElt(0).printPretty(Out, Ctx, ElemTy); 500 for (unsigned i = 1; i != getVectorLength(); ++i) { 501 Out << ", "; 502 getVectorElt(i).printPretty(Out, Ctx, ElemTy); 503 } 504 Out << '}'; 505 return; 506 } 507 case APValue::ComplexInt: 508 Out << getComplexIntReal() << "+" << getComplexIntImag() << "i"; 509 return; 510 case APValue::ComplexFloat: 511 Out << GetApproxValue(getComplexFloatReal()) << "+" 512 << GetApproxValue(getComplexFloatImag()) << "i"; 513 return; 514 case APValue::LValue: { 515 bool IsReference = Ty->isReferenceType(); 516 QualType InnerTy 517 = IsReference ? Ty.getNonReferenceType() : Ty->getPointeeType(); 518 if (InnerTy.isNull()) 519 InnerTy = Ty; 520 521 LValueBase Base = getLValueBase(); 522 if (!Base) { 523 if (isNullPointer()) { 524 Out << (Ctx.getLangOpts().CPlusPlus11 ? "nullptr" : "0"); 525 } else if (IsReference) { 526 Out << "*(" << InnerTy.stream(Ctx.getPrintingPolicy()) << "*)" 527 << getLValueOffset().getQuantity(); 528 } else { 529 Out << "(" << Ty.stream(Ctx.getPrintingPolicy()) << ")" 530 << getLValueOffset().getQuantity(); 531 } 532 return; 533 } 534 535 if (!hasLValuePath()) { 536 // No lvalue path: just print the offset. 537 CharUnits O = getLValueOffset(); 538 CharUnits S = Ctx.getTypeSizeInChars(InnerTy); 539 if (!O.isZero()) { 540 if (IsReference) 541 Out << "*("; 542 if (O % S) { 543 Out << "(char*)"; 544 S = CharUnits::One(); 545 } 546 Out << '&'; 547 } else if (!IsReference) { 548 Out << '&'; 549 } 550 551 if (const ValueDecl *VD = Base.dyn_cast<const ValueDecl*>()) 552 Out << *VD; 553 else if (TypeInfoLValue TI = Base.dyn_cast<TypeInfoLValue>()) { 554 TI.print(Out, Ctx.getPrintingPolicy()); 555 } else if (DynamicAllocLValue DA = Base.dyn_cast<DynamicAllocLValue>()) { 556 Out << "{*new " 557 << Base.getDynamicAllocType().stream(Ctx.getPrintingPolicy()) << "#" 558 << DA.getIndex() << "}"; 559 } else { 560 assert(Base.get<const Expr *>() != nullptr && 561 "Expecting non-null Expr"); 562 Base.get<const Expr*>()->printPretty(Out, nullptr, 563 Ctx.getPrintingPolicy()); 564 } 565 566 if (!O.isZero()) { 567 Out << " + " << (O / S); 568 if (IsReference) 569 Out << ')'; 570 } 571 return; 572 } 573 574 // We have an lvalue path. Print it out nicely. 575 if (!IsReference) 576 Out << '&'; 577 else if (isLValueOnePastTheEnd()) 578 Out << "*(&"; 579 580 QualType ElemTy; 581 if (const ValueDecl *VD = Base.dyn_cast<const ValueDecl*>()) { 582 Out << *VD; 583 ElemTy = VD->getType(); 584 } else if (TypeInfoLValue TI = Base.dyn_cast<TypeInfoLValue>()) { 585 TI.print(Out, Ctx.getPrintingPolicy()); 586 ElemTy = Base.getTypeInfoType(); 587 } else if (DynamicAllocLValue DA = Base.dyn_cast<DynamicAllocLValue>()) { 588 Out << "{*new " 589 << Base.getDynamicAllocType().stream(Ctx.getPrintingPolicy()) << "#" 590 << DA.getIndex() << "}"; 591 ElemTy = Base.getDynamicAllocType(); 592 } else { 593 const Expr *E = Base.get<const Expr*>(); 594 assert(E != nullptr && "Expecting non-null Expr"); 595 E->printPretty(Out, nullptr, Ctx.getPrintingPolicy()); 596 // FIXME: This is wrong if E is a MaterializeTemporaryExpr with an lvalue 597 // adjustment. 598 ElemTy = E->getType(); 599 } 600 601 ArrayRef<LValuePathEntry> Path = getLValuePath(); 602 const CXXRecordDecl *CastToBase = nullptr; 603 for (unsigned I = 0, N = Path.size(); I != N; ++I) { 604 if (ElemTy->getAs<RecordType>()) { 605 // The lvalue refers to a class type, so the next path entry is a base 606 // or member. 607 const Decl *BaseOrMember = Path[I].getAsBaseOrMember().getPointer(); 608 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(BaseOrMember)) { 609 CastToBase = RD; 610 ElemTy = Ctx.getRecordType(RD); 611 } else { 612 const ValueDecl *VD = cast<ValueDecl>(BaseOrMember); 613 Out << "."; 614 if (CastToBase) 615 Out << *CastToBase << "::"; 616 Out << *VD; 617 ElemTy = VD->getType(); 618 } 619 } else { 620 // The lvalue must refer to an array. 621 Out << '[' << Path[I].getAsArrayIndex() << ']'; 622 ElemTy = Ctx.getAsArrayType(ElemTy)->getElementType(); 623 } 624 } 625 626 // Handle formatting of one-past-the-end lvalues. 627 if (isLValueOnePastTheEnd()) { 628 // FIXME: If CastToBase is non-0, we should prefix the output with 629 // "(CastToBase*)". 630 Out << " + 1"; 631 if (IsReference) 632 Out << ')'; 633 } 634 return; 635 } 636 case APValue::Array: { 637 const ArrayType *AT = Ctx.getAsArrayType(Ty); 638 QualType ElemTy = AT->getElementType(); 639 Out << '{'; 640 if (unsigned N = getArrayInitializedElts()) { 641 getArrayInitializedElt(0).printPretty(Out, Ctx, ElemTy); 642 for (unsigned I = 1; I != N; ++I) { 643 Out << ", "; 644 if (I == 10) { 645 // Avoid printing out the entire contents of large arrays. 646 Out << "..."; 647 break; 648 } 649 getArrayInitializedElt(I).printPretty(Out, Ctx, ElemTy); 650 } 651 } 652 Out << '}'; 653 return; 654 } 655 case APValue::Struct: { 656 Out << '{'; 657 const RecordDecl *RD = Ty->castAs<RecordType>()->getDecl(); 658 bool First = true; 659 if (unsigned N = getStructNumBases()) { 660 const CXXRecordDecl *CD = cast<CXXRecordDecl>(RD); 661 CXXRecordDecl::base_class_const_iterator BI = CD->bases_begin(); 662 for (unsigned I = 0; I != N; ++I, ++BI) { 663 assert(BI != CD->bases_end()); 664 if (!First) 665 Out << ", "; 666 getStructBase(I).printPretty(Out, Ctx, BI->getType()); 667 First = false; 668 } 669 } 670 for (const auto *FI : RD->fields()) { 671 if (!First) 672 Out << ", "; 673 if (FI->isUnnamedBitfield()) continue; 674 getStructField(FI->getFieldIndex()). 675 printPretty(Out, Ctx, FI->getType()); 676 First = false; 677 } 678 Out << '}'; 679 return; 680 } 681 case APValue::Union: 682 Out << '{'; 683 if (const FieldDecl *FD = getUnionField()) { 684 Out << "." << *FD << " = "; 685 getUnionValue().printPretty(Out, Ctx, FD->getType()); 686 } 687 Out << '}'; 688 return; 689 case APValue::MemberPointer: 690 // FIXME: This is not enough to unambiguously identify the member in a 691 // multiple-inheritance scenario. 692 if (const ValueDecl *VD = getMemberPointerDecl()) { 693 Out << '&' << *cast<CXXRecordDecl>(VD->getDeclContext()) << "::" << *VD; 694 return; 695 } 696 Out << "0"; 697 return; 698 case APValue::AddrLabelDiff: 699 Out << "&&" << getAddrLabelDiffLHS()->getLabel()->getName(); 700 Out << " - "; 701 Out << "&&" << getAddrLabelDiffRHS()->getLabel()->getName(); 702 return; 703 } 704 llvm_unreachable("Unknown APValue kind!"); 705 } 706 707 std::string APValue::getAsString(const ASTContext &Ctx, QualType Ty) const { 708 std::string Result; 709 llvm::raw_string_ostream Out(Result); 710 printPretty(Out, Ctx, Ty); 711 Out.flush(); 712 return Result; 713 } 714 715 bool APValue::toIntegralConstant(APSInt &Result, QualType SrcTy, 716 const ASTContext &Ctx) const { 717 if (isInt()) { 718 Result = getInt(); 719 return true; 720 } 721 722 if (isLValue() && isNullPointer()) { 723 Result = Ctx.MakeIntValue(Ctx.getTargetNullPointerValue(SrcTy), SrcTy); 724 return true; 725 } 726 727 if (isLValue() && !getLValueBase()) { 728 Result = Ctx.MakeIntValue(getLValueOffset().getQuantity(), SrcTy); 729 return true; 730 } 731 732 return false; 733 } 734 735 const APValue::LValueBase APValue::getLValueBase() const { 736 assert(isLValue() && "Invalid accessor"); 737 return ((const LV*)(const void*)Data.buffer)->Base; 738 } 739 740 bool APValue::isLValueOnePastTheEnd() const { 741 assert(isLValue() && "Invalid accessor"); 742 return ((const LV*)(const void*)Data.buffer)->IsOnePastTheEnd; 743 } 744 745 CharUnits &APValue::getLValueOffset() { 746 assert(isLValue() && "Invalid accessor"); 747 return ((LV*)(void*)Data.buffer)->Offset; 748 } 749 750 bool APValue::hasLValuePath() const { 751 assert(isLValue() && "Invalid accessor"); 752 return ((const LV*)(const char*)Data.buffer)->hasPath(); 753 } 754 755 ArrayRef<APValue::LValuePathEntry> APValue::getLValuePath() const { 756 assert(isLValue() && hasLValuePath() && "Invalid accessor"); 757 const LV &LVal = *((const LV*)(const char*)Data.buffer); 758 return llvm::makeArrayRef(LVal.getPath(), LVal.PathLength); 759 } 760 761 unsigned APValue::getLValueCallIndex() const { 762 assert(isLValue() && "Invalid accessor"); 763 return ((const LV*)(const char*)Data.buffer)->Base.getCallIndex(); 764 } 765 766 unsigned APValue::getLValueVersion() const { 767 assert(isLValue() && "Invalid accessor"); 768 return ((const LV*)(const char*)Data.buffer)->Base.getVersion(); 769 } 770 771 bool APValue::isNullPointer() const { 772 assert(isLValue() && "Invalid usage"); 773 return ((const LV*)(const char*)Data.buffer)->IsNullPtr; 774 } 775 776 void APValue::setLValue(LValueBase B, const CharUnits &O, NoLValuePath, 777 bool IsNullPtr) { 778 assert(isLValue() && "Invalid accessor"); 779 LV &LVal = *((LV*)(char*)Data.buffer); 780 LVal.Base = B; 781 LVal.IsOnePastTheEnd = false; 782 LVal.Offset = O; 783 LVal.resizePath((unsigned)-1); 784 LVal.IsNullPtr = IsNullPtr; 785 } 786 787 void APValue::setLValue(LValueBase B, const CharUnits &O, 788 ArrayRef<LValuePathEntry> Path, bool IsOnePastTheEnd, 789 bool IsNullPtr) { 790 assert(isLValue() && "Invalid accessor"); 791 LV &LVal = *((LV*)(char*)Data.buffer); 792 LVal.Base = B; 793 LVal.IsOnePastTheEnd = IsOnePastTheEnd; 794 LVal.Offset = O; 795 LVal.resizePath(Path.size()); 796 memcpy(LVal.getPath(), Path.data(), Path.size() * sizeof(LValuePathEntry)); 797 LVal.IsNullPtr = IsNullPtr; 798 } 799 800 const ValueDecl *APValue::getMemberPointerDecl() const { 801 assert(isMemberPointer() && "Invalid accessor"); 802 const MemberPointerData &MPD = 803 *((const MemberPointerData *)(const char *)Data.buffer); 804 return MPD.MemberAndIsDerivedMember.getPointer(); 805 } 806 807 bool APValue::isMemberPointerToDerivedMember() const { 808 assert(isMemberPointer() && "Invalid accessor"); 809 const MemberPointerData &MPD = 810 *((const MemberPointerData *)(const char *)Data.buffer); 811 return MPD.MemberAndIsDerivedMember.getInt(); 812 } 813 814 ArrayRef<const CXXRecordDecl*> APValue::getMemberPointerPath() const { 815 assert(isMemberPointer() && "Invalid accessor"); 816 const MemberPointerData &MPD = 817 *((const MemberPointerData *)(const char *)Data.buffer); 818 return llvm::makeArrayRef(MPD.getPath(), MPD.PathLength); 819 } 820 821 void APValue::MakeLValue() { 822 assert(isAbsent() && "Bad state change"); 823 static_assert(sizeof(LV) <= DataSize, "LV too big"); 824 new ((void*)(char*)Data.buffer) LV(); 825 Kind = LValue; 826 } 827 828 void APValue::MakeArray(unsigned InitElts, unsigned Size) { 829 assert(isAbsent() && "Bad state change"); 830 new ((void*)(char*)Data.buffer) Arr(InitElts, Size); 831 Kind = Array; 832 } 833 834 void APValue::MakeMemberPointer(const ValueDecl *Member, bool IsDerivedMember, 835 ArrayRef<const CXXRecordDecl*> Path) { 836 assert(isAbsent() && "Bad state change"); 837 MemberPointerData *MPD = new ((void*)(char*)Data.buffer) MemberPointerData; 838 Kind = MemberPointer; 839 MPD->MemberAndIsDerivedMember.setPointer(Member); 840 MPD->MemberAndIsDerivedMember.setInt(IsDerivedMember); 841 MPD->resizePath(Path.size()); 842 memcpy(MPD->getPath(), Path.data(), Path.size()*sizeof(const CXXRecordDecl*)); 843 } 844