From 8db87547beb337bee12941f379473f065adeba88 Mon Sep 17 00:00:00 2001 From: Chuanqi Xu Date: Fri, 8 Jul 2022 14:32:33 +0800 Subject: [PATCH] [NFC] Move isSameDefaultTemplateArgument into ASTContext Move isSameDefaultTemplateArgument into ASTContext to keep consistent with other ASTContext:isSame* methods. --- clang/include/clang/AST/ASTContext.h | 5 +++ clang/lib/AST/ASTContext.cpp | 39 +++++++++++++++++++++ clang/lib/Sema/SemaTemplate.cpp | 51 ++++------------------------ 3 files changed, 50 insertions(+), 45 deletions(-) diff --git a/clang/include/clang/AST/ASTContext.h b/clang/include/clang/AST/ASTContext.h index 92293622cc3d5..87b5a6053f1f2 100644 --- a/clang/include/clang/AST/ASTContext.h +++ b/clang/include/clang/AST/ASTContext.h @@ -2664,6 +2664,11 @@ class ASTContext : public RefCountedBase { /// that they may be used in declarations of the same template. bool isSameTemplateParameter(const NamedDecl *X, const NamedDecl *Y) const; + /// Determine whether two default template arguments are similar enough + /// that they may be used in declarations of the same template. + bool isSameDefaultTemplateArgument(const NamedDecl *X, + const NamedDecl *Y) const; + /// Retrieve the "canonical" template argument. /// /// The canonical template argument is the simplest template argument diff --git a/clang/lib/AST/ASTContext.cpp b/clang/lib/AST/ASTContext.cpp index aced0ab39acef..748fa6ebf1d6c 100644 --- a/clang/lib/AST/ASTContext.cpp +++ b/clang/lib/AST/ASTContext.cpp @@ -6294,6 +6294,45 @@ bool ASTContext::isSameTemplateParameterList( return true; } +bool ASTContext::isSameDefaultTemplateArgument(const NamedDecl *X, + const NamedDecl *Y) const { + // If the type parameter isn't the same already, we don't need to check the + // default argument further. + if (!isSameTemplateParameter(X, Y)) + return false; + + if (auto *TTPX = dyn_cast(X)) { + auto *TTPY = cast(Y); + if (!TTPX->hasDefaultArgument() || !TTPY->hasDefaultArgument()) + return false; + + return hasSameType(TTPX->getDefaultArgument(), TTPY->getDefaultArgument()); + } + + if (auto *NTTPX = dyn_cast(X)) { + auto *NTTPY = cast(Y); + if (!NTTPX->hasDefaultArgument() || !NTTPY->hasDefaultArgument()) + return false; + + Expr *DefaultArgumentX = NTTPX->getDefaultArgument()->IgnoreImpCasts(); + Expr *DefaultArgumentY = NTTPY->getDefaultArgument()->IgnoreImpCasts(); + llvm::FoldingSetNodeID XID, YID; + DefaultArgumentX->Profile(XID, *this, /*Canonical=*/true); + DefaultArgumentY->Profile(YID, *this, /*Canonical=*/true); + return XID == YID; + } + + auto *TTPX = cast(X); + auto *TTPY = cast(Y); + + if (!TTPX->hasDefaultArgument() || !TTPY->hasDefaultArgument()) + return false; + + const TemplateArgument &TAX = TTPX->getDefaultArgument().getArgument(); + const TemplateArgument &TAY = TTPY->getDefaultArgument().getArgument(); + return hasSameTemplateName(TAX.getAsTemplate(), TAY.getAsTemplate()); +} + static NamespaceDecl *getNamespace(const NestedNameSpecifier *X) { if (auto *NS = X->getAsNamespace()) return NS; diff --git a/clang/lib/Sema/SemaTemplate.cpp b/clang/lib/Sema/SemaTemplate.cpp index 8e6a810d95df0..67cf8f0371c5c 100644 --- a/clang/lib/Sema/SemaTemplate.cpp +++ b/clang/lib/Sema/SemaTemplate.cpp @@ -2643,46 +2643,6 @@ static bool DiagnoseUnexpandedParameterPacks(Sema &S, return false; } -static bool hasSameDefaultTemplateArgument(NamedDecl *X, NamedDecl *Y) { - ASTContext &C = X->getASTContext(); - // If the type parameter isn't the same already, we don't need to check the - // default argument further. - if (!C.isSameTemplateParameter(X, Y)) - return false; - - if (auto *TTPX = dyn_cast(X)) { - auto *TTPY = cast(Y); - if (!TTPX->hasDefaultArgument() || !TTPY->hasDefaultArgument()) - return false; - - return C.hasSameType(TTPX->getDefaultArgument(), - TTPY->getDefaultArgument()); - } - - if (auto *NTTPX = dyn_cast(X)) { - auto *NTTPY = cast(Y); - if (!NTTPX->hasDefaultArgument() || !NTTPY->hasDefaultArgument()) - return false; - - Expr *DefaultArgumentX = NTTPX->getDefaultArgument()->IgnoreImpCasts(); - Expr *DefaultArgumentY = NTTPY->getDefaultArgument()->IgnoreImpCasts(); - llvm::FoldingSetNodeID XID, YID; - DefaultArgumentX->Profile(XID, C, /*Canonical=*/true); - DefaultArgumentY->Profile(YID, C, /*Canonical=*/true); - return XID == YID; - } - - auto *TTPX = cast(X); - auto *TTPY = cast(Y); - - if (!TTPX->hasDefaultArgument() || !TTPY->hasDefaultArgument()) - return false; - - const TemplateArgument &TAX = TTPX->getDefaultArgument().getArgument(); - const TemplateArgument &TAY = TTPY->getDefaultArgument().getArgument(); - return C.hasSameTemplateName(TAX.getAsTemplate(), TAY.getAsTemplate()); -} - /// Checks the validity of a template parameter list, possibly /// considering the template parameter list from a previous /// declaration. @@ -2780,7 +2740,8 @@ bool Sema::CheckTemplateParameterList(TemplateParameterList *NewParams, if (!OldTypeParm->getOwningModule() || isModuleUnitOfCurrentTU(OldTypeParm->getOwningModule())) RedundantDefaultArg = true; - else if (!hasSameDefaultTemplateArgument(OldTypeParm, NewTypeParm)) { + else if (!getASTContext().isSameDefaultTemplateArgument(OldTypeParm, + NewTypeParm)) { InconsistentDefaultArg = true; PrevModuleName = OldTypeParm->getImportedOwningModule()->getFullModuleName(); @@ -2832,8 +2793,8 @@ bool Sema::CheckTemplateParameterList(TemplateParameterList *NewParams, if (!OldNonTypeParm->getOwningModule() || isModuleUnitOfCurrentTU(OldNonTypeParm->getOwningModule())) RedundantDefaultArg = true; - else if (!hasSameDefaultTemplateArgument(OldNonTypeParm, - NewNonTypeParm)) { + else if (!getASTContext().isSameDefaultTemplateArgument( + OldNonTypeParm, NewNonTypeParm)) { InconsistentDefaultArg = true; PrevModuleName = OldNonTypeParm->getImportedOwningModule()->getFullModuleName(); @@ -2884,8 +2845,8 @@ bool Sema::CheckTemplateParameterList(TemplateParameterList *NewParams, if (!OldTemplateParm->getOwningModule() || isModuleUnitOfCurrentTU(OldTemplateParm->getOwningModule())) RedundantDefaultArg = true; - else if (!hasSameDefaultTemplateArgument(OldTemplateParm, - NewTemplateParm)) { + else if (!getASTContext().isSameDefaultTemplateArgument( + OldTemplateParm, NewTemplateParm)) { InconsistentDefaultArg = true; PrevModuleName = OldTemplateParm->getImportedOwningModule()->getFullModuleName();