blob: 5cf9942a848721450147740c509a4b373e865922 [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 }
246}
247
Richard Smith4e9e5232012-03-10 00:28:11 +0000248void APValue::swap(APValue &RHS) {
249 std::swap(Kind, RHS.Kind);
250 char TmpData[MaxSize];
251 memcpy(TmpData, Data, MaxSize);
252 memcpy(Data, RHS.Data, MaxSize);
253 memcpy(RHS.Data, TmpData, MaxSize);
254}
255
Chris Lattner981f33b2008-11-16 07:46:48 +0000256void APValue::dump() const {
Richard Smithf6f003a2011-12-16 19:06:07 +0000257 dump(llvm::errs());
Chris Lattner981f33b2008-11-16 07:46:48 +0000258 llvm::errs() << '\n';
Chris Lattner981f33b2008-11-16 07:46:48 +0000259}
260
261static double GetApproxValue(const llvm::APFloat &F) {
262 llvm::APFloat V = F;
263 bool ignored;
264 V.convert(llvm::APFloat::IEEEdouble, llvm::APFloat::rmNearestTiesToEven,
265 &ignored);
266 return V.convertToDouble();
267}
268
Richard Smithf6f003a2011-12-16 19:06:07 +0000269void APValue::dump(raw_ostream &OS) const {
Chris Lattner981f33b2008-11-16 07:46:48 +0000270 switch (getKind()) {
Chris Lattner981f33b2008-11-16 07:46:48 +0000271 case Uninitialized:
272 OS << "Uninitialized";
273 return;
274 case Int:
275 OS << "Int: " << getInt();
276 return;
277 case Float:
278 OS << "Float: " << GetApproxValue(getFloat());
279 return;
Nate Begeman1e31b162009-01-18 01:01:34 +0000280 case Vector:
Richard Smithf6f003a2011-12-16 19:06:07 +0000281 OS << "Vector: ";
282 getVectorElt(0).dump(OS);
283 for (unsigned i = 1; i != getVectorLength(); ++i) {
284 OS << ", ";
285 getVectorElt(i).dump(OS);
286 }
Nate Begeman1e31b162009-01-18 01:01:34 +0000287 return;
Chris Lattner981f33b2008-11-16 07:46:48 +0000288 case ComplexInt:
289 OS << "ComplexInt: " << getComplexIntReal() << ", " << getComplexIntImag();
290 return;
291 case ComplexFloat:
292 OS << "ComplexFloat: " << GetApproxValue(getComplexFloatReal())
293 << ", " << GetApproxValue(getComplexFloatImag());
Richard Smithf3e9e432011-11-07 09:22:26 +0000294 return;
Chris Lattner981f33b2008-11-16 07:46:48 +0000295 case LValue:
296 OS << "LValue: <todo>";
297 return;
Richard Smithf3e9e432011-11-07 09:22:26 +0000298 case Array:
299 OS << "Array: ";
300 for (unsigned I = 0, N = getArrayInitializedElts(); I != N; ++I) {
Richard Smithf6f003a2011-12-16 19:06:07 +0000301 getArrayInitializedElt(I).dump(OS);
Richard Smithf3e9e432011-11-07 09:22:26 +0000302 if (I != getArraySize() - 1) OS << ", ";
303 }
Richard Smithf6f003a2011-12-16 19:06:07 +0000304 if (hasArrayFiller()) {
305 OS << getArraySize() - getArrayInitializedElts() << " x ";
306 getArrayFiller().dump(OS);
307 }
Richard Smithf3e9e432011-11-07 09:22:26 +0000308 return;
Richard Smithd62306a2011-11-10 06:34:14 +0000309 case Struct:
310 OS << "Struct ";
311 if (unsigned N = getStructNumBases()) {
Richard Smithf6f003a2011-12-16 19:06:07 +0000312 OS << " bases: ";
313 getStructBase(0).dump(OS);
314 for (unsigned I = 1; I != N; ++I) {
315 OS << ", ";
316 getStructBase(I).dump(OS);
317 }
Richard Smithd62306a2011-11-10 06:34:14 +0000318 }
319 if (unsigned N = getStructNumFields()) {
Richard Smithf6f003a2011-12-16 19:06:07 +0000320 OS << " fields: ";
321 getStructField(0).dump(OS);
322 for (unsigned I = 1; I != N; ++I) {
323 OS << ", ";
324 getStructField(I).dump(OS);
325 }
Richard Smithd62306a2011-11-10 06:34:14 +0000326 }
327 return;
328 case Union:
Richard Smithf6f003a2011-12-16 19:06:07 +0000329 OS << "Union: ";
330 getUnionValue().dump(OS);
Richard Smithd62306a2011-11-10 06:34:14 +0000331 return;
Richard Smith027bf112011-11-17 22:56:20 +0000332 case MemberPointer:
333 OS << "MemberPointer: <todo>";
334 return;
Eli Friedmanfd5e54d2012-01-04 23:13:47 +0000335 case AddrLabelDiff:
336 OS << "AddrLabelDiff: <todo>";
337 return;
Chris Lattner981f33b2008-11-16 07:46:48 +0000338 }
Richard Smithd62306a2011-11-10 06:34:14 +0000339 llvm_unreachable("Unknown APValue kind!");
Chris Lattner981f33b2008-11-16 07:46:48 +0000340}
341
Richard Smithf6f003a2011-12-16 19:06:07 +0000342void APValue::printPretty(raw_ostream &Out, ASTContext &Ctx, QualType Ty) const{
343 switch (getKind()) {
Jeffrey Yasskind2af9622011-07-18 16:43:53 +0000344 case APValue::Uninitialized:
Richard Smithf6f003a2011-12-16 19:06:07 +0000345 Out << "<uninitialized>";
Richard Smithd62306a2011-11-10 06:34:14 +0000346 return;
Jeffrey Yasskind2af9622011-07-18 16:43:53 +0000347 case APValue::Int:
Richard Smith5614ca72012-03-23 23:55:39 +0000348 if (Ty->isBooleanType())
349 Out << (getInt().getBoolValue() ? "true" : "false");
350 else
351 Out << getInt();
Richard Smithd62306a2011-11-10 06:34:14 +0000352 return;
Jeffrey Yasskind2af9622011-07-18 16:43:53 +0000353 case APValue::Float:
Richard Smithf6f003a2011-12-16 19:06:07 +0000354 Out << GetApproxValue(getFloat());
Richard Smithd62306a2011-11-10 06:34:14 +0000355 return;
Richard Smithf6f003a2011-12-16 19:06:07 +0000356 case APValue::Vector: {
357 Out << '{';
358 QualType ElemTy = Ty->getAs<VectorType>()->getElementType();
359 getVectorElt(0).printPretty(Out, Ctx, ElemTy);
360 for (unsigned i = 1; i != getVectorLength(); ++i) {
Jeffrey Yasskind2af9622011-07-18 16:43:53 +0000361 Out << ", ";
Richard Smithf6f003a2011-12-16 19:06:07 +0000362 getVectorElt(i).printPretty(Out, Ctx, ElemTy);
Jeffrey Yasskind2af9622011-07-18 16:43:53 +0000363 }
Richard Smithf6f003a2011-12-16 19:06:07 +0000364 Out << '}';
Richard Smithd62306a2011-11-10 06:34:14 +0000365 return;
Richard Smithf6f003a2011-12-16 19:06:07 +0000366 }
Jeffrey Yasskind2af9622011-07-18 16:43:53 +0000367 case APValue::ComplexInt:
Richard Smithf6f003a2011-12-16 19:06:07 +0000368 Out << getComplexIntReal() << "+" << getComplexIntImag() << "i";
Richard Smithd62306a2011-11-10 06:34:14 +0000369 return;
Jeffrey Yasskind2af9622011-07-18 16:43:53 +0000370 case APValue::ComplexFloat:
Richard Smithf6f003a2011-12-16 19:06:07 +0000371 Out << GetApproxValue(getComplexFloatReal()) << "+"
372 << GetApproxValue(getComplexFloatImag()) << "i";
Richard Smithd62306a2011-11-10 06:34:14 +0000373 return;
Richard Smithf6f003a2011-12-16 19:06:07 +0000374 case APValue::LValue: {
375 LValueBase Base = getLValueBase();
376 if (!Base) {
377 Out << "0";
378 return;
Richard Smithf3e9e432011-11-07 09:22:26 +0000379 }
Richard Smithf6f003a2011-12-16 19:06:07 +0000380
381 bool IsReference = Ty->isReferenceType();
382 QualType InnerTy
383 = IsReference ? Ty.getNonReferenceType() : Ty->getPointeeType();
Douglas Gregor0b7bc7f2013-01-29 01:26:43 +0000384 if (InnerTy.isNull())
385 InnerTy = Ty;
Richard Smithf6f003a2011-12-16 19:06:07 +0000386
387 if (!hasLValuePath()) {
388 // No lvalue path: just print the offset.
389 CharUnits O = getLValueOffset();
390 CharUnits S = Ctx.getTypeSizeInChars(InnerTy);
391 if (!O.isZero()) {
392 if (IsReference)
393 Out << "*(";
394 if (O % S) {
395 Out << "(char*)";
396 S = CharUnits::One();
397 }
398 Out << '&';
399 } else if (!IsReference)
400 Out << '&';
401
402 if (const ValueDecl *VD = Base.dyn_cast<const ValueDecl*>())
403 Out << *VD;
404 else
Richard Smith235341b2012-08-16 03:56:14 +0000405 Base.get<const Expr*>()->printPretty(Out, 0, Ctx.getPrintingPolicy());
Richard Smithf6f003a2011-12-16 19:06:07 +0000406 if (!O.isZero()) {
407 Out << " + " << (O / S);
408 if (IsReference)
409 Out << ')';
410 }
411 return;
412 }
413
414 // We have an lvalue path. Print it out nicely.
415 if (!IsReference)
416 Out << '&';
417 else if (isLValueOnePastTheEnd())
418 Out << "*(&";
419
420 QualType ElemTy;
421 if (const ValueDecl *VD = Base.dyn_cast<const ValueDecl*>()) {
422 Out << *VD;
423 ElemTy = VD->getType();
424 } else {
425 const Expr *E = Base.get<const Expr*>();
Richard Smith235341b2012-08-16 03:56:14 +0000426 E->printPretty(Out, 0, Ctx.getPrintingPolicy());
Richard Smithf6f003a2011-12-16 19:06:07 +0000427 ElemTy = E->getType();
428 }
429
430 ArrayRef<LValuePathEntry> Path = getLValuePath();
431 const CXXRecordDecl *CastToBase = 0;
432 for (unsigned I = 0, N = Path.size(); I != N; ++I) {
433 if (ElemTy->getAs<RecordType>()) {
434 // The lvalue refers to a class type, so the next path entry is a base
435 // or member.
436 const Decl *BaseOrMember =
437 BaseOrMemberType::getFromOpaqueValue(Path[I].BaseOrMember).getPointer();
438 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(BaseOrMember)) {
439 CastToBase = RD;
440 ElemTy = Ctx.getRecordType(RD);
441 } else {
442 const ValueDecl *VD = cast<ValueDecl>(BaseOrMember);
443 Out << ".";
444 if (CastToBase)
445 Out << *CastToBase << "::";
446 Out << *VD;
447 ElemTy = VD->getType();
448 }
449 } else {
450 // The lvalue must refer to an array.
451 Out << '[' << Path[I].ArrayIndex << ']';
452 ElemTy = Ctx.getAsArrayType(ElemTy)->getElementType();
453 }
454 }
455
456 // Handle formatting of one-past-the-end lvalues.
457 if (isLValueOnePastTheEnd()) {
458 // FIXME: If CastToBase is non-0, we should prefix the output with
459 // "(CastToBase*)".
460 Out << " + 1";
461 if (IsReference)
462 Out << ')';
463 }
Richard Smithd62306a2011-11-10 06:34:14 +0000464 return;
Richard Smithf6f003a2011-12-16 19:06:07 +0000465 }
466 case APValue::Array: {
467 const ArrayType *AT = Ctx.getAsArrayType(Ty);
468 QualType ElemTy = AT->getElementType();
Richard Smithd62306a2011-11-10 06:34:14 +0000469 Out << '{';
Richard Smithf6f003a2011-12-16 19:06:07 +0000470 if (unsigned N = getArrayInitializedElts()) {
471 getArrayInitializedElt(0).printPretty(Out, Ctx, ElemTy);
472 for (unsigned I = 1; I != N; ++I) {
Richard Smithd62306a2011-11-10 06:34:14 +0000473 Out << ", ";
Richard Smithf6f003a2011-12-16 19:06:07 +0000474 if (I == 10) {
475 // Avoid printing out the entire contents of large arrays.
476 Out << "...";
477 break;
478 }
479 getArrayInitializedElt(I).printPretty(Out, Ctx, ElemTy);
480 }
Richard Smithd62306a2011-11-10 06:34:14 +0000481 }
482 Out << '}';
483 return;
Richard Smithf6f003a2011-12-16 19:06:07 +0000484 }
485 case APValue::Struct: {
486 Out << '{';
487 const RecordDecl *RD = Ty->getAs<RecordType>()->getDecl();
488 bool First = true;
489 if (unsigned N = getStructNumBases()) {
490 const CXXRecordDecl *CD = cast<CXXRecordDecl>(RD);
491 CXXRecordDecl::base_class_const_iterator BI = CD->bases_begin();
492 for (unsigned I = 0; I != N; ++I, ++BI) {
493 assert(BI != CD->bases_end());
494 if (!First)
495 Out << ", ";
496 getStructBase(I).printPretty(Out, Ctx, BI->getType());
497 First = false;
498 }
499 }
500 for (RecordDecl::field_iterator FI = RD->field_begin();
501 FI != RD->field_end(); ++FI) {
502 if (!First)
503 Out << ", ";
David Blaikie2d7c57e2012-04-30 02:36:29 +0000504 if (FI->isUnnamedBitfield()) continue;
505 getStructField(FI->getFieldIndex()).
506 printPretty(Out, Ctx, FI->getType());
Richard Smithf6f003a2011-12-16 19:06:07 +0000507 First = false;
508 }
509 Out << '}';
510 return;
511 }
Richard Smithd62306a2011-11-10 06:34:14 +0000512 case APValue::Union:
Richard Smithf6f003a2011-12-16 19:06:07 +0000513 Out << '{';
514 if (const FieldDecl *FD = getUnionField()) {
515 Out << "." << *FD << " = ";
516 getUnionValue().printPretty(Out, Ctx, FD->getType());
517 }
518 Out << '}';
Richard Smithd62306a2011-11-10 06:34:14 +0000519 return;
Richard Smith027bf112011-11-17 22:56:20 +0000520 case APValue::MemberPointer:
Richard Smithf6f003a2011-12-16 19:06:07 +0000521 // FIXME: This is not enough to unambiguously identify the member in a
522 // multiple-inheritance scenario.
523 if (const ValueDecl *VD = getMemberPointerDecl()) {
524 Out << '&' << *cast<CXXRecordDecl>(VD->getDeclContext()) << "::" << *VD;
525 return;
526 }
527 Out << "0";
Richard Smith027bf112011-11-17 22:56:20 +0000528 return;
Eli Friedmanfd5e54d2012-01-04 23:13:47 +0000529 case APValue::AddrLabelDiff:
530 Out << "&&" << getAddrLabelDiffLHS()->getLabel()->getName();
531 Out << " - ";
532 Out << "&&" << getAddrLabelDiffRHS()->getLabel()->getName();
533 return;
Jeffrey Yasskind2af9622011-07-18 16:43:53 +0000534 }
Richard Smithd62306a2011-11-10 06:34:14 +0000535 llvm_unreachable("Unknown APValue kind!");
Jeffrey Yasskind2af9622011-07-18 16:43:53 +0000536}
537
Richard Smithf6f003a2011-12-16 19:06:07 +0000538std::string APValue::getAsString(ASTContext &Ctx, QualType Ty) const {
539 std::string Result;
540 llvm::raw_string_ostream Out(Result);
541 printPretty(Out, Ctx, Ty);
Eli Friedman375f09f2011-12-16 22:12:23 +0000542 Out.flush();
Richard Smithf6f003a2011-12-16 19:06:07 +0000543 return Result;
Jeffrey Yasskind2af9622011-07-18 16:43:53 +0000544}
545
Richard Smithce40ad62011-11-12 22:28:03 +0000546const APValue::LValueBase APValue::getLValueBase() const {
Ken Dyck02990832010-01-15 12:37:54 +0000547 assert(isLValue() && "Invalid accessor");
Richard Smith027bf112011-11-17 22:56:20 +0000548 return ((const LV*)(const void*)Data)->BaseAndIsOnePastTheEnd.getPointer();
549}
550
551bool APValue::isLValueOnePastTheEnd() const {
552 assert(isLValue() && "Invalid accessor");
553 return ((const LV*)(const void*)Data)->BaseAndIsOnePastTheEnd.getInt();
Ken Dyck02990832010-01-15 12:37:54 +0000554}
555
Richard Smith0b0a0b62011-10-29 20:57:55 +0000556CharUnits &APValue::getLValueOffset() {
557 assert(isLValue() && "Invalid accessor");
558 return ((LV*)(void*)Data)->Offset;
Ken Dyck02990832010-01-15 12:37:54 +0000559}
560
Richard Smith80815602011-11-07 05:07:52 +0000561bool APValue::hasLValuePath() const {
Ken Dyck02990832010-01-15 12:37:54 +0000562 assert(isLValue() && "Invalid accessor");
Richard Smithbcb4eb22011-11-07 07:31:09 +0000563 return ((const LV*)(const char*)Data)->hasPath();
Richard Smith80815602011-11-07 05:07:52 +0000564}
565
566ArrayRef<APValue::LValuePathEntry> APValue::getLValuePath() const {
567 assert(isLValue() && hasLValuePath() && "Invalid accessor");
Richard Smithbcb4eb22011-11-07 07:31:09 +0000568 const LV &LVal = *((const LV*)(const char*)Data);
Richard Smith80815602011-11-07 05:07:52 +0000569 return ArrayRef<LValuePathEntry>(LVal.getPath(), LVal.PathLength);
570}
571
Richard Smithb228a862012-02-15 02:18:13 +0000572unsigned APValue::getLValueCallIndex() const {
573 assert(isLValue() && "Invalid accessor");
574 return ((const LV*)(const char*)Data)->CallIndex;
575}
576
577void APValue::setLValue(LValueBase B, const CharUnits &O, NoLValuePath,
578 unsigned CallIndex) {
Richard Smith80815602011-11-07 05:07:52 +0000579 assert(isLValue() && "Invalid accessor");
580 LV &LVal = *((LV*)(char*)Data);
Richard Smith027bf112011-11-17 22:56:20 +0000581 LVal.BaseAndIsOnePastTheEnd.setPointer(B);
582 LVal.BaseAndIsOnePastTheEnd.setInt(false);
Richard Smith80815602011-11-07 05:07:52 +0000583 LVal.Offset = O;
Richard Smithb228a862012-02-15 02:18:13 +0000584 LVal.CallIndex = CallIndex;
Richard Smith027bf112011-11-17 22:56:20 +0000585 LVal.resizePath((unsigned)-1);
Richard Smith80815602011-11-07 05:07:52 +0000586}
587
Richard Smithce40ad62011-11-12 22:28:03 +0000588void APValue::setLValue(LValueBase B, const CharUnits &O,
Richard Smithb228a862012-02-15 02:18:13 +0000589 ArrayRef<LValuePathEntry> Path, bool IsOnePastTheEnd,
590 unsigned CallIndex) {
Richard Smith80815602011-11-07 05:07:52 +0000591 assert(isLValue() && "Invalid accessor");
592 LV &LVal = *((LV*)(char*)Data);
Richard Smith027bf112011-11-17 22:56:20 +0000593 LVal.BaseAndIsOnePastTheEnd.setPointer(B);
594 LVal.BaseAndIsOnePastTheEnd.setInt(IsOnePastTheEnd);
Richard Smith80815602011-11-07 05:07:52 +0000595 LVal.Offset = O;
Richard Smithb228a862012-02-15 02:18:13 +0000596 LVal.CallIndex = CallIndex;
Richard Smith027bf112011-11-17 22:56:20 +0000597 LVal.resizePath(Path.size());
Richard Smith80815602011-11-07 05:07:52 +0000598 memcpy(LVal.getPath(), Path.data(), Path.size() * sizeof(LValuePathEntry));
Ken Dyck02990832010-01-15 12:37:54 +0000599}
600
Richard Smith027bf112011-11-17 22:56:20 +0000601const ValueDecl *APValue::getMemberPointerDecl() const {
602 assert(isMemberPointer() && "Invalid accessor");
603 const MemberPointerData &MPD = *((const MemberPointerData*)(const char*)Data);
604 return MPD.MemberAndIsDerivedMember.getPointer();
605}
606
607bool APValue::isMemberPointerToDerivedMember() const {
608 assert(isMemberPointer() && "Invalid accessor");
609 const MemberPointerData &MPD = *((const MemberPointerData*)(const char*)Data);
610 return MPD.MemberAndIsDerivedMember.getInt();
611}
612
613ArrayRef<const CXXRecordDecl*> APValue::getMemberPointerPath() const {
614 assert(isMemberPointer() && "Invalid accessor");
615 const MemberPointerData &MPD = *((const MemberPointerData*)(const char*)Data);
616 return ArrayRef<const CXXRecordDecl*>(MPD.getPath(), MPD.PathLength);
617}
618
Ken Dyck02990832010-01-15 12:37:54 +0000619void APValue::MakeLValue() {
620 assert(isUninit() && "Bad state change");
Richard Smith80815602011-11-07 05:07:52 +0000621 assert(sizeof(LV) <= MaxSize && "LV too big");
Ken Dyck02990832010-01-15 12:37:54 +0000622 new ((void*)(char*)Data) LV();
623 Kind = LValue;
624}
Richard Smithf3e9e432011-11-07 09:22:26 +0000625
626void APValue::MakeArray(unsigned InitElts, unsigned Size) {
627 assert(isUninit() && "Bad state change");
628 new ((void*)(char*)Data) Arr(InitElts, Size);
629 Kind = Array;
630}
Richard Smith027bf112011-11-17 22:56:20 +0000631
632void APValue::MakeMemberPointer(const ValueDecl *Member, bool IsDerivedMember,
633 ArrayRef<const CXXRecordDecl*> Path) {
634 assert(isUninit() && "Bad state change");
635 MemberPointerData *MPD = new ((void*)(char*)Data) MemberPointerData;
636 Kind = MemberPointer;
637 MPD->MemberAndIsDerivedMember.setPointer(Member);
638 MPD->MemberAndIsDerivedMember.setInt(IsDerivedMember);
639 MPD->resizePath(Path.size());
640 memcpy(MPD->getPath(), Path.data(), Path.size()*sizeof(const CXXRecordDecl*));
641}