blob: c05b160b8e3d9ca521f29656d5349992e28bdee5 [file] [log] [blame]
Chris Lattner981f33b2008-11-16 07:46:48 +00001//===--- 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"
Richard Smithf6f003a2011-12-16 19:06:07 +000015#include "clang/AST/ASTContext.h"
Ken Dyck02990832010-01-15 12:37:54 +000016#include "clang/AST/CharUnits.h"
Richard Smithf6f003a2011-12-16 19:06:07 +000017#include "clang/AST/DeclCXX.h"
18#include "clang/AST/Expr.h"
19#include "clang/AST/Type.h"
David Blaikie76bd3c82011-09-23 05:35:21 +000020#include "llvm/Support/ErrorHandling.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000021#include "llvm/Support/raw_ostream.h"
Chris Lattner981f33b2008-11-16 07:46:48 +000022using namespace clang;
23
Ken Dyck02990832010-01-15 12:37:54 +000024namespace {
Richard Smith80815602011-11-07 05:07:52 +000025 struct LVBase {
Akira Hatanaka4e2698c2018-04-10 05:15:01 +000026 APValue::LValueBase Base;
Ken Dyck02990832010-01-15 12:37:54 +000027 CharUnits Offset;
Richard Smith80815602011-11-07 05:07:52 +000028 unsigned PathLength;
Akira Hatanaka4e2698c2018-04-10 05:15:01 +000029 bool IsNullPtr : 1;
30 bool IsOnePastTheEnd : 1;
Ken Dyck02990832010-01-15 12:37:54 +000031 };
32}
33
Akira Hatanaka4e2698c2018-04-10 05:15:01 +000034void *APValue::LValueBase::getOpaqueValue() const {
35 return Ptr.getOpaqueValue();
36}
37
38bool APValue::LValueBase::isNull() const {
39 return Ptr.isNull();
40}
41
42APValue::LValueBase::operator bool () const {
43 return static_cast<bool>(Ptr);
44}
45
46clang::APValue::LValueBase
47llvm::DenseMapInfo<clang::APValue::LValueBase>::getEmptyKey() {
48 return clang::APValue::LValueBase(
49 DenseMapInfo<clang::APValue::LValueBase::PtrTy>::getEmptyKey(),
50 DenseMapInfo<unsigned>::getEmptyKey(),
51 DenseMapInfo<unsigned>::getEmptyKey());
52}
53
54clang::APValue::LValueBase
55llvm::DenseMapInfo<clang::APValue::LValueBase>::getTombstoneKey() {
56 return clang::APValue::LValueBase(
57 DenseMapInfo<clang::APValue::LValueBase::PtrTy>::getTombstoneKey(),
58 DenseMapInfo<unsigned>::getTombstoneKey(),
59 DenseMapInfo<unsigned>::getTombstoneKey());
60}
61
62unsigned llvm::DenseMapInfo<clang::APValue::LValueBase>::getHashValue(
63 const clang::APValue::LValueBase &Base) {
64 llvm::FoldingSetNodeID ID;
65 ID.AddPointer(Base.getOpaqueValue());
66 ID.AddInteger(Base.getCallIndex());
67 ID.AddInteger(Base.getVersion());
68 return ID.ComputeHash();
69}
70
71bool llvm::DenseMapInfo<clang::APValue::LValueBase>::isEqual(
72 const clang::APValue::LValueBase &LHS,
73 const clang::APValue::LValueBase &RHS) {
74 return LHS == RHS;
75}
76
Richard Smith80815602011-11-07 05:07:52 +000077struct APValue::LV : LVBase {
78 static const unsigned InlinePathSpace =
Richard Smith7073a2d2014-01-10 00:40:45 +000079 (DataSize - sizeof(LVBase)) / sizeof(LValuePathEntry);
Richard Smith80815602011-11-07 05:07:52 +000080
81 /// Path - The sequence of base classes, fields and array indices to follow to
82 /// walk from Base to the subobject. When performing GCC-style folding, there
83 /// may not be such a path.
84 union {
85 LValuePathEntry Path[InlinePathSpace];
86 LValuePathEntry *PathPtr;
87 };
88
89 LV() { PathLength = (unsigned)-1; }
Richard Smith027bf112011-11-17 22:56:20 +000090 ~LV() { resizePath(0); }
Richard Smith80815602011-11-07 05:07:52 +000091
Richard Smith027bf112011-11-17 22:56:20 +000092 void resizePath(unsigned Length) {
93 if (Length == PathLength)
94 return;
95 if (hasPathPtr())
96 delete [] PathPtr;
97 PathLength = Length;
98 if (hasPathPtr())
99 PathPtr = new LValuePathEntry[Length];
Richard Smith80815602011-11-07 05:07:52 +0000100 }
101
102 bool hasPath() const { return PathLength != (unsigned)-1; }
103 bool hasPathPtr() const { return hasPath() && PathLength > InlinePathSpace; }
104
105 LValuePathEntry *getPath() { return hasPathPtr() ? PathPtr : Path; }
Richard Smithbcb4eb22011-11-07 07:31:09 +0000106 const LValuePathEntry *getPath() const {
107 return hasPathPtr() ? PathPtr : Path;
108 }
Richard Smith80815602011-11-07 05:07:52 +0000109};
110
Richard Smith027bf112011-11-17 22:56:20 +0000111namespace {
112 struct MemberPointerBase {
113 llvm::PointerIntPair<const ValueDecl*, 1, bool> MemberAndIsDerivedMember;
114 unsigned PathLength;
115 };
116}
117
118struct APValue::MemberPointerData : MemberPointerBase {
119 static const unsigned InlinePathSpace =
Richard Smith7073a2d2014-01-10 00:40:45 +0000120 (DataSize - sizeof(MemberPointerBase)) / sizeof(const CXXRecordDecl*);
Richard Smith027bf112011-11-17 22:56:20 +0000121 typedef const CXXRecordDecl *PathElem;
122 union {
123 PathElem Path[InlinePathSpace];
124 PathElem *PathPtr;
125 };
126
127 MemberPointerData() { PathLength = 0; }
128 ~MemberPointerData() { resizePath(0); }
129
130 void resizePath(unsigned Length) {
131 if (Length == PathLength)
132 return;
133 if (hasPathPtr())
134 delete [] PathPtr;
135 PathLength = Length;
136 if (hasPathPtr())
137 PathPtr = new PathElem[Length];
138 }
139
140 bool hasPathPtr() const { return PathLength > InlinePathSpace; }
141
142 PathElem *getPath() { return hasPathPtr() ? PathPtr : Path; }
143 const PathElem *getPath() const {
144 return hasPathPtr() ? PathPtr : Path;
145 }
146};
147
Richard Smithf3e9e432011-11-07 09:22:26 +0000148// FIXME: Reduce the malloc traffic here.
149
150APValue::Arr::Arr(unsigned NumElts, unsigned Size) :
151 Elts(new APValue[NumElts + (NumElts != Size ? 1 : 0)]),
152 NumElts(NumElts), ArrSize(Size) {}
153APValue::Arr::~Arr() { delete [] Elts; }
154
Richard Smithd62306a2011-11-10 06:34:14 +0000155APValue::StructData::StructData(unsigned NumBases, unsigned NumFields) :
156 Elts(new APValue[NumBases+NumFields]),
157 NumBases(NumBases), NumFields(NumFields) {}
158APValue::StructData::~StructData() {
159 delete [] Elts;
160}
161
Craig Topper36250ad2014-05-12 05:36:57 +0000162APValue::UnionData::UnionData() : Field(nullptr), Value(new APValue) {}
Richard Smithd62306a2011-11-10 06:34:14 +0000163APValue::UnionData::~UnionData () {
164 delete Value;
165}
166
Richard Smith4e9e5232012-03-10 00:28:11 +0000167APValue::APValue(const APValue &RHS) : Kind(Uninitialized) {
168 switch (RHS.getKind()) {
169 case Uninitialized:
170 break;
171 case Int:
172 MakeInt();
Chris Lattner981f33b2008-11-16 07:46:48 +0000173 setInt(RHS.getInt());
Richard Smith4e9e5232012-03-10 00:28:11 +0000174 break;
175 case Float:
176 MakeFloat();
Chris Lattner981f33b2008-11-16 07:46:48 +0000177 setFloat(RHS.getFloat());
Richard Smith4e9e5232012-03-10 00:28:11 +0000178 break;
179 case Vector:
180 MakeVector();
Richard Smith7073a2d2014-01-10 00:40:45 +0000181 setVector(((const Vec *)(const char *)RHS.Data.buffer)->Elts,
Dan Gohman145f3f12010-04-19 16:39:44 +0000182 RHS.getVectorLength());
Richard Smith4e9e5232012-03-10 00:28:11 +0000183 break;
184 case ComplexInt:
185 MakeComplexInt();
Chris Lattner981f33b2008-11-16 07:46:48 +0000186 setComplexInt(RHS.getComplexIntReal(), RHS.getComplexIntImag());
Richard Smith4e9e5232012-03-10 00:28:11 +0000187 break;
188 case ComplexFloat:
189 MakeComplexFloat();
Chris Lattner981f33b2008-11-16 07:46:48 +0000190 setComplexFloat(RHS.getComplexFloatReal(), RHS.getComplexFloatImag());
Richard Smith4e9e5232012-03-10 00:28:11 +0000191 break;
192 case LValue:
193 MakeLValue();
Richard Smith80815602011-11-07 05:07:52 +0000194 if (RHS.hasLValuePath())
Richard Smith027bf112011-11-17 22:56:20 +0000195 setLValue(RHS.getLValueBase(), RHS.getLValueOffset(), RHS.getLValuePath(),
Akira Hatanaka4e2698c2018-04-10 05:15:01 +0000196 RHS.isLValueOnePastTheEnd(), RHS.isNullPointer());
Richard Smith80815602011-11-07 05:07:52 +0000197 else
Richard Smithb228a862012-02-15 02:18:13 +0000198 setLValue(RHS.getLValueBase(), RHS.getLValueOffset(), NoLValuePath(),
Akira Hatanaka4e2698c2018-04-10 05:15:01 +0000199 RHS.isNullPointer());
Richard Smith4e9e5232012-03-10 00:28:11 +0000200 break;
201 case Array:
202 MakeArray(RHS.getArrayInitializedElts(), RHS.getArraySize());
Richard Smithf3e9e432011-11-07 09:22:26 +0000203 for (unsigned I = 0, N = RHS.getArrayInitializedElts(); I != N; ++I)
204 getArrayInitializedElt(I) = RHS.getArrayInitializedElt(I);
205 if (RHS.hasArrayFiller())
206 getArrayFiller() = RHS.getArrayFiller();
Richard Smith4e9e5232012-03-10 00:28:11 +0000207 break;
208 case Struct:
209 MakeStruct(RHS.getStructNumBases(), RHS.getStructNumFields());
Richard Smithd62306a2011-11-10 06:34:14 +0000210 for (unsigned I = 0, N = RHS.getStructNumBases(); I != N; ++I)
211 getStructBase(I) = RHS.getStructBase(I);
212 for (unsigned I = 0, N = RHS.getStructNumFields(); I != N; ++I)
213 getStructField(I) = RHS.getStructField(I);
Richard Smith4e9e5232012-03-10 00:28:11 +0000214 break;
215 case Union:
216 MakeUnion();
Richard Smithd62306a2011-11-10 06:34:14 +0000217 setUnion(RHS.getUnionField(), RHS.getUnionValue());
Richard Smith4e9e5232012-03-10 00:28:11 +0000218 break;
219 case MemberPointer:
220 MakeMemberPointer(RHS.getMemberPointerDecl(),
221 RHS.isMemberPointerToDerivedMember(),
222 RHS.getMemberPointerPath());
223 break;
224 case AddrLabelDiff:
225 MakeAddrLabelDiff();
Eli Friedmanfd5e54d2012-01-04 23:13:47 +0000226 setAddrLabelDiff(RHS.getAddrLabelDiffLHS(), RHS.getAddrLabelDiffRHS());
Richard Smith4e9e5232012-03-10 00:28:11 +0000227 break;
Eli Friedmanfd5e54d2012-01-04 23:13:47 +0000228 }
Chris Lattner981f33b2008-11-16 07:46:48 +0000229}
230
Daniel Dunbarb7431572012-03-08 20:28:55 +0000231void APValue::DestroyDataAndMakeUninit() {
Chris Lattner981f33b2008-11-16 07:46:48 +0000232 if (Kind == Int)
Richard Smith7073a2d2014-01-10 00:40:45 +0000233 ((APSInt*)(char*)Data.buffer)->~APSInt();
Chris Lattner981f33b2008-11-16 07:46:48 +0000234 else if (Kind == Float)
Richard Smith7073a2d2014-01-10 00:40:45 +0000235 ((APFloat*)(char*)Data.buffer)->~APFloat();
Nate Begeman1e31b162009-01-18 01:01:34 +0000236 else if (Kind == Vector)
Richard Smith7073a2d2014-01-10 00:40:45 +0000237 ((Vec*)(char*)Data.buffer)->~Vec();
Chris Lattner981f33b2008-11-16 07:46:48 +0000238 else if (Kind == ComplexInt)
Richard Smith7073a2d2014-01-10 00:40:45 +0000239 ((ComplexAPSInt*)(char*)Data.buffer)->~ComplexAPSInt();
Chris Lattner981f33b2008-11-16 07:46:48 +0000240 else if (Kind == ComplexFloat)
Richard Smith7073a2d2014-01-10 00:40:45 +0000241 ((ComplexAPFloat*)(char*)Data.buffer)->~ComplexAPFloat();
Richard Smithf3e9e432011-11-07 09:22:26 +0000242 else if (Kind == LValue)
Richard Smith7073a2d2014-01-10 00:40:45 +0000243 ((LV*)(char*)Data.buffer)->~LV();
Richard Smithf3e9e432011-11-07 09:22:26 +0000244 else if (Kind == Array)
Richard Smith7073a2d2014-01-10 00:40:45 +0000245 ((Arr*)(char*)Data.buffer)->~Arr();
Richard Smithd62306a2011-11-10 06:34:14 +0000246 else if (Kind == Struct)
Richard Smith7073a2d2014-01-10 00:40:45 +0000247 ((StructData*)(char*)Data.buffer)->~StructData();
Richard Smithd62306a2011-11-10 06:34:14 +0000248 else if (Kind == Union)
Richard Smith7073a2d2014-01-10 00:40:45 +0000249 ((UnionData*)(char*)Data.buffer)->~UnionData();
Richard Smith027bf112011-11-17 22:56:20 +0000250 else if (Kind == MemberPointer)
Richard Smith7073a2d2014-01-10 00:40:45 +0000251 ((MemberPointerData*)(char*)Data.buffer)->~MemberPointerData();
Eli Friedmanfd5e54d2012-01-04 23:13:47 +0000252 else if (Kind == AddrLabelDiff)
Richard Smith7073a2d2014-01-10 00:40:45 +0000253 ((AddrLabelDiffData*)(char*)Data.buffer)->~AddrLabelDiffData();
Nate Begeman1e31b162009-01-18 01:01:34 +0000254 Kind = Uninitialized;
Chris Lattner981f33b2008-11-16 07:46:48 +0000255}
256
Manuel Klimeka7328992013-06-03 13:51:33 +0000257bool APValue::needsCleanup() const {
258 switch (getKind()) {
259 case Uninitialized:
260 case AddrLabelDiff:
261 return false;
262 case Struct:
263 case Union:
264 case Array:
265 case Vector:
266 return true;
267 case Int:
268 return getInt().needsCleanup();
269 case Float:
270 return getFloat().needsCleanup();
271 case ComplexFloat:
272 assert(getComplexFloatImag().needsCleanup() ==
273 getComplexFloatReal().needsCleanup() &&
274 "In _Complex float types, real and imaginary values always have the "
275 "same size.");
276 return getComplexFloatReal().needsCleanup();
277 case ComplexInt:
278 assert(getComplexIntImag().needsCleanup() ==
279 getComplexIntReal().needsCleanup() &&
280 "In _Complex int types, real and imaginary values must have the "
281 "same size.");
282 return getComplexIntReal().needsCleanup();
283 case LValue:
Richard Smith7073a2d2014-01-10 00:40:45 +0000284 return reinterpret_cast<const LV *>(Data.buffer)->hasPathPtr();
Manuel Klimeka7328992013-06-03 13:51:33 +0000285 case MemberPointer:
Richard Smith7073a2d2014-01-10 00:40:45 +0000286 return reinterpret_cast<const MemberPointerData *>(Data.buffer)
287 ->hasPathPtr();
Manuel Klimeka7328992013-06-03 13:51:33 +0000288 }
Benjamin Kramerd1b7cd72013-06-03 21:26:13 +0000289 llvm_unreachable("Unknown APValue kind!");
Manuel Klimeka7328992013-06-03 13:51:33 +0000290}
291
Richard Smith4e9e5232012-03-10 00:28:11 +0000292void APValue::swap(APValue &RHS) {
293 std::swap(Kind, RHS.Kind);
Richard Smith7073a2d2014-01-10 00:40:45 +0000294 char TmpData[DataSize];
295 memcpy(TmpData, Data.buffer, DataSize);
296 memcpy(Data.buffer, RHS.Data.buffer, DataSize);
297 memcpy(RHS.Data.buffer, TmpData, DataSize);
Richard Smith4e9e5232012-03-10 00:28:11 +0000298}
299
Yaron Kerencdae9412016-01-29 19:38:18 +0000300LLVM_DUMP_METHOD void APValue::dump() const {
Richard Smithf6f003a2011-12-16 19:06:07 +0000301 dump(llvm::errs());
Chris Lattner981f33b2008-11-16 07:46:48 +0000302 llvm::errs() << '\n';
Chris Lattner981f33b2008-11-16 07:46:48 +0000303}
304
305static double GetApproxValue(const llvm::APFloat &F) {
306 llvm::APFloat V = F;
307 bool ignored;
Stephan Bergmann17c7f702016-12-14 11:57:17 +0000308 V.convert(llvm::APFloat::IEEEdouble(), llvm::APFloat::rmNearestTiesToEven,
Chris Lattner981f33b2008-11-16 07:46:48 +0000309 &ignored);
310 return V.convertToDouble();
311}
312
Richard Smithf6f003a2011-12-16 19:06:07 +0000313void APValue::dump(raw_ostream &OS) const {
Chris Lattner981f33b2008-11-16 07:46:48 +0000314 switch (getKind()) {
Chris Lattner981f33b2008-11-16 07:46:48 +0000315 case Uninitialized:
316 OS << "Uninitialized";
317 return;
318 case Int:
319 OS << "Int: " << getInt();
320 return;
321 case Float:
322 OS << "Float: " << GetApproxValue(getFloat());
323 return;
Nate Begeman1e31b162009-01-18 01:01:34 +0000324 case Vector:
Richard Smithf6f003a2011-12-16 19:06:07 +0000325 OS << "Vector: ";
326 getVectorElt(0).dump(OS);
327 for (unsigned i = 1; i != getVectorLength(); ++i) {
328 OS << ", ";
329 getVectorElt(i).dump(OS);
330 }
Nate Begeman1e31b162009-01-18 01:01:34 +0000331 return;
Chris Lattner981f33b2008-11-16 07:46:48 +0000332 case ComplexInt:
333 OS << "ComplexInt: " << getComplexIntReal() << ", " << getComplexIntImag();
334 return;
335 case ComplexFloat:
336 OS << "ComplexFloat: " << GetApproxValue(getComplexFloatReal())
337 << ", " << GetApproxValue(getComplexFloatImag());
Richard Smithf3e9e432011-11-07 09:22:26 +0000338 return;
Chris Lattner981f33b2008-11-16 07:46:48 +0000339 case LValue:
340 OS << "LValue: <todo>";
341 return;
Richard Smithf3e9e432011-11-07 09:22:26 +0000342 case Array:
343 OS << "Array: ";
344 for (unsigned I = 0, N = getArrayInitializedElts(); I != N; ++I) {
Richard Smithf6f003a2011-12-16 19:06:07 +0000345 getArrayInitializedElt(I).dump(OS);
Richard Smithf3e9e432011-11-07 09:22:26 +0000346 if (I != getArraySize() - 1) OS << ", ";
347 }
Richard Smithf6f003a2011-12-16 19:06:07 +0000348 if (hasArrayFiller()) {
349 OS << getArraySize() - getArrayInitializedElts() << " x ";
350 getArrayFiller().dump(OS);
351 }
Richard Smithf3e9e432011-11-07 09:22:26 +0000352 return;
Richard Smithd62306a2011-11-10 06:34:14 +0000353 case Struct:
354 OS << "Struct ";
355 if (unsigned N = getStructNumBases()) {
Richard Smithf6f003a2011-12-16 19:06:07 +0000356 OS << " bases: ";
357 getStructBase(0).dump(OS);
358 for (unsigned I = 1; I != N; ++I) {
359 OS << ", ";
360 getStructBase(I).dump(OS);
361 }
Richard Smithd62306a2011-11-10 06:34:14 +0000362 }
363 if (unsigned N = getStructNumFields()) {
Richard Smithf6f003a2011-12-16 19:06:07 +0000364 OS << " fields: ";
365 getStructField(0).dump(OS);
366 for (unsigned I = 1; I != N; ++I) {
367 OS << ", ";
368 getStructField(I).dump(OS);
369 }
Richard Smithd62306a2011-11-10 06:34:14 +0000370 }
371 return;
372 case Union:
Richard Smithf6f003a2011-12-16 19:06:07 +0000373 OS << "Union: ";
374 getUnionValue().dump(OS);
Richard Smithd62306a2011-11-10 06:34:14 +0000375 return;
Richard Smith027bf112011-11-17 22:56:20 +0000376 case MemberPointer:
377 OS << "MemberPointer: <todo>";
378 return;
Eli Friedmanfd5e54d2012-01-04 23:13:47 +0000379 case AddrLabelDiff:
380 OS << "AddrLabelDiff: <todo>";
381 return;
Chris Lattner981f33b2008-11-16 07:46:48 +0000382 }
Richard Smithd62306a2011-11-10 06:34:14 +0000383 llvm_unreachable("Unknown APValue kind!");
Chris Lattner981f33b2008-11-16 07:46:48 +0000384}
385
Richard Smithf6f003a2011-12-16 19:06:07 +0000386void APValue::printPretty(raw_ostream &Out, ASTContext &Ctx, QualType Ty) const{
387 switch (getKind()) {
Jeffrey Yasskind2af9622011-07-18 16:43:53 +0000388 case APValue::Uninitialized:
Richard Smithf6f003a2011-12-16 19:06:07 +0000389 Out << "<uninitialized>";
Richard Smithd62306a2011-11-10 06:34:14 +0000390 return;
Jeffrey Yasskind2af9622011-07-18 16:43:53 +0000391 case APValue::Int:
Richard Smith5614ca72012-03-23 23:55:39 +0000392 if (Ty->isBooleanType())
393 Out << (getInt().getBoolValue() ? "true" : "false");
394 else
395 Out << getInt();
Richard Smithd62306a2011-11-10 06:34:14 +0000396 return;
Jeffrey Yasskind2af9622011-07-18 16:43:53 +0000397 case APValue::Float:
Richard Smithf6f003a2011-12-16 19:06:07 +0000398 Out << GetApproxValue(getFloat());
Richard Smithd62306a2011-11-10 06:34:14 +0000399 return;
Richard Smithf6f003a2011-12-16 19:06:07 +0000400 case APValue::Vector: {
401 Out << '{';
402 QualType ElemTy = Ty->getAs<VectorType>()->getElementType();
403 getVectorElt(0).printPretty(Out, Ctx, ElemTy);
404 for (unsigned i = 1; i != getVectorLength(); ++i) {
Jeffrey Yasskind2af9622011-07-18 16:43:53 +0000405 Out << ", ";
Richard Smithf6f003a2011-12-16 19:06:07 +0000406 getVectorElt(i).printPretty(Out, Ctx, ElemTy);
Jeffrey Yasskind2af9622011-07-18 16:43:53 +0000407 }
Richard Smithf6f003a2011-12-16 19:06:07 +0000408 Out << '}';
Richard Smithd62306a2011-11-10 06:34:14 +0000409 return;
Richard Smithf6f003a2011-12-16 19:06:07 +0000410 }
Jeffrey Yasskind2af9622011-07-18 16:43:53 +0000411 case APValue::ComplexInt:
Richard Smithf6f003a2011-12-16 19:06:07 +0000412 Out << getComplexIntReal() << "+" << getComplexIntImag() << "i";
Richard Smithd62306a2011-11-10 06:34:14 +0000413 return;
Jeffrey Yasskind2af9622011-07-18 16:43:53 +0000414 case APValue::ComplexFloat:
Richard Smithf6f003a2011-12-16 19:06:07 +0000415 Out << GetApproxValue(getComplexFloatReal()) << "+"
416 << GetApproxValue(getComplexFloatImag()) << "i";
Richard Smithd62306a2011-11-10 06:34:14 +0000417 return;
Richard Smithf6f003a2011-12-16 19:06:07 +0000418 case APValue::LValue: {
Richard Smithf6f003a2011-12-16 19:06:07 +0000419 bool IsReference = Ty->isReferenceType();
420 QualType InnerTy
421 = IsReference ? Ty.getNonReferenceType() : Ty->getPointeeType();
Douglas Gregor0b7bc7f2013-01-29 01:26:43 +0000422 if (InnerTy.isNull())
423 InnerTy = Ty;
Richard Smithf6f003a2011-12-16 19:06:07 +0000424
Richard Smith128719c2018-09-13 22:47:33 +0000425 LValueBase Base = getLValueBase();
426 if (!Base) {
427 if (isNullPointer()) {
428 Out << (Ctx.getLangOpts().CPlusPlus11 ? "nullptr" : "0");
429 } else if (IsReference) {
430 Out << "*(" << InnerTy.stream(Ctx.getPrintingPolicy()) << "*)"
431 << getLValueOffset().getQuantity();
432 } else {
433 Out << "(" << Ty.stream(Ctx.getPrintingPolicy()) << ")"
434 << getLValueOffset().getQuantity();
435 }
436 return;
437 }
438
Richard Smithf6f003a2011-12-16 19:06:07 +0000439 if (!hasLValuePath()) {
440 // No lvalue path: just print the offset.
441 CharUnits O = getLValueOffset();
442 CharUnits S = Ctx.getTypeSizeInChars(InnerTy);
443 if (!O.isZero()) {
444 if (IsReference)
445 Out << "*(";
446 if (O % S) {
447 Out << "(char*)";
448 S = CharUnits::One();
449 }
450 Out << '&';
451 } else if (!IsReference)
452 Out << '&';
453
454 if (const ValueDecl *VD = Base.dyn_cast<const ValueDecl*>())
455 Out << *VD;
Richard Trieuddd01ce2014-06-09 22:53:25 +0000456 else {
457 assert(Base.get<const Expr *>() != nullptr &&
458 "Expecting non-null Expr");
Craig Topper36250ad2014-05-12 05:36:57 +0000459 Base.get<const Expr*>()->printPretty(Out, nullptr,
460 Ctx.getPrintingPolicy());
Richard Trieuddd01ce2014-06-09 22:53:25 +0000461 }
462
Richard Smithf6f003a2011-12-16 19:06:07 +0000463 if (!O.isZero()) {
464 Out << " + " << (O / S);
465 if (IsReference)
466 Out << ')';
467 }
468 return;
469 }
470
471 // We have an lvalue path. Print it out nicely.
472 if (!IsReference)
473 Out << '&';
474 else if (isLValueOnePastTheEnd())
475 Out << "*(&";
476
477 QualType ElemTy;
478 if (const ValueDecl *VD = Base.dyn_cast<const ValueDecl*>()) {
479 Out << *VD;
480 ElemTy = VD->getType();
481 } else {
482 const Expr *E = Base.get<const Expr*>();
Richard Trieuddd01ce2014-06-09 22:53:25 +0000483 assert(E != nullptr && "Expecting non-null Expr");
Craig Topper36250ad2014-05-12 05:36:57 +0000484 E->printPretty(Out, nullptr, Ctx.getPrintingPolicy());
Richard Smithf6f003a2011-12-16 19:06:07 +0000485 ElemTy = E->getType();
486 }
487
488 ArrayRef<LValuePathEntry> Path = getLValuePath();
Craig Topper36250ad2014-05-12 05:36:57 +0000489 const CXXRecordDecl *CastToBase = nullptr;
Richard Smithf6f003a2011-12-16 19:06:07 +0000490 for (unsigned I = 0, N = Path.size(); I != N; ++I) {
491 if (ElemTy->getAs<RecordType>()) {
492 // The lvalue refers to a class type, so the next path entry is a base
493 // or member.
494 const Decl *BaseOrMember =
495 BaseOrMemberType::getFromOpaqueValue(Path[I].BaseOrMember).getPointer();
496 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(BaseOrMember)) {
497 CastToBase = RD;
498 ElemTy = Ctx.getRecordType(RD);
499 } else {
500 const ValueDecl *VD = cast<ValueDecl>(BaseOrMember);
501 Out << ".";
502 if (CastToBase)
503 Out << *CastToBase << "::";
504 Out << *VD;
505 ElemTy = VD->getType();
506 }
507 } else {
508 // The lvalue must refer to an array.
509 Out << '[' << Path[I].ArrayIndex << ']';
510 ElemTy = Ctx.getAsArrayType(ElemTy)->getElementType();
511 }
512 }
513
514 // Handle formatting of one-past-the-end lvalues.
515 if (isLValueOnePastTheEnd()) {
516 // FIXME: If CastToBase is non-0, we should prefix the output with
517 // "(CastToBase*)".
518 Out << " + 1";
519 if (IsReference)
520 Out << ')';
521 }
Richard Smithd62306a2011-11-10 06:34:14 +0000522 return;
Richard Smithf6f003a2011-12-16 19:06:07 +0000523 }
524 case APValue::Array: {
525 const ArrayType *AT = Ctx.getAsArrayType(Ty);
526 QualType ElemTy = AT->getElementType();
Richard Smithd62306a2011-11-10 06:34:14 +0000527 Out << '{';
Richard Smithf6f003a2011-12-16 19:06:07 +0000528 if (unsigned N = getArrayInitializedElts()) {
529 getArrayInitializedElt(0).printPretty(Out, Ctx, ElemTy);
530 for (unsigned I = 1; I != N; ++I) {
Richard Smithd62306a2011-11-10 06:34:14 +0000531 Out << ", ";
Richard Smithf6f003a2011-12-16 19:06:07 +0000532 if (I == 10) {
533 // Avoid printing out the entire contents of large arrays.
534 Out << "...";
535 break;
536 }
537 getArrayInitializedElt(I).printPretty(Out, Ctx, ElemTy);
538 }
Richard Smithd62306a2011-11-10 06:34:14 +0000539 }
540 Out << '}';
541 return;
Richard Smithf6f003a2011-12-16 19:06:07 +0000542 }
543 case APValue::Struct: {
544 Out << '{';
545 const RecordDecl *RD = Ty->getAs<RecordType>()->getDecl();
546 bool First = true;
547 if (unsigned N = getStructNumBases()) {
548 const CXXRecordDecl *CD = cast<CXXRecordDecl>(RD);
549 CXXRecordDecl::base_class_const_iterator BI = CD->bases_begin();
550 for (unsigned I = 0; I != N; ++I, ++BI) {
551 assert(BI != CD->bases_end());
552 if (!First)
553 Out << ", ";
554 getStructBase(I).printPretty(Out, Ctx, BI->getType());
555 First = false;
556 }
557 }
Aaron Ballmane8a8bae2014-03-08 20:12:42 +0000558 for (const auto *FI : RD->fields()) {
Richard Smithf6f003a2011-12-16 19:06:07 +0000559 if (!First)
560 Out << ", ";
David Blaikie2d7c57e2012-04-30 02:36:29 +0000561 if (FI->isUnnamedBitfield()) continue;
562 getStructField(FI->getFieldIndex()).
563 printPretty(Out, Ctx, FI->getType());
Richard Smithf6f003a2011-12-16 19:06:07 +0000564 First = false;
565 }
566 Out << '}';
567 return;
568 }
Richard Smithd62306a2011-11-10 06:34:14 +0000569 case APValue::Union:
Richard Smithf6f003a2011-12-16 19:06:07 +0000570 Out << '{';
571 if (const FieldDecl *FD = getUnionField()) {
572 Out << "." << *FD << " = ";
573 getUnionValue().printPretty(Out, Ctx, FD->getType());
574 }
575 Out << '}';
Richard Smithd62306a2011-11-10 06:34:14 +0000576 return;
Richard Smith027bf112011-11-17 22:56:20 +0000577 case APValue::MemberPointer:
Richard Smithf6f003a2011-12-16 19:06:07 +0000578 // FIXME: This is not enough to unambiguously identify the member in a
579 // multiple-inheritance scenario.
580 if (const ValueDecl *VD = getMemberPointerDecl()) {
581 Out << '&' << *cast<CXXRecordDecl>(VD->getDeclContext()) << "::" << *VD;
582 return;
583 }
584 Out << "0";
Richard Smith027bf112011-11-17 22:56:20 +0000585 return;
Eli Friedmanfd5e54d2012-01-04 23:13:47 +0000586 case APValue::AddrLabelDiff:
587 Out << "&&" << getAddrLabelDiffLHS()->getLabel()->getName();
588 Out << " - ";
589 Out << "&&" << getAddrLabelDiffRHS()->getLabel()->getName();
590 return;
Jeffrey Yasskind2af9622011-07-18 16:43:53 +0000591 }
Richard Smithd62306a2011-11-10 06:34:14 +0000592 llvm_unreachable("Unknown APValue kind!");
Jeffrey Yasskind2af9622011-07-18 16:43:53 +0000593}
594
Richard Smithf6f003a2011-12-16 19:06:07 +0000595std::string APValue::getAsString(ASTContext &Ctx, QualType Ty) const {
596 std::string Result;
597 llvm::raw_string_ostream Out(Result);
598 printPretty(Out, Ctx, Ty);
Eli Friedman375f09f2011-12-16 22:12:23 +0000599 Out.flush();
Richard Smithf6f003a2011-12-16 19:06:07 +0000600 return Result;
Jeffrey Yasskind2af9622011-07-18 16:43:53 +0000601}
602
Richard Smithce40ad62011-11-12 22:28:03 +0000603const APValue::LValueBase APValue::getLValueBase() const {
Ken Dyck02990832010-01-15 12:37:54 +0000604 assert(isLValue() && "Invalid accessor");
Akira Hatanaka4e2698c2018-04-10 05:15:01 +0000605 return ((const LV*)(const void*)Data.buffer)->Base;
Richard Smith027bf112011-11-17 22:56:20 +0000606}
607
608bool APValue::isLValueOnePastTheEnd() const {
609 assert(isLValue() && "Invalid accessor");
Akira Hatanaka4e2698c2018-04-10 05:15:01 +0000610 return ((const LV*)(const void*)Data.buffer)->IsOnePastTheEnd;
Ken Dyck02990832010-01-15 12:37:54 +0000611}
612
Richard Smith0b0a0b62011-10-29 20:57:55 +0000613CharUnits &APValue::getLValueOffset() {
614 assert(isLValue() && "Invalid accessor");
Richard Smith7073a2d2014-01-10 00:40:45 +0000615 return ((LV*)(void*)Data.buffer)->Offset;
Ken Dyck02990832010-01-15 12:37:54 +0000616}
617
Richard Smith80815602011-11-07 05:07:52 +0000618bool APValue::hasLValuePath() const {
Ken Dyck02990832010-01-15 12:37:54 +0000619 assert(isLValue() && "Invalid accessor");
Richard Smith7073a2d2014-01-10 00:40:45 +0000620 return ((const LV*)(const char*)Data.buffer)->hasPath();
Richard Smith80815602011-11-07 05:07:52 +0000621}
622
623ArrayRef<APValue::LValuePathEntry> APValue::getLValuePath() const {
624 assert(isLValue() && hasLValuePath() && "Invalid accessor");
Richard Smith7073a2d2014-01-10 00:40:45 +0000625 const LV &LVal = *((const LV*)(const char*)Data.buffer);
Craig Topper5fc8fc22014-08-27 06:28:36 +0000626 return llvm::makeArrayRef(LVal.getPath(), LVal.PathLength);
Richard Smith80815602011-11-07 05:07:52 +0000627}
628
Richard Smithb228a862012-02-15 02:18:13 +0000629unsigned APValue::getLValueCallIndex() const {
630 assert(isLValue() && "Invalid accessor");
Akira Hatanaka4e2698c2018-04-10 05:15:01 +0000631 return ((const LV*)(const char*)Data.buffer)->Base.getCallIndex();
632}
633
634unsigned APValue::getLValueVersion() const {
635 assert(isLValue() && "Invalid accessor");
636 return ((const LV*)(const char*)Data.buffer)->Base.getVersion();
Richard Smithb228a862012-02-15 02:18:13 +0000637}
638
Yaxun Liu402804b2016-12-15 08:09:08 +0000639bool APValue::isNullPointer() const {
640 assert(isLValue() && "Invalid usage");
641 return ((const LV*)(const char*)Data.buffer)->IsNullPtr;
642}
643
Richard Smithb228a862012-02-15 02:18:13 +0000644void APValue::setLValue(LValueBase B, const CharUnits &O, NoLValuePath,
Akira Hatanaka4e2698c2018-04-10 05:15:01 +0000645 bool IsNullPtr) {
Richard Smith80815602011-11-07 05:07:52 +0000646 assert(isLValue() && "Invalid accessor");
Richard Smith7073a2d2014-01-10 00:40:45 +0000647 LV &LVal = *((LV*)(char*)Data.buffer);
Akira Hatanaka4e2698c2018-04-10 05:15:01 +0000648 LVal.Base = B;
649 LVal.IsOnePastTheEnd = false;
Richard Smith80815602011-11-07 05:07:52 +0000650 LVal.Offset = O;
Richard Smith027bf112011-11-17 22:56:20 +0000651 LVal.resizePath((unsigned)-1);
Yaxun Liu402804b2016-12-15 08:09:08 +0000652 LVal.IsNullPtr = IsNullPtr;
Richard Smith80815602011-11-07 05:07:52 +0000653}
654
Richard Smithce40ad62011-11-12 22:28:03 +0000655void APValue::setLValue(LValueBase B, const CharUnits &O,
Richard Smithb228a862012-02-15 02:18:13 +0000656 ArrayRef<LValuePathEntry> Path, bool IsOnePastTheEnd,
Akira Hatanaka4e2698c2018-04-10 05:15:01 +0000657 bool IsNullPtr) {
Richard Smith80815602011-11-07 05:07:52 +0000658 assert(isLValue() && "Invalid accessor");
Richard Smith7073a2d2014-01-10 00:40:45 +0000659 LV &LVal = *((LV*)(char*)Data.buffer);
Akira Hatanaka4e2698c2018-04-10 05:15:01 +0000660 LVal.Base = B;
661 LVal.IsOnePastTheEnd = IsOnePastTheEnd;
Richard Smith80815602011-11-07 05:07:52 +0000662 LVal.Offset = O;
Richard Smith027bf112011-11-17 22:56:20 +0000663 LVal.resizePath(Path.size());
Richard Smith80815602011-11-07 05:07:52 +0000664 memcpy(LVal.getPath(), Path.data(), Path.size() * sizeof(LValuePathEntry));
Yaxun Liu402804b2016-12-15 08:09:08 +0000665 LVal.IsNullPtr = IsNullPtr;
Ken Dyck02990832010-01-15 12:37:54 +0000666}
667
Richard Smith027bf112011-11-17 22:56:20 +0000668const ValueDecl *APValue::getMemberPointerDecl() const {
669 assert(isMemberPointer() && "Invalid accessor");
Richard Smith7073a2d2014-01-10 00:40:45 +0000670 const MemberPointerData &MPD =
671 *((const MemberPointerData *)(const char *)Data.buffer);
Richard Smith027bf112011-11-17 22:56:20 +0000672 return MPD.MemberAndIsDerivedMember.getPointer();
673}
674
675bool APValue::isMemberPointerToDerivedMember() const {
676 assert(isMemberPointer() && "Invalid accessor");
Richard Smith7073a2d2014-01-10 00:40:45 +0000677 const MemberPointerData &MPD =
678 *((const MemberPointerData *)(const char *)Data.buffer);
Richard Smith027bf112011-11-17 22:56:20 +0000679 return MPD.MemberAndIsDerivedMember.getInt();
680}
681
682ArrayRef<const CXXRecordDecl*> APValue::getMemberPointerPath() const {
683 assert(isMemberPointer() && "Invalid accessor");
Richard Smith7073a2d2014-01-10 00:40:45 +0000684 const MemberPointerData &MPD =
685 *((const MemberPointerData *)(const char *)Data.buffer);
Craig Topper5fc8fc22014-08-27 06:28:36 +0000686 return llvm::makeArrayRef(MPD.getPath(), MPD.PathLength);
Richard Smith027bf112011-11-17 22:56:20 +0000687}
688
Ken Dyck02990832010-01-15 12:37:54 +0000689void APValue::MakeLValue() {
690 assert(isUninit() && "Bad state change");
Benjamin Kramera939c232014-03-15 18:54:13 +0000691 static_assert(sizeof(LV) <= DataSize, "LV too big");
Richard Smith7073a2d2014-01-10 00:40:45 +0000692 new ((void*)(char*)Data.buffer) LV();
Ken Dyck02990832010-01-15 12:37:54 +0000693 Kind = LValue;
694}
Richard Smithf3e9e432011-11-07 09:22:26 +0000695
696void APValue::MakeArray(unsigned InitElts, unsigned Size) {
697 assert(isUninit() && "Bad state change");
Richard Smith7073a2d2014-01-10 00:40:45 +0000698 new ((void*)(char*)Data.buffer) Arr(InitElts, Size);
Richard Smithf3e9e432011-11-07 09:22:26 +0000699 Kind = Array;
700}
Richard Smith027bf112011-11-17 22:56:20 +0000701
702void APValue::MakeMemberPointer(const ValueDecl *Member, bool IsDerivedMember,
703 ArrayRef<const CXXRecordDecl*> Path) {
704 assert(isUninit() && "Bad state change");
Richard Smith7073a2d2014-01-10 00:40:45 +0000705 MemberPointerData *MPD = new ((void*)(char*)Data.buffer) MemberPointerData;
Richard Smith027bf112011-11-17 22:56:20 +0000706 Kind = MemberPointer;
707 MPD->MemberAndIsDerivedMember.setPointer(Member);
708 MPD->MemberAndIsDerivedMember.setInt(IsDerivedMember);
709 MPD->resizePath(Path.size());
710 memcpy(MPD->getPath(), Path.data(), Path.size()*sizeof(const CXXRecordDecl*));
711}