blob: b8942c33101c1ef6be43fd180460407edaa3bd34 [file] [log] [blame]
Chris Lattner64c34f12008-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 Smith08d6e032011-12-16 19:06:07 +000015#include "clang/AST/ASTContext.h"
Ken Dycka7305832010-01-15 12:37:54 +000016#include "clang/AST/CharUnits.h"
Richard Smith08d6e032011-12-16 19:06:07 +000017#include "clang/AST/DeclCXX.h"
18#include "clang/AST/Expr.h"
19#include "clang/AST/Type.h"
Jeffrey Yasskin5b106a82011-07-18 16:43:53 +000020#include "clang/Basic/Diagnostic.h"
21#include "llvm/ADT/SmallString.h"
Chris Lattner64c34f12008-11-16 07:46:48 +000022#include "llvm/Support/raw_ostream.h"
David Blaikie9fe8c742011-09-23 05:35:21 +000023#include "llvm/Support/ErrorHandling.h"
Chris Lattner64c34f12008-11-16 07:46:48 +000024using namespace clang;
25
Ken Dycka7305832010-01-15 12:37:54 +000026namespace {
Richard Smith9a17a682011-11-07 05:07:52 +000027 struct LVBase {
Richard Smithe24f5fc2011-11-17 22:56:20 +000028 llvm::PointerIntPair<APValue::LValueBase, 1, bool> BaseAndIsOnePastTheEnd;
Ken Dycka7305832010-01-15 12:37:54 +000029 CharUnits Offset;
Richard Smith9a17a682011-11-07 05:07:52 +000030 unsigned PathLength;
Ken Dycka7305832010-01-15 12:37:54 +000031 };
32}
33
Richard Smith9a17a682011-11-07 05:07:52 +000034struct APValue::LV : LVBase {
35 static const unsigned InlinePathSpace =
36 (MaxSize - sizeof(LVBase)) / sizeof(LValuePathEntry);
37
38 /// Path - The sequence of base classes, fields and array indices to follow to
39 /// walk from Base to the subobject. When performing GCC-style folding, there
40 /// may not be such a path.
41 union {
42 LValuePathEntry Path[InlinePathSpace];
43 LValuePathEntry *PathPtr;
44 };
45
46 LV() { PathLength = (unsigned)-1; }
Richard Smithe24f5fc2011-11-17 22:56:20 +000047 ~LV() { resizePath(0); }
Richard Smith9a17a682011-11-07 05:07:52 +000048
Richard Smithe24f5fc2011-11-17 22:56:20 +000049 void resizePath(unsigned Length) {
50 if (Length == PathLength)
51 return;
52 if (hasPathPtr())
53 delete [] PathPtr;
54 PathLength = Length;
55 if (hasPathPtr())
56 PathPtr = new LValuePathEntry[Length];
Richard Smith9a17a682011-11-07 05:07:52 +000057 }
58
59 bool hasPath() const { return PathLength != (unsigned)-1; }
60 bool hasPathPtr() const { return hasPath() && PathLength > InlinePathSpace; }
61
62 LValuePathEntry *getPath() { return hasPathPtr() ? PathPtr : Path; }
Richard Smith38dce9b2011-11-07 07:31:09 +000063 const LValuePathEntry *getPath() const {
64 return hasPathPtr() ? PathPtr : Path;
65 }
Richard Smith9a17a682011-11-07 05:07:52 +000066};
67
Richard Smithe24f5fc2011-11-17 22:56:20 +000068namespace {
69 struct MemberPointerBase {
70 llvm::PointerIntPair<const ValueDecl*, 1, bool> MemberAndIsDerivedMember;
71 unsigned PathLength;
72 };
73}
74
75struct APValue::MemberPointerData : MemberPointerBase {
76 static const unsigned InlinePathSpace =
77 (MaxSize - sizeof(MemberPointerBase)) / sizeof(const CXXRecordDecl*);
78 typedef const CXXRecordDecl *PathElem;
79 union {
80 PathElem Path[InlinePathSpace];
81 PathElem *PathPtr;
82 };
83
84 MemberPointerData() { PathLength = 0; }
85 ~MemberPointerData() { resizePath(0); }
86
87 void resizePath(unsigned Length) {
88 if (Length == PathLength)
89 return;
90 if (hasPathPtr())
91 delete [] PathPtr;
92 PathLength = Length;
93 if (hasPathPtr())
94 PathPtr = new PathElem[Length];
95 }
96
97 bool hasPathPtr() const { return PathLength > InlinePathSpace; }
98
99 PathElem *getPath() { return hasPathPtr() ? PathPtr : Path; }
100 const PathElem *getPath() const {
101 return hasPathPtr() ? PathPtr : Path;
102 }
103};
104
Richard Smithcc5d4f62011-11-07 09:22:26 +0000105// FIXME: Reduce the malloc traffic here.
106
107APValue::Arr::Arr(unsigned NumElts, unsigned Size) :
108 Elts(new APValue[NumElts + (NumElts != Size ? 1 : 0)]),
109 NumElts(NumElts), ArrSize(Size) {}
110APValue::Arr::~Arr() { delete [] Elts; }
111
Richard Smith180f4792011-11-10 06:34:14 +0000112APValue::StructData::StructData(unsigned NumBases, unsigned NumFields) :
113 Elts(new APValue[NumBases+NumFields]),
114 NumBases(NumBases), NumFields(NumFields) {}
115APValue::StructData::~StructData() {
116 delete [] Elts;
117}
118
119APValue::UnionData::UnionData() : Field(0), Value(new APValue) {}
120APValue::UnionData::~UnionData () {
121 delete Value;
122}
123
Chris Lattner64c34f12008-11-16 07:46:48 +0000124const APValue &APValue::operator=(const APValue &RHS) {
Richard Smith180f4792011-11-10 06:34:14 +0000125 if (this == &RHS)
126 return *this;
Richard Smithe24f5fc2011-11-17 22:56:20 +0000127 if (Kind != RHS.Kind || Kind == Array || Kind == Struct ||
128 Kind == MemberPointer) {
Chris Lattner64c34f12008-11-16 07:46:48 +0000129 MakeUninit();
130 if (RHS.isInt())
131 MakeInt();
132 else if (RHS.isFloat())
133 MakeFloat();
Nate Begeman3d309f92009-01-18 01:01:34 +0000134 else if (RHS.isVector())
135 MakeVector();
Chris Lattner64c34f12008-11-16 07:46:48 +0000136 else if (RHS.isComplexInt())
137 MakeComplexInt();
138 else if (RHS.isComplexFloat())
139 MakeComplexFloat();
140 else if (RHS.isLValue())
141 MakeLValue();
Richard Smithcc5d4f62011-11-07 09:22:26 +0000142 else if (RHS.isArray())
143 MakeArray(RHS.getArrayInitializedElts(), RHS.getArraySize());
Richard Smith180f4792011-11-10 06:34:14 +0000144 else if (RHS.isStruct())
145 MakeStruct(RHS.getStructNumBases(), RHS.getStructNumFields());
146 else if (RHS.isUnion())
147 MakeUnion();
Richard Smithe24f5fc2011-11-17 22:56:20 +0000148 else if (RHS.isMemberPointer())
149 MakeMemberPointer(RHS.getMemberPointerDecl(),
150 RHS.isMemberPointerToDerivedMember(),
151 RHS.getMemberPointerPath());
Eli Friedman65639282012-01-04 23:13:47 +0000152 else if (RHS.isAddrLabelDiff())
153 MakeAddrLabelDiff();
Chris Lattner64c34f12008-11-16 07:46:48 +0000154 }
155 if (isInt())
156 setInt(RHS.getInt());
157 else if (isFloat())
158 setFloat(RHS.getFloat());
Nate Begeman3d309f92009-01-18 01:01:34 +0000159 else if (isVector())
Dan Gohmancb421fa2010-04-19 16:39:44 +0000160 setVector(((const Vec *)(const char *)RHS.Data)->Elts,
161 RHS.getVectorLength());
Chris Lattner64c34f12008-11-16 07:46:48 +0000162 else if (isComplexInt())
163 setComplexInt(RHS.getComplexIntReal(), RHS.getComplexIntImag());
164 else if (isComplexFloat())
165 setComplexFloat(RHS.getComplexFloatReal(), RHS.getComplexFloatImag());
Richard Smith9a17a682011-11-07 05:07:52 +0000166 else if (isLValue()) {
167 if (RHS.hasLValuePath())
Richard Smithe24f5fc2011-11-17 22:56:20 +0000168 setLValue(RHS.getLValueBase(), RHS.getLValueOffset(), RHS.getLValuePath(),
169 RHS.isLValueOnePastTheEnd());
Richard Smith9a17a682011-11-07 05:07:52 +0000170 else
171 setLValue(RHS.getLValueBase(), RHS.getLValueOffset(), NoLValuePath());
Richard Smithcc5d4f62011-11-07 09:22:26 +0000172 } else if (isArray()) {
173 for (unsigned I = 0, N = RHS.getArrayInitializedElts(); I != N; ++I)
174 getArrayInitializedElt(I) = RHS.getArrayInitializedElt(I);
175 if (RHS.hasArrayFiller())
176 getArrayFiller() = RHS.getArrayFiller();
Richard Smith180f4792011-11-10 06:34:14 +0000177 } else if (isStruct()) {
178 for (unsigned I = 0, N = RHS.getStructNumBases(); I != N; ++I)
179 getStructBase(I) = RHS.getStructBase(I);
180 for (unsigned I = 0, N = RHS.getStructNumFields(); I != N; ++I)
181 getStructField(I) = RHS.getStructField(I);
Eli Friedman65639282012-01-04 23:13:47 +0000182 } else if (isUnion()) {
Richard Smith180f4792011-11-10 06:34:14 +0000183 setUnion(RHS.getUnionField(), RHS.getUnionValue());
Eli Friedman65639282012-01-04 23:13:47 +0000184 } else if (isAddrLabelDiff()) {
185 setAddrLabelDiff(RHS.getAddrLabelDiffLHS(), RHS.getAddrLabelDiffRHS());
186 }
Chris Lattner64c34f12008-11-16 07:46:48 +0000187 return *this;
188}
189
190void APValue::MakeUninit() {
191 if (Kind == Int)
Douglas Gregor98300462009-09-08 19:57:33 +0000192 ((APSInt*)(char*)Data)->~APSInt();
Chris Lattner64c34f12008-11-16 07:46:48 +0000193 else if (Kind == Float)
Douglas Gregor98300462009-09-08 19:57:33 +0000194 ((APFloat*)(char*)Data)->~APFloat();
Nate Begeman3d309f92009-01-18 01:01:34 +0000195 else if (Kind == Vector)
Douglas Gregor98300462009-09-08 19:57:33 +0000196 ((Vec*)(char*)Data)->~Vec();
Chris Lattner64c34f12008-11-16 07:46:48 +0000197 else if (Kind == ComplexInt)
Douglas Gregor98300462009-09-08 19:57:33 +0000198 ((ComplexAPSInt*)(char*)Data)->~ComplexAPSInt();
Chris Lattner64c34f12008-11-16 07:46:48 +0000199 else if (Kind == ComplexFloat)
Douglas Gregor98300462009-09-08 19:57:33 +0000200 ((ComplexAPFloat*)(char*)Data)->~ComplexAPFloat();
Richard Smithcc5d4f62011-11-07 09:22:26 +0000201 else if (Kind == LValue)
Douglas Gregor98300462009-09-08 19:57:33 +0000202 ((LV*)(char*)Data)->~LV();
Richard Smithcc5d4f62011-11-07 09:22:26 +0000203 else if (Kind == Array)
204 ((Arr*)(char*)Data)->~Arr();
Richard Smith180f4792011-11-10 06:34:14 +0000205 else if (Kind == Struct)
206 ((StructData*)(char*)Data)->~StructData();
207 else if (Kind == Union)
208 ((UnionData*)(char*)Data)->~UnionData();
Richard Smithe24f5fc2011-11-17 22:56:20 +0000209 else if (Kind == MemberPointer)
210 ((MemberPointerData*)(char*)Data)->~MemberPointerData();
Eli Friedman65639282012-01-04 23:13:47 +0000211 else if (Kind == AddrLabelDiff)
212 ((AddrLabelDiffData*)(char*)Data)->~AddrLabelDiffData();
Nate Begeman3d309f92009-01-18 01:01:34 +0000213 Kind = Uninitialized;
Chris Lattner64c34f12008-11-16 07:46:48 +0000214}
215
216void APValue::dump() const {
Richard Smith08d6e032011-12-16 19:06:07 +0000217 dump(llvm::errs());
Chris Lattner64c34f12008-11-16 07:46:48 +0000218 llvm::errs() << '\n';
Chris Lattner64c34f12008-11-16 07:46:48 +0000219}
220
221static double GetApproxValue(const llvm::APFloat &F) {
222 llvm::APFloat V = F;
223 bool ignored;
224 V.convert(llvm::APFloat::IEEEdouble, llvm::APFloat::rmNearestTiesToEven,
225 &ignored);
226 return V.convertToDouble();
227}
228
Richard Smith08d6e032011-12-16 19:06:07 +0000229void APValue::dump(raw_ostream &OS) const {
Chris Lattner64c34f12008-11-16 07:46:48 +0000230 switch (getKind()) {
Chris Lattner64c34f12008-11-16 07:46:48 +0000231 case Uninitialized:
232 OS << "Uninitialized";
233 return;
234 case Int:
235 OS << "Int: " << getInt();
236 return;
237 case Float:
238 OS << "Float: " << GetApproxValue(getFloat());
239 return;
Nate Begeman3d309f92009-01-18 01:01:34 +0000240 case Vector:
Richard Smith08d6e032011-12-16 19:06:07 +0000241 OS << "Vector: ";
242 getVectorElt(0).dump(OS);
243 for (unsigned i = 1; i != getVectorLength(); ++i) {
244 OS << ", ";
245 getVectorElt(i).dump(OS);
246 }
Nate Begeman3d309f92009-01-18 01:01:34 +0000247 return;
Chris Lattner64c34f12008-11-16 07:46:48 +0000248 case ComplexInt:
249 OS << "ComplexInt: " << getComplexIntReal() << ", " << getComplexIntImag();
250 return;
251 case ComplexFloat:
252 OS << "ComplexFloat: " << GetApproxValue(getComplexFloatReal())
253 << ", " << GetApproxValue(getComplexFloatImag());
Richard Smithcc5d4f62011-11-07 09:22:26 +0000254 return;
Chris Lattner64c34f12008-11-16 07:46:48 +0000255 case LValue:
256 OS << "LValue: <todo>";
257 return;
Richard Smithcc5d4f62011-11-07 09:22:26 +0000258 case Array:
259 OS << "Array: ";
260 for (unsigned I = 0, N = getArrayInitializedElts(); I != N; ++I) {
Richard Smith08d6e032011-12-16 19:06:07 +0000261 getArrayInitializedElt(I).dump(OS);
Richard Smithcc5d4f62011-11-07 09:22:26 +0000262 if (I != getArraySize() - 1) OS << ", ";
263 }
Richard Smith08d6e032011-12-16 19:06:07 +0000264 if (hasArrayFiller()) {
265 OS << getArraySize() - getArrayInitializedElts() << " x ";
266 getArrayFiller().dump(OS);
267 }
Richard Smithcc5d4f62011-11-07 09:22:26 +0000268 return;
Richard Smith180f4792011-11-10 06:34:14 +0000269 case Struct:
270 OS << "Struct ";
271 if (unsigned N = getStructNumBases()) {
Richard Smith08d6e032011-12-16 19:06:07 +0000272 OS << " bases: ";
273 getStructBase(0).dump(OS);
274 for (unsigned I = 1; I != N; ++I) {
275 OS << ", ";
276 getStructBase(I).dump(OS);
277 }
Richard Smith180f4792011-11-10 06:34:14 +0000278 }
279 if (unsigned N = getStructNumFields()) {
Richard Smith08d6e032011-12-16 19:06:07 +0000280 OS << " fields: ";
281 getStructField(0).dump(OS);
282 for (unsigned I = 1; I != N; ++I) {
283 OS << ", ";
284 getStructField(I).dump(OS);
285 }
Richard Smith180f4792011-11-10 06:34:14 +0000286 }
287 return;
288 case Union:
Richard Smith08d6e032011-12-16 19:06:07 +0000289 OS << "Union: ";
290 getUnionValue().dump(OS);
Richard Smith180f4792011-11-10 06:34:14 +0000291 return;
Richard Smithe24f5fc2011-11-17 22:56:20 +0000292 case MemberPointer:
293 OS << "MemberPointer: <todo>";
294 return;
Eli Friedman65639282012-01-04 23:13:47 +0000295 case AddrLabelDiff:
296 OS << "AddrLabelDiff: <todo>";
297 return;
Chris Lattner64c34f12008-11-16 07:46:48 +0000298 }
Richard Smith180f4792011-11-10 06:34:14 +0000299 llvm_unreachable("Unknown APValue kind!");
Chris Lattner64c34f12008-11-16 07:46:48 +0000300}
301
Richard Smith08d6e032011-12-16 19:06:07 +0000302void APValue::printPretty(raw_ostream &Out, ASTContext &Ctx, QualType Ty) const{
303 switch (getKind()) {
Jeffrey Yasskin5b106a82011-07-18 16:43:53 +0000304 case APValue::Uninitialized:
Richard Smith08d6e032011-12-16 19:06:07 +0000305 Out << "<uninitialized>";
Richard Smith180f4792011-11-10 06:34:14 +0000306 return;
Jeffrey Yasskin5b106a82011-07-18 16:43:53 +0000307 case APValue::Int:
Richard Smith08d6e032011-12-16 19:06:07 +0000308 Out << getInt();
Richard Smith180f4792011-11-10 06:34:14 +0000309 return;
Jeffrey Yasskin5b106a82011-07-18 16:43:53 +0000310 case APValue::Float:
Richard Smith08d6e032011-12-16 19:06:07 +0000311 Out << GetApproxValue(getFloat());
Richard Smith180f4792011-11-10 06:34:14 +0000312 return;
Richard Smith08d6e032011-12-16 19:06:07 +0000313 case APValue::Vector: {
314 Out << '{';
315 QualType ElemTy = Ty->getAs<VectorType>()->getElementType();
316 getVectorElt(0).printPretty(Out, Ctx, ElemTy);
317 for (unsigned i = 1; i != getVectorLength(); ++i) {
Jeffrey Yasskin5b106a82011-07-18 16:43:53 +0000318 Out << ", ";
Richard Smith08d6e032011-12-16 19:06:07 +0000319 getVectorElt(i).printPretty(Out, Ctx, ElemTy);
Jeffrey Yasskin5b106a82011-07-18 16:43:53 +0000320 }
Richard Smith08d6e032011-12-16 19:06:07 +0000321 Out << '}';
Richard Smith180f4792011-11-10 06:34:14 +0000322 return;
Richard Smith08d6e032011-12-16 19:06:07 +0000323 }
Jeffrey Yasskin5b106a82011-07-18 16:43:53 +0000324 case APValue::ComplexInt:
Richard Smith08d6e032011-12-16 19:06:07 +0000325 Out << getComplexIntReal() << "+" << getComplexIntImag() << "i";
Richard Smith180f4792011-11-10 06:34:14 +0000326 return;
Jeffrey Yasskin5b106a82011-07-18 16:43:53 +0000327 case APValue::ComplexFloat:
Richard Smith08d6e032011-12-16 19:06:07 +0000328 Out << GetApproxValue(getComplexFloatReal()) << "+"
329 << GetApproxValue(getComplexFloatImag()) << "i";
Richard Smith180f4792011-11-10 06:34:14 +0000330 return;
Richard Smith08d6e032011-12-16 19:06:07 +0000331 case APValue::LValue: {
332 LValueBase Base = getLValueBase();
333 if (!Base) {
334 Out << "0";
335 return;
Richard Smithcc5d4f62011-11-07 09:22:26 +0000336 }
Richard Smith08d6e032011-12-16 19:06:07 +0000337
338 bool IsReference = Ty->isReferenceType();
339 QualType InnerTy
340 = IsReference ? Ty.getNonReferenceType() : Ty->getPointeeType();
341
342 if (!hasLValuePath()) {
343 // No lvalue path: just print the offset.
344 CharUnits O = getLValueOffset();
345 CharUnits S = Ctx.getTypeSizeInChars(InnerTy);
346 if (!O.isZero()) {
347 if (IsReference)
348 Out << "*(";
349 if (O % S) {
350 Out << "(char*)";
351 S = CharUnits::One();
352 }
353 Out << '&';
354 } else if (!IsReference)
355 Out << '&';
356
357 if (const ValueDecl *VD = Base.dyn_cast<const ValueDecl*>())
358 Out << *VD;
359 else
360 Base.get<const Expr*>()->printPretty(Out, Ctx, 0,
361 Ctx.getPrintingPolicy());
362 if (!O.isZero()) {
363 Out << " + " << (O / S);
364 if (IsReference)
365 Out << ')';
366 }
367 return;
368 }
369
370 // We have an lvalue path. Print it out nicely.
371 if (!IsReference)
372 Out << '&';
373 else if (isLValueOnePastTheEnd())
374 Out << "*(&";
375
376 QualType ElemTy;
377 if (const ValueDecl *VD = Base.dyn_cast<const ValueDecl*>()) {
378 Out << *VD;
379 ElemTy = VD->getType();
380 } else {
381 const Expr *E = Base.get<const Expr*>();
382 E->printPretty(Out, Ctx, 0,Ctx.getPrintingPolicy());
383 ElemTy = E->getType();
384 }
385
386 ArrayRef<LValuePathEntry> Path = getLValuePath();
387 const CXXRecordDecl *CastToBase = 0;
388 for (unsigned I = 0, N = Path.size(); I != N; ++I) {
389 if (ElemTy->getAs<RecordType>()) {
390 // The lvalue refers to a class type, so the next path entry is a base
391 // or member.
392 const Decl *BaseOrMember =
393 BaseOrMemberType::getFromOpaqueValue(Path[I].BaseOrMember).getPointer();
394 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(BaseOrMember)) {
395 CastToBase = RD;
396 ElemTy = Ctx.getRecordType(RD);
397 } else {
398 const ValueDecl *VD = cast<ValueDecl>(BaseOrMember);
399 Out << ".";
400 if (CastToBase)
401 Out << *CastToBase << "::";
402 Out << *VD;
403 ElemTy = VD->getType();
404 }
405 } else {
406 // The lvalue must refer to an array.
407 Out << '[' << Path[I].ArrayIndex << ']';
408 ElemTy = Ctx.getAsArrayType(ElemTy)->getElementType();
409 }
410 }
411
412 // Handle formatting of one-past-the-end lvalues.
413 if (isLValueOnePastTheEnd()) {
414 // FIXME: If CastToBase is non-0, we should prefix the output with
415 // "(CastToBase*)".
416 Out << " + 1";
417 if (IsReference)
418 Out << ')';
419 }
Richard Smith180f4792011-11-10 06:34:14 +0000420 return;
Richard Smith08d6e032011-12-16 19:06:07 +0000421 }
422 case APValue::Array: {
423 const ArrayType *AT = Ctx.getAsArrayType(Ty);
424 QualType ElemTy = AT->getElementType();
Richard Smith180f4792011-11-10 06:34:14 +0000425 Out << '{';
Richard Smith08d6e032011-12-16 19:06:07 +0000426 if (unsigned N = getArrayInitializedElts()) {
427 getArrayInitializedElt(0).printPretty(Out, Ctx, ElemTy);
428 for (unsigned I = 1; I != N; ++I) {
Richard Smith180f4792011-11-10 06:34:14 +0000429 Out << ", ";
Richard Smith08d6e032011-12-16 19:06:07 +0000430 if (I == 10) {
431 // Avoid printing out the entire contents of large arrays.
432 Out << "...";
433 break;
434 }
435 getArrayInitializedElt(I).printPretty(Out, Ctx, ElemTy);
436 }
Richard Smith180f4792011-11-10 06:34:14 +0000437 }
438 Out << '}';
439 return;
Richard Smith08d6e032011-12-16 19:06:07 +0000440 }
441 case APValue::Struct: {
442 Out << '{';
443 const RecordDecl *RD = Ty->getAs<RecordType>()->getDecl();
444 bool First = true;
445 if (unsigned N = getStructNumBases()) {
446 const CXXRecordDecl *CD = cast<CXXRecordDecl>(RD);
447 CXXRecordDecl::base_class_const_iterator BI = CD->bases_begin();
448 for (unsigned I = 0; I != N; ++I, ++BI) {
449 assert(BI != CD->bases_end());
450 if (!First)
451 Out << ", ";
452 getStructBase(I).printPretty(Out, Ctx, BI->getType());
453 First = false;
454 }
455 }
456 for (RecordDecl::field_iterator FI = RD->field_begin();
457 FI != RD->field_end(); ++FI) {
458 if (!First)
459 Out << ", ";
460 if ((*FI)->isUnnamedBitfield()) continue;
461 getStructField((*FI)->getFieldIndex()).
462 printPretty(Out, Ctx, (*FI)->getType());
463 First = false;
464 }
465 Out << '}';
466 return;
467 }
Richard Smith180f4792011-11-10 06:34:14 +0000468 case APValue::Union:
Richard Smith08d6e032011-12-16 19:06:07 +0000469 Out << '{';
470 if (const FieldDecl *FD = getUnionField()) {
471 Out << "." << *FD << " = ";
472 getUnionValue().printPretty(Out, Ctx, FD->getType());
473 }
474 Out << '}';
Richard Smith180f4792011-11-10 06:34:14 +0000475 return;
Richard Smithe24f5fc2011-11-17 22:56:20 +0000476 case APValue::MemberPointer:
Richard Smith08d6e032011-12-16 19:06:07 +0000477 // FIXME: This is not enough to unambiguously identify the member in a
478 // multiple-inheritance scenario.
479 if (const ValueDecl *VD = getMemberPointerDecl()) {
480 Out << '&' << *cast<CXXRecordDecl>(VD->getDeclContext()) << "::" << *VD;
481 return;
482 }
483 Out << "0";
Richard Smithe24f5fc2011-11-17 22:56:20 +0000484 return;
Eli Friedman65639282012-01-04 23:13:47 +0000485 case APValue::AddrLabelDiff:
486 Out << "&&" << getAddrLabelDiffLHS()->getLabel()->getName();
487 Out << " - ";
488 Out << "&&" << getAddrLabelDiffRHS()->getLabel()->getName();
489 return;
Jeffrey Yasskin5b106a82011-07-18 16:43:53 +0000490 }
Richard Smith180f4792011-11-10 06:34:14 +0000491 llvm_unreachable("Unknown APValue kind!");
Jeffrey Yasskin5b106a82011-07-18 16:43:53 +0000492}
493
Richard Smith08d6e032011-12-16 19:06:07 +0000494std::string APValue::getAsString(ASTContext &Ctx, QualType Ty) const {
495 std::string Result;
496 llvm::raw_string_ostream Out(Result);
497 printPretty(Out, Ctx, Ty);
Eli Friedmand9ce41e2011-12-16 22:12:23 +0000498 Out.flush();
Richard Smith08d6e032011-12-16 19:06:07 +0000499 return Result;
Jeffrey Yasskin5b106a82011-07-18 16:43:53 +0000500}
501
Richard Smith1bf9a9e2011-11-12 22:28:03 +0000502const APValue::LValueBase APValue::getLValueBase() const {
Ken Dycka7305832010-01-15 12:37:54 +0000503 assert(isLValue() && "Invalid accessor");
Richard Smithe24f5fc2011-11-17 22:56:20 +0000504 return ((const LV*)(const void*)Data)->BaseAndIsOnePastTheEnd.getPointer();
505}
506
507bool APValue::isLValueOnePastTheEnd() const {
508 assert(isLValue() && "Invalid accessor");
509 return ((const LV*)(const void*)Data)->BaseAndIsOnePastTheEnd.getInt();
Ken Dycka7305832010-01-15 12:37:54 +0000510}
511
Richard Smith47a1eed2011-10-29 20:57:55 +0000512CharUnits &APValue::getLValueOffset() {
513 assert(isLValue() && "Invalid accessor");
514 return ((LV*)(void*)Data)->Offset;
Ken Dycka7305832010-01-15 12:37:54 +0000515}
516
Richard Smith9a17a682011-11-07 05:07:52 +0000517bool APValue::hasLValuePath() const {
Ken Dycka7305832010-01-15 12:37:54 +0000518 assert(isLValue() && "Invalid accessor");
Richard Smith38dce9b2011-11-07 07:31:09 +0000519 return ((const LV*)(const char*)Data)->hasPath();
Richard Smith9a17a682011-11-07 05:07:52 +0000520}
521
522ArrayRef<APValue::LValuePathEntry> APValue::getLValuePath() const {
523 assert(isLValue() && hasLValuePath() && "Invalid accessor");
Richard Smith38dce9b2011-11-07 07:31:09 +0000524 const LV &LVal = *((const LV*)(const char*)Data);
Richard Smith9a17a682011-11-07 05:07:52 +0000525 return ArrayRef<LValuePathEntry>(LVal.getPath(), LVal.PathLength);
526}
527
Richard Smith1bf9a9e2011-11-12 22:28:03 +0000528void APValue::setLValue(LValueBase B, const CharUnits &O, NoLValuePath) {
Richard Smith9a17a682011-11-07 05:07:52 +0000529 assert(isLValue() && "Invalid accessor");
530 LV &LVal = *((LV*)(char*)Data);
Richard Smithe24f5fc2011-11-17 22:56:20 +0000531 LVal.BaseAndIsOnePastTheEnd.setPointer(B);
532 LVal.BaseAndIsOnePastTheEnd.setInt(false);
Richard Smith9a17a682011-11-07 05:07:52 +0000533 LVal.Offset = O;
Richard Smithe24f5fc2011-11-17 22:56:20 +0000534 LVal.resizePath((unsigned)-1);
Richard Smith9a17a682011-11-07 05:07:52 +0000535}
536
Richard Smith1bf9a9e2011-11-12 22:28:03 +0000537void APValue::setLValue(LValueBase B, const CharUnits &O,
Richard Smithe24f5fc2011-11-17 22:56:20 +0000538 ArrayRef<LValuePathEntry> Path, bool IsOnePastTheEnd) {
Richard Smith9a17a682011-11-07 05:07:52 +0000539 assert(isLValue() && "Invalid accessor");
540 LV &LVal = *((LV*)(char*)Data);
Richard Smithe24f5fc2011-11-17 22:56:20 +0000541 LVal.BaseAndIsOnePastTheEnd.setPointer(B);
542 LVal.BaseAndIsOnePastTheEnd.setInt(IsOnePastTheEnd);
Richard Smith9a17a682011-11-07 05:07:52 +0000543 LVal.Offset = O;
Richard Smithe24f5fc2011-11-17 22:56:20 +0000544 LVal.resizePath(Path.size());
Richard Smith9a17a682011-11-07 05:07:52 +0000545 memcpy(LVal.getPath(), Path.data(), Path.size() * sizeof(LValuePathEntry));
Ken Dycka7305832010-01-15 12:37:54 +0000546}
547
Richard Smithe24f5fc2011-11-17 22:56:20 +0000548const ValueDecl *APValue::getMemberPointerDecl() const {
549 assert(isMemberPointer() && "Invalid accessor");
550 const MemberPointerData &MPD = *((const MemberPointerData*)(const char*)Data);
551 return MPD.MemberAndIsDerivedMember.getPointer();
552}
553
554bool APValue::isMemberPointerToDerivedMember() const {
555 assert(isMemberPointer() && "Invalid accessor");
556 const MemberPointerData &MPD = *((const MemberPointerData*)(const char*)Data);
557 return MPD.MemberAndIsDerivedMember.getInt();
558}
559
560ArrayRef<const CXXRecordDecl*> APValue::getMemberPointerPath() const {
561 assert(isMemberPointer() && "Invalid accessor");
562 const MemberPointerData &MPD = *((const MemberPointerData*)(const char*)Data);
563 return ArrayRef<const CXXRecordDecl*>(MPD.getPath(), MPD.PathLength);
564}
565
Ken Dycka7305832010-01-15 12:37:54 +0000566void APValue::MakeLValue() {
567 assert(isUninit() && "Bad state change");
Richard Smith9a17a682011-11-07 05:07:52 +0000568 assert(sizeof(LV) <= MaxSize && "LV too big");
Ken Dycka7305832010-01-15 12:37:54 +0000569 new ((void*)(char*)Data) LV();
570 Kind = LValue;
571}
Richard Smithcc5d4f62011-11-07 09:22:26 +0000572
573void APValue::MakeArray(unsigned InitElts, unsigned Size) {
574 assert(isUninit() && "Bad state change");
575 new ((void*)(char*)Data) Arr(InitElts, Size);
576 Kind = Array;
577}
Richard Smithe24f5fc2011-11-17 22:56:20 +0000578
579void APValue::MakeMemberPointer(const ValueDecl *Member, bool IsDerivedMember,
580 ArrayRef<const CXXRecordDecl*> Path) {
581 assert(isUninit() && "Bad state change");
582 MemberPointerData *MPD = new ((void*)(char*)Data) MemberPointerData;
583 Kind = MemberPointer;
584 MPD->MemberAndIsDerivedMember.setPointer(Member);
585 MPD->MemberAndIsDerivedMember.setInt(IsDerivedMember);
586 MPD->resizePath(Path.size());
587 memcpy(MPD->getPath(), Path.data(), Path.size()*sizeof(const CXXRecordDecl*));
588}