blob: 2d7c9bd7864ad6477945fb512df0a97b298c410b [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
Richard Smith0069b842012-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 Lattner64c34f12008-11-16 07:46:48 +0000131 setInt(RHS.getInt());
Richard Smith0069b842012-03-10 00:28:11 +0000132 break;
133 case Float:
134 MakeFloat();
Chris Lattner64c34f12008-11-16 07:46:48 +0000135 setFloat(RHS.getFloat());
Richard Smith0069b842012-03-10 00:28:11 +0000136 break;
137 case Vector:
138 MakeVector();
Dan Gohmancb421fa2010-04-19 16:39:44 +0000139 setVector(((const Vec *)(const char *)RHS.Data)->Elts,
140 RHS.getVectorLength());
Richard Smith0069b842012-03-10 00:28:11 +0000141 break;
142 case ComplexInt:
143 MakeComplexInt();
Chris Lattner64c34f12008-11-16 07:46:48 +0000144 setComplexInt(RHS.getComplexIntReal(), RHS.getComplexIntImag());
Richard Smith0069b842012-03-10 00:28:11 +0000145 break;
146 case ComplexFloat:
147 MakeComplexFloat();
Chris Lattner64c34f12008-11-16 07:46:48 +0000148 setComplexFloat(RHS.getComplexFloatReal(), RHS.getComplexFloatImag());
Richard Smith0069b842012-03-10 00:28:11 +0000149 break;
150 case LValue:
151 MakeLValue();
Richard Smith9a17a682011-11-07 05:07:52 +0000152 if (RHS.hasLValuePath())
Richard Smithe24f5fc2011-11-17 22:56:20 +0000153 setLValue(RHS.getLValueBase(), RHS.getLValueOffset(), RHS.getLValuePath(),
Richard Smith83587db2012-02-15 02:18:13 +0000154 RHS.isLValueOnePastTheEnd(), RHS.getLValueCallIndex());
Richard Smith9a17a682011-11-07 05:07:52 +0000155 else
Richard Smith83587db2012-02-15 02:18:13 +0000156 setLValue(RHS.getLValueBase(), RHS.getLValueOffset(), NoLValuePath(),
157 RHS.getLValueCallIndex());
Richard Smith0069b842012-03-10 00:28:11 +0000158 break;
159 case Array:
160 MakeArray(RHS.getArrayInitializedElts(), RHS.getArraySize());
Richard Smithcc5d4f62011-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 Smith0069b842012-03-10 00:28:11 +0000165 break;
166 case Struct:
167 MakeStruct(RHS.getStructNumBases(), RHS.getStructNumFields());
Richard Smith180f4792011-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 Smith0069b842012-03-10 00:28:11 +0000172 break;
173 case Union:
174 MakeUnion();
Richard Smith180f4792011-11-10 06:34:14 +0000175 setUnion(RHS.getUnionField(), RHS.getUnionValue());
Richard Smith0069b842012-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 Friedman65639282012-01-04 23:13:47 +0000184 setAddrLabelDiff(RHS.getAddrLabelDiffLHS(), RHS.getAddrLabelDiffRHS());
Richard Smith0069b842012-03-10 00:28:11 +0000185 break;
Eli Friedman65639282012-01-04 23:13:47 +0000186 }
Chris Lattner64c34f12008-11-16 07:46:48 +0000187}
188
Daniel Dunbar7a8c7582012-03-08 20:28:55 +0000189void APValue::DestroyDataAndMakeUninit() {
Chris Lattner64c34f12008-11-16 07:46:48 +0000190 if (Kind == Int)
Douglas Gregor98300462009-09-08 19:57:33 +0000191 ((APSInt*)(char*)Data)->~APSInt();
Chris Lattner64c34f12008-11-16 07:46:48 +0000192 else if (Kind == Float)
Douglas Gregor98300462009-09-08 19:57:33 +0000193 ((APFloat*)(char*)Data)->~APFloat();
Nate Begeman3d309f92009-01-18 01:01:34 +0000194 else if (Kind == Vector)
Douglas Gregor98300462009-09-08 19:57:33 +0000195 ((Vec*)(char*)Data)->~Vec();
Chris Lattner64c34f12008-11-16 07:46:48 +0000196 else if (Kind == ComplexInt)
Douglas Gregor98300462009-09-08 19:57:33 +0000197 ((ComplexAPSInt*)(char*)Data)->~ComplexAPSInt();
Chris Lattner64c34f12008-11-16 07:46:48 +0000198 else if (Kind == ComplexFloat)
Douglas Gregor98300462009-09-08 19:57:33 +0000199 ((ComplexAPFloat*)(char*)Data)->~ComplexAPFloat();
Richard Smithcc5d4f62011-11-07 09:22:26 +0000200 else if (Kind == LValue)
Douglas Gregor98300462009-09-08 19:57:33 +0000201 ((LV*)(char*)Data)->~LV();
Richard Smithcc5d4f62011-11-07 09:22:26 +0000202 else if (Kind == Array)
203 ((Arr*)(char*)Data)->~Arr();
Richard Smith180f4792011-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 Smithe24f5fc2011-11-17 22:56:20 +0000208 else if (Kind == MemberPointer)
209 ((MemberPointerData*)(char*)Data)->~MemberPointerData();
Eli Friedman65639282012-01-04 23:13:47 +0000210 else if (Kind == AddrLabelDiff)
211 ((AddrLabelDiffData*)(char*)Data)->~AddrLabelDiffData();
Nate Begeman3d309f92009-01-18 01:01:34 +0000212 Kind = Uninitialized;
Chris Lattner64c34f12008-11-16 07:46:48 +0000213}
214
Richard Smith0069b842012-03-10 00:28:11 +0000215void APValue::swap(APValue &RHS) {
216 std::swap(Kind, RHS.Kind);
217 char TmpData[MaxSize];
218 memcpy(TmpData, Data, MaxSize);
219 memcpy(Data, RHS.Data, MaxSize);
220 memcpy(RHS.Data, TmpData, MaxSize);
221}
222
Chris Lattner64c34f12008-11-16 07:46:48 +0000223void APValue::dump() const {
Richard Smith08d6e032011-12-16 19:06:07 +0000224 dump(llvm::errs());
Chris Lattner64c34f12008-11-16 07:46:48 +0000225 llvm::errs() << '\n';
Chris Lattner64c34f12008-11-16 07:46:48 +0000226}
227
228static double GetApproxValue(const llvm::APFloat &F) {
229 llvm::APFloat V = F;
230 bool ignored;
231 V.convert(llvm::APFloat::IEEEdouble, llvm::APFloat::rmNearestTiesToEven,
232 &ignored);
233 return V.convertToDouble();
234}
235
Richard Smith08d6e032011-12-16 19:06:07 +0000236void APValue::dump(raw_ostream &OS) const {
Chris Lattner64c34f12008-11-16 07:46:48 +0000237 switch (getKind()) {
Chris Lattner64c34f12008-11-16 07:46:48 +0000238 case Uninitialized:
239 OS << "Uninitialized";
240 return;
241 case Int:
242 OS << "Int: " << getInt();
243 return;
244 case Float:
245 OS << "Float: " << GetApproxValue(getFloat());
246 return;
Nate Begeman3d309f92009-01-18 01:01:34 +0000247 case Vector:
Richard Smith08d6e032011-12-16 19:06:07 +0000248 OS << "Vector: ";
249 getVectorElt(0).dump(OS);
250 for (unsigned i = 1; i != getVectorLength(); ++i) {
251 OS << ", ";
252 getVectorElt(i).dump(OS);
253 }
Nate Begeman3d309f92009-01-18 01:01:34 +0000254 return;
Chris Lattner64c34f12008-11-16 07:46:48 +0000255 case ComplexInt:
256 OS << "ComplexInt: " << getComplexIntReal() << ", " << getComplexIntImag();
257 return;
258 case ComplexFloat:
259 OS << "ComplexFloat: " << GetApproxValue(getComplexFloatReal())
260 << ", " << GetApproxValue(getComplexFloatImag());
Richard Smithcc5d4f62011-11-07 09:22:26 +0000261 return;
Chris Lattner64c34f12008-11-16 07:46:48 +0000262 case LValue:
263 OS << "LValue: <todo>";
264 return;
Richard Smithcc5d4f62011-11-07 09:22:26 +0000265 case Array:
266 OS << "Array: ";
267 for (unsigned I = 0, N = getArrayInitializedElts(); I != N; ++I) {
Richard Smith08d6e032011-12-16 19:06:07 +0000268 getArrayInitializedElt(I).dump(OS);
Richard Smithcc5d4f62011-11-07 09:22:26 +0000269 if (I != getArraySize() - 1) OS << ", ";
270 }
Richard Smith08d6e032011-12-16 19:06:07 +0000271 if (hasArrayFiller()) {
272 OS << getArraySize() - getArrayInitializedElts() << " x ";
273 getArrayFiller().dump(OS);
274 }
Richard Smithcc5d4f62011-11-07 09:22:26 +0000275 return;
Richard Smith180f4792011-11-10 06:34:14 +0000276 case Struct:
277 OS << "Struct ";
278 if (unsigned N = getStructNumBases()) {
Richard Smith08d6e032011-12-16 19:06:07 +0000279 OS << " bases: ";
280 getStructBase(0).dump(OS);
281 for (unsigned I = 1; I != N; ++I) {
282 OS << ", ";
283 getStructBase(I).dump(OS);
284 }
Richard Smith180f4792011-11-10 06:34:14 +0000285 }
286 if (unsigned N = getStructNumFields()) {
Richard Smith08d6e032011-12-16 19:06:07 +0000287 OS << " fields: ";
288 getStructField(0).dump(OS);
289 for (unsigned I = 1; I != N; ++I) {
290 OS << ", ";
291 getStructField(I).dump(OS);
292 }
Richard Smith180f4792011-11-10 06:34:14 +0000293 }
294 return;
295 case Union:
Richard Smith08d6e032011-12-16 19:06:07 +0000296 OS << "Union: ";
297 getUnionValue().dump(OS);
Richard Smith180f4792011-11-10 06:34:14 +0000298 return;
Richard Smithe24f5fc2011-11-17 22:56:20 +0000299 case MemberPointer:
300 OS << "MemberPointer: <todo>";
301 return;
Eli Friedman65639282012-01-04 23:13:47 +0000302 case AddrLabelDiff:
303 OS << "AddrLabelDiff: <todo>";
304 return;
Chris Lattner64c34f12008-11-16 07:46:48 +0000305 }
Richard Smith180f4792011-11-10 06:34:14 +0000306 llvm_unreachable("Unknown APValue kind!");
Chris Lattner64c34f12008-11-16 07:46:48 +0000307}
308
Richard Smith08d6e032011-12-16 19:06:07 +0000309void APValue::printPretty(raw_ostream &Out, ASTContext &Ctx, QualType Ty) const{
310 switch (getKind()) {
Jeffrey Yasskin5b106a82011-07-18 16:43:53 +0000311 case APValue::Uninitialized:
Richard Smith08d6e032011-12-16 19:06:07 +0000312 Out << "<uninitialized>";
Richard Smith180f4792011-11-10 06:34:14 +0000313 return;
Jeffrey Yasskin5b106a82011-07-18 16:43:53 +0000314 case APValue::Int:
Richard Smithf6028062012-03-23 23:55:39 +0000315 if (Ty->isBooleanType())
316 Out << (getInt().getBoolValue() ? "true" : "false");
317 else
318 Out << getInt();
Richard Smith180f4792011-11-10 06:34:14 +0000319 return;
Jeffrey Yasskin5b106a82011-07-18 16:43:53 +0000320 case APValue::Float:
Richard Smith08d6e032011-12-16 19:06:07 +0000321 Out << GetApproxValue(getFloat());
Richard Smith180f4792011-11-10 06:34:14 +0000322 return;
Richard Smith08d6e032011-12-16 19:06:07 +0000323 case APValue::Vector: {
324 Out << '{';
325 QualType ElemTy = Ty->getAs<VectorType>()->getElementType();
326 getVectorElt(0).printPretty(Out, Ctx, ElemTy);
327 for (unsigned i = 1; i != getVectorLength(); ++i) {
Jeffrey Yasskin5b106a82011-07-18 16:43:53 +0000328 Out << ", ";
Richard Smith08d6e032011-12-16 19:06:07 +0000329 getVectorElt(i).printPretty(Out, Ctx, ElemTy);
Jeffrey Yasskin5b106a82011-07-18 16:43:53 +0000330 }
Richard Smith08d6e032011-12-16 19:06:07 +0000331 Out << '}';
Richard Smith180f4792011-11-10 06:34:14 +0000332 return;
Richard Smith08d6e032011-12-16 19:06:07 +0000333 }
Jeffrey Yasskin5b106a82011-07-18 16:43:53 +0000334 case APValue::ComplexInt:
Richard Smith08d6e032011-12-16 19:06:07 +0000335 Out << getComplexIntReal() << "+" << getComplexIntImag() << "i";
Richard Smith180f4792011-11-10 06:34:14 +0000336 return;
Jeffrey Yasskin5b106a82011-07-18 16:43:53 +0000337 case APValue::ComplexFloat:
Richard Smith08d6e032011-12-16 19:06:07 +0000338 Out << GetApproxValue(getComplexFloatReal()) << "+"
339 << GetApproxValue(getComplexFloatImag()) << "i";
Richard Smith180f4792011-11-10 06:34:14 +0000340 return;
Richard Smith08d6e032011-12-16 19:06:07 +0000341 case APValue::LValue: {
342 LValueBase Base = getLValueBase();
343 if (!Base) {
344 Out << "0";
345 return;
Richard Smithcc5d4f62011-11-07 09:22:26 +0000346 }
Richard Smith08d6e032011-12-16 19:06:07 +0000347
348 bool IsReference = Ty->isReferenceType();
349 QualType InnerTy
350 = IsReference ? Ty.getNonReferenceType() : Ty->getPointeeType();
351
352 if (!hasLValuePath()) {
353 // No lvalue path: just print the offset.
354 CharUnits O = getLValueOffset();
355 CharUnits S = Ctx.getTypeSizeInChars(InnerTy);
356 if (!O.isZero()) {
357 if (IsReference)
358 Out << "*(";
359 if (O % S) {
360 Out << "(char*)";
361 S = CharUnits::One();
362 }
363 Out << '&';
364 } else if (!IsReference)
365 Out << '&';
366
367 if (const ValueDecl *VD = Base.dyn_cast<const ValueDecl*>())
368 Out << *VD;
369 else
Richard Smithd1420c62012-08-16 03:56:14 +0000370 Base.get<const Expr*>()->printPretty(Out, 0, Ctx.getPrintingPolicy());
Richard Smith08d6e032011-12-16 19:06:07 +0000371 if (!O.isZero()) {
372 Out << " + " << (O / S);
373 if (IsReference)
374 Out << ')';
375 }
376 return;
377 }
378
379 // We have an lvalue path. Print it out nicely.
380 if (!IsReference)
381 Out << '&';
382 else if (isLValueOnePastTheEnd())
383 Out << "*(&";
384
385 QualType ElemTy;
386 if (const ValueDecl *VD = Base.dyn_cast<const ValueDecl*>()) {
387 Out << *VD;
388 ElemTy = VD->getType();
389 } else {
390 const Expr *E = Base.get<const Expr*>();
Richard Smithd1420c62012-08-16 03:56:14 +0000391 E->printPretty(Out, 0, Ctx.getPrintingPolicy());
Richard Smith08d6e032011-12-16 19:06:07 +0000392 ElemTy = E->getType();
393 }
394
395 ArrayRef<LValuePathEntry> Path = getLValuePath();
396 const CXXRecordDecl *CastToBase = 0;
397 for (unsigned I = 0, N = Path.size(); I != N; ++I) {
398 if (ElemTy->getAs<RecordType>()) {
399 // The lvalue refers to a class type, so the next path entry is a base
400 // or member.
401 const Decl *BaseOrMember =
402 BaseOrMemberType::getFromOpaqueValue(Path[I].BaseOrMember).getPointer();
403 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(BaseOrMember)) {
404 CastToBase = RD;
405 ElemTy = Ctx.getRecordType(RD);
406 } else {
407 const ValueDecl *VD = cast<ValueDecl>(BaseOrMember);
408 Out << ".";
409 if (CastToBase)
410 Out << *CastToBase << "::";
411 Out << *VD;
412 ElemTy = VD->getType();
413 }
414 } else {
415 // The lvalue must refer to an array.
416 Out << '[' << Path[I].ArrayIndex << ']';
417 ElemTy = Ctx.getAsArrayType(ElemTy)->getElementType();
418 }
419 }
420
421 // Handle formatting of one-past-the-end lvalues.
422 if (isLValueOnePastTheEnd()) {
423 // FIXME: If CastToBase is non-0, we should prefix the output with
424 // "(CastToBase*)".
425 Out << " + 1";
426 if (IsReference)
427 Out << ')';
428 }
Richard Smith180f4792011-11-10 06:34:14 +0000429 return;
Richard Smith08d6e032011-12-16 19:06:07 +0000430 }
431 case APValue::Array: {
432 const ArrayType *AT = Ctx.getAsArrayType(Ty);
433 QualType ElemTy = AT->getElementType();
Richard Smith180f4792011-11-10 06:34:14 +0000434 Out << '{';
Richard Smith08d6e032011-12-16 19:06:07 +0000435 if (unsigned N = getArrayInitializedElts()) {
436 getArrayInitializedElt(0).printPretty(Out, Ctx, ElemTy);
437 for (unsigned I = 1; I != N; ++I) {
Richard Smith180f4792011-11-10 06:34:14 +0000438 Out << ", ";
Richard Smith08d6e032011-12-16 19:06:07 +0000439 if (I == 10) {
440 // Avoid printing out the entire contents of large arrays.
441 Out << "...";
442 break;
443 }
444 getArrayInitializedElt(I).printPretty(Out, Ctx, ElemTy);
445 }
Richard Smith180f4792011-11-10 06:34:14 +0000446 }
447 Out << '}';
448 return;
Richard Smith08d6e032011-12-16 19:06:07 +0000449 }
450 case APValue::Struct: {
451 Out << '{';
452 const RecordDecl *RD = Ty->getAs<RecordType>()->getDecl();
453 bool First = true;
454 if (unsigned N = getStructNumBases()) {
455 const CXXRecordDecl *CD = cast<CXXRecordDecl>(RD);
456 CXXRecordDecl::base_class_const_iterator BI = CD->bases_begin();
457 for (unsigned I = 0; I != N; ++I, ++BI) {
458 assert(BI != CD->bases_end());
459 if (!First)
460 Out << ", ";
461 getStructBase(I).printPretty(Out, Ctx, BI->getType());
462 First = false;
463 }
464 }
465 for (RecordDecl::field_iterator FI = RD->field_begin();
466 FI != RD->field_end(); ++FI) {
467 if (!First)
468 Out << ", ";
David Blaikie262bc182012-04-30 02:36:29 +0000469 if (FI->isUnnamedBitfield()) continue;
470 getStructField(FI->getFieldIndex()).
471 printPretty(Out, Ctx, FI->getType());
Richard Smith08d6e032011-12-16 19:06:07 +0000472 First = false;
473 }
474 Out << '}';
475 return;
476 }
Richard Smith180f4792011-11-10 06:34:14 +0000477 case APValue::Union:
Richard Smith08d6e032011-12-16 19:06:07 +0000478 Out << '{';
479 if (const FieldDecl *FD = getUnionField()) {
480 Out << "." << *FD << " = ";
481 getUnionValue().printPretty(Out, Ctx, FD->getType());
482 }
483 Out << '}';
Richard Smith180f4792011-11-10 06:34:14 +0000484 return;
Richard Smithe24f5fc2011-11-17 22:56:20 +0000485 case APValue::MemberPointer:
Richard Smith08d6e032011-12-16 19:06:07 +0000486 // FIXME: This is not enough to unambiguously identify the member in a
487 // multiple-inheritance scenario.
488 if (const ValueDecl *VD = getMemberPointerDecl()) {
489 Out << '&' << *cast<CXXRecordDecl>(VD->getDeclContext()) << "::" << *VD;
490 return;
491 }
492 Out << "0";
Richard Smithe24f5fc2011-11-17 22:56:20 +0000493 return;
Eli Friedman65639282012-01-04 23:13:47 +0000494 case APValue::AddrLabelDiff:
495 Out << "&&" << getAddrLabelDiffLHS()->getLabel()->getName();
496 Out << " - ";
497 Out << "&&" << getAddrLabelDiffRHS()->getLabel()->getName();
498 return;
Jeffrey Yasskin5b106a82011-07-18 16:43:53 +0000499 }
Richard Smith180f4792011-11-10 06:34:14 +0000500 llvm_unreachable("Unknown APValue kind!");
Jeffrey Yasskin5b106a82011-07-18 16:43:53 +0000501}
502
Richard Smith08d6e032011-12-16 19:06:07 +0000503std::string APValue::getAsString(ASTContext &Ctx, QualType Ty) const {
504 std::string Result;
505 llvm::raw_string_ostream Out(Result);
506 printPretty(Out, Ctx, Ty);
Eli Friedmand9ce41e2011-12-16 22:12:23 +0000507 Out.flush();
Richard Smith08d6e032011-12-16 19:06:07 +0000508 return Result;
Jeffrey Yasskin5b106a82011-07-18 16:43:53 +0000509}
510
Richard Smith1bf9a9e2011-11-12 22:28:03 +0000511const APValue::LValueBase APValue::getLValueBase() const {
Ken Dycka7305832010-01-15 12:37:54 +0000512 assert(isLValue() && "Invalid accessor");
Richard Smithe24f5fc2011-11-17 22:56:20 +0000513 return ((const LV*)(const void*)Data)->BaseAndIsOnePastTheEnd.getPointer();
514}
515
516bool APValue::isLValueOnePastTheEnd() const {
517 assert(isLValue() && "Invalid accessor");
518 return ((const LV*)(const void*)Data)->BaseAndIsOnePastTheEnd.getInt();
Ken Dycka7305832010-01-15 12:37:54 +0000519}
520
Richard Smith47a1eed2011-10-29 20:57:55 +0000521CharUnits &APValue::getLValueOffset() {
522 assert(isLValue() && "Invalid accessor");
523 return ((LV*)(void*)Data)->Offset;
Ken Dycka7305832010-01-15 12:37:54 +0000524}
525
Richard Smith9a17a682011-11-07 05:07:52 +0000526bool APValue::hasLValuePath() const {
Ken Dycka7305832010-01-15 12:37:54 +0000527 assert(isLValue() && "Invalid accessor");
Richard Smith38dce9b2011-11-07 07:31:09 +0000528 return ((const LV*)(const char*)Data)->hasPath();
Richard Smith9a17a682011-11-07 05:07:52 +0000529}
530
531ArrayRef<APValue::LValuePathEntry> APValue::getLValuePath() const {
532 assert(isLValue() && hasLValuePath() && "Invalid accessor");
Richard Smith38dce9b2011-11-07 07:31:09 +0000533 const LV &LVal = *((const LV*)(const char*)Data);
Richard Smith9a17a682011-11-07 05:07:52 +0000534 return ArrayRef<LValuePathEntry>(LVal.getPath(), LVal.PathLength);
535}
536
Richard Smith83587db2012-02-15 02:18:13 +0000537unsigned APValue::getLValueCallIndex() const {
538 assert(isLValue() && "Invalid accessor");
539 return ((const LV*)(const char*)Data)->CallIndex;
540}
541
542void APValue::setLValue(LValueBase B, const CharUnits &O, NoLValuePath,
543 unsigned CallIndex) {
Richard Smith9a17a682011-11-07 05:07:52 +0000544 assert(isLValue() && "Invalid accessor");
545 LV &LVal = *((LV*)(char*)Data);
Richard Smithe24f5fc2011-11-17 22:56:20 +0000546 LVal.BaseAndIsOnePastTheEnd.setPointer(B);
547 LVal.BaseAndIsOnePastTheEnd.setInt(false);
Richard Smith9a17a682011-11-07 05:07:52 +0000548 LVal.Offset = O;
Richard Smith83587db2012-02-15 02:18:13 +0000549 LVal.CallIndex = CallIndex;
Richard Smithe24f5fc2011-11-17 22:56:20 +0000550 LVal.resizePath((unsigned)-1);
Richard Smith9a17a682011-11-07 05:07:52 +0000551}
552
Richard Smith1bf9a9e2011-11-12 22:28:03 +0000553void APValue::setLValue(LValueBase B, const CharUnits &O,
Richard Smith83587db2012-02-15 02:18:13 +0000554 ArrayRef<LValuePathEntry> Path, bool IsOnePastTheEnd,
555 unsigned CallIndex) {
Richard Smith9a17a682011-11-07 05:07:52 +0000556 assert(isLValue() && "Invalid accessor");
557 LV &LVal = *((LV*)(char*)Data);
Richard Smithe24f5fc2011-11-17 22:56:20 +0000558 LVal.BaseAndIsOnePastTheEnd.setPointer(B);
559 LVal.BaseAndIsOnePastTheEnd.setInt(IsOnePastTheEnd);
Richard Smith9a17a682011-11-07 05:07:52 +0000560 LVal.Offset = O;
Richard Smith83587db2012-02-15 02:18:13 +0000561 LVal.CallIndex = CallIndex;
Richard Smithe24f5fc2011-11-17 22:56:20 +0000562 LVal.resizePath(Path.size());
Richard Smith9a17a682011-11-07 05:07:52 +0000563 memcpy(LVal.getPath(), Path.data(), Path.size() * sizeof(LValuePathEntry));
Ken Dycka7305832010-01-15 12:37:54 +0000564}
565
Richard Smithe24f5fc2011-11-17 22:56:20 +0000566const ValueDecl *APValue::getMemberPointerDecl() const {
567 assert(isMemberPointer() && "Invalid accessor");
568 const MemberPointerData &MPD = *((const MemberPointerData*)(const char*)Data);
569 return MPD.MemberAndIsDerivedMember.getPointer();
570}
571
572bool APValue::isMemberPointerToDerivedMember() const {
573 assert(isMemberPointer() && "Invalid accessor");
574 const MemberPointerData &MPD = *((const MemberPointerData*)(const char*)Data);
575 return MPD.MemberAndIsDerivedMember.getInt();
576}
577
578ArrayRef<const CXXRecordDecl*> APValue::getMemberPointerPath() const {
579 assert(isMemberPointer() && "Invalid accessor");
580 const MemberPointerData &MPD = *((const MemberPointerData*)(const char*)Data);
581 return ArrayRef<const CXXRecordDecl*>(MPD.getPath(), MPD.PathLength);
582}
583
Ken Dycka7305832010-01-15 12:37:54 +0000584void APValue::MakeLValue() {
585 assert(isUninit() && "Bad state change");
Richard Smith9a17a682011-11-07 05:07:52 +0000586 assert(sizeof(LV) <= MaxSize && "LV too big");
Ken Dycka7305832010-01-15 12:37:54 +0000587 new ((void*)(char*)Data) LV();
588 Kind = LValue;
589}
Richard Smithcc5d4f62011-11-07 09:22:26 +0000590
591void APValue::MakeArray(unsigned InitElts, unsigned Size) {
592 assert(isUninit() && "Bad state change");
593 new ((void*)(char*)Data) Arr(InitElts, Size);
594 Kind = Array;
595}
Richard Smithe24f5fc2011-11-17 22:56:20 +0000596
597void APValue::MakeMemberPointer(const ValueDecl *Member, bool IsDerivedMember,
598 ArrayRef<const CXXRecordDecl*> Path) {
599 assert(isUninit() && "Bad state change");
600 MemberPointerData *MPD = new ((void*)(char*)Data) MemberPointerData;
601 Kind = MemberPointer;
602 MPD->MemberAndIsDerivedMember.setPointer(Member);
603 MPD->MemberAndIsDerivedMember.setInt(IsDerivedMember);
604 MPD->resizePath(Path.size());
605 memcpy(MPD->getPath(), Path.data(), Path.size()*sizeof(const CXXRecordDecl*));
606}