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 APValue::APValue(APValue &&RHS) : Kind(RHS.Kind), Data(RHS.Data) { 308 RHS.Kind = None; 309 } 310 311 APValue &APValue::operator=(const APValue &RHS) { 312 if (this != &RHS) 313 *this = APValue(RHS); 314 return *this; 315 } 316 317 APValue &APValue::operator=(APValue &&RHS) { 318 if (Kind != None && Kind != Indeterminate) 319 DestroyDataAndMakeUninit(); 320 Kind = RHS.Kind; 321 Data = RHS.Data; 322 RHS.Kind = None; 323 return *this; 324 } 325 326 void APValue::DestroyDataAndMakeUninit() { 327 if (Kind == Int) 328 ((APSInt*)(char*)Data.buffer)->~APSInt(); 329 else if (Kind == Float) 330 ((APFloat*)(char*)Data.buffer)->~APFloat(); 331 else if (Kind == FixedPoint) 332 ((APFixedPoint *)(char *)Data.buffer)->~APFixedPoint(); 333 else if (Kind == Vector) 334 ((Vec*)(char*)Data.buffer)->~Vec(); 335 else if (Kind == ComplexInt) 336 ((ComplexAPSInt*)(char*)Data.buffer)->~ComplexAPSInt(); 337 else if (Kind == ComplexFloat) 338 ((ComplexAPFloat*)(char*)Data.buffer)->~ComplexAPFloat(); 339 else if (Kind == LValue) 340 ((LV*)(char*)Data.buffer)->~LV(); 341 else if (Kind == Array) 342 ((Arr*)(char*)Data.buffer)->~Arr(); 343 else if (Kind == Struct) 344 ((StructData*)(char*)Data.buffer)->~StructData(); 345 else if (Kind == Union) 346 ((UnionData*)(char*)Data.buffer)->~UnionData(); 347 else if (Kind == MemberPointer) 348 ((MemberPointerData*)(char*)Data.buffer)->~MemberPointerData(); 349 else if (Kind == AddrLabelDiff) 350 ((AddrLabelDiffData*)(char*)Data.buffer)->~AddrLabelDiffData(); 351 Kind = None; 352 } 353 354 bool APValue::needsCleanup() const { 355 switch (getKind()) { 356 case None: 357 case Indeterminate: 358 case AddrLabelDiff: 359 return false; 360 case Struct: 361 case Union: 362 case Array: 363 case Vector: 364 return true; 365 case Int: 366 return getInt().needsCleanup(); 367 case Float: 368 return getFloat().needsCleanup(); 369 case FixedPoint: 370 return getFixedPoint().getValue().needsCleanup(); 371 case ComplexFloat: 372 assert(getComplexFloatImag().needsCleanup() == 373 getComplexFloatReal().needsCleanup() && 374 "In _Complex float types, real and imaginary values always have the " 375 "same size."); 376 return getComplexFloatReal().needsCleanup(); 377 case ComplexInt: 378 assert(getComplexIntImag().needsCleanup() == 379 getComplexIntReal().needsCleanup() && 380 "In _Complex int types, real and imaginary values must have the " 381 "same size."); 382 return getComplexIntReal().needsCleanup(); 383 case LValue: 384 return reinterpret_cast<const LV *>(Data.buffer)->hasPathPtr(); 385 case MemberPointer: 386 return reinterpret_cast<const MemberPointerData *>(Data.buffer) 387 ->hasPathPtr(); 388 } 389 llvm_unreachable("Unknown APValue kind!"); 390 } 391 392 void APValue::swap(APValue &RHS) { 393 std::swap(Kind, RHS.Kind); 394 std::swap(Data, RHS.Data); 395 } 396 397 static double GetApproxValue(const llvm::APFloat &F) { 398 llvm::APFloat V = F; 399 bool ignored; 400 V.convert(llvm::APFloat::IEEEdouble(), llvm::APFloat::rmNearestTiesToEven, 401 &ignored); 402 return V.convertToDouble(); 403 } 404 405 void APValue::printPretty(raw_ostream &Out, const ASTContext &Ctx, 406 QualType Ty) const { 407 // There are no objects of type 'void', but values of this type can be 408 // returned from functions. 409 if (Ty->isVoidType()) { 410 Out << "void()"; 411 return; 412 } 413 414 switch (getKind()) { 415 case APValue::None: 416 Out << "<out of lifetime>"; 417 return; 418 case APValue::Indeterminate: 419 Out << "<uninitialized>"; 420 return; 421 case APValue::Int: 422 if (Ty->isBooleanType()) 423 Out << (getInt().getBoolValue() ? "true" : "false"); 424 else 425 Out << getInt(); 426 return; 427 case APValue::Float: 428 Out << GetApproxValue(getFloat()); 429 return; 430 case APValue::FixedPoint: 431 Out << getFixedPoint(); 432 return; 433 case APValue::Vector: { 434 Out << '{'; 435 QualType ElemTy = Ty->castAs<VectorType>()->getElementType(); 436 getVectorElt(0).printPretty(Out, Ctx, ElemTy); 437 for (unsigned i = 1; i != getVectorLength(); ++i) { 438 Out << ", "; 439 getVectorElt(i).printPretty(Out, Ctx, ElemTy); 440 } 441 Out << '}'; 442 return; 443 } 444 case APValue::ComplexInt: 445 Out << getComplexIntReal() << "+" << getComplexIntImag() << "i"; 446 return; 447 case APValue::ComplexFloat: 448 Out << GetApproxValue(getComplexFloatReal()) << "+" 449 << GetApproxValue(getComplexFloatImag()) << "i"; 450 return; 451 case APValue::LValue: { 452 bool IsReference = Ty->isReferenceType(); 453 QualType InnerTy 454 = IsReference ? Ty.getNonReferenceType() : Ty->getPointeeType(); 455 if (InnerTy.isNull()) 456 InnerTy = Ty; 457 458 LValueBase Base = getLValueBase(); 459 if (!Base) { 460 if (isNullPointer()) { 461 Out << (Ctx.getLangOpts().CPlusPlus11 ? "nullptr" : "0"); 462 } else if (IsReference) { 463 Out << "*(" << InnerTy.stream(Ctx.getPrintingPolicy()) << "*)" 464 << getLValueOffset().getQuantity(); 465 } else { 466 Out << "(" << Ty.stream(Ctx.getPrintingPolicy()) << ")" 467 << getLValueOffset().getQuantity(); 468 } 469 return; 470 } 471 472 if (!hasLValuePath()) { 473 // No lvalue path: just print the offset. 474 CharUnits O = getLValueOffset(); 475 CharUnits S = Ctx.getTypeSizeInChars(InnerTy); 476 if (!O.isZero()) { 477 if (IsReference) 478 Out << "*("; 479 if (O % S) { 480 Out << "(char*)"; 481 S = CharUnits::One(); 482 } 483 Out << '&'; 484 } else if (!IsReference) { 485 Out << '&'; 486 } 487 488 if (const ValueDecl *VD = Base.dyn_cast<const ValueDecl*>()) 489 Out << *VD; 490 else if (TypeInfoLValue TI = Base.dyn_cast<TypeInfoLValue>()) { 491 TI.print(Out, Ctx.getPrintingPolicy()); 492 } else if (DynamicAllocLValue DA = Base.dyn_cast<DynamicAllocLValue>()) { 493 Out << "{*new " 494 << Base.getDynamicAllocType().stream(Ctx.getPrintingPolicy()) << "#" 495 << DA.getIndex() << "}"; 496 } else { 497 assert(Base.get<const Expr *>() != nullptr && 498 "Expecting non-null Expr"); 499 Base.get<const Expr*>()->printPretty(Out, nullptr, 500 Ctx.getPrintingPolicy()); 501 } 502 503 if (!O.isZero()) { 504 Out << " + " << (O / S); 505 if (IsReference) 506 Out << ')'; 507 } 508 return; 509 } 510 511 // We have an lvalue path. Print it out nicely. 512 if (!IsReference) 513 Out << '&'; 514 else if (isLValueOnePastTheEnd()) 515 Out << "*(&"; 516 517 QualType ElemTy; 518 if (const ValueDecl *VD = Base.dyn_cast<const ValueDecl*>()) { 519 Out << *VD; 520 ElemTy = VD->getType(); 521 } else if (TypeInfoLValue TI = Base.dyn_cast<TypeInfoLValue>()) { 522 TI.print(Out, Ctx.getPrintingPolicy()); 523 ElemTy = Base.getTypeInfoType(); 524 } else if (DynamicAllocLValue DA = Base.dyn_cast<DynamicAllocLValue>()) { 525 Out << "{*new " 526 << Base.getDynamicAllocType().stream(Ctx.getPrintingPolicy()) << "#" 527 << DA.getIndex() << "}"; 528 ElemTy = Base.getDynamicAllocType(); 529 } else { 530 const Expr *E = Base.get<const Expr*>(); 531 assert(E != nullptr && "Expecting non-null Expr"); 532 E->printPretty(Out, nullptr, Ctx.getPrintingPolicy()); 533 // FIXME: This is wrong if E is a MaterializeTemporaryExpr with an lvalue 534 // adjustment. 535 ElemTy = E->getType(); 536 } 537 538 ArrayRef<LValuePathEntry> Path = getLValuePath(); 539 const CXXRecordDecl *CastToBase = nullptr; 540 for (unsigned I = 0, N = Path.size(); I != N; ++I) { 541 if (ElemTy->getAs<RecordType>()) { 542 // The lvalue refers to a class type, so the next path entry is a base 543 // or member. 544 const Decl *BaseOrMember = Path[I].getAsBaseOrMember().getPointer(); 545 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(BaseOrMember)) { 546 CastToBase = RD; 547 ElemTy = Ctx.getRecordType(RD); 548 } else { 549 const ValueDecl *VD = cast<ValueDecl>(BaseOrMember); 550 Out << "."; 551 if (CastToBase) 552 Out << *CastToBase << "::"; 553 Out << *VD; 554 ElemTy = VD->getType(); 555 } 556 } else { 557 // The lvalue must refer to an array. 558 Out << '[' << Path[I].getAsArrayIndex() << ']'; 559 ElemTy = Ctx.getAsArrayType(ElemTy)->getElementType(); 560 } 561 } 562 563 // Handle formatting of one-past-the-end lvalues. 564 if (isLValueOnePastTheEnd()) { 565 // FIXME: If CastToBase is non-0, we should prefix the output with 566 // "(CastToBase*)". 567 Out << " + 1"; 568 if (IsReference) 569 Out << ')'; 570 } 571 return; 572 } 573 case APValue::Array: { 574 const ArrayType *AT = Ctx.getAsArrayType(Ty); 575 QualType ElemTy = AT->getElementType(); 576 Out << '{'; 577 if (unsigned N = getArrayInitializedElts()) { 578 getArrayInitializedElt(0).printPretty(Out, Ctx, ElemTy); 579 for (unsigned I = 1; I != N; ++I) { 580 Out << ", "; 581 if (I == 10) { 582 // Avoid printing out the entire contents of large arrays. 583 Out << "..."; 584 break; 585 } 586 getArrayInitializedElt(I).printPretty(Out, Ctx, ElemTy); 587 } 588 } 589 Out << '}'; 590 return; 591 } 592 case APValue::Struct: { 593 Out << '{'; 594 const RecordDecl *RD = Ty->castAs<RecordType>()->getDecl(); 595 bool First = true; 596 if (unsigned N = getStructNumBases()) { 597 const CXXRecordDecl *CD = cast<CXXRecordDecl>(RD); 598 CXXRecordDecl::base_class_const_iterator BI = CD->bases_begin(); 599 for (unsigned I = 0; I != N; ++I, ++BI) { 600 assert(BI != CD->bases_end()); 601 if (!First) 602 Out << ", "; 603 getStructBase(I).printPretty(Out, Ctx, BI->getType()); 604 First = false; 605 } 606 } 607 for (const auto *FI : RD->fields()) { 608 if (!First) 609 Out << ", "; 610 if (FI->isUnnamedBitfield()) continue; 611 getStructField(FI->getFieldIndex()). 612 printPretty(Out, Ctx, FI->getType()); 613 First = false; 614 } 615 Out << '}'; 616 return; 617 } 618 case APValue::Union: 619 Out << '{'; 620 if (const FieldDecl *FD = getUnionField()) { 621 Out << "." << *FD << " = "; 622 getUnionValue().printPretty(Out, Ctx, FD->getType()); 623 } 624 Out << '}'; 625 return; 626 case APValue::MemberPointer: 627 // FIXME: This is not enough to unambiguously identify the member in a 628 // multiple-inheritance scenario. 629 if (const ValueDecl *VD = getMemberPointerDecl()) { 630 Out << '&' << *cast<CXXRecordDecl>(VD->getDeclContext()) << "::" << *VD; 631 return; 632 } 633 Out << "0"; 634 return; 635 case APValue::AddrLabelDiff: 636 Out << "&&" << getAddrLabelDiffLHS()->getLabel()->getName(); 637 Out << " - "; 638 Out << "&&" << getAddrLabelDiffRHS()->getLabel()->getName(); 639 return; 640 } 641 llvm_unreachable("Unknown APValue kind!"); 642 } 643 644 std::string APValue::getAsString(const ASTContext &Ctx, QualType Ty) const { 645 std::string Result; 646 llvm::raw_string_ostream Out(Result); 647 printPretty(Out, Ctx, Ty); 648 Out.flush(); 649 return Result; 650 } 651 652 bool APValue::toIntegralConstant(APSInt &Result, QualType SrcTy, 653 const ASTContext &Ctx) const { 654 if (isInt()) { 655 Result = getInt(); 656 return true; 657 } 658 659 if (isLValue() && isNullPointer()) { 660 Result = Ctx.MakeIntValue(Ctx.getTargetNullPointerValue(SrcTy), SrcTy); 661 return true; 662 } 663 664 if (isLValue() && !getLValueBase()) { 665 Result = Ctx.MakeIntValue(getLValueOffset().getQuantity(), SrcTy); 666 return true; 667 } 668 669 return false; 670 } 671 672 const APValue::LValueBase APValue::getLValueBase() const { 673 assert(isLValue() && "Invalid accessor"); 674 return ((const LV*)(const void*)Data.buffer)->Base; 675 } 676 677 bool APValue::isLValueOnePastTheEnd() const { 678 assert(isLValue() && "Invalid accessor"); 679 return ((const LV*)(const void*)Data.buffer)->IsOnePastTheEnd; 680 } 681 682 CharUnits &APValue::getLValueOffset() { 683 assert(isLValue() && "Invalid accessor"); 684 return ((LV*)(void*)Data.buffer)->Offset; 685 } 686 687 bool APValue::hasLValuePath() const { 688 assert(isLValue() && "Invalid accessor"); 689 return ((const LV*)(const char*)Data.buffer)->hasPath(); 690 } 691 692 ArrayRef<APValue::LValuePathEntry> APValue::getLValuePath() const { 693 assert(isLValue() && hasLValuePath() && "Invalid accessor"); 694 const LV &LVal = *((const LV*)(const char*)Data.buffer); 695 return llvm::makeArrayRef(LVal.getPath(), LVal.PathLength); 696 } 697 698 unsigned APValue::getLValueCallIndex() const { 699 assert(isLValue() && "Invalid accessor"); 700 return ((const LV*)(const char*)Data.buffer)->Base.getCallIndex(); 701 } 702 703 unsigned APValue::getLValueVersion() const { 704 assert(isLValue() && "Invalid accessor"); 705 return ((const LV*)(const char*)Data.buffer)->Base.getVersion(); 706 } 707 708 bool APValue::isNullPointer() const { 709 assert(isLValue() && "Invalid usage"); 710 return ((const LV*)(const char*)Data.buffer)->IsNullPtr; 711 } 712 713 void APValue::setLValue(LValueBase B, const CharUnits &O, NoLValuePath, 714 bool IsNullPtr) { 715 assert(isLValue() && "Invalid accessor"); 716 LV &LVal = *((LV*)(char*)Data.buffer); 717 LVal.Base = B; 718 LVal.IsOnePastTheEnd = false; 719 LVal.Offset = O; 720 LVal.resizePath((unsigned)-1); 721 LVal.IsNullPtr = IsNullPtr; 722 } 723 724 void APValue::setLValue(LValueBase B, const CharUnits &O, 725 ArrayRef<LValuePathEntry> Path, bool IsOnePastTheEnd, 726 bool IsNullPtr) { 727 assert(isLValue() && "Invalid accessor"); 728 LV &LVal = *((LV*)(char*)Data.buffer); 729 LVal.Base = B; 730 LVal.IsOnePastTheEnd = IsOnePastTheEnd; 731 LVal.Offset = O; 732 LVal.resizePath(Path.size()); 733 memcpy(LVal.getPath(), Path.data(), Path.size() * sizeof(LValuePathEntry)); 734 LVal.IsNullPtr = IsNullPtr; 735 } 736 737 const ValueDecl *APValue::getMemberPointerDecl() const { 738 assert(isMemberPointer() && "Invalid accessor"); 739 const MemberPointerData &MPD = 740 *((const MemberPointerData *)(const char *)Data.buffer); 741 return MPD.MemberAndIsDerivedMember.getPointer(); 742 } 743 744 bool APValue::isMemberPointerToDerivedMember() const { 745 assert(isMemberPointer() && "Invalid accessor"); 746 const MemberPointerData &MPD = 747 *((const MemberPointerData *)(const char *)Data.buffer); 748 return MPD.MemberAndIsDerivedMember.getInt(); 749 } 750 751 ArrayRef<const CXXRecordDecl*> APValue::getMemberPointerPath() const { 752 assert(isMemberPointer() && "Invalid accessor"); 753 const MemberPointerData &MPD = 754 *((const MemberPointerData *)(const char *)Data.buffer); 755 return llvm::makeArrayRef(MPD.getPath(), MPD.PathLength); 756 } 757 758 void APValue::MakeLValue() { 759 assert(isAbsent() && "Bad state change"); 760 static_assert(sizeof(LV) <= DataSize, "LV too big"); 761 new ((void*)(char*)Data.buffer) LV(); 762 Kind = LValue; 763 } 764 765 void APValue::MakeArray(unsigned InitElts, unsigned Size) { 766 assert(isAbsent() && "Bad state change"); 767 new ((void*)(char*)Data.buffer) Arr(InitElts, Size); 768 Kind = Array; 769 } 770 771 void APValue::MakeMemberPointer(const ValueDecl *Member, bool IsDerivedMember, 772 ArrayRef<const CXXRecordDecl*> Path) { 773 assert(isAbsent() && "Bad state change"); 774 MemberPointerData *MPD = new ((void*)(char*)Data.buffer) MemberPointerData; 775 Kind = MemberPointer; 776 MPD->MemberAndIsDerivedMember.setPointer(Member); 777 MPD->MemberAndIsDerivedMember.setInt(IsDerivedMember); 778 MPD->resizePath(Path.size()); 779 memcpy(MPD->getPath(), Path.data(), Path.size()*sizeof(const CXXRecordDecl*)); 780 } 781