SemaTemplate.cpp revision 24bae92f08ae098cc50a602d8cf1273b423e14da
13ed852eea50f9d4cd633efb8c2b054b8e33c253cristy//===------- SemaTemplate.cpp - Semantic Analysis for C++ Templates -------===/ 23ed852eea50f9d4cd633efb8c2b054b8e33c253cristy// 33ed852eea50f9d4cd633efb8c2b054b8e33c253cristy// The LLVM Compiler Infrastructure 43ed852eea50f9d4cd633efb8c2b054b8e33c253cristy// 53ed852eea50f9d4cd633efb8c2b054b8e33c253cristy// This file is distributed under the University of Illinois Open Source 63ed852eea50f9d4cd633efb8c2b054b8e33c253cristy// License. See LICENSE.TXT for details. 73ed852eea50f9d4cd633efb8c2b054b8e33c253cristy//===----------------------------------------------------------------------===/ 83ed852eea50f9d4cd633efb8c2b054b8e33c253cristy// 93ed852eea50f9d4cd633efb8c2b054b8e33c253cristy// This file implements semantic analysis for C++ templates. 103ed852eea50f9d4cd633efb8c2b054b8e33c253cristy//===----------------------------------------------------------------------===/ 113ed852eea50f9d4cd633efb8c2b054b8e33c253cristy 123ed852eea50f9d4cd633efb8c2b054b8e33c253cristy#include "Sema.h" 133ed852eea50f9d4cd633efb8c2b054b8e33c253cristy#include "Lookup.h" 143ed852eea50f9d4cd633efb8c2b054b8e33c253cristy#include "TreeTransform.h" 153ed852eea50f9d4cd633efb8c2b054b8e33c253cristy#include "clang/AST/ASTContext.h" 163ed852eea50f9d4cd633efb8c2b054b8e33c253cristy#include "clang/AST/Expr.h" 173ed852eea50f9d4cd633efb8c2b054b8e33c253cristy#include "clang/AST/ExprCXX.h" 183ed852eea50f9d4cd633efb8c2b054b8e33c253cristy#include "clang/AST/DeclFriend.h" 197e41fe84a841d7b9d7b36b245b65e9dcb3314943cristy#include "clang/AST/DeclTemplate.h" 203ed852eea50f9d4cd633efb8c2b054b8e33c253cristy#include "clang/Parse/DeclSpec.h" 213ed852eea50f9d4cd633efb8c2b054b8e33c253cristy#include "clang/Parse/Template.h" 223ed852eea50f9d4cd633efb8c2b054b8e33c253cristy#include "clang/Basic/LangOptions.h" 233ed852eea50f9d4cd633efb8c2b054b8e33c253cristy#include "clang/Basic/PartialDiagnostic.h" 243ed852eea50f9d4cd633efb8c2b054b8e33c253cristy#include "llvm/ADT/StringExtras.h" 253ed852eea50f9d4cd633efb8c2b054b8e33c253cristyusing namespace clang; 263ed852eea50f9d4cd633efb8c2b054b8e33c253cristy 273ed852eea50f9d4cd633efb8c2b054b8e33c253cristy/// \brief Determine whether the declaration found is acceptable as the name 283ed852eea50f9d4cd633efb8c2b054b8e33c253cristy/// of a template and, if so, return that template declaration. Otherwise, 293ed852eea50f9d4cd633efb8c2b054b8e33c253cristy/// returns NULL. 303ed852eea50f9d4cd633efb8c2b054b8e33c253cristystatic NamedDecl *isAcceptableTemplateName(ASTContext &Context, 313ed852eea50f9d4cd633efb8c2b054b8e33c253cristy NamedDecl *Orig) { 323ed852eea50f9d4cd633efb8c2b054b8e33c253cristy NamedDecl *D = Orig->getUnderlyingDecl(); 333ed852eea50f9d4cd633efb8c2b054b8e33c253cristy 343ed852eea50f9d4cd633efb8c2b054b8e33c253cristy if (isa<TemplateDecl>(D)) 353ed852eea50f9d4cd633efb8c2b054b8e33c253cristy return Orig; 363ed852eea50f9d4cd633efb8c2b054b8e33c253cristy 373ed852eea50f9d4cd633efb8c2b054b8e33c253cristy if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) { 383ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // C++ [temp.local]p1: 393ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // Like normal (non-template) classes, class templates have an 403ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // injected-class-name (Clause 9). The injected-class-name 413ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // can be used with or without a template-argument-list. When 423ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // it is used without a template-argument-list, it is 433ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // equivalent to the injected-class-name followed by the 443ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // template-parameters of the class template enclosed in 45e7e40559f0408c5f987aec5c1a51c30b0456509fcristy // <>. When it is used with a template-argument-list, it 46316d51773c093e74e15de805e8bf620d6b56bc8bcristy // refers to the specified class template specialization, 473ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // which could be the current specialization or another 483ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // specialization. 493ed852eea50f9d4cd633efb8c2b054b8e33c253cristy if (Record->isInjectedClassName()) { 503ed852eea50f9d4cd633efb8c2b054b8e33c253cristy Record = cast<CXXRecordDecl>(Record->getDeclContext()); 513ed852eea50f9d4cd633efb8c2b054b8e33c253cristy if (Record->getDescribedClassTemplate()) 523ed852eea50f9d4cd633efb8c2b054b8e33c253cristy return Record->getDescribedClassTemplate(); 533ed852eea50f9d4cd633efb8c2b054b8e33c253cristy 543ed852eea50f9d4cd633efb8c2b054b8e33c253cristy if (ClassTemplateSpecializationDecl *Spec 553ed852eea50f9d4cd633efb8c2b054b8e33c253cristy = dyn_cast<ClassTemplateSpecializationDecl>(Record)) 563ed852eea50f9d4cd633efb8c2b054b8e33c253cristy return Spec->getSpecializedTemplate(); 573ed852eea50f9d4cd633efb8c2b054b8e33c253cristy } 583ed852eea50f9d4cd633efb8c2b054b8e33c253cristy 593ed852eea50f9d4cd633efb8c2b054b8e33c253cristy return 0; 603ed852eea50f9d4cd633efb8c2b054b8e33c253cristy } 613ed852eea50f9d4cd633efb8c2b054b8e33c253cristy 623ed852eea50f9d4cd633efb8c2b054b8e33c253cristy return 0; 633ed852eea50f9d4cd633efb8c2b054b8e33c253cristy} 643ed852eea50f9d4cd633efb8c2b054b8e33c253cristy 653ed852eea50f9d4cd633efb8c2b054b8e33c253cristystatic void FilterAcceptableTemplateNames(ASTContext &C, LookupResult &R) { 663ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // The set of class templates we've already seen. 673ed852eea50f9d4cd633efb8c2b054b8e33c253cristy llvm::SmallPtrSet<ClassTemplateDecl *, 8> ClassTemplates; 683ed852eea50f9d4cd633efb8c2b054b8e33c253cristy LookupResult::Filter filter = R.makeFilter(); 693ed852eea50f9d4cd633efb8c2b054b8e33c253cristy while (filter.hasNext()) { 703ed852eea50f9d4cd633efb8c2b054b8e33c253cristy NamedDecl *Orig = filter.next(); 713ed852eea50f9d4cd633efb8c2b054b8e33c253cristy NamedDecl *Repl = isAcceptableTemplateName(C, Orig); 723ed852eea50f9d4cd633efb8c2b054b8e33c253cristy if (!Repl) 733ed852eea50f9d4cd633efb8c2b054b8e33c253cristy filter.erase(); 743ed852eea50f9d4cd633efb8c2b054b8e33c253cristy else if (Repl != Orig) { 753ed852eea50f9d4cd633efb8c2b054b8e33c253cristy 763ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // C++ [temp.local]p3: 773ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // A lookup that finds an injected-class-name (10.2) can result in an 783ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // ambiguity in certain cases (for example, if it is found in more than 793ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // one base class). If all of the injected-class-names that are found 803ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // refer to specializations of the same class template, and if the name 813ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // is followed by a template-argument-list, the reference refers to the 823ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // class template itself and not a specialization thereof, and is not 833ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // ambiguous. 843ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // 853ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // FIXME: Will we eventually have to do the same for alias templates? 863ed852eea50f9d4cd633efb8c2b054b8e33c253cristy if (ClassTemplateDecl *ClassTmpl = dyn_cast<ClassTemplateDecl>(Repl)) 873ed852eea50f9d4cd633efb8c2b054b8e33c253cristy if (!ClassTemplates.insert(ClassTmpl)) { 883ed852eea50f9d4cd633efb8c2b054b8e33c253cristy filter.erase(); 893ed852eea50f9d4cd633efb8c2b054b8e33c253cristy continue; 903ed852eea50f9d4cd633efb8c2b054b8e33c253cristy } 913ed852eea50f9d4cd633efb8c2b054b8e33c253cristy 923ed852eea50f9d4cd633efb8c2b054b8e33c253cristy filter.replace(Repl); 933ed852eea50f9d4cd633efb8c2b054b8e33c253cristy } 943ed852eea50f9d4cd633efb8c2b054b8e33c253cristy } 953ed852eea50f9d4cd633efb8c2b054b8e33c253cristy filter.done(); 963ed852eea50f9d4cd633efb8c2b054b8e33c253cristy} 973ed852eea50f9d4cd633efb8c2b054b8e33c253cristy 983ed852eea50f9d4cd633efb8c2b054b8e33c253cristyTemplateNameKind Sema::isTemplateName(Scope *S, 993ed852eea50f9d4cd633efb8c2b054b8e33c253cristy CXXScopeSpec &SS, 1003ed852eea50f9d4cd633efb8c2b054b8e33c253cristy UnqualifiedId &Name, 1013ed852eea50f9d4cd633efb8c2b054b8e33c253cristy TypeTy *ObjectTypePtr, 1023ed852eea50f9d4cd633efb8c2b054b8e33c253cristy bool EnteringContext, 1033ed852eea50f9d4cd633efb8c2b054b8e33c253cristy TemplateTy &TemplateResult, 1043ed852eea50f9d4cd633efb8c2b054b8e33c253cristy bool &MemberOfUnknownSpecialization) { 1053ed852eea50f9d4cd633efb8c2b054b8e33c253cristy assert(getLangOptions().CPlusPlus && "No template names in C!"); 1063ed852eea50f9d4cd633efb8c2b054b8e33c253cristy 1073ed852eea50f9d4cd633efb8c2b054b8e33c253cristy DeclarationName TName; 1083ed852eea50f9d4cd633efb8c2b054b8e33c253cristy MemberOfUnknownSpecialization = false; 1093ed852eea50f9d4cd633efb8c2b054b8e33c253cristy 1103ed852eea50f9d4cd633efb8c2b054b8e33c253cristy switch (Name.getKind()) { 1113ed852eea50f9d4cd633efb8c2b054b8e33c253cristy case UnqualifiedId::IK_Identifier: 1123ed852eea50f9d4cd633efb8c2b054b8e33c253cristy TName = DeclarationName(Name.Identifier); 1133ed852eea50f9d4cd633efb8c2b054b8e33c253cristy break; 1143ed852eea50f9d4cd633efb8c2b054b8e33c253cristy 1153ed852eea50f9d4cd633efb8c2b054b8e33c253cristy case UnqualifiedId::IK_OperatorFunctionId: 1163ed852eea50f9d4cd633efb8c2b054b8e33c253cristy TName = Context.DeclarationNames.getCXXOperatorName( 1173ed852eea50f9d4cd633efb8c2b054b8e33c253cristy Name.OperatorFunctionId.Operator); 1183ed852eea50f9d4cd633efb8c2b054b8e33c253cristy break; 1193ed852eea50f9d4cd633efb8c2b054b8e33c253cristy 1203ed852eea50f9d4cd633efb8c2b054b8e33c253cristy case UnqualifiedId::IK_LiteralOperatorId: 1213ed852eea50f9d4cd633efb8c2b054b8e33c253cristy TName = Context.DeclarationNames.getCXXLiteralOperatorName(Name.Identifier); 1223ed852eea50f9d4cd633efb8c2b054b8e33c253cristy break; 1233ed852eea50f9d4cd633efb8c2b054b8e33c253cristy 1243ed852eea50f9d4cd633efb8c2b054b8e33c253cristy default: 1253ed852eea50f9d4cd633efb8c2b054b8e33c253cristy return TNK_Non_template; 1263ed852eea50f9d4cd633efb8c2b054b8e33c253cristy } 1273ed852eea50f9d4cd633efb8c2b054b8e33c253cristy 1283ed852eea50f9d4cd633efb8c2b054b8e33c253cristy QualType ObjectType = QualType::getFromOpaquePtr(ObjectTypePtr); 1293ed852eea50f9d4cd633efb8c2b054b8e33c253cristy 1303ed852eea50f9d4cd633efb8c2b054b8e33c253cristy LookupResult R(*this, TName, Name.getSourceRange().getBegin(), 1313ed852eea50f9d4cd633efb8c2b054b8e33c253cristy LookupOrdinaryName); 1323ed852eea50f9d4cd633efb8c2b054b8e33c253cristy R.suppressDiagnostics(); 1333ed852eea50f9d4cd633efb8c2b054b8e33c253cristy LookupTemplateName(R, S, SS, ObjectType, EnteringContext, 1343ed852eea50f9d4cd633efb8c2b054b8e33c253cristy MemberOfUnknownSpecialization); 1353ed852eea50f9d4cd633efb8c2b054b8e33c253cristy if (R.empty() || R.isAmbiguous()) 1363ed852eea50f9d4cd633efb8c2b054b8e33c253cristy return TNK_Non_template; 1373ed852eea50f9d4cd633efb8c2b054b8e33c253cristy 1383ed852eea50f9d4cd633efb8c2b054b8e33c253cristy TemplateName Template; 1393ed852eea50f9d4cd633efb8c2b054b8e33c253cristy TemplateNameKind TemplateKind; 1403ed852eea50f9d4cd633efb8c2b054b8e33c253cristy 1413ed852eea50f9d4cd633efb8c2b054b8e33c253cristy unsigned ResultCount = R.end() - R.begin(); 1423ed852eea50f9d4cd633efb8c2b054b8e33c253cristy if (ResultCount > 1) { 1433ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // We assume that we'll preserve the qualifier from a function 1443ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // template name in other ways. 1453ed852eea50f9d4cd633efb8c2b054b8e33c253cristy Template = Context.getOverloadedTemplateName(R.begin(), R.end()); 1463ed852eea50f9d4cd633efb8c2b054b8e33c253cristy TemplateKind = TNK_Function_template; 1473ed852eea50f9d4cd633efb8c2b054b8e33c253cristy } else { 1483ed852eea50f9d4cd633efb8c2b054b8e33c253cristy TemplateDecl *TD = cast<TemplateDecl>((*R.begin())->getUnderlyingDecl()); 1493ed852eea50f9d4cd633efb8c2b054b8e33c253cristy 1503ed852eea50f9d4cd633efb8c2b054b8e33c253cristy if (SS.isSet() && !SS.isInvalid()) { 1513ed852eea50f9d4cd633efb8c2b054b8e33c253cristy NestedNameSpecifier *Qualifier 1523ed852eea50f9d4cd633efb8c2b054b8e33c253cristy = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); 1533ed852eea50f9d4cd633efb8c2b054b8e33c253cristy Template = Context.getQualifiedTemplateName(Qualifier, false, TD); 1543ed852eea50f9d4cd633efb8c2b054b8e33c253cristy } else { 1553ed852eea50f9d4cd633efb8c2b054b8e33c253cristy Template = TemplateName(TD); 1563ed852eea50f9d4cd633efb8c2b054b8e33c253cristy } 1573ed852eea50f9d4cd633efb8c2b054b8e33c253cristy 1583ed852eea50f9d4cd633efb8c2b054b8e33c253cristy if (isa<FunctionTemplateDecl>(TD)) 1593ed852eea50f9d4cd633efb8c2b054b8e33c253cristy TemplateKind = TNK_Function_template; 1603ed852eea50f9d4cd633efb8c2b054b8e33c253cristy else { 1613ed852eea50f9d4cd633efb8c2b054b8e33c253cristy assert(isa<ClassTemplateDecl>(TD) || isa<TemplateTemplateParmDecl>(TD)); 1623ed852eea50f9d4cd633efb8c2b054b8e33c253cristy TemplateKind = TNK_Type_template; 1633ed852eea50f9d4cd633efb8c2b054b8e33c253cristy } 1643ed852eea50f9d4cd633efb8c2b054b8e33c253cristy } 1653ed852eea50f9d4cd633efb8c2b054b8e33c253cristy 1663ed852eea50f9d4cd633efb8c2b054b8e33c253cristy TemplateResult = TemplateTy::make(Template); 1673ed852eea50f9d4cd633efb8c2b054b8e33c253cristy return TemplateKind; 1683ed852eea50f9d4cd633efb8c2b054b8e33c253cristy} 1693ed852eea50f9d4cd633efb8c2b054b8e33c253cristy 1703ed852eea50f9d4cd633efb8c2b054b8e33c253cristybool Sema::DiagnoseUnknownTemplateName(const IdentifierInfo &II, 1713ed852eea50f9d4cd633efb8c2b054b8e33c253cristy SourceLocation IILoc, 1723ed852eea50f9d4cd633efb8c2b054b8e33c253cristy Scope *S, 1733ed852eea50f9d4cd633efb8c2b054b8e33c253cristy const CXXScopeSpec *SS, 1743ed852eea50f9d4cd633efb8c2b054b8e33c253cristy TemplateTy &SuggestedTemplate, 1753ed852eea50f9d4cd633efb8c2b054b8e33c253cristy TemplateNameKind &SuggestedKind) { 1763ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // We can't recover unless there's a dependent scope specifier preceding the 1773ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // template name. 1783ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // FIXME: Typo correction? 1793ed852eea50f9d4cd633efb8c2b054b8e33c253cristy if (!SS || !SS->isSet() || !isDependentScopeSpecifier(*SS) || 1803ed852eea50f9d4cd633efb8c2b054b8e33c253cristy computeDeclContext(*SS)) 1813ed852eea50f9d4cd633efb8c2b054b8e33c253cristy return false; 1823ed852eea50f9d4cd633efb8c2b054b8e33c253cristy 1833ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // The code is missing a 'template' keyword prior to the dependent template 1843ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // name. 1853ed852eea50f9d4cd633efb8c2b054b8e33c253cristy NestedNameSpecifier *Qualifier = (NestedNameSpecifier*)SS->getScopeRep(); 1863ed852eea50f9d4cd633efb8c2b054b8e33c253cristy Diag(IILoc, diag::err_template_kw_missing) 1873ed852eea50f9d4cd633efb8c2b054b8e33c253cristy << Qualifier << II.getName() 1883ed852eea50f9d4cd633efb8c2b054b8e33c253cristy << FixItHint::CreateInsertion(IILoc, "template "); 1893ed852eea50f9d4cd633efb8c2b054b8e33c253cristy SuggestedTemplate 1903ed852eea50f9d4cd633efb8c2b054b8e33c253cristy = TemplateTy::make(Context.getDependentTemplateName(Qualifier, &II)); 1913ed852eea50f9d4cd633efb8c2b054b8e33c253cristy SuggestedKind = TNK_Dependent_template_name; 1923ed852eea50f9d4cd633efb8c2b054b8e33c253cristy return true; 1933ed852eea50f9d4cd633efb8c2b054b8e33c253cristy} 1943ed852eea50f9d4cd633efb8c2b054b8e33c253cristy 1953ed852eea50f9d4cd633efb8c2b054b8e33c253cristyvoid Sema::LookupTemplateName(LookupResult &Found, 1963ed852eea50f9d4cd633efb8c2b054b8e33c253cristy Scope *S, CXXScopeSpec &SS, 1973ed852eea50f9d4cd633efb8c2b054b8e33c253cristy QualType ObjectType, 1983ed852eea50f9d4cd633efb8c2b054b8e33c253cristy bool EnteringContext, 1993ed852eea50f9d4cd633efb8c2b054b8e33c253cristy bool &MemberOfUnknownSpecialization) { 2003ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // Determine where to perform name lookup 2013ed852eea50f9d4cd633efb8c2b054b8e33c253cristy MemberOfUnknownSpecialization = false; 2023ed852eea50f9d4cd633efb8c2b054b8e33c253cristy DeclContext *LookupCtx = 0; 2033ed852eea50f9d4cd633efb8c2b054b8e33c253cristy bool isDependent = false; 2043ed852eea50f9d4cd633efb8c2b054b8e33c253cristy if (!ObjectType.isNull()) { 2053ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // This nested-name-specifier occurs in a member access expression, e.g., 2063ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // x->B::f, and we are looking into the type of the object. 2073ed852eea50f9d4cd633efb8c2b054b8e33c253cristy assert(!SS.isSet() && "ObjectType and scope specifier cannot coexist"); 2083ed852eea50f9d4cd633efb8c2b054b8e33c253cristy LookupCtx = computeDeclContext(ObjectType); 2093ed852eea50f9d4cd633efb8c2b054b8e33c253cristy isDependent = ObjectType->isDependentType(); 2103ed852eea50f9d4cd633efb8c2b054b8e33c253cristy assert((isDependent || !ObjectType->isIncompleteType()) && 2113ed852eea50f9d4cd633efb8c2b054b8e33c253cristy "Caller should have completed object type"); 2123ed852eea50f9d4cd633efb8c2b054b8e33c253cristy } else if (SS.isSet()) { 2133ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // This nested-name-specifier occurs after another nested-name-specifier, 2143ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // so long into the context associated with the prior nested-name-specifier. 2153ed852eea50f9d4cd633efb8c2b054b8e33c253cristy LookupCtx = computeDeclContext(SS, EnteringContext); 2163ed852eea50f9d4cd633efb8c2b054b8e33c253cristy isDependent = isDependentScopeSpecifier(SS); 2173ed852eea50f9d4cd633efb8c2b054b8e33c253cristy 2183ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // The declaration context must be complete. 2193ed852eea50f9d4cd633efb8c2b054b8e33c253cristy if (LookupCtx && RequireCompleteDeclContext(SS, LookupCtx)) 2203ed852eea50f9d4cd633efb8c2b054b8e33c253cristy return; 2213ed852eea50f9d4cd633efb8c2b054b8e33c253cristy } 2223ed852eea50f9d4cd633efb8c2b054b8e33c253cristy 2233ed852eea50f9d4cd633efb8c2b054b8e33c253cristy bool ObjectTypeSearchedInScope = false; 2243ed852eea50f9d4cd633efb8c2b054b8e33c253cristy if (LookupCtx) { 2253ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // Perform "qualified" name lookup into the declaration context we 2263ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // computed, which is either the type of the base of a member access 2273ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // expression or the declaration context associated with a prior 2283ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // nested-name-specifier. 2293ed852eea50f9d4cd633efb8c2b054b8e33c253cristy LookupQualifiedName(Found, LookupCtx); 2303ed852eea50f9d4cd633efb8c2b054b8e33c253cristy 2313ed852eea50f9d4cd633efb8c2b054b8e33c253cristy if (!ObjectType.isNull() && Found.empty()) { 2323ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // C++ [basic.lookup.classref]p1: 2333ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // In a class member access expression (5.2.5), if the . or -> token is 2343ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // immediately followed by an identifier followed by a <, the 2353ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // identifier must be looked up to determine whether the < is the 2363ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // beginning of a template argument list (14.2) or a less-than operator. 2373ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // The identifier is first looked up in the class of the object 2383ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // expression. If the identifier is not found, it is then looked up in 2393ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // the context of the entire postfix-expression and shall name a class 2403ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // or function template. 2413ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // 242bb50337b2a8a16ca7e903cc04ab195ff0fd47ae6cristy // FIXME: When we're instantiating a template, do we actually have to 2433ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // look in the scope of the template? Seems fishy... 2443ed852eea50f9d4cd633efb8c2b054b8e33c253cristy if (S) LookupName(Found, S); 2453ed852eea50f9d4cd633efb8c2b054b8e33c253cristy ObjectTypeSearchedInScope = true; 2463ed852eea50f9d4cd633efb8c2b054b8e33c253cristy } 2473ed852eea50f9d4cd633efb8c2b054b8e33c253cristy } else if (isDependent) { 2483ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // We cannot look into a dependent object type or nested nme 2493ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // specifier. 2503ed852eea50f9d4cd633efb8c2b054b8e33c253cristy MemberOfUnknownSpecialization = true; 2513ed852eea50f9d4cd633efb8c2b054b8e33c253cristy return; 252bb50337b2a8a16ca7e903cc04ab195ff0fd47ae6cristy } else { 2533ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // Perform unqualified name lookup in the current scope. 254bb50337b2a8a16ca7e903cc04ab195ff0fd47ae6cristy LookupName(Found, S); 2553ed852eea50f9d4cd633efb8c2b054b8e33c253cristy } 2563ed852eea50f9d4cd633efb8c2b054b8e33c253cristy 2573ed852eea50f9d4cd633efb8c2b054b8e33c253cristy if (Found.empty() && !isDependent) { 2583ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // If we did not find any names, attempt to correct any typos. 259bb50337b2a8a16ca7e903cc04ab195ff0fd47ae6cristy DeclarationName Name = Found.getLookupName(); 2603ed852eea50f9d4cd633efb8c2b054b8e33c253cristy if (DeclarationName Corrected = CorrectTypo(Found, S, &SS, LookupCtx, 261bb50337b2a8a16ca7e903cc04ab195ff0fd47ae6cristy false, CTC_CXXCasts)) { 2623ed852eea50f9d4cd633efb8c2b054b8e33c253cristy FilterAcceptableTemplateNames(Context, Found); 2633ed852eea50f9d4cd633efb8c2b054b8e33c253cristy if (!Found.empty()) { 2643ed852eea50f9d4cd633efb8c2b054b8e33c253cristy if (LookupCtx) 265bb50337b2a8a16ca7e903cc04ab195ff0fd47ae6cristy Diag(Found.getNameLoc(), diag::err_no_member_template_suggest) 2663ed852eea50f9d4cd633efb8c2b054b8e33c253cristy << Name << LookupCtx << Found.getLookupName() << SS.getRange() 2673ed852eea50f9d4cd633efb8c2b054b8e33c253cristy << FixItHint::CreateReplacement(Found.getNameLoc(), 2683ed852eea50f9d4cd633efb8c2b054b8e33c253cristy Found.getLookupName().getAsString()); 2693ed852eea50f9d4cd633efb8c2b054b8e33c253cristy else 2703ed852eea50f9d4cd633efb8c2b054b8e33c253cristy Diag(Found.getNameLoc(), diag::err_no_template_suggest) 2713ed852eea50f9d4cd633efb8c2b054b8e33c253cristy << Name << Found.getLookupName() 2723ed852eea50f9d4cd633efb8c2b054b8e33c253cristy << FixItHint::CreateReplacement(Found.getNameLoc(), 273bb50337b2a8a16ca7e903cc04ab195ff0fd47ae6cristy Found.getLookupName().getAsString()); 2743ed852eea50f9d4cd633efb8c2b054b8e33c253cristy if (TemplateDecl *Template = Found.getAsSingle<TemplateDecl>()) 2753ed852eea50f9d4cd633efb8c2b054b8e33c253cristy Diag(Template->getLocation(), diag::note_previous_decl) 2763ed852eea50f9d4cd633efb8c2b054b8e33c253cristy << Template->getDeclName(); 2773ed852eea50f9d4cd633efb8c2b054b8e33c253cristy } 2783ed852eea50f9d4cd633efb8c2b054b8e33c253cristy } else { 2793ed852eea50f9d4cd633efb8c2b054b8e33c253cristy Found.clear(); 2803ed852eea50f9d4cd633efb8c2b054b8e33c253cristy Found.setLookupName(Name); 2813ed852eea50f9d4cd633efb8c2b054b8e33c253cristy } 2823ed852eea50f9d4cd633efb8c2b054b8e33c253cristy } 2833ed852eea50f9d4cd633efb8c2b054b8e33c253cristy 2843ed852eea50f9d4cd633efb8c2b054b8e33c253cristy FilterAcceptableTemplateNames(Context, Found); 2853ed852eea50f9d4cd633efb8c2b054b8e33c253cristy if (Found.empty()) 2863ed852eea50f9d4cd633efb8c2b054b8e33c253cristy return; 2873ed852eea50f9d4cd633efb8c2b054b8e33c253cristy 2883ed852eea50f9d4cd633efb8c2b054b8e33c253cristy if (S && !ObjectType.isNull() && !ObjectTypeSearchedInScope) { 2893ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // C++ [basic.lookup.classref]p1: 2903ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // [...] If the lookup in the class of the object expression finds a 2913ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // template, the name is also looked up in the context of the entire 2923ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // postfix-expression and [...] 2933ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // 294bb50337b2a8a16ca7e903cc04ab195ff0fd47ae6cristy LookupResult FoundOuter(*this, Found.getLookupName(), Found.getNameLoc(), 2953ed852eea50f9d4cd633efb8c2b054b8e33c253cristy LookupOrdinaryName); 2963ed852eea50f9d4cd633efb8c2b054b8e33c253cristy LookupName(FoundOuter, S); 2973ed852eea50f9d4cd633efb8c2b054b8e33c253cristy FilterAcceptableTemplateNames(Context, FoundOuter); 2983ed852eea50f9d4cd633efb8c2b054b8e33c253cristy 2994e82e51d7ebce7b4ef0f808d906124dd6f812248cristy if (FoundOuter.empty()) { 3006a1c5a9375dfad129bc7d6ae8f3eadea60cebe46cristy // - if the name is not found, the name found in the class of the 3016a1c5a9375dfad129bc7d6ae8f3eadea60cebe46cristy // object expression is used, otherwise 3026a1c5a9375dfad129bc7d6ae8f3eadea60cebe46cristy } else if (!FoundOuter.getAsSingle<ClassTemplateDecl>()) { 3036a1c5a9375dfad129bc7d6ae8f3eadea60cebe46cristy // - if the name is found in the context of the entire 3043ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // postfix-expression and does not name a class template, the name 3053ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // found in the class of the object expression is used, otherwise 3063ed852eea50f9d4cd633efb8c2b054b8e33c253cristy } else if (!Found.isSuppressingDiagnostics()) { 3073ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // - if the name found is a class template, it must refer to the same 3083ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // entity as the one found in the class of the object expression, 309bb50337b2a8a16ca7e903cc04ab195ff0fd47ae6cristy // otherwise the program is ill-formed. 3103ed852eea50f9d4cd633efb8c2b054b8e33c253cristy if (!Found.isSingleResult() || 3113ed852eea50f9d4cd633efb8c2b054b8e33c253cristy Found.getFoundDecl()->getCanonicalDecl() 3124e82e51d7ebce7b4ef0f808d906124dd6f812248cristy != FoundOuter.getFoundDecl()->getCanonicalDecl()) { 3136a1c5a9375dfad129bc7d6ae8f3eadea60cebe46cristy Diag(Found.getNameLoc(), 3146a1c5a9375dfad129bc7d6ae8f3eadea60cebe46cristy diag::ext_nested_name_member_ref_lookup_ambiguous) 3156a1c5a9375dfad129bc7d6ae8f3eadea60cebe46cristy << Found.getLookupName() 3166a1c5a9375dfad129bc7d6ae8f3eadea60cebe46cristy << ObjectType; 3173ed852eea50f9d4cd633efb8c2b054b8e33c253cristy Diag(Found.getRepresentativeDecl()->getLocation(), 3183ed852eea50f9d4cd633efb8c2b054b8e33c253cristy diag::note_ambig_member_ref_object_type) 3193ed852eea50f9d4cd633efb8c2b054b8e33c253cristy << ObjectType; 3203ed852eea50f9d4cd633efb8c2b054b8e33c253cristy Diag(FoundOuter.getFoundDecl()->getLocation(), 3213ed852eea50f9d4cd633efb8c2b054b8e33c253cristy diag::note_ambig_member_ref_scope); 3223ed852eea50f9d4cd633efb8c2b054b8e33c253cristy 3233ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // Recover by taking the template that we found in the object 3243ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // expression's type. 3253ed852eea50f9d4cd633efb8c2b054b8e33c253cristy } 3263ed852eea50f9d4cd633efb8c2b054b8e33c253cristy } 3273ed852eea50f9d4cd633efb8c2b054b8e33c253cristy } 3283ed852eea50f9d4cd633efb8c2b054b8e33c253cristy} 3293ed852eea50f9d4cd633efb8c2b054b8e33c253cristy 330bb50337b2a8a16ca7e903cc04ab195ff0fd47ae6cristy/// ActOnDependentIdExpression - Handle a dependent id-expression that 3313ed852eea50f9d4cd633efb8c2b054b8e33c253cristy/// was just parsed. This is only possible with an explicit scope 3323ed852eea50f9d4cd633efb8c2b054b8e33c253cristy/// specifier naming a dependent type. 3336a1c5a9375dfad129bc7d6ae8f3eadea60cebe46cristySema::OwningExprResult 3346a1c5a9375dfad129bc7d6ae8f3eadea60cebe46cristySema::ActOnDependentIdExpression(const CXXScopeSpec &SS, 3356a1c5a9375dfad129bc7d6ae8f3eadea60cebe46cristy DeclarationName Name, 3366a1c5a9375dfad129bc7d6ae8f3eadea60cebe46cristy SourceLocation NameLoc, 3376a1c5a9375dfad129bc7d6ae8f3eadea60cebe46cristy bool isAddressOfOperand, 3383ed852eea50f9d4cd633efb8c2b054b8e33c253cristy const TemplateArgumentListInfo *TemplateArgs) { 3396a1c5a9375dfad129bc7d6ae8f3eadea60cebe46cristy NestedNameSpecifier *Qualifier 3406a1c5a9375dfad129bc7d6ae8f3eadea60cebe46cristy = static_cast<NestedNameSpecifier*>(SS.getScopeRep()); 3416a1c5a9375dfad129bc7d6ae8f3eadea60cebe46cristy 3426a1c5a9375dfad129bc7d6ae8f3eadea60cebe46cristy DeclContext *DC = getFunctionLevelDeclContext(); 3436a1c5a9375dfad129bc7d6ae8f3eadea60cebe46cristy 3443ed852eea50f9d4cd633efb8c2b054b8e33c253cristy if (!isAddressOfOperand && 3456a1c5a9375dfad129bc7d6ae8f3eadea60cebe46cristy isa<CXXMethodDecl>(DC) && 3466a1c5a9375dfad129bc7d6ae8f3eadea60cebe46cristy cast<CXXMethodDecl>(DC)->isInstance()) { 3476a1c5a9375dfad129bc7d6ae8f3eadea60cebe46cristy QualType ThisType = cast<CXXMethodDecl>(DC)->getThisType(Context); 3486a1c5a9375dfad129bc7d6ae8f3eadea60cebe46cristy 3496a1c5a9375dfad129bc7d6ae8f3eadea60cebe46cristy // Since the 'this' expression is synthesized, we don't need to 3503ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // perform the double-lookup check. 3516a1c5a9375dfad129bc7d6ae8f3eadea60cebe46cristy NamedDecl *FirstQualifierInScope = 0; 3526a1c5a9375dfad129bc7d6ae8f3eadea60cebe46cristy 3536a1c5a9375dfad129bc7d6ae8f3eadea60cebe46cristy return Owned(CXXDependentScopeMemberExpr::Create(Context, 3546a1c5a9375dfad129bc7d6ae8f3eadea60cebe46cristy /*This*/ 0, ThisType, 3553ed852eea50f9d4cd633efb8c2b054b8e33c253cristy /*IsArrow*/ true, 3566a1c5a9375dfad129bc7d6ae8f3eadea60cebe46cristy /*Op*/ SourceLocation(), 3573ed852eea50f9d4cd633efb8c2b054b8e33c253cristy Qualifier, SS.getRange(), 3583ed852eea50f9d4cd633efb8c2b054b8e33c253cristy FirstQualifierInScope, 3593ed852eea50f9d4cd633efb8c2b054b8e33c253cristy Name, NameLoc, 3603ed852eea50f9d4cd633efb8c2b054b8e33c253cristy TemplateArgs)); 3616a1c5a9375dfad129bc7d6ae8f3eadea60cebe46cristy } 3626a1c5a9375dfad129bc7d6ae8f3eadea60cebe46cristy 3636a1c5a9375dfad129bc7d6ae8f3eadea60cebe46cristy return BuildDependentDeclRefExpr(SS, Name, NameLoc, TemplateArgs); 3646a1c5a9375dfad129bc7d6ae8f3eadea60cebe46cristy} 3656a1c5a9375dfad129bc7d6ae8f3eadea60cebe46cristy 3663ed852eea50f9d4cd633efb8c2b054b8e33c253cristySema::OwningExprResult 3673ed852eea50f9d4cd633efb8c2b054b8e33c253cristySema::BuildDependentDeclRefExpr(const CXXScopeSpec &SS, 3683ed852eea50f9d4cd633efb8c2b054b8e33c253cristy DeclarationName Name, 3693ed852eea50f9d4cd633efb8c2b054b8e33c253cristy SourceLocation NameLoc, 3706a1c5a9375dfad129bc7d6ae8f3eadea60cebe46cristy const TemplateArgumentListInfo *TemplateArgs) { 3716a1c5a9375dfad129bc7d6ae8f3eadea60cebe46cristy return Owned(DependentScopeDeclRefExpr::Create(Context, 3726a1c5a9375dfad129bc7d6ae8f3eadea60cebe46cristy static_cast<NestedNameSpecifier*>(SS.getScopeRep()), 3736a1c5a9375dfad129bc7d6ae8f3eadea60cebe46cristy SS.getRange(), 3746a1c5a9375dfad129bc7d6ae8f3eadea60cebe46cristy Name, NameLoc, 3756a1c5a9375dfad129bc7d6ae8f3eadea60cebe46cristy TemplateArgs)); 3763ed852eea50f9d4cd633efb8c2b054b8e33c253cristy} 3773ed852eea50f9d4cd633efb8c2b054b8e33c253cristy 3783ed852eea50f9d4cd633efb8c2b054b8e33c253cristy/// DiagnoseTemplateParameterShadow - Produce a diagnostic complaining 3793ed852eea50f9d4cd633efb8c2b054b8e33c253cristy/// that the template parameter 'PrevDecl' is being shadowed by a new 3806a1c5a9375dfad129bc7d6ae8f3eadea60cebe46cristy/// declaration at location Loc. Returns true to indicate that this is 3816a1c5a9375dfad129bc7d6ae8f3eadea60cebe46cristy/// an error, and false otherwise. 3826a1c5a9375dfad129bc7d6ae8f3eadea60cebe46cristybool Sema::DiagnoseTemplateParameterShadow(SourceLocation Loc, Decl *PrevDecl) { 3836a1c5a9375dfad129bc7d6ae8f3eadea60cebe46cristy assert(PrevDecl->isTemplateParameter() && "Not a template parameter"); 3846a1c5a9375dfad129bc7d6ae8f3eadea60cebe46cristy 3856a1c5a9375dfad129bc7d6ae8f3eadea60cebe46cristy // Microsoft Visual C++ permits template parameters to be shadowed. 3866a1c5a9375dfad129bc7d6ae8f3eadea60cebe46cristy if (getLangOptions().Microsoft) 3876a1c5a9375dfad129bc7d6ae8f3eadea60cebe46cristy return false; 3883ed852eea50f9d4cd633efb8c2b054b8e33c253cristy 3893ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // C++ [temp.local]p4: 3903ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // A template-parameter shall not be redeclared within its 3913ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // scope (including nested scopes). 3923ed852eea50f9d4cd633efb8c2b054b8e33c253cristy Diag(Loc, diag::err_template_param_shadow) 3933ed852eea50f9d4cd633efb8c2b054b8e33c253cristy << cast<NamedDecl>(PrevDecl)->getDeclName(); 3943ed852eea50f9d4cd633efb8c2b054b8e33c253cristy Diag(PrevDecl->getLocation(), diag::note_template_param_here); 3953ed852eea50f9d4cd633efb8c2b054b8e33c253cristy return true; 3963ed852eea50f9d4cd633efb8c2b054b8e33c253cristy} 3973ed852eea50f9d4cd633efb8c2b054b8e33c253cristy 3983ed852eea50f9d4cd633efb8c2b054b8e33c253cristy/// AdjustDeclIfTemplate - If the given decl happens to be a template, reset 3993ed852eea50f9d4cd633efb8c2b054b8e33c253cristy/// the parameter D to reference the templated declaration and return a pointer 4003ed852eea50f9d4cd633efb8c2b054b8e33c253cristy/// to the template declaration. Otherwise, do nothing to D and return null. 4013ed852eea50f9d4cd633efb8c2b054b8e33c253cristyTemplateDecl *Sema::AdjustDeclIfTemplate(DeclPtrTy &D) { 4023ed852eea50f9d4cd633efb8c2b054b8e33c253cristy if (TemplateDecl *Temp = dyn_cast_or_null<TemplateDecl>(D.getAs<Decl>())) { 403bb50337b2a8a16ca7e903cc04ab195ff0fd47ae6cristy D = DeclPtrTy::make(Temp->getTemplatedDecl()); 4043ed852eea50f9d4cd633efb8c2b054b8e33c253cristy return Temp; 4053ed852eea50f9d4cd633efb8c2b054b8e33c253cristy } 4066a1c5a9375dfad129bc7d6ae8f3eadea60cebe46cristy return 0; 4076a1c5a9375dfad129bc7d6ae8f3eadea60cebe46cristy} 4086a1c5a9375dfad129bc7d6ae8f3eadea60cebe46cristy 4096a1c5a9375dfad129bc7d6ae8f3eadea60cebe46cristystatic TemplateArgumentLoc translateTemplateArgument(Sema &SemaRef, 4106a1c5a9375dfad129bc7d6ae8f3eadea60cebe46cristy const ParsedTemplateArgument &Arg) { 4113ed852eea50f9d4cd633efb8c2b054b8e33c253cristy 4126a1c5a9375dfad129bc7d6ae8f3eadea60cebe46cristy switch (Arg.getKind()) { 4136a1c5a9375dfad129bc7d6ae8f3eadea60cebe46cristy case ParsedTemplateArgument::Type: { 4146a1c5a9375dfad129bc7d6ae8f3eadea60cebe46cristy TypeSourceInfo *DI; 4156a1c5a9375dfad129bc7d6ae8f3eadea60cebe46cristy QualType T = SemaRef.GetTypeFromParser(Arg.getAsType(), &DI); 4163ed852eea50f9d4cd633efb8c2b054b8e33c253cristy if (!DI) 4176a1c5a9375dfad129bc7d6ae8f3eadea60cebe46cristy DI = SemaRef.Context.getTrivialTypeSourceInfo(T, Arg.getLocation()); 4183ed852eea50f9d4cd633efb8c2b054b8e33c253cristy return TemplateArgumentLoc(TemplateArgument(T), DI); 4193ed852eea50f9d4cd633efb8c2b054b8e33c253cristy } 4203ed852eea50f9d4cd633efb8c2b054b8e33c253cristy 4213ed852eea50f9d4cd633efb8c2b054b8e33c253cristy case ParsedTemplateArgument::NonType: { 4226a1c5a9375dfad129bc7d6ae8f3eadea60cebe46cristy Expr *E = static_cast<Expr *>(Arg.getAsExpr()); 4236a1c5a9375dfad129bc7d6ae8f3eadea60cebe46cristy return TemplateArgumentLoc(TemplateArgument(E), E); 4246a1c5a9375dfad129bc7d6ae8f3eadea60cebe46cristy } 4256a1c5a9375dfad129bc7d6ae8f3eadea60cebe46cristy 4263ed852eea50f9d4cd633efb8c2b054b8e33c253cristy case ParsedTemplateArgument::Template: { 4276a1c5a9375dfad129bc7d6ae8f3eadea60cebe46cristy TemplateName Template 4283ed852eea50f9d4cd633efb8c2b054b8e33c253cristy = TemplateName::getFromVoidPointer(Arg.getAsTemplate().get()); 4293ed852eea50f9d4cd633efb8c2b054b8e33c253cristy return TemplateArgumentLoc(TemplateArgument(Template), 4303ed852eea50f9d4cd633efb8c2b054b8e33c253cristy Arg.getScopeSpec().getRange(), 4313ed852eea50f9d4cd633efb8c2b054b8e33c253cristy Arg.getLocation()); 4323ed852eea50f9d4cd633efb8c2b054b8e33c253cristy } 4333ed852eea50f9d4cd633efb8c2b054b8e33c253cristy } 4343ed852eea50f9d4cd633efb8c2b054b8e33c253cristy 4353ed852eea50f9d4cd633efb8c2b054b8e33c253cristy llvm_unreachable("Unhandled parsed template argument"); 4363ed852eea50f9d4cd633efb8c2b054b8e33c253cristy return TemplateArgumentLoc(); 4373ed852eea50f9d4cd633efb8c2b054b8e33c253cristy} 4383ed852eea50f9d4cd633efb8c2b054b8e33c253cristy 439bb50337b2a8a16ca7e903cc04ab195ff0fd47ae6cristy/// \brief Translates template arguments as provided by the parser 4403ed852eea50f9d4cd633efb8c2b054b8e33c253cristy/// into template arguments used by semantic analysis. 4413ed852eea50f9d4cd633efb8c2b054b8e33c253cristyvoid Sema::translateTemplateArguments(const ASTTemplateArgsPtr &TemplateArgsIn, 4426a1c5a9375dfad129bc7d6ae8f3eadea60cebe46cristy TemplateArgumentListInfo &TemplateArgs) { 4436a1c5a9375dfad129bc7d6ae8f3eadea60cebe46cristy for (unsigned I = 0, Last = TemplateArgsIn.size(); I != Last; ++I) 4446a1c5a9375dfad129bc7d6ae8f3eadea60cebe46cristy TemplateArgs.addArgument(translateTemplateArgument(*this, 4456a1c5a9375dfad129bc7d6ae8f3eadea60cebe46cristy TemplateArgsIn[I])); 4463ed852eea50f9d4cd633efb8c2b054b8e33c253cristy} 4476a1c5a9375dfad129bc7d6ae8f3eadea60cebe46cristy 4483ed852eea50f9d4cd633efb8c2b054b8e33c253cristy/// ActOnTypeParameter - Called when a C++ template type parameter 4493ed852eea50f9d4cd633efb8c2b054b8e33c253cristy/// (e.g., "typename T") has been parsed. Typename specifies whether 4503ed852eea50f9d4cd633efb8c2b054b8e33c253cristy/// the keyword "typename" was used to declare the type parameter 4513ed852eea50f9d4cd633efb8c2b054b8e33c253cristy/// (otherwise, "class" was used), and KeyLoc is the location of the 4523ed852eea50f9d4cd633efb8c2b054b8e33c253cristy/// "class" or "typename" keyword. ParamName is the name of the 4533ed852eea50f9d4cd633efb8c2b054b8e33c253cristy/// parameter (NULL indicates an unnamed template parameter) and 4543ed852eea50f9d4cd633efb8c2b054b8e33c253cristy/// ParamName is the location of the parameter name (if any). 4553ed852eea50f9d4cd633efb8c2b054b8e33c253cristy/// If the type parameter has a default argument, it will be added 4563ed852eea50f9d4cd633efb8c2b054b8e33c253cristy/// later via ActOnTypeParameterDefault. 4573ed852eea50f9d4cd633efb8c2b054b8e33c253cristySema::DeclPtrTy Sema::ActOnTypeParameter(Scope *S, bool Typename, bool Ellipsis, 458bb50337b2a8a16ca7e903cc04ab195ff0fd47ae6cristy SourceLocation EllipsisLoc, 4593ed852eea50f9d4cd633efb8c2b054b8e33c253cristy SourceLocation KeyLoc, 4605fde9aa7d6fc3d8aee731855a35463fca177c217cristy IdentifierInfo *ParamName, 4615fde9aa7d6fc3d8aee731855a35463fca177c217cristy SourceLocation ParamNameLoc, 4625fde9aa7d6fc3d8aee731855a35463fca177c217cristy unsigned Depth, unsigned Position, 4633ed852eea50f9d4cd633efb8c2b054b8e33c253cristy SourceLocation EqualLoc, 4643ed852eea50f9d4cd633efb8c2b054b8e33c253cristy TypeTy *DefaultArg) { 4653ed852eea50f9d4cd633efb8c2b054b8e33c253cristy assert(S->isTemplateParamScope() && 4663ed852eea50f9d4cd633efb8c2b054b8e33c253cristy "Template type parameter not in template parameter scope!"); 4673ed852eea50f9d4cd633efb8c2b054b8e33c253cristy bool Invalid = false; 4683ed852eea50f9d4cd633efb8c2b054b8e33c253cristy 4693ed852eea50f9d4cd633efb8c2b054b8e33c253cristy if (ParamName) { 4703ed852eea50f9d4cd633efb8c2b054b8e33c253cristy NamedDecl *PrevDecl = LookupSingleName(S, ParamName, ParamNameLoc, 4713ed852eea50f9d4cd633efb8c2b054b8e33c253cristy LookupOrdinaryName, 4723ed852eea50f9d4cd633efb8c2b054b8e33c253cristy ForRedeclaration); 4733ed852eea50f9d4cd633efb8c2b054b8e33c253cristy if (PrevDecl && PrevDecl->isTemplateParameter()) 4743ed852eea50f9d4cd633efb8c2b054b8e33c253cristy Invalid = Invalid || DiagnoseTemplateParameterShadow(ParamNameLoc, 4753ed852eea50f9d4cd633efb8c2b054b8e33c253cristy PrevDecl); 4763ed852eea50f9d4cd633efb8c2b054b8e33c253cristy } 477bb50337b2a8a16ca7e903cc04ab195ff0fd47ae6cristy 4783ed852eea50f9d4cd633efb8c2b054b8e33c253cristy SourceLocation Loc = ParamNameLoc; 479bb50337b2a8a16ca7e903cc04ab195ff0fd47ae6cristy if (!ParamName) 4803ed852eea50f9d4cd633efb8c2b054b8e33c253cristy Loc = KeyLoc; 4813ed852eea50f9d4cd633efb8c2b054b8e33c253cristy 4823ed852eea50f9d4cd633efb8c2b054b8e33c253cristy TemplateTypeParmDecl *Param 4833ed852eea50f9d4cd633efb8c2b054b8e33c253cristy = TemplateTypeParmDecl::Create(Context, Context.getTranslationUnitDecl(), 4843ed852eea50f9d4cd633efb8c2b054b8e33c253cristy Loc, Depth, Position, ParamName, Typename, 4853ed852eea50f9d4cd633efb8c2b054b8e33c253cristy Ellipsis); 4863ed852eea50f9d4cd633efb8c2b054b8e33c253cristy if (Invalid) 4873ed852eea50f9d4cd633efb8c2b054b8e33c253cristy Param->setInvalidDecl(); 4883ed852eea50f9d4cd633efb8c2b054b8e33c253cristy 4893ed852eea50f9d4cd633efb8c2b054b8e33c253cristy if (ParamName) { 4903ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // Add the template parameter into the current scope. 4913ed852eea50f9d4cd633efb8c2b054b8e33c253cristy S->AddDecl(DeclPtrTy::make(Param)); 4923ed852eea50f9d4cd633efb8c2b054b8e33c253cristy IdResolver.AddDecl(Param); 4933ed852eea50f9d4cd633efb8c2b054b8e33c253cristy } 4943ed852eea50f9d4cd633efb8c2b054b8e33c253cristy 4953ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // Handle the default argument, if provided. 4963ed852eea50f9d4cd633efb8c2b054b8e33c253cristy if (DefaultArg) { 497bb50337b2a8a16ca7e903cc04ab195ff0fd47ae6cristy TypeSourceInfo *DefaultTInfo; 4983ed852eea50f9d4cd633efb8c2b054b8e33c253cristy GetTypeFromParser(DefaultArg, &DefaultTInfo); 4993ed852eea50f9d4cd633efb8c2b054b8e33c253cristy 5003ed852eea50f9d4cd633efb8c2b054b8e33c253cristy assert(DefaultTInfo && "expected source information for type"); 5013ed852eea50f9d4cd633efb8c2b054b8e33c253cristy 5023ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // C++0x [temp.param]p9: 503bb50337b2a8a16ca7e903cc04ab195ff0fd47ae6cristy // A default template-argument may be specified for any kind of 5043ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // template-parameter that is not a template parameter pack. 5053ed852eea50f9d4cd633efb8c2b054b8e33c253cristy if (Ellipsis) { 5063ed852eea50f9d4cd633efb8c2b054b8e33c253cristy Diag(EqualLoc, diag::err_template_param_pack_default_arg); 5073ed852eea50f9d4cd633efb8c2b054b8e33c253cristy return DeclPtrTy::make(Param); 508bb50337b2a8a16ca7e903cc04ab195ff0fd47ae6cristy } 5093ed852eea50f9d4cd633efb8c2b054b8e33c253cristy 5103ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // Check the template argument itself. 5113ed852eea50f9d4cd633efb8c2b054b8e33c253cristy if (CheckTemplateArgument(Param, DefaultTInfo)) { 5123ed852eea50f9d4cd633efb8c2b054b8e33c253cristy Param->setInvalidDecl(); 5133ed852eea50f9d4cd633efb8c2b054b8e33c253cristy return DeclPtrTy::make(Param);; 5143ed852eea50f9d4cd633efb8c2b054b8e33c253cristy } 5153ed852eea50f9d4cd633efb8c2b054b8e33c253cristy 5163ed852eea50f9d4cd633efb8c2b054b8e33c253cristy Param->setDefaultArgument(DefaultTInfo, false); 5173ed852eea50f9d4cd633efb8c2b054b8e33c253cristy } 5183ed852eea50f9d4cd633efb8c2b054b8e33c253cristy 5193ed852eea50f9d4cd633efb8c2b054b8e33c253cristy return DeclPtrTy::make(Param); 5203ed852eea50f9d4cd633efb8c2b054b8e33c253cristy} 5213ed852eea50f9d4cd633efb8c2b054b8e33c253cristy 5223ed852eea50f9d4cd633efb8c2b054b8e33c253cristy/// \brief Check that the type of a non-type template parameter is 5233ed852eea50f9d4cd633efb8c2b054b8e33c253cristy/// well-formed. 5243ed852eea50f9d4cd633efb8c2b054b8e33c253cristy/// 5253ed852eea50f9d4cd633efb8c2b054b8e33c253cristy/// \returns the (possibly-promoted) parameter type if valid; 5263ed852eea50f9d4cd633efb8c2b054b8e33c253cristy/// otherwise, produces a diagnostic and returns a NULL type. 5273ed852eea50f9d4cd633efb8c2b054b8e33c253cristyQualType 5283ed852eea50f9d4cd633efb8c2b054b8e33c253cristySema::CheckNonTypeTemplateParameterType(QualType T, SourceLocation Loc) { 5293ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // We don't allow variably-modified types as the type of non-type template 5303ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // parameters. 5313ed852eea50f9d4cd633efb8c2b054b8e33c253cristy if (T->isVariablyModifiedType()) { 5323ed852eea50f9d4cd633efb8c2b054b8e33c253cristy Diag(Loc, diag::err_variably_modified_nontype_template_param) 5333ed852eea50f9d4cd633efb8c2b054b8e33c253cristy << T; 5343ed852eea50f9d4cd633efb8c2b054b8e33c253cristy return QualType(); 5353ed852eea50f9d4cd633efb8c2b054b8e33c253cristy } 5363ed852eea50f9d4cd633efb8c2b054b8e33c253cristy 5373ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // C++ [temp.param]p4: 5383ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // 5393ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // A non-type template-parameter shall have one of the following 5403ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // (optionally cv-qualified) types: 5413ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // 5423ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // -- integral or enumeration type, 5433ed852eea50f9d4cd633efb8c2b054b8e33c253cristy if (T->isIntegralOrEnumerationType() || 5443ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // -- pointer to object or pointer to function, 5453ed852eea50f9d4cd633efb8c2b054b8e33c253cristy (T->isPointerType() && 546bb50337b2a8a16ca7e903cc04ab195ff0fd47ae6cristy (T->getAs<PointerType>()->getPointeeType()->isObjectType() || 5473ed852eea50f9d4cd633efb8c2b054b8e33c253cristy T->getAs<PointerType>()->getPointeeType()->isFunctionType())) || 5483ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // -- reference to object or reference to function, 5493ed852eea50f9d4cd633efb8c2b054b8e33c253cristy T->isReferenceType() || 5503ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // -- pointer to member. 5513ed852eea50f9d4cd633efb8c2b054b8e33c253cristy T->isMemberPointerType() || 5523ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // If T is a dependent type, we can't do the check now, so we 5533ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // assume that it is well-formed. 5543ed852eea50f9d4cd633efb8c2b054b8e33c253cristy T->isDependentType()) 5553ed852eea50f9d4cd633efb8c2b054b8e33c253cristy return T; 5563ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // C++ [temp.param]p8: 5573ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // 5583ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // A non-type template-parameter of type "array of T" or 5593ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // "function returning T" is adjusted to be of type "pointer to 5603ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // T" or "pointer to function returning T", respectively. 5613ed852eea50f9d4cd633efb8c2b054b8e33c253cristy else if (T->isArrayType()) 5623ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // FIXME: Keep the type prior to promotion? 5633ed852eea50f9d4cd633efb8c2b054b8e33c253cristy return Context.getArrayDecayedType(T); 5643ed852eea50f9d4cd633efb8c2b054b8e33c253cristy else if (T->isFunctionType()) 5653ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // FIXME: Keep the type prior to promotion? 5663ed852eea50f9d4cd633efb8c2b054b8e33c253cristy return Context.getPointerType(T); 5673ed852eea50f9d4cd633efb8c2b054b8e33c253cristy 5683ed852eea50f9d4cd633efb8c2b054b8e33c253cristy Diag(Loc, diag::err_template_nontype_parm_bad_type) 569bb50337b2a8a16ca7e903cc04ab195ff0fd47ae6cristy << T; 5703ed852eea50f9d4cd633efb8c2b054b8e33c253cristy 571bb50337b2a8a16ca7e903cc04ab195ff0fd47ae6cristy return QualType(); 5723ed852eea50f9d4cd633efb8c2b054b8e33c253cristy} 5733ed852eea50f9d4cd633efb8c2b054b8e33c253cristy 5743ed852eea50f9d4cd633efb8c2b054b8e33c253cristySema::DeclPtrTy Sema::ActOnNonTypeTemplateParameter(Scope *S, Declarator &D, 5753ed852eea50f9d4cd633efb8c2b054b8e33c253cristy unsigned Depth, 5763ed852eea50f9d4cd633efb8c2b054b8e33c253cristy unsigned Position, 5773ed852eea50f9d4cd633efb8c2b054b8e33c253cristy SourceLocation EqualLoc, 5783ed852eea50f9d4cd633efb8c2b054b8e33c253cristy ExprArg DefaultArg) { 5796a1c5a9375dfad129bc7d6ae8f3eadea60cebe46cristy TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S); 5806a1c5a9375dfad129bc7d6ae8f3eadea60cebe46cristy QualType T = TInfo->getType(); 5816a1c5a9375dfad129bc7d6ae8f3eadea60cebe46cristy 5826a1c5a9375dfad129bc7d6ae8f3eadea60cebe46cristy assert(S->isTemplateParamScope() && 5836a1c5a9375dfad129bc7d6ae8f3eadea60cebe46cristy "Non-type template parameter not in template parameter scope!"); 5846a1c5a9375dfad129bc7d6ae8f3eadea60cebe46cristy bool Invalid = false; 5856a1c5a9375dfad129bc7d6ae8f3eadea60cebe46cristy 5866a1c5a9375dfad129bc7d6ae8f3eadea60cebe46cristy IdentifierInfo *ParamName = D.getIdentifier(); 5876a1c5a9375dfad129bc7d6ae8f3eadea60cebe46cristy if (ParamName) { 5883ed852eea50f9d4cd633efb8c2b054b8e33c253cristy NamedDecl *PrevDecl = LookupSingleName(S, ParamName, D.getIdentifierLoc(), 5893ed852eea50f9d4cd633efb8c2b054b8e33c253cristy LookupOrdinaryName, 5903ed852eea50f9d4cd633efb8c2b054b8e33c253cristy ForRedeclaration); 5913ed852eea50f9d4cd633efb8c2b054b8e33c253cristy if (PrevDecl && PrevDecl->isTemplateParameter()) 5923ed852eea50f9d4cd633efb8c2b054b8e33c253cristy Invalid = Invalid || DiagnoseTemplateParameterShadow(D.getIdentifierLoc(), 5933ed852eea50f9d4cd633efb8c2b054b8e33c253cristy PrevDecl); 5943ed852eea50f9d4cd633efb8c2b054b8e33c253cristy } 5953ed852eea50f9d4cd633efb8c2b054b8e33c253cristy 5963ed852eea50f9d4cd633efb8c2b054b8e33c253cristy T = CheckNonTypeTemplateParameterType(T, D.getIdentifierLoc()); 5973ed852eea50f9d4cd633efb8c2b054b8e33c253cristy if (T.isNull()) { 5983ed852eea50f9d4cd633efb8c2b054b8e33c253cristy T = Context.IntTy; // Recover with an 'int' type. 5993ed852eea50f9d4cd633efb8c2b054b8e33c253cristy Invalid = true; 6003ed852eea50f9d4cd633efb8c2b054b8e33c253cristy } 6013ed852eea50f9d4cd633efb8c2b054b8e33c253cristy 602bb50337b2a8a16ca7e903cc04ab195ff0fd47ae6cristy NonTypeTemplateParmDecl *Param 6033ed852eea50f9d4cd633efb8c2b054b8e33c253cristy = NonTypeTemplateParmDecl::Create(Context, Context.getTranslationUnitDecl(), 6043ed852eea50f9d4cd633efb8c2b054b8e33c253cristy D.getIdentifierLoc(), 6053ed852eea50f9d4cd633efb8c2b054b8e33c253cristy Depth, Position, ParamName, T, TInfo); 6063ed852eea50f9d4cd633efb8c2b054b8e33c253cristy if (Invalid) 6073ed852eea50f9d4cd633efb8c2b054b8e33c253cristy Param->setInvalidDecl(); 6083ed852eea50f9d4cd633efb8c2b054b8e33c253cristy 6093ed852eea50f9d4cd633efb8c2b054b8e33c253cristy if (D.getIdentifier()) { 6103ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // Add the template parameter into the current scope. 6113ed852eea50f9d4cd633efb8c2b054b8e33c253cristy S->AddDecl(DeclPtrTy::make(Param)); 6123ed852eea50f9d4cd633efb8c2b054b8e33c253cristy IdResolver.AddDecl(Param); 6133ed852eea50f9d4cd633efb8c2b054b8e33c253cristy } 6143ed852eea50f9d4cd633efb8c2b054b8e33c253cristy 6153ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // Check the well-formedness of the default template argument, if provided. 6163ed852eea50f9d4cd633efb8c2b054b8e33c253cristy if (Expr *Default = static_cast<Expr *>(DefaultArg.get())) { 6173ed852eea50f9d4cd633efb8c2b054b8e33c253cristy TemplateArgument Converted; 6183ed852eea50f9d4cd633efb8c2b054b8e33c253cristy if (CheckTemplateArgument(Param, Param->getType(), Default, Converted)) { 6193ed852eea50f9d4cd633efb8c2b054b8e33c253cristy Param->setInvalidDecl(); 6203ed852eea50f9d4cd633efb8c2b054b8e33c253cristy return DeclPtrTy::make(Param);; 6213ed852eea50f9d4cd633efb8c2b054b8e33c253cristy } 6223ed852eea50f9d4cd633efb8c2b054b8e33c253cristy 6233ed852eea50f9d4cd633efb8c2b054b8e33c253cristy Param->setDefaultArgument(DefaultArg.takeAs<Expr>(), false); 6243ed852eea50f9d4cd633efb8c2b054b8e33c253cristy } 6253ed852eea50f9d4cd633efb8c2b054b8e33c253cristy 6263ed852eea50f9d4cd633efb8c2b054b8e33c253cristy return DeclPtrTy::make(Param); 6273ed852eea50f9d4cd633efb8c2b054b8e33c253cristy} 6283ed852eea50f9d4cd633efb8c2b054b8e33c253cristy 6293ed852eea50f9d4cd633efb8c2b054b8e33c253cristy/// ActOnTemplateTemplateParameter - Called when a C++ template template 6303ed852eea50f9d4cd633efb8c2b054b8e33c253cristy/// parameter (e.g. T in template <template <typename> class T> class array) 6313ed852eea50f9d4cd633efb8c2b054b8e33c253cristy/// has been parsed. S is the current scope. 6323ed852eea50f9d4cd633efb8c2b054b8e33c253cristySema::DeclPtrTy Sema::ActOnTemplateTemplateParameter(Scope* S, 6333ed852eea50f9d4cd633efb8c2b054b8e33c253cristy SourceLocation TmpLoc, 6343ed852eea50f9d4cd633efb8c2b054b8e33c253cristy TemplateParamsTy *Params, 6353ed852eea50f9d4cd633efb8c2b054b8e33c253cristy IdentifierInfo *Name, 6363ed852eea50f9d4cd633efb8c2b054b8e33c253cristy SourceLocation NameLoc, 6373ed852eea50f9d4cd633efb8c2b054b8e33c253cristy unsigned Depth, 6383ed852eea50f9d4cd633efb8c2b054b8e33c253cristy unsigned Position, 6393ed852eea50f9d4cd633efb8c2b054b8e33c253cristy SourceLocation EqualLoc, 6403ed852eea50f9d4cd633efb8c2b054b8e33c253cristy const ParsedTemplateArgument &Default) { 6413ed852eea50f9d4cd633efb8c2b054b8e33c253cristy assert(S->isTemplateParamScope() && 6423ed852eea50f9d4cd633efb8c2b054b8e33c253cristy "Template template parameter not in template parameter scope!"); 643e8c25f9b4c9fb72cad6db08eeda58c7c5784014ecristy 644e8c25f9b4c9fb72cad6db08eeda58c7c5784014ecristy // Construct the parameter object. 6453ed852eea50f9d4cd633efb8c2b054b8e33c253cristy TemplateTemplateParmDecl *Param = 6463ed852eea50f9d4cd633efb8c2b054b8e33c253cristy TemplateTemplateParmDecl::Create(Context, Context.getTranslationUnitDecl(), 6473ed852eea50f9d4cd633efb8c2b054b8e33c253cristy TmpLoc, Depth, Position, Name, 6483ed852eea50f9d4cd633efb8c2b054b8e33c253cristy (TemplateParameterList*)Params); 6493ed852eea50f9d4cd633efb8c2b054b8e33c253cristy 6503ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // If the template template parameter has a name, then link the identifier 651bb50337b2a8a16ca7e903cc04ab195ff0fd47ae6cristy // into the scope and lookup mechanisms. 6523ed852eea50f9d4cd633efb8c2b054b8e33c253cristy if (Name) { 6533ed852eea50f9d4cd633efb8c2b054b8e33c253cristy S->AddDecl(DeclPtrTy::make(Param)); 6543ed852eea50f9d4cd633efb8c2b054b8e33c253cristy IdResolver.AddDecl(Param); 6553ed852eea50f9d4cd633efb8c2b054b8e33c253cristy } 6563ed852eea50f9d4cd633efb8c2b054b8e33c253cristy 6573ed852eea50f9d4cd633efb8c2b054b8e33c253cristy if (!Default.isInvalid()) { 6583ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // Check only that we have a template template argument. We don't want to 6593ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // try to check well-formedness now, because our template template parameter 6603ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // might have dependent types in its template parameters, which we wouldn't 6613ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // be able to match now. 6623ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // 6633ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // If none of the template template parameter's template arguments mention 6643ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // other template parameters, we could actually perform more checking here. 6653ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // However, it isn't worth doing. 6663ed852eea50f9d4cd633efb8c2b054b8e33c253cristy TemplateArgumentLoc DefaultArg = translateTemplateArgument(*this, Default); 6673ed852eea50f9d4cd633efb8c2b054b8e33c253cristy if (DefaultArg.getArgument().getAsTemplate().isNull()) { 6683ed852eea50f9d4cd633efb8c2b054b8e33c253cristy Diag(DefaultArg.getLocation(), diag::err_template_arg_not_class_template) 6693ed852eea50f9d4cd633efb8c2b054b8e33c253cristy << DefaultArg.getSourceRange(); 6703ed852eea50f9d4cd633efb8c2b054b8e33c253cristy return DeclPtrTy::make(Param); 6713ed852eea50f9d4cd633efb8c2b054b8e33c253cristy } 6723ed852eea50f9d4cd633efb8c2b054b8e33c253cristy 6733ed852eea50f9d4cd633efb8c2b054b8e33c253cristy Param->setDefaultArgument(DefaultArg, false); 6743ed852eea50f9d4cd633efb8c2b054b8e33c253cristy } 6753ed852eea50f9d4cd633efb8c2b054b8e33c253cristy 6763ed852eea50f9d4cd633efb8c2b054b8e33c253cristy return DeclPtrTy::make(Param); 6773ed852eea50f9d4cd633efb8c2b054b8e33c253cristy} 6783ed852eea50f9d4cd633efb8c2b054b8e33c253cristy 6793ed852eea50f9d4cd633efb8c2b054b8e33c253cristy/// ActOnTemplateParameterList - Builds a TemplateParameterList that 6803ed852eea50f9d4cd633efb8c2b054b8e33c253cristy/// contains the template parameters in Params/NumParams. 6813ed852eea50f9d4cd633efb8c2b054b8e33c253cristySema::TemplateParamsTy * 6823ed852eea50f9d4cd633efb8c2b054b8e33c253cristySema::ActOnTemplateParameterList(unsigned Depth, 6833ed852eea50f9d4cd633efb8c2b054b8e33c253cristy SourceLocation ExportLoc, 6843ed852eea50f9d4cd633efb8c2b054b8e33c253cristy SourceLocation TemplateLoc, 6853ed852eea50f9d4cd633efb8c2b054b8e33c253cristy SourceLocation LAngleLoc, 6863ed852eea50f9d4cd633efb8c2b054b8e33c253cristy DeclPtrTy *Params, unsigned NumParams, 6873ed852eea50f9d4cd633efb8c2b054b8e33c253cristy SourceLocation RAngleLoc) { 6883ed852eea50f9d4cd633efb8c2b054b8e33c253cristy if (ExportLoc.isValid()) 6893ed852eea50f9d4cd633efb8c2b054b8e33c253cristy Diag(ExportLoc, diag::warn_template_export_unsupported); 6903ed852eea50f9d4cd633efb8c2b054b8e33c253cristy 6913ed852eea50f9d4cd633efb8c2b054b8e33c253cristy return TemplateParameterList::Create(Context, TemplateLoc, LAngleLoc, 6923ed852eea50f9d4cd633efb8c2b054b8e33c253cristy (NamedDecl**)Params, NumParams, 6933ed852eea50f9d4cd633efb8c2b054b8e33c253cristy RAngleLoc); 694bb50337b2a8a16ca7e903cc04ab195ff0fd47ae6cristy} 6953ed852eea50f9d4cd633efb8c2b054b8e33c253cristy 6963ed852eea50f9d4cd633efb8c2b054b8e33c253cristystatic void SetNestedNameSpecifier(TagDecl *T, const CXXScopeSpec &SS) { 6973ed852eea50f9d4cd633efb8c2b054b8e33c253cristy if (SS.isSet()) 6983ed852eea50f9d4cd633efb8c2b054b8e33c253cristy T->setQualifierInfo(static_cast<NestedNameSpecifier*>(SS.getScopeRep()), 6993ed852eea50f9d4cd633efb8c2b054b8e33c253cristy SS.getRange()); 7003ed852eea50f9d4cd633efb8c2b054b8e33c253cristy} 7013ed852eea50f9d4cd633efb8c2b054b8e33c253cristy 7023ed852eea50f9d4cd633efb8c2b054b8e33c253cristySema::DeclResult 7033ed852eea50f9d4cd633efb8c2b054b8e33c253cristySema::CheckClassTemplate(Scope *S, unsigned TagSpec, TagUseKind TUK, 7043ed852eea50f9d4cd633efb8c2b054b8e33c253cristy SourceLocation KWLoc, CXXScopeSpec &SS, 7053ed852eea50f9d4cd633efb8c2b054b8e33c253cristy IdentifierInfo *Name, SourceLocation NameLoc, 7063ed852eea50f9d4cd633efb8c2b054b8e33c253cristy AttributeList *Attr, 7073ed852eea50f9d4cd633efb8c2b054b8e33c253cristy TemplateParameterList *TemplateParams, 7083ed852eea50f9d4cd633efb8c2b054b8e33c253cristy AccessSpecifier AS) { 7093ed852eea50f9d4cd633efb8c2b054b8e33c253cristy assert(TemplateParams && TemplateParams->size() > 0 && 7103ed852eea50f9d4cd633efb8c2b054b8e33c253cristy "No template parameters"); 7113ed852eea50f9d4cd633efb8c2b054b8e33c253cristy assert(TUK != TUK_Reference && "Can only declare or define class templates"); 7123ed852eea50f9d4cd633efb8c2b054b8e33c253cristy bool Invalid = false; 7133ed852eea50f9d4cd633efb8c2b054b8e33c253cristy 7143ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // Check that we can declare a template here. 7153ed852eea50f9d4cd633efb8c2b054b8e33c253cristy if (CheckTemplateDeclScope(S, TemplateParams)) 7163ed852eea50f9d4cd633efb8c2b054b8e33c253cristy return true; 7173ed852eea50f9d4cd633efb8c2b054b8e33c253cristy 7183ed852eea50f9d4cd633efb8c2b054b8e33c253cristy TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); 7193ed852eea50f9d4cd633efb8c2b054b8e33c253cristy assert(Kind != TTK_Enum && "can't build template of enumerated type"); 7203ed852eea50f9d4cd633efb8c2b054b8e33c253cristy 7213ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // There is no such thing as an unnamed class template. 7223ed852eea50f9d4cd633efb8c2b054b8e33c253cristy if (!Name) { 7233ed852eea50f9d4cd633efb8c2b054b8e33c253cristy Diag(KWLoc, diag::err_template_unnamed_class); 7243ed852eea50f9d4cd633efb8c2b054b8e33c253cristy return true; 7253ed852eea50f9d4cd633efb8c2b054b8e33c253cristy } 7263ed852eea50f9d4cd633efb8c2b054b8e33c253cristy 7273ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // Find any previous declaration with this name. 7283ed852eea50f9d4cd633efb8c2b054b8e33c253cristy DeclContext *SemanticContext; 7293ed852eea50f9d4cd633efb8c2b054b8e33c253cristy LookupResult Previous(*this, Name, NameLoc, LookupOrdinaryName, 7303ed852eea50f9d4cd633efb8c2b054b8e33c253cristy ForRedeclaration); 7313ed852eea50f9d4cd633efb8c2b054b8e33c253cristy if (SS.isNotEmpty() && !SS.isInvalid()) { 7323ed852eea50f9d4cd633efb8c2b054b8e33c253cristy SemanticContext = computeDeclContext(SS, true); 7333ed852eea50f9d4cd633efb8c2b054b8e33c253cristy if (!SemanticContext) { 7343ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // FIXME: Produce a reasonable diagnostic here 7353ed852eea50f9d4cd633efb8c2b054b8e33c253cristy return true; 7363ed852eea50f9d4cd633efb8c2b054b8e33c253cristy } 7373ed852eea50f9d4cd633efb8c2b054b8e33c253cristy 7383ed852eea50f9d4cd633efb8c2b054b8e33c253cristy if (RequireCompleteDeclContext(SS, SemanticContext)) 7393ed852eea50f9d4cd633efb8c2b054b8e33c253cristy return true; 7403ed852eea50f9d4cd633efb8c2b054b8e33c253cristy 7413ed852eea50f9d4cd633efb8c2b054b8e33c253cristy LookupQualifiedName(Previous, SemanticContext); 7423ed852eea50f9d4cd633efb8c2b054b8e33c253cristy } else { 7433ed852eea50f9d4cd633efb8c2b054b8e33c253cristy SemanticContext = CurContext; 7443ed852eea50f9d4cd633efb8c2b054b8e33c253cristy LookupName(Previous, S); 7453ed852eea50f9d4cd633efb8c2b054b8e33c253cristy } 7463ed852eea50f9d4cd633efb8c2b054b8e33c253cristy 7473ed852eea50f9d4cd633efb8c2b054b8e33c253cristy if (Previous.isAmbiguous()) 7483ed852eea50f9d4cd633efb8c2b054b8e33c253cristy return true; 7493ed852eea50f9d4cd633efb8c2b054b8e33c253cristy 7503ed852eea50f9d4cd633efb8c2b054b8e33c253cristy NamedDecl *PrevDecl = 0; 7513ed852eea50f9d4cd633efb8c2b054b8e33c253cristy if (Previous.begin() != Previous.end()) 7523ed852eea50f9d4cd633efb8c2b054b8e33c253cristy PrevDecl = (*Previous.begin())->getUnderlyingDecl(); 753bb50337b2a8a16ca7e903cc04ab195ff0fd47ae6cristy 7543ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // If there is a previous declaration with the same name, check 7553ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // whether this is a valid redeclaration. 7563ed852eea50f9d4cd633efb8c2b054b8e33c253cristy ClassTemplateDecl *PrevClassTemplate 7573ed852eea50f9d4cd633efb8c2b054b8e33c253cristy = dyn_cast_or_null<ClassTemplateDecl>(PrevDecl); 7583ed852eea50f9d4cd633efb8c2b054b8e33c253cristy 7593ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // We may have found the injected-class-name of a class template, 7603ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // class template partial specialization, or class template specialization. 7613ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // In these cases, grab the template that is being defined or specialized. 7623ed852eea50f9d4cd633efb8c2b054b8e33c253cristy if (!PrevClassTemplate && PrevDecl && isa<CXXRecordDecl>(PrevDecl) && 7633ed852eea50f9d4cd633efb8c2b054b8e33c253cristy cast<CXXRecordDecl>(PrevDecl)->isInjectedClassName()) { 7643ed852eea50f9d4cd633efb8c2b054b8e33c253cristy PrevDecl = cast<CXXRecordDecl>(PrevDecl->getDeclContext()); 7653ed852eea50f9d4cd633efb8c2b054b8e33c253cristy PrevClassTemplate 7663ed852eea50f9d4cd633efb8c2b054b8e33c253cristy = cast<CXXRecordDecl>(PrevDecl)->getDescribedClassTemplate(); 7673ed852eea50f9d4cd633efb8c2b054b8e33c253cristy if (!PrevClassTemplate && isa<ClassTemplateSpecializationDecl>(PrevDecl)) { 7683ed852eea50f9d4cd633efb8c2b054b8e33c253cristy PrevClassTemplate 7693ed852eea50f9d4cd633efb8c2b054b8e33c253cristy = cast<ClassTemplateSpecializationDecl>(PrevDecl) 7703ed852eea50f9d4cd633efb8c2b054b8e33c253cristy ->getSpecializedTemplate(); 7713ed852eea50f9d4cd633efb8c2b054b8e33c253cristy } 7723ed852eea50f9d4cd633efb8c2b054b8e33c253cristy } 7733ed852eea50f9d4cd633efb8c2b054b8e33c253cristy 7743ed852eea50f9d4cd633efb8c2b054b8e33c253cristy if (TUK == TUK_Friend) { 7753ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // C++ [namespace.memdef]p3: 7763ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // [...] When looking for a prior declaration of a class or a function 7773ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // declared as a friend, and when the name of the friend class or 7783ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // function is neither a qualified name nor a template-id, scopes outside 7793ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // the innermost enclosing namespace scope are not considered. 7803ed852eea50f9d4cd633efb8c2b054b8e33c253cristy if (!SS.isSet()) { 7813ed852eea50f9d4cd633efb8c2b054b8e33c253cristy DeclContext *OutermostContext = CurContext; 7823ed852eea50f9d4cd633efb8c2b054b8e33c253cristy while (!OutermostContext->isFileContext()) 7833ed852eea50f9d4cd633efb8c2b054b8e33c253cristy OutermostContext = OutermostContext->getLookupParent(); 7843ed852eea50f9d4cd633efb8c2b054b8e33c253cristy 7853ed852eea50f9d4cd633efb8c2b054b8e33c253cristy if (PrevDecl && 7863ed852eea50f9d4cd633efb8c2b054b8e33c253cristy (OutermostContext->Equals(PrevDecl->getDeclContext()) || 7873ed852eea50f9d4cd633efb8c2b054b8e33c253cristy OutermostContext->Encloses(PrevDecl->getDeclContext()))) { 7883ed852eea50f9d4cd633efb8c2b054b8e33c253cristy SemanticContext = PrevDecl->getDeclContext(); 7893ed852eea50f9d4cd633efb8c2b054b8e33c253cristy } else { 7903ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // Declarations in outer scopes don't matter. However, the outermost 7913ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // context we computed is the semantic context for our new 7923ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // declaration. 7933ed852eea50f9d4cd633efb8c2b054b8e33c253cristy PrevDecl = PrevClassTemplate = 0; 7943ed852eea50f9d4cd633efb8c2b054b8e33c253cristy SemanticContext = OutermostContext; 7953ed852eea50f9d4cd633efb8c2b054b8e33c253cristy } 7963ed852eea50f9d4cd633efb8c2b054b8e33c253cristy } 7973ed852eea50f9d4cd633efb8c2b054b8e33c253cristy 7983ed852eea50f9d4cd633efb8c2b054b8e33c253cristy if (CurContext->isDependentContext()) { 7993ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // If this is a dependent context, we don't want to link the friend 8003ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // class template to the template in scope, because that would perform 8013ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // checking of the template parameter lists that can't be performed 8023ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // until the outer context is instantiated. 8033ed852eea50f9d4cd633efb8c2b054b8e33c253cristy PrevDecl = PrevClassTemplate = 0; 8043ed852eea50f9d4cd633efb8c2b054b8e33c253cristy } 8053ed852eea50f9d4cd633efb8c2b054b8e33c253cristy } else if (PrevDecl && !isDeclInScope(PrevDecl, SemanticContext, S)) 80653a8bc9b0c80f3027e032dcd8e77d8987c53bd6fcristy PrevDecl = PrevClassTemplate = 0; 80753a8bc9b0c80f3027e032dcd8e77d8987c53bd6fcristy 8083ed852eea50f9d4cd633efb8c2b054b8e33c253cristy if (PrevClassTemplate) { 8093ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // Ensure that the template parameter lists are compatible. 8103ed852eea50f9d4cd633efb8c2b054b8e33c253cristy if (!TemplateParameterListsAreEqual(TemplateParams, 8113ed852eea50f9d4cd633efb8c2b054b8e33c253cristy PrevClassTemplate->getTemplateParameters(), 8123ed852eea50f9d4cd633efb8c2b054b8e33c253cristy /*Complain=*/true, 8133ed852eea50f9d4cd633efb8c2b054b8e33c253cristy TPL_TemplateMatch)) 8143ed852eea50f9d4cd633efb8c2b054b8e33c253cristy return true; 8153ed852eea50f9d4cd633efb8c2b054b8e33c253cristy 8163ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // C++ [temp.class]p4: 8173ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // In a redeclaration, partial specialization, explicit 8183ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // specialization or explicit instantiation of a class template, 8193ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // the class-key shall agree in kind with the original class 8203ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // template declaration (7.1.5.3). 8213ed852eea50f9d4cd633efb8c2b054b8e33c253cristy RecordDecl *PrevRecordDecl = PrevClassTemplate->getTemplatedDecl(); 8223ed852eea50f9d4cd633efb8c2b054b8e33c253cristy if (!isAcceptableTagRedeclaration(PrevRecordDecl, Kind, KWLoc, *Name)) { 8233ed852eea50f9d4cd633efb8c2b054b8e33c253cristy Diag(KWLoc, diag::err_use_with_wrong_tag) 8243ed852eea50f9d4cd633efb8c2b054b8e33c253cristy << Name 8253ed852eea50f9d4cd633efb8c2b054b8e33c253cristy << FixItHint::CreateReplacement(KWLoc, PrevRecordDecl->getKindName()); 8263ed852eea50f9d4cd633efb8c2b054b8e33c253cristy Diag(PrevRecordDecl->getLocation(), diag::note_previous_use); 8273ed852eea50f9d4cd633efb8c2b054b8e33c253cristy Kind = PrevRecordDecl->getTagKind(); 8283ed852eea50f9d4cd633efb8c2b054b8e33c253cristy } 8293ed852eea50f9d4cd633efb8c2b054b8e33c253cristy 8303ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // Check for redefinition of this class template. 8313ed852eea50f9d4cd633efb8c2b054b8e33c253cristy if (TUK == TUK_Definition) { 8323ed852eea50f9d4cd633efb8c2b054b8e33c253cristy if (TagDecl *Def = PrevRecordDecl->getDefinition()) { 8333ed852eea50f9d4cd633efb8c2b054b8e33c253cristy Diag(NameLoc, diag::err_redefinition) << Name; 8343ed852eea50f9d4cd633efb8c2b054b8e33c253cristy Diag(Def->getLocation(), diag::note_previous_definition); 8353ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // FIXME: Would it make sense to try to "forget" the previous 8363ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // definition, as part of error recovery? 8373ed852eea50f9d4cd633efb8c2b054b8e33c253cristy return true; 8383ed852eea50f9d4cd633efb8c2b054b8e33c253cristy } 8393ed852eea50f9d4cd633efb8c2b054b8e33c253cristy } 8403ed852eea50f9d4cd633efb8c2b054b8e33c253cristy } else if (PrevDecl && PrevDecl->isTemplateParameter()) { 8413ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // Maybe we will complain about the shadowed template parameter. 8423ed852eea50f9d4cd633efb8c2b054b8e33c253cristy DiagnoseTemplateParameterShadow(NameLoc, PrevDecl); 8433ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // Just pretend that we didn't see the previous declaration. 8443ed852eea50f9d4cd633efb8c2b054b8e33c253cristy PrevDecl = 0; 8453ed852eea50f9d4cd633efb8c2b054b8e33c253cristy } else if (PrevDecl) { 8463ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // C++ [temp]p5: 8473ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // A class template shall not have the same name as any other 8483ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // template, class, function, object, enumeration, enumerator, 8493ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // namespace, or type in the same scope (3.3), except as specified 8503ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // in (14.5.4). 8513ed852eea50f9d4cd633efb8c2b054b8e33c253cristy Diag(NameLoc, diag::err_redefinition_different_kind) << Name; 8523ed852eea50f9d4cd633efb8c2b054b8e33c253cristy Diag(PrevDecl->getLocation(), diag::note_previous_definition); 8533ed852eea50f9d4cd633efb8c2b054b8e33c253cristy return true; 8543ed852eea50f9d4cd633efb8c2b054b8e33c253cristy } 8553ed852eea50f9d4cd633efb8c2b054b8e33c253cristy 8563ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // Check the template parameter list of this declaration, possibly 8573ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // merging in the template parameter list from the previous class 8583ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // template declaration. 8593ed852eea50f9d4cd633efb8c2b054b8e33c253cristy if (CheckTemplateParameterList(TemplateParams, 8603ed852eea50f9d4cd633efb8c2b054b8e33c253cristy PrevClassTemplate? PrevClassTemplate->getTemplateParameters() : 0, 8613ed852eea50f9d4cd633efb8c2b054b8e33c253cristy TPC_ClassTemplate)) 8623ed852eea50f9d4cd633efb8c2b054b8e33c253cristy Invalid = true; 8633ed852eea50f9d4cd633efb8c2b054b8e33c253cristy 8643ed852eea50f9d4cd633efb8c2b054b8e33c253cristy if (SS.isSet()) { 8653ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // If the name of the template was qualified, we must be defining the 8663ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // template out-of-line. 8673ed852eea50f9d4cd633efb8c2b054b8e33c253cristy if (!SS.isInvalid() && !Invalid && !PrevClassTemplate && 8683ed852eea50f9d4cd633efb8c2b054b8e33c253cristy !(TUK == TUK_Friend && CurContext->isDependentContext())) 8693ed852eea50f9d4cd633efb8c2b054b8e33c253cristy Diag(NameLoc, diag::err_member_def_does_not_match) 8703ed852eea50f9d4cd633efb8c2b054b8e33c253cristy << Name << SemanticContext << SS.getRange(); 871bb50337b2a8a16ca7e903cc04ab195ff0fd47ae6cristy } 8723ed852eea50f9d4cd633efb8c2b054b8e33c253cristy 8733ed852eea50f9d4cd633efb8c2b054b8e33c253cristy CXXRecordDecl *NewClass = 8743ed852eea50f9d4cd633efb8c2b054b8e33c253cristy CXXRecordDecl::Create(Context, Kind, SemanticContext, NameLoc, Name, KWLoc, 8753ed852eea50f9d4cd633efb8c2b054b8e33c253cristy PrevClassTemplate? 8763ed852eea50f9d4cd633efb8c2b054b8e33c253cristy PrevClassTemplate->getTemplatedDecl() : 0, 8773ed852eea50f9d4cd633efb8c2b054b8e33c253cristy /*DelayTypeCreation=*/true); 8783ed852eea50f9d4cd633efb8c2b054b8e33c253cristy SetNestedNameSpecifier(NewClass, SS); 8793ed852eea50f9d4cd633efb8c2b054b8e33c253cristy 8803ed852eea50f9d4cd633efb8c2b054b8e33c253cristy ClassTemplateDecl *NewTemplate 8813ed852eea50f9d4cd633efb8c2b054b8e33c253cristy = ClassTemplateDecl::Create(Context, SemanticContext, NameLoc, 8823ed852eea50f9d4cd633efb8c2b054b8e33c253cristy DeclarationName(Name), TemplateParams, 8833ed852eea50f9d4cd633efb8c2b054b8e33c253cristy NewClass, PrevClassTemplate); 884bb50337b2a8a16ca7e903cc04ab195ff0fd47ae6cristy NewClass->setDescribedClassTemplate(NewTemplate); 8853ed852eea50f9d4cd633efb8c2b054b8e33c253cristy 8863ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // Build the type for the class template declaration now. 8873ed852eea50f9d4cd633efb8c2b054b8e33c253cristy QualType T = NewTemplate->getInjectedClassNameSpecialization(); 8883ed852eea50f9d4cd633efb8c2b054b8e33c253cristy T = Context.getInjectedClassNameType(NewClass, T); 8893ed852eea50f9d4cd633efb8c2b054b8e33c253cristy assert(T->isDependentType() && "Class template type is not dependent?"); 8903ed852eea50f9d4cd633efb8c2b054b8e33c253cristy (void)T; 891bb50337b2a8a16ca7e903cc04ab195ff0fd47ae6cristy 892bb50337b2a8a16ca7e903cc04ab195ff0fd47ae6cristy // If we are providing an explicit specialization of a member that is a 8933ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // class template, make a note of that. 8943ed852eea50f9d4cd633efb8c2b054b8e33c253cristy if (PrevClassTemplate && 8953ed852eea50f9d4cd633efb8c2b054b8e33c253cristy PrevClassTemplate->getInstantiatedFromMemberTemplate()) 8963ed852eea50f9d4cd633efb8c2b054b8e33c253cristy PrevClassTemplate->setMemberSpecialization(); 8973ed852eea50f9d4cd633efb8c2b054b8e33c253cristy 8983ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // Set the access specifier. 8993ed852eea50f9d4cd633efb8c2b054b8e33c253cristy if (!Invalid && TUK != TUK_Friend) 9003ed852eea50f9d4cd633efb8c2b054b8e33c253cristy SetMemberAccessSpecifier(NewTemplate, PrevClassTemplate, AS); 9013ed852eea50f9d4cd633efb8c2b054b8e33c253cristy 9023ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // Set the lexical context of these templates 9033ed852eea50f9d4cd633efb8c2b054b8e33c253cristy NewClass->setLexicalDeclContext(CurContext); 9043ed852eea50f9d4cd633efb8c2b054b8e33c253cristy NewTemplate->setLexicalDeclContext(CurContext); 9053ed852eea50f9d4cd633efb8c2b054b8e33c253cristy 9063ed852eea50f9d4cd633efb8c2b054b8e33c253cristy if (TUK == TUK_Definition) 9073ed852eea50f9d4cd633efb8c2b054b8e33c253cristy NewClass->startDefinition(); 9083ed852eea50f9d4cd633efb8c2b054b8e33c253cristy 9093ed852eea50f9d4cd633efb8c2b054b8e33c253cristy if (Attr) 9103ed852eea50f9d4cd633efb8c2b054b8e33c253cristy ProcessDeclAttributeList(S, NewClass, Attr); 9113ed852eea50f9d4cd633efb8c2b054b8e33c253cristy 9123ed852eea50f9d4cd633efb8c2b054b8e33c253cristy if (TUK != TUK_Friend) 9133ed852eea50f9d4cd633efb8c2b054b8e33c253cristy PushOnScopeChains(NewTemplate, S); 9143ed852eea50f9d4cd633efb8c2b054b8e33c253cristy else { 9153ed852eea50f9d4cd633efb8c2b054b8e33c253cristy if (PrevClassTemplate && PrevClassTemplate->getAccess() != AS_none) { 9163ed852eea50f9d4cd633efb8c2b054b8e33c253cristy NewTemplate->setAccess(PrevClassTemplate->getAccess()); 9173ed852eea50f9d4cd633efb8c2b054b8e33c253cristy NewClass->setAccess(PrevClassTemplate->getAccess()); 9183ed852eea50f9d4cd633efb8c2b054b8e33c253cristy } 9193ed852eea50f9d4cd633efb8c2b054b8e33c253cristy 9203ed852eea50f9d4cd633efb8c2b054b8e33c253cristy NewTemplate->setObjectOfFriendDecl(/* PreviouslyDeclared = */ 9213ed852eea50f9d4cd633efb8c2b054b8e33c253cristy PrevClassTemplate != NULL); 9223ed852eea50f9d4cd633efb8c2b054b8e33c253cristy 9233ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // Friend templates are visible in fairly strange ways. 9243ed852eea50f9d4cd633efb8c2b054b8e33c253cristy if (!CurContext->isDependentContext()) { 9253ed852eea50f9d4cd633efb8c2b054b8e33c253cristy DeclContext *DC = SemanticContext->getLookupContext(); 9263ed852eea50f9d4cd633efb8c2b054b8e33c253cristy DC->makeDeclVisibleInContext(NewTemplate, /* Recoverable = */ false); 9273ed852eea50f9d4cd633efb8c2b054b8e33c253cristy if (Scope *EnclosingScope = getScopeForDeclContext(S, DC)) 9283ed852eea50f9d4cd633efb8c2b054b8e33c253cristy PushOnScopeChains(NewTemplate, EnclosingScope, 9293ed852eea50f9d4cd633efb8c2b054b8e33c253cristy /* AddToContext = */ false); 9303ed852eea50f9d4cd633efb8c2b054b8e33c253cristy } 9313ed852eea50f9d4cd633efb8c2b054b8e33c253cristy 9323ed852eea50f9d4cd633efb8c2b054b8e33c253cristy FriendDecl *Friend = FriendDecl::Create(Context, CurContext, 9333ed852eea50f9d4cd633efb8c2b054b8e33c253cristy NewClass->getLocation(), 9343ed852eea50f9d4cd633efb8c2b054b8e33c253cristy NewTemplate, 9353ed852eea50f9d4cd633efb8c2b054b8e33c253cristy /*FIXME:*/NewClass->getLocation()); 9363ed852eea50f9d4cd633efb8c2b054b8e33c253cristy Friend->setAccess(AS_public); 9373ed852eea50f9d4cd633efb8c2b054b8e33c253cristy CurContext->addDecl(Friend); 9383ed852eea50f9d4cd633efb8c2b054b8e33c253cristy } 9393ed852eea50f9d4cd633efb8c2b054b8e33c253cristy 940bb50337b2a8a16ca7e903cc04ab195ff0fd47ae6cristy if (Invalid) { 9413ed852eea50f9d4cd633efb8c2b054b8e33c253cristy NewTemplate->setInvalidDecl(); 9423ed852eea50f9d4cd633efb8c2b054b8e33c253cristy NewClass->setInvalidDecl(); 9433ed852eea50f9d4cd633efb8c2b054b8e33c253cristy } 9443ed852eea50f9d4cd633efb8c2b054b8e33c253cristy return DeclPtrTy::make(NewTemplate); 9453ed852eea50f9d4cd633efb8c2b054b8e33c253cristy} 9463ed852eea50f9d4cd633efb8c2b054b8e33c253cristy 9473ed852eea50f9d4cd633efb8c2b054b8e33c253cristy/// \brief Diagnose the presence of a default template argument on a 9483ed852eea50f9d4cd633efb8c2b054b8e33c253cristy/// template parameter, which is ill-formed in certain contexts. 9493ed852eea50f9d4cd633efb8c2b054b8e33c253cristy/// 9503ed852eea50f9d4cd633efb8c2b054b8e33c253cristy/// \returns true if the default template argument should be dropped. 9513ed852eea50f9d4cd633efb8c2b054b8e33c253cristystatic bool DiagnoseDefaultTemplateArgument(Sema &S, 9523ed852eea50f9d4cd633efb8c2b054b8e33c253cristy Sema::TemplateParamListContext TPC, 9533ed852eea50f9d4cd633efb8c2b054b8e33c253cristy SourceLocation ParamLoc, 9543ed852eea50f9d4cd633efb8c2b054b8e33c253cristy SourceRange DefArgRange) { 9553ed852eea50f9d4cd633efb8c2b054b8e33c253cristy switch (TPC) { 9563ed852eea50f9d4cd633efb8c2b054b8e33c253cristy case Sema::TPC_ClassTemplate: 9573ed852eea50f9d4cd633efb8c2b054b8e33c253cristy return false; 9583ed852eea50f9d4cd633efb8c2b054b8e33c253cristy 9593ed852eea50f9d4cd633efb8c2b054b8e33c253cristy case Sema::TPC_FunctionTemplate: 9603ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // C++ [temp.param]p9: 9613ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // A default template-argument shall not be specified in a 9623ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // function template declaration or a function template 9633ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // definition [...] 9643ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // (This sentence is not in C++0x, per DR226). 9653ed852eea50f9d4cd633efb8c2b054b8e33c253cristy if (!S.getLangOptions().CPlusPlus0x) 9663ed852eea50f9d4cd633efb8c2b054b8e33c253cristy S.Diag(ParamLoc, 9673ed852eea50f9d4cd633efb8c2b054b8e33c253cristy diag::err_template_parameter_default_in_function_template) 9683ed852eea50f9d4cd633efb8c2b054b8e33c253cristy << DefArgRange; 9693ed852eea50f9d4cd633efb8c2b054b8e33c253cristy return false; 9703ed852eea50f9d4cd633efb8c2b054b8e33c253cristy 9713ed852eea50f9d4cd633efb8c2b054b8e33c253cristy case Sema::TPC_ClassTemplateMember: 9723ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // C++0x [temp.param]p9: 9733ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // A default template-argument shall not be specified in the 9743ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // template-parameter-lists of the definition of a member of a 9753ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // class template that appears outside of the member's class. 9763ed852eea50f9d4cd633efb8c2b054b8e33c253cristy S.Diag(ParamLoc, diag::err_template_parameter_default_template_member) 9773ed852eea50f9d4cd633efb8c2b054b8e33c253cristy << DefArgRange; 9783ed852eea50f9d4cd633efb8c2b054b8e33c253cristy return true; 9793ed852eea50f9d4cd633efb8c2b054b8e33c253cristy 980bb50337b2a8a16ca7e903cc04ab195ff0fd47ae6cristy case Sema::TPC_FriendFunctionTemplate: 9813ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // C++ [temp.param]p9: 9823ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // A default template-argument shall not be specified in a 983eaedf06777741da32408da72c1e512975c600c48cristy // friend template declaration. 984eaedf06777741da32408da72c1e512975c600c48cristy S.Diag(ParamLoc, diag::err_template_parameter_default_friend_template) 985eaedf06777741da32408da72c1e512975c600c48cristy << DefArgRange; 9863ed852eea50f9d4cd633efb8c2b054b8e33c253cristy return true; 9873ed852eea50f9d4cd633efb8c2b054b8e33c253cristy 9883ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // FIXME: C++0x [temp.param]p9 allows default template-arguments 9893ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // for friend function templates if there is only a single 9903ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // declaration (and it is a definition). Strange! 9913ed852eea50f9d4cd633efb8c2b054b8e33c253cristy } 9923ed852eea50f9d4cd633efb8c2b054b8e33c253cristy 9933ed852eea50f9d4cd633efb8c2b054b8e33c253cristy return false; 9943ed852eea50f9d4cd633efb8c2b054b8e33c253cristy} 9953ed852eea50f9d4cd633efb8c2b054b8e33c253cristy 9963ed852eea50f9d4cd633efb8c2b054b8e33c253cristy/// \brief Checks the validity of a template parameter list, possibly 9973ed852eea50f9d4cd633efb8c2b054b8e33c253cristy/// considering the template parameter list from a previous 998eaedf06777741da32408da72c1e512975c600c48cristy/// declaration. 9993ed852eea50f9d4cd633efb8c2b054b8e33c253cristy/// 10003ed852eea50f9d4cd633efb8c2b054b8e33c253cristy/// If an "old" template parameter list is provided, it must be 10013ed852eea50f9d4cd633efb8c2b054b8e33c253cristy/// equivalent (per TemplateParameterListsAreEqual) to the "new" 10023ed852eea50f9d4cd633efb8c2b054b8e33c253cristy/// template parameter list. 10033ed852eea50f9d4cd633efb8c2b054b8e33c253cristy/// 10043ed852eea50f9d4cd633efb8c2b054b8e33c253cristy/// \param NewParams Template parameter list for a new template 10053ed852eea50f9d4cd633efb8c2b054b8e33c253cristy/// declaration. This template parameter list will be updated with any 10063ed852eea50f9d4cd633efb8c2b054b8e33c253cristy/// default arguments that are carried through from the previous 10073ed852eea50f9d4cd633efb8c2b054b8e33c253cristy/// template parameter list. 10083ed852eea50f9d4cd633efb8c2b054b8e33c253cristy/// 10093ed852eea50f9d4cd633efb8c2b054b8e33c253cristy/// \param OldParams If provided, template parameter list from a 10103ed852eea50f9d4cd633efb8c2b054b8e33c253cristy/// previous declaration of the same template. Default template 10113ed852eea50f9d4cd633efb8c2b054b8e33c253cristy/// arguments will be merged from the old template parameter list to 10123ed852eea50f9d4cd633efb8c2b054b8e33c253cristy/// the new template parameter list. 10133ed852eea50f9d4cd633efb8c2b054b8e33c253cristy/// 10143ed852eea50f9d4cd633efb8c2b054b8e33c253cristy/// \param TPC Describes the context in which we are checking the given 10153ed852eea50f9d4cd633efb8c2b054b8e33c253cristy/// template parameter list. 10163ed852eea50f9d4cd633efb8c2b054b8e33c253cristy/// 10173ed852eea50f9d4cd633efb8c2b054b8e33c253cristy/// \returns true if an error occurred, false otherwise. 10183ed852eea50f9d4cd633efb8c2b054b8e33c253cristybool Sema::CheckTemplateParameterList(TemplateParameterList *NewParams, 10193ed852eea50f9d4cd633efb8c2b054b8e33c253cristy TemplateParameterList *OldParams, 10203ed852eea50f9d4cd633efb8c2b054b8e33c253cristy TemplateParamListContext TPC) { 10213ed852eea50f9d4cd633efb8c2b054b8e33c253cristy bool Invalid = false; 10223ed852eea50f9d4cd633efb8c2b054b8e33c253cristy 10233ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // C++ [temp.param]p10: 10243ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // The set of default template-arguments available for use with a 10253ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // template declaration or definition is obtained by merging the 10263ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // default arguments from the definition (if in scope) and all 10273ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // declarations in scope in the same way default function 10283ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // arguments are (8.3.6). 10293ed852eea50f9d4cd633efb8c2b054b8e33c253cristy bool SawDefaultArgument = false; 10303ed852eea50f9d4cd633efb8c2b054b8e33c253cristy SourceLocation PreviousDefaultArgLoc; 10313ed852eea50f9d4cd633efb8c2b054b8e33c253cristy 10323ed852eea50f9d4cd633efb8c2b054b8e33c253cristy bool SawParameterPack = false; 10333ed852eea50f9d4cd633efb8c2b054b8e33c253cristy SourceLocation ParameterPackLoc; 10343ed852eea50f9d4cd633efb8c2b054b8e33c253cristy 10353ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // Dummy initialization to avoid warnings. 10363ed852eea50f9d4cd633efb8c2b054b8e33c253cristy TemplateParameterList::iterator OldParam = NewParams->end(); 10373ed852eea50f9d4cd633efb8c2b054b8e33c253cristy if (OldParams) 10383ed852eea50f9d4cd633efb8c2b054b8e33c253cristy OldParam = OldParams->begin(); 10393ed852eea50f9d4cd633efb8c2b054b8e33c253cristy 10403ed852eea50f9d4cd633efb8c2b054b8e33c253cristy for (TemplateParameterList::iterator NewParam = NewParams->begin(), 10413ed852eea50f9d4cd633efb8c2b054b8e33c253cristy NewParamEnd = NewParams->end(); 10423ed852eea50f9d4cd633efb8c2b054b8e33c253cristy NewParam != NewParamEnd; ++NewParam) { 10433ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // Variables used to diagnose redundant default arguments 10443ed852eea50f9d4cd633efb8c2b054b8e33c253cristy bool RedundantDefaultArg = false; 10453ed852eea50f9d4cd633efb8c2b054b8e33c253cristy SourceLocation OldDefaultLoc; 10463ed852eea50f9d4cd633efb8c2b054b8e33c253cristy SourceLocation NewDefaultLoc; 10473ed852eea50f9d4cd633efb8c2b054b8e33c253cristy 10483ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // Variables used to diagnose missing default arguments 10493ed852eea50f9d4cd633efb8c2b054b8e33c253cristy bool MissingDefaultArg = false; 10503ed852eea50f9d4cd633efb8c2b054b8e33c253cristy 10513ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // C++0x [temp.param]p11: 10523ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // If a template parameter of a class template is a template parameter pack, 10533ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // it must be the last template parameter. 10543ed852eea50f9d4cd633efb8c2b054b8e33c253cristy if (SawParameterPack) { 10553ed852eea50f9d4cd633efb8c2b054b8e33c253cristy Diag(ParameterPackLoc, 10563ed852eea50f9d4cd633efb8c2b054b8e33c253cristy diag::err_template_param_pack_must_be_last_template_parameter); 10573ed852eea50f9d4cd633efb8c2b054b8e33c253cristy Invalid = true; 10583ed852eea50f9d4cd633efb8c2b054b8e33c253cristy } 10593ed852eea50f9d4cd633efb8c2b054b8e33c253cristy 10603ed852eea50f9d4cd633efb8c2b054b8e33c253cristy if (TemplateTypeParmDecl *NewTypeParm 10613ed852eea50f9d4cd633efb8c2b054b8e33c253cristy = dyn_cast<TemplateTypeParmDecl>(*NewParam)) { 10623ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // Check the presence of a default argument here. 10633ed852eea50f9d4cd633efb8c2b054b8e33c253cristy if (NewTypeParm->hasDefaultArgument() && 10643ed852eea50f9d4cd633efb8c2b054b8e33c253cristy DiagnoseDefaultTemplateArgument(*this, TPC, 10653ed852eea50f9d4cd633efb8c2b054b8e33c253cristy NewTypeParm->getLocation(), 10663ed852eea50f9d4cd633efb8c2b054b8e33c253cristy NewTypeParm->getDefaultArgumentInfo()->getTypeLoc() 10673ed852eea50f9d4cd633efb8c2b054b8e33c253cristy .getSourceRange())) 10683ed852eea50f9d4cd633efb8c2b054b8e33c253cristy NewTypeParm->removeDefaultArgument(); 10693ed852eea50f9d4cd633efb8c2b054b8e33c253cristy 10703ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // Merge default arguments for template type parameters. 10713ed852eea50f9d4cd633efb8c2b054b8e33c253cristy TemplateTypeParmDecl *OldTypeParm 10723ed852eea50f9d4cd633efb8c2b054b8e33c253cristy = OldParams? cast<TemplateTypeParmDecl>(*OldParam) : 0; 10733ed852eea50f9d4cd633efb8c2b054b8e33c253cristy 10743ed852eea50f9d4cd633efb8c2b054b8e33c253cristy if (NewTypeParm->isParameterPack()) { 10753ed852eea50f9d4cd633efb8c2b054b8e33c253cristy assert(!NewTypeParm->hasDefaultArgument() && 10763ed852eea50f9d4cd633efb8c2b054b8e33c253cristy "Parameter packs can't have a default argument!"); 10773ed852eea50f9d4cd633efb8c2b054b8e33c253cristy SawParameterPack = true; 10783ed852eea50f9d4cd633efb8c2b054b8e33c253cristy ParameterPackLoc = NewTypeParm->getLocation(); 10793ed852eea50f9d4cd633efb8c2b054b8e33c253cristy } else if (OldTypeParm && OldTypeParm->hasDefaultArgument() && 10803ed852eea50f9d4cd633efb8c2b054b8e33c253cristy NewTypeParm->hasDefaultArgument()) { 10813ed852eea50f9d4cd633efb8c2b054b8e33c253cristy OldDefaultLoc = OldTypeParm->getDefaultArgumentLoc(); 10823ed852eea50f9d4cd633efb8c2b054b8e33c253cristy NewDefaultLoc = NewTypeParm->getDefaultArgumentLoc(); 10833ed852eea50f9d4cd633efb8c2b054b8e33c253cristy SawDefaultArgument = true; 10843ed852eea50f9d4cd633efb8c2b054b8e33c253cristy RedundantDefaultArg = true; 10853ed852eea50f9d4cd633efb8c2b054b8e33c253cristy PreviousDefaultArgLoc = NewDefaultLoc; 10863ed852eea50f9d4cd633efb8c2b054b8e33c253cristy } else if (OldTypeParm && OldTypeParm->hasDefaultArgument()) { 10873ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // Merge the default argument from the old declaration to the 10883ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // new declaration. 10893ed852eea50f9d4cd633efb8c2b054b8e33c253cristy SawDefaultArgument = true; 10903ed852eea50f9d4cd633efb8c2b054b8e33c253cristy NewTypeParm->setDefaultArgument(OldTypeParm->getDefaultArgumentInfo(), 10913ed852eea50f9d4cd633efb8c2b054b8e33c253cristy true); 1092bb50337b2a8a16ca7e903cc04ab195ff0fd47ae6cristy PreviousDefaultArgLoc = OldTypeParm->getDefaultArgumentLoc(); 10933ed852eea50f9d4cd633efb8c2b054b8e33c253cristy } else if (NewTypeParm->hasDefaultArgument()) { 10943ed852eea50f9d4cd633efb8c2b054b8e33c253cristy SawDefaultArgument = true; 10953ed852eea50f9d4cd633efb8c2b054b8e33c253cristy PreviousDefaultArgLoc = NewTypeParm->getDefaultArgumentLoc(); 10963ed852eea50f9d4cd633efb8c2b054b8e33c253cristy } else if (SawDefaultArgument) 10973ed852eea50f9d4cd633efb8c2b054b8e33c253cristy MissingDefaultArg = true; 10983ed852eea50f9d4cd633efb8c2b054b8e33c253cristy } else if (NonTypeTemplateParmDecl *NewNonTypeParm 10993ed852eea50f9d4cd633efb8c2b054b8e33c253cristy = dyn_cast<NonTypeTemplateParmDecl>(*NewParam)) { 11003ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // Check the presence of a default argument here. 11013ed852eea50f9d4cd633efb8c2b054b8e33c253cristy if (NewNonTypeParm->hasDefaultArgument() && 11023ed852eea50f9d4cd633efb8c2b054b8e33c253cristy DiagnoseDefaultTemplateArgument(*this, TPC, 11033ed852eea50f9d4cd633efb8c2b054b8e33c253cristy NewNonTypeParm->getLocation(), 11043ed852eea50f9d4cd633efb8c2b054b8e33c253cristy NewNonTypeParm->getDefaultArgument()->getSourceRange())) { 11053ed852eea50f9d4cd633efb8c2b054b8e33c253cristy NewNonTypeParm->getDefaultArgument()->Destroy(Context); 11063ed852eea50f9d4cd633efb8c2b054b8e33c253cristy NewNonTypeParm->removeDefaultArgument(); 11073ed852eea50f9d4cd633efb8c2b054b8e33c253cristy } 11083ed852eea50f9d4cd633efb8c2b054b8e33c253cristy 11093ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // Merge default arguments for non-type template parameters 11103ed852eea50f9d4cd633efb8c2b054b8e33c253cristy NonTypeTemplateParmDecl *OldNonTypeParm 11113ed852eea50f9d4cd633efb8c2b054b8e33c253cristy = OldParams? cast<NonTypeTemplateParmDecl>(*OldParam) : 0; 11123ed852eea50f9d4cd633efb8c2b054b8e33c253cristy if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument() && 11133ed852eea50f9d4cd633efb8c2b054b8e33c253cristy NewNonTypeParm->hasDefaultArgument()) { 11143ed852eea50f9d4cd633efb8c2b054b8e33c253cristy OldDefaultLoc = OldNonTypeParm->getDefaultArgumentLoc(); 11153ed852eea50f9d4cd633efb8c2b054b8e33c253cristy NewDefaultLoc = NewNonTypeParm->getDefaultArgumentLoc(); 11163ed852eea50f9d4cd633efb8c2b054b8e33c253cristy SawDefaultArgument = true; 11173ed852eea50f9d4cd633efb8c2b054b8e33c253cristy RedundantDefaultArg = true; 11183ed852eea50f9d4cd633efb8c2b054b8e33c253cristy PreviousDefaultArgLoc = NewDefaultLoc; 11193ed852eea50f9d4cd633efb8c2b054b8e33c253cristy } else if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument()) { 11203ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // Merge the default argument from the old declaration to the 11213ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // new declaration. 11223ed852eea50f9d4cd633efb8c2b054b8e33c253cristy SawDefaultArgument = true; 11233ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // FIXME: We need to create a new kind of "default argument" 11243ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // expression that points to a previous template template 11253ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // parameter. 1126eaedf06777741da32408da72c1e512975c600c48cristy NewNonTypeParm->setDefaultArgument( 11273ed852eea50f9d4cd633efb8c2b054b8e33c253cristy OldNonTypeParm->getDefaultArgument(), 11283ed852eea50f9d4cd633efb8c2b054b8e33c253cristy /*Inherited=*/ true); 11293ed852eea50f9d4cd633efb8c2b054b8e33c253cristy PreviousDefaultArgLoc = OldNonTypeParm->getDefaultArgumentLoc(); 11303ed852eea50f9d4cd633efb8c2b054b8e33c253cristy } else if (NewNonTypeParm->hasDefaultArgument()) { 11313ed852eea50f9d4cd633efb8c2b054b8e33c253cristy SawDefaultArgument = true; 11323ed852eea50f9d4cd633efb8c2b054b8e33c253cristy PreviousDefaultArgLoc = NewNonTypeParm->getDefaultArgumentLoc(); 11333ed852eea50f9d4cd633efb8c2b054b8e33c253cristy } else if (SawDefaultArgument) 11343ed852eea50f9d4cd633efb8c2b054b8e33c253cristy MissingDefaultArg = true; 11353ed852eea50f9d4cd633efb8c2b054b8e33c253cristy } else { 11363ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // Check the presence of a default argument here. 11373ed852eea50f9d4cd633efb8c2b054b8e33c253cristy TemplateTemplateParmDecl *NewTemplateParm 11383ed852eea50f9d4cd633efb8c2b054b8e33c253cristy = cast<TemplateTemplateParmDecl>(*NewParam); 11393ed852eea50f9d4cd633efb8c2b054b8e33c253cristy if (NewTemplateParm->hasDefaultArgument() && 11403ed852eea50f9d4cd633efb8c2b054b8e33c253cristy DiagnoseDefaultTemplateArgument(*this, TPC, 11413ed852eea50f9d4cd633efb8c2b054b8e33c253cristy NewTemplateParm->getLocation(), 11423ed852eea50f9d4cd633efb8c2b054b8e33c253cristy NewTemplateParm->getDefaultArgument().getSourceRange())) 11433ed852eea50f9d4cd633efb8c2b054b8e33c253cristy NewTemplateParm->removeDefaultArgument(); 1144eaedf06777741da32408da72c1e512975c600c48cristy 11453ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // Merge default arguments for template template parameters 1146eaedf06777741da32408da72c1e512975c600c48cristy TemplateTemplateParmDecl *OldTemplateParm 11473ed852eea50f9d4cd633efb8c2b054b8e33c253cristy = OldParams? cast<TemplateTemplateParmDecl>(*OldParam) : 0; 11483ed852eea50f9d4cd633efb8c2b054b8e33c253cristy if (OldTemplateParm && OldTemplateParm->hasDefaultArgument() && 11493ed852eea50f9d4cd633efb8c2b054b8e33c253cristy NewTemplateParm->hasDefaultArgument()) { 11503ed852eea50f9d4cd633efb8c2b054b8e33c253cristy OldDefaultLoc = OldTemplateParm->getDefaultArgument().getLocation(); 11513ed852eea50f9d4cd633efb8c2b054b8e33c253cristy NewDefaultLoc = NewTemplateParm->getDefaultArgument().getLocation(); 11523ed852eea50f9d4cd633efb8c2b054b8e33c253cristy SawDefaultArgument = true; 11533ed852eea50f9d4cd633efb8c2b054b8e33c253cristy RedundantDefaultArg = true; 11543ed852eea50f9d4cd633efb8c2b054b8e33c253cristy PreviousDefaultArgLoc = NewDefaultLoc; 11553ed852eea50f9d4cd633efb8c2b054b8e33c253cristy } else if (OldTemplateParm && OldTemplateParm->hasDefaultArgument()) { 11563ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // Merge the default argument from the old declaration to the 11573ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // new declaration. 11583ed852eea50f9d4cd633efb8c2b054b8e33c253cristy SawDefaultArgument = true; 11593ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // FIXME: We need to create a new kind of "default argument" expression 11603ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // that points to a previous template template parameter. 11613ed852eea50f9d4cd633efb8c2b054b8e33c253cristy NewTemplateParm->setDefaultArgument( 11623ed852eea50f9d4cd633efb8c2b054b8e33c253cristy OldTemplateParm->getDefaultArgument(), 11633ed852eea50f9d4cd633efb8c2b054b8e33c253cristy /*Inherited=*/ true); 11643ed852eea50f9d4cd633efb8c2b054b8e33c253cristy PreviousDefaultArgLoc 11653ed852eea50f9d4cd633efb8c2b054b8e33c253cristy = OldTemplateParm->getDefaultArgument().getLocation(); 11663ed852eea50f9d4cd633efb8c2b054b8e33c253cristy } else if (NewTemplateParm->hasDefaultArgument()) { 11673ed852eea50f9d4cd633efb8c2b054b8e33c253cristy SawDefaultArgument = true; 11683ed852eea50f9d4cd633efb8c2b054b8e33c253cristy PreviousDefaultArgLoc 11693ed852eea50f9d4cd633efb8c2b054b8e33c253cristy = NewTemplateParm->getDefaultArgument().getLocation(); 11703ed852eea50f9d4cd633efb8c2b054b8e33c253cristy } else if (SawDefaultArgument) 11713ed852eea50f9d4cd633efb8c2b054b8e33c253cristy MissingDefaultArg = true; 11723ed852eea50f9d4cd633efb8c2b054b8e33c253cristy } 11733ed852eea50f9d4cd633efb8c2b054b8e33c253cristy 11743ed852eea50f9d4cd633efb8c2b054b8e33c253cristy if (RedundantDefaultArg) { 11753ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // C++ [temp.param]p12: 11763ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // A template-parameter shall not be given default arguments 11773ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // by two different declarations in the same scope. 11783ed852eea50f9d4cd633efb8c2b054b8e33c253cristy Diag(NewDefaultLoc, diag::err_template_param_default_arg_redefinition); 11793ed852eea50f9d4cd633efb8c2b054b8e33c253cristy Diag(OldDefaultLoc, diag::note_template_param_prev_default_arg); 11803ed852eea50f9d4cd633efb8c2b054b8e33c253cristy Invalid = true; 11813ed852eea50f9d4cd633efb8c2b054b8e33c253cristy } else if (MissingDefaultArg) { 11823ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // C++ [temp.param]p11: 11833ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // If a template-parameter has a default template-argument, 11843ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // all subsequent template-parameters shall have a default 11853ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // template-argument supplied. 11863ed852eea50f9d4cd633efb8c2b054b8e33c253cristy Diag((*NewParam)->getLocation(), 11873ed852eea50f9d4cd633efb8c2b054b8e33c253cristy diag::err_template_param_default_arg_missing); 11883ed852eea50f9d4cd633efb8c2b054b8e33c253cristy Diag(PreviousDefaultArgLoc, diag::note_template_param_prev_default_arg); 11893ed852eea50f9d4cd633efb8c2b054b8e33c253cristy Invalid = true; 11903ed852eea50f9d4cd633efb8c2b054b8e33c253cristy } 11913ed852eea50f9d4cd633efb8c2b054b8e33c253cristy 11923ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // If we have an old template parameter list that we're merging 11933ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // in, move on to the next parameter. 11943ed852eea50f9d4cd633efb8c2b054b8e33c253cristy if (OldParams) 11953ed852eea50f9d4cd633efb8c2b054b8e33c253cristy ++OldParam; 11963ed852eea50f9d4cd633efb8c2b054b8e33c253cristy } 11973ed852eea50f9d4cd633efb8c2b054b8e33c253cristy 11983ed852eea50f9d4cd633efb8c2b054b8e33c253cristy return Invalid; 11993ed852eea50f9d4cd633efb8c2b054b8e33c253cristy} 12003ed852eea50f9d4cd633efb8c2b054b8e33c253cristy 12013ed852eea50f9d4cd633efb8c2b054b8e33c253cristy/// \brief Match the given template parameter lists to the given scope 12023ed852eea50f9d4cd633efb8c2b054b8e33c253cristy/// specifier, returning the template parameter list that applies to the 12033ed852eea50f9d4cd633efb8c2b054b8e33c253cristy/// name. 12043ed852eea50f9d4cd633efb8c2b054b8e33c253cristy/// 12053ed852eea50f9d4cd633efb8c2b054b8e33c253cristy/// \param DeclStartLoc the start of the declaration that has a scope 12063ed852eea50f9d4cd633efb8c2b054b8e33c253cristy/// specifier or a template parameter list. 12073ed852eea50f9d4cd633efb8c2b054b8e33c253cristy/// 12083ed852eea50f9d4cd633efb8c2b054b8e33c253cristy/// \param SS the scope specifier that will be matched to the given template 12093ed852eea50f9d4cd633efb8c2b054b8e33c253cristy/// parameter lists. This scope specifier precedes a qualified name that is 12103ed852eea50f9d4cd633efb8c2b054b8e33c253cristy/// being declared. 12113ed852eea50f9d4cd633efb8c2b054b8e33c253cristy/// 12123ed852eea50f9d4cd633efb8c2b054b8e33c253cristy/// \param ParamLists the template parameter lists, from the outermost to the 12133ed852eea50f9d4cd633efb8c2b054b8e33c253cristy/// innermost template parameter lists. 12143ed852eea50f9d4cd633efb8c2b054b8e33c253cristy/// 12153ed852eea50f9d4cd633efb8c2b054b8e33c253cristy/// \param NumParamLists the number of template parameter lists in ParamLists. 12163ed852eea50f9d4cd633efb8c2b054b8e33c253cristy/// 12173ed852eea50f9d4cd633efb8c2b054b8e33c253cristy/// \param IsFriend Whether to apply the slightly different rules for 1218bb50337b2a8a16ca7e903cc04ab195ff0fd47ae6cristy/// matching template parameters to scope specifiers in friend 12193ed852eea50f9d4cd633efb8c2b054b8e33c253cristy/// declarations. 12203ed852eea50f9d4cd633efb8c2b054b8e33c253cristy/// 12213ed852eea50f9d4cd633efb8c2b054b8e33c253cristy/// \param IsExplicitSpecialization will be set true if the entity being 12223ed852eea50f9d4cd633efb8c2b054b8e33c253cristy/// declared is an explicit specialization, false otherwise. 12233ed852eea50f9d4cd633efb8c2b054b8e33c253cristy/// 12243ed852eea50f9d4cd633efb8c2b054b8e33c253cristy/// \returns the template parameter list, if any, that corresponds to the 12253ed852eea50f9d4cd633efb8c2b054b8e33c253cristy/// name that is preceded by the scope specifier @p SS. This template 12263ed852eea50f9d4cd633efb8c2b054b8e33c253cristy/// parameter list may be have template parameters (if we're declaring a 12273ed852eea50f9d4cd633efb8c2b054b8e33c253cristy/// template) or may have no template parameters (if we're declaring a 12283ed852eea50f9d4cd633efb8c2b054b8e33c253cristy/// template specialization), or may be NULL (if we were's declaring isn't 12293ed852eea50f9d4cd633efb8c2b054b8e33c253cristy/// itself a template). 12303ed852eea50f9d4cd633efb8c2b054b8e33c253cristyTemplateParameterList * 12313ed852eea50f9d4cd633efb8c2b054b8e33c253cristySema::MatchTemplateParametersToScopeSpecifier(SourceLocation DeclStartLoc, 12323ed852eea50f9d4cd633efb8c2b054b8e33c253cristy const CXXScopeSpec &SS, 12333ed852eea50f9d4cd633efb8c2b054b8e33c253cristy TemplateParameterList **ParamLists, 12343ed852eea50f9d4cd633efb8c2b054b8e33c253cristy unsigned NumParamLists, 12353ed852eea50f9d4cd633efb8c2b054b8e33c253cristy bool IsFriend, 12363ed852eea50f9d4cd633efb8c2b054b8e33c253cristy bool &IsExplicitSpecialization) { 12373ed852eea50f9d4cd633efb8c2b054b8e33c253cristy IsExplicitSpecialization = false; 12383ed852eea50f9d4cd633efb8c2b054b8e33c253cristy 12393ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // Find the template-ids that occur within the nested-name-specifier. These 12403ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // template-ids will match up with the template parameter lists. 12413ed852eea50f9d4cd633efb8c2b054b8e33c253cristy llvm::SmallVector<const TemplateSpecializationType *, 4> 12423ed852eea50f9d4cd633efb8c2b054b8e33c253cristy TemplateIdsInSpecifier; 12433ed852eea50f9d4cd633efb8c2b054b8e33c253cristy llvm::SmallVector<ClassTemplateSpecializationDecl *, 4> 12443ed852eea50f9d4cd633efb8c2b054b8e33c253cristy ExplicitSpecializationsInSpecifier; 12453ed852eea50f9d4cd633efb8c2b054b8e33c253cristy for (NestedNameSpecifier *NNS = (NestedNameSpecifier *)SS.getScopeRep(); 12463ed852eea50f9d4cd633efb8c2b054b8e33c253cristy NNS; NNS = NNS->getPrefix()) { 12473ed852eea50f9d4cd633efb8c2b054b8e33c253cristy const Type *T = NNS->getAsType(); 12483ed852eea50f9d4cd633efb8c2b054b8e33c253cristy if (!T) break; 12493ed852eea50f9d4cd633efb8c2b054b8e33c253cristy 12503ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // C++0x [temp.expl.spec]p17: 12513ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // A member or a member template may be nested within many 12523ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // enclosing class templates. In an explicit specialization for 12533ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // such a member, the member declaration shall be preceded by a 12543ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // template<> for each enclosing class template that is 12553ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // explicitly specialized. 12563ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // 12573ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // Following the existing practice of GNU and EDG, we allow a typedef of a 12583ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // template specialization type. 12593ed852eea50f9d4cd633efb8c2b054b8e33c253cristy if (const TypedefType *TT = dyn_cast<TypedefType>(T)) 12603ed852eea50f9d4cd633efb8c2b054b8e33c253cristy T = TT->LookThroughTypedefs().getTypePtr(); 12613ed852eea50f9d4cd633efb8c2b054b8e33c253cristy 12623ed852eea50f9d4cd633efb8c2b054b8e33c253cristy if (const TemplateSpecializationType *SpecType 12633ed852eea50f9d4cd633efb8c2b054b8e33c253cristy = dyn_cast<TemplateSpecializationType>(T)) { 12643ed852eea50f9d4cd633efb8c2b054b8e33c253cristy TemplateDecl *Template = SpecType->getTemplateName().getAsTemplateDecl(); 12653ed852eea50f9d4cd633efb8c2b054b8e33c253cristy if (!Template) 12663ed852eea50f9d4cd633efb8c2b054b8e33c253cristy continue; // FIXME: should this be an error? probably... 12673ed852eea50f9d4cd633efb8c2b054b8e33c253cristy 12683ed852eea50f9d4cd633efb8c2b054b8e33c253cristy if (const RecordType *Record = SpecType->getAs<RecordType>()) { 12693ed852eea50f9d4cd633efb8c2b054b8e33c253cristy ClassTemplateSpecializationDecl *SpecDecl 12703ed852eea50f9d4cd633efb8c2b054b8e33c253cristy = cast<ClassTemplateSpecializationDecl>(Record->getDecl()); 12713ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // If the nested name specifier refers to an explicit specialization, 12723ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // we don't need a template<> header. 12733ed852eea50f9d4cd633efb8c2b054b8e33c253cristy if (SpecDecl->getSpecializationKind() == TSK_ExplicitSpecialization) { 12743ed852eea50f9d4cd633efb8c2b054b8e33c253cristy ExplicitSpecializationsInSpecifier.push_back(SpecDecl); 12753ed852eea50f9d4cd633efb8c2b054b8e33c253cristy continue; 12763ed852eea50f9d4cd633efb8c2b054b8e33c253cristy } 12773ed852eea50f9d4cd633efb8c2b054b8e33c253cristy } 12783ed852eea50f9d4cd633efb8c2b054b8e33c253cristy 12793ed852eea50f9d4cd633efb8c2b054b8e33c253cristy TemplateIdsInSpecifier.push_back(SpecType); 12803ed852eea50f9d4cd633efb8c2b054b8e33c253cristy } 12813ed852eea50f9d4cd633efb8c2b054b8e33c253cristy } 12823ed852eea50f9d4cd633efb8c2b054b8e33c253cristy 12833ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // Reverse the list of template-ids in the scope specifier, so that we can 12843ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // more easily match up the template-ids and the template parameter lists. 12853ed852eea50f9d4cd633efb8c2b054b8e33c253cristy std::reverse(TemplateIdsInSpecifier.begin(), TemplateIdsInSpecifier.end()); 12863ed852eea50f9d4cd633efb8c2b054b8e33c253cristy 12873ed852eea50f9d4cd633efb8c2b054b8e33c253cristy SourceLocation FirstTemplateLoc = DeclStartLoc; 12883ed852eea50f9d4cd633efb8c2b054b8e33c253cristy if (NumParamLists) 12893ed852eea50f9d4cd633efb8c2b054b8e33c253cristy FirstTemplateLoc = ParamLists[0]->getTemplateLoc(); 12903ed852eea50f9d4cd633efb8c2b054b8e33c253cristy 12913ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // Match the template-ids found in the specifier to the template parameter 12923ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // lists. 12933ed852eea50f9d4cd633efb8c2b054b8e33c253cristy unsigned Idx = 0; 12943ed852eea50f9d4cd633efb8c2b054b8e33c253cristy for (unsigned NumTemplateIds = TemplateIdsInSpecifier.size(); 12953ed852eea50f9d4cd633efb8c2b054b8e33c253cristy Idx != NumTemplateIds; ++Idx) { 12963ed852eea50f9d4cd633efb8c2b054b8e33c253cristy QualType TemplateId = QualType(TemplateIdsInSpecifier[Idx], 0); 12973ed852eea50f9d4cd633efb8c2b054b8e33c253cristy bool DependentTemplateId = TemplateId->isDependentType(); 12983ed852eea50f9d4cd633efb8c2b054b8e33c253cristy if (Idx >= NumParamLists) { 12993ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // We have a template-id without a corresponding template parameter 13003ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // list. 13013ed852eea50f9d4cd633efb8c2b054b8e33c253cristy 13023ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // ...which is fine if this is a friend declaration. 13033ed852eea50f9d4cd633efb8c2b054b8e33c253cristy if (IsFriend) { 13043ed852eea50f9d4cd633efb8c2b054b8e33c253cristy IsExplicitSpecialization = true; 13053ed852eea50f9d4cd633efb8c2b054b8e33c253cristy break; 13060b29b25525c4612c77b1b3c8abcc40685d0aa33fcristy } 13070b29b25525c4612c77b1b3c8abcc40685d0aa33fcristy 13080b29b25525c4612c77b1b3c8abcc40685d0aa33fcristy if (DependentTemplateId) { 13090b29b25525c4612c77b1b3c8abcc40685d0aa33fcristy // FIXME: the location information here isn't great. 13100b29b25525c4612c77b1b3c8abcc40685d0aa33fcristy Diag(SS.getRange().getBegin(), 13113ed852eea50f9d4cd633efb8c2b054b8e33c253cristy diag::err_template_spec_needs_template_parameters) 13123ed852eea50f9d4cd633efb8c2b054b8e33c253cristy << TemplateId 13133ed852eea50f9d4cd633efb8c2b054b8e33c253cristy << SS.getRange(); 13143ed852eea50f9d4cd633efb8c2b054b8e33c253cristy } else { 13153ed852eea50f9d4cd633efb8c2b054b8e33c253cristy Diag(SS.getRange().getBegin(), diag::err_template_spec_needs_header) 13163ed852eea50f9d4cd633efb8c2b054b8e33c253cristy << SS.getRange() 1317eaedf06777741da32408da72c1e512975c600c48cristy << FixItHint::CreateInsertion(FirstTemplateLoc, "template<> "); 13183ed852eea50f9d4cd633efb8c2b054b8e33c253cristy IsExplicitSpecialization = true; 1319eaedf06777741da32408da72c1e512975c600c48cristy } 13203ed852eea50f9d4cd633efb8c2b054b8e33c253cristy return 0; 13213ed852eea50f9d4cd633efb8c2b054b8e33c253cristy } 13223ed852eea50f9d4cd633efb8c2b054b8e33c253cristy 13233ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // Check the template parameter list against its corresponding template-id. 13243ed852eea50f9d4cd633efb8c2b054b8e33c253cristy if (DependentTemplateId) { 13253ed852eea50f9d4cd633efb8c2b054b8e33c253cristy TemplateParameterList *ExpectedTemplateParams = 0; 13263ed852eea50f9d4cd633efb8c2b054b8e33c253cristy 13273ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // Are there cases in (e.g.) friends where this won't match? 1328bb50337b2a8a16ca7e903cc04ab195ff0fd47ae6cristy if (const InjectedClassNameType *Injected 13293ed852eea50f9d4cd633efb8c2b054b8e33c253cristy = TemplateId->getAs<InjectedClassNameType>()) { 13303ed852eea50f9d4cd633efb8c2b054b8e33c253cristy CXXRecordDecl *Record = Injected->getDecl(); 13313ed852eea50f9d4cd633efb8c2b054b8e33c253cristy if (ClassTemplatePartialSpecializationDecl *Partial = 13323ed852eea50f9d4cd633efb8c2b054b8e33c253cristy dyn_cast<ClassTemplatePartialSpecializationDecl>(Record)) 13333ed852eea50f9d4cd633efb8c2b054b8e33c253cristy ExpectedTemplateParams = Partial->getTemplateParameters(); 1334bb50337b2a8a16ca7e903cc04ab195ff0fd47ae6cristy else 13353ed852eea50f9d4cd633efb8c2b054b8e33c253cristy ExpectedTemplateParams = Record->getDescribedClassTemplate() 13363ed852eea50f9d4cd633efb8c2b054b8e33c253cristy ->getTemplateParameters(); 13373ed852eea50f9d4cd633efb8c2b054b8e33c253cristy } 13383ed852eea50f9d4cd633efb8c2b054b8e33c253cristy 13393ed852eea50f9d4cd633efb8c2b054b8e33c253cristy if (ExpectedTemplateParams) 13403ed852eea50f9d4cd633efb8c2b054b8e33c253cristy TemplateParameterListsAreEqual(ParamLists[Idx], 13413ed852eea50f9d4cd633efb8c2b054b8e33c253cristy ExpectedTemplateParams, 13423ed852eea50f9d4cd633efb8c2b054b8e33c253cristy true, TPL_TemplateMatch); 13433ed852eea50f9d4cd633efb8c2b054b8e33c253cristy 13443ed852eea50f9d4cd633efb8c2b054b8e33c253cristy CheckTemplateParameterList(ParamLists[Idx], 0, TPC_ClassTemplateMember); 13453ed852eea50f9d4cd633efb8c2b054b8e33c253cristy } else if (ParamLists[Idx]->size() > 0) 13463ed852eea50f9d4cd633efb8c2b054b8e33c253cristy Diag(ParamLists[Idx]->getTemplateLoc(), 13473ed852eea50f9d4cd633efb8c2b054b8e33c253cristy diag::err_template_param_list_matches_nontemplate) 13483ed852eea50f9d4cd633efb8c2b054b8e33c253cristy << TemplateId 13493ed852eea50f9d4cd633efb8c2b054b8e33c253cristy << ParamLists[Idx]->getSourceRange(); 13503ed852eea50f9d4cd633efb8c2b054b8e33c253cristy else 13513ed852eea50f9d4cd633efb8c2b054b8e33c253cristy IsExplicitSpecialization = true; 13523ed852eea50f9d4cd633efb8c2b054b8e33c253cristy } 13533ed852eea50f9d4cd633efb8c2b054b8e33c253cristy 13543ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // If there were at least as many template-ids as there were template 13553ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // parameter lists, then there are no template parameter lists remaining for 13563ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // the declaration itself. 13573ed852eea50f9d4cd633efb8c2b054b8e33c253cristy if (Idx >= NumParamLists) 13583ed852eea50f9d4cd633efb8c2b054b8e33c253cristy return 0; 13593ed852eea50f9d4cd633efb8c2b054b8e33c253cristy 13603ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // If there were too many template parameter lists, complain about that now. 13613ed852eea50f9d4cd633efb8c2b054b8e33c253cristy if (Idx != NumParamLists - 1) { 13623ed852eea50f9d4cd633efb8c2b054b8e33c253cristy while (Idx < NumParamLists - 1) { 13633ed852eea50f9d4cd633efb8c2b054b8e33c253cristy bool isExplicitSpecHeader = ParamLists[Idx]->size() == 0; 13643ed852eea50f9d4cd633efb8c2b054b8e33c253cristy Diag(ParamLists[Idx]->getTemplateLoc(), 13653ed852eea50f9d4cd633efb8c2b054b8e33c253cristy isExplicitSpecHeader? diag::warn_template_spec_extra_headers 13663ed852eea50f9d4cd633efb8c2b054b8e33c253cristy : diag::err_template_spec_extra_headers) 13673ed852eea50f9d4cd633efb8c2b054b8e33c253cristy << SourceRange(ParamLists[Idx]->getTemplateLoc(), 13683ed852eea50f9d4cd633efb8c2b054b8e33c253cristy ParamLists[Idx]->getRAngleLoc()); 13693ed852eea50f9d4cd633efb8c2b054b8e33c253cristy 13703ed852eea50f9d4cd633efb8c2b054b8e33c253cristy if (isExplicitSpecHeader && !ExplicitSpecializationsInSpecifier.empty()) { 13713ed852eea50f9d4cd633efb8c2b054b8e33c253cristy Diag(ExplicitSpecializationsInSpecifier.back()->getLocation(), 13723ed852eea50f9d4cd633efb8c2b054b8e33c253cristy diag::note_explicit_template_spec_does_not_need_header) 13733ed852eea50f9d4cd633efb8c2b054b8e33c253cristy << ExplicitSpecializationsInSpecifier.back(); 13743ed852eea50f9d4cd633efb8c2b054b8e33c253cristy ExplicitSpecializationsInSpecifier.pop_back(); 13753ed852eea50f9d4cd633efb8c2b054b8e33c253cristy } 13763ed852eea50f9d4cd633efb8c2b054b8e33c253cristy 13773ed852eea50f9d4cd633efb8c2b054b8e33c253cristy ++Idx; 13783ed852eea50f9d4cd633efb8c2b054b8e33c253cristy } 13793ed852eea50f9d4cd633efb8c2b054b8e33c253cristy } 13803ed852eea50f9d4cd633efb8c2b054b8e33c253cristy 13813ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // Return the last template parameter list, which corresponds to the 13823ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // entity being declared. 13833ed852eea50f9d4cd633efb8c2b054b8e33c253cristy return ParamLists[NumParamLists - 1]; 13843ed852eea50f9d4cd633efb8c2b054b8e33c253cristy} 13853ed852eea50f9d4cd633efb8c2b054b8e33c253cristy 13863ed852eea50f9d4cd633efb8c2b054b8e33c253cristyQualType Sema::CheckTemplateIdType(TemplateName Name, 13873ed852eea50f9d4cd633efb8c2b054b8e33c253cristy SourceLocation TemplateLoc, 13883ed852eea50f9d4cd633efb8c2b054b8e33c253cristy const TemplateArgumentListInfo &TemplateArgs) { 13893ed852eea50f9d4cd633efb8c2b054b8e33c253cristy TemplateDecl *Template = Name.getAsTemplateDecl(); 13903ed852eea50f9d4cd633efb8c2b054b8e33c253cristy if (!Template) { 13913ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // The template name does not resolve to a template, so we just 13923ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // build a dependent template-id type. 13933ed852eea50f9d4cd633efb8c2b054b8e33c253cristy return Context.getTemplateSpecializationType(Name, TemplateArgs); 13943ed852eea50f9d4cd633efb8c2b054b8e33c253cristy } 13953ed852eea50f9d4cd633efb8c2b054b8e33c253cristy 1396bb50337b2a8a16ca7e903cc04ab195ff0fd47ae6cristy // Check that the template argument list is well-formed for this 13973ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // template. 13983ed852eea50f9d4cd633efb8c2b054b8e33c253cristy TemplateArgumentListBuilder Converted(Template->getTemplateParameters(), 13993ed852eea50f9d4cd633efb8c2b054b8e33c253cristy TemplateArgs.size()); 14003ed852eea50f9d4cd633efb8c2b054b8e33c253cristy if (CheckTemplateArgumentList(Template, TemplateLoc, TemplateArgs, 1401da16f16767eb31921af855f17bda465fffc4e000cristy false, Converted)) 14023ed852eea50f9d4cd633efb8c2b054b8e33c253cristy return QualType(); 14033ed852eea50f9d4cd633efb8c2b054b8e33c253cristy 14043ed852eea50f9d4cd633efb8c2b054b8e33c253cristy assert((Converted.structuredSize() == 14053ed852eea50f9d4cd633efb8c2b054b8e33c253cristy Template->getTemplateParameters()->size()) && 14063ed852eea50f9d4cd633efb8c2b054b8e33c253cristy "Converted template argument list is too short!"); 14073ed852eea50f9d4cd633efb8c2b054b8e33c253cristy 14083ed852eea50f9d4cd633efb8c2b054b8e33c253cristy QualType CanonType; 14093ed852eea50f9d4cd633efb8c2b054b8e33c253cristy 14103ed852eea50f9d4cd633efb8c2b054b8e33c253cristy if (Name.isDependent() || 14113ed852eea50f9d4cd633efb8c2b054b8e33c253cristy TemplateSpecializationType::anyDependentTemplateArguments( 14123ed852eea50f9d4cd633efb8c2b054b8e33c253cristy TemplateArgs)) { 14133ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // This class template specialization is a dependent 14143ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // type. Therefore, its canonical type is another class template 14153ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // specialization type that contains all of the converted 14163ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // arguments in canonical form. This ensures that, e.g., A<T> and 14173ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // A<T, T> have identical types when A is declared as: 14183ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // 14193ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // template<typename T, typename U = T> struct A; 14203ed852eea50f9d4cd633efb8c2b054b8e33c253cristy TemplateName CanonName = Context.getCanonicalTemplateName(Name); 1421bb50337b2a8a16ca7e903cc04ab195ff0fd47ae6cristy CanonType = Context.getTemplateSpecializationType(CanonName, 14223ed852eea50f9d4cd633efb8c2b054b8e33c253cristy Converted.getFlatArguments(), 14233ed852eea50f9d4cd633efb8c2b054b8e33c253cristy Converted.flatSize()); 14243ed852eea50f9d4cd633efb8c2b054b8e33c253cristy 14253ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // FIXME: CanonType is not actually the canonical type, and unfortunately 14263ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // it is a TemplateSpecializationType that we will never use again. 14273ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // In the future, we need to teach getTemplateSpecializationType to only 14283ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // build the canonical type and return that to us. 14293ed852eea50f9d4cd633efb8c2b054b8e33c253cristy CanonType = Context.getCanonicalType(CanonType); 14303ed852eea50f9d4cd633efb8c2b054b8e33c253cristy 14313ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // This might work out to be a current instantiation, in which 14323ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // case the canonical type needs to be the InjectedClassNameType. 14333ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // 14343ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // TODO: in theory this could be a simple hashtable lookup; most 14353ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // changes to CurContext don't change the set of current 14363ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // instantiations. 14373ed852eea50f9d4cd633efb8c2b054b8e33c253cristy if (isa<ClassTemplateDecl>(Template)) { 14383ed852eea50f9d4cd633efb8c2b054b8e33c253cristy for (DeclContext *Ctx = CurContext; Ctx; Ctx = Ctx->getLookupParent()) { 14393ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // If we get out to a namespace, we're done. 14403ed852eea50f9d4cd633efb8c2b054b8e33c253cristy if (Ctx->isFileContext()) break; 14413ed852eea50f9d4cd633efb8c2b054b8e33c253cristy 14423ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // If this isn't a record, keep looking. 14433ed852eea50f9d4cd633efb8c2b054b8e33c253cristy CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Ctx); 1444bb50337b2a8a16ca7e903cc04ab195ff0fd47ae6cristy if (!Record) continue; 14453ed852eea50f9d4cd633efb8c2b054b8e33c253cristy 14463ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // Look for one of the two cases with InjectedClassNameTypes 14473ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // and check whether it's the same template. 14483ed852eea50f9d4cd633efb8c2b054b8e33c253cristy if (!isa<ClassTemplatePartialSpecializationDecl>(Record) && 14493ed852eea50f9d4cd633efb8c2b054b8e33c253cristy !Record->getDescribedClassTemplate()) 14503ed852eea50f9d4cd633efb8c2b054b8e33c253cristy continue; 14513ed852eea50f9d4cd633efb8c2b054b8e33c253cristy 14523ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // Fetch the injected class name type and check whether its 14533ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // injected type is equal to the type we just built. 14543ed852eea50f9d4cd633efb8c2b054b8e33c253cristy QualType ICNT = Context.getTypeDeclType(Record); 14553ed852eea50f9d4cd633efb8c2b054b8e33c253cristy QualType Injected = cast<InjectedClassNameType>(ICNT) 14563ed852eea50f9d4cd633efb8c2b054b8e33c253cristy ->getInjectedSpecializationType(); 14573ed852eea50f9d4cd633efb8c2b054b8e33c253cristy 14583ed852eea50f9d4cd633efb8c2b054b8e33c253cristy if (CanonType != Injected->getCanonicalTypeInternal()) 14593ed852eea50f9d4cd633efb8c2b054b8e33c253cristy continue; 14603ed852eea50f9d4cd633efb8c2b054b8e33c253cristy 14613ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // If so, the canonical type of this TST is the injected 14623ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // class name type of the record we just found. 14633ed852eea50f9d4cd633efb8c2b054b8e33c253cristy assert(ICNT.isCanonical()); 14643ed852eea50f9d4cd633efb8c2b054b8e33c253cristy CanonType = ICNT; 14653ed852eea50f9d4cd633efb8c2b054b8e33c253cristy break; 14663ed852eea50f9d4cd633efb8c2b054b8e33c253cristy } 14673ed852eea50f9d4cd633efb8c2b054b8e33c253cristy } 14683ed852eea50f9d4cd633efb8c2b054b8e33c253cristy } else if (ClassTemplateDecl *ClassTemplate 14693ed852eea50f9d4cd633efb8c2b054b8e33c253cristy = dyn_cast<ClassTemplateDecl>(Template)) { 14703ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // Find the class template specialization declaration that 14713ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // corresponds to these arguments. 1472bb50337b2a8a16ca7e903cc04ab195ff0fd47ae6cristy llvm::FoldingSetNodeID ID; 14733ed852eea50f9d4cd633efb8c2b054b8e33c253cristy ClassTemplateSpecializationDecl::Profile(ID, 14743ed852eea50f9d4cd633efb8c2b054b8e33c253cristy Converted.getFlatArguments(), 1475bb50337b2a8a16ca7e903cc04ab195ff0fd47ae6cristy Converted.flatSize(), 14763ed852eea50f9d4cd633efb8c2b054b8e33c253cristy Context); 14773ed852eea50f9d4cd633efb8c2b054b8e33c253cristy void *InsertPos = 0; 14783ed852eea50f9d4cd633efb8c2b054b8e33c253cristy ClassTemplateSpecializationDecl *Decl 14793ed852eea50f9d4cd633efb8c2b054b8e33c253cristy = ClassTemplate->getSpecializations().FindNodeOrInsertPos(ID, InsertPos); 14803ed852eea50f9d4cd633efb8c2b054b8e33c253cristy if (!Decl) { 14813ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // This is the first time we have referenced this class template 14823ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // specialization. Create the canonical declaration and add it to 14833ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // the set of specializations. 14843ed852eea50f9d4cd633efb8c2b054b8e33c253cristy Decl = ClassTemplateSpecializationDecl::Create(Context, 14853ed852eea50f9d4cd633efb8c2b054b8e33c253cristy ClassTemplate->getTemplatedDecl()->getTagKind(), 14863ed852eea50f9d4cd633efb8c2b054b8e33c253cristy ClassTemplate->getDeclContext(), 14873ed852eea50f9d4cd633efb8c2b054b8e33c253cristy ClassTemplate->getLocation(), 14883ed852eea50f9d4cd633efb8c2b054b8e33c253cristy ClassTemplate, 14893ed852eea50f9d4cd633efb8c2b054b8e33c253cristy Converted, 0); 14903ed852eea50f9d4cd633efb8c2b054b8e33c253cristy ClassTemplate->getSpecializations().InsertNode(Decl, InsertPos); 14913ed852eea50f9d4cd633efb8c2b054b8e33c253cristy Decl->setLexicalDeclContext(CurContext); 14923ed852eea50f9d4cd633efb8c2b054b8e33c253cristy } 14933ed852eea50f9d4cd633efb8c2b054b8e33c253cristy 14943ed852eea50f9d4cd633efb8c2b054b8e33c253cristy CanonType = Context.getTypeDeclType(Decl); 14953ed852eea50f9d4cd633efb8c2b054b8e33c253cristy assert(isa<RecordType>(CanonType) && 14963ed852eea50f9d4cd633efb8c2b054b8e33c253cristy "type of non-dependent specialization is not a RecordType"); 14973ed852eea50f9d4cd633efb8c2b054b8e33c253cristy } 14983ed852eea50f9d4cd633efb8c2b054b8e33c253cristy 14993ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // Build the fully-sugared type for this class template 15003ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // specialization, which refers back to the class template 15013ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // specialization we created or found. 15023ed852eea50f9d4cd633efb8c2b054b8e33c253cristy return Context.getTemplateSpecializationType(Name, TemplateArgs, CanonType); 15033ed852eea50f9d4cd633efb8c2b054b8e33c253cristy} 15043ed852eea50f9d4cd633efb8c2b054b8e33c253cristy 15053ed852eea50f9d4cd633efb8c2b054b8e33c253cristyAction::TypeResult 15063ed852eea50f9d4cd633efb8c2b054b8e33c253cristySema::ActOnTemplateIdType(TemplateTy TemplateD, SourceLocation TemplateLoc, 15073ed852eea50f9d4cd633efb8c2b054b8e33c253cristy SourceLocation LAngleLoc, 15083ed852eea50f9d4cd633efb8c2b054b8e33c253cristy ASTTemplateArgsPtr TemplateArgsIn, 15093ed852eea50f9d4cd633efb8c2b054b8e33c253cristy SourceLocation RAngleLoc) { 15103ed852eea50f9d4cd633efb8c2b054b8e33c253cristy TemplateName Template = TemplateD.getAsVal<TemplateName>(); 15113ed852eea50f9d4cd633efb8c2b054b8e33c253cristy 15123ed852eea50f9d4cd633efb8c2b054b8e33c253cristy // Translate the parser's template argument list in our AST format. 1513 TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); 1514 translateTemplateArguments(TemplateArgsIn, TemplateArgs); 1515 1516 QualType Result = CheckTemplateIdType(Template, TemplateLoc, TemplateArgs); 1517 TemplateArgsIn.release(); 1518 1519 if (Result.isNull()) 1520 return true; 1521 1522 TypeSourceInfo *DI = Context.CreateTypeSourceInfo(Result); 1523 TemplateSpecializationTypeLoc TL 1524 = cast<TemplateSpecializationTypeLoc>(DI->getTypeLoc()); 1525 TL.setTemplateNameLoc(TemplateLoc); 1526 TL.setLAngleLoc(LAngleLoc); 1527 TL.setRAngleLoc(RAngleLoc); 1528 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) 1529 TL.setArgLocInfo(i, TemplateArgs[i].getLocInfo()); 1530 1531 return CreateLocInfoType(Result, DI).getAsOpaquePtr(); 1532} 1533 1534Sema::TypeResult Sema::ActOnTagTemplateIdType(TypeResult TypeResult, 1535 TagUseKind TUK, 1536 DeclSpec::TST TagSpec, 1537 SourceLocation TagLoc) { 1538 if (TypeResult.isInvalid()) 1539 return Sema::TypeResult(); 1540 1541 // FIXME: preserve source info, ideally without copying the DI. 1542 TypeSourceInfo *DI; 1543 QualType Type = GetTypeFromParser(TypeResult.get(), &DI); 1544 1545 // Verify the tag specifier. 1546 TagTypeKind TagKind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); 1547 1548 if (const RecordType *RT = Type->getAs<RecordType>()) { 1549 RecordDecl *D = RT->getDecl(); 1550 1551 IdentifierInfo *Id = D->getIdentifier(); 1552 assert(Id && "templated class must have an identifier"); 1553 1554 if (!isAcceptableTagRedeclaration(D, TagKind, TagLoc, *Id)) { 1555 Diag(TagLoc, diag::err_use_with_wrong_tag) 1556 << Type 1557 << FixItHint::CreateReplacement(SourceRange(TagLoc), D->getKindName()); 1558 Diag(D->getLocation(), diag::note_previous_use); 1559 } 1560 } 1561 1562 ElaboratedTypeKeyword Keyword 1563 = TypeWithKeyword::getKeywordForTagTypeKind(TagKind); 1564 QualType ElabType = Context.getElaboratedType(Keyword, /*NNS=*/0, Type); 1565 1566 return ElabType.getAsOpaquePtr(); 1567} 1568 1569Sema::OwningExprResult Sema::BuildTemplateIdExpr(const CXXScopeSpec &SS, 1570 LookupResult &R, 1571 bool RequiresADL, 1572 const TemplateArgumentListInfo &TemplateArgs) { 1573 // FIXME: Can we do any checking at this point? I guess we could check the 1574 // template arguments that we have against the template name, if the template 1575 // name refers to a single template. That's not a terribly common case, 1576 // though. 1577 1578 // These should be filtered out by our callers. 1579 assert(!R.empty() && "empty lookup results when building templateid"); 1580 assert(!R.isAmbiguous() && "ambiguous lookup when building templateid"); 1581 1582 NestedNameSpecifier *Qualifier = 0; 1583 SourceRange QualifierRange; 1584 if (SS.isSet()) { 1585 Qualifier = static_cast<NestedNameSpecifier*>(SS.getScopeRep()); 1586 QualifierRange = SS.getRange(); 1587 } 1588 1589 // We don't want lookup warnings at this point. 1590 R.suppressDiagnostics(); 1591 1592 bool Dependent 1593 = UnresolvedLookupExpr::ComputeDependence(R.begin(), R.end(), 1594 &TemplateArgs); 1595 UnresolvedLookupExpr *ULE 1596 = UnresolvedLookupExpr::Create(Context, Dependent, R.getNamingClass(), 1597 Qualifier, QualifierRange, 1598 R.getLookupName(), R.getNameLoc(), 1599 RequiresADL, TemplateArgs, 1600 R.begin(), R.end()); 1601 1602 return Owned(ULE); 1603} 1604 1605// We actually only call this from template instantiation. 1606Sema::OwningExprResult 1607Sema::BuildQualifiedTemplateIdExpr(CXXScopeSpec &SS, 1608 DeclarationName Name, 1609 SourceLocation NameLoc, 1610 const TemplateArgumentListInfo &TemplateArgs) { 1611 DeclContext *DC; 1612 if (!(DC = computeDeclContext(SS, false)) || 1613 DC->isDependentContext() || 1614 RequireCompleteDeclContext(SS, DC)) 1615 return BuildDependentDeclRefExpr(SS, Name, NameLoc, &TemplateArgs); 1616 1617 bool MemberOfUnknownSpecialization; 1618 LookupResult R(*this, Name, NameLoc, LookupOrdinaryName); 1619 LookupTemplateName(R, (Scope*) 0, SS, QualType(), /*Entering*/ false, 1620 MemberOfUnknownSpecialization); 1621 1622 if (R.isAmbiguous()) 1623 return ExprError(); 1624 1625 if (R.empty()) { 1626 Diag(NameLoc, diag::err_template_kw_refers_to_non_template) 1627 << Name << SS.getRange(); 1628 return ExprError(); 1629 } 1630 1631 if (ClassTemplateDecl *Temp = R.getAsSingle<ClassTemplateDecl>()) { 1632 Diag(NameLoc, diag::err_template_kw_refers_to_class_template) 1633 << (NestedNameSpecifier*) SS.getScopeRep() << Name << SS.getRange(); 1634 Diag(Temp->getLocation(), diag::note_referenced_class_template); 1635 return ExprError(); 1636 } 1637 1638 return BuildTemplateIdExpr(SS, R, /* ADL */ false, TemplateArgs); 1639} 1640 1641/// \brief Form a dependent template name. 1642/// 1643/// This action forms a dependent template name given the template 1644/// name and its (presumably dependent) scope specifier. For 1645/// example, given "MetaFun::template apply", the scope specifier \p 1646/// SS will be "MetaFun::", \p TemplateKWLoc contains the location 1647/// of the "template" keyword, and "apply" is the \p Name. 1648TemplateNameKind Sema::ActOnDependentTemplateName(Scope *S, 1649 SourceLocation TemplateKWLoc, 1650 CXXScopeSpec &SS, 1651 UnqualifiedId &Name, 1652 TypeTy *ObjectType, 1653 bool EnteringContext, 1654 TemplateTy &Result) { 1655 if (TemplateKWLoc.isValid() && S && !S->getTemplateParamParent() && 1656 !getLangOptions().CPlusPlus0x) 1657 Diag(TemplateKWLoc, diag::ext_template_outside_of_template) 1658 << FixItHint::CreateRemoval(TemplateKWLoc); 1659 1660 DeclContext *LookupCtx = 0; 1661 if (SS.isSet()) 1662 LookupCtx = computeDeclContext(SS, EnteringContext); 1663 if (!LookupCtx && ObjectType) 1664 LookupCtx = computeDeclContext(QualType::getFromOpaquePtr(ObjectType)); 1665 if (LookupCtx) { 1666 // C++0x [temp.names]p5: 1667 // If a name prefixed by the keyword template is not the name of 1668 // a template, the program is ill-formed. [Note: the keyword 1669 // template may not be applied to non-template members of class 1670 // templates. -end note ] [ Note: as is the case with the 1671 // typename prefix, the template prefix is allowed in cases 1672 // where it is not strictly necessary; i.e., when the 1673 // nested-name-specifier or the expression on the left of the -> 1674 // or . is not dependent on a template-parameter, or the use 1675 // does not appear in the scope of a template. -end note] 1676 // 1677 // Note: C++03 was more strict here, because it banned the use of 1678 // the "template" keyword prior to a template-name that was not a 1679 // dependent name. C++ DR468 relaxed this requirement (the 1680 // "template" keyword is now permitted). We follow the C++0x 1681 // rules, even in C++03 mode with a warning, retroactively applying the DR. 1682 bool MemberOfUnknownSpecialization; 1683 TemplateNameKind TNK = isTemplateName(0, SS, Name, ObjectType, 1684 EnteringContext, Result, 1685 MemberOfUnknownSpecialization); 1686 if (TNK == TNK_Non_template && LookupCtx->isDependentContext() && 1687 isa<CXXRecordDecl>(LookupCtx) && 1688 cast<CXXRecordDecl>(LookupCtx)->hasAnyDependentBases()) { 1689 // This is a dependent template. Handle it below. 1690 } else if (TNK == TNK_Non_template) { 1691 Diag(Name.getSourceRange().getBegin(), 1692 diag::err_template_kw_refers_to_non_template) 1693 << GetNameFromUnqualifiedId(Name) 1694 << Name.getSourceRange() 1695 << TemplateKWLoc; 1696 return TNK_Non_template; 1697 } else { 1698 // We found something; return it. 1699 return TNK; 1700 } 1701 } 1702 1703 NestedNameSpecifier *Qualifier 1704 = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); 1705 1706 switch (Name.getKind()) { 1707 case UnqualifiedId::IK_Identifier: 1708 Result = TemplateTy::make(Context.getDependentTemplateName(Qualifier, 1709 Name.Identifier)); 1710 return TNK_Dependent_template_name; 1711 1712 case UnqualifiedId::IK_OperatorFunctionId: 1713 Result = TemplateTy::make(Context.getDependentTemplateName(Qualifier, 1714 Name.OperatorFunctionId.Operator)); 1715 return TNK_Dependent_template_name; 1716 1717 case UnqualifiedId::IK_LiteralOperatorId: 1718 assert(false && "We don't support these; Parse shouldn't have allowed propagation"); 1719 1720 default: 1721 break; 1722 } 1723 1724 Diag(Name.getSourceRange().getBegin(), 1725 diag::err_template_kw_refers_to_non_template) 1726 << GetNameFromUnqualifiedId(Name) 1727 << Name.getSourceRange() 1728 << TemplateKWLoc; 1729 return TNK_Non_template; 1730} 1731 1732bool Sema::CheckTemplateTypeArgument(TemplateTypeParmDecl *Param, 1733 const TemplateArgumentLoc &AL, 1734 TemplateArgumentListBuilder &Converted) { 1735 const TemplateArgument &Arg = AL.getArgument(); 1736 1737 // Check template type parameter. 1738 switch(Arg.getKind()) { 1739 case TemplateArgument::Type: 1740 // C++ [temp.arg.type]p1: 1741 // A template-argument for a template-parameter which is a 1742 // type shall be a type-id. 1743 break; 1744 case TemplateArgument::Template: { 1745 // We have a template type parameter but the template argument 1746 // is a template without any arguments. 1747 SourceRange SR = AL.getSourceRange(); 1748 TemplateName Name = Arg.getAsTemplate(); 1749 Diag(SR.getBegin(), diag::err_template_missing_args) 1750 << Name << SR; 1751 if (TemplateDecl *Decl = Name.getAsTemplateDecl()) 1752 Diag(Decl->getLocation(), diag::note_template_decl_here); 1753 1754 return true; 1755 } 1756 default: { 1757 // We have a template type parameter but the template argument 1758 // is not a type. 1759 SourceRange SR = AL.getSourceRange(); 1760 Diag(SR.getBegin(), diag::err_template_arg_must_be_type) << SR; 1761 Diag(Param->getLocation(), diag::note_template_param_here); 1762 1763 return true; 1764 } 1765 } 1766 1767 if (CheckTemplateArgument(Param, AL.getTypeSourceInfo())) 1768 return true; 1769 1770 // Add the converted template type argument. 1771 Converted.Append( 1772 TemplateArgument(Context.getCanonicalType(Arg.getAsType()))); 1773 return false; 1774} 1775 1776/// \brief Substitute template arguments into the default template argument for 1777/// the given template type parameter. 1778/// 1779/// \param SemaRef the semantic analysis object for which we are performing 1780/// the substitution. 1781/// 1782/// \param Template the template that we are synthesizing template arguments 1783/// for. 1784/// 1785/// \param TemplateLoc the location of the template name that started the 1786/// template-id we are checking. 1787/// 1788/// \param RAngleLoc the location of the right angle bracket ('>') that 1789/// terminates the template-id. 1790/// 1791/// \param Param the template template parameter whose default we are 1792/// substituting into. 1793/// 1794/// \param Converted the list of template arguments provided for template 1795/// parameters that precede \p Param in the template parameter list. 1796/// 1797/// \returns the substituted template argument, or NULL if an error occurred. 1798static TypeSourceInfo * 1799SubstDefaultTemplateArgument(Sema &SemaRef, 1800 TemplateDecl *Template, 1801 SourceLocation TemplateLoc, 1802 SourceLocation RAngleLoc, 1803 TemplateTypeParmDecl *Param, 1804 TemplateArgumentListBuilder &Converted) { 1805 TypeSourceInfo *ArgType = Param->getDefaultArgumentInfo(); 1806 1807 // If the argument type is dependent, instantiate it now based 1808 // on the previously-computed template arguments. 1809 if (ArgType->getType()->isDependentType()) { 1810 TemplateArgumentList TemplateArgs(SemaRef.Context, Converted, 1811 /*TakeArgs=*/false); 1812 1813 MultiLevelTemplateArgumentList AllTemplateArgs 1814 = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs); 1815 1816 Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc, 1817 Template, Converted.getFlatArguments(), 1818 Converted.flatSize(), 1819 SourceRange(TemplateLoc, RAngleLoc)); 1820 1821 ArgType = SemaRef.SubstType(ArgType, AllTemplateArgs, 1822 Param->getDefaultArgumentLoc(), 1823 Param->getDeclName()); 1824 } 1825 1826 return ArgType; 1827} 1828 1829/// \brief Substitute template arguments into the default template argument for 1830/// the given non-type template parameter. 1831/// 1832/// \param SemaRef the semantic analysis object for which we are performing 1833/// the substitution. 1834/// 1835/// \param Template the template that we are synthesizing template arguments 1836/// for. 1837/// 1838/// \param TemplateLoc the location of the template name that started the 1839/// template-id we are checking. 1840/// 1841/// \param RAngleLoc the location of the right angle bracket ('>') that 1842/// terminates the template-id. 1843/// 1844/// \param Param the non-type template parameter whose default we are 1845/// substituting into. 1846/// 1847/// \param Converted the list of template arguments provided for template 1848/// parameters that precede \p Param in the template parameter list. 1849/// 1850/// \returns the substituted template argument, or NULL if an error occurred. 1851static Sema::OwningExprResult 1852SubstDefaultTemplateArgument(Sema &SemaRef, 1853 TemplateDecl *Template, 1854 SourceLocation TemplateLoc, 1855 SourceLocation RAngleLoc, 1856 NonTypeTemplateParmDecl *Param, 1857 TemplateArgumentListBuilder &Converted) { 1858 TemplateArgumentList TemplateArgs(SemaRef.Context, Converted, 1859 /*TakeArgs=*/false); 1860 1861 MultiLevelTemplateArgumentList AllTemplateArgs 1862 = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs); 1863 1864 Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc, 1865 Template, Converted.getFlatArguments(), 1866 Converted.flatSize(), 1867 SourceRange(TemplateLoc, RAngleLoc)); 1868 1869 return SemaRef.SubstExpr(Param->getDefaultArgument(), AllTemplateArgs); 1870} 1871 1872/// \brief Substitute template arguments into the default template argument for 1873/// the given template template parameter. 1874/// 1875/// \param SemaRef the semantic analysis object for which we are performing 1876/// the substitution. 1877/// 1878/// \param Template the template that we are synthesizing template arguments 1879/// for. 1880/// 1881/// \param TemplateLoc the location of the template name that started the 1882/// template-id we are checking. 1883/// 1884/// \param RAngleLoc the location of the right angle bracket ('>') that 1885/// terminates the template-id. 1886/// 1887/// \param Param the template template parameter whose default we are 1888/// substituting into. 1889/// 1890/// \param Converted the list of template arguments provided for template 1891/// parameters that precede \p Param in the template parameter list. 1892/// 1893/// \returns the substituted template argument, or NULL if an error occurred. 1894static TemplateName 1895SubstDefaultTemplateArgument(Sema &SemaRef, 1896 TemplateDecl *Template, 1897 SourceLocation TemplateLoc, 1898 SourceLocation RAngleLoc, 1899 TemplateTemplateParmDecl *Param, 1900 TemplateArgumentListBuilder &Converted) { 1901 TemplateArgumentList TemplateArgs(SemaRef.Context, Converted, 1902 /*TakeArgs=*/false); 1903 1904 MultiLevelTemplateArgumentList AllTemplateArgs 1905 = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs); 1906 1907 Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc, 1908 Template, Converted.getFlatArguments(), 1909 Converted.flatSize(), 1910 SourceRange(TemplateLoc, RAngleLoc)); 1911 1912 return SemaRef.SubstTemplateName( 1913 Param->getDefaultArgument().getArgument().getAsTemplate(), 1914 Param->getDefaultArgument().getTemplateNameLoc(), 1915 AllTemplateArgs); 1916} 1917 1918/// \brief If the given template parameter has a default template 1919/// argument, substitute into that default template argument and 1920/// return the corresponding template argument. 1921TemplateArgumentLoc 1922Sema::SubstDefaultTemplateArgumentIfAvailable(TemplateDecl *Template, 1923 SourceLocation TemplateLoc, 1924 SourceLocation RAngleLoc, 1925 Decl *Param, 1926 TemplateArgumentListBuilder &Converted) { 1927 if (TemplateTypeParmDecl *TypeParm = dyn_cast<TemplateTypeParmDecl>(Param)) { 1928 if (!TypeParm->hasDefaultArgument()) 1929 return TemplateArgumentLoc(); 1930 1931 TypeSourceInfo *DI = SubstDefaultTemplateArgument(*this, Template, 1932 TemplateLoc, 1933 RAngleLoc, 1934 TypeParm, 1935 Converted); 1936 if (DI) 1937 return TemplateArgumentLoc(TemplateArgument(DI->getType()), DI); 1938 1939 return TemplateArgumentLoc(); 1940 } 1941 1942 if (NonTypeTemplateParmDecl *NonTypeParm 1943 = dyn_cast<NonTypeTemplateParmDecl>(Param)) { 1944 if (!NonTypeParm->hasDefaultArgument()) 1945 return TemplateArgumentLoc(); 1946 1947 OwningExprResult Arg = SubstDefaultTemplateArgument(*this, Template, 1948 TemplateLoc, 1949 RAngleLoc, 1950 NonTypeParm, 1951 Converted); 1952 if (Arg.isInvalid()) 1953 return TemplateArgumentLoc(); 1954 1955 Expr *ArgE = Arg.takeAs<Expr>(); 1956 return TemplateArgumentLoc(TemplateArgument(ArgE), ArgE); 1957 } 1958 1959 TemplateTemplateParmDecl *TempTempParm 1960 = cast<TemplateTemplateParmDecl>(Param); 1961 if (!TempTempParm->hasDefaultArgument()) 1962 return TemplateArgumentLoc(); 1963 1964 TemplateName TName = SubstDefaultTemplateArgument(*this, Template, 1965 TemplateLoc, 1966 RAngleLoc, 1967 TempTempParm, 1968 Converted); 1969 if (TName.isNull()) 1970 return TemplateArgumentLoc(); 1971 1972 return TemplateArgumentLoc(TemplateArgument(TName), 1973 TempTempParm->getDefaultArgument().getTemplateQualifierRange(), 1974 TempTempParm->getDefaultArgument().getTemplateNameLoc()); 1975} 1976 1977/// \brief Check that the given template argument corresponds to the given 1978/// template parameter. 1979bool Sema::CheckTemplateArgument(NamedDecl *Param, 1980 const TemplateArgumentLoc &Arg, 1981 TemplateDecl *Template, 1982 SourceLocation TemplateLoc, 1983 SourceLocation RAngleLoc, 1984 TemplateArgumentListBuilder &Converted, 1985 CheckTemplateArgumentKind CTAK) { 1986 // Check template type parameters. 1987 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) 1988 return CheckTemplateTypeArgument(TTP, Arg, Converted); 1989 1990 // Check non-type template parameters. 1991 if (NonTypeTemplateParmDecl *NTTP =dyn_cast<NonTypeTemplateParmDecl>(Param)) { 1992 // Do substitution on the type of the non-type template parameter 1993 // with the template arguments we've seen thus far. 1994 QualType NTTPType = NTTP->getType(); 1995 if (NTTPType->isDependentType()) { 1996 // Do substitution on the type of the non-type template parameter. 1997 InstantiatingTemplate Inst(*this, TemplateLoc, Template, 1998 NTTP, Converted.getFlatArguments(), 1999 Converted.flatSize(), 2000 SourceRange(TemplateLoc, RAngleLoc)); 2001 2002 TemplateArgumentList TemplateArgs(Context, Converted, 2003 /*TakeArgs=*/false); 2004 NTTPType = SubstType(NTTPType, 2005 MultiLevelTemplateArgumentList(TemplateArgs), 2006 NTTP->getLocation(), 2007 NTTP->getDeclName()); 2008 // If that worked, check the non-type template parameter type 2009 // for validity. 2010 if (!NTTPType.isNull()) 2011 NTTPType = CheckNonTypeTemplateParameterType(NTTPType, 2012 NTTP->getLocation()); 2013 if (NTTPType.isNull()) 2014 return true; 2015 } 2016 2017 switch (Arg.getArgument().getKind()) { 2018 case TemplateArgument::Null: 2019 assert(false && "Should never see a NULL template argument here"); 2020 return true; 2021 2022 case TemplateArgument::Expression: { 2023 Expr *E = Arg.getArgument().getAsExpr(); 2024 TemplateArgument Result; 2025 if (CheckTemplateArgument(NTTP, NTTPType, E, Result, CTAK)) 2026 return true; 2027 2028 Converted.Append(Result); 2029 break; 2030 } 2031 2032 case TemplateArgument::Declaration: 2033 case TemplateArgument::Integral: 2034 // We've already checked this template argument, so just copy 2035 // it to the list of converted arguments. 2036 Converted.Append(Arg.getArgument()); 2037 break; 2038 2039 case TemplateArgument::Template: 2040 // We were given a template template argument. It may not be ill-formed; 2041 // see below. 2042 if (DependentTemplateName *DTN 2043 = Arg.getArgument().getAsTemplate().getAsDependentTemplateName()) { 2044 // We have a template argument such as \c T::template X, which we 2045 // parsed as a template template argument. However, since we now 2046 // know that we need a non-type template argument, convert this 2047 // template name into an expression. 2048 Expr *E = DependentScopeDeclRefExpr::Create(Context, 2049 DTN->getQualifier(), 2050 Arg.getTemplateQualifierRange(), 2051 DTN->getIdentifier(), 2052 Arg.getTemplateNameLoc()); 2053 2054 TemplateArgument Result; 2055 if (CheckTemplateArgument(NTTP, NTTPType, E, Result)) 2056 return true; 2057 2058 Converted.Append(Result); 2059 break; 2060 } 2061 2062 // We have a template argument that actually does refer to a class 2063 // template, template alias, or template template parameter, and 2064 // therefore cannot be a non-type template argument. 2065 Diag(Arg.getLocation(), diag::err_template_arg_must_be_expr) 2066 << Arg.getSourceRange(); 2067 2068 Diag(Param->getLocation(), diag::note_template_param_here); 2069 return true; 2070 2071 case TemplateArgument::Type: { 2072 // We have a non-type template parameter but the template 2073 // argument is a type. 2074 2075 // C++ [temp.arg]p2: 2076 // In a template-argument, an ambiguity between a type-id and 2077 // an expression is resolved to a type-id, regardless of the 2078 // form of the corresponding template-parameter. 2079 // 2080 // We warn specifically about this case, since it can be rather 2081 // confusing for users. 2082 QualType T = Arg.getArgument().getAsType(); 2083 SourceRange SR = Arg.getSourceRange(); 2084 if (T->isFunctionType()) 2085 Diag(SR.getBegin(), diag::err_template_arg_nontype_ambig) << SR << T; 2086 else 2087 Diag(SR.getBegin(), diag::err_template_arg_must_be_expr) << SR; 2088 Diag(Param->getLocation(), diag::note_template_param_here); 2089 return true; 2090 } 2091 2092 case TemplateArgument::Pack: 2093 llvm_unreachable("Caller must expand template argument packs"); 2094 break; 2095 } 2096 2097 return false; 2098 } 2099 2100 2101 // Check template template parameters. 2102 TemplateTemplateParmDecl *TempParm = cast<TemplateTemplateParmDecl>(Param); 2103 2104 // Substitute into the template parameter list of the template 2105 // template parameter, since previously-supplied template arguments 2106 // may appear within the template template parameter. 2107 { 2108 // Set up a template instantiation context. 2109 LocalInstantiationScope Scope(*this); 2110 InstantiatingTemplate Inst(*this, TemplateLoc, Template, 2111 TempParm, Converted.getFlatArguments(), 2112 Converted.flatSize(), 2113 SourceRange(TemplateLoc, RAngleLoc)); 2114 2115 TemplateArgumentList TemplateArgs(Context, Converted, 2116 /*TakeArgs=*/false); 2117 TempParm = cast_or_null<TemplateTemplateParmDecl>( 2118 SubstDecl(TempParm, CurContext, 2119 MultiLevelTemplateArgumentList(TemplateArgs))); 2120 if (!TempParm) 2121 return true; 2122 2123 // FIXME: TempParam is leaked. 2124 } 2125 2126 switch (Arg.getArgument().getKind()) { 2127 case TemplateArgument::Null: 2128 assert(false && "Should never see a NULL template argument here"); 2129 return true; 2130 2131 case TemplateArgument::Template: 2132 if (CheckTemplateArgument(TempParm, Arg)) 2133 return true; 2134 2135 Converted.Append(Arg.getArgument()); 2136 break; 2137 2138 case TemplateArgument::Expression: 2139 case TemplateArgument::Type: 2140 // We have a template template parameter but the template 2141 // argument does not refer to a template. 2142 Diag(Arg.getLocation(), diag::err_template_arg_must_be_template); 2143 return true; 2144 2145 case TemplateArgument::Declaration: 2146 llvm_unreachable( 2147 "Declaration argument with template template parameter"); 2148 break; 2149 case TemplateArgument::Integral: 2150 llvm_unreachable( 2151 "Integral argument with template template parameter"); 2152 break; 2153 2154 case TemplateArgument::Pack: 2155 llvm_unreachable("Caller must expand template argument packs"); 2156 break; 2157 } 2158 2159 return false; 2160} 2161 2162/// \brief Check that the given template argument list is well-formed 2163/// for specializing the given template. 2164bool Sema::CheckTemplateArgumentList(TemplateDecl *Template, 2165 SourceLocation TemplateLoc, 2166 const TemplateArgumentListInfo &TemplateArgs, 2167 bool PartialTemplateArgs, 2168 TemplateArgumentListBuilder &Converted) { 2169 TemplateParameterList *Params = Template->getTemplateParameters(); 2170 unsigned NumParams = Params->size(); 2171 unsigned NumArgs = TemplateArgs.size(); 2172 bool Invalid = false; 2173 2174 SourceLocation RAngleLoc = TemplateArgs.getRAngleLoc(); 2175 2176 bool HasParameterPack = 2177 NumParams > 0 && Params->getParam(NumParams - 1)->isTemplateParameterPack(); 2178 2179 if ((NumArgs > NumParams && !HasParameterPack) || 2180 (NumArgs < Params->getMinRequiredArguments() && 2181 !PartialTemplateArgs)) { 2182 // FIXME: point at either the first arg beyond what we can handle, 2183 // or the '>', depending on whether we have too many or too few 2184 // arguments. 2185 SourceRange Range; 2186 if (NumArgs > NumParams) 2187 Range = SourceRange(TemplateArgs[NumParams].getLocation(), RAngleLoc); 2188 Diag(TemplateLoc, diag::err_template_arg_list_different_arity) 2189 << (NumArgs > NumParams) 2190 << (isa<ClassTemplateDecl>(Template)? 0 : 2191 isa<FunctionTemplateDecl>(Template)? 1 : 2192 isa<TemplateTemplateParmDecl>(Template)? 2 : 3) 2193 << Template << Range; 2194 Diag(Template->getLocation(), diag::note_template_decl_here) 2195 << Params->getSourceRange(); 2196 Invalid = true; 2197 } 2198 2199 // C++ [temp.arg]p1: 2200 // [...] The type and form of each template-argument specified in 2201 // a template-id shall match the type and form specified for the 2202 // corresponding parameter declared by the template in its 2203 // template-parameter-list. 2204 unsigned ArgIdx = 0; 2205 for (TemplateParameterList::iterator Param = Params->begin(), 2206 ParamEnd = Params->end(); 2207 Param != ParamEnd; ++Param, ++ArgIdx) { 2208 if (ArgIdx > NumArgs && PartialTemplateArgs) 2209 break; 2210 2211 // If we have a template parameter pack, check every remaining template 2212 // argument against that template parameter pack. 2213 if ((*Param)->isTemplateParameterPack()) { 2214 Converted.BeginPack(); 2215 for (; ArgIdx < NumArgs; ++ArgIdx) { 2216 if (CheckTemplateArgument(*Param, TemplateArgs[ArgIdx], Template, 2217 TemplateLoc, RAngleLoc, Converted)) { 2218 Invalid = true; 2219 break; 2220 } 2221 } 2222 Converted.EndPack(); 2223 continue; 2224 } 2225 2226 if (ArgIdx < NumArgs) { 2227 // Check the template argument we were given. 2228 if (CheckTemplateArgument(*Param, TemplateArgs[ArgIdx], Template, 2229 TemplateLoc, RAngleLoc, Converted)) 2230 return true; 2231 2232 continue; 2233 } 2234 2235 // We have a default template argument that we will use. 2236 TemplateArgumentLoc Arg; 2237 2238 // Retrieve the default template argument from the template 2239 // parameter. For each kind of template parameter, we substitute the 2240 // template arguments provided thus far and any "outer" template arguments 2241 // (when the template parameter was part of a nested template) into 2242 // the default argument. 2243 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*Param)) { 2244 if (!TTP->hasDefaultArgument()) { 2245 assert((Invalid || PartialTemplateArgs) && "Missing default argument"); 2246 break; 2247 } 2248 2249 TypeSourceInfo *ArgType = SubstDefaultTemplateArgument(*this, 2250 Template, 2251 TemplateLoc, 2252 RAngleLoc, 2253 TTP, 2254 Converted); 2255 if (!ArgType) 2256 return true; 2257 2258 Arg = TemplateArgumentLoc(TemplateArgument(ArgType->getType()), 2259 ArgType); 2260 } else if (NonTypeTemplateParmDecl *NTTP 2261 = dyn_cast<NonTypeTemplateParmDecl>(*Param)) { 2262 if (!NTTP->hasDefaultArgument()) { 2263 assert((Invalid || PartialTemplateArgs) && "Missing default argument"); 2264 break; 2265 } 2266 2267 Sema::OwningExprResult E = SubstDefaultTemplateArgument(*this, Template, 2268 TemplateLoc, 2269 RAngleLoc, 2270 NTTP, 2271 Converted); 2272 if (E.isInvalid()) 2273 return true; 2274 2275 Expr *Ex = E.takeAs<Expr>(); 2276 Arg = TemplateArgumentLoc(TemplateArgument(Ex), Ex); 2277 } else { 2278 TemplateTemplateParmDecl *TempParm 2279 = cast<TemplateTemplateParmDecl>(*Param); 2280 2281 if (!TempParm->hasDefaultArgument()) { 2282 assert((Invalid || PartialTemplateArgs) && "Missing default argument"); 2283 break; 2284 } 2285 2286 TemplateName Name = SubstDefaultTemplateArgument(*this, Template, 2287 TemplateLoc, 2288 RAngleLoc, 2289 TempParm, 2290 Converted); 2291 if (Name.isNull()) 2292 return true; 2293 2294 Arg = TemplateArgumentLoc(TemplateArgument(Name), 2295 TempParm->getDefaultArgument().getTemplateQualifierRange(), 2296 TempParm->getDefaultArgument().getTemplateNameLoc()); 2297 } 2298 2299 // Introduce an instantiation record that describes where we are using 2300 // the default template argument. 2301 InstantiatingTemplate Instantiating(*this, RAngleLoc, Template, *Param, 2302 Converted.getFlatArguments(), 2303 Converted.flatSize(), 2304 SourceRange(TemplateLoc, RAngleLoc)); 2305 2306 // Check the default template argument. 2307 if (CheckTemplateArgument(*Param, Arg, Template, TemplateLoc, 2308 RAngleLoc, Converted)) 2309 return true; 2310 } 2311 2312 return Invalid; 2313} 2314 2315/// \brief Check a template argument against its corresponding 2316/// template type parameter. 2317/// 2318/// This routine implements the semantics of C++ [temp.arg.type]. It 2319/// returns true if an error occurred, and false otherwise. 2320bool Sema::CheckTemplateArgument(TemplateTypeParmDecl *Param, 2321 TypeSourceInfo *ArgInfo) { 2322 assert(ArgInfo && "invalid TypeSourceInfo"); 2323 QualType Arg = ArgInfo->getType(); 2324 2325 // C++ [temp.arg.type]p2: 2326 // A local type, a type with no linkage, an unnamed type or a type 2327 // compounded from any of these types shall not be used as a 2328 // template-argument for a template type-parameter. 2329 // 2330 // FIXME: Perform the unnamed type check. 2331 SourceRange SR = ArgInfo->getTypeLoc().getSourceRange(); 2332 const TagType *Tag = 0; 2333 if (const EnumType *EnumT = Arg->getAs<EnumType>()) 2334 Tag = EnumT; 2335 else if (const RecordType *RecordT = Arg->getAs<RecordType>()) 2336 Tag = RecordT; 2337 if (Tag && Tag->getDecl()->getDeclContext()->isFunctionOrMethod()) { 2338 SourceRange SR = ArgInfo->getTypeLoc().getSourceRange(); 2339 return Diag(SR.getBegin(), diag::err_template_arg_local_type) 2340 << QualType(Tag, 0) << SR; 2341 } else if (Tag && !Tag->getDecl()->getDeclName() && 2342 !Tag->getDecl()->getTypedefForAnonDecl()) { 2343 Diag(SR.getBegin(), diag::err_template_arg_unnamed_type) << SR; 2344 Diag(Tag->getDecl()->getLocation(), diag::note_template_unnamed_type_here); 2345 return true; 2346 } else if (Arg->isVariablyModifiedType()) { 2347 Diag(SR.getBegin(), diag::err_variably_modified_template_arg) 2348 << Arg; 2349 return true; 2350 } else if (Context.hasSameUnqualifiedType(Arg, Context.OverloadTy)) { 2351 return Diag(SR.getBegin(), diag::err_template_arg_overload_type) << SR; 2352 } 2353 2354 return false; 2355} 2356 2357/// \brief Checks whether the given template argument is the address 2358/// of an object or function according to C++ [temp.arg.nontype]p1. 2359static bool 2360CheckTemplateArgumentAddressOfObjectOrFunction(Sema &S, 2361 NonTypeTemplateParmDecl *Param, 2362 QualType ParamType, 2363 Expr *ArgIn, 2364 TemplateArgument &Converted) { 2365 bool Invalid = false; 2366 Expr *Arg = ArgIn; 2367 QualType ArgType = Arg->getType(); 2368 2369 // See through any implicit casts we added to fix the type. 2370 while (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(Arg)) 2371 Arg = Cast->getSubExpr(); 2372 2373 // C++ [temp.arg.nontype]p1: 2374 // 2375 // A template-argument for a non-type, non-template 2376 // template-parameter shall be one of: [...] 2377 // 2378 // -- the address of an object or function with external 2379 // linkage, including function templates and function 2380 // template-ids but excluding non-static class members, 2381 // expressed as & id-expression where the & is optional if 2382 // the name refers to a function or array, or if the 2383 // corresponding template-parameter is a reference; or 2384 DeclRefExpr *DRE = 0; 2385 2386 // Ignore (and complain about) any excess parentheses. 2387 while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) { 2388 if (!Invalid) { 2389 S.Diag(Arg->getSourceRange().getBegin(), 2390 diag::err_template_arg_extra_parens) 2391 << Arg->getSourceRange(); 2392 Invalid = true; 2393 } 2394 2395 Arg = Parens->getSubExpr(); 2396 } 2397 2398 bool AddressTaken = false; 2399 SourceLocation AddrOpLoc; 2400 if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) { 2401 if (UnOp->getOpcode() == UnaryOperator::AddrOf) { 2402 DRE = dyn_cast<DeclRefExpr>(UnOp->getSubExpr()); 2403 AddressTaken = true; 2404 AddrOpLoc = UnOp->getOperatorLoc(); 2405 } 2406 } else 2407 DRE = dyn_cast<DeclRefExpr>(Arg); 2408 2409 if (!DRE) { 2410 S.Diag(Arg->getLocStart(), diag::err_template_arg_not_decl_ref) 2411 << Arg->getSourceRange(); 2412 S.Diag(Param->getLocation(), diag::note_template_param_here); 2413 return true; 2414 } 2415 2416 // Stop checking the precise nature of the argument if it is value dependent, 2417 // it should be checked when instantiated. 2418 if (Arg->isValueDependent()) { 2419 Converted = TemplateArgument(ArgIn->Retain()); 2420 return false; 2421 } 2422 2423 if (!isa<ValueDecl>(DRE->getDecl())) { 2424 S.Diag(Arg->getSourceRange().getBegin(), 2425 diag::err_template_arg_not_object_or_func_form) 2426 << Arg->getSourceRange(); 2427 S.Diag(Param->getLocation(), diag::note_template_param_here); 2428 return true; 2429 } 2430 2431 NamedDecl *Entity = 0; 2432 2433 // Cannot refer to non-static data members 2434 if (FieldDecl *Field = dyn_cast<FieldDecl>(DRE->getDecl())) { 2435 S.Diag(Arg->getSourceRange().getBegin(), diag::err_template_arg_field) 2436 << Field << Arg->getSourceRange(); 2437 S.Diag(Param->getLocation(), diag::note_template_param_here); 2438 return true; 2439 } 2440 2441 // Cannot refer to non-static member functions 2442 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(DRE->getDecl())) 2443 if (!Method->isStatic()) { 2444 S.Diag(Arg->getSourceRange().getBegin(), diag::err_template_arg_method) 2445 << Method << Arg->getSourceRange(); 2446 S.Diag(Param->getLocation(), diag::note_template_param_here); 2447 return true; 2448 } 2449 2450 // Functions must have external linkage. 2451 if (FunctionDecl *Func = dyn_cast<FunctionDecl>(DRE->getDecl())) { 2452 if (!isExternalLinkage(Func->getLinkage())) { 2453 S.Diag(Arg->getSourceRange().getBegin(), 2454 diag::err_template_arg_function_not_extern) 2455 << Func << Arg->getSourceRange(); 2456 S.Diag(Func->getLocation(), diag::note_template_arg_internal_object) 2457 << true; 2458 return true; 2459 } 2460 2461 // Okay: we've named a function with external linkage. 2462 Entity = Func; 2463 2464 // If the template parameter has pointer type, the function decays. 2465 if (ParamType->isPointerType() && !AddressTaken) 2466 ArgType = S.Context.getPointerType(Func->getType()); 2467 else if (AddressTaken && ParamType->isReferenceType()) { 2468 // If we originally had an address-of operator, but the 2469 // parameter has reference type, complain and (if things look 2470 // like they will work) drop the address-of operator. 2471 if (!S.Context.hasSameUnqualifiedType(Func->getType(), 2472 ParamType.getNonReferenceType())) { 2473 S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) 2474 << ParamType; 2475 S.Diag(Param->getLocation(), diag::note_template_param_here); 2476 return true; 2477 } 2478 2479 S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) 2480 << ParamType 2481 << FixItHint::CreateRemoval(AddrOpLoc); 2482 S.Diag(Param->getLocation(), diag::note_template_param_here); 2483 2484 ArgType = Func->getType(); 2485 } 2486 } else if (VarDecl *Var = dyn_cast<VarDecl>(DRE->getDecl())) { 2487 if (!isExternalLinkage(Var->getLinkage())) { 2488 S.Diag(Arg->getSourceRange().getBegin(), 2489 diag::err_template_arg_object_not_extern) 2490 << Var << Arg->getSourceRange(); 2491 S.Diag(Var->getLocation(), diag::note_template_arg_internal_object) 2492 << true; 2493 return true; 2494 } 2495 2496 // A value of reference type is not an object. 2497 if (Var->getType()->isReferenceType()) { 2498 S.Diag(Arg->getSourceRange().getBegin(), 2499 diag::err_template_arg_reference_var) 2500 << Var->getType() << Arg->getSourceRange(); 2501 S.Diag(Param->getLocation(), diag::note_template_param_here); 2502 return true; 2503 } 2504 2505 // Okay: we've named an object with external linkage 2506 Entity = Var; 2507 2508 // If the template parameter has pointer type, we must have taken 2509 // the address of this object. 2510 if (ParamType->isReferenceType()) { 2511 if (AddressTaken) { 2512 // If we originally had an address-of operator, but the 2513 // parameter has reference type, complain and (if things look 2514 // like they will work) drop the address-of operator. 2515 if (!S.Context.hasSameUnqualifiedType(Var->getType(), 2516 ParamType.getNonReferenceType())) { 2517 S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) 2518 << ParamType; 2519 S.Diag(Param->getLocation(), diag::note_template_param_here); 2520 return true; 2521 } 2522 2523 S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) 2524 << ParamType 2525 << FixItHint::CreateRemoval(AddrOpLoc); 2526 S.Diag(Param->getLocation(), diag::note_template_param_here); 2527 2528 ArgType = Var->getType(); 2529 } 2530 } else if (!AddressTaken && ParamType->isPointerType()) { 2531 if (Var->getType()->isArrayType()) { 2532 // Array-to-pointer decay. 2533 ArgType = S.Context.getArrayDecayedType(Var->getType()); 2534 } else { 2535 // If the template parameter has pointer type but the address of 2536 // this object was not taken, complain and (possibly) recover by 2537 // taking the address of the entity. 2538 ArgType = S.Context.getPointerType(Var->getType()); 2539 if (!S.Context.hasSameUnqualifiedType(ArgType, ParamType)) { 2540 S.Diag(Arg->getLocStart(), diag::err_template_arg_not_address_of) 2541 << ParamType; 2542 S.Diag(Param->getLocation(), diag::note_template_param_here); 2543 return true; 2544 } 2545 2546 S.Diag(Arg->getLocStart(), diag::err_template_arg_not_address_of) 2547 << ParamType 2548 << FixItHint::CreateInsertion(Arg->getLocStart(), "&"); 2549 2550 S.Diag(Param->getLocation(), diag::note_template_param_here); 2551 } 2552 } 2553 } else { 2554 // We found something else, but we don't know specifically what it is. 2555 S.Diag(Arg->getSourceRange().getBegin(), 2556 diag::err_template_arg_not_object_or_func) 2557 << Arg->getSourceRange(); 2558 S.Diag(DRE->getDecl()->getLocation(), diag::note_template_arg_refers_here); 2559 return true; 2560 } 2561 2562 if (ParamType->isPointerType() && 2563 !ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType() && 2564 S.IsQualificationConversion(ArgType, ParamType)) { 2565 // For pointer-to-object types, qualification conversions are 2566 // permitted. 2567 } else { 2568 if (const ReferenceType *ParamRef = ParamType->getAs<ReferenceType>()) { 2569 if (!ParamRef->getPointeeType()->isFunctionType()) { 2570 // C++ [temp.arg.nontype]p5b3: 2571 // For a non-type template-parameter of type reference to 2572 // object, no conversions apply. The type referred to by the 2573 // reference may be more cv-qualified than the (otherwise 2574 // identical) type of the template- argument. The 2575 // template-parameter is bound directly to the 2576 // template-argument, which shall be an lvalue. 2577 2578 // FIXME: Other qualifiers? 2579 unsigned ParamQuals = ParamRef->getPointeeType().getCVRQualifiers(); 2580 unsigned ArgQuals = ArgType.getCVRQualifiers(); 2581 2582 if ((ParamQuals | ArgQuals) != ParamQuals) { 2583 S.Diag(Arg->getSourceRange().getBegin(), 2584 diag::err_template_arg_ref_bind_ignores_quals) 2585 << ParamType << Arg->getType() 2586 << Arg->getSourceRange(); 2587 S.Diag(Param->getLocation(), diag::note_template_param_here); 2588 return true; 2589 } 2590 } 2591 } 2592 2593 // At this point, the template argument refers to an object or 2594 // function with external linkage. We now need to check whether the 2595 // argument and parameter types are compatible. 2596 if (!S.Context.hasSameUnqualifiedType(ArgType, 2597 ParamType.getNonReferenceType())) { 2598 // We can't perform this conversion or binding. 2599 if (ParamType->isReferenceType()) 2600 S.Diag(Arg->getLocStart(), diag::err_template_arg_no_ref_bind) 2601 << ParamType << Arg->getType() << Arg->getSourceRange(); 2602 else 2603 S.Diag(Arg->getLocStart(), diag::err_template_arg_not_convertible) 2604 << Arg->getType() << ParamType << Arg->getSourceRange(); 2605 S.Diag(Param->getLocation(), diag::note_template_param_here); 2606 return true; 2607 } 2608 } 2609 2610 // Create the template argument. 2611 Converted = TemplateArgument(Entity->getCanonicalDecl()); 2612 S.MarkDeclarationReferenced(Arg->getLocStart(), Entity); 2613 return false; 2614} 2615 2616/// \brief Checks whether the given template argument is a pointer to 2617/// member constant according to C++ [temp.arg.nontype]p1. 2618bool Sema::CheckTemplateArgumentPointerToMember(Expr *Arg, 2619 TemplateArgument &Converted) { 2620 bool Invalid = false; 2621 2622 // See through any implicit casts we added to fix the type. 2623 while (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(Arg)) 2624 Arg = Cast->getSubExpr(); 2625 2626 // C++ [temp.arg.nontype]p1: 2627 // 2628 // A template-argument for a non-type, non-template 2629 // template-parameter shall be one of: [...] 2630 // 2631 // -- a pointer to member expressed as described in 5.3.1. 2632 DeclRefExpr *DRE = 0; 2633 2634 // Ignore (and complain about) any excess parentheses. 2635 while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) { 2636 if (!Invalid) { 2637 Diag(Arg->getSourceRange().getBegin(), 2638 diag::err_template_arg_extra_parens) 2639 << Arg->getSourceRange(); 2640 Invalid = true; 2641 } 2642 2643 Arg = Parens->getSubExpr(); 2644 } 2645 2646 // A pointer-to-member constant written &Class::member. 2647 if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) { 2648 if (UnOp->getOpcode() == UnaryOperator::AddrOf) { 2649 DRE = dyn_cast<DeclRefExpr>(UnOp->getSubExpr()); 2650 if (DRE && !DRE->getQualifier()) 2651 DRE = 0; 2652 } 2653 } 2654 // A constant of pointer-to-member type. 2655 else if ((DRE = dyn_cast<DeclRefExpr>(Arg))) { 2656 if (ValueDecl *VD = dyn_cast<ValueDecl>(DRE->getDecl())) { 2657 if (VD->getType()->isMemberPointerType()) { 2658 if (isa<NonTypeTemplateParmDecl>(VD) || 2659 (isa<VarDecl>(VD) && 2660 Context.getCanonicalType(VD->getType()).isConstQualified())) { 2661 if (Arg->isTypeDependent() || Arg->isValueDependent()) 2662 Converted = TemplateArgument(Arg->Retain()); 2663 else 2664 Converted = TemplateArgument(VD->getCanonicalDecl()); 2665 return Invalid; 2666 } 2667 } 2668 } 2669 2670 DRE = 0; 2671 } 2672 2673 if (!DRE) 2674 return Diag(Arg->getSourceRange().getBegin(), 2675 diag::err_template_arg_not_pointer_to_member_form) 2676 << Arg->getSourceRange(); 2677 2678 if (isa<FieldDecl>(DRE->getDecl()) || isa<CXXMethodDecl>(DRE->getDecl())) { 2679 assert((isa<FieldDecl>(DRE->getDecl()) || 2680 !cast<CXXMethodDecl>(DRE->getDecl())->isStatic()) && 2681 "Only non-static member pointers can make it here"); 2682 2683 // Okay: this is the address of a non-static member, and therefore 2684 // a member pointer constant. 2685 if (Arg->isTypeDependent() || Arg->isValueDependent()) 2686 Converted = TemplateArgument(Arg->Retain()); 2687 else 2688 Converted = TemplateArgument(DRE->getDecl()->getCanonicalDecl()); 2689 return Invalid; 2690 } 2691 2692 // We found something else, but we don't know specifically what it is. 2693 Diag(Arg->getSourceRange().getBegin(), 2694 diag::err_template_arg_not_pointer_to_member_form) 2695 << Arg->getSourceRange(); 2696 Diag(DRE->getDecl()->getLocation(), 2697 diag::note_template_arg_refers_here); 2698 return true; 2699} 2700 2701/// \brief Check a template argument against its corresponding 2702/// non-type template parameter. 2703/// 2704/// This routine implements the semantics of C++ [temp.arg.nontype]. 2705/// It returns true if an error occurred, and false otherwise. \p 2706/// InstantiatedParamType is the type of the non-type template 2707/// parameter after it has been instantiated. 2708/// 2709/// If no error was detected, Converted receives the converted template argument. 2710bool Sema::CheckTemplateArgument(NonTypeTemplateParmDecl *Param, 2711 QualType InstantiatedParamType, Expr *&Arg, 2712 TemplateArgument &Converted, 2713 CheckTemplateArgumentKind CTAK) { 2714 SourceLocation StartLoc = Arg->getSourceRange().getBegin(); 2715 2716 // If either the parameter has a dependent type or the argument is 2717 // type-dependent, there's nothing we can check now. 2718 if (InstantiatedParamType->isDependentType() || Arg->isTypeDependent()) { 2719 // FIXME: Produce a cloned, canonical expression? 2720 Converted = TemplateArgument(Arg); 2721 return false; 2722 } 2723 2724 // C++ [temp.arg.nontype]p5: 2725 // The following conversions are performed on each expression used 2726 // as a non-type template-argument. If a non-type 2727 // template-argument cannot be converted to the type of the 2728 // corresponding template-parameter then the program is 2729 // ill-formed. 2730 // 2731 // -- for a non-type template-parameter of integral or 2732 // enumeration type, integral promotions (4.5) and integral 2733 // conversions (4.7) are applied. 2734 QualType ParamType = InstantiatedParamType; 2735 QualType ArgType = Arg->getType(); 2736 if (ParamType->isIntegralOrEnumerationType()) { 2737 // C++ [temp.arg.nontype]p1: 2738 // A template-argument for a non-type, non-template 2739 // template-parameter shall be one of: 2740 // 2741 // -- an integral constant-expression of integral or enumeration 2742 // type; or 2743 // -- the name of a non-type template-parameter; or 2744 SourceLocation NonConstantLoc; 2745 llvm::APSInt Value; 2746 if (!ArgType->isIntegralOrEnumerationType()) { 2747 Diag(Arg->getSourceRange().getBegin(), 2748 diag::err_template_arg_not_integral_or_enumeral) 2749 << ArgType << Arg->getSourceRange(); 2750 Diag(Param->getLocation(), diag::note_template_param_here); 2751 return true; 2752 } else if (!Arg->isValueDependent() && 2753 !Arg->isIntegerConstantExpr(Value, Context, &NonConstantLoc)) { 2754 Diag(NonConstantLoc, diag::err_template_arg_not_ice) 2755 << ArgType << Arg->getSourceRange(); 2756 return true; 2757 } 2758 2759 // From here on out, all we care about are the unqualified forms 2760 // of the parameter and argument types. 2761 ParamType = ParamType.getUnqualifiedType(); 2762 ArgType = ArgType.getUnqualifiedType(); 2763 2764 // Try to convert the argument to the parameter's type. 2765 if (Context.hasSameType(ParamType, ArgType)) { 2766 // Okay: no conversion necessary 2767 } else if (CTAK == CTAK_Deduced) { 2768 // C++ [temp.deduct.type]p17: 2769 // If, in the declaration of a function template with a non-type 2770 // template-parameter, the non-type template- parameter is used 2771 // in an expression in the function parameter-list and, if the 2772 // corresponding template-argument is deduced, the 2773 // template-argument type shall match the type of the 2774 // template-parameter exactly, except that a template-argument 2775 // deduced from an array bound may be of any integral type. 2776 Diag(StartLoc, diag::err_deduced_non_type_template_arg_type_mismatch) 2777 << ArgType << ParamType; 2778 Diag(Param->getLocation(), diag::note_template_param_here); 2779 return true; 2780 } else if (IsIntegralPromotion(Arg, ArgType, ParamType) || 2781 !ParamType->isEnumeralType()) { 2782 // This is an integral promotion or conversion. 2783 ImpCastExprToType(Arg, ParamType, CastExpr::CK_IntegralCast); 2784 } else { 2785 // We can't perform this conversion. 2786 Diag(Arg->getSourceRange().getBegin(), 2787 diag::err_template_arg_not_convertible) 2788 << Arg->getType() << InstantiatedParamType << Arg->getSourceRange(); 2789 Diag(Param->getLocation(), diag::note_template_param_here); 2790 return true; 2791 } 2792 2793 QualType IntegerType = Context.getCanonicalType(ParamType); 2794 if (const EnumType *Enum = IntegerType->getAs<EnumType>()) 2795 IntegerType = Context.getCanonicalType(Enum->getDecl()->getIntegerType()); 2796 2797 if (!Arg->isValueDependent()) { 2798 llvm::APSInt OldValue = Value; 2799 2800 // Coerce the template argument's value to the value it will have 2801 // based on the template parameter's type. 2802 unsigned AllowedBits = Context.getTypeSize(IntegerType); 2803 if (Value.getBitWidth() != AllowedBits) 2804 Value.extOrTrunc(AllowedBits); 2805 Value.setIsSigned(IntegerType->isSignedIntegerType()); 2806 2807 // Complain if an unsigned parameter received a negative value. 2808 if (IntegerType->isUnsignedIntegerType() 2809 && (OldValue.isSigned() && OldValue.isNegative())) { 2810 Diag(Arg->getSourceRange().getBegin(), diag::warn_template_arg_negative) 2811 << OldValue.toString(10) << Value.toString(10) << Param->getType() 2812 << Arg->getSourceRange(); 2813 Diag(Param->getLocation(), diag::note_template_param_here); 2814 } 2815 2816 // Complain if we overflowed the template parameter's type. 2817 unsigned RequiredBits; 2818 if (IntegerType->isUnsignedIntegerType()) 2819 RequiredBits = OldValue.getActiveBits(); 2820 else if (OldValue.isUnsigned()) 2821 RequiredBits = OldValue.getActiveBits() + 1; 2822 else 2823 RequiredBits = OldValue.getMinSignedBits(); 2824 if (RequiredBits > AllowedBits) { 2825 Diag(Arg->getSourceRange().getBegin(), 2826 diag::warn_template_arg_too_large) 2827 << OldValue.toString(10) << Value.toString(10) << Param->getType() 2828 << Arg->getSourceRange(); 2829 Diag(Param->getLocation(), diag::note_template_param_here); 2830 } 2831 } 2832 2833 // Add the value of this argument to the list of converted 2834 // arguments. We use the bitwidth and signedness of the template 2835 // parameter. 2836 if (Arg->isValueDependent()) { 2837 // The argument is value-dependent. Create a new 2838 // TemplateArgument with the converted expression. 2839 Converted = TemplateArgument(Arg); 2840 return false; 2841 } 2842 2843 Converted = TemplateArgument(Value, 2844 ParamType->isEnumeralType() ? ParamType 2845 : IntegerType); 2846 return false; 2847 } 2848 2849 DeclAccessPair FoundResult; // temporary for ResolveOverloadedFunction 2850 2851 // C++0x [temp.arg.nontype]p5 bullets 2, 4 and 6 permit conversion 2852 // from a template argument of type std::nullptr_t to a non-type 2853 // template parameter of type pointer to object, pointer to 2854 // function, or pointer-to-member, respectively. 2855 if (ArgType->isNullPtrType() && 2856 (ParamType->isPointerType() || ParamType->isMemberPointerType())) { 2857 Converted = TemplateArgument((NamedDecl *)0); 2858 return false; 2859 } 2860 2861 // Handle pointer-to-function, reference-to-function, and 2862 // pointer-to-member-function all in (roughly) the same way. 2863 if (// -- For a non-type template-parameter of type pointer to 2864 // function, only the function-to-pointer conversion (4.3) is 2865 // applied. If the template-argument represents a set of 2866 // overloaded functions (or a pointer to such), the matching 2867 // function is selected from the set (13.4). 2868 (ParamType->isPointerType() && 2869 ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType()) || 2870 // -- For a non-type template-parameter of type reference to 2871 // function, no conversions apply. If the template-argument 2872 // represents a set of overloaded functions, the matching 2873 // function is selected from the set (13.4). 2874 (ParamType->isReferenceType() && 2875 ParamType->getAs<ReferenceType>()->getPointeeType()->isFunctionType()) || 2876 // -- For a non-type template-parameter of type pointer to 2877 // member function, no conversions apply. If the 2878 // template-argument represents a set of overloaded member 2879 // functions, the matching member function is selected from 2880 // the set (13.4). 2881 (ParamType->isMemberPointerType() && 2882 ParamType->getAs<MemberPointerType>()->getPointeeType() 2883 ->isFunctionType())) { 2884 2885 if (Arg->getType() == Context.OverloadTy) { 2886 if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg, ParamType, 2887 true, 2888 FoundResult)) { 2889 if (DiagnoseUseOfDecl(Fn, Arg->getSourceRange().getBegin())) 2890 return true; 2891 2892 Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn); 2893 ArgType = Arg->getType(); 2894 } else 2895 return true; 2896 } 2897 2898 if (!ParamType->isMemberPointerType()) 2899 return CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param, 2900 ParamType, 2901 Arg, Converted); 2902 2903 if (IsQualificationConversion(ArgType, ParamType.getNonReferenceType())) { 2904 ImpCastExprToType(Arg, ParamType, CastExpr::CK_NoOp, 2905 Arg->isLvalue(Context) == Expr::LV_Valid); 2906 } else if (!Context.hasSameUnqualifiedType(ArgType, 2907 ParamType.getNonReferenceType())) { 2908 // We can't perform this conversion. 2909 Diag(Arg->getSourceRange().getBegin(), 2910 diag::err_template_arg_not_convertible) 2911 << Arg->getType() << InstantiatedParamType << Arg->getSourceRange(); 2912 Diag(Param->getLocation(), diag::note_template_param_here); 2913 return true; 2914 } 2915 2916 return CheckTemplateArgumentPointerToMember(Arg, Converted); 2917 } 2918 2919 if (ParamType->isPointerType()) { 2920 // -- for a non-type template-parameter of type pointer to 2921 // object, qualification conversions (4.4) and the 2922 // array-to-pointer conversion (4.2) are applied. 2923 // C++0x also allows a value of std::nullptr_t. 2924 assert(ParamType->getAs<PointerType>()->getPointeeType()->isObjectType() && 2925 "Only object pointers allowed here"); 2926 2927 return CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param, 2928 ParamType, 2929 Arg, Converted); 2930 } 2931 2932 if (const ReferenceType *ParamRefType = ParamType->getAs<ReferenceType>()) { 2933 // -- For a non-type template-parameter of type reference to 2934 // object, no conversions apply. The type referred to by the 2935 // reference may be more cv-qualified than the (otherwise 2936 // identical) type of the template-argument. The 2937 // template-parameter is bound directly to the 2938 // template-argument, which must be an lvalue. 2939 assert(ParamRefType->getPointeeType()->isObjectType() && 2940 "Only object references allowed here"); 2941 2942 if (Arg->getType() == Context.OverloadTy) { 2943 if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg, 2944 ParamRefType->getPointeeType(), 2945 true, 2946 FoundResult)) { 2947 if (DiagnoseUseOfDecl(Fn, Arg->getSourceRange().getBegin())) 2948 return true; 2949 2950 Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn); 2951 ArgType = Arg->getType(); 2952 } else 2953 return true; 2954 } 2955 2956 return CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param, 2957 ParamType, 2958 Arg, Converted); 2959 } 2960 2961 // -- For a non-type template-parameter of type pointer to data 2962 // member, qualification conversions (4.4) are applied. 2963 assert(ParamType->isMemberPointerType() && "Only pointers to members remain"); 2964 2965 if (Context.hasSameUnqualifiedType(ParamType, ArgType)) { 2966 // Types match exactly: nothing more to do here. 2967 } else if (IsQualificationConversion(ArgType, ParamType)) { 2968 ImpCastExprToType(Arg, ParamType, CastExpr::CK_NoOp, 2969 Arg->isLvalue(Context) == Expr::LV_Valid); 2970 } else { 2971 // We can't perform this conversion. 2972 Diag(Arg->getSourceRange().getBegin(), 2973 diag::err_template_arg_not_convertible) 2974 << Arg->getType() << InstantiatedParamType << Arg->getSourceRange(); 2975 Diag(Param->getLocation(), diag::note_template_param_here); 2976 return true; 2977 } 2978 2979 return CheckTemplateArgumentPointerToMember(Arg, Converted); 2980} 2981 2982/// \brief Check a template argument against its corresponding 2983/// template template parameter. 2984/// 2985/// This routine implements the semantics of C++ [temp.arg.template]. 2986/// It returns true if an error occurred, and false otherwise. 2987bool Sema::CheckTemplateArgument(TemplateTemplateParmDecl *Param, 2988 const TemplateArgumentLoc &Arg) { 2989 TemplateName Name = Arg.getArgument().getAsTemplate(); 2990 TemplateDecl *Template = Name.getAsTemplateDecl(); 2991 if (!Template) { 2992 // Any dependent template name is fine. 2993 assert(Name.isDependent() && "Non-dependent template isn't a declaration?"); 2994 return false; 2995 } 2996 2997 // C++ [temp.arg.template]p1: 2998 // A template-argument for a template template-parameter shall be 2999 // the name of a class template, expressed as id-expression. Only 3000 // primary class templates are considered when matching the 3001 // template template argument with the corresponding parameter; 3002 // partial specializations are not considered even if their 3003 // parameter lists match that of the template template parameter. 3004 // 3005 // Note that we also allow template template parameters here, which 3006 // will happen when we are dealing with, e.g., class template 3007 // partial specializations. 3008 if (!isa<ClassTemplateDecl>(Template) && 3009 !isa<TemplateTemplateParmDecl>(Template)) { 3010 assert(isa<FunctionTemplateDecl>(Template) && 3011 "Only function templates are possible here"); 3012 Diag(Arg.getLocation(), diag::err_template_arg_not_class_template); 3013 Diag(Template->getLocation(), diag::note_template_arg_refers_here_func) 3014 << Template; 3015 } 3016 3017 return !TemplateParameterListsAreEqual(Template->getTemplateParameters(), 3018 Param->getTemplateParameters(), 3019 true, 3020 TPL_TemplateTemplateArgumentMatch, 3021 Arg.getLocation()); 3022} 3023 3024/// \brief Given a non-type template argument that refers to a 3025/// declaration and the type of its corresponding non-type template 3026/// parameter, produce an expression that properly refers to that 3027/// declaration. 3028Sema::OwningExprResult 3029Sema::BuildExpressionFromDeclTemplateArgument(const TemplateArgument &Arg, 3030 QualType ParamType, 3031 SourceLocation Loc) { 3032 assert(Arg.getKind() == TemplateArgument::Declaration && 3033 "Only declaration template arguments permitted here"); 3034 ValueDecl *VD = cast<ValueDecl>(Arg.getAsDecl()); 3035 3036 if (VD->getDeclContext()->isRecord() && 3037 (isa<CXXMethodDecl>(VD) || isa<FieldDecl>(VD))) { 3038 // If the value is a class member, we might have a pointer-to-member. 3039 // Determine whether the non-type template template parameter is of 3040 // pointer-to-member type. If so, we need to build an appropriate 3041 // expression for a pointer-to-member, since a "normal" DeclRefExpr 3042 // would refer to the member itself. 3043 if (ParamType->isMemberPointerType()) { 3044 QualType ClassType 3045 = Context.getTypeDeclType(cast<RecordDecl>(VD->getDeclContext())); 3046 NestedNameSpecifier *Qualifier 3047 = NestedNameSpecifier::Create(Context, 0, false, ClassType.getTypePtr()); 3048 CXXScopeSpec SS; 3049 SS.setScopeRep(Qualifier); 3050 OwningExprResult RefExpr = BuildDeclRefExpr(VD, 3051 VD->getType().getNonReferenceType(), 3052 Loc, 3053 &SS); 3054 if (RefExpr.isInvalid()) 3055 return ExprError(); 3056 3057 RefExpr = CreateBuiltinUnaryOp(Loc, UnaryOperator::AddrOf, move(RefExpr)); 3058 3059 // We might need to perform a trailing qualification conversion, since 3060 // the element type on the parameter could be more qualified than the 3061 // element type in the expression we constructed. 3062 if (IsQualificationConversion(((Expr*) RefExpr.get())->getType(), 3063 ParamType.getUnqualifiedType())) { 3064 Expr *RefE = RefExpr.takeAs<Expr>(); 3065 ImpCastExprToType(RefE, ParamType.getUnqualifiedType(), 3066 CastExpr::CK_NoOp); 3067 RefExpr = Owned(RefE); 3068 } 3069 3070 assert(!RefExpr.isInvalid() && 3071 Context.hasSameType(((Expr*) RefExpr.get())->getType(), 3072 ParamType.getUnqualifiedType())); 3073 return move(RefExpr); 3074 } 3075 } 3076 3077 QualType T = VD->getType().getNonReferenceType(); 3078 if (ParamType->isPointerType()) { 3079 // When the non-type template parameter is a pointer, take the 3080 // address of the declaration. 3081 OwningExprResult RefExpr = BuildDeclRefExpr(VD, T, Loc); 3082 if (RefExpr.isInvalid()) 3083 return ExprError(); 3084 3085 if (T->isFunctionType() || T->isArrayType()) { 3086 // Decay functions and arrays. 3087 Expr *RefE = (Expr *)RefExpr.get(); 3088 DefaultFunctionArrayConversion(RefE); 3089 if (RefE != RefExpr.get()) { 3090 RefExpr.release(); 3091 RefExpr = Owned(RefE); 3092 } 3093 3094 return move(RefExpr); 3095 } 3096 3097 // Take the address of everything else 3098 return CreateBuiltinUnaryOp(Loc, UnaryOperator::AddrOf, move(RefExpr)); 3099 } 3100 3101 // If the non-type template parameter has reference type, qualify the 3102 // resulting declaration reference with the extra qualifiers on the 3103 // type that the reference refers to. 3104 if (const ReferenceType *TargetRef = ParamType->getAs<ReferenceType>()) 3105 T = Context.getQualifiedType(T, TargetRef->getPointeeType().getQualifiers()); 3106 3107 return BuildDeclRefExpr(VD, T, Loc); 3108} 3109 3110/// \brief Construct a new expression that refers to the given 3111/// integral template argument with the given source-location 3112/// information. 3113/// 3114/// This routine takes care of the mapping from an integral template 3115/// argument (which may have any integral type) to the appropriate 3116/// literal value. 3117Sema::OwningExprResult 3118Sema::BuildExpressionFromIntegralTemplateArgument(const TemplateArgument &Arg, 3119 SourceLocation Loc) { 3120 assert(Arg.getKind() == TemplateArgument::Integral && 3121 "Operation is only value for integral template arguments"); 3122 QualType T = Arg.getIntegralType(); 3123 if (T->isCharType() || T->isWideCharType()) 3124 return Owned(new (Context) CharacterLiteral( 3125 Arg.getAsIntegral()->getZExtValue(), 3126 T->isWideCharType(), 3127 T, 3128 Loc)); 3129 if (T->isBooleanType()) 3130 return Owned(new (Context) CXXBoolLiteralExpr( 3131 Arg.getAsIntegral()->getBoolValue(), 3132 T, 3133 Loc)); 3134 3135 return Owned(new (Context) IntegerLiteral(*Arg.getAsIntegral(), T, Loc)); 3136} 3137 3138 3139/// \brief Determine whether the given template parameter lists are 3140/// equivalent. 3141/// 3142/// \param New The new template parameter list, typically written in the 3143/// source code as part of a new template declaration. 3144/// 3145/// \param Old The old template parameter list, typically found via 3146/// name lookup of the template declared with this template parameter 3147/// list. 3148/// 3149/// \param Complain If true, this routine will produce a diagnostic if 3150/// the template parameter lists are not equivalent. 3151/// 3152/// \param Kind describes how we are to match the template parameter lists. 3153/// 3154/// \param TemplateArgLoc If this source location is valid, then we 3155/// are actually checking the template parameter list of a template 3156/// argument (New) against the template parameter list of its 3157/// corresponding template template parameter (Old). We produce 3158/// slightly different diagnostics in this scenario. 3159/// 3160/// \returns True if the template parameter lists are equal, false 3161/// otherwise. 3162bool 3163Sema::TemplateParameterListsAreEqual(TemplateParameterList *New, 3164 TemplateParameterList *Old, 3165 bool Complain, 3166 TemplateParameterListEqualKind Kind, 3167 SourceLocation TemplateArgLoc) { 3168 if (Old->size() != New->size()) { 3169 if (Complain) { 3170 unsigned NextDiag = diag::err_template_param_list_different_arity; 3171 if (TemplateArgLoc.isValid()) { 3172 Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch); 3173 NextDiag = diag::note_template_param_list_different_arity; 3174 } 3175 Diag(New->getTemplateLoc(), NextDiag) 3176 << (New->size() > Old->size()) 3177 << (Kind != TPL_TemplateMatch) 3178 << SourceRange(New->getTemplateLoc(), New->getRAngleLoc()); 3179 Diag(Old->getTemplateLoc(), diag::note_template_prev_declaration) 3180 << (Kind != TPL_TemplateMatch) 3181 << SourceRange(Old->getTemplateLoc(), Old->getRAngleLoc()); 3182 } 3183 3184 return false; 3185 } 3186 3187 for (TemplateParameterList::iterator OldParm = Old->begin(), 3188 OldParmEnd = Old->end(), NewParm = New->begin(); 3189 OldParm != OldParmEnd; ++OldParm, ++NewParm) { 3190 if ((*OldParm)->getKind() != (*NewParm)->getKind()) { 3191 if (Complain) { 3192 unsigned NextDiag = diag::err_template_param_different_kind; 3193 if (TemplateArgLoc.isValid()) { 3194 Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch); 3195 NextDiag = diag::note_template_param_different_kind; 3196 } 3197 Diag((*NewParm)->getLocation(), NextDiag) 3198 << (Kind != TPL_TemplateMatch); 3199 Diag((*OldParm)->getLocation(), diag::note_template_prev_declaration) 3200 << (Kind != TPL_TemplateMatch); 3201 } 3202 return false; 3203 } 3204 3205 if (TemplateTypeParmDecl *OldTTP 3206 = dyn_cast<TemplateTypeParmDecl>(*OldParm)) { 3207 // Template type parameters are equivalent if either both are template 3208 // type parameter packs or neither are (since we know we're at the same 3209 // index). 3210 TemplateTypeParmDecl *NewTTP = cast<TemplateTypeParmDecl>(*NewParm); 3211 if (OldTTP->isParameterPack() != NewTTP->isParameterPack()) { 3212 // FIXME: Implement the rules in C++0x [temp.arg.template]p5 that 3213 // allow one to match a template parameter pack in the template 3214 // parameter list of a template template parameter to one or more 3215 // template parameters in the template parameter list of the 3216 // corresponding template template argument. 3217 if (Complain) { 3218 unsigned NextDiag = diag::err_template_parameter_pack_non_pack; 3219 if (TemplateArgLoc.isValid()) { 3220 Diag(TemplateArgLoc, 3221 diag::err_template_arg_template_params_mismatch); 3222 NextDiag = diag::note_template_parameter_pack_non_pack; 3223 } 3224 Diag(NewTTP->getLocation(), NextDiag) 3225 << 0 << NewTTP->isParameterPack(); 3226 Diag(OldTTP->getLocation(), diag::note_template_parameter_pack_here) 3227 << 0 << OldTTP->isParameterPack(); 3228 } 3229 return false; 3230 } 3231 } else if (NonTypeTemplateParmDecl *OldNTTP 3232 = dyn_cast<NonTypeTemplateParmDecl>(*OldParm)) { 3233 // The types of non-type template parameters must agree. 3234 NonTypeTemplateParmDecl *NewNTTP 3235 = cast<NonTypeTemplateParmDecl>(*NewParm); 3236 3237 // If we are matching a template template argument to a template 3238 // template parameter and one of the non-type template parameter types 3239 // is dependent, then we must wait until template instantiation time 3240 // to actually compare the arguments. 3241 if (Kind == TPL_TemplateTemplateArgumentMatch && 3242 (OldNTTP->getType()->isDependentType() || 3243 NewNTTP->getType()->isDependentType())) 3244 continue; 3245 3246 if (Context.getCanonicalType(OldNTTP->getType()) != 3247 Context.getCanonicalType(NewNTTP->getType())) { 3248 if (Complain) { 3249 unsigned NextDiag = diag::err_template_nontype_parm_different_type; 3250 if (TemplateArgLoc.isValid()) { 3251 Diag(TemplateArgLoc, 3252 diag::err_template_arg_template_params_mismatch); 3253 NextDiag = diag::note_template_nontype_parm_different_type; 3254 } 3255 Diag(NewNTTP->getLocation(), NextDiag) 3256 << NewNTTP->getType() 3257 << (Kind != TPL_TemplateMatch); 3258 Diag(OldNTTP->getLocation(), 3259 diag::note_template_nontype_parm_prev_declaration) 3260 << OldNTTP->getType(); 3261 } 3262 return false; 3263 } 3264 } else { 3265 // The template parameter lists of template template 3266 // parameters must agree. 3267 assert(isa<TemplateTemplateParmDecl>(*OldParm) && 3268 "Only template template parameters handled here"); 3269 TemplateTemplateParmDecl *OldTTP 3270 = cast<TemplateTemplateParmDecl>(*OldParm); 3271 TemplateTemplateParmDecl *NewTTP 3272 = cast<TemplateTemplateParmDecl>(*NewParm); 3273 if (!TemplateParameterListsAreEqual(NewTTP->getTemplateParameters(), 3274 OldTTP->getTemplateParameters(), 3275 Complain, 3276 (Kind == TPL_TemplateMatch? TPL_TemplateTemplateParmMatch : Kind), 3277 TemplateArgLoc)) 3278 return false; 3279 } 3280 } 3281 3282 return true; 3283} 3284 3285/// \brief Check whether a template can be declared within this scope. 3286/// 3287/// If the template declaration is valid in this scope, returns 3288/// false. Otherwise, issues a diagnostic and returns true. 3289bool 3290Sema::CheckTemplateDeclScope(Scope *S, TemplateParameterList *TemplateParams) { 3291 // Find the nearest enclosing declaration scope. 3292 while ((S->getFlags() & Scope::DeclScope) == 0 || 3293 (S->getFlags() & Scope::TemplateParamScope) != 0) 3294 S = S->getParent(); 3295 3296 // C++ [temp]p2: 3297 // A template-declaration can appear only as a namespace scope or 3298 // class scope declaration. 3299 DeclContext *Ctx = static_cast<DeclContext *>(S->getEntity()); 3300 if (Ctx && isa<LinkageSpecDecl>(Ctx) && 3301 cast<LinkageSpecDecl>(Ctx)->getLanguage() != LinkageSpecDecl::lang_cxx) 3302 return Diag(TemplateParams->getTemplateLoc(), diag::err_template_linkage) 3303 << TemplateParams->getSourceRange(); 3304 3305 while (Ctx && isa<LinkageSpecDecl>(Ctx)) 3306 Ctx = Ctx->getParent(); 3307 3308 if (Ctx && (Ctx->isFileContext() || Ctx->isRecord())) 3309 return false; 3310 3311 return Diag(TemplateParams->getTemplateLoc(), 3312 diag::err_template_outside_namespace_or_class_scope) 3313 << TemplateParams->getSourceRange(); 3314} 3315 3316/// \brief Determine what kind of template specialization the given declaration 3317/// is. 3318static TemplateSpecializationKind getTemplateSpecializationKind(NamedDecl *D) { 3319 if (!D) 3320 return TSK_Undeclared; 3321 3322 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) 3323 return Record->getTemplateSpecializationKind(); 3324 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) 3325 return Function->getTemplateSpecializationKind(); 3326 if (VarDecl *Var = dyn_cast<VarDecl>(D)) 3327 return Var->getTemplateSpecializationKind(); 3328 3329 return TSK_Undeclared; 3330} 3331 3332/// \brief Check whether a specialization is well-formed in the current 3333/// context. 3334/// 3335/// This routine determines whether a template specialization can be declared 3336/// in the current context (C++ [temp.expl.spec]p2). 3337/// 3338/// \param S the semantic analysis object for which this check is being 3339/// performed. 3340/// 3341/// \param Specialized the entity being specialized or instantiated, which 3342/// may be a kind of template (class template, function template, etc.) or 3343/// a member of a class template (member function, static data member, 3344/// member class). 3345/// 3346/// \param PrevDecl the previous declaration of this entity, if any. 3347/// 3348/// \param Loc the location of the explicit specialization or instantiation of 3349/// this entity. 3350/// 3351/// \param IsPartialSpecialization whether this is a partial specialization of 3352/// a class template. 3353/// 3354/// \returns true if there was an error that we cannot recover from, false 3355/// otherwise. 3356static bool CheckTemplateSpecializationScope(Sema &S, 3357 NamedDecl *Specialized, 3358 NamedDecl *PrevDecl, 3359 SourceLocation Loc, 3360 bool IsPartialSpecialization) { 3361 // Keep these "kind" numbers in sync with the %select statements in the 3362 // various diagnostics emitted by this routine. 3363 int EntityKind = 0; 3364 bool isTemplateSpecialization = false; 3365 if (isa<ClassTemplateDecl>(Specialized)) { 3366 EntityKind = IsPartialSpecialization? 1 : 0; 3367 isTemplateSpecialization = true; 3368 } else if (isa<FunctionTemplateDecl>(Specialized)) { 3369 EntityKind = 2; 3370 isTemplateSpecialization = true; 3371 } else if (isa<CXXMethodDecl>(Specialized)) 3372 EntityKind = 3; 3373 else if (isa<VarDecl>(Specialized)) 3374 EntityKind = 4; 3375 else if (isa<RecordDecl>(Specialized)) 3376 EntityKind = 5; 3377 else { 3378 S.Diag(Loc, diag::err_template_spec_unknown_kind); 3379 S.Diag(Specialized->getLocation(), diag::note_specialized_entity); 3380 return true; 3381 } 3382 3383 // C++ [temp.expl.spec]p2: 3384 // An explicit specialization shall be declared in the namespace 3385 // of which the template is a member, or, for member templates, in 3386 // the namespace of which the enclosing class or enclosing class 3387 // template is a member. An explicit specialization of a member 3388 // function, member class or static data member of a class 3389 // template shall be declared in the namespace of which the class 3390 // template is a member. Such a declaration may also be a 3391 // definition. If the declaration is not a definition, the 3392 // specialization may be defined later in the name- space in which 3393 // the explicit specialization was declared, or in a namespace 3394 // that encloses the one in which the explicit specialization was 3395 // declared. 3396 if (S.CurContext->getLookupContext()->isFunctionOrMethod()) { 3397 S.Diag(Loc, diag::err_template_spec_decl_function_scope) 3398 << Specialized; 3399 return true; 3400 } 3401 3402 if (S.CurContext->isRecord() && !IsPartialSpecialization) { 3403 S.Diag(Loc, diag::err_template_spec_decl_class_scope) 3404 << Specialized; 3405 return true; 3406 } 3407 3408 // C++ [temp.class.spec]p6: 3409 // A class template partial specialization may be declared or redeclared 3410 // in any namespace scope in which its definition may be defined (14.5.1 3411 // and 14.5.2). 3412 bool ComplainedAboutScope = false; 3413 DeclContext *SpecializedContext 3414 = Specialized->getDeclContext()->getEnclosingNamespaceContext(); 3415 DeclContext *DC = S.CurContext->getEnclosingNamespaceContext(); 3416 if ((!PrevDecl || 3417 getTemplateSpecializationKind(PrevDecl) == TSK_Undeclared || 3418 getTemplateSpecializationKind(PrevDecl) == TSK_ImplicitInstantiation)){ 3419 // There is no prior declaration of this entity, so this 3420 // specialization must be in the same context as the template 3421 // itself. 3422 if (!DC->Equals(SpecializedContext)) { 3423 if (isa<TranslationUnitDecl>(SpecializedContext)) 3424 S.Diag(Loc, diag::err_template_spec_decl_out_of_scope_global) 3425 << EntityKind << Specialized; 3426 else if (isa<NamespaceDecl>(SpecializedContext)) 3427 S.Diag(Loc, diag::err_template_spec_decl_out_of_scope) 3428 << EntityKind << Specialized 3429 << cast<NamedDecl>(SpecializedContext); 3430 3431 S.Diag(Specialized->getLocation(), diag::note_specialized_entity); 3432 ComplainedAboutScope = true; 3433 } 3434 } 3435 3436 // Make sure that this redeclaration (or definition) occurs in an enclosing 3437 // namespace. 3438 // Note that HandleDeclarator() performs this check for explicit 3439 // specializations of function templates, static data members, and member 3440 // functions, so we skip the check here for those kinds of entities. 3441 // FIXME: HandleDeclarator's diagnostics aren't quite as good, though. 3442 // Should we refactor that check, so that it occurs later? 3443 if (!ComplainedAboutScope && !DC->Encloses(SpecializedContext) && 3444 !(isa<FunctionTemplateDecl>(Specialized) || isa<VarDecl>(Specialized) || 3445 isa<FunctionDecl>(Specialized))) { 3446 if (isa<TranslationUnitDecl>(SpecializedContext)) 3447 S.Diag(Loc, diag::err_template_spec_redecl_global_scope) 3448 << EntityKind << Specialized; 3449 else if (isa<NamespaceDecl>(SpecializedContext)) 3450 S.Diag(Loc, diag::err_template_spec_redecl_out_of_scope) 3451 << EntityKind << Specialized 3452 << cast<NamedDecl>(SpecializedContext); 3453 3454 S.Diag(Specialized->getLocation(), diag::note_specialized_entity); 3455 } 3456 3457 // FIXME: check for specialization-after-instantiation errors and such. 3458 3459 return false; 3460} 3461 3462/// \brief Check the non-type template arguments of a class template 3463/// partial specialization according to C++ [temp.class.spec]p9. 3464/// 3465/// \param TemplateParams the template parameters of the primary class 3466/// template. 3467/// 3468/// \param TemplateArg the template arguments of the class template 3469/// partial specialization. 3470/// 3471/// \param MirrorsPrimaryTemplate will be set true if the class 3472/// template partial specialization arguments are identical to the 3473/// implicit template arguments of the primary template. This is not 3474/// necessarily an error (C++0x), and it is left to the caller to diagnose 3475/// this condition when it is an error. 3476/// 3477/// \returns true if there was an error, false otherwise. 3478bool Sema::CheckClassTemplatePartialSpecializationArgs( 3479 TemplateParameterList *TemplateParams, 3480 const TemplateArgumentListBuilder &TemplateArgs, 3481 bool &MirrorsPrimaryTemplate) { 3482 // FIXME: the interface to this function will have to change to 3483 // accommodate variadic templates. 3484 MirrorsPrimaryTemplate = true; 3485 3486 const TemplateArgument *ArgList = TemplateArgs.getFlatArguments(); 3487 3488 for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) { 3489 // Determine whether the template argument list of the partial 3490 // specialization is identical to the implicit argument list of 3491 // the primary template. The caller may need to diagnostic this as 3492 // an error per C++ [temp.class.spec]p9b3. 3493 if (MirrorsPrimaryTemplate) { 3494 if (TemplateTypeParmDecl *TTP 3495 = dyn_cast<TemplateTypeParmDecl>(TemplateParams->getParam(I))) { 3496 if (Context.getCanonicalType(Context.getTypeDeclType(TTP)) != 3497 Context.getCanonicalType(ArgList[I].getAsType())) 3498 MirrorsPrimaryTemplate = false; 3499 } else if (TemplateTemplateParmDecl *TTP 3500 = dyn_cast<TemplateTemplateParmDecl>( 3501 TemplateParams->getParam(I))) { 3502 TemplateName Name = ArgList[I].getAsTemplate(); 3503 TemplateTemplateParmDecl *ArgDecl 3504 = dyn_cast_or_null<TemplateTemplateParmDecl>(Name.getAsTemplateDecl()); 3505 if (!ArgDecl || 3506 ArgDecl->getIndex() != TTP->getIndex() || 3507 ArgDecl->getDepth() != TTP->getDepth()) 3508 MirrorsPrimaryTemplate = false; 3509 } 3510 } 3511 3512 NonTypeTemplateParmDecl *Param 3513 = dyn_cast<NonTypeTemplateParmDecl>(TemplateParams->getParam(I)); 3514 if (!Param) { 3515 continue; 3516 } 3517 3518 Expr *ArgExpr = ArgList[I].getAsExpr(); 3519 if (!ArgExpr) { 3520 MirrorsPrimaryTemplate = false; 3521 continue; 3522 } 3523 3524 // C++ [temp.class.spec]p8: 3525 // A non-type argument is non-specialized if it is the name of a 3526 // non-type parameter. All other non-type arguments are 3527 // specialized. 3528 // 3529 // Below, we check the two conditions that only apply to 3530 // specialized non-type arguments, so skip any non-specialized 3531 // arguments. 3532 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ArgExpr)) 3533 if (NonTypeTemplateParmDecl *NTTP 3534 = dyn_cast<NonTypeTemplateParmDecl>(DRE->getDecl())) { 3535 if (MirrorsPrimaryTemplate && 3536 (Param->getIndex() != NTTP->getIndex() || 3537 Param->getDepth() != NTTP->getDepth())) 3538 MirrorsPrimaryTemplate = false; 3539 3540 continue; 3541 } 3542 3543 // C++ [temp.class.spec]p9: 3544 // Within the argument list of a class template partial 3545 // specialization, the following restrictions apply: 3546 // -- A partially specialized non-type argument expression 3547 // shall not involve a template parameter of the partial 3548 // specialization except when the argument expression is a 3549 // simple identifier. 3550 if (ArgExpr->isTypeDependent() || ArgExpr->isValueDependent()) { 3551 Diag(ArgExpr->getLocStart(), 3552 diag::err_dependent_non_type_arg_in_partial_spec) 3553 << ArgExpr->getSourceRange(); 3554 return true; 3555 } 3556 3557 // -- The type of a template parameter corresponding to a 3558 // specialized non-type argument shall not be dependent on a 3559 // parameter of the specialization. 3560 if (Param->getType()->isDependentType()) { 3561 Diag(ArgExpr->getLocStart(), 3562 diag::err_dependent_typed_non_type_arg_in_partial_spec) 3563 << Param->getType() 3564 << ArgExpr->getSourceRange(); 3565 Diag(Param->getLocation(), diag::note_template_param_here); 3566 return true; 3567 } 3568 3569 MirrorsPrimaryTemplate = false; 3570 } 3571 3572 return false; 3573} 3574 3575/// \brief Retrieve the previous declaration of the given declaration. 3576static NamedDecl *getPreviousDecl(NamedDecl *ND) { 3577 if (VarDecl *VD = dyn_cast<VarDecl>(ND)) 3578 return VD->getPreviousDeclaration(); 3579 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) 3580 return FD->getPreviousDeclaration(); 3581 if (TagDecl *TD = dyn_cast<TagDecl>(ND)) 3582 return TD->getPreviousDeclaration(); 3583 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(ND)) 3584 return TD->getPreviousDeclaration(); 3585 if (FunctionTemplateDecl *FTD = dyn_cast<FunctionTemplateDecl>(ND)) 3586 return FTD->getPreviousDeclaration(); 3587 if (ClassTemplateDecl *CTD = dyn_cast<ClassTemplateDecl>(ND)) 3588 return CTD->getPreviousDeclaration(); 3589 return 0; 3590} 3591 3592Sema::DeclResult 3593Sema::ActOnClassTemplateSpecialization(Scope *S, unsigned TagSpec, 3594 TagUseKind TUK, 3595 SourceLocation KWLoc, 3596 CXXScopeSpec &SS, 3597 TemplateTy TemplateD, 3598 SourceLocation TemplateNameLoc, 3599 SourceLocation LAngleLoc, 3600 ASTTemplateArgsPtr TemplateArgsIn, 3601 SourceLocation RAngleLoc, 3602 AttributeList *Attr, 3603 MultiTemplateParamsArg TemplateParameterLists) { 3604 assert(TUK != TUK_Reference && "References are not specializations"); 3605 3606 // Find the class template we're specializing 3607 TemplateName Name = TemplateD.getAsVal<TemplateName>(); 3608 ClassTemplateDecl *ClassTemplate 3609 = dyn_cast_or_null<ClassTemplateDecl>(Name.getAsTemplateDecl()); 3610 3611 if (!ClassTemplate) { 3612 Diag(TemplateNameLoc, diag::err_not_class_template_specialization) 3613 << (Name.getAsTemplateDecl() && 3614 isa<TemplateTemplateParmDecl>(Name.getAsTemplateDecl())); 3615 return true; 3616 } 3617 3618 bool isExplicitSpecialization = false; 3619 bool isPartialSpecialization = false; 3620 3621 // Check the validity of the template headers that introduce this 3622 // template. 3623 // FIXME: We probably shouldn't complain about these headers for 3624 // friend declarations. 3625 TemplateParameterList *TemplateParams 3626 = MatchTemplateParametersToScopeSpecifier(TemplateNameLoc, SS, 3627 (TemplateParameterList**)TemplateParameterLists.get(), 3628 TemplateParameterLists.size(), 3629 TUK == TUK_Friend, 3630 isExplicitSpecialization); 3631 unsigned NumMatchedTemplateParamLists = TemplateParameterLists.size(); 3632 if (TemplateParams) 3633 --NumMatchedTemplateParamLists; 3634 3635 if (TemplateParams && TemplateParams->size() > 0) { 3636 isPartialSpecialization = true; 3637 3638 // C++ [temp.class.spec]p10: 3639 // The template parameter list of a specialization shall not 3640 // contain default template argument values. 3641 for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) { 3642 Decl *Param = TemplateParams->getParam(I); 3643 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) { 3644 if (TTP->hasDefaultArgument()) { 3645 Diag(TTP->getDefaultArgumentLoc(), 3646 diag::err_default_arg_in_partial_spec); 3647 TTP->removeDefaultArgument(); 3648 } 3649 } else if (NonTypeTemplateParmDecl *NTTP 3650 = dyn_cast<NonTypeTemplateParmDecl>(Param)) { 3651 if (Expr *DefArg = NTTP->getDefaultArgument()) { 3652 Diag(NTTP->getDefaultArgumentLoc(), 3653 diag::err_default_arg_in_partial_spec) 3654 << DefArg->getSourceRange(); 3655 NTTP->removeDefaultArgument(); 3656 DefArg->Destroy(Context); 3657 } 3658 } else { 3659 TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(Param); 3660 if (TTP->hasDefaultArgument()) { 3661 Diag(TTP->getDefaultArgument().getLocation(), 3662 diag::err_default_arg_in_partial_spec) 3663 << TTP->getDefaultArgument().getSourceRange(); 3664 TTP->removeDefaultArgument(); 3665 } 3666 } 3667 } 3668 } else if (TemplateParams) { 3669 if (TUK == TUK_Friend) 3670 Diag(KWLoc, diag::err_template_spec_friend) 3671 << FixItHint::CreateRemoval( 3672 SourceRange(TemplateParams->getTemplateLoc(), 3673 TemplateParams->getRAngleLoc())) 3674 << SourceRange(LAngleLoc, RAngleLoc); 3675 else 3676 isExplicitSpecialization = true; 3677 } else if (TUK != TUK_Friend) { 3678 Diag(KWLoc, diag::err_template_spec_needs_header) 3679 << FixItHint::CreateInsertion(KWLoc, "template<> "); 3680 isExplicitSpecialization = true; 3681 } 3682 3683 // Check that the specialization uses the same tag kind as the 3684 // original template. 3685 TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); 3686 assert(Kind != TTK_Enum && "Invalid enum tag in class template spec!"); 3687 if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(), 3688 Kind, KWLoc, 3689 *ClassTemplate->getIdentifier())) { 3690 Diag(KWLoc, diag::err_use_with_wrong_tag) 3691 << ClassTemplate 3692 << FixItHint::CreateReplacement(KWLoc, 3693 ClassTemplate->getTemplatedDecl()->getKindName()); 3694 Diag(ClassTemplate->getTemplatedDecl()->getLocation(), 3695 diag::note_previous_use); 3696 Kind = ClassTemplate->getTemplatedDecl()->getTagKind(); 3697 } 3698 3699 // Translate the parser's template argument list in our AST format. 3700 TemplateArgumentListInfo TemplateArgs; 3701 TemplateArgs.setLAngleLoc(LAngleLoc); 3702 TemplateArgs.setRAngleLoc(RAngleLoc); 3703 translateTemplateArguments(TemplateArgsIn, TemplateArgs); 3704 3705 // Check that the template argument list is well-formed for this 3706 // template. 3707 TemplateArgumentListBuilder Converted(ClassTemplate->getTemplateParameters(), 3708 TemplateArgs.size()); 3709 if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc, 3710 TemplateArgs, false, Converted)) 3711 return true; 3712 3713 assert((Converted.structuredSize() == 3714 ClassTemplate->getTemplateParameters()->size()) && 3715 "Converted template argument list is too short!"); 3716 3717 // Find the class template (partial) specialization declaration that 3718 // corresponds to these arguments. 3719 llvm::FoldingSetNodeID ID; 3720 if (isPartialSpecialization) { 3721 bool MirrorsPrimaryTemplate; 3722 if (CheckClassTemplatePartialSpecializationArgs( 3723 ClassTemplate->getTemplateParameters(), 3724 Converted, MirrorsPrimaryTemplate)) 3725 return true; 3726 3727 if (MirrorsPrimaryTemplate) { 3728 // C++ [temp.class.spec]p9b3: 3729 // 3730 // -- The argument list of the specialization shall not be identical 3731 // to the implicit argument list of the primary template. 3732 Diag(TemplateNameLoc, diag::err_partial_spec_args_match_primary_template) 3733 << (TUK == TUK_Definition) 3734 << FixItHint::CreateRemoval(SourceRange(LAngleLoc, RAngleLoc)); 3735 return CheckClassTemplate(S, TagSpec, TUK, KWLoc, SS, 3736 ClassTemplate->getIdentifier(), 3737 TemplateNameLoc, 3738 Attr, 3739 TemplateParams, 3740 AS_none); 3741 } 3742 3743 // FIXME: Diagnose friend partial specializations 3744 3745 if (!Name.isDependent() && 3746 !TemplateSpecializationType::anyDependentTemplateArguments( 3747 TemplateArgs.getArgumentArray(), 3748 TemplateArgs.size())) { 3749 Diag(TemplateNameLoc, diag::err_partial_spec_fully_specialized) 3750 << ClassTemplate->getDeclName(); 3751 isPartialSpecialization = false; 3752 } else { 3753 // FIXME: Template parameter list matters, too 3754 ClassTemplatePartialSpecializationDecl::Profile(ID, 3755 Converted.getFlatArguments(), 3756 Converted.flatSize(), 3757 Context); 3758 } 3759 } 3760 3761 if (!isPartialSpecialization) 3762 ClassTemplateSpecializationDecl::Profile(ID, 3763 Converted.getFlatArguments(), 3764 Converted.flatSize(), 3765 Context); 3766 void *InsertPos = 0; 3767 ClassTemplateSpecializationDecl *PrevDecl = 0; 3768 3769 if (isPartialSpecialization) 3770 PrevDecl 3771 = ClassTemplate->getPartialSpecializations().FindNodeOrInsertPos(ID, 3772 InsertPos); 3773 else 3774 PrevDecl 3775 = ClassTemplate->getSpecializations().FindNodeOrInsertPos(ID, InsertPos); 3776 3777 ClassTemplateSpecializationDecl *Specialization = 0; 3778 3779 // Check whether we can declare a class template specialization in 3780 // the current scope. 3781 if (TUK != TUK_Friend && 3782 CheckTemplateSpecializationScope(*this, ClassTemplate, PrevDecl, 3783 TemplateNameLoc, 3784 isPartialSpecialization)) 3785 return true; 3786 3787 // The canonical type 3788 QualType CanonType; 3789 if (PrevDecl && 3790 (PrevDecl->getSpecializationKind() == TSK_Undeclared || 3791 TUK == TUK_Friend)) { 3792 // Since the only prior class template specialization with these 3793 // arguments was referenced but not declared, or we're only 3794 // referencing this specialization as a friend, reuse that 3795 // declaration node as our own, updating its source location to 3796 // reflect our new declaration. 3797 Specialization = PrevDecl; 3798 Specialization->setLocation(TemplateNameLoc); 3799 PrevDecl = 0; 3800 CanonType = Context.getTypeDeclType(Specialization); 3801 } else if (isPartialSpecialization) { 3802 // Build the canonical type that describes the converted template 3803 // arguments of the class template partial specialization. 3804 TemplateName CanonTemplate = Context.getCanonicalTemplateName(Name); 3805 CanonType = Context.getTemplateSpecializationType(CanonTemplate, 3806 Converted.getFlatArguments(), 3807 Converted.flatSize()); 3808 3809 // Create a new class template partial specialization declaration node. 3810 ClassTemplatePartialSpecializationDecl *PrevPartial 3811 = cast_or_null<ClassTemplatePartialSpecializationDecl>(PrevDecl); 3812 unsigned SequenceNumber = PrevPartial? PrevPartial->getSequenceNumber() 3813 : ClassTemplate->getPartialSpecializations().size(); 3814 ClassTemplatePartialSpecializationDecl *Partial 3815 = ClassTemplatePartialSpecializationDecl::Create(Context, Kind, 3816 ClassTemplate->getDeclContext(), 3817 TemplateNameLoc, 3818 TemplateParams, 3819 ClassTemplate, 3820 Converted, 3821 TemplateArgs, 3822 CanonType, 3823 PrevPartial, 3824 SequenceNumber); 3825 SetNestedNameSpecifier(Partial, SS); 3826 if (NumMatchedTemplateParamLists > 0) { 3827 Partial->setTemplateParameterListsInfo(Context, 3828 NumMatchedTemplateParamLists, 3829 (TemplateParameterList**) TemplateParameterLists.release()); 3830 } 3831 3832 if (PrevPartial) { 3833 ClassTemplate->getPartialSpecializations().RemoveNode(PrevPartial); 3834 ClassTemplate->getPartialSpecializations().GetOrInsertNode(Partial); 3835 } else { 3836 ClassTemplate->getPartialSpecializations().InsertNode(Partial, InsertPos); 3837 } 3838 Specialization = Partial; 3839 3840 // If we are providing an explicit specialization of a member class 3841 // template specialization, make a note of that. 3842 if (PrevPartial && PrevPartial->getInstantiatedFromMember()) 3843 PrevPartial->setMemberSpecialization(); 3844 3845 // Check that all of the template parameters of the class template 3846 // partial specialization are deducible from the template 3847 // arguments. If not, this class template partial specialization 3848 // will never be used. 3849 llvm::SmallVector<bool, 8> DeducibleParams; 3850 DeducibleParams.resize(TemplateParams->size()); 3851 MarkUsedTemplateParameters(Partial->getTemplateArgs(), true, 3852 TemplateParams->getDepth(), 3853 DeducibleParams); 3854 unsigned NumNonDeducible = 0; 3855 for (unsigned I = 0, N = DeducibleParams.size(); I != N; ++I) 3856 if (!DeducibleParams[I]) 3857 ++NumNonDeducible; 3858 3859 if (NumNonDeducible) { 3860 Diag(TemplateNameLoc, diag::warn_partial_specs_not_deducible) 3861 << (NumNonDeducible > 1) 3862 << SourceRange(TemplateNameLoc, RAngleLoc); 3863 for (unsigned I = 0, N = DeducibleParams.size(); I != N; ++I) { 3864 if (!DeducibleParams[I]) { 3865 NamedDecl *Param = cast<NamedDecl>(TemplateParams->getParam(I)); 3866 if (Param->getDeclName()) 3867 Diag(Param->getLocation(), 3868 diag::note_partial_spec_unused_parameter) 3869 << Param->getDeclName(); 3870 else 3871 Diag(Param->getLocation(), 3872 diag::note_partial_spec_unused_parameter) 3873 << std::string("<anonymous>"); 3874 } 3875 } 3876 } 3877 } else { 3878 // Create a new class template specialization declaration node for 3879 // this explicit specialization or friend declaration. 3880 Specialization 3881 = ClassTemplateSpecializationDecl::Create(Context, Kind, 3882 ClassTemplate->getDeclContext(), 3883 TemplateNameLoc, 3884 ClassTemplate, 3885 Converted, 3886 PrevDecl); 3887 SetNestedNameSpecifier(Specialization, SS); 3888 if (NumMatchedTemplateParamLists > 0) { 3889 Specialization->setTemplateParameterListsInfo(Context, 3890 NumMatchedTemplateParamLists, 3891 (TemplateParameterList**) TemplateParameterLists.release()); 3892 } 3893 3894 if (PrevDecl) { 3895 ClassTemplate->getSpecializations().RemoveNode(PrevDecl); 3896 ClassTemplate->getSpecializations().GetOrInsertNode(Specialization); 3897 } else { 3898 ClassTemplate->getSpecializations().InsertNode(Specialization, 3899 InsertPos); 3900 } 3901 3902 CanonType = Context.getTypeDeclType(Specialization); 3903 } 3904 3905 // C++ [temp.expl.spec]p6: 3906 // If a template, a member template or the member of a class template is 3907 // explicitly specialized then that specialization shall be declared 3908 // before the first use of that specialization that would cause an implicit 3909 // instantiation to take place, in every translation unit in which such a 3910 // use occurs; no diagnostic is required. 3911 if (PrevDecl && PrevDecl->getPointOfInstantiation().isValid()) { 3912 bool Okay = false; 3913 for (NamedDecl *Prev = PrevDecl; Prev; Prev = getPreviousDecl(Prev)) { 3914 // Is there any previous explicit specialization declaration? 3915 if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) { 3916 Okay = true; 3917 break; 3918 } 3919 } 3920 3921 if (!Okay) { 3922 SourceRange Range(TemplateNameLoc, RAngleLoc); 3923 Diag(TemplateNameLoc, diag::err_specialization_after_instantiation) 3924 << Context.getTypeDeclType(Specialization) << Range; 3925 3926 Diag(PrevDecl->getPointOfInstantiation(), 3927 diag::note_instantiation_required_here) 3928 << (PrevDecl->getTemplateSpecializationKind() 3929 != TSK_ImplicitInstantiation); 3930 return true; 3931 } 3932 } 3933 3934 // If this is not a friend, note that this is an explicit specialization. 3935 if (TUK != TUK_Friend) 3936 Specialization->setSpecializationKind(TSK_ExplicitSpecialization); 3937 3938 // Check that this isn't a redefinition of this specialization. 3939 if (TUK == TUK_Definition) { 3940 if (RecordDecl *Def = Specialization->getDefinition()) { 3941 SourceRange Range(TemplateNameLoc, RAngleLoc); 3942 Diag(TemplateNameLoc, diag::err_redefinition) 3943 << Context.getTypeDeclType(Specialization) << Range; 3944 Diag(Def->getLocation(), diag::note_previous_definition); 3945 Specialization->setInvalidDecl(); 3946 return true; 3947 } 3948 } 3949 3950 // Build the fully-sugared type for this class template 3951 // specialization as the user wrote in the specialization 3952 // itself. This means that we'll pretty-print the type retrieved 3953 // from the specialization's declaration the way that the user 3954 // actually wrote the specialization, rather than formatting the 3955 // name based on the "canonical" representation used to store the 3956 // template arguments in the specialization. 3957 TypeSourceInfo *WrittenTy 3958 = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc, 3959 TemplateArgs, CanonType); 3960 if (TUK != TUK_Friend) { 3961 Specialization->setTypeAsWritten(WrittenTy); 3962 if (TemplateParams) 3963 Specialization->setTemplateKeywordLoc(TemplateParams->getTemplateLoc()); 3964 } 3965 TemplateArgsIn.release(); 3966 3967 // C++ [temp.expl.spec]p9: 3968 // A template explicit specialization is in the scope of the 3969 // namespace in which the template was defined. 3970 // 3971 // We actually implement this paragraph where we set the semantic 3972 // context (in the creation of the ClassTemplateSpecializationDecl), 3973 // but we also maintain the lexical context where the actual 3974 // definition occurs. 3975 Specialization->setLexicalDeclContext(CurContext); 3976 3977 // We may be starting the definition of this specialization. 3978 if (TUK == TUK_Definition) 3979 Specialization->startDefinition(); 3980 3981 if (TUK == TUK_Friend) { 3982 FriendDecl *Friend = FriendDecl::Create(Context, CurContext, 3983 TemplateNameLoc, 3984 WrittenTy, 3985 /*FIXME:*/KWLoc); 3986 Friend->setAccess(AS_public); 3987 CurContext->addDecl(Friend); 3988 } else { 3989 // Add the specialization into its lexical context, so that it can 3990 // be seen when iterating through the list of declarations in that 3991 // context. However, specializations are not found by name lookup. 3992 CurContext->addDecl(Specialization); 3993 } 3994 return DeclPtrTy::make(Specialization); 3995} 3996 3997Sema::DeclPtrTy 3998Sema::ActOnTemplateDeclarator(Scope *S, 3999 MultiTemplateParamsArg TemplateParameterLists, 4000 Declarator &D) { 4001 return HandleDeclarator(S, D, move(TemplateParameterLists), false); 4002} 4003 4004Sema::DeclPtrTy 4005Sema::ActOnStartOfFunctionTemplateDef(Scope *FnBodyScope, 4006 MultiTemplateParamsArg TemplateParameterLists, 4007 Declarator &D) { 4008 assert(getCurFunctionDecl() == 0 && "Function parsing confused"); 4009 assert(D.getTypeObject(0).Kind == DeclaratorChunk::Function && 4010 "Not a function declarator!"); 4011 DeclaratorChunk::FunctionTypeInfo &FTI = D.getTypeObject(0).Fun; 4012 4013 if (FTI.hasPrototype) { 4014 // FIXME: Diagnose arguments without names in C. 4015 } 4016 4017 Scope *ParentScope = FnBodyScope->getParent(); 4018 4019 DeclPtrTy DP = HandleDeclarator(ParentScope, D, 4020 move(TemplateParameterLists), 4021 /*IsFunctionDefinition=*/true); 4022 if (FunctionTemplateDecl *FunctionTemplate 4023 = dyn_cast_or_null<FunctionTemplateDecl>(DP.getAs<Decl>())) 4024 return ActOnStartOfFunctionDef(FnBodyScope, 4025 DeclPtrTy::make(FunctionTemplate->getTemplatedDecl())); 4026 if (FunctionDecl *Function = dyn_cast_or_null<FunctionDecl>(DP.getAs<Decl>())) 4027 return ActOnStartOfFunctionDef(FnBodyScope, DeclPtrTy::make(Function)); 4028 return DeclPtrTy(); 4029} 4030 4031/// \brief Strips various properties off an implicit instantiation 4032/// that has just been explicitly specialized. 4033static void StripImplicitInstantiation(NamedDecl *D) { 4034 D->invalidateAttrs(); 4035 4036 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { 4037 FD->setInlineSpecified(false); 4038 } 4039} 4040 4041/// \brief Diagnose cases where we have an explicit template specialization 4042/// before/after an explicit template instantiation, producing diagnostics 4043/// for those cases where they are required and determining whether the 4044/// new specialization/instantiation will have any effect. 4045/// 4046/// \param NewLoc the location of the new explicit specialization or 4047/// instantiation. 4048/// 4049/// \param NewTSK the kind of the new explicit specialization or instantiation. 4050/// 4051/// \param PrevDecl the previous declaration of the entity. 4052/// 4053/// \param PrevTSK the kind of the old explicit specialization or instantiatin. 4054/// 4055/// \param PrevPointOfInstantiation if valid, indicates where the previus 4056/// declaration was instantiated (either implicitly or explicitly). 4057/// 4058/// \param HasNoEffect will be set to true to indicate that the new 4059/// specialization or instantiation has no effect and should be ignored. 4060/// 4061/// \returns true if there was an error that should prevent the introduction of 4062/// the new declaration into the AST, false otherwise. 4063bool 4064Sema::CheckSpecializationInstantiationRedecl(SourceLocation NewLoc, 4065 TemplateSpecializationKind NewTSK, 4066 NamedDecl *PrevDecl, 4067 TemplateSpecializationKind PrevTSK, 4068 SourceLocation PrevPointOfInstantiation, 4069 bool &HasNoEffect) { 4070 HasNoEffect = false; 4071 4072 switch (NewTSK) { 4073 case TSK_Undeclared: 4074 case TSK_ImplicitInstantiation: 4075 assert(false && "Don't check implicit instantiations here"); 4076 return false; 4077 4078 case TSK_ExplicitSpecialization: 4079 switch (PrevTSK) { 4080 case TSK_Undeclared: 4081 case TSK_ExplicitSpecialization: 4082 // Okay, we're just specializing something that is either already 4083 // explicitly specialized or has merely been mentioned without any 4084 // instantiation. 4085 return false; 4086 4087 case TSK_ImplicitInstantiation: 4088 if (PrevPointOfInstantiation.isInvalid()) { 4089 // The declaration itself has not actually been instantiated, so it is 4090 // still okay to specialize it. 4091 StripImplicitInstantiation(PrevDecl); 4092 return false; 4093 } 4094 // Fall through 4095 4096 case TSK_ExplicitInstantiationDeclaration: 4097 case TSK_ExplicitInstantiationDefinition: 4098 assert((PrevTSK == TSK_ImplicitInstantiation || 4099 PrevPointOfInstantiation.isValid()) && 4100 "Explicit instantiation without point of instantiation?"); 4101 4102 // C++ [temp.expl.spec]p6: 4103 // If a template, a member template or the member of a class template 4104 // is explicitly specialized then that specialization shall be declared 4105 // before the first use of that specialization that would cause an 4106 // implicit instantiation to take place, in every translation unit in 4107 // which such a use occurs; no diagnostic is required. 4108 for (NamedDecl *Prev = PrevDecl; Prev; Prev = getPreviousDecl(Prev)) { 4109 // Is there any previous explicit specialization declaration? 4110 if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) 4111 return false; 4112 } 4113 4114 Diag(NewLoc, diag::err_specialization_after_instantiation) 4115 << PrevDecl; 4116 Diag(PrevPointOfInstantiation, diag::note_instantiation_required_here) 4117 << (PrevTSK != TSK_ImplicitInstantiation); 4118 4119 return true; 4120 } 4121 break; 4122 4123 case TSK_ExplicitInstantiationDeclaration: 4124 switch (PrevTSK) { 4125 case TSK_ExplicitInstantiationDeclaration: 4126 // This explicit instantiation declaration is redundant (that's okay). 4127 HasNoEffect = true; 4128 return false; 4129 4130 case TSK_Undeclared: 4131 case TSK_ImplicitInstantiation: 4132 // We're explicitly instantiating something that may have already been 4133 // implicitly instantiated; that's fine. 4134 return false; 4135 4136 case TSK_ExplicitSpecialization: 4137 // C++0x [temp.explicit]p4: 4138 // For a given set of template parameters, if an explicit instantiation 4139 // of a template appears after a declaration of an explicit 4140 // specialization for that template, the explicit instantiation has no 4141 // effect. 4142 HasNoEffect = true; 4143 return false; 4144 4145 case TSK_ExplicitInstantiationDefinition: 4146 // C++0x [temp.explicit]p10: 4147 // If an entity is the subject of both an explicit instantiation 4148 // declaration and an explicit instantiation definition in the same 4149 // translation unit, the definition shall follow the declaration. 4150 Diag(NewLoc, 4151 diag::err_explicit_instantiation_declaration_after_definition); 4152 Diag(PrevPointOfInstantiation, 4153 diag::note_explicit_instantiation_definition_here); 4154 assert(PrevPointOfInstantiation.isValid() && 4155 "Explicit instantiation without point of instantiation?"); 4156 HasNoEffect = true; 4157 return false; 4158 } 4159 break; 4160 4161 case TSK_ExplicitInstantiationDefinition: 4162 switch (PrevTSK) { 4163 case TSK_Undeclared: 4164 case TSK_ImplicitInstantiation: 4165 // We're explicitly instantiating something that may have already been 4166 // implicitly instantiated; that's fine. 4167 return false; 4168 4169 case TSK_ExplicitSpecialization: 4170 // C++ DR 259, C++0x [temp.explicit]p4: 4171 // For a given set of template parameters, if an explicit 4172 // instantiation of a template appears after a declaration of 4173 // an explicit specialization for that template, the explicit 4174 // instantiation has no effect. 4175 // 4176 // In C++98/03 mode, we only give an extension warning here, because it 4177 // is not harmful to try to explicitly instantiate something that 4178 // has been explicitly specialized. 4179 if (!getLangOptions().CPlusPlus0x) { 4180 Diag(NewLoc, diag::ext_explicit_instantiation_after_specialization) 4181 << PrevDecl; 4182 Diag(PrevDecl->getLocation(), 4183 diag::note_previous_template_specialization); 4184 } 4185 HasNoEffect = true; 4186 return false; 4187 4188 case TSK_ExplicitInstantiationDeclaration: 4189 // We're explicity instantiating a definition for something for which we 4190 // were previously asked to suppress instantiations. That's fine. 4191 return false; 4192 4193 case TSK_ExplicitInstantiationDefinition: 4194 // C++0x [temp.spec]p5: 4195 // For a given template and a given set of template-arguments, 4196 // - an explicit instantiation definition shall appear at most once 4197 // in a program, 4198 Diag(NewLoc, diag::err_explicit_instantiation_duplicate) 4199 << PrevDecl; 4200 Diag(PrevPointOfInstantiation, 4201 diag::note_previous_explicit_instantiation); 4202 HasNoEffect = true; 4203 return false; 4204 } 4205 break; 4206 } 4207 4208 assert(false && "Missing specialization/instantiation case?"); 4209 4210 return false; 4211} 4212 4213/// \brief Perform semantic analysis for the given dependent function 4214/// template specialization. The only possible way to get a dependent 4215/// function template specialization is with a friend declaration, 4216/// like so: 4217/// 4218/// template <class T> void foo(T); 4219/// template <class T> class A { 4220/// friend void foo<>(T); 4221/// }; 4222/// 4223/// There really isn't any useful analysis we can do here, so we 4224/// just store the information. 4225bool 4226Sema::CheckDependentFunctionTemplateSpecialization(FunctionDecl *FD, 4227 const TemplateArgumentListInfo &ExplicitTemplateArgs, 4228 LookupResult &Previous) { 4229 // Remove anything from Previous that isn't a function template in 4230 // the correct context. 4231 DeclContext *FDLookupContext = FD->getDeclContext()->getLookupContext(); 4232 LookupResult::Filter F = Previous.makeFilter(); 4233 while (F.hasNext()) { 4234 NamedDecl *D = F.next()->getUnderlyingDecl(); 4235 if (!isa<FunctionTemplateDecl>(D) || 4236 !FDLookupContext->Equals(D->getDeclContext()->getLookupContext())) 4237 F.erase(); 4238 } 4239 F.done(); 4240 4241 // Should this be diagnosed here? 4242 if (Previous.empty()) return true; 4243 4244 FD->setDependentTemplateSpecialization(Context, Previous.asUnresolvedSet(), 4245 ExplicitTemplateArgs); 4246 return false; 4247} 4248 4249/// \brief Perform semantic analysis for the given function template 4250/// specialization. 4251/// 4252/// This routine performs all of the semantic analysis required for an 4253/// explicit function template specialization. On successful completion, 4254/// the function declaration \p FD will become a function template 4255/// specialization. 4256/// 4257/// \param FD the function declaration, which will be updated to become a 4258/// function template specialization. 4259/// 4260/// \param ExplicitTemplateArgs the explicitly-provided template arguments, 4261/// if any. Note that this may be valid info even when 0 arguments are 4262/// explicitly provided as in, e.g., \c void sort<>(char*, char*); 4263/// as it anyway contains info on the angle brackets locations. 4264/// 4265/// \param PrevDecl the set of declarations that may be specialized by 4266/// this function specialization. 4267bool 4268Sema::CheckFunctionTemplateSpecialization(FunctionDecl *FD, 4269 const TemplateArgumentListInfo *ExplicitTemplateArgs, 4270 LookupResult &Previous) { 4271 // The set of function template specializations that could match this 4272 // explicit function template specialization. 4273 UnresolvedSet<8> Candidates; 4274 4275 DeclContext *FDLookupContext = FD->getDeclContext()->getLookupContext(); 4276 for (LookupResult::iterator I = Previous.begin(), E = Previous.end(); 4277 I != E; ++I) { 4278 NamedDecl *Ovl = (*I)->getUnderlyingDecl(); 4279 if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Ovl)) { 4280 // Only consider templates found within the same semantic lookup scope as 4281 // FD. 4282 if (!FDLookupContext->Equals(Ovl->getDeclContext()->getLookupContext())) 4283 continue; 4284 4285 // C++ [temp.expl.spec]p11: 4286 // A trailing template-argument can be left unspecified in the 4287 // template-id naming an explicit function template specialization 4288 // provided it can be deduced from the function argument type. 4289 // Perform template argument deduction to determine whether we may be 4290 // specializing this template. 4291 // FIXME: It is somewhat wasteful to build 4292 TemplateDeductionInfo Info(Context, FD->getLocation()); 4293 FunctionDecl *Specialization = 0; 4294 if (TemplateDeductionResult TDK 4295 = DeduceTemplateArguments(FunTmpl, ExplicitTemplateArgs, 4296 FD->getType(), 4297 Specialization, 4298 Info)) { 4299 // FIXME: Template argument deduction failed; record why it failed, so 4300 // that we can provide nifty diagnostics. 4301 (void)TDK; 4302 continue; 4303 } 4304 4305 // Record this candidate. 4306 Candidates.addDecl(Specialization, I.getAccess()); 4307 } 4308 } 4309 4310 // Find the most specialized function template. 4311 UnresolvedSetIterator Result 4312 = getMostSpecialized(Candidates.begin(), Candidates.end(), 4313 TPOC_Other, FD->getLocation(), 4314 PDiag(diag::err_function_template_spec_no_match) 4315 << FD->getDeclName(), 4316 PDiag(diag::err_function_template_spec_ambiguous) 4317 << FD->getDeclName() << (ExplicitTemplateArgs != 0), 4318 PDiag(diag::note_function_template_spec_matched)); 4319 if (Result == Candidates.end()) 4320 return true; 4321 4322 // Ignore access information; it doesn't figure into redeclaration checking. 4323 FunctionDecl *Specialization = cast<FunctionDecl>(*Result); 4324 Specialization->setLocation(FD->getLocation()); 4325 4326 // FIXME: Check if the prior specialization has a point of instantiation. 4327 // If so, we have run afoul of . 4328 4329 // If this is a friend declaration, then we're not really declaring 4330 // an explicit specialization. 4331 bool isFriend = (FD->getFriendObjectKind() != Decl::FOK_None); 4332 4333 // Check the scope of this explicit specialization. 4334 if (!isFriend && 4335 CheckTemplateSpecializationScope(*this, 4336 Specialization->getPrimaryTemplate(), 4337 Specialization, FD->getLocation(), 4338 false)) 4339 return true; 4340 4341 // C++ [temp.expl.spec]p6: 4342 // If a template, a member template or the member of a class template is 4343 // explicitly specialized then that specialization shall be declared 4344 // before the first use of that specialization that would cause an implicit 4345 // instantiation to take place, in every translation unit in which such a 4346 // use occurs; no diagnostic is required. 4347 FunctionTemplateSpecializationInfo *SpecInfo 4348 = Specialization->getTemplateSpecializationInfo(); 4349 assert(SpecInfo && "Function template specialization info missing?"); 4350 4351 bool HasNoEffect = false; 4352 if (!isFriend && 4353 CheckSpecializationInstantiationRedecl(FD->getLocation(), 4354 TSK_ExplicitSpecialization, 4355 Specialization, 4356 SpecInfo->getTemplateSpecializationKind(), 4357 SpecInfo->getPointOfInstantiation(), 4358 HasNoEffect)) 4359 return true; 4360 4361 // Mark the prior declaration as an explicit specialization, so that later 4362 // clients know that this is an explicit specialization. 4363 if (!isFriend) 4364 SpecInfo->setTemplateSpecializationKind(TSK_ExplicitSpecialization); 4365 4366 // Turn the given function declaration into a function template 4367 // specialization, with the template arguments from the previous 4368 // specialization. 4369 // Take copies of (semantic and syntactic) template argument lists. 4370 const TemplateArgumentList* TemplArgs = new (Context) 4371 TemplateArgumentList(Specialization->getTemplateSpecializationArgs()); 4372 const TemplateArgumentListInfo* TemplArgsAsWritten = ExplicitTemplateArgs 4373 ? new (Context) TemplateArgumentListInfo(*ExplicitTemplateArgs) : 0; 4374 FD->setFunctionTemplateSpecialization(Specialization->getPrimaryTemplate(), 4375 TemplArgs, /*InsertPos=*/0, 4376 SpecInfo->getTemplateSpecializationKind(), 4377 TemplArgsAsWritten); 4378 4379 // The "previous declaration" for this function template specialization is 4380 // the prior function template specialization. 4381 Previous.clear(); 4382 Previous.addDecl(Specialization); 4383 return false; 4384} 4385 4386/// \brief Perform semantic analysis for the given non-template member 4387/// specialization. 4388/// 4389/// This routine performs all of the semantic analysis required for an 4390/// explicit member function specialization. On successful completion, 4391/// the function declaration \p FD will become a member function 4392/// specialization. 4393/// 4394/// \param Member the member declaration, which will be updated to become a 4395/// specialization. 4396/// 4397/// \param Previous the set of declarations, one of which may be specialized 4398/// by this function specialization; the set will be modified to contain the 4399/// redeclared member. 4400bool 4401Sema::CheckMemberSpecialization(NamedDecl *Member, LookupResult &Previous) { 4402 assert(!isa<TemplateDecl>(Member) && "Only for non-template members"); 4403 4404 // Try to find the member we are instantiating. 4405 NamedDecl *Instantiation = 0; 4406 NamedDecl *InstantiatedFrom = 0; 4407 MemberSpecializationInfo *MSInfo = 0; 4408 4409 if (Previous.empty()) { 4410 // Nowhere to look anyway. 4411 } else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Member)) { 4412 for (LookupResult::iterator I = Previous.begin(), E = Previous.end(); 4413 I != E; ++I) { 4414 NamedDecl *D = (*I)->getUnderlyingDecl(); 4415 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) { 4416 if (Context.hasSameType(Function->getType(), Method->getType())) { 4417 Instantiation = Method; 4418 InstantiatedFrom = Method->getInstantiatedFromMemberFunction(); 4419 MSInfo = Method->getMemberSpecializationInfo(); 4420 break; 4421 } 4422 } 4423 } 4424 } else if (isa<VarDecl>(Member)) { 4425 VarDecl *PrevVar; 4426 if (Previous.isSingleResult() && 4427 (PrevVar = dyn_cast<VarDecl>(Previous.getFoundDecl()))) 4428 if (PrevVar->isStaticDataMember()) { 4429 Instantiation = PrevVar; 4430 InstantiatedFrom = PrevVar->getInstantiatedFromStaticDataMember(); 4431 MSInfo = PrevVar->getMemberSpecializationInfo(); 4432 } 4433 } else if (isa<RecordDecl>(Member)) { 4434 CXXRecordDecl *PrevRecord; 4435 if (Previous.isSingleResult() && 4436 (PrevRecord = dyn_cast<CXXRecordDecl>(Previous.getFoundDecl()))) { 4437 Instantiation = PrevRecord; 4438 InstantiatedFrom = PrevRecord->getInstantiatedFromMemberClass(); 4439 MSInfo = PrevRecord->getMemberSpecializationInfo(); 4440 } 4441 } 4442 4443 if (!Instantiation) { 4444 // There is no previous declaration that matches. Since member 4445 // specializations are always out-of-line, the caller will complain about 4446 // this mismatch later. 4447 return false; 4448 } 4449 4450 // If this is a friend, just bail out here before we start turning 4451 // things into explicit specializations. 4452 if (Member->getFriendObjectKind() != Decl::FOK_None) { 4453 // Preserve instantiation information. 4454 if (InstantiatedFrom && isa<CXXMethodDecl>(Member)) { 4455 cast<CXXMethodDecl>(Member)->setInstantiationOfMemberFunction( 4456 cast<CXXMethodDecl>(InstantiatedFrom), 4457 cast<CXXMethodDecl>(Instantiation)->getTemplateSpecializationKind()); 4458 } else if (InstantiatedFrom && isa<CXXRecordDecl>(Member)) { 4459 cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass( 4460 cast<CXXRecordDecl>(InstantiatedFrom), 4461 cast<CXXRecordDecl>(Instantiation)->getTemplateSpecializationKind()); 4462 } 4463 4464 Previous.clear(); 4465 Previous.addDecl(Instantiation); 4466 return false; 4467 } 4468 4469 // Make sure that this is a specialization of a member. 4470 if (!InstantiatedFrom) { 4471 Diag(Member->getLocation(), diag::err_spec_member_not_instantiated) 4472 << Member; 4473 Diag(Instantiation->getLocation(), diag::note_specialized_decl); 4474 return true; 4475 } 4476 4477 // C++ [temp.expl.spec]p6: 4478 // If a template, a member template or the member of a class template is 4479 // explicitly specialized then that spe- cialization shall be declared 4480 // before the first use of that specialization that would cause an implicit 4481 // instantiation to take place, in every translation unit in which such a 4482 // use occurs; no diagnostic is required. 4483 assert(MSInfo && "Member specialization info missing?"); 4484 4485 bool HasNoEffect = false; 4486 if (CheckSpecializationInstantiationRedecl(Member->getLocation(), 4487 TSK_ExplicitSpecialization, 4488 Instantiation, 4489 MSInfo->getTemplateSpecializationKind(), 4490 MSInfo->getPointOfInstantiation(), 4491 HasNoEffect)) 4492 return true; 4493 4494 // Check the scope of this explicit specialization. 4495 if (CheckTemplateSpecializationScope(*this, 4496 InstantiatedFrom, 4497 Instantiation, Member->getLocation(), 4498 false)) 4499 return true; 4500 4501 // Note that this is an explicit instantiation of a member. 4502 // the original declaration to note that it is an explicit specialization 4503 // (if it was previously an implicit instantiation). This latter step 4504 // makes bookkeeping easier. 4505 if (isa<FunctionDecl>(Member)) { 4506 FunctionDecl *InstantiationFunction = cast<FunctionDecl>(Instantiation); 4507 if (InstantiationFunction->getTemplateSpecializationKind() == 4508 TSK_ImplicitInstantiation) { 4509 InstantiationFunction->setTemplateSpecializationKind( 4510 TSK_ExplicitSpecialization); 4511 InstantiationFunction->setLocation(Member->getLocation()); 4512 } 4513 4514 cast<FunctionDecl>(Member)->setInstantiationOfMemberFunction( 4515 cast<CXXMethodDecl>(InstantiatedFrom), 4516 TSK_ExplicitSpecialization); 4517 } else if (isa<VarDecl>(Member)) { 4518 VarDecl *InstantiationVar = cast<VarDecl>(Instantiation); 4519 if (InstantiationVar->getTemplateSpecializationKind() == 4520 TSK_ImplicitInstantiation) { 4521 InstantiationVar->setTemplateSpecializationKind( 4522 TSK_ExplicitSpecialization); 4523 InstantiationVar->setLocation(Member->getLocation()); 4524 } 4525 4526 Context.setInstantiatedFromStaticDataMember(cast<VarDecl>(Member), 4527 cast<VarDecl>(InstantiatedFrom), 4528 TSK_ExplicitSpecialization); 4529 } else { 4530 assert(isa<CXXRecordDecl>(Member) && "Only member classes remain"); 4531 CXXRecordDecl *InstantiationClass = cast<CXXRecordDecl>(Instantiation); 4532 if (InstantiationClass->getTemplateSpecializationKind() == 4533 TSK_ImplicitInstantiation) { 4534 InstantiationClass->setTemplateSpecializationKind( 4535 TSK_ExplicitSpecialization); 4536 InstantiationClass->setLocation(Member->getLocation()); 4537 } 4538 4539 cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass( 4540 cast<CXXRecordDecl>(InstantiatedFrom), 4541 TSK_ExplicitSpecialization); 4542 } 4543 4544 // Save the caller the trouble of having to figure out which declaration 4545 // this specialization matches. 4546 Previous.clear(); 4547 Previous.addDecl(Instantiation); 4548 return false; 4549} 4550 4551/// \brief Check the scope of an explicit instantiation. 4552static void CheckExplicitInstantiationScope(Sema &S, NamedDecl *D, 4553 SourceLocation InstLoc, 4554 bool WasQualifiedName) { 4555 DeclContext *ExpectedContext 4556 = D->getDeclContext()->getEnclosingNamespaceContext()->getLookupContext(); 4557 DeclContext *CurContext = S.CurContext->getLookupContext(); 4558 4559 // C++0x [temp.explicit]p2: 4560 // An explicit instantiation shall appear in an enclosing namespace of its 4561 // template. 4562 // 4563 // This is DR275, which we do not retroactively apply to C++98/03. 4564 if (S.getLangOptions().CPlusPlus0x && 4565 !CurContext->Encloses(ExpectedContext)) { 4566 if (NamespaceDecl *NS = dyn_cast<NamespaceDecl>(ExpectedContext)) 4567 S.Diag(InstLoc, 4568 S.getLangOptions().CPlusPlus0x? 4569 diag::err_explicit_instantiation_out_of_scope 4570 : diag::warn_explicit_instantiation_out_of_scope_0x) 4571 << D << NS; 4572 else 4573 S.Diag(InstLoc, 4574 S.getLangOptions().CPlusPlus0x? 4575 diag::err_explicit_instantiation_must_be_global 4576 : diag::warn_explicit_instantiation_out_of_scope_0x) 4577 << D; 4578 S.Diag(D->getLocation(), diag::note_explicit_instantiation_here); 4579 return; 4580 } 4581 4582 // C++0x [temp.explicit]p2: 4583 // If the name declared in the explicit instantiation is an unqualified 4584 // name, the explicit instantiation shall appear in the namespace where 4585 // its template is declared or, if that namespace is inline (7.3.1), any 4586 // namespace from its enclosing namespace set. 4587 if (WasQualifiedName) 4588 return; 4589 4590 if (CurContext->Equals(ExpectedContext)) 4591 return; 4592 4593 S.Diag(InstLoc, 4594 S.getLangOptions().CPlusPlus0x? 4595 diag::err_explicit_instantiation_unqualified_wrong_namespace 4596 : diag::warn_explicit_instantiation_unqualified_wrong_namespace_0x) 4597 << D << ExpectedContext; 4598 S.Diag(D->getLocation(), diag::note_explicit_instantiation_here); 4599} 4600 4601/// \brief Determine whether the given scope specifier has a template-id in it. 4602static bool ScopeSpecifierHasTemplateId(const CXXScopeSpec &SS) { 4603 if (!SS.isSet()) 4604 return false; 4605 4606 // C++0x [temp.explicit]p2: 4607 // If the explicit instantiation is for a member function, a member class 4608 // or a static data member of a class template specialization, the name of 4609 // the class template specialization in the qualified-id for the member 4610 // name shall be a simple-template-id. 4611 // 4612 // C++98 has the same restriction, just worded differently. 4613 for (NestedNameSpecifier *NNS = (NestedNameSpecifier *)SS.getScopeRep(); 4614 NNS; NNS = NNS->getPrefix()) 4615 if (Type *T = NNS->getAsType()) 4616 if (isa<TemplateSpecializationType>(T)) 4617 return true; 4618 4619 return false; 4620} 4621 4622// Explicit instantiation of a class template specialization 4623Sema::DeclResult 4624Sema::ActOnExplicitInstantiation(Scope *S, 4625 SourceLocation ExternLoc, 4626 SourceLocation TemplateLoc, 4627 unsigned TagSpec, 4628 SourceLocation KWLoc, 4629 const CXXScopeSpec &SS, 4630 TemplateTy TemplateD, 4631 SourceLocation TemplateNameLoc, 4632 SourceLocation LAngleLoc, 4633 ASTTemplateArgsPtr TemplateArgsIn, 4634 SourceLocation RAngleLoc, 4635 AttributeList *Attr) { 4636 // Find the class template we're specializing 4637 TemplateName Name = TemplateD.getAsVal<TemplateName>(); 4638 ClassTemplateDecl *ClassTemplate 4639 = cast<ClassTemplateDecl>(Name.getAsTemplateDecl()); 4640 4641 // Check that the specialization uses the same tag kind as the 4642 // original template. 4643 TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); 4644 assert(Kind != TTK_Enum && 4645 "Invalid enum tag in class template explicit instantiation!"); 4646 if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(), 4647 Kind, KWLoc, 4648 *ClassTemplate->getIdentifier())) { 4649 Diag(KWLoc, diag::err_use_with_wrong_tag) 4650 << ClassTemplate 4651 << FixItHint::CreateReplacement(KWLoc, 4652 ClassTemplate->getTemplatedDecl()->getKindName()); 4653 Diag(ClassTemplate->getTemplatedDecl()->getLocation(), 4654 diag::note_previous_use); 4655 Kind = ClassTemplate->getTemplatedDecl()->getTagKind(); 4656 } 4657 4658 // C++0x [temp.explicit]p2: 4659 // There are two forms of explicit instantiation: an explicit instantiation 4660 // definition and an explicit instantiation declaration. An explicit 4661 // instantiation declaration begins with the extern keyword. [...] 4662 TemplateSpecializationKind TSK 4663 = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition 4664 : TSK_ExplicitInstantiationDeclaration; 4665 4666 // Translate the parser's template argument list in our AST format. 4667 TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); 4668 translateTemplateArguments(TemplateArgsIn, TemplateArgs); 4669 4670 // Check that the template argument list is well-formed for this 4671 // template. 4672 TemplateArgumentListBuilder Converted(ClassTemplate->getTemplateParameters(), 4673 TemplateArgs.size()); 4674 if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc, 4675 TemplateArgs, false, Converted)) 4676 return true; 4677 4678 assert((Converted.structuredSize() == 4679 ClassTemplate->getTemplateParameters()->size()) && 4680 "Converted template argument list is too short!"); 4681 4682 // Find the class template specialization declaration that 4683 // corresponds to these arguments. 4684 llvm::FoldingSetNodeID ID; 4685 ClassTemplateSpecializationDecl::Profile(ID, 4686 Converted.getFlatArguments(), 4687 Converted.flatSize(), 4688 Context); 4689 void *InsertPos = 0; 4690 ClassTemplateSpecializationDecl *PrevDecl 4691 = ClassTemplate->getSpecializations().FindNodeOrInsertPos(ID, InsertPos); 4692 4693 TemplateSpecializationKind PrevDecl_TSK 4694 = PrevDecl ? PrevDecl->getTemplateSpecializationKind() : TSK_Undeclared; 4695 4696 // C++0x [temp.explicit]p2: 4697 // [...] An explicit instantiation shall appear in an enclosing 4698 // namespace of its template. [...] 4699 // 4700 // This is C++ DR 275. 4701 CheckExplicitInstantiationScope(*this, ClassTemplate, TemplateNameLoc, 4702 SS.isSet()); 4703 4704 ClassTemplateSpecializationDecl *Specialization = 0; 4705 4706 bool ReusedDecl = false; 4707 bool HasNoEffect = false; 4708 if (PrevDecl) { 4709 if (CheckSpecializationInstantiationRedecl(TemplateNameLoc, TSK, 4710 PrevDecl, PrevDecl_TSK, 4711 PrevDecl->getPointOfInstantiation(), 4712 HasNoEffect)) 4713 return DeclPtrTy::make(PrevDecl); 4714 4715 // Even though HasNoEffect == true means that this explicit instantiation 4716 // has no effect on semantics, we go on to put its syntax in the AST. 4717 4718 if (PrevDecl_TSK == TSK_ImplicitInstantiation || 4719 PrevDecl_TSK == TSK_Undeclared) { 4720 // Since the only prior class template specialization with these 4721 // arguments was referenced but not declared, reuse that 4722 // declaration node as our own, updating the source location 4723 // for the template name to reflect our new declaration. 4724 // (Other source locations will be updated later.) 4725 Specialization = PrevDecl; 4726 Specialization->setLocation(TemplateNameLoc); 4727 PrevDecl = 0; 4728 ReusedDecl = true; 4729 } 4730 } 4731 4732 if (!Specialization) { 4733 // Create a new class template specialization declaration node for 4734 // this explicit specialization. 4735 Specialization 4736 = ClassTemplateSpecializationDecl::Create(Context, Kind, 4737 ClassTemplate->getDeclContext(), 4738 TemplateNameLoc, 4739 ClassTemplate, 4740 Converted, PrevDecl); 4741 SetNestedNameSpecifier(Specialization, SS); 4742 4743 if (!HasNoEffect) { 4744 if (PrevDecl) { 4745 // Remove the previous declaration from the folding set, since we want 4746 // to introduce a new declaration. 4747 ClassTemplate->getSpecializations().RemoveNode(PrevDecl); 4748 ClassTemplate->getSpecializations().FindNodeOrInsertPos(ID, InsertPos); 4749 } 4750 // Insert the new specialization. 4751 ClassTemplate->getSpecializations().InsertNode(Specialization, InsertPos); 4752 } 4753 } 4754 4755 // Build the fully-sugared type for this explicit instantiation as 4756 // the user wrote in the explicit instantiation itself. This means 4757 // that we'll pretty-print the type retrieved from the 4758 // specialization's declaration the way that the user actually wrote 4759 // the explicit instantiation, rather than formatting the name based 4760 // on the "canonical" representation used to store the template 4761 // arguments in the specialization. 4762 TypeSourceInfo *WrittenTy 4763 = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc, 4764 TemplateArgs, 4765 Context.getTypeDeclType(Specialization)); 4766 Specialization->setTypeAsWritten(WrittenTy); 4767 TemplateArgsIn.release(); 4768 4769 // Set source locations for keywords. 4770 Specialization->setExternLoc(ExternLoc); 4771 Specialization->setTemplateKeywordLoc(TemplateLoc); 4772 4773 // Add the explicit instantiation into its lexical context. However, 4774 // since explicit instantiations are never found by name lookup, we 4775 // just put it into the declaration context directly. 4776 Specialization->setLexicalDeclContext(CurContext); 4777 CurContext->addDecl(Specialization); 4778 4779 // Syntax is now OK, so return if it has no other effect on semantics. 4780 if (HasNoEffect) { 4781 // Set the template specialization kind. 4782 Specialization->setTemplateSpecializationKind(TSK); 4783 return DeclPtrTy::make(Specialization); 4784 } 4785 4786 // C++ [temp.explicit]p3: 4787 // A definition of a class template or class member template 4788 // shall be in scope at the point of the explicit instantiation of 4789 // the class template or class member template. 4790 // 4791 // This check comes when we actually try to perform the 4792 // instantiation. 4793 ClassTemplateSpecializationDecl *Def 4794 = cast_or_null<ClassTemplateSpecializationDecl>( 4795 Specialization->getDefinition()); 4796 if (!Def) 4797 InstantiateClassTemplateSpecialization(TemplateNameLoc, Specialization, TSK); 4798 else if (TSK == TSK_ExplicitInstantiationDefinition) { 4799 MarkVTableUsed(TemplateNameLoc, Specialization, true); 4800 Specialization->setPointOfInstantiation(Def->getPointOfInstantiation()); 4801 } 4802 4803 // Instantiate the members of this class template specialization. 4804 Def = cast_or_null<ClassTemplateSpecializationDecl>( 4805 Specialization->getDefinition()); 4806 if (Def) { 4807 TemplateSpecializationKind Old_TSK = Def->getTemplateSpecializationKind(); 4808 4809 // Fix a TSK_ExplicitInstantiationDeclaration followed by a 4810 // TSK_ExplicitInstantiationDefinition 4811 if (Old_TSK == TSK_ExplicitInstantiationDeclaration && 4812 TSK == TSK_ExplicitInstantiationDefinition) 4813 Def->setTemplateSpecializationKind(TSK); 4814 4815 InstantiateClassTemplateSpecializationMembers(TemplateNameLoc, Def, TSK); 4816 } 4817 4818 // Set the template specialization kind. 4819 Specialization->setTemplateSpecializationKind(TSK); 4820 return DeclPtrTy::make(Specialization); 4821} 4822 4823// Explicit instantiation of a member class of a class template. 4824Sema::DeclResult 4825Sema::ActOnExplicitInstantiation(Scope *S, 4826 SourceLocation ExternLoc, 4827 SourceLocation TemplateLoc, 4828 unsigned TagSpec, 4829 SourceLocation KWLoc, 4830 CXXScopeSpec &SS, 4831 IdentifierInfo *Name, 4832 SourceLocation NameLoc, 4833 AttributeList *Attr) { 4834 4835 bool Owned = false; 4836 bool IsDependent = false; 4837 DeclPtrTy TagD = ActOnTag(S, TagSpec, Action::TUK_Reference, 4838 KWLoc, SS, Name, NameLoc, Attr, AS_none, 4839 MultiTemplateParamsArg(*this, 0, 0), 4840 Owned, IsDependent); 4841 assert(!IsDependent && "explicit instantiation of dependent name not yet handled"); 4842 4843 if (!TagD) 4844 return true; 4845 4846 TagDecl *Tag = cast<TagDecl>(TagD.getAs<Decl>()); 4847 if (Tag->isEnum()) { 4848 Diag(TemplateLoc, diag::err_explicit_instantiation_enum) 4849 << Context.getTypeDeclType(Tag); 4850 return true; 4851 } 4852 4853 if (Tag->isInvalidDecl()) 4854 return true; 4855 4856 CXXRecordDecl *Record = cast<CXXRecordDecl>(Tag); 4857 CXXRecordDecl *Pattern = Record->getInstantiatedFromMemberClass(); 4858 if (!Pattern) { 4859 Diag(TemplateLoc, diag::err_explicit_instantiation_nontemplate_type) 4860 << Context.getTypeDeclType(Record); 4861 Diag(Record->getLocation(), diag::note_nontemplate_decl_here); 4862 return true; 4863 } 4864 4865 // C++0x [temp.explicit]p2: 4866 // If the explicit instantiation is for a class or member class, the 4867 // elaborated-type-specifier in the declaration shall include a 4868 // simple-template-id. 4869 // 4870 // C++98 has the same restriction, just worded differently. 4871 if (!ScopeSpecifierHasTemplateId(SS)) 4872 Diag(TemplateLoc, diag::ext_explicit_instantiation_without_qualified_id) 4873 << Record << SS.getRange(); 4874 4875 // C++0x [temp.explicit]p2: 4876 // There are two forms of explicit instantiation: an explicit instantiation 4877 // definition and an explicit instantiation declaration. An explicit 4878 // instantiation declaration begins with the extern keyword. [...] 4879 TemplateSpecializationKind TSK 4880 = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition 4881 : TSK_ExplicitInstantiationDeclaration; 4882 4883 // C++0x [temp.explicit]p2: 4884 // [...] An explicit instantiation shall appear in an enclosing 4885 // namespace of its template. [...] 4886 // 4887 // This is C++ DR 275. 4888 CheckExplicitInstantiationScope(*this, Record, NameLoc, true); 4889 4890 // Verify that it is okay to explicitly instantiate here. 4891 CXXRecordDecl *PrevDecl 4892 = cast_or_null<CXXRecordDecl>(Record->getPreviousDeclaration()); 4893 if (!PrevDecl && Record->getDefinition()) 4894 PrevDecl = Record; 4895 if (PrevDecl) { 4896 MemberSpecializationInfo *MSInfo = PrevDecl->getMemberSpecializationInfo(); 4897 bool HasNoEffect = false; 4898 assert(MSInfo && "No member specialization information?"); 4899 if (CheckSpecializationInstantiationRedecl(TemplateLoc, TSK, 4900 PrevDecl, 4901 MSInfo->getTemplateSpecializationKind(), 4902 MSInfo->getPointOfInstantiation(), 4903 HasNoEffect)) 4904 return true; 4905 if (HasNoEffect) 4906 return TagD; 4907 } 4908 4909 CXXRecordDecl *RecordDef 4910 = cast_or_null<CXXRecordDecl>(Record->getDefinition()); 4911 if (!RecordDef) { 4912 // C++ [temp.explicit]p3: 4913 // A definition of a member class of a class template shall be in scope 4914 // at the point of an explicit instantiation of the member class. 4915 CXXRecordDecl *Def 4916 = cast_or_null<CXXRecordDecl>(Pattern->getDefinition()); 4917 if (!Def) { 4918 Diag(TemplateLoc, diag::err_explicit_instantiation_undefined_member) 4919 << 0 << Record->getDeclName() << Record->getDeclContext(); 4920 Diag(Pattern->getLocation(), diag::note_forward_declaration) 4921 << Pattern; 4922 return true; 4923 } else { 4924 if (InstantiateClass(NameLoc, Record, Def, 4925 getTemplateInstantiationArgs(Record), 4926 TSK)) 4927 return true; 4928 4929 RecordDef = cast_or_null<CXXRecordDecl>(Record->getDefinition()); 4930 if (!RecordDef) 4931 return true; 4932 } 4933 } 4934 4935 // Instantiate all of the members of the class. 4936 InstantiateClassMembers(NameLoc, RecordDef, 4937 getTemplateInstantiationArgs(Record), TSK); 4938 4939 if (TSK == TSK_ExplicitInstantiationDefinition) 4940 MarkVTableUsed(NameLoc, RecordDef, true); 4941 4942 // FIXME: We don't have any representation for explicit instantiations of 4943 // member classes. Such a representation is not needed for compilation, but it 4944 // should be available for clients that want to see all of the declarations in 4945 // the source code. 4946 return TagD; 4947} 4948 4949Sema::DeclResult Sema::ActOnExplicitInstantiation(Scope *S, 4950 SourceLocation ExternLoc, 4951 SourceLocation TemplateLoc, 4952 Declarator &D) { 4953 // Explicit instantiations always require a name. 4954 DeclarationName Name = GetNameForDeclarator(D); 4955 if (!Name) { 4956 if (!D.isInvalidType()) 4957 Diag(D.getDeclSpec().getSourceRange().getBegin(), 4958 diag::err_explicit_instantiation_requires_name) 4959 << D.getDeclSpec().getSourceRange() 4960 << D.getSourceRange(); 4961 4962 return true; 4963 } 4964 4965 // The scope passed in may not be a decl scope. Zip up the scope tree until 4966 // we find one that is. 4967 while ((S->getFlags() & Scope::DeclScope) == 0 || 4968 (S->getFlags() & Scope::TemplateParamScope) != 0) 4969 S = S->getParent(); 4970 4971 // Determine the type of the declaration. 4972 TypeSourceInfo *T = GetTypeForDeclarator(D, S); 4973 QualType R = T->getType(); 4974 if (R.isNull()) 4975 return true; 4976 4977 if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef) { 4978 // Cannot explicitly instantiate a typedef. 4979 Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_of_typedef) 4980 << Name; 4981 return true; 4982 } 4983 4984 // C++0x [temp.explicit]p1: 4985 // [...] An explicit instantiation of a function template shall not use the 4986 // inline or constexpr specifiers. 4987 // Presumably, this also applies to member functions of class templates as 4988 // well. 4989 if (D.getDeclSpec().isInlineSpecified() && getLangOptions().CPlusPlus0x) 4990 Diag(D.getDeclSpec().getInlineSpecLoc(), 4991 diag::err_explicit_instantiation_inline) 4992 <<FixItHint::CreateRemoval(D.getDeclSpec().getInlineSpecLoc()); 4993 4994 // FIXME: check for constexpr specifier. 4995 4996 // C++0x [temp.explicit]p2: 4997 // There are two forms of explicit instantiation: an explicit instantiation 4998 // definition and an explicit instantiation declaration. An explicit 4999 // instantiation declaration begins with the extern keyword. [...] 5000 TemplateSpecializationKind TSK 5001 = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition 5002 : TSK_ExplicitInstantiationDeclaration; 5003 5004 LookupResult Previous(*this, Name, D.getIdentifierLoc(), LookupOrdinaryName); 5005 LookupParsedName(Previous, S, &D.getCXXScopeSpec()); 5006 5007 if (!R->isFunctionType()) { 5008 // C++ [temp.explicit]p1: 5009 // A [...] static data member of a class template can be explicitly 5010 // instantiated from the member definition associated with its class 5011 // template. 5012 if (Previous.isAmbiguous()) 5013 return true; 5014 5015 VarDecl *Prev = Previous.getAsSingle<VarDecl>(); 5016 if (!Prev || !Prev->isStaticDataMember()) { 5017 // We expect to see a data data member here. 5018 Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_not_known) 5019 << Name; 5020 for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end(); 5021 P != PEnd; ++P) 5022 Diag((*P)->getLocation(), diag::note_explicit_instantiation_here); 5023 return true; 5024 } 5025 5026 if (!Prev->getInstantiatedFromStaticDataMember()) { 5027 // FIXME: Check for explicit specialization? 5028 Diag(D.getIdentifierLoc(), 5029 diag::err_explicit_instantiation_data_member_not_instantiated) 5030 << Prev; 5031 Diag(Prev->getLocation(), diag::note_explicit_instantiation_here); 5032 // FIXME: Can we provide a note showing where this was declared? 5033 return true; 5034 } 5035 5036 // C++0x [temp.explicit]p2: 5037 // If the explicit instantiation is for a member function, a member class 5038 // or a static data member of a class template specialization, the name of 5039 // the class template specialization in the qualified-id for the member 5040 // name shall be a simple-template-id. 5041 // 5042 // C++98 has the same restriction, just worded differently. 5043 if (!ScopeSpecifierHasTemplateId(D.getCXXScopeSpec())) 5044 Diag(D.getIdentifierLoc(), 5045 diag::ext_explicit_instantiation_without_qualified_id) 5046 << Prev << D.getCXXScopeSpec().getRange(); 5047 5048 // Check the scope of this explicit instantiation. 5049 CheckExplicitInstantiationScope(*this, Prev, D.getIdentifierLoc(), true); 5050 5051 // Verify that it is okay to explicitly instantiate here. 5052 MemberSpecializationInfo *MSInfo = Prev->getMemberSpecializationInfo(); 5053 assert(MSInfo && "Missing static data member specialization info?"); 5054 bool HasNoEffect = false; 5055 if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK, Prev, 5056 MSInfo->getTemplateSpecializationKind(), 5057 MSInfo->getPointOfInstantiation(), 5058 HasNoEffect)) 5059 return true; 5060 if (HasNoEffect) 5061 return DeclPtrTy(); 5062 5063 // Instantiate static data member. 5064 Prev->setTemplateSpecializationKind(TSK, D.getIdentifierLoc()); 5065 if (TSK == TSK_ExplicitInstantiationDefinition) 5066 InstantiateStaticDataMemberDefinition(D.getIdentifierLoc(), Prev, false, 5067 /*DefinitionRequired=*/true); 5068 5069 // FIXME: Create an ExplicitInstantiation node? 5070 return DeclPtrTy(); 5071 } 5072 5073 // If the declarator is a template-id, translate the parser's template 5074 // argument list into our AST format. 5075 bool HasExplicitTemplateArgs = false; 5076 TemplateArgumentListInfo TemplateArgs; 5077 if (D.getName().getKind() == UnqualifiedId::IK_TemplateId) { 5078 TemplateIdAnnotation *TemplateId = D.getName().TemplateId; 5079 TemplateArgs.setLAngleLoc(TemplateId->LAngleLoc); 5080 TemplateArgs.setRAngleLoc(TemplateId->RAngleLoc); 5081 ASTTemplateArgsPtr TemplateArgsPtr(*this, 5082 TemplateId->getTemplateArgs(), 5083 TemplateId->NumArgs); 5084 translateTemplateArguments(TemplateArgsPtr, TemplateArgs); 5085 HasExplicitTemplateArgs = true; 5086 TemplateArgsPtr.release(); 5087 } 5088 5089 // C++ [temp.explicit]p1: 5090 // A [...] function [...] can be explicitly instantiated from its template. 5091 // A member function [...] of a class template can be explicitly 5092 // instantiated from the member definition associated with its class 5093 // template. 5094 UnresolvedSet<8> Matches; 5095 for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end(); 5096 P != PEnd; ++P) { 5097 NamedDecl *Prev = *P; 5098 if (!HasExplicitTemplateArgs) { 5099 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Prev)) { 5100 if (Context.hasSameUnqualifiedType(Method->getType(), R)) { 5101 Matches.clear(); 5102 5103 Matches.addDecl(Method, P.getAccess()); 5104 if (Method->getTemplateSpecializationKind() == TSK_Undeclared) 5105 break; 5106 } 5107 } 5108 } 5109 5110 FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Prev); 5111 if (!FunTmpl) 5112 continue; 5113 5114 TemplateDeductionInfo Info(Context, D.getIdentifierLoc()); 5115 FunctionDecl *Specialization = 0; 5116 if (TemplateDeductionResult TDK 5117 = DeduceTemplateArguments(FunTmpl, 5118 (HasExplicitTemplateArgs ? &TemplateArgs : 0), 5119 R, Specialization, Info)) { 5120 // FIXME: Keep track of almost-matches? 5121 (void)TDK; 5122 continue; 5123 } 5124 5125 Matches.addDecl(Specialization, P.getAccess()); 5126 } 5127 5128 // Find the most specialized function template specialization. 5129 UnresolvedSetIterator Result 5130 = getMostSpecialized(Matches.begin(), Matches.end(), TPOC_Other, 5131 D.getIdentifierLoc(), 5132 PDiag(diag::err_explicit_instantiation_not_known) << Name, 5133 PDiag(diag::err_explicit_instantiation_ambiguous) << Name, 5134 PDiag(diag::note_explicit_instantiation_candidate)); 5135 5136 if (Result == Matches.end()) 5137 return true; 5138 5139 // Ignore access control bits, we don't need them for redeclaration checking. 5140 FunctionDecl *Specialization = cast<FunctionDecl>(*Result); 5141 5142 if (Specialization->getTemplateSpecializationKind() == TSK_Undeclared) { 5143 Diag(D.getIdentifierLoc(), 5144 diag::err_explicit_instantiation_member_function_not_instantiated) 5145 << Specialization 5146 << (Specialization->getTemplateSpecializationKind() == 5147 TSK_ExplicitSpecialization); 5148 Diag(Specialization->getLocation(), diag::note_explicit_instantiation_here); 5149 return true; 5150 } 5151 5152 FunctionDecl *PrevDecl = Specialization->getPreviousDeclaration(); 5153 if (!PrevDecl && Specialization->isThisDeclarationADefinition()) 5154 PrevDecl = Specialization; 5155 5156 if (PrevDecl) { 5157 bool HasNoEffect = false; 5158 if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK, 5159 PrevDecl, 5160 PrevDecl->getTemplateSpecializationKind(), 5161 PrevDecl->getPointOfInstantiation(), 5162 HasNoEffect)) 5163 return true; 5164 5165 // FIXME: We may still want to build some representation of this 5166 // explicit specialization. 5167 if (HasNoEffect) 5168 return DeclPtrTy(); 5169 } 5170 5171 Specialization->setTemplateSpecializationKind(TSK, D.getIdentifierLoc()); 5172 5173 if (TSK == TSK_ExplicitInstantiationDefinition) 5174 InstantiateFunctionDefinition(D.getIdentifierLoc(), Specialization, 5175 false, /*DefinitionRequired=*/true); 5176 5177 // C++0x [temp.explicit]p2: 5178 // If the explicit instantiation is for a member function, a member class 5179 // or a static data member of a class template specialization, the name of 5180 // the class template specialization in the qualified-id for the member 5181 // name shall be a simple-template-id. 5182 // 5183 // C++98 has the same restriction, just worded differently. 5184 FunctionTemplateDecl *FunTmpl = Specialization->getPrimaryTemplate(); 5185 if (D.getName().getKind() != UnqualifiedId::IK_TemplateId && !FunTmpl && 5186 D.getCXXScopeSpec().isSet() && 5187 !ScopeSpecifierHasTemplateId(D.getCXXScopeSpec())) 5188 Diag(D.getIdentifierLoc(), 5189 diag::ext_explicit_instantiation_without_qualified_id) 5190 << Specialization << D.getCXXScopeSpec().getRange(); 5191 5192 CheckExplicitInstantiationScope(*this, 5193 FunTmpl? (NamedDecl *)FunTmpl 5194 : Specialization->getInstantiatedFromMemberFunction(), 5195 D.getIdentifierLoc(), 5196 D.getCXXScopeSpec().isSet()); 5197 5198 // FIXME: Create some kind of ExplicitInstantiationDecl here. 5199 return DeclPtrTy(); 5200} 5201 5202Sema::TypeResult 5203Sema::ActOnDependentTag(Scope *S, unsigned TagSpec, TagUseKind TUK, 5204 const CXXScopeSpec &SS, IdentifierInfo *Name, 5205 SourceLocation TagLoc, SourceLocation NameLoc) { 5206 // This has to hold, because SS is expected to be defined. 5207 assert(Name && "Expected a name in a dependent tag"); 5208 5209 NestedNameSpecifier *NNS 5210 = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); 5211 if (!NNS) 5212 return true; 5213 5214 TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); 5215 5216 if (TUK == TUK_Declaration || TUK == TUK_Definition) { 5217 Diag(NameLoc, diag::err_dependent_tag_decl) 5218 << (TUK == TUK_Definition) << Kind << SS.getRange(); 5219 return true; 5220 } 5221 5222 ElaboratedTypeKeyword Kwd = TypeWithKeyword::getKeywordForTagTypeKind(Kind); 5223 return Context.getDependentNameType(Kwd, NNS, Name).getAsOpaquePtr(); 5224} 5225 5226Sema::TypeResult 5227Sema::ActOnTypenameType(Scope *S, SourceLocation TypenameLoc, 5228 const CXXScopeSpec &SS, const IdentifierInfo &II, 5229 SourceLocation IdLoc) { 5230 NestedNameSpecifier *NNS 5231 = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); 5232 if (!NNS) 5233 return true; 5234 5235 if (TypenameLoc.isValid() && S && !S->getTemplateParamParent() && 5236 !getLangOptions().CPlusPlus0x) 5237 Diag(TypenameLoc, diag::ext_typename_outside_of_template) 5238 << FixItHint::CreateRemoval(TypenameLoc); 5239 5240 QualType T = CheckTypenameType(ETK_Typename, NNS, II, 5241 TypenameLoc, SS.getRange(), IdLoc); 5242 if (T.isNull()) 5243 return true; 5244 5245 TypeSourceInfo *TSI = Context.CreateTypeSourceInfo(T); 5246 if (isa<DependentNameType>(T)) { 5247 DependentNameTypeLoc TL = cast<DependentNameTypeLoc>(TSI->getTypeLoc()); 5248 TL.setKeywordLoc(TypenameLoc); 5249 TL.setQualifierRange(SS.getRange()); 5250 TL.setNameLoc(IdLoc); 5251 } else { 5252 ElaboratedTypeLoc TL = cast<ElaboratedTypeLoc>(TSI->getTypeLoc()); 5253 TL.setKeywordLoc(TypenameLoc); 5254 TL.setQualifierRange(SS.getRange()); 5255 cast<TypeSpecTypeLoc>(TL.getNamedTypeLoc()).setNameLoc(IdLoc); 5256 } 5257 5258 return CreateLocInfoType(T, TSI).getAsOpaquePtr(); 5259} 5260 5261Sema::TypeResult 5262Sema::ActOnTypenameType(Scope *S, SourceLocation TypenameLoc, 5263 const CXXScopeSpec &SS, SourceLocation TemplateLoc, 5264 TypeTy *Ty) { 5265 if (TypenameLoc.isValid() && S && !S->getTemplateParamParent() && 5266 !getLangOptions().CPlusPlus0x) 5267 Diag(TypenameLoc, diag::ext_typename_outside_of_template) 5268 << FixItHint::CreateRemoval(TypenameLoc); 5269 5270 TypeSourceInfo *InnerTSI = 0; 5271 QualType T = GetTypeFromParser(Ty, &InnerTSI); 5272 NestedNameSpecifier *NNS 5273 = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); 5274 5275 assert(isa<TemplateSpecializationType>(T) && 5276 "Expected a template specialization type"); 5277 5278 if (computeDeclContext(SS, false)) { 5279 // If we can compute a declaration context, then the "typename" 5280 // keyword was superfluous. Just build an ElaboratedType to keep 5281 // track of the nested-name-specifier. 5282 5283 // Push the inner type, preserving its source locations if possible. 5284 TypeLocBuilder Builder; 5285 if (InnerTSI) 5286 Builder.pushFullCopy(InnerTSI->getTypeLoc()); 5287 else 5288 Builder.push<TemplateSpecializationTypeLoc>(T).initialize(TemplateLoc); 5289 5290 T = Context.getElaboratedType(ETK_Typename, NNS, T); 5291 ElaboratedTypeLoc TL = Builder.push<ElaboratedTypeLoc>(T); 5292 TL.setKeywordLoc(TypenameLoc); 5293 TL.setQualifierRange(SS.getRange()); 5294 5295 TypeSourceInfo *TSI = Builder.getTypeSourceInfo(Context, T); 5296 return CreateLocInfoType(T, TSI).getAsOpaquePtr(); 5297 } 5298 5299 // TODO: it's really silly that we make a template specialization 5300 // type earlier only to drop it again here. 5301 TemplateSpecializationType *TST = cast<TemplateSpecializationType>(T); 5302 DependentTemplateName *DTN = 5303 TST->getTemplateName().getAsDependentTemplateName(); 5304 assert(DTN && "dependent template has non-dependent name?"); 5305 T = Context.getDependentTemplateSpecializationType(ETK_Typename, NNS, 5306 DTN->getIdentifier(), 5307 TST->getNumArgs(), 5308 TST->getArgs()); 5309 TypeSourceInfo *TSI = Context.CreateTypeSourceInfo(T); 5310 DependentTemplateSpecializationTypeLoc TL = 5311 cast<DependentTemplateSpecializationTypeLoc>(TSI->getTypeLoc()); 5312 if (InnerTSI) { 5313 TemplateSpecializationTypeLoc TSTL = 5314 cast<TemplateSpecializationTypeLoc>(InnerTSI->getTypeLoc()); 5315 TL.setLAngleLoc(TSTL.getLAngleLoc()); 5316 TL.setRAngleLoc(TSTL.getRAngleLoc()); 5317 for (unsigned I = 0, E = TST->getNumArgs(); I != E; ++I) 5318 TL.setArgLocInfo(I, TSTL.getArgLocInfo(I)); 5319 } else { 5320 TL.initializeLocal(SourceLocation()); 5321 } 5322 TL.setKeywordLoc(TypenameLoc); 5323 TL.setQualifierRange(SS.getRange()); 5324 return CreateLocInfoType(T, TSI).getAsOpaquePtr(); 5325} 5326 5327/// \brief Build the type that describes a C++ typename specifier, 5328/// e.g., "typename T::type". 5329QualType 5330Sema::CheckTypenameType(ElaboratedTypeKeyword Keyword, 5331 NestedNameSpecifier *NNS, const IdentifierInfo &II, 5332 SourceLocation KeywordLoc, SourceRange NNSRange, 5333 SourceLocation IILoc) { 5334 CXXScopeSpec SS; 5335 SS.setScopeRep(NNS); 5336 SS.setRange(NNSRange); 5337 5338 DeclContext *Ctx = computeDeclContext(SS); 5339 if (!Ctx) { 5340 // If the nested-name-specifier is dependent and couldn't be 5341 // resolved to a type, build a typename type. 5342 assert(NNS->isDependent()); 5343 return Context.getDependentNameType(Keyword, NNS, &II); 5344 } 5345 5346 // If the nested-name-specifier refers to the current instantiation, 5347 // the "typename" keyword itself is superfluous. In C++03, the 5348 // program is actually ill-formed. However, DR 382 (in C++0x CD1) 5349 // allows such extraneous "typename" keywords, and we retroactively 5350 // apply this DR to C++03 code with only a warning. In any case we continue. 5351 5352 if (RequireCompleteDeclContext(SS, Ctx)) 5353 return QualType(); 5354 5355 DeclarationName Name(&II); 5356 LookupResult Result(*this, Name, IILoc, LookupOrdinaryName); 5357 LookupQualifiedName(Result, Ctx); 5358 unsigned DiagID = 0; 5359 Decl *Referenced = 0; 5360 switch (Result.getResultKind()) { 5361 case LookupResult::NotFound: 5362 DiagID = diag::err_typename_nested_not_found; 5363 break; 5364 5365 case LookupResult::NotFoundInCurrentInstantiation: 5366 // Okay, it's a member of an unknown instantiation. 5367 return Context.getDependentNameType(Keyword, NNS, &II); 5368 5369 case LookupResult::Found: 5370 if (TypeDecl *Type = dyn_cast<TypeDecl>(Result.getFoundDecl())) { 5371 // We found a type. Build an ElaboratedType, since the 5372 // typename-specifier was just sugar. 5373 return Context.getElaboratedType(ETK_Typename, NNS, 5374 Context.getTypeDeclType(Type)); 5375 } 5376 5377 DiagID = diag::err_typename_nested_not_type; 5378 Referenced = Result.getFoundDecl(); 5379 break; 5380 5381 case LookupResult::FoundUnresolvedValue: 5382 llvm_unreachable("unresolved using decl in non-dependent context"); 5383 return QualType(); 5384 5385 case LookupResult::FoundOverloaded: 5386 DiagID = diag::err_typename_nested_not_type; 5387 Referenced = *Result.begin(); 5388 break; 5389 5390 case LookupResult::Ambiguous: 5391 return QualType(); 5392 } 5393 5394 // If we get here, it's because name lookup did not find a 5395 // type. Emit an appropriate diagnostic and return an error. 5396 SourceRange FullRange(KeywordLoc.isValid() ? KeywordLoc : NNSRange.getBegin(), 5397 IILoc); 5398 Diag(IILoc, DiagID) << FullRange << Name << Ctx; 5399 if (Referenced) 5400 Diag(Referenced->getLocation(), diag::note_typename_refers_here) 5401 << Name; 5402 return QualType(); 5403} 5404 5405namespace { 5406 // See Sema::RebuildTypeInCurrentInstantiation 5407 class CurrentInstantiationRebuilder 5408 : public TreeTransform<CurrentInstantiationRebuilder> { 5409 SourceLocation Loc; 5410 DeclarationName Entity; 5411 5412 public: 5413 typedef TreeTransform<CurrentInstantiationRebuilder> inherited; 5414 5415 CurrentInstantiationRebuilder(Sema &SemaRef, 5416 SourceLocation Loc, 5417 DeclarationName Entity) 5418 : TreeTransform<CurrentInstantiationRebuilder>(SemaRef), 5419 Loc(Loc), Entity(Entity) { } 5420 5421 /// \brief Determine whether the given type \p T has already been 5422 /// transformed. 5423 /// 5424 /// For the purposes of type reconstruction, a type has already been 5425 /// transformed if it is NULL or if it is not dependent. 5426 bool AlreadyTransformed(QualType T) { 5427 return T.isNull() || !T->isDependentType(); 5428 } 5429 5430 /// \brief Returns the location of the entity whose type is being 5431 /// rebuilt. 5432 SourceLocation getBaseLocation() { return Loc; } 5433 5434 /// \brief Returns the name of the entity whose type is being rebuilt. 5435 DeclarationName getBaseEntity() { return Entity; } 5436 5437 /// \brief Sets the "base" location and entity when that 5438 /// information is known based on another transformation. 5439 void setBase(SourceLocation Loc, DeclarationName Entity) { 5440 this->Loc = Loc; 5441 this->Entity = Entity; 5442 } 5443 5444 /// \brief Transforms an expression by returning the expression itself 5445 /// (an identity function). 5446 /// 5447 /// FIXME: This is completely unsafe; we will need to actually clone the 5448 /// expressions. 5449 Sema::OwningExprResult TransformExpr(Expr *E) { 5450 return getSema().Owned(E->Retain()); 5451 } 5452 }; 5453} 5454 5455/// \brief Rebuilds a type within the context of the current instantiation. 5456/// 5457/// The type \p T is part of the type of an out-of-line member definition of 5458/// a class template (or class template partial specialization) that was parsed 5459/// and constructed before we entered the scope of the class template (or 5460/// partial specialization thereof). This routine will rebuild that type now 5461/// that we have entered the declarator's scope, which may produce different 5462/// canonical types, e.g., 5463/// 5464/// \code 5465/// template<typename T> 5466/// struct X { 5467/// typedef T* pointer; 5468/// pointer data(); 5469/// }; 5470/// 5471/// template<typename T> 5472/// typename X<T>::pointer X<T>::data() { ... } 5473/// \endcode 5474/// 5475/// Here, the type "typename X<T>::pointer" will be created as a DependentNameType, 5476/// since we do not know that we can look into X<T> when we parsed the type. 5477/// This function will rebuild the type, performing the lookup of "pointer" 5478/// in X<T> and returning an ElaboratedType whose canonical type is the same 5479/// as the canonical type of T*, allowing the return types of the out-of-line 5480/// definition and the declaration to match. 5481TypeSourceInfo *Sema::RebuildTypeInCurrentInstantiation(TypeSourceInfo *T, 5482 SourceLocation Loc, 5483 DeclarationName Name) { 5484 if (!T || !T->getType()->isDependentType()) 5485 return T; 5486 5487 CurrentInstantiationRebuilder Rebuilder(*this, Loc, Name); 5488 return Rebuilder.TransformType(T); 5489} 5490 5491bool Sema::RebuildNestedNameSpecifierInCurrentInstantiation(CXXScopeSpec &SS) { 5492 if (SS.isInvalid()) return true; 5493 5494 NestedNameSpecifier *NNS = static_cast<NestedNameSpecifier*>(SS.getScopeRep()); 5495 CurrentInstantiationRebuilder Rebuilder(*this, SS.getRange().getBegin(), 5496 DeclarationName()); 5497 NestedNameSpecifier *Rebuilt = 5498 Rebuilder.TransformNestedNameSpecifier(NNS, SS.getRange()); 5499 if (!Rebuilt) return true; 5500 5501 SS.setScopeRep(Rebuilt); 5502 return false; 5503} 5504 5505/// \brief Produces a formatted string that describes the binding of 5506/// template parameters to template arguments. 5507std::string 5508Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params, 5509 const TemplateArgumentList &Args) { 5510 // FIXME: For variadic templates, we'll need to get the structured list. 5511 return getTemplateArgumentBindingsText(Params, Args.getFlatArgumentList(), 5512 Args.flat_size()); 5513} 5514 5515std::string 5516Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params, 5517 const TemplateArgument *Args, 5518 unsigned NumArgs) { 5519 std::string Result; 5520 5521 if (!Params || Params->size() == 0 || NumArgs == 0) 5522 return Result; 5523 5524 for (unsigned I = 0, N = Params->size(); I != N; ++I) { 5525 if (I >= NumArgs) 5526 break; 5527 5528 if (I == 0) 5529 Result += "[with "; 5530 else 5531 Result += ", "; 5532 5533 if (const IdentifierInfo *Id = Params->getParam(I)->getIdentifier()) { 5534 Result += Id->getName(); 5535 } else { 5536 Result += '$'; 5537 Result += llvm::utostr(I); 5538 } 5539 5540 Result += " = "; 5541 5542 switch (Args[I].getKind()) { 5543 case TemplateArgument::Null: 5544 Result += "<no value>"; 5545 break; 5546 5547 case TemplateArgument::Type: { 5548 std::string TypeStr; 5549 Args[I].getAsType().getAsStringInternal(TypeStr, 5550 Context.PrintingPolicy); 5551 Result += TypeStr; 5552 break; 5553 } 5554 5555 case TemplateArgument::Declaration: { 5556 bool Unnamed = true; 5557 if (NamedDecl *ND = dyn_cast_or_null<NamedDecl>(Args[I].getAsDecl())) { 5558 if (ND->getDeclName()) { 5559 Unnamed = false; 5560 Result += ND->getNameAsString(); 5561 } 5562 } 5563 5564 if (Unnamed) { 5565 Result += "<anonymous>"; 5566 } 5567 break; 5568 } 5569 5570 case TemplateArgument::Template: { 5571 std::string Str; 5572 llvm::raw_string_ostream OS(Str); 5573 Args[I].getAsTemplate().print(OS, Context.PrintingPolicy); 5574 Result += OS.str(); 5575 break; 5576 } 5577 5578 case TemplateArgument::Integral: { 5579 Result += Args[I].getAsIntegral()->toString(10); 5580 break; 5581 } 5582 5583 case TemplateArgument::Expression: { 5584 // FIXME: This is non-optimal, since we're regurgitating the 5585 // expression we were given. 5586 std::string Str; 5587 { 5588 llvm::raw_string_ostream OS(Str); 5589 Args[I].getAsExpr()->printPretty(OS, Context, 0, 5590 Context.PrintingPolicy); 5591 } 5592 Result += Str; 5593 break; 5594 } 5595 5596 case TemplateArgument::Pack: 5597 // FIXME: Format template argument packs 5598 Result += "<template argument pack>"; 5599 break; 5600 } 5601 } 5602 5603 Result += ']'; 5604 return Result; 5605} 5606