Chris Lattner | 64c34f1 | 2008-11-16 07:46:48 +0000 | [diff] [blame] | 1 | //===--- 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 Smith | 08d6e03 | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 15 | #include "clang/AST/ASTContext.h" |
Ken Dyck | a730583 | 2010-01-15 12:37:54 +0000 | [diff] [blame] | 16 | #include "clang/AST/CharUnits.h" |
Richard Smith | 08d6e03 | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 17 | #include "clang/AST/DeclCXX.h" |
| 18 | #include "clang/AST/Expr.h" |
| 19 | #include "clang/AST/Type.h" |
Jeffrey Yasskin | 5b106a8 | 2011-07-18 16:43:53 +0000 | [diff] [blame] | 20 | #include "clang/Basic/Diagnostic.h" |
| 21 | #include "llvm/ADT/SmallString.h" |
Chris Lattner | 64c34f1 | 2008-11-16 07:46:48 +0000 | [diff] [blame] | 22 | #include "llvm/Support/raw_ostream.h" |
David Blaikie | 9fe8c74 | 2011-09-23 05:35:21 +0000 | [diff] [blame] | 23 | #include "llvm/Support/ErrorHandling.h" |
Chris Lattner | 64c34f1 | 2008-11-16 07:46:48 +0000 | [diff] [blame] | 24 | using namespace clang; |
| 25 | |
Ken Dyck | a730583 | 2010-01-15 12:37:54 +0000 | [diff] [blame] | 26 | namespace { |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 27 | struct LVBase { |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 28 | llvm::PointerIntPair<APValue::LValueBase, 1, bool> BaseAndIsOnePastTheEnd; |
Ken Dyck | a730583 | 2010-01-15 12:37:54 +0000 | [diff] [blame] | 29 | CharUnits Offset; |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 30 | unsigned PathLength; |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame^] | 31 | unsigned CallIndex; |
Ken Dyck | a730583 | 2010-01-15 12:37:54 +0000 | [diff] [blame] | 32 | }; |
| 33 | } |
| 34 | |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 35 | struct 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 Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 48 | ~LV() { resizePath(0); } |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 49 | |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 50 | 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 Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 58 | } |
| 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 Smith | 38dce9b | 2011-11-07 07:31:09 +0000 | [diff] [blame] | 64 | const LValuePathEntry *getPath() const { |
| 65 | return hasPathPtr() ? PathPtr : Path; |
| 66 | } |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 67 | }; |
| 68 | |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 69 | namespace { |
| 70 | struct MemberPointerBase { |
| 71 | llvm::PointerIntPair<const ValueDecl*, 1, bool> MemberAndIsDerivedMember; |
| 72 | unsigned PathLength; |
| 73 | }; |
| 74 | } |
| 75 | |
| 76 | struct 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 Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 106 | // FIXME: Reduce the malloc traffic here. |
| 107 | |
| 108 | APValue::Arr::Arr(unsigned NumElts, unsigned Size) : |
| 109 | Elts(new APValue[NumElts + (NumElts != Size ? 1 : 0)]), |
| 110 | NumElts(NumElts), ArrSize(Size) {} |
| 111 | APValue::Arr::~Arr() { delete [] Elts; } |
| 112 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 113 | APValue::StructData::StructData(unsigned NumBases, unsigned NumFields) : |
| 114 | Elts(new APValue[NumBases+NumFields]), |
| 115 | NumBases(NumBases), NumFields(NumFields) {} |
| 116 | APValue::StructData::~StructData() { |
| 117 | delete [] Elts; |
| 118 | } |
| 119 | |
| 120 | APValue::UnionData::UnionData() : Field(0), Value(new APValue) {} |
| 121 | APValue::UnionData::~UnionData () { |
| 122 | delete Value; |
| 123 | } |
| 124 | |
Chris Lattner | 64c34f1 | 2008-11-16 07:46:48 +0000 | [diff] [blame] | 125 | const APValue &APValue::operator=(const APValue &RHS) { |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 126 | if (this == &RHS) |
| 127 | return *this; |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 128 | if (Kind != RHS.Kind || Kind == Array || Kind == Struct || |
| 129 | Kind == MemberPointer) { |
Chris Lattner | 64c34f1 | 2008-11-16 07:46:48 +0000 | [diff] [blame] | 130 | MakeUninit(); |
| 131 | if (RHS.isInt()) |
| 132 | MakeInt(); |
| 133 | else if (RHS.isFloat()) |
| 134 | MakeFloat(); |
Nate Begeman | 3d309f9 | 2009-01-18 01:01:34 +0000 | [diff] [blame] | 135 | else if (RHS.isVector()) |
| 136 | MakeVector(); |
Chris Lattner | 64c34f1 | 2008-11-16 07:46:48 +0000 | [diff] [blame] | 137 | else if (RHS.isComplexInt()) |
| 138 | MakeComplexInt(); |
| 139 | else if (RHS.isComplexFloat()) |
| 140 | MakeComplexFloat(); |
| 141 | else if (RHS.isLValue()) |
| 142 | MakeLValue(); |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 143 | else if (RHS.isArray()) |
| 144 | MakeArray(RHS.getArrayInitializedElts(), RHS.getArraySize()); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 145 | else if (RHS.isStruct()) |
| 146 | MakeStruct(RHS.getStructNumBases(), RHS.getStructNumFields()); |
| 147 | else if (RHS.isUnion()) |
| 148 | MakeUnion(); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 149 | else if (RHS.isMemberPointer()) |
| 150 | MakeMemberPointer(RHS.getMemberPointerDecl(), |
| 151 | RHS.isMemberPointerToDerivedMember(), |
| 152 | RHS.getMemberPointerPath()); |
Eli Friedman | 6563928 | 2012-01-04 23:13:47 +0000 | [diff] [blame] | 153 | else if (RHS.isAddrLabelDiff()) |
| 154 | MakeAddrLabelDiff(); |
Chris Lattner | 64c34f1 | 2008-11-16 07:46:48 +0000 | [diff] [blame] | 155 | } |
| 156 | if (isInt()) |
| 157 | setInt(RHS.getInt()); |
| 158 | else if (isFloat()) |
| 159 | setFloat(RHS.getFloat()); |
Nate Begeman | 3d309f9 | 2009-01-18 01:01:34 +0000 | [diff] [blame] | 160 | else if (isVector()) |
Dan Gohman | cb421fa | 2010-04-19 16:39:44 +0000 | [diff] [blame] | 161 | setVector(((const Vec *)(const char *)RHS.Data)->Elts, |
| 162 | RHS.getVectorLength()); |
Chris Lattner | 64c34f1 | 2008-11-16 07:46:48 +0000 | [diff] [blame] | 163 | else if (isComplexInt()) |
| 164 | setComplexInt(RHS.getComplexIntReal(), RHS.getComplexIntImag()); |
| 165 | else if (isComplexFloat()) |
| 166 | setComplexFloat(RHS.getComplexFloatReal(), RHS.getComplexFloatImag()); |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 167 | else if (isLValue()) { |
| 168 | if (RHS.hasLValuePath()) |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 169 | setLValue(RHS.getLValueBase(), RHS.getLValueOffset(), RHS.getLValuePath(), |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame^] | 170 | RHS.isLValueOnePastTheEnd(), RHS.getLValueCallIndex()); |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 171 | else |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame^] | 172 | setLValue(RHS.getLValueBase(), RHS.getLValueOffset(), NoLValuePath(), |
| 173 | RHS.getLValueCallIndex()); |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 174 | } else if (isArray()) { |
| 175 | for (unsigned I = 0, N = RHS.getArrayInitializedElts(); I != N; ++I) |
| 176 | getArrayInitializedElt(I) = RHS.getArrayInitializedElt(I); |
| 177 | if (RHS.hasArrayFiller()) |
| 178 | getArrayFiller() = RHS.getArrayFiller(); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 179 | } else if (isStruct()) { |
| 180 | for (unsigned I = 0, N = RHS.getStructNumBases(); I != N; ++I) |
| 181 | getStructBase(I) = RHS.getStructBase(I); |
| 182 | for (unsigned I = 0, N = RHS.getStructNumFields(); I != N; ++I) |
| 183 | getStructField(I) = RHS.getStructField(I); |
Eli Friedman | 6563928 | 2012-01-04 23:13:47 +0000 | [diff] [blame] | 184 | } else if (isUnion()) { |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 185 | setUnion(RHS.getUnionField(), RHS.getUnionValue()); |
Eli Friedman | 6563928 | 2012-01-04 23:13:47 +0000 | [diff] [blame] | 186 | } else if (isAddrLabelDiff()) { |
| 187 | setAddrLabelDiff(RHS.getAddrLabelDiffLHS(), RHS.getAddrLabelDiffRHS()); |
| 188 | } |
Chris Lattner | 64c34f1 | 2008-11-16 07:46:48 +0000 | [diff] [blame] | 189 | return *this; |
| 190 | } |
| 191 | |
| 192 | void APValue::MakeUninit() { |
| 193 | if (Kind == Int) |
Douglas Gregor | 9830046 | 2009-09-08 19:57:33 +0000 | [diff] [blame] | 194 | ((APSInt*)(char*)Data)->~APSInt(); |
Chris Lattner | 64c34f1 | 2008-11-16 07:46:48 +0000 | [diff] [blame] | 195 | else if (Kind == Float) |
Douglas Gregor | 9830046 | 2009-09-08 19:57:33 +0000 | [diff] [blame] | 196 | ((APFloat*)(char*)Data)->~APFloat(); |
Nate Begeman | 3d309f9 | 2009-01-18 01:01:34 +0000 | [diff] [blame] | 197 | else if (Kind == Vector) |
Douglas Gregor | 9830046 | 2009-09-08 19:57:33 +0000 | [diff] [blame] | 198 | ((Vec*)(char*)Data)->~Vec(); |
Chris Lattner | 64c34f1 | 2008-11-16 07:46:48 +0000 | [diff] [blame] | 199 | else if (Kind == ComplexInt) |
Douglas Gregor | 9830046 | 2009-09-08 19:57:33 +0000 | [diff] [blame] | 200 | ((ComplexAPSInt*)(char*)Data)->~ComplexAPSInt(); |
Chris Lattner | 64c34f1 | 2008-11-16 07:46:48 +0000 | [diff] [blame] | 201 | else if (Kind == ComplexFloat) |
Douglas Gregor | 9830046 | 2009-09-08 19:57:33 +0000 | [diff] [blame] | 202 | ((ComplexAPFloat*)(char*)Data)->~ComplexAPFloat(); |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 203 | else if (Kind == LValue) |
Douglas Gregor | 9830046 | 2009-09-08 19:57:33 +0000 | [diff] [blame] | 204 | ((LV*)(char*)Data)->~LV(); |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 205 | else if (Kind == Array) |
| 206 | ((Arr*)(char*)Data)->~Arr(); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 207 | else if (Kind == Struct) |
| 208 | ((StructData*)(char*)Data)->~StructData(); |
| 209 | else if (Kind == Union) |
| 210 | ((UnionData*)(char*)Data)->~UnionData(); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 211 | else if (Kind == MemberPointer) |
| 212 | ((MemberPointerData*)(char*)Data)->~MemberPointerData(); |
Eli Friedman | 6563928 | 2012-01-04 23:13:47 +0000 | [diff] [blame] | 213 | else if (Kind == AddrLabelDiff) |
| 214 | ((AddrLabelDiffData*)(char*)Data)->~AddrLabelDiffData(); |
Nate Begeman | 3d309f9 | 2009-01-18 01:01:34 +0000 | [diff] [blame] | 215 | Kind = Uninitialized; |
Chris Lattner | 64c34f1 | 2008-11-16 07:46:48 +0000 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | void APValue::dump() const { |
Richard Smith | 08d6e03 | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 219 | dump(llvm::errs()); |
Chris Lattner | 64c34f1 | 2008-11-16 07:46:48 +0000 | [diff] [blame] | 220 | llvm::errs() << '\n'; |
Chris Lattner | 64c34f1 | 2008-11-16 07:46:48 +0000 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | static double GetApproxValue(const llvm::APFloat &F) { |
| 224 | llvm::APFloat V = F; |
| 225 | bool ignored; |
| 226 | V.convert(llvm::APFloat::IEEEdouble, llvm::APFloat::rmNearestTiesToEven, |
| 227 | &ignored); |
| 228 | return V.convertToDouble(); |
| 229 | } |
| 230 | |
Richard Smith | 08d6e03 | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 231 | void APValue::dump(raw_ostream &OS) const { |
Chris Lattner | 64c34f1 | 2008-11-16 07:46:48 +0000 | [diff] [blame] | 232 | switch (getKind()) { |
Chris Lattner | 64c34f1 | 2008-11-16 07:46:48 +0000 | [diff] [blame] | 233 | case Uninitialized: |
| 234 | OS << "Uninitialized"; |
| 235 | return; |
| 236 | case Int: |
| 237 | OS << "Int: " << getInt(); |
| 238 | return; |
| 239 | case Float: |
| 240 | OS << "Float: " << GetApproxValue(getFloat()); |
| 241 | return; |
Nate Begeman | 3d309f9 | 2009-01-18 01:01:34 +0000 | [diff] [blame] | 242 | case Vector: |
Richard Smith | 08d6e03 | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 243 | OS << "Vector: "; |
| 244 | getVectorElt(0).dump(OS); |
| 245 | for (unsigned i = 1; i != getVectorLength(); ++i) { |
| 246 | OS << ", "; |
| 247 | getVectorElt(i).dump(OS); |
| 248 | } |
Nate Begeman | 3d309f9 | 2009-01-18 01:01:34 +0000 | [diff] [blame] | 249 | return; |
Chris Lattner | 64c34f1 | 2008-11-16 07:46:48 +0000 | [diff] [blame] | 250 | case ComplexInt: |
| 251 | OS << "ComplexInt: " << getComplexIntReal() << ", " << getComplexIntImag(); |
| 252 | return; |
| 253 | case ComplexFloat: |
| 254 | OS << "ComplexFloat: " << GetApproxValue(getComplexFloatReal()) |
| 255 | << ", " << GetApproxValue(getComplexFloatImag()); |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 256 | return; |
Chris Lattner | 64c34f1 | 2008-11-16 07:46:48 +0000 | [diff] [blame] | 257 | case LValue: |
| 258 | OS << "LValue: <todo>"; |
| 259 | return; |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 260 | case Array: |
| 261 | OS << "Array: "; |
| 262 | for (unsigned I = 0, N = getArrayInitializedElts(); I != N; ++I) { |
Richard Smith | 08d6e03 | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 263 | getArrayInitializedElt(I).dump(OS); |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 264 | if (I != getArraySize() - 1) OS << ", "; |
| 265 | } |
Richard Smith | 08d6e03 | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 266 | if (hasArrayFiller()) { |
| 267 | OS << getArraySize() - getArrayInitializedElts() << " x "; |
| 268 | getArrayFiller().dump(OS); |
| 269 | } |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 270 | return; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 271 | case Struct: |
| 272 | OS << "Struct "; |
| 273 | if (unsigned N = getStructNumBases()) { |
Richard Smith | 08d6e03 | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 274 | OS << " bases: "; |
| 275 | getStructBase(0).dump(OS); |
| 276 | for (unsigned I = 1; I != N; ++I) { |
| 277 | OS << ", "; |
| 278 | getStructBase(I).dump(OS); |
| 279 | } |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 280 | } |
| 281 | if (unsigned N = getStructNumFields()) { |
Richard Smith | 08d6e03 | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 282 | OS << " fields: "; |
| 283 | getStructField(0).dump(OS); |
| 284 | for (unsigned I = 1; I != N; ++I) { |
| 285 | OS << ", "; |
| 286 | getStructField(I).dump(OS); |
| 287 | } |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 288 | } |
| 289 | return; |
| 290 | case Union: |
Richard Smith | 08d6e03 | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 291 | OS << "Union: "; |
| 292 | getUnionValue().dump(OS); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 293 | return; |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 294 | case MemberPointer: |
| 295 | OS << "MemberPointer: <todo>"; |
| 296 | return; |
Eli Friedman | 6563928 | 2012-01-04 23:13:47 +0000 | [diff] [blame] | 297 | case AddrLabelDiff: |
| 298 | OS << "AddrLabelDiff: <todo>"; |
| 299 | return; |
Chris Lattner | 64c34f1 | 2008-11-16 07:46:48 +0000 | [diff] [blame] | 300 | } |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 301 | llvm_unreachable("Unknown APValue kind!"); |
Chris Lattner | 64c34f1 | 2008-11-16 07:46:48 +0000 | [diff] [blame] | 302 | } |
| 303 | |
Richard Smith | 08d6e03 | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 304 | void APValue::printPretty(raw_ostream &Out, ASTContext &Ctx, QualType Ty) const{ |
| 305 | switch (getKind()) { |
Jeffrey Yasskin | 5b106a8 | 2011-07-18 16:43:53 +0000 | [diff] [blame] | 306 | case APValue::Uninitialized: |
Richard Smith | 08d6e03 | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 307 | Out << "<uninitialized>"; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 308 | return; |
Jeffrey Yasskin | 5b106a8 | 2011-07-18 16:43:53 +0000 | [diff] [blame] | 309 | case APValue::Int: |
Richard Smith | 08d6e03 | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 310 | Out << getInt(); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 311 | return; |
Jeffrey Yasskin | 5b106a8 | 2011-07-18 16:43:53 +0000 | [diff] [blame] | 312 | case APValue::Float: |
Richard Smith | 08d6e03 | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 313 | Out << GetApproxValue(getFloat()); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 314 | return; |
Richard Smith | 08d6e03 | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 315 | case APValue::Vector: { |
| 316 | Out << '{'; |
| 317 | QualType ElemTy = Ty->getAs<VectorType>()->getElementType(); |
| 318 | getVectorElt(0).printPretty(Out, Ctx, ElemTy); |
| 319 | for (unsigned i = 1; i != getVectorLength(); ++i) { |
Jeffrey Yasskin | 5b106a8 | 2011-07-18 16:43:53 +0000 | [diff] [blame] | 320 | Out << ", "; |
Richard Smith | 08d6e03 | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 321 | getVectorElt(i).printPretty(Out, Ctx, ElemTy); |
Jeffrey Yasskin | 5b106a8 | 2011-07-18 16:43:53 +0000 | [diff] [blame] | 322 | } |
Richard Smith | 08d6e03 | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 323 | Out << '}'; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 324 | return; |
Richard Smith | 08d6e03 | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 325 | } |
Jeffrey Yasskin | 5b106a8 | 2011-07-18 16:43:53 +0000 | [diff] [blame] | 326 | case APValue::ComplexInt: |
Richard Smith | 08d6e03 | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 327 | Out << getComplexIntReal() << "+" << getComplexIntImag() << "i"; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 328 | return; |
Jeffrey Yasskin | 5b106a8 | 2011-07-18 16:43:53 +0000 | [diff] [blame] | 329 | case APValue::ComplexFloat: |
Richard Smith | 08d6e03 | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 330 | Out << GetApproxValue(getComplexFloatReal()) << "+" |
| 331 | << GetApproxValue(getComplexFloatImag()) << "i"; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 332 | return; |
Richard Smith | 08d6e03 | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 333 | case APValue::LValue: { |
| 334 | LValueBase Base = getLValueBase(); |
| 335 | if (!Base) { |
| 336 | Out << "0"; |
| 337 | return; |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 338 | } |
Richard Smith | 08d6e03 | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 339 | |
| 340 | bool IsReference = Ty->isReferenceType(); |
| 341 | QualType InnerTy |
| 342 | = IsReference ? Ty.getNonReferenceType() : Ty->getPointeeType(); |
| 343 | |
| 344 | if (!hasLValuePath()) { |
| 345 | // No lvalue path: just print the offset. |
| 346 | CharUnits O = getLValueOffset(); |
| 347 | CharUnits S = Ctx.getTypeSizeInChars(InnerTy); |
| 348 | if (!O.isZero()) { |
| 349 | if (IsReference) |
| 350 | Out << "*("; |
| 351 | if (O % S) { |
| 352 | Out << "(char*)"; |
| 353 | S = CharUnits::One(); |
| 354 | } |
| 355 | Out << '&'; |
| 356 | } else if (!IsReference) |
| 357 | Out << '&'; |
| 358 | |
| 359 | if (const ValueDecl *VD = Base.dyn_cast<const ValueDecl*>()) |
| 360 | Out << *VD; |
| 361 | else |
| 362 | Base.get<const Expr*>()->printPretty(Out, Ctx, 0, |
| 363 | Ctx.getPrintingPolicy()); |
| 364 | if (!O.isZero()) { |
| 365 | Out << " + " << (O / S); |
| 366 | if (IsReference) |
| 367 | Out << ')'; |
| 368 | } |
| 369 | return; |
| 370 | } |
| 371 | |
| 372 | // We have an lvalue path. Print it out nicely. |
| 373 | if (!IsReference) |
| 374 | Out << '&'; |
| 375 | else if (isLValueOnePastTheEnd()) |
| 376 | Out << "*(&"; |
| 377 | |
| 378 | QualType ElemTy; |
| 379 | if (const ValueDecl *VD = Base.dyn_cast<const ValueDecl*>()) { |
| 380 | Out << *VD; |
| 381 | ElemTy = VD->getType(); |
| 382 | } else { |
| 383 | const Expr *E = Base.get<const Expr*>(); |
| 384 | E->printPretty(Out, Ctx, 0,Ctx.getPrintingPolicy()); |
| 385 | ElemTy = E->getType(); |
| 386 | } |
| 387 | |
| 388 | ArrayRef<LValuePathEntry> Path = getLValuePath(); |
| 389 | const CXXRecordDecl *CastToBase = 0; |
| 390 | for (unsigned I = 0, N = Path.size(); I != N; ++I) { |
| 391 | if (ElemTy->getAs<RecordType>()) { |
| 392 | // The lvalue refers to a class type, so the next path entry is a base |
| 393 | // or member. |
| 394 | const Decl *BaseOrMember = |
| 395 | BaseOrMemberType::getFromOpaqueValue(Path[I].BaseOrMember).getPointer(); |
| 396 | if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(BaseOrMember)) { |
| 397 | CastToBase = RD; |
| 398 | ElemTy = Ctx.getRecordType(RD); |
| 399 | } else { |
| 400 | const ValueDecl *VD = cast<ValueDecl>(BaseOrMember); |
| 401 | Out << "."; |
| 402 | if (CastToBase) |
| 403 | Out << *CastToBase << "::"; |
| 404 | Out << *VD; |
| 405 | ElemTy = VD->getType(); |
| 406 | } |
| 407 | } else { |
| 408 | // The lvalue must refer to an array. |
| 409 | Out << '[' << Path[I].ArrayIndex << ']'; |
| 410 | ElemTy = Ctx.getAsArrayType(ElemTy)->getElementType(); |
| 411 | } |
| 412 | } |
| 413 | |
| 414 | // Handle formatting of one-past-the-end lvalues. |
| 415 | if (isLValueOnePastTheEnd()) { |
| 416 | // FIXME: If CastToBase is non-0, we should prefix the output with |
| 417 | // "(CastToBase*)". |
| 418 | Out << " + 1"; |
| 419 | if (IsReference) |
| 420 | Out << ')'; |
| 421 | } |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 422 | return; |
Richard Smith | 08d6e03 | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 423 | } |
| 424 | case APValue::Array: { |
| 425 | const ArrayType *AT = Ctx.getAsArrayType(Ty); |
| 426 | QualType ElemTy = AT->getElementType(); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 427 | Out << '{'; |
Richard Smith | 08d6e03 | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 428 | if (unsigned N = getArrayInitializedElts()) { |
| 429 | getArrayInitializedElt(0).printPretty(Out, Ctx, ElemTy); |
| 430 | for (unsigned I = 1; I != N; ++I) { |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 431 | Out << ", "; |
Richard Smith | 08d6e03 | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 432 | if (I == 10) { |
| 433 | // Avoid printing out the entire contents of large arrays. |
| 434 | Out << "..."; |
| 435 | break; |
| 436 | } |
| 437 | getArrayInitializedElt(I).printPretty(Out, Ctx, ElemTy); |
| 438 | } |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 439 | } |
| 440 | Out << '}'; |
| 441 | return; |
Richard Smith | 08d6e03 | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 442 | } |
| 443 | case APValue::Struct: { |
| 444 | Out << '{'; |
| 445 | const RecordDecl *RD = Ty->getAs<RecordType>()->getDecl(); |
| 446 | bool First = true; |
| 447 | if (unsigned N = getStructNumBases()) { |
| 448 | const CXXRecordDecl *CD = cast<CXXRecordDecl>(RD); |
| 449 | CXXRecordDecl::base_class_const_iterator BI = CD->bases_begin(); |
| 450 | for (unsigned I = 0; I != N; ++I, ++BI) { |
| 451 | assert(BI != CD->bases_end()); |
| 452 | if (!First) |
| 453 | Out << ", "; |
| 454 | getStructBase(I).printPretty(Out, Ctx, BI->getType()); |
| 455 | First = false; |
| 456 | } |
| 457 | } |
| 458 | for (RecordDecl::field_iterator FI = RD->field_begin(); |
| 459 | FI != RD->field_end(); ++FI) { |
| 460 | if (!First) |
| 461 | Out << ", "; |
| 462 | if ((*FI)->isUnnamedBitfield()) continue; |
| 463 | getStructField((*FI)->getFieldIndex()). |
| 464 | printPretty(Out, Ctx, (*FI)->getType()); |
| 465 | First = false; |
| 466 | } |
| 467 | Out << '}'; |
| 468 | return; |
| 469 | } |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 470 | case APValue::Union: |
Richard Smith | 08d6e03 | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 471 | Out << '{'; |
| 472 | if (const FieldDecl *FD = getUnionField()) { |
| 473 | Out << "." << *FD << " = "; |
| 474 | getUnionValue().printPretty(Out, Ctx, FD->getType()); |
| 475 | } |
| 476 | Out << '}'; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 477 | return; |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 478 | case APValue::MemberPointer: |
Richard Smith | 08d6e03 | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 479 | // FIXME: This is not enough to unambiguously identify the member in a |
| 480 | // multiple-inheritance scenario. |
| 481 | if (const ValueDecl *VD = getMemberPointerDecl()) { |
| 482 | Out << '&' << *cast<CXXRecordDecl>(VD->getDeclContext()) << "::" << *VD; |
| 483 | return; |
| 484 | } |
| 485 | Out << "0"; |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 486 | return; |
Eli Friedman | 6563928 | 2012-01-04 23:13:47 +0000 | [diff] [blame] | 487 | case APValue::AddrLabelDiff: |
| 488 | Out << "&&" << getAddrLabelDiffLHS()->getLabel()->getName(); |
| 489 | Out << " - "; |
| 490 | Out << "&&" << getAddrLabelDiffRHS()->getLabel()->getName(); |
| 491 | return; |
Jeffrey Yasskin | 5b106a8 | 2011-07-18 16:43:53 +0000 | [diff] [blame] | 492 | } |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 493 | llvm_unreachable("Unknown APValue kind!"); |
Jeffrey Yasskin | 5b106a8 | 2011-07-18 16:43:53 +0000 | [diff] [blame] | 494 | } |
| 495 | |
Richard Smith | 08d6e03 | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 496 | std::string APValue::getAsString(ASTContext &Ctx, QualType Ty) const { |
| 497 | std::string Result; |
| 498 | llvm::raw_string_ostream Out(Result); |
| 499 | printPretty(Out, Ctx, Ty); |
Eli Friedman | d9ce41e | 2011-12-16 22:12:23 +0000 | [diff] [blame] | 500 | Out.flush(); |
Richard Smith | 08d6e03 | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 501 | return Result; |
Jeffrey Yasskin | 5b106a8 | 2011-07-18 16:43:53 +0000 | [diff] [blame] | 502 | } |
| 503 | |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 504 | const APValue::LValueBase APValue::getLValueBase() const { |
Ken Dyck | a730583 | 2010-01-15 12:37:54 +0000 | [diff] [blame] | 505 | assert(isLValue() && "Invalid accessor"); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 506 | return ((const LV*)(const void*)Data)->BaseAndIsOnePastTheEnd.getPointer(); |
| 507 | } |
| 508 | |
| 509 | bool APValue::isLValueOnePastTheEnd() const { |
| 510 | assert(isLValue() && "Invalid accessor"); |
| 511 | return ((const LV*)(const void*)Data)->BaseAndIsOnePastTheEnd.getInt(); |
Ken Dyck | a730583 | 2010-01-15 12:37:54 +0000 | [diff] [blame] | 512 | } |
| 513 | |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 514 | CharUnits &APValue::getLValueOffset() { |
| 515 | assert(isLValue() && "Invalid accessor"); |
| 516 | return ((LV*)(void*)Data)->Offset; |
Ken Dyck | a730583 | 2010-01-15 12:37:54 +0000 | [diff] [blame] | 517 | } |
| 518 | |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 519 | bool APValue::hasLValuePath() const { |
Ken Dyck | a730583 | 2010-01-15 12:37:54 +0000 | [diff] [blame] | 520 | assert(isLValue() && "Invalid accessor"); |
Richard Smith | 38dce9b | 2011-11-07 07:31:09 +0000 | [diff] [blame] | 521 | return ((const LV*)(const char*)Data)->hasPath(); |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 522 | } |
| 523 | |
| 524 | ArrayRef<APValue::LValuePathEntry> APValue::getLValuePath() const { |
| 525 | assert(isLValue() && hasLValuePath() && "Invalid accessor"); |
Richard Smith | 38dce9b | 2011-11-07 07:31:09 +0000 | [diff] [blame] | 526 | const LV &LVal = *((const LV*)(const char*)Data); |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 527 | return ArrayRef<LValuePathEntry>(LVal.getPath(), LVal.PathLength); |
| 528 | } |
| 529 | |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame^] | 530 | unsigned APValue::getLValueCallIndex() const { |
| 531 | assert(isLValue() && "Invalid accessor"); |
| 532 | return ((const LV*)(const char*)Data)->CallIndex; |
| 533 | } |
| 534 | |
| 535 | void APValue::setLValue(LValueBase B, const CharUnits &O, NoLValuePath, |
| 536 | unsigned CallIndex) { |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 537 | assert(isLValue() && "Invalid accessor"); |
| 538 | LV &LVal = *((LV*)(char*)Data); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 539 | LVal.BaseAndIsOnePastTheEnd.setPointer(B); |
| 540 | LVal.BaseAndIsOnePastTheEnd.setInt(false); |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 541 | LVal.Offset = O; |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame^] | 542 | LVal.CallIndex = CallIndex; |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 543 | LVal.resizePath((unsigned)-1); |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 544 | } |
| 545 | |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 546 | void APValue::setLValue(LValueBase B, const CharUnits &O, |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame^] | 547 | ArrayRef<LValuePathEntry> Path, bool IsOnePastTheEnd, |
| 548 | unsigned CallIndex) { |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 549 | assert(isLValue() && "Invalid accessor"); |
| 550 | LV &LVal = *((LV*)(char*)Data); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 551 | LVal.BaseAndIsOnePastTheEnd.setPointer(B); |
| 552 | LVal.BaseAndIsOnePastTheEnd.setInt(IsOnePastTheEnd); |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 553 | LVal.Offset = O; |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame^] | 554 | LVal.CallIndex = CallIndex; |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 555 | LVal.resizePath(Path.size()); |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 556 | memcpy(LVal.getPath(), Path.data(), Path.size() * sizeof(LValuePathEntry)); |
Ken Dyck | a730583 | 2010-01-15 12:37:54 +0000 | [diff] [blame] | 557 | } |
| 558 | |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 559 | const ValueDecl *APValue::getMemberPointerDecl() const { |
| 560 | assert(isMemberPointer() && "Invalid accessor"); |
| 561 | const MemberPointerData &MPD = *((const MemberPointerData*)(const char*)Data); |
| 562 | return MPD.MemberAndIsDerivedMember.getPointer(); |
| 563 | } |
| 564 | |
| 565 | bool APValue::isMemberPointerToDerivedMember() const { |
| 566 | assert(isMemberPointer() && "Invalid accessor"); |
| 567 | const MemberPointerData &MPD = *((const MemberPointerData*)(const char*)Data); |
| 568 | return MPD.MemberAndIsDerivedMember.getInt(); |
| 569 | } |
| 570 | |
| 571 | ArrayRef<const CXXRecordDecl*> APValue::getMemberPointerPath() const { |
| 572 | assert(isMemberPointer() && "Invalid accessor"); |
| 573 | const MemberPointerData &MPD = *((const MemberPointerData*)(const char*)Data); |
| 574 | return ArrayRef<const CXXRecordDecl*>(MPD.getPath(), MPD.PathLength); |
| 575 | } |
| 576 | |
Ken Dyck | a730583 | 2010-01-15 12:37:54 +0000 | [diff] [blame] | 577 | void APValue::MakeLValue() { |
| 578 | assert(isUninit() && "Bad state change"); |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 579 | assert(sizeof(LV) <= MaxSize && "LV too big"); |
Ken Dyck | a730583 | 2010-01-15 12:37:54 +0000 | [diff] [blame] | 580 | new ((void*)(char*)Data) LV(); |
| 581 | Kind = LValue; |
| 582 | } |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 583 | |
| 584 | void APValue::MakeArray(unsigned InitElts, unsigned Size) { |
| 585 | assert(isUninit() && "Bad state change"); |
| 586 | new ((void*)(char*)Data) Arr(InitElts, Size); |
| 587 | Kind = Array; |
| 588 | } |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 589 | |
| 590 | void APValue::MakeMemberPointer(const ValueDecl *Member, bool IsDerivedMember, |
| 591 | ArrayRef<const CXXRecordDecl*> Path) { |
| 592 | assert(isUninit() && "Bad state change"); |
| 593 | MemberPointerData *MPD = new ((void*)(char*)Data) MemberPointerData; |
| 594 | Kind = MemberPointer; |
| 595 | MPD->MemberAndIsDerivedMember.setPointer(Member); |
| 596 | MPD->MemberAndIsDerivedMember.setInt(IsDerivedMember); |
| 597 | MPD->resizePath(Path.size()); |
| 598 | memcpy(MPD->getPath(), Path.data(), Path.size()*sizeof(const CXXRecordDecl*)); |
| 599 | } |