1 //===--- APValue.cpp - Union class for APFloat/APSInt/Complex -------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This file implements the APValue class. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "clang/AST/APValue.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/Type.h" 20 #include "clang/Basic/Diagnostic.h" 21 #include "llvm/ADT/SmallString.h" 22 #include "llvm/Support/raw_ostream.h" 23 #include "llvm/Support/ErrorHandling.h" 24 using namespace clang; 25 26 namespace { 27 struct LVBase { 28 llvm::PointerIntPair<APValue::LValueBase, 1, bool> BaseAndIsOnePastTheEnd; 29 CharUnits Offset; 30 unsigned PathLength; 31 unsigned CallIndex; 32 }; 33 } 34 35 struct APValue::LV : LVBase { 36 static const unsigned InlinePathSpace = 37 (MaxSize - sizeof(LVBase)) / sizeof(LValuePathEntry); 38 39 /// Path - The sequence of base classes, fields and array indices to follow to 40 /// walk from Base to the subobject. When performing GCC-style folding, there 41 /// may not be such a path. 42 union { 43 LValuePathEntry Path[InlinePathSpace]; 44 LValuePathEntry *PathPtr; 45 }; 46 47 LV() { PathLength = (unsigned)-1; } 48 ~LV() { resizePath(0); } 49 50 void resizePath(unsigned Length) { 51 if (Length == PathLength) 52 return; 53 if (hasPathPtr()) 54 delete [] PathPtr; 55 PathLength = Length; 56 if (hasPathPtr()) 57 PathPtr = new LValuePathEntry[Length]; 58 } 59 60 bool hasPath() const { return PathLength != (unsigned)-1; } 61 bool hasPathPtr() const { return hasPath() && PathLength > InlinePathSpace; } 62 63 LValuePathEntry *getPath() { return hasPathPtr() ? PathPtr : Path; } 64 const LValuePathEntry *getPath() const { 65 return hasPathPtr() ? PathPtr : Path; 66 } 67 }; 68 69 namespace { 70 struct MemberPointerBase { 71 llvm::PointerIntPair<const ValueDecl*, 1, bool> MemberAndIsDerivedMember; 72 unsigned PathLength; 73 }; 74 } 75 76 struct APValue::MemberPointerData : MemberPointerBase { 77 static const unsigned InlinePathSpace = 78 (MaxSize - sizeof(MemberPointerBase)) / sizeof(const CXXRecordDecl*); 79 typedef const CXXRecordDecl *PathElem; 80 union { 81 PathElem Path[InlinePathSpace]; 82 PathElem *PathPtr; 83 }; 84 85 MemberPointerData() { PathLength = 0; } 86 ~MemberPointerData() { resizePath(0); } 87 88 void resizePath(unsigned Length) { 89 if (Length == PathLength) 90 return; 91 if (hasPathPtr()) 92 delete [] PathPtr; 93 PathLength = Length; 94 if (hasPathPtr()) 95 PathPtr = new PathElem[Length]; 96 } 97 98 bool hasPathPtr() const { return PathLength > InlinePathSpace; } 99 100 PathElem *getPath() { return hasPathPtr() ? PathPtr : Path; } 101 const PathElem *getPath() const { 102 return hasPathPtr() ? PathPtr : Path; 103 } 104 }; 105 106 // FIXME: Reduce the malloc traffic here. 107 108 APValue::Arr::Arr(unsigned NumElts, unsigned Size) : 109 Elts(new APValue[NumElts + (NumElts != Size ? 1 : 0)]), 110 NumElts(NumElts), ArrSize(Size) {} 111 APValue::Arr::~Arr() { delete [] Elts; } 112 113 APValue::StructData::StructData(unsigned NumBases, unsigned NumFields) : 114 Elts(new APValue[NumBases+NumFields]), 115 NumBases(NumBases), NumFields(NumFields) {} 116 APValue::StructData::~StructData() { 117 delete [] Elts; 118 } 119 120 APValue::UnionData::UnionData() : Field(0), Value(new APValue) {} 121 APValue::UnionData::~UnionData () { 122 delete Value; 123 } 124 125 APValue::APValue(const APValue &RHS) : Kind(Uninitialized) { 126 switch (RHS.getKind()) { 127 case Uninitialized: 128 break; 129 case Int: 130 MakeInt(); 131 setInt(RHS.getInt()); 132 break; 133 case Float: 134 MakeFloat(); 135 setFloat(RHS.getFloat()); 136 break; 137 case Vector: 138 MakeVector(); 139 setVector(((const Vec *)(const char *)RHS.Data)->Elts, 140 RHS.getVectorLength()); 141 break; 142 case ComplexInt: 143 MakeComplexInt(); 144 setComplexInt(RHS.getComplexIntReal(), RHS.getComplexIntImag()); 145 break; 146 case ComplexFloat: 147 MakeComplexFloat(); 148 setComplexFloat(RHS.getComplexFloatReal(), RHS.getComplexFloatImag()); 149 break; 150 case LValue: 151 MakeLValue(); 152 if (RHS.hasLValuePath()) 153 setLValue(RHS.getLValueBase(), RHS.getLValueOffset(), RHS.getLValuePath(), 154 RHS.isLValueOnePastTheEnd(), RHS.getLValueCallIndex()); 155 else 156 setLValue(RHS.getLValueBase(), RHS.getLValueOffset(), NoLValuePath(), 157 RHS.getLValueCallIndex()); 158 break; 159 case Array: 160 MakeArray(RHS.getArrayInitializedElts(), RHS.getArraySize()); 161 for (unsigned I = 0, N = RHS.getArrayInitializedElts(); I != N; ++I) 162 getArrayInitializedElt(I) = RHS.getArrayInitializedElt(I); 163 if (RHS.hasArrayFiller()) 164 getArrayFiller() = RHS.getArrayFiller(); 165 break; 166 case Struct: 167 MakeStruct(RHS.getStructNumBases(), RHS.getStructNumFields()); 168 for (unsigned I = 0, N = RHS.getStructNumBases(); I != N; ++I) 169 getStructBase(I) = RHS.getStructBase(I); 170 for (unsigned I = 0, N = RHS.getStructNumFields(); I != N; ++I) 171 getStructField(I) = RHS.getStructField(I); 172 break; 173 case Union: 174 MakeUnion(); 175 setUnion(RHS.getUnionField(), RHS.getUnionValue()); 176 break; 177 case MemberPointer: 178 MakeMemberPointer(RHS.getMemberPointerDecl(), 179 RHS.isMemberPointerToDerivedMember(), 180 RHS.getMemberPointerPath()); 181 break; 182 case AddrLabelDiff: 183 MakeAddrLabelDiff(); 184 setAddrLabelDiff(RHS.getAddrLabelDiffLHS(), RHS.getAddrLabelDiffRHS()); 185 break; 186 } 187 } 188 189 void APValue::DestroyDataAndMakeUninit() { 190 if (Kind == Int) 191 ((APSInt*)(char*)Data)->~APSInt(); 192 else if (Kind == Float) 193 ((APFloat*)(char*)Data)->~APFloat(); 194 else if (Kind == Vector) 195 ((Vec*)(char*)Data)->~Vec(); 196 else if (Kind == ComplexInt) 197 ((ComplexAPSInt*)(char*)Data)->~ComplexAPSInt(); 198 else if (Kind == ComplexFloat) 199 ((ComplexAPFloat*)(char*)Data)->~ComplexAPFloat(); 200 else if (Kind == LValue) 201 ((LV*)(char*)Data)->~LV(); 202 else if (Kind == Array) 203 ((Arr*)(char*)Data)->~Arr(); 204 else if (Kind == Struct) 205 ((StructData*)(char*)Data)->~StructData(); 206 else if (Kind == Union) 207 ((UnionData*)(char*)Data)->~UnionData(); 208 else if (Kind == MemberPointer) 209 ((MemberPointerData*)(char*)Data)->~MemberPointerData(); 210 else if (Kind == AddrLabelDiff) 211 ((AddrLabelDiffData*)(char*)Data)->~AddrLabelDiffData(); 212 Kind = Uninitialized; 213 } 214 215 void APValue::swap(APValue &RHS) { 216 std::swap(Kind, RHS.Kind); 217 char TmpData[MaxSize]; 218 memcpy(TmpData, Data, MaxSize); 219 memcpy(Data, RHS.Data, MaxSize); 220 memcpy(RHS.Data, TmpData, MaxSize); 221 } 222 223 void APValue::dump() const { 224 dump(llvm::errs()); 225 llvm::errs() << '\n'; 226 } 227 228 static double GetApproxValue(const llvm::APFloat &F) { 229 llvm::APFloat V = F; 230 bool ignored; 231 V.convert(llvm::APFloat::IEEEdouble, llvm::APFloat::rmNearestTiesToEven, 232 &ignored); 233 return V.convertToDouble(); 234 } 235 236 void APValue::dump(raw_ostream &OS) const { 237 switch (getKind()) { 238 case Uninitialized: 239 OS << "Uninitialized"; 240 return; 241 case Int: 242 OS << "Int: " << getInt(); 243 return; 244 case Float: 245 OS << "Float: " << GetApproxValue(getFloat()); 246 return; 247 case Vector: 248 OS << "Vector: "; 249 getVectorElt(0).dump(OS); 250 for (unsigned i = 1; i != getVectorLength(); ++i) { 251 OS << ", "; 252 getVectorElt(i).dump(OS); 253 } 254 return; 255 case ComplexInt: 256 OS << "ComplexInt: " << getComplexIntReal() << ", " << getComplexIntImag(); 257 return; 258 case ComplexFloat: 259 OS << "ComplexFloat: " << GetApproxValue(getComplexFloatReal()) 260 << ", " << GetApproxValue(getComplexFloatImag()); 261 return; 262 case LValue: 263 OS << "LValue: <todo>"; 264 return; 265 case Array: 266 OS << "Array: "; 267 for (unsigned I = 0, N = getArrayInitializedElts(); I != N; ++I) { 268 getArrayInitializedElt(I).dump(OS); 269 if (I != getArraySize() - 1) OS << ", "; 270 } 271 if (hasArrayFiller()) { 272 OS << getArraySize() - getArrayInitializedElts() << " x "; 273 getArrayFiller().dump(OS); 274 } 275 return; 276 case Struct: 277 OS << "Struct "; 278 if (unsigned N = getStructNumBases()) { 279 OS << " bases: "; 280 getStructBase(0).dump(OS); 281 for (unsigned I = 1; I != N; ++I) { 282 OS << ", "; 283 getStructBase(I).dump(OS); 284 } 285 } 286 if (unsigned N = getStructNumFields()) { 287 OS << " fields: "; 288 getStructField(0).dump(OS); 289 for (unsigned I = 1; I != N; ++I) { 290 OS << ", "; 291 getStructField(I).dump(OS); 292 } 293 } 294 return; 295 case Union: 296 OS << "Union: "; 297 getUnionValue().dump(OS); 298 return; 299 case MemberPointer: 300 OS << "MemberPointer: <todo>"; 301 return; 302 case AddrLabelDiff: 303 OS << "AddrLabelDiff: <todo>"; 304 return; 305 } 306 llvm_unreachable("Unknown APValue kind!"); 307 } 308 309 void APValue::printPretty(raw_ostream &Out, ASTContext &Ctx, QualType Ty) const{ 310 switch (getKind()) { 311 case APValue::Uninitialized: 312 Out << "<uninitialized>"; 313 return; 314 case APValue::Int: 315 if (Ty->isBooleanType()) 316 Out << (getInt().getBoolValue() ? "true" : "false"); 317 else 318 Out << getInt(); 319 return; 320 case APValue::Float: 321 Out << GetApproxValue(getFloat()); 322 return; 323 case APValue::Vector: { 324 Out << '{'; 325 QualType ElemTy = Ty->getAs<VectorType>()->getElementType(); 326 getVectorElt(0).printPretty(Out, Ctx, ElemTy); 327 for (unsigned i = 1; i != getVectorLength(); ++i) { 328 Out << ", "; 329 getVectorElt(i).printPretty(Out, Ctx, ElemTy); 330 } 331 Out << '}'; 332 return; 333 } 334 case APValue::ComplexInt: 335 Out << getComplexIntReal() << "+" << getComplexIntImag() << "i"; 336 return; 337 case APValue::ComplexFloat: 338 Out << GetApproxValue(getComplexFloatReal()) << "+" 339 << GetApproxValue(getComplexFloatImag()) << "i"; 340 return; 341 case APValue::LValue: { 342 LValueBase Base = getLValueBase(); 343 if (!Base) { 344 Out << "0"; 345 return; 346 } 347 348 bool IsReference = Ty->isReferenceType(); 349 QualType InnerTy 350 = IsReference ? Ty.getNonReferenceType() : Ty->getPointeeType(); 351 352 if (!hasLValuePath()) { 353 // No lvalue path: just print the offset. 354 CharUnits O = getLValueOffset(); 355 CharUnits S = Ctx.getTypeSizeInChars(InnerTy); 356 if (!O.isZero()) { 357 if (IsReference) 358 Out << "*("; 359 if (O % S) { 360 Out << "(char*)"; 361 S = CharUnits::One(); 362 } 363 Out << '&'; 364 } else if (!IsReference) 365 Out << '&'; 366 367 if (const ValueDecl *VD = Base.dyn_cast<const ValueDecl*>()) 368 Out << *VD; 369 else 370 Base.get<const Expr*>()->printPretty(Out, Ctx, 0, 371 Ctx.getPrintingPolicy()); 372 if (!O.isZero()) { 373 Out << " + " << (O / S); 374 if (IsReference) 375 Out << ')'; 376 } 377 return; 378 } 379 380 // We have an lvalue path. Print it out nicely. 381 if (!IsReference) 382 Out << '&'; 383 else if (isLValueOnePastTheEnd()) 384 Out << "*(&"; 385 386 QualType ElemTy; 387 if (const ValueDecl *VD = Base.dyn_cast<const ValueDecl*>()) { 388 Out << *VD; 389 ElemTy = VD->getType(); 390 } else { 391 const Expr *E = Base.get<const Expr*>(); 392 E->printPretty(Out, Ctx, 0,Ctx.getPrintingPolicy()); 393 ElemTy = E->getType(); 394 } 395 396 ArrayRef<LValuePathEntry> Path = getLValuePath(); 397 const CXXRecordDecl *CastToBase = 0; 398 for (unsigned I = 0, N = Path.size(); I != N; ++I) { 399 if (ElemTy->getAs<RecordType>()) { 400 // The lvalue refers to a class type, so the next path entry is a base 401 // or member. 402 const Decl *BaseOrMember = 403 BaseOrMemberType::getFromOpaqueValue(Path[I].BaseOrMember).getPointer(); 404 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(BaseOrMember)) { 405 CastToBase = RD; 406 ElemTy = Ctx.getRecordType(RD); 407 } else { 408 const ValueDecl *VD = cast<ValueDecl>(BaseOrMember); 409 Out << "."; 410 if (CastToBase) 411 Out << *CastToBase << "::"; 412 Out << *VD; 413 ElemTy = VD->getType(); 414 } 415 } else { 416 // The lvalue must refer to an array. 417 Out << '[' << Path[I].ArrayIndex << ']'; 418 ElemTy = Ctx.getAsArrayType(ElemTy)->getElementType(); 419 } 420 } 421 422 // Handle formatting of one-past-the-end lvalues. 423 if (isLValueOnePastTheEnd()) { 424 // FIXME: If CastToBase is non-0, we should prefix the output with 425 // "(CastToBase*)". 426 Out << " + 1"; 427 if (IsReference) 428 Out << ')'; 429 } 430 return; 431 } 432 case APValue::Array: { 433 const ArrayType *AT = Ctx.getAsArrayType(Ty); 434 QualType ElemTy = AT->getElementType(); 435 Out << '{'; 436 if (unsigned N = getArrayInitializedElts()) { 437 getArrayInitializedElt(0).printPretty(Out, Ctx, ElemTy); 438 for (unsigned I = 1; I != N; ++I) { 439 Out << ", "; 440 if (I == 10) { 441 // Avoid printing out the entire contents of large arrays. 442 Out << "..."; 443 break; 444 } 445 getArrayInitializedElt(I).printPretty(Out, Ctx, ElemTy); 446 } 447 } 448 Out << '}'; 449 return; 450 } 451 case APValue::Struct: { 452 Out << '{'; 453 const RecordDecl *RD = Ty->getAs<RecordType>()->getDecl(); 454 bool First = true; 455 if (unsigned N = getStructNumBases()) { 456 const CXXRecordDecl *CD = cast<CXXRecordDecl>(RD); 457 CXXRecordDecl::base_class_const_iterator BI = CD->bases_begin(); 458 for (unsigned I = 0; I != N; ++I, ++BI) { 459 assert(BI != CD->bases_end()); 460 if (!First) 461 Out << ", "; 462 getStructBase(I).printPretty(Out, Ctx, BI->getType()); 463 First = false; 464 } 465 } 466 for (RecordDecl::field_iterator FI = RD->field_begin(); 467 FI != RD->field_end(); ++FI) { 468 if (!First) 469 Out << ", "; 470 if ((*FI)->isUnnamedBitfield()) continue; 471 getStructField((*FI)->getFieldIndex()). 472 printPretty(Out, Ctx, (*FI)->getType()); 473 First = false; 474 } 475 Out << '}'; 476 return; 477 } 478 case APValue::Union: 479 Out << '{'; 480 if (const FieldDecl *FD = getUnionField()) { 481 Out << "." << *FD << " = "; 482 getUnionValue().printPretty(Out, Ctx, FD->getType()); 483 } 484 Out << '}'; 485 return; 486 case APValue::MemberPointer: 487 // FIXME: This is not enough to unambiguously identify the member in a 488 // multiple-inheritance scenario. 489 if (const ValueDecl *VD = getMemberPointerDecl()) { 490 Out << '&' << *cast<CXXRecordDecl>(VD->getDeclContext()) << "::" << *VD; 491 return; 492 } 493 Out << "0"; 494 return; 495 case APValue::AddrLabelDiff: 496 Out << "&&" << getAddrLabelDiffLHS()->getLabel()->getName(); 497 Out << " - "; 498 Out << "&&" << getAddrLabelDiffRHS()->getLabel()->getName(); 499 return; 500 } 501 llvm_unreachable("Unknown APValue kind!"); 502 } 503 504 std::string APValue::getAsString(ASTContext &Ctx, QualType Ty) const { 505 std::string Result; 506 llvm::raw_string_ostream Out(Result); 507 printPretty(Out, Ctx, Ty); 508 Out.flush(); 509 return Result; 510 } 511 512 const APValue::LValueBase APValue::getLValueBase() const { 513 assert(isLValue() && "Invalid accessor"); 514 return ((const LV*)(const void*)Data)->BaseAndIsOnePastTheEnd.getPointer(); 515 } 516 517 bool APValue::isLValueOnePastTheEnd() const { 518 assert(isLValue() && "Invalid accessor"); 519 return ((const LV*)(const void*)Data)->BaseAndIsOnePastTheEnd.getInt(); 520 } 521 522 CharUnits &APValue::getLValueOffset() { 523 assert(isLValue() && "Invalid accessor"); 524 return ((LV*)(void*)Data)->Offset; 525 } 526 527 bool APValue::hasLValuePath() const { 528 assert(isLValue() && "Invalid accessor"); 529 return ((const LV*)(const char*)Data)->hasPath(); 530 } 531 532 ArrayRef<APValue::LValuePathEntry> APValue::getLValuePath() const { 533 assert(isLValue() && hasLValuePath() && "Invalid accessor"); 534 const LV &LVal = *((const LV*)(const char*)Data); 535 return ArrayRef<LValuePathEntry>(LVal.getPath(), LVal.PathLength); 536 } 537 538 unsigned APValue::getLValueCallIndex() const { 539 assert(isLValue() && "Invalid accessor"); 540 return ((const LV*)(const char*)Data)->CallIndex; 541 } 542 543 void APValue::setLValue(LValueBase B, const CharUnits &O, NoLValuePath, 544 unsigned CallIndex) { 545 assert(isLValue() && "Invalid accessor"); 546 LV &LVal = *((LV*)(char*)Data); 547 LVal.BaseAndIsOnePastTheEnd.setPointer(B); 548 LVal.BaseAndIsOnePastTheEnd.setInt(false); 549 LVal.Offset = O; 550 LVal.CallIndex = CallIndex; 551 LVal.resizePath((unsigned)-1); 552 } 553 554 void APValue::setLValue(LValueBase B, const CharUnits &O, 555 ArrayRef<LValuePathEntry> Path, bool IsOnePastTheEnd, 556 unsigned CallIndex) { 557 assert(isLValue() && "Invalid accessor"); 558 LV &LVal = *((LV*)(char*)Data); 559 LVal.BaseAndIsOnePastTheEnd.setPointer(B); 560 LVal.BaseAndIsOnePastTheEnd.setInt(IsOnePastTheEnd); 561 LVal.Offset = O; 562 LVal.CallIndex = CallIndex; 563 LVal.resizePath(Path.size()); 564 memcpy(LVal.getPath(), Path.data(), Path.size() * sizeof(LValuePathEntry)); 565 } 566 567 const ValueDecl *APValue::getMemberPointerDecl() const { 568 assert(isMemberPointer() && "Invalid accessor"); 569 const MemberPointerData &MPD = *((const MemberPointerData*)(const char*)Data); 570 return MPD.MemberAndIsDerivedMember.getPointer(); 571 } 572 573 bool APValue::isMemberPointerToDerivedMember() const { 574 assert(isMemberPointer() && "Invalid accessor"); 575 const MemberPointerData &MPD = *((const MemberPointerData*)(const char*)Data); 576 return MPD.MemberAndIsDerivedMember.getInt(); 577 } 578 579 ArrayRef<const CXXRecordDecl*> APValue::getMemberPointerPath() const { 580 assert(isMemberPointer() && "Invalid accessor"); 581 const MemberPointerData &MPD = *((const MemberPointerData*)(const char*)Data); 582 return ArrayRef<const CXXRecordDecl*>(MPD.getPath(), MPD.PathLength); 583 } 584 585 void APValue::MakeLValue() { 586 assert(isUninit() && "Bad state change"); 587 assert(sizeof(LV) <= MaxSize && "LV too big"); 588 new ((void*)(char*)Data) LV(); 589 Kind = LValue; 590 } 591 592 void APValue::MakeArray(unsigned InitElts, unsigned Size) { 593 assert(isUninit() && "Bad state change"); 594 new ((void*)(char*)Data) Arr(InitElts, Size); 595 Kind = Array; 596 } 597 598 void APValue::MakeMemberPointer(const ValueDecl *Member, bool IsDerivedMember, 599 ArrayRef<const CXXRecordDecl*> Path) { 600 assert(isUninit() && "Bad state change"); 601 MemberPointerData *MPD = new ((void*)(char*)Data) MemberPointerData; 602 Kind = MemberPointer; 603 MPD->MemberAndIsDerivedMember.setPointer(Member); 604 MPD->MemberAndIsDerivedMember.setInt(IsDerivedMember); 605 MPD->resizePath(Path.size()); 606 memcpy(MPD->getPath(), Path.data(), Path.size()*sizeof(const CXXRecordDecl*)); 607 } 608