Skip to content

[Clang] Implement P2280R4 Using unknown pointers and references in constant expressions #95474

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 33 additions & 15 deletions clang/include/clang/AST/APValue.h
Original file line number Diff line number Diff line change
Expand Up @@ -249,13 +249,15 @@ class APValue {
struct NoLValuePath {};
struct UninitArray {};
struct UninitStruct {};
struct ConstexprUnknown {};

template <typename Impl> friend class clang::serialization::BasicReaderBase;
friend class ASTImporter;
friend class ASTNodeImporter;

private:
ValueKind Kind;
bool AllowConstexprUnknown : 1;

struct ComplexAPSInt {
APSInt Real, Imag;
Expand Down Expand Up @@ -314,53 +316,69 @@ class APValue {
DataType Data;

public:
APValue() : Kind(None) {}
explicit APValue(APSInt I) : Kind(None) {
bool allowConstexprUnknown() const { return AllowConstexprUnknown; }

void setConstexprUnknown() { AllowConstexprUnknown = true; }

APValue() : Kind(None), AllowConstexprUnknown(false) {}
explicit APValue(APSInt I) : Kind(None), AllowConstexprUnknown(false) {
MakeInt(); setInt(std::move(I));
}
explicit APValue(APFloat F) : Kind(None) {
explicit APValue(APFloat F) : Kind(None), AllowConstexprUnknown(false) {
MakeFloat(); setFloat(std::move(F));
}
explicit APValue(APFixedPoint FX) : Kind(None) {
explicit APValue(APFixedPoint FX) : Kind(None), AllowConstexprUnknown(false) {
MakeFixedPoint(std::move(FX));
}
explicit APValue(const APValue *E, unsigned N) : Kind(None) {
explicit APValue(const APValue *E, unsigned N)
: Kind(None), AllowConstexprUnknown(false) {
MakeVector(); setVector(E, N);
}
APValue(APSInt R, APSInt I) : Kind(None) {
APValue(APSInt R, APSInt I) : Kind(None), AllowConstexprUnknown(false) {
MakeComplexInt(); setComplexInt(std::move(R), std::move(I));
}
APValue(APFloat R, APFloat I) : Kind(None) {
APValue(APFloat R, APFloat I) : Kind(None), AllowConstexprUnknown(false) {
MakeComplexFloat(); setComplexFloat(std::move(R), std::move(I));
}
APValue(const APValue &RHS);
APValue(APValue &&RHS);
APValue(LValueBase B, const CharUnits &O, NoLValuePath N,
bool IsNullPtr = false)
: Kind(None) {
: Kind(None), AllowConstexprUnknown(false) {
MakeLValue(); setLValue(B, O, N, IsNullPtr);
}
APValue(LValueBase B, const CharUnits &O, ArrayRef<LValuePathEntry> Path,
bool OnePastTheEnd, bool IsNullPtr = false)
: Kind(None) {
: Kind(None), AllowConstexprUnknown(false) {
MakeLValue(); setLValue(B, O, Path, OnePastTheEnd, IsNullPtr);
}
APValue(UninitArray, unsigned InitElts, unsigned Size) : Kind(None) {

APValue(LValueBase B, ConstexprUnknown, const CharUnits &O,
bool IsNullPtr = false)
: Kind(None), AllowConstexprUnknown(true) {
MakeLValue();
setLValue(B, O, NoLValuePath{}, IsNullPtr);
}

APValue(UninitArray, unsigned InitElts, unsigned Size)
: Kind(None), AllowConstexprUnknown(false) {
MakeArray(InitElts, Size);
}
APValue(UninitStruct, unsigned B, unsigned M) : Kind(None) {
APValue(UninitStruct, unsigned B, unsigned M)
: Kind(None), AllowConstexprUnknown(false) {
MakeStruct(B, M);
}
explicit APValue(const FieldDecl *D, const APValue &V = APValue())
: Kind(None) {
: Kind(None), AllowConstexprUnknown(false) {
MakeUnion(); setUnion(D, V);
}
APValue(const ValueDecl *Member, bool IsDerivedMember,
ArrayRef<const CXXRecordDecl*> Path) : Kind(None) {
ArrayRef<const CXXRecordDecl *> Path)
: Kind(None), AllowConstexprUnknown(false) {
MakeMemberPointer(Member, IsDerivedMember, Path);
}
APValue(const AddrLabelExpr* LHSExpr, const AddrLabelExpr* RHSExpr)
: Kind(None) {
APValue(const AddrLabelExpr *LHSExpr, const AddrLabelExpr *RHSExpr)
: Kind(None), AllowConstexprUnknown(false) {
MakeAddrLabelDiff(); setAddrLabelDiff(LHSExpr, RHSExpr);
}
static APValue IndeterminateValue() {
Expand Down
15 changes: 13 additions & 2 deletions clang/lib/AST/APValue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,8 @@ APValue::UnionData::~UnionData () {
delete Value;
}

APValue::APValue(const APValue &RHS) : Kind(None) {
APValue::APValue(const APValue &RHS)
: Kind(None), AllowConstexprUnknown(RHS.AllowConstexprUnknown) {
switch (RHS.getKind()) {
case None:
case Indeterminate:
Expand Down Expand Up @@ -379,13 +380,17 @@ APValue::APValue(const APValue &RHS) : Kind(None) {
}
}

APValue::APValue(APValue &&RHS) : Kind(RHS.Kind), Data(RHS.Data) {
APValue::APValue(APValue &&RHS)
: Kind(RHS.Kind), AllowConstexprUnknown(RHS.AllowConstexprUnknown),
Data(RHS.Data) {
RHS.Kind = None;
}

APValue &APValue::operator=(const APValue &RHS) {
if (this != &RHS)
*this = APValue(RHS);

AllowConstexprUnknown = RHS.AllowConstexprUnknown;
return *this;
}

Expand All @@ -395,6 +400,7 @@ APValue &APValue::operator=(APValue &&RHS) {
DestroyDataAndMakeUninit();
Kind = RHS.Kind;
Data = RHS.Data;
AllowConstexprUnknown = RHS.AllowConstexprUnknown;
RHS.Kind = None;
}
return *this;
Expand Down Expand Up @@ -426,6 +432,7 @@ void APValue::DestroyDataAndMakeUninit() {
else if (Kind == AddrLabelDiff)
((AddrLabelDiffData *)(char *)&Data)->~AddrLabelDiffData();
Kind = None;
AllowConstexprUnknown = false;
}

bool APValue::needsCleanup() const {
Expand Down Expand Up @@ -468,6 +475,10 @@ bool APValue::needsCleanup() const {
void APValue::swap(APValue &RHS) {
std::swap(Kind, RHS.Kind);
std::swap(Data, RHS.Data);
// We can't use std::swap w/ bit-fields
bool tmp = AllowConstexprUnknown;
AllowConstexprUnknown = RHS.AllowConstexprUnknown;
RHS.AllowConstexprUnknown = tmp;
}

/// Profile the value of an APInt, excluding its bit-width.
Expand Down
Loading
Loading