2008-11-16 15:46:48 +08:00
|
|
|
//===--- APValue.cpp - Union class for APFloat/APSInt/Complex -------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements the APValue class.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "clang/AST/APValue.h"
|
2011-12-17 03:06:07 +08:00
|
|
|
#include "clang/AST/ASTContext.h"
|
2010-01-15 20:37:54 +08:00
|
|
|
#include "clang/AST/CharUnits.h"
|
2011-12-17 03:06:07 +08:00
|
|
|
#include "clang/AST/DeclCXX.h"
|
|
|
|
#include "clang/AST/Expr.h"
|
|
|
|
#include "clang/AST/Type.h"
|
2011-07-19 00:43:53 +08:00
|
|
|
#include "clang/Basic/Diagnostic.h"
|
|
|
|
#include "llvm/ADT/SmallString.h"
|
2011-09-23 13:35:21 +08:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2008-11-16 15:46:48 +08:00
|
|
|
using namespace clang;
|
|
|
|
|
2010-01-15 20:37:54 +08:00
|
|
|
namespace {
|
2011-11-07 13:07:52 +08:00
|
|
|
struct LVBase {
|
2011-11-18 06:56:20 +08:00
|
|
|
llvm::PointerIntPair<APValue::LValueBase, 1, bool> BaseAndIsOnePastTheEnd;
|
2010-01-15 20:37:54 +08:00
|
|
|
CharUnits Offset;
|
2011-11-07 13:07:52 +08:00
|
|
|
unsigned PathLength;
|
2012-02-15 10:18:13 +08:00
|
|
|
unsigned CallIndex;
|
2010-01-15 20:37:54 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2011-11-07 13:07:52 +08:00
|
|
|
struct APValue::LV : LVBase {
|
|
|
|
static const unsigned InlinePathSpace =
|
2014-01-10 08:40:45 +08:00
|
|
|
(DataSize - sizeof(LVBase)) / sizeof(LValuePathEntry);
|
2011-11-07 13:07:52 +08:00
|
|
|
|
|
|
|
/// Path - The sequence of base classes, fields and array indices to follow to
|
|
|
|
/// walk from Base to the subobject. When performing GCC-style folding, there
|
|
|
|
/// may not be such a path.
|
|
|
|
union {
|
|
|
|
LValuePathEntry Path[InlinePathSpace];
|
|
|
|
LValuePathEntry *PathPtr;
|
|
|
|
};
|
|
|
|
|
|
|
|
LV() { PathLength = (unsigned)-1; }
|
2011-11-18 06:56:20 +08:00
|
|
|
~LV() { resizePath(0); }
|
2011-11-07 13:07:52 +08:00
|
|
|
|
2011-11-18 06:56:20 +08:00
|
|
|
void resizePath(unsigned Length) {
|
|
|
|
if (Length == PathLength)
|
|
|
|
return;
|
|
|
|
if (hasPathPtr())
|
|
|
|
delete [] PathPtr;
|
|
|
|
PathLength = Length;
|
|
|
|
if (hasPathPtr())
|
|
|
|
PathPtr = new LValuePathEntry[Length];
|
2011-11-07 13:07:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool hasPath() const { return PathLength != (unsigned)-1; }
|
|
|
|
bool hasPathPtr() const { return hasPath() && PathLength > InlinePathSpace; }
|
|
|
|
|
|
|
|
LValuePathEntry *getPath() { return hasPathPtr() ? PathPtr : Path; }
|
2011-11-07 15:31:09 +08:00
|
|
|
const LValuePathEntry *getPath() const {
|
|
|
|
return hasPathPtr() ? PathPtr : Path;
|
|
|
|
}
|
2011-11-07 13:07:52 +08:00
|
|
|
};
|
|
|
|
|
2011-11-18 06:56:20 +08:00
|
|
|
namespace {
|
|
|
|
struct MemberPointerBase {
|
|
|
|
llvm::PointerIntPair<const ValueDecl*, 1, bool> MemberAndIsDerivedMember;
|
|
|
|
unsigned PathLength;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
struct APValue::MemberPointerData : MemberPointerBase {
|
|
|
|
static const unsigned InlinePathSpace =
|
2014-01-10 08:40:45 +08:00
|
|
|
(DataSize - sizeof(MemberPointerBase)) / sizeof(const CXXRecordDecl*);
|
2011-11-18 06:56:20 +08:00
|
|
|
typedef const CXXRecordDecl *PathElem;
|
|
|
|
union {
|
|
|
|
PathElem Path[InlinePathSpace];
|
|
|
|
PathElem *PathPtr;
|
|
|
|
};
|
|
|
|
|
|
|
|
MemberPointerData() { PathLength = 0; }
|
|
|
|
~MemberPointerData() { resizePath(0); }
|
|
|
|
|
|
|
|
void resizePath(unsigned Length) {
|
|
|
|
if (Length == PathLength)
|
|
|
|
return;
|
|
|
|
if (hasPathPtr())
|
|
|
|
delete [] PathPtr;
|
|
|
|
PathLength = Length;
|
|
|
|
if (hasPathPtr())
|
|
|
|
PathPtr = new PathElem[Length];
|
|
|
|
}
|
|
|
|
|
|
|
|
bool hasPathPtr() const { return PathLength > InlinePathSpace; }
|
|
|
|
|
|
|
|
PathElem *getPath() { return hasPathPtr() ? PathPtr : Path; }
|
|
|
|
const PathElem *getPath() const {
|
|
|
|
return hasPathPtr() ? PathPtr : Path;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2011-11-07 17:22:26 +08:00
|
|
|
// FIXME: Reduce the malloc traffic here.
|
|
|
|
|
|
|
|
APValue::Arr::Arr(unsigned NumElts, unsigned Size) :
|
|
|
|
Elts(new APValue[NumElts + (NumElts != Size ? 1 : 0)]),
|
|
|
|
NumElts(NumElts), ArrSize(Size) {}
|
|
|
|
APValue::Arr::~Arr() { delete [] Elts; }
|
|
|
|
|
2011-11-10 14:34:14 +08:00
|
|
|
APValue::StructData::StructData(unsigned NumBases, unsigned NumFields) :
|
|
|
|
Elts(new APValue[NumBases+NumFields]),
|
|
|
|
NumBases(NumBases), NumFields(NumFields) {}
|
|
|
|
APValue::StructData::~StructData() {
|
|
|
|
delete [] Elts;
|
|
|
|
}
|
|
|
|
|
2014-05-12 13:36:57 +08:00
|
|
|
APValue::UnionData::UnionData() : Field(nullptr), Value(new APValue) {}
|
2011-11-10 14:34:14 +08:00
|
|
|
APValue::UnionData::~UnionData () {
|
|
|
|
delete Value;
|
|
|
|
}
|
|
|
|
|
2012-03-10 08:28:11 +08:00
|
|
|
APValue::APValue(const APValue &RHS) : Kind(Uninitialized) {
|
|
|
|
switch (RHS.getKind()) {
|
|
|
|
case Uninitialized:
|
|
|
|
break;
|
|
|
|
case Int:
|
|
|
|
MakeInt();
|
2008-11-16 15:46:48 +08:00
|
|
|
setInt(RHS.getInt());
|
2012-03-10 08:28:11 +08:00
|
|
|
break;
|
|
|
|
case Float:
|
|
|
|
MakeFloat();
|
2008-11-16 15:46:48 +08:00
|
|
|
setFloat(RHS.getFloat());
|
2012-03-10 08:28:11 +08:00
|
|
|
break;
|
|
|
|
case Vector:
|
|
|
|
MakeVector();
|
2014-01-10 08:40:45 +08:00
|
|
|
setVector(((const Vec *)(const char *)RHS.Data.buffer)->Elts,
|
2010-04-20 00:39:44 +08:00
|
|
|
RHS.getVectorLength());
|
2012-03-10 08:28:11 +08:00
|
|
|
break;
|
|
|
|
case ComplexInt:
|
|
|
|
MakeComplexInt();
|
2008-11-16 15:46:48 +08:00
|
|
|
setComplexInt(RHS.getComplexIntReal(), RHS.getComplexIntImag());
|
2012-03-10 08:28:11 +08:00
|
|
|
break;
|
|
|
|
case ComplexFloat:
|
|
|
|
MakeComplexFloat();
|
2008-11-16 15:46:48 +08:00
|
|
|
setComplexFloat(RHS.getComplexFloatReal(), RHS.getComplexFloatImag());
|
2012-03-10 08:28:11 +08:00
|
|
|
break;
|
|
|
|
case LValue:
|
|
|
|
MakeLValue();
|
2011-11-07 13:07:52 +08:00
|
|
|
if (RHS.hasLValuePath())
|
2011-11-18 06:56:20 +08:00
|
|
|
setLValue(RHS.getLValueBase(), RHS.getLValueOffset(), RHS.getLValuePath(),
|
2012-02-15 10:18:13 +08:00
|
|
|
RHS.isLValueOnePastTheEnd(), RHS.getLValueCallIndex());
|
2011-11-07 13:07:52 +08:00
|
|
|
else
|
2012-02-15 10:18:13 +08:00
|
|
|
setLValue(RHS.getLValueBase(), RHS.getLValueOffset(), NoLValuePath(),
|
|
|
|
RHS.getLValueCallIndex());
|
2012-03-10 08:28:11 +08:00
|
|
|
break;
|
|
|
|
case Array:
|
|
|
|
MakeArray(RHS.getArrayInitializedElts(), RHS.getArraySize());
|
2011-11-07 17:22:26 +08:00
|
|
|
for (unsigned I = 0, N = RHS.getArrayInitializedElts(); I != N; ++I)
|
|
|
|
getArrayInitializedElt(I) = RHS.getArrayInitializedElt(I);
|
|
|
|
if (RHS.hasArrayFiller())
|
|
|
|
getArrayFiller() = RHS.getArrayFiller();
|
2012-03-10 08:28:11 +08:00
|
|
|
break;
|
|
|
|
case Struct:
|
|
|
|
MakeStruct(RHS.getStructNumBases(), RHS.getStructNumFields());
|
2011-11-10 14:34:14 +08:00
|
|
|
for (unsigned I = 0, N = RHS.getStructNumBases(); I != N; ++I)
|
|
|
|
getStructBase(I) = RHS.getStructBase(I);
|
|
|
|
for (unsigned I = 0, N = RHS.getStructNumFields(); I != N; ++I)
|
|
|
|
getStructField(I) = RHS.getStructField(I);
|
2012-03-10 08:28:11 +08:00
|
|
|
break;
|
|
|
|
case Union:
|
|
|
|
MakeUnion();
|
2011-11-10 14:34:14 +08:00
|
|
|
setUnion(RHS.getUnionField(), RHS.getUnionValue());
|
2012-03-10 08:28:11 +08:00
|
|
|
break;
|
|
|
|
case MemberPointer:
|
|
|
|
MakeMemberPointer(RHS.getMemberPointerDecl(),
|
|
|
|
RHS.isMemberPointerToDerivedMember(),
|
|
|
|
RHS.getMemberPointerPath());
|
|
|
|
break;
|
|
|
|
case AddrLabelDiff:
|
|
|
|
MakeAddrLabelDiff();
|
2012-01-05 07:13:47 +08:00
|
|
|
setAddrLabelDiff(RHS.getAddrLabelDiffLHS(), RHS.getAddrLabelDiffRHS());
|
2012-03-10 08:28:11 +08:00
|
|
|
break;
|
2012-01-05 07:13:47 +08:00
|
|
|
}
|
2008-11-16 15:46:48 +08:00
|
|
|
}
|
|
|
|
|
2012-03-09 04:28:55 +08:00
|
|
|
void APValue::DestroyDataAndMakeUninit() {
|
2008-11-16 15:46:48 +08:00
|
|
|
if (Kind == Int)
|
2014-01-10 08:40:45 +08:00
|
|
|
((APSInt*)(char*)Data.buffer)->~APSInt();
|
2008-11-16 15:46:48 +08:00
|
|
|
else if (Kind == Float)
|
2014-01-10 08:40:45 +08:00
|
|
|
((APFloat*)(char*)Data.buffer)->~APFloat();
|
2009-01-18 09:01:34 +08:00
|
|
|
else if (Kind == Vector)
|
2014-01-10 08:40:45 +08:00
|
|
|
((Vec*)(char*)Data.buffer)->~Vec();
|
2008-11-16 15:46:48 +08:00
|
|
|
else if (Kind == ComplexInt)
|
2014-01-10 08:40:45 +08:00
|
|
|
((ComplexAPSInt*)(char*)Data.buffer)->~ComplexAPSInt();
|
2008-11-16 15:46:48 +08:00
|
|
|
else if (Kind == ComplexFloat)
|
2014-01-10 08:40:45 +08:00
|
|
|
((ComplexAPFloat*)(char*)Data.buffer)->~ComplexAPFloat();
|
2011-11-07 17:22:26 +08:00
|
|
|
else if (Kind == LValue)
|
2014-01-10 08:40:45 +08:00
|
|
|
((LV*)(char*)Data.buffer)->~LV();
|
2011-11-07 17:22:26 +08:00
|
|
|
else if (Kind == Array)
|
2014-01-10 08:40:45 +08:00
|
|
|
((Arr*)(char*)Data.buffer)->~Arr();
|
2011-11-10 14:34:14 +08:00
|
|
|
else if (Kind == Struct)
|
2014-01-10 08:40:45 +08:00
|
|
|
((StructData*)(char*)Data.buffer)->~StructData();
|
2011-11-10 14:34:14 +08:00
|
|
|
else if (Kind == Union)
|
2014-01-10 08:40:45 +08:00
|
|
|
((UnionData*)(char*)Data.buffer)->~UnionData();
|
2011-11-18 06:56:20 +08:00
|
|
|
else if (Kind == MemberPointer)
|
2014-01-10 08:40:45 +08:00
|
|
|
((MemberPointerData*)(char*)Data.buffer)->~MemberPointerData();
|
2012-01-05 07:13:47 +08:00
|
|
|
else if (Kind == AddrLabelDiff)
|
2014-01-10 08:40:45 +08:00
|
|
|
((AddrLabelDiffData*)(char*)Data.buffer)->~AddrLabelDiffData();
|
2009-01-18 09:01:34 +08:00
|
|
|
Kind = Uninitialized;
|
2008-11-16 15:46:48 +08:00
|
|
|
}
|
|
|
|
|
2013-06-03 21:51:33 +08:00
|
|
|
bool APValue::needsCleanup() const {
|
|
|
|
switch (getKind()) {
|
|
|
|
case Uninitialized:
|
|
|
|
case AddrLabelDiff:
|
|
|
|
return false;
|
|
|
|
case Struct:
|
|
|
|
case Union:
|
|
|
|
case Array:
|
|
|
|
case Vector:
|
|
|
|
return true;
|
|
|
|
case Int:
|
|
|
|
return getInt().needsCleanup();
|
|
|
|
case Float:
|
|
|
|
return getFloat().needsCleanup();
|
|
|
|
case ComplexFloat:
|
|
|
|
assert(getComplexFloatImag().needsCleanup() ==
|
|
|
|
getComplexFloatReal().needsCleanup() &&
|
|
|
|
"In _Complex float types, real and imaginary values always have the "
|
|
|
|
"same size.");
|
|
|
|
return getComplexFloatReal().needsCleanup();
|
|
|
|
case ComplexInt:
|
|
|
|
assert(getComplexIntImag().needsCleanup() ==
|
|
|
|
getComplexIntReal().needsCleanup() &&
|
|
|
|
"In _Complex int types, real and imaginary values must have the "
|
|
|
|
"same size.");
|
|
|
|
return getComplexIntReal().needsCleanup();
|
|
|
|
case LValue:
|
2014-01-10 08:40:45 +08:00
|
|
|
return reinterpret_cast<const LV *>(Data.buffer)->hasPathPtr();
|
2013-06-03 21:51:33 +08:00
|
|
|
case MemberPointer:
|
2014-01-10 08:40:45 +08:00
|
|
|
return reinterpret_cast<const MemberPointerData *>(Data.buffer)
|
|
|
|
->hasPathPtr();
|
2013-06-03 21:51:33 +08:00
|
|
|
}
|
2013-06-04 05:26:13 +08:00
|
|
|
llvm_unreachable("Unknown APValue kind!");
|
2013-06-03 21:51:33 +08:00
|
|
|
}
|
|
|
|
|
2012-03-10 08:28:11 +08:00
|
|
|
void APValue::swap(APValue &RHS) {
|
|
|
|
std::swap(Kind, RHS.Kind);
|
2014-01-10 08:40:45 +08:00
|
|
|
char TmpData[DataSize];
|
|
|
|
memcpy(TmpData, Data.buffer, DataSize);
|
|
|
|
memcpy(Data.buffer, RHS.Data.buffer, DataSize);
|
|
|
|
memcpy(RHS.Data.buffer, TmpData, DataSize);
|
2012-03-10 08:28:11 +08:00
|
|
|
}
|
|
|
|
|
2008-11-16 15:46:48 +08:00
|
|
|
void APValue::dump() const {
|
2011-12-17 03:06:07 +08:00
|
|
|
dump(llvm::errs());
|
2008-11-16 15:46:48 +08:00
|
|
|
llvm::errs() << '\n';
|
|
|
|
}
|
|
|
|
|
|
|
|
static double GetApproxValue(const llvm::APFloat &F) {
|
|
|
|
llvm::APFloat V = F;
|
|
|
|
bool ignored;
|
|
|
|
V.convert(llvm::APFloat::IEEEdouble, llvm::APFloat::rmNearestTiesToEven,
|
|
|
|
&ignored);
|
|
|
|
return V.convertToDouble();
|
|
|
|
}
|
|
|
|
|
2011-12-17 03:06:07 +08:00
|
|
|
void APValue::dump(raw_ostream &OS) const {
|
2008-11-16 15:46:48 +08:00
|
|
|
switch (getKind()) {
|
|
|
|
case Uninitialized:
|
|
|
|
OS << "Uninitialized";
|
|
|
|
return;
|
|
|
|
case Int:
|
|
|
|
OS << "Int: " << getInt();
|
|
|
|
return;
|
|
|
|
case Float:
|
|
|
|
OS << "Float: " << GetApproxValue(getFloat());
|
|
|
|
return;
|
2009-01-18 09:01:34 +08:00
|
|
|
case Vector:
|
2011-12-17 03:06:07 +08:00
|
|
|
OS << "Vector: ";
|
|
|
|
getVectorElt(0).dump(OS);
|
|
|
|
for (unsigned i = 1; i != getVectorLength(); ++i) {
|
|
|
|
OS << ", ";
|
|
|
|
getVectorElt(i).dump(OS);
|
|
|
|
}
|
2009-01-18 09:01:34 +08:00
|
|
|
return;
|
2008-11-16 15:46:48 +08:00
|
|
|
case ComplexInt:
|
|
|
|
OS << "ComplexInt: " << getComplexIntReal() << ", " << getComplexIntImag();
|
|
|
|
return;
|
|
|
|
case ComplexFloat:
|
|
|
|
OS << "ComplexFloat: " << GetApproxValue(getComplexFloatReal())
|
|
|
|
<< ", " << GetApproxValue(getComplexFloatImag());
|
2011-11-07 17:22:26 +08:00
|
|
|
return;
|
2008-11-16 15:46:48 +08:00
|
|
|
case LValue:
|
|
|
|
OS << "LValue: <todo>";
|
|
|
|
return;
|
2011-11-07 17:22:26 +08:00
|
|
|
case Array:
|
|
|
|
OS << "Array: ";
|
|
|
|
for (unsigned I = 0, N = getArrayInitializedElts(); I != N; ++I) {
|
2011-12-17 03:06:07 +08:00
|
|
|
getArrayInitializedElt(I).dump(OS);
|
2011-11-07 17:22:26 +08:00
|
|
|
if (I != getArraySize() - 1) OS << ", ";
|
|
|
|
}
|
2011-12-17 03:06:07 +08:00
|
|
|
if (hasArrayFiller()) {
|
|
|
|
OS << getArraySize() - getArrayInitializedElts() << " x ";
|
|
|
|
getArrayFiller().dump(OS);
|
|
|
|
}
|
2011-11-07 17:22:26 +08:00
|
|
|
return;
|
2011-11-10 14:34:14 +08:00
|
|
|
case Struct:
|
|
|
|
OS << "Struct ";
|
|
|
|
if (unsigned N = getStructNumBases()) {
|
2011-12-17 03:06:07 +08:00
|
|
|
OS << " bases: ";
|
|
|
|
getStructBase(0).dump(OS);
|
|
|
|
for (unsigned I = 1; I != N; ++I) {
|
|
|
|
OS << ", ";
|
|
|
|
getStructBase(I).dump(OS);
|
|
|
|
}
|
2011-11-10 14:34:14 +08:00
|
|
|
}
|
|
|
|
if (unsigned N = getStructNumFields()) {
|
2011-12-17 03:06:07 +08:00
|
|
|
OS << " fields: ";
|
|
|
|
getStructField(0).dump(OS);
|
|
|
|
for (unsigned I = 1; I != N; ++I) {
|
|
|
|
OS << ", ";
|
|
|
|
getStructField(I).dump(OS);
|
|
|
|
}
|
2011-11-10 14:34:14 +08:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
case Union:
|
2011-12-17 03:06:07 +08:00
|
|
|
OS << "Union: ";
|
|
|
|
getUnionValue().dump(OS);
|
2011-11-10 14:34:14 +08:00
|
|
|
return;
|
2011-11-18 06:56:20 +08:00
|
|
|
case MemberPointer:
|
|
|
|
OS << "MemberPointer: <todo>";
|
|
|
|
return;
|
2012-01-05 07:13:47 +08:00
|
|
|
case AddrLabelDiff:
|
|
|
|
OS << "AddrLabelDiff: <todo>";
|
|
|
|
return;
|
2008-11-16 15:46:48 +08:00
|
|
|
}
|
2011-11-10 14:34:14 +08:00
|
|
|
llvm_unreachable("Unknown APValue kind!");
|
2008-11-16 15:46:48 +08:00
|
|
|
}
|
|
|
|
|
2011-12-17 03:06:07 +08:00
|
|
|
void APValue::printPretty(raw_ostream &Out, ASTContext &Ctx, QualType Ty) const{
|
|
|
|
switch (getKind()) {
|
2011-07-19 00:43:53 +08:00
|
|
|
case APValue::Uninitialized:
|
2011-12-17 03:06:07 +08:00
|
|
|
Out << "<uninitialized>";
|
2011-11-10 14:34:14 +08:00
|
|
|
return;
|
2011-07-19 00:43:53 +08:00
|
|
|
case APValue::Int:
|
2012-03-24 07:55:39 +08:00
|
|
|
if (Ty->isBooleanType())
|
|
|
|
Out << (getInt().getBoolValue() ? "true" : "false");
|
|
|
|
else
|
|
|
|
Out << getInt();
|
2011-11-10 14:34:14 +08:00
|
|
|
return;
|
2011-07-19 00:43:53 +08:00
|
|
|
case APValue::Float:
|
2011-12-17 03:06:07 +08:00
|
|
|
Out << GetApproxValue(getFloat());
|
2011-11-10 14:34:14 +08:00
|
|
|
return;
|
2011-12-17 03:06:07 +08:00
|
|
|
case APValue::Vector: {
|
|
|
|
Out << '{';
|
|
|
|
QualType ElemTy = Ty->getAs<VectorType>()->getElementType();
|
|
|
|
getVectorElt(0).printPretty(Out, Ctx, ElemTy);
|
|
|
|
for (unsigned i = 1; i != getVectorLength(); ++i) {
|
2011-07-19 00:43:53 +08:00
|
|
|
Out << ", ";
|
2011-12-17 03:06:07 +08:00
|
|
|
getVectorElt(i).printPretty(Out, Ctx, ElemTy);
|
2011-07-19 00:43:53 +08:00
|
|
|
}
|
2011-12-17 03:06:07 +08:00
|
|
|
Out << '}';
|
2011-11-10 14:34:14 +08:00
|
|
|
return;
|
2011-12-17 03:06:07 +08:00
|
|
|
}
|
2011-07-19 00:43:53 +08:00
|
|
|
case APValue::ComplexInt:
|
2011-12-17 03:06:07 +08:00
|
|
|
Out << getComplexIntReal() << "+" << getComplexIntImag() << "i";
|
2011-11-10 14:34:14 +08:00
|
|
|
return;
|
2011-07-19 00:43:53 +08:00
|
|
|
case APValue::ComplexFloat:
|
2011-12-17 03:06:07 +08:00
|
|
|
Out << GetApproxValue(getComplexFloatReal()) << "+"
|
|
|
|
<< GetApproxValue(getComplexFloatImag()) << "i";
|
2011-11-10 14:34:14 +08:00
|
|
|
return;
|
2011-12-17 03:06:07 +08:00
|
|
|
case APValue::LValue: {
|
|
|
|
LValueBase Base = getLValueBase();
|
|
|
|
if (!Base) {
|
|
|
|
Out << "0";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IsReference = Ty->isReferenceType();
|
|
|
|
QualType InnerTy
|
|
|
|
= IsReference ? Ty.getNonReferenceType() : Ty->getPointeeType();
|
2013-01-29 09:26:43 +08:00
|
|
|
if (InnerTy.isNull())
|
|
|
|
InnerTy = Ty;
|
2011-12-17 03:06:07 +08:00
|
|
|
|
|
|
|
if (!hasLValuePath()) {
|
|
|
|
// No lvalue path: just print the offset.
|
|
|
|
CharUnits O = getLValueOffset();
|
|
|
|
CharUnits S = Ctx.getTypeSizeInChars(InnerTy);
|
|
|
|
if (!O.isZero()) {
|
|
|
|
if (IsReference)
|
|
|
|
Out << "*(";
|
|
|
|
if (O % S) {
|
|
|
|
Out << "(char*)";
|
|
|
|
S = CharUnits::One();
|
|
|
|
}
|
|
|
|
Out << '&';
|
|
|
|
} else if (!IsReference)
|
|
|
|
Out << '&';
|
|
|
|
|
|
|
|
if (const ValueDecl *VD = Base.dyn_cast<const ValueDecl*>())
|
|
|
|
Out << *VD;
|
2014-06-10 06:53:25 +08:00
|
|
|
else {
|
|
|
|
assert(Base.get<const Expr *>() != nullptr &&
|
|
|
|
"Expecting non-null Expr");
|
2014-05-12 13:36:57 +08:00
|
|
|
Base.get<const Expr*>()->printPretty(Out, nullptr,
|
|
|
|
Ctx.getPrintingPolicy());
|
2014-06-10 06:53:25 +08:00
|
|
|
}
|
|
|
|
|
2011-12-17 03:06:07 +08:00
|
|
|
if (!O.isZero()) {
|
|
|
|
Out << " + " << (O / S);
|
|
|
|
if (IsReference)
|
|
|
|
Out << ')';
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We have an lvalue path. Print it out nicely.
|
|
|
|
if (!IsReference)
|
|
|
|
Out << '&';
|
|
|
|
else if (isLValueOnePastTheEnd())
|
|
|
|
Out << "*(&";
|
|
|
|
|
|
|
|
QualType ElemTy;
|
|
|
|
if (const ValueDecl *VD = Base.dyn_cast<const ValueDecl*>()) {
|
|
|
|
Out << *VD;
|
|
|
|
ElemTy = VD->getType();
|
|
|
|
} else {
|
|
|
|
const Expr *E = Base.get<const Expr*>();
|
2014-06-10 06:53:25 +08:00
|
|
|
assert(E != nullptr && "Expecting non-null Expr");
|
2014-05-12 13:36:57 +08:00
|
|
|
E->printPretty(Out, nullptr, Ctx.getPrintingPolicy());
|
2011-12-17 03:06:07 +08:00
|
|
|
ElemTy = E->getType();
|
|
|
|
}
|
|
|
|
|
|
|
|
ArrayRef<LValuePathEntry> Path = getLValuePath();
|
2014-05-12 13:36:57 +08:00
|
|
|
const CXXRecordDecl *CastToBase = nullptr;
|
2011-12-17 03:06:07 +08:00
|
|
|
for (unsigned I = 0, N = Path.size(); I != N; ++I) {
|
|
|
|
if (ElemTy->getAs<RecordType>()) {
|
|
|
|
// The lvalue refers to a class type, so the next path entry is a base
|
|
|
|
// or member.
|
|
|
|
const Decl *BaseOrMember =
|
|
|
|
BaseOrMemberType::getFromOpaqueValue(Path[I].BaseOrMember).getPointer();
|
|
|
|
if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(BaseOrMember)) {
|
|
|
|
CastToBase = RD;
|
|
|
|
ElemTy = Ctx.getRecordType(RD);
|
|
|
|
} else {
|
|
|
|
const ValueDecl *VD = cast<ValueDecl>(BaseOrMember);
|
|
|
|
Out << ".";
|
|
|
|
if (CastToBase)
|
|
|
|
Out << *CastToBase << "::";
|
|
|
|
Out << *VD;
|
|
|
|
ElemTy = VD->getType();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// The lvalue must refer to an array.
|
|
|
|
Out << '[' << Path[I].ArrayIndex << ']';
|
|
|
|
ElemTy = Ctx.getAsArrayType(ElemTy)->getElementType();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle formatting of one-past-the-end lvalues.
|
|
|
|
if (isLValueOnePastTheEnd()) {
|
|
|
|
// FIXME: If CastToBase is non-0, we should prefix the output with
|
|
|
|
// "(CastToBase*)".
|
|
|
|
Out << " + 1";
|
|
|
|
if (IsReference)
|
|
|
|
Out << ')';
|
|
|
|
}
|
2011-11-10 14:34:14 +08:00
|
|
|
return;
|
2011-12-17 03:06:07 +08:00
|
|
|
}
|
|
|
|
case APValue::Array: {
|
|
|
|
const ArrayType *AT = Ctx.getAsArrayType(Ty);
|
|
|
|
QualType ElemTy = AT->getElementType();
|
2011-11-07 17:22:26 +08:00
|
|
|
Out << '{';
|
2011-12-17 03:06:07 +08:00
|
|
|
if (unsigned N = getArrayInitializedElts()) {
|
|
|
|
getArrayInitializedElt(0).printPretty(Out, Ctx, ElemTy);
|
|
|
|
for (unsigned I = 1; I != N; ++I) {
|
|
|
|
Out << ", ";
|
|
|
|
if (I == 10) {
|
|
|
|
// Avoid printing out the entire contents of large arrays.
|
|
|
|
Out << "...";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
getArrayInitializedElt(I).printPretty(Out, Ctx, ElemTy);
|
|
|
|
}
|
2011-11-07 17:22:26 +08:00
|
|
|
}
|
|
|
|
Out << '}';
|
2011-11-10 14:34:14 +08:00
|
|
|
return;
|
2011-12-17 03:06:07 +08:00
|
|
|
}
|
|
|
|
case APValue::Struct: {
|
2011-11-10 14:34:14 +08:00
|
|
|
Out << '{';
|
2011-12-17 03:06:07 +08:00
|
|
|
const RecordDecl *RD = Ty->getAs<RecordType>()->getDecl();
|
|
|
|
bool First = true;
|
|
|
|
if (unsigned N = getStructNumBases()) {
|
|
|
|
const CXXRecordDecl *CD = cast<CXXRecordDecl>(RD);
|
|
|
|
CXXRecordDecl::base_class_const_iterator BI = CD->bases_begin();
|
|
|
|
for (unsigned I = 0; I != N; ++I, ++BI) {
|
|
|
|
assert(BI != CD->bases_end());
|
|
|
|
if (!First)
|
|
|
|
Out << ", ";
|
|
|
|
getStructBase(I).printPretty(Out, Ctx, BI->getType());
|
|
|
|
First = false;
|
|
|
|
}
|
2011-11-10 14:34:14 +08:00
|
|
|
}
|
2014-03-09 04:12:42 +08:00
|
|
|
for (const auto *FI : RD->fields()) {
|
2011-12-17 03:06:07 +08:00
|
|
|
if (!First)
|
|
|
|
Out << ", ";
|
2012-04-30 10:36:29 +08:00
|
|
|
if (FI->isUnnamedBitfield()) continue;
|
|
|
|
getStructField(FI->getFieldIndex()).
|
|
|
|
printPretty(Out, Ctx, FI->getType());
|
2011-12-17 03:06:07 +08:00
|
|
|
First = false;
|
2011-11-10 14:34:14 +08:00
|
|
|
}
|
|
|
|
Out << '}';
|
|
|
|
return;
|
2011-12-17 03:06:07 +08:00
|
|
|
}
|
2011-11-10 14:34:14 +08:00
|
|
|
case APValue::Union:
|
2011-12-17 03:06:07 +08:00
|
|
|
Out << '{';
|
|
|
|
if (const FieldDecl *FD = getUnionField()) {
|
|
|
|
Out << "." << *FD << " = ";
|
|
|
|
getUnionValue().printPretty(Out, Ctx, FD->getType());
|
|
|
|
}
|
|
|
|
Out << '}';
|
2011-11-10 14:34:14 +08:00
|
|
|
return;
|
2011-11-18 06:56:20 +08:00
|
|
|
case APValue::MemberPointer:
|
2011-12-17 03:06:07 +08:00
|
|
|
// FIXME: This is not enough to unambiguously identify the member in a
|
|
|
|
// multiple-inheritance scenario.
|
|
|
|
if (const ValueDecl *VD = getMemberPointerDecl()) {
|
|
|
|
Out << '&' << *cast<CXXRecordDecl>(VD->getDeclContext()) << "::" << *VD;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Out << "0";
|
2011-11-18 06:56:20 +08:00
|
|
|
return;
|
2012-01-05 07:13:47 +08:00
|
|
|
case APValue::AddrLabelDiff:
|
|
|
|
Out << "&&" << getAddrLabelDiffLHS()->getLabel()->getName();
|
|
|
|
Out << " - ";
|
|
|
|
Out << "&&" << getAddrLabelDiffRHS()->getLabel()->getName();
|
|
|
|
return;
|
2011-07-19 00:43:53 +08:00
|
|
|
}
|
2011-11-10 14:34:14 +08:00
|
|
|
llvm_unreachable("Unknown APValue kind!");
|
2011-07-19 00:43:53 +08:00
|
|
|
}
|
|
|
|
|
2011-12-17 03:06:07 +08:00
|
|
|
std::string APValue::getAsString(ASTContext &Ctx, QualType Ty) const {
|
|
|
|
std::string Result;
|
|
|
|
llvm::raw_string_ostream Out(Result);
|
|
|
|
printPretty(Out, Ctx, Ty);
|
2011-12-17 06:12:23 +08:00
|
|
|
Out.flush();
|
2011-12-17 03:06:07 +08:00
|
|
|
return Result;
|
2011-07-19 00:43:53 +08:00
|
|
|
}
|
|
|
|
|
2011-11-13 06:28:03 +08:00
|
|
|
const APValue::LValueBase APValue::getLValueBase() const {
|
2010-01-15 20:37:54 +08:00
|
|
|
assert(isLValue() && "Invalid accessor");
|
2014-01-10 08:40:45 +08:00
|
|
|
return ((const LV*)(const void*)Data.buffer)->BaseAndIsOnePastTheEnd.getPointer();
|
2011-11-18 06:56:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool APValue::isLValueOnePastTheEnd() const {
|
|
|
|
assert(isLValue() && "Invalid accessor");
|
2014-01-10 08:40:45 +08:00
|
|
|
return ((const LV*)(const void*)Data.buffer)->BaseAndIsOnePastTheEnd.getInt();
|
2010-01-15 20:37:54 +08:00
|
|
|
}
|
|
|
|
|
2011-10-30 04:57:55 +08:00
|
|
|
CharUnits &APValue::getLValueOffset() {
|
|
|
|
assert(isLValue() && "Invalid accessor");
|
2014-01-10 08:40:45 +08:00
|
|
|
return ((LV*)(void*)Data.buffer)->Offset;
|
2010-01-15 20:37:54 +08:00
|
|
|
}
|
|
|
|
|
2011-11-07 13:07:52 +08:00
|
|
|
bool APValue::hasLValuePath() const {
|
|
|
|
assert(isLValue() && "Invalid accessor");
|
2014-01-10 08:40:45 +08:00
|
|
|
return ((const LV*)(const char*)Data.buffer)->hasPath();
|
2011-11-07 13:07:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ArrayRef<APValue::LValuePathEntry> APValue::getLValuePath() const {
|
|
|
|
assert(isLValue() && hasLValuePath() && "Invalid accessor");
|
2014-01-10 08:40:45 +08:00
|
|
|
const LV &LVal = *((const LV*)(const char*)Data.buffer);
|
2011-11-07 13:07:52 +08:00
|
|
|
return ArrayRef<LValuePathEntry>(LVal.getPath(), LVal.PathLength);
|
|
|
|
}
|
|
|
|
|
2012-02-15 10:18:13 +08:00
|
|
|
unsigned APValue::getLValueCallIndex() const {
|
|
|
|
assert(isLValue() && "Invalid accessor");
|
2014-01-10 08:40:45 +08:00
|
|
|
return ((const LV*)(const char*)Data.buffer)->CallIndex;
|
2012-02-15 10:18:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void APValue::setLValue(LValueBase B, const CharUnits &O, NoLValuePath,
|
|
|
|
unsigned CallIndex) {
|
2011-11-07 13:07:52 +08:00
|
|
|
assert(isLValue() && "Invalid accessor");
|
2014-01-10 08:40:45 +08:00
|
|
|
LV &LVal = *((LV*)(char*)Data.buffer);
|
2011-11-18 06:56:20 +08:00
|
|
|
LVal.BaseAndIsOnePastTheEnd.setPointer(B);
|
|
|
|
LVal.BaseAndIsOnePastTheEnd.setInt(false);
|
2011-11-07 13:07:52 +08:00
|
|
|
LVal.Offset = O;
|
2012-02-15 10:18:13 +08:00
|
|
|
LVal.CallIndex = CallIndex;
|
2011-11-18 06:56:20 +08:00
|
|
|
LVal.resizePath((unsigned)-1);
|
2011-11-07 13:07:52 +08:00
|
|
|
}
|
|
|
|
|
2011-11-13 06:28:03 +08:00
|
|
|
void APValue::setLValue(LValueBase B, const CharUnits &O,
|
2012-02-15 10:18:13 +08:00
|
|
|
ArrayRef<LValuePathEntry> Path, bool IsOnePastTheEnd,
|
|
|
|
unsigned CallIndex) {
|
2010-01-15 20:37:54 +08:00
|
|
|
assert(isLValue() && "Invalid accessor");
|
2014-01-10 08:40:45 +08:00
|
|
|
LV &LVal = *((LV*)(char*)Data.buffer);
|
2011-11-18 06:56:20 +08:00
|
|
|
LVal.BaseAndIsOnePastTheEnd.setPointer(B);
|
|
|
|
LVal.BaseAndIsOnePastTheEnd.setInt(IsOnePastTheEnd);
|
2011-11-07 13:07:52 +08:00
|
|
|
LVal.Offset = O;
|
2012-02-15 10:18:13 +08:00
|
|
|
LVal.CallIndex = CallIndex;
|
2011-11-18 06:56:20 +08:00
|
|
|
LVal.resizePath(Path.size());
|
2011-11-07 13:07:52 +08:00
|
|
|
memcpy(LVal.getPath(), Path.data(), Path.size() * sizeof(LValuePathEntry));
|
2010-01-15 20:37:54 +08:00
|
|
|
}
|
|
|
|
|
2011-11-18 06:56:20 +08:00
|
|
|
const ValueDecl *APValue::getMemberPointerDecl() const {
|
|
|
|
assert(isMemberPointer() && "Invalid accessor");
|
2014-01-10 08:40:45 +08:00
|
|
|
const MemberPointerData &MPD =
|
|
|
|
*((const MemberPointerData *)(const char *)Data.buffer);
|
2011-11-18 06:56:20 +08:00
|
|
|
return MPD.MemberAndIsDerivedMember.getPointer();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool APValue::isMemberPointerToDerivedMember() const {
|
|
|
|
assert(isMemberPointer() && "Invalid accessor");
|
2014-01-10 08:40:45 +08:00
|
|
|
const MemberPointerData &MPD =
|
|
|
|
*((const MemberPointerData *)(const char *)Data.buffer);
|
2011-11-18 06:56:20 +08:00
|
|
|
return MPD.MemberAndIsDerivedMember.getInt();
|
|
|
|
}
|
|
|
|
|
|
|
|
ArrayRef<const CXXRecordDecl*> APValue::getMemberPointerPath() const {
|
|
|
|
assert(isMemberPointer() && "Invalid accessor");
|
2014-01-10 08:40:45 +08:00
|
|
|
const MemberPointerData &MPD =
|
|
|
|
*((const MemberPointerData *)(const char *)Data.buffer);
|
2011-11-18 06:56:20 +08:00
|
|
|
return ArrayRef<const CXXRecordDecl*>(MPD.getPath(), MPD.PathLength);
|
|
|
|
}
|
|
|
|
|
2010-01-15 20:37:54 +08:00
|
|
|
void APValue::MakeLValue() {
|
|
|
|
assert(isUninit() && "Bad state change");
|
2014-03-16 02:54:13 +08:00
|
|
|
static_assert(sizeof(LV) <= DataSize, "LV too big");
|
2014-01-10 08:40:45 +08:00
|
|
|
new ((void*)(char*)Data.buffer) LV();
|
2010-01-15 20:37:54 +08:00
|
|
|
Kind = LValue;
|
|
|
|
}
|
2011-11-07 17:22:26 +08:00
|
|
|
|
|
|
|
void APValue::MakeArray(unsigned InitElts, unsigned Size) {
|
|
|
|
assert(isUninit() && "Bad state change");
|
2014-01-10 08:40:45 +08:00
|
|
|
new ((void*)(char*)Data.buffer) Arr(InitElts, Size);
|
2011-11-07 17:22:26 +08:00
|
|
|
Kind = Array;
|
|
|
|
}
|
2011-11-18 06:56:20 +08:00
|
|
|
|
|
|
|
void APValue::MakeMemberPointer(const ValueDecl *Member, bool IsDerivedMember,
|
|
|
|
ArrayRef<const CXXRecordDecl*> Path) {
|
|
|
|
assert(isUninit() && "Bad state change");
|
2014-01-10 08:40:45 +08:00
|
|
|
MemberPointerData *MPD = new ((void*)(char*)Data.buffer) MemberPointerData;
|
2011-11-18 06:56:20 +08:00
|
|
|
Kind = MemberPointer;
|
|
|
|
MPD->MemberAndIsDerivedMember.setPointer(Member);
|
|
|
|
MPD->MemberAndIsDerivedMember.setInt(IsDerivedMember);
|
|
|
|
MPD->resizePath(Path.size());
|
|
|
|
memcpy(MPD->getPath(), Path.data(), Path.size()*sizeof(const CXXRecordDecl*));
|
|
|
|
}
|