xref: /llvm-project/clang/lib/AST/APValue.cpp (revision b228a86fcfd165b06919e43d28b8f1d0713c9802)
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"
15 #include "clang/AST/ASTContext.h"
16 #include "clang/AST/CharUnits.h"
17 #include "clang/AST/DeclCXX.h"
18 #include "clang/AST/Expr.h"
19 #include "clang/AST/Type.h"
20 #include "clang/Basic/Diagnostic.h"
21 #include "llvm/ADT/SmallString.h"
22 #include "llvm/Support/raw_ostream.h"
23 #include "llvm/Support/ErrorHandling.h"
24 using namespace clang;
25 
26 namespace {
27   struct LVBase {
28     llvm::PointerIntPair<APValue::LValueBase, 1, bool> BaseAndIsOnePastTheEnd;
29     CharUnits Offset;
30     unsigned PathLength;
31     unsigned CallIndex;
32   };
33 }
34 
35 struct APValue::LV : LVBase {
36   static const unsigned InlinePathSpace =
37       (MaxSize - sizeof(LVBase)) / sizeof(LValuePathEntry);
38 
39   /// Path - The sequence of base classes, fields and array indices to follow to
40   /// walk from Base to the subobject. When performing GCC-style folding, there
41   /// may not be such a path.
42   union {
43     LValuePathEntry Path[InlinePathSpace];
44     LValuePathEntry *PathPtr;
45   };
46 
47   LV() { PathLength = (unsigned)-1; }
48   ~LV() { resizePath(0); }
49 
50   void resizePath(unsigned Length) {
51     if (Length == PathLength)
52       return;
53     if (hasPathPtr())
54       delete [] PathPtr;
55     PathLength = Length;
56     if (hasPathPtr())
57       PathPtr = new LValuePathEntry[Length];
58   }
59 
60   bool hasPath() const { return PathLength != (unsigned)-1; }
61   bool hasPathPtr() const { return hasPath() && PathLength > InlinePathSpace; }
62 
63   LValuePathEntry *getPath() { return hasPathPtr() ? PathPtr : Path; }
64   const LValuePathEntry *getPath() const {
65     return hasPathPtr() ? PathPtr : Path;
66   }
67 };
68 
69 namespace {
70   struct MemberPointerBase {
71     llvm::PointerIntPair<const ValueDecl*, 1, bool> MemberAndIsDerivedMember;
72     unsigned PathLength;
73   };
74 }
75 
76 struct APValue::MemberPointerData : MemberPointerBase {
77   static const unsigned InlinePathSpace =
78       (MaxSize - sizeof(MemberPointerBase)) / sizeof(const CXXRecordDecl*);
79   typedef const CXXRecordDecl *PathElem;
80   union {
81     PathElem Path[InlinePathSpace];
82     PathElem *PathPtr;
83   };
84 
85   MemberPointerData() { PathLength = 0; }
86   ~MemberPointerData() { resizePath(0); }
87 
88   void resizePath(unsigned Length) {
89     if (Length == PathLength)
90       return;
91     if (hasPathPtr())
92       delete [] PathPtr;
93     PathLength = Length;
94     if (hasPathPtr())
95       PathPtr = new PathElem[Length];
96   }
97 
98   bool hasPathPtr() const { return PathLength > InlinePathSpace; }
99 
100   PathElem *getPath() { return hasPathPtr() ? PathPtr : Path; }
101   const PathElem *getPath() const {
102     return hasPathPtr() ? PathPtr : Path;
103   }
104 };
105 
106 // FIXME: Reduce the malloc traffic here.
107 
108 APValue::Arr::Arr(unsigned NumElts, unsigned Size) :
109   Elts(new APValue[NumElts + (NumElts != Size ? 1 : 0)]),
110   NumElts(NumElts), ArrSize(Size) {}
111 APValue::Arr::~Arr() { delete [] Elts; }
112 
113 APValue::StructData::StructData(unsigned NumBases, unsigned NumFields) :
114   Elts(new APValue[NumBases+NumFields]),
115   NumBases(NumBases), NumFields(NumFields) {}
116 APValue::StructData::~StructData() {
117   delete [] Elts;
118 }
119 
120 APValue::UnionData::UnionData() : Field(0), Value(new APValue) {}
121 APValue::UnionData::~UnionData () {
122   delete Value;
123 }
124 
125 const APValue &APValue::operator=(const APValue &RHS) {
126   if (this == &RHS)
127     return *this;
128   if (Kind != RHS.Kind || Kind == Array || Kind == Struct ||
129       Kind == MemberPointer) {
130     MakeUninit();
131     if (RHS.isInt())
132       MakeInt();
133     else if (RHS.isFloat())
134       MakeFloat();
135     else if (RHS.isVector())
136       MakeVector();
137     else if (RHS.isComplexInt())
138       MakeComplexInt();
139     else if (RHS.isComplexFloat())
140       MakeComplexFloat();
141     else if (RHS.isLValue())
142       MakeLValue();
143     else if (RHS.isArray())
144       MakeArray(RHS.getArrayInitializedElts(), RHS.getArraySize());
145     else if (RHS.isStruct())
146       MakeStruct(RHS.getStructNumBases(), RHS.getStructNumFields());
147     else if (RHS.isUnion())
148       MakeUnion();
149     else if (RHS.isMemberPointer())
150       MakeMemberPointer(RHS.getMemberPointerDecl(),
151                         RHS.isMemberPointerToDerivedMember(),
152                         RHS.getMemberPointerPath());
153     else if (RHS.isAddrLabelDiff())
154       MakeAddrLabelDiff();
155   }
156   if (isInt())
157     setInt(RHS.getInt());
158   else if (isFloat())
159     setFloat(RHS.getFloat());
160   else if (isVector())
161     setVector(((const Vec *)(const char *)RHS.Data)->Elts,
162               RHS.getVectorLength());
163   else if (isComplexInt())
164     setComplexInt(RHS.getComplexIntReal(), RHS.getComplexIntImag());
165   else if (isComplexFloat())
166     setComplexFloat(RHS.getComplexFloatReal(), RHS.getComplexFloatImag());
167   else if (isLValue()) {
168     if (RHS.hasLValuePath())
169       setLValue(RHS.getLValueBase(), RHS.getLValueOffset(), RHS.getLValuePath(),
170                 RHS.isLValueOnePastTheEnd(), RHS.getLValueCallIndex());
171     else
172       setLValue(RHS.getLValueBase(), RHS.getLValueOffset(), NoLValuePath(),
173                 RHS.getLValueCallIndex());
174   } else if (isArray()) {
175     for (unsigned I = 0, N = RHS.getArrayInitializedElts(); I != N; ++I)
176       getArrayInitializedElt(I) = RHS.getArrayInitializedElt(I);
177     if (RHS.hasArrayFiller())
178       getArrayFiller() = RHS.getArrayFiller();
179   } else if (isStruct()) {
180     for (unsigned I = 0, N = RHS.getStructNumBases(); I != N; ++I)
181       getStructBase(I) = RHS.getStructBase(I);
182     for (unsigned I = 0, N = RHS.getStructNumFields(); I != N; ++I)
183       getStructField(I) = RHS.getStructField(I);
184   } else if (isUnion()) {
185     setUnion(RHS.getUnionField(), RHS.getUnionValue());
186   } else if (isAddrLabelDiff()) {
187     setAddrLabelDiff(RHS.getAddrLabelDiffLHS(), RHS.getAddrLabelDiffRHS());
188   }
189   return *this;
190 }
191 
192 void APValue::MakeUninit() {
193   if (Kind == Int)
194     ((APSInt*)(char*)Data)->~APSInt();
195   else if (Kind == Float)
196     ((APFloat*)(char*)Data)->~APFloat();
197   else if (Kind == Vector)
198     ((Vec*)(char*)Data)->~Vec();
199   else if (Kind == ComplexInt)
200     ((ComplexAPSInt*)(char*)Data)->~ComplexAPSInt();
201   else if (Kind == ComplexFloat)
202     ((ComplexAPFloat*)(char*)Data)->~ComplexAPFloat();
203   else if (Kind == LValue)
204     ((LV*)(char*)Data)->~LV();
205   else if (Kind == Array)
206     ((Arr*)(char*)Data)->~Arr();
207   else if (Kind == Struct)
208     ((StructData*)(char*)Data)->~StructData();
209   else if (Kind == Union)
210     ((UnionData*)(char*)Data)->~UnionData();
211   else if (Kind == MemberPointer)
212     ((MemberPointerData*)(char*)Data)->~MemberPointerData();
213   else if (Kind == AddrLabelDiff)
214     ((AddrLabelDiffData*)(char*)Data)->~AddrLabelDiffData();
215   Kind = Uninitialized;
216 }
217 
218 void APValue::dump() const {
219   dump(llvm::errs());
220   llvm::errs() << '\n';
221 }
222 
223 static double GetApproxValue(const llvm::APFloat &F) {
224   llvm::APFloat V = F;
225   bool ignored;
226   V.convert(llvm::APFloat::IEEEdouble, llvm::APFloat::rmNearestTiesToEven,
227             &ignored);
228   return V.convertToDouble();
229 }
230 
231 void APValue::dump(raw_ostream &OS) const {
232   switch (getKind()) {
233   case Uninitialized:
234     OS << "Uninitialized";
235     return;
236   case Int:
237     OS << "Int: " << getInt();
238     return;
239   case Float:
240     OS << "Float: " << GetApproxValue(getFloat());
241     return;
242   case Vector:
243     OS << "Vector: ";
244     getVectorElt(0).dump(OS);
245     for (unsigned i = 1; i != getVectorLength(); ++i) {
246       OS << ", ";
247       getVectorElt(i).dump(OS);
248     }
249     return;
250   case ComplexInt:
251     OS << "ComplexInt: " << getComplexIntReal() << ", " << getComplexIntImag();
252     return;
253   case ComplexFloat:
254     OS << "ComplexFloat: " << GetApproxValue(getComplexFloatReal())
255        << ", " << GetApproxValue(getComplexFloatImag());
256     return;
257   case LValue:
258     OS << "LValue: <todo>";
259     return;
260   case Array:
261     OS << "Array: ";
262     for (unsigned I = 0, N = getArrayInitializedElts(); I != N; ++I) {
263       getArrayInitializedElt(I).dump(OS);
264       if (I != getArraySize() - 1) OS << ", ";
265     }
266     if (hasArrayFiller()) {
267       OS << getArraySize() - getArrayInitializedElts() << " x ";
268       getArrayFiller().dump(OS);
269     }
270     return;
271   case Struct:
272     OS << "Struct ";
273     if (unsigned N = getStructNumBases()) {
274       OS << " bases: ";
275       getStructBase(0).dump(OS);
276       for (unsigned I = 1; I != N; ++I) {
277         OS << ", ";
278         getStructBase(I).dump(OS);
279       }
280     }
281     if (unsigned N = getStructNumFields()) {
282       OS << " fields: ";
283       getStructField(0).dump(OS);
284       for (unsigned I = 1; I != N; ++I) {
285         OS << ", ";
286         getStructField(I).dump(OS);
287       }
288     }
289     return;
290   case Union:
291     OS << "Union: ";
292     getUnionValue().dump(OS);
293     return;
294   case MemberPointer:
295     OS << "MemberPointer: <todo>";
296     return;
297   case AddrLabelDiff:
298     OS << "AddrLabelDiff: <todo>";
299     return;
300   }
301   llvm_unreachable("Unknown APValue kind!");
302 }
303 
304 void APValue::printPretty(raw_ostream &Out, ASTContext &Ctx, QualType Ty) const{
305   switch (getKind()) {
306   case APValue::Uninitialized:
307     Out << "<uninitialized>";
308     return;
309   case APValue::Int:
310     Out << getInt();
311     return;
312   case APValue::Float:
313     Out << GetApproxValue(getFloat());
314     return;
315   case APValue::Vector: {
316     Out << '{';
317     QualType ElemTy = Ty->getAs<VectorType>()->getElementType();
318     getVectorElt(0).printPretty(Out, Ctx, ElemTy);
319     for (unsigned i = 1; i != getVectorLength(); ++i) {
320       Out << ", ";
321       getVectorElt(i).printPretty(Out, Ctx, ElemTy);
322     }
323     Out << '}';
324     return;
325   }
326   case APValue::ComplexInt:
327     Out << getComplexIntReal() << "+" << getComplexIntImag() << "i";
328     return;
329   case APValue::ComplexFloat:
330     Out << GetApproxValue(getComplexFloatReal()) << "+"
331         << GetApproxValue(getComplexFloatImag()) << "i";
332     return;
333   case APValue::LValue: {
334     LValueBase Base = getLValueBase();
335     if (!Base) {
336       Out << "0";
337       return;
338     }
339 
340     bool IsReference = Ty->isReferenceType();
341     QualType InnerTy
342       = IsReference ? Ty.getNonReferenceType() : Ty->getPointeeType();
343 
344     if (!hasLValuePath()) {
345       // No lvalue path: just print the offset.
346       CharUnits O = getLValueOffset();
347       CharUnits S = Ctx.getTypeSizeInChars(InnerTy);
348       if (!O.isZero()) {
349         if (IsReference)
350           Out << "*(";
351         if (O % S) {
352           Out << "(char*)";
353           S = CharUnits::One();
354         }
355         Out << '&';
356       } else if (!IsReference)
357         Out << '&';
358 
359       if (const ValueDecl *VD = Base.dyn_cast<const ValueDecl*>())
360         Out << *VD;
361       else
362         Base.get<const Expr*>()->printPretty(Out, Ctx, 0,
363                                              Ctx.getPrintingPolicy());
364       if (!O.isZero()) {
365         Out << " + " << (O / S);
366         if (IsReference)
367           Out << ')';
368       }
369       return;
370     }
371 
372     // We have an lvalue path. Print it out nicely.
373     if (!IsReference)
374       Out << '&';
375     else if (isLValueOnePastTheEnd())
376       Out << "*(&";
377 
378     QualType ElemTy;
379     if (const ValueDecl *VD = Base.dyn_cast<const ValueDecl*>()) {
380       Out << *VD;
381       ElemTy = VD->getType();
382     } else {
383       const Expr *E = Base.get<const Expr*>();
384       E->printPretty(Out, Ctx, 0,Ctx.getPrintingPolicy());
385       ElemTy = E->getType();
386     }
387 
388     ArrayRef<LValuePathEntry> Path = getLValuePath();
389     const CXXRecordDecl *CastToBase = 0;
390     for (unsigned I = 0, N = Path.size(); I != N; ++I) {
391       if (ElemTy->getAs<RecordType>()) {
392         // The lvalue refers to a class type, so the next path entry is a base
393         // or member.
394         const Decl *BaseOrMember =
395         BaseOrMemberType::getFromOpaqueValue(Path[I].BaseOrMember).getPointer();
396         if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(BaseOrMember)) {
397           CastToBase = RD;
398           ElemTy = Ctx.getRecordType(RD);
399         } else {
400           const ValueDecl *VD = cast<ValueDecl>(BaseOrMember);
401           Out << ".";
402           if (CastToBase)
403             Out << *CastToBase << "::";
404           Out << *VD;
405           ElemTy = VD->getType();
406         }
407       } else {
408         // The lvalue must refer to an array.
409         Out << '[' << Path[I].ArrayIndex << ']';
410         ElemTy = Ctx.getAsArrayType(ElemTy)->getElementType();
411       }
412     }
413 
414     // Handle formatting of one-past-the-end lvalues.
415     if (isLValueOnePastTheEnd()) {
416       // FIXME: If CastToBase is non-0, we should prefix the output with
417       // "(CastToBase*)".
418       Out << " + 1";
419       if (IsReference)
420         Out << ')';
421     }
422     return;
423   }
424   case APValue::Array: {
425     const ArrayType *AT = Ctx.getAsArrayType(Ty);
426     QualType ElemTy = AT->getElementType();
427     Out << '{';
428     if (unsigned N = getArrayInitializedElts()) {
429       getArrayInitializedElt(0).printPretty(Out, Ctx, ElemTy);
430       for (unsigned I = 1; I != N; ++I) {
431         Out << ", ";
432         if (I == 10) {
433           // Avoid printing out the entire contents of large arrays.
434           Out << "...";
435           break;
436         }
437         getArrayInitializedElt(I).printPretty(Out, Ctx, ElemTy);
438       }
439     }
440     Out << '}';
441     return;
442   }
443   case APValue::Struct: {
444     Out << '{';
445     const RecordDecl *RD = Ty->getAs<RecordType>()->getDecl();
446     bool First = true;
447     if (unsigned N = getStructNumBases()) {
448       const CXXRecordDecl *CD = cast<CXXRecordDecl>(RD);
449       CXXRecordDecl::base_class_const_iterator BI = CD->bases_begin();
450       for (unsigned I = 0; I != N; ++I, ++BI) {
451         assert(BI != CD->bases_end());
452         if (!First)
453           Out << ", ";
454         getStructBase(I).printPretty(Out, Ctx, BI->getType());
455         First = false;
456       }
457     }
458     for (RecordDecl::field_iterator FI = RD->field_begin();
459          FI != RD->field_end(); ++FI) {
460       if (!First)
461         Out << ", ";
462       if ((*FI)->isUnnamedBitfield()) continue;
463       getStructField((*FI)->getFieldIndex()).
464         printPretty(Out, Ctx, (*FI)->getType());
465       First = false;
466     }
467     Out << '}';
468     return;
469   }
470   case APValue::Union:
471     Out << '{';
472     if (const FieldDecl *FD = getUnionField()) {
473       Out << "." << *FD << " = ";
474       getUnionValue().printPretty(Out, Ctx, FD->getType());
475     }
476     Out << '}';
477     return;
478   case APValue::MemberPointer:
479     // FIXME: This is not enough to unambiguously identify the member in a
480     // multiple-inheritance scenario.
481     if (const ValueDecl *VD = getMemberPointerDecl()) {
482       Out << '&' << *cast<CXXRecordDecl>(VD->getDeclContext()) << "::" << *VD;
483       return;
484     }
485     Out << "0";
486     return;
487   case APValue::AddrLabelDiff:
488     Out << "&&" << getAddrLabelDiffLHS()->getLabel()->getName();
489     Out << " - ";
490     Out << "&&" << getAddrLabelDiffRHS()->getLabel()->getName();
491     return;
492   }
493   llvm_unreachable("Unknown APValue kind!");
494 }
495 
496 std::string APValue::getAsString(ASTContext &Ctx, QualType Ty) const {
497   std::string Result;
498   llvm::raw_string_ostream Out(Result);
499   printPretty(Out, Ctx, Ty);
500   Out.flush();
501   return Result;
502 }
503 
504 const APValue::LValueBase APValue::getLValueBase() const {
505   assert(isLValue() && "Invalid accessor");
506   return ((const LV*)(const void*)Data)->BaseAndIsOnePastTheEnd.getPointer();
507 }
508 
509 bool APValue::isLValueOnePastTheEnd() const {
510   assert(isLValue() && "Invalid accessor");
511   return ((const LV*)(const void*)Data)->BaseAndIsOnePastTheEnd.getInt();
512 }
513 
514 CharUnits &APValue::getLValueOffset() {
515   assert(isLValue() && "Invalid accessor");
516   return ((LV*)(void*)Data)->Offset;
517 }
518 
519 bool APValue::hasLValuePath() const {
520   assert(isLValue() && "Invalid accessor");
521   return ((const LV*)(const char*)Data)->hasPath();
522 }
523 
524 ArrayRef<APValue::LValuePathEntry> APValue::getLValuePath() const {
525   assert(isLValue() && hasLValuePath() && "Invalid accessor");
526   const LV &LVal = *((const LV*)(const char*)Data);
527   return ArrayRef<LValuePathEntry>(LVal.getPath(), LVal.PathLength);
528 }
529 
530 unsigned APValue::getLValueCallIndex() const {
531   assert(isLValue() && "Invalid accessor");
532   return ((const LV*)(const char*)Data)->CallIndex;
533 }
534 
535 void APValue::setLValue(LValueBase B, const CharUnits &O, NoLValuePath,
536                         unsigned CallIndex) {
537   assert(isLValue() && "Invalid accessor");
538   LV &LVal = *((LV*)(char*)Data);
539   LVal.BaseAndIsOnePastTheEnd.setPointer(B);
540   LVal.BaseAndIsOnePastTheEnd.setInt(false);
541   LVal.Offset = O;
542   LVal.CallIndex = CallIndex;
543   LVal.resizePath((unsigned)-1);
544 }
545 
546 void APValue::setLValue(LValueBase B, const CharUnits &O,
547                         ArrayRef<LValuePathEntry> Path, bool IsOnePastTheEnd,
548                         unsigned CallIndex) {
549   assert(isLValue() && "Invalid accessor");
550   LV &LVal = *((LV*)(char*)Data);
551   LVal.BaseAndIsOnePastTheEnd.setPointer(B);
552   LVal.BaseAndIsOnePastTheEnd.setInt(IsOnePastTheEnd);
553   LVal.Offset = O;
554   LVal.CallIndex = CallIndex;
555   LVal.resizePath(Path.size());
556   memcpy(LVal.getPath(), Path.data(), Path.size() * sizeof(LValuePathEntry));
557 }
558 
559 const ValueDecl *APValue::getMemberPointerDecl() const {
560   assert(isMemberPointer() && "Invalid accessor");
561   const MemberPointerData &MPD = *((const MemberPointerData*)(const char*)Data);
562   return MPD.MemberAndIsDerivedMember.getPointer();
563 }
564 
565 bool APValue::isMemberPointerToDerivedMember() const {
566   assert(isMemberPointer() && "Invalid accessor");
567   const MemberPointerData &MPD = *((const MemberPointerData*)(const char*)Data);
568   return MPD.MemberAndIsDerivedMember.getInt();
569 }
570 
571 ArrayRef<const CXXRecordDecl*> APValue::getMemberPointerPath() const {
572   assert(isMemberPointer() && "Invalid accessor");
573   const MemberPointerData &MPD = *((const MemberPointerData*)(const char*)Data);
574   return ArrayRef<const CXXRecordDecl*>(MPD.getPath(), MPD.PathLength);
575 }
576 
577 void APValue::MakeLValue() {
578   assert(isUninit() && "Bad state change");
579   assert(sizeof(LV) <= MaxSize && "LV too big");
580   new ((void*)(char*)Data) LV();
581   Kind = LValue;
582 }
583 
584 void APValue::MakeArray(unsigned InitElts, unsigned Size) {
585   assert(isUninit() && "Bad state change");
586   new ((void*)(char*)Data) Arr(InitElts, Size);
587   Kind = Array;
588 }
589 
590 void APValue::MakeMemberPointer(const ValueDecl *Member, bool IsDerivedMember,
591                                 ArrayRef<const CXXRecordDecl*> Path) {
592   assert(isUninit() && "Bad state change");
593   MemberPointerData *MPD = new ((void*)(char*)Data) MemberPointerData;
594   Kind = MemberPointer;
595   MPD->MemberAndIsDerivedMember.setPointer(Member);
596   MPD->MemberAndIsDerivedMember.setInt(IsDerivedMember);
597   MPD->resizePath(Path.size());
598   memcpy(MPD->getPath(), Path.data(), Path.size()*sizeof(const CXXRecordDecl*));
599 }
600