Lines Matching +full:fetch +full:- +full:depth

1 //===------- SemaTemplate.cpp - Semantic Analysis for C++ Templates -------===//
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //===----------------------------------------------------------------------===//
9 //===----------------------------------------------------------------------===//
52 return SourceRange(Ps[0]->getTemplateLoc(), Ps[N-1]->getRAngleLoc());
56 unsigned Depth = 0;
59 // depth.
60 for (Scope *TempParamScope = S->getTemplateParamParent(); TempParamScope;
61 TempParamScope = TempParamScope->getParent()->getTemplateParamParent()) {
62 ++Depth;
65 // Note that there are template parameters with the given depth.
66 auto ParamsAtDepth = [&](unsigned D) { Depth = std::max(Depth, D + 1); };
72 if (!LSI->TemplateParams.empty()) {
73 ParamsAtDepth(LSI->AutoTemplateParameterDepth);
76 if (LSI->GLTemplateParameterList) {
77 ParamsAtDepth(LSI->GLTemplateParameterList->getDepth());
93 return Depth;
105 D = D->getUnderlyingDecl();
116 // Like normal (non-template) classes, class templates have an
117 // injected-class-name (Clause 9). The injected-class-name
118 // can be used with or without a template-argument-list. When
119 // it is used without a template-argument-list, it is
120 // equivalent to the injected-class-name followed by the
121 // template-parameters of the class template enclosed in
122 // <>. When it is used with a template-argument-list, it
126 if (Record->isInjectedClassName()) {
127 Record = cast<CXXRecordDecl>(Record->getDeclContext());
128 if (Record->getDescribedClassTemplate())
129 return Record->getDescribedClassTemplate();
132 return Spec->getSpecializedTemplate();
140 // injected-class-name).
167 isa<FunctionDecl>((*I)->getUnderlyingDecl()))
233 // If we got an ambiguity involving a non-function template, treat this
262 // or we have a non-empty set of results R containing either one template name
268 unsigned ResultCount = R.end() - R.begin();
284 // We don't yet know whether this is a template-name or not.
292 assert(!FoundUsingShadow || FoundUsingShadow->getTargetDecl() == TD);
337 // We only treat template-names that name type templates as valid deduction
360 if (!SS || !SS->isSet() || !isDependentScopeSpecifier(*SS) ||
366 NestedNameSpecifier *Qualifier = (NestedNameSpecifier*)SS->getScopeRep();
393 // This nested-name-specifier occurs in a member access expression, e.g.,
394 // x->B::f, and we are looking into the type of the object.
397 IsDependent = !LookupCtx && ObjectType->isDependentType();
398 assert((IsDependent || !ObjectType->isIncompleteType() ||
399 !ObjectType->getAs<TagType>() ||
400 ObjectType->castAs<TagType>()->isBeingDefined()) &&
403 // Template names cannot appear inside an Objective-C class or object type
415 // for lookups into Objective-C class and object types, where the same
417 if (ObjectType->isObjCObjectOrInterfaceType() ||
418 ObjectType->isVectorType()) {
423 // This nested-name-specifier occurs after another nested-name-specifier,
424 // so long into the context associated with the prior nested-name-specifier.
439 // nested-name-specifier.
444 // Clang, we treat a name after a . or -> as a template-name if lookup
445 // finds a non-dependent member or member of the current instantiation that
447 // of the postfix-expression finds a type template. In the latter case, the
455 // In a class member access expression (5.2.5), if the . or -> token is
458 // beginning of a template argument list (14.2) or a less-than operator.
461 // the context of the entire postfix-expression and shall name a class
467 // FIXME: We should filter out all non-type templates here, particularly
484 // unqualified-id followed by a < and name lookup finds either one or more
489 // successfully form a call to an undeclared template-id.
492 return isa<FunctionDecl>(ND->getUnderlyingDecl());
497 // template-id.
550 // If a 'template' keyword was used, a lookup that finds only non-template
557 Diag(ExampleLookupResult->getUnderlyingDecl()->getLocation(),
571 // postfix-expression and [...]
584 // - if the name is not found, the name found in the class of the
589 // - if the name is found in the context of the entire
590 // postfix-expression and does not name a class template, the name
594 // - if the name found is a class template, it must refer to the same
596 // otherwise the program is ill-formed.
598 getAsTemplateNameDecl(Found.getFoundDecl())->getCanonicalDecl() !=
599 OuterTemplate->getCanonicalDecl()) {
604 Diag(Found.getRepresentativeDecl()->getLocation(),
607 Diag(FoundOuter.getFoundDecl()->getLocation(),
635 NameInfo = DRE->getNameInfo();
636 SS.Adopt(DRE->getQualifierLoc());
638 Found = DRE->getFoundDecl();
640 NameInfo = ME->getMemberNameInfo();
641 SS.Adopt(ME->getQualifierLoc());
643 LookupCtx = ME->getBase()->getType()->getAsCXXRecordDecl();
644 Found = ME->getMemberDecl();
647 NameInfo = DSDRE->getNameInfo();
648 SS.Adopt(DSDRE->getQualifierLoc());
652 NameInfo = DSME->getMemberNameInfo();
653 SS.Adopt(DSME->getQualifierLoc());
659 // If this is a dependent-scope lookup, diagnose that the 'template' keyword
709 Diag(Found->getLocation(),
718 Diag(Found->getLocation(), diag::note_non_template_in_template_id_found);
729 // (e.g. a dependent conversion-function-id, or operator= once we support
755 TemplateArgs ? TemplateArgs->getRAngleLoc() : NameInfo.getEndLoc(), {});
774 IsEntityBeingDefined = TD->isBeingDefined();
791 if (!Complain || (PatternDef && PatternDef->isInvalidDecl()))
804 Instantiation->setInvalidDecl();
809 << /*member function*/ 1 << Instantiation->getDeclName()
810 << Instantiation->getDeclContext();
811 Diag(Pattern->getLocation(), diag::note_explicit_instantiation_here);
817 Diag(Pattern->getLocation(), diag::note_member_declared_at);
824 Diag(Pattern->getLocation(), diag::note_explicit_instantiation_here);
836 Instantiation->setInvalidDecl();
840 << /*static data member*/ 2 << Instantiation->getDeclName()
841 << Instantiation->getDeclContext();
842 Diag(Pattern->getLocation(), diag::note_explicit_instantiation_here);
848 // explicit declaration -> explicit definition conversion can't handle
851 Instantiation->setInvalidDecl();
857 assert(PrevDecl->isTemplateParameter() && "Not a template parameter");
860 // The name of a template-parameter shall not be bound to any following.
862 // template-parameter belongs.
866 // true, in which case it is a default-to-error warning.
873 Diag(Loc, DiagId) << ND->getDeclName();
879 D = Temp->getTemplatedDecl();
943 if (PrevDecl && PrevDecl->isTemplateParameter())
957 TypeLoc TL = TInfo->getTypeLoc();
971 TemplateName Name = DTST.getTypePtr()->getTemplateName();
981 // argument is an injected-class-name for a template, it has a dual nature
986 TInfo->getTypeLoc().getBeginLoc());
994 unsigned Depth, unsigned Position,
998 assert(S->isTemplateParamScope() &&
1004 KeyLoc, ParamNameLoc, Depth, Position,
1007 Param->setAccess(AS_public);
1009 if (Param->isParameterPack())
1011 CSI->LocalPacks.push_back(Param);
1017 S->AddDecl(Param);
1022 // A default template-argument may be specified for any kind of
1023 // template-parameter that is not a template parameter pack.
1043 Param->setInvalidDecl();
1047 Param->setDefaultArgument(
1048 Context, TemplateArgumentLoc(DefaultTInfo->getType(), DefaultTInfo));
1067 TemplateName TN = TypeConstr->Template.get();
1071 // [...] The concept designated by a type-constraint shall be a type
1073 if (!CD->isTypeConcept()) {
1074 Diag(TypeConstr->TemplateNameLoc,
1079 if (CheckConceptUseInDefinition(CD, TypeConstr->TemplateNameLoc))
1082 bool WereArgsSpecified = TypeConstr->LAngleLoc.isValid();
1085 CD->getTemplateParameters()->getMinRequiredArguments() > 1) {
1086 Diag(TypeConstr->TemplateNameLoc,
1111 TemplateName TN = TypeConstr->Template.get();
1115 DeclarationNameInfo ConceptName(DeclarationName(TypeConstr->Name),
1116 TypeConstr->TemplateNameLoc);
1119 if (TypeConstr->LAngleLoc.isValid()) {
1133 TypeConstr->LAngleLoc.isValid() ? &TemplateArgs : nullptr,
1156 // [...] This constraint-expression E is called the immediately-declared
1205 TemplateArgs ? TemplateArgs->getLAngleLoc() : SourceLocation(),
1206 TemplateArgs ? TemplateArgs->getRAngleLoc() : SourceLocation(),
1207 ParamAsArgument, ConstrainedParameter->getLocation(),
1210 for (const auto &ArgLoc : TemplateArgs->arguments())
1223 ConstrainedParameter->setTypeConstraint(CL,
1232 if (NewConstrainedParm->getType().getNonPackExpansionType() != TL.getType() ||
1234 Diag(NewConstrainedParm->getTypeSourceInfo()->getTypeLoc().getBeginLoc(),
1236 << NewConstrainedParm->getTypeSourceInfo()
1237 ->getTypeLoc()
1244 BuildDeclRefExpr(OrigConstrainedParm, OrigConstrainedParm->getType(),
1245 VK_PRValue, OrigConstrainedParm->getLocation());
1252 OrigConstrainedParm->getLocation(),
1262 NewConstrainedParm->setPlaceholderTypeConstraint(
1269 if (TSI->getType()->isUndeducedType()) {
1271 // An id-expression is type-dependent if it contains
1272 // - an identifier associated by name lookup with a non-type
1273 // template-parameter declared with a type that contains a
1278 return CheckNonTypeTemplateParameterType(TSI->getType(), Loc);
1282 if (T->isDependentType())
1288 if (T->isStructuralType())
1292 if (T->isRValueReferenceType()) {
1298 // there's not much more we can say about non-scalar non-class types --
1302 (!T->isScalarType() && !T->isRecordType())) {
1313 // Drill down into the reason why the class is non-structural.
1314 while (const CXXRecordDecl *RD = T->getAsCXXRecordDecl()) {
1315 // All members are required to be public and non-mutable, and can't be of
1318 for (const FieldDecl *FD : RD->fields()) {
1319 if (FD->getAccess() != AS_public) {
1320 Diag(FD->getLocation(), diag::note_not_structural_non_public) << T << 0;
1323 if (FD->isMutable()) {
1324 Diag(FD->getLocation(), diag::note_not_structural_mutable_field) << T;
1327 if (FD->getType()->isRValueReferenceType()) {
1328 Diag(FD->getLocation(), diag::note_not_structural_rvalue_ref_field)
1335 for (const auto &BaseSpec : RD->bases()) {
1346 int Kind = -1;
1348 for (const FieldDecl *FD : RD->fields()) {
1349 QualType T = Context.getBaseElementType(FD->getType());
1350 if (!T->isStructuralType()) {
1351 SubLoc = FD->getLocation();
1358 if (Kind == -1) {
1359 for (const auto &BaseSpec : RD->bases()) {
1361 if (!T->isStructuralType()) {
1370 assert(Kind != -1 && "couldn't find reason why type is not structural");
1374 RD = T->getAsCXXRecordDecl();
1382 // We don't allow variably-modified types as the type of non-type template
1384 if (T->isVariablyModifiedType()) {
1392 // A non-type template-parameter shall have one of the following
1393 // (optionally cv-qualified) types:
1395 // -- integral or enumeration type,
1396 if (T->isIntegralOrEnumerationType() ||
1397 // -- pointer to object or pointer to function,
1398 T->isPointerType() ||
1399 // -- lvalue reference to object or lvalue reference to function,
1400 T->isLValueReferenceType() ||
1401 // -- pointer to member,
1402 T->isMemberPointerType() ||
1403 // -- std::nullptr_t, or
1404 T->isNullPtrType() ||
1405 // -- a type that contains a placeholder type.
1406 T->isUndeducedType()) {
1407 // C++ [temp.param]p5: The top-level cv-qualifiers on the template-parameter
1414 // A non-type template-parameter of type "array of T" or
1417 if (T->isArrayType() || T->isFunctionType())
1421 // assume that it is well-formed. Note that stripping off the
1426 if (T->isDependentType())
1430 // -- a structural type
1447 unsigned Depth,
1453 // Check that we have valid decl-specifiers specified.
1457 // template-parameter:
1459 // parameter-declaration
1461 // ... A storage class shall not be specified in a template-parameter
1464 // The typedef specifier [...] shall not be used in the decl-specifier-seq
1465 // of a parameter-declaration
1493 // Function-specifiers can be used only in function declarations.
1507 if (const auto *T = TInfo->getType()->getContainedDeducedType())
1511 << QualType(TInfo->getType()->getContainedAutoType(), 0);
1513 assert(S->isTemplateParamScope() &&
1514 "Non-type template parameter not in template parameter scope!");
1529 D.getIdentifierLoc(), Depth, Position, ParamName, T, IsParameterPack,
1531 Param->setAccess(AS_public);
1533 if (AutoTypeLoc TL = TInfo->getTypeLoc().getContainedAutoTypeLoc())
1536 T->containsUnexpandedParameterPack()) {
1537 assert(TL.getConceptReference()->getTemplateArgsAsWritten());
1539 TL.getConceptReference()->getTemplateArgsAsWritten()->arguments())
1549 Param->setInvalidDecl();
1551 if (Param->isParameterPack())
1553 CSI->LocalPacks.push_back(Param);
1560 S->AddDecl(Param);
1565 // A default template-argument may be specified for any kind of
1566 // template-parameter that is not a template parameter pack.
1572 // Check the well-formedness of the default template argument, if provided.
1578 Param->setDefaultArgument(
1589 SourceLocation NameLoc, unsigned Depth, unsigned Position,
1591 assert(S->isTemplateParamScope() &&
1598 NameLoc.isInvalid() ? TmpLoc : NameLoc, Depth, Position, IsParameterPack,
1600 Param->setAccess(AS_public);
1602 if (Param->isParameterPack())
1604 LSI->LocalPacks.push_back(Param);
1611 S->AddDecl(Param);
1615 if (Params->size() == 0) {
1616 Diag(Param->getLocation(), diag::err_template_template_parm_no_parms)
1617 << SourceRange(Params->getLAngleLoc(), Params->getRAngleLoc());
1618 Param->setInvalidDecl();
1622 // A default template-argument may be specified for any kind of
1623 // template-parameter that is not a template parameter pack.
1631 // try to check well-formedness now, because our template template parameter
1651 Param->setDefaultArgument(Context, DefaultArg);
1664 // Check a record-decl that we've seen to see if it is a lexical parent of the
1667 CheckingRD = CheckingRD->getMostRecentDecl();
1668 if (!CheckingRD->isTemplated())
1671 for (const DeclContext *DC = Friend->getLexicalDeclContext();
1672 DC && !DC->isFileContext(); DC = DC->getParent())
1674 if (CheckingRD == RD->getMostRecentDecl())
1679 if (D->getDepth() < TemplateDepth)
1684 TransformType(D->getType());
1703 if (TL.getDecl()->getDepth() < TemplateDepth)
1718 TransformType(TD->getUnderlyingType());
1722 TransformType(VD->getType());
1724 TransformTemplateParameterList(TD->getTemplateParameters());
1739 assert(Friend->getFriendObjectKind() && "Only works on a friend");
1747 Sema::ActOnTemplateParameterList(unsigned Depth,
1768 T->setQualifierInfo(SS.getWithLocInContext(S.Context));
1777 Decl *D = TD->getMostRecentDecl();
1794 while (D->getFriendObjectKind() != Decl::FriendObjectKind::FOK_None &&
1795 D->getPreviousDecl())
1796 D = D->getPreviousDecl();
1797 return cast<TemplateDecl>(D)->getTemplateParameters();
1807 assert(TemplateParams && TemplateParams->size() > 0 &&
1855 if (SemanticContext->isDependentContext()) {
1863 /*TemplateId-*/ nullptr,
1873 // -- every member template of class T
1898 // -> RecordScope N
1899 // -> TemplateParamScope (where we will find Param)
1900 // -> NamespaceScope ns
1903 if (S->isTemplateParamScope())
1908 PrevDecl = (*Previous.begin())->getUnderlyingDecl();
1910 if (PrevDecl && PrevDecl->isTemplateParameter()) {
1922 // We may have found the injected-class-name of a class template,
1926 cast<CXXRecordDecl>(PrevDecl)->isInjectedClassName()) {
1927 PrevDecl = cast<CXXRecordDecl>(PrevDecl->getDeclContext());
1929 = cast<CXXRecordDecl>(PrevDecl)->getDescribedClassTemplate();
1933 ->getSpecializedTemplate();
1941 // function is neither a qualified name nor a template-id, scopes outside
1945 while (!OutermostContext->isFileContext())
1946 OutermostContext = OutermostContext->getLookupParent();
1949 (OutermostContext->Equals(PrevDecl->getDeclContext()) ||
1950 OutermostContext->Encloses(PrevDecl->getDeclContext()))) {
1951 SemanticContext = PrevDecl->getDeclContext();
1960 // declaration of this name as a non-tag type.
1963 while (LookupContext->isTransparentContext())
1964 LookupContext = LookupContext->getLookupParent();
1971 PrevDecl = (*Previous.begin())->getUnderlyingDecl();
1982 PrevClassTemplate->getDeclContext()->getRedeclContext()->Equals(
1983 SemanticContext->getRedeclContext()))) {
1985 Diag(Shadow->getTargetDecl()->getLocation(),
1987 Diag(Shadow->getIntroducer()->getLocation(), diag::note_using_decl) << 0;
1997 if (!(TUK == TagUseKind::Friend && CurContext->isDependentContext()) &&
2003 PrevClassTemplate->getTemplateParameters(), /*Complain=*/true,
2010 // the class-key shall agree in kind with the original class
2012 RecordDecl *PrevRecordDecl = PrevClassTemplate->getTemplatedDecl();
2017 << FixItHint::CreateReplacement(KWLoc, PrevRecordDecl->getKindName());
2018 Diag(PrevRecordDecl->getLocation(), diag::note_previous_use);
2019 Kind = PrevRecordDecl->getTagKind();
2024 if (TagDecl *Def = PrevRecordDecl->getDefinition()) {
2029 SkipBody->ShouldSkip = true;
2030 SkipBody->Previous = Def;
2031 auto *Tmpl = cast<CXXRecordDecl>(Hidden)->getDescribedClassTemplate();
2038 Diag(Def->getLocation(), diag::note_previous_definition);
2052 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
2060 if (!(TUK == TagUseKind::Friend && CurContext->isDependentContext()) &&
2065 (SS.isSet() && SemanticContext && SemanticContext->isRecord() &&
2066 SemanticContext->isDependentContext())
2075 // template out-of-line.
2091 !(TUK == TagUseKind::Friend && CurContext->isDependentContext());
2096 PrevClassTemplate->getTemplatedDecl() : nullptr,
2100 NewClass->setTemplateParameterListsInfo(
2106 if (TUK == TagUseKind::Definition && (!SkipBody || !SkipBody->ShouldSkip)) {
2117 NewTemplate->setPreviousDecl(PrevClassTemplate);
2119 NewClass->setDescribedClassTemplate(NewTemplate);
2122 NewTemplate->setModulePrivate();
2125 QualType T = NewTemplate->getInjectedClassNameSpecialization();
2127 assert(T->isDependentType() && "Class template type is not dependent?");
2133 PrevClassTemplate->getInstantiatedFromMemberTemplate())
2134 PrevClassTemplate->setMemberSpecialization();
2138 NewTemplate->getDeclContext()->isRecord())
2142 NewClass->setLexicalDeclContext(CurContext);
2143 NewTemplate->setLexicalDeclContext(CurContext);
2145 if (TUK == TagUseKind::Definition && (!SkipBody || !SkipBody->ShouldSkip))
2146 NewClass->startDefinition();
2151 mergeDeclAttributes(NewClass, PrevClassTemplate->getTemplatedDecl());
2160 while ((Outer->getFlags() & Scope::TemplateParamScope) != 0)
2161 Outer = Outer->getParent();
2164 if (PrevClassTemplate && PrevClassTemplate->getAccess() != AS_none) {
2165 NewTemplate->setAccess(PrevClassTemplate->getAccess());
2166 NewClass->setAccess(PrevClassTemplate->getAccess());
2169 NewTemplate->setObjectOfFriendDecl();
2172 if (!CurContext->isDependentContext()) {
2173 DeclContext *DC = SemanticContext->getRedeclContext();
2174 DC->makeDeclVisibleInContext(NewTemplate);
2181 Context, CurContext, NewClass->getLocation(), NewTemplate, FriendLoc);
2182 Friend->setAccess(AS_public);
2183 CurContext->addDecl(Friend);
2190 NewTemplate->setInvalidDecl();
2191 NewClass->setInvalidDecl();
2196 if (SkipBody && SkipBody->ShouldSkip)
2197 return SkipBody->Previous;
2203 /// template parameter, which is ill-formed in certain contexts.
2219 // A default template-argument shall not be specified in a
2223 // template-argument, that declaration shall be a definition and shall be
2234 // A default template-argument shall not be specified in the
2235 // template-parameter-lists of the definition of a member of a
2244 // A default template-argument shall not be specified in a
2250 // FIXME: C++0x [temp.param]p9 allows default template-arguments
2264 if (TTP->isParameterPack())
2267 TemplateParameterList *Params = TTP->getTemplateParameters();
2268 for (unsigned I = 0, N = Params->size(); I != N; ++I) {
2269 NamedDecl *P = Params->getParam(I);
2271 if (!TTP->isParameterPack())
2272 if (const TypeConstraint *TC = TTP->getTypeConstraint())
2273 if (TC->hasExplicitTemplateArgs())
2274 for (auto &ArgLoc : TC->getTemplateArgsAsWritten()->arguments())
2282 if (!NTTP->isParameterPack() &&
2283 S.DiagnoseUnexpandedParameterPack(NTTP->getLocation(),
2284 NTTP->getTypeSourceInfo(),
2307 // The set of default template-arguments available for use with a
2316 TemplateParameterList::iterator OldParam = NewParams->end();
2318 OldParam = OldParams->begin();
2321 for (TemplateParameterList::iterator NewParam = NewParams->begin(),
2322 NewParamEnd = NewParams->end();
2339 // Variable used to diagnose non-final parameter packs
2345 if (NewTypeParm->hasDefaultArgument() &&
2347 *this, TPC, NewTypeParm->getLocation(),
2348 NewTypeParm->getDefaultArgument().getSourceRange()))
2349 NewTypeParm->removeDefaultArgument();
2354 if (NewTypeParm->isParameterPack()) {
2355 assert(!NewTypeParm->hasDefaultArgument() &&
2359 NewTypeParm->hasDefaultArgument() &&
2360 (!SkipBody || !SkipBody->ShouldSkip)) {
2361 OldDefaultLoc = OldTypeParm->getDefaultArgumentLoc();
2362 NewDefaultLoc = NewTypeParm->getDefaultArgumentLoc();
2365 if (!OldTypeParm->getOwningModule())
2371 OldTypeParm->getImportedOwningModule()->getFullModuleName();
2374 } else if (OldTypeParm && OldTypeParm->hasDefaultArgument()) {
2377 NewTypeParm->setInheritedDefaultArgument(Context, OldTypeParm);
2378 PreviousDefaultArgLoc = OldTypeParm->getDefaultArgumentLoc();
2379 } else if (NewTypeParm->hasDefaultArgument()) {
2381 PreviousDefaultArgLoc = NewTypeParm->getDefaultArgumentLoc();
2387 if (!NewNonTypeParm->isParameterPack() &&
2388 DiagnoseUnexpandedParameterPack(NewNonTypeParm->getLocation(),
2389 NewNonTypeParm->getTypeSourceInfo(),
2396 if (NewNonTypeParm->hasDefaultArgument() &&
2398 *this, TPC, NewNonTypeParm->getLocation(),
2399 NewNonTypeParm->getDefaultArgument().getSourceRange())) {
2400 NewNonTypeParm->removeDefaultArgument();
2403 // Merge default arguments for non-type template parameters
2406 if (NewNonTypeParm->isParameterPack()) {
2407 assert(!NewNonTypeParm->hasDefaultArgument() &&
2409 if (!NewNonTypeParm->isPackExpansion())
2412 NewNonTypeParm->hasDefaultArgument() &&
2413 (!SkipBody || !SkipBody->ShouldSkip)) {
2414 OldDefaultLoc = OldNonTypeParm->getDefaultArgumentLoc();
2415 NewDefaultLoc = NewNonTypeParm->getDefaultArgumentLoc();
2417 if (!OldNonTypeParm->getOwningModule())
2423 OldNonTypeParm->getImportedOwningModule()->getFullModuleName();
2426 } else if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument()) {
2429 NewNonTypeParm->setInheritedDefaultArgument(Context, OldNonTypeParm);
2430 PreviousDefaultArgLoc = OldNonTypeParm->getDefaultArgumentLoc();
2431 } else if (NewNonTypeParm->hasDefaultArgument()) {
2433 PreviousDefaultArgLoc = NewNonTypeParm->getDefaultArgumentLoc();
2447 if (NewTemplateParm->hasDefaultArgument() &&
2449 NewTemplateParm->getLocation(),
2450 NewTemplateParm->getDefaultArgument().getSourceRange()))
2451 NewTemplateParm->removeDefaultArgument();
2456 if (NewTemplateParm->isParameterPack()) {
2457 assert(!NewTemplateParm->hasDefaultArgument() &&
2459 if (!NewTemplateParm->isPackExpansion())
2463 NewTemplateParm->hasDefaultArgument() &&
2464 (!SkipBody || !SkipBody->ShouldSkip)) {
2465 OldDefaultLoc = OldTemplateParm->getDefaultArgument().getLocation();
2466 NewDefaultLoc = NewTemplateParm->getDefaultArgument().getLocation();
2468 if (!OldTemplateParm->getOwningModule())
2474 OldTemplateParm->getImportedOwningModule()->getFullModuleName();
2477 } else if (OldTemplateParm && OldTemplateParm->hasDefaultArgument()) {
2480 NewTemplateParm->setInheritedDefaultArgument(Context, OldTemplateParm);
2482 = OldTemplateParm->getDefaultArgument().getLocation();
2483 } else if (NewTemplateParm->hasDefaultArgument()) {
2486 = NewTemplateParm->getDefaultArgument().getLocation();
2497 Diag((*NewParam)->getLocation(),
2508 // translation unit and the definitions satisfy the [same-meaning
2532 // If a template-parameter of a class template, variable template, or
2534 // template-parameter shall either have a default template argument
2536 Diag((*NewParam)->getLocation(),
2552 for (TemplateParameterList::iterator NewParam = NewParams->begin(),
2553 NewParamEnd = NewParams->end();
2556 TTP->removeDefaultArgument();
2559 NTTP->removeDefaultArgument();
2561 cast<TemplateTemplateParmDecl>(*NewParam)->removeDefaultArgument();
2573 unsigned Depth;
2576 // overall construct type-dependent / a dependent type. This is strictly
2577 // best-effort for now; we may fail to match at all for a dependent type
2584 DependencyChecker(unsigned Depth, bool IgnoreNonTypeDependent)
2585 : Depth(Depth), IgnoreNonTypeDependent(IgnoreNonTypeDependent),
2590 NamedDecl *ND = Params->getParam(0);
2592 Depth = PD->getDepth();
2595 Depth = PD->getDepth();
2597 Depth = cast<TemplateTemplateParmDecl>(ND)->getDepth();
2602 if (ParmDepth >= Depth) {
2611 // Prune out non-type-dependent expressions if requested. This can
2613 // (if a value-dependent expression creates a dependent type), but this
2614 // mode is best-effort only.
2616 if (IgnoreNonTypeDependent && !E->isTypeDependent())
2623 !TL.getType()->isDependentType())
2629 return !Matches(TL.getTypePtr()->getDepth(), TL.getNameLoc());
2633 // For a best-effort search, keep looking until we find a location.
2634 return IgnoreNonTypeDependent || !Matches(T->getDepth());
2640 if (Matches(PD->getDepth()))
2647 dyn_cast<NonTypeTemplateParmDecl>(E->getDecl()))
2648 if (Matches(PD->getDepth(), E->getExprLoc()))
2654 return TraverseType(T->getReplacementType());
2659 return TraverseTemplateArgument(T->getArgumentPack());
2663 return TraverseType(T->getInjectedSpecializationType());
2672 if (!Params->size())
2681 // nested-name-specifier, if any.
2687 if (const Type *CurType = NNS->getAsType()) {
2709 // by the nested-name-specifier and walking out until we run out of types.
2717 T = QualType(SS.getScopeRep()->getAsType(), 0);
2729 if (CXXRecordDecl *Record = T->getAsCXXRecordDecl()) {
2734 Spec->getSpecializationKind() == TSK_ExplicitSpecialization) {
2735 ExplicitSpecLoc = Spec->getLocation();
2738 } else if (Record->getTemplateSpecializationKind()
2740 ExplicitSpecLoc = Record->getLocation();
2744 if (TypeDecl *Parent = dyn_cast<TypeDecl>(Record->getParent()))
2752 = T->getAs<TemplateSpecializationType>()) {
2753 if (TemplateDecl *Template = TST->getTemplateName().getAsTemplateDecl()) {
2754 if (TypeDecl *Parent = dyn_cast<TypeDecl>(Template->getDeclContext()))
2764 = T->getAs<DependentTemplateSpecializationType>()) {
2765 if (NestedNameSpecifier *NNS = DependentTST->getQualifier())
2766 T = QualType(NNS->getAsType(), 0);
2773 if (const DependentNameType *DependentName = T->getAs<DependentNameType>()){
2774 if (NestedNameSpecifier *NNS = DependentName->getQualifier())
2775 T = QualType(NNS->getAsType(), 0);
2782 if (const EnumType *EnumT = T->getAs<EnumType>()) {
2783 // FIXME: Forward-declared enums require a TSK_ExplicitSpecialization
2785 EnumDecl *Enum = EnumT->getDecl();
2788 if (TypeDecl *Parent = dyn_cast<TypeDecl>(Enum->getParent()))
2798 // to the innermost while checking template-parameter-lists.
2830 ExpectedTemplateLoc = ParamLists[0]->getTemplateLoc();
2861 if (CXXRecordDecl *Record = T->getAsCXXRecordDecl()) {
2864 ExpectedTemplateParams = Partial->getTemplateParameters();
2866 } else if (Record->isDependentType()) {
2867 if (Record->getDescribedClassTemplate()) {
2868 ExpectedTemplateParams = Record->getDescribedClassTemplate()
2869 ->getTemplateParameters();
2878 if (Spec->getSpecializationKind() != TSK_ExplicitSpecialization)
2882 } else if (Record->getTemplateSpecializationKind()) {
2883 if (Record->getTemplateSpecializationKind()
2885 TypeIdx == NumTypes - 1)
2891 = T->getAs<TemplateSpecializationType>()) {
2892 if (TemplateDecl *Template = TST->getTemplateName().getAsTemplateDecl()) {
2893 ExpectedTemplateParams = Template->getTemplateParameters();
2896 } else if (T->getAs<DependentTemplateSpecializationType>()) {
2910 if (ParamLists[ParamIdx]->size() == 0) {
2911 if (CheckExplicitSpecialization(ParamLists[ParamIdx]->getSourceRange(),
2921 if (TypeIdx == NumTypes - 1)
2925 if (ParamLists[ParamIdx]->size() > 0) {
2928 Diag(ParamLists[ParamIdx]->getTemplateLoc(),
2931 << SourceRange(ParamLists[ParamIdx]->getLAngleLoc(),
2932 ParamLists[ParamIdx]->getRAngleLoc())
2952 // In friend declarations we can have template-ids which don't
2955 // template-id.
2956 if (IsFriend && T->isDependentType()) {
2990 // If there were at least as many template-ids as there were template
2997 DiagnoseMissingExplicitSpecialization(SourceRange(TemplateId->LAngleLoc,
2998 TemplateId->RAngleLoc));
3010 if (ParamIdx < ParamLists.size() - 1) {
3013 for (unsigned I = ParamIdx, E = ParamLists.size() - 1; I != E; ++I) {
3014 if (ParamLists[I]->size() == 0)
3021 Diag(ParamLists[ParamIdx]->getTemplateLoc(),
3024 << SourceRange(ParamLists[ParamIdx]->getTemplateLoc(),
3025 ParamLists[ParamLists.size() - 2]->getRAngleLoc());
3045 // template or a member template that ap- pears in namespace scope, the
3048 // specialize a class member template if its en- closing class templates
3050 if (ParamLists.back()->size() == 0 &&
3051 CheckExplicitSpecialization(ParamLists[ParamIdx]->getSourceRange(),
3062 Diag(Template->getLocation(), diag::note_template_declared_here)
3070 << Template->getDeclName();
3075 for (OverloadedTemplateStorage::iterator I = OST->begin(),
3076 IEnd = OST->end();
3078 Diag((*I)->getLocation(), diag::note_template_declared_here)
3079 << 0 << (*I)->getDeclName();
3089 TemplateArgument T2) -> QualType {
3091 // builtins - users aren't allowed to specialize for them
3092 if (T1.getAsType()->isBuiltinType() && T2.getAsType()->isBuiltinType())
3124 // pack T. The member typedef-name type shall denote the same type, if any, as
3146 auto CheckConditionalOperands = [&](bool ConstRefQual) -> QualType {
3190 // COND-RES(X, Y) be
3194 // Otherwise, if COND-RES(CREF(D1), CREF(D2)) denotes a type, let C denote
3195 // the type decay_t<COND-RES(CREF(D1), CREF(D2))>.
3205 // a type C, the member typedef-name type shall denote the same type, if any,
3226 switch (BTD->getBuiltinTemplateKind()) {
3229 // S<T, 0, ..., N-1>.
3234 if (!OrigType->isDependentType() && !OrigType->isIntegralType(Context)) {
3254 // Expand N into 0 ... N-1.
3263 // If N is negative the program is ill-formed.
3289 "type std::size_t, and hence be non-negative");
3290 // If the Index is out of bounds, the program is ill-formed.
3330 return AliasTemplate->getName() == "enable_if_t" ||
3331 AliasTemplate->getName() == "__enable_if_t";
3342 if (auto BinOp = dyn_cast<BinaryOperator>(Clause->IgnoreParenImpCasts())) {
3343 if (BinOp->getOpcode() == BO_LAnd) {
3344 collectConjunctionTerms(BinOp->getLHS(), Terms);
3345 collectConjunctionTerms(BinOp->getRHS(), Terms);
3353 // The ranges-v3 library uses an odd pattern of a top-level "||" with
3354 // a left-hand side that is value-dependent but never true. Identify
3357 // Top-level '||'.
3358 auto *BinOp = dyn_cast<BinaryOperator>(Cond->IgnoreParenImpCasts());
3361 if (BinOp->getOpcode() != BO_LOr) return Cond;
3363 // With an inner '==' that has a literal on the right-hand side.
3364 Expr *LHS = BinOp->getLHS();
3365 auto *InnerBinOp = dyn_cast<BinaryOperator>(LHS->IgnoreParenImpCasts());
3368 if (InnerBinOp->getOpcode() != BO_EQ ||
3369 !isa<IntegerLiteral>(InnerBinOp->getRHS()))
3373 // CONCEPT_REQUIRES or CONCEPT_REQUIRES_, return the right-hand side
3374 // of the '||', which is the real, user-provided condition.
3375 SourceLocation Loc = InnerBinOp->getExprLoc();
3380 return BinOp->getRHS();
3387 // A PrinterHelper that prints more helpful diagnostics for some sub-expressions
3397 if (DR && DR->getQualifier()) {
3400 DR->getQualifier()->print(OS, Policy, true);
3402 const ValueDecl *VD = DR->getDecl();
3403 OS << VD->getName();
3407 OS, IV->getTemplateArgs().asArray(), Policy,
3408 IV->getSpecializedTemplate()->getTemplateParameters());
3432 Expr *TermAsWritten = Term->IgnoreParenImpCasts();
3440 // a constant-evaluated context.
3445 if (Term->EvaluateAsBooleanCondition(Succeeded, Context) &&
3452 FailedCond = Cond->IgnoreParenImpCasts();
3460 FailedCond->printPretty(Out, &Helper, Policy, 0, "\n", nullptr);
3470 if (DTN && DTN->isIdentifier())
3471 // When building a template-id where the template-name is dependent,
3473 // correct, or the code is ill-formed and will be diagnosed when the
3476 ElaboratedTypeKeyword::None, DTN->getQualifier(), DTN->getIdentifier(),
3499 // Check that the template argument list is well-formed for this
3514 TypeAliasDecl *Pattern = AliasTemplate->getTemplatedDecl();
3515 if (Pattern->isInvalidDecl())
3520 // substitution non-final in that case. Note that these external resugarers
3529 AliasTemplate->getTemplateParameters()->getDepth());
3544 if (!AliasTemplate->getDeclContext()->isFileContext())
3545 SavedContext.emplace(*this, AliasTemplate->getDeclContext());
3548 SubstType(Pattern->getUnderlyingType(), TemplateArgLists,
3549 AliasTemplate->getLocation(), AliasTemplate->getDeclName());
3557 (*DeductionInfo)->hasSFINAEDiagnostic() &&
3558 (*DeductionInfo)->peekSFINAEDiagnostic().second.getDiagID() ==
3570 (*DeductionInfo)->takeSFINAEDiagnostic(OldDiag);
3574 (*DeductionInfo)->addSFINAEDiagnostic(
3578 << FailedCond->getSourceRange());
3608 for (DeclContext *Ctx = CurContext; Ctx; Ctx = Ctx->getLookupParent()) {
3610 if (Ctx->isFileContext()) break;
3619 !Record->getDescribedClassTemplate())
3622 // Fetch the injected class name type and check whether its
3626 ->getInjectedSpecializationType();
3628 if (CanonType != Injected->getCanonicalTypeInternal())
3644 ClassTemplate->findSpecialization(CTAI.CanonicalConverted, InsertPos);
3650 Context, ClassTemplate->getTemplatedDecl()->getTagKind(),
3651 ClassTemplate->getDeclContext(),
3652 ClassTemplate->getTemplatedDecl()->getBeginLoc(),
3653 ClassTemplate->getLocation(), ClassTemplate, CTAI.CanonicalConverted,
3655 ClassTemplate->AddSpecialization(Decl, InsertPos);
3656 if (ClassTemplate->isOutOfLine())
3657 Decl->setLexicalDeclContext(ClassTemplate->getLexicalDeclContext());
3660 if (Decl->getSpecializationKind() == TSK_Undeclared &&
3661 ClassTemplate->getTemplatedDecl()->hasAttrs()) {
3668 ClassTemplate->getTemplatedDecl(), Decl);
3677 "type of non-dependent specialization is not a RecordType");
3682 // Build the fully-sugared type for this class template
3698 II = ATN->getDeclName().getAsIdentifierInfo();
3710 // We assumed this undeclared identifier to be an (ADL-only) function
3712 // Try to typo-correct it now.
3716 LookupResult R(*this, ATN->getDeclName(), NameLoc, LookupOrdinaryName);
3732 << ATN->getDeclName());
3758 // A qualified-id that refers to a type and in which the
3759 // nested-name-specifier depends on a template-parameter (14.6.2)
3761 // qualified-id denotes a type, forming an
3762 // elaborated-type-specifier (7.1.5.3).
3770 << SS.getScopeRep() << TemplateII->getName()
3774 << SS.getScopeRep() << TemplateII->getName();
3784 // Per C++ [class.qual]p2, if the template-id was an injected-class-name,
3789 if (LookupRD && LookupRD->getIdentifier() == TemplateII) {
3794 << TemplateII << 0 /*injected-class-name used as template name*/
3809 assert(SS.getScopeRep() == DTN->getQualifier());
3811 ElaboratedTypeKeyword::None, DTN->getQualifier(), DTN->getIdentifier(),
3813 // Build type-source information.
3832 // Build type-source information.
3843 // Create an elaborated-type-specifier containing the nested-name-specifier.
3879 assert(SS.getScopeRep() == DTN->getQualifier());
3881 Keyword, DTN->getQualifier(), DTN->getIdentifier(),
3884 // Build type-source information.
3902 // If the identifier resolves to a typedef-name or the simple-template-id
3904 // elaborated-type-specifier is ill-formed.
3907 Diag(TAT->getLocation(), diag::note_declared_at);
3915 if (const RecordType *RT = Result->getAs<RecordType>()) {
3916 RecordDecl *D = RT->getDecl();
3918 IdentifierInfo *Id = D->getIdentifier();
3925 << FixItHint::CreateReplacement(SourceRange(TagLoc), D->getKindName());
3926 Diag(D->getLocation(), diag::note_previous_use);
3930 // Provide source-location information for the template specialization.
3941 // Construct an elaborated type containing the nested-name-specifier (if any)
3958 const TemplateArgument &Arg, unsigned Depth, unsigned Index) {
3972 Arg.getAsType()->getAs<TemplateTypeParmType>();
3974 TPT->getDepth() == Depth && TPT->getIndex() == Index;
3979 if (!DRE || !DRE->getDecl())
3982 dyn_cast<NonTypeTemplateParmDecl>(DRE->getDecl());
3983 return NTTP && NTTP->getDepth() == Depth && NTTP->getIndex() == Index;
3990 return TTP && TTP->getDepth() == Depth && TTP->getIndex() == Index;
3997 if (Params->size() != Args.size())
4000 unsigned Depth = Params->getDepth();
4007 if (Params->getParam(I)->isParameterPack()) {
4009 !Arg.pack_begin()->isPackExpansion())
4011 Arg = Arg.pack_begin()->getPackExpansionPattern();
4014 if (!isTemplateArgumentTemplateParameter(Arg, Depth, I))
4023 if (Partial->getDeclContext()->isDependentContext())
4027 // for non-substitution-failure issues?
4028 TemplateDeductionInfo Info(Partial->getLocation());
4032 auto *Template = Partial->getSpecializedTemplate();
4033 S.Diag(Partial->getLocation(),
4050 Template->getAssociatedConstraints(TemplateAC);
4051 Partial->getAssociatedConstraints(PartialAC);
4061 NamedDecl *Param = TemplateParams->getParam(I);
4062 if (Param->getDeclName())
4063 S.Diag(Param->getLocation(), diag::note_non_deducible_parameter)
4064 << Param->getDeclName();
4066 S.Diag(Param->getLocation(), diag::note_non_deducible_parameter)
4077 // - The specialization shall be more specialized than the primary
4082 // - Each template-parameter shall appear at least once in the
4083 // template-id outside a non-deduced context.
4086 // deduced because of the structure of its template-parameter-list
4087 // and the template-id, the program is ill-formed.
4088 auto *TemplateParams = Partial->getTemplateParameters();
4089 llvm::SmallBitVector DeducibleParams(TemplateParams->size());
4090 S.MarkUsedTemplateParameters(Partial->getTemplateArgs(), true,
4091 TemplateParams->getDepth(), DeducibleParams);
4094 unsigned NumNonDeducible = DeducibleParams.size() - DeducibleParams.count();
4095 S.Diag(Partial->getLocation(), diag::ext_partial_specs_not_deducible)
4098 << SourceRange(Partial->getLocation(),
4099 Partial->getTemplateArgsAsWritten()->RAngleLoc);
4117 // default-argument shall be deducible from the parameter-type-list of the
4119 auto *TemplateParams = TD->getTemplateParameters();
4120 llvm::SmallBitVector DeducibleParams(TemplateParams->size());
4122 for (unsigned I = 0; I != TemplateParams->size(); ++I) {
4124 auto *Param = TemplateParams->getParam(I);
4125 if (Param->isParameterPack() || hasVisibleDefaultArgument(Param))
4130 unsigned NumNonDeducible = DeducibleParams.size() - DeducibleParams.count();
4131 Diag(TD->getLocation(), diag::err_deduction_guide_template_not_deducible)
4149 SourceLocation LAngleLoc = TemplateId->LAngleLoc;
4150 SourceLocation RAngleLoc = TemplateId->RAngleLoc;
4152 TemplateName Name = TemplateId->Template.get();
4154 // The template-id must name a variable template.
4160 FnTemplate = *OTS->begin();
4165 << FnTemplate->getDeclName();
4170 if (const auto *DSA = VarTemplate->getAttr<NoSpecializationsAttr>()) {
4171 auto Message = DSA->getMessage();
4174 Diag(DSA->getLoc(), diag::note_marked_here) << DSA;
4185 // Check that the template argument list is well-formed for this
4208 << VarTemplate->getDeclName();
4212 if (isSameAsPrimaryTemplate(VarTemplate->getTemplateParameters(),
4215 !TemplateParams->hasAssociatedConstraints())) {
4218 // -- The argument list of the specialization shall not be identical
4222 << /*is definition*/ (SC != SC_Extern && !CurContext->isRecord())
4234 PrevDecl = VarTemplate->findPartialSpecialization(
4238 VarTemplate->findSpecialization(CTAI.CanonicalConverted, InsertPos);
4249 if (PrevDecl && PrevDecl->getSpecializationKind() == TSK_Undeclared) {
4255 Specialization->setLocation(TemplateNameLoc);
4263 Context, VarTemplate->getDeclContext(), TemplateKWLoc,
4264 TemplateNameLoc, TemplateParams, VarTemplate, DI->getType(), DI, SC,
4266 Partial->setTemplateArgsAsWritten(TemplateArgs);
4269 VarTemplate->AddPartialSpecialization(Partial, InsertPos);
4274 if (PrevPartial && PrevPartial->getInstantiatedFromMember())
4275 PrevPartial->setMemberSpecialization();
4282 Context, VarTemplate->getDeclContext(), TemplateKWLoc, TemplateNameLoc,
4283 VarTemplate, DI->getType(), DI, SC, CTAI.CanonicalConverted);
4284 Specialization->setTemplateArgsAsWritten(TemplateArgs);
4287 VarTemplate->AddSpecialization(Specialization, InsertPos);
4296 if (PrevDecl && PrevDecl->getPointOfInstantiation().isValid()) {
4298 for (Decl *Prev = PrevDecl; Prev; Prev = Prev->getPreviousDecl()) {
4311 Diag(PrevDecl->getPointOfInstantiation(),
4313 << (PrevDecl->getTemplateSpecializationKind() !=
4319 Specialization->setLexicalDeclContext(CurContext);
4324 CurContext->addDecl(Specialization);
4327 Specialization->setSpecializationKind(TSK_ExplicitSpecialization);
4332 else if (Specialization->isStaticDataMember() &&
4333 Specialization->isOutOfLine())
4334 Specialization->setAccess(VarTemplate->getAccess());
4341 /// a given template-id.
4354 // Check that the template argument list is well-formed for this template.
4364 if (Template->getDeclContext()->isDependentContext() ||
4373 Template->findSpecialization(CTAI.CanonicalConverted, InsertPos)) {
4383 VarDecl *InstantiationPattern = Template->getTemplatedDecl();
4398 Template->getPartialSpecializations(PartialSpecs);
4410 if (Template->getMostRecentDecl()->isMemberSpecialization() &&
4411 !Partial->getMostRecentDecl()->isMemberSpecialization())
4419 // Store the failed-deduction information for use in diagnostics, later.
4420 // TODO: Actually use the failed-deduction info?
4435 // -- If exactly one matching specialization is found, the
4439 // -- If more than one matching specialization is found, the
4445 // ambiguous and the program is ill-formed.
4449 if (getMoreSpecializedPartialSpecialization(P->Partial, Best->Partial,
4451 P->Partial)
4461 P->Partial, Best->Partial,
4462 PointOfInstantiation) != Best->Partial) {
4470 InstantiationPattern = Best->Partial;
4471 PartialSpecArgs = Best->Args;
4473 // -- If no match is found, the instantiation is generated
4475 // InstantiationPattern = Template->getTemplatedDecl();
4479 // Note that we do not instantiate a definition until we see an odr-use
4490 Decl->setInvalidDecl();
4496 Diag(P.Partial->getLocation(), diag::note_partial_spec_match)
4497 << getTemplateArgumentBindingsText(P.Partial->getTemplateParameters(),
4504 Decl->setInstantiationOf(D, PartialSpecArgs);
4526 if (!Var->getTemplateSpecializationKind())
4527 Var->setTemplateSpecializationKind(TSK_ImplicitInstantiation,
4539 NoteTemplateLocation(*TD, TD->getTemplateParameters()->getSourceRange());
4561 if (NamedConcept->isInvalidDecl())
4576 Context, NamedConcept->getDeclContext(), NamedConcept->getLocation(),
4591 NamedConcept, {NamedConcept->getConstraintExpr()}, MLTAL,
4593 TemplateArgs->getRAngleLoc()),
4623 // Non-function templates require a template argument list.
4661 if (ULE->getType() == Context.OverloadTy && R.isSingleResult() &&
4662 !R.getFoundDecl()->getAsFunction())
4663 ULE->setType(Context.UnresolvedTemplateTy);
4708 if (TemplateKWLoc.isValid() && S && !S->getTemplateParamParent())
4727 // a template, the program is ill-formed. [Note: the keyword
4728 // template may not be applied to non-template members of class
4729 // templates. -end note ] [ Note: as is the case with the
4732 // nested-name-specifier or the expression on the left of the ->
4733 // or . is not dependent on a template-parameter, or the use
4734 // does not appear in the scope of a template. -end note]
4737 // the "template" keyword prior to a template-name that was not a
4746 // We resolved this to a (non-dependent) template name. Return it.
4750 Name.Identifier && LookupRD->getIdentifier() == Name.Identifier) {
4753 // nested-name-specifier nominates a class C, if the name specified
4754 // [...] is the injected-class-name of C, [...] the name is instead
4759 // injected-class-name as naming the template.
4763 << 0 /*injected-class-name used as template name*/
4778 // cases where it finds a non-template.
4838 // A template-argument for a template-parameter which is a
4839 // type shall be a type-id.
4860 SS.Adopt(ArgExpr->getQualifierLoc());
4861 NameInfo = ArgExpr->getNameInfo();
4864 if (ArgExpr->isImplicitAccess()) {
4865 SS.Adopt(ArgExpr->getQualifierLoc());
4866 NameInfo = ArgExpr->getMemberNameInfo();
4930 // Objective-C ARC:
4931 // If an explicitly-specified template argument type is a lifetime type
4934 ArgType->isObjCLifetimeType() &&
4957 /// template-id we are checking.
4960 /// terminates the template-id.
4977 Output = Param->getDefaultArgument();
4980 // on the previously-computed template arguments.
4991 for (unsigned i = 0, e = Param->getDepth(); i != e; ++i)
4995 if (const auto *Rec = dyn_cast<CXXRecordDecl>(Template->getDeclContext()))
4996 ForLambdaCallOperator = Rec->isLambda();
4997 Sema::ContextRAII SavedContext(SemaRef, Template->getDeclContext(),
5001 Param->getDefaultArgumentLoc(),
5002 Param->getDeclName()))
5010 /// the given non-type template parameter.
5019 /// template-id we are checking.
5022 /// terminates the template-id.
5024 /// \param Param the non-type template parameter whose default we are
5046 for (unsigned i = 0, e = Param->getDepth(); i != e; ++i)
5049 Sema::ContextRAII SavedContext(SemaRef, Template->getDeclContext());
5052 return SemaRef.SubstTemplateArgument(Param->getDefaultArgument(),
5066 /// template-id we are checking.
5069 /// terminates the template-id.
5077 /// \param QualifierLoc Will be set to the nested-name-specifier (with
5078 /// source-location information) that precedes the template name.
5096 for (unsigned i = 0, e = Param->getDepth(); i != e; ++i)
5099 Sema::ContextRAII SavedContext(SemaRef, Template->getDeclContext());
5100 // Substitute into the nested-name-specifier first,
5101 QualifierLoc = Param->getDefaultArgument().getTemplateQualifierLoc();
5111 Param->getDefaultArgument().getArgument().getAsTemplate(),
5112 Param->getDefaultArgument().getTemplateNameLoc(),
5165 TempTempParm->getDefaultArgument().getTemplateQualifierLoc(),
5166 TempTempParm->getDefaultArgument().getTemplateNameLoc());
5169 /// Convert a template-argument that we parsed as a type into a template, if
5170 /// possible. C++ permits injected-class-names to perform dual service as
5174 // Extract and step over any surrounding nested-name-specifier.
5177 if (ETLoc.getTypePtr()->getKeyword() != ElaboratedTypeKeyword::None)
5183 // If this type was written as an injected-class-name, it can be used as a
5186 return TemplateArgumentLoc(Context, InjLoc.getTypePtr()->getTemplateName(),
5189 // If this type was written as an injected-class-name, it may have been
5192 // template specialization, it must have come from an injected-class-name.
5197 TemplateName(CTSD->getSpecializedTemplate()),
5216 // Check non-type template parameters.
5218 // Do substitution on the type of the non-type template parameter
5221 QualType NTTPType = NTTP->getType();
5222 if (NTTP->isParameterPack() && NTTP->isExpandedParameterPack())
5223 NTTPType = NTTP->getExpansionType(ArgumentPackIndex);
5225 if (NTTPType->isInstantiationDependentType() &&
5227 !Template->getDeclContext()->isDependentContext()) {
5228 // Do substitution on the type of the non-type template parameter.
5238 if (auto *PET = NTTPType->getAs<PackExpansionType>()) {
5241 NTTPType = SubstType(PET->getPattern(), MLTAL, NTTP->getLocation(),
5242 NTTP->getDeclName());
5244 NTTPType = SubstType(NTTPType, MLTAL, NTTP->getLocation(),
5245 NTTP->getDeclName());
5248 // If that worked, check the non-type template parameter type
5252 NTTP->getLocation());
5257 auto checkExpr = [&](Expr *E) -> Expr * {
5310 // We were given a template template argument. It may not be ill-formed;
5316 // know that we need a non-type template argument, convert this
5319 DeclarationNameInfo NameInfo(DTN->getIdentifier(),
5324 // FIXME: the template-template arg was a DependentTemplateName,
5354 // therefore cannot be a non-type template argument.
5362 // We have a non-type template parameter but the template
5366 // In a template-argument, an ambiguity between a type-id and
5367 // an expression is resolved to a type-id, regardless of the
5368 // form of the corresponding template-parameter.
5374 if (T->isFunctionType())
5393 TemplateParameterList *Params = TempParm->getTemplateParameters();
5394 if (TempParm->isExpandedParameterPack())
5395 Params = TempParm->getExpansionTemplateParameters(ArgumentPackIndex);
5398 // template parameter, since previously-supplied template arguments
5401 // FIXME: Skip this if the parameters aren't instantiation-dependent.
5421 // When [the injected-class-name] is used [...] as a template-argument for
5422 // a template template-parameter [...] it refers to the class template
5426 Context, ArgLoc.getTypeSourceInfo()->getTypeLoc());
5459 llvm_unreachable("non-type argument with template template parameter");
5476 D = cast<TemplateParmDecl>(cast<TemplateDecl>(TD->getMostRecentDecl())
5477 ->getTemplateParameters()
5478 ->getParam(D->getIndex()));
5483 if (D->hasDefaultArgument() && !S.hasReachableDefaultArgument(D, &Modules)) {
5485 D->getDefaultArgumentLoc(), Modules,
5494 TemplateParameterList *Params = TD->getTemplateParameters();
5500 S.NoteTemplateLocation(*TD, Params->getSourceRange());
5504 /// Check that the given template argument list is well-formed
5525 // [...] The type and form of each template-argument specified in
5526 // a template-id shall match the type and form specified for the
5528 // template-parameter-list.
5534 for (TemplateParameterList::iterator ParamBegin = Params->begin(),
5535 ParamEnd = Params->end(),
5539 if (size_t ParamIdx = Param - ParamBegin;
5579 NoteTemplateLocation(*Template, Params->getSourceRange());
5587 !(*Param)->isTemplateParameterPack() || getExpandedPackSize(*Param);
5591 SmallVector<TemplateArgument, 4> Args(ParamEnd - Param);
5594 TemplateArgument &Arg = Args[Param - First];
5596 if (!(*Param)->isTemplateParameterPack() ||
5610 Params->getDepth()));
5624 Params->getDepth()));
5672 if ((*Param)->isTemplateParameterPack()) {
5688 if ((*Param)->isTemplateParameterPack() && !SugaredArgumentPack.empty()) {
5699 if ((*Param)->isTemplateParameterPack()) {
5703 // A non-expanded parameter pack before the end of the parameter list
5704 // only occurs for an ill-formed template parameter list, unless we've
5708 (Template->getMostRecentDecl()->getKind() != Decl::Kind::Concept) &&
5791 CurrentInstantiationScope->getPartiallySubstitutedPack())) {
5809 NoteTemplateLocation(*Template, Params->getSourceRange());
5820 // re-instantiate constraints outside of normal instantiation.
5821 DeclContext *NewContext = Template->getDeclContext();
5826 NewContext = Decl::castToDeclContext(TD->getTemplatedDecl());
5831 dyn_cast_or_null<CXXMethodDecl>(Template->getTemplatedDecl()))
5832 ThisQuals = Method->getMethodQualifiers();
5888 return Visit(T->getElementType());
5892 return Visit(T->getPointeeType());
5897 return Visit(T->getPointeeType());
5902 return Visit(T->getPointeeType());
5907 return Visit(T->getPointeeType());
5912 return Visit(T->getPointeeType()) || Visit(QualType(T->getClass(), 0));
5917 return Visit(T->getElementType());
5922 return Visit(T->getElementType());
5927 return Visit(T->getElementType());
5932 return Visit(T->getElementType());
5937 return Visit(T->getElementType());
5942 return Visit(T->getElementType());
5947 return Visit(T->getPointeeType());
5951 return Visit(T->getElementType());
5956 return Visit(T->getElementType());
5960 return Visit(T->getElementType());
5965 return Visit(T->getElementType());
5970 for (const auto &A : T->param_types()) {
5975 return Visit(T->getReturnType());
5980 return Visit(T->getReturnType());
5993 return Visit(T->getUnmodifiedType());
6011 return Visit(T->getDeducedType());
6016 return Visit(T->getDeducedType());
6020 return VisitTagDecl(T->getDecl());
6024 return VisitTagDecl(T->getDecl());
6044 return VisitTagDecl(T->getDecl());
6049 return VisitNestedNameSpecifier(T->getQualifier());
6054 if (auto *Q = T->getQualifier())
6061 return Visit(T->getPattern());
6079 return Visit(T->getValueType());
6101 if (Tag->getDeclContext()->isFunctionOrMethod()) {
6110 if (!Tag->hasNameForLinkage()) {
6115 S.Diag(Tag->getLocation(), diag::note_template_unnamed_type_here);
6125 if (NNS->getPrefix() && VisitNestedNameSpecifier(NNS->getPrefix()))
6128 switch (NNS->getKind()) {
6138 return Visit(QualType(NNS->getAsType(), 0));
6145 if (T->hasContainedType() && Visit(T->getContainedType()))
6147 return Visit(T->getWrappedType());
6152 QualType Arg = ArgInfo->getType();
6153 SourceRange SR = ArgInfo->getTypeLoc().getSourceRange();
6156 if (CanonArg->isVariablyModifiedType()) {
6165 // template-argument for a template type-parameter.
6169 if (LangOpts.CPlusPlus11 || CanonArg->hasUnnamedOrLocalType()) {
6189 if (Arg->isValueDependent() || Arg->isTypeDependent())
6194 if (Entity && Entity->hasAttr<DLLImportAttr>())
6197 if (!S.isCompleteType(Arg->getExprLoc(), ParamType))
6213 if (!Arg->EvaluateAsRValue(EvalResult, S.Context) ||
6215 SourceLocation DiagLoc = Arg->getExprLoc();
6227 << Arg->getType() << Arg->getSourceRange();
6236 // - an address constant expression of type std::nullptr_t
6237 if (Arg->getType()->isNullPtrType())
6240 // - a constant expression that evaluates to a null pointer value (4.10); or
6241 // - a constant expression that evaluates to a null member pointer value
6248 if (S.Context.hasSameUnqualifiedType(Arg->getType(), ParamType) ||
6249 S.IsQualificationConversion(Arg->getType(), ParamType, false,
6255 S.Diag(Arg->getExprLoc(), diag::err_template_arg_wrongtype_null_constant)
6256 << Arg->getType() << ParamType << Arg->getSourceRange();
6265 S.Diag(Arg->getExprLoc(), diag::err_template_arg_invalid)
6273 if (Arg->isNullPointerConstant(S.Context, Expr::NPC_NeverValueDependent)) {
6275 S.Diag(Arg->getExprLoc(), diag::err_template_arg_untyped_null_constant)
6276 << ParamType << FixItHint::CreateInsertion(Arg->getBeginLoc(), Code)
6277 << FixItHint::CreateInsertion(S.getLocForEndOfToken(Arg->getEndLoc()),
6283 // FIXME: If we ever want to support general, address-constant expressions
6284 // as non-type template arguments, we should return the ExprResult here to
6295 if (ParamType->isPointerType() &&
6296 !ParamType->castAs<PointerType>()->getPointeeType()->isFunctionType() &&
6299 // For pointer-to-object types, qualification conversions are
6302 if (const ReferenceType *ParamRef = ParamType->getAs<ReferenceType>()) {
6303 if (!ParamRef->getPointeeType()->isFunctionType()) {
6305 // For a non-type template-parameter of type reference to
6307 // reference may be more cv-qualified than the (otherwise
6308 // identical) type of the template- argument. The
6309 // template-parameter is bound directly to the
6310 // template-argument, which shall be an lvalue.
6313 unsigned ParamQuals = ParamRef->getPointeeType().getCVRQualifiers();
6317 S.Diag(Arg->getBeginLoc(),
6319 << ParamType << Arg->getType() << Arg->getSourceRange();
6332 if (ParamType->isReferenceType())
6333 S.Diag(Arg->getBeginLoc(), diag::err_template_arg_no_ref_bind)
6334 << ParamType << ArgIn->getType() << Arg->getSourceRange();
6336 S.Diag(Arg->getBeginLoc(), diag::err_template_arg_not_convertible)
6337 << ArgIn->getType() << ParamType << Arg->getSourceRange();
6353 QualType ArgType = Arg->getType();
6359 // dereference and address-of operators.
6360 Arg = Arg->IgnoreParenCasts();
6366 UnaryOperatorKind UnOpKind = UnOp->getOpcode();
6370 Arg = UnOp->getSubExpr()->IgnoreParenCasts();
6373 FirstOpLoc = UnOp->getOperatorLoc();
6380 S.Diag(ArgIn->getBeginLoc(), diag::ext_ms_deref_template_argument)
6381 << ArgIn->getSourceRange();
6385 else if (Arg->getType()->isPointerType()) {
6389 S.Diag(Arg->getBeginLoc(), diag::err_template_arg_not_decl_ref)
6390 << Arg->getSourceRange();
6395 Arg = Arg->IgnoreImpCasts();
6399 // A template-argument for a non-type, non-template
6400 // template-parameter shall be one of: [...]
6402 // -- the address of an object or function with external
6404 // template-ids but excluding non-static class members,
6405 // expressed as & id-expression where the & is optional if
6407 // corresponding template-parameter is a reference; or
6410 // See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#773
6414 S.Diag(Arg->getBeginLoc(),
6418 << Arg->getSourceRange();
6422 Arg = Parens->getSubExpr();
6427 Arg = subst->getReplacement()->IgnoreImpCasts();
6430 if (UnOp->getOpcode() == UO_AddrOf) {
6431 Arg = UnOp->getSubExpr();
6433 AddrOpLoc = UnOp->getOperatorLoc();
6439 Arg = subst->getReplacement()->IgnoreImpCasts();
6444 Entity = DRE->getDecl();
6446 Entity = CUE->getGuidDecl();
6449 if (ParamType->isPointerType() || ParamType->isNullPtrType()) {
6453 S.Diag(Arg->getExprLoc(), diag::warn_cxx98_compat_template_arg_null);
6471 if (Arg->isValueDependent()) {
6479 S.Diag(Arg->getBeginLoc(), diag::err_template_arg_not_decl_ref)
6480 << Arg->getSourceRange();
6485 // Cannot refer to non-static data members
6487 S.Diag(Arg->getBeginLoc(), diag::err_template_arg_field)
6488 << Entity << Arg->getSourceRange();
6493 // Cannot refer to non-static member functions
6495 if (!Method->isStatic()) {
6496 S.Diag(Arg->getBeginLoc(), diag::err_template_arg_method)
6497 << Method << Arg->getSourceRange();
6507 // A non-type template argument must refer to an object or function.
6510 S.Diag(Arg->getBeginLoc(), diag::err_template_arg_not_object_or_func)
6511 << Arg->getSourceRange();
6512 S.Diag(Entity->getLocation(), diag::note_template_arg_refers_here);
6517 if (Entity->getFormalLinkage() == Linkage::Internal) {
6518 S.Diag(Arg->getBeginLoc(),
6522 << !Func << Entity << Arg->getSourceRange();
6523 S.Diag(Entity->getLocation(), diag::note_template_arg_internal_object)
6525 } else if (!Entity->hasLinkage()) {
6526 S.Diag(Arg->getBeginLoc(), diag::err_template_arg_object_no_linkage)
6527 << !Func << Entity << Arg->getSourceRange();
6528 S.Diag(Entity->getLocation(), diag::note_template_arg_internal_object)
6535 if (Var->getType()->isReferenceType()) {
6536 S.Diag(Arg->getBeginLoc(), diag::err_template_arg_reference_var)
6537 << Var->getType() << Arg->getSourceRange();
6543 if (Var->getTLSKind()) {
6544 S.Diag(Arg->getBeginLoc(), diag::err_template_arg_thread_local)
6545 << Arg->getSourceRange();
6546 S.Diag(Var->getLocation(), diag::note_template_arg_refers_here);
6551 if (AddressTaken && ParamType->isReferenceType()) {
6552 // If we originally had an address-of operator, but the
6554 // like they will work) drop the address-of operator.
6555 if (!S.Context.hasSameUnqualifiedType(Entity->getType(),
6568 ArgType = Entity->getType();
6573 if (!AddressTaken && ParamType->isPointerType()) {
6575 // Function-to-pointer decay.
6576 ArgType = S.Context.getPointerType(Func->getType());
6577 } else if (Entity->getType()->isArrayType()) {
6578 // Array-to-pointer decay.
6579 ArgType = S.Context.getArrayDecayedType(Entity->getType());
6584 ArgType = S.Context.getPointerType(Entity->getType());
6586 S.Diag(Arg->getBeginLoc(), diag::err_template_arg_not_address_of)
6592 S.Diag(Arg->getBeginLoc(), diag::err_template_arg_not_address_of)
6593 << ParamType << FixItHint::CreateInsertion(Arg->getBeginLoc(), "&");
6606 TemplateArgument(cast<ValueDecl>(Entity->getCanonicalDecl()),
6608 S.MarkAnyDeclReferenced(Arg->getBeginLoc(), Entity, false);
6626 // A template-argument for a non-type, non-template
6627 // template-parameter shall be one of: [...]
6629 // -- a pointer to member expressed as described in 5.3.1.
6633 // See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#773
6637 S.Diag(Arg->getBeginLoc(),
6641 << Arg->getSourceRange();
6645 Arg = Parens->getSubExpr();
6650 Arg = subst->getReplacement()->IgnoreImpCasts();
6652 // A pointer-to-member constant written &Class::member.
6654 if (UnOp->getOpcode() == UO_AddrOf) {
6655 DRE = dyn_cast<DeclRefExpr>(UnOp->getSubExpr());
6656 if (DRE && !DRE->getQualifier())
6660 // A constant of pointer-to-member type.
6662 ValueDecl *VD = DRE->getDecl();
6663 if (VD->getType()->isMemberPointerType()) {
6665 if (Arg->isTypeDependent() || Arg->isValueDependent()) {
6672 TemplateArgument(cast<ValueDecl>(VD->getCanonicalDecl()),
6682 ValueDecl *Entity = DRE ? DRE->getDecl() : nullptr;
6690 S.Diag(ResultArg->getExprLoc(), diag::warn_cxx98_compat_template_arg_null);
6700 if (S.IsQualificationConversion(ResultArg->getType(),
6704 ResultArg->getValueKind())
6707 ResultArg->getType(), ParamType.getNonReferenceType())) {
6709 S.Diag(ResultArg->getBeginLoc(), diag::err_template_arg_not_convertible)
6710 << ResultArg->getType() << ParamType << ResultArg->getSourceRange();
6716 return S.Diag(Arg->getBeginLoc(),
6718 << Arg->getSourceRange();
6720 if (isa<FieldDecl>(DRE->getDecl()) ||
6721 isa<IndirectFieldDecl>(DRE->getDecl()) ||
6722 isa<CXXMethodDecl>(DRE->getDecl())) {
6723 assert((isa<FieldDecl>(DRE->getDecl()) ||
6724 isa<IndirectFieldDecl>(DRE->getDecl()) ||
6725 cast<CXXMethodDecl>(DRE->getDecl())
6726 ->isImplicitObjectMemberFunction()) &&
6727 "Only non-static member pointers can make it here");
6729 // Okay: this is the address of a non-static member, and therefore
6731 if (Arg->isTypeDependent() || Arg->isValueDependent()) {
6736 ValueDecl *D = DRE->getDecl();
6739 TemplateArgument(cast<ValueDecl>(D->getCanonicalDecl()),
6746 S.Diag(Arg->getBeginLoc(), diag::err_template_arg_not_pointer_to_member_form)
6747 << Arg->getSourceRange();
6748 S.Diag(DRE->getDecl()->getLocation(), diag::note_template_arg_refers_here);
6758 SourceLocation StartLoc = Arg->getBeginLoc();
6761 DeducedType *DeducedT = ParamType->getContainedDeducedType();
6762 if (getLangOpts().CPlusPlus17 && DeducedT && !DeducedT->isDeduced()) {
6767 // expression is merely instantiation-dependent; is this enough?
6768 if (Arg->isTypeDependent()) {
6770 if (AT && AT->isDecltypeAuto()) {
6779 // the type is dependent, in order to check the types of non-type template
6783 DeductionArg = PE->getPattern();
6785 Context.getTrivialTypeSourceInfo(ParamType, Param->getLocation());
6790 DeductionArg->getBeginLoc(), /*DirectInit*/false, DeductionArg);
6797 TemplateDeductionInfo Info(DeductionArg->getExprLoc(),
6798 Param->getDepth() + 1);
6801 DeduceAutoType(TSI->getTypeLoc(), DeductionArg, ParamType, Info,
6804 // immediately-declared constraint of the auto type is
6813 Diag(Arg->getExprLoc(),
6815 << Param->getDeclName() << Param->getType() << Arg->getType()
6816 << Arg->getSourceRange();
6825 ParamType = CheckNonTypeTemplateParameterType(ParamType, Arg->getExprLoc());
6832 // We should have already dropped all cv-qualifiers by now.
6834 "non-type template parameter type cannot be qualified");
6838 (ParamType->isReferenceType()
6840 Arg->getType())
6841 : !Context.hasSameUnqualifiedType(ParamType, Arg->getType()))) {
6844 // template parameter with some unique (non-dependent) placeholder.
6849 if ((ParamType->isDependentType() || Arg->isTypeDependent()) &&
6850 !Arg->getType()->getContainedDeducedType()) {
6861 << Arg->getType()
6868 // type-dependent, there's nothing we can check now.
6869 if (ParamType->isDependentType() || Arg->isTypeDependent()) {
6873 Arg = PE->getPattern();
6876 ParamType->isLValueReferenceType() ? VK_LValue
6877 : ParamType->isRValueReferenceType() ? VK_XValue
6884 PackExpansionExpr(E.get()->getType(), E.get(), PE->getEllipsisLoc(),
6885 PE->getNumExpansions());
6902 Expr *InnerArg = Arg->IgnoreParenImpCasts();
6903 if (ParamType->isRecordType() && isa<DeclRefExpr>(InnerArg) &&
6904 Context.hasSameUnqualifiedType(ParamType, InnerArg->getType())) {
6905 NamedDecl *ND = cast<DeclRefExpr>(InnerArg)->getDecl();
6910 TemplateArgument(TPO->getCanonicalDecl(), CanonParamType);
6922 // a constant-evaluated context.
6927 if (isa<InitListExpr>(Arg) || ParamType->isRecordType()) {
6929 Arg->getBeginLoc(), /*DirectInit=*/false, Arg);
6940 Arg = ActOnFinishFullExpr(Result.get(), Arg->getBeginLoc(),
6949 // A template-argument for a non-type template parameter shall be
6950 // a converted constant expression of the type of the template-parameter.
6966 // For a value-dependent argument, CheckConvertedConstantExpression is
6968 if (ArgResult.get()->isValueDependent()) {
6985 // For a non-type template-parameter of pointer or reference type,
6987 assert(ParamType->isPointerOrReferenceType() ||
6988 ParamType->isNullPtrType());
6989 // -- a temporary object
6990 // -- a string literal
6991 // -- the result of a typeid expression, or
6992 // -- a predefined __func__ variable
6996 Diag(Arg->getBeginLoc(), diag::err_template_arg_not_decl_ref)
6997 << Arg->getSourceRange();
7002 VD->getType()->isArrayType() &&
7004 !Value.isLValueOnePastTheEnd() && ParamType->isPointerType()) {
7007 cast<ValueDecl>(VD->getCanonicalDecl()), CanonParamType);
7011 // -- a subobject [until C++20]
7019 assert((VD || !ParamType->isReferenceType()) &&
7021 assert((!VD || !ParamType->isNullPtrType()) &&
7022 "non-null value of type nullptr_t?");
7036 // as a non-type template-argument. If a non-type
7037 // template-argument cannot be converted to the type of the
7038 // corresponding template-parameter then the program is
7039 // ill-formed.
7040 if (ParamType->isIntegralOrEnumerationType()) {
7042 // -- for a non-type template-parameter of integral or
7047 // -- for a non-type template-parameter of integral or
7053 // A template-argument for a non-type, non-template template-parameter
7056 // -- for a non-type template-parameter of integral or enumeration
7058 // template-parameter; or
7066 // We can't check arbitrary value-dependent arguments.
7067 if (ArgResult.get()->isValueDependent()) {
7075 // always a no-op, except when the parameter type is bool. In
7078 if (const EnumType *Enum = IntegerType->getAs<EnumType>())
7079 IntegerType = Enum->getDecl()->getIntegerType();
7080 Value = Value.extOrTrunc(IntegerType->isBitIntType()
7095 QualType ArgType = Arg->getType();
7098 // A template-argument for a non-type, non-template
7099 // template-parameter shall be one of:
7101 // -- an integral constant-expression of integral or enumeration
7103 // -- the name of a non-type template-parameter; or
7105 if (!ArgType->isIntegralOrEnumerationType()) {
7106 Diag(Arg->getBeginLoc(), diag::err_template_arg_not_integral_or_enumeral)
7107 << ArgType << Arg->getSourceRange();
7110 } else if (!Arg->isValueDependent()) {
7135 } else if (ParamType->isBooleanType()) {
7136 // This is an integral-to-boolean conversion.
7139 !ParamType->isEnumeralType()) {
7144 Diag(Arg->getBeginLoc(), diag::err_template_arg_not_convertible)
7145 << Arg->getType() << ParamType << Arg->getSourceRange();
7153 if (Arg->isValueDependent()) {
7154 // The argument is value-dependent. Create a new
7163 if (const EnumType *Enum = IntegerType->getAs<EnumType>()) {
7164 IntegerType = Enum->getDecl()->getIntegerType();
7167 if (ParamType->isBooleanType()) {
7173 Value.setIsSigned(IntegerType->isSignedIntegerOrEnumerationType());
7179 unsigned AllowedBits = IntegerType->isBitIntType()
7184 Value.setIsSigned(IntegerType->isSignedIntegerOrEnumerationType());
7187 if (IntegerType->isUnsignedIntegerOrEnumerationType() &&
7189 Diag(Arg->getBeginLoc(), diag::warn_template_arg_negative)
7190 << toString(OldValue, 10) << toString(Value, 10) << Param->getType()
7191 << Arg->getSourceRange();
7197 if (IntegerType->isUnsignedIntegerOrEnumerationType())
7204 Diag(Arg->getBeginLoc(), diag::warn_template_arg_too_large)
7205 << toString(OldValue, 10) << toString(Value, 10) << Param->getType()
7206 << Arg->getSourceRange();
7211 QualType T = ParamType->isEnumeralType() ? ParamType : IntegerType;
7218 QualType ArgType = Arg->getType();
7221 // Handle pointer-to-function, reference-to-function, and
7222 // pointer-to-member-function all in (roughly) the same way.
7223 if (// -- For a non-type template-parameter of type pointer to
7224 // function, only the function-to-pointer conversion (4.3) is
7225 // applied. If the template-argument represents a set of
7228 (ParamType->isPointerType() &&
7229 ParamType->castAs<PointerType>()->getPointeeType()->isFunctionType()) ||
7230 // -- For a non-type template-parameter of type reference to
7231 // function, no conversions apply. If the template-argument
7234 (ParamType->isReferenceType() &&
7235 ParamType->castAs<ReferenceType>()->getPointeeType()->isFunctionType()) ||
7236 // -- For a non-type template-parameter of type pointer to
7238 // template-argument represents a set of overloaded member
7241 (ParamType->isMemberPointerType() &&
7242 ParamType->castAs<MemberPointerType>()->getPointeeType()
7243 ->isFunctionType())) {
7245 if (Arg->getType() == Context.OverloadTy) {
7249 if (DiagnoseUseOfDecl(Fn, Arg->getBeginLoc()))
7256 ArgType = Arg->getType();
7261 if (!ParamType->isMemberPointerType()) {
7275 if (ParamType->isPointerType()) {
7276 // -- for a non-type template-parameter of type pointer to
7278 // array-to-pointer conversion (4.2) are applied.
7280 assert(ParamType->getPointeeType()->isIncompleteOrObjectType() &&
7289 if (const ReferenceType *ParamRefType = ParamType->getAs<ReferenceType>()) {
7290 // -- For a non-type template-parameter of type reference to
7292 // reference may be more cv-qualified than the (otherwise
7293 // identical) type of the template-argument. The
7294 // template-parameter is bound directly to the
7295 // template-argument, which must be an lvalue.
7296 assert(ParamRefType->getPointeeType()->isIncompleteOrObjectType() &&
7299 if (Arg->getType() == Context.OverloadTy) {
7301 ParamRefType->getPointeeType(),
7304 if (DiagnoseUseOfDecl(Fn, Arg->getBeginLoc()))
7310 ArgType = Arg->getType();
7322 if (ParamType->isNullPtrType()) {
7323 if (Arg->isTypeDependent() || Arg->isValueDependent()) {
7332 Diag(Arg->getExprLoc(), diag::err_template_arg_not_convertible)
7333 << Arg->getType() << ParamType;
7341 Diag(Arg->getExprLoc(), diag::warn_cxx98_compat_template_arg_null);
7350 // -- For a non-type template-parameter of type pointer to data
7352 assert(ParamType->isMemberPointerType() && "Only pointers to members remain");
7372 assert(Name.isDependent() && "Non-dependent template isn't a declaration?");
7376 if (Template->isInvalidDecl())
7380 // A template-argument for a template template-parameter shall be
7382 // id-expression. When the template-argument names a class template, only
7398 Diag(Template->getLocation(), diag::note_template_arg_refers_here_func)
7403 // A template-argument matches a template template-parameter P when P
7404 // is at least as specialized as the template-argument A.
7414 Params->getAssociatedConstraints(ParamsAC);
7421 Template->getAssociatedConstraints(TemplateAC);
7431 Diag(Param->getLocation(), diag::note_entity_declared_at) << Param;
7432 Diag(Template->getLocation(), diag::note_entity_declared_at) << Template;
7459 if (ParamRange && ParamRange->isValid()) {
7476 // A non-type template-parameter of type "array of T" or
7479 if (ParamType->isArrayType())
7481 else if (ParamType->isFunctionType())
7484 // For a NULL non-type template argument, return nullptr casted to the
7490 ParamType->getAs<MemberPointerType>()
7500 if (ParamType->isMemberPointerType()) {
7502 // form a suitable pointer-to-member constant.
7503 assert(VD->getDeclContext()->isRecord() &&
7507 = Context.getTypeDeclType(cast<RecordDecl>(VD->getDeclContext()));
7515 SS, DeclarationNameInfo(VD->getDeclName(), Loc), VD);
7520 QualType ElemT(RefExpr.get()->getType()->getArrayElementTypeNoTypeQual(), 0);
7521 if (ParamType->isPointerType() && !ElemT.isNull() &&
7522 Context.hasSimilarType(ElemT, ParamType->getPointeeType())) {
7527 } else if (ParamType->isPointerType() || ParamType->isMemberPointerType()) {
7528 // For any other pointer, take the address (or form a pointer-to-member).
7532 } else if (ParamType->isRecordType()) {
7538 assert(ParamType->isReferenceType() &&
7542 QualType TemplateParamType = NTTP->getType();
7543 const AutoType *AT = TemplateParamType->getAs<AutoType>();
7544 if (AT && AT->isDecltypeAuto()) {
7546 ParamType->getPointeeType(), RefExpr.get()->getValueKind(),
7547 RefExpr.get()->getExprLoc(), RefExpr.get(), VD, NTTP->getIndex(),
7555 assert(ParamType->isReferenceType() == RefExpr.get()->isLValue() &&
7556 "value kind mismatch for non-type template argument");
7561 if (!Context.hasSameType(RefExpr.get()->getType(), DestExprType)) {
7564 if (Context.hasSimilarType(RefExpr.get()->getType(), DestExprType) ||
7565 IsFunctionConversion(RefExpr.get()->getType(), DestExprType, Ignored)) {
7567 } else if (ParamType->isVoidPointerType() &&
7568 RefExpr.get()->getType()->isPointerType()) {
7571 // FIXME: Pointers to members can need conversion derived-to-base or
7572 // base-to-derived conversions. We currently don't retain enough
7576 "unexpected conversion required for non-type template argument");
7579 RefExpr.get()->getValueKind());
7586 /// integral template argument with the given source-location
7594 assert(OrigT->isIntegralOrEnumerationType());
7602 if (const EnumType *ET = OrigT->getAs<EnumType>())
7603 T = ET->getDecl()->getIntegerType();
7606 if (T->isAnyCharacterType()) {
7608 if (T->isWideCharType())
7610 else if (T->isChar8Type() && S.getLangOpts().Char8)
7612 else if (T->isChar16Type())
7614 else if (T->isChar32Type())
7620 } else if (T->isBooleanType()) {
7626 if (OrigT->isEnumeralType()) {
7628 // non-type template parameters.
7640 auto MakeInitList = [&](ArrayRef<Expr *> Elts) -> Expr * {
7642 ILE->setType(T);
7670 QualType ElemT = T->castAs<ComplexType>()->getElementType();
7678 QualType ElemT = T->castAs<ComplexType>()->getElementType();
7687 QualType ElemT = T->castAs<VectorType>()->getElementType();
7700 // There isn't necessarily a valid equivalent source-level syntax for
7706 if (T->isReferenceType()) {
7707 T = T->getPointeeType();
7725 llvm_unreachable("not a non-type template argument");
7752 // Check the actual kind (type, non-type, template).
7753 if (Old->getKind() != New->getKind()) {
7760 S.Diag(New->getLocation(), NextDiag)
7762 S.Diag(Old->getLocation(), diag::note_template_prev_declaration)
7773 if (Old->isTemplateParameterPack() != New->isTemplateParameterPack()) {
7785 S.Diag(New->getLocation(), NextDiag)
7786 << ParamKind << New->isParameterPack();
7787 S.Diag(Old->getLocation(), diag::note_template_parameter_pack_here)
7788 << ParamKind << Old->isParameterPack();
7794 // For non-type template parameters, check the type of the parameter.
7800 // Two [non-type] template-parameters are equivalent [if] they have
7801 // equivalent types ignoring the use of type-constraints for
7803 QualType OldType = S.Context.getUnconstrainedType(OldNTTP->getType());
7804 QualType NewType = S.Context.getUnconstrainedType(NewNTTP->getType());
7813 S.Diag(NewNTTP->getLocation(), NextDiag)
7814 << NewNTTP->getType() << (Kind != Sema::TPL_TemplateMatch);
7815 S.Diag(OldNTTP->getLocation(),
7817 << OldNTTP->getType();
7830 NewInstFrom, NewTTP->getTemplateParameters(), OldInstFrom,
7831 OldTTP->getTemplateParameters(), Complain,
7844 if (const auto *TC = cast<TemplateTypeParmDecl>(New)->getTypeConstraint())
7845 NewC = TC->getImmediatelyDeclaredConstraint();
7846 if (const auto *TC = cast<TemplateTypeParmDecl>(Old)->getTypeConstraint())
7847 OldC = TC->getImmediatelyDeclaredConstraint();
7850 ->getPlaceholderTypeConstraint())
7853 ->getPlaceholderTypeConstraint())
7859 S.Diag(NewC ? NewC->getBeginLoc() : New->getBeginLoc(),
7861 S.Diag(OldC ? OldC->getBeginLoc() : Old->getBeginLoc(),
7897 S.Diag(New->getTemplateLoc(), NextDiag)
7898 << (New->size() > Old->size())
7900 << SourceRange(New->getTemplateLoc(), New->getRAngleLoc());
7901 S.Diag(Old->getTemplateLoc(), diag::note_template_prev_declaration)
7903 << SourceRange(Old->getTemplateLoc(), Old->getRAngleLoc());
7910 if (Old->size() != New->size()) {
7919 // A template-argument matches a template template-parameter (call it P)
7920 // when each of the template parameters in the template-parameter-list of
7921 // the template-argument's corresponding class template or alias template
7923 // template-parameter-list of P. [...]
7924 TemplateParameterList::iterator NewParm = New->begin();
7925 TemplateParameterList::iterator NewParmEnd = New->end();
7926 for (TemplateParameterList::iterator OldParm = Old->begin(),
7927 OldParmEnd = Old->end();
7951 const Expr *NewRC = New->getRequiresClause();
7952 const Expr *OldRC = Old->getRequiresClause();
7955 Diag(NewRC ? NewRC->getBeginLoc() : New->getTemplateLoc(),
7957 Diag(OldRC ? OldRC->getBeginLoc() : Old->getTemplateLoc(),
7986 S = S->getDeclParent();
7991 DeclContext *Ctx = S->getEntity();
7992 if (Ctx && Ctx->isExternCContext()) {
7993 Diag(TemplateParams->getTemplateLoc(), diag::err_template_linkage)
7994 << TemplateParams->getSourceRange();
7995 if (const LinkageSpecDecl *LSD = Ctx->getExternCContext())
7996 Diag(LSD->getExternLoc(), diag::note_extern_c_begins_here);
7999 Ctx = Ctx ? Ctx->getRedeclContext() : nullptr;
8002 // A template-declaration can appear only as a namespace scope or
8011 if (Ctx->isFileContext())
8016 if (RD->isLocalClass())
8017 return Diag(TemplateParams->getTemplateLoc(),
8019 << TemplateParams->getSourceRange();
8025 return Diag(TemplateParams->getTemplateLoc(),
8027 << TemplateParams->getSourceRange();
8037 return Record->getTemplateSpecializationKind();
8039 return Function->getTemplateSpecializationKind();
8041 return Var->getTemplateSpecializationKind();
8046 /// Check whether a specialization is well-formed in the current
8095 S.Diag(Specialized->getLocation(), diag::note_specialized_entity);
8102 if (S.CurContext->getRedeclContext()->isFunctionOrMethod()) {
8112 Specialized->getDeclContext()->getRedeclContext();
8113 DeclContext *DC = S.CurContext->getRedeclContext();
8117 if (!(DC->isFileContext() ? DC->Encloses(SpecializedContext)
8118 : DC->Equals(SpecializedContext))) {
8125 if (S.getLangOpts().MicrosoftExt && !DC->isRecord())
8131 S.Diag(Specialized->getLocation(), diag::note_specialized_entity);
8135 if (DC->isRecord())
8142 static SourceRange findTemplateParameterInType(unsigned Depth, Expr *E) {
8143 if (!E->isTypeDependent())
8145 DependencyChecker Checker(Depth, /*IgnoreNonTypeDependent*/true);
8148 return E->getSourceRange();
8152 static SourceRange findTemplateParameter(unsigned Depth, TypeLoc TL) {
8153 if (!TL.getType()->isDependentType())
8155 DependencyChecker Checker(Depth, /*IgnoreNonTypeDependent*/true);
8163 /// that checks non-type template partial specialization arguments.
8184 ArgExpr = Expansion->getPattern();
8188 ArgExpr = ICE->getSubExpr();
8191 // A non-type argument is non-specialized if it is the name of a
8192 // non-type parameter. All other non-type arguments are
8196 // specialized non-type arguments, so skip any non-specialized
8199 if (isa<NonTypeTemplateParmDecl>(DRE->getDecl()))
8205 // -- A partially specialized non-type argument expression
8209 // -- The type of a template parameter corresponding to a
8210 // specialized non-type argument shall not be dependent on a
8214 // -- A specialized non-type template argument shall not be
8215 // type-dependent and the corresponding template parameter
8216 // shall have a non-dependent type.
8218 findTemplateParameterInType(Param->getDepth(), ArgExpr);
8235 Param->getDepth(), Param->getTypeSourceInfo()->getTypeLoc());
8237 S.Diag(IsDefaultArgument ? TemplateNameLoc : ArgExpr->getBeginLoc(),
8239 << Param->getType();
8253 if (PrimaryTemplate->getDeclContext()->isDependentContext())
8257 PrimaryTemplate->getTemplateParameters();
8258 for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) {
8260 = dyn_cast<NonTypeTemplateParmDecl>(TemplateParams->getParam(I));
8296 if (const auto *DSA = ClassTemplate->getAttr<NoSpecializationsAttr>()) {
8297 auto Message = DSA->getMessage();
8300 Diag(DSA->getLoc(), diag::note_marked_here) << DSA;
8303 if (S->isTemplateParamScope())
8304 EnterTemplatedContext(S, ClassTemplate->getTemplatedDecl());
8306 DeclContext *DC = ClassTemplate->getDeclContext();
8313 diagnoseQualifiedDeclaration(SS, DC, ClassTemplate->getDeclName(),
8335 if (TemplateParams && DC->isDependentContext()) {
8341 if (TemplateParams && TemplateParams->size() > 0) {
8353 for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) {
8354 Decl *Param = TemplateParams->getParam(I);
8356 if (TTP->hasDefaultArgument()) {
8357 Diag(TTP->getDefaultArgumentLoc(),
8359 TTP->removeDefaultArgument();
8363 if (NTTP->hasDefaultArgument()) {
8364 Diag(NTTP->getDefaultArgumentLoc(),
8366 << NTTP->getDefaultArgument().getSourceRange();
8367 NTTP->removeDefaultArgument();
8371 if (TTP->hasDefaultArgument()) {
8372 Diag(TTP->getDefaultArgument().getLocation(),
8374 << TTP->getDefaultArgument().getSourceRange();
8375 TTP->removeDefaultArgument();
8383 SourceRange(TemplateParams->getTemplateLoc(),
8384 TemplateParams->getRAngleLoc()))
8396 if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(), Kind,
8398 ClassTemplate->getIdentifier())) {
8402 ClassTemplate->getTemplatedDecl()->getKindName());
8403 Diag(ClassTemplate->getTemplatedDecl()->getLocation(),
8405 Kind = ClassTemplate->getTemplatedDecl()->getTagKind();
8420 // Check that the template argument list is well-formed for this
8443 << ClassTemplate->getDeclName();
8453 PrevDecl = ClassTemplate->findPartialSpecialization(
8457 ClassTemplate->findSpecialization(CTAI.CanonicalConverted, InsertPos);
8479 ClassTemplate->getInjectedClassNameSpecialization()) &&
8481 !TemplateParams->hasAssociatedConstraints())) {
8484 // -- The argument list of the specialization shall not be identical
8493 S, TagSpec, TUK, KWLoc, SS, ClassTemplate->getIdentifier(),
8496 /*FriendLoc*/ SourceLocation(), TemplateParameterLists.size() - 1,
8507 Partial->setTemplateArgsAsWritten(TemplateArgs);
8510 Partial->setTemplateParameterListsInfo(
8515 ClassTemplate->AddPartialSpecialization(Partial, InsertPos);
8520 if (PrevPartial && PrevPartial->getInstantiatedFromMember())
8521 PrevPartial->setMemberSpecialization();
8530 Specialization->setTemplateArgsAsWritten(TemplateArgs);
8533 Specialization->setTemplateParameterListsInfo(Context,
8538 ClassTemplate->AddSpecialization(Specialization, InsertPos);
8540 if (CurContext->isDependentContext()) {
8555 if (PrevDecl && PrevDecl->getPointOfInstantiation().isValid()) {
8557 for (Decl *Prev = PrevDecl; Prev; Prev = Prev->getPreviousDecl()) {
8570 Diag(PrevDecl->getPointOfInstantiation(),
8572 << (PrevDecl->getTemplateSpecializationKind()
8580 Specialization->setSpecializationKind(TSK_ExplicitSpecialization);
8584 RecordDecl *Def = Specialization->getDefinition();
8587 SkipBody->ShouldSkip = true;
8588 SkipBody->Previous = Def;
8593 Diag(Def->getLocation(), diag::note_previous_definition);
8594 Specialization->setInvalidDecl();
8604 if (TUK == TagUseKind::Definition && (!SkipBody || !SkipBody->ShouldSkip)) {
8610 Diag(Specialization->getLocation(), diag::err_module_private_specialization)
8622 Specialization->setLexicalDeclContext(CurContext);
8625 if (TUK == TagUseKind::Definition && (!SkipBody || !SkipBody->ShouldSkip))
8626 Specialization->startDefinition();
8629 // Build the fully-sugared type for this class template
8631 // itself. This means that we'll pretty-print the type retrieved
8642 Friend->setAccess(AS_public);
8643 CurContext->addDecl(Friend);
8648 CurContext->addDecl(Specialization);
8651 if (SkipBody && SkipBody->ShouldSkip)
8652 return SkipBody->Previous;
8654 Specialization->setInvalidDecl(Invalid);
8672 if (!DC->getRedeclContext()->isFileContext()) {
8685 if (Params->size() == 0) {
8692 for (TemplateParameterList::const_iterator ParamIt = Params->begin(),
8693 ParamEnd = Params->end();
8696 if (Param->isParameterPack()) {
8699 Diag(Param->getLocation(),
8708 if (NewDecl->hasAssociatedConstraints()) {
8712 NewDecl->setInvalidDecl();
8715 DeclarationNameInfo NameInfo(NewDecl->getDeclName(), NewDecl->getBeginLoc());
8750 assert(!C->hasDefinition() && "Concept already defined");
8753 C->setDefinition(ConstraintExpr);
8757 DeclarationNameInfo NameInfo(C->getDeclName(), C->getBeginLoc());
8781 auto *OldConcept = dyn_cast<ConceptDecl>(Previous.getRepresentativeDecl()->getUnderlyingDecl());
8784 Diag(NewDecl->getLocation(), diag::err_redefinition_different_kind)
8785 << NewDecl->getDeclName();
8786 notePreviousDefinition(Old, NewDecl->getLocation());
8793 Diag(NewDecl->getLocation(), diag::err_redefinition_different_concept)
8794 << NewDecl->getDeclName();
8795 notePreviousDefinition(OldConcept, NewDecl->getLocation());
8801 Diag(NewDecl->getLocation(), diag::err_redefinition)
8802 << NewDecl->getDeclName();
8803 notePreviousDefinition(OldConcept, NewDecl->getLocation());
8813 Context.setPrimaryMergedDecl(NewDecl, OldConcept->getCanonicalDecl());
8818 if (!Concept->isInvalidDecl() && !Concept->hasDefinition()) {
8820 Diag(Concept->getLocation(), diag::note_declared_at);
8830 cast<FunctionDecl>(D)->isFunctionTemplateSpecialization()))
8831 D->dropAttrs<DLLImportAttr, DLLExportAttr>();
8834 FD->setInlineSpecified(false);
8846 Prev = Prev->getPreviousDecl()) {
8847 PrevDiagLoc = Prev->getLocation();
8904 for (Decl *Prev = PrevDecl; Prev; Prev = Prev->getPreviousDecl()) {
8975 Diag(PrevDecl->getLocation(),
8989 for (Decl *Prev = PrevDecl; Prev; Prev = Prev->getPreviousDecl()) {
9001 // For a given template and a given set of template-arguments,
9002 // - an explicit instantiation definition shall appear at most once
9025 DeclContext *FDLookupContext = FD->getDeclContext()->getRedeclContext();
9030 NamedDecl *D = F.next()->getUnderlyingDecl();
9037 if (!FDLookupContext->InEnclosingNamespaceSetOf(
9038 D->getDeclContext()->getRedeclContext())) {
9046 bool IsFriend = FD->getFriendObjectKind() != Decl::FOK_None;
9048 Diag(FD->getLocation(), diag::err_dependent_function_template_spec_no_match)
9051 Diag(P.second->getLocation(),
9057 FD->setDependentTemplateSpecialization(Context, Previous.asUnresolvedSet(),
9068 TemplateSpecCandidateSet FailedCandidates(FD->getLocation(),
9074 DeclContext *FDLookupContext = FD->getDeclContext()->getRedeclContext();
9077 NamedDecl *Ovl = (*I)->getUnderlyingDecl();
9081 if (!FDLookupContext->InEnclosingNamespaceSetOf(
9082 Ovl->getDeclContext()->getRedeclContext()))
9085 QualType FT = FD->getType();
9087 // A constexpr specifier for a non-static member function that is not
9096 !getLangOpts().CPlusPlus14 && NewMD && NewMD->isConstexpr() &&
9098 auto *OldMD = dyn_cast<CXXMethodDecl>(FunTmpl->getTemplatedDecl());
9099 if (OldMD && OldMD->isConst()) {
9100 const FunctionProtoType *FPT = FT->castAs<FunctionProtoType>();
9101 FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo();
9103 FT = Context.getFunctionType(FPT->getReturnType(),
9104 FPT->getParamTypes(), EPI);
9113 // A trailing template-argument can be left unspecified in the
9114 // template-id naming an explicit function template specialization
9122 cast<FunctionTemplateDecl>(FunTmpl->getFirstDecl()),
9128 I.getPair(), FunTmpl->getTemplatedDecl(),
9144 I.getPair(), FunTmpl->getTemplatedDecl(),
9159 // non-template) candidates.
9161 Diag(FD->getLocation(), diag::err_qualified_friend_no_match)
9162 << FD->getDeclName() << FDLookupContext;
9166 if (auto *OldFD = dyn_cast<FunctionDecl>(OldND->getUnderlyingDecl()))
9167 NoteOverloadCandidate(OldND, OldFD, CRK_None, FD->getType(), false);
9169 FailedCandidates.NoteCandidates(*this, FD->getLocation());
9175 Candidates.begin(), Candidates.end(), FailedCandidates, FD->getLocation(),
9176 PDiag(diag::err_function_template_spec_no_match) << FD->getDeclName(),
9178 << FD->getDeclName() << (ExplicitTemplateArgs != nullptr),
9187 if (const auto *PT = Specialization->getPrimaryTemplate();
9188 const auto *DSA = PT->getAttr<NoSpecializationsAttr>()) {
9189 auto Message = DSA->getMessage();
9190 Diag(FD->getLocation(), diag::warn_invalid_specialization)
9192 Diag(DSA->getLoc(), diag::note_marked_here) << DSA;
9197 // - [...]
9198 // - the exception specification is compared to that of another declaration
9200 // - [...]
9203 // described above only when needed; similarly, the noexcept-specifier of a
9221 Specialization->getType()->castAs<FunctionProtoType>();
9225 isUnresolvedExceptionSpec(SpecializationFPT->getExceptionSpecType()) &&
9226 !ResolveExceptionSpec(FD->getLocation(), SpecializationFPT))
9230 = Specialization->getTemplateSpecializationInfo();
9235 TemplateSpecializationKind TSK = SpecInfo->getTemplateSpecializationKind();
9237 Specialization->setLocation(FD->getLocation());
9238 Specialization->setLexicalDeclContext(FD->getLexicalDeclContext());
9245 Specialization->setConstexprKind(FD->getConstexprKind());
9253 bool isFriend = (FD->getFriendObjectKind() != Decl::FOK_None);
9258 Specialization->getPrimaryTemplate(),
9259 Specialization, FD->getLocation(),
9271 CheckSpecializationInstantiationRedecl(FD->getLocation(),
9274 SpecInfo->getTemplateSpecializationKind(),
9275 SpecInfo->getPointOfInstantiation(),
9283 // primary function template - check if the 'specialization' that was
9289 // flag to not-deleted, so that we can inherit that information from 'FD'.
9290 if (Specialization->isDeleted() && !SpecInfo->isExplicitSpecialization() &&
9291 !Specialization->getCanonicalDecl()->isReferenced()) {
9294 Specialization->getCanonicalDecl() == Specialization &&
9297 Specialization->setDeletedAsWritten(false);
9300 SpecInfo->setTemplateSpecializationKind(TSK_ExplicitSpecialization);
9309 Context, Specialization->getTemplateSpecializationArgs()->asArray());
9310 FD->setFunctionTemplateSpecialization(
9311 Specialization->getPrimaryTemplate(), TemplArgs, /*InsertPos=*/nullptr,
9312 SpecInfo->getTemplateSpecializationKind(),
9321 CUDA().inheritTargetAttrs(FD, *Specialization->getPrimaryTemplate());
9332 assert(!Member->isTemplateDecl() && !Member->getDescribedTemplate() &&
9333 "Only for non-template members");
9346 auto *Method = dyn_cast<CXXMethodDecl>(Candidate->getUnderlyingDecl());
9351 QualType Adjusted = Function->getType();
9353 Adjusted = adjustCCAndNoReturn(Adjusted, Method->getType());
9359 if (!Context.hasSameType(Adjusted, Method->getType()))
9364 Method->getTrailingRequiresClause() &&
9366 /*UsageLoc=*/Member->getLocation(),
9384 auto *Method = cast<CXXMethodDecl>(I->getUnderlyingDecl());
9394 InstantiatedFrom = BestMethod->getInstantiatedFromMemberFunction();
9395 MSInfo = BestMethod->getMemberSpecializationInfo();
9401 auto *Method = cast<CXXMethodDecl>(I->getUnderlyingDecl());
9410 Diag(Member->getLocation(), diag::err_function_member_spec_ambiguous)
9413 Candidate = Candidate->getUnderlyingDecl();
9414 Diag(Candidate->getLocation(), diag::note_function_member_spec_matched)
9423 if (PrevVar->isStaticDataMember()) {
9426 InstantiatedFrom = PrevVar->getInstantiatedFromStaticDataMember();
9427 MSInfo = PrevVar->getMemberSpecializationInfo();
9435 InstantiatedFrom = PrevRecord->getInstantiatedFromMemberClass();
9436 MSInfo = PrevRecord->getMemberSpecializationInfo();
9444 InstantiatedFrom = PrevEnum->getInstantiatedFromMemberEnum();
9445 MSInfo = PrevEnum->getMemberSpecializationInfo();
9451 // specializations are always out-of-line, the caller will complain about
9461 if (Member->getFriendObjectKind() != Decl::FOK_None) {
9464 cast<CXXMethodDecl>(Member)->setInstantiationOfMemberFunction(
9466 cast<CXXMethodDecl>(Instantiation)->getTemplateSpecializationKind());
9468 cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass(
9470 cast<CXXRecordDecl>(Instantiation)->getTemplateSpecializationKind());
9480 Diag(Member->getLocation(), diag::err_spec_member_not_instantiated)
9482 Diag(Instantiation->getLocation(), diag::note_specialized_decl);
9495 if (CheckSpecializationInstantiationRedecl(Member->getLocation(),
9498 MSInfo->getTemplateSpecializationKind(),
9499 MSInfo->getPointOfInstantiation(),
9506 Instantiation, Member->getLocation(),
9514 if (InstantiationFunction->getTemplateSpecializationKind() ==
9518 if (InstantiationFunction->isDeleted()) {
9520 assert(InstantiationFunction->getCanonicalDecl() ==
9523 InstantiationFunction->setDeletedAsWritten(false);
9527 MemberFunction->setInstantiationOfMemberFunction(
9530 MemberVar->setInstantiationOfStaticDataMember(
9533 MemberClass->setInstantiationOfMemberClass(
9536 MemberEnum->setInstantiationOfMemberEnum(
9557 if (OrigD->getTemplateSpecializationKind() != TSK_ImplicitInstantiation)
9561 // FIXME: If there are multiple in-class declarations of the member (from
9564 OrigD->setTemplateSpecializationKind(TSK_ExplicitSpecialization);
9565 OrigD->setLocation(Loc);
9570 NamedDecl *Instantiation = cast<NamedDecl>(Member->getCanonicalDecl());
9575 completeMemberSpecializationImpl(*this, Function, Member->getLocation());
9577 completeMemberSpecializationImpl(*this, Var, Member->getLocation());
9579 completeMemberSpecializationImpl(*this, Record, Member->getLocation());
9581 completeMemberSpecializationImpl(*this, Enum, Member->getLocation());
9592 DeclContext *OrigContext= D->getDeclContext()->getEnclosingNamespaceContext();
9593 DeclContext *CurContext = S.CurContext->getRedeclContext();
9595 if (CurContext->isRecord()) {
9610 if (CurContext->Encloses(OrigContext))
9613 if (CurContext->InEnclosingNamespaceSetOf(OrigContext))
9636 S.Diag(D->getLocation(), diag::note_explicit_instantiation_here);
9649 D->getFormalLinkage() == Linkage::Internal) {
9663 /// Determine whether the given scope specifier has a template-id in it.
9671 // the class template specialization in the qualified-id for the member
9672 // name shall be a simple-template-id.
9676 NNS = NNS->getPrefix())
9677 if (const Type *T = NNS->getAsType())
9699 for (auto &B : Def->bases()) {
9701 B.getType()->getAsCXXRecordDecl()))
9729 Diag(TD->getLocation(), diag::note_previous_use);
9733 if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(),
9735 ClassTemplate->getIdentifier())) {
9739 ClassTemplate->getTemplatedDecl()->getKindName());
9740 Diag(ClassTemplate->getTemplatedDecl()->getLocation(),
9742 Kind = ClassTemplate->getTemplatedDecl()->getTagKind();
9766 if (auto *A = ClassTemplate->getTemplatedDecl()->getAttr<DLLExportAttr>()) {
9769 Diag(A->getLocation(), diag::note_attribute);
9780 ClassTemplate->getTemplatedDecl()->getAttr<DLLImportAttr>();
9800 // Check that the template argument list is well-formed for this
9813 ClassTemplate->findSpecialization(CTAI.CanonicalConverted, InsertPos);
9816 = PrevDecl ? PrevDecl->getTemplateSpecializationKind() : TSK_Undeclared;
9841 PrevDecl->getPointOfInstantiation(),
9856 Specialization->setLocation(TemplateNameLoc);
9871 Context, Kind, ClassTemplate->getDeclContext(), KWLoc, TemplateNameLoc,
9878 if (const auto *A = PrevDecl->getAttr<MSInheritanceAttr>()) {
9879 auto *Clone = A->clone(getASTContext());
9880 Clone->setInherited(true);
9881 Specialization->addAttr(Clone);
9888 ClassTemplate->AddSpecialization(Specialization, InsertPos);
9892 Specialization->setTemplateArgsAsWritten(TemplateArgs);
9895 Specialization->setExternKeywordLoc(ExternLoc);
9896 Specialization->setTemplateKeywordLoc(TemplateLoc);
9897 Specialization->setBraceRange(SourceRange());
9899 bool PreviouslyDLLExported = Specialization->hasAttr<DLLExportAttr>();
9906 Specialization->setLexicalDeclContext(CurContext);
9907 CurContext->addDecl(Specialization);
9912 Specialization->setTemplateSpecializationKind(TSK);
9925 Specialization->getDefinition());
9932 Specialization->setPointOfInstantiation(Def->getPointOfInstantiation());
9937 Specialization->getDefinition());
9939 TemplateSpecializationKind Old_TSK = Def->getTemplateSpecializationKind();
9946 Def->setTemplateSpecializationKind(TSK);
9954 getDLLAttr(Specialization)->clone(getASTContext()));
9955 A->setInherited(true);
9956 Def->addAttr(A);
9964 !PreviouslyDLLExported && Specialization->hasAttr<DLLExportAttr>();
9988 PrevDecl->hasAttr<DLLExportAttr>()) {
9994 Specialization->setTemplateSpecializationKind(TSK);
9999 Specialization->setTemplateSpecializationKind(TSK);
10027 assert(!Tag->isEnum() && "shouldn't see enumerations here");
10029 if (Tag->isInvalidDecl())
10033 CXXRecordDecl *Pattern = Record->getInstantiatedFromMemberClass();
10037 Diag(Record->getLocation(), diag::note_nontemplate_decl_here);
10043 // elaborated-type-specifier in the declaration shall include a
10044 // simple-template-id.
10063 = cast_or_null<CXXRecordDecl>(Record->getPreviousDecl());
10064 if (!PrevDecl && Record->getDefinition())
10067 MemberSpecializationInfo *MSInfo = PrevDecl->getMemberSpecializationInfo();
10072 MSInfo->getTemplateSpecializationKind(),
10073 MSInfo->getPointOfInstantiation(),
10081 = cast_or_null<CXXRecordDecl>(Record->getDefinition());
10087 = cast_or_null<CXXRecordDecl>(Pattern->getDefinition());
10090 << 0 << Record->getDeclName() << Record->getDeclContext();
10091 Diag(Pattern->getLocation(), diag::note_forward_declaration)
10100 RecordDef = cast_or_null<CXXRecordDecl>(Record->getDefinition());
10138 S = S->getDeclParent();
10142 QualType R = T->getType();
10147 // A storage-class-specifier shall not be specified in [...] an explicit
10173 if (D.getDeclSpec().hasConstexprSpecifier() && R->isFunctionType())
10174 // FIXME: Add a fix-it to remove the 'constexpr' and add a 'const' if one is
10199 if (!R->isFunctionType()) {
10214 if (!Prev || !Prev->isStaticDataMember()) {
10220 Diag((*P)->getLocation(), diag::note_explicit_instantiation_here);
10224 if (!Prev->getInstantiatedFromStaticDataMember()) {
10229 Diag(Prev->getLocation(), diag::note_explicit_instantiation_here);
10238 // explicitly allowed in this section is ill-formed.
10240 // This includes auto-typed variable template instantiations.
10241 if (R->isUndeducedType()) {
10242 Diag(T->getTypeLoc().getBeginLoc(),
10249 // If the explicit instantiation is for a variable, the unqualified-id
10250 // in the declaration shall be a template-id.
10254 Diag(PrevTemplate->getLocation(),
10284 // the class template specialization in the qualified-id for the member
10285 // name shall be a simple-template-id.
10290 // template-id is in the unqualified-id instead.
10299 TemplateSpecializationKind PrevTSK = Prev->getTemplateSpecializationKind();
10300 SourceLocation POI = Prev->getPointOfInstantiation();
10308 Prev->setTemplateSpecializationKind(TSK, D.getIdentifierLoc());
10310 VTSD->setExternKeywordLoc(ExternLoc);
10311 VTSD->setTemplateKeywordLoc(TemplateLoc);
10324 if (PrevTemplate && !Context.hasSameType(Prev->getType(), R)) {
10325 Diag(T->getTypeLoc().getBeginLoc(),
10327 << 0 << PrevTemplate << R << Prev->getType();
10328 Diag(PrevTemplate->getLocation(), diag::note_template_declared_here)
10329 << 2 << PrevTemplate->getDeclName();
10337 // If the declarator is a template-id, translate the parser's template
10360 QualType Adjusted = adjustCCAndNoReturn(R, Method->getType(),
10362 if (Context.hasSameUnqualifiedType(Method->getType(), Adjusted)) {
10363 if (Method->getPrimaryTemplate()) {
10371 if (Method->getTrailingRequiresClause() &&
10393 // Keep track of almost-matches.
10395 P.getPair(), FunTmpl->getTemplatedDecl(),
10411 P.getPair(), FunTmpl->getTemplatedDecl(),
10429 Specialization = cast<FunctionDecl>(Best->Function);
10466 // In an explicit instantiation an exception-specification may be specified,
10468 // If an exception-specification is specified in an explicit instantiation
10469 // directive, it shall be compatible with the exception-specifications of
10471 if (auto *FPT = R->getAs<FunctionProtoType>())
10472 if (FPT->hasExceptionSpec()) {
10478 PDiag(DiagID) << Specialization->getType(),
10480 Specialization->getType()->getAs<FunctionProtoType>(),
10481 Specialization->getLocation(), FPT, D.getBeginLoc());
10488 if (Specialization->getTemplateSpecializationKind() == TSK_Undeclared) {
10492 << (Specialization->getTemplateSpecializationKind() ==
10494 Diag(Specialization->getLocation(), diag::note_explicit_instantiation_here);
10498 FunctionDecl *PrevDecl = Specialization->getPreviousDecl();
10499 if (!PrevDecl && Specialization->isThisDeclarationADefinition())
10506 PrevDecl->getTemplateSpecializationKind(),
10507 PrevDecl->getPointOfInstantiation(),
10523 if (Specialization->hasAttr<InternalLinkageAttr>() &&
10525 if (auto *RD = dyn_cast<CXXRecordDecl>(Specialization->getDeclContext()))
10526 if (RD->getIdentifier() && RD->getIdentifier()->isStr("valarray") &&
10527 RD->isInStdNamespace())
10537 Specialization->hasAttr<DLLImportAttr>() &&
10541 Specialization->setTemplateSpecializationKind(TSK, D.getIdentifierLoc());
10543 if (Specialization->isDefined()) {
10553 // the class template specialization in the qualified-id for the member
10554 // name shall be a simple-template-id.
10557 FunctionTemplateDecl *FunTmpl = Specialization->getPrimaryTemplate();
10568 : Specialization->getInstantiatedFromMemberFunction(),
10600 // Create type-source location information for this type.
10617 if (TypenameLoc.isValid() && S && !S->getTemplateParamParent())
10645 if (TypenameLoc.isValid() && S && !S->getTemplateParamParent())
10652 // Strangely, non-type results are not ignored by this lookup, so the
10653 // program is ill-formed if it finds an injected-class-name.
10657 if (LookupRD && LookupRD->getIdentifier() == TemplateII) {
10660 << TemplateII << 0 /*injected-class-name used as template name*/
10672 assert(DTN && "dependent template has non-dependent name?");
10673 assert(DTN->getQualifier() == SS.getScopeRep());
10675 if (!DTN->isIdentifier()) {
10682 ElaboratedTypeKeyword::Typename, DTN->getQualifier(),
10683 DTN->getIdentifier(), TemplateArgs.arguments());
10685 // Create source-location information for this type.
10704 // Provide source-location information for the template specialization type.
10733 // ... within an explicitly-written template specialization...
10734 if (!NNS || !NNS.getNestedNameSpecifier()->getAsType())
10745 EnableIfTST->getTemplateName().getAsTemplateDecl();
10746 if (!EnableIfDecl || EnableIfTST->isIncompleteType())
10751 EnableIfDecl->getDeclName().getAsIdentifierInfo();
10752 if (!EnableIfII || !EnableIfII->isStr("enable_if"))
10767 if (isa<CXXBoolLiteralExpr>(Cond->IgnoreParenCasts()))
10789 (*TSI)->getTypeLoc().castAs<DependentNameTypeLoc>();
10794 ElaboratedTypeLoc TL = (*TSI)->getTypeLoc().castAs<ElaboratedTypeLoc>();
10817 // If the nested-name-specifier is dependent and couldn't be
10819 assert(QualifierLoc.getNestedNameSpecifier()->isDependent());
10825 // If the nested-name-specifier refers to the current instantiation,
10827 // program is actually ill-formed. However, DR 382 (in C++0x CD1)
10858 Diag(FailedCond->getExprLoc(),
10861 << FailedCond->getSourceRange();
10885 SourceLocation Loc = Using->getQualifierLoc().getBeginLoc();
10904 // nested-name-specifier nominates a class C, if the name specified
10905 // after the nested-name-specifier, when looked up in C, is the
10906 // injected-class-name of C [...] then the name is instead considered
10909 // Unlike in an elaborated-type-specifier, function names are not ignored
10910 // in typename-specifier lookup. However, they are ignored in all the
10912 // mem-initializer-ids, base-specifiers, and elaborated-type-specifiers).
10914 // FIXME: That's not strictly true: mem-initializer-id lookup does not
10919 FoundRD->isInjectedClassName() &&
10920 declaresSameEntity(LookupRD, cast<Decl>(FoundRD->getParent())))
10925 // typename-specifier was just sugar.
10926 MarkAnyDeclReferenced(Type->getLocation(), Type, /*OdrUse=*/false);
10933 // A type-specifier of the form
10934 // typename[opt] nested-name-specifier[opt] template-name
10940 ? QualifierLoc.getNestedNameSpecifier()->getAsType()
10982 Diag(Referenced->getLocation(),
11011 return T.isNull() || !T->isInstantiationDependentType();
11024 this->Loc = Loc;
11025 this->Entity = Entity;
11038 if (!T || !T->getType()->isInstantiationDependentType())
11046 CurrentInstantiationRebuilder Rebuilder(*this, E->getExprLoc(),
11069 for (unsigned I = 0, N = Params->size(); I != N; ++I) {
11070 Decl *Param = Params->getParam(I);
11080 TTP->getTemplateParameters()))
11086 // Rebuild the type of a non-type template parameter.
11089 = RebuildTypeInCurrentInstantiation(NTTP->getTypeSourceInfo(),
11090 NTTP->getLocation(),
11091 NTTP->getDeclName());
11095 if (NewTSI->getType()->isUndeducedType()) {
11097 // An id-expression is type-dependent if it contains
11098 // - an identifier associated by name lookup with a non-type
11099 // template-parameter declared with a type that contains a
11104 if (NewTSI != NTTP->getTypeSourceInfo()) {
11105 NTTP->setTypeSourceInfo(NewTSI);
11106 NTTP->setType(NewTSI->getType());
11126 if (!Params || Params->size() == 0 || NumArgs == 0)
11129 for (unsigned I = 0, N = Params->size(); I != N; ++I) {
11138 if (const IdentifierInfo *Id = Params->getParam(I)->getIdentifier()) {
11139 Out << Id->getName();
11162 LPT->Toks.swap(Toks);
11163 LPT->D = FnD;
11164 LPT->FPO = getCurFPFeatures();
11167 FD->setLateTemplateParsed(true);
11173 FD->setLateTemplateParsed(false);
11181 const FunctionDecl *FD = RD->isLocalClass();
11182 return (FD && FD->getTemplatedKind() != FunctionDecl::TK_NonTemplate);
11183 } else if (DC->isTranslationUnit() || DC->isNamespace())
11186 DC = DC->getParent();
11243 S.diagnoseMissingImport(Loc, D, D->getLocation(), Modules, Kind, Recover);
11278 if (Spec->getTemplateSpecializationKind() == TSK_ExplicitSpecialization) {
11279 IsHiddenExplicitSpecialization = Spec->getMemberSpecializationInfo()
11287 diagnose(Spec->getMostRecentDecl(), false);
11291 if (auto *TD = FD->getPrimaryTemplate())
11300 auto From = SD->getSpecializedTemplateOrPartial();
11316 auto From = SD->getSpecializedTemplateOrPartial();
11331 if (TD->isMemberSpecialization()) {
11333 diagnose(TD->getMostRecentDecl(), false);
11360 return N->getLocation();
11362 if (!FD->isFunctionTemplateSpecialization())
11363 return FD->getLocation();
11366 return N->getLocation();
11373 return N->getLocation();