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" |
Ken Dyck | a730583 | 2010-01-15 12:37:54 +0000 | [diff] [blame] | 15 | #include "clang/AST/CharUnits.h" |
Jeffrey Yasskin | 5b106a8 | 2011-07-18 16:43:53 +0000 | [diff] [blame] | 16 | #include "clang/Basic/Diagnostic.h" |
| 17 | #include "llvm/ADT/SmallString.h" |
Chris Lattner | 64c34f1 | 2008-11-16 07:46:48 +0000 | [diff] [blame] | 18 | #include "llvm/Support/raw_ostream.h" |
David Blaikie | 9fe8c74 | 2011-09-23 05:35:21 +0000 | [diff] [blame] | 19 | #include "llvm/Support/ErrorHandling.h" |
Chris Lattner | 64c34f1 | 2008-11-16 07:46:48 +0000 | [diff] [blame] | 20 | using namespace clang; |
| 21 | |
Ken Dyck | a730583 | 2010-01-15 12:37:54 +0000 | [diff] [blame] | 22 | namespace { |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 23 | struct LVBase { |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame^] | 24 | llvm::PointerIntPair<APValue::LValueBase, 1, bool> BaseAndIsOnePastTheEnd; |
Ken Dyck | a730583 | 2010-01-15 12:37:54 +0000 | [diff] [blame] | 25 | CharUnits Offset; |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 26 | unsigned PathLength; |
Ken Dyck | a730583 | 2010-01-15 12:37:54 +0000 | [diff] [blame] | 27 | }; |
| 28 | } |
| 29 | |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 30 | struct APValue::LV : LVBase { |
| 31 | static const unsigned InlinePathSpace = |
| 32 | (MaxSize - sizeof(LVBase)) / sizeof(LValuePathEntry); |
| 33 | |
| 34 | /// Path - The sequence of base classes, fields and array indices to follow to |
| 35 | /// walk from Base to the subobject. When performing GCC-style folding, there |
| 36 | /// may not be such a path. |
| 37 | union { |
| 38 | LValuePathEntry Path[InlinePathSpace]; |
| 39 | LValuePathEntry *PathPtr; |
| 40 | }; |
| 41 | |
| 42 | LV() { PathLength = (unsigned)-1; } |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame^] | 43 | ~LV() { resizePath(0); } |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 44 | |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame^] | 45 | void resizePath(unsigned Length) { |
| 46 | if (Length == PathLength) |
| 47 | return; |
| 48 | if (hasPathPtr()) |
| 49 | delete [] PathPtr; |
| 50 | PathLength = Length; |
| 51 | if (hasPathPtr()) |
| 52 | PathPtr = new LValuePathEntry[Length]; |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | bool hasPath() const { return PathLength != (unsigned)-1; } |
| 56 | bool hasPathPtr() const { return hasPath() && PathLength > InlinePathSpace; } |
| 57 | |
| 58 | LValuePathEntry *getPath() { return hasPathPtr() ? PathPtr : Path; } |
Richard Smith | 38dce9b | 2011-11-07 07:31:09 +0000 | [diff] [blame] | 59 | const LValuePathEntry *getPath() const { |
| 60 | return hasPathPtr() ? PathPtr : Path; |
| 61 | } |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 62 | }; |
| 63 | |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame^] | 64 | namespace { |
| 65 | struct MemberPointerBase { |
| 66 | llvm::PointerIntPair<const ValueDecl*, 1, bool> MemberAndIsDerivedMember; |
| 67 | unsigned PathLength; |
| 68 | }; |
| 69 | } |
| 70 | |
| 71 | struct APValue::MemberPointerData : MemberPointerBase { |
| 72 | static const unsigned InlinePathSpace = |
| 73 | (MaxSize - sizeof(MemberPointerBase)) / sizeof(const CXXRecordDecl*); |
| 74 | typedef const CXXRecordDecl *PathElem; |
| 75 | union { |
| 76 | PathElem Path[InlinePathSpace]; |
| 77 | PathElem *PathPtr; |
| 78 | }; |
| 79 | |
| 80 | MemberPointerData() { PathLength = 0; } |
| 81 | ~MemberPointerData() { resizePath(0); } |
| 82 | |
| 83 | void resizePath(unsigned Length) { |
| 84 | if (Length == PathLength) |
| 85 | return; |
| 86 | if (hasPathPtr()) |
| 87 | delete [] PathPtr; |
| 88 | PathLength = Length; |
| 89 | if (hasPathPtr()) |
| 90 | PathPtr = new PathElem[Length]; |
| 91 | } |
| 92 | |
| 93 | bool hasPathPtr() const { return PathLength > InlinePathSpace; } |
| 94 | |
| 95 | PathElem *getPath() { return hasPathPtr() ? PathPtr : Path; } |
| 96 | const PathElem *getPath() const { |
| 97 | return hasPathPtr() ? PathPtr : Path; |
| 98 | } |
| 99 | }; |
| 100 | |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 101 | // FIXME: Reduce the malloc traffic here. |
| 102 | |
| 103 | APValue::Arr::Arr(unsigned NumElts, unsigned Size) : |
| 104 | Elts(new APValue[NumElts + (NumElts != Size ? 1 : 0)]), |
| 105 | NumElts(NumElts), ArrSize(Size) {} |
| 106 | APValue::Arr::~Arr() { delete [] Elts; } |
| 107 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 108 | APValue::StructData::StructData(unsigned NumBases, unsigned NumFields) : |
| 109 | Elts(new APValue[NumBases+NumFields]), |
| 110 | NumBases(NumBases), NumFields(NumFields) {} |
| 111 | APValue::StructData::~StructData() { |
| 112 | delete [] Elts; |
| 113 | } |
| 114 | |
| 115 | APValue::UnionData::UnionData() : Field(0), Value(new APValue) {} |
| 116 | APValue::UnionData::~UnionData () { |
| 117 | delete Value; |
| 118 | } |
| 119 | |
Chris Lattner | 64c34f1 | 2008-11-16 07:46:48 +0000 | [diff] [blame] | 120 | const APValue &APValue::operator=(const APValue &RHS) { |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 121 | if (this == &RHS) |
| 122 | return *this; |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame^] | 123 | if (Kind != RHS.Kind || Kind == Array || Kind == Struct || |
| 124 | Kind == MemberPointer) { |
Chris Lattner | 64c34f1 | 2008-11-16 07:46:48 +0000 | [diff] [blame] | 125 | MakeUninit(); |
| 126 | if (RHS.isInt()) |
| 127 | MakeInt(); |
| 128 | else if (RHS.isFloat()) |
| 129 | MakeFloat(); |
Nate Begeman | 3d309f9 | 2009-01-18 01:01:34 +0000 | [diff] [blame] | 130 | else if (RHS.isVector()) |
| 131 | MakeVector(); |
Chris Lattner | 64c34f1 | 2008-11-16 07:46:48 +0000 | [diff] [blame] | 132 | else if (RHS.isComplexInt()) |
| 133 | MakeComplexInt(); |
| 134 | else if (RHS.isComplexFloat()) |
| 135 | MakeComplexFloat(); |
| 136 | else if (RHS.isLValue()) |
| 137 | MakeLValue(); |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 138 | else if (RHS.isArray()) |
| 139 | MakeArray(RHS.getArrayInitializedElts(), RHS.getArraySize()); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 140 | else if (RHS.isStruct()) |
| 141 | MakeStruct(RHS.getStructNumBases(), RHS.getStructNumFields()); |
| 142 | else if (RHS.isUnion()) |
| 143 | MakeUnion(); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame^] | 144 | else if (RHS.isMemberPointer()) |
| 145 | MakeMemberPointer(RHS.getMemberPointerDecl(), |
| 146 | RHS.isMemberPointerToDerivedMember(), |
| 147 | RHS.getMemberPointerPath()); |
Chris Lattner | 64c34f1 | 2008-11-16 07:46:48 +0000 | [diff] [blame] | 148 | } |
| 149 | if (isInt()) |
| 150 | setInt(RHS.getInt()); |
| 151 | else if (isFloat()) |
| 152 | setFloat(RHS.getFloat()); |
Nate Begeman | 3d309f9 | 2009-01-18 01:01:34 +0000 | [diff] [blame] | 153 | else if (isVector()) |
Dan Gohman | cb421fa | 2010-04-19 16:39:44 +0000 | [diff] [blame] | 154 | setVector(((const Vec *)(const char *)RHS.Data)->Elts, |
| 155 | RHS.getVectorLength()); |
Chris Lattner | 64c34f1 | 2008-11-16 07:46:48 +0000 | [diff] [blame] | 156 | else if (isComplexInt()) |
| 157 | setComplexInt(RHS.getComplexIntReal(), RHS.getComplexIntImag()); |
| 158 | else if (isComplexFloat()) |
| 159 | setComplexFloat(RHS.getComplexFloatReal(), RHS.getComplexFloatImag()); |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 160 | else if (isLValue()) { |
| 161 | if (RHS.hasLValuePath()) |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame^] | 162 | setLValue(RHS.getLValueBase(), RHS.getLValueOffset(), RHS.getLValuePath(), |
| 163 | RHS.isLValueOnePastTheEnd()); |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 164 | else |
| 165 | setLValue(RHS.getLValueBase(), RHS.getLValueOffset(), NoLValuePath()); |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 166 | } else if (isArray()) { |
| 167 | for (unsigned I = 0, N = RHS.getArrayInitializedElts(); I != N; ++I) |
| 168 | getArrayInitializedElt(I) = RHS.getArrayInitializedElt(I); |
| 169 | if (RHS.hasArrayFiller()) |
| 170 | getArrayFiller() = RHS.getArrayFiller(); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 171 | } else if (isStruct()) { |
| 172 | for (unsigned I = 0, N = RHS.getStructNumBases(); I != N; ++I) |
| 173 | getStructBase(I) = RHS.getStructBase(I); |
| 174 | for (unsigned I = 0, N = RHS.getStructNumFields(); I != N; ++I) |
| 175 | getStructField(I) = RHS.getStructField(I); |
| 176 | } else if (isUnion()) |
| 177 | setUnion(RHS.getUnionField(), RHS.getUnionValue()); |
Chris Lattner | 64c34f1 | 2008-11-16 07:46:48 +0000 | [diff] [blame] | 178 | return *this; |
| 179 | } |
| 180 | |
| 181 | void APValue::MakeUninit() { |
| 182 | if (Kind == Int) |
Douglas Gregor | 9830046 | 2009-09-08 19:57:33 +0000 | [diff] [blame] | 183 | ((APSInt*)(char*)Data)->~APSInt(); |
Chris Lattner | 64c34f1 | 2008-11-16 07:46:48 +0000 | [diff] [blame] | 184 | else if (Kind == Float) |
Douglas Gregor | 9830046 | 2009-09-08 19:57:33 +0000 | [diff] [blame] | 185 | ((APFloat*)(char*)Data)->~APFloat(); |
Nate Begeman | 3d309f9 | 2009-01-18 01:01:34 +0000 | [diff] [blame] | 186 | else if (Kind == Vector) |
Douglas Gregor | 9830046 | 2009-09-08 19:57:33 +0000 | [diff] [blame] | 187 | ((Vec*)(char*)Data)->~Vec(); |
Chris Lattner | 64c34f1 | 2008-11-16 07:46:48 +0000 | [diff] [blame] | 188 | else if (Kind == ComplexInt) |
Douglas Gregor | 9830046 | 2009-09-08 19:57:33 +0000 | [diff] [blame] | 189 | ((ComplexAPSInt*)(char*)Data)->~ComplexAPSInt(); |
Chris Lattner | 64c34f1 | 2008-11-16 07:46:48 +0000 | [diff] [blame] | 190 | else if (Kind == ComplexFloat) |
Douglas Gregor | 9830046 | 2009-09-08 19:57:33 +0000 | [diff] [blame] | 191 | ((ComplexAPFloat*)(char*)Data)->~ComplexAPFloat(); |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 192 | else if (Kind == LValue) |
Douglas Gregor | 9830046 | 2009-09-08 19:57:33 +0000 | [diff] [blame] | 193 | ((LV*)(char*)Data)->~LV(); |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 194 | else if (Kind == Array) |
| 195 | ((Arr*)(char*)Data)->~Arr(); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 196 | else if (Kind == Struct) |
| 197 | ((StructData*)(char*)Data)->~StructData(); |
| 198 | else if (Kind == Union) |
| 199 | ((UnionData*)(char*)Data)->~UnionData(); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame^] | 200 | else if (Kind == MemberPointer) |
| 201 | ((MemberPointerData*)(char*)Data)->~MemberPointerData(); |
Nate Begeman | 3d309f9 | 2009-01-18 01:01:34 +0000 | [diff] [blame] | 202 | Kind = Uninitialized; |
Chris Lattner | 64c34f1 | 2008-11-16 07:46:48 +0000 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | void APValue::dump() const { |
| 206 | print(llvm::errs()); |
| 207 | llvm::errs() << '\n'; |
Chris Lattner | 64c34f1 | 2008-11-16 07:46:48 +0000 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | static double GetApproxValue(const llvm::APFloat &F) { |
| 211 | llvm::APFloat V = F; |
| 212 | bool ignored; |
| 213 | V.convert(llvm::APFloat::IEEEdouble, llvm::APFloat::rmNearestTiesToEven, |
| 214 | &ignored); |
| 215 | return V.convertToDouble(); |
| 216 | } |
| 217 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 218 | void APValue::print(raw_ostream &OS) const { |
Chris Lattner | 64c34f1 | 2008-11-16 07:46:48 +0000 | [diff] [blame] | 219 | switch (getKind()) { |
Chris Lattner | 64c34f1 | 2008-11-16 07:46:48 +0000 | [diff] [blame] | 220 | case Uninitialized: |
| 221 | OS << "Uninitialized"; |
| 222 | return; |
| 223 | case Int: |
| 224 | OS << "Int: " << getInt(); |
| 225 | return; |
| 226 | case Float: |
| 227 | OS << "Float: " << GetApproxValue(getFloat()); |
| 228 | return; |
Nate Begeman | 3d309f9 | 2009-01-18 01:01:34 +0000 | [diff] [blame] | 229 | case Vector: |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 230 | OS << "Vector: " << getVectorElt(0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 231 | for (unsigned i = 1; i != getVectorLength(); ++i) |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 232 | OS << ", " << getVectorElt(i); |
Nate Begeman | 3d309f9 | 2009-01-18 01:01:34 +0000 | [diff] [blame] | 233 | return; |
Chris Lattner | 64c34f1 | 2008-11-16 07:46:48 +0000 | [diff] [blame] | 234 | case ComplexInt: |
| 235 | OS << "ComplexInt: " << getComplexIntReal() << ", " << getComplexIntImag(); |
| 236 | return; |
| 237 | case ComplexFloat: |
| 238 | OS << "ComplexFloat: " << GetApproxValue(getComplexFloatReal()) |
| 239 | << ", " << GetApproxValue(getComplexFloatImag()); |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 240 | return; |
Chris Lattner | 64c34f1 | 2008-11-16 07:46:48 +0000 | [diff] [blame] | 241 | case LValue: |
| 242 | OS << "LValue: <todo>"; |
| 243 | return; |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 244 | case Array: |
| 245 | OS << "Array: "; |
| 246 | for (unsigned I = 0, N = getArrayInitializedElts(); I != N; ++I) { |
| 247 | OS << getArrayInitializedElt(I); |
| 248 | if (I != getArraySize() - 1) OS << ", "; |
| 249 | } |
| 250 | if (hasArrayFiller()) |
| 251 | OS << getArraySize() - getArrayInitializedElts() << " x " |
| 252 | << getArrayFiller(); |
| 253 | return; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 254 | case Struct: |
| 255 | OS << "Struct "; |
| 256 | if (unsigned N = getStructNumBases()) { |
| 257 | OS << " bases: " << getStructBase(0); |
| 258 | for (unsigned I = 1; I != N; ++I) |
| 259 | OS << ", " << getStructBase(I); |
| 260 | } |
| 261 | if (unsigned N = getStructNumFields()) { |
| 262 | OS << " fields: " << getStructField(0); |
| 263 | for (unsigned I = 1; I != N; ++I) |
| 264 | OS << ", " << getStructField(I); |
| 265 | } |
| 266 | return; |
| 267 | case Union: |
| 268 | OS << "Union: " << getUnionValue(); |
| 269 | return; |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame^] | 270 | case MemberPointer: |
| 271 | OS << "MemberPointer: <todo>"; |
| 272 | return; |
Chris Lattner | 64c34f1 | 2008-11-16 07:46:48 +0000 | [diff] [blame] | 273 | } |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 274 | llvm_unreachable("Unknown APValue kind!"); |
Chris Lattner | 64c34f1 | 2008-11-16 07:46:48 +0000 | [diff] [blame] | 275 | } |
| 276 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 277 | static void WriteShortAPValueToStream(raw_ostream& Out, |
Jeffrey Yasskin | 5b106a8 | 2011-07-18 16:43:53 +0000 | [diff] [blame] | 278 | const APValue& V) { |
| 279 | switch (V.getKind()) { |
Jeffrey Yasskin | 5b106a8 | 2011-07-18 16:43:53 +0000 | [diff] [blame] | 280 | case APValue::Uninitialized: |
| 281 | Out << "Uninitialized"; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 282 | return; |
Jeffrey Yasskin | 5b106a8 | 2011-07-18 16:43:53 +0000 | [diff] [blame] | 283 | case APValue::Int: |
| 284 | Out << V.getInt(); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 285 | return; |
Jeffrey Yasskin | 5b106a8 | 2011-07-18 16:43:53 +0000 | [diff] [blame] | 286 | case APValue::Float: |
| 287 | Out << GetApproxValue(V.getFloat()); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 288 | return; |
Jeffrey Yasskin | 5b106a8 | 2011-07-18 16:43:53 +0000 | [diff] [blame] | 289 | case APValue::Vector: |
| 290 | Out << '['; |
| 291 | WriteShortAPValueToStream(Out, V.getVectorElt(0)); |
| 292 | for (unsigned i = 1; i != V.getVectorLength(); ++i) { |
| 293 | Out << ", "; |
| 294 | WriteShortAPValueToStream(Out, V.getVectorElt(i)); |
| 295 | } |
| 296 | Out << ']'; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 297 | return; |
Jeffrey Yasskin | 5b106a8 | 2011-07-18 16:43:53 +0000 | [diff] [blame] | 298 | case APValue::ComplexInt: |
| 299 | Out << V.getComplexIntReal() << "+" << V.getComplexIntImag() << "i"; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 300 | return; |
Jeffrey Yasskin | 5b106a8 | 2011-07-18 16:43:53 +0000 | [diff] [blame] | 301 | case APValue::ComplexFloat: |
| 302 | Out << GetApproxValue(V.getComplexFloatReal()) << "+" |
| 303 | << GetApproxValue(V.getComplexFloatImag()) << "i"; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 304 | return; |
Jeffrey Yasskin | 5b106a8 | 2011-07-18 16:43:53 +0000 | [diff] [blame] | 305 | case APValue::LValue: |
| 306 | Out << "LValue: <todo>"; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 307 | return; |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 308 | case APValue::Array: |
| 309 | Out << '{'; |
| 310 | if (unsigned N = V.getArrayInitializedElts()) { |
| 311 | Out << V.getArrayInitializedElt(0); |
| 312 | for (unsigned I = 1; I != N; ++I) |
| 313 | Out << ", " << V.getArrayInitializedElt(I); |
| 314 | } |
| 315 | Out << '}'; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 316 | return; |
| 317 | case APValue::Struct: |
| 318 | Out << '{'; |
| 319 | if (unsigned N = V.getStructNumBases()) { |
| 320 | Out << V.getStructBase(0); |
| 321 | for (unsigned I = 1; I != N; ++I) |
| 322 | Out << ", " << V.getStructBase(I); |
| 323 | if (V.getStructNumFields()) |
| 324 | Out << ", "; |
| 325 | } |
| 326 | if (unsigned N = V.getStructNumFields()) { |
| 327 | Out << V.getStructField(0); |
| 328 | for (unsigned I = 1; I != N; ++I) |
| 329 | Out << ", " << V.getStructField(I); |
| 330 | } |
| 331 | Out << '}'; |
| 332 | return; |
| 333 | case APValue::Union: |
| 334 | Out << '{' << V.getUnionValue() << '}'; |
| 335 | return; |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame^] | 336 | case APValue::MemberPointer: |
| 337 | Out << "MemberPointer: <todo>"; |
| 338 | return; |
Jeffrey Yasskin | 5b106a8 | 2011-07-18 16:43:53 +0000 | [diff] [blame] | 339 | } |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 340 | llvm_unreachable("Unknown APValue kind!"); |
Jeffrey Yasskin | 5b106a8 | 2011-07-18 16:43:53 +0000 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | const DiagnosticBuilder &clang::operator<<(const DiagnosticBuilder &DB, |
| 344 | const APValue &V) { |
| 345 | llvm::SmallString<64> Buffer; |
| 346 | llvm::raw_svector_ostream Out(Buffer); |
| 347 | WriteShortAPValueToStream(Out, V); |
| 348 | return DB << Out.str(); |
| 349 | } |
| 350 | |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 351 | const APValue::LValueBase APValue::getLValueBase() const { |
Ken Dyck | a730583 | 2010-01-15 12:37:54 +0000 | [diff] [blame] | 352 | assert(isLValue() && "Invalid accessor"); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame^] | 353 | return ((const LV*)(const void*)Data)->BaseAndIsOnePastTheEnd.getPointer(); |
| 354 | } |
| 355 | |
| 356 | bool APValue::isLValueOnePastTheEnd() const { |
| 357 | assert(isLValue() && "Invalid accessor"); |
| 358 | return ((const LV*)(const void*)Data)->BaseAndIsOnePastTheEnd.getInt(); |
Ken Dyck | a730583 | 2010-01-15 12:37:54 +0000 | [diff] [blame] | 359 | } |
| 360 | |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 361 | CharUnits &APValue::getLValueOffset() { |
| 362 | assert(isLValue() && "Invalid accessor"); |
| 363 | return ((LV*)(void*)Data)->Offset; |
Ken Dyck | a730583 | 2010-01-15 12:37:54 +0000 | [diff] [blame] | 364 | } |
| 365 | |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 366 | bool APValue::hasLValuePath() const { |
Ken Dyck | a730583 | 2010-01-15 12:37:54 +0000 | [diff] [blame] | 367 | assert(isLValue() && "Invalid accessor"); |
Richard Smith | 38dce9b | 2011-11-07 07:31:09 +0000 | [diff] [blame] | 368 | return ((const LV*)(const char*)Data)->hasPath(); |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | ArrayRef<APValue::LValuePathEntry> APValue::getLValuePath() const { |
| 372 | assert(isLValue() && hasLValuePath() && "Invalid accessor"); |
Richard Smith | 38dce9b | 2011-11-07 07:31:09 +0000 | [diff] [blame] | 373 | const LV &LVal = *((const LV*)(const char*)Data); |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 374 | return ArrayRef<LValuePathEntry>(LVal.getPath(), LVal.PathLength); |
| 375 | } |
| 376 | |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 377 | void APValue::setLValue(LValueBase B, const CharUnits &O, NoLValuePath) { |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 378 | assert(isLValue() && "Invalid accessor"); |
| 379 | LV &LVal = *((LV*)(char*)Data); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame^] | 380 | LVal.BaseAndIsOnePastTheEnd.setPointer(B); |
| 381 | LVal.BaseAndIsOnePastTheEnd.setInt(false); |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 382 | LVal.Offset = O; |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame^] | 383 | LVal.resizePath((unsigned)-1); |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 384 | } |
| 385 | |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 386 | void APValue::setLValue(LValueBase B, const CharUnits &O, |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame^] | 387 | ArrayRef<LValuePathEntry> Path, bool IsOnePastTheEnd) { |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 388 | assert(isLValue() && "Invalid accessor"); |
| 389 | LV &LVal = *((LV*)(char*)Data); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame^] | 390 | LVal.BaseAndIsOnePastTheEnd.setPointer(B); |
| 391 | LVal.BaseAndIsOnePastTheEnd.setInt(IsOnePastTheEnd); |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 392 | LVal.Offset = O; |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame^] | 393 | LVal.resizePath(Path.size()); |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 394 | memcpy(LVal.getPath(), Path.data(), Path.size() * sizeof(LValuePathEntry)); |
Ken Dyck | a730583 | 2010-01-15 12:37:54 +0000 | [diff] [blame] | 395 | } |
| 396 | |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame^] | 397 | const ValueDecl *APValue::getMemberPointerDecl() const { |
| 398 | assert(isMemberPointer() && "Invalid accessor"); |
| 399 | const MemberPointerData &MPD = *((const MemberPointerData*)(const char*)Data); |
| 400 | return MPD.MemberAndIsDerivedMember.getPointer(); |
| 401 | } |
| 402 | |
| 403 | bool APValue::isMemberPointerToDerivedMember() const { |
| 404 | assert(isMemberPointer() && "Invalid accessor"); |
| 405 | const MemberPointerData &MPD = *((const MemberPointerData*)(const char*)Data); |
| 406 | return MPD.MemberAndIsDerivedMember.getInt(); |
| 407 | } |
| 408 | |
| 409 | ArrayRef<const CXXRecordDecl*> APValue::getMemberPointerPath() const { |
| 410 | assert(isMemberPointer() && "Invalid accessor"); |
| 411 | const MemberPointerData &MPD = *((const MemberPointerData*)(const char*)Data); |
| 412 | return ArrayRef<const CXXRecordDecl*>(MPD.getPath(), MPD.PathLength); |
| 413 | } |
| 414 | |
Ken Dyck | a730583 | 2010-01-15 12:37:54 +0000 | [diff] [blame] | 415 | void APValue::MakeLValue() { |
| 416 | assert(isUninit() && "Bad state change"); |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 417 | assert(sizeof(LV) <= MaxSize && "LV too big"); |
Ken Dyck | a730583 | 2010-01-15 12:37:54 +0000 | [diff] [blame] | 418 | new ((void*)(char*)Data) LV(); |
| 419 | Kind = LValue; |
| 420 | } |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 421 | |
| 422 | void APValue::MakeArray(unsigned InitElts, unsigned Size) { |
| 423 | assert(isUninit() && "Bad state change"); |
| 424 | new ((void*)(char*)Data) Arr(InitElts, Size); |
| 425 | Kind = Array; |
| 426 | } |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame^] | 427 | |
| 428 | void APValue::MakeMemberPointer(const ValueDecl *Member, bool IsDerivedMember, |
| 429 | ArrayRef<const CXXRecordDecl*> Path) { |
| 430 | assert(isUninit() && "Bad state change"); |
| 431 | MemberPointerData *MPD = new ((void*)(char*)Data) MemberPointerData; |
| 432 | Kind = MemberPointer; |
| 433 | MPD->MemberAndIsDerivedMember.setPointer(Member); |
| 434 | MPD->MemberAndIsDerivedMember.setInt(IsDerivedMember); |
| 435 | MPD->resizePath(Path.size()); |
| 436 | memcpy(MPD->getPath(), Path.data(), Path.size()*sizeof(const CXXRecordDecl*)); |
| 437 | } |