-
Notifications
You must be signed in to change notification settings - Fork 10.8k
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
[NFC][clang] change remaining context-dependent type nodes to ContextualFoldingSet #67751
[NFC][clang] change remaining context-dependent type nodes to ContextualFoldingSet #67751
Conversation
@llvm/pr-subscribers-clang ChangesStacked, consider only last commit. Patch is 28.71 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/67751.diff 6 Files Affected:
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 1d74c492845a6c3..09302040a3510b6 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -266,6 +266,9 @@ Bug Fixes in This Version
(`#64836 <https://github.com/llvm/llvm-project/issues/64836>`_)
- Clang now allows an ``_Atomic`` qualified integer in a switch statement. Fixes
(`#65557 <https://github.com/llvm/llvm-project/issues/65557>`_)
+- Fixes crash when trying to obtain the common sugared type of
+ `decltype(instantiation-dependent-expr)`.
+ Fixes (`#67603 <https://github.com/llvm/llvm-project/issues/67603>`_)
Bug Fixes to Compiler Builtins
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
diff --git a/clang/include/clang/AST/ASTContext.h b/clang/include/clang/AST/ASTContext.h
index 4ee32c76a95d8e3..8ad0514ee2ce227 100644
--- a/clang/include/clang/AST/ASTContext.h
+++ b/clang/include/clang/AST/ASTContext.h
@@ -195,20 +195,25 @@ class ASTContext : public RefCountedBase<ASTContext> {
ConstantArrayTypes;
mutable llvm::FoldingSet<IncompleteArrayType> IncompleteArrayTypes;
mutable std::vector<VariableArrayType*> VariableArrayTypes;
- mutable llvm::FoldingSet<DependentSizedArrayType> DependentSizedArrayTypes;
- mutable llvm::FoldingSet<DependentSizedExtVectorType>
- DependentSizedExtVectorTypes;
- mutable llvm::FoldingSet<DependentAddressSpaceType>
+ mutable llvm::ContextualFoldingSet<DependentSizedArrayType, ASTContext &>
+ DependentSizedArrayTypes;
+ mutable llvm::ContextualFoldingSet<DependentSizedExtVectorType, ASTContext &>
+ DependentSizedExtVectorTypes;
+ mutable llvm::ContextualFoldingSet<DependentAddressSpaceType, ASTContext &>
DependentAddressSpaceTypes;
mutable llvm::FoldingSet<VectorType> VectorTypes;
- mutable llvm::FoldingSet<DependentVectorType> DependentVectorTypes;
+ mutable llvm::ContextualFoldingSet<DependentVectorType, ASTContext &>
+ DependentVectorTypes;
mutable llvm::FoldingSet<ConstantMatrixType> MatrixTypes;
- mutable llvm::FoldingSet<DependentSizedMatrixType> DependentSizedMatrixTypes;
+ mutable llvm::ContextualFoldingSet<DependentSizedMatrixType, ASTContext &>
+ DependentSizedMatrixTypes;
mutable llvm::FoldingSet<FunctionNoProtoType> FunctionNoProtoTypes;
mutable llvm::ContextualFoldingSet<FunctionProtoType, ASTContext&>
FunctionProtoTypes;
- mutable llvm::FoldingSet<DependentTypeOfExprType> DependentTypeOfExprTypes;
- mutable llvm::FoldingSet<DependentDecltypeType> DependentDecltypeTypes;
+ mutable llvm::ContextualFoldingSet<DependentTypeOfExprType, ASTContext &>
+ DependentTypeOfExprTypes;
+ mutable llvm::ContextualFoldingSet<DependentDecltypeType, ASTContext &>
+ DependentDecltypeTypes;
mutable llvm::FoldingSet<TemplateTypeParmType> TemplateTypeParmTypes;
mutable llvm::FoldingSet<ObjCTypeParamType> ObjCTypeParamTypes;
mutable llvm::FoldingSet<SubstTemplateTypeParmType>
@@ -238,7 +243,8 @@ class ASTContext : public RefCountedBase<ASTContext> {
mutable llvm::FoldingSet<AttributedType> AttributedTypes;
mutable llvm::FoldingSet<PipeType> PipeTypes;
mutable llvm::FoldingSet<BitIntType> BitIntTypes;
- mutable llvm::FoldingSet<DependentBitIntType> DependentBitIntTypes;
+ mutable llvm::ContextualFoldingSet<DependentBitIntType, ASTContext &>
+ DependentBitIntTypes;
llvm::FoldingSet<BTFTagAttributedType> BTFTagAttributedTypes;
mutable llvm::FoldingSet<QualifiedTemplateName> QualifiedTemplateNames;
diff --git a/clang/include/clang/AST/Type.h b/clang/include/clang/AST/Type.h
index 4799f89db82fa7f..a78d8f60462b231 100644
--- a/clang/include/clang/AST/Type.h
+++ b/clang/include/clang/AST/Type.h
@@ -3289,8 +3289,6 @@ class VariableArrayType : public ArrayType {
class DependentSizedArrayType : public ArrayType {
friend class ASTContext; // ASTContext creates these.
- const ASTContext &Context;
-
/// An assignment expression that will instantiate to the
/// size of the array.
///
@@ -3301,8 +3299,8 @@ class DependentSizedArrayType : public ArrayType {
/// The range spanned by the left and right array brackets.
SourceRange Brackets;
- DependentSizedArrayType(const ASTContext &Context, QualType et, QualType can,
- Expr *e, ArraySizeModifier sm, unsigned tq,
+ DependentSizedArrayType(QualType et, QualType can, Expr *e,
+ ArraySizeModifier sm, unsigned tq,
SourceRange brackets);
public:
@@ -3325,7 +3323,7 @@ class DependentSizedArrayType : public ArrayType {
return T->getTypeClass() == DependentSizedArray;
}
- void Profile(llvm::FoldingSetNodeID &ID) {
+ void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context) {
Profile(ID, Context, getElementType(),
getSizeModifier(), getIndexTypeCVRQualifiers(), getSizeExpr());
}
@@ -3349,14 +3347,12 @@ class DependentSizedArrayType : public ArrayType {
class DependentAddressSpaceType : public Type, public llvm::FoldingSetNode {
friend class ASTContext;
- const ASTContext &Context;
Expr *AddrSpaceExpr;
QualType PointeeType;
SourceLocation loc;
- DependentAddressSpaceType(const ASTContext &Context, QualType PointeeType,
- QualType can, Expr *AddrSpaceExpr,
- SourceLocation loc);
+ DependentAddressSpaceType(QualType PointeeType, QualType can,
+ Expr *AddrSpaceExpr, SourceLocation loc);
public:
Expr *getAddrSpaceExpr() const { return AddrSpaceExpr; }
@@ -3370,7 +3366,7 @@ class DependentAddressSpaceType : public Type, public llvm::FoldingSetNode {
return T->getTypeClass() == DependentAddressSpace;
}
- void Profile(llvm::FoldingSetNodeID &ID) {
+ void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context) {
Profile(ID, Context, getPointeeType(), getAddrSpaceExpr());
}
@@ -3391,7 +3387,6 @@ class DependentAddressSpaceType : public Type, public llvm::FoldingSetNode {
class DependentSizedExtVectorType : public Type, public llvm::FoldingSetNode {
friend class ASTContext;
- const ASTContext &Context;
Expr *SizeExpr;
/// The element type of the array.
@@ -3399,8 +3394,8 @@ class DependentSizedExtVectorType : public Type, public llvm::FoldingSetNode {
SourceLocation loc;
- DependentSizedExtVectorType(const ASTContext &Context, QualType ElementType,
- QualType can, Expr *SizeExpr, SourceLocation loc);
+ DependentSizedExtVectorType(QualType ElementType, QualType can,
+ Expr *SizeExpr, SourceLocation loc);
public:
Expr *getSizeExpr() const { return SizeExpr; }
@@ -3414,7 +3409,7 @@ class DependentSizedExtVectorType : public Type, public llvm::FoldingSetNode {
return T->getTypeClass() == DependentSizedExtVector;
}
- void Profile(llvm::FoldingSetNodeID &ID) {
+ void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context) {
Profile(ID, Context, getElementType(), getSizeExpr());
}
@@ -3513,14 +3508,12 @@ class VectorType : public Type, public llvm::FoldingSetNode {
class DependentVectorType : public Type, public llvm::FoldingSetNode {
friend class ASTContext;
- const ASTContext &Context;
QualType ElementType;
Expr *SizeExpr;
SourceLocation Loc;
- DependentVectorType(const ASTContext &Context, QualType ElementType,
- QualType CanonType, Expr *SizeExpr,
- SourceLocation Loc, VectorType::VectorKind vecKind);
+ DependentVectorType(QualType ElementType, QualType CanonType, Expr *SizeExpr,
+ SourceLocation Loc, VectorType::VectorKind vecKind);
public:
Expr *getSizeExpr() const { return SizeExpr; }
@@ -3537,7 +3530,7 @@ class DependentVectorType : public Type, public llvm::FoldingSetNode {
return T->getTypeClass() == DependentVector;
}
- void Profile(llvm::FoldingSetNodeID &ID) {
+ void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context) {
Profile(ID, Context, getElementType(), getSizeExpr(), getVectorKind());
}
@@ -3719,15 +3712,13 @@ class ConstantMatrixType final : public MatrixType {
class DependentSizedMatrixType final : public MatrixType {
friend class ASTContext;
- const ASTContext &Context;
Expr *RowExpr;
Expr *ColumnExpr;
SourceLocation loc;
- DependentSizedMatrixType(const ASTContext &Context, QualType ElementType,
- QualType CanonicalType, Expr *RowExpr,
- Expr *ColumnExpr, SourceLocation loc);
+ DependentSizedMatrixType(QualType ElementType, QualType CanonicalType,
+ Expr *RowExpr, Expr *ColumnExpr, SourceLocation loc);
public:
Expr *getRowExpr() const { return RowExpr; }
@@ -3738,7 +3729,7 @@ class DependentSizedMatrixType final : public MatrixType {
return T->getTypeClass() == DependentSizedMatrix;
}
- void Profile(llvm::FoldingSetNodeID &ID) {
+ void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context) {
Profile(ID, Context, getElementType(), getRowExpr(), getColumnExpr());
}
@@ -4749,15 +4740,12 @@ class TypeOfExprType : public Type {
/// This class is used internally by the ASTContext to manage
/// canonical, dependent types, only. Clients will only see instances
/// of this class via TypeOfExprType nodes.
-class DependentTypeOfExprType
- : public TypeOfExprType, public llvm::FoldingSetNode {
- const ASTContext &Context;
-
+class DependentTypeOfExprType : public TypeOfExprType,
+ public llvm::FoldingSetNode {
public:
- DependentTypeOfExprType(const ASTContext &Context, Expr *E, TypeOfKind Kind)
- : TypeOfExprType(E, Kind), Context(Context) {}
+ DependentTypeOfExprType(Expr *E, TypeOfKind Kind) : TypeOfExprType(E, Kind) {}
- void Profile(llvm::FoldingSetNodeID &ID) {
+ void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context) {
Profile(ID, Context, getUnderlyingExpr(),
getKind() == TypeOfKind::Unqualified);
}
@@ -4833,12 +4821,10 @@ class DecltypeType : public Type {
/// canonical, dependent types, only. Clients will only see instances
/// of this class via DecltypeType nodes.
class DependentDecltypeType : public DecltypeType, public llvm::FoldingSetNode {
- const ASTContext &Context;
-
public:
- DependentDecltypeType(const ASTContext &Context, Expr *E);
+ DependentDecltypeType(Expr *E, QualType UnderlyingTpe);
- void Profile(llvm::FoldingSetNodeID &ID) {
+ void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context) {
Profile(ID, Context, getUnderlyingExpr());
}
@@ -6657,12 +6643,10 @@ class BitIntType final : public Type, public llvm::FoldingSetNode {
class DependentBitIntType final : public Type, public llvm::FoldingSetNode {
friend class ASTContext;
- const ASTContext &Context;
llvm::PointerIntPair<Expr*, 1, bool> ExprAndUnsigned;
protected:
- DependentBitIntType(const ASTContext &Context, bool IsUnsigned,
- Expr *NumBits);
+ DependentBitIntType(bool IsUnsigned, Expr *NumBits);
public:
bool isUnsigned() const;
@@ -6672,7 +6656,7 @@ class DependentBitIntType final : public Type, public llvm::FoldingSetNode {
bool isSugared() const { return false; }
QualType desugar() const { return QualType(this, 0); }
- void Profile(llvm::FoldingSetNodeID &ID) {
+ void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context) {
Profile(ID, Context, isUnsigned(), getNumBitsExpr());
}
static void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context,
diff --git a/clang/lib/AST/ASTContext.cpp b/clang/lib/AST/ASTContext.cpp
index 57aaa05b1d81ddb..93c4baecbe6fd30 100644
--- a/clang/lib/AST/ASTContext.cpp
+++ b/clang/lib/AST/ASTContext.cpp
@@ -925,6 +925,10 @@ ASTContext::ASTContext(LangOptions &LOpts, SourceManager &SM,
IdentifierTable &idents, SelectorTable &sels,
Builtin::Context &builtins, TranslationUnitKind TUKind)
: ConstantArrayTypes(this_(), ConstantArrayTypesLog2InitSize),
+ DependentSizedArrayTypes(this_()), DependentSizedExtVectorTypes(this_()),
+ DependentAddressSpaceTypes(this_()), DependentVectorTypes(this_()),
+ DependentSizedMatrixTypes(this_()), DependentTypeOfExprTypes(this_()),
+ DependentDecltypeTypes(this_()), DependentBitIntTypes(this_()),
FunctionProtoTypes(this_(), FunctionProtoTypesLog2InitSize),
TemplateSpecializationTypes(this_()),
DependentTemplateSpecializationTypes(this_()), AutoTypes(this_()),
@@ -3786,11 +3790,8 @@ QualType ASTContext::getDependentSizedArrayType(QualType elementType,
// initializer. We do no canonicalization here at all, which is okay
// because they can't be used in most locations.
if (!numElements) {
- auto *newType
- = new (*this, TypeAlignment)
- DependentSizedArrayType(*this, elementType, QualType(),
- numElements, ASM, elementTypeQuals,
- brackets);
+ auto *newType = new (*this, TypeAlignment) DependentSizedArrayType(
+ elementType, QualType(), numElements, ASM, elementTypeQuals, brackets);
Types.push_back(newType);
return QualType(newType, 0);
}
@@ -3813,9 +3814,8 @@ QualType ASTContext::getDependentSizedArrayType(QualType elementType,
// If we don't have one, build one.
if (!canonTy) {
canonTy = new (*this, TypeAlignment)
- DependentSizedArrayType(*this, QualType(canonElementType.Ty, 0),
- QualType(), numElements, ASM, elementTypeQuals,
- brackets);
+ DependentSizedArrayType(QualType(canonElementType.Ty, 0), QualType(),
+ numElements, ASM, elementTypeQuals, brackets);
DependentSizedArrayTypes.InsertNode(canonTy, insertPos);
Types.push_back(canonTy);
}
@@ -3832,10 +3832,8 @@ QualType ASTContext::getDependentSizedArrayType(QualType elementType,
// Otherwise, we need to build a type which follows the spelling
// of the element type.
- auto *sugaredType
- = new (*this, TypeAlignment)
- DependentSizedArrayType(*this, elementType, canon, numElements,
- ASM, elementTypeQuals, brackets);
+ auto *sugaredType = new (*this, TypeAlignment) DependentSizedArrayType(
+ elementType, canon, numElements, ASM, elementTypeQuals, brackets);
Types.push_back(sugaredType);
return QualType(sugaredType, 0);
}
@@ -4111,12 +4109,12 @@ ASTContext::getDependentVectorType(QualType VecType, Expr *SizeExpr,
if (Canon) {
New = new (*this, TypeAlignment) DependentVectorType(
- *this, VecType, QualType(Canon, 0), SizeExpr, AttrLoc, VecKind);
+ VecType, QualType(Canon, 0), SizeExpr, AttrLoc, VecKind);
} else {
QualType CanonVecTy = getCanonicalType(VecType);
if (CanonVecTy == VecType) {
- New = new (*this, TypeAlignment) DependentVectorType(
- *this, VecType, QualType(), SizeExpr, AttrLoc, VecKind);
+ New = new (*this, TypeAlignment)
+ DependentVectorType(VecType, QualType(), SizeExpr, AttrLoc, VecKind);
DependentVectorType *CanonCheck =
DependentVectorTypes.FindNodeOrInsertPos(ID, InsertPos);
@@ -4127,8 +4125,8 @@ ASTContext::getDependentVectorType(QualType VecType, Expr *SizeExpr,
} else {
QualType CanonTy = getDependentVectorType(CanonVecTy, SizeExpr,
SourceLocation(), VecKind);
- New = new (*this, TypeAlignment) DependentVectorType(
- *this, VecType, CanonTy, SizeExpr, AttrLoc, VecKind);
+ New = new (*this, TypeAlignment)
+ DependentVectorType(VecType, CanonTy, SizeExpr, AttrLoc, VecKind);
}
}
@@ -4186,15 +4184,13 @@ ASTContext::getDependentSizedExtVectorType(QualType vecType,
if (Canon) {
// We already have a canonical version of this array type; use it as
// the canonical type for a newly-built type.
- New = new (*this, TypeAlignment)
- DependentSizedExtVectorType(*this, vecType, QualType(Canon, 0),
- SizeExpr, AttrLoc);
+ New = new (*this, TypeAlignment) DependentSizedExtVectorType(
+ vecType, QualType(Canon, 0), SizeExpr, AttrLoc);
} else {
QualType CanonVecTy = getCanonicalType(vecType);
if (CanonVecTy == vecType) {
New = new (*this, TypeAlignment)
- DependentSizedExtVectorType(*this, vecType, QualType(), SizeExpr,
- AttrLoc);
+ DependentSizedExtVectorType(vecType, QualType(), SizeExpr, AttrLoc);
DependentSizedExtVectorType *CanonCheck
= DependentSizedExtVectorTypes.FindNodeOrInsertPos(ID, InsertPos);
@@ -4204,8 +4200,8 @@ ASTContext::getDependentSizedExtVectorType(QualType vecType,
} else {
QualType CanonExtTy = getDependentSizedExtVectorType(CanonVecTy, SizeExpr,
SourceLocation());
- New = new (*this, TypeAlignment) DependentSizedExtVectorType(
- *this, vecType, CanonExtTy, SizeExpr, AttrLoc);
+ New = new (*this, TypeAlignment)
+ DependentSizedExtVectorType(vecType, CanonExtTy, SizeExpr, AttrLoc);
}
}
@@ -4260,7 +4256,7 @@ QualType ASTContext::getDependentSizedMatrixType(QualType ElementTy,
if (!Canon) {
Canon = new (*this, TypeAlignment) DependentSizedMatrixType(
- *this, CanonElementTy, QualType(), RowExpr, ColumnExpr, AttrLoc);
+ CanonElementTy, QualType(), RowExpr, ColumnExpr, AttrLoc);
#ifndef NDEBUG
DependentSizedMatrixType *CanonCheck =
DependentSizedMatrixTypes.FindNodeOrInsertPos(ID, InsertPos);
@@ -4279,7 +4275,7 @@ QualType ASTContext::getDependentSizedMatrixType(QualType ElementTy,
// Use Canon as the canonical type for newly-built type.
DependentSizedMatrixType *New = new (*this, TypeAlignment)
- DependentSizedMatrixType(*this, ElementTy, QualType(Canon, 0), RowExpr,
+ DependentSizedMatrixType(ElementTy, QualType(Canon, 0), RowExpr,
ColumnExpr, AttrLoc);
Types.push_back(New);
return QualType(New, 0);
@@ -4301,9 +4297,8 @@ QualType ASTContext::getDependentAddressSpaceType(QualType PointeeType,
DependentAddressSpaceTypes.FindNodeOrInsertPos(ID, insertPos);
if (!canonTy) {
- canonTy = new (*this, TypeAlignment)
- DependentAddressSpaceType(*this, canonPointeeType,
- QualType(), AddrSpaceExpr, AttrLoc);
+ canonTy = new (*this, TypeAlignment) DependentAddressSpaceType(
+ canonPointeeType, QualType(), AddrSpaceExpr, AttrLoc);
DependentAddressSpaceTypes.InsertNode(canonTy, insertPos);
Types.push_back(canonTy);
}
@@ -4312,10 +4307,8 @@ QualType ASTContext::getDependentAddressSpaceType(QualType PointeeType,
canonTy->getAddrSpaceExpr() == AddrSpaceExpr)
return QualType(canonTy, 0);
- auto *sugaredType
- = new (*this, TypeAlignment)
- DependentAddressSpaceType(*this, PointeeType, QualType(canonTy, 0),
- AddrSpaceExpr, AttrLoc);
+ auto *sugaredType = new (*this, TypeAlignment) DependentAddressSpaceType(
+ PointeeType, QualType(canonTy, 0), AddrSpaceExpr, AttrLoc);
Types.push_back(sugaredType);
return QualType(sugaredType, 0);
}
@@ -4619,8 +4612,8 @@ QualType ASTContext::getDependentBitIntType(bool IsUnsigned,
DependentBitIntTypes.FindNodeOrInsertPos(ID, InsertPos))
return QualType(Existing, 0);
- auto *New = new (*this, TypeAlignment)
- DependentBitIntType(*this, IsUnsigned, NumBitsExpr);
+ auto *New =
+ new (*this, TypeAlignment) DependentBitIntType(IsUnsigned, NumBitsExpr);
DependentBitIntTypes.InsertNode(New, InsertPos);
Types.push_back(New);
@@ -5662,8 +5655,7 @@ QualType ASTContext::getTypeOfExprType(Expr *tofExpr, TypeOfKind Kind) const {
TypeOfExprType(tofExpr, Kind, QualType((TypeOfExprType *)Canon, 0));
} else {
// Build a new, canonical typeof(expr) type.
- Canon = new (*this, TypeAlignment)
- DependentTypeOfExprType(*this, tofExpr, Kind);
+ Canon = new (*this, TypeAlignment) DependentTypeOfExprType(tofExpr, Kind);
DependentTypeOfExprTypes.InsertNode(Canon, InsertPos);
toe = Canon;
}
@@ -5731,7 +5723,7 @@ QualType ASTCont...
[truncated]
|
b6f194c
to
5919cbf
Compare
Thank you for the PR. Could you provide some context in the PR description? From the title it sounds like there were prior changes that this is finishing. It would be helpful to have a link to those prior changes. |
Thanks for the review. I think the change stands on itself, as we are avoiding storing one pointer per Type Node instance, for the cost of one extra pointer per ASTContext, which is negligible. These type nodes we are changing here are old, at least as far back as 2008. But we never went back and updated the existing users. This patch does that. |
Thank you for improving the description. The description is usually what ends up in the git log and so it is important for that summary there to be as helpful and descriptive as possible. |
@shafik from the last message, it seemed like this is good to go, but you didn't approve, so I am wondering if you forgot. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, thanks!
With this change, we are avoiding storing one pointer per Type Node instance, for the cost of one extra pointer per ASTContext, which is negligible.
After we introduced
ContextualFoldingSet
a long time ago, we never went back and updated these existing users.This patch corrects that.