APValue.cpp 25 KB

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