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 { |
| 24 | const Expr *Base; |
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; } |
| 43 | ~LV() { if (hasPathPtr()) delete [] PathPtr; } |
| 44 | |
| 45 | void allocPath() { |
| 46 | if (hasPathPtr()) PathPtr = new LValuePathEntry[PathLength]; |
| 47 | } |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame^] | 48 | void freePath() { if (hasPathPtr()) delete [] PathPtr; } |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 49 | |
| 50 | bool hasPath() const { return PathLength != (unsigned)-1; } |
| 51 | bool hasPathPtr() const { return hasPath() && PathLength > InlinePathSpace; } |
| 52 | |
| 53 | LValuePathEntry *getPath() { return hasPathPtr() ? PathPtr : Path; } |
Richard Smith | 38dce9b | 2011-11-07 07:31:09 +0000 | [diff] [blame] | 54 | const LValuePathEntry *getPath() const { |
| 55 | return hasPathPtr() ? PathPtr : Path; |
| 56 | } |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 57 | }; |
| 58 | |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 59 | // FIXME: Reduce the malloc traffic here. |
| 60 | |
| 61 | APValue::Arr::Arr(unsigned NumElts, unsigned Size) : |
| 62 | Elts(new APValue[NumElts + (NumElts != Size ? 1 : 0)]), |
| 63 | NumElts(NumElts), ArrSize(Size) {} |
| 64 | APValue::Arr::~Arr() { delete [] Elts; } |
| 65 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame^] | 66 | APValue::StructData::StructData(unsigned NumBases, unsigned NumFields) : |
| 67 | Elts(new APValue[NumBases+NumFields]), |
| 68 | NumBases(NumBases), NumFields(NumFields) {} |
| 69 | APValue::StructData::~StructData() { |
| 70 | delete [] Elts; |
| 71 | } |
| 72 | |
| 73 | APValue::UnionData::UnionData() : Field(0), Value(new APValue) {} |
| 74 | APValue::UnionData::~UnionData () { |
| 75 | delete Value; |
| 76 | } |
| 77 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 78 | APValue::APValue(const Expr* B) : Kind(Uninitialized) { |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 79 | MakeLValue(); |
| 80 | setLValue(B, CharUnits::Zero(), ArrayRef<LValuePathEntry>()); |
Ken Dyck | a730583 | 2010-01-15 12:37:54 +0000 | [diff] [blame] | 81 | } |
Chris Lattner | 64c34f1 | 2008-11-16 07:46:48 +0000 | [diff] [blame] | 82 | |
| 83 | const APValue &APValue::operator=(const APValue &RHS) { |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame^] | 84 | if (this == &RHS) |
| 85 | return *this; |
| 86 | if (Kind != RHS.Kind || Kind == Array || Kind == Struct) { |
Chris Lattner | 64c34f1 | 2008-11-16 07:46:48 +0000 | [diff] [blame] | 87 | MakeUninit(); |
| 88 | if (RHS.isInt()) |
| 89 | MakeInt(); |
| 90 | else if (RHS.isFloat()) |
| 91 | MakeFloat(); |
Nate Begeman | 3d309f9 | 2009-01-18 01:01:34 +0000 | [diff] [blame] | 92 | else if (RHS.isVector()) |
| 93 | MakeVector(); |
Chris Lattner | 64c34f1 | 2008-11-16 07:46:48 +0000 | [diff] [blame] | 94 | else if (RHS.isComplexInt()) |
| 95 | MakeComplexInt(); |
| 96 | else if (RHS.isComplexFloat()) |
| 97 | MakeComplexFloat(); |
| 98 | else if (RHS.isLValue()) |
| 99 | MakeLValue(); |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 100 | else if (RHS.isArray()) |
| 101 | MakeArray(RHS.getArrayInitializedElts(), RHS.getArraySize()); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame^] | 102 | else if (RHS.isStruct()) |
| 103 | MakeStruct(RHS.getStructNumBases(), RHS.getStructNumFields()); |
| 104 | else if (RHS.isUnion()) |
| 105 | MakeUnion(); |
Chris Lattner | 64c34f1 | 2008-11-16 07:46:48 +0000 | [diff] [blame] | 106 | } |
| 107 | if (isInt()) |
| 108 | setInt(RHS.getInt()); |
| 109 | else if (isFloat()) |
| 110 | setFloat(RHS.getFloat()); |
Nate Begeman | 3d309f9 | 2009-01-18 01:01:34 +0000 | [diff] [blame] | 111 | else if (isVector()) |
Dan Gohman | cb421fa | 2010-04-19 16:39:44 +0000 | [diff] [blame] | 112 | setVector(((const Vec *)(const char *)RHS.Data)->Elts, |
| 113 | RHS.getVectorLength()); |
Chris Lattner | 64c34f1 | 2008-11-16 07:46:48 +0000 | [diff] [blame] | 114 | else if (isComplexInt()) |
| 115 | setComplexInt(RHS.getComplexIntReal(), RHS.getComplexIntImag()); |
| 116 | else if (isComplexFloat()) |
| 117 | setComplexFloat(RHS.getComplexFloatReal(), RHS.getComplexFloatImag()); |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 118 | else if (isLValue()) { |
| 119 | if (RHS.hasLValuePath()) |
| 120 | setLValue(RHS.getLValueBase(), RHS.getLValueOffset(),RHS.getLValuePath()); |
| 121 | else |
| 122 | setLValue(RHS.getLValueBase(), RHS.getLValueOffset(), NoLValuePath()); |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 123 | } else if (isArray()) { |
| 124 | for (unsigned I = 0, N = RHS.getArrayInitializedElts(); I != N; ++I) |
| 125 | getArrayInitializedElt(I) = RHS.getArrayInitializedElt(I); |
| 126 | if (RHS.hasArrayFiller()) |
| 127 | getArrayFiller() = RHS.getArrayFiller(); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame^] | 128 | } else if (isStruct()) { |
| 129 | for (unsigned I = 0, N = RHS.getStructNumBases(); I != N; ++I) |
| 130 | getStructBase(I) = RHS.getStructBase(I); |
| 131 | for (unsigned I = 0, N = RHS.getStructNumFields(); I != N; ++I) |
| 132 | getStructField(I) = RHS.getStructField(I); |
| 133 | } else if (isUnion()) |
| 134 | setUnion(RHS.getUnionField(), RHS.getUnionValue()); |
Chris Lattner | 64c34f1 | 2008-11-16 07:46:48 +0000 | [diff] [blame] | 135 | return *this; |
| 136 | } |
| 137 | |
| 138 | void APValue::MakeUninit() { |
| 139 | if (Kind == Int) |
Douglas Gregor | 9830046 | 2009-09-08 19:57:33 +0000 | [diff] [blame] | 140 | ((APSInt*)(char*)Data)->~APSInt(); |
Chris Lattner | 64c34f1 | 2008-11-16 07:46:48 +0000 | [diff] [blame] | 141 | else if (Kind == Float) |
Douglas Gregor | 9830046 | 2009-09-08 19:57:33 +0000 | [diff] [blame] | 142 | ((APFloat*)(char*)Data)->~APFloat(); |
Nate Begeman | 3d309f9 | 2009-01-18 01:01:34 +0000 | [diff] [blame] | 143 | else if (Kind == Vector) |
Douglas Gregor | 9830046 | 2009-09-08 19:57:33 +0000 | [diff] [blame] | 144 | ((Vec*)(char*)Data)->~Vec(); |
Chris Lattner | 64c34f1 | 2008-11-16 07:46:48 +0000 | [diff] [blame] | 145 | else if (Kind == ComplexInt) |
Douglas Gregor | 9830046 | 2009-09-08 19:57:33 +0000 | [diff] [blame] | 146 | ((ComplexAPSInt*)(char*)Data)->~ComplexAPSInt(); |
Chris Lattner | 64c34f1 | 2008-11-16 07:46:48 +0000 | [diff] [blame] | 147 | else if (Kind == ComplexFloat) |
Douglas Gregor | 9830046 | 2009-09-08 19:57:33 +0000 | [diff] [blame] | 148 | ((ComplexAPFloat*)(char*)Data)->~ComplexAPFloat(); |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 149 | else if (Kind == LValue) |
Douglas Gregor | 9830046 | 2009-09-08 19:57:33 +0000 | [diff] [blame] | 150 | ((LV*)(char*)Data)->~LV(); |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 151 | else if (Kind == Array) |
| 152 | ((Arr*)(char*)Data)->~Arr(); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame^] | 153 | else if (Kind == Struct) |
| 154 | ((StructData*)(char*)Data)->~StructData(); |
| 155 | else if (Kind == Union) |
| 156 | ((UnionData*)(char*)Data)->~UnionData(); |
Nate Begeman | 3d309f9 | 2009-01-18 01:01:34 +0000 | [diff] [blame] | 157 | Kind = Uninitialized; |
Chris Lattner | 64c34f1 | 2008-11-16 07:46:48 +0000 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | void APValue::dump() const { |
| 161 | print(llvm::errs()); |
| 162 | llvm::errs() << '\n'; |
Chris Lattner | 64c34f1 | 2008-11-16 07:46:48 +0000 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | static double GetApproxValue(const llvm::APFloat &F) { |
| 166 | llvm::APFloat V = F; |
| 167 | bool ignored; |
| 168 | V.convert(llvm::APFloat::IEEEdouble, llvm::APFloat::rmNearestTiesToEven, |
| 169 | &ignored); |
| 170 | return V.convertToDouble(); |
| 171 | } |
| 172 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 173 | void APValue::print(raw_ostream &OS) const { |
Chris Lattner | 64c34f1 | 2008-11-16 07:46:48 +0000 | [diff] [blame] | 174 | switch (getKind()) { |
Chris Lattner | 64c34f1 | 2008-11-16 07:46:48 +0000 | [diff] [blame] | 175 | case Uninitialized: |
| 176 | OS << "Uninitialized"; |
| 177 | return; |
| 178 | case Int: |
| 179 | OS << "Int: " << getInt(); |
| 180 | return; |
| 181 | case Float: |
| 182 | OS << "Float: " << GetApproxValue(getFloat()); |
| 183 | return; |
Nate Begeman | 3d309f9 | 2009-01-18 01:01:34 +0000 | [diff] [blame] | 184 | case Vector: |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 185 | OS << "Vector: " << getVectorElt(0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 186 | for (unsigned i = 1; i != getVectorLength(); ++i) |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 187 | OS << ", " << getVectorElt(i); |
Nate Begeman | 3d309f9 | 2009-01-18 01:01:34 +0000 | [diff] [blame] | 188 | return; |
Chris Lattner | 64c34f1 | 2008-11-16 07:46:48 +0000 | [diff] [blame] | 189 | case ComplexInt: |
| 190 | OS << "ComplexInt: " << getComplexIntReal() << ", " << getComplexIntImag(); |
| 191 | return; |
| 192 | case ComplexFloat: |
| 193 | OS << "ComplexFloat: " << GetApproxValue(getComplexFloatReal()) |
| 194 | << ", " << GetApproxValue(getComplexFloatImag()); |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 195 | return; |
Chris Lattner | 64c34f1 | 2008-11-16 07:46:48 +0000 | [diff] [blame] | 196 | case LValue: |
| 197 | OS << "LValue: <todo>"; |
| 198 | return; |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 199 | case Array: |
| 200 | OS << "Array: "; |
| 201 | for (unsigned I = 0, N = getArrayInitializedElts(); I != N; ++I) { |
| 202 | OS << getArrayInitializedElt(I); |
| 203 | if (I != getArraySize() - 1) OS << ", "; |
| 204 | } |
| 205 | if (hasArrayFiller()) |
| 206 | OS << getArraySize() - getArrayInitializedElts() << " x " |
| 207 | << getArrayFiller(); |
| 208 | return; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame^] | 209 | case Struct: |
| 210 | OS << "Struct "; |
| 211 | if (unsigned N = getStructNumBases()) { |
| 212 | OS << " bases: " << getStructBase(0); |
| 213 | for (unsigned I = 1; I != N; ++I) |
| 214 | OS << ", " << getStructBase(I); |
| 215 | } |
| 216 | if (unsigned N = getStructNumFields()) { |
| 217 | OS << " fields: " << getStructField(0); |
| 218 | for (unsigned I = 1; I != N; ++I) |
| 219 | OS << ", " << getStructField(I); |
| 220 | } |
| 221 | return; |
| 222 | case Union: |
| 223 | OS << "Union: " << getUnionValue(); |
| 224 | return; |
Chris Lattner | 64c34f1 | 2008-11-16 07:46:48 +0000 | [diff] [blame] | 225 | } |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame^] | 226 | llvm_unreachable("Unknown APValue kind!"); |
Chris Lattner | 64c34f1 | 2008-11-16 07:46:48 +0000 | [diff] [blame] | 227 | } |
| 228 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 229 | static void WriteShortAPValueToStream(raw_ostream& Out, |
Jeffrey Yasskin | 5b106a8 | 2011-07-18 16:43:53 +0000 | [diff] [blame] | 230 | const APValue& V) { |
| 231 | switch (V.getKind()) { |
Jeffrey Yasskin | 5b106a8 | 2011-07-18 16:43:53 +0000 | [diff] [blame] | 232 | case APValue::Uninitialized: |
| 233 | Out << "Uninitialized"; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame^] | 234 | return; |
Jeffrey Yasskin | 5b106a8 | 2011-07-18 16:43:53 +0000 | [diff] [blame] | 235 | case APValue::Int: |
| 236 | Out << V.getInt(); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame^] | 237 | return; |
Jeffrey Yasskin | 5b106a8 | 2011-07-18 16:43:53 +0000 | [diff] [blame] | 238 | case APValue::Float: |
| 239 | Out << GetApproxValue(V.getFloat()); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame^] | 240 | return; |
Jeffrey Yasskin | 5b106a8 | 2011-07-18 16:43:53 +0000 | [diff] [blame] | 241 | case APValue::Vector: |
| 242 | Out << '['; |
| 243 | WriteShortAPValueToStream(Out, V.getVectorElt(0)); |
| 244 | for (unsigned i = 1; i != V.getVectorLength(); ++i) { |
| 245 | Out << ", "; |
| 246 | WriteShortAPValueToStream(Out, V.getVectorElt(i)); |
| 247 | } |
| 248 | Out << ']'; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame^] | 249 | return; |
Jeffrey Yasskin | 5b106a8 | 2011-07-18 16:43:53 +0000 | [diff] [blame] | 250 | case APValue::ComplexInt: |
| 251 | Out << V.getComplexIntReal() << "+" << V.getComplexIntImag() << "i"; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame^] | 252 | return; |
Jeffrey Yasskin | 5b106a8 | 2011-07-18 16:43:53 +0000 | [diff] [blame] | 253 | case APValue::ComplexFloat: |
| 254 | Out << GetApproxValue(V.getComplexFloatReal()) << "+" |
| 255 | << GetApproxValue(V.getComplexFloatImag()) << "i"; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame^] | 256 | return; |
Jeffrey Yasskin | 5b106a8 | 2011-07-18 16:43:53 +0000 | [diff] [blame] | 257 | case APValue::LValue: |
| 258 | Out << "LValue: <todo>"; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame^] | 259 | return; |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 260 | case APValue::Array: |
| 261 | Out << '{'; |
| 262 | if (unsigned N = V.getArrayInitializedElts()) { |
| 263 | Out << V.getArrayInitializedElt(0); |
| 264 | for (unsigned I = 1; I != N; ++I) |
| 265 | Out << ", " << V.getArrayInitializedElt(I); |
| 266 | } |
| 267 | Out << '}'; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame^] | 268 | return; |
| 269 | case APValue::Struct: |
| 270 | Out << '{'; |
| 271 | if (unsigned N = V.getStructNumBases()) { |
| 272 | Out << V.getStructBase(0); |
| 273 | for (unsigned I = 1; I != N; ++I) |
| 274 | Out << ", " << V.getStructBase(I); |
| 275 | if (V.getStructNumFields()) |
| 276 | Out << ", "; |
| 277 | } |
| 278 | if (unsigned N = V.getStructNumFields()) { |
| 279 | Out << V.getStructField(0); |
| 280 | for (unsigned I = 1; I != N; ++I) |
| 281 | Out << ", " << V.getStructField(I); |
| 282 | } |
| 283 | Out << '}'; |
| 284 | return; |
| 285 | case APValue::Union: |
| 286 | Out << '{' << V.getUnionValue() << '}'; |
| 287 | return; |
Jeffrey Yasskin | 5b106a8 | 2011-07-18 16:43:53 +0000 | [diff] [blame] | 288 | } |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame^] | 289 | llvm_unreachable("Unknown APValue kind!"); |
Jeffrey Yasskin | 5b106a8 | 2011-07-18 16:43:53 +0000 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | const DiagnosticBuilder &clang::operator<<(const DiagnosticBuilder &DB, |
| 293 | const APValue &V) { |
| 294 | llvm::SmallString<64> Buffer; |
| 295 | llvm::raw_svector_ostream Out(Buffer); |
| 296 | WriteShortAPValueToStream(Out, V); |
| 297 | return DB << Out.str(); |
| 298 | } |
| 299 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 300 | const Expr* APValue::getLValueBase() const { |
Ken Dyck | a730583 | 2010-01-15 12:37:54 +0000 | [diff] [blame] | 301 | assert(isLValue() && "Invalid accessor"); |
| 302 | return ((const LV*)(const void*)Data)->Base; |
| 303 | } |
| 304 | |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 305 | CharUnits &APValue::getLValueOffset() { |
| 306 | assert(isLValue() && "Invalid accessor"); |
| 307 | return ((LV*)(void*)Data)->Offset; |
Ken Dyck | a730583 | 2010-01-15 12:37:54 +0000 | [diff] [blame] | 308 | } |
| 309 | |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 310 | bool APValue::hasLValuePath() const { |
Ken Dyck | a730583 | 2010-01-15 12:37:54 +0000 | [diff] [blame] | 311 | assert(isLValue() && "Invalid accessor"); |
Richard Smith | 38dce9b | 2011-11-07 07:31:09 +0000 | [diff] [blame] | 312 | return ((const LV*)(const char*)Data)->hasPath(); |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | ArrayRef<APValue::LValuePathEntry> APValue::getLValuePath() const { |
| 316 | assert(isLValue() && hasLValuePath() && "Invalid accessor"); |
Richard Smith | 38dce9b | 2011-11-07 07:31:09 +0000 | [diff] [blame] | 317 | const LV &LVal = *((const LV*)(const char*)Data); |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 318 | return ArrayRef<LValuePathEntry>(LVal.getPath(), LVal.PathLength); |
| 319 | } |
| 320 | |
| 321 | void APValue::setLValue(const Expr *B, const CharUnits &O, NoLValuePath) { |
| 322 | assert(isLValue() && "Invalid accessor"); |
| 323 | LV &LVal = *((LV*)(char*)Data); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame^] | 324 | LVal.freePath(); |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 325 | LVal.Base = B; |
| 326 | LVal.Offset = O; |
| 327 | LVal.PathLength = (unsigned)-1; |
| 328 | } |
| 329 | |
| 330 | void APValue::setLValue(const Expr *B, const CharUnits &O, |
| 331 | ArrayRef<LValuePathEntry> Path) { |
| 332 | assert(isLValue() && "Invalid accessor"); |
| 333 | LV &LVal = *((LV*)(char*)Data); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame^] | 334 | LVal.freePath(); |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 335 | LVal.Base = B; |
| 336 | LVal.Offset = O; |
| 337 | LVal.PathLength = Path.size(); |
Richard Smith | 38dce9b | 2011-11-07 07:31:09 +0000 | [diff] [blame] | 338 | LVal.allocPath(); |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 339 | memcpy(LVal.getPath(), Path.data(), Path.size() * sizeof(LValuePathEntry)); |
Ken Dyck | a730583 | 2010-01-15 12:37:54 +0000 | [diff] [blame] | 340 | } |
| 341 | |
| 342 | void APValue::MakeLValue() { |
| 343 | assert(isUninit() && "Bad state change"); |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 344 | assert(sizeof(LV) <= MaxSize && "LV too big"); |
Ken Dyck | a730583 | 2010-01-15 12:37:54 +0000 | [diff] [blame] | 345 | new ((void*)(char*)Data) LV(); |
| 346 | Kind = LValue; |
| 347 | } |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 348 | |
| 349 | void APValue::MakeArray(unsigned InitElts, unsigned Size) { |
| 350 | assert(isUninit() && "Bad state change"); |
| 351 | new ((void*)(char*)Data) Arr(InitElts, Size); |
| 352 | Kind = Array; |
| 353 | } |