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