Chris Lattner | 981f33b | 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 | f6f003a | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 15 | #include "clang/AST/ASTContext.h" |
Ken Dyck | 0299083 | 2010-01-15 12:37:54 +0000 | [diff] [blame] | 16 | #include "clang/AST/CharUnits.h" |
Richard Smith | f6f003a | 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" |
David Blaikie | 76bd3c8 | 2011-09-23 05:35:21 +0000 | [diff] [blame] | 20 | #include "llvm/Support/ErrorHandling.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 21 | #include "llvm/Support/raw_ostream.h" |
Chris Lattner | 981f33b | 2008-11-16 07:46:48 +0000 | [diff] [blame] | 22 | using namespace clang; |
| 23 | |
Ken Dyck | 0299083 | 2010-01-15 12:37:54 +0000 | [diff] [blame] | 24 | namespace { |
Richard Smith | 8081560 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 25 | struct LVBase { |
Akira Hatanaka | 4e2698c | 2018-04-10 05:15:01 +0000 | [diff] [blame] | 26 | APValue::LValueBase Base; |
Ken Dyck | 0299083 | 2010-01-15 12:37:54 +0000 | [diff] [blame] | 27 | CharUnits Offset; |
Richard Smith | 8081560 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 28 | unsigned PathLength; |
Akira Hatanaka | 4e2698c | 2018-04-10 05:15:01 +0000 | [diff] [blame] | 29 | bool IsNullPtr : 1; |
| 30 | bool IsOnePastTheEnd : 1; |
Ken Dyck | 0299083 | 2010-01-15 12:37:54 +0000 | [diff] [blame] | 31 | }; |
| 32 | } |
| 33 | |
Akira Hatanaka | 4e2698c | 2018-04-10 05:15:01 +0000 | [diff] [blame] | 34 | void *APValue::LValueBase::getOpaqueValue() const { |
| 35 | return Ptr.getOpaqueValue(); |
| 36 | } |
| 37 | |
| 38 | bool APValue::LValueBase::isNull() const { |
| 39 | return Ptr.isNull(); |
| 40 | } |
| 41 | |
| 42 | APValue::LValueBase::operator bool () const { |
| 43 | return static_cast<bool>(Ptr); |
| 44 | } |
| 45 | |
| 46 | clang::APValue::LValueBase |
| 47 | llvm::DenseMapInfo<clang::APValue::LValueBase>::getEmptyKey() { |
| 48 | return clang::APValue::LValueBase( |
| 49 | DenseMapInfo<clang::APValue::LValueBase::PtrTy>::getEmptyKey(), |
| 50 | DenseMapInfo<unsigned>::getEmptyKey(), |
| 51 | DenseMapInfo<unsigned>::getEmptyKey()); |
| 52 | } |
| 53 | |
| 54 | clang::APValue::LValueBase |
| 55 | llvm::DenseMapInfo<clang::APValue::LValueBase>::getTombstoneKey() { |
| 56 | return clang::APValue::LValueBase( |
| 57 | DenseMapInfo<clang::APValue::LValueBase::PtrTy>::getTombstoneKey(), |
| 58 | DenseMapInfo<unsigned>::getTombstoneKey(), |
| 59 | DenseMapInfo<unsigned>::getTombstoneKey()); |
| 60 | } |
| 61 | |
| 62 | unsigned llvm::DenseMapInfo<clang::APValue::LValueBase>::getHashValue( |
| 63 | const clang::APValue::LValueBase &Base) { |
| 64 | llvm::FoldingSetNodeID ID; |
| 65 | ID.AddPointer(Base.getOpaqueValue()); |
| 66 | ID.AddInteger(Base.getCallIndex()); |
| 67 | ID.AddInteger(Base.getVersion()); |
| 68 | return ID.ComputeHash(); |
| 69 | } |
| 70 | |
| 71 | bool llvm::DenseMapInfo<clang::APValue::LValueBase>::isEqual( |
| 72 | const clang::APValue::LValueBase &LHS, |
| 73 | const clang::APValue::LValueBase &RHS) { |
| 74 | return LHS == RHS; |
| 75 | } |
| 76 | |
Richard Smith | 8081560 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 77 | struct APValue::LV : LVBase { |
| 78 | static const unsigned InlinePathSpace = |
Richard Smith | 7073a2d | 2014-01-10 00:40:45 +0000 | [diff] [blame] | 79 | (DataSize - sizeof(LVBase)) / sizeof(LValuePathEntry); |
Richard Smith | 8081560 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 80 | |
| 81 | /// Path - The sequence of base classes, fields and array indices to follow to |
| 82 | /// walk from Base to the subobject. When performing GCC-style folding, there |
| 83 | /// may not be such a path. |
| 84 | union { |
| 85 | LValuePathEntry Path[InlinePathSpace]; |
| 86 | LValuePathEntry *PathPtr; |
| 87 | }; |
| 88 | |
| 89 | LV() { PathLength = (unsigned)-1; } |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 90 | ~LV() { resizePath(0); } |
Richard Smith | 8081560 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 91 | |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 92 | void resizePath(unsigned Length) { |
| 93 | if (Length == PathLength) |
| 94 | return; |
| 95 | if (hasPathPtr()) |
| 96 | delete [] PathPtr; |
| 97 | PathLength = Length; |
| 98 | if (hasPathPtr()) |
| 99 | PathPtr = new LValuePathEntry[Length]; |
Richard Smith | 8081560 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | bool hasPath() const { return PathLength != (unsigned)-1; } |
| 103 | bool hasPathPtr() const { return hasPath() && PathLength > InlinePathSpace; } |
| 104 | |
| 105 | LValuePathEntry *getPath() { return hasPathPtr() ? PathPtr : Path; } |
Richard Smith | bcb4eb2 | 2011-11-07 07:31:09 +0000 | [diff] [blame] | 106 | const LValuePathEntry *getPath() const { |
| 107 | return hasPathPtr() ? PathPtr : Path; |
| 108 | } |
Richard Smith | 8081560 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 109 | }; |
| 110 | |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 111 | namespace { |
| 112 | struct MemberPointerBase { |
| 113 | llvm::PointerIntPair<const ValueDecl*, 1, bool> MemberAndIsDerivedMember; |
| 114 | unsigned PathLength; |
| 115 | }; |
| 116 | } |
| 117 | |
| 118 | struct APValue::MemberPointerData : MemberPointerBase { |
| 119 | static const unsigned InlinePathSpace = |
Richard Smith | 7073a2d | 2014-01-10 00:40:45 +0000 | [diff] [blame] | 120 | (DataSize - sizeof(MemberPointerBase)) / sizeof(const CXXRecordDecl*); |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 121 | typedef const CXXRecordDecl *PathElem; |
| 122 | union { |
| 123 | PathElem Path[InlinePathSpace]; |
| 124 | PathElem *PathPtr; |
| 125 | }; |
| 126 | |
| 127 | MemberPointerData() { PathLength = 0; } |
| 128 | ~MemberPointerData() { resizePath(0); } |
| 129 | |
| 130 | void resizePath(unsigned Length) { |
| 131 | if (Length == PathLength) |
| 132 | return; |
| 133 | if (hasPathPtr()) |
| 134 | delete [] PathPtr; |
| 135 | PathLength = Length; |
| 136 | if (hasPathPtr()) |
| 137 | PathPtr = new PathElem[Length]; |
| 138 | } |
| 139 | |
| 140 | bool hasPathPtr() const { return PathLength > InlinePathSpace; } |
| 141 | |
| 142 | PathElem *getPath() { return hasPathPtr() ? PathPtr : Path; } |
| 143 | const PathElem *getPath() const { |
| 144 | return hasPathPtr() ? PathPtr : Path; |
| 145 | } |
| 146 | }; |
| 147 | |
Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 148 | // FIXME: Reduce the malloc traffic here. |
| 149 | |
| 150 | APValue::Arr::Arr(unsigned NumElts, unsigned Size) : |
| 151 | Elts(new APValue[NumElts + (NumElts != Size ? 1 : 0)]), |
| 152 | NumElts(NumElts), ArrSize(Size) {} |
| 153 | APValue::Arr::~Arr() { delete [] Elts; } |
| 154 | |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 155 | APValue::StructData::StructData(unsigned NumBases, unsigned NumFields) : |
| 156 | Elts(new APValue[NumBases+NumFields]), |
| 157 | NumBases(NumBases), NumFields(NumFields) {} |
| 158 | APValue::StructData::~StructData() { |
| 159 | delete [] Elts; |
| 160 | } |
| 161 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 162 | APValue::UnionData::UnionData() : Field(nullptr), Value(new APValue) {} |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 163 | APValue::UnionData::~UnionData () { |
| 164 | delete Value; |
| 165 | } |
| 166 | |
Richard Smith | 4e9e523 | 2012-03-10 00:28:11 +0000 | [diff] [blame] | 167 | APValue::APValue(const APValue &RHS) : Kind(Uninitialized) { |
| 168 | switch (RHS.getKind()) { |
| 169 | case Uninitialized: |
| 170 | break; |
| 171 | case Int: |
| 172 | MakeInt(); |
Chris Lattner | 981f33b | 2008-11-16 07:46:48 +0000 | [diff] [blame] | 173 | setInt(RHS.getInt()); |
Richard Smith | 4e9e523 | 2012-03-10 00:28:11 +0000 | [diff] [blame] | 174 | break; |
| 175 | case Float: |
| 176 | MakeFloat(); |
Chris Lattner | 981f33b | 2008-11-16 07:46:48 +0000 | [diff] [blame] | 177 | setFloat(RHS.getFloat()); |
Richard Smith | 4e9e523 | 2012-03-10 00:28:11 +0000 | [diff] [blame] | 178 | break; |
| 179 | case Vector: |
| 180 | MakeVector(); |
Richard Smith | 7073a2d | 2014-01-10 00:40:45 +0000 | [diff] [blame] | 181 | setVector(((const Vec *)(const char *)RHS.Data.buffer)->Elts, |
Dan Gohman | 145f3f1 | 2010-04-19 16:39:44 +0000 | [diff] [blame] | 182 | RHS.getVectorLength()); |
Richard Smith | 4e9e523 | 2012-03-10 00:28:11 +0000 | [diff] [blame] | 183 | break; |
| 184 | case ComplexInt: |
| 185 | MakeComplexInt(); |
Chris Lattner | 981f33b | 2008-11-16 07:46:48 +0000 | [diff] [blame] | 186 | setComplexInt(RHS.getComplexIntReal(), RHS.getComplexIntImag()); |
Richard Smith | 4e9e523 | 2012-03-10 00:28:11 +0000 | [diff] [blame] | 187 | break; |
| 188 | case ComplexFloat: |
| 189 | MakeComplexFloat(); |
Chris Lattner | 981f33b | 2008-11-16 07:46:48 +0000 | [diff] [blame] | 190 | setComplexFloat(RHS.getComplexFloatReal(), RHS.getComplexFloatImag()); |
Richard Smith | 4e9e523 | 2012-03-10 00:28:11 +0000 | [diff] [blame] | 191 | break; |
| 192 | case LValue: |
| 193 | MakeLValue(); |
Richard Smith | 8081560 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 194 | if (RHS.hasLValuePath()) |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 195 | setLValue(RHS.getLValueBase(), RHS.getLValueOffset(), RHS.getLValuePath(), |
Akira Hatanaka | 4e2698c | 2018-04-10 05:15:01 +0000 | [diff] [blame] | 196 | RHS.isLValueOnePastTheEnd(), RHS.isNullPointer()); |
Richard Smith | 8081560 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 197 | else |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 198 | setLValue(RHS.getLValueBase(), RHS.getLValueOffset(), NoLValuePath(), |
Akira Hatanaka | 4e2698c | 2018-04-10 05:15:01 +0000 | [diff] [blame] | 199 | RHS.isNullPointer()); |
Richard Smith | 4e9e523 | 2012-03-10 00:28:11 +0000 | [diff] [blame] | 200 | break; |
| 201 | case Array: |
| 202 | MakeArray(RHS.getArrayInitializedElts(), RHS.getArraySize()); |
Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 203 | for (unsigned I = 0, N = RHS.getArrayInitializedElts(); I != N; ++I) |
| 204 | getArrayInitializedElt(I) = RHS.getArrayInitializedElt(I); |
| 205 | if (RHS.hasArrayFiller()) |
| 206 | getArrayFiller() = RHS.getArrayFiller(); |
Richard Smith | 4e9e523 | 2012-03-10 00:28:11 +0000 | [diff] [blame] | 207 | break; |
| 208 | case Struct: |
| 209 | MakeStruct(RHS.getStructNumBases(), RHS.getStructNumFields()); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 210 | for (unsigned I = 0, N = RHS.getStructNumBases(); I != N; ++I) |
| 211 | getStructBase(I) = RHS.getStructBase(I); |
| 212 | for (unsigned I = 0, N = RHS.getStructNumFields(); I != N; ++I) |
| 213 | getStructField(I) = RHS.getStructField(I); |
Richard Smith | 4e9e523 | 2012-03-10 00:28:11 +0000 | [diff] [blame] | 214 | break; |
| 215 | case Union: |
| 216 | MakeUnion(); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 217 | setUnion(RHS.getUnionField(), RHS.getUnionValue()); |
Richard Smith | 4e9e523 | 2012-03-10 00:28:11 +0000 | [diff] [blame] | 218 | break; |
| 219 | case MemberPointer: |
| 220 | MakeMemberPointer(RHS.getMemberPointerDecl(), |
| 221 | RHS.isMemberPointerToDerivedMember(), |
| 222 | RHS.getMemberPointerPath()); |
| 223 | break; |
| 224 | case AddrLabelDiff: |
| 225 | MakeAddrLabelDiff(); |
Eli Friedman | fd5e54d | 2012-01-04 23:13:47 +0000 | [diff] [blame] | 226 | setAddrLabelDiff(RHS.getAddrLabelDiffLHS(), RHS.getAddrLabelDiffRHS()); |
Richard Smith | 4e9e523 | 2012-03-10 00:28:11 +0000 | [diff] [blame] | 227 | break; |
Eli Friedman | fd5e54d | 2012-01-04 23:13:47 +0000 | [diff] [blame] | 228 | } |
Chris Lattner | 981f33b | 2008-11-16 07:46:48 +0000 | [diff] [blame] | 229 | } |
| 230 | |
Daniel Dunbar | b743157 | 2012-03-08 20:28:55 +0000 | [diff] [blame] | 231 | void APValue::DestroyDataAndMakeUninit() { |
Chris Lattner | 981f33b | 2008-11-16 07:46:48 +0000 | [diff] [blame] | 232 | if (Kind == Int) |
Richard Smith | 7073a2d | 2014-01-10 00:40:45 +0000 | [diff] [blame] | 233 | ((APSInt*)(char*)Data.buffer)->~APSInt(); |
Chris Lattner | 981f33b | 2008-11-16 07:46:48 +0000 | [diff] [blame] | 234 | else if (Kind == Float) |
Richard Smith | 7073a2d | 2014-01-10 00:40:45 +0000 | [diff] [blame] | 235 | ((APFloat*)(char*)Data.buffer)->~APFloat(); |
Nate Begeman | 1e31b16 | 2009-01-18 01:01:34 +0000 | [diff] [blame] | 236 | else if (Kind == Vector) |
Richard Smith | 7073a2d | 2014-01-10 00:40:45 +0000 | [diff] [blame] | 237 | ((Vec*)(char*)Data.buffer)->~Vec(); |
Chris Lattner | 981f33b | 2008-11-16 07:46:48 +0000 | [diff] [blame] | 238 | else if (Kind == ComplexInt) |
Richard Smith | 7073a2d | 2014-01-10 00:40:45 +0000 | [diff] [blame] | 239 | ((ComplexAPSInt*)(char*)Data.buffer)->~ComplexAPSInt(); |
Chris Lattner | 981f33b | 2008-11-16 07:46:48 +0000 | [diff] [blame] | 240 | else if (Kind == ComplexFloat) |
Richard Smith | 7073a2d | 2014-01-10 00:40:45 +0000 | [diff] [blame] | 241 | ((ComplexAPFloat*)(char*)Data.buffer)->~ComplexAPFloat(); |
Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 242 | else if (Kind == LValue) |
Richard Smith | 7073a2d | 2014-01-10 00:40:45 +0000 | [diff] [blame] | 243 | ((LV*)(char*)Data.buffer)->~LV(); |
Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 244 | else if (Kind == Array) |
Richard Smith | 7073a2d | 2014-01-10 00:40:45 +0000 | [diff] [blame] | 245 | ((Arr*)(char*)Data.buffer)->~Arr(); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 246 | else if (Kind == Struct) |
Richard Smith | 7073a2d | 2014-01-10 00:40:45 +0000 | [diff] [blame] | 247 | ((StructData*)(char*)Data.buffer)->~StructData(); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 248 | else if (Kind == Union) |
Richard Smith | 7073a2d | 2014-01-10 00:40:45 +0000 | [diff] [blame] | 249 | ((UnionData*)(char*)Data.buffer)->~UnionData(); |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 250 | else if (Kind == MemberPointer) |
Richard Smith | 7073a2d | 2014-01-10 00:40:45 +0000 | [diff] [blame] | 251 | ((MemberPointerData*)(char*)Data.buffer)->~MemberPointerData(); |
Eli Friedman | fd5e54d | 2012-01-04 23:13:47 +0000 | [diff] [blame] | 252 | else if (Kind == AddrLabelDiff) |
Richard Smith | 7073a2d | 2014-01-10 00:40:45 +0000 | [diff] [blame] | 253 | ((AddrLabelDiffData*)(char*)Data.buffer)->~AddrLabelDiffData(); |
Nate Begeman | 1e31b16 | 2009-01-18 01:01:34 +0000 | [diff] [blame] | 254 | Kind = Uninitialized; |
Chris Lattner | 981f33b | 2008-11-16 07:46:48 +0000 | [diff] [blame] | 255 | } |
| 256 | |
Manuel Klimek | a732899 | 2013-06-03 13:51:33 +0000 | [diff] [blame] | 257 | bool APValue::needsCleanup() const { |
| 258 | switch (getKind()) { |
| 259 | case Uninitialized: |
| 260 | case AddrLabelDiff: |
| 261 | return false; |
| 262 | case Struct: |
| 263 | case Union: |
| 264 | case Array: |
| 265 | case Vector: |
| 266 | return true; |
| 267 | case Int: |
| 268 | return getInt().needsCleanup(); |
| 269 | case Float: |
| 270 | return getFloat().needsCleanup(); |
| 271 | case ComplexFloat: |
| 272 | assert(getComplexFloatImag().needsCleanup() == |
| 273 | getComplexFloatReal().needsCleanup() && |
| 274 | "In _Complex float types, real and imaginary values always have the " |
| 275 | "same size."); |
| 276 | return getComplexFloatReal().needsCleanup(); |
| 277 | case ComplexInt: |
| 278 | assert(getComplexIntImag().needsCleanup() == |
| 279 | getComplexIntReal().needsCleanup() && |
| 280 | "In _Complex int types, real and imaginary values must have the " |
| 281 | "same size."); |
| 282 | return getComplexIntReal().needsCleanup(); |
| 283 | case LValue: |
Richard Smith | 7073a2d | 2014-01-10 00:40:45 +0000 | [diff] [blame] | 284 | return reinterpret_cast<const LV *>(Data.buffer)->hasPathPtr(); |
Manuel Klimek | a732899 | 2013-06-03 13:51:33 +0000 | [diff] [blame] | 285 | case MemberPointer: |
Richard Smith | 7073a2d | 2014-01-10 00:40:45 +0000 | [diff] [blame] | 286 | return reinterpret_cast<const MemberPointerData *>(Data.buffer) |
| 287 | ->hasPathPtr(); |
Manuel Klimek | a732899 | 2013-06-03 13:51:33 +0000 | [diff] [blame] | 288 | } |
Benjamin Kramer | d1b7cd7 | 2013-06-03 21:26:13 +0000 | [diff] [blame] | 289 | llvm_unreachable("Unknown APValue kind!"); |
Manuel Klimek | a732899 | 2013-06-03 13:51:33 +0000 | [diff] [blame] | 290 | } |
| 291 | |
Richard Smith | 4e9e523 | 2012-03-10 00:28:11 +0000 | [diff] [blame] | 292 | void APValue::swap(APValue &RHS) { |
| 293 | std::swap(Kind, RHS.Kind); |
Richard Smith | 7073a2d | 2014-01-10 00:40:45 +0000 | [diff] [blame] | 294 | char TmpData[DataSize]; |
| 295 | memcpy(TmpData, Data.buffer, DataSize); |
| 296 | memcpy(Data.buffer, RHS.Data.buffer, DataSize); |
| 297 | memcpy(RHS.Data.buffer, TmpData, DataSize); |
Richard Smith | 4e9e523 | 2012-03-10 00:28:11 +0000 | [diff] [blame] | 298 | } |
| 299 | |
Yaron Keren | cdae941 | 2016-01-29 19:38:18 +0000 | [diff] [blame] | 300 | LLVM_DUMP_METHOD void APValue::dump() const { |
Richard Smith | f6f003a | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 301 | dump(llvm::errs()); |
Chris Lattner | 981f33b | 2008-11-16 07:46:48 +0000 | [diff] [blame] | 302 | llvm::errs() << '\n'; |
Chris Lattner | 981f33b | 2008-11-16 07:46:48 +0000 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | static double GetApproxValue(const llvm::APFloat &F) { |
| 306 | llvm::APFloat V = F; |
| 307 | bool ignored; |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 308 | V.convert(llvm::APFloat::IEEEdouble(), llvm::APFloat::rmNearestTiesToEven, |
Chris Lattner | 981f33b | 2008-11-16 07:46:48 +0000 | [diff] [blame] | 309 | &ignored); |
| 310 | return V.convertToDouble(); |
| 311 | } |
| 312 | |
Richard Smith | f6f003a | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 313 | void APValue::dump(raw_ostream &OS) const { |
Chris Lattner | 981f33b | 2008-11-16 07:46:48 +0000 | [diff] [blame] | 314 | switch (getKind()) { |
Chris Lattner | 981f33b | 2008-11-16 07:46:48 +0000 | [diff] [blame] | 315 | case Uninitialized: |
| 316 | OS << "Uninitialized"; |
| 317 | return; |
| 318 | case Int: |
| 319 | OS << "Int: " << getInt(); |
| 320 | return; |
| 321 | case Float: |
| 322 | OS << "Float: " << GetApproxValue(getFloat()); |
| 323 | return; |
Nate Begeman | 1e31b16 | 2009-01-18 01:01:34 +0000 | [diff] [blame] | 324 | case Vector: |
Richard Smith | f6f003a | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 325 | OS << "Vector: "; |
| 326 | getVectorElt(0).dump(OS); |
| 327 | for (unsigned i = 1; i != getVectorLength(); ++i) { |
| 328 | OS << ", "; |
| 329 | getVectorElt(i).dump(OS); |
| 330 | } |
Nate Begeman | 1e31b16 | 2009-01-18 01:01:34 +0000 | [diff] [blame] | 331 | return; |
Chris Lattner | 981f33b | 2008-11-16 07:46:48 +0000 | [diff] [blame] | 332 | case ComplexInt: |
| 333 | OS << "ComplexInt: " << getComplexIntReal() << ", " << getComplexIntImag(); |
| 334 | return; |
| 335 | case ComplexFloat: |
| 336 | OS << "ComplexFloat: " << GetApproxValue(getComplexFloatReal()) |
| 337 | << ", " << GetApproxValue(getComplexFloatImag()); |
Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 338 | return; |
Chris Lattner | 981f33b | 2008-11-16 07:46:48 +0000 | [diff] [blame] | 339 | case LValue: |
| 340 | OS << "LValue: <todo>"; |
| 341 | return; |
Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 342 | case Array: |
| 343 | OS << "Array: "; |
| 344 | for (unsigned I = 0, N = getArrayInitializedElts(); I != N; ++I) { |
Richard Smith | f6f003a | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 345 | getArrayInitializedElt(I).dump(OS); |
Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 346 | if (I != getArraySize() - 1) OS << ", "; |
| 347 | } |
Richard Smith | f6f003a | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 348 | if (hasArrayFiller()) { |
| 349 | OS << getArraySize() - getArrayInitializedElts() << " x "; |
| 350 | getArrayFiller().dump(OS); |
| 351 | } |
Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 352 | return; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 353 | case Struct: |
| 354 | OS << "Struct "; |
| 355 | if (unsigned N = getStructNumBases()) { |
Richard Smith | f6f003a | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 356 | OS << " bases: "; |
| 357 | getStructBase(0).dump(OS); |
| 358 | for (unsigned I = 1; I != N; ++I) { |
| 359 | OS << ", "; |
| 360 | getStructBase(I).dump(OS); |
| 361 | } |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 362 | } |
| 363 | if (unsigned N = getStructNumFields()) { |
Richard Smith | f6f003a | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 364 | OS << " fields: "; |
| 365 | getStructField(0).dump(OS); |
| 366 | for (unsigned I = 1; I != N; ++I) { |
| 367 | OS << ", "; |
| 368 | getStructField(I).dump(OS); |
| 369 | } |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 370 | } |
| 371 | return; |
| 372 | case Union: |
Richard Smith | f6f003a | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 373 | OS << "Union: "; |
| 374 | getUnionValue().dump(OS); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 375 | return; |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 376 | case MemberPointer: |
| 377 | OS << "MemberPointer: <todo>"; |
| 378 | return; |
Eli Friedman | fd5e54d | 2012-01-04 23:13:47 +0000 | [diff] [blame] | 379 | case AddrLabelDiff: |
| 380 | OS << "AddrLabelDiff: <todo>"; |
| 381 | return; |
Chris Lattner | 981f33b | 2008-11-16 07:46:48 +0000 | [diff] [blame] | 382 | } |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 383 | llvm_unreachable("Unknown APValue kind!"); |
Chris Lattner | 981f33b | 2008-11-16 07:46:48 +0000 | [diff] [blame] | 384 | } |
| 385 | |
Richard Smith | f6f003a | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 386 | void APValue::printPretty(raw_ostream &Out, ASTContext &Ctx, QualType Ty) const{ |
| 387 | switch (getKind()) { |
Jeffrey Yasskin | d2af962 | 2011-07-18 16:43:53 +0000 | [diff] [blame] | 388 | case APValue::Uninitialized: |
Richard Smith | f6f003a | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 389 | Out << "<uninitialized>"; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 390 | return; |
Jeffrey Yasskin | d2af962 | 2011-07-18 16:43:53 +0000 | [diff] [blame] | 391 | case APValue::Int: |
Richard Smith | 5614ca7 | 2012-03-23 23:55:39 +0000 | [diff] [blame] | 392 | if (Ty->isBooleanType()) |
| 393 | Out << (getInt().getBoolValue() ? "true" : "false"); |
| 394 | else |
| 395 | Out << getInt(); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 396 | return; |
Jeffrey Yasskin | d2af962 | 2011-07-18 16:43:53 +0000 | [diff] [blame] | 397 | case APValue::Float: |
Richard Smith | f6f003a | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 398 | Out << GetApproxValue(getFloat()); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 399 | return; |
Richard Smith | f6f003a | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 400 | case APValue::Vector: { |
| 401 | Out << '{'; |
| 402 | QualType ElemTy = Ty->getAs<VectorType>()->getElementType(); |
| 403 | getVectorElt(0).printPretty(Out, Ctx, ElemTy); |
| 404 | for (unsigned i = 1; i != getVectorLength(); ++i) { |
Jeffrey Yasskin | d2af962 | 2011-07-18 16:43:53 +0000 | [diff] [blame] | 405 | Out << ", "; |
Richard Smith | f6f003a | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 406 | getVectorElt(i).printPretty(Out, Ctx, ElemTy); |
Jeffrey Yasskin | d2af962 | 2011-07-18 16:43:53 +0000 | [diff] [blame] | 407 | } |
Richard Smith | f6f003a | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 408 | Out << '}'; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 409 | return; |
Richard Smith | f6f003a | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 410 | } |
Jeffrey Yasskin | d2af962 | 2011-07-18 16:43:53 +0000 | [diff] [blame] | 411 | case APValue::ComplexInt: |
Richard Smith | f6f003a | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 412 | Out << getComplexIntReal() << "+" << getComplexIntImag() << "i"; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 413 | return; |
Jeffrey Yasskin | d2af962 | 2011-07-18 16:43:53 +0000 | [diff] [blame] | 414 | case APValue::ComplexFloat: |
Richard Smith | f6f003a | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 415 | Out << GetApproxValue(getComplexFloatReal()) << "+" |
| 416 | << GetApproxValue(getComplexFloatImag()) << "i"; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 417 | return; |
Richard Smith | f6f003a | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 418 | case APValue::LValue: { |
Richard Smith | f6f003a | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 419 | bool IsReference = Ty->isReferenceType(); |
| 420 | QualType InnerTy |
| 421 | = IsReference ? Ty.getNonReferenceType() : Ty->getPointeeType(); |
Douglas Gregor | 0b7bc7f | 2013-01-29 01:26:43 +0000 | [diff] [blame] | 422 | if (InnerTy.isNull()) |
| 423 | InnerTy = Ty; |
Richard Smith | f6f003a | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 424 | |
Richard Smith | 128719c | 2018-09-13 22:47:33 +0000 | [diff] [blame] | 425 | LValueBase Base = getLValueBase(); |
| 426 | if (!Base) { |
| 427 | if (isNullPointer()) { |
| 428 | Out << (Ctx.getLangOpts().CPlusPlus11 ? "nullptr" : "0"); |
| 429 | } else if (IsReference) { |
| 430 | Out << "*(" << InnerTy.stream(Ctx.getPrintingPolicy()) << "*)" |
| 431 | << getLValueOffset().getQuantity(); |
| 432 | } else { |
| 433 | Out << "(" << Ty.stream(Ctx.getPrintingPolicy()) << ")" |
| 434 | << getLValueOffset().getQuantity(); |
| 435 | } |
| 436 | return; |
| 437 | } |
| 438 | |
Richard Smith | f6f003a | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 439 | if (!hasLValuePath()) { |
| 440 | // No lvalue path: just print the offset. |
| 441 | CharUnits O = getLValueOffset(); |
| 442 | CharUnits S = Ctx.getTypeSizeInChars(InnerTy); |
| 443 | if (!O.isZero()) { |
| 444 | if (IsReference) |
| 445 | Out << "*("; |
| 446 | if (O % S) { |
| 447 | Out << "(char*)"; |
| 448 | S = CharUnits::One(); |
| 449 | } |
| 450 | Out << '&'; |
| 451 | } else if (!IsReference) |
| 452 | Out << '&'; |
| 453 | |
| 454 | if (const ValueDecl *VD = Base.dyn_cast<const ValueDecl*>()) |
| 455 | Out << *VD; |
Richard Trieu | ddd01ce | 2014-06-09 22:53:25 +0000 | [diff] [blame] | 456 | else { |
| 457 | assert(Base.get<const Expr *>() != nullptr && |
| 458 | "Expecting non-null Expr"); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 459 | Base.get<const Expr*>()->printPretty(Out, nullptr, |
| 460 | Ctx.getPrintingPolicy()); |
Richard Trieu | ddd01ce | 2014-06-09 22:53:25 +0000 | [diff] [blame] | 461 | } |
| 462 | |
Richard Smith | f6f003a | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 463 | if (!O.isZero()) { |
| 464 | Out << " + " << (O / S); |
| 465 | if (IsReference) |
| 466 | Out << ')'; |
| 467 | } |
| 468 | return; |
| 469 | } |
| 470 | |
| 471 | // We have an lvalue path. Print it out nicely. |
| 472 | if (!IsReference) |
| 473 | Out << '&'; |
| 474 | else if (isLValueOnePastTheEnd()) |
| 475 | Out << "*(&"; |
| 476 | |
| 477 | QualType ElemTy; |
| 478 | if (const ValueDecl *VD = Base.dyn_cast<const ValueDecl*>()) { |
| 479 | Out << *VD; |
| 480 | ElemTy = VD->getType(); |
| 481 | } else { |
| 482 | const Expr *E = Base.get<const Expr*>(); |
Richard Trieu | ddd01ce | 2014-06-09 22:53:25 +0000 | [diff] [blame] | 483 | assert(E != nullptr && "Expecting non-null Expr"); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 484 | E->printPretty(Out, nullptr, Ctx.getPrintingPolicy()); |
Richard Smith | f6f003a | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 485 | ElemTy = E->getType(); |
| 486 | } |
| 487 | |
| 488 | ArrayRef<LValuePathEntry> Path = getLValuePath(); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 489 | const CXXRecordDecl *CastToBase = nullptr; |
Richard Smith | f6f003a | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 490 | for (unsigned I = 0, N = Path.size(); I != N; ++I) { |
| 491 | if (ElemTy->getAs<RecordType>()) { |
| 492 | // The lvalue refers to a class type, so the next path entry is a base |
| 493 | // or member. |
| 494 | const Decl *BaseOrMember = |
| 495 | BaseOrMemberType::getFromOpaqueValue(Path[I].BaseOrMember).getPointer(); |
| 496 | if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(BaseOrMember)) { |
| 497 | CastToBase = RD; |
| 498 | ElemTy = Ctx.getRecordType(RD); |
| 499 | } else { |
| 500 | const ValueDecl *VD = cast<ValueDecl>(BaseOrMember); |
| 501 | Out << "."; |
| 502 | if (CastToBase) |
| 503 | Out << *CastToBase << "::"; |
| 504 | Out << *VD; |
| 505 | ElemTy = VD->getType(); |
| 506 | } |
| 507 | } else { |
| 508 | // The lvalue must refer to an array. |
| 509 | Out << '[' << Path[I].ArrayIndex << ']'; |
| 510 | ElemTy = Ctx.getAsArrayType(ElemTy)->getElementType(); |
| 511 | } |
| 512 | } |
| 513 | |
| 514 | // Handle formatting of one-past-the-end lvalues. |
| 515 | if (isLValueOnePastTheEnd()) { |
| 516 | // FIXME: If CastToBase is non-0, we should prefix the output with |
| 517 | // "(CastToBase*)". |
| 518 | Out << " + 1"; |
| 519 | if (IsReference) |
| 520 | Out << ')'; |
| 521 | } |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 522 | return; |
Richard Smith | f6f003a | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 523 | } |
| 524 | case APValue::Array: { |
| 525 | const ArrayType *AT = Ctx.getAsArrayType(Ty); |
| 526 | QualType ElemTy = AT->getElementType(); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 527 | Out << '{'; |
Richard Smith | f6f003a | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 528 | if (unsigned N = getArrayInitializedElts()) { |
| 529 | getArrayInitializedElt(0).printPretty(Out, Ctx, ElemTy); |
| 530 | for (unsigned I = 1; I != N; ++I) { |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 531 | Out << ", "; |
Richard Smith | f6f003a | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 532 | if (I == 10) { |
| 533 | // Avoid printing out the entire contents of large arrays. |
| 534 | Out << "..."; |
| 535 | break; |
| 536 | } |
| 537 | getArrayInitializedElt(I).printPretty(Out, Ctx, ElemTy); |
| 538 | } |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 539 | } |
| 540 | Out << '}'; |
| 541 | return; |
Richard Smith | f6f003a | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 542 | } |
| 543 | case APValue::Struct: { |
| 544 | Out << '{'; |
| 545 | const RecordDecl *RD = Ty->getAs<RecordType>()->getDecl(); |
| 546 | bool First = true; |
| 547 | if (unsigned N = getStructNumBases()) { |
| 548 | const CXXRecordDecl *CD = cast<CXXRecordDecl>(RD); |
| 549 | CXXRecordDecl::base_class_const_iterator BI = CD->bases_begin(); |
| 550 | for (unsigned I = 0; I != N; ++I, ++BI) { |
| 551 | assert(BI != CD->bases_end()); |
| 552 | if (!First) |
| 553 | Out << ", "; |
| 554 | getStructBase(I).printPretty(Out, Ctx, BI->getType()); |
| 555 | First = false; |
| 556 | } |
| 557 | } |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 558 | for (const auto *FI : RD->fields()) { |
Richard Smith | f6f003a | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 559 | if (!First) |
| 560 | Out << ", "; |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 561 | if (FI->isUnnamedBitfield()) continue; |
| 562 | getStructField(FI->getFieldIndex()). |
| 563 | printPretty(Out, Ctx, FI->getType()); |
Richard Smith | f6f003a | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 564 | First = false; |
| 565 | } |
| 566 | Out << '}'; |
| 567 | return; |
| 568 | } |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 569 | case APValue::Union: |
Richard Smith | f6f003a | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 570 | Out << '{'; |
| 571 | if (const FieldDecl *FD = getUnionField()) { |
| 572 | Out << "." << *FD << " = "; |
| 573 | getUnionValue().printPretty(Out, Ctx, FD->getType()); |
| 574 | } |
| 575 | Out << '}'; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 576 | return; |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 577 | case APValue::MemberPointer: |
Richard Smith | f6f003a | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 578 | // FIXME: This is not enough to unambiguously identify the member in a |
| 579 | // multiple-inheritance scenario. |
| 580 | if (const ValueDecl *VD = getMemberPointerDecl()) { |
| 581 | Out << '&' << *cast<CXXRecordDecl>(VD->getDeclContext()) << "::" << *VD; |
| 582 | return; |
| 583 | } |
| 584 | Out << "0"; |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 585 | return; |
Eli Friedman | fd5e54d | 2012-01-04 23:13:47 +0000 | [diff] [blame] | 586 | case APValue::AddrLabelDiff: |
| 587 | Out << "&&" << getAddrLabelDiffLHS()->getLabel()->getName(); |
| 588 | Out << " - "; |
| 589 | Out << "&&" << getAddrLabelDiffRHS()->getLabel()->getName(); |
| 590 | return; |
Jeffrey Yasskin | d2af962 | 2011-07-18 16:43:53 +0000 | [diff] [blame] | 591 | } |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 592 | llvm_unreachable("Unknown APValue kind!"); |
Jeffrey Yasskin | d2af962 | 2011-07-18 16:43:53 +0000 | [diff] [blame] | 593 | } |
| 594 | |
Richard Smith | f6f003a | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 595 | std::string APValue::getAsString(ASTContext &Ctx, QualType Ty) const { |
| 596 | std::string Result; |
| 597 | llvm::raw_string_ostream Out(Result); |
| 598 | printPretty(Out, Ctx, Ty); |
Eli Friedman | 375f09f | 2011-12-16 22:12:23 +0000 | [diff] [blame] | 599 | Out.flush(); |
Richard Smith | f6f003a | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 600 | return Result; |
Jeffrey Yasskin | d2af962 | 2011-07-18 16:43:53 +0000 | [diff] [blame] | 601 | } |
| 602 | |
Richard Smith | ce40ad6 | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 603 | const APValue::LValueBase APValue::getLValueBase() const { |
Ken Dyck | 0299083 | 2010-01-15 12:37:54 +0000 | [diff] [blame] | 604 | assert(isLValue() && "Invalid accessor"); |
Akira Hatanaka | 4e2698c | 2018-04-10 05:15:01 +0000 | [diff] [blame] | 605 | return ((const LV*)(const void*)Data.buffer)->Base; |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 606 | } |
| 607 | |
| 608 | bool APValue::isLValueOnePastTheEnd() const { |
| 609 | assert(isLValue() && "Invalid accessor"); |
Akira Hatanaka | 4e2698c | 2018-04-10 05:15:01 +0000 | [diff] [blame] | 610 | return ((const LV*)(const void*)Data.buffer)->IsOnePastTheEnd; |
Ken Dyck | 0299083 | 2010-01-15 12:37:54 +0000 | [diff] [blame] | 611 | } |
| 612 | |
Richard Smith | 0b0a0b6 | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 613 | CharUnits &APValue::getLValueOffset() { |
| 614 | assert(isLValue() && "Invalid accessor"); |
Richard Smith | 7073a2d | 2014-01-10 00:40:45 +0000 | [diff] [blame] | 615 | return ((LV*)(void*)Data.buffer)->Offset; |
Ken Dyck | 0299083 | 2010-01-15 12:37:54 +0000 | [diff] [blame] | 616 | } |
| 617 | |
Richard Smith | 8081560 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 618 | bool APValue::hasLValuePath() const { |
Ken Dyck | 0299083 | 2010-01-15 12:37:54 +0000 | [diff] [blame] | 619 | assert(isLValue() && "Invalid accessor"); |
Richard Smith | 7073a2d | 2014-01-10 00:40:45 +0000 | [diff] [blame] | 620 | return ((const LV*)(const char*)Data.buffer)->hasPath(); |
Richard Smith | 8081560 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 621 | } |
| 622 | |
| 623 | ArrayRef<APValue::LValuePathEntry> APValue::getLValuePath() const { |
| 624 | assert(isLValue() && hasLValuePath() && "Invalid accessor"); |
Richard Smith | 7073a2d | 2014-01-10 00:40:45 +0000 | [diff] [blame] | 625 | const LV &LVal = *((const LV*)(const char*)Data.buffer); |
Craig Topper | 5fc8fc2 | 2014-08-27 06:28:36 +0000 | [diff] [blame] | 626 | return llvm::makeArrayRef(LVal.getPath(), LVal.PathLength); |
Richard Smith | 8081560 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 627 | } |
| 628 | |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 629 | unsigned APValue::getLValueCallIndex() const { |
| 630 | assert(isLValue() && "Invalid accessor"); |
Akira Hatanaka | 4e2698c | 2018-04-10 05:15:01 +0000 | [diff] [blame] | 631 | return ((const LV*)(const char*)Data.buffer)->Base.getCallIndex(); |
| 632 | } |
| 633 | |
| 634 | unsigned APValue::getLValueVersion() const { |
| 635 | assert(isLValue() && "Invalid accessor"); |
| 636 | return ((const LV*)(const char*)Data.buffer)->Base.getVersion(); |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 637 | } |
| 638 | |
Yaxun Liu | 402804b | 2016-12-15 08:09:08 +0000 | [diff] [blame] | 639 | bool APValue::isNullPointer() const { |
| 640 | assert(isLValue() && "Invalid usage"); |
| 641 | return ((const LV*)(const char*)Data.buffer)->IsNullPtr; |
| 642 | } |
| 643 | |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 644 | void APValue::setLValue(LValueBase B, const CharUnits &O, NoLValuePath, |
Akira Hatanaka | 4e2698c | 2018-04-10 05:15:01 +0000 | [diff] [blame] | 645 | bool IsNullPtr) { |
Richard Smith | 8081560 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 646 | assert(isLValue() && "Invalid accessor"); |
Richard Smith | 7073a2d | 2014-01-10 00:40:45 +0000 | [diff] [blame] | 647 | LV &LVal = *((LV*)(char*)Data.buffer); |
Akira Hatanaka | 4e2698c | 2018-04-10 05:15:01 +0000 | [diff] [blame] | 648 | LVal.Base = B; |
| 649 | LVal.IsOnePastTheEnd = false; |
Richard Smith | 8081560 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 650 | LVal.Offset = O; |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 651 | LVal.resizePath((unsigned)-1); |
Yaxun Liu | 402804b | 2016-12-15 08:09:08 +0000 | [diff] [blame] | 652 | LVal.IsNullPtr = IsNullPtr; |
Richard Smith | 8081560 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 653 | } |
| 654 | |
Richard Smith | ce40ad6 | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 655 | void APValue::setLValue(LValueBase B, const CharUnits &O, |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 656 | ArrayRef<LValuePathEntry> Path, bool IsOnePastTheEnd, |
Akira Hatanaka | 4e2698c | 2018-04-10 05:15:01 +0000 | [diff] [blame] | 657 | bool IsNullPtr) { |
Richard Smith | 8081560 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 658 | assert(isLValue() && "Invalid accessor"); |
Richard Smith | 7073a2d | 2014-01-10 00:40:45 +0000 | [diff] [blame] | 659 | LV &LVal = *((LV*)(char*)Data.buffer); |
Akira Hatanaka | 4e2698c | 2018-04-10 05:15:01 +0000 | [diff] [blame] | 660 | LVal.Base = B; |
| 661 | LVal.IsOnePastTheEnd = IsOnePastTheEnd; |
Richard Smith | 8081560 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 662 | LVal.Offset = O; |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 663 | LVal.resizePath(Path.size()); |
Richard Smith | 8081560 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 664 | memcpy(LVal.getPath(), Path.data(), Path.size() * sizeof(LValuePathEntry)); |
Yaxun Liu | 402804b | 2016-12-15 08:09:08 +0000 | [diff] [blame] | 665 | LVal.IsNullPtr = IsNullPtr; |
Ken Dyck | 0299083 | 2010-01-15 12:37:54 +0000 | [diff] [blame] | 666 | } |
| 667 | |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 668 | const ValueDecl *APValue::getMemberPointerDecl() const { |
| 669 | assert(isMemberPointer() && "Invalid accessor"); |
Richard Smith | 7073a2d | 2014-01-10 00:40:45 +0000 | [diff] [blame] | 670 | const MemberPointerData &MPD = |
| 671 | *((const MemberPointerData *)(const char *)Data.buffer); |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 672 | return MPD.MemberAndIsDerivedMember.getPointer(); |
| 673 | } |
| 674 | |
| 675 | bool APValue::isMemberPointerToDerivedMember() const { |
| 676 | assert(isMemberPointer() && "Invalid accessor"); |
Richard Smith | 7073a2d | 2014-01-10 00:40:45 +0000 | [diff] [blame] | 677 | const MemberPointerData &MPD = |
| 678 | *((const MemberPointerData *)(const char *)Data.buffer); |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 679 | return MPD.MemberAndIsDerivedMember.getInt(); |
| 680 | } |
| 681 | |
| 682 | ArrayRef<const CXXRecordDecl*> APValue::getMemberPointerPath() const { |
| 683 | assert(isMemberPointer() && "Invalid accessor"); |
Richard Smith | 7073a2d | 2014-01-10 00:40:45 +0000 | [diff] [blame] | 684 | const MemberPointerData &MPD = |
| 685 | *((const MemberPointerData *)(const char *)Data.buffer); |
Craig Topper | 5fc8fc2 | 2014-08-27 06:28:36 +0000 | [diff] [blame] | 686 | return llvm::makeArrayRef(MPD.getPath(), MPD.PathLength); |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 687 | } |
| 688 | |
Ken Dyck | 0299083 | 2010-01-15 12:37:54 +0000 | [diff] [blame] | 689 | void APValue::MakeLValue() { |
| 690 | assert(isUninit() && "Bad state change"); |
Benjamin Kramer | a939c23 | 2014-03-15 18:54:13 +0000 | [diff] [blame] | 691 | static_assert(sizeof(LV) <= DataSize, "LV too big"); |
Richard Smith | 7073a2d | 2014-01-10 00:40:45 +0000 | [diff] [blame] | 692 | new ((void*)(char*)Data.buffer) LV(); |
Ken Dyck | 0299083 | 2010-01-15 12:37:54 +0000 | [diff] [blame] | 693 | Kind = LValue; |
| 694 | } |
Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 695 | |
| 696 | void APValue::MakeArray(unsigned InitElts, unsigned Size) { |
| 697 | assert(isUninit() && "Bad state change"); |
Richard Smith | 7073a2d | 2014-01-10 00:40:45 +0000 | [diff] [blame] | 698 | new ((void*)(char*)Data.buffer) Arr(InitElts, Size); |
Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 699 | Kind = Array; |
| 700 | } |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 701 | |
| 702 | void APValue::MakeMemberPointer(const ValueDecl *Member, bool IsDerivedMember, |
| 703 | ArrayRef<const CXXRecordDecl*> Path) { |
| 704 | assert(isUninit() && "Bad state change"); |
Richard Smith | 7073a2d | 2014-01-10 00:40:45 +0000 | [diff] [blame] | 705 | MemberPointerData *MPD = new ((void*)(char*)Data.buffer) MemberPointerData; |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 706 | Kind = MemberPointer; |
| 707 | MPD->MemberAndIsDerivedMember.setPointer(Member); |
| 708 | MPD->MemberAndIsDerivedMember.setInt(IsDerivedMember); |
| 709 | MPD->resizePath(Path.size()); |
| 710 | memcpy(MPD->getPath(), Path.data(), Path.size()*sizeof(const CXXRecordDecl*)); |
| 711 | } |