blob: 2a68341e5a1800ef98360cfa45a16bc44f1ffee9 [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());
Chris Lattner64c34f12008-11-16 07:46:48 +0000152 }
153 if (isInt())
154 setInt(RHS.getInt());
155 else if (isFloat())
156 setFloat(RHS.getFloat());
Nate Begeman3d309f92009-01-18 01:01:34 +0000157 else if (isVector())
Dan Gohmancb421fa2010-04-19 16:39:44 +0000158 setVector(((const Vec *)(const char *)RHS.Data)->Elts,
159 RHS.getVectorLength());
Chris Lattner64c34f12008-11-16 07:46:48 +0000160 else if (isComplexInt())
161 setComplexInt(RHS.getComplexIntReal(), RHS.getComplexIntImag());
162 else if (isComplexFloat())
163 setComplexFloat(RHS.getComplexFloatReal(), RHS.getComplexFloatImag());
Richard Smith9a17a682011-11-07 05:07:52 +0000164 else if (isLValue()) {
165 if (RHS.hasLValuePath())
Richard Smithe24f5fc2011-11-17 22:56:20 +0000166 setLValue(RHS.getLValueBase(), RHS.getLValueOffset(), RHS.getLValuePath(),
167 RHS.isLValueOnePastTheEnd());
Richard Smith9a17a682011-11-07 05:07:52 +0000168 else
169 setLValue(RHS.getLValueBase(), RHS.getLValueOffset(), NoLValuePath());
Richard Smithcc5d4f62011-11-07 09:22:26 +0000170 } else if (isArray()) {
171 for (unsigned I = 0, N = RHS.getArrayInitializedElts(); I != N; ++I)
172 getArrayInitializedElt(I) = RHS.getArrayInitializedElt(I);
173 if (RHS.hasArrayFiller())
174 getArrayFiller() = RHS.getArrayFiller();
Richard Smith180f4792011-11-10 06:34:14 +0000175 } else if (isStruct()) {
176 for (unsigned I = 0, N = RHS.getStructNumBases(); I != N; ++I)
177 getStructBase(I) = RHS.getStructBase(I);
178 for (unsigned I = 0, N = RHS.getStructNumFields(); I != N; ++I)
179 getStructField(I) = RHS.getStructField(I);
180 } else if (isUnion())
181 setUnion(RHS.getUnionField(), RHS.getUnionValue());
Chris Lattner64c34f12008-11-16 07:46:48 +0000182 return *this;
183}
184
185void APValue::MakeUninit() {
186 if (Kind == Int)
Douglas Gregor98300462009-09-08 19:57:33 +0000187 ((APSInt*)(char*)Data)->~APSInt();
Chris Lattner64c34f12008-11-16 07:46:48 +0000188 else if (Kind == Float)
Douglas Gregor98300462009-09-08 19:57:33 +0000189 ((APFloat*)(char*)Data)->~APFloat();
Nate Begeman3d309f92009-01-18 01:01:34 +0000190 else if (Kind == Vector)
Douglas Gregor98300462009-09-08 19:57:33 +0000191 ((Vec*)(char*)Data)->~Vec();
Chris Lattner64c34f12008-11-16 07:46:48 +0000192 else if (Kind == ComplexInt)
Douglas Gregor98300462009-09-08 19:57:33 +0000193 ((ComplexAPSInt*)(char*)Data)->~ComplexAPSInt();
Chris Lattner64c34f12008-11-16 07:46:48 +0000194 else if (Kind == ComplexFloat)
Douglas Gregor98300462009-09-08 19:57:33 +0000195 ((ComplexAPFloat*)(char*)Data)->~ComplexAPFloat();
Richard Smithcc5d4f62011-11-07 09:22:26 +0000196 else if (Kind == LValue)
Douglas Gregor98300462009-09-08 19:57:33 +0000197 ((LV*)(char*)Data)->~LV();
Richard Smithcc5d4f62011-11-07 09:22:26 +0000198 else if (Kind == Array)
199 ((Arr*)(char*)Data)->~Arr();
Richard Smith180f4792011-11-10 06:34:14 +0000200 else if (Kind == Struct)
201 ((StructData*)(char*)Data)->~StructData();
202 else if (Kind == Union)
203 ((UnionData*)(char*)Data)->~UnionData();
Richard Smithe24f5fc2011-11-17 22:56:20 +0000204 else if (Kind == MemberPointer)
205 ((MemberPointerData*)(char*)Data)->~MemberPointerData();
Nate Begeman3d309f92009-01-18 01:01:34 +0000206 Kind = Uninitialized;
Chris Lattner64c34f12008-11-16 07:46:48 +0000207}
208
209void APValue::dump() const {
Richard Smith08d6e032011-12-16 19:06:07 +0000210 dump(llvm::errs());
Chris Lattner64c34f12008-11-16 07:46:48 +0000211 llvm::errs() << '\n';
Chris Lattner64c34f12008-11-16 07:46:48 +0000212}
213
214static double GetApproxValue(const llvm::APFloat &F) {
215 llvm::APFloat V = F;
216 bool ignored;
217 V.convert(llvm::APFloat::IEEEdouble, llvm::APFloat::rmNearestTiesToEven,
218 &ignored);
219 return V.convertToDouble();
220}
221
Richard Smith08d6e032011-12-16 19:06:07 +0000222void APValue::dump(raw_ostream &OS) const {
Chris Lattner64c34f12008-11-16 07:46:48 +0000223 switch (getKind()) {
Chris Lattner64c34f12008-11-16 07:46:48 +0000224 case Uninitialized:
225 OS << "Uninitialized";
226 return;
227 case Int:
228 OS << "Int: " << getInt();
229 return;
230 case Float:
231 OS << "Float: " << GetApproxValue(getFloat());
232 return;
Nate Begeman3d309f92009-01-18 01:01:34 +0000233 case Vector:
Richard Smith08d6e032011-12-16 19:06:07 +0000234 OS << "Vector: ";
235 getVectorElt(0).dump(OS);
236 for (unsigned i = 1; i != getVectorLength(); ++i) {
237 OS << ", ";
238 getVectorElt(i).dump(OS);
239 }
Nate Begeman3d309f92009-01-18 01:01:34 +0000240 return;
Chris Lattner64c34f12008-11-16 07:46:48 +0000241 case ComplexInt:
242 OS << "ComplexInt: " << getComplexIntReal() << ", " << getComplexIntImag();
243 return;
244 case ComplexFloat:
245 OS << "ComplexFloat: " << GetApproxValue(getComplexFloatReal())
246 << ", " << GetApproxValue(getComplexFloatImag());
Richard Smithcc5d4f62011-11-07 09:22:26 +0000247 return;
Chris Lattner64c34f12008-11-16 07:46:48 +0000248 case LValue:
249 OS << "LValue: <todo>";
250 return;
Richard Smithcc5d4f62011-11-07 09:22:26 +0000251 case Array:
252 OS << "Array: ";
253 for (unsigned I = 0, N = getArrayInitializedElts(); I != N; ++I) {
Richard Smith08d6e032011-12-16 19:06:07 +0000254 getArrayInitializedElt(I).dump(OS);
Richard Smithcc5d4f62011-11-07 09:22:26 +0000255 if (I != getArraySize() - 1) OS << ", ";
256 }
Richard Smith08d6e032011-12-16 19:06:07 +0000257 if (hasArrayFiller()) {
258 OS << getArraySize() - getArrayInitializedElts() << " x ";
259 getArrayFiller().dump(OS);
260 }
Richard Smithcc5d4f62011-11-07 09:22:26 +0000261 return;
Richard Smith180f4792011-11-10 06:34:14 +0000262 case Struct:
263 OS << "Struct ";
264 if (unsigned N = getStructNumBases()) {
Richard Smith08d6e032011-12-16 19:06:07 +0000265 OS << " bases: ";
266 getStructBase(0).dump(OS);
267 for (unsigned I = 1; I != N; ++I) {
268 OS << ", ";
269 getStructBase(I).dump(OS);
270 }
Richard Smith180f4792011-11-10 06:34:14 +0000271 }
272 if (unsigned N = getStructNumFields()) {
Richard Smith08d6e032011-12-16 19:06:07 +0000273 OS << " fields: ";
274 getStructField(0).dump(OS);
275 for (unsigned I = 1; I != N; ++I) {
276 OS << ", ";
277 getStructField(I).dump(OS);
278 }
Richard Smith180f4792011-11-10 06:34:14 +0000279 }
280 return;
281 case Union:
Richard Smith08d6e032011-12-16 19:06:07 +0000282 OS << "Union: ";
283 getUnionValue().dump(OS);
Richard Smith180f4792011-11-10 06:34:14 +0000284 return;
Richard Smithe24f5fc2011-11-17 22:56:20 +0000285 case MemberPointer:
286 OS << "MemberPointer: <todo>";
287 return;
Chris Lattner64c34f12008-11-16 07:46:48 +0000288 }
Richard Smith180f4792011-11-10 06:34:14 +0000289 llvm_unreachable("Unknown APValue kind!");
Chris Lattner64c34f12008-11-16 07:46:48 +0000290}
291
Richard Smith08d6e032011-12-16 19:06:07 +0000292void APValue::printPretty(raw_ostream &Out, ASTContext &Ctx, QualType Ty) const{
293 switch (getKind()) {
Jeffrey Yasskin5b106a82011-07-18 16:43:53 +0000294 case APValue::Uninitialized:
Richard Smith08d6e032011-12-16 19:06:07 +0000295 Out << "<uninitialized>";
Richard Smith180f4792011-11-10 06:34:14 +0000296 return;
Jeffrey Yasskin5b106a82011-07-18 16:43:53 +0000297 case APValue::Int:
Richard Smith08d6e032011-12-16 19:06:07 +0000298 Out << getInt();
Richard Smith180f4792011-11-10 06:34:14 +0000299 return;
Jeffrey Yasskin5b106a82011-07-18 16:43:53 +0000300 case APValue::Float:
Richard Smith08d6e032011-12-16 19:06:07 +0000301 Out << GetApproxValue(getFloat());
Richard Smith180f4792011-11-10 06:34:14 +0000302 return;
Richard Smith08d6e032011-12-16 19:06:07 +0000303 case APValue::Vector: {
304 Out << '{';
305 QualType ElemTy = Ty->getAs<VectorType>()->getElementType();
306 getVectorElt(0).printPretty(Out, Ctx, ElemTy);
307 for (unsigned i = 1; i != getVectorLength(); ++i) {
Jeffrey Yasskin5b106a82011-07-18 16:43:53 +0000308 Out << ", ";
Richard Smith08d6e032011-12-16 19:06:07 +0000309 getVectorElt(i).printPretty(Out, Ctx, ElemTy);
Jeffrey Yasskin5b106a82011-07-18 16:43:53 +0000310 }
Richard Smith08d6e032011-12-16 19:06:07 +0000311 Out << '}';
Richard Smith180f4792011-11-10 06:34:14 +0000312 return;
Richard Smith08d6e032011-12-16 19:06:07 +0000313 }
Jeffrey Yasskin5b106a82011-07-18 16:43:53 +0000314 case APValue::ComplexInt:
Richard Smith08d6e032011-12-16 19:06:07 +0000315 Out << getComplexIntReal() << "+" << getComplexIntImag() << "i";
Richard Smith180f4792011-11-10 06:34:14 +0000316 return;
Jeffrey Yasskin5b106a82011-07-18 16:43:53 +0000317 case APValue::ComplexFloat:
Richard Smith08d6e032011-12-16 19:06:07 +0000318 Out << GetApproxValue(getComplexFloatReal()) << "+"
319 << GetApproxValue(getComplexFloatImag()) << "i";
Richard Smith180f4792011-11-10 06:34:14 +0000320 return;
Richard Smith08d6e032011-12-16 19:06:07 +0000321 case APValue::LValue: {
322 LValueBase Base = getLValueBase();
323 if (!Base) {
324 Out << "0";
325 return;
Richard Smithcc5d4f62011-11-07 09:22:26 +0000326 }
Richard Smith08d6e032011-12-16 19:06:07 +0000327
328 bool IsReference = Ty->isReferenceType();
329 QualType InnerTy
330 = IsReference ? Ty.getNonReferenceType() : Ty->getPointeeType();
331
332 if (!hasLValuePath()) {
333 // No lvalue path: just print the offset.
334 CharUnits O = getLValueOffset();
335 CharUnits S = Ctx.getTypeSizeInChars(InnerTy);
336 if (!O.isZero()) {
337 if (IsReference)
338 Out << "*(";
339 if (O % S) {
340 Out << "(char*)";
341 S = CharUnits::One();
342 }
343 Out << '&';
344 } else if (!IsReference)
345 Out << '&';
346
347 if (const ValueDecl *VD = Base.dyn_cast<const ValueDecl*>())
348 Out << *VD;
349 else
350 Base.get<const Expr*>()->printPretty(Out, Ctx, 0,
351 Ctx.getPrintingPolicy());
352 if (!O.isZero()) {
353 Out << " + " << (O / S);
354 if (IsReference)
355 Out << ')';
356 }
357 return;
358 }
359
360 // We have an lvalue path. Print it out nicely.
361 if (!IsReference)
362 Out << '&';
363 else if (isLValueOnePastTheEnd())
364 Out << "*(&";
365
366 QualType ElemTy;
367 if (const ValueDecl *VD = Base.dyn_cast<const ValueDecl*>()) {
368 Out << *VD;
369 ElemTy = VD->getType();
370 } else {
371 const Expr *E = Base.get<const Expr*>();
372 E->printPretty(Out, Ctx, 0,Ctx.getPrintingPolicy());
373 ElemTy = E->getType();
374 }
375
376 ArrayRef<LValuePathEntry> Path = getLValuePath();
377 const CXXRecordDecl *CastToBase = 0;
378 for (unsigned I = 0, N = Path.size(); I != N; ++I) {
379 if (ElemTy->getAs<RecordType>()) {
380 // The lvalue refers to a class type, so the next path entry is a base
381 // or member.
382 const Decl *BaseOrMember =
383 BaseOrMemberType::getFromOpaqueValue(Path[I].BaseOrMember).getPointer();
384 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(BaseOrMember)) {
385 CastToBase = RD;
386 ElemTy = Ctx.getRecordType(RD);
387 } else {
388 const ValueDecl *VD = cast<ValueDecl>(BaseOrMember);
389 Out << ".";
390 if (CastToBase)
391 Out << *CastToBase << "::";
392 Out << *VD;
393 ElemTy = VD->getType();
394 }
395 } else {
396 // The lvalue must refer to an array.
397 Out << '[' << Path[I].ArrayIndex << ']';
398 ElemTy = Ctx.getAsArrayType(ElemTy)->getElementType();
399 }
400 }
401
402 // Handle formatting of one-past-the-end lvalues.
403 if (isLValueOnePastTheEnd()) {
404 // FIXME: If CastToBase is non-0, we should prefix the output with
405 // "(CastToBase*)".
406 Out << " + 1";
407 if (IsReference)
408 Out << ')';
409 }
Richard Smith180f4792011-11-10 06:34:14 +0000410 return;
Richard Smith08d6e032011-12-16 19:06:07 +0000411 }
412 case APValue::Array: {
413 const ArrayType *AT = Ctx.getAsArrayType(Ty);
414 QualType ElemTy = AT->getElementType();
Richard Smith180f4792011-11-10 06:34:14 +0000415 Out << '{';
Richard Smith08d6e032011-12-16 19:06:07 +0000416 if (unsigned N = getArrayInitializedElts()) {
417 getArrayInitializedElt(0).printPretty(Out, Ctx, ElemTy);
418 for (unsigned I = 1; I != N; ++I) {
Richard Smith180f4792011-11-10 06:34:14 +0000419 Out << ", ";
Richard Smith08d6e032011-12-16 19:06:07 +0000420 if (I == 10) {
421 // Avoid printing out the entire contents of large arrays.
422 Out << "...";
423 break;
424 }
425 getArrayInitializedElt(I).printPretty(Out, Ctx, ElemTy);
426 }
Richard Smith180f4792011-11-10 06:34:14 +0000427 }
428 Out << '}';
429 return;
Richard Smith08d6e032011-12-16 19:06:07 +0000430 }
431 case APValue::Struct: {
432 Out << '{';
433 const RecordDecl *RD = Ty->getAs<RecordType>()->getDecl();
434 bool First = true;
435 if (unsigned N = getStructNumBases()) {
436 const CXXRecordDecl *CD = cast<CXXRecordDecl>(RD);
437 CXXRecordDecl::base_class_const_iterator BI = CD->bases_begin();
438 for (unsigned I = 0; I != N; ++I, ++BI) {
439 assert(BI != CD->bases_end());
440 if (!First)
441 Out << ", ";
442 getStructBase(I).printPretty(Out, Ctx, BI->getType());
443 First = false;
444 }
445 }
446 for (RecordDecl::field_iterator FI = RD->field_begin();
447 FI != RD->field_end(); ++FI) {
448 if (!First)
449 Out << ", ";
450 if ((*FI)->isUnnamedBitfield()) continue;
451 getStructField((*FI)->getFieldIndex()).
452 printPretty(Out, Ctx, (*FI)->getType());
453 First = false;
454 }
455 Out << '}';
456 return;
457 }
Richard Smith180f4792011-11-10 06:34:14 +0000458 case APValue::Union:
Richard Smith08d6e032011-12-16 19:06:07 +0000459 Out << '{';
460 if (const FieldDecl *FD = getUnionField()) {
461 Out << "." << *FD << " = ";
462 getUnionValue().printPretty(Out, Ctx, FD->getType());
463 }
464 Out << '}';
Richard Smith180f4792011-11-10 06:34:14 +0000465 return;
Richard Smithe24f5fc2011-11-17 22:56:20 +0000466 case APValue::MemberPointer:
Richard Smith08d6e032011-12-16 19:06:07 +0000467 // FIXME: This is not enough to unambiguously identify the member in a
468 // multiple-inheritance scenario.
469 if (const ValueDecl *VD = getMemberPointerDecl()) {
470 Out << '&' << *cast<CXXRecordDecl>(VD->getDeclContext()) << "::" << *VD;
471 return;
472 }
473 Out << "0";
Richard Smithe24f5fc2011-11-17 22:56:20 +0000474 return;
Jeffrey Yasskin5b106a82011-07-18 16:43:53 +0000475 }
Richard Smith180f4792011-11-10 06:34:14 +0000476 llvm_unreachable("Unknown APValue kind!");
Jeffrey Yasskin5b106a82011-07-18 16:43:53 +0000477}
478
Richard Smith08d6e032011-12-16 19:06:07 +0000479std::string APValue::getAsString(ASTContext &Ctx, QualType Ty) const {
480 std::string Result;
481 llvm::raw_string_ostream Out(Result);
482 printPretty(Out, Ctx, Ty);
Eli Friedmand9ce41e2011-12-16 22:12:23 +0000483 Out.flush();
Richard Smith08d6e032011-12-16 19:06:07 +0000484 return Result;
Jeffrey Yasskin5b106a82011-07-18 16:43:53 +0000485}
486
Richard Smith1bf9a9e2011-11-12 22:28:03 +0000487const APValue::LValueBase APValue::getLValueBase() const {
Ken Dycka7305832010-01-15 12:37:54 +0000488 assert(isLValue() && "Invalid accessor");
Richard Smithe24f5fc2011-11-17 22:56:20 +0000489 return ((const LV*)(const void*)Data)->BaseAndIsOnePastTheEnd.getPointer();
490}
491
492bool APValue::isLValueOnePastTheEnd() const {
493 assert(isLValue() && "Invalid accessor");
494 return ((const LV*)(const void*)Data)->BaseAndIsOnePastTheEnd.getInt();
Ken Dycka7305832010-01-15 12:37:54 +0000495}
496
Richard Smith47a1eed2011-10-29 20:57:55 +0000497CharUnits &APValue::getLValueOffset() {
498 assert(isLValue() && "Invalid accessor");
499 return ((LV*)(void*)Data)->Offset;
Ken Dycka7305832010-01-15 12:37:54 +0000500}
501
Richard Smith9a17a682011-11-07 05:07:52 +0000502bool APValue::hasLValuePath() const {
Ken Dycka7305832010-01-15 12:37:54 +0000503 assert(isLValue() && "Invalid accessor");
Richard Smith38dce9b2011-11-07 07:31:09 +0000504 return ((const LV*)(const char*)Data)->hasPath();
Richard Smith9a17a682011-11-07 05:07:52 +0000505}
506
507ArrayRef<APValue::LValuePathEntry> APValue::getLValuePath() const {
508 assert(isLValue() && hasLValuePath() && "Invalid accessor");
Richard Smith38dce9b2011-11-07 07:31:09 +0000509 const LV &LVal = *((const LV*)(const char*)Data);
Richard Smith9a17a682011-11-07 05:07:52 +0000510 return ArrayRef<LValuePathEntry>(LVal.getPath(), LVal.PathLength);
511}
512
Richard Smith1bf9a9e2011-11-12 22:28:03 +0000513void APValue::setLValue(LValueBase B, const CharUnits &O, NoLValuePath) {
Richard Smith9a17a682011-11-07 05:07:52 +0000514 assert(isLValue() && "Invalid accessor");
515 LV &LVal = *((LV*)(char*)Data);
Richard Smithe24f5fc2011-11-17 22:56:20 +0000516 LVal.BaseAndIsOnePastTheEnd.setPointer(B);
517 LVal.BaseAndIsOnePastTheEnd.setInt(false);
Richard Smith9a17a682011-11-07 05:07:52 +0000518 LVal.Offset = O;
Richard Smithe24f5fc2011-11-17 22:56:20 +0000519 LVal.resizePath((unsigned)-1);
Richard Smith9a17a682011-11-07 05:07:52 +0000520}
521
Richard Smith1bf9a9e2011-11-12 22:28:03 +0000522void APValue::setLValue(LValueBase B, const CharUnits &O,
Richard Smithe24f5fc2011-11-17 22:56:20 +0000523 ArrayRef<LValuePathEntry> Path, bool IsOnePastTheEnd) {
Richard Smith9a17a682011-11-07 05:07:52 +0000524 assert(isLValue() && "Invalid accessor");
525 LV &LVal = *((LV*)(char*)Data);
Richard Smithe24f5fc2011-11-17 22:56:20 +0000526 LVal.BaseAndIsOnePastTheEnd.setPointer(B);
527 LVal.BaseAndIsOnePastTheEnd.setInt(IsOnePastTheEnd);
Richard Smith9a17a682011-11-07 05:07:52 +0000528 LVal.Offset = O;
Richard Smithe24f5fc2011-11-17 22:56:20 +0000529 LVal.resizePath(Path.size());
Richard Smith9a17a682011-11-07 05:07:52 +0000530 memcpy(LVal.getPath(), Path.data(), Path.size() * sizeof(LValuePathEntry));
Ken Dycka7305832010-01-15 12:37:54 +0000531}
532
Richard Smithe24f5fc2011-11-17 22:56:20 +0000533const ValueDecl *APValue::getMemberPointerDecl() const {
534 assert(isMemberPointer() && "Invalid accessor");
535 const MemberPointerData &MPD = *((const MemberPointerData*)(const char*)Data);
536 return MPD.MemberAndIsDerivedMember.getPointer();
537}
538
539bool APValue::isMemberPointerToDerivedMember() const {
540 assert(isMemberPointer() && "Invalid accessor");
541 const MemberPointerData &MPD = *((const MemberPointerData*)(const char*)Data);
542 return MPD.MemberAndIsDerivedMember.getInt();
543}
544
545ArrayRef<const CXXRecordDecl*> APValue::getMemberPointerPath() const {
546 assert(isMemberPointer() && "Invalid accessor");
547 const MemberPointerData &MPD = *((const MemberPointerData*)(const char*)Data);
548 return ArrayRef<const CXXRecordDecl*>(MPD.getPath(), MPD.PathLength);
549}
550
Ken Dycka7305832010-01-15 12:37:54 +0000551void APValue::MakeLValue() {
552 assert(isUninit() && "Bad state change");
Richard Smith9a17a682011-11-07 05:07:52 +0000553 assert(sizeof(LV) <= MaxSize && "LV too big");
Ken Dycka7305832010-01-15 12:37:54 +0000554 new ((void*)(char*)Data) LV();
555 Kind = LValue;
556}
Richard Smithcc5d4f62011-11-07 09:22:26 +0000557
558void APValue::MakeArray(unsigned InitElts, unsigned Size) {
559 assert(isUninit() && "Bad state change");
560 new ((void*)(char*)Data) Arr(InitElts, Size);
561 Kind = Array;
562}
Richard Smithe24f5fc2011-11-17 22:56:20 +0000563
564void APValue::MakeMemberPointer(const ValueDecl *Member, bool IsDerivedMember,
565 ArrayRef<const CXXRecordDecl*> Path) {
566 assert(isUninit() && "Bad state change");
567 MemberPointerData *MPD = new ((void*)(char*)Data) MemberPointerData;
568 Kind = MemberPointer;
569 MPD->MemberAndIsDerivedMember.setPointer(Member);
570 MPD->MemberAndIsDerivedMember.setInt(IsDerivedMember);
571 MPD->resizePath(Path.size());
572 memcpy(MPD->getPath(), Path.data(), Path.size()*sizeof(const CXXRecordDecl*));
573}