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