Skip to content

Commit e8420f0

Browse files
committed
Improved based on the review
1 parent 816fef9 commit e8420f0

File tree

6 files changed

+33
-55
lines changed

6 files changed

+33
-55
lines changed

clang/include/clang/AST/ASTContext.h

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,6 @@ class ASTContext : public RefCountedBase<ASTContext> {
258258
mutable llvm::FoldingSet<BitIntType> BitIntTypes;
259259
mutable llvm::ContextualFoldingSet<DependentBitIntType, ASTContext &>
260260
DependentBitIntTypes;
261-
mutable llvm::FoldingSet<PredefinedSugarType> PredefinedSugarTypes;
262261
mutable llvm::FoldingSet<BTFTagAttributedType> BTFTagAttributedTypes;
263262
llvm::FoldingSet<HLSLAttributedResourceType> HLSLAttributedResourceTypes;
264263
llvm::FoldingSet<HLSLInlineSpirvType> HLSLInlineSpirvTypes;
@@ -278,6 +277,11 @@ class ASTContext : public RefCountedBase<ASTContext> {
278277
mutable llvm::ContextualFoldingSet<ArrayParameterType, ASTContext &>
279278
ArrayParameterTypes;
280279

280+
/// Store the unique Type corresponding to each Kind.
281+
mutable std::array<Type *,
282+
llvm::to_underlying(PredefinedSugarType::Kind::Max)>
283+
PredefinedSugarTypes;
284+
281285
/// The set of nested name specifiers.
282286
///
283287
/// This set is managed by the NestedNameSpecifier class.
@@ -1993,12 +1997,6 @@ class ASTContext : public RefCountedBase<ASTContext> {
19931997
bool IsDependent,
19941998
QualType Canon) const;
19951999

1996-
// The core language uses these types as the result types of some expressions,
1997-
// which are typically standard integer types and consistent with it's
1998-
// typedefs (if any).
1999-
QualType SizeType; // __size_t
2000-
QualType SignedSizeType; // __signed_size_t
2001-
QualType PtrdiffType; // __ptrdiff_t
20022000
public:
20032001
/// Return the unique reference to the type for the specified TagDecl
20042002
/// (struct/union/class/enum) decl.

clang/include/clang/AST/Type.h

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2266,7 +2266,10 @@ class alignas(TypeAlignment) Type : public ExtQualsTypeCommonBase {
22662266
SignedSizeT,
22672267

22682268
/// The "ptrdiff_t" type.
2269-
PtrdiffT
2269+
PtrdiffT,
2270+
2271+
// Indicates how many items the enum has.
2272+
Max
22702273
};
22712274

22722275
class PresefinedSugarTypeBitfields {
@@ -8060,7 +8063,7 @@ class DependentBitIntType final : public Type, public llvm::FoldingSetNode {
80608063
}
80618064
};
80628065

8063-
class PredefinedSugarType final : public Type, public llvm::FoldingSetNode {
8066+
class PredefinedSugarType final : public Type {
80648067
public:
80658068
friend class ASTContext;
80668069
using Kind = PredefinedSugarKind;
@@ -8089,14 +8092,6 @@ class PredefinedSugarType final : public Type, public llvm::FoldingSetNode {
80898092
static bool classof(const Type *T) {
80908093
return T->getTypeClass() == PredefinedSugar;
80918094
}
8092-
8093-
void Profile(llvm::FoldingSetNodeID &ID) const {
8094-
Profile(ID, llvm::to_underlying(getKind()));
8095-
}
8096-
8097-
static void Profile(llvm::FoldingSetNodeID &ID, unsigned KD) {
8098-
ID.AddInteger(KD);
8099-
}
81008095
};
81018096

81028097
/// A qualifier set is used to build a set of qualifiers.

clang/lib/AST/ASTContext.cpp

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1516,16 +1516,6 @@ void ASTContext::InitBuiltinTypes(const TargetInfo &Target,
15161516
MSGuidTagDecl = buildImplicitRecord("_GUID");
15171517
getTranslationUnitDecl()->addDecl(MSGuidTagDecl);
15181518
}
1519-
1520-
// size_t (C99TC3 6.5.3.4), signed size_t (C++23 5.13.2) and
1521-
// ptrdiff_t (C99TC3 6.5.6) Although these types are not built-in, they are
1522-
// part of the core language and are widely used.
1523-
// Using PredefinedSugarType makes these types as named sugar types rather
1524-
// than standard integer types, enabling better hints and diagnostics.
1525-
using Kind = PredefinedSugarType::Kind;
1526-
SizeType = getPredefinedSugarType(Kind::SizeT);
1527-
SignedSizeType = getPredefinedSugarType(Kind::SignedSizeT);
1528-
PtrdiffType = getPredefinedSugarType(Kind::PtrdiffT);
15291519
}
15301520

15311521
DiagnosticsEngine &ASTContext::getDiagnostics() const {
@@ -5232,24 +5222,27 @@ QualType ASTContext::getDependentBitIntType(bool IsUnsigned,
52325222

52335223
QualType
52345224
ASTContext::getPredefinedSugarType(PredefinedSugarType::Kind KD) const {
5235-
llvm::FoldingSetNodeID ID;
5236-
PredefinedSugarType::Profile(ID, llvm::to_underlying(KD));
52375225

5238-
void *InsertPos = nullptr;
5239-
if (PredefinedSugarType *Existing =
5240-
PredefinedSugarTypes.FindNodeOrInsertPos(ID, InsertPos))
5241-
return QualType(Existing, 0);
5226+
auto Target = PredefinedSugarTypes[llvm::to_underlying(KD)];
5227+
if (Target != nullptr)
5228+
return QualType(Target, 0);
52425229

52435230
using Kind = PredefinedSugarType::Kind;
52445231

52455232
auto getCanonicalType = [](const ASTContext &Ctx, Kind KDI) -> QualType {
52465233
switch (KDI) {
5234+
// size_t (C99TC3 6.5.3.4), signed size_t (C++23 5.13.2) and
5235+
// ptrdiff_t (C99TC3 6.5.6) Although these types are not built-in, they
5236+
// are part of the core language and are widely used. Using
5237+
// PredefinedSugarType makes these types as named sugar types rather than
5238+
// standard integer types, enabling better hints and diagnostics.
52475239
case Kind::SizeT:
52485240
return Ctx.getFromTargetType(Ctx.Target->getSizeType());
52495241
case Kind::SignedSizeT:
52505242
return Ctx.getFromTargetType(Ctx.Target->getSignedSizeType());
52515243
case Kind::PtrdiffT:
52525244
return Ctx.getFromTargetType(Ctx.Target->getPtrDiffType(LangAS::Default));
5245+
case Kind::Max:;
52535246
}
52545247
llvm_unreachable("unexpected kind");
52555248
};
@@ -5258,7 +5251,7 @@ ASTContext::getPredefinedSugarType(PredefinedSugarType::Kind KD) const {
52585251
PredefinedSugarType(KD, &Idents.get(PredefinedSugarType::getName(KD)),
52595252
getCanonicalType(*this, static_cast<Kind>(KD)));
52605253
Types.push_back(New);
5261-
PredefinedSugarTypes.InsertNode(New, InsertPos);
5254+
Target = New;
52625255
return QualType(New, 0);
52635256
}
52645257

@@ -6842,19 +6835,25 @@ QualType ASTContext::getTagDeclType(const TagDecl *Decl) const {
68426835
/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
68436836
/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
68446837
/// needs to agree with the definition in <stddef.h>.
6845-
QualType ASTContext::getSizeType() const { return SizeType; }
6838+
QualType ASTContext::getSizeType() const {
6839+
return getPredefinedSugarType(PredefinedSugarType::Kind::SizeT);
6840+
}
68466841

68476842
CanQualType ASTContext::getCanonicalSizeType() const {
68486843
return getFromTargetType(Target->getSizeType());
68496844
}
68506845

68516846
/// Return the unique signed counterpart of the integer type
68526847
/// corresponding to size_t.
6853-
QualType ASTContext::getSignedSizeType() const { return SignedSizeType; }
6848+
QualType ASTContext::getSignedSizeType() const {
6849+
return getPredefinedSugarType(PredefinedSugarType::Kind::SignedSizeT);
6850+
}
68546851

68556852
/// getPointerDiffType - Return the unique type for "ptrdiff_t" (C99 7.17)
68566853
/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
6857-
QualType ASTContext::getPointerDiffType() const { return PtrdiffType; }
6854+
QualType ASTContext::getPointerDiffType() const {
6855+
return getPredefinedSugarType(PredefinedSugarType::Kind::PtrdiffT);
6856+
}
68586857

68596858
/// Return the unique unsigned counterpart of "ptrdiff_t"
68606859
/// integer type. The standard (C11 7.21.6.1p7) refers to this type

clang/lib/AST/FormatString.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,8 @@ static bool namedTypeToLengthModifierKind(ASTContext &Ctx, QualType QT,
362362
case Kind::PtrdiffT:
363363
K = LengthModifier::AsPtrDiff;
364364
return true;
365+
case Kind::Max:
366+
llvm_unreachable("unexpected kind");
365367
}
366368
}
367369
return false;

clang/lib/AST/Type.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5631,6 +5631,7 @@ StringRef PredefinedSugarType::getName(Kind KD) {
56315631
return "__signed_size_t";
56325632
case Kind::PtrdiffT:
56335633
return "__ptrdiff_t";
5634+
case Kind::Max:;
56345635
}
56355636
llvm_unreachable("unexpected kind");
56365637
}

clang/lib/Sema/TreeTransform.h

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1278,8 +1278,6 @@ class TreeTransform {
12781278
QualType RebuildDependentBitIntType(bool IsUnsigned, Expr *NumBitsExpr,
12791279
SourceLocation Loc);
12801280

1281-
QualType RebuildPredefinedSugarType(PredefinedSugarType::Kind K);
1282-
12831281
/// Build a new template name given a nested name specifier, a flag
12841282
/// indicating whether the "template" keyword was provided, and the template
12851283
/// that the template name refers to.
@@ -7253,16 +7251,7 @@ QualType TreeTransform<Derived>::TransformDependentBitIntType(
72537251
template <typename Derived>
72547252
QualType TreeTransform<Derived>::TransformPredefinedSugarType(
72557253
TypeLocBuilder &TLB, PredefinedSugarTypeLoc TL) {
7256-
const PredefinedSugarType *EIT = TL.getTypePtr();
7257-
QualType Result = TL.getType();
7258-
7259-
if (getDerived().AlwaysRebuild()) {
7260-
Result = getDerived().RebuildPredefinedSugarType(EIT->getKind());
7261-
}
7262-
7263-
PredefinedSugarTypeLoc NewTL = TLB.push<PredefinedSugarTypeLoc>(Result);
7264-
NewTL.setNameLoc(TL.getNameLoc());
7265-
return Result;
7254+
llvm_unreachable("This type does not need to be transformed.");
72667255
}
72677256

72687257
/// Simple iterator that traverses the template arguments in a
@@ -17446,12 +17435,6 @@ QualType TreeTransform<Derived>::RebuildDependentBitIntType(
1744617435
return SemaRef.BuildBitIntType(IsUnsigned, NumBitsExpr, Loc);
1744717436
}
1744817437

17449-
template <typename Derived>
17450-
QualType TreeTransform<Derived>::RebuildPredefinedSugarType(
17451-
PredefinedSugarType::Kind K) {
17452-
return SemaRef.Context.getPredefinedSugarType(K);
17453-
}
17454-
1745517438
template<typename Derived>
1745617439
TemplateName
1745717440
TreeTransform<Derived>::RebuildTemplateName(CXXScopeSpec &SS,

0 commit comments

Comments
 (0)