blob: c5db3cd582d050936ce745207595c82ceca39a5e [file] [log] [blame]
Chris Lattner981f33b2008-11-16 07:46:48 +00001//===--- APValue.cpp - Union class for APFloat/APSInt/Complex -------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Chris Lattner981f33b2008-11-16 07:46:48 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This file implements the APValue class.
10//
11//===----------------------------------------------------------------------===//
12
13#include "clang/AST/APValue.h"
Richard Smithf6f003a2011-12-16 19:06:07 +000014#include "clang/AST/ASTContext.h"
Ken Dyck02990832010-01-15 12:37:54 +000015#include "clang/AST/CharUnits.h"
Richard Smithf6f003a2011-12-16 19:06:07 +000016#include "clang/AST/DeclCXX.h"
17#include "clang/AST/Expr.h"
18#include "clang/AST/Type.h"
David Blaikie76bd3c82011-09-23 05:35:21 +000019#include "llvm/Support/ErrorHandling.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000020#include "llvm/Support/raw_ostream.h"
Chris Lattner981f33b2008-11-16 07:46:48 +000021using namespace clang;
22
Richard Smithee0ce3022019-05-17 07:06:46 +000023/// The identity of a type_info object depends on the canonical unqualified
24/// type only.
25TypeInfoLValue::TypeInfoLValue(const Type *T)
26 : T(T->getCanonicalTypeUnqualified().getTypePtr()) {}
27
28void TypeInfoLValue::print(llvm::raw_ostream &Out,
29 const PrintingPolicy &Policy) const {
30 Out << "typeid(";
31 QualType(getType(), 0).print(Out, Policy);
32 Out << ")";
33}
34
35static_assert(
36 1 << llvm::PointerLikeTypeTraits<TypeInfoLValue>::NumLowBitsAvailable <=
Richard Smith77483762019-05-17 07:28:41 +000037 alignof(Type),
Richard Smithee0ce3022019-05-17 07:06:46 +000038 "Type is insufficiently aligned");
39
40APValue::LValueBase::LValueBase(const ValueDecl *P, unsigned I, unsigned V)
41 : Ptr(P), Local{I, V} {}
42APValue::LValueBase::LValueBase(const Expr *P, unsigned I, unsigned V)
43 : Ptr(P), Local{I, V} {}
44
45APValue::LValueBase APValue::LValueBase::getTypeInfo(TypeInfoLValue LV,
46 QualType TypeInfo) {
47 LValueBase Base;
48 Base.Ptr = LV;
49 Base.TypeInfoType = TypeInfo.getAsOpaquePtr();
50 return Base;
51}
52
53unsigned APValue::LValueBase::getCallIndex() const {
54 return is<TypeInfoLValue>() ? 0 : Local.CallIndex;
55}
56
57unsigned APValue::LValueBase::getVersion() const {
58 return is<TypeInfoLValue>() ? 0 : Local.Version;
59}
60
61QualType APValue::LValueBase::getTypeInfoType() const {
62 assert(is<TypeInfoLValue>() && "not a type_info lvalue");
63 return QualType::getFromOpaquePtr(TypeInfoType);
64}
65
66namespace clang {
67bool operator==(const APValue::LValueBase &LHS,
68 const APValue::LValueBase &RHS) {
69 if (LHS.Ptr != RHS.Ptr)
70 return false;
71 if (LHS.is<TypeInfoLValue>())
72 return true;
73 return LHS.Local.CallIndex == RHS.Local.CallIndex &&
74 LHS.Local.Version == RHS.Local.Version;
75}
76}
77
Ken Dyck02990832010-01-15 12:37:54 +000078namespace {
Richard Smith80815602011-11-07 05:07:52 +000079 struct LVBase {
Akira Hatanaka4e2698c2018-04-10 05:15:01 +000080 APValue::LValueBase Base;
Ken Dyck02990832010-01-15 12:37:54 +000081 CharUnits Offset;
Richard Smith80815602011-11-07 05:07:52 +000082 unsigned PathLength;
Akira Hatanaka4e2698c2018-04-10 05:15:01 +000083 bool IsNullPtr : 1;
84 bool IsOnePastTheEnd : 1;
Ken Dyck02990832010-01-15 12:37:54 +000085 };
86}
87
Akira Hatanaka4e2698c2018-04-10 05:15:01 +000088void *APValue::LValueBase::getOpaqueValue() const {
89 return Ptr.getOpaqueValue();
90}
91
92bool APValue::LValueBase::isNull() const {
93 return Ptr.isNull();
94}
95
96APValue::LValueBase::operator bool () const {
97 return static_cast<bool>(Ptr);
98}
99
100clang::APValue::LValueBase
101llvm::DenseMapInfo<clang::APValue::LValueBase>::getEmptyKey() {
102 return clang::APValue::LValueBase(
Richard Smithee0ce3022019-05-17 07:06:46 +0000103 DenseMapInfo<const ValueDecl*>::getEmptyKey());
Akira Hatanaka4e2698c2018-04-10 05:15:01 +0000104}
105
106clang::APValue::LValueBase
107llvm::DenseMapInfo<clang::APValue::LValueBase>::getTombstoneKey() {
108 return clang::APValue::LValueBase(
Richard Smithee0ce3022019-05-17 07:06:46 +0000109 DenseMapInfo<const ValueDecl*>::getTombstoneKey());
Akira Hatanaka4e2698c2018-04-10 05:15:01 +0000110}
111
Richard Smithd3d6f4f2019-05-12 08:57:59 +0000112namespace clang {
113llvm::hash_code hash_value(const APValue::LValueBase &Base) {
Richard Smithee0ce3022019-05-17 07:06:46 +0000114 if (Base.is<TypeInfoLValue>())
115 return llvm::hash_value(Base.getOpaqueValue());
Richard Smithd3d6f4f2019-05-12 08:57:59 +0000116 return llvm::hash_combine(Base.getOpaqueValue(), Base.getCallIndex(),
117 Base.getVersion());
118}
119}
120
Akira Hatanaka4e2698c2018-04-10 05:15:01 +0000121unsigned llvm::DenseMapInfo<clang::APValue::LValueBase>::getHashValue(
122 const clang::APValue::LValueBase &Base) {
Richard Smithd3d6f4f2019-05-12 08:57:59 +0000123 return hash_value(Base);
Akira Hatanaka4e2698c2018-04-10 05:15:01 +0000124}
125
126bool llvm::DenseMapInfo<clang::APValue::LValueBase>::isEqual(
127 const clang::APValue::LValueBase &LHS,
128 const clang::APValue::LValueBase &RHS) {
129 return LHS == RHS;
130}
131
Richard Smith80815602011-11-07 05:07:52 +0000132struct APValue::LV : LVBase {
133 static const unsigned InlinePathSpace =
Richard Smith7073a2d2014-01-10 00:40:45 +0000134 (DataSize - sizeof(LVBase)) / sizeof(LValuePathEntry);
Richard Smith80815602011-11-07 05:07:52 +0000135
136 /// Path - The sequence of base classes, fields and array indices to follow to
137 /// walk from Base to the subobject. When performing GCC-style folding, there
138 /// may not be such a path.
139 union {
140 LValuePathEntry Path[InlinePathSpace];
141 LValuePathEntry *PathPtr;
142 };
143
144 LV() { PathLength = (unsigned)-1; }
Richard Smith027bf112011-11-17 22:56:20 +0000145 ~LV() { resizePath(0); }
Richard Smith80815602011-11-07 05:07:52 +0000146
Richard Smith027bf112011-11-17 22:56:20 +0000147 void resizePath(unsigned Length) {
148 if (Length == PathLength)
149 return;
150 if (hasPathPtr())
151 delete [] PathPtr;
152 PathLength = Length;
153 if (hasPathPtr())
154 PathPtr = new LValuePathEntry[Length];
Richard Smith80815602011-11-07 05:07:52 +0000155 }
156
157 bool hasPath() const { return PathLength != (unsigned)-1; }
158 bool hasPathPtr() const { return hasPath() && PathLength > InlinePathSpace; }
159
160 LValuePathEntry *getPath() { return hasPathPtr() ? PathPtr : Path; }
Richard Smithbcb4eb22011-11-07 07:31:09 +0000161 const LValuePathEntry *getPath() const {
162 return hasPathPtr() ? PathPtr : Path;
163 }
Richard Smith80815602011-11-07 05:07:52 +0000164};
165
Richard Smith027bf112011-11-17 22:56:20 +0000166namespace {
167 struct MemberPointerBase {
168 llvm::PointerIntPair<const ValueDecl*, 1, bool> MemberAndIsDerivedMember;
169 unsigned PathLength;
170 };
171}
172
173struct APValue::MemberPointerData : MemberPointerBase {
174 static const unsigned InlinePathSpace =
Richard Smith7073a2d2014-01-10 00:40:45 +0000175 (DataSize - sizeof(MemberPointerBase)) / sizeof(const CXXRecordDecl*);
Richard Smith027bf112011-11-17 22:56:20 +0000176 typedef const CXXRecordDecl *PathElem;
177 union {
178 PathElem Path[InlinePathSpace];
179 PathElem *PathPtr;
180 };
181
182 MemberPointerData() { PathLength = 0; }
183 ~MemberPointerData() { resizePath(0); }
184
185 void resizePath(unsigned Length) {
186 if (Length == PathLength)
187 return;
188 if (hasPathPtr())
189 delete [] PathPtr;
190 PathLength = Length;
191 if (hasPathPtr())
192 PathPtr = new PathElem[Length];
193 }
194
195 bool hasPathPtr() const { return PathLength > InlinePathSpace; }
196
197 PathElem *getPath() { return hasPathPtr() ? PathPtr : Path; }
198 const PathElem *getPath() const {
199 return hasPathPtr() ? PathPtr : Path;
200 }
201};
202
Richard Smithf3e9e432011-11-07 09:22:26 +0000203// FIXME: Reduce the malloc traffic here.
204
205APValue::Arr::Arr(unsigned NumElts, unsigned Size) :
206 Elts(new APValue[NumElts + (NumElts != Size ? 1 : 0)]),
207 NumElts(NumElts), ArrSize(Size) {}
208APValue::Arr::~Arr() { delete [] Elts; }
209
Richard Smithd62306a2011-11-10 06:34:14 +0000210APValue::StructData::StructData(unsigned NumBases, unsigned NumFields) :
211 Elts(new APValue[NumBases+NumFields]),
212 NumBases(NumBases), NumFields(NumFields) {}
213APValue::StructData::~StructData() {
214 delete [] Elts;
215}
216
Craig Topper36250ad2014-05-12 05:36:57 +0000217APValue::UnionData::UnionData() : Field(nullptr), Value(new APValue) {}
Richard Smithd62306a2011-11-10 06:34:14 +0000218APValue::UnionData::~UnionData () {
219 delete Value;
220}
221
Richard Smith4e9e5232012-03-10 00:28:11 +0000222APValue::APValue(const APValue &RHS) : Kind(Uninitialized) {
223 switch (RHS.getKind()) {
224 case Uninitialized:
225 break;
226 case Int:
227 MakeInt();
Chris Lattner981f33b2008-11-16 07:46:48 +0000228 setInt(RHS.getInt());
Richard Smith4e9e5232012-03-10 00:28:11 +0000229 break;
230 case Float:
231 MakeFloat();
Chris Lattner981f33b2008-11-16 07:46:48 +0000232 setFloat(RHS.getFloat());
Richard Smith4e9e5232012-03-10 00:28:11 +0000233 break;
Leonard Chan86285d22019-01-16 18:53:05 +0000234 case FixedPoint: {
235 APFixedPoint FXCopy = RHS.getFixedPoint();
236 MakeFixedPoint(std::move(FXCopy));
237 break;
238 }
Richard Smith4e9e5232012-03-10 00:28:11 +0000239 case Vector:
240 MakeVector();
Richard Smith7073a2d2014-01-10 00:40:45 +0000241 setVector(((const Vec *)(const char *)RHS.Data.buffer)->Elts,
Dan Gohman145f3f12010-04-19 16:39:44 +0000242 RHS.getVectorLength());
Richard Smith4e9e5232012-03-10 00:28:11 +0000243 break;
244 case ComplexInt:
245 MakeComplexInt();
Chris Lattner981f33b2008-11-16 07:46:48 +0000246 setComplexInt(RHS.getComplexIntReal(), RHS.getComplexIntImag());
Richard Smith4e9e5232012-03-10 00:28:11 +0000247 break;
248 case ComplexFloat:
249 MakeComplexFloat();
Chris Lattner981f33b2008-11-16 07:46:48 +0000250 setComplexFloat(RHS.getComplexFloatReal(), RHS.getComplexFloatImag());
Richard Smith4e9e5232012-03-10 00:28:11 +0000251 break;
252 case LValue:
253 MakeLValue();
Richard Smith80815602011-11-07 05:07:52 +0000254 if (RHS.hasLValuePath())
Richard Smith027bf112011-11-17 22:56:20 +0000255 setLValue(RHS.getLValueBase(), RHS.getLValueOffset(), RHS.getLValuePath(),
Akira Hatanaka4e2698c2018-04-10 05:15:01 +0000256 RHS.isLValueOnePastTheEnd(), RHS.isNullPointer());
Richard Smith80815602011-11-07 05:07:52 +0000257 else
Richard Smithb228a862012-02-15 02:18:13 +0000258 setLValue(RHS.getLValueBase(), RHS.getLValueOffset(), NoLValuePath(),
Akira Hatanaka4e2698c2018-04-10 05:15:01 +0000259 RHS.isNullPointer());
Richard Smith4e9e5232012-03-10 00:28:11 +0000260 break;
261 case Array:
262 MakeArray(RHS.getArrayInitializedElts(), RHS.getArraySize());
Richard Smithf3e9e432011-11-07 09:22:26 +0000263 for (unsigned I = 0, N = RHS.getArrayInitializedElts(); I != N; ++I)
264 getArrayInitializedElt(I) = RHS.getArrayInitializedElt(I);
265 if (RHS.hasArrayFiller())
266 getArrayFiller() = RHS.getArrayFiller();
Richard Smith4e9e5232012-03-10 00:28:11 +0000267 break;
268 case Struct:
269 MakeStruct(RHS.getStructNumBases(), RHS.getStructNumFields());
Richard Smithd62306a2011-11-10 06:34:14 +0000270 for (unsigned I = 0, N = RHS.getStructNumBases(); I != N; ++I)
271 getStructBase(I) = RHS.getStructBase(I);
272 for (unsigned I = 0, N = RHS.getStructNumFields(); I != N; ++I)
273 getStructField(I) = RHS.getStructField(I);
Richard Smith4e9e5232012-03-10 00:28:11 +0000274 break;
275 case Union:
276 MakeUnion();
Richard Smithd62306a2011-11-10 06:34:14 +0000277 setUnion(RHS.getUnionField(), RHS.getUnionValue());
Richard Smith4e9e5232012-03-10 00:28:11 +0000278 break;
279 case MemberPointer:
280 MakeMemberPointer(RHS.getMemberPointerDecl(),
281 RHS.isMemberPointerToDerivedMember(),
282 RHS.getMemberPointerPath());
283 break;
284 case AddrLabelDiff:
285 MakeAddrLabelDiff();
Eli Friedmanfd5e54d2012-01-04 23:13:47 +0000286 setAddrLabelDiff(RHS.getAddrLabelDiffLHS(), RHS.getAddrLabelDiffRHS());
Richard Smith4e9e5232012-03-10 00:28:11 +0000287 break;
Eli Friedmanfd5e54d2012-01-04 23:13:47 +0000288 }
Chris Lattner981f33b2008-11-16 07:46:48 +0000289}
290
Daniel Dunbarb7431572012-03-08 20:28:55 +0000291void APValue::DestroyDataAndMakeUninit() {
Chris Lattner981f33b2008-11-16 07:46:48 +0000292 if (Kind == Int)
Richard Smith7073a2d2014-01-10 00:40:45 +0000293 ((APSInt*)(char*)Data.buffer)->~APSInt();
Chris Lattner981f33b2008-11-16 07:46:48 +0000294 else if (Kind == Float)
Richard Smith7073a2d2014-01-10 00:40:45 +0000295 ((APFloat*)(char*)Data.buffer)->~APFloat();
Leonard Chan86285d22019-01-16 18:53:05 +0000296 else if (Kind == FixedPoint)
297 ((APFixedPoint *)(char *)Data.buffer)->~APFixedPoint();
Nate Begeman1e31b162009-01-18 01:01:34 +0000298 else if (Kind == Vector)
Richard Smith7073a2d2014-01-10 00:40:45 +0000299 ((Vec*)(char*)Data.buffer)->~Vec();
Chris Lattner981f33b2008-11-16 07:46:48 +0000300 else if (Kind == ComplexInt)
Richard Smith7073a2d2014-01-10 00:40:45 +0000301 ((ComplexAPSInt*)(char*)Data.buffer)->~ComplexAPSInt();
Chris Lattner981f33b2008-11-16 07:46:48 +0000302 else if (Kind == ComplexFloat)
Richard Smith7073a2d2014-01-10 00:40:45 +0000303 ((ComplexAPFloat*)(char*)Data.buffer)->~ComplexAPFloat();
Richard Smithf3e9e432011-11-07 09:22:26 +0000304 else if (Kind == LValue)
Richard Smith7073a2d2014-01-10 00:40:45 +0000305 ((LV*)(char*)Data.buffer)->~LV();
Richard Smithf3e9e432011-11-07 09:22:26 +0000306 else if (Kind == Array)
Richard Smith7073a2d2014-01-10 00:40:45 +0000307 ((Arr*)(char*)Data.buffer)->~Arr();
Richard Smithd62306a2011-11-10 06:34:14 +0000308 else if (Kind == Struct)
Richard Smith7073a2d2014-01-10 00:40:45 +0000309 ((StructData*)(char*)Data.buffer)->~StructData();
Richard Smithd62306a2011-11-10 06:34:14 +0000310 else if (Kind == Union)
Richard Smith7073a2d2014-01-10 00:40:45 +0000311 ((UnionData*)(char*)Data.buffer)->~UnionData();
Richard Smith027bf112011-11-17 22:56:20 +0000312 else if (Kind == MemberPointer)
Richard Smith7073a2d2014-01-10 00:40:45 +0000313 ((MemberPointerData*)(char*)Data.buffer)->~MemberPointerData();
Eli Friedmanfd5e54d2012-01-04 23:13:47 +0000314 else if (Kind == AddrLabelDiff)
Richard Smith7073a2d2014-01-10 00:40:45 +0000315 ((AddrLabelDiffData*)(char*)Data.buffer)->~AddrLabelDiffData();
Nate Begeman1e31b162009-01-18 01:01:34 +0000316 Kind = Uninitialized;
Chris Lattner981f33b2008-11-16 07:46:48 +0000317}
318
Manuel Klimeka7328992013-06-03 13:51:33 +0000319bool APValue::needsCleanup() const {
320 switch (getKind()) {
321 case Uninitialized:
322 case AddrLabelDiff:
323 return false;
324 case Struct:
325 case Union:
326 case Array:
327 case Vector:
328 return true;
329 case Int:
330 return getInt().needsCleanup();
331 case Float:
332 return getFloat().needsCleanup();
Leonard Chan86285d22019-01-16 18:53:05 +0000333 case FixedPoint:
334 return getFixedPoint().getValue().needsCleanup();
Manuel Klimeka7328992013-06-03 13:51:33 +0000335 case ComplexFloat:
336 assert(getComplexFloatImag().needsCleanup() ==
337 getComplexFloatReal().needsCleanup() &&
338 "In _Complex float types, real and imaginary values always have the "
339 "same size.");
340 return getComplexFloatReal().needsCleanup();
341 case ComplexInt:
342 assert(getComplexIntImag().needsCleanup() ==
343 getComplexIntReal().needsCleanup() &&
344 "In _Complex int types, real and imaginary values must have the "
345 "same size.");
346 return getComplexIntReal().needsCleanup();
347 case LValue:
Richard Smith7073a2d2014-01-10 00:40:45 +0000348 return reinterpret_cast<const LV *>(Data.buffer)->hasPathPtr();
Manuel Klimeka7328992013-06-03 13:51:33 +0000349 case MemberPointer:
Richard Smith7073a2d2014-01-10 00:40:45 +0000350 return reinterpret_cast<const MemberPointerData *>(Data.buffer)
351 ->hasPathPtr();
Manuel Klimeka7328992013-06-03 13:51:33 +0000352 }
Benjamin Kramerd1b7cd72013-06-03 21:26:13 +0000353 llvm_unreachable("Unknown APValue kind!");
Manuel Klimeka7328992013-06-03 13:51:33 +0000354}
355
Richard Smith4e9e5232012-03-10 00:28:11 +0000356void APValue::swap(APValue &RHS) {
357 std::swap(Kind, RHS.Kind);
Richard Smith7073a2d2014-01-10 00:40:45 +0000358 char TmpData[DataSize];
359 memcpy(TmpData, Data.buffer, DataSize);
360 memcpy(Data.buffer, RHS.Data.buffer, DataSize);
361 memcpy(RHS.Data.buffer, TmpData, DataSize);
Richard Smith4e9e5232012-03-10 00:28:11 +0000362}
363
Yaron Kerencdae9412016-01-29 19:38:18 +0000364LLVM_DUMP_METHOD void APValue::dump() const {
Richard Smithf6f003a2011-12-16 19:06:07 +0000365 dump(llvm::errs());
Chris Lattner981f33b2008-11-16 07:46:48 +0000366 llvm::errs() << '\n';
Chris Lattner981f33b2008-11-16 07:46:48 +0000367}
368
369static double GetApproxValue(const llvm::APFloat &F) {
370 llvm::APFloat V = F;
371 bool ignored;
Stephan Bergmann17c7f702016-12-14 11:57:17 +0000372 V.convert(llvm::APFloat::IEEEdouble(), llvm::APFloat::rmNearestTiesToEven,
Chris Lattner981f33b2008-11-16 07:46:48 +0000373 &ignored);
374 return V.convertToDouble();
375}
376
Richard Smithf6f003a2011-12-16 19:06:07 +0000377void APValue::dump(raw_ostream &OS) const {
Chris Lattner981f33b2008-11-16 07:46:48 +0000378 switch (getKind()) {
Chris Lattner981f33b2008-11-16 07:46:48 +0000379 case Uninitialized:
380 OS << "Uninitialized";
381 return;
382 case Int:
383 OS << "Int: " << getInt();
384 return;
385 case Float:
386 OS << "Float: " << GetApproxValue(getFloat());
387 return;
Leonard Chan86285d22019-01-16 18:53:05 +0000388 case FixedPoint:
389 OS << "FixedPoint : " << getFixedPoint();
390 return;
Nate Begeman1e31b162009-01-18 01:01:34 +0000391 case Vector:
Richard Smithf6f003a2011-12-16 19:06:07 +0000392 OS << "Vector: ";
393 getVectorElt(0).dump(OS);
394 for (unsigned i = 1; i != getVectorLength(); ++i) {
395 OS << ", ";
396 getVectorElt(i).dump(OS);
397 }
Nate Begeman1e31b162009-01-18 01:01:34 +0000398 return;
Chris Lattner981f33b2008-11-16 07:46:48 +0000399 case ComplexInt:
400 OS << "ComplexInt: " << getComplexIntReal() << ", " << getComplexIntImag();
401 return;
402 case ComplexFloat:
403 OS << "ComplexFloat: " << GetApproxValue(getComplexFloatReal())
404 << ", " << GetApproxValue(getComplexFloatImag());
Richard Smithf3e9e432011-11-07 09:22:26 +0000405 return;
Chris Lattner981f33b2008-11-16 07:46:48 +0000406 case LValue:
407 OS << "LValue: <todo>";
408 return;
Richard Smithf3e9e432011-11-07 09:22:26 +0000409 case Array:
410 OS << "Array: ";
411 for (unsigned I = 0, N = getArrayInitializedElts(); I != N; ++I) {
Richard Smithf6f003a2011-12-16 19:06:07 +0000412 getArrayInitializedElt(I).dump(OS);
Richard Smithf3e9e432011-11-07 09:22:26 +0000413 if (I != getArraySize() - 1) OS << ", ";
414 }
Richard Smithf6f003a2011-12-16 19:06:07 +0000415 if (hasArrayFiller()) {
416 OS << getArraySize() - getArrayInitializedElts() << " x ";
417 getArrayFiller().dump(OS);
418 }
Richard Smithf3e9e432011-11-07 09:22:26 +0000419 return;
Richard Smithd62306a2011-11-10 06:34:14 +0000420 case Struct:
421 OS << "Struct ";
422 if (unsigned N = getStructNumBases()) {
Richard Smithf6f003a2011-12-16 19:06:07 +0000423 OS << " bases: ";
424 getStructBase(0).dump(OS);
425 for (unsigned I = 1; I != N; ++I) {
426 OS << ", ";
427 getStructBase(I).dump(OS);
428 }
Richard Smithd62306a2011-11-10 06:34:14 +0000429 }
430 if (unsigned N = getStructNumFields()) {
Richard Smithf6f003a2011-12-16 19:06:07 +0000431 OS << " fields: ";
432 getStructField(0).dump(OS);
433 for (unsigned I = 1; I != N; ++I) {
434 OS << ", ";
435 getStructField(I).dump(OS);
436 }
Richard Smithd62306a2011-11-10 06:34:14 +0000437 }
438 return;
439 case Union:
Richard Smithf6f003a2011-12-16 19:06:07 +0000440 OS << "Union: ";
441 getUnionValue().dump(OS);
Richard Smithd62306a2011-11-10 06:34:14 +0000442 return;
Richard Smith027bf112011-11-17 22:56:20 +0000443 case MemberPointer:
444 OS << "MemberPointer: <todo>";
445 return;
Eli Friedmanfd5e54d2012-01-04 23:13:47 +0000446 case AddrLabelDiff:
447 OS << "AddrLabelDiff: <todo>";
448 return;
Chris Lattner981f33b2008-11-16 07:46:48 +0000449 }
Richard Smithd62306a2011-11-10 06:34:14 +0000450 llvm_unreachable("Unknown APValue kind!");
Chris Lattner981f33b2008-11-16 07:46:48 +0000451}
452
Richard Smithf6f003a2011-12-16 19:06:07 +0000453void APValue::printPretty(raw_ostream &Out, ASTContext &Ctx, QualType Ty) const{
454 switch (getKind()) {
Jeffrey Yasskind2af9622011-07-18 16:43:53 +0000455 case APValue::Uninitialized:
Richard Smithf6f003a2011-12-16 19:06:07 +0000456 Out << "<uninitialized>";
Richard Smithd62306a2011-11-10 06:34:14 +0000457 return;
Jeffrey Yasskind2af9622011-07-18 16:43:53 +0000458 case APValue::Int:
Richard Smith5614ca72012-03-23 23:55:39 +0000459 if (Ty->isBooleanType())
460 Out << (getInt().getBoolValue() ? "true" : "false");
461 else
462 Out << getInt();
Richard Smithd62306a2011-11-10 06:34:14 +0000463 return;
Jeffrey Yasskind2af9622011-07-18 16:43:53 +0000464 case APValue::Float:
Richard Smithf6f003a2011-12-16 19:06:07 +0000465 Out << GetApproxValue(getFloat());
Richard Smithd62306a2011-11-10 06:34:14 +0000466 return;
Leonard Chan86285d22019-01-16 18:53:05 +0000467 case APValue::FixedPoint:
468 Out << getFixedPoint();
469 return;
Richard Smithf6f003a2011-12-16 19:06:07 +0000470 case APValue::Vector: {
471 Out << '{';
472 QualType ElemTy = Ty->getAs<VectorType>()->getElementType();
473 getVectorElt(0).printPretty(Out, Ctx, ElemTy);
474 for (unsigned i = 1; i != getVectorLength(); ++i) {
Jeffrey Yasskind2af9622011-07-18 16:43:53 +0000475 Out << ", ";
Richard Smithf6f003a2011-12-16 19:06:07 +0000476 getVectorElt(i).printPretty(Out, Ctx, ElemTy);
Jeffrey Yasskind2af9622011-07-18 16:43:53 +0000477 }
Richard Smithf6f003a2011-12-16 19:06:07 +0000478 Out << '}';
Richard Smithd62306a2011-11-10 06:34:14 +0000479 return;
Richard Smithf6f003a2011-12-16 19:06:07 +0000480 }
Jeffrey Yasskind2af9622011-07-18 16:43:53 +0000481 case APValue::ComplexInt:
Richard Smithf6f003a2011-12-16 19:06:07 +0000482 Out << getComplexIntReal() << "+" << getComplexIntImag() << "i";
Richard Smithd62306a2011-11-10 06:34:14 +0000483 return;
Jeffrey Yasskind2af9622011-07-18 16:43:53 +0000484 case APValue::ComplexFloat:
Richard Smithf6f003a2011-12-16 19:06:07 +0000485 Out << GetApproxValue(getComplexFloatReal()) << "+"
486 << GetApproxValue(getComplexFloatImag()) << "i";
Richard Smithd62306a2011-11-10 06:34:14 +0000487 return;
Richard Smithf6f003a2011-12-16 19:06:07 +0000488 case APValue::LValue: {
Richard Smithf6f003a2011-12-16 19:06:07 +0000489 bool IsReference = Ty->isReferenceType();
490 QualType InnerTy
491 = IsReference ? Ty.getNonReferenceType() : Ty->getPointeeType();
Douglas Gregor0b7bc7f2013-01-29 01:26:43 +0000492 if (InnerTy.isNull())
493 InnerTy = Ty;
Richard Smithf6f003a2011-12-16 19:06:07 +0000494
Richard Smith128719c2018-09-13 22:47:33 +0000495 LValueBase Base = getLValueBase();
496 if (!Base) {
497 if (isNullPointer()) {
498 Out << (Ctx.getLangOpts().CPlusPlus11 ? "nullptr" : "0");
499 } else if (IsReference) {
500 Out << "*(" << InnerTy.stream(Ctx.getPrintingPolicy()) << "*)"
501 << getLValueOffset().getQuantity();
502 } else {
503 Out << "(" << Ty.stream(Ctx.getPrintingPolicy()) << ")"
504 << getLValueOffset().getQuantity();
505 }
506 return;
507 }
508
Richard Smithf6f003a2011-12-16 19:06:07 +0000509 if (!hasLValuePath()) {
510 // No lvalue path: just print the offset.
511 CharUnits O = getLValueOffset();
512 CharUnits S = Ctx.getTypeSizeInChars(InnerTy);
513 if (!O.isZero()) {
514 if (IsReference)
515 Out << "*(";
516 if (O % S) {
517 Out << "(char*)";
518 S = CharUnits::One();
519 }
520 Out << '&';
521 } else if (!IsReference)
522 Out << '&';
523
524 if (const ValueDecl *VD = Base.dyn_cast<const ValueDecl*>())
525 Out << *VD;
Richard Smithee0ce3022019-05-17 07:06:46 +0000526 else if (TypeInfoLValue TI = Base.dyn_cast<TypeInfoLValue>()) {
527 TI.print(Out, Ctx.getPrintingPolicy());
528 } else {
Richard Trieuddd01ce2014-06-09 22:53:25 +0000529 assert(Base.get<const Expr *>() != nullptr &&
530 "Expecting non-null Expr");
Craig Topper36250ad2014-05-12 05:36:57 +0000531 Base.get<const Expr*>()->printPretty(Out, nullptr,
532 Ctx.getPrintingPolicy());
Richard Trieuddd01ce2014-06-09 22:53:25 +0000533 }
534
Richard Smithf6f003a2011-12-16 19:06:07 +0000535 if (!O.isZero()) {
536 Out << " + " << (O / S);
537 if (IsReference)
538 Out << ')';
539 }
540 return;
541 }
542
543 // We have an lvalue path. Print it out nicely.
544 if (!IsReference)
545 Out << '&';
546 else if (isLValueOnePastTheEnd())
547 Out << "*(&";
548
549 QualType ElemTy;
550 if (const ValueDecl *VD = Base.dyn_cast<const ValueDecl*>()) {
551 Out << *VD;
552 ElemTy = VD->getType();
Richard Smithee0ce3022019-05-17 07:06:46 +0000553 } else if (TypeInfoLValue TI = Base.dyn_cast<TypeInfoLValue>()) {
554 TI.print(Out, Ctx.getPrintingPolicy());
555 ElemTy = Base.getTypeInfoType();
Richard Smithf6f003a2011-12-16 19:06:07 +0000556 } else {
557 const Expr *E = Base.get<const Expr*>();
Richard Trieuddd01ce2014-06-09 22:53:25 +0000558 assert(E != nullptr && "Expecting non-null Expr");
Craig Topper36250ad2014-05-12 05:36:57 +0000559 E->printPretty(Out, nullptr, Ctx.getPrintingPolicy());
Richard Smithf6f003a2011-12-16 19:06:07 +0000560 ElemTy = E->getType();
561 }
562
563 ArrayRef<LValuePathEntry> Path = getLValuePath();
Craig Topper36250ad2014-05-12 05:36:57 +0000564 const CXXRecordDecl *CastToBase = nullptr;
Richard Smithf6f003a2011-12-16 19:06:07 +0000565 for (unsigned I = 0, N = Path.size(); I != N; ++I) {
566 if (ElemTy->getAs<RecordType>()) {
567 // The lvalue refers to a class type, so the next path entry is a base
568 // or member.
Richard Smith5b5e27a2019-05-10 20:05:31 +0000569 const Decl *BaseOrMember = Path[I].getAsBaseOrMember().getPointer();
Richard Smithf6f003a2011-12-16 19:06:07 +0000570 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(BaseOrMember)) {
571 CastToBase = RD;
572 ElemTy = Ctx.getRecordType(RD);
573 } else {
574 const ValueDecl *VD = cast<ValueDecl>(BaseOrMember);
575 Out << ".";
576 if (CastToBase)
577 Out << *CastToBase << "::";
578 Out << *VD;
579 ElemTy = VD->getType();
580 }
581 } else {
582 // The lvalue must refer to an array.
Richard Smith5b5e27a2019-05-10 20:05:31 +0000583 Out << '[' << Path[I].getAsArrayIndex() << ']';
Richard Smithf6f003a2011-12-16 19:06:07 +0000584 ElemTy = Ctx.getAsArrayType(ElemTy)->getElementType();
585 }
586 }
587
588 // Handle formatting of one-past-the-end lvalues.
589 if (isLValueOnePastTheEnd()) {
590 // FIXME: If CastToBase is non-0, we should prefix the output with
591 // "(CastToBase*)".
592 Out << " + 1";
593 if (IsReference)
594 Out << ')';
595 }
Richard Smithd62306a2011-11-10 06:34:14 +0000596 return;
Richard Smithf6f003a2011-12-16 19:06:07 +0000597 }
598 case APValue::Array: {
599 const ArrayType *AT = Ctx.getAsArrayType(Ty);
600 QualType ElemTy = AT->getElementType();
Richard Smithd62306a2011-11-10 06:34:14 +0000601 Out << '{';
Richard Smithf6f003a2011-12-16 19:06:07 +0000602 if (unsigned N = getArrayInitializedElts()) {
603 getArrayInitializedElt(0).printPretty(Out, Ctx, ElemTy);
604 for (unsigned I = 1; I != N; ++I) {
Richard Smithd62306a2011-11-10 06:34:14 +0000605 Out << ", ";
Richard Smithf6f003a2011-12-16 19:06:07 +0000606 if (I == 10) {
607 // Avoid printing out the entire contents of large arrays.
608 Out << "...";
609 break;
610 }
611 getArrayInitializedElt(I).printPretty(Out, Ctx, ElemTy);
612 }
Richard Smithd62306a2011-11-10 06:34:14 +0000613 }
614 Out << '}';
615 return;
Richard Smithf6f003a2011-12-16 19:06:07 +0000616 }
617 case APValue::Struct: {
618 Out << '{';
619 const RecordDecl *RD = Ty->getAs<RecordType>()->getDecl();
620 bool First = true;
621 if (unsigned N = getStructNumBases()) {
622 const CXXRecordDecl *CD = cast<CXXRecordDecl>(RD);
623 CXXRecordDecl::base_class_const_iterator BI = CD->bases_begin();
624 for (unsigned I = 0; I != N; ++I, ++BI) {
625 assert(BI != CD->bases_end());
626 if (!First)
627 Out << ", ";
628 getStructBase(I).printPretty(Out, Ctx, BI->getType());
629 First = false;
630 }
631 }
Aaron Ballmane8a8bae2014-03-08 20:12:42 +0000632 for (const auto *FI : RD->fields()) {
Richard Smithf6f003a2011-12-16 19:06:07 +0000633 if (!First)
634 Out << ", ";
David Blaikie2d7c57e2012-04-30 02:36:29 +0000635 if (FI->isUnnamedBitfield()) continue;
636 getStructField(FI->getFieldIndex()).
637 printPretty(Out, Ctx, FI->getType());
Richard Smithf6f003a2011-12-16 19:06:07 +0000638 First = false;
639 }
640 Out << '}';
641 return;
642 }
Richard Smithd62306a2011-11-10 06:34:14 +0000643 case APValue::Union:
Richard Smithf6f003a2011-12-16 19:06:07 +0000644 Out << '{';
645 if (const FieldDecl *FD = getUnionField()) {
646 Out << "." << *FD << " = ";
647 getUnionValue().printPretty(Out, Ctx, FD->getType());
648 }
649 Out << '}';
Richard Smithd62306a2011-11-10 06:34:14 +0000650 return;
Richard Smith027bf112011-11-17 22:56:20 +0000651 case APValue::MemberPointer:
Richard Smithf6f003a2011-12-16 19:06:07 +0000652 // FIXME: This is not enough to unambiguously identify the member in a
653 // multiple-inheritance scenario.
654 if (const ValueDecl *VD = getMemberPointerDecl()) {
655 Out << '&' << *cast<CXXRecordDecl>(VD->getDeclContext()) << "::" << *VD;
656 return;
657 }
658 Out << "0";
Richard Smith027bf112011-11-17 22:56:20 +0000659 return;
Eli Friedmanfd5e54d2012-01-04 23:13:47 +0000660 case APValue::AddrLabelDiff:
661 Out << "&&" << getAddrLabelDiffLHS()->getLabel()->getName();
662 Out << " - ";
663 Out << "&&" << getAddrLabelDiffRHS()->getLabel()->getName();
664 return;
Jeffrey Yasskind2af9622011-07-18 16:43:53 +0000665 }
Richard Smithd62306a2011-11-10 06:34:14 +0000666 llvm_unreachable("Unknown APValue kind!");
Jeffrey Yasskind2af9622011-07-18 16:43:53 +0000667}
668
Richard Smithf6f003a2011-12-16 19:06:07 +0000669std::string APValue::getAsString(ASTContext &Ctx, QualType Ty) const {
670 std::string Result;
671 llvm::raw_string_ostream Out(Result);
672 printPretty(Out, Ctx, Ty);
Eli Friedman375f09f2011-12-16 22:12:23 +0000673 Out.flush();
Richard Smithf6f003a2011-12-16 19:06:07 +0000674 return Result;
Jeffrey Yasskind2af9622011-07-18 16:43:53 +0000675}
676
Hans Wennborgdd1ea8a2019-03-06 10:26:19 +0000677bool APValue::toIntegralConstant(APSInt &Result, QualType SrcTy,
678 const ASTContext &Ctx) const {
679 if (isInt()) {
680 Result = getInt();
681 return true;
682 }
683
684 if (isLValue() && isNullPointer()) {
685 Result = Ctx.MakeIntValue(Ctx.getTargetNullPointerValue(SrcTy), SrcTy);
686 return true;
687 }
688
689 if (isLValue() && !getLValueBase()) {
690 Result = Ctx.MakeIntValue(getLValueOffset().getQuantity(), SrcTy);
691 return true;
692 }
693
694 return false;
695}
696
Richard Smithce40ad62011-11-12 22:28:03 +0000697const APValue::LValueBase APValue::getLValueBase() const {
Ken Dyck02990832010-01-15 12:37:54 +0000698 assert(isLValue() && "Invalid accessor");
Akira Hatanaka4e2698c2018-04-10 05:15:01 +0000699 return ((const LV*)(const void*)Data.buffer)->Base;
Richard Smith027bf112011-11-17 22:56:20 +0000700}
701
702bool APValue::isLValueOnePastTheEnd() const {
703 assert(isLValue() && "Invalid accessor");
Akira Hatanaka4e2698c2018-04-10 05:15:01 +0000704 return ((const LV*)(const void*)Data.buffer)->IsOnePastTheEnd;
Ken Dyck02990832010-01-15 12:37:54 +0000705}
706
Richard Smith0b0a0b62011-10-29 20:57:55 +0000707CharUnits &APValue::getLValueOffset() {
708 assert(isLValue() && "Invalid accessor");
Richard Smith7073a2d2014-01-10 00:40:45 +0000709 return ((LV*)(void*)Data.buffer)->Offset;
Ken Dyck02990832010-01-15 12:37:54 +0000710}
711
Richard Smith80815602011-11-07 05:07:52 +0000712bool APValue::hasLValuePath() const {
Ken Dyck02990832010-01-15 12:37:54 +0000713 assert(isLValue() && "Invalid accessor");
Richard Smith7073a2d2014-01-10 00:40:45 +0000714 return ((const LV*)(const char*)Data.buffer)->hasPath();
Richard Smith80815602011-11-07 05:07:52 +0000715}
716
717ArrayRef<APValue::LValuePathEntry> APValue::getLValuePath() const {
718 assert(isLValue() && hasLValuePath() && "Invalid accessor");
Richard Smith7073a2d2014-01-10 00:40:45 +0000719 const LV &LVal = *((const LV*)(const char*)Data.buffer);
Craig Topper5fc8fc22014-08-27 06:28:36 +0000720 return llvm::makeArrayRef(LVal.getPath(), LVal.PathLength);
Richard Smith80815602011-11-07 05:07:52 +0000721}
722
Richard Smithb228a862012-02-15 02:18:13 +0000723unsigned APValue::getLValueCallIndex() const {
724 assert(isLValue() && "Invalid accessor");
Akira Hatanaka4e2698c2018-04-10 05:15:01 +0000725 return ((const LV*)(const char*)Data.buffer)->Base.getCallIndex();
726}
727
728unsigned APValue::getLValueVersion() const {
729 assert(isLValue() && "Invalid accessor");
730 return ((const LV*)(const char*)Data.buffer)->Base.getVersion();
Richard Smithb228a862012-02-15 02:18:13 +0000731}
732
Yaxun Liu402804b2016-12-15 08:09:08 +0000733bool APValue::isNullPointer() const {
734 assert(isLValue() && "Invalid usage");
735 return ((const LV*)(const char*)Data.buffer)->IsNullPtr;
736}
737
Richard Smithb228a862012-02-15 02:18:13 +0000738void APValue::setLValue(LValueBase B, const CharUnits &O, NoLValuePath,
Akira Hatanaka4e2698c2018-04-10 05:15:01 +0000739 bool IsNullPtr) {
Richard Smith80815602011-11-07 05:07:52 +0000740 assert(isLValue() && "Invalid accessor");
Richard Smith7073a2d2014-01-10 00:40:45 +0000741 LV &LVal = *((LV*)(char*)Data.buffer);
Akira Hatanaka4e2698c2018-04-10 05:15:01 +0000742 LVal.Base = B;
743 LVal.IsOnePastTheEnd = false;
Richard Smith80815602011-11-07 05:07:52 +0000744 LVal.Offset = O;
Richard Smith027bf112011-11-17 22:56:20 +0000745 LVal.resizePath((unsigned)-1);
Yaxun Liu402804b2016-12-15 08:09:08 +0000746 LVal.IsNullPtr = IsNullPtr;
Richard Smith80815602011-11-07 05:07:52 +0000747}
748
Richard Smithce40ad62011-11-12 22:28:03 +0000749void APValue::setLValue(LValueBase B, const CharUnits &O,
Richard Smithb228a862012-02-15 02:18:13 +0000750 ArrayRef<LValuePathEntry> Path, bool IsOnePastTheEnd,
Akira Hatanaka4e2698c2018-04-10 05:15:01 +0000751 bool IsNullPtr) {
Richard Smith80815602011-11-07 05:07:52 +0000752 assert(isLValue() && "Invalid accessor");
Richard Smith7073a2d2014-01-10 00:40:45 +0000753 LV &LVal = *((LV*)(char*)Data.buffer);
Akira Hatanaka4e2698c2018-04-10 05:15:01 +0000754 LVal.Base = B;
755 LVal.IsOnePastTheEnd = IsOnePastTheEnd;
Richard Smith80815602011-11-07 05:07:52 +0000756 LVal.Offset = O;
Richard Smith027bf112011-11-17 22:56:20 +0000757 LVal.resizePath(Path.size());
Richard Smith80815602011-11-07 05:07:52 +0000758 memcpy(LVal.getPath(), Path.data(), Path.size() * sizeof(LValuePathEntry));
Yaxun Liu402804b2016-12-15 08:09:08 +0000759 LVal.IsNullPtr = IsNullPtr;
Ken Dyck02990832010-01-15 12:37:54 +0000760}
761
Richard Smith027bf112011-11-17 22:56:20 +0000762const ValueDecl *APValue::getMemberPointerDecl() const {
763 assert(isMemberPointer() && "Invalid accessor");
Richard Smith7073a2d2014-01-10 00:40:45 +0000764 const MemberPointerData &MPD =
765 *((const MemberPointerData *)(const char *)Data.buffer);
Richard Smith027bf112011-11-17 22:56:20 +0000766 return MPD.MemberAndIsDerivedMember.getPointer();
767}
768
769bool APValue::isMemberPointerToDerivedMember() const {
770 assert(isMemberPointer() && "Invalid accessor");
Richard Smith7073a2d2014-01-10 00:40:45 +0000771 const MemberPointerData &MPD =
772 *((const MemberPointerData *)(const char *)Data.buffer);
Richard Smith027bf112011-11-17 22:56:20 +0000773 return MPD.MemberAndIsDerivedMember.getInt();
774}
775
776ArrayRef<const CXXRecordDecl*> APValue::getMemberPointerPath() const {
777 assert(isMemberPointer() && "Invalid accessor");
Richard Smith7073a2d2014-01-10 00:40:45 +0000778 const MemberPointerData &MPD =
779 *((const MemberPointerData *)(const char *)Data.buffer);
Craig Topper5fc8fc22014-08-27 06:28:36 +0000780 return llvm::makeArrayRef(MPD.getPath(), MPD.PathLength);
Richard Smith027bf112011-11-17 22:56:20 +0000781}
782
Ken Dyck02990832010-01-15 12:37:54 +0000783void APValue::MakeLValue() {
784 assert(isUninit() && "Bad state change");
Benjamin Kramera939c232014-03-15 18:54:13 +0000785 static_assert(sizeof(LV) <= DataSize, "LV too big");
Richard Smith7073a2d2014-01-10 00:40:45 +0000786 new ((void*)(char*)Data.buffer) LV();
Ken Dyck02990832010-01-15 12:37:54 +0000787 Kind = LValue;
788}
Richard Smithf3e9e432011-11-07 09:22:26 +0000789
790void APValue::MakeArray(unsigned InitElts, unsigned Size) {
791 assert(isUninit() && "Bad state change");
Richard Smith7073a2d2014-01-10 00:40:45 +0000792 new ((void*)(char*)Data.buffer) Arr(InitElts, Size);
Richard Smithf3e9e432011-11-07 09:22:26 +0000793 Kind = Array;
794}
Richard Smith027bf112011-11-17 22:56:20 +0000795
796void APValue::MakeMemberPointer(const ValueDecl *Member, bool IsDerivedMember,
797 ArrayRef<const CXXRecordDecl*> Path) {
798 assert(isUninit() && "Bad state change");
Richard Smith7073a2d2014-01-10 00:40:45 +0000799 MemberPointerData *MPD = new ((void*)(char*)Data.buffer) MemberPointerData;
Richard Smith027bf112011-11-17 22:56:20 +0000800 Kind = MemberPointer;
801 MPD->MemberAndIsDerivedMember.setPointer(Member);
802 MPD->MemberAndIsDerivedMember.setInt(IsDerivedMember);
803 MPD->resizePath(Path.size());
804 memcpy(MPD->getPath(), Path.data(), Path.size()*sizeof(const CXXRecordDecl*));
805}