blob: 91f1e20d73b64d148f6b7a6e6d791f7f5d7965c4 [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"
Jeffrey Yasskind2af9622011-07-18 16:43:53 +000020#include "clang/Basic/Diagnostic.h"
21#include "llvm/ADT/SmallString.h"
David Blaikie76bd3c82011-09-23 05:35:21 +000022#include "llvm/Support/ErrorHandling.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000023#include "llvm/Support/raw_ostream.h"
Chris Lattner981f33b2008-11-16 07:46:48 +000024using namespace clang;
25
Ken Dyck02990832010-01-15 12:37:54 +000026namespace {
Richard Smith80815602011-11-07 05:07:52 +000027 struct LVBase {
Richard Smith027bf112011-11-17 22:56:20 +000028 llvm::PointerIntPair<APValue::LValueBase, 1, bool> BaseAndIsOnePastTheEnd;
Ken Dyck02990832010-01-15 12:37:54 +000029 CharUnits Offset;
Richard Smith80815602011-11-07 05:07:52 +000030 unsigned PathLength;
Richard Smithb228a862012-02-15 02:18:13 +000031 unsigned CallIndex;
Ken Dyck02990832010-01-15 12:37:54 +000032 };
33}
34
Richard Smith80815602011-11-07 05:07:52 +000035struct APValue::LV : LVBase {
36 static const unsigned InlinePathSpace =
Richard Smith7073a2d2014-01-10 00:40:45 +000037 (DataSize - sizeof(LVBase)) / sizeof(LValuePathEntry);
Richard Smith80815602011-11-07 05:07:52 +000038
39 /// Path - The sequence of base classes, fields and array indices to follow to
40 /// walk from Base to the subobject. When performing GCC-style folding, there
41 /// may not be such a path.
42 union {
43 LValuePathEntry Path[InlinePathSpace];
44 LValuePathEntry *PathPtr;
45 };
46
47 LV() { PathLength = (unsigned)-1; }
Richard Smith027bf112011-11-17 22:56:20 +000048 ~LV() { resizePath(0); }
Richard Smith80815602011-11-07 05:07:52 +000049
Richard Smith027bf112011-11-17 22:56:20 +000050 void resizePath(unsigned Length) {
51 if (Length == PathLength)
52 return;
53 if (hasPathPtr())
54 delete [] PathPtr;
55 PathLength = Length;
56 if (hasPathPtr())
57 PathPtr = new LValuePathEntry[Length];
Richard Smith80815602011-11-07 05:07:52 +000058 }
59
60 bool hasPath() const { return PathLength != (unsigned)-1; }
61 bool hasPathPtr() const { return hasPath() && PathLength > InlinePathSpace; }
62
63 LValuePathEntry *getPath() { return hasPathPtr() ? PathPtr : Path; }
Richard Smithbcb4eb22011-11-07 07:31:09 +000064 const LValuePathEntry *getPath() const {
65 return hasPathPtr() ? PathPtr : Path;
66 }
Richard Smith80815602011-11-07 05:07:52 +000067};
68
Richard Smith027bf112011-11-17 22:56:20 +000069namespace {
70 struct MemberPointerBase {
71 llvm::PointerIntPair<const ValueDecl*, 1, bool> MemberAndIsDerivedMember;
72 unsigned PathLength;
73 };
74}
75
76struct APValue::MemberPointerData : MemberPointerBase {
77 static const unsigned InlinePathSpace =
Richard Smith7073a2d2014-01-10 00:40:45 +000078 (DataSize - sizeof(MemberPointerBase)) / sizeof(const CXXRecordDecl*);
Richard Smith027bf112011-11-17 22:56:20 +000079 typedef const CXXRecordDecl *PathElem;
80 union {
81 PathElem Path[InlinePathSpace];
82 PathElem *PathPtr;
83 };
84
85 MemberPointerData() { PathLength = 0; }
86 ~MemberPointerData() { resizePath(0); }
87
88 void resizePath(unsigned Length) {
89 if (Length == PathLength)
90 return;
91 if (hasPathPtr())
92 delete [] PathPtr;
93 PathLength = Length;
94 if (hasPathPtr())
95 PathPtr = new PathElem[Length];
96 }
97
98 bool hasPathPtr() const { return PathLength > InlinePathSpace; }
99
100 PathElem *getPath() { return hasPathPtr() ? PathPtr : Path; }
101 const PathElem *getPath() const {
102 return hasPathPtr() ? PathPtr : Path;
103 }
104};
105
Richard Smithf3e9e432011-11-07 09:22:26 +0000106// FIXME: Reduce the malloc traffic here.
107
108APValue::Arr::Arr(unsigned NumElts, unsigned Size) :
109 Elts(new APValue[NumElts + (NumElts != Size ? 1 : 0)]),
110 NumElts(NumElts), ArrSize(Size) {}
111APValue::Arr::~Arr() { delete [] Elts; }
112
Richard Smithd62306a2011-11-10 06:34:14 +0000113APValue::StructData::StructData(unsigned NumBases, unsigned NumFields) :
114 Elts(new APValue[NumBases+NumFields]),
115 NumBases(NumBases), NumFields(NumFields) {}
116APValue::StructData::~StructData() {
117 delete [] Elts;
118}
119
Craig Topper36250ad2014-05-12 05:36:57 +0000120APValue::UnionData::UnionData() : Field(nullptr), Value(new APValue) {}
Richard Smithd62306a2011-11-10 06:34:14 +0000121APValue::UnionData::~UnionData () {
122 delete Value;
123}
124
Richard Smith4e9e5232012-03-10 00:28:11 +0000125APValue::APValue(const APValue &RHS) : Kind(Uninitialized) {
126 switch (RHS.getKind()) {
127 case Uninitialized:
128 break;
129 case Int:
130 MakeInt();
Chris Lattner981f33b2008-11-16 07:46:48 +0000131 setInt(RHS.getInt());
Richard Smith4e9e5232012-03-10 00:28:11 +0000132 break;
133 case Float:
134 MakeFloat();
Chris Lattner981f33b2008-11-16 07:46:48 +0000135 setFloat(RHS.getFloat());
Richard Smith4e9e5232012-03-10 00:28:11 +0000136 break;
137 case Vector:
138 MakeVector();
Richard Smith7073a2d2014-01-10 00:40:45 +0000139 setVector(((const Vec *)(const char *)RHS.Data.buffer)->Elts,
Dan Gohman145f3f12010-04-19 16:39:44 +0000140 RHS.getVectorLength());
Richard Smith4e9e5232012-03-10 00:28:11 +0000141 break;
142 case ComplexInt:
143 MakeComplexInt();
Chris Lattner981f33b2008-11-16 07:46:48 +0000144 setComplexInt(RHS.getComplexIntReal(), RHS.getComplexIntImag());
Richard Smith4e9e5232012-03-10 00:28:11 +0000145 break;
146 case ComplexFloat:
147 MakeComplexFloat();
Chris Lattner981f33b2008-11-16 07:46:48 +0000148 setComplexFloat(RHS.getComplexFloatReal(), RHS.getComplexFloatImag());
Richard Smith4e9e5232012-03-10 00:28:11 +0000149 break;
150 case LValue:
151 MakeLValue();
Richard Smith80815602011-11-07 05:07:52 +0000152 if (RHS.hasLValuePath())
Richard Smith027bf112011-11-17 22:56:20 +0000153 setLValue(RHS.getLValueBase(), RHS.getLValueOffset(), RHS.getLValuePath(),
Richard Smithb228a862012-02-15 02:18:13 +0000154 RHS.isLValueOnePastTheEnd(), RHS.getLValueCallIndex());
Richard Smith80815602011-11-07 05:07:52 +0000155 else
Richard Smithb228a862012-02-15 02:18:13 +0000156 setLValue(RHS.getLValueBase(), RHS.getLValueOffset(), NoLValuePath(),
157 RHS.getLValueCallIndex());
Richard Smith4e9e5232012-03-10 00:28:11 +0000158 break;
159 case Array:
160 MakeArray(RHS.getArrayInitializedElts(), RHS.getArraySize());
Richard Smithf3e9e432011-11-07 09:22:26 +0000161 for (unsigned I = 0, N = RHS.getArrayInitializedElts(); I != N; ++I)
162 getArrayInitializedElt(I) = RHS.getArrayInitializedElt(I);
163 if (RHS.hasArrayFiller())
164 getArrayFiller() = RHS.getArrayFiller();
Richard Smith4e9e5232012-03-10 00:28:11 +0000165 break;
166 case Struct:
167 MakeStruct(RHS.getStructNumBases(), RHS.getStructNumFields());
Richard Smithd62306a2011-11-10 06:34:14 +0000168 for (unsigned I = 0, N = RHS.getStructNumBases(); I != N; ++I)
169 getStructBase(I) = RHS.getStructBase(I);
170 for (unsigned I = 0, N = RHS.getStructNumFields(); I != N; ++I)
171 getStructField(I) = RHS.getStructField(I);
Richard Smith4e9e5232012-03-10 00:28:11 +0000172 break;
173 case Union:
174 MakeUnion();
Richard Smithd62306a2011-11-10 06:34:14 +0000175 setUnion(RHS.getUnionField(), RHS.getUnionValue());
Richard Smith4e9e5232012-03-10 00:28:11 +0000176 break;
177 case MemberPointer:
178 MakeMemberPointer(RHS.getMemberPointerDecl(),
179 RHS.isMemberPointerToDerivedMember(),
180 RHS.getMemberPointerPath());
181 break;
182 case AddrLabelDiff:
183 MakeAddrLabelDiff();
Eli Friedmanfd5e54d2012-01-04 23:13:47 +0000184 setAddrLabelDiff(RHS.getAddrLabelDiffLHS(), RHS.getAddrLabelDiffRHS());
Richard Smith4e9e5232012-03-10 00:28:11 +0000185 break;
Eli Friedmanfd5e54d2012-01-04 23:13:47 +0000186 }
Chris Lattner981f33b2008-11-16 07:46:48 +0000187}
188
Daniel Dunbarb7431572012-03-08 20:28:55 +0000189void APValue::DestroyDataAndMakeUninit() {
Chris Lattner981f33b2008-11-16 07:46:48 +0000190 if (Kind == Int)
Richard Smith7073a2d2014-01-10 00:40:45 +0000191 ((APSInt*)(char*)Data.buffer)->~APSInt();
Chris Lattner981f33b2008-11-16 07:46:48 +0000192 else if (Kind == Float)
Richard Smith7073a2d2014-01-10 00:40:45 +0000193 ((APFloat*)(char*)Data.buffer)->~APFloat();
Nate Begeman1e31b162009-01-18 01:01:34 +0000194 else if (Kind == Vector)
Richard Smith7073a2d2014-01-10 00:40:45 +0000195 ((Vec*)(char*)Data.buffer)->~Vec();
Chris Lattner981f33b2008-11-16 07:46:48 +0000196 else if (Kind == ComplexInt)
Richard Smith7073a2d2014-01-10 00:40:45 +0000197 ((ComplexAPSInt*)(char*)Data.buffer)->~ComplexAPSInt();
Chris Lattner981f33b2008-11-16 07:46:48 +0000198 else if (Kind == ComplexFloat)
Richard Smith7073a2d2014-01-10 00:40:45 +0000199 ((ComplexAPFloat*)(char*)Data.buffer)->~ComplexAPFloat();
Richard Smithf3e9e432011-11-07 09:22:26 +0000200 else if (Kind == LValue)
Richard Smith7073a2d2014-01-10 00:40:45 +0000201 ((LV*)(char*)Data.buffer)->~LV();
Richard Smithf3e9e432011-11-07 09:22:26 +0000202 else if (Kind == Array)
Richard Smith7073a2d2014-01-10 00:40:45 +0000203 ((Arr*)(char*)Data.buffer)->~Arr();
Richard Smithd62306a2011-11-10 06:34:14 +0000204 else if (Kind == Struct)
Richard Smith7073a2d2014-01-10 00:40:45 +0000205 ((StructData*)(char*)Data.buffer)->~StructData();
Richard Smithd62306a2011-11-10 06:34:14 +0000206 else if (Kind == Union)
Richard Smith7073a2d2014-01-10 00:40:45 +0000207 ((UnionData*)(char*)Data.buffer)->~UnionData();
Richard Smith027bf112011-11-17 22:56:20 +0000208 else if (Kind == MemberPointer)
Richard Smith7073a2d2014-01-10 00:40:45 +0000209 ((MemberPointerData*)(char*)Data.buffer)->~MemberPointerData();
Eli Friedmanfd5e54d2012-01-04 23:13:47 +0000210 else if (Kind == AddrLabelDiff)
Richard Smith7073a2d2014-01-10 00:40:45 +0000211 ((AddrLabelDiffData*)(char*)Data.buffer)->~AddrLabelDiffData();
Nate Begeman1e31b162009-01-18 01:01:34 +0000212 Kind = Uninitialized;
Chris Lattner981f33b2008-11-16 07:46:48 +0000213}
214
Manuel Klimeka7328992013-06-03 13:51:33 +0000215bool APValue::needsCleanup() const {
216 switch (getKind()) {
217 case Uninitialized:
218 case AddrLabelDiff:
219 return false;
220 case Struct:
221 case Union:
222 case Array:
223 case Vector:
224 return true;
225 case Int:
226 return getInt().needsCleanup();
227 case Float:
228 return getFloat().needsCleanup();
229 case ComplexFloat:
230 assert(getComplexFloatImag().needsCleanup() ==
231 getComplexFloatReal().needsCleanup() &&
232 "In _Complex float types, real and imaginary values always have the "
233 "same size.");
234 return getComplexFloatReal().needsCleanup();
235 case ComplexInt:
236 assert(getComplexIntImag().needsCleanup() ==
237 getComplexIntReal().needsCleanup() &&
238 "In _Complex int types, real and imaginary values must have the "
239 "same size.");
240 return getComplexIntReal().needsCleanup();
241 case LValue:
Richard Smith7073a2d2014-01-10 00:40:45 +0000242 return reinterpret_cast<const LV *>(Data.buffer)->hasPathPtr();
Manuel Klimeka7328992013-06-03 13:51:33 +0000243 case MemberPointer:
Richard Smith7073a2d2014-01-10 00:40:45 +0000244 return reinterpret_cast<const MemberPointerData *>(Data.buffer)
245 ->hasPathPtr();
Manuel Klimeka7328992013-06-03 13:51:33 +0000246 }
Benjamin Kramerd1b7cd72013-06-03 21:26:13 +0000247 llvm_unreachable("Unknown APValue kind!");
Manuel Klimeka7328992013-06-03 13:51:33 +0000248}
249
Richard Smith4e9e5232012-03-10 00:28:11 +0000250void APValue::swap(APValue &RHS) {
251 std::swap(Kind, RHS.Kind);
Richard Smith7073a2d2014-01-10 00:40:45 +0000252 char TmpData[DataSize];
253 memcpy(TmpData, Data.buffer, DataSize);
254 memcpy(Data.buffer, RHS.Data.buffer, DataSize);
255 memcpy(RHS.Data.buffer, TmpData, DataSize);
Richard Smith4e9e5232012-03-10 00:28:11 +0000256}
257
Chris Lattner981f33b2008-11-16 07:46:48 +0000258void APValue::dump() const {
Richard Smithf6f003a2011-12-16 19:06:07 +0000259 dump(llvm::errs());
Chris Lattner981f33b2008-11-16 07:46:48 +0000260 llvm::errs() << '\n';
Chris Lattner981f33b2008-11-16 07:46:48 +0000261}
262
263static double GetApproxValue(const llvm::APFloat &F) {
264 llvm::APFloat V = F;
265 bool ignored;
266 V.convert(llvm::APFloat::IEEEdouble, llvm::APFloat::rmNearestTiesToEven,
267 &ignored);
268 return V.convertToDouble();
269}
270
Richard Smithf6f003a2011-12-16 19:06:07 +0000271void APValue::dump(raw_ostream &OS) const {
Chris Lattner981f33b2008-11-16 07:46:48 +0000272 switch (getKind()) {
Chris Lattner981f33b2008-11-16 07:46:48 +0000273 case Uninitialized:
274 OS << "Uninitialized";
275 return;
276 case Int:
277 OS << "Int: " << getInt();
278 return;
279 case Float:
280 OS << "Float: " << GetApproxValue(getFloat());
281 return;
Nate Begeman1e31b162009-01-18 01:01:34 +0000282 case Vector:
Richard Smithf6f003a2011-12-16 19:06:07 +0000283 OS << "Vector: ";
284 getVectorElt(0).dump(OS);
285 for (unsigned i = 1; i != getVectorLength(); ++i) {
286 OS << ", ";
287 getVectorElt(i).dump(OS);
288 }
Nate Begeman1e31b162009-01-18 01:01:34 +0000289 return;
Chris Lattner981f33b2008-11-16 07:46:48 +0000290 case ComplexInt:
291 OS << "ComplexInt: " << getComplexIntReal() << ", " << getComplexIntImag();
292 return;
293 case ComplexFloat:
294 OS << "ComplexFloat: " << GetApproxValue(getComplexFloatReal())
295 << ", " << GetApproxValue(getComplexFloatImag());
Richard Smithf3e9e432011-11-07 09:22:26 +0000296 return;
Chris Lattner981f33b2008-11-16 07:46:48 +0000297 case LValue:
298 OS << "LValue: <todo>";
299 return;
Richard Smithf3e9e432011-11-07 09:22:26 +0000300 case Array:
301 OS << "Array: ";
302 for (unsigned I = 0, N = getArrayInitializedElts(); I != N; ++I) {
Richard Smithf6f003a2011-12-16 19:06:07 +0000303 getArrayInitializedElt(I).dump(OS);
Richard Smithf3e9e432011-11-07 09:22:26 +0000304 if (I != getArraySize() - 1) OS << ", ";
305 }
Richard Smithf6f003a2011-12-16 19:06:07 +0000306 if (hasArrayFiller()) {
307 OS << getArraySize() - getArrayInitializedElts() << " x ";
308 getArrayFiller().dump(OS);
309 }
Richard Smithf3e9e432011-11-07 09:22:26 +0000310 return;
Richard Smithd62306a2011-11-10 06:34:14 +0000311 case Struct:
312 OS << "Struct ";
313 if (unsigned N = getStructNumBases()) {
Richard Smithf6f003a2011-12-16 19:06:07 +0000314 OS << " bases: ";
315 getStructBase(0).dump(OS);
316 for (unsigned I = 1; I != N; ++I) {
317 OS << ", ";
318 getStructBase(I).dump(OS);
319 }
Richard Smithd62306a2011-11-10 06:34:14 +0000320 }
321 if (unsigned N = getStructNumFields()) {
Richard Smithf6f003a2011-12-16 19:06:07 +0000322 OS << " fields: ";
323 getStructField(0).dump(OS);
324 for (unsigned I = 1; I != N; ++I) {
325 OS << ", ";
326 getStructField(I).dump(OS);
327 }
Richard Smithd62306a2011-11-10 06:34:14 +0000328 }
329 return;
330 case Union:
Richard Smithf6f003a2011-12-16 19:06:07 +0000331 OS << "Union: ";
332 getUnionValue().dump(OS);
Richard Smithd62306a2011-11-10 06:34:14 +0000333 return;
Richard Smith027bf112011-11-17 22:56:20 +0000334 case MemberPointer:
335 OS << "MemberPointer: <todo>";
336 return;
Eli Friedmanfd5e54d2012-01-04 23:13:47 +0000337 case AddrLabelDiff:
338 OS << "AddrLabelDiff: <todo>";
339 return;
Chris Lattner981f33b2008-11-16 07:46:48 +0000340 }
Richard Smithd62306a2011-11-10 06:34:14 +0000341 llvm_unreachable("Unknown APValue kind!");
Chris Lattner981f33b2008-11-16 07:46:48 +0000342}
343
Richard Smithf6f003a2011-12-16 19:06:07 +0000344void APValue::printPretty(raw_ostream &Out, ASTContext &Ctx, QualType Ty) const{
345 switch (getKind()) {
Jeffrey Yasskind2af9622011-07-18 16:43:53 +0000346 case APValue::Uninitialized:
Richard Smithf6f003a2011-12-16 19:06:07 +0000347 Out << "<uninitialized>";
Richard Smithd62306a2011-11-10 06:34:14 +0000348 return;
Jeffrey Yasskind2af9622011-07-18 16:43:53 +0000349 case APValue::Int:
Richard Smith5614ca72012-03-23 23:55:39 +0000350 if (Ty->isBooleanType())
351 Out << (getInt().getBoolValue() ? "true" : "false");
352 else
353 Out << getInt();
Richard Smithd62306a2011-11-10 06:34:14 +0000354 return;
Jeffrey Yasskind2af9622011-07-18 16:43:53 +0000355 case APValue::Float:
Richard Smithf6f003a2011-12-16 19:06:07 +0000356 Out << GetApproxValue(getFloat());
Richard Smithd62306a2011-11-10 06:34:14 +0000357 return;
Richard Smithf6f003a2011-12-16 19:06:07 +0000358 case APValue::Vector: {
359 Out << '{';
360 QualType ElemTy = Ty->getAs<VectorType>()->getElementType();
361 getVectorElt(0).printPretty(Out, Ctx, ElemTy);
362 for (unsigned i = 1; i != getVectorLength(); ++i) {
Jeffrey Yasskind2af9622011-07-18 16:43:53 +0000363 Out << ", ";
Richard Smithf6f003a2011-12-16 19:06:07 +0000364 getVectorElt(i).printPretty(Out, Ctx, ElemTy);
Jeffrey Yasskind2af9622011-07-18 16:43:53 +0000365 }
Richard Smithf6f003a2011-12-16 19:06:07 +0000366 Out << '}';
Richard Smithd62306a2011-11-10 06:34:14 +0000367 return;
Richard Smithf6f003a2011-12-16 19:06:07 +0000368 }
Jeffrey Yasskind2af9622011-07-18 16:43:53 +0000369 case APValue::ComplexInt:
Richard Smithf6f003a2011-12-16 19:06:07 +0000370 Out << getComplexIntReal() << "+" << getComplexIntImag() << "i";
Richard Smithd62306a2011-11-10 06:34:14 +0000371 return;
Jeffrey Yasskind2af9622011-07-18 16:43:53 +0000372 case APValue::ComplexFloat:
Richard Smithf6f003a2011-12-16 19:06:07 +0000373 Out << GetApproxValue(getComplexFloatReal()) << "+"
374 << GetApproxValue(getComplexFloatImag()) << "i";
Richard Smithd62306a2011-11-10 06:34:14 +0000375 return;
Richard Smithf6f003a2011-12-16 19:06:07 +0000376 case APValue::LValue: {
377 LValueBase Base = getLValueBase();
378 if (!Base) {
379 Out << "0";
380 return;
Richard Smithf3e9e432011-11-07 09:22:26 +0000381 }
Richard Smithf6f003a2011-12-16 19:06:07 +0000382
383 bool IsReference = Ty->isReferenceType();
384 QualType InnerTy
385 = IsReference ? Ty.getNonReferenceType() : Ty->getPointeeType();
Douglas Gregor0b7bc7f2013-01-29 01:26:43 +0000386 if (InnerTy.isNull())
387 InnerTy = Ty;
Richard Smithf6f003a2011-12-16 19:06:07 +0000388
389 if (!hasLValuePath()) {
390 // No lvalue path: just print the offset.
391 CharUnits O = getLValueOffset();
392 CharUnits S = Ctx.getTypeSizeInChars(InnerTy);
393 if (!O.isZero()) {
394 if (IsReference)
395 Out << "*(";
396 if (O % S) {
397 Out << "(char*)";
398 S = CharUnits::One();
399 }
400 Out << '&';
401 } else if (!IsReference)
402 Out << '&';
403
404 if (const ValueDecl *VD = Base.dyn_cast<const ValueDecl*>())
405 Out << *VD;
Richard Trieuddd01ce2014-06-09 22:53:25 +0000406 else {
407 assert(Base.get<const Expr *>() != nullptr &&
408 "Expecting non-null Expr");
Craig Topper36250ad2014-05-12 05:36:57 +0000409 Base.get<const Expr*>()->printPretty(Out, nullptr,
410 Ctx.getPrintingPolicy());
Richard Trieuddd01ce2014-06-09 22:53:25 +0000411 }
412
Richard Smithf6f003a2011-12-16 19:06:07 +0000413 if (!O.isZero()) {
414 Out << " + " << (O / S);
415 if (IsReference)
416 Out << ')';
417 }
418 return;
419 }
420
421 // We have an lvalue path. Print it out nicely.
422 if (!IsReference)
423 Out << '&';
424 else if (isLValueOnePastTheEnd())
425 Out << "*(&";
426
427 QualType ElemTy;
428 if (const ValueDecl *VD = Base.dyn_cast<const ValueDecl*>()) {
429 Out << *VD;
430 ElemTy = VD->getType();
431 } else {
432 const Expr *E = Base.get<const Expr*>();
Richard Trieuddd01ce2014-06-09 22:53:25 +0000433 assert(E != nullptr && "Expecting non-null Expr");
Craig Topper36250ad2014-05-12 05:36:57 +0000434 E->printPretty(Out, nullptr, Ctx.getPrintingPolicy());
Richard Smithf6f003a2011-12-16 19:06:07 +0000435 ElemTy = E->getType();
436 }
437
438 ArrayRef<LValuePathEntry> Path = getLValuePath();
Craig Topper36250ad2014-05-12 05:36:57 +0000439 const CXXRecordDecl *CastToBase = nullptr;
Richard Smithf6f003a2011-12-16 19:06:07 +0000440 for (unsigned I = 0, N = Path.size(); I != N; ++I) {
441 if (ElemTy->getAs<RecordType>()) {
442 // The lvalue refers to a class type, so the next path entry is a base
443 // or member.
444 const Decl *BaseOrMember =
445 BaseOrMemberType::getFromOpaqueValue(Path[I].BaseOrMember).getPointer();
446 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(BaseOrMember)) {
447 CastToBase = RD;
448 ElemTy = Ctx.getRecordType(RD);
449 } else {
450 const ValueDecl *VD = cast<ValueDecl>(BaseOrMember);
451 Out << ".";
452 if (CastToBase)
453 Out << *CastToBase << "::";
454 Out << *VD;
455 ElemTy = VD->getType();
456 }
457 } else {
458 // The lvalue must refer to an array.
459 Out << '[' << Path[I].ArrayIndex << ']';
460 ElemTy = Ctx.getAsArrayType(ElemTy)->getElementType();
461 }
462 }
463
464 // Handle formatting of one-past-the-end lvalues.
465 if (isLValueOnePastTheEnd()) {
466 // FIXME: If CastToBase is non-0, we should prefix the output with
467 // "(CastToBase*)".
468 Out << " + 1";
469 if (IsReference)
470 Out << ')';
471 }
Richard Smithd62306a2011-11-10 06:34:14 +0000472 return;
Richard Smithf6f003a2011-12-16 19:06:07 +0000473 }
474 case APValue::Array: {
475 const ArrayType *AT = Ctx.getAsArrayType(Ty);
476 QualType ElemTy = AT->getElementType();
Richard Smithd62306a2011-11-10 06:34:14 +0000477 Out << '{';
Richard Smithf6f003a2011-12-16 19:06:07 +0000478 if (unsigned N = getArrayInitializedElts()) {
479 getArrayInitializedElt(0).printPretty(Out, Ctx, ElemTy);
480 for (unsigned I = 1; I != N; ++I) {
Richard Smithd62306a2011-11-10 06:34:14 +0000481 Out << ", ";
Richard Smithf6f003a2011-12-16 19:06:07 +0000482 if (I == 10) {
483 // Avoid printing out the entire contents of large arrays.
484 Out << "...";
485 break;
486 }
487 getArrayInitializedElt(I).printPretty(Out, Ctx, ElemTy);
488 }
Richard Smithd62306a2011-11-10 06:34:14 +0000489 }
490 Out << '}';
491 return;
Richard Smithf6f003a2011-12-16 19:06:07 +0000492 }
493 case APValue::Struct: {
494 Out << '{';
495 const RecordDecl *RD = Ty->getAs<RecordType>()->getDecl();
496 bool First = true;
497 if (unsigned N = getStructNumBases()) {
498 const CXXRecordDecl *CD = cast<CXXRecordDecl>(RD);
499 CXXRecordDecl::base_class_const_iterator BI = CD->bases_begin();
500 for (unsigned I = 0; I != N; ++I, ++BI) {
501 assert(BI != CD->bases_end());
502 if (!First)
503 Out << ", ";
504 getStructBase(I).printPretty(Out, Ctx, BI->getType());
505 First = false;
506 }
507 }
Aaron Ballmane8a8bae2014-03-08 20:12:42 +0000508 for (const auto *FI : RD->fields()) {
Richard Smithf6f003a2011-12-16 19:06:07 +0000509 if (!First)
510 Out << ", ";
David Blaikie2d7c57e2012-04-30 02:36:29 +0000511 if (FI->isUnnamedBitfield()) continue;
512 getStructField(FI->getFieldIndex()).
513 printPretty(Out, Ctx, FI->getType());
Richard Smithf6f003a2011-12-16 19:06:07 +0000514 First = false;
515 }
516 Out << '}';
517 return;
518 }
Richard Smithd62306a2011-11-10 06:34:14 +0000519 case APValue::Union:
Richard Smithf6f003a2011-12-16 19:06:07 +0000520 Out << '{';
521 if (const FieldDecl *FD = getUnionField()) {
522 Out << "." << *FD << " = ";
523 getUnionValue().printPretty(Out, Ctx, FD->getType());
524 }
525 Out << '}';
Richard Smithd62306a2011-11-10 06:34:14 +0000526 return;
Richard Smith027bf112011-11-17 22:56:20 +0000527 case APValue::MemberPointer:
Richard Smithf6f003a2011-12-16 19:06:07 +0000528 // FIXME: This is not enough to unambiguously identify the member in a
529 // multiple-inheritance scenario.
530 if (const ValueDecl *VD = getMemberPointerDecl()) {
531 Out << '&' << *cast<CXXRecordDecl>(VD->getDeclContext()) << "::" << *VD;
532 return;
533 }
534 Out << "0";
Richard Smith027bf112011-11-17 22:56:20 +0000535 return;
Eli Friedmanfd5e54d2012-01-04 23:13:47 +0000536 case APValue::AddrLabelDiff:
537 Out << "&&" << getAddrLabelDiffLHS()->getLabel()->getName();
538 Out << " - ";
539 Out << "&&" << getAddrLabelDiffRHS()->getLabel()->getName();
540 return;
Jeffrey Yasskind2af9622011-07-18 16:43:53 +0000541 }
Richard Smithd62306a2011-11-10 06:34:14 +0000542 llvm_unreachable("Unknown APValue kind!");
Jeffrey Yasskind2af9622011-07-18 16:43:53 +0000543}
544
Richard Smithf6f003a2011-12-16 19:06:07 +0000545std::string APValue::getAsString(ASTContext &Ctx, QualType Ty) const {
546 std::string Result;
547 llvm::raw_string_ostream Out(Result);
548 printPretty(Out, Ctx, Ty);
Eli Friedman375f09f2011-12-16 22:12:23 +0000549 Out.flush();
Richard Smithf6f003a2011-12-16 19:06:07 +0000550 return Result;
Jeffrey Yasskind2af9622011-07-18 16:43:53 +0000551}
552
Richard Smithce40ad62011-11-12 22:28:03 +0000553const APValue::LValueBase APValue::getLValueBase() const {
Ken Dyck02990832010-01-15 12:37:54 +0000554 assert(isLValue() && "Invalid accessor");
Richard Smith7073a2d2014-01-10 00:40:45 +0000555 return ((const LV*)(const void*)Data.buffer)->BaseAndIsOnePastTheEnd.getPointer();
Richard Smith027bf112011-11-17 22:56:20 +0000556}
557
558bool APValue::isLValueOnePastTheEnd() const {
559 assert(isLValue() && "Invalid accessor");
Richard Smith7073a2d2014-01-10 00:40:45 +0000560 return ((const LV*)(const void*)Data.buffer)->BaseAndIsOnePastTheEnd.getInt();
Ken Dyck02990832010-01-15 12:37:54 +0000561}
562
Richard Smith0b0a0b62011-10-29 20:57:55 +0000563CharUnits &APValue::getLValueOffset() {
564 assert(isLValue() && "Invalid accessor");
Richard Smith7073a2d2014-01-10 00:40:45 +0000565 return ((LV*)(void*)Data.buffer)->Offset;
Ken Dyck02990832010-01-15 12:37:54 +0000566}
567
Richard Smith80815602011-11-07 05:07:52 +0000568bool APValue::hasLValuePath() const {
Ken Dyck02990832010-01-15 12:37:54 +0000569 assert(isLValue() && "Invalid accessor");
Richard Smith7073a2d2014-01-10 00:40:45 +0000570 return ((const LV*)(const char*)Data.buffer)->hasPath();
Richard Smith80815602011-11-07 05:07:52 +0000571}
572
573ArrayRef<APValue::LValuePathEntry> APValue::getLValuePath() const {
574 assert(isLValue() && hasLValuePath() && "Invalid accessor");
Richard Smith7073a2d2014-01-10 00:40:45 +0000575 const LV &LVal = *((const LV*)(const char*)Data.buffer);
Craig Topper5fc8fc22014-08-27 06:28:36 +0000576 return llvm::makeArrayRef(LVal.getPath(), LVal.PathLength);
Richard Smith80815602011-11-07 05:07:52 +0000577}
578
Richard Smithb228a862012-02-15 02:18:13 +0000579unsigned APValue::getLValueCallIndex() const {
580 assert(isLValue() && "Invalid accessor");
Richard Smith7073a2d2014-01-10 00:40:45 +0000581 return ((const LV*)(const char*)Data.buffer)->CallIndex;
Richard Smithb228a862012-02-15 02:18:13 +0000582}
583
584void APValue::setLValue(LValueBase B, const CharUnits &O, NoLValuePath,
585 unsigned CallIndex) {
Richard Smith80815602011-11-07 05:07:52 +0000586 assert(isLValue() && "Invalid accessor");
Richard Smith7073a2d2014-01-10 00:40:45 +0000587 LV &LVal = *((LV*)(char*)Data.buffer);
Richard Smith027bf112011-11-17 22:56:20 +0000588 LVal.BaseAndIsOnePastTheEnd.setPointer(B);
589 LVal.BaseAndIsOnePastTheEnd.setInt(false);
Richard Smith80815602011-11-07 05:07:52 +0000590 LVal.Offset = O;
Richard Smithb228a862012-02-15 02:18:13 +0000591 LVal.CallIndex = CallIndex;
Richard Smith027bf112011-11-17 22:56:20 +0000592 LVal.resizePath((unsigned)-1);
Richard Smith80815602011-11-07 05:07:52 +0000593}
594
Richard Smithce40ad62011-11-12 22:28:03 +0000595void APValue::setLValue(LValueBase B, const CharUnits &O,
Richard Smithb228a862012-02-15 02:18:13 +0000596 ArrayRef<LValuePathEntry> Path, bool IsOnePastTheEnd,
597 unsigned CallIndex) {
Richard Smith80815602011-11-07 05:07:52 +0000598 assert(isLValue() && "Invalid accessor");
Richard Smith7073a2d2014-01-10 00:40:45 +0000599 LV &LVal = *((LV*)(char*)Data.buffer);
Richard Smith027bf112011-11-17 22:56:20 +0000600 LVal.BaseAndIsOnePastTheEnd.setPointer(B);
601 LVal.BaseAndIsOnePastTheEnd.setInt(IsOnePastTheEnd);
Richard Smith80815602011-11-07 05:07:52 +0000602 LVal.Offset = O;
Richard Smithb228a862012-02-15 02:18:13 +0000603 LVal.CallIndex = CallIndex;
Richard Smith027bf112011-11-17 22:56:20 +0000604 LVal.resizePath(Path.size());
Richard Smith80815602011-11-07 05:07:52 +0000605 memcpy(LVal.getPath(), Path.data(), Path.size() * sizeof(LValuePathEntry));
Ken Dyck02990832010-01-15 12:37:54 +0000606}
607
Richard Smith027bf112011-11-17 22:56:20 +0000608const ValueDecl *APValue::getMemberPointerDecl() const {
609 assert(isMemberPointer() && "Invalid accessor");
Richard Smith7073a2d2014-01-10 00:40:45 +0000610 const MemberPointerData &MPD =
611 *((const MemberPointerData *)(const char *)Data.buffer);
Richard Smith027bf112011-11-17 22:56:20 +0000612 return MPD.MemberAndIsDerivedMember.getPointer();
613}
614
615bool APValue::isMemberPointerToDerivedMember() const {
616 assert(isMemberPointer() && "Invalid accessor");
Richard Smith7073a2d2014-01-10 00:40:45 +0000617 const MemberPointerData &MPD =
618 *((const MemberPointerData *)(const char *)Data.buffer);
Richard Smith027bf112011-11-17 22:56:20 +0000619 return MPD.MemberAndIsDerivedMember.getInt();
620}
621
622ArrayRef<const CXXRecordDecl*> APValue::getMemberPointerPath() const {
623 assert(isMemberPointer() && "Invalid accessor");
Richard Smith7073a2d2014-01-10 00:40:45 +0000624 const MemberPointerData &MPD =
625 *((const MemberPointerData *)(const char *)Data.buffer);
Craig Topper5fc8fc22014-08-27 06:28:36 +0000626 return llvm::makeArrayRef(MPD.getPath(), MPD.PathLength);
Richard Smith027bf112011-11-17 22:56:20 +0000627}
628
Ken Dyck02990832010-01-15 12:37:54 +0000629void APValue::MakeLValue() {
630 assert(isUninit() && "Bad state change");
Benjamin Kramera939c232014-03-15 18:54:13 +0000631 static_assert(sizeof(LV) <= DataSize, "LV too big");
Richard Smith7073a2d2014-01-10 00:40:45 +0000632 new ((void*)(char*)Data.buffer) LV();
Ken Dyck02990832010-01-15 12:37:54 +0000633 Kind = LValue;
634}
Richard Smithf3e9e432011-11-07 09:22:26 +0000635
636void APValue::MakeArray(unsigned InitElts, unsigned Size) {
637 assert(isUninit() && "Bad state change");
Richard Smith7073a2d2014-01-10 00:40:45 +0000638 new ((void*)(char*)Data.buffer) Arr(InitElts, Size);
Richard Smithf3e9e432011-11-07 09:22:26 +0000639 Kind = Array;
640}
Richard Smith027bf112011-11-17 22:56:20 +0000641
642void APValue::MakeMemberPointer(const ValueDecl *Member, bool IsDerivedMember,
643 ArrayRef<const CXXRecordDecl*> Path) {
644 assert(isUninit() && "Bad state change");
Richard Smith7073a2d2014-01-10 00:40:45 +0000645 MemberPointerData *MPD = new ((void*)(char*)Data.buffer) MemberPointerData;
Richard Smith027bf112011-11-17 22:56:20 +0000646 Kind = MemberPointer;
647 MPD->MemberAndIsDerivedMember.setPointer(Member);
648 MPD->MemberAndIsDerivedMember.setInt(IsDerivedMember);
649 MPD->resizePath(Path.size());
650 memcpy(MPD->getPath(), Path.data(), Path.size()*sizeof(const CXXRecordDecl*));
651}