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 all 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
2 changes: 2 additions & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,8 @@ C++23 Feature Support

- ``__cpp_explicit_this_parameter`` is now defined. (#GH82780)

- Add support for `P2280R4 Using unknown pointers and references in constant expressions <https://wg21.link/P2280R4>`_. (#GH63139)

C++20 Feature Support
^^^^^^^^^^^^^^^^^^^^^

Expand Down
53 changes: 38 additions & 15 deletions clang/include/clang/AST/APValue.h
Original file line number Diff line number Diff line change
Expand Up @@ -247,13 +247,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 @@ -312,32 +314,39 @@ class APValue {
DataType Data;

public:
bool allowConstexprUnknown() const { return AllowConstexprUnknown; }

void setConstexprUnknown(bool IsConstexprUnknown = true) {
AllowConstexprUnknown = IsConstexprUnknown;
}

/// Creates an empty APValue of type None.
APValue() : Kind(None) {}
APValue() : Kind(None), AllowConstexprUnknown(false) {}
/// Creates an integer APValue holding the given value.
explicit APValue(APSInt I) : Kind(None) {
explicit APValue(APSInt I) : Kind(None), AllowConstexprUnknown(false) {
MakeInt(); setInt(std::move(I));
}
/// Creates a float APValue holding the given value.
explicit APValue(APFloat F) : Kind(None) {
explicit APValue(APFloat F) : Kind(None), AllowConstexprUnknown(false) {
MakeFloat(); setFloat(std::move(F));
}
/// Creates a fixed-point APValue holding the given value.
explicit APValue(APFixedPoint FX) : Kind(None) {
explicit APValue(APFixedPoint FX) : Kind(None), AllowConstexprUnknown(false) {
MakeFixedPoint(std::move(FX));
}
/// Creates a vector APValue with \p N elements. The elements
/// are read from \p E.
explicit APValue(const APValue *E, unsigned N) : Kind(None) {
explicit APValue(const APValue *E, unsigned N)
: Kind(None), AllowConstexprUnknown(false) {
MakeVector(); setVector(E, N);
}
/// Creates an integer complex APValue with the given real and imaginary
/// values.
APValue(APSInt R, APSInt I) : Kind(None) {
APValue(APSInt R, APSInt I) : Kind(None), AllowConstexprUnknown(false) {
MakeComplexInt(); setComplexInt(std::move(R), std::move(I));
}
/// Creates a float complex APValue with the given real and imaginary values.
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);
Expand All @@ -348,7 +357,7 @@ class APValue {
/// \param IsNullPtr Whether this lvalue is a null pointer.
APValue(LValueBase Base, const CharUnits &Offset, NoLValuePath,
bool IsNullPtr = false)
: Kind(None) {
: Kind(None), AllowConstexprUnknown(false) {
MakeLValue();
setLValue(Base, Offset, NoLValuePath{}, IsNullPtr);
}
Expand All @@ -362,31 +371,44 @@ class APValue {
APValue(LValueBase Base, const CharUnits &Offset,
ArrayRef<LValuePathEntry> Path, bool OnePastTheEnd,
bool IsNullPtr = false)
: Kind(None) {
: Kind(None), AllowConstexprUnknown(false) {
MakeLValue();
setLValue(Base, Offset, Path, OnePastTheEnd, IsNullPtr);
}
/// Creates a constexpr unknown lvalue APValue.
/// \param Base The base of the lvalue.
/// \param Offset The offset of the lvalue.
/// \param IsNullPtr Whether this lvalue is a null pointer.
APValue(LValueBase Base, const CharUnits &Offset, ConstexprUnknown,
bool IsNullPtr = false)
: Kind(None), AllowConstexprUnknown(true) {
MakeLValue();
setLValue(Base, Offset, NoLValuePath{}, IsNullPtr);
}

/// Creates a new array APValue.
/// \param UninitArray Marker. Pass an empty UninitArray.
/// \param InitElts Number of elements you're going to initialize in the
/// array.
/// \param Size Full size of the array.
APValue(UninitArray, unsigned InitElts, unsigned Size) : Kind(None) {
APValue(UninitArray, unsigned InitElts, unsigned Size)
: Kind(None), AllowConstexprUnknown(false) {
MakeArray(InitElts, Size);
}
/// Creates a new struct APValue.
/// \param UninitStruct Marker. Pass an empty UninitStruct.
/// \param NumBases Number of bases.
/// \param NumMembers Number of members.
APValue(UninitStruct, unsigned NumBases, unsigned NumMembers) : Kind(None) {
APValue(UninitStruct, unsigned NumBases, unsigned NumMembers)
: Kind(None), AllowConstexprUnknown(false) {
MakeStruct(NumBases, NumMembers);
}
/// Creates a new union APValue.
/// \param ActiveDecl The FieldDecl of the active union member.
/// \param ActiveValue The value of the active union member.
explicit APValue(const FieldDecl *ActiveDecl,
const APValue &ActiveValue = APValue())
: Kind(None) {
: Kind(None), AllowConstexprUnknown(false) {
MakeUnion();
setUnion(ActiveDecl, ActiveValue);
}
Expand All @@ -395,14 +417,15 @@ class APValue {
/// \param IsDerivedMember Whether member is a derived one.
/// \param Path The path of the member.
APValue(const ValueDecl *Member, bool IsDerivedMember,
ArrayRef<const CXXRecordDecl*> Path) : Kind(None) {
ArrayRef<const CXXRecordDecl *> Path)
: Kind(None), AllowConstexprUnknown(false) {
MakeMemberPointer(Member, IsDerivedMember, Path);
}
/// Creates a new address label diff APValue.
/// \param LHSExpr The left-hand side of the difference.
/// \param RHSExpr The right-hand side of the difference.
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