@@ -6666,16 +6666,55 @@ QualType ASTContext::getTagDeclType(const TagDecl *Decl) const {
66666666 return getTypeDeclType(const_cast<TagDecl*>(Decl));
66676667}
66686668
6669+ static QualType LookupCGlobalCXXStdNSTypedef(const ASTContext &Ctx,
6670+ StringRef DefName,
6671+ CanQualType const &CanType) {
6672+ DeclContextLookupResult Lookup;
6673+ if (Ctx.getLangOpts().C99) {
6674+ Lookup = Ctx.getTranslationUnitDecl()->lookup(&Ctx.Idents.get(DefName));
6675+ } else if (Ctx.getLangOpts().CPlusPlus) {
6676+ const NamespaceDecl *StdNS = nullptr;
6677+ auto LookupStdNS =
6678+ Ctx.getTranslationUnitDecl()->lookup(&Ctx.Idents.get("std"));
6679+ if (!LookupStdNS.empty()) {
6680+ StdNS = dyn_cast<NamespaceDecl>(LookupStdNS.front());
6681+ }
6682+ if (StdNS) {
6683+ Lookup = StdNS->lookup(&Ctx.Idents.get(DefName));
6684+ } else {
6685+ Lookup = Ctx.getTranslationUnitDecl()->lookup(&Ctx.Idents.get(DefName));
6686+ }
6687+ }
6688+ if (!Lookup.empty()) {
6689+ if (auto *TD = dyn_cast<TypedefNameDecl>(Lookup.front())) {
6690+ auto Result = Ctx.getTypeDeclType(TD);
6691+ if (!Result.isNull() && Ctx.hasSameType(Result, CanType) &&
6692+ Ctx.getTypeAlign(Result) == Ctx.getTypeAlign(CanType)) {
6693+ return Result;
6694+ }
6695+ }
6696+ }
6697+ return QualType();
6698+ }
6699+
66696700/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
66706701/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
66716702/// needs to agree with the definition in <stddef.h>.
6672- CanQualType ASTContext::getSizeType() const {
6673- return getFromTargetType(Target->getSizeType());
6703+ QualType ASTContext::getSizeType() const {
6704+ if (UnsignedSizeTy.isNull()) {
6705+ auto CanType = getFromTargetType(Target->getSizeType());
6706+ if (auto TypeDef = LookupCGlobalCXXStdNSTypedef(*this, "size_t", CanType);
6707+ TypeDef.isNull())
6708+ return CanType;
6709+ else
6710+ UnsignedSizeTy = TypeDef;
6711+ }
6712+ return UnsignedSizeTy;
66746713}
66756714
66766715/// Return the unique signed counterpart of the integer type
66776716/// corresponding to size_t.
6678- CanQualType ASTContext::getSignedSizeType() const {
6717+ QualType ASTContext::getSignedSizeType() const {
66796718 return getFromTargetType(Target->getSignedSizeType());
66806719}
66816720
@@ -6714,7 +6753,16 @@ QualType ASTContext::getUIntPtrType() const {
67146753/// getPointerDiffType - Return the unique type for "ptrdiff_t" (C99 7.17)
67156754/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
67166755QualType ASTContext::getPointerDiffType() const {
6717- return getFromTargetType(Target->getPtrDiffType(LangAS::Default));
6756+ if (PtrDiffTy.isNull()) {
6757+ auto CanType = getFromTargetType(Target->getPtrDiffType(LangAS::Default));
6758+ if (auto TypeDef =
6759+ LookupCGlobalCXXStdNSTypedef(*this, "ptrdiff_t", CanType);
6760+ TypeDef.isNull())
6761+ return CanType;
6762+ else
6763+ PtrDiffTy = TypeDef;
6764+ }
6765+ return PtrDiffTy;
67186766}
67196767
67206768/// Return the unique unsigned counterpart of "ptrdiff_t"
@@ -12556,35 +12604,6 @@ QualType ASTContext::GetBuiltinType(unsigned Id,
1255612604 return getFunctionType(ResType, ArgTypes, EPI);
1255712605}
1255812606
12559- QualType ASTContext::getCGlobalCXXStdNSTypedef(const NamespaceDecl *StdNS,
12560- StringRef DefName,
12561- QualType FallBack) const {
12562- DeclContextLookupResult Lookup;
12563- if (getLangOpts().C99) {
12564- Lookup = getTranslationUnitDecl()->lookup(&Idents.get(DefName));
12565- } else if (getLangOpts().CPlusPlus) {
12566- if (StdNS == nullptr) {
12567- auto LookupStdNS = getTranslationUnitDecl()->lookup(&Idents.get("std"));
12568- if (!LookupStdNS.empty()) {
12569- StdNS = dyn_cast<NamespaceDecl>(LookupStdNS.front());
12570- }
12571- }
12572- if (StdNS) {
12573- Lookup = StdNS->lookup(&Idents.get(DefName));
12574- } else {
12575- Lookup = getTranslationUnitDecl()->lookup(&Idents.get(DefName));
12576- }
12577- }
12578- if (!Lookup.empty()) {
12579- if (auto *TD = dyn_cast<TypedefNameDecl>(Lookup.front())) {
12580- if (auto Result = getTypeDeclType(TD); !Result.isNull()) {
12581- return Result;
12582- }
12583- }
12584- }
12585- return FallBack;
12586- }
12587-
1258812607static GVALinkage basicGVALinkageForFunction(const ASTContext &Context,
1258912608 const FunctionDecl *FD) {
1259012609 if (!FD->isExternallyVisible())
0 commit comments