blob: c45b52a65a4dbf50e00518455ecf3fbc7f557109 [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: {
419 LValueBase Base = getLValueBase();
420 if (!Base) {
421 Out << "0";
422 return;
Richard Smithf3e9e432011-11-07 09:22:26 +0000423 }
Richard Smithf6f003a2011-12-16 19:06:07 +0000424
425 bool IsReference = Ty->isReferenceType();
426 QualType InnerTy
427 = IsReference ? Ty.getNonReferenceType() : Ty->getPointeeType();
Douglas Gregor0b7bc7f2013-01-29 01:26:43 +0000428 if (InnerTy.isNull())
429 InnerTy = Ty;
Richard Smithf6f003a2011-12-16 19:06:07 +0000430
431 if (!hasLValuePath()) {
432 // No lvalue path: just print the offset.
433 CharUnits O = getLValueOffset();
434 CharUnits S = Ctx.getTypeSizeInChars(InnerTy);
435 if (!O.isZero()) {
436 if (IsReference)
437 Out << "*(";
438 if (O % S) {
439 Out << "(char*)";
440 S = CharUnits::One();
441 }
442 Out << '&';
443 } else if (!IsReference)
444 Out << '&';
445
446 if (const ValueDecl *VD = Base.dyn_cast<const ValueDecl*>())
447 Out << *VD;
Richard Trieuddd01ce2014-06-09 22:53:25 +0000448 else {
449 assert(Base.get<const Expr *>() != nullptr &&
450 "Expecting non-null Expr");
Craig Topper36250ad2014-05-12 05:36:57 +0000451 Base.get<const Expr*>()->printPretty(Out, nullptr,
452 Ctx.getPrintingPolicy());
Richard Trieuddd01ce2014-06-09 22:53:25 +0000453 }
454
Richard Smithf6f003a2011-12-16 19:06:07 +0000455 if (!O.isZero()) {
456 Out << " + " << (O / S);
457 if (IsReference)
458 Out << ')';
459 }
460 return;
461 }
462
463 // We have an lvalue path. Print it out nicely.
464 if (!IsReference)
465 Out << '&';
466 else if (isLValueOnePastTheEnd())
467 Out << "*(&";
468
469 QualType ElemTy;
470 if (const ValueDecl *VD = Base.dyn_cast<const ValueDecl*>()) {
471 Out << *VD;
472 ElemTy = VD->getType();
473 } else {
474 const Expr *E = Base.get<const Expr*>();
Richard Trieuddd01ce2014-06-09 22:53:25 +0000475 assert(E != nullptr && "Expecting non-null Expr");
Craig Topper36250ad2014-05-12 05:36:57 +0000476 E->printPretty(Out, nullptr, Ctx.getPrintingPolicy());
Richard Smithf6f003a2011-12-16 19:06:07 +0000477 ElemTy = E->getType();
478 }
479
480 ArrayRef<LValuePathEntry> Path = getLValuePath();
Craig Topper36250ad2014-05-12 05:36:57 +0000481 const CXXRecordDecl *CastToBase = nullptr;
Richard Smithf6f003a2011-12-16 19:06:07 +0000482 for (unsigned I = 0, N = Path.size(); I != N; ++I) {
483 if (ElemTy->getAs<RecordType>()) {
484 // The lvalue refers to a class type, so the next path entry is a base
485 // or member.
486 const Decl *BaseOrMember =
487 BaseOrMemberType::getFromOpaqueValue(Path[I].BaseOrMember).getPointer();
488 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(BaseOrMember)) {
489 CastToBase = RD;
490 ElemTy = Ctx.getRecordType(RD);
491 } else {
492 const ValueDecl *VD = cast<ValueDecl>(BaseOrMember);
493 Out << ".";
494 if (CastToBase)
495 Out << *CastToBase << "::";
496 Out << *VD;
497 ElemTy = VD->getType();
498 }
499 } else {
500 // The lvalue must refer to an array.
501 Out << '[' << Path[I].ArrayIndex << ']';
502 ElemTy = Ctx.getAsArrayType(ElemTy)->getElementType();
503 }
504 }
505
506 // Handle formatting of one-past-the-end lvalues.
507 if (isLValueOnePastTheEnd()) {
508 // FIXME: If CastToBase is non-0, we should prefix the output with
509 // "(CastToBase*)".
510 Out << " + 1";
511 if (IsReference)
512 Out << ')';
513 }
Richard Smithd62306a2011-11-10 06:34:14 +0000514 return;
Richard Smithf6f003a2011-12-16 19:06:07 +0000515 }
516 case APValue::Array: {
517 const ArrayType *AT = Ctx.getAsArrayType(Ty);
518 QualType ElemTy = AT->getElementType();
Richard Smithd62306a2011-11-10 06:34:14 +0000519 Out << '{';
Richard Smithf6f003a2011-12-16 19:06:07 +0000520 if (unsigned N = getArrayInitializedElts()) {
521 getArrayInitializedElt(0).printPretty(Out, Ctx, ElemTy);
522 for (unsigned I = 1; I != N; ++I) {
Richard Smithd62306a2011-11-10 06:34:14 +0000523 Out << ", ";
Richard Smithf6f003a2011-12-16 19:06:07 +0000524 if (I == 10) {
525 // Avoid printing out the entire contents of large arrays.
526 Out << "...";
527 break;
528 }
529 getArrayInitializedElt(I).printPretty(Out, Ctx, ElemTy);
530 }
Richard Smithd62306a2011-11-10 06:34:14 +0000531 }
532 Out << '}';
533 return;
Richard Smithf6f003a2011-12-16 19:06:07 +0000534 }
535 case APValue::Struct: {
536 Out << '{';
537 const RecordDecl *RD = Ty->getAs<RecordType>()->getDecl();
538 bool First = true;
539 if (unsigned N = getStructNumBases()) {
540 const CXXRecordDecl *CD = cast<CXXRecordDecl>(RD);
541 CXXRecordDecl::base_class_const_iterator BI = CD->bases_begin();
542 for (unsigned I = 0; I != N; ++I, ++BI) {
543 assert(BI != CD->bases_end());
544 if (!First)
545 Out << ", ";
546 getStructBase(I).printPretty(Out, Ctx, BI->getType());
547 First = false;
548 }
549 }
Aaron Ballmane8a8bae2014-03-08 20:12:42 +0000550 for (const auto *FI : RD->fields()) {
Richard Smithf6f003a2011-12-16 19:06:07 +0000551 if (!First)
552 Out << ", ";
David Blaikie2d7c57e2012-04-30 02:36:29 +0000553 if (FI->isUnnamedBitfield()) continue;
554 getStructField(FI->getFieldIndex()).
555 printPretty(Out, Ctx, FI->getType());
Richard Smithf6f003a2011-12-16 19:06:07 +0000556 First = false;
557 }
558 Out << '}';
559 return;
560 }
Richard Smithd62306a2011-11-10 06:34:14 +0000561 case APValue::Union:
Richard Smithf6f003a2011-12-16 19:06:07 +0000562 Out << '{';
563 if (const FieldDecl *FD = getUnionField()) {
564 Out << "." << *FD << " = ";
565 getUnionValue().printPretty(Out, Ctx, FD->getType());
566 }
567 Out << '}';
Richard Smithd62306a2011-11-10 06:34:14 +0000568 return;
Richard Smith027bf112011-11-17 22:56:20 +0000569 case APValue::MemberPointer:
Richard Smithf6f003a2011-12-16 19:06:07 +0000570 // FIXME: This is not enough to unambiguously identify the member in a
571 // multiple-inheritance scenario.
572 if (const ValueDecl *VD = getMemberPointerDecl()) {
573 Out << '&' << *cast<CXXRecordDecl>(VD->getDeclContext()) << "::" << *VD;
574 return;
575 }
576 Out << "0";
Richard Smith027bf112011-11-17 22:56:20 +0000577 return;
Eli Friedmanfd5e54d2012-01-04 23:13:47 +0000578 case APValue::AddrLabelDiff:
579 Out << "&&" << getAddrLabelDiffLHS()->getLabel()->getName();
580 Out << " - ";
581 Out << "&&" << getAddrLabelDiffRHS()->getLabel()->getName();
582 return;
Jeffrey Yasskind2af9622011-07-18 16:43:53 +0000583 }
Richard Smithd62306a2011-11-10 06:34:14 +0000584 llvm_unreachable("Unknown APValue kind!");
Jeffrey Yasskind2af9622011-07-18 16:43:53 +0000585}
586
Richard Smithf6f003a2011-12-16 19:06:07 +0000587std::string APValue::getAsString(ASTContext &Ctx, QualType Ty) const {
588 std::string Result;
589 llvm::raw_string_ostream Out(Result);
590 printPretty(Out, Ctx, Ty);
Eli Friedman375f09f2011-12-16 22:12:23 +0000591 Out.flush();
Richard Smithf6f003a2011-12-16 19:06:07 +0000592 return Result;
Jeffrey Yasskind2af9622011-07-18 16:43:53 +0000593}
594
Richard Smithce40ad62011-11-12 22:28:03 +0000595const APValue::LValueBase APValue::getLValueBase() const {
Ken Dyck02990832010-01-15 12:37:54 +0000596 assert(isLValue() && "Invalid accessor");
Akira Hatanaka4e2698c2018-04-10 05:15:01 +0000597 return ((const LV*)(const void*)Data.buffer)->Base;
Richard Smith027bf112011-11-17 22:56:20 +0000598}
599
600bool APValue::isLValueOnePastTheEnd() const {
601 assert(isLValue() && "Invalid accessor");
Akira Hatanaka4e2698c2018-04-10 05:15:01 +0000602 return ((const LV*)(const void*)Data.buffer)->IsOnePastTheEnd;
Ken Dyck02990832010-01-15 12:37:54 +0000603}
604
Richard Smith0b0a0b62011-10-29 20:57:55 +0000605CharUnits &APValue::getLValueOffset() {
606 assert(isLValue() && "Invalid accessor");
Richard Smith7073a2d2014-01-10 00:40:45 +0000607 return ((LV*)(void*)Data.buffer)->Offset;
Ken Dyck02990832010-01-15 12:37:54 +0000608}
609
Richard Smith80815602011-11-07 05:07:52 +0000610bool APValue::hasLValuePath() const {
Ken Dyck02990832010-01-15 12:37:54 +0000611 assert(isLValue() && "Invalid accessor");
Richard Smith7073a2d2014-01-10 00:40:45 +0000612 return ((const LV*)(const char*)Data.buffer)->hasPath();
Richard Smith80815602011-11-07 05:07:52 +0000613}
614
615ArrayRef<APValue::LValuePathEntry> APValue::getLValuePath() const {
616 assert(isLValue() && hasLValuePath() && "Invalid accessor");
Richard Smith7073a2d2014-01-10 00:40:45 +0000617 const LV &LVal = *((const LV*)(const char*)Data.buffer);
Craig Topper5fc8fc22014-08-27 06:28:36 +0000618 return llvm::makeArrayRef(LVal.getPath(), LVal.PathLength);
Richard Smith80815602011-11-07 05:07:52 +0000619}
620
Richard Smithb228a862012-02-15 02:18:13 +0000621unsigned APValue::getLValueCallIndex() const {
622 assert(isLValue() && "Invalid accessor");
Akira Hatanaka4e2698c2018-04-10 05:15:01 +0000623 return ((const LV*)(const char*)Data.buffer)->Base.getCallIndex();
624}
625
626unsigned APValue::getLValueVersion() const {
627 assert(isLValue() && "Invalid accessor");
628 return ((const LV*)(const char*)Data.buffer)->Base.getVersion();
Richard Smithb228a862012-02-15 02:18:13 +0000629}
630
Yaxun Liu402804b2016-12-15 08:09:08 +0000631bool APValue::isNullPointer() const {
632 assert(isLValue() && "Invalid usage");
633 return ((const LV*)(const char*)Data.buffer)->IsNullPtr;
634}
635
Richard Smithb228a862012-02-15 02:18:13 +0000636void APValue::setLValue(LValueBase B, const CharUnits &O, NoLValuePath,
Akira Hatanaka4e2698c2018-04-10 05:15:01 +0000637 bool IsNullPtr) {
Richard Smith80815602011-11-07 05:07:52 +0000638 assert(isLValue() && "Invalid accessor");
Richard Smith7073a2d2014-01-10 00:40:45 +0000639 LV &LVal = *((LV*)(char*)Data.buffer);
Akira Hatanaka4e2698c2018-04-10 05:15:01 +0000640 LVal.Base = B;
641 LVal.IsOnePastTheEnd = false;
Richard Smith80815602011-11-07 05:07:52 +0000642 LVal.Offset = O;
Richard Smith027bf112011-11-17 22:56:20 +0000643 LVal.resizePath((unsigned)-1);
Yaxun Liu402804b2016-12-15 08:09:08 +0000644 LVal.IsNullPtr = IsNullPtr;
Richard Smith80815602011-11-07 05:07:52 +0000645}
646
Richard Smithce40ad62011-11-12 22:28:03 +0000647void APValue::setLValue(LValueBase B, const CharUnits &O,
Richard Smithb228a862012-02-15 02:18:13 +0000648 ArrayRef<LValuePathEntry> Path, bool IsOnePastTheEnd,
Akira Hatanaka4e2698c2018-04-10 05:15:01 +0000649 bool IsNullPtr) {
Richard Smith80815602011-11-07 05:07:52 +0000650 assert(isLValue() && "Invalid accessor");
Richard Smith7073a2d2014-01-10 00:40:45 +0000651 LV &LVal = *((LV*)(char*)Data.buffer);
Akira Hatanaka4e2698c2018-04-10 05:15:01 +0000652 LVal.Base = B;
653 LVal.IsOnePastTheEnd = IsOnePastTheEnd;
Richard Smith80815602011-11-07 05:07:52 +0000654 LVal.Offset = O;
Richard Smith027bf112011-11-17 22:56:20 +0000655 LVal.resizePath(Path.size());
Richard Smith80815602011-11-07 05:07:52 +0000656 memcpy(LVal.getPath(), Path.data(), Path.size() * sizeof(LValuePathEntry));
Yaxun Liu402804b2016-12-15 08:09:08 +0000657 LVal.IsNullPtr = IsNullPtr;
Ken Dyck02990832010-01-15 12:37:54 +0000658}
659
Richard Smith027bf112011-11-17 22:56:20 +0000660const ValueDecl *APValue::getMemberPointerDecl() const {
661 assert(isMemberPointer() && "Invalid accessor");
Richard Smith7073a2d2014-01-10 00:40:45 +0000662 const MemberPointerData &MPD =
663 *((const MemberPointerData *)(const char *)Data.buffer);
Richard Smith027bf112011-11-17 22:56:20 +0000664 return MPD.MemberAndIsDerivedMember.getPointer();
665}
666
667bool APValue::isMemberPointerToDerivedMember() const {
668 assert(isMemberPointer() && "Invalid accessor");
Richard Smith7073a2d2014-01-10 00:40:45 +0000669 const MemberPointerData &MPD =
670 *((const MemberPointerData *)(const char *)Data.buffer);
Richard Smith027bf112011-11-17 22:56:20 +0000671 return MPD.MemberAndIsDerivedMember.getInt();
672}
673
674ArrayRef<const CXXRecordDecl*> APValue::getMemberPointerPath() const {
675 assert(isMemberPointer() && "Invalid accessor");
Richard Smith7073a2d2014-01-10 00:40:45 +0000676 const MemberPointerData &MPD =
677 *((const MemberPointerData *)(const char *)Data.buffer);
Craig Topper5fc8fc22014-08-27 06:28:36 +0000678 return llvm::makeArrayRef(MPD.getPath(), MPD.PathLength);
Richard Smith027bf112011-11-17 22:56:20 +0000679}
680
Ken Dyck02990832010-01-15 12:37:54 +0000681void APValue::MakeLValue() {
682 assert(isUninit() && "Bad state change");
Benjamin Kramera939c232014-03-15 18:54:13 +0000683 static_assert(sizeof(LV) <= DataSize, "LV too big");
Richard Smith7073a2d2014-01-10 00:40:45 +0000684 new ((void*)(char*)Data.buffer) LV();
Ken Dyck02990832010-01-15 12:37:54 +0000685 Kind = LValue;
686}
Richard Smithf3e9e432011-11-07 09:22:26 +0000687
688void APValue::MakeArray(unsigned InitElts, unsigned Size) {
689 assert(isUninit() && "Bad state change");
Richard Smith7073a2d2014-01-10 00:40:45 +0000690 new ((void*)(char*)Data.buffer) Arr(InitElts, Size);
Richard Smithf3e9e432011-11-07 09:22:26 +0000691 Kind = Array;
692}
Richard Smith027bf112011-11-17 22:56:20 +0000693
694void APValue::MakeMemberPointer(const ValueDecl *Member, bool IsDerivedMember,
695 ArrayRef<const CXXRecordDecl*> Path) {
696 assert(isUninit() && "Bad state change");
Richard Smith7073a2d2014-01-10 00:40:45 +0000697 MemberPointerData *MPD = new ((void*)(char*)Data.buffer) MemberPointerData;
Richard Smith027bf112011-11-17 22:56:20 +0000698 Kind = MemberPointer;
699 MPD->MemberAndIsDerivedMember.setPointer(Member);
700 MPD->MemberAndIsDerivedMember.setInt(IsDerivedMember);
701 MPD->resizePath(Path.size());
702 memcpy(MPD->getPath(), Path.data(), Path.size()*sizeof(const CXXRecordDecl*));
703}