blob: 4e17d3b69ebf6b3519245d0824fbde1997bc4d49 [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;
Richard Smith83587db2012-02-15 02:18:13 +000031 unsigned CallIndex;
Ken Dycka7305832010-01-15 12:37:54 +000032 };
33}
34
Richard Smith9a17a682011-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 Smithe24f5fc2011-11-17 22:56:20 +000048 ~LV() { resizePath(0); }
Richard Smith9a17a682011-11-07 05:07:52 +000049
Richard Smithe24f5fc2011-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 Smith9a17a682011-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 Smith38dce9b2011-11-07 07:31:09 +000064 const LValuePathEntry *getPath() const {
65 return hasPathPtr() ? PathPtr : Path;
66 }
Richard Smith9a17a682011-11-07 05:07:52 +000067};
68
Richard Smithe24f5fc2011-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 Smithcc5d4f62011-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 Smith180f4792011-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
Chris Lattner64c34f12008-11-16 07:46:48 +0000125const APValue &APValue::operator=(const APValue &RHS) {
Richard Smith180f4792011-11-10 06:34:14 +0000126 if (this == &RHS)
127 return *this;
Richard Smithe24f5fc2011-11-17 22:56:20 +0000128 if (Kind != RHS.Kind || Kind == Array || Kind == Struct ||
129 Kind == MemberPointer) {
Chris Lattner64c34f12008-11-16 07:46:48 +0000130 MakeUninit();
131 if (RHS.isInt())
132 MakeInt();
133 else if (RHS.isFloat())
134 MakeFloat();
Nate Begeman3d309f92009-01-18 01:01:34 +0000135 else if (RHS.isVector())
136 MakeVector();
Chris Lattner64c34f12008-11-16 07:46:48 +0000137 else if (RHS.isComplexInt())
138 MakeComplexInt();
139 else if (RHS.isComplexFloat())
140 MakeComplexFloat();
141 else if (RHS.isLValue())
142 MakeLValue();
Richard Smithcc5d4f62011-11-07 09:22:26 +0000143 else if (RHS.isArray())
144 MakeArray(RHS.getArrayInitializedElts(), RHS.getArraySize());
Richard Smith180f4792011-11-10 06:34:14 +0000145 else if (RHS.isStruct())
146 MakeStruct(RHS.getStructNumBases(), RHS.getStructNumFields());
147 else if (RHS.isUnion())
148 MakeUnion();
Richard Smithe24f5fc2011-11-17 22:56:20 +0000149 else if (RHS.isMemberPointer())
150 MakeMemberPointer(RHS.getMemberPointerDecl(),
151 RHS.isMemberPointerToDerivedMember(),
152 RHS.getMemberPointerPath());
Eli Friedman65639282012-01-04 23:13:47 +0000153 else if (RHS.isAddrLabelDiff())
154 MakeAddrLabelDiff();
Chris Lattner64c34f12008-11-16 07:46:48 +0000155 }
156 if (isInt())
157 setInt(RHS.getInt());
158 else if (isFloat())
159 setFloat(RHS.getFloat());
Nate Begeman3d309f92009-01-18 01:01:34 +0000160 else if (isVector())
Dan Gohmancb421fa2010-04-19 16:39:44 +0000161 setVector(((const Vec *)(const char *)RHS.Data)->Elts,
162 RHS.getVectorLength());
Chris Lattner64c34f12008-11-16 07:46:48 +0000163 else if (isComplexInt())
164 setComplexInt(RHS.getComplexIntReal(), RHS.getComplexIntImag());
165 else if (isComplexFloat())
166 setComplexFloat(RHS.getComplexFloatReal(), RHS.getComplexFloatImag());
Richard Smith9a17a682011-11-07 05:07:52 +0000167 else if (isLValue()) {
168 if (RHS.hasLValuePath())
Richard Smithe24f5fc2011-11-17 22:56:20 +0000169 setLValue(RHS.getLValueBase(), RHS.getLValueOffset(), RHS.getLValuePath(),
Richard Smith83587db2012-02-15 02:18:13 +0000170 RHS.isLValueOnePastTheEnd(), RHS.getLValueCallIndex());
Richard Smith9a17a682011-11-07 05:07:52 +0000171 else
Richard Smith83587db2012-02-15 02:18:13 +0000172 setLValue(RHS.getLValueBase(), RHS.getLValueOffset(), NoLValuePath(),
173 RHS.getLValueCallIndex());
Richard Smithcc5d4f62011-11-07 09:22:26 +0000174 } else if (isArray()) {
175 for (unsigned I = 0, N = RHS.getArrayInitializedElts(); I != N; ++I)
176 getArrayInitializedElt(I) = RHS.getArrayInitializedElt(I);
177 if (RHS.hasArrayFiller())
178 getArrayFiller() = RHS.getArrayFiller();
Richard Smith180f4792011-11-10 06:34:14 +0000179 } else if (isStruct()) {
180 for (unsigned I = 0, N = RHS.getStructNumBases(); I != N; ++I)
181 getStructBase(I) = RHS.getStructBase(I);
182 for (unsigned I = 0, N = RHS.getStructNumFields(); I != N; ++I)
183 getStructField(I) = RHS.getStructField(I);
Eli Friedman65639282012-01-04 23:13:47 +0000184 } else if (isUnion()) {
Richard Smith180f4792011-11-10 06:34:14 +0000185 setUnion(RHS.getUnionField(), RHS.getUnionValue());
Eli Friedman65639282012-01-04 23:13:47 +0000186 } else if (isAddrLabelDiff()) {
187 setAddrLabelDiff(RHS.getAddrLabelDiffLHS(), RHS.getAddrLabelDiffRHS());
188 }
Chris Lattner64c34f12008-11-16 07:46:48 +0000189 return *this;
190}
191
192void APValue::MakeUninit() {
193 if (Kind == Int)
Douglas Gregor98300462009-09-08 19:57:33 +0000194 ((APSInt*)(char*)Data)->~APSInt();
Chris Lattner64c34f12008-11-16 07:46:48 +0000195 else if (Kind == Float)
Douglas Gregor98300462009-09-08 19:57:33 +0000196 ((APFloat*)(char*)Data)->~APFloat();
Nate Begeman3d309f92009-01-18 01:01:34 +0000197 else if (Kind == Vector)
Douglas Gregor98300462009-09-08 19:57:33 +0000198 ((Vec*)(char*)Data)->~Vec();
Chris Lattner64c34f12008-11-16 07:46:48 +0000199 else if (Kind == ComplexInt)
Douglas Gregor98300462009-09-08 19:57:33 +0000200 ((ComplexAPSInt*)(char*)Data)->~ComplexAPSInt();
Chris Lattner64c34f12008-11-16 07:46:48 +0000201 else if (Kind == ComplexFloat)
Douglas Gregor98300462009-09-08 19:57:33 +0000202 ((ComplexAPFloat*)(char*)Data)->~ComplexAPFloat();
Richard Smithcc5d4f62011-11-07 09:22:26 +0000203 else if (Kind == LValue)
Douglas Gregor98300462009-09-08 19:57:33 +0000204 ((LV*)(char*)Data)->~LV();
Richard Smithcc5d4f62011-11-07 09:22:26 +0000205 else if (Kind == Array)
206 ((Arr*)(char*)Data)->~Arr();
Richard Smith180f4792011-11-10 06:34:14 +0000207 else if (Kind == Struct)
208 ((StructData*)(char*)Data)->~StructData();
209 else if (Kind == Union)
210 ((UnionData*)(char*)Data)->~UnionData();
Richard Smithe24f5fc2011-11-17 22:56:20 +0000211 else if (Kind == MemberPointer)
212 ((MemberPointerData*)(char*)Data)->~MemberPointerData();
Eli Friedman65639282012-01-04 23:13:47 +0000213 else if (Kind == AddrLabelDiff)
214 ((AddrLabelDiffData*)(char*)Data)->~AddrLabelDiffData();
Nate Begeman3d309f92009-01-18 01:01:34 +0000215 Kind = Uninitialized;
Chris Lattner64c34f12008-11-16 07:46:48 +0000216}
217
218void APValue::dump() const {
Richard Smith08d6e032011-12-16 19:06:07 +0000219 dump(llvm::errs());
Chris Lattner64c34f12008-11-16 07:46:48 +0000220 llvm::errs() << '\n';
Chris Lattner64c34f12008-11-16 07:46:48 +0000221}
222
223static double GetApproxValue(const llvm::APFloat &F) {
224 llvm::APFloat V = F;
225 bool ignored;
226 V.convert(llvm::APFloat::IEEEdouble, llvm::APFloat::rmNearestTiesToEven,
227 &ignored);
228 return V.convertToDouble();
229}
230
Richard Smith08d6e032011-12-16 19:06:07 +0000231void APValue::dump(raw_ostream &OS) const {
Chris Lattner64c34f12008-11-16 07:46:48 +0000232 switch (getKind()) {
Chris Lattner64c34f12008-11-16 07:46:48 +0000233 case Uninitialized:
234 OS << "Uninitialized";
235 return;
236 case Int:
237 OS << "Int: " << getInt();
238 return;
239 case Float:
240 OS << "Float: " << GetApproxValue(getFloat());
241 return;
Nate Begeman3d309f92009-01-18 01:01:34 +0000242 case Vector:
Richard Smith08d6e032011-12-16 19:06:07 +0000243 OS << "Vector: ";
244 getVectorElt(0).dump(OS);
245 for (unsigned i = 1; i != getVectorLength(); ++i) {
246 OS << ", ";
247 getVectorElt(i).dump(OS);
248 }
Nate Begeman3d309f92009-01-18 01:01:34 +0000249 return;
Chris Lattner64c34f12008-11-16 07:46:48 +0000250 case ComplexInt:
251 OS << "ComplexInt: " << getComplexIntReal() << ", " << getComplexIntImag();
252 return;
253 case ComplexFloat:
254 OS << "ComplexFloat: " << GetApproxValue(getComplexFloatReal())
255 << ", " << GetApproxValue(getComplexFloatImag());
Richard Smithcc5d4f62011-11-07 09:22:26 +0000256 return;
Chris Lattner64c34f12008-11-16 07:46:48 +0000257 case LValue:
258 OS << "LValue: <todo>";
259 return;
Richard Smithcc5d4f62011-11-07 09:22:26 +0000260 case Array:
261 OS << "Array: ";
262 for (unsigned I = 0, N = getArrayInitializedElts(); I != N; ++I) {
Richard Smith08d6e032011-12-16 19:06:07 +0000263 getArrayInitializedElt(I).dump(OS);
Richard Smithcc5d4f62011-11-07 09:22:26 +0000264 if (I != getArraySize() - 1) OS << ", ";
265 }
Richard Smith08d6e032011-12-16 19:06:07 +0000266 if (hasArrayFiller()) {
267 OS << getArraySize() - getArrayInitializedElts() << " x ";
268 getArrayFiller().dump(OS);
269 }
Richard Smithcc5d4f62011-11-07 09:22:26 +0000270 return;
Richard Smith180f4792011-11-10 06:34:14 +0000271 case Struct:
272 OS << "Struct ";
273 if (unsigned N = getStructNumBases()) {
Richard Smith08d6e032011-12-16 19:06:07 +0000274 OS << " bases: ";
275 getStructBase(0).dump(OS);
276 for (unsigned I = 1; I != N; ++I) {
277 OS << ", ";
278 getStructBase(I).dump(OS);
279 }
Richard Smith180f4792011-11-10 06:34:14 +0000280 }
281 if (unsigned N = getStructNumFields()) {
Richard Smith08d6e032011-12-16 19:06:07 +0000282 OS << " fields: ";
283 getStructField(0).dump(OS);
284 for (unsigned I = 1; I != N; ++I) {
285 OS << ", ";
286 getStructField(I).dump(OS);
287 }
Richard Smith180f4792011-11-10 06:34:14 +0000288 }
289 return;
290 case Union:
Richard Smith08d6e032011-12-16 19:06:07 +0000291 OS << "Union: ";
292 getUnionValue().dump(OS);
Richard Smith180f4792011-11-10 06:34:14 +0000293 return;
Richard Smithe24f5fc2011-11-17 22:56:20 +0000294 case MemberPointer:
295 OS << "MemberPointer: <todo>";
296 return;
Eli Friedman65639282012-01-04 23:13:47 +0000297 case AddrLabelDiff:
298 OS << "AddrLabelDiff: <todo>";
299 return;
Chris Lattner64c34f12008-11-16 07:46:48 +0000300 }
Richard Smith180f4792011-11-10 06:34:14 +0000301 llvm_unreachable("Unknown APValue kind!");
Chris Lattner64c34f12008-11-16 07:46:48 +0000302}
303
Richard Smith08d6e032011-12-16 19:06:07 +0000304void APValue::printPretty(raw_ostream &Out, ASTContext &Ctx, QualType Ty) const{
305 switch (getKind()) {
Jeffrey Yasskin5b106a82011-07-18 16:43:53 +0000306 case APValue::Uninitialized:
Richard Smith08d6e032011-12-16 19:06:07 +0000307 Out << "<uninitialized>";
Richard Smith180f4792011-11-10 06:34:14 +0000308 return;
Jeffrey Yasskin5b106a82011-07-18 16:43:53 +0000309 case APValue::Int:
Richard Smith08d6e032011-12-16 19:06:07 +0000310 Out << getInt();
Richard Smith180f4792011-11-10 06:34:14 +0000311 return;
Jeffrey Yasskin5b106a82011-07-18 16:43:53 +0000312 case APValue::Float:
Richard Smith08d6e032011-12-16 19:06:07 +0000313 Out << GetApproxValue(getFloat());
Richard Smith180f4792011-11-10 06:34:14 +0000314 return;
Richard Smith08d6e032011-12-16 19:06:07 +0000315 case APValue::Vector: {
316 Out << '{';
317 QualType ElemTy = Ty->getAs<VectorType>()->getElementType();
318 getVectorElt(0).printPretty(Out, Ctx, ElemTy);
319 for (unsigned i = 1; i != getVectorLength(); ++i) {
Jeffrey Yasskin5b106a82011-07-18 16:43:53 +0000320 Out << ", ";
Richard Smith08d6e032011-12-16 19:06:07 +0000321 getVectorElt(i).printPretty(Out, Ctx, ElemTy);
Jeffrey Yasskin5b106a82011-07-18 16:43:53 +0000322 }
Richard Smith08d6e032011-12-16 19:06:07 +0000323 Out << '}';
Richard Smith180f4792011-11-10 06:34:14 +0000324 return;
Richard Smith08d6e032011-12-16 19:06:07 +0000325 }
Jeffrey Yasskin5b106a82011-07-18 16:43:53 +0000326 case APValue::ComplexInt:
Richard Smith08d6e032011-12-16 19:06:07 +0000327 Out << getComplexIntReal() << "+" << getComplexIntImag() << "i";
Richard Smith180f4792011-11-10 06:34:14 +0000328 return;
Jeffrey Yasskin5b106a82011-07-18 16:43:53 +0000329 case APValue::ComplexFloat:
Richard Smith08d6e032011-12-16 19:06:07 +0000330 Out << GetApproxValue(getComplexFloatReal()) << "+"
331 << GetApproxValue(getComplexFloatImag()) << "i";
Richard Smith180f4792011-11-10 06:34:14 +0000332 return;
Richard Smith08d6e032011-12-16 19:06:07 +0000333 case APValue::LValue: {
334 LValueBase Base = getLValueBase();
335 if (!Base) {
336 Out << "0";
337 return;
Richard Smithcc5d4f62011-11-07 09:22:26 +0000338 }
Richard Smith08d6e032011-12-16 19:06:07 +0000339
340 bool IsReference = Ty->isReferenceType();
341 QualType InnerTy
342 = IsReference ? Ty.getNonReferenceType() : Ty->getPointeeType();
343
344 if (!hasLValuePath()) {
345 // No lvalue path: just print the offset.
346 CharUnits O = getLValueOffset();
347 CharUnits S = Ctx.getTypeSizeInChars(InnerTy);
348 if (!O.isZero()) {
349 if (IsReference)
350 Out << "*(";
351 if (O % S) {
352 Out << "(char*)";
353 S = CharUnits::One();
354 }
355 Out << '&';
356 } else if (!IsReference)
357 Out << '&';
358
359 if (const ValueDecl *VD = Base.dyn_cast<const ValueDecl*>())
360 Out << *VD;
361 else
362 Base.get<const Expr*>()->printPretty(Out, Ctx, 0,
363 Ctx.getPrintingPolicy());
364 if (!O.isZero()) {
365 Out << " + " << (O / S);
366 if (IsReference)
367 Out << ')';
368 }
369 return;
370 }
371
372 // We have an lvalue path. Print it out nicely.
373 if (!IsReference)
374 Out << '&';
375 else if (isLValueOnePastTheEnd())
376 Out << "*(&";
377
378 QualType ElemTy;
379 if (const ValueDecl *VD = Base.dyn_cast<const ValueDecl*>()) {
380 Out << *VD;
381 ElemTy = VD->getType();
382 } else {
383 const Expr *E = Base.get<const Expr*>();
384 E->printPretty(Out, Ctx, 0,Ctx.getPrintingPolicy());
385 ElemTy = E->getType();
386 }
387
388 ArrayRef<LValuePathEntry> Path = getLValuePath();
389 const CXXRecordDecl *CastToBase = 0;
390 for (unsigned I = 0, N = Path.size(); I != N; ++I) {
391 if (ElemTy->getAs<RecordType>()) {
392 // The lvalue refers to a class type, so the next path entry is a base
393 // or member.
394 const Decl *BaseOrMember =
395 BaseOrMemberType::getFromOpaqueValue(Path[I].BaseOrMember).getPointer();
396 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(BaseOrMember)) {
397 CastToBase = RD;
398 ElemTy = Ctx.getRecordType(RD);
399 } else {
400 const ValueDecl *VD = cast<ValueDecl>(BaseOrMember);
401 Out << ".";
402 if (CastToBase)
403 Out << *CastToBase << "::";
404 Out << *VD;
405 ElemTy = VD->getType();
406 }
407 } else {
408 // The lvalue must refer to an array.
409 Out << '[' << Path[I].ArrayIndex << ']';
410 ElemTy = Ctx.getAsArrayType(ElemTy)->getElementType();
411 }
412 }
413
414 // Handle formatting of one-past-the-end lvalues.
415 if (isLValueOnePastTheEnd()) {
416 // FIXME: If CastToBase is non-0, we should prefix the output with
417 // "(CastToBase*)".
418 Out << " + 1";
419 if (IsReference)
420 Out << ')';
421 }
Richard Smith180f4792011-11-10 06:34:14 +0000422 return;
Richard Smith08d6e032011-12-16 19:06:07 +0000423 }
424 case APValue::Array: {
425 const ArrayType *AT = Ctx.getAsArrayType(Ty);
426 QualType ElemTy = AT->getElementType();
Richard Smith180f4792011-11-10 06:34:14 +0000427 Out << '{';
Richard Smith08d6e032011-12-16 19:06:07 +0000428 if (unsigned N = getArrayInitializedElts()) {
429 getArrayInitializedElt(0).printPretty(Out, Ctx, ElemTy);
430 for (unsigned I = 1; I != N; ++I) {
Richard Smith180f4792011-11-10 06:34:14 +0000431 Out << ", ";
Richard Smith08d6e032011-12-16 19:06:07 +0000432 if (I == 10) {
433 // Avoid printing out the entire contents of large arrays.
434 Out << "...";
435 break;
436 }
437 getArrayInitializedElt(I).printPretty(Out, Ctx, ElemTy);
438 }
Richard Smith180f4792011-11-10 06:34:14 +0000439 }
440 Out << '}';
441 return;
Richard Smith08d6e032011-12-16 19:06:07 +0000442 }
443 case APValue::Struct: {
444 Out << '{';
445 const RecordDecl *RD = Ty->getAs<RecordType>()->getDecl();
446 bool First = true;
447 if (unsigned N = getStructNumBases()) {
448 const CXXRecordDecl *CD = cast<CXXRecordDecl>(RD);
449 CXXRecordDecl::base_class_const_iterator BI = CD->bases_begin();
450 for (unsigned I = 0; I != N; ++I, ++BI) {
451 assert(BI != CD->bases_end());
452 if (!First)
453 Out << ", ";
454 getStructBase(I).printPretty(Out, Ctx, BI->getType());
455 First = false;
456 }
457 }
458 for (RecordDecl::field_iterator FI = RD->field_begin();
459 FI != RD->field_end(); ++FI) {
460 if (!First)
461 Out << ", ";
462 if ((*FI)->isUnnamedBitfield()) continue;
463 getStructField((*FI)->getFieldIndex()).
464 printPretty(Out, Ctx, (*FI)->getType());
465 First = false;
466 }
467 Out << '}';
468 return;
469 }
Richard Smith180f4792011-11-10 06:34:14 +0000470 case APValue::Union:
Richard Smith08d6e032011-12-16 19:06:07 +0000471 Out << '{';
472 if (const FieldDecl *FD = getUnionField()) {
473 Out << "." << *FD << " = ";
474 getUnionValue().printPretty(Out, Ctx, FD->getType());
475 }
476 Out << '}';
Richard Smith180f4792011-11-10 06:34:14 +0000477 return;
Richard Smithe24f5fc2011-11-17 22:56:20 +0000478 case APValue::MemberPointer:
Richard Smith08d6e032011-12-16 19:06:07 +0000479 // FIXME: This is not enough to unambiguously identify the member in a
480 // multiple-inheritance scenario.
481 if (const ValueDecl *VD = getMemberPointerDecl()) {
482 Out << '&' << *cast<CXXRecordDecl>(VD->getDeclContext()) << "::" << *VD;
483 return;
484 }
485 Out << "0";
Richard Smithe24f5fc2011-11-17 22:56:20 +0000486 return;
Eli Friedman65639282012-01-04 23:13:47 +0000487 case APValue::AddrLabelDiff:
488 Out << "&&" << getAddrLabelDiffLHS()->getLabel()->getName();
489 Out << " - ";
490 Out << "&&" << getAddrLabelDiffRHS()->getLabel()->getName();
491 return;
Jeffrey Yasskin5b106a82011-07-18 16:43:53 +0000492 }
Richard Smith180f4792011-11-10 06:34:14 +0000493 llvm_unreachable("Unknown APValue kind!");
Jeffrey Yasskin5b106a82011-07-18 16:43:53 +0000494}
495
Richard Smith08d6e032011-12-16 19:06:07 +0000496std::string APValue::getAsString(ASTContext &Ctx, QualType Ty) const {
497 std::string Result;
498 llvm::raw_string_ostream Out(Result);
499 printPretty(Out, Ctx, Ty);
Eli Friedmand9ce41e2011-12-16 22:12:23 +0000500 Out.flush();
Richard Smith08d6e032011-12-16 19:06:07 +0000501 return Result;
Jeffrey Yasskin5b106a82011-07-18 16:43:53 +0000502}
503
Richard Smith1bf9a9e2011-11-12 22:28:03 +0000504const APValue::LValueBase APValue::getLValueBase() const {
Ken Dycka7305832010-01-15 12:37:54 +0000505 assert(isLValue() && "Invalid accessor");
Richard Smithe24f5fc2011-11-17 22:56:20 +0000506 return ((const LV*)(const void*)Data)->BaseAndIsOnePastTheEnd.getPointer();
507}
508
509bool APValue::isLValueOnePastTheEnd() const {
510 assert(isLValue() && "Invalid accessor");
511 return ((const LV*)(const void*)Data)->BaseAndIsOnePastTheEnd.getInt();
Ken Dycka7305832010-01-15 12:37:54 +0000512}
513
Richard Smith47a1eed2011-10-29 20:57:55 +0000514CharUnits &APValue::getLValueOffset() {
515 assert(isLValue() && "Invalid accessor");
516 return ((LV*)(void*)Data)->Offset;
Ken Dycka7305832010-01-15 12:37:54 +0000517}
518
Richard Smith9a17a682011-11-07 05:07:52 +0000519bool APValue::hasLValuePath() const {
Ken Dycka7305832010-01-15 12:37:54 +0000520 assert(isLValue() && "Invalid accessor");
Richard Smith38dce9b2011-11-07 07:31:09 +0000521 return ((const LV*)(const char*)Data)->hasPath();
Richard Smith9a17a682011-11-07 05:07:52 +0000522}
523
524ArrayRef<APValue::LValuePathEntry> APValue::getLValuePath() const {
525 assert(isLValue() && hasLValuePath() && "Invalid accessor");
Richard Smith38dce9b2011-11-07 07:31:09 +0000526 const LV &LVal = *((const LV*)(const char*)Data);
Richard Smith9a17a682011-11-07 05:07:52 +0000527 return ArrayRef<LValuePathEntry>(LVal.getPath(), LVal.PathLength);
528}
529
Richard Smith83587db2012-02-15 02:18:13 +0000530unsigned APValue::getLValueCallIndex() const {
531 assert(isLValue() && "Invalid accessor");
532 return ((const LV*)(const char*)Data)->CallIndex;
533}
534
535void APValue::setLValue(LValueBase B, const CharUnits &O, NoLValuePath,
536 unsigned CallIndex) {
Richard Smith9a17a682011-11-07 05:07:52 +0000537 assert(isLValue() && "Invalid accessor");
538 LV &LVal = *((LV*)(char*)Data);
Richard Smithe24f5fc2011-11-17 22:56:20 +0000539 LVal.BaseAndIsOnePastTheEnd.setPointer(B);
540 LVal.BaseAndIsOnePastTheEnd.setInt(false);
Richard Smith9a17a682011-11-07 05:07:52 +0000541 LVal.Offset = O;
Richard Smith83587db2012-02-15 02:18:13 +0000542 LVal.CallIndex = CallIndex;
Richard Smithe24f5fc2011-11-17 22:56:20 +0000543 LVal.resizePath((unsigned)-1);
Richard Smith9a17a682011-11-07 05:07:52 +0000544}
545
Richard Smith1bf9a9e2011-11-12 22:28:03 +0000546void APValue::setLValue(LValueBase B, const CharUnits &O,
Richard Smith83587db2012-02-15 02:18:13 +0000547 ArrayRef<LValuePathEntry> Path, bool IsOnePastTheEnd,
548 unsigned CallIndex) {
Richard Smith9a17a682011-11-07 05:07:52 +0000549 assert(isLValue() && "Invalid accessor");
550 LV &LVal = *((LV*)(char*)Data);
Richard Smithe24f5fc2011-11-17 22:56:20 +0000551 LVal.BaseAndIsOnePastTheEnd.setPointer(B);
552 LVal.BaseAndIsOnePastTheEnd.setInt(IsOnePastTheEnd);
Richard Smith9a17a682011-11-07 05:07:52 +0000553 LVal.Offset = O;
Richard Smith83587db2012-02-15 02:18:13 +0000554 LVal.CallIndex = CallIndex;
Richard Smithe24f5fc2011-11-17 22:56:20 +0000555 LVal.resizePath(Path.size());
Richard Smith9a17a682011-11-07 05:07:52 +0000556 memcpy(LVal.getPath(), Path.data(), Path.size() * sizeof(LValuePathEntry));
Ken Dycka7305832010-01-15 12:37:54 +0000557}
558
Richard Smithe24f5fc2011-11-17 22:56:20 +0000559const ValueDecl *APValue::getMemberPointerDecl() const {
560 assert(isMemberPointer() && "Invalid accessor");
561 const MemberPointerData &MPD = *((const MemberPointerData*)(const char*)Data);
562 return MPD.MemberAndIsDerivedMember.getPointer();
563}
564
565bool APValue::isMemberPointerToDerivedMember() const {
566 assert(isMemberPointer() && "Invalid accessor");
567 const MemberPointerData &MPD = *((const MemberPointerData*)(const char*)Data);
568 return MPD.MemberAndIsDerivedMember.getInt();
569}
570
571ArrayRef<const CXXRecordDecl*> APValue::getMemberPointerPath() const {
572 assert(isMemberPointer() && "Invalid accessor");
573 const MemberPointerData &MPD = *((const MemberPointerData*)(const char*)Data);
574 return ArrayRef<const CXXRecordDecl*>(MPD.getPath(), MPD.PathLength);
575}
576
Ken Dycka7305832010-01-15 12:37:54 +0000577void APValue::MakeLValue() {
578 assert(isUninit() && "Bad state change");
Richard Smith9a17a682011-11-07 05:07:52 +0000579 assert(sizeof(LV) <= MaxSize && "LV too big");
Ken Dycka7305832010-01-15 12:37:54 +0000580 new ((void*)(char*)Data) LV();
581 Kind = LValue;
582}
Richard Smithcc5d4f62011-11-07 09:22:26 +0000583
584void APValue::MakeArray(unsigned InitElts, unsigned Size) {
585 assert(isUninit() && "Bad state change");
586 new ((void*)(char*)Data) Arr(InitElts, Size);
587 Kind = Array;
588}
Richard Smithe24f5fc2011-11-17 22:56:20 +0000589
590void APValue::MakeMemberPointer(const ValueDecl *Member, bool IsDerivedMember,
591 ArrayRef<const CXXRecordDecl*> Path) {
592 assert(isUninit() && "Bad state change");
593 MemberPointerData *MPD = new ((void*)(char*)Data) MemberPointerData;
594 Kind = MemberPointer;
595 MPD->MemberAndIsDerivedMember.setPointer(Member);
596 MPD->MemberAndIsDerivedMember.setInt(IsDerivedMember);
597 MPD->resizePath(Path.size());
598 memcpy(MPD->getPath(), Path.data(), Path.size()*sizeof(const CXXRecordDecl*));
599}