164c34f1c6f613eef02a7b488f8edadbe7a8650a8Chris Lattner//===--- APValue.cpp - Union class for APFloat/APSInt/Complex -------------===//
264c34f1c6f613eef02a7b488f8edadbe7a8650a8Chris Lattner//
364c34f1c6f613eef02a7b488f8edadbe7a8650a8Chris Lattner//                     The LLVM Compiler Infrastructure
464c34f1c6f613eef02a7b488f8edadbe7a8650a8Chris Lattner//
564c34f1c6f613eef02a7b488f8edadbe7a8650a8Chris Lattner// This file is distributed under the University of Illinois Open Source
664c34f1c6f613eef02a7b488f8edadbe7a8650a8Chris Lattner// License. See LICENSE.TXT for details.
764c34f1c6f613eef02a7b488f8edadbe7a8650a8Chris Lattner//
864c34f1c6f613eef02a7b488f8edadbe7a8650a8Chris Lattner//===----------------------------------------------------------------------===//
964c34f1c6f613eef02a7b488f8edadbe7a8650a8Chris Lattner//
1064c34f1c6f613eef02a7b488f8edadbe7a8650a8Chris Lattner//  This file implements the APValue class.
1164c34f1c6f613eef02a7b488f8edadbe7a8650a8Chris Lattner//
1264c34f1c6f613eef02a7b488f8edadbe7a8650a8Chris Lattner//===----------------------------------------------------------------------===//
1364c34f1c6f613eef02a7b488f8edadbe7a8650a8Chris Lattner
1464c34f1c6f613eef02a7b488f8edadbe7a8650a8Chris Lattner#include "clang/AST/APValue.h"
1508d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith#include "clang/AST/ASTContext.h"
16a73058324197b7bdfd19307965954f626e26199dKen Dyck#include "clang/AST/CharUnits.h"
1708d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith#include "clang/AST/DeclCXX.h"
1808d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith#include "clang/AST/Expr.h"
1908d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith#include "clang/AST/Type.h"
205b106a872d66f57522b1cc6d1b67f93704409114Jeffrey Yasskin#include "clang/Basic/Diagnostic.h"
215b106a872d66f57522b1cc6d1b67f93704409114Jeffrey Yasskin#include "llvm/ADT/SmallString.h"
229fe8c74a93ac8e92512615c5f83e7a328b3b0544David Blaikie#include "llvm/Support/ErrorHandling.h"
2355fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "llvm/Support/raw_ostream.h"
2464c34f1c6f613eef02a7b488f8edadbe7a8650a8Chris Lattnerusing namespace clang;
2564c34f1c6f613eef02a7b488f8edadbe7a8650a8Chris Lattner
26a73058324197b7bdfd19307965954f626e26199dKen Dycknamespace {
279a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smith  struct LVBase {
28e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    llvm::PointerIntPair<APValue::LValueBase, 1, bool> BaseAndIsOnePastTheEnd;
29a73058324197b7bdfd19307965954f626e26199dKen Dyck    CharUnits Offset;
309a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smith    unsigned PathLength;
3183587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith    unsigned CallIndex;
32a73058324197b7bdfd19307965954f626e26199dKen Dyck  };
33a73058324197b7bdfd19307965954f626e26199dKen Dyck}
34a73058324197b7bdfd19307965954f626e26199dKen Dyck
359a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smithstruct APValue::LV : LVBase {
369a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smith  static const unsigned InlinePathSpace =
379a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smith      (MaxSize - sizeof(LVBase)) / sizeof(LValuePathEntry);
389a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smith
399a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smith  /// Path - The sequence of base classes, fields and array indices to follow to
409a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smith  /// walk from Base to the subobject. When performing GCC-style folding, there
419a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smith  /// may not be such a path.
429a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smith  union {
439a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smith    LValuePathEntry Path[InlinePathSpace];
449a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smith    LValuePathEntry *PathPtr;
459a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smith  };
469a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smith
479a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smith  LV() { PathLength = (unsigned)-1; }
48e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  ~LV() { resizePath(0); }
499a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smith
50e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  void resizePath(unsigned Length) {
51e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    if (Length == PathLength)
52e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      return;
53e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    if (hasPathPtr())
54e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      delete [] PathPtr;
55e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    PathLength = Length;
56e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    if (hasPathPtr())
57e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      PathPtr = new LValuePathEntry[Length];
589a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smith  }
599a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smith
609a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smith  bool hasPath() const { return PathLength != (unsigned)-1; }
619a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smith  bool hasPathPtr() const { return hasPath() && PathLength > InlinePathSpace; }
629a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smith
639a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smith  LValuePathEntry *getPath() { return hasPathPtr() ? PathPtr : Path; }
6438dce9b19c3cc85698fb44ea182f9e7fa0fa4a69Richard Smith  const LValuePathEntry *getPath() const {
6538dce9b19c3cc85698fb44ea182f9e7fa0fa4a69Richard Smith    return hasPathPtr() ? PathPtr : Path;
6638dce9b19c3cc85698fb44ea182f9e7fa0fa4a69Richard Smith  }
679a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smith};
689a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smith
69e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smithnamespace {
70e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  struct MemberPointerBase {
71e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    llvm::PointerIntPair<const ValueDecl*, 1, bool> MemberAndIsDerivedMember;
72e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    unsigned PathLength;
73e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  };
74e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith}
75e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
76e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smithstruct APValue::MemberPointerData : MemberPointerBase {
77e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  static const unsigned InlinePathSpace =
78e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      (MaxSize - sizeof(MemberPointerBase)) / sizeof(const CXXRecordDecl*);
79e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  typedef const CXXRecordDecl *PathElem;
80e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  union {
81e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    PathElem Path[InlinePathSpace];
82e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    PathElem *PathPtr;
83e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  };
84e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
85e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  MemberPointerData() { PathLength = 0; }
86e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  ~MemberPointerData() { resizePath(0); }
87e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
88e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  void resizePath(unsigned Length) {
89e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    if (Length == PathLength)
90e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      return;
91e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    if (hasPathPtr())
92e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      delete [] PathPtr;
93e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    PathLength = Length;
94e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    if (hasPathPtr())
95e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      PathPtr = new PathElem[Length];
96e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  }
97e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
98e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  bool hasPathPtr() const { return PathLength > InlinePathSpace; }
99e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
100e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  PathElem *getPath() { return hasPathPtr() ? PathPtr : Path; }
101e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  const PathElem *getPath() const {
102e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    return hasPathPtr() ? PathPtr : Path;
103e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  }
104e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith};
105e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
106cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith// FIXME: Reduce the malloc traffic here.
107cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith
108cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard SmithAPValue::Arr::Arr(unsigned NumElts, unsigned Size) :
109cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith  Elts(new APValue[NumElts + (NumElts != Size ? 1 : 0)]),
110cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith  NumElts(NumElts), ArrSize(Size) {}
111cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard SmithAPValue::Arr::~Arr() { delete [] Elts; }
112cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith
113180f47959a066795cc0f409433023af448bb0328Richard SmithAPValue::StructData::StructData(unsigned NumBases, unsigned NumFields) :
114180f47959a066795cc0f409433023af448bb0328Richard Smith  Elts(new APValue[NumBases+NumFields]),
115180f47959a066795cc0f409433023af448bb0328Richard Smith  NumBases(NumBases), NumFields(NumFields) {}
116180f47959a066795cc0f409433023af448bb0328Richard SmithAPValue::StructData::~StructData() {
117180f47959a066795cc0f409433023af448bb0328Richard Smith  delete [] Elts;
118180f47959a066795cc0f409433023af448bb0328Richard Smith}
119180f47959a066795cc0f409433023af448bb0328Richard Smith
120180f47959a066795cc0f409433023af448bb0328Richard SmithAPValue::UnionData::UnionData() : Field(0), Value(new APValue) {}
121180f47959a066795cc0f409433023af448bb0328Richard SmithAPValue::UnionData::~UnionData () {
122180f47959a066795cc0f409433023af448bb0328Richard Smith  delete Value;
123180f47959a066795cc0f409433023af448bb0328Richard Smith}
124180f47959a066795cc0f409433023af448bb0328Richard Smith
1250069b84c2aa7cc39263e85997b7cb1ed0b132ccdRichard SmithAPValue::APValue(const APValue &RHS) : Kind(Uninitialized) {
1260069b84c2aa7cc39263e85997b7cb1ed0b132ccdRichard Smith  switch (RHS.getKind()) {
1270069b84c2aa7cc39263e85997b7cb1ed0b132ccdRichard Smith  case Uninitialized:
1280069b84c2aa7cc39263e85997b7cb1ed0b132ccdRichard Smith    break;
1290069b84c2aa7cc39263e85997b7cb1ed0b132ccdRichard Smith  case Int:
1300069b84c2aa7cc39263e85997b7cb1ed0b132ccdRichard Smith    MakeInt();
13164c34f1c6f613eef02a7b488f8edadbe7a8650a8Chris Lattner    setInt(RHS.getInt());
1320069b84c2aa7cc39263e85997b7cb1ed0b132ccdRichard Smith    break;
1330069b84c2aa7cc39263e85997b7cb1ed0b132ccdRichard Smith  case Float:
1340069b84c2aa7cc39263e85997b7cb1ed0b132ccdRichard Smith    MakeFloat();
13564c34f1c6f613eef02a7b488f8edadbe7a8650a8Chris Lattner    setFloat(RHS.getFloat());
1360069b84c2aa7cc39263e85997b7cb1ed0b132ccdRichard Smith    break;
1370069b84c2aa7cc39263e85997b7cb1ed0b132ccdRichard Smith  case Vector:
1380069b84c2aa7cc39263e85997b7cb1ed0b132ccdRichard Smith    MakeVector();
139cb421fa690da545b58a720abe5f1c49b166dbde7Dan Gohman    setVector(((const Vec *)(const char *)RHS.Data)->Elts,
140cb421fa690da545b58a720abe5f1c49b166dbde7Dan Gohman              RHS.getVectorLength());
1410069b84c2aa7cc39263e85997b7cb1ed0b132ccdRichard Smith    break;
1420069b84c2aa7cc39263e85997b7cb1ed0b132ccdRichard Smith  case ComplexInt:
1430069b84c2aa7cc39263e85997b7cb1ed0b132ccdRichard Smith    MakeComplexInt();
14464c34f1c6f613eef02a7b488f8edadbe7a8650a8Chris Lattner    setComplexInt(RHS.getComplexIntReal(), RHS.getComplexIntImag());
1450069b84c2aa7cc39263e85997b7cb1ed0b132ccdRichard Smith    break;
1460069b84c2aa7cc39263e85997b7cb1ed0b132ccdRichard Smith  case ComplexFloat:
1470069b84c2aa7cc39263e85997b7cb1ed0b132ccdRichard Smith    MakeComplexFloat();
14864c34f1c6f613eef02a7b488f8edadbe7a8650a8Chris Lattner    setComplexFloat(RHS.getComplexFloatReal(), RHS.getComplexFloatImag());
1490069b84c2aa7cc39263e85997b7cb1ed0b132ccdRichard Smith    break;
1500069b84c2aa7cc39263e85997b7cb1ed0b132ccdRichard Smith  case LValue:
1510069b84c2aa7cc39263e85997b7cb1ed0b132ccdRichard Smith    MakeLValue();
1529a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smith    if (RHS.hasLValuePath())
153e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      setLValue(RHS.getLValueBase(), RHS.getLValueOffset(), RHS.getLValuePath(),
15483587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith                RHS.isLValueOnePastTheEnd(), RHS.getLValueCallIndex());
1559a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smith    else
15683587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith      setLValue(RHS.getLValueBase(), RHS.getLValueOffset(), NoLValuePath(),
15783587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith                RHS.getLValueCallIndex());
1580069b84c2aa7cc39263e85997b7cb1ed0b132ccdRichard Smith    break;
1590069b84c2aa7cc39263e85997b7cb1ed0b132ccdRichard Smith  case Array:
1600069b84c2aa7cc39263e85997b7cb1ed0b132ccdRichard Smith    MakeArray(RHS.getArrayInitializedElts(), RHS.getArraySize());
161cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith    for (unsigned I = 0, N = RHS.getArrayInitializedElts(); I != N; ++I)
162cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith      getArrayInitializedElt(I) = RHS.getArrayInitializedElt(I);
163cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith    if (RHS.hasArrayFiller())
164cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith      getArrayFiller() = RHS.getArrayFiller();
1650069b84c2aa7cc39263e85997b7cb1ed0b132ccdRichard Smith    break;
1660069b84c2aa7cc39263e85997b7cb1ed0b132ccdRichard Smith  case Struct:
1670069b84c2aa7cc39263e85997b7cb1ed0b132ccdRichard Smith    MakeStruct(RHS.getStructNumBases(), RHS.getStructNumFields());
168180f47959a066795cc0f409433023af448bb0328Richard Smith    for (unsigned I = 0, N = RHS.getStructNumBases(); I != N; ++I)
169180f47959a066795cc0f409433023af448bb0328Richard Smith      getStructBase(I) = RHS.getStructBase(I);
170180f47959a066795cc0f409433023af448bb0328Richard Smith    for (unsigned I = 0, N = RHS.getStructNumFields(); I != N; ++I)
171180f47959a066795cc0f409433023af448bb0328Richard Smith      getStructField(I) = RHS.getStructField(I);
1720069b84c2aa7cc39263e85997b7cb1ed0b132ccdRichard Smith    break;
1730069b84c2aa7cc39263e85997b7cb1ed0b132ccdRichard Smith  case Union:
1740069b84c2aa7cc39263e85997b7cb1ed0b132ccdRichard Smith    MakeUnion();
175180f47959a066795cc0f409433023af448bb0328Richard Smith    setUnion(RHS.getUnionField(), RHS.getUnionValue());
1760069b84c2aa7cc39263e85997b7cb1ed0b132ccdRichard Smith    break;
1770069b84c2aa7cc39263e85997b7cb1ed0b132ccdRichard Smith  case MemberPointer:
1780069b84c2aa7cc39263e85997b7cb1ed0b132ccdRichard Smith    MakeMemberPointer(RHS.getMemberPointerDecl(),
1790069b84c2aa7cc39263e85997b7cb1ed0b132ccdRichard Smith                      RHS.isMemberPointerToDerivedMember(),
1800069b84c2aa7cc39263e85997b7cb1ed0b132ccdRichard Smith                      RHS.getMemberPointerPath());
1810069b84c2aa7cc39263e85997b7cb1ed0b132ccdRichard Smith    break;
1820069b84c2aa7cc39263e85997b7cb1ed0b132ccdRichard Smith  case AddrLabelDiff:
1830069b84c2aa7cc39263e85997b7cb1ed0b132ccdRichard Smith    MakeAddrLabelDiff();
18465639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman    setAddrLabelDiff(RHS.getAddrLabelDiffLHS(), RHS.getAddrLabelDiffRHS());
1850069b84c2aa7cc39263e85997b7cb1ed0b132ccdRichard Smith    break;
18665639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman  }
18764c34f1c6f613eef02a7b488f8edadbe7a8650a8Chris Lattner}
18864c34f1c6f613eef02a7b488f8edadbe7a8650a8Chris Lattner
1897a8c758868f00b7fbe105ad2b469a289cfc92b6dDaniel Dunbarvoid APValue::DestroyDataAndMakeUninit() {
19064c34f1c6f613eef02a7b488f8edadbe7a8650a8Chris Lattner  if (Kind == Int)
191983004686a1bfc816f7aeb47bac7e16049550278Douglas Gregor    ((APSInt*)(char*)Data)->~APSInt();
19264c34f1c6f613eef02a7b488f8edadbe7a8650a8Chris Lattner  else if (Kind == Float)
193983004686a1bfc816f7aeb47bac7e16049550278Douglas Gregor    ((APFloat*)(char*)Data)->~APFloat();
1943d309f9d62a6f9f634b869937139d533ccd7265bNate Begeman  else if (Kind == Vector)
195983004686a1bfc816f7aeb47bac7e16049550278Douglas Gregor    ((Vec*)(char*)Data)->~Vec();
19664c34f1c6f613eef02a7b488f8edadbe7a8650a8Chris Lattner  else if (Kind == ComplexInt)
197983004686a1bfc816f7aeb47bac7e16049550278Douglas Gregor    ((ComplexAPSInt*)(char*)Data)->~ComplexAPSInt();
19864c34f1c6f613eef02a7b488f8edadbe7a8650a8Chris Lattner  else if (Kind == ComplexFloat)
199983004686a1bfc816f7aeb47bac7e16049550278Douglas Gregor    ((ComplexAPFloat*)(char*)Data)->~ComplexAPFloat();
200cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith  else if (Kind == LValue)
201983004686a1bfc816f7aeb47bac7e16049550278Douglas Gregor    ((LV*)(char*)Data)->~LV();
202cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith  else if (Kind == Array)
203cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith    ((Arr*)(char*)Data)->~Arr();
204180f47959a066795cc0f409433023af448bb0328Richard Smith  else if (Kind == Struct)
205180f47959a066795cc0f409433023af448bb0328Richard Smith    ((StructData*)(char*)Data)->~StructData();
206180f47959a066795cc0f409433023af448bb0328Richard Smith  else if (Kind == Union)
207180f47959a066795cc0f409433023af448bb0328Richard Smith    ((UnionData*)(char*)Data)->~UnionData();
208e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  else if (Kind == MemberPointer)
209e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    ((MemberPointerData*)(char*)Data)->~MemberPointerData();
21065639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman  else if (Kind == AddrLabelDiff)
21165639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman    ((AddrLabelDiffData*)(char*)Data)->~AddrLabelDiffData();
2123d309f9d62a6f9f634b869937139d533ccd7265bNate Begeman  Kind = Uninitialized;
21364c34f1c6f613eef02a7b488f8edadbe7a8650a8Chris Lattner}
21464c34f1c6f613eef02a7b488f8edadbe7a8650a8Chris Lattner
2150069b84c2aa7cc39263e85997b7cb1ed0b132ccdRichard Smithvoid APValue::swap(APValue &RHS) {
2160069b84c2aa7cc39263e85997b7cb1ed0b132ccdRichard Smith  std::swap(Kind, RHS.Kind);
2170069b84c2aa7cc39263e85997b7cb1ed0b132ccdRichard Smith  char TmpData[MaxSize];
2180069b84c2aa7cc39263e85997b7cb1ed0b132ccdRichard Smith  memcpy(TmpData, Data, MaxSize);
2190069b84c2aa7cc39263e85997b7cb1ed0b132ccdRichard Smith  memcpy(Data, RHS.Data, MaxSize);
2200069b84c2aa7cc39263e85997b7cb1ed0b132ccdRichard Smith  memcpy(RHS.Data, TmpData, MaxSize);
2210069b84c2aa7cc39263e85997b7cb1ed0b132ccdRichard Smith}
2220069b84c2aa7cc39263e85997b7cb1ed0b132ccdRichard Smith
22364c34f1c6f613eef02a7b488f8edadbe7a8650a8Chris Lattnervoid APValue::dump() const {
22408d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith  dump(llvm::errs());
22564c34f1c6f613eef02a7b488f8edadbe7a8650a8Chris Lattner  llvm::errs() << '\n';
22664c34f1c6f613eef02a7b488f8edadbe7a8650a8Chris Lattner}
22764c34f1c6f613eef02a7b488f8edadbe7a8650a8Chris Lattner
22864c34f1c6f613eef02a7b488f8edadbe7a8650a8Chris Lattnerstatic double GetApproxValue(const llvm::APFloat &F) {
22964c34f1c6f613eef02a7b488f8edadbe7a8650a8Chris Lattner  llvm::APFloat V = F;
23064c34f1c6f613eef02a7b488f8edadbe7a8650a8Chris Lattner  bool ignored;
23164c34f1c6f613eef02a7b488f8edadbe7a8650a8Chris Lattner  V.convert(llvm::APFloat::IEEEdouble, llvm::APFloat::rmNearestTiesToEven,
23264c34f1c6f613eef02a7b488f8edadbe7a8650a8Chris Lattner            &ignored);
23364c34f1c6f613eef02a7b488f8edadbe7a8650a8Chris Lattner  return V.convertToDouble();
23464c34f1c6f613eef02a7b488f8edadbe7a8650a8Chris Lattner}
23564c34f1c6f613eef02a7b488f8edadbe7a8650a8Chris Lattner
23608d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smithvoid APValue::dump(raw_ostream &OS) const {
23764c34f1c6f613eef02a7b488f8edadbe7a8650a8Chris Lattner  switch (getKind()) {
23864c34f1c6f613eef02a7b488f8edadbe7a8650a8Chris Lattner  case Uninitialized:
23964c34f1c6f613eef02a7b488f8edadbe7a8650a8Chris Lattner    OS << "Uninitialized";
24064c34f1c6f613eef02a7b488f8edadbe7a8650a8Chris Lattner    return;
24164c34f1c6f613eef02a7b488f8edadbe7a8650a8Chris Lattner  case Int:
24264c34f1c6f613eef02a7b488f8edadbe7a8650a8Chris Lattner    OS << "Int: " << getInt();
24364c34f1c6f613eef02a7b488f8edadbe7a8650a8Chris Lattner    return;
24464c34f1c6f613eef02a7b488f8edadbe7a8650a8Chris Lattner  case Float:
24564c34f1c6f613eef02a7b488f8edadbe7a8650a8Chris Lattner    OS << "Float: " << GetApproxValue(getFloat());
24664c34f1c6f613eef02a7b488f8edadbe7a8650a8Chris Lattner    return;
2473d309f9d62a6f9f634b869937139d533ccd7265bNate Begeman  case Vector:
24808d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith    OS << "Vector: ";
24908d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith    getVectorElt(0).dump(OS);
25008d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith    for (unsigned i = 1; i != getVectorLength(); ++i) {
25108d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith      OS << ", ";
25208d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith      getVectorElt(i).dump(OS);
25308d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith    }
2543d309f9d62a6f9f634b869937139d533ccd7265bNate Begeman    return;
25564c34f1c6f613eef02a7b488f8edadbe7a8650a8Chris Lattner  case ComplexInt:
25664c34f1c6f613eef02a7b488f8edadbe7a8650a8Chris Lattner    OS << "ComplexInt: " << getComplexIntReal() << ", " << getComplexIntImag();
25764c34f1c6f613eef02a7b488f8edadbe7a8650a8Chris Lattner    return;
25864c34f1c6f613eef02a7b488f8edadbe7a8650a8Chris Lattner  case ComplexFloat:
25964c34f1c6f613eef02a7b488f8edadbe7a8650a8Chris Lattner    OS << "ComplexFloat: " << GetApproxValue(getComplexFloatReal())
26064c34f1c6f613eef02a7b488f8edadbe7a8650a8Chris Lattner       << ", " << GetApproxValue(getComplexFloatImag());
261cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith    return;
26264c34f1c6f613eef02a7b488f8edadbe7a8650a8Chris Lattner  case LValue:
26364c34f1c6f613eef02a7b488f8edadbe7a8650a8Chris Lattner    OS << "LValue: <todo>";
26464c34f1c6f613eef02a7b488f8edadbe7a8650a8Chris Lattner    return;
265cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith  case Array:
266cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith    OS << "Array: ";
267cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith    for (unsigned I = 0, N = getArrayInitializedElts(); I != N; ++I) {
26808d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith      getArrayInitializedElt(I).dump(OS);
269cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith      if (I != getArraySize() - 1) OS << ", ";
270cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith    }
27108d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith    if (hasArrayFiller()) {
27208d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith      OS << getArraySize() - getArrayInitializedElts() << " x ";
27308d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith      getArrayFiller().dump(OS);
27408d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith    }
275cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith    return;
276180f47959a066795cc0f409433023af448bb0328Richard Smith  case Struct:
277180f47959a066795cc0f409433023af448bb0328Richard Smith    OS << "Struct ";
278180f47959a066795cc0f409433023af448bb0328Richard Smith    if (unsigned N = getStructNumBases()) {
27908d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith      OS << " bases: ";
28008d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith      getStructBase(0).dump(OS);
28108d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith      for (unsigned I = 1; I != N; ++I) {
28208d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith        OS << ", ";
28308d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith        getStructBase(I).dump(OS);
28408d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith      }
285180f47959a066795cc0f409433023af448bb0328Richard Smith    }
286180f47959a066795cc0f409433023af448bb0328Richard Smith    if (unsigned N = getStructNumFields()) {
28708d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith      OS << " fields: ";
28808d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith      getStructField(0).dump(OS);
28908d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith      for (unsigned I = 1; I != N; ++I) {
29008d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith        OS << ", ";
29108d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith        getStructField(I).dump(OS);
29208d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith      }
293180f47959a066795cc0f409433023af448bb0328Richard Smith    }
294180f47959a066795cc0f409433023af448bb0328Richard Smith    return;
295180f47959a066795cc0f409433023af448bb0328Richard Smith  case Union:
29608d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith    OS << "Union: ";
29708d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith    getUnionValue().dump(OS);
298180f47959a066795cc0f409433023af448bb0328Richard Smith    return;
299e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  case MemberPointer:
300e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    OS << "MemberPointer: <todo>";
301e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    return;
30265639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman  case AddrLabelDiff:
30365639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman    OS << "AddrLabelDiff: <todo>";
30465639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman    return;
30564c34f1c6f613eef02a7b488f8edadbe7a8650a8Chris Lattner  }
306180f47959a066795cc0f409433023af448bb0328Richard Smith  llvm_unreachable("Unknown APValue kind!");
30764c34f1c6f613eef02a7b488f8edadbe7a8650a8Chris Lattner}
30864c34f1c6f613eef02a7b488f8edadbe7a8650a8Chris Lattner
30908d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smithvoid APValue::printPretty(raw_ostream &Out, ASTContext &Ctx, QualType Ty) const{
31008d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith  switch (getKind()) {
3115b106a872d66f57522b1cc6d1b67f93704409114Jeffrey Yasskin  case APValue::Uninitialized:
31208d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith    Out << "<uninitialized>";
313180f47959a066795cc0f409433023af448bb0328Richard Smith    return;
3145b106a872d66f57522b1cc6d1b67f93704409114Jeffrey Yasskin  case APValue::Int:
315f602806965531ee06fd8664b9d7a8912c4af2870Richard Smith    if (Ty->isBooleanType())
316f602806965531ee06fd8664b9d7a8912c4af2870Richard Smith      Out << (getInt().getBoolValue() ? "true" : "false");
317f602806965531ee06fd8664b9d7a8912c4af2870Richard Smith    else
318f602806965531ee06fd8664b9d7a8912c4af2870Richard Smith      Out << getInt();
319180f47959a066795cc0f409433023af448bb0328Richard Smith    return;
3205b106a872d66f57522b1cc6d1b67f93704409114Jeffrey Yasskin  case APValue::Float:
32108d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith    Out << GetApproxValue(getFloat());
322180f47959a066795cc0f409433023af448bb0328Richard Smith    return;
32308d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith  case APValue::Vector: {
32408d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith    Out << '{';
32508d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith    QualType ElemTy = Ty->getAs<VectorType>()->getElementType();
32608d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith    getVectorElt(0).printPretty(Out, Ctx, ElemTy);
32708d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith    for (unsigned i = 1; i != getVectorLength(); ++i) {
3285b106a872d66f57522b1cc6d1b67f93704409114Jeffrey Yasskin      Out << ", ";
32908d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith      getVectorElt(i).printPretty(Out, Ctx, ElemTy);
3305b106a872d66f57522b1cc6d1b67f93704409114Jeffrey Yasskin    }
33108d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith    Out << '}';
332180f47959a066795cc0f409433023af448bb0328Richard Smith    return;
33308d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith  }
3345b106a872d66f57522b1cc6d1b67f93704409114Jeffrey Yasskin  case APValue::ComplexInt:
33508d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith    Out << getComplexIntReal() << "+" << getComplexIntImag() << "i";
336180f47959a066795cc0f409433023af448bb0328Richard Smith    return;
3375b106a872d66f57522b1cc6d1b67f93704409114Jeffrey Yasskin  case APValue::ComplexFloat:
33808d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith    Out << GetApproxValue(getComplexFloatReal()) << "+"
33908d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith        << GetApproxValue(getComplexFloatImag()) << "i";
340180f47959a066795cc0f409433023af448bb0328Richard Smith    return;
34108d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith  case APValue::LValue: {
34208d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith    LValueBase Base = getLValueBase();
34308d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith    if (!Base) {
34408d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith      Out << "0";
34508d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith      return;
34608d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith    }
34708d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith
34808d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith    bool IsReference = Ty->isReferenceType();
34908d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith    QualType InnerTy
35008d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith      = IsReference ? Ty.getNonReferenceType() : Ty->getPointeeType();
351f727e1c6cc382c1b5fe23b38ba04df2d4a2f358aDouglas Gregor    if (InnerTy.isNull())
352f727e1c6cc382c1b5fe23b38ba04df2d4a2f358aDouglas Gregor      InnerTy = Ty;
35308d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith
35408d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith    if (!hasLValuePath()) {
35508d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith      // No lvalue path: just print the offset.
35608d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith      CharUnits O = getLValueOffset();
35708d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith      CharUnits S = Ctx.getTypeSizeInChars(InnerTy);
35808d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith      if (!O.isZero()) {
35908d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith        if (IsReference)
36008d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith          Out << "*(";
36108d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith        if (O % S) {
36208d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith          Out << "(char*)";
36308d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith          S = CharUnits::One();
36408d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith        }
36508d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith        Out << '&';
36608d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith      } else if (!IsReference)
36708d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith        Out << '&';
36808d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith
36908d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith      if (const ValueDecl *VD = Base.dyn_cast<const ValueDecl*>())
37008d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith        Out << *VD;
37108d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith      else
372d1420c6fa788669e49f21e184927c7833881e399Richard Smith        Base.get<const Expr*>()->printPretty(Out, 0, Ctx.getPrintingPolicy());
37308d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith      if (!O.isZero()) {
37408d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith        Out << " + " << (O / S);
37508d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith        if (IsReference)
37608d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith          Out << ')';
37708d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith      }
37808d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith      return;
37908d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith    }
38008d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith
38108d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith    // We have an lvalue path. Print it out nicely.
38208d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith    if (!IsReference)
38308d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith      Out << '&';
38408d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith    else if (isLValueOnePastTheEnd())
38508d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith      Out << "*(&";
38608d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith
38708d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith    QualType ElemTy;
38808d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith    if (const ValueDecl *VD = Base.dyn_cast<const ValueDecl*>()) {
38908d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith      Out << *VD;
39008d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith      ElemTy = VD->getType();
39108d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith    } else {
39208d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith      const Expr *E = Base.get<const Expr*>();
393d1420c6fa788669e49f21e184927c7833881e399Richard Smith      E->printPretty(Out, 0, Ctx.getPrintingPolicy());
39408d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith      ElemTy = E->getType();
39508d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith    }
39608d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith
39708d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith    ArrayRef<LValuePathEntry> Path = getLValuePath();
39808d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith    const CXXRecordDecl *CastToBase = 0;
39908d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith    for (unsigned I = 0, N = Path.size(); I != N; ++I) {
40008d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith      if (ElemTy->getAs<RecordType>()) {
40108d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith        // The lvalue refers to a class type, so the next path entry is a base
40208d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith        // or member.
40308d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith        const Decl *BaseOrMember =
40408d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith        BaseOrMemberType::getFromOpaqueValue(Path[I].BaseOrMember).getPointer();
40508d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith        if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(BaseOrMember)) {
40608d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith          CastToBase = RD;
40708d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith          ElemTy = Ctx.getRecordType(RD);
40808d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith        } else {
40908d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith          const ValueDecl *VD = cast<ValueDecl>(BaseOrMember);
41008d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith          Out << ".";
41108d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith          if (CastToBase)
41208d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith            Out << *CastToBase << "::";
41308d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith          Out << *VD;
41408d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith          ElemTy = VD->getType();
41508d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith        }
41608d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith      } else {
41708d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith        // The lvalue must refer to an array.
41808d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith        Out << '[' << Path[I].ArrayIndex << ']';
41908d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith        ElemTy = Ctx.getAsArrayType(ElemTy)->getElementType();
42008d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith      }
42108d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith    }
42208d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith
42308d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith    // Handle formatting of one-past-the-end lvalues.
42408d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith    if (isLValueOnePastTheEnd()) {
42508d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith      // FIXME: If CastToBase is non-0, we should prefix the output with
42608d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith      // "(CastToBase*)".
42708d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith      Out << " + 1";
42808d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith      if (IsReference)
42908d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith        Out << ')';
43008d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith    }
431180f47959a066795cc0f409433023af448bb0328Richard Smith    return;
43208d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith  }
43308d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith  case APValue::Array: {
43408d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith    const ArrayType *AT = Ctx.getAsArrayType(Ty);
43508d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith    QualType ElemTy = AT->getElementType();
436cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith    Out << '{';
43708d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith    if (unsigned N = getArrayInitializedElts()) {
43808d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith      getArrayInitializedElt(0).printPretty(Out, Ctx, ElemTy);
43908d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith      for (unsigned I = 1; I != N; ++I) {
44008d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith        Out << ", ";
44108d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith        if (I == 10) {
44208d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith          // Avoid printing out the entire contents of large arrays.
44308d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith          Out << "...";
44408d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith          break;
44508d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith        }
44608d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith        getArrayInitializedElt(I).printPretty(Out, Ctx, ElemTy);
44708d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith      }
448cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith    }
449cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith    Out << '}';
450180f47959a066795cc0f409433023af448bb0328Richard Smith    return;
45108d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith  }
45208d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith  case APValue::Struct: {
453180f47959a066795cc0f409433023af448bb0328Richard Smith    Out << '{';
45408d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith    const RecordDecl *RD = Ty->getAs<RecordType>()->getDecl();
45508d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith    bool First = true;
45608d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith    if (unsigned N = getStructNumBases()) {
45708d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith      const CXXRecordDecl *CD = cast<CXXRecordDecl>(RD);
45808d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith      CXXRecordDecl::base_class_const_iterator BI = CD->bases_begin();
45908d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith      for (unsigned I = 0; I != N; ++I, ++BI) {
46008d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith        assert(BI != CD->bases_end());
46108d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith        if (!First)
46208d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith          Out << ", ";
46308d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith        getStructBase(I).printPretty(Out, Ctx, BI->getType());
46408d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith        First = false;
46508d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith      }
466180f47959a066795cc0f409433023af448bb0328Richard Smith    }
46708d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith    for (RecordDecl::field_iterator FI = RD->field_begin();
46808d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith         FI != RD->field_end(); ++FI) {
46908d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith      if (!First)
47008d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith        Out << ", ";
471262bc18e32500558af7cb0afa205b34bd37bafedDavid Blaikie      if (FI->isUnnamedBitfield()) continue;
472262bc18e32500558af7cb0afa205b34bd37bafedDavid Blaikie      getStructField(FI->getFieldIndex()).
473262bc18e32500558af7cb0afa205b34bd37bafedDavid Blaikie        printPretty(Out, Ctx, FI->getType());
47408d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith      First = false;
475180f47959a066795cc0f409433023af448bb0328Richard Smith    }
476180f47959a066795cc0f409433023af448bb0328Richard Smith    Out << '}';
477180f47959a066795cc0f409433023af448bb0328Richard Smith    return;
47808d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith  }
479180f47959a066795cc0f409433023af448bb0328Richard Smith  case APValue::Union:
48008d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith    Out << '{';
48108d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith    if (const FieldDecl *FD = getUnionField()) {
48208d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith      Out << "." << *FD << " = ";
48308d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith      getUnionValue().printPretty(Out, Ctx, FD->getType());
48408d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith    }
48508d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith    Out << '}';
486180f47959a066795cc0f409433023af448bb0328Richard Smith    return;
487e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  case APValue::MemberPointer:
48808d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith    // FIXME: This is not enough to unambiguously identify the member in a
48908d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith    // multiple-inheritance scenario.
49008d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith    if (const ValueDecl *VD = getMemberPointerDecl()) {
49108d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith      Out << '&' << *cast<CXXRecordDecl>(VD->getDeclContext()) << "::" << *VD;
49208d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith      return;
49308d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith    }
49408d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith    Out << "0";
495e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    return;
49665639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman  case APValue::AddrLabelDiff:
49765639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman    Out << "&&" << getAddrLabelDiffLHS()->getLabel()->getName();
49865639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman    Out << " - ";
49965639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman    Out << "&&" << getAddrLabelDiffRHS()->getLabel()->getName();
50065639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman    return;
5015b106a872d66f57522b1cc6d1b67f93704409114Jeffrey Yasskin  }
502180f47959a066795cc0f409433023af448bb0328Richard Smith  llvm_unreachable("Unknown APValue kind!");
5035b106a872d66f57522b1cc6d1b67f93704409114Jeffrey Yasskin}
5045b106a872d66f57522b1cc6d1b67f93704409114Jeffrey Yasskin
50508d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smithstd::string APValue::getAsString(ASTContext &Ctx, QualType Ty) const {
50608d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith  std::string Result;
50708d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith  llvm::raw_string_ostream Out(Result);
50808d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith  printPretty(Out, Ctx, Ty);
509d9ce41e19dfd848a8192c2a7f6f9c5b0f3448076Eli Friedman  Out.flush();
51008d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith  return Result;
5115b106a872d66f57522b1cc6d1b67f93704409114Jeffrey Yasskin}
5125b106a872d66f57522b1cc6d1b67f93704409114Jeffrey Yasskin
5131bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smithconst APValue::LValueBase APValue::getLValueBase() const {
514a73058324197b7bdfd19307965954f626e26199dKen Dyck  assert(isLValue() && "Invalid accessor");
515e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  return ((const LV*)(const void*)Data)->BaseAndIsOnePastTheEnd.getPointer();
516e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith}
517e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
518e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smithbool APValue::isLValueOnePastTheEnd() const {
519e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  assert(isLValue() && "Invalid accessor");
520e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  return ((const LV*)(const void*)Data)->BaseAndIsOnePastTheEnd.getInt();
521a73058324197b7bdfd19307965954f626e26199dKen Dyck}
522a73058324197b7bdfd19307965954f626e26199dKen Dyck
52347a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard SmithCharUnits &APValue::getLValueOffset() {
52447a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith  assert(isLValue() && "Invalid accessor");
52547a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith  return ((LV*)(void*)Data)->Offset;
526a73058324197b7bdfd19307965954f626e26199dKen Dyck}
527a73058324197b7bdfd19307965954f626e26199dKen Dyck
5289a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smithbool APValue::hasLValuePath() const {
5299a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smith  assert(isLValue() && "Invalid accessor");
53038dce9b19c3cc85698fb44ea182f9e7fa0fa4a69Richard Smith  return ((const LV*)(const char*)Data)->hasPath();
5319a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smith}
5329a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smith
5339a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard SmithArrayRef<APValue::LValuePathEntry> APValue::getLValuePath() const {
5349a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smith  assert(isLValue() && hasLValuePath() && "Invalid accessor");
53538dce9b19c3cc85698fb44ea182f9e7fa0fa4a69Richard Smith  const LV &LVal = *((const LV*)(const char*)Data);
5369a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smith  return ArrayRef<LValuePathEntry>(LVal.getPath(), LVal.PathLength);
5379a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smith}
5389a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smith
53983587db1bda97f45d2b5a4189e584e2a18be511aRichard Smithunsigned APValue::getLValueCallIndex() const {
54083587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith  assert(isLValue() && "Invalid accessor");
54183587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith  return ((const LV*)(const char*)Data)->CallIndex;
54283587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith}
54383587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith
54483587db1bda97f45d2b5a4189e584e2a18be511aRichard Smithvoid APValue::setLValue(LValueBase B, const CharUnits &O, NoLValuePath,
54583587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith                        unsigned CallIndex) {
5469a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smith  assert(isLValue() && "Invalid accessor");
5479a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smith  LV &LVal = *((LV*)(char*)Data);
548e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  LVal.BaseAndIsOnePastTheEnd.setPointer(B);
549e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  LVal.BaseAndIsOnePastTheEnd.setInt(false);
5509a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smith  LVal.Offset = O;
55183587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith  LVal.CallIndex = CallIndex;
552e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  LVal.resizePath((unsigned)-1);
5539a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smith}
5549a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smith
5551bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smithvoid APValue::setLValue(LValueBase B, const CharUnits &O,
55683587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith                        ArrayRef<LValuePathEntry> Path, bool IsOnePastTheEnd,
55783587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith                        unsigned CallIndex) {
558a73058324197b7bdfd19307965954f626e26199dKen Dyck  assert(isLValue() && "Invalid accessor");
5599a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smith  LV &LVal = *((LV*)(char*)Data);
560e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  LVal.BaseAndIsOnePastTheEnd.setPointer(B);
561e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  LVal.BaseAndIsOnePastTheEnd.setInt(IsOnePastTheEnd);
5629a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smith  LVal.Offset = O;
56383587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith  LVal.CallIndex = CallIndex;
564e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  LVal.resizePath(Path.size());
5659a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smith  memcpy(LVal.getPath(), Path.data(), Path.size() * sizeof(LValuePathEntry));
566a73058324197b7bdfd19307965954f626e26199dKen Dyck}
567a73058324197b7bdfd19307965954f626e26199dKen Dyck
568e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smithconst ValueDecl *APValue::getMemberPointerDecl() const {
569e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  assert(isMemberPointer() && "Invalid accessor");
570e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  const MemberPointerData &MPD = *((const MemberPointerData*)(const char*)Data);
571e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  return MPD.MemberAndIsDerivedMember.getPointer();
572e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith}
573e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
574e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smithbool APValue::isMemberPointerToDerivedMember() const {
575e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  assert(isMemberPointer() && "Invalid accessor");
576e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  const MemberPointerData &MPD = *((const MemberPointerData*)(const char*)Data);
577e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  return MPD.MemberAndIsDerivedMember.getInt();
578e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith}
579e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
580e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard SmithArrayRef<const CXXRecordDecl*> APValue::getMemberPointerPath() const {
581e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  assert(isMemberPointer() && "Invalid accessor");
582e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  const MemberPointerData &MPD = *((const MemberPointerData*)(const char*)Data);
583e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  return ArrayRef<const CXXRecordDecl*>(MPD.getPath(), MPD.PathLength);
584e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith}
585e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
586a73058324197b7bdfd19307965954f626e26199dKen Dyckvoid APValue::MakeLValue() {
587a73058324197b7bdfd19307965954f626e26199dKen Dyck  assert(isUninit() && "Bad state change");
5889a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smith  assert(sizeof(LV) <= MaxSize && "LV too big");
589a73058324197b7bdfd19307965954f626e26199dKen Dyck  new ((void*)(char*)Data) LV();
590a73058324197b7bdfd19307965954f626e26199dKen Dyck  Kind = LValue;
591a73058324197b7bdfd19307965954f626e26199dKen Dyck}
592cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith
593cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smithvoid APValue::MakeArray(unsigned InitElts, unsigned Size) {
594cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith  assert(isUninit() && "Bad state change");
595cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith  new ((void*)(char*)Data) Arr(InitElts, Size);
596cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith  Kind = Array;
597cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith}
598e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
599e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smithvoid APValue::MakeMemberPointer(const ValueDecl *Member, bool IsDerivedMember,
600e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith                                ArrayRef<const CXXRecordDecl*> Path) {
601e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  assert(isUninit() && "Bad state change");
602e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  MemberPointerData *MPD = new ((void*)(char*)Data) MemberPointerData;
603e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  Kind = MemberPointer;
604e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  MPD->MemberAndIsDerivedMember.setPointer(Member);
605e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  MPD->MemberAndIsDerivedMember.setInt(IsDerivedMember);
606e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  MPD->resizePath(Path.size());
607e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  memcpy(MPD->getPath(), Path.data(), Path.size()*sizeof(const CXXRecordDecl*));
608e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith}
609