blob: 541836b21b70c71bd5b2b139358090d3370cf842 [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 =
37 (MaxSize - sizeof(LVBase)) / sizeof(LValuePathEntry);
38
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 =
78 (MaxSize - sizeof(MemberPointerBase)) / sizeof(const CXXRecordDecl*);
79 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
120APValue::UnionData::UnionData() : Field(0), Value(new APValue) {}
121APValue::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();
Dan Gohman145f3f12010-04-19 16:39:44 +0000139 setVector(((const Vec *)(const char *)RHS.Data)->Elts,
140 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)
Douglas Gregor5b5559b2009-09-08 19:57:33 +0000191 ((APSInt*)(char*)Data)->~APSInt();
Chris Lattner981f33b2008-11-16 07:46:48 +0000192 else if (Kind == Float)
Douglas Gregor5b5559b2009-09-08 19:57:33 +0000193 ((APFloat*)(char*)Data)->~APFloat();
Nate Begeman1e31b162009-01-18 01:01:34 +0000194 else if (Kind == Vector)
Douglas Gregor5b5559b2009-09-08 19:57:33 +0000195 ((Vec*)(char*)Data)->~Vec();
Chris Lattner981f33b2008-11-16 07:46:48 +0000196 else if (Kind == ComplexInt)
Douglas Gregor5b5559b2009-09-08 19:57:33 +0000197 ((ComplexAPSInt*)(char*)Data)->~ComplexAPSInt();
Chris Lattner981f33b2008-11-16 07:46:48 +0000198 else if (Kind == ComplexFloat)
Douglas Gregor5b5559b2009-09-08 19:57:33 +0000199 ((ComplexAPFloat*)(char*)Data)->~ComplexAPFloat();
Richard Smithf3e9e432011-11-07 09:22:26 +0000200 else if (Kind == LValue)
Douglas Gregor5b5559b2009-09-08 19:57:33 +0000201 ((LV*)(char*)Data)->~LV();
Richard Smithf3e9e432011-11-07 09:22:26 +0000202 else if (Kind == Array)
203 ((Arr*)(char*)Data)->~Arr();
Richard Smithd62306a2011-11-10 06:34:14 +0000204 else if (Kind == Struct)
205 ((StructData*)(char*)Data)->~StructData();
206 else if (Kind == Union)
207 ((UnionData*)(char*)Data)->~UnionData();
Richard Smith027bf112011-11-17 22:56:20 +0000208 else if (Kind == MemberPointer)
209 ((MemberPointerData*)(char*)Data)->~MemberPointerData();
Eli Friedmanfd5e54d2012-01-04 23:13:47 +0000210 else if (Kind == AddrLabelDiff)
211 ((AddrLabelDiffData*)(char*)Data)->~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:
242 return reinterpret_cast<const LV *>(Data)->hasPathPtr();
243 case MemberPointer:
244 return reinterpret_cast<const MemberPointerData *>(Data)->hasPathPtr();
245 }
Benjamin Kramerd1b7cd72013-06-03 21:26:13 +0000246 llvm_unreachable("Unknown APValue kind!");
Manuel Klimeka7328992013-06-03 13:51:33 +0000247}
248
Richard Smith4e9e5232012-03-10 00:28:11 +0000249void APValue::swap(APValue &RHS) {
250 std::swap(Kind, RHS.Kind);
251 char TmpData[MaxSize];
252 memcpy(TmpData, Data, MaxSize);
253 memcpy(Data, RHS.Data, MaxSize);
254 memcpy(RHS.Data, TmpData, MaxSize);
255}
256
Chris Lattner981f33b2008-11-16 07:46:48 +0000257void APValue::dump() const {
Richard Smithf6f003a2011-12-16 19:06:07 +0000258 dump(llvm::errs());
Chris Lattner981f33b2008-11-16 07:46:48 +0000259 llvm::errs() << '\n';
Chris Lattner981f33b2008-11-16 07:46:48 +0000260}
261
262static double GetApproxValue(const llvm::APFloat &F) {
263 llvm::APFloat V = F;
264 bool ignored;
265 V.convert(llvm::APFloat::IEEEdouble, llvm::APFloat::rmNearestTiesToEven,
266 &ignored);
267 return V.convertToDouble();
268}
269
Richard Smithf6f003a2011-12-16 19:06:07 +0000270void APValue::dump(raw_ostream &OS) const {
Chris Lattner981f33b2008-11-16 07:46:48 +0000271 switch (getKind()) {
Chris Lattner981f33b2008-11-16 07:46:48 +0000272 case Uninitialized:
273 OS << "Uninitialized";
274 return;
275 case Int:
276 OS << "Int: " << getInt();
277 return;
278 case Float:
279 OS << "Float: " << GetApproxValue(getFloat());
280 return;
Nate Begeman1e31b162009-01-18 01:01:34 +0000281 case Vector:
Richard Smithf6f003a2011-12-16 19:06:07 +0000282 OS << "Vector: ";
283 getVectorElt(0).dump(OS);
284 for (unsigned i = 1; i != getVectorLength(); ++i) {
285 OS << ", ";
286 getVectorElt(i).dump(OS);
287 }
Nate Begeman1e31b162009-01-18 01:01:34 +0000288 return;
Chris Lattner981f33b2008-11-16 07:46:48 +0000289 case ComplexInt:
290 OS << "ComplexInt: " << getComplexIntReal() << ", " << getComplexIntImag();
291 return;
292 case ComplexFloat:
293 OS << "ComplexFloat: " << GetApproxValue(getComplexFloatReal())
294 << ", " << GetApproxValue(getComplexFloatImag());
Richard Smithf3e9e432011-11-07 09:22:26 +0000295 return;
Chris Lattner981f33b2008-11-16 07:46:48 +0000296 case LValue:
297 OS << "LValue: <todo>";
298 return;
Richard Smithf3e9e432011-11-07 09:22:26 +0000299 case Array:
300 OS << "Array: ";
301 for (unsigned I = 0, N = getArrayInitializedElts(); I != N; ++I) {
Richard Smithf6f003a2011-12-16 19:06:07 +0000302 getArrayInitializedElt(I).dump(OS);
Richard Smithf3e9e432011-11-07 09:22:26 +0000303 if (I != getArraySize() - 1) OS << ", ";
304 }
Richard Smithf6f003a2011-12-16 19:06:07 +0000305 if (hasArrayFiller()) {
306 OS << getArraySize() - getArrayInitializedElts() << " x ";
307 getArrayFiller().dump(OS);
308 }
Richard Smithf3e9e432011-11-07 09:22:26 +0000309 return;
Richard Smithd62306a2011-11-10 06:34:14 +0000310 case Struct:
311 OS << "Struct ";
312 if (unsigned N = getStructNumBases()) {
Richard Smithf6f003a2011-12-16 19:06:07 +0000313 OS << " bases: ";
314 getStructBase(0).dump(OS);
315 for (unsigned I = 1; I != N; ++I) {
316 OS << ", ";
317 getStructBase(I).dump(OS);
318 }
Richard Smithd62306a2011-11-10 06:34:14 +0000319 }
320 if (unsigned N = getStructNumFields()) {
Richard Smithf6f003a2011-12-16 19:06:07 +0000321 OS << " fields: ";
322 getStructField(0).dump(OS);
323 for (unsigned I = 1; I != N; ++I) {
324 OS << ", ";
325 getStructField(I).dump(OS);
326 }
Richard Smithd62306a2011-11-10 06:34:14 +0000327 }
328 return;
329 case Union:
Richard Smithf6f003a2011-12-16 19:06:07 +0000330 OS << "Union: ";
331 getUnionValue().dump(OS);
Richard Smithd62306a2011-11-10 06:34:14 +0000332 return;
Richard Smith027bf112011-11-17 22:56:20 +0000333 case MemberPointer:
334 OS << "MemberPointer: <todo>";
335 return;
Eli Friedmanfd5e54d2012-01-04 23:13:47 +0000336 case AddrLabelDiff:
337 OS << "AddrLabelDiff: <todo>";
338 return;
Chris Lattner981f33b2008-11-16 07:46:48 +0000339 }
Richard Smithd62306a2011-11-10 06:34:14 +0000340 llvm_unreachable("Unknown APValue kind!");
Chris Lattner981f33b2008-11-16 07:46:48 +0000341}
342
Richard Smithf6f003a2011-12-16 19:06:07 +0000343void APValue::printPretty(raw_ostream &Out, ASTContext &Ctx, QualType Ty) const{
344 switch (getKind()) {
Jeffrey Yasskind2af9622011-07-18 16:43:53 +0000345 case APValue::Uninitialized:
Richard Smithf6f003a2011-12-16 19:06:07 +0000346 Out << "<uninitialized>";
Richard Smithd62306a2011-11-10 06:34:14 +0000347 return;
Jeffrey Yasskind2af9622011-07-18 16:43:53 +0000348 case APValue::Int:
Richard Smith5614ca72012-03-23 23:55:39 +0000349 if (Ty->isBooleanType())
350 Out << (getInt().getBoolValue() ? "true" : "false");
351 else
352 Out << getInt();
Richard Smithd62306a2011-11-10 06:34:14 +0000353 return;
Jeffrey Yasskind2af9622011-07-18 16:43:53 +0000354 case APValue::Float:
Richard Smithf6f003a2011-12-16 19:06:07 +0000355 Out << GetApproxValue(getFloat());
Richard Smithd62306a2011-11-10 06:34:14 +0000356 return;
Richard Smithf6f003a2011-12-16 19:06:07 +0000357 case APValue::Vector: {
358 Out << '{';
359 QualType ElemTy = Ty->getAs<VectorType>()->getElementType();
360 getVectorElt(0).printPretty(Out, Ctx, ElemTy);
361 for (unsigned i = 1; i != getVectorLength(); ++i) {
Jeffrey Yasskind2af9622011-07-18 16:43:53 +0000362 Out << ", ";
Richard Smithf6f003a2011-12-16 19:06:07 +0000363 getVectorElt(i).printPretty(Out, Ctx, ElemTy);
Jeffrey Yasskind2af9622011-07-18 16:43:53 +0000364 }
Richard Smithf6f003a2011-12-16 19:06:07 +0000365 Out << '}';
Richard Smithd62306a2011-11-10 06:34:14 +0000366 return;
Richard Smithf6f003a2011-12-16 19:06:07 +0000367 }
Jeffrey Yasskind2af9622011-07-18 16:43:53 +0000368 case APValue::ComplexInt:
Richard Smithf6f003a2011-12-16 19:06:07 +0000369 Out << getComplexIntReal() << "+" << getComplexIntImag() << "i";
Richard Smithd62306a2011-11-10 06:34:14 +0000370 return;
Jeffrey Yasskind2af9622011-07-18 16:43:53 +0000371 case APValue::ComplexFloat:
Richard Smithf6f003a2011-12-16 19:06:07 +0000372 Out << GetApproxValue(getComplexFloatReal()) << "+"
373 << GetApproxValue(getComplexFloatImag()) << "i";
Richard Smithd62306a2011-11-10 06:34:14 +0000374 return;
Richard Smithf6f003a2011-12-16 19:06:07 +0000375 case APValue::LValue: {
376 LValueBase Base = getLValueBase();
377 if (!Base) {
378 Out << "0";
379 return;
Richard Smithf3e9e432011-11-07 09:22:26 +0000380 }
Richard Smithf6f003a2011-12-16 19:06:07 +0000381
382 bool IsReference = Ty->isReferenceType();
383 QualType InnerTy
384 = IsReference ? Ty.getNonReferenceType() : Ty->getPointeeType();
Douglas Gregor0b7bc7f2013-01-29 01:26:43 +0000385 if (InnerTy.isNull())
386 InnerTy = Ty;
Richard Smithf6f003a2011-12-16 19:06:07 +0000387
388 if (!hasLValuePath()) {
389 // No lvalue path: just print the offset.
390 CharUnits O = getLValueOffset();
391 CharUnits S = Ctx.getTypeSizeInChars(InnerTy);
392 if (!O.isZero()) {
393 if (IsReference)
394 Out << "*(";
395 if (O % S) {
396 Out << "(char*)";
397 S = CharUnits::One();
398 }
399 Out << '&';
400 } else if (!IsReference)
401 Out << '&';
402
403 if (const ValueDecl *VD = Base.dyn_cast<const ValueDecl*>())
404 Out << *VD;
405 else
Richard Smith235341b2012-08-16 03:56:14 +0000406 Base.get<const Expr*>()->printPretty(Out, 0, Ctx.getPrintingPolicy());
Richard Smithf6f003a2011-12-16 19:06:07 +0000407 if (!O.isZero()) {
408 Out << " + " << (O / S);
409 if (IsReference)
410 Out << ')';
411 }
412 return;
413 }
414
415 // We have an lvalue path. Print it out nicely.
416 if (!IsReference)
417 Out << '&';
418 else if (isLValueOnePastTheEnd())
419 Out << "*(&";
420
421 QualType ElemTy;
422 if (const ValueDecl *VD = Base.dyn_cast<const ValueDecl*>()) {
423 Out << *VD;
424 ElemTy = VD->getType();
425 } else {
426 const Expr *E = Base.get<const Expr*>();
Richard Smith235341b2012-08-16 03:56:14 +0000427 E->printPretty(Out, 0, Ctx.getPrintingPolicy());
Richard Smithf6f003a2011-12-16 19:06:07 +0000428 ElemTy = E->getType();
429 }
430
431 ArrayRef<LValuePathEntry> Path = getLValuePath();
432 const CXXRecordDecl *CastToBase = 0;
433 for (unsigned I = 0, N = Path.size(); I != N; ++I) {
434 if (ElemTy->getAs<RecordType>()) {
435 // The lvalue refers to a class type, so the next path entry is a base
436 // or member.
437 const Decl *BaseOrMember =
438 BaseOrMemberType::getFromOpaqueValue(Path[I].BaseOrMember).getPointer();
439 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(BaseOrMember)) {
440 CastToBase = RD;
441 ElemTy = Ctx.getRecordType(RD);
442 } else {
443 const ValueDecl *VD = cast<ValueDecl>(BaseOrMember);
444 Out << ".";
445 if (CastToBase)
446 Out << *CastToBase << "::";
447 Out << *VD;
448 ElemTy = VD->getType();
449 }
450 } else {
451 // The lvalue must refer to an array.
452 Out << '[' << Path[I].ArrayIndex << ']';
453 ElemTy = Ctx.getAsArrayType(ElemTy)->getElementType();
454 }
455 }
456
457 // Handle formatting of one-past-the-end lvalues.
458 if (isLValueOnePastTheEnd()) {
459 // FIXME: If CastToBase is non-0, we should prefix the output with
460 // "(CastToBase*)".
461 Out << " + 1";
462 if (IsReference)
463 Out << ')';
464 }
Richard Smithd62306a2011-11-10 06:34:14 +0000465 return;
Richard Smithf6f003a2011-12-16 19:06:07 +0000466 }
467 case APValue::Array: {
468 const ArrayType *AT = Ctx.getAsArrayType(Ty);
469 QualType ElemTy = AT->getElementType();
Richard Smithd62306a2011-11-10 06:34:14 +0000470 Out << '{';
Richard Smithf6f003a2011-12-16 19:06:07 +0000471 if (unsigned N = getArrayInitializedElts()) {
472 getArrayInitializedElt(0).printPretty(Out, Ctx, ElemTy);
473 for (unsigned I = 1; I != N; ++I) {
Richard Smithd62306a2011-11-10 06:34:14 +0000474 Out << ", ";
Richard Smithf6f003a2011-12-16 19:06:07 +0000475 if (I == 10) {
476 // Avoid printing out the entire contents of large arrays.
477 Out << "...";
478 break;
479 }
480 getArrayInitializedElt(I).printPretty(Out, Ctx, ElemTy);
481 }
Richard Smithd62306a2011-11-10 06:34:14 +0000482 }
483 Out << '}';
484 return;
Richard Smithf6f003a2011-12-16 19:06:07 +0000485 }
486 case APValue::Struct: {
487 Out << '{';
488 const RecordDecl *RD = Ty->getAs<RecordType>()->getDecl();
489 bool First = true;
490 if (unsigned N = getStructNumBases()) {
491 const CXXRecordDecl *CD = cast<CXXRecordDecl>(RD);
492 CXXRecordDecl::base_class_const_iterator BI = CD->bases_begin();
493 for (unsigned I = 0; I != N; ++I, ++BI) {
494 assert(BI != CD->bases_end());
495 if (!First)
496 Out << ", ";
497 getStructBase(I).printPretty(Out, Ctx, BI->getType());
498 First = false;
499 }
500 }
501 for (RecordDecl::field_iterator FI = RD->field_begin();
502 FI != RD->field_end(); ++FI) {
503 if (!First)
504 Out << ", ";
David Blaikie2d7c57e2012-04-30 02:36:29 +0000505 if (FI->isUnnamedBitfield()) continue;
506 getStructField(FI->getFieldIndex()).
507 printPretty(Out, Ctx, FI->getType());
Richard Smithf6f003a2011-12-16 19:06:07 +0000508 First = false;
509 }
510 Out << '}';
511 return;
512 }
Richard Smithd62306a2011-11-10 06:34:14 +0000513 case APValue::Union:
Richard Smithf6f003a2011-12-16 19:06:07 +0000514 Out << '{';
515 if (const FieldDecl *FD = getUnionField()) {
516 Out << "." << *FD << " = ";
517 getUnionValue().printPretty(Out, Ctx, FD->getType());
518 }
519 Out << '}';
Richard Smithd62306a2011-11-10 06:34:14 +0000520 return;
Richard Smith027bf112011-11-17 22:56:20 +0000521 case APValue::MemberPointer:
Richard Smithf6f003a2011-12-16 19:06:07 +0000522 // FIXME: This is not enough to unambiguously identify the member in a
523 // multiple-inheritance scenario.
524 if (const ValueDecl *VD = getMemberPointerDecl()) {
525 Out << '&' << *cast<CXXRecordDecl>(VD->getDeclContext()) << "::" << *VD;
526 return;
527 }
528 Out << "0";
Richard Smith027bf112011-11-17 22:56:20 +0000529 return;
Eli Friedmanfd5e54d2012-01-04 23:13:47 +0000530 case APValue::AddrLabelDiff:
531 Out << "&&" << getAddrLabelDiffLHS()->getLabel()->getName();
532 Out << " - ";
533 Out << "&&" << getAddrLabelDiffRHS()->getLabel()->getName();
534 return;
Jeffrey Yasskind2af9622011-07-18 16:43:53 +0000535 }
Richard Smithd62306a2011-11-10 06:34:14 +0000536 llvm_unreachable("Unknown APValue kind!");
Jeffrey Yasskind2af9622011-07-18 16:43:53 +0000537}
538
Richard Smithf6f003a2011-12-16 19:06:07 +0000539std::string APValue::getAsString(ASTContext &Ctx, QualType Ty) const {
540 std::string Result;
541 llvm::raw_string_ostream Out(Result);
542 printPretty(Out, Ctx, Ty);
Eli Friedman375f09f2011-12-16 22:12:23 +0000543 Out.flush();
Richard Smithf6f003a2011-12-16 19:06:07 +0000544 return Result;
Jeffrey Yasskind2af9622011-07-18 16:43:53 +0000545}
546
Richard Smithce40ad62011-11-12 22:28:03 +0000547const APValue::LValueBase APValue::getLValueBase() const {
Ken Dyck02990832010-01-15 12:37:54 +0000548 assert(isLValue() && "Invalid accessor");
Richard Smith027bf112011-11-17 22:56:20 +0000549 return ((const LV*)(const void*)Data)->BaseAndIsOnePastTheEnd.getPointer();
550}
551
552bool APValue::isLValueOnePastTheEnd() const {
553 assert(isLValue() && "Invalid accessor");
554 return ((const LV*)(const void*)Data)->BaseAndIsOnePastTheEnd.getInt();
Ken Dyck02990832010-01-15 12:37:54 +0000555}
556
Richard Smith0b0a0b62011-10-29 20:57:55 +0000557CharUnits &APValue::getLValueOffset() {
558 assert(isLValue() && "Invalid accessor");
559 return ((LV*)(void*)Data)->Offset;
Ken Dyck02990832010-01-15 12:37:54 +0000560}
561
Richard Smith80815602011-11-07 05:07:52 +0000562bool APValue::hasLValuePath() const {
Ken Dyck02990832010-01-15 12:37:54 +0000563 assert(isLValue() && "Invalid accessor");
Richard Smithbcb4eb22011-11-07 07:31:09 +0000564 return ((const LV*)(const char*)Data)->hasPath();
Richard Smith80815602011-11-07 05:07:52 +0000565}
566
567ArrayRef<APValue::LValuePathEntry> APValue::getLValuePath() const {
568 assert(isLValue() && hasLValuePath() && "Invalid accessor");
Richard Smithbcb4eb22011-11-07 07:31:09 +0000569 const LV &LVal = *((const LV*)(const char*)Data);
Richard Smith80815602011-11-07 05:07:52 +0000570 return ArrayRef<LValuePathEntry>(LVal.getPath(), LVal.PathLength);
571}
572
Richard Smithb228a862012-02-15 02:18:13 +0000573unsigned APValue::getLValueCallIndex() const {
574 assert(isLValue() && "Invalid accessor");
575 return ((const LV*)(const char*)Data)->CallIndex;
576}
577
578void APValue::setLValue(LValueBase B, const CharUnits &O, NoLValuePath,
579 unsigned CallIndex) {
Richard Smith80815602011-11-07 05:07:52 +0000580 assert(isLValue() && "Invalid accessor");
581 LV &LVal = *((LV*)(char*)Data);
Richard Smith027bf112011-11-17 22:56:20 +0000582 LVal.BaseAndIsOnePastTheEnd.setPointer(B);
583 LVal.BaseAndIsOnePastTheEnd.setInt(false);
Richard Smith80815602011-11-07 05:07:52 +0000584 LVal.Offset = O;
Richard Smithb228a862012-02-15 02:18:13 +0000585 LVal.CallIndex = CallIndex;
Richard Smith027bf112011-11-17 22:56:20 +0000586 LVal.resizePath((unsigned)-1);
Richard Smith80815602011-11-07 05:07:52 +0000587}
588
Richard Smithce40ad62011-11-12 22:28:03 +0000589void APValue::setLValue(LValueBase B, const CharUnits &O,
Richard Smithb228a862012-02-15 02:18:13 +0000590 ArrayRef<LValuePathEntry> Path, bool IsOnePastTheEnd,
591 unsigned CallIndex) {
Richard Smith80815602011-11-07 05:07:52 +0000592 assert(isLValue() && "Invalid accessor");
593 LV &LVal = *((LV*)(char*)Data);
Richard Smith027bf112011-11-17 22:56:20 +0000594 LVal.BaseAndIsOnePastTheEnd.setPointer(B);
595 LVal.BaseAndIsOnePastTheEnd.setInt(IsOnePastTheEnd);
Richard Smith80815602011-11-07 05:07:52 +0000596 LVal.Offset = O;
Richard Smithb228a862012-02-15 02:18:13 +0000597 LVal.CallIndex = CallIndex;
Richard Smith027bf112011-11-17 22:56:20 +0000598 LVal.resizePath(Path.size());
Richard Smith80815602011-11-07 05:07:52 +0000599 memcpy(LVal.getPath(), Path.data(), Path.size() * sizeof(LValuePathEntry));
Ken Dyck02990832010-01-15 12:37:54 +0000600}
601
Richard Smith027bf112011-11-17 22:56:20 +0000602const ValueDecl *APValue::getMemberPointerDecl() const {
603 assert(isMemberPointer() && "Invalid accessor");
604 const MemberPointerData &MPD = *((const MemberPointerData*)(const char*)Data);
605 return MPD.MemberAndIsDerivedMember.getPointer();
606}
607
608bool APValue::isMemberPointerToDerivedMember() const {
609 assert(isMemberPointer() && "Invalid accessor");
610 const MemberPointerData &MPD = *((const MemberPointerData*)(const char*)Data);
611 return MPD.MemberAndIsDerivedMember.getInt();
612}
613
614ArrayRef<const CXXRecordDecl*> APValue::getMemberPointerPath() const {
615 assert(isMemberPointer() && "Invalid accessor");
616 const MemberPointerData &MPD = *((const MemberPointerData*)(const char*)Data);
617 return ArrayRef<const CXXRecordDecl*>(MPD.getPath(), MPD.PathLength);
618}
619
Ken Dyck02990832010-01-15 12:37:54 +0000620void APValue::MakeLValue() {
621 assert(isUninit() && "Bad state change");
Richard Smith80815602011-11-07 05:07:52 +0000622 assert(sizeof(LV) <= MaxSize && "LV too big");
Ken Dyck02990832010-01-15 12:37:54 +0000623 new ((void*)(char*)Data) LV();
624 Kind = LValue;
625}
Richard Smithf3e9e432011-11-07 09:22:26 +0000626
627void APValue::MakeArray(unsigned InitElts, unsigned Size) {
628 assert(isUninit() && "Bad state change");
629 new ((void*)(char*)Data) Arr(InitElts, Size);
630 Kind = Array;
631}
Richard Smith027bf112011-11-17 22:56:20 +0000632
633void APValue::MakeMemberPointer(const ValueDecl *Member, bool IsDerivedMember,
634 ArrayRef<const CXXRecordDecl*> Path) {
635 assert(isUninit() && "Bad state change");
636 MemberPointerData *MPD = new ((void*)(char*)Data) MemberPointerData;
637 Kind = MemberPointer;
638 MPD->MemberAndIsDerivedMember.setPointer(Member);
639 MPD->MemberAndIsDerivedMember.setInt(IsDerivedMember);
640 MPD->resizePath(Path.size());
641 memcpy(MPD->getPath(), Path.data(), Path.size()*sizeof(const CXXRecordDecl*));
642}