1 //===--- APValue.h - Union class for APFloat/APSInt/Complex -----*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the APValue class.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_CLANG_AST_APVALUE_H
15 #define LLVM_CLANG_AST_APVALUE_H
17 #include "clang/Basic/LLVM.h"
18 #include "llvm/ADT/APFloat.h"
19 #include "llvm/ADT/APSInt.h"
20 #include "llvm/ADT/PointerIntPair.h"
21 #include "llvm/ADT/PointerUnion.h"
27 class DiagnosticBuilder;
35 /// APValue - This class implements a discriminated union of [uninitialized]
36 /// [APSInt] [APFloat], [Complex APSInt] [Complex APFloat], [Expr + Offset],
37 /// [Vector: N * APValue], [Array: N * APValue]
39 typedef llvm::APSInt APSInt;
40 typedef llvm::APFloat APFloat;
56 typedef llvm::PointerUnion<const ValueDecl *, const Expr *> LValueBase;
57 typedef llvm::PointerIntPair<const Decl *, 1, bool> BaseOrMemberType;
58 union LValuePathEntry {
59 /// BaseOrMember - The FieldDecl or CXXRecordDecl indicating the next item
60 /// in the path. An opaque value of type BaseOrMemberType.
62 /// ArrayIndex - The array index of the next item in the path.
65 struct NoLValuePath {};
66 struct UninitArray {};
67 struct UninitStruct {};
71 struct ComplexAPSInt {
73 ComplexAPSInt() : Real(1), Imag(1) {}
75 struct ComplexAPFloat {
77 ComplexAPFloat() : Real(0.0), Imag(0.0) {}
83 Vec() : Elts(nullptr), NumElts(0) {}
84 ~Vec() { delete[] Elts; }
88 unsigned NumElts, ArrSize;
89 Arr(unsigned NumElts, unsigned ArrSize);
96 StructData(unsigned NumBases, unsigned NumFields);
100 const FieldDecl *Field;
105 struct AddrLabelDiffData {
106 const AddrLabelExpr* LHSExpr;
107 const AddrLabelExpr* RHSExpr;
109 struct MemberPointerData;
111 // We ensure elsewhere that Data is big enough for LV and MemberPointerData.
112 typedef llvm::AlignedCharArrayUnion<void *, APSInt, APFloat, ComplexAPSInt,
113 ComplexAPFloat, Vec, Arr, StructData,
114 UnionData, AddrLabelDiffData> DataType;
115 static const size_t DataSize = sizeof(DataType);
120 APValue() : Kind(Uninitialized) {}
121 explicit APValue(APSInt I) : Kind(Uninitialized) {
122 MakeInt(); setInt(std::move(I));
124 explicit APValue(APFloat F) : Kind(Uninitialized) {
125 MakeFloat(); setFloat(std::move(F));
127 explicit APValue(const APValue *E, unsigned N) : Kind(Uninitialized) {
128 MakeVector(); setVector(E, N);
130 APValue(APSInt R, APSInt I) : Kind(Uninitialized) {
131 MakeComplexInt(); setComplexInt(std::move(R), std::move(I));
133 APValue(APFloat R, APFloat I) : Kind(Uninitialized) {
134 MakeComplexFloat(); setComplexFloat(std::move(R), std::move(I));
136 APValue(const APValue &RHS);
137 APValue(APValue &&RHS) : Kind(Uninitialized) { swap(RHS); }
138 APValue(LValueBase B, const CharUnits &O, NoLValuePath N, unsigned CallIndex,
139 bool IsNullPtr = false)
140 : Kind(Uninitialized) {
141 MakeLValue(); setLValue(B, O, N, CallIndex, IsNullPtr);
143 APValue(LValueBase B, const CharUnits &O, ArrayRef<LValuePathEntry> Path,
144 bool OnePastTheEnd, unsigned CallIndex, bool IsNullPtr = false)
145 : Kind(Uninitialized) {
146 MakeLValue(); setLValue(B, O, Path, OnePastTheEnd, CallIndex, IsNullPtr);
148 APValue(UninitArray, unsigned InitElts, unsigned Size) : Kind(Uninitialized) {
149 MakeArray(InitElts, Size);
151 APValue(UninitStruct, unsigned B, unsigned M) : Kind(Uninitialized) {
154 explicit APValue(const FieldDecl *D, const APValue &V = APValue())
155 : Kind(Uninitialized) {
156 MakeUnion(); setUnion(D, V);
158 APValue(const ValueDecl *Member, bool IsDerivedMember,
159 ArrayRef<const CXXRecordDecl*> Path) : Kind(Uninitialized) {
160 MakeMemberPointer(Member, IsDerivedMember, Path);
162 APValue(const AddrLabelExpr* LHSExpr, const AddrLabelExpr* RHSExpr)
163 : Kind(Uninitialized) {
164 MakeAddrLabelDiff(); setAddrLabelDiff(LHSExpr, RHSExpr);
171 /// \brief Returns whether the object performed allocations.
173 /// If APValues are constructed via placement new, \c needsCleanup()
174 /// indicates whether the destructor must be called in order to correctly
175 /// free all allocated memory.
176 bool needsCleanup() const;
178 /// \brief Swaps the contents of this and the given APValue.
179 void swap(APValue &RHS);
181 ValueKind getKind() const { return Kind; }
182 bool isUninit() const { return Kind == Uninitialized; }
183 bool isInt() const { return Kind == Int; }
184 bool isFloat() const { return Kind == Float; }
185 bool isComplexInt() const { return Kind == ComplexInt; }
186 bool isComplexFloat() const { return Kind == ComplexFloat; }
187 bool isLValue() const { return Kind == LValue; }
188 bool isVector() const { return Kind == Vector; }
189 bool isArray() const { return Kind == Array; }
190 bool isStruct() const { return Kind == Struct; }
191 bool isUnion() const { return Kind == Union; }
192 bool isMemberPointer() const { return Kind == MemberPointer; }
193 bool isAddrLabelDiff() const { return Kind == AddrLabelDiff; }
196 void dump(raw_ostream &OS) const;
198 void printPretty(raw_ostream &OS, ASTContext &Ctx, QualType Ty) const;
199 std::string getAsString(ASTContext &Ctx, QualType Ty) const;
202 assert(isInt() && "Invalid accessor");
203 return *(APSInt*)(char*)Data.buffer;
205 const APSInt &getInt() const {
206 return const_cast<APValue*>(this)->getInt();
209 APFloat &getFloat() {
210 assert(isFloat() && "Invalid accessor");
211 return *(APFloat*)(char*)Data.buffer;
213 const APFloat &getFloat() const {
214 return const_cast<APValue*>(this)->getFloat();
217 APSInt &getComplexIntReal() {
218 assert(isComplexInt() && "Invalid accessor");
219 return ((ComplexAPSInt*)(char*)Data.buffer)->Real;
221 const APSInt &getComplexIntReal() const {
222 return const_cast<APValue*>(this)->getComplexIntReal();
225 APSInt &getComplexIntImag() {
226 assert(isComplexInt() && "Invalid accessor");
227 return ((ComplexAPSInt*)(char*)Data.buffer)->Imag;
229 const APSInt &getComplexIntImag() const {
230 return const_cast<APValue*>(this)->getComplexIntImag();
233 APFloat &getComplexFloatReal() {
234 assert(isComplexFloat() && "Invalid accessor");
235 return ((ComplexAPFloat*)(char*)Data.buffer)->Real;
237 const APFloat &getComplexFloatReal() const {
238 return const_cast<APValue*>(this)->getComplexFloatReal();
241 APFloat &getComplexFloatImag() {
242 assert(isComplexFloat() && "Invalid accessor");
243 return ((ComplexAPFloat*)(char*)Data.buffer)->Imag;
245 const APFloat &getComplexFloatImag() const {
246 return const_cast<APValue*>(this)->getComplexFloatImag();
249 const LValueBase getLValueBase() const;
250 CharUnits &getLValueOffset();
251 const CharUnits &getLValueOffset() const {
252 return const_cast<APValue*>(this)->getLValueOffset();
254 bool isLValueOnePastTheEnd() const;
255 bool hasLValuePath() const;
256 ArrayRef<LValuePathEntry> getLValuePath() const;
257 unsigned getLValueCallIndex() const;
258 bool isNullPointer() const;
260 APValue &getVectorElt(unsigned I) {
261 assert(isVector() && "Invalid accessor");
262 assert(I < getVectorLength() && "Index out of range");
263 return ((Vec*)(char*)Data.buffer)->Elts[I];
265 const APValue &getVectorElt(unsigned I) const {
266 return const_cast<APValue*>(this)->getVectorElt(I);
268 unsigned getVectorLength() const {
269 assert(isVector() && "Invalid accessor");
270 return ((const Vec*)(const void *)Data.buffer)->NumElts;
273 APValue &getArrayInitializedElt(unsigned I) {
274 assert(isArray() && "Invalid accessor");
275 assert(I < getArrayInitializedElts() && "Index out of range");
276 return ((Arr*)(char*)Data.buffer)->Elts[I];
278 const APValue &getArrayInitializedElt(unsigned I) const {
279 return const_cast<APValue*>(this)->getArrayInitializedElt(I);
281 bool hasArrayFiller() const {
282 return getArrayInitializedElts() != getArraySize();
284 APValue &getArrayFiller() {
285 assert(isArray() && "Invalid accessor");
286 assert(hasArrayFiller() && "No array filler");
287 return ((Arr*)(char*)Data.buffer)->Elts[getArrayInitializedElts()];
289 const APValue &getArrayFiller() const {
290 return const_cast<APValue*>(this)->getArrayFiller();
292 unsigned getArrayInitializedElts() const {
293 assert(isArray() && "Invalid accessor");
294 return ((const Arr*)(const void *)Data.buffer)->NumElts;
296 unsigned getArraySize() const {
297 assert(isArray() && "Invalid accessor");
298 return ((const Arr*)(const void *)Data.buffer)->ArrSize;
301 unsigned getStructNumBases() const {
302 assert(isStruct() && "Invalid accessor");
303 return ((const StructData*)(const char*)Data.buffer)->NumBases;
305 unsigned getStructNumFields() const {
306 assert(isStruct() && "Invalid accessor");
307 return ((const StructData*)(const char*)Data.buffer)->NumFields;
309 APValue &getStructBase(unsigned i) {
310 assert(isStruct() && "Invalid accessor");
311 return ((StructData*)(char*)Data.buffer)->Elts[i];
313 APValue &getStructField(unsigned i) {
314 assert(isStruct() && "Invalid accessor");
315 return ((StructData*)(char*)Data.buffer)->Elts[getStructNumBases() + i];
317 const APValue &getStructBase(unsigned i) const {
318 return const_cast<APValue*>(this)->getStructBase(i);
320 const APValue &getStructField(unsigned i) const {
321 return const_cast<APValue*>(this)->getStructField(i);
324 const FieldDecl *getUnionField() const {
325 assert(isUnion() && "Invalid accessor");
326 return ((const UnionData*)(const char*)Data.buffer)->Field;
328 APValue &getUnionValue() {
329 assert(isUnion() && "Invalid accessor");
330 return *((UnionData*)(char*)Data.buffer)->Value;
332 const APValue &getUnionValue() const {
333 return const_cast<APValue*>(this)->getUnionValue();
336 const ValueDecl *getMemberPointerDecl() const;
337 bool isMemberPointerToDerivedMember() const;
338 ArrayRef<const CXXRecordDecl*> getMemberPointerPath() const;
340 const AddrLabelExpr* getAddrLabelDiffLHS() const {
341 assert(isAddrLabelDiff() && "Invalid accessor");
342 return ((const AddrLabelDiffData*)(const char*)Data.buffer)->LHSExpr;
344 const AddrLabelExpr* getAddrLabelDiffRHS() const {
345 assert(isAddrLabelDiff() && "Invalid accessor");
346 return ((const AddrLabelDiffData*)(const char*)Data.buffer)->RHSExpr;
349 void setInt(APSInt I) {
350 assert(isInt() && "Invalid accessor");
351 *(APSInt *)(char *)Data.buffer = std::move(I);
353 void setFloat(APFloat F) {
354 assert(isFloat() && "Invalid accessor");
355 *(APFloat *)(char *)Data.buffer = std::move(F);
357 void setVector(const APValue *E, unsigned N) {
358 assert(isVector() && "Invalid accessor");
359 ((Vec*)(char*)Data.buffer)->Elts = new APValue[N];
360 ((Vec*)(char*)Data.buffer)->NumElts = N;
361 for (unsigned i = 0; i != N; ++i)
362 ((Vec*)(char*)Data.buffer)->Elts[i] = E[i];
364 void setComplexInt(APSInt R, APSInt I) {
365 assert(R.getBitWidth() == I.getBitWidth() &&
366 "Invalid complex int (type mismatch).");
367 assert(isComplexInt() && "Invalid accessor");
368 ((ComplexAPSInt *)(char *)Data.buffer)->Real = std::move(R);
369 ((ComplexAPSInt *)(char *)Data.buffer)->Imag = std::move(I);
371 void setComplexFloat(APFloat R, APFloat I) {
372 assert(&R.getSemantics() == &I.getSemantics() &&
373 "Invalid complex float (type mismatch).");
374 assert(isComplexFloat() && "Invalid accessor");
375 ((ComplexAPFloat *)(char *)Data.buffer)->Real = std::move(R);
376 ((ComplexAPFloat *)(char *)Data.buffer)->Imag = std::move(I);
378 void setLValue(LValueBase B, const CharUnits &O, NoLValuePath,
379 unsigned CallIndex, bool IsNullPtr);
380 void setLValue(LValueBase B, const CharUnits &O,
381 ArrayRef<LValuePathEntry> Path, bool OnePastTheEnd,
382 unsigned CallIndex, bool IsNullPtr);
383 void setUnion(const FieldDecl *Field, const APValue &Value) {
384 assert(isUnion() && "Invalid accessor");
385 ((UnionData*)(char*)Data.buffer)->Field = Field;
386 *((UnionData*)(char*)Data.buffer)->Value = Value;
388 void setAddrLabelDiff(const AddrLabelExpr* LHSExpr,
389 const AddrLabelExpr* RHSExpr) {
390 ((AddrLabelDiffData*)(char*)Data.buffer)->LHSExpr = LHSExpr;
391 ((AddrLabelDiffData*)(char*)Data.buffer)->RHSExpr = RHSExpr;
394 /// Assign by swapping from a copy of the RHS.
395 APValue &operator=(APValue RHS) {
401 void DestroyDataAndMakeUninit();
403 if (Kind != Uninitialized)
404 DestroyDataAndMakeUninit();
407 assert(isUninit() && "Bad state change");
408 new ((void*)Data.buffer) APSInt(1);
412 assert(isUninit() && "Bad state change");
413 new ((void*)(char*)Data.buffer) APFloat(0.0);
417 assert(isUninit() && "Bad state change");
418 new ((void*)(char*)Data.buffer) Vec();
421 void MakeComplexInt() {
422 assert(isUninit() && "Bad state change");
423 new ((void*)(char*)Data.buffer) ComplexAPSInt();
426 void MakeComplexFloat() {
427 assert(isUninit() && "Bad state change");
428 new ((void*)(char*)Data.buffer) ComplexAPFloat();
432 void MakeArray(unsigned InitElts, unsigned Size);
433 void MakeStruct(unsigned B, unsigned M) {
434 assert(isUninit() && "Bad state change");
435 new ((void*)(char*)Data.buffer) StructData(B, M);
439 assert(isUninit() && "Bad state change");
440 new ((void*)(char*)Data.buffer) UnionData();
443 void MakeMemberPointer(const ValueDecl *Member, bool IsDerivedMember,
444 ArrayRef<const CXXRecordDecl*> Path);
445 void MakeAddrLabelDiff() {
446 assert(isUninit() && "Bad state change");
447 new ((void*)(char*)Data.buffer) AddrLabelDiffData();
448 Kind = AddrLabelDiff;
452 } // end namespace clang.