@@ -1516,6 +1516,24 @@ 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+ if (!LangOpts.HLSL) {
1524+ // Using PredefinedSugarType makes these types as named sugar types rather
1525+ // than standard integer types, enabling better hints and diagnostics.
1526+ SizeType = getPredefinedSugarType(
1527+ llvm::to_underlying(Type::PredefinedSugarKind::SizeT));
1528+ SignedSizeType = getPredefinedSugarType(
1529+ llvm::to_underlying(Type::PredefinedSugarKind::SignedSizeT));
1530+ PtrdiffType = getPredefinedSugarType(
1531+ llvm::to_underlying(Type::PredefinedSugarKind::PtrdiffT));
1532+ } else {
1533+ SizeType = getFromTargetType(Target.getSizeType());
1534+ SignedSizeType = getFromTargetType(Target.getSignedSizeType());
1535+ PtrdiffType = getFromTargetType(Target.getPtrDiffType(LangAS::Default));
1536+ }
15191537}
15201538
15211539DiagnosticsEngine &ASTContext::getDiagnostics() const {
@@ -6750,61 +6768,22 @@ QualType ASTContext::getTagDeclType(const TagDecl *Decl) const {
67506768 return getTypeDeclType(const_cast<TagDecl*>(Decl));
67516769}
67526770
6753- // Using PredefinedSugarType makes size_t, signed size_t, and ptrdiff_t behave
6754- // as named sugar types rather than built-in types, enabling better hints and
6755- // diagnostics. In C and C++, expressions of type size_t can be obtained via the
6756- // sizeof operator, expressions of type ptrdiff_t via pointer subtraction, and
6757- // expressions of type signed size_t via the z literal suffix (since C++23).
6758- // However, no core language mechanism directly produces an expression of type
6759- // unsigned ptrdiff_t. The unsigned ptrdiff_t type is solely required by format
6760- // specifiers for printf and scanf. Consequently, no expression's type needs to
6761- // be displayed as unsigned ptrdiff_t. Verification of whether a type is
6762- // unsigned ptrdiff_t is also unnecessary, as no corresponding typedefs exist.
6763- // Therefore, unsigned ptrdiff_t does not need to do that.
6764-
67656771/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
67666772/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
67676773/// needs to agree with the definition in <stddef.h>.
6768- QualType ASTContext::getSizeType() const {
6769- if (SizeType.isNull()) {
6770- if (!getLangOpts().HLSL)
6771- SizeType = getPredefinedSugarType(
6772- llvm::to_underlying(Type::PredefinedSugarKind::SizeT));
6773- else
6774- SizeType = getFromTargetType(Target->getSizeType());
6775- }
6776- return SizeType;
6777- }
6774+ QualType ASTContext::getSizeType() const { return SizeType; }
67786775
67796776CanQualType ASTContext::getCanonicalSizeType() const {
67806777 return getFromTargetType(Target->getSizeType());
67816778}
67826779
67836780/// Return the unique signed counterpart of the integer type
67846781/// corresponding to size_t.
6785- QualType ASTContext::getSignedSizeType() const {
6786- if (SignedSizeType.isNull()) {
6787- if (!getLangOpts().HLSL)
6788- SignedSizeType = getPredefinedSugarType(
6789- llvm::to_underlying(Type::PredefinedSugarKind::SignedSizeT));
6790- else
6791- SignedSizeType = getFromTargetType(Target->getSignedSizeType());
6792- }
6793- return SignedSizeType;
6794- }
6782+ QualType ASTContext::getSignedSizeType() const { return SignedSizeType; }
67956783
67966784/// getPointerDiffType - Return the unique type for "ptrdiff_t" (C99 7.17)
67976785/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
6798- QualType ASTContext::getPointerDiffType() const {
6799- if (PtrdiffType.isNull()) {
6800- if (!getLangOpts().HLSL)
6801- PtrdiffType = getPredefinedSugarType(
6802- llvm::to_underlying(Type::PredefinedSugarKind::PtrdiffT));
6803- else
6804- PtrdiffType = getFromTargetType(Target->getPtrDiffType(LangAS::Default));
6805- }
6806- return PtrdiffType;
6807- }
6786+ QualType ASTContext::getPointerDiffType() const { return PtrdiffType; }
68086787
68096788/// Return the unique unsigned counterpart of "ptrdiff_t"
68106789/// integer type. The standard (C11 7.21.6.1p7) refers to this type
0 commit comments