SemaTemplate.cpp revision 6fb745bdf1ff1e32caf07e42093a7920726892c1
107393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com//===------- SemaTemplate.cpp - Semantic Analysis for C++ Templates -------===/ 207393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com// 307393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com// The LLVM Compiler Infrastructure 407393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com// 507393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com// This file is distributed under the University of Illinois Open Source 607393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com// License. See LICENSE.TXT for details. 7ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark//===----------------------------------------------------------------------===/ 8dac1d17027dcaa5596885a9f333979418b35001ccaryclark// 907393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com// This file implements semantic analysis for C++ templates. 1007393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com//===----------------------------------------------------------------------===/ 11ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark 12ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark#include "Sema.h" 13ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark#include "Lookup.h" 14ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark#include "TreeTransform.h" 15ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark#include "clang/AST/ASTContext.h" 16ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark#include "clang/AST/Expr.h" 17ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark#include "clang/AST/ExprCXX.h" 18ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark#include "clang/AST/DeclFriend.h" 19ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark#include "clang/AST/DeclTemplate.h" 20ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark#include "clang/Parse/DeclSpec.h" 2107393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com#include "clang/Parse/Template.h" 2207393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com#include "clang/Basic/LangOptions.h" 2307393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com#include "clang/Basic/PartialDiagnostic.h" 2407393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com#include "llvm/ADT/StringExtras.h" 2507393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.comusing namespace clang; 2607393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com 2707393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com/// \brief Determine whether the declaration found is acceptable as the name 2807393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com/// of a template and, if so, return that template declaration. Otherwise, 2907393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com/// returns NULL. 3007393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.comstatic NamedDecl *isAcceptableTemplateName(ASTContext &Context, NamedDecl *D) { 3107393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com if (!D) 3207393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com return 0; 334431e7757cfcb8cfa99535eed0e9f156dabf95c2commit-bot@chromium.org 344431e7757cfcb8cfa99535eed0e9f156dabf95c2commit-bot@chromium.org if (isa<TemplateDecl>(D)) 3507393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com return D; 3607393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com 3707393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) { 3807393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com // C++ [temp.local]p1: 3907393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com // Like normal (non-template) classes, class templates have an 4007393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com // injected-class-name (Clause 9). The injected-class-name 4107393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com // can be used with or without a template-argument-list. When 4207393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com // it is used without a template-argument-list, it is 4307393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com // equivalent to the injected-class-name followed by the 4407393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com // template-parameters of the class template enclosed in 45ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark // <>. When it is used with a template-argument-list, it 46ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark // refers to the specified class template specialization, 47ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark // which could be the current specialization or another 484431e7757cfcb8cfa99535eed0e9f156dabf95c2commit-bot@chromium.org // specialization. 4907393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com if (Record->isInjectedClassName()) { 50ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark Record = cast<CXXRecordDecl>(Record->getDeclContext()); 51ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark if (Record->getDescribedClassTemplate()) 5207393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com return Record->getDescribedClassTemplate(); 534431e7757cfcb8cfa99535eed0e9f156dabf95c2commit-bot@chromium.org 5407393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com if (ClassTemplateSpecializationDecl *Spec 5507393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com = dyn_cast<ClassTemplateSpecializationDecl>(Record)) 56ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark return Spec->getSpecializedTemplate(); 57ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark } 58ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark 59ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark return 0; 60ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark } 61ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark 62ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark return 0; 63ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark} 64ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark 6507393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.comstatic void FilterAcceptableTemplateNames(ASTContext &C, LookupResult &R) { 66ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark // The set of class templates we've already seen. 67ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark llvm::SmallPtrSet<ClassTemplateDecl *, 8> ClassTemplates; 68ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark LookupResult::Filter filter = R.makeFilter(); 694431e7757cfcb8cfa99535eed0e9f156dabf95c2commit-bot@chromium.org while (filter.hasNext()) { 704431e7757cfcb8cfa99535eed0e9f156dabf95c2commit-bot@chromium.org NamedDecl *Orig = filter.next(); 714431e7757cfcb8cfa99535eed0e9f156dabf95c2commit-bot@chromium.org NamedDecl *Repl = isAcceptableTemplateName(C, Orig->getUnderlyingDecl()); 724431e7757cfcb8cfa99535eed0e9f156dabf95c2commit-bot@chromium.org if (!Repl) 73ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark filter.erase(); 7407393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com else if (Repl != Orig) { 7507393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com 76ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark // C++ [temp.local]p3: 7707393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com // A lookup that finds an injected-class-name (10.2) can result in an 78ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark // ambiguity in certain cases (for example, if it is found in more than 79ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark // one base class). If all of the injected-class-names that are found 80ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark // refer to specializations of the same class template, and if the name 81ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark // is followed by a template-argument-list, the reference refers to the 82ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark // class template itself and not a specialization thereof, and is not 83ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark // ambiguous. 84ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark // 85ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark // FIXME: Will we eventually have to do the same for alias templates? 86ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark if (ClassTemplateDecl *ClassTmpl = dyn_cast<ClassTemplateDecl>(Repl)) 874431e7757cfcb8cfa99535eed0e9f156dabf95c2commit-bot@chromium.org if (!ClassTemplates.insert(ClassTmpl)) { 884431e7757cfcb8cfa99535eed0e9f156dabf95c2commit-bot@chromium.org filter.erase(); 8907393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com continue; 904431e7757cfcb8cfa99535eed0e9f156dabf95c2commit-bot@chromium.org } 91ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark 9207393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com filter.replace(Repl); 9307393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com } 944431e7757cfcb8cfa99535eed0e9f156dabf95c2commit-bot@chromium.org } 954431e7757cfcb8cfa99535eed0e9f156dabf95c2commit-bot@chromium.org filter.done(); 964431e7757cfcb8cfa99535eed0e9f156dabf95c2commit-bot@chromium.org} 97ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark 98ccec0f958ffc71a9986d236bc2eb335cb2111119caryclarkTemplateNameKind Sema::isTemplateName(Scope *S, 99ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark CXXScopeSpec &SS, 100ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark UnqualifiedId &Name, 101ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark TypeTy *ObjectTypePtr, 102ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark bool EnteringContext, 10307393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com TemplateTy &TemplateResult) { 10407393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com assert(getLangOptions().CPlusPlus && "No template names in C!"); 105ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark 10607393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com DeclarationName TName; 10707393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com 10807393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com switch (Name.getKind()) { 10907393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com case UnqualifiedId::IK_Identifier: 11007393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com TName = DeclarationName(Name.Identifier); 111ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark break; 112ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark 113ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark case UnqualifiedId::IK_OperatorFunctionId: 11407393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com TName = Context.DeclarationNames.getCXXOperatorName( 11507393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com Name.OperatorFunctionId.Operator); 11607393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com break; 117ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark 11807393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com case UnqualifiedId::IK_LiteralOperatorId: 11907393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com TName = Context.DeclarationNames.getCXXLiteralOperatorName(Name.Identifier); 120ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark break; 121ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark 12207393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com default: 12307393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com return TNK_Non_template; 12407393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com } 125ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark 126ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark QualType ObjectType = QualType::getFromOpaquePtr(ObjectTypePtr); 12707393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com 12807393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com LookupResult R(*this, TName, Name.getSourceRange().getBegin(), 12907393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com LookupOrdinaryName); 130ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark R.suppressDiagnostics(); 131ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark LookupTemplateName(R, S, SS, ObjectType, EnteringContext); 13207393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com if (R.empty() || R.isAmbiguous()) 13307393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com return TNK_Non_template; 13407393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com 135ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark TemplateName Template; 13607393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com TemplateNameKind TemplateKind; 13707393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com 138ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark unsigned ResultCount = R.end() - R.begin(); 139ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark if (ResultCount > 1) { 140ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark // We assume that we'll preserve the qualifier from a function 141ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark // template name in other ways. 142ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark Template = Context.getOverloadedTemplateName(R.begin(), R.end()); 14307393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com TemplateKind = TNK_Function_template; 14407393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com } else { 14507393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com TemplateDecl *TD = cast<TemplateDecl>((*R.begin())->getUnderlyingDecl()); 146ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark 147ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark if (SS.isSet() && !SS.isInvalid()) { 148ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark NestedNameSpecifier *Qualifier 149ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); 15065f553182ab7069378ef863d30094d0327f178d0caryclark Template = Context.getQualifiedTemplateName(Qualifier, false, TD); 15165f553182ab7069378ef863d30094d0327f178d0caryclark } else { 15265f553182ab7069378ef863d30094d0327f178d0caryclark Template = TemplateName(TD); 15365f553182ab7069378ef863d30094d0327f178d0caryclark } 154ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark 15507393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com if (isa<FunctionTemplateDecl>(TD)) 15607393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com TemplateKind = TNK_Function_template; 157ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark else { 15807393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com assert(isa<ClassTemplateDecl>(TD) || isa<TemplateTemplateParmDecl>(TD)); 15907393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com TemplateKind = TNK_Type_template; 160ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark } 161ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark } 1624431e7757cfcb8cfa99535eed0e9f156dabf95c2commit-bot@chromium.org 163ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark TemplateResult = TemplateTy::make(Template); 1644431e7757cfcb8cfa99535eed0e9f156dabf95c2commit-bot@chromium.org return TemplateKind; 16507393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com} 16607393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com 16707393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.combool Sema::DiagnoseUnknownTemplateName(const IdentifierInfo &II, 16807393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com SourceLocation IILoc, 16907393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com Scope *S, 1704431e7757cfcb8cfa99535eed0e9f156dabf95c2commit-bot@chromium.org const CXXScopeSpec *SS, 1714431e7757cfcb8cfa99535eed0e9f156dabf95c2commit-bot@chromium.org TemplateTy &SuggestedTemplate, 1724431e7757cfcb8cfa99535eed0e9f156dabf95c2commit-bot@chromium.org TemplateNameKind &SuggestedKind) { 1734431e7757cfcb8cfa99535eed0e9f156dabf95c2commit-bot@chromium.org // We can't recover unless there's a dependent scope specifier preceding the 17407393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com // template name. 1754431e7757cfcb8cfa99535eed0e9f156dabf95c2commit-bot@chromium.org if (!SS || !SS->isSet() || !isDependentScopeSpecifier(*SS) || 1764431e7757cfcb8cfa99535eed0e9f156dabf95c2commit-bot@chromium.org computeDeclContext(*SS)) 1774431e7757cfcb8cfa99535eed0e9f156dabf95c2commit-bot@chromium.org return false; 1784431e7757cfcb8cfa99535eed0e9f156dabf95c2commit-bot@chromium.org 17907393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com // The code is missing a 'template' keyword prior to the dependent template 18007393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com // name. 18107393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com NestedNameSpecifier *Qualifier = (NestedNameSpecifier*)SS->getScopeRep(); 182dac1d17027dcaa5596885a9f333979418b35001ccaryclark Diag(IILoc, diag::err_template_kw_missing) 183ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark << Qualifier << II.getName() 184570863f2e22b8ea7d7c504bd15e4f766af097df2caryclark@google.com << FixItHint::CreateInsertion(IILoc, "template "); 18507393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com SuggestedTemplate 18607393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com = TemplateTy::make(Context.getDependentTemplateName(Qualifier, &II)); 18707393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com SuggestedKind = TNK_Dependent_template_name; 18807393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com return true; 189ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark} 190ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark 191ccec0f958ffc71a9986d236bc2eb335cb2111119caryclarkvoid Sema::LookupTemplateName(LookupResult &Found, 19207393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com Scope *S, CXXScopeSpec &SS, 19307393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com QualType ObjectType, 194ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark bool EnteringContext) { 1954431e7757cfcb8cfa99535eed0e9f156dabf95c2commit-bot@chromium.org // Determine where to perform name lookup 196ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark DeclContext *LookupCtx = 0; 1974431e7757cfcb8cfa99535eed0e9f156dabf95c2commit-bot@chromium.org bool isDependent = false; 19807393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com if (!ObjectType.isNull()) { 19907393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com // This nested-name-specifier occurs in a member access expression, e.g., 20007393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com // x->B::f, and we are looking into the type of the object. 20107393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com assert(!SS.isSet() && "ObjectType and scope specifier cannot coexist"); 20207393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com LookupCtx = computeDeclContext(ObjectType); 203ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark isDependent = ObjectType->isDependentType(); 204ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark assert((isDependent || !ObjectType->isIncompleteType()) && 20507393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com "Caller should have completed object type"); 20607393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com } else if (SS.isSet()) { 207ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark // This nested-name-specifier occurs after another nested-name-specifier, 20807393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com // so long into the context associated with the prior nested-name-specifier. 20907393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com LookupCtx = computeDeclContext(SS, EnteringContext); 21007393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com isDependent = isDependentScopeSpecifier(SS); 21107393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com 21207393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com // The declaration context must be complete. 21307393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com if (LookupCtx && RequireCompleteDeclContext(SS, LookupCtx)) 21407393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com return; 215ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark } 21607393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com 217277c3f87656c44e0a651ed0dd56efa16c0ab07b4reed@google.com bool ObjectTypeSearchedInScope = false; 21807393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com if (LookupCtx) { 21907393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com // Perform "qualified" name lookup into the declaration context we 22007393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com // computed, which is either the type of the base of a member access 22107393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com // expression or the declaration context associated with a prior 22207393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com // nested-name-specifier. 22307393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com LookupQualifiedName(Found, LookupCtx); 22407393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com 22507393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com if (!ObjectType.isNull() && Found.empty()) { 22607393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com // C++ [basic.lookup.classref]p1: 22707393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com // In a class member access expression (5.2.5), if the . or -> token is 22807393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com // immediately followed by an identifier followed by a <, the 22907393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com // identifier must be looked up to determine whether the < is the 23007393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com // beginning of a template argument list (14.2) or a less-than operator. 23107393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com // The identifier is first looked up in the class of the object 23207393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com // expression. If the identifier is not found, it is then looked up in 23307393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com // the context of the entire postfix-expression and shall name a class 23407393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com // or function template. 23507393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com // 23607393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com // FIXME: When we're instantiating a template, do we actually have to 23707393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com // look in the scope of the template? Seems fishy... 23807393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com if (S) LookupName(Found, S); 23907393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com ObjectTypeSearchedInScope = true; 24007393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com } 24107393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com } else if (isDependent) { 24207393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com // We cannot look into a dependent object type or nested nme 24307393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com // specifier. 24407393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com return; 24507393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com } else { 24607393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com // Perform unqualified name lookup in the current scope. 24707393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com LookupName(Found, S); 24807393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com } 24907393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com 250ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark if (Found.empty() && !isDependent) { 251ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark // If we did not find any names, attempt to correct any typos. 252ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark DeclarationName Name = Found.getLookupName(); 253ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark if (DeclarationName Corrected = CorrectTypo(Found, S, &SS, LookupCtx, 2544431e7757cfcb8cfa99535eed0e9f156dabf95c2commit-bot@chromium.org false, CTC_CXXCasts)) { 255ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark FilterAcceptableTemplateNames(Context, Found); 256ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark if (!Found.empty() && isa<TemplateDecl>(*Found.begin())) { 257ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark if (LookupCtx) 258ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark Diag(Found.getNameLoc(), diag::err_no_member_template_suggest) 259ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark << Name << LookupCtx << Found.getLookupName() << SS.getRange() 260ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark << FixItHint::CreateReplacement(Found.getNameLoc(), 261ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark Found.getLookupName().getAsString()); 262ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark else 263ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark Diag(Found.getNameLoc(), diag::err_no_template_suggest) 264ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark << Name << Found.getLookupName() 265ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark << FixItHint::CreateReplacement(Found.getNameLoc(), 266ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark Found.getLookupName().getAsString()); 26707393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com if (TemplateDecl *Template = Found.getAsSingle<TemplateDecl>()) 268ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark Diag(Template->getLocation(), diag::note_previous_decl) 269ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark << Template->getDeclName(); 27007393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com } else 27107393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com Found.clear(); 272ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark } else { 273ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark Found.clear(); 274ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark } 275ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark } 276ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark 277ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark FilterAcceptableTemplateNames(Context, Found); 278ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark if (Found.empty()) 279ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark return; 280ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark 281ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark if (S && !ObjectType.isNull() && !ObjectTypeSearchedInScope) { 282ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark // C++ [basic.lookup.classref]p1: 283ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark // [...] If the lookup in the class of the object expression finds a 284ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark // template, the name is also looked up in the context of the entire 285ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark // postfix-expression and [...] 286ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark // 287ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark LookupResult FoundOuter(*this, Found.getLookupName(), Found.getNameLoc(), 288ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark LookupOrdinaryName); 2894431e7757cfcb8cfa99535eed0e9f156dabf95c2commit-bot@chromium.org LookupName(FoundOuter, S); 2904431e7757cfcb8cfa99535eed0e9f156dabf95c2commit-bot@chromium.org FilterAcceptableTemplateNames(Context, FoundOuter); 291ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark 292ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark if (FoundOuter.empty()) { 293ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark // - if the name is not found, the name found in the class of the 2944431e7757cfcb8cfa99535eed0e9f156dabf95c2commit-bot@chromium.org // object expression is used, otherwise 2954431e7757cfcb8cfa99535eed0e9f156dabf95c2commit-bot@chromium.org } else if (!FoundOuter.getAsSingle<ClassTemplateDecl>()) { 296ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark // - if the name is found in the context of the entire 297ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark // postfix-expression and does not name a class template, the name 298ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark // found in the class of the object expression is used, otherwise 299ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark } else { 3004431e7757cfcb8cfa99535eed0e9f156dabf95c2commit-bot@chromium.org // - if the name found is a class template, it must refer to the same 301ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark // entity as the one found in the class of the object expression, 3024431e7757cfcb8cfa99535eed0e9f156dabf95c2commit-bot@chromium.org // otherwise the program is ill-formed. 3034431e7757cfcb8cfa99535eed0e9f156dabf95c2commit-bot@chromium.org if (!Found.isSingleResult() || 304ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark Found.getFoundDecl()->getCanonicalDecl() 3054431e7757cfcb8cfa99535eed0e9f156dabf95c2commit-bot@chromium.org != FoundOuter.getFoundDecl()->getCanonicalDecl()) { 306dac1d17027dcaa5596885a9f333979418b35001ccaryclark Diag(Found.getNameLoc(), 3074431e7757cfcb8cfa99535eed0e9f156dabf95c2commit-bot@chromium.org diag::err_nested_name_member_ref_lookup_ambiguous) 308ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark << Found.getLookupName(); 3094431e7757cfcb8cfa99535eed0e9f156dabf95c2commit-bot@chromium.org Diag(Found.getRepresentativeDecl()->getLocation(), 310ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark diag::note_ambig_member_ref_object_type) 3114431e7757cfcb8cfa99535eed0e9f156dabf95c2commit-bot@chromium.org << ObjectType; 312dac1d17027dcaa5596885a9f333979418b35001ccaryclark Diag(FoundOuter.getFoundDecl()->getLocation(), 313dac1d17027dcaa5596885a9f333979418b35001ccaryclark diag::note_ambig_member_ref_scope); 3144431e7757cfcb8cfa99535eed0e9f156dabf95c2commit-bot@chromium.org 3154431e7757cfcb8cfa99535eed0e9f156dabf95c2commit-bot@chromium.org // Recover by taking the template that we found in the object 316ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark // expression's type. 317ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark } 318ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark } 319ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark } 320ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark} 321ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark 322ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark/// ActOnDependentIdExpression - Handle a dependent id-expression that 323ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark/// was just parsed. This is only possible with an explicit scope 324ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark/// specifier naming a dependent type. 325ccec0f958ffc71a9986d236bc2eb335cb2111119caryclarkSema::OwningExprResult 326ccec0f958ffc71a9986d236bc2eb335cb2111119caryclarkSema::ActOnDependentIdExpression(const CXXScopeSpec &SS, 327ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark DeclarationName Name, 328ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark SourceLocation NameLoc, 329ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark bool isAddressOfOperand, 330ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark const TemplateArgumentListInfo *TemplateArgs) { 331ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark NestedNameSpecifier *Qualifier 332ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark = static_cast<NestedNameSpecifier*>(SS.getScopeRep()); 333ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark 334ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark if (!isAddressOfOperand && 335ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark isa<CXXMethodDecl>(CurContext) && 336ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark cast<CXXMethodDecl>(CurContext)->isInstance()) { 337ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark QualType ThisType = cast<CXXMethodDecl>(CurContext)->getThisType(Context); 338ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark 339ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark // Since the 'this' expression is synthesized, we don't need to 340ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark // perform the double-lookup check. 341ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark NamedDecl *FirstQualifierInScope = 0; 342ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark 343ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark return Owned(CXXDependentScopeMemberExpr::Create(Context, 344ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark /*This*/ 0, ThisType, 345ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark /*IsArrow*/ true, 3464431e7757cfcb8cfa99535eed0e9f156dabf95c2commit-bot@chromium.org /*Op*/ SourceLocation(), 3474431e7757cfcb8cfa99535eed0e9f156dabf95c2commit-bot@chromium.org Qualifier, SS.getRange(), 348ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark FirstQualifierInScope, 349ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark Name, NameLoc, 350ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark TemplateArgs)); 351ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark } 3524431e7757cfcb8cfa99535eed0e9f156dabf95c2commit-bot@chromium.org 353ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark return BuildDependentDeclRefExpr(SS, Name, NameLoc, TemplateArgs); 354ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark} 355ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark 356ccec0f958ffc71a9986d236bc2eb335cb2111119caryclarkSema::OwningExprResult 357ccec0f958ffc71a9986d236bc2eb335cb2111119caryclarkSema::BuildDependentDeclRefExpr(const CXXScopeSpec &SS, 358ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark DeclarationName Name, 359ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark SourceLocation NameLoc, 360ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark const TemplateArgumentListInfo *TemplateArgs) { 361ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark return Owned(DependentScopeDeclRefExpr::Create(Context, 362ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark static_cast<NestedNameSpecifier*>(SS.getScopeRep()), 363ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark SS.getRange(), 364ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark Name, NameLoc, 365ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark TemplateArgs)); 366ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark} 367ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark 368ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark/// DiagnoseTemplateParameterShadow - Produce a diagnostic complaining 369ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark/// that the template parameter 'PrevDecl' is being shadowed by a new 370ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark/// declaration at location Loc. Returns true to indicate that this is 371ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark/// an error, and false otherwise. 372ccec0f958ffc71a9986d236bc2eb335cb2111119caryclarkbool Sema::DiagnoseTemplateParameterShadow(SourceLocation Loc, Decl *PrevDecl) { 373ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark assert(PrevDecl->isTemplateParameter() && "Not a template parameter"); 374ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark 375ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark // Microsoft Visual C++ permits template parameters to be shadowed. 376ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark if (getLangOptions().Microsoft) 377ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark return false; 378ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark 379ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark // C++ [temp.local]p4: 380ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark // A template-parameter shall not be redeclared within its 381ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark // scope (including nested scopes). 382ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark Diag(Loc, diag::err_template_param_shadow) 383ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark << cast<NamedDecl>(PrevDecl)->getDeclName(); 384ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark Diag(PrevDecl->getLocation(), diag::note_template_param_here); 385ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark return true; 386ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark} 387ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark 388ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark/// AdjustDeclIfTemplate - If the given decl happens to be a template, reset 389ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark/// the parameter D to reference the templated declaration and return a pointer 390ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark/// to the template declaration. Otherwise, do nothing to D and return null. 391ccec0f958ffc71a9986d236bc2eb335cb2111119caryclarkTemplateDecl *Sema::AdjustDeclIfTemplate(DeclPtrTy &D) { 392ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark if (TemplateDecl *Temp = dyn_cast_or_null<TemplateDecl>(D.getAs<Decl>())) { 393ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark D = DeclPtrTy::make(Temp->getTemplatedDecl()); 394ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark return Temp; 395ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark } 396ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark return 0; 397ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark} 3984431e7757cfcb8cfa99535eed0e9f156dabf95c2commit-bot@chromium.org 3994431e7757cfcb8cfa99535eed0e9f156dabf95c2commit-bot@chromium.orgstatic TemplateArgumentLoc translateTemplateArgument(Sema &SemaRef, 400ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark const ParsedTemplateArgument &Arg) { 401ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark 402ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark switch (Arg.getKind()) { 403ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark case ParsedTemplateArgument::Type: { 404ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark TypeSourceInfo *DI; 405ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark QualType T = SemaRef.GetTypeFromParser(Arg.getAsType(), &DI); 406ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark if (!DI) 407ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark DI = SemaRef.Context.getTrivialTypeSourceInfo(T, Arg.getLocation()); 408ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark return TemplateArgumentLoc(TemplateArgument(T), DI); 40907393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com } 41007393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com 411ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark case ParsedTemplateArgument::NonType: { 41207393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com Expr *E = static_cast<Expr *>(Arg.getAsExpr()); 413ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark return TemplateArgumentLoc(TemplateArgument(E), E); 414ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark } 41507393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com 416ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark case ParsedTemplateArgument::Template: { 417ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark TemplateName Template 418ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark = TemplateName::getFromVoidPointer(Arg.getAsTemplate().get()); 419ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark return TemplateArgumentLoc(TemplateArgument(Template), 420ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark Arg.getScopeSpec().getRange(), 421ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark Arg.getLocation()); 422ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark } 423dac1d17027dcaa5596885a9f333979418b35001ccaryclark } 424ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark 42565f553182ab7069378ef863d30094d0327f178d0caryclark llvm_unreachable("Unhandled parsed template argument"); 42665f553182ab7069378ef863d30094d0327f178d0caryclark return TemplateArgumentLoc(); 427ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark} 428ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark 429ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark/// \brief Translates template arguments as provided by the parser 430ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark/// into template arguments used by semantic analysis. 43107393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.comvoid Sema::translateTemplateArguments(const ASTTemplateArgsPtr &TemplateArgsIn, 432ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark TemplateArgumentListInfo &TemplateArgs) { 433ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark for (unsigned I = 0, Last = TemplateArgsIn.size(); I != Last; ++I) 434ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark TemplateArgs.addArgument(translateTemplateArgument(*this, 435ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark TemplateArgsIn[I])); 436ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark} 437ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark 438ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark/// ActOnTypeParameter - Called when a C++ template type parameter 43907393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com/// (e.g., "typename T") has been parsed. Typename specifies whether 440ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark/// the keyword "typename" was used to declare the type parameter 441ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark/// (otherwise, "class" was used), and KeyLoc is the location of the 442ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark/// "class" or "typename" keyword. ParamName is the name of the 443ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark/// parameter (NULL indicates an unnamed template parameter) and 444ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark/// ParamName is the location of the parameter name (if any). 445ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark/// If the type parameter has a default argument, it will be added 446ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark/// later via ActOnTypeParameterDefault. 447ccec0f958ffc71a9986d236bc2eb335cb2111119caryclarkSema::DeclPtrTy Sema::ActOnTypeParameter(Scope *S, bool Typename, bool Ellipsis, 448ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark SourceLocation EllipsisLoc, 449ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark SourceLocation KeyLoc, 450ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark IdentifierInfo *ParamName, 4514431e7757cfcb8cfa99535eed0e9f156dabf95c2commit-bot@chromium.org SourceLocation ParamNameLoc, 452ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark unsigned Depth, unsigned Position) { 453ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark assert(S->isTemplateParamScope() && 4544431e7757cfcb8cfa99535eed0e9f156dabf95c2commit-bot@chromium.org "Template type parameter not in template parameter scope!"); 45565f553182ab7069378ef863d30094d0327f178d0caryclark bool Invalid = false; 45665f553182ab7069378ef863d30094d0327f178d0caryclark 457ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark if (ParamName) { 458ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark NamedDecl *PrevDecl = LookupSingleName(S, ParamName, ParamNameLoc, 459ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark LookupOrdinaryName, 46065f553182ab7069378ef863d30094d0327f178d0caryclark ForRedeclaration); 461ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark if (PrevDecl && PrevDecl->isTemplateParameter()) 462ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark Invalid = Invalid || DiagnoseTemplateParameterShadow(ParamNameLoc, 463ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark PrevDecl); 46465f553182ab7069378ef863d30094d0327f178d0caryclark } 465ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark 466ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark SourceLocation Loc = ParamNameLoc; 46765f553182ab7069378ef863d30094d0327f178d0caryclark if (!ParamName) 468ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark Loc = KeyLoc; 469ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark 470ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark TemplateTypeParmDecl *Param 471ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark = TemplateTypeParmDecl::Create(Context, Context.getTranslationUnitDecl(), 47265f553182ab7069378ef863d30094d0327f178d0caryclark Loc, Depth, Position, ParamName, Typename, 473ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark Ellipsis); 47407393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com if (Invalid) 47507393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com Param->setInvalidDecl(); 476ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark 477ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark if (ParamName) { 478ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark // Add the template parameter into the current scope. 479ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark S->AddDecl(DeclPtrTy::make(Param)); 480ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark IdResolver.AddDecl(Param); 481ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark } 482ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark 483ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark return DeclPtrTy::make(Param); 484ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark} 485ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark 486ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark/// ActOnTypeParameterDefault - Adds a default argument (the type 487ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark/// Default) to the given template type parameter (TypeParam). 488ccec0f958ffc71a9986d236bc2eb335cb2111119caryclarkvoid Sema::ActOnTypeParameterDefault(DeclPtrTy TypeParam, 489ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark SourceLocation EqualLoc, 490ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark SourceLocation DefaultLoc, 491ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark TypeTy *DefaultT) { 492ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark TemplateTypeParmDecl *Parm 493ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark = cast<TemplateTypeParmDecl>(TypeParam.getAs<Decl>()); 494ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark 495a5e55925ea03e76885804bda77408a1d6f04c335caryclark@google.com TypeSourceInfo *DefaultTInfo; 496ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark GetTypeFromParser(DefaultT, &DefaultTInfo); 497ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark 498ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark assert(DefaultTInfo && "expected source information for type"); 499a5e55925ea03e76885804bda77408a1d6f04c335caryclark@google.com 500dac1d17027dcaa5596885a9f333979418b35001ccaryclark // C++0x [temp.param]p9: 501dac1d17027dcaa5596885a9f333979418b35001ccaryclark // A default template-argument may be specified for any kind of 502ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark // template-parameter that is not a template parameter pack. 503ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark if (Parm->isParameterPack()) { 504ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark Diag(DefaultLoc, diag::err_template_param_pack_default_arg); 505ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark return; 506ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark } 507ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark 508dac1d17027dcaa5596885a9f333979418b35001ccaryclark // C++ [temp.param]p14: 509ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark // A template-parameter shall not be used in its own default argument. 510ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark // FIXME: Implement this check! Needs a recursive walk over the types. 511ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark 512ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark // Check the template argument itself. 513ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark if (CheckTemplateArgument(Parm, DefaultTInfo)) { 514ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark Parm->setInvalidDecl(); 51507393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com return; 51607393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com } 517ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark 518ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark Parm->setDefaultArgument(DefaultTInfo, false); 519ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark} 520ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark 521ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark/// \brief Check that the type of a non-type template parameter is 522ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark/// well-formed. 523ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark/// 524ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark/// \returns the (possibly-promoted) parameter type if valid; 52507393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com/// otherwise, produces a diagnostic and returns a NULL type. 526ccec0f958ffc71a9986d236bc2eb335cb2111119caryclarkQualType 52707393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.comSema::CheckNonTypeTemplateParameterType(QualType T, SourceLocation Loc) { 52807393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com // C++ [temp.param]p4: 529ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark // 530ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark // A non-type template-parameter shall have one of the following 531ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark // (optionally cv-qualified) types: 532ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark // 533ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark // -- integral or enumeration type, 534ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark if (T->isIntegralType() || T->isEnumeralType() || 535ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark // -- pointer to object or pointer to function, 536ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark (T->isPointerType() && 537ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark (T->getAs<PointerType>()->getPointeeType()->isObjectType() || 538ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark T->getAs<PointerType>()->getPointeeType()->isFunctionType())) || 53919eb3b2f0aa6dce5c0335230a8930e90733e5d5dcaryclark // -- reference to object or reference to function, 540ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark T->isReferenceType() || 541ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark // -- pointer to member. 542ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark T->isMemberPointerType() || 543ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark // If T is a dependent type, we can't do the check now, so we 544ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark // assume that it is well-formed. 545ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark T->isDependentType()) 546ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark return T; 547ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark // C++ [temp.param]p8: 548ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark // 549ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark // A non-type template-parameter of type "array of T" or 5504431e7757cfcb8cfa99535eed0e9f156dabf95c2commit-bot@chromium.org // "function returning T" is adjusted to be of type "pointer to 551ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark // T" or "pointer to function returning T", respectively. 552ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark else if (T->isArrayType()) 553ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark // FIXME: Keep the type prior to promotion? 5544431e7757cfcb8cfa99535eed0e9f156dabf95c2commit-bot@chromium.org return Context.getArrayDecayedType(T); 555ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark else if (T->isFunctionType()) 5564431e7757cfcb8cfa99535eed0e9f156dabf95c2commit-bot@chromium.org // FIXME: Keep the type prior to promotion? 5574431e7757cfcb8cfa99535eed0e9f156dabf95c2commit-bot@chromium.org return Context.getPointerType(T); 558ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark 559ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark Diag(Loc, diag::err_template_nontype_parm_bad_type) 560ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark << T; 561ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark 562ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark return QualType(); 563ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark} 564570863f2e22b8ea7d7c504bd15e4f766af097df2caryclark@google.com 565ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark/// ActOnNonTypeTemplateParameter - Called when a C++ non-type 566ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark/// template parameter (e.g., "int Size" in "template<int Size> 567ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark/// class Array") has been parsed. S is the current scope and D is 568570863f2e22b8ea7d7c504bd15e4f766af097df2caryclark@google.com/// the parsed declarator. 5694431e7757cfcb8cfa99535eed0e9f156dabf95c2commit-bot@chromium.orgSema::DeclPtrTy Sema::ActOnNonTypeTemplateParameter(Scope *S, Declarator &D, 570ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark unsigned Depth, 571ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark unsigned Position) { 572ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark TypeSourceInfo *TInfo = 0; 573ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark QualType T = GetTypeForDeclarator(D, S, &TInfo); 574ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark 575ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark assert(S->isTemplateParamScope() && 576ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark "Non-type template parameter not in template parameter scope!"); 577ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark bool Invalid = false; 578ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark 5794431e7757cfcb8cfa99535eed0e9f156dabf95c2commit-bot@chromium.org IdentifierInfo *ParamName = D.getIdentifier(); 580ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark if (ParamName) { 581ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark NamedDecl *PrevDecl = LookupSingleName(S, ParamName, D.getIdentifierLoc(), 582ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark LookupOrdinaryName, 583570863f2e22b8ea7d7c504bd15e4f766af097df2caryclark@google.com ForRedeclaration); 584ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark if (PrevDecl && PrevDecl->isTemplateParameter()) 5854431e7757cfcb8cfa99535eed0e9f156dabf95c2commit-bot@chromium.org Invalid = Invalid || DiagnoseTemplateParameterShadow(D.getIdentifierLoc(), 5864431e7757cfcb8cfa99535eed0e9f156dabf95c2commit-bot@chromium.org PrevDecl); 587dac1d17027dcaa5596885a9f333979418b35001ccaryclark } 588ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark 589ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark T = CheckNonTypeTemplateParameterType(T, D.getIdentifierLoc()); 5904431e7757cfcb8cfa99535eed0e9f156dabf95c2commit-bot@chromium.org if (T.isNull()) { 591ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark T = Context.IntTy; // Recover with an 'int' type. 59265b427cff9cd34a06ff060d65d00cc3615d8fd94caryclark Invalid = true; 5934431e7757cfcb8cfa99535eed0e9f156dabf95c2commit-bot@chromium.org } 594570863f2e22b8ea7d7c504bd15e4f766af097df2caryclark@google.com 595570863f2e22b8ea7d7c504bd15e4f766af097df2caryclark@google.com NonTypeTemplateParmDecl *Param 596570863f2e22b8ea7d7c504bd15e4f766af097df2caryclark@google.com = NonTypeTemplateParmDecl::Create(Context, Context.getTranslationUnitDecl(), 597570863f2e22b8ea7d7c504bd15e4f766af097df2caryclark@google.com D.getIdentifierLoc(), 598570863f2e22b8ea7d7c504bd15e4f766af097df2caryclark@google.com Depth, Position, ParamName, T, TInfo); 599dac1d17027dcaa5596885a9f333979418b35001ccaryclark if (Invalid) 600dac1d17027dcaa5596885a9f333979418b35001ccaryclark Param->setInvalidDecl(); 6014431e7757cfcb8cfa99535eed0e9f156dabf95c2commit-bot@chromium.org 602570863f2e22b8ea7d7c504bd15e4f766af097df2caryclark@google.com if (D.getIdentifier()) { 603570863f2e22b8ea7d7c504bd15e4f766af097df2caryclark@google.com // Add the template parameter into the current scope. 604dac1d17027dcaa5596885a9f333979418b35001ccaryclark S->AddDecl(DeclPtrTy::make(Param)); 605dac1d17027dcaa5596885a9f333979418b35001ccaryclark IdResolver.AddDecl(Param); 606dac1d17027dcaa5596885a9f333979418b35001ccaryclark } 60707393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com return DeclPtrTy::make(Param); 608dac1d17027dcaa5596885a9f333979418b35001ccaryclark} 609dac1d17027dcaa5596885a9f333979418b35001ccaryclark 610dac1d17027dcaa5596885a9f333979418b35001ccaryclark/// \brief Adds a default argument to the given non-type template 611dac1d17027dcaa5596885a9f333979418b35001ccaryclark/// parameter. 612dac1d17027dcaa5596885a9f333979418b35001ccaryclarkvoid Sema::ActOnNonTypeTemplateParameterDefault(DeclPtrTy TemplateParamD, 613dac1d17027dcaa5596885a9f333979418b35001ccaryclark SourceLocation EqualLoc, 614dac1d17027dcaa5596885a9f333979418b35001ccaryclark ExprArg DefaultE) { 6154431e7757cfcb8cfa99535eed0e9f156dabf95c2commit-bot@chromium.org NonTypeTemplateParmDecl *TemplateParm 6164431e7757cfcb8cfa99535eed0e9f156dabf95c2commit-bot@chromium.org = cast<NonTypeTemplateParmDecl>(TemplateParamD.getAs<Decl>()); 617ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark Expr *Default = static_cast<Expr *>(DefaultE.get()); 618dac1d17027dcaa5596885a9f333979418b35001ccaryclark 619dac1d17027dcaa5596885a9f333979418b35001ccaryclark // C++ [temp.param]p14: 6204431e7757cfcb8cfa99535eed0e9f156dabf95c2commit-bot@chromium.org // A template-parameter shall not be used in its own default argument. 6214431e7757cfcb8cfa99535eed0e9f156dabf95c2commit-bot@chromium.org // FIXME: Implement this check! Needs a recursive walk over the types. 6224431e7757cfcb8cfa99535eed0e9f156dabf95c2commit-bot@chromium.org 6234431e7757cfcb8cfa99535eed0e9f156dabf95c2commit-bot@chromium.org // Check the well-formedness of the default template argument. 6244431e7757cfcb8cfa99535eed0e9f156dabf95c2commit-bot@chromium.org TemplateArgument Converted; 625ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark if (CheckTemplateArgument(TemplateParm, TemplateParm->getType(), Default, 6264431e7757cfcb8cfa99535eed0e9f156dabf95c2commit-bot@chromium.org Converted)) { 627dac1d17027dcaa5596885a9f333979418b35001ccaryclark TemplateParm->setInvalidDecl(); 628ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark return; 6294431e7757cfcb8cfa99535eed0e9f156dabf95c2commit-bot@chromium.org } 6304431e7757cfcb8cfa99535eed0e9f156dabf95c2commit-bot@chromium.org 6314431e7757cfcb8cfa99535eed0e9f156dabf95c2commit-bot@chromium.org TemplateParm->setDefaultArgument(DefaultE.takeAs<Expr>()); 6324431e7757cfcb8cfa99535eed0e9f156dabf95c2commit-bot@chromium.org} 6334431e7757cfcb8cfa99535eed0e9f156dabf95c2commit-bot@chromium.org 634dac1d17027dcaa5596885a9f333979418b35001ccaryclark 635570863f2e22b8ea7d7c504bd15e4f766af097df2caryclark@google.com/// ActOnTemplateTemplateParameter - Called when a C++ template template 636dac1d17027dcaa5596885a9f333979418b35001ccaryclark/// parameter (e.g. T in template <template <typename> class T> class array) 637dac1d17027dcaa5596885a9f333979418b35001ccaryclark/// has been parsed. S is the current scope. 638dac1d17027dcaa5596885a9f333979418b35001ccaryclarkSema::DeclPtrTy Sema::ActOnTemplateTemplateParameter(Scope* S, 639dac1d17027dcaa5596885a9f333979418b35001ccaryclark SourceLocation TmpLoc, 640dac1d17027dcaa5596885a9f333979418b35001ccaryclark TemplateParamsTy *Params, 641dac1d17027dcaa5596885a9f333979418b35001ccaryclark IdentifierInfo *Name, 642dac1d17027dcaa5596885a9f333979418b35001ccaryclark SourceLocation NameLoc, 643dac1d17027dcaa5596885a9f333979418b35001ccaryclark unsigned Depth, 644ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark unsigned Position) { 6454431e7757cfcb8cfa99535eed0e9f156dabf95c2commit-bot@chromium.org assert(S->isTemplateParamScope() && 6464431e7757cfcb8cfa99535eed0e9f156dabf95c2commit-bot@chromium.org "Template template parameter not in template parameter scope!"); 647570863f2e22b8ea7d7c504bd15e4f766af097df2caryclark@google.com 64807393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com // Construct the parameter object. 649570863f2e22b8ea7d7c504bd15e4f766af097df2caryclark@google.com TemplateTemplateParmDecl *Param = 6504431e7757cfcb8cfa99535eed0e9f156dabf95c2commit-bot@chromium.org TemplateTemplateParmDecl::Create(Context, Context.getTranslationUnitDecl(), 651ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark TmpLoc, Depth, Position, Name, 652570863f2e22b8ea7d7c504bd15e4f766af097df2caryclark@google.com (TemplateParameterList*)Params); 653dac1d17027dcaa5596885a9f333979418b35001ccaryclark 654570863f2e22b8ea7d7c504bd15e4f766af097df2caryclark@google.com // Make sure the parameter is valid. 655ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark // FIXME: Decl object is not currently invalidated anywhere so this doesn't 65607393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com // do anything yet. However, if the template parameter list or (eventual) 65707393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com // default value is ever invalidated, that will propagate here. 658ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark bool Invalid = false; 659ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark if (Invalid) { 66007393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com Param->setInvalidDecl(); 661ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark } 66207393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com 663ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark // If the tt-param has a name, then link the identifier into the scope 66407393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com // and lookup mechanisms. 66507393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com if (Name) { 66607393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com S->AddDecl(DeclPtrTy::make(Param)); 667ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark IdResolver.AddDecl(Param); 66807393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com } 66907393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com 670ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark return DeclPtrTy::make(Param); 67107393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com} 67207393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com 673ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark/// \brief Adds a default argument to the given template template 67407393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com/// parameter. 67507393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.comvoid Sema::ActOnTemplateTemplateParameterDefault(DeclPtrTy TemplateParamD, 676ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark SourceLocation EqualLoc, 677ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark const ParsedTemplateArgument &Default) { 678ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark TemplateTemplateParmDecl *TemplateParm 6794431e7757cfcb8cfa99535eed0e9f156dabf95c2commit-bot@chromium.org = cast<TemplateTemplateParmDecl>(TemplateParamD.getAs<Decl>()); 680ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark 681ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark // C++ [temp.param]p14: 682ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark // A template-parameter shall not be used in its own default argument. 683ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark // FIXME: Implement this check! Needs a recursive walk over the types. 684ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark 685ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark // Check only that we have a template template argument. We don't want to 686ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark // try to check well-formedness now, because our template template parameter 687ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark // might have dependent types in its template parameters, which we wouldn't 688ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark // be able to match now. 6894431e7757cfcb8cfa99535eed0e9f156dabf95c2commit-bot@chromium.org // 690ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark // If none of the template template parameter's template arguments mention 691ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark // other template parameters, we could actually perform more checking here. 692ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark // However, it isn't worth doing. 693ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark TemplateArgumentLoc DefaultArg = translateTemplateArgument(*this, Default); 694ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark if (DefaultArg.getArgument().getAsTemplate().isNull()) { 695ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark Diag(DefaultArg.getLocation(), diag::err_template_arg_not_class_template) 696ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark << DefaultArg.getSourceRange(); 697ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark return; 698ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark } 6994431e7757cfcb8cfa99535eed0e9f156dabf95c2commit-bot@chromium.org 7004431e7757cfcb8cfa99535eed0e9f156dabf95c2commit-bot@chromium.org TemplateParm->setDefaultArgument(DefaultArg); 701ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark} 7024431e7757cfcb8cfa99535eed0e9f156dabf95c2commit-bot@chromium.org 703ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark/// ActOnTemplateParameterList - Builds a TemplateParameterList that 704ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark/// contains the template parameters in Params/NumParams. 705ccec0f958ffc71a9986d236bc2eb335cb2111119caryclarkSema::TemplateParamsTy * 706ccec0f958ffc71a9986d236bc2eb335cb2111119caryclarkSema::ActOnTemplateParameterList(unsigned Depth, 707ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark SourceLocation ExportLoc, 708570863f2e22b8ea7d7c504bd15e4f766af097df2caryclark@google.com SourceLocation TemplateLoc, 709570863f2e22b8ea7d7c504bd15e4f766af097df2caryclark@google.com SourceLocation LAngleLoc, 710ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark DeclPtrTy *Params, unsigned NumParams, 711ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark SourceLocation RAngleLoc) { 712ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark if (ExportLoc.isValid()) 713570863f2e22b8ea7d7c504bd15e4f766af097df2caryclark@google.com Diag(ExportLoc, diag::warn_template_export_unsupported); 714570863f2e22b8ea7d7c504bd15e4f766af097df2caryclark@google.com 715ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark return TemplateParameterList::Create(Context, TemplateLoc, LAngleLoc, 716ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark (NamedDecl**)Params, NumParams, 717ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark RAngleLoc); 718ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark} 719ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark 720ccec0f958ffc71a9986d236bc2eb335cb2111119caryclarkstatic void SetNestedNameSpecifier(TagDecl *T, const CXXScopeSpec &SS) { 721ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark if (SS.isSet()) 722ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark T->setQualifierInfo(static_cast<NestedNameSpecifier*>(SS.getScopeRep()), 723ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark SS.getRange()); 724570863f2e22b8ea7d7c504bd15e4f766af097df2caryclark@google.com} 725570863f2e22b8ea7d7c504bd15e4f766af097df2caryclark@google.com 726ccec0f958ffc71a9986d236bc2eb335cb2111119caryclarkSema::DeclResult 727ccec0f958ffc71a9986d236bc2eb335cb2111119caryclarkSema::CheckClassTemplate(Scope *S, unsigned TagSpec, TagUseKind TUK, 728570863f2e22b8ea7d7c504bd15e4f766af097df2caryclark@google.com SourceLocation KWLoc, CXXScopeSpec &SS, 729ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark IdentifierInfo *Name, SourceLocation NameLoc, 730ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark AttributeList *Attr, 731570863f2e22b8ea7d7c504bd15e4f766af097df2caryclark@google.com TemplateParameterList *TemplateParams, 732ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark AccessSpecifier AS) { 733ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark assert(TemplateParams && TemplateParams->size() > 0 && 734ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark "No template parameters"); 735ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark assert(TUK != TUK_Reference && "Can only declare or define class templates"); 736ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark bool Invalid = false; 737ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark 738ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark // Check that we can declare a template here. 739ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark if (CheckTemplateDeclScope(S, TemplateParams)) 740ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark return true; 741ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark 7424431e7757cfcb8cfa99535eed0e9f156dabf95c2commit-bot@chromium.org TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); 743ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark assert(Kind != TTK_Enum && "can't build template of enumerated type"); 744ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark 745ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark // There is no such thing as an unnamed class template. 746ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark if (!Name) { 747ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark Diag(KWLoc, diag::err_template_unnamed_class); 748ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark return true; 749ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark } 750ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark 751570863f2e22b8ea7d7c504bd15e4f766af097df2caryclark@google.com // Find any previous declaration with this name. 752ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark DeclContext *SemanticContext; 75307393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com LookupResult Previous(*this, Name, NameLoc, LookupOrdinaryName, 75407393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com ForRedeclaration); 755ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark if (SS.isNotEmpty() && !SS.isInvalid()) { 756ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark SemanticContext = computeDeclContext(SS, true); 757ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark if (!SemanticContext) { 758dac1d17027dcaa5596885a9f333979418b35001ccaryclark // FIXME: Produce a reasonable diagnostic here 759ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark return true; 760dac1d17027dcaa5596885a9f333979418b35001ccaryclark } 761dac1d17027dcaa5596885a9f333979418b35001ccaryclark 762ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark if (RequireCompleteDeclContext(SS, SemanticContext)) 763ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark return true; 764ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark 765ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark LookupQualifiedName(Previous, SemanticContext); 766ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark } else { 767ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark SemanticContext = CurContext; 768ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark LookupName(Previous, S); 769ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark } 770ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark 771ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark if (Previous.isAmbiguous()) 772ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark return true; 773ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark 774ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark NamedDecl *PrevDecl = 0; 775ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark if (Previous.begin() != Previous.end()) 776a2bbc6e19d5332e81784e582c290cc060f40c4c7caryclark@google.com PrevDecl = (*Previous.begin())->getUnderlyingDecl(); 777ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark 778ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark // If there is a previous declaration with the same name, check 779ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark // whether this is a valid redeclaration. 780a2bbc6e19d5332e81784e582c290cc060f40c4c7caryclark@google.com ClassTemplateDecl *PrevClassTemplate 781ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark = dyn_cast_or_null<ClassTemplateDecl>(PrevDecl); 782ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark 7834431e7757cfcb8cfa99535eed0e9f156dabf95c2commit-bot@chromium.org // We may have found the injected-class-name of a class template, 7844431e7757cfcb8cfa99535eed0e9f156dabf95c2commit-bot@chromium.org // class template partial specialization, or class template specialization. 78507393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com // In these cases, grab the template that is being defined or specialized. 78607393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com if (!PrevClassTemplate && PrevDecl && isa<CXXRecordDecl>(PrevDecl) && 78707393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com cast<CXXRecordDecl>(PrevDecl)->isInjectedClassName()) { 78807393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com PrevDecl = cast<CXXRecordDecl>(PrevDecl->getDeclContext()); 78907393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com PrevClassTemplate 79007393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com = cast<CXXRecordDecl>(PrevDecl)->getDescribedClassTemplate(); 79107393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com if (!PrevClassTemplate && isa<ClassTemplateSpecializationDecl>(PrevDecl)) { 792ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark PrevClassTemplate 793ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark = cast<ClassTemplateSpecializationDecl>(PrevDecl) 794ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark ->getSpecializedTemplate(); 795ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark } 796ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark } 797ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark 798ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark if (TUK == TUK_Friend) { 799ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark // C++ [namespace.memdef]p3: 80007393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com // [...] When looking for a prior declaration of a class or a function 80107393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com // declared as a friend, and when the name of the friend class or 80207393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com // function is neither a qualified name nor a template-id, scopes outside 80307393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com // the innermost enclosing namespace scope are not considered. 80407393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com if (!SS.isSet()) { 805ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark DeclContext *OutermostContext = CurContext; 806ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark while (!OutermostContext->isFileContext()) 807cffbcc3b9665f2c928544b6fc6b8a0e22a4210fbcaryclark@google.com OutermostContext = OutermostContext->getLookupParent(); 808cffbcc3b9665f2c928544b6fc6b8a0e22a4210fbcaryclark@google.com 809ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark if (PrevDecl && 810ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark (OutermostContext->Equals(PrevDecl->getDeclContext()) || 81107393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com OutermostContext->Encloses(PrevDecl->getDeclContext()))) { 81207393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com SemanticContext = PrevDecl->getDeclContext(); 813ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark } else { 814ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark // Declarations in outer scopes don't matter. However, the outermost 815ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark // context we computed is the semantic context for our new 816ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark // declaration. 817ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark PrevDecl = PrevClassTemplate = 0; 8184431e7757cfcb8cfa99535eed0e9f156dabf95c2commit-bot@chromium.org SemanticContext = OutermostContext; 819ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark } 820570863f2e22b8ea7d7c504bd15e4f766af097df2caryclark@google.com } 8214431e7757cfcb8cfa99535eed0e9f156dabf95c2commit-bot@chromium.org 8227eaa53d8f7e48fd17d02b5e3bd91f90e9c1899efcaryclark@google.com if (CurContext->isDependentContext()) { 823ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark // If this is a dependent context, we don't want to link the friend 8247eaa53d8f7e48fd17d02b5e3bd91f90e9c1899efcaryclark@google.com // class template to the template in scope, because that would perform 8257eaa53d8f7e48fd17d02b5e3bd91f90e9c1899efcaryclark@google.com // checking of the template parameter lists that can't be performed 826ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark // until the outer context is instantiated. 8274431e7757cfcb8cfa99535eed0e9f156dabf95c2commit-bot@chromium.org PrevDecl = PrevClassTemplate = 0; 82807393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com } 829ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark } else if (PrevDecl && !isDeclInScope(PrevDecl, SemanticContext, S)) 83007393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com PrevDecl = PrevClassTemplate = 0; 83107393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com 8324431e7757cfcb8cfa99535eed0e9f156dabf95c2commit-bot@chromium.org if (PrevClassTemplate) { 8334431e7757cfcb8cfa99535eed0e9f156dabf95c2commit-bot@chromium.org // Ensure that the template parameter lists are compatible. 8344431e7757cfcb8cfa99535eed0e9f156dabf95c2commit-bot@chromium.org if (!TemplateParameterListsAreEqual(TemplateParams, 83507393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com PrevClassTemplate->getTemplateParameters(), 836ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark /*Complain=*/true, 8378cb1daaa1e4343eb60a7c4f21c12e33de30dad64commit-bot@chromium.org TPL_TemplateMatch)) 8388cb1daaa1e4343eb60a7c4f21c12e33de30dad64commit-bot@chromium.org return true; 839ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark 8408cb1daaa1e4343eb60a7c4f21c12e33de30dad64commit-bot@chromium.org // C++ [temp.class]p4: 8418cb1daaa1e4343eb60a7c4f21c12e33de30dad64commit-bot@chromium.org // In a redeclaration, partial specialization, explicit 842ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark // specialization or explicit instantiation of a class template, 84307393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com // the class-key shall agree in kind with the original class 84407393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com // template declaration (7.1.5.3). 84507393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com RecordDecl *PrevRecordDecl = PrevClassTemplate->getTemplatedDecl(); 8464431e7757cfcb8cfa99535eed0e9f156dabf95c2commit-bot@chromium.org if (!isAcceptableTagRedeclaration(PrevRecordDecl, Kind, KWLoc, *Name)) { 84707393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com Diag(KWLoc, diag::err_use_with_wrong_tag) 84807393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com << Name 84907393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com << FixItHint::CreateReplacement(KWLoc, PrevRecordDecl->getKindName()); 85007393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com Diag(PrevRecordDecl->getLocation(), diag::note_previous_use); 85107393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com Kind = PrevRecordDecl->getTagKind(); 85207393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com } 85307393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com 85407393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com // Check for redefinition of this class template. 8554431e7757cfcb8cfa99535eed0e9f156dabf95c2commit-bot@chromium.org if (TUK == TUK_Definition) { 85607393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com if (TagDecl *Def = PrevRecordDecl->getDefinition()) { 85707393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com Diag(NameLoc, diag::err_redefinition) << Name; 85807393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com Diag(Def->getLocation(), diag::note_previous_definition); 85907393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com // FIXME: Would it make sense to try to "forget" the previous 860570863f2e22b8ea7d7c504bd15e4f766af097df2caryclark@google.com // definition, as part of error recovery? 86107393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com return true; 86207393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com } 86307393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com } 86407393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com } else if (PrevDecl && PrevDecl->isTemplateParameter()) { 86507393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com // Maybe we will complain about the shadowed template parameter. 866570863f2e22b8ea7d7c504bd15e4f766af097df2caryclark@google.com DiagnoseTemplateParameterShadow(NameLoc, PrevDecl); 867ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark // Just pretend that we didn't see the previous declaration. 868570863f2e22b8ea7d7c504bd15e4f766af097df2caryclark@google.com PrevDecl = 0; 869ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark } else if (PrevDecl) { 87007393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com // C++ [temp]p5: 8714431e7757cfcb8cfa99535eed0e9f156dabf95c2commit-bot@chromium.org // A class template shall not have the same name as any other 87207393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com // template, class, function, object, enumeration, enumerator, 87307393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com // namespace, or type in the same scope (3.3), except as specified 874ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark // in (14.5.4). 875ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark Diag(NameLoc, diag::err_redefinition_different_kind) << Name; 876ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark Diag(PrevDecl->getLocation(), diag::note_previous_definition); 877ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark return true; 878ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark } 879ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark 88007393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com // Check the template parameter list of this declaration, possibly 88107393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com // merging in the template parameter list from the previous class 8824431e7757cfcb8cfa99535eed0e9f156dabf95c2commit-bot@chromium.org // template declaration. 883ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark if (CheckTemplateParameterList(TemplateParams, 88407393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com PrevClassTemplate? PrevClassTemplate->getTemplateParameters() : 0, 88507393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com TPC_ClassTemplate)) 88607393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com Invalid = true; 88707393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com 88807393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com if (SS.isSet()) { 88907393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com // If the name of the template was qualified, we must be defining the 89007393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com // template out-of-line. 89107393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com if (!SS.isInvalid() && !Invalid && !PrevClassTemplate && 89207393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com !(TUK == TUK_Friend && CurContext->isDependentContext())) 89307393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com Diag(NameLoc, diag::err_member_def_does_not_match) 89407393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com << Name << SemanticContext << SS.getRange(); 89507393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com } 89607393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com 897ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark CXXRecordDecl *NewClass = 898ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark CXXRecordDecl::Create(Context, Kind, SemanticContext, NameLoc, Name, KWLoc, 899ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark PrevClassTemplate? 900ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark PrevClassTemplate->getTemplatedDecl() : 0, 901ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark /*DelayTypeCreation=*/true); 902ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark SetNestedNameSpecifier(NewClass, SS); 903ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark 904ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark ClassTemplateDecl *NewTemplate 90507393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com = ClassTemplateDecl::Create(Context, SemanticContext, NameLoc, 90607393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com DeclarationName(Name), TemplateParams, 90707393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com NewClass, PrevClassTemplate); 90807393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com NewClass->setDescribedClassTemplate(NewTemplate); 90907393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com 910ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark // Build the type for the class template declaration now. 911ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark QualType T = NewTemplate->getInjectedClassNameSpecialization(Context); 912570863f2e22b8ea7d7c504bd15e4f766af097df2caryclark@google.com T = Context.getInjectedClassNameType(NewClass, T); 913570863f2e22b8ea7d7c504bd15e4f766af097df2caryclark@google.com assert(T->isDependentType() && "Class template type is not dependent?"); 914ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark (void)T; 915ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark 91607393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com // If we are providing an explicit specialization of a member that is a 91707393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com // class template, make a note of that. 918ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark if (PrevClassTemplate && 919ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark PrevClassTemplate->getInstantiatedFromMemberTemplate()) 920ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark PrevClassTemplate->setMemberSpecialization(); 921ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark 922ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark // Set the access specifier. 9234431e7757cfcb8cfa99535eed0e9f156dabf95c2commit-bot@chromium.org if (!Invalid && TUK != TUK_Friend) 924ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark SetMemberAccessSpecifier(NewTemplate, PrevClassTemplate, AS); 925570863f2e22b8ea7d7c504bd15e4f766af097df2caryclark@google.com 92607393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com // Set the lexical context of these templates 92707393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com NewClass->setLexicalDeclContext(CurContext); 928ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark NewTemplate->setLexicalDeclContext(CurContext); 929ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark 930ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark if (TUK == TUK_Definition) 931ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark NewClass->startDefinition(); 932ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark 933ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark if (Attr) 934ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark ProcessDeclAttributeList(S, NewClass, Attr); 93507393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com 93607393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com if (TUK != TUK_Friend) 9374431e7757cfcb8cfa99535eed0e9f156dabf95c2commit-bot@chromium.org PushOnScopeChains(NewTemplate, S); 9384431e7757cfcb8cfa99535eed0e9f156dabf95c2commit-bot@chromium.org else { 9394431e7757cfcb8cfa99535eed0e9f156dabf95c2commit-bot@chromium.org if (PrevClassTemplate && PrevClassTemplate->getAccess() != AS_none) { 94007393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com NewTemplate->setAccess(PrevClassTemplate->getAccess()); 941ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark NewClass->setAccess(PrevClassTemplate->getAccess()); 9424431e7757cfcb8cfa99535eed0e9f156dabf95c2commit-bot@chromium.org } 94307393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com 94407393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com NewTemplate->setObjectOfFriendDecl(/* PreviouslyDeclared = */ 945ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark PrevClassTemplate != NULL); 94607393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com 94707393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com // Friend templates are visible in fairly strange ways. 94807393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com if (!CurContext->isDependentContext()) { 94907393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com DeclContext *DC = SemanticContext->getLookupContext(); 95007393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com DC->makeDeclVisibleInContext(NewTemplate, /* Recoverable = */ false); 9514431e7757cfcb8cfa99535eed0e9f156dabf95c2commit-bot@chromium.org if (Scope *EnclosingScope = getScopeForDeclContext(S, DC)) 95207393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com PushOnScopeChains(NewTemplate, EnclosingScope, 95307393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com /* AddToContext = */ false); 95407393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com } 95507393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com 95607393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com FriendDecl *Friend = FriendDecl::Create(Context, CurContext, 95707393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com NewClass->getLocation(), 95807393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com NewTemplate, 95907393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com /*FIXME:*/NewClass->getLocation()); 96007393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com Friend->setAccess(AS_public); 96107393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com CurContext->addDecl(Friend); 962570863f2e22b8ea7d7c504bd15e4f766af097df2caryclark@google.com } 963ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark 964570863f2e22b8ea7d7c504bd15e4f766af097df2caryclark@google.com if (Invalid) { 965ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark NewTemplate->setInvalidDecl(); 96607393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com NewClass->setInvalidDecl(); 9674431e7757cfcb8cfa99535eed0e9f156dabf95c2commit-bot@chromium.org } 96807393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com return DeclPtrTy::make(NewTemplate); 96907393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com} 970ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark 971ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark/// \brief Diagnose the presence of a default template argument on a 972ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark/// template parameter, which is ill-formed in certain contexts. 973ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark/// 974ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark/// \returns true if the default template argument should be dropped. 975ccec0f958ffc71a9986d236bc2eb335cb2111119caryclarkstatic bool DiagnoseDefaultTemplateArgument(Sema &S, 97607393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com Sema::TemplateParamListContext TPC, 97707393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com SourceLocation ParamLoc, 9784431e7757cfcb8cfa99535eed0e9f156dabf95c2commit-bot@chromium.org SourceRange DefArgRange) { 979ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark switch (TPC) { 98007393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com case Sema::TPC_ClassTemplate: 98107393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com return false; 98207393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com 98307393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com case Sema::TPC_FunctionTemplate: 98407393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com // C++ [temp.param]p9: 98507393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com // A default template-argument shall not be specified in a 98607393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com // function template declaration or a function template 98707393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com // definition [...] 98807393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com // (This sentence is not in C++0x, per DR226). 98907393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com if (!S.getLangOptions().CPlusPlus0x) 99007393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com S.Diag(ParamLoc, 99107393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com diag::err_template_parameter_default_in_function_template) 99207393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com << DefArgRange; 993ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark return false; 994ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark 995ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark case Sema::TPC_ClassTemplateMember: 996ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark // C++0x [temp.param]p9: 997ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark // A default template-argument shall not be specified in the 998ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark // template-parameter-lists of the definition of a member of a 999ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark // class template that appears outside of the member's class. 1000ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark S.Diag(ParamLoc, diag::err_template_parameter_default_template_member) 1001ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark << DefArgRange; 1002ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark return true; 100307393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com 100407393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com case Sema::TPC_FriendFunctionTemplate: 100507393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com // C++ [temp.param]p9: 1006ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark // A default template-argument shall not be specified in a 1007ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark // friend template declaration. 100807393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com S.Diag(ParamLoc, diag::err_template_parameter_default_friend_template) 100907393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com << DefArgRange; 1010ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark return true; 1011ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark 101207393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com // FIXME: C++0x [temp.param]p9 allows default template-arguments 101307393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com // for friend function templates if there is only a single 1014ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark // declaration (and it is a definition). Strange! 1015ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark } 1016ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark 1017ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark return false; 1018ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark} 1019ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark 1020ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark/// \brief Checks the validity of a template parameter list, possibly 1021ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark/// considering the template parameter list from a previous 1022ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark/// declaration. 1023ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark/// 1024ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark/// If an "old" template parameter list is provided, it must be 1025ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark/// equivalent (per TemplateParameterListsAreEqual) to the "new" 10264431e7757cfcb8cfa99535eed0e9f156dabf95c2commit-bot@chromium.org/// template parameter list. 10274431e7757cfcb8cfa99535eed0e9f156dabf95c2commit-bot@chromium.org/// 10284431e7757cfcb8cfa99535eed0e9f156dabf95c2commit-bot@chromium.org/// \param NewParams Template parameter list for a new template 1029570863f2e22b8ea7d7c504bd15e4f766af097df2caryclark@google.com/// declaration. This template parameter list will be updated with any 10304431e7757cfcb8cfa99535eed0e9f156dabf95c2commit-bot@chromium.org/// default arguments that are carried through from the previous 103107393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com/// template parameter list. 103207393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com/// 1033ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark/// \param OldParams If provided, template parameter list from a 103407393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com/// previous declaration of the same template. Default template 103507393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com/// arguments will be merged from the old template parameter list to 103607393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com/// the new template parameter list. 103707393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com/// 103807393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com/// \param TPC Describes the context in which we are checking the given 103907393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com/// template parameter list. 104007393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com/// 10414431e7757cfcb8cfa99535eed0e9f156dabf95c2commit-bot@chromium.org/// \returns true if an error occurred, false otherwise. 10424431e7757cfcb8cfa99535eed0e9f156dabf95c2commit-bot@chromium.orgbool Sema::CheckTemplateParameterList(TemplateParameterList *NewParams, 10434431e7757cfcb8cfa99535eed0e9f156dabf95c2commit-bot@chromium.org TemplateParameterList *OldParams, 104407393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com TemplateParamListContext TPC) { 10454431e7757cfcb8cfa99535eed0e9f156dabf95c2commit-bot@chromium.org bool Invalid = false; 10464431e7757cfcb8cfa99535eed0e9f156dabf95c2commit-bot@chromium.org 1047ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark // C++ [temp.param]p10: 104807393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com // The set of default template-arguments available for use with a 104907393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com // template declaration or definition is obtained by merging the 105007393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com // default arguments from the definition (if in scope) and all 105107393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com // declarations in scope in the same way default function 105207393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com // arguments are (8.3.6). 105307393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com bool SawDefaultArgument = false; 105407393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com SourceLocation PreviousDefaultArgLoc; 105507393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com 105607393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com bool SawParameterPack = false; 105707393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com SourceLocation ParameterPackLoc; 105807393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com 105907393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com // Dummy initialization to avoid warnings. 106007393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com TemplateParameterList::iterator OldParam = NewParams->end(); 1061ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark if (OldParams) 1062ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark OldParam = OldParams->begin(); 106307393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com 106407393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com for (TemplateParameterList::iterator NewParam = NewParams->begin(), 106507393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com NewParamEnd = NewParams->end(); 1066ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark NewParam != NewParamEnd; ++NewParam) { 1067ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark // Variables used to diagnose redundant default arguments 1068ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark bool RedundantDefaultArg = false; 10698cb1daaa1e4343eb60a7c4f21c12e33de30dad64commit-bot@chromium.org SourceLocation OldDefaultLoc; 1070ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark SourceLocation NewDefaultLoc; 1071ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark 1072ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark // Variables used to diagnose missing default arguments 107307393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com bool MissingDefaultArg = false; 1074ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark 1075ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark // C++0x [temp.param]p11: 107607393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com // If a template parameter of a class template is a template parameter pack, 107707393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com // it must be the last template parameter. 107807393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com if (SawParameterPack) { 107907393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com Diag(ParameterPackLoc, 1080ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark diag::err_template_param_pack_must_be_last_template_parameter); 1081ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark Invalid = true; 108207393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com } 1083ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark 1084ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark if (TemplateTypeParmDecl *NewTypeParm 1085ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark = dyn_cast<TemplateTypeParmDecl>(*NewParam)) { 1086ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark // Check the presence of a default argument here. 108707393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com if (NewTypeParm->hasDefaultArgument() && 108807393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com DiagnoseDefaultTemplateArgument(*this, TPC, 108907393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com NewTypeParm->getLocation(), 1090ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark NewTypeParm->getDefaultArgumentInfo()->getTypeLoc() 10914431e7757cfcb8cfa99535eed0e9f156dabf95c2commit-bot@chromium.org .getFullSourceRange())) 10924431e7757cfcb8cfa99535eed0e9f156dabf95c2commit-bot@chromium.org NewTypeParm->removeDefaultArgument(); 1093ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark 10944431e7757cfcb8cfa99535eed0e9f156dabf95c2commit-bot@chromium.org // Merge default arguments for template type parameters. 10954431e7757cfcb8cfa99535eed0e9f156dabf95c2commit-bot@chromium.org TemplateTypeParmDecl *OldTypeParm 1096e4097e3a0b10bb0047a45b6949ca01826f0807a7caryclark = OldParams? cast<TemplateTypeParmDecl>(*OldParam) : 0; 1097e4097e3a0b10bb0047a45b6949ca01826f0807a7caryclark 10984431e7757cfcb8cfa99535eed0e9f156dabf95c2commit-bot@chromium.org if (NewTypeParm->isParameterPack()) { 10994431e7757cfcb8cfa99535eed0e9f156dabf95c2commit-bot@chromium.org assert(!NewTypeParm->hasDefaultArgument() && 1100570863f2e22b8ea7d7c504bd15e4f766af097df2caryclark@google.com "Parameter packs can't have a default argument!"); 110107393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com SawParameterPack = true; 11024431e7757cfcb8cfa99535eed0e9f156dabf95c2commit-bot@chromium.org ParameterPackLoc = NewTypeParm->getLocation(); 11034431e7757cfcb8cfa99535eed0e9f156dabf95c2commit-bot@chromium.org } else if (OldTypeParm && OldTypeParm->hasDefaultArgument() && 11044431e7757cfcb8cfa99535eed0e9f156dabf95c2commit-bot@chromium.org NewTypeParm->hasDefaultArgument()) { 11054431e7757cfcb8cfa99535eed0e9f156dabf95c2commit-bot@chromium.org OldDefaultLoc = OldTypeParm->getDefaultArgumentLoc(); 1106ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark NewDefaultLoc = NewTypeParm->getDefaultArgumentLoc(); 11074431e7757cfcb8cfa99535eed0e9f156dabf95c2commit-bot@chromium.org SawDefaultArgument = true; 11084431e7757cfcb8cfa99535eed0e9f156dabf95c2commit-bot@chromium.org RedundantDefaultArg = true; 110965f553182ab7069378ef863d30094d0327f178d0caryclark PreviousDefaultArgLoc = NewDefaultLoc; 111065f553182ab7069378ef863d30094d0327f178d0caryclark } else if (OldTypeParm && OldTypeParm->hasDefaultArgument()) { 111165f553182ab7069378ef863d30094d0327f178d0caryclark // Merge the default argument from the old declaration to the 111265f553182ab7069378ef863d30094d0327f178d0caryclark // new declaration. 11134431e7757cfcb8cfa99535eed0e9f156dabf95c2commit-bot@chromium.org SawDefaultArgument = true; 11144431e7757cfcb8cfa99535eed0e9f156dabf95c2commit-bot@chromium.org NewTypeParm->setDefaultArgument(OldTypeParm->getDefaultArgumentInfo(), 11154431e7757cfcb8cfa99535eed0e9f156dabf95c2commit-bot@chromium.org true); 111607393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com PreviousDefaultArgLoc = OldTypeParm->getDefaultArgumentLoc(); 11174431e7757cfcb8cfa99535eed0e9f156dabf95c2commit-bot@chromium.org } else if (NewTypeParm->hasDefaultArgument()) { 11184431e7757cfcb8cfa99535eed0e9f156dabf95c2commit-bot@chromium.org SawDefaultArgument = true; 111965f553182ab7069378ef863d30094d0327f178d0caryclark PreviousDefaultArgLoc = NewTypeParm->getDefaultArgumentLoc(); 112065f553182ab7069378ef863d30094d0327f178d0caryclark } else if (SawDefaultArgument) 112165f553182ab7069378ef863d30094d0327f178d0caryclark MissingDefaultArg = true; 11224431e7757cfcb8cfa99535eed0e9f156dabf95c2commit-bot@chromium.org } else if (NonTypeTemplateParmDecl *NewNonTypeParm 11234431e7757cfcb8cfa99535eed0e9f156dabf95c2commit-bot@chromium.org = dyn_cast<NonTypeTemplateParmDecl>(*NewParam)) { 11244431e7757cfcb8cfa99535eed0e9f156dabf95c2commit-bot@chromium.org // Check the presence of a default argument here. 112507393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com if (NewNonTypeParm->hasDefaultArgument() && 112607393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com DiagnoseDefaultTemplateArgument(*this, TPC, 11274431e7757cfcb8cfa99535eed0e9f156dabf95c2commit-bot@chromium.org NewNonTypeParm->getLocation(), 11288cb1daaa1e4343eb60a7c4f21c12e33de30dad64commit-bot@chromium.org NewNonTypeParm->getDefaultArgument()->getSourceRange())) { 11298cb1daaa1e4343eb60a7c4f21c12e33de30dad64commit-bot@chromium.org NewNonTypeParm->getDefaultArgument()->Destroy(Context); 113007393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com NewNonTypeParm->setDefaultArgument(0); 11318cb1daaa1e4343eb60a7c4f21c12e33de30dad64commit-bot@chromium.org } 11328cb1daaa1e4343eb60a7c4f21c12e33de30dad64commit-bot@chromium.org 11338cb1daaa1e4343eb60a7c4f21c12e33de30dad64commit-bot@chromium.org // Merge default arguments for non-type template parameters 1134ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark NonTypeTemplateParmDecl *OldNonTypeParm 1135ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark = OldParams? cast<NonTypeTemplateParmDecl>(*OldParam) : 0; 1136ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument() && 1137ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark NewNonTypeParm->hasDefaultArgument()) { 11388cb1daaa1e4343eb60a7c4f21c12e33de30dad64commit-bot@chromium.org OldDefaultLoc = OldNonTypeParm->getDefaultArgumentLoc(); 11398cb1daaa1e4343eb60a7c4f21c12e33de30dad64commit-bot@chromium.org NewDefaultLoc = NewNonTypeParm->getDefaultArgumentLoc(); 11408cb1daaa1e4343eb60a7c4f21c12e33de30dad64commit-bot@chromium.org SawDefaultArgument = true; 11414431e7757cfcb8cfa99535eed0e9f156dabf95c2commit-bot@chromium.org RedundantDefaultArg = true; 11428cb1daaa1e4343eb60a7c4f21c12e33de30dad64commit-bot@chromium.org PreviousDefaultArgLoc = NewDefaultLoc; 11438cb1daaa1e4343eb60a7c4f21c12e33de30dad64commit-bot@chromium.org } else if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument()) { 11448cb1daaa1e4343eb60a7c4f21c12e33de30dad64commit-bot@chromium.org // Merge the default argument from the old declaration to the 11458cb1daaa1e4343eb60a7c4f21c12e33de30dad64commit-bot@chromium.org // new declaration. 11468cb1daaa1e4343eb60a7c4f21c12e33de30dad64commit-bot@chromium.org SawDefaultArgument = true; 114707393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com // FIXME: We need to create a new kind of "default argument" 1148ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark // expression that points to a previous template template 1149ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark // parameter. 1150e4097e3a0b10bb0047a45b6949ca01826f0807a7caryclark NewNonTypeParm->setDefaultArgument( 1151ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark OldNonTypeParm->getDefaultArgument()); 115207393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com PreviousDefaultArgLoc = OldNonTypeParm->getDefaultArgumentLoc(); 1153ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark } else if (NewNonTypeParm->hasDefaultArgument()) { 11548cb1daaa1e4343eb60a7c4f21c12e33de30dad64commit-bot@chromium.org SawDefaultArgument = true; 1155ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark PreviousDefaultArgLoc = NewNonTypeParm->getDefaultArgumentLoc(); 1156ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark } else if (SawDefaultArgument) 115707393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com MissingDefaultArg = true; 115807393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com } else { 115907393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com // Check the presence of a default argument here. 116007393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com TemplateTemplateParmDecl *NewTemplateParm 1161ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark = cast<TemplateTemplateParmDecl>(*NewParam); 116207393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com if (NewTemplateParm->hasDefaultArgument() && 116307393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com DiagnoseDefaultTemplateArgument(*this, TPC, 116407393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com NewTemplateParm->getLocation(), 116507393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com NewTemplateParm->getDefaultArgument().getSourceRange())) 116607393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com NewTemplateParm->setDefaultArgument(TemplateArgumentLoc()); 116707393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com 1168ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark // Merge default arguments for template template parameters 1169ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark TemplateTemplateParmDecl *OldTemplateParm 117065f553182ab7069378ef863d30094d0327f178d0caryclark = OldParams? cast<TemplateTemplateParmDecl>(*OldParam) : 0; 117165f553182ab7069378ef863d30094d0327f178d0caryclark if (OldTemplateParm && OldTemplateParm->hasDefaultArgument() && 1172ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark NewTemplateParm->hasDefaultArgument()) { 1173ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark OldDefaultLoc = OldTemplateParm->getDefaultArgument().getLocation(); 1174ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark NewDefaultLoc = NewTemplateParm->getDefaultArgument().getLocation(); 117507393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com SawDefaultArgument = true; 117607393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com RedundantDefaultArg = true; 1177ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark PreviousDefaultArgLoc = NewDefaultLoc; 1178ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark } else if (OldTemplateParm && OldTemplateParm->hasDefaultArgument()) { 1179ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark // Merge the default argument from the old declaration to the 1180ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark // new declaration. 1181ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark SawDefaultArgument = true; 1182ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark // FIXME: We need to create a new kind of "default argument" expression 1183ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark // that points to a previous template template parameter. 1184ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark NewTemplateParm->setDefaultArgument( 1185ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark OldTemplateParm->getDefaultArgument()); 1186ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark PreviousDefaultArgLoc 1187ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark = OldTemplateParm->getDefaultArgument().getLocation(); 1188ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark } else if (NewTemplateParm->hasDefaultArgument()) { 1189ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark SawDefaultArgument = true; 1190ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark PreviousDefaultArgLoc 119165f553182ab7069378ef863d30094d0327f178d0caryclark = NewTemplateParm->getDefaultArgument().getLocation(); 11924431e7757cfcb8cfa99535eed0e9f156dabf95c2commit-bot@chromium.org } else if (SawDefaultArgument) 1193ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark MissingDefaultArg = true; 119465f553182ab7069378ef863d30094d0327f178d0caryclark } 119507393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com 119665f553182ab7069378ef863d30094d0327f178d0caryclark if (RedundantDefaultArg) { 11974431e7757cfcb8cfa99535eed0e9f156dabf95c2commit-bot@chromium.org // C++ [temp.param]p12: 119865f553182ab7069378ef863d30094d0327f178d0caryclark // A template-parameter shall not be given default arguments 11994431e7757cfcb8cfa99535eed0e9f156dabf95c2commit-bot@chromium.org // by two different declarations in the same scope. 120065f553182ab7069378ef863d30094d0327f178d0caryclark Diag(NewDefaultLoc, diag::err_template_param_default_arg_redefinition); 12014431e7757cfcb8cfa99535eed0e9f156dabf95c2commit-bot@chromium.org Diag(OldDefaultLoc, diag::note_template_param_prev_default_arg); 120265f553182ab7069378ef863d30094d0327f178d0caryclark Invalid = true; 120307393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com } else if (MissingDefaultArg) { 120407393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com // C++ [temp.param]p11: 120507393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com // If a template-parameter has a default template-argument, 120607393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com // all subsequent template-parameters shall have a default 120707393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com // template-argument supplied. 120807393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com Diag((*NewParam)->getLocation(), 120907393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com diag::err_template_param_default_arg_missing); 121007393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com Diag(PreviousDefaultArgLoc, diag::note_template_param_prev_default_arg); 121107393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com Invalid = true; 121207393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com } 121307393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com 1214ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark // If we have an old template parameter list that we're merging 1215ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark // in, move on to the next parameter. 1216ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark if (OldParams) 121707393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com ++OldParam; 1218277c3f87656c44e0a651ed0dd56efa16c0ab07b4reed@google.com } 1219ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark 1220ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark return Invalid; 122107393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com} 1222dac1d17027dcaa5596885a9f333979418b35001ccaryclark 122307393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com/// \brief Match the given template parameter lists to the given scope 122407393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com/// specifier, returning the template parameter list that applies to the 12254431e7757cfcb8cfa99535eed0e9f156dabf95c2commit-bot@chromium.org/// name. 12264431e7757cfcb8cfa99535eed0e9f156dabf95c2commit-bot@chromium.org/// 12274431e7757cfcb8cfa99535eed0e9f156dabf95c2commit-bot@chromium.org/// \param DeclStartLoc the start of the declaration that has a scope 122807393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com/// specifier or a template parameter list. 1229ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark/// 123007393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com/// \param SS the scope specifier that will be matched to the given template 1231ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark/// parameter lists. This scope specifier precedes a qualified name that is 123207393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com/// being declared. 123307393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com/// 123407393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com/// \param ParamLists the template parameter lists, from the outermost to the 123507393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com/// innermost template parameter lists. 123607393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com/// 123707393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com/// \param NumParamLists the number of template parameter lists in ParamLists. 123807393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com/// 123907393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com/// \param IsFriend Whether to apply the slightly different rules for 1240dac1d17027dcaa5596885a9f333979418b35001ccaryclark/// matching template parameters to scope specifiers in friend 1241dac1d17027dcaa5596885a9f333979418b35001ccaryclark/// declarations. 1242dac1d17027dcaa5596885a9f333979418b35001ccaryclark/// 124365f553182ab7069378ef863d30094d0327f178d0caryclark/// \param IsExplicitSpecialization will be set true if the entity being 124465f553182ab7069378ef863d30094d0327f178d0caryclark/// declared is an explicit specialization, false otherwise. 12457eaa53d8f7e48fd17d02b5e3bd91f90e9c1899efcaryclark@google.com/// 124665f553182ab7069378ef863d30094d0327f178d0caryclark/// \returns the template parameter list, if any, that corresponds to the 124765f553182ab7069378ef863d30094d0327f178d0caryclark/// name that is preceded by the scope specifier @p SS. This template 124807393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com/// parameter list may be have template parameters (if we're declaring a 124907393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com/// template) or may have no template parameters (if we're declaring a 1250ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark/// template specialization), or may be NULL (if we were's declaring isn't 1251ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark/// itself a template). 1252ccec0f958ffc71a9986d236bc2eb335cb2111119caryclarkTemplateParameterList * 1253ccec0f958ffc71a9986d236bc2eb335cb2111119caryclarkSema::MatchTemplateParametersToScopeSpecifier(SourceLocation DeclStartLoc, 1254ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark const CXXScopeSpec &SS, 1255ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark TemplateParameterList **ParamLists, 1256ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark unsigned NumParamLists, 1257ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark bool IsFriend, 1258ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark bool &IsExplicitSpecialization) { 1259ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark IsExplicitSpecialization = false; 1260ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark 1261866f4e34a943c115ac372c22123a1520aa5f9b06commit-bot@chromium.org // Find the template-ids that occur within the nested-name-specifier. These 1262866f4e34a943c115ac372c22123a1520aa5f9b06commit-bot@chromium.org // template-ids will match up with the template parameter lists. 1263ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark llvm::SmallVector<const TemplateSpecializationType *, 4> 1264a2bbc6e19d5332e81784e582c290cc060f40c4c7caryclark@google.com TemplateIdsInSpecifier; 1265a2bbc6e19d5332e81784e582c290cc060f40c4c7caryclark@google.com llvm::SmallVector<ClassTemplateSpecializationDecl *, 4> 1266a2bbc6e19d5332e81784e582c290cc060f40c4c7caryclark@google.com ExplicitSpecializationsInSpecifier; 1267ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark for (NestedNameSpecifier *NNS = (NestedNameSpecifier *)SS.getScopeRep(); 1268ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark NNS; NNS = NNS->getPrefix()) { 126907393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com const Type *T = NNS->getAsType(); 127007393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com if (!T) break; 1271ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark 1272ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark // C++0x [temp.expl.spec]p17: 1273ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark // A member or a member template may be nested within many 1274ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark // enclosing class templates. In an explicit specialization for 1275ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark // such a member, the member declaration shall be preceded by a 127607393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com // template<> for each enclosing class template that is 1277ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark // explicitly specialized. 127807393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com // 1279dac1d17027dcaa5596885a9f333979418b35001ccaryclark // Following the existing practice of GNU and EDG, we allow a typedef of a 1280dac1d17027dcaa5596885a9f333979418b35001ccaryclark // template specialization type. 128107393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com if (const TypedefType *TT = dyn_cast<TypedefType>(T)) 1282ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark T = TT->LookThroughTypedefs().getTypePtr(); 128307393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com 128407393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com if (const TemplateSpecializationType *SpecType 128507393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com = dyn_cast<TemplateSpecializationType>(T)) { 128607393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com TemplateDecl *Template = SpecType->getTemplateName().getAsTemplateDecl(); 1287ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark if (!Template) 1288ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark continue; // FIXME: should this be an error? probably... 1289ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark 1290ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark if (const RecordType *Record = SpecType->getAs<RecordType>()) { 1291ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark ClassTemplateSpecializationDecl *SpecDecl 1292ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark = cast<ClassTemplateSpecializationDecl>(Record->getDecl()); 12934431e7757cfcb8cfa99535eed0e9f156dabf95c2commit-bot@chromium.org // If the nested name specifier refers to an explicit specialization, 1294ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark // we don't need a template<> header. 1295ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark if (SpecDecl->getSpecializationKind() == TSK_ExplicitSpecialization) { 1296ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark ExplicitSpecializationsInSpecifier.push_back(SpecDecl); 1297dac1d17027dcaa5596885a9f333979418b35001ccaryclark continue; 1298dac1d17027dcaa5596885a9f333979418b35001ccaryclark } 12994431e7757cfcb8cfa99535eed0e9f156dabf95c2commit-bot@chromium.org } 1300ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark 13016f726addf3178b01949bb389ef83cf14a1d7b6b2caryclark TemplateIdsInSpecifier.push_back(SpecType); 130265f553182ab7069378ef863d30094d0327f178d0caryclark } 130365f553182ab7069378ef863d30094d0327f178d0caryclark } 130465f553182ab7069378ef863d30094d0327f178d0caryclark 130565f553182ab7069378ef863d30094d0327f178d0caryclark // Reverse the list of template-ids in the scope specifier, so that we can 13064431e7757cfcb8cfa99535eed0e9f156dabf95c2commit-bot@chromium.org // more easily match up the template-ids and the template parameter lists. 13074431e7757cfcb8cfa99535eed0e9f156dabf95c2commit-bot@chromium.org std::reverse(TemplateIdsInSpecifier.begin(), TemplateIdsInSpecifier.end()); 1308ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark 1309ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark SourceLocation FirstTemplateLoc = DeclStartLoc; 1310ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark if (NumParamLists) 1311ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark FirstTemplateLoc = ParamLists[0]->getTemplateLoc(); 1312ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark 1313ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark // Match the template-ids found in the specifier to the template parameter 131407393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com // lists. 1315ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark unsigned Idx = 0; 1316ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark for (unsigned NumTemplateIds = TemplateIdsInSpecifier.size(); 1317ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark Idx != NumTemplateIds; ++Idx) { 1318ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark QualType TemplateId = QualType(TemplateIdsInSpecifier[Idx], 0); 1319ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark bool DependentTemplateId = TemplateId->isDependentType(); 1320ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark if (Idx >= NumParamLists) { 1321ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark // We have a template-id without a corresponding template parameter 1322dac1d17027dcaa5596885a9f333979418b35001ccaryclark // list. 1323ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark 1324ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark // ...which is fine if this is a friend declaration. 1325ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark if (IsFriend) { 1326dac1d17027dcaa5596885a9f333979418b35001ccaryclark IsExplicitSpecialization = true; 1327dac1d17027dcaa5596885a9f333979418b35001ccaryclark break; 1328dac1d17027dcaa5596885a9f333979418b35001ccaryclark } 1329dac1d17027dcaa5596885a9f333979418b35001ccaryclark 1330ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark if (DependentTemplateId) { 1331ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark // FIXME: the location information here isn't great. 1332dac1d17027dcaa5596885a9f333979418b35001ccaryclark Diag(SS.getRange().getBegin(), 1333ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark diag::err_template_spec_needs_template_parameters) 133407393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com << TemplateId 133507393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com << SS.getRange(); 133665f553182ab7069378ef863d30094d0327f178d0caryclark } else { 133765f553182ab7069378ef863d30094d0327f178d0caryclark Diag(SS.getRange().getBegin(), diag::err_template_spec_needs_header) 133865f553182ab7069378ef863d30094d0327f178d0caryclark << SS.getRange() 133965f553182ab7069378ef863d30094d0327f178d0caryclark << FixItHint::CreateInsertion(FirstTemplateLoc, "template<> "); 134007393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com IsExplicitSpecialization = true; 134107393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com } 1342ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark return 0; 134307393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com } 134407393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com 134507393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com // Check the template parameter list against its corresponding template-id. 134607393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com if (DependentTemplateId) { 1347ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark TemplateParameterList *ExpectedTemplateParams = 0; 1348ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark 1349570863f2e22b8ea7d7c504bd15e4f766af097df2caryclark@google.com // Are there cases in (e.g.) friends where this won't match? 1350570863f2e22b8ea7d7c504bd15e4f766af097df2caryclark@google.com if (const InjectedClassNameType *Injected 1351ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark = TemplateId->getAs<InjectedClassNameType>()) { 1352ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark CXXRecordDecl *Record = Injected->getDecl(); 1353ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark if (ClassTemplatePartialSpecializationDecl *Partial = 1354ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark dyn_cast<ClassTemplatePartialSpecializationDecl>(Record)) 1355ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark ExpectedTemplateParams = Partial->getTemplateParameters(); 1356ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark else 1357ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark ExpectedTemplateParams = Record->getDescribedClassTemplate() 1358570863f2e22b8ea7d7c504bd15e4f766af097df2caryclark@google.com ->getTemplateParameters(); 1359570863f2e22b8ea7d7c504bd15e4f766af097df2caryclark@google.com } 136007393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com 136107393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com if (ExpectedTemplateParams) 136207393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com TemplateParameterListsAreEqual(ParamLists[Idx], 1363ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark ExpectedTemplateParams, 1364ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark true, TPL_TemplateMatch); 136507393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com 136607393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com CheckTemplateParameterList(ParamLists[Idx], 0, TPC_ClassTemplateMember); 136707393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com } else if (ParamLists[Idx]->size() > 0) 136807393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com Diag(ParamLists[Idx]->getTemplateLoc(), 136907393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com diag::err_template_param_list_matches_nontemplate) 137007393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com << TemplateId 137107393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com << ParamLists[Idx]->getSourceRange(); 1372ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark else 137365f553182ab7069378ef863d30094d0327f178d0caryclark IsExplicitSpecialization = true; 1374ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark } 1375570863f2e22b8ea7d7c504bd15e4f766af097df2caryclark@google.com 1376570863f2e22b8ea7d7c504bd15e4f766af097df2caryclark@google.com // If there were at least as many template-ids as there were template 1377ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark // parameter lists, then there are no template parameter lists remaining for 1378ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark // the declaration itself. 1379ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark if (Idx >= NumParamLists) 1380ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark return 0; 1381ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark 1382ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark // If there were too many template parameter lists, complain about that now. 1383ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark if (Idx != NumParamLists - 1) { 1384570863f2e22b8ea7d7c504bd15e4f766af097df2caryclark@google.com while (Idx < NumParamLists - 1) { 1385570863f2e22b8ea7d7c504bd15e4f766af097df2caryclark@google.com bool isExplicitSpecHeader = ParamLists[Idx]->size() == 0; 138607393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com Diag(ParamLists[Idx]->getTemplateLoc(), 138707393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com isExplicitSpecHeader? diag::warn_template_spec_extra_headers 138807393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com : diag::err_template_spec_extra_headers) 1389ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark << SourceRange(ParamLists[Idx]->getTemplateLoc(), 1390ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark ParamLists[Idx]->getRAngleLoc()); 1391ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark 139207393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com if (isExplicitSpecHeader && !ExplicitSpecializationsInSpecifier.empty()) { 139307393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com Diag(ExplicitSpecializationsInSpecifier.back()->getLocation(), 1394ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark diag::note_explicit_template_spec_does_not_need_header) 1395ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark << ExplicitSpecializationsInSpecifier.back(); 1396ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark ExplicitSpecializationsInSpecifier.pop_back(); 1397ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark } 1398ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark 1399ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark ++Idx; 140007393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com } 140107393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com } 1402ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark 1403ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark // Return the last template parameter list, which corresponds to the 1404ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark // entity being declared. 1405ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark return ParamLists[NumParamLists - 1]; 140665f553182ab7069378ef863d30094d0327f178d0caryclark} 140707393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com 140807393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.comQualType Sema::CheckTemplateIdType(TemplateName Name, 1409ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark SourceLocation TemplateLoc, 14108cb1daaa1e4343eb60a7c4f21c12e33de30dad64commit-bot@chromium.org const TemplateArgumentListInfo &TemplateArgs) { 1411ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark TemplateDecl *Template = Name.getAsTemplateDecl(); 1412ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark if (!Template) { 141365f553182ab7069378ef863d30094d0327f178d0caryclark // The template name does not resolve to a template, so we just 141407393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com // build a dependent template-id type. 141507393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com return Context.getTemplateSpecializationType(Name, TemplateArgs); 1416ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark } 1417ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark 1418ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark // Check that the template argument list is well-formed for this 1419ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark // template. 142065f553182ab7069378ef863d30094d0327f178d0caryclark TemplateArgumentListBuilder Converted(Template->getTemplateParameters(), 142107393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com TemplateArgs.size()); 142207393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com if (CheckTemplateArgumentList(Template, TemplateLoc, TemplateArgs, 1423ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark false, Converted)) 14248cb1daaa1e4343eb60a7c4f21c12e33de30dad64commit-bot@chromium.org return QualType(); 1425ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark 1426ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark assert((Converted.structuredSize() == 14274431e7757cfcb8cfa99535eed0e9f156dabf95c2commit-bot@chromium.org Template->getTemplateParameters()->size()) && 142865f553182ab7069378ef863d30094d0327f178d0caryclark "Converted template argument list is too short!"); 142907393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com 143007393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com QualType CanonType; 1431ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark bool IsCurrentInstantiation = false; 1432ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark 1433ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark if (Name.isDependent() || 1434ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark TemplateSpecializationType::anyDependentTemplateArguments( 1435ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark TemplateArgs)) { 143607393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com // This class template specialization is a dependent 1437ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark // type. Therefore, its canonical type is another class template 1438ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark // specialization type that contains all of the converted 1439e4097e3a0b10bb0047a45b6949ca01826f0807a7caryclark // arguments in canonical form. This ensures that, e.g., A<T> and 1440ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark // A<T, T> have identical types when A is declared as: 1441ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark // 1442ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark // template<typename T, typename U = T> struct A; 1443ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark TemplateName CanonName = Context.getCanonicalTemplateName(Name); 1444ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark CanonType = Context.getTemplateSpecializationType(CanonName, 1445ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark Converted.getFlatArguments(), 144607393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com Converted.flatSize()); 1447ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark 144807393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com // FIXME: CanonType is not actually the canonical type, and unfortunately 144907393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com // it is a TemplateSpecializationType that we will never use again. 1450ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark // In the future, we need to teach getTemplateSpecializationType to only 14518cb1daaa1e4343eb60a7c4f21c12e33de30dad64commit-bot@chromium.org // build the canonical type and return that to us. 145207393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com CanonType = Context.getCanonicalType(CanonType); 1453ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark 145407393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com // This might work out to be a current instantiation, in which 145507393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com // case the canonical type needs to be the InjectedClassNameType. 145607393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com // 1457ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark // TODO: in theory this could be a simple hashtable lookup; most 145807393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com // changes to CurContext don't change the set of current 145907393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com // instantiations. 146007393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com if (isa<ClassTemplateDecl>(Template)) { 1461ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark for (DeclContext *Ctx = CurContext; Ctx; Ctx = Ctx->getLookupParent()) { 1462ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark // If we get out to a namespace, we're done. 1463ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark if (Ctx->isFileContext()) break; 1464ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark 146507393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com // If this isn't a record, keep looking. 146607393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Ctx); 1467ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark if (!Record) continue; 146807393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com 1469ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark // Look for one of the two cases with InjectedClassNameTypes 1470ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark // and check whether it's the same template. 147107393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com if (!isa<ClassTemplatePartialSpecializationDecl>(Record) && 147207393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com !Record->getDescribedClassTemplate()) 147307393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com continue; 1474ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark 1475ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark // Fetch the injected class name type and check whether its 1476ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark // injected type is equal to the type we just built. 1477dac1d17027dcaa5596885a9f333979418b35001ccaryclark QualType ICNT = Context.getTypeDeclType(Record); 1478ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark QualType Injected = cast<InjectedClassNameType>(ICNT) 1479ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark ->getInjectedSpecializationType(); 1480ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark 1481ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark if (CanonType != Injected->getCanonicalTypeInternal()) 1482ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark continue; 1483dac1d17027dcaa5596885a9f333979418b35001ccaryclark 1484dac1d17027dcaa5596885a9f333979418b35001ccaryclark // If so, the canonical type of this TST is the injected 1485ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark // class name type of the record we just found. 1486dac1d17027dcaa5596885a9f333979418b35001ccaryclark assert(ICNT.isCanonical()); 1487dac1d17027dcaa5596885a9f333979418b35001ccaryclark CanonType = ICNT; 1488ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark IsCurrentInstantiation = true; 1489ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark break; 1490ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark } 1491ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark } 1492dac1d17027dcaa5596885a9f333979418b35001ccaryclark } else if (ClassTemplateDecl *ClassTemplate 1493dac1d17027dcaa5596885a9f333979418b35001ccaryclark = dyn_cast<ClassTemplateDecl>(Template)) { 1494dac1d17027dcaa5596885a9f333979418b35001ccaryclark // Find the class template specialization declaration that 1495ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark // corresponds to these arguments. 1496dac1d17027dcaa5596885a9f333979418b35001ccaryclark llvm::FoldingSetNodeID ID; 1497dac1d17027dcaa5596885a9f333979418b35001ccaryclark ClassTemplateSpecializationDecl::Profile(ID, 149865b427cff9cd34a06ff060d65d00cc3615d8fd94caryclark Converted.getFlatArguments(), 149965b427cff9cd34a06ff060d65d00cc3615d8fd94caryclark Converted.flatSize(), 150065b427cff9cd34a06ff060d65d00cc3615d8fd94caryclark Context); 1501dac1d17027dcaa5596885a9f333979418b35001ccaryclark void *InsertPos = 0; 1502ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark ClassTemplateSpecializationDecl *Decl 1503ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark = ClassTemplate->getSpecializations().FindNodeOrInsertPos(ID, InsertPos); 1504dac1d17027dcaa5596885a9f333979418b35001ccaryclark if (!Decl) { 1505dac1d17027dcaa5596885a9f333979418b35001ccaryclark // This is the first time we have referenced this class template 1506ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark // specialization. Create the canonical declaration and add it to 1507dac1d17027dcaa5596885a9f333979418b35001ccaryclark // the set of specializations. 1508ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark Decl = ClassTemplateSpecializationDecl::Create(Context, 1509dac1d17027dcaa5596885a9f333979418b35001ccaryclark ClassTemplate->getTemplatedDecl()->getTagKind(), 1510570863f2e22b8ea7d7c504bd15e4f766af097df2caryclark@google.com ClassTemplate->getDeclContext(), 1511ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark ClassTemplate->getLocation(), 1512dac1d17027dcaa5596885a9f333979418b35001ccaryclark ClassTemplate, 1513ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark Converted, 0); 151407393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com ClassTemplate->getSpecializations().InsertNode(Decl, InsertPos); 1515ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark Decl->setLexicalDeclContext(CurContext); 1516ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark } 151765b427cff9cd34a06ff060d65d00cc3615d8fd94caryclark 151865b427cff9cd34a06ff060d65d00cc3615d8fd94caryclark CanonType = Context.getTypeDeclType(Decl); 151965b427cff9cd34a06ff060d65d00cc3615d8fd94caryclark assert(isa<RecordType>(CanonType) && 1520ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark "type of non-dependent specialization is not a RecordType"); 1521ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark } 1522ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark 1523ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark // Build the fully-sugared type for this class template 1524ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark // specialization, which refers back to the class template 1525dac1d17027dcaa5596885a9f333979418b35001ccaryclark // specialization we created or found. 1526ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark return Context.getTemplateSpecializationType(Name, TemplateArgs, CanonType, 1527dac1d17027dcaa5596885a9f333979418b35001ccaryclark IsCurrentInstantiation); 1528dac1d17027dcaa5596885a9f333979418b35001ccaryclark} 1529dac1d17027dcaa5596885a9f333979418b35001ccaryclark 1530a5e55925ea03e76885804bda77408a1d6f04c335caryclark@google.comAction::TypeResult 153107393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.comSema::ActOnTemplateIdType(TemplateTy TemplateD, SourceLocation TemplateLoc, 153207393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com SourceLocation LAngleLoc, 153307393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com ASTTemplateArgsPtr TemplateArgsIn, 1534ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark SourceLocation RAngleLoc) { 1535ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark TemplateName Template = TemplateD.getAsVal<TemplateName>(); 1536ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark 1537ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark // Translate the parser's template argument list in our AST format. 1538ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); 1539ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark translateTemplateArguments(TemplateArgsIn, TemplateArgs); 1540ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark 154107393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com QualType Result = CheckTemplateIdType(Template, TemplateLoc, TemplateArgs); 1542ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark TemplateArgsIn.release(); 154307393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com 154407393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com if (Result.isNull()) 1545ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark return true; 1546ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark 1547ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark TypeSourceInfo *DI = Context.CreateTypeSourceInfo(Result); 1548ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark TemplateSpecializationTypeLoc TL 1549ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark = cast<TemplateSpecializationTypeLoc>(DI->getTypeLoc()); 1550ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark TL.setTemplateNameLoc(TemplateLoc); 1551ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark TL.setLAngleLoc(LAngleLoc); 1552ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark TL.setRAngleLoc(RAngleLoc); 1553ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) 1554ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark TL.setArgLocInfo(i, TemplateArgs[i].getLocInfo()); 1555ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark 1556ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark return CreateLocInfoType(Result, DI).getAsOpaquePtr(); 1557ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark} 1558ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark 1559ccec0f958ffc71a9986d236bc2eb335cb2111119caryclarkSema::TypeResult Sema::ActOnTagTemplateIdType(TypeResult TypeResult, 1560ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark TagUseKind TUK, 1561ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark DeclSpec::TST TagSpec, 1562570863f2e22b8ea7d7c504bd15e4f766af097df2caryclark@google.com SourceLocation TagLoc) { 1563570863f2e22b8ea7d7c504bd15e4f766af097df2caryclark@google.com if (TypeResult.isInvalid()) 1564ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark return Sema::TypeResult(); 1565ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark 1566ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark // FIXME: preserve source info, ideally without copying the DI. 1567ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark TypeSourceInfo *DI; 1568ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark QualType Type = GetTypeFromParser(TypeResult.get(), &DI); 1569ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark 1570ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark // Verify the tag specifier. 1571ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark TagTypeKind TagKind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); 1572ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark 1573ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark if (const RecordType *RT = Type->getAs<RecordType>()) { 1574ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark RecordDecl *D = RT->getDecl(); 1575ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark 1576ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark IdentifierInfo *Id = D->getIdentifier(); 1577ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark assert(Id && "templated class must have an identifier"); 1578ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark 1579ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark if (!isAcceptableTagRedeclaration(D, TagKind, TagLoc, *Id)) { 1580ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark Diag(TagLoc, diag::err_use_with_wrong_tag) 1581ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark << Type 1582ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark << FixItHint::CreateReplacement(SourceRange(TagLoc), D->getKindName()); 1583ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark Diag(D->getLocation(), diag::note_previous_use); 1584ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark } 1585ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark } 1586ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark 1587ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark ElaboratedTypeKeyword Keyword 1588ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark = TypeWithKeyword::getKeywordForTagTypeKind(TagKind); 1589ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark QualType ElabType = Context.getElaboratedType(Keyword, /*NNS=*/0, Type); 1590ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark 1591ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark return ElabType.getAsOpaquePtr(); 1592ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark} 1593ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark 1594ccec0f958ffc71a9986d236bc2eb335cb2111119caryclarkSema::OwningExprResult Sema::BuildTemplateIdExpr(const CXXScopeSpec &SS, 1595ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark LookupResult &R, 1596ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark bool RequiresADL, 1597ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark const TemplateArgumentListInfo &TemplateArgs) { 1598ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark // FIXME: Can we do any checking at this point? I guess we could check the 1599ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark // template arguments that we have against the template name, if the template 1600ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark // name refers to a single template. That's not a terribly common case, 1601ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark // though. 1602ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark 1603ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark // These should be filtered out by our callers. 1604ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark assert(!R.empty() && "empty lookup results when building templateid"); 1605ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark assert(!R.isAmbiguous() && "ambiguous lookup when building templateid"); 1606ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark 1607ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark NestedNameSpecifier *Qualifier = 0; 1608ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark SourceRange QualifierRange; 1609ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark if (SS.isSet()) { 1610ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark Qualifier = static_cast<NestedNameSpecifier*>(SS.getScopeRep()); 1611ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark QualifierRange = SS.getRange(); 1612ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark } 1613ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark 1614ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark // We don't want lookup warnings at this point. 1615ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark R.suppressDiagnostics(); 1616ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark 1617ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark bool Dependent 1618ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark = UnresolvedLookupExpr::ComputeDependence(R.begin(), R.end(), 1619ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark &TemplateArgs); 1620ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark UnresolvedLookupExpr *ULE 1621ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark = UnresolvedLookupExpr::Create(Context, Dependent, R.getNamingClass(), 1622ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark Qualifier, QualifierRange, 1623ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark R.getLookupName(), R.getNameLoc(), 1624ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark RequiresADL, TemplateArgs); 1625ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark ULE->addDecls(R.begin(), R.end()); 1626ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark 1627ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark return Owned(ULE); 1628ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark} 1629ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark 1630ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark// We actually only call this from template instantiation. 1631ccec0f958ffc71a9986d236bc2eb335cb2111119caryclarkSema::OwningExprResult 1632ccec0f958ffc71a9986d236bc2eb335cb2111119caryclarkSema::BuildQualifiedTemplateIdExpr(CXXScopeSpec &SS, 1633ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark DeclarationName Name, 1634ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark SourceLocation NameLoc, 1635ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark const TemplateArgumentListInfo &TemplateArgs) { 1636ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark DeclContext *DC; 1637ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark if (!(DC = computeDeclContext(SS, false)) || 1638ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark DC->isDependentContext() || 1639ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark RequireCompleteDeclContext(SS, DC)) 1640ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark return BuildDependentDeclRefExpr(SS, Name, NameLoc, &TemplateArgs); 1641ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark 1642ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark LookupResult R(*this, Name, NameLoc, LookupOrdinaryName); 1643ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark LookupTemplateName(R, (Scope*) 0, SS, QualType(), /*Entering*/ false); 1644ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark 1645ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark if (R.isAmbiguous()) 1646ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark return ExprError(); 1647ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark 1648ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark if (R.empty()) { 1649570863f2e22b8ea7d7c504bd15e4f766af097df2caryclark@google.com Diag(NameLoc, diag::err_template_kw_refers_to_non_template) 1650ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark << Name << SS.getRange(); 1651ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark return ExprError(); 1652ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark } 1653ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark 1654ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark if (ClassTemplateDecl *Temp = R.getAsSingle<ClassTemplateDecl>()) { 1655ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark Diag(NameLoc, diag::err_template_kw_refers_to_class_template) 1656ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark << (NestedNameSpecifier*) SS.getScopeRep() << Name << SS.getRange(); 1657ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark Diag(Temp->getLocation(), diag::note_referenced_class_template); 1658ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark return ExprError(); 1659ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark } 1660ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark 1661ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark return BuildTemplateIdExpr(SS, R, /* ADL */ false, TemplateArgs); 1662ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark} 1663ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark 1664ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark/// \brief Form a dependent template name. 1665ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark/// 1666ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark/// This action forms a dependent template name given the template 1667ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark/// name and its (presumably dependent) scope specifier. For 1668570863f2e22b8ea7d7c504bd15e4f766af097df2caryclark@google.com/// example, given "MetaFun::template apply", the scope specifier \p 1669570863f2e22b8ea7d7c504bd15e4f766af097df2caryclark@google.com/// SS will be "MetaFun::", \p TemplateKWLoc contains the location 167007393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com/// of the "template" keyword, and "apply" is the \p Name. 1671ccec0f958ffc71a9986d236bc2eb335cb2111119caryclarkSema::TemplateTy 1672ccec0f958ffc71a9986d236bc2eb335cb2111119caryclarkSema::ActOnDependentTemplateName(SourceLocation TemplateKWLoc, 1673ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark CXXScopeSpec &SS, 1674ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark UnqualifiedId &Name, 1675ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark TypeTy *ObjectType, 1676ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark bool EnteringContext) { 1677ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark DeclContext *LookupCtx = 0; 1678ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark if (SS.isSet()) 1679ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark LookupCtx = computeDeclContext(SS, EnteringContext); 1680ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark if (!LookupCtx && ObjectType) 1681ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark LookupCtx = computeDeclContext(QualType::getFromOpaquePtr(ObjectType)); 1682ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark if (LookupCtx) { 1683ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark // C++0x [temp.names]p5: 1684ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark // If a name prefixed by the keyword template is not the name of 1685ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark // a template, the program is ill-formed. [Note: the keyword 1686ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark // template may not be applied to non-template members of class 1687ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark // templates. -end note ] [ Note: as is the case with the 1688ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark // typename prefix, the template prefix is allowed in cases 1689ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark // where it is not strictly necessary; i.e., when the 1690ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark // nested-name-specifier or the expression on the left of the -> 1691ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark // or . is not dependent on a template-parameter, or the use 1692ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark // does not appear in the scope of a template. -end note] 1693ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark // 1694ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark // Note: C++03 was more strict here, because it banned the use of 1695ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark // the "template" keyword prior to a template-name that was not a 1696ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark // dependent name. C++ DR468 relaxed this requirement (the 1697ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark // "template" keyword is now permitted). We follow the C++0x 1698ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark // rules, even in C++03 mode, retroactively applying the DR. 1699ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark TemplateTy Template; 1700ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark TemplateNameKind TNK = isTemplateName(0, SS, Name, ObjectType, 1701ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark EnteringContext, Template); 1702ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark if (TNK == TNK_Non_template && LookupCtx->isDependentContext() && 1703ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark isa<CXXRecordDecl>(LookupCtx) && 1704ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark cast<CXXRecordDecl>(LookupCtx)->hasAnyDependentBases()) { 1705ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark // This is a dependent template. 170607393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com } else if (TNK == TNK_Non_template) { 170707393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com Diag(Name.getSourceRange().getBegin(), 1708ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark diag::err_template_kw_refers_to_non_template) 1709ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark << GetNameFromUnqualifiedId(Name) 1710dac1d17027dcaa5596885a9f333979418b35001ccaryclark << Name.getSourceRange() 1711dac1d17027dcaa5596885a9f333979418b35001ccaryclark << TemplateKWLoc; 1712ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark return TemplateTy(); 1713ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark } else { 17145e27e0eb1d1d4c7674e221d3ba3314500ea0b97acaryclark // We found something; return it. 17155e27e0eb1d1d4c7674e221d3ba3314500ea0b97acaryclark return Template; 1716ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark } 1717ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark } 1718ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark 1719dac1d17027dcaa5596885a9f333979418b35001ccaryclark NestedNameSpecifier *Qualifier 1720ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); 1721ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark 1722ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark switch (Name.getKind()) { 1723ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark case UnqualifiedId::IK_Identifier: 1724ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark return TemplateTy::make(Context.getDependentTemplateName(Qualifier, 1725ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark Name.Identifier)); 1726ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark 1727ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark case UnqualifiedId::IK_OperatorFunctionId: 1728ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark return TemplateTy::make(Context.getDependentTemplateName(Qualifier, 1729ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark Name.OperatorFunctionId.Operator)); 1730ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark 1731ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark case UnqualifiedId::IK_LiteralOperatorId: 1732ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark assert(false && "We don't support these; Parse shouldn't have allowed propagation"); 1733ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark 1734ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark default: 1735ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark break; 1736ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark } 1737ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark 1738dac1d17027dcaa5596885a9f333979418b35001ccaryclark Diag(Name.getSourceRange().getBegin(), 1739dac1d17027dcaa5596885a9f333979418b35001ccaryclark diag::err_template_kw_refers_to_non_template) 1740ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark << GetNameFromUnqualifiedId(Name) 1741ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark << Name.getSourceRange() 1742ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark << TemplateKWLoc; 1743ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark return TemplateTy(); 1744ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark} 174507393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com 174607393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.combool Sema::CheckTemplateTypeArgument(TemplateTypeParmDecl *Param, 174707393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com const TemplateArgumentLoc &AL, 174807393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com TemplateArgumentListBuilder &Converted) { 174907393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com const TemplateArgument &Arg = AL.getArgument(); 175007393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com 175107393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com // Check template type parameter. 175207393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com switch(Arg.getKind()) { 175307393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com case TemplateArgument::Type: 175407393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com // C++ [temp.arg.type]p1: 175507393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com // A template-argument for a template-parameter which is a 1756ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark // type shall be a type-id. 1757ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark break; 175807393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com case TemplateArgument::Template: { 175907393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com // We have a template type parameter but the template argument 17604431e7757cfcb8cfa99535eed0e9f156dabf95c2commit-bot@chromium.org // is a template without any arguments. 1761ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark SourceRange SR = AL.getSourceRange(); 17624431e7757cfcb8cfa99535eed0e9f156dabf95c2commit-bot@chromium.org TemplateName Name = Arg.getAsTemplate(); 1763ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark Diag(SR.getBegin(), diag::err_template_missing_args) 1764ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark << Name << SR; 1765dac1d17027dcaa5596885a9f333979418b35001ccaryclark if (TemplateDecl *Decl = Name.getAsTemplateDecl()) 17664431e7757cfcb8cfa99535eed0e9f156dabf95c2commit-bot@chromium.org Diag(Decl->getLocation(), diag::note_template_decl_here); 17674431e7757cfcb8cfa99535eed0e9f156dabf95c2commit-bot@chromium.org 1768cffbcc3b9665f2c928544b6fc6b8a0e22a4210fbcaryclark@google.com return true; 17694431e7757cfcb8cfa99535eed0e9f156dabf95c2commit-bot@chromium.org } 1770cffbcc3b9665f2c928544b6fc6b8a0e22a4210fbcaryclark@google.com default: { 1771ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark // We have a template type parameter but the template argument 1772ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark // is not a type. 17734431e7757cfcb8cfa99535eed0e9f156dabf95c2commit-bot@chromium.org SourceRange SR = AL.getSourceRange(); 1774ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark Diag(SR.getBegin(), diag::err_template_arg_must_be_type) << SR; 1775ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark Diag(Param->getLocation(), diag::note_template_param_here); 1776ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark 17774431e7757cfcb8cfa99535eed0e9f156dabf95c2commit-bot@chromium.org return true; 1778ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark } 1779ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark } 1780ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark 178107393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com if (CheckTemplateArgument(Param, AL.getTypeSourceInfo())) 1782ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark return true; 17834431e7757cfcb8cfa99535eed0e9f156dabf95c2commit-bot@chromium.org 1784ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark // Add the converted template type argument. 1785ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark Converted.Append( 1786ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark TemplateArgument(Context.getCanonicalType(Arg.getAsType()))); 1787ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark return false; 1788ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark} 1789dac1d17027dcaa5596885a9f333979418b35001ccaryclark 1790570863f2e22b8ea7d7c504bd15e4f766af097df2caryclark@google.com/// \brief Substitute template arguments into the default template argument for 17914431e7757cfcb8cfa99535eed0e9f156dabf95c2commit-bot@chromium.org/// the given template type parameter. 1792ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark/// 1793ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark/// \param SemaRef the semantic analysis object for which we are performing 17944431e7757cfcb8cfa99535eed0e9f156dabf95c2commit-bot@chromium.org/// the substitution. 17954431e7757cfcb8cfa99535eed0e9f156dabf95c2commit-bot@chromium.org/// 1796570863f2e22b8ea7d7c504bd15e4f766af097df2caryclark@google.com/// \param Template the template that we are synthesizing template arguments 1797ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark/// for. 17988cb1daaa1e4343eb60a7c4f21c12e33de30dad64commit-bot@chromium.org/// 17998cb1daaa1e4343eb60a7c4f21c12e33de30dad64commit-bot@chromium.org/// \param TemplateLoc the location of the template name that started the 18004431e7757cfcb8cfa99535eed0e9f156dabf95c2commit-bot@chromium.org/// template-id we are checking. 1801ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark/// 1802ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark/// \param RAngleLoc the location of the right angle bracket ('>') that 1803ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark/// terminates the template-id. 18044431e7757cfcb8cfa99535eed0e9f156dabf95c2commit-bot@chromium.org/// 1805ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark/// \param Param the template template parameter whose default we are 1806ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark/// substituting into. 1807ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark/// 1808ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark/// \param Converted the list of template arguments provided for template 1809ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark/// parameters that precede \p Param in the template parameter list. 18104431e7757cfcb8cfa99535eed0e9f156dabf95c2commit-bot@chromium.org/// 1811ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark/// \returns the substituted template argument, or NULL if an error occurred. 1812ccec0f958ffc71a9986d236bc2eb335cb2111119caryclarkstatic TypeSourceInfo * 1813ccec0f958ffc71a9986d236bc2eb335cb2111119caryclarkSubstDefaultTemplateArgument(Sema &SemaRef, 18148cb1daaa1e4343eb60a7c4f21c12e33de30dad64commit-bot@chromium.org TemplateDecl *Template, 18154431e7757cfcb8cfa99535eed0e9f156dabf95c2commit-bot@chromium.org SourceLocation TemplateLoc, 1816ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark SourceLocation RAngleLoc, 1817ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark TemplateTypeParmDecl *Param, 1818ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark TemplateArgumentListBuilder &Converted) { 1819ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark TypeSourceInfo *ArgType = Param->getDefaultArgumentInfo(); 1820ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark 18214431e7757cfcb8cfa99535eed0e9f156dabf95c2commit-bot@chromium.org // If the argument type is dependent, instantiate it now based 18224431e7757cfcb8cfa99535eed0e9f156dabf95c2commit-bot@chromium.org // on the previously-computed template arguments. 18234431e7757cfcb8cfa99535eed0e9f156dabf95c2commit-bot@chromium.org if (ArgType->getType()->isDependentType()) { 18244431e7757cfcb8cfa99535eed0e9f156dabf95c2commit-bot@chromium.org TemplateArgumentList TemplateArgs(SemaRef.Context, Converted, 18254431e7757cfcb8cfa99535eed0e9f156dabf95c2commit-bot@chromium.org /*TakeArgs=*/false); 18264431e7757cfcb8cfa99535eed0e9f156dabf95c2commit-bot@chromium.org 1827ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark MultiLevelTemplateArgumentList AllTemplateArgs 1828570863f2e22b8ea7d7c504bd15e4f766af097df2caryclark@google.com = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs); 1829570863f2e22b8ea7d7c504bd15e4f766af097df2caryclark@google.com 1830cffbcc3b9665f2c928544b6fc6b8a0e22a4210fbcaryclark@google.com Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc, 1831ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark Template, Converted.getFlatArguments(), 1832ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark Converted.flatSize(), 1833cffbcc3b9665f2c928544b6fc6b8a0e22a4210fbcaryclark@google.com SourceRange(TemplateLoc, RAngleLoc)); 1834ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark 1835ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark ArgType = SemaRef.SubstType(ArgType, AllTemplateArgs, 1836ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark Param->getDefaultArgumentLoc(), 1837cffbcc3b9665f2c928544b6fc6b8a0e22a4210fbcaryclark@google.com Param->getDeclName()); 1838ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark } 1839cffbcc3b9665f2c928544b6fc6b8a0e22a4210fbcaryclark@google.com 1840cffbcc3b9665f2c928544b6fc6b8a0e22a4210fbcaryclark@google.com return ArgType; 1841cffbcc3b9665f2c928544b6fc6b8a0e22a4210fbcaryclark@google.com} 1842ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark 1843ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark/// \brief Substitute template arguments into the default template argument for 1844cffbcc3b9665f2c928544b6fc6b8a0e22a4210fbcaryclark@google.com/// the given non-type template parameter. 1845cffbcc3b9665f2c928544b6fc6b8a0e22a4210fbcaryclark@google.com/// 184607393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com/// \param SemaRef the semantic analysis object for which we are performing 1847cffbcc3b9665f2c928544b6fc6b8a0e22a4210fbcaryclark@google.com/// the substitution. 1848cffbcc3b9665f2c928544b6fc6b8a0e22a4210fbcaryclark@google.com/// 184907393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com/// \param Template the template that we are synthesizing template arguments 1850cffbcc3b9665f2c928544b6fc6b8a0e22a4210fbcaryclark@google.com/// for. 1851cffbcc3b9665f2c928544b6fc6b8a0e22a4210fbcaryclark@google.com/// 1852cffbcc3b9665f2c928544b6fc6b8a0e22a4210fbcaryclark@google.com/// \param TemplateLoc the location of the template name that started the 1853cffbcc3b9665f2c928544b6fc6b8a0e22a4210fbcaryclark@google.com/// template-id we are checking. 1854cffbcc3b9665f2c928544b6fc6b8a0e22a4210fbcaryclark@google.com/// 1855cffbcc3b9665f2c928544b6fc6b8a0e22a4210fbcaryclark@google.com/// \param RAngleLoc the location of the right angle bracket ('>') that 1856cffbcc3b9665f2c928544b6fc6b8a0e22a4210fbcaryclark@google.com/// terminates the template-id. 1857cffbcc3b9665f2c928544b6fc6b8a0e22a4210fbcaryclark@google.com/// 1858cffbcc3b9665f2c928544b6fc6b8a0e22a4210fbcaryclark@google.com/// \param Param the non-type template parameter whose default we are 1859cffbcc3b9665f2c928544b6fc6b8a0e22a4210fbcaryclark@google.com/// substituting into. 1860cffbcc3b9665f2c928544b6fc6b8a0e22a4210fbcaryclark@google.com/// 1861cffbcc3b9665f2c928544b6fc6b8a0e22a4210fbcaryclark@google.com/// \param Converted the list of template arguments provided for template 1862cffbcc3b9665f2c928544b6fc6b8a0e22a4210fbcaryclark@google.com/// parameters that precede \p Param in the template parameter list. 1863cffbcc3b9665f2c928544b6fc6b8a0e22a4210fbcaryclark@google.com/// 1864cffbcc3b9665f2c928544b6fc6b8a0e22a4210fbcaryclark@google.com/// \returns the substituted template argument, or NULL if an error occurred. 1865cffbcc3b9665f2c928544b6fc6b8a0e22a4210fbcaryclark@google.comstatic Sema::OwningExprResult 1866cffbcc3b9665f2c928544b6fc6b8a0e22a4210fbcaryclark@google.comSubstDefaultTemplateArgument(Sema &SemaRef, 1867cffbcc3b9665f2c928544b6fc6b8a0e22a4210fbcaryclark@google.com TemplateDecl *Template, 1868cffbcc3b9665f2c928544b6fc6b8a0e22a4210fbcaryclark@google.com SourceLocation TemplateLoc, 186907393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com SourceLocation RAngleLoc, 1870cffbcc3b9665f2c928544b6fc6b8a0e22a4210fbcaryclark@google.com NonTypeTemplateParmDecl *Param, 1871cffbcc3b9665f2c928544b6fc6b8a0e22a4210fbcaryclark@google.com TemplateArgumentListBuilder &Converted) { 1872cffbcc3b9665f2c928544b6fc6b8a0e22a4210fbcaryclark@google.com TemplateArgumentList TemplateArgs(SemaRef.Context, Converted, 1873ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark /*TakeArgs=*/false); 1874ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark 1875cffbcc3b9665f2c928544b6fc6b8a0e22a4210fbcaryclark@google.com MultiLevelTemplateArgumentList AllTemplateArgs 1876ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs); 1877ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark 1878ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc, 1879cffbcc3b9665f2c928544b6fc6b8a0e22a4210fbcaryclark@google.com Template, Converted.getFlatArguments(), 1880ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark Converted.flatSize(), 1881cffbcc3b9665f2c928544b6fc6b8a0e22a4210fbcaryclark@google.com SourceRange(TemplateLoc, RAngleLoc)); 1882cffbcc3b9665f2c928544b6fc6b8a0e22a4210fbcaryclark@google.com 1883cffbcc3b9665f2c928544b6fc6b8a0e22a4210fbcaryclark@google.com return SemaRef.SubstExpr(Param->getDefaultArgument(), AllTemplateArgs); 1884ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark} 1885ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark 1886cffbcc3b9665f2c928544b6fc6b8a0e22a4210fbcaryclark@google.com/// \brief Substitute template arguments into the default template argument for 1887cffbcc3b9665f2c928544b6fc6b8a0e22a4210fbcaryclark@google.com/// the given template template parameter. 1888cffbcc3b9665f2c928544b6fc6b8a0e22a4210fbcaryclark@google.com/// 1889cffbcc3b9665f2c928544b6fc6b8a0e22a4210fbcaryclark@google.com/// \param SemaRef the semantic analysis object for which we are performing 1890cffbcc3b9665f2c928544b6fc6b8a0e22a4210fbcaryclark@google.com/// the substitution. 1891cffbcc3b9665f2c928544b6fc6b8a0e22a4210fbcaryclark@google.com/// 1892cffbcc3b9665f2c928544b6fc6b8a0e22a4210fbcaryclark@google.com/// \param Template the template that we are synthesizing template arguments 1893ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark/// for. 1894cffbcc3b9665f2c928544b6fc6b8a0e22a4210fbcaryclark@google.com/// 1895cffbcc3b9665f2c928544b6fc6b8a0e22a4210fbcaryclark@google.com/// \param TemplateLoc the location of the template name that started the 1896cffbcc3b9665f2c928544b6fc6b8a0e22a4210fbcaryclark@google.com/// template-id we are checking. 1897cffbcc3b9665f2c928544b6fc6b8a0e22a4210fbcaryclark@google.com/// 1898cffbcc3b9665f2c928544b6fc6b8a0e22a4210fbcaryclark@google.com/// \param RAngleLoc the location of the right angle bracket ('>') that 1899cffbcc3b9665f2c928544b6fc6b8a0e22a4210fbcaryclark@google.com/// terminates the template-id. 1900cffbcc3b9665f2c928544b6fc6b8a0e22a4210fbcaryclark@google.com/// 1901cffbcc3b9665f2c928544b6fc6b8a0e22a4210fbcaryclark@google.com/// \param Param the template template parameter whose default we are 1902cffbcc3b9665f2c928544b6fc6b8a0e22a4210fbcaryclark@google.com/// substituting into. 1903cffbcc3b9665f2c928544b6fc6b8a0e22a4210fbcaryclark@google.com/// 1904cffbcc3b9665f2c928544b6fc6b8a0e22a4210fbcaryclark@google.com/// \param Converted the list of template arguments provided for template 1905cffbcc3b9665f2c928544b6fc6b8a0e22a4210fbcaryclark@google.com/// parameters that precede \p Param in the template parameter list. 1906cffbcc3b9665f2c928544b6fc6b8a0e22a4210fbcaryclark@google.com/// 1907cffbcc3b9665f2c928544b6fc6b8a0e22a4210fbcaryclark@google.com/// \returns the substituted template argument, or NULL if an error occurred. 1908cffbcc3b9665f2c928544b6fc6b8a0e22a4210fbcaryclark@google.comstatic TemplateName 190907393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.comSubstDefaultTemplateArgument(Sema &SemaRef, 191007393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com TemplateDecl *Template, 1911ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark SourceLocation TemplateLoc, 1912ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark SourceLocation RAngleLoc, 191307393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com TemplateTemplateParmDecl *Param, 191407393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com TemplateArgumentListBuilder &Converted) { 1915277c3f87656c44e0a651ed0dd56efa16c0ab07b4reed@google.com TemplateArgumentList TemplateArgs(SemaRef.Context, Converted, 191607393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com /*TakeArgs=*/false); 191707393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com 1918ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark MultiLevelTemplateArgumentList AllTemplateArgs 1919ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs); 1920ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark 1921ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc, 192207393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com Template, Converted.getFlatArguments(), 192307393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com Converted.flatSize(), 1924ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark SourceRange(TemplateLoc, RAngleLoc)); 1925ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark 1926ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark return SemaRef.SubstTemplateName( 1927ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark Param->getDefaultArgument().getArgument().getAsTemplate(), 192807393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com Param->getDefaultArgument().getTemplateNameLoc(), 192907393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com AllTemplateArgs); 1930ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark} 1931ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark 1932ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark/// \brief If the given template parameter has a default template 1933ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark/// argument, substitute into that default template argument and 193407393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com/// return the corresponding template argument. 193507393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.comTemplateArgumentLoc 193607393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.comSema::SubstDefaultTemplateArgumentIfAvailable(TemplateDecl *Template, 193707393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com SourceLocation TemplateLoc, 193807393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com SourceLocation RAngleLoc, 193907393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com Decl *Param, 194007393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com TemplateArgumentListBuilder &Converted) { 194107393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com if (TemplateTypeParmDecl *TypeParm = dyn_cast<TemplateTypeParmDecl>(Param)) { 1942ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark if (!TypeParm->hasDefaultArgument()) 1943ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark return TemplateArgumentLoc(); 1944ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark 194507393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com TypeSourceInfo *DI = SubstDefaultTemplateArgument(*this, Template, 194607393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com TemplateLoc, 194707393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com RAngleLoc, 1948ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark TypeParm, 1949ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark Converted); 1950ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark if (DI) 195107393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com return TemplateArgumentLoc(TemplateArgument(DI->getType()), DI); 195207393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com 1953ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark return TemplateArgumentLoc(); 1954ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark } 1955ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark 19568cb1daaa1e4343eb60a7c4f21c12e33de30dad64commit-bot@chromium.org if (NonTypeTemplateParmDecl *NonTypeParm 19578cb1daaa1e4343eb60a7c4f21c12e33de30dad64commit-bot@chromium.org = dyn_cast<NonTypeTemplateParmDecl>(Param)) { 19588cb1daaa1e4343eb60a7c4f21c12e33de30dad64commit-bot@chromium.org if (!NonTypeParm->hasDefaultArgument()) 1959ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark return TemplateArgumentLoc(); 1960570863f2e22b8ea7d7c504bd15e4f766af097df2caryclark@google.com 1961570863f2e22b8ea7d7c504bd15e4f766af097df2caryclark@google.com OwningExprResult Arg = SubstDefaultTemplateArgument(*this, Template, 196207393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com TemplateLoc, 196307393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com RAngleLoc, 196407393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com NonTypeParm, 196507393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com Converted); 196607393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com if (Arg.isInvalid()) 196707393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com return TemplateArgumentLoc(); 1968ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark 1969ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark Expr *ArgE = Arg.takeAs<Expr>(); 1970ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark return TemplateArgumentLoc(TemplateArgument(ArgE), ArgE); 1971570863f2e22b8ea7d7c504bd15e4f766af097df2caryclark@google.com } 1972570863f2e22b8ea7d7c504bd15e4f766af097df2caryclark@google.com 197307393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com TemplateTemplateParmDecl *TempTempParm 1974ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark = cast<TemplateTemplateParmDecl>(Param); 1975ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark if (!TempTempParm->hasDefaultArgument()) 1976ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark return TemplateArgumentLoc(); 1977570863f2e22b8ea7d7c504bd15e4f766af097df2caryclark@google.com 1978570863f2e22b8ea7d7c504bd15e4f766af097df2caryclark@google.com TemplateName TName = SubstDefaultTemplateArgument(*this, Template, 1979570863f2e22b8ea7d7c504bd15e4f766af097df2caryclark@google.com TemplateLoc, 1980570863f2e22b8ea7d7c504bd15e4f766af097df2caryclark@google.com RAngleLoc, 1981570863f2e22b8ea7d7c504bd15e4f766af097df2caryclark@google.com TempTempParm, 1982570863f2e22b8ea7d7c504bd15e4f766af097df2caryclark@google.com Converted); 1983570863f2e22b8ea7d7c504bd15e4f766af097df2caryclark@google.com if (TName.isNull()) 1984570863f2e22b8ea7d7c504bd15e4f766af097df2caryclark@google.com return TemplateArgumentLoc(); 1985570863f2e22b8ea7d7c504bd15e4f766af097df2caryclark@google.com 1986570863f2e22b8ea7d7c504bd15e4f766af097df2caryclark@google.com return TemplateArgumentLoc(TemplateArgument(TName), 1987570863f2e22b8ea7d7c504bd15e4f766af097df2caryclark@google.com TempTempParm->getDefaultArgument().getTemplateQualifierRange(), 1988570863f2e22b8ea7d7c504bd15e4f766af097df2caryclark@google.com TempTempParm->getDefaultArgument().getTemplateNameLoc()); 1989570863f2e22b8ea7d7c504bd15e4f766af097df2caryclark@google.com} 1990570863f2e22b8ea7d7c504bd15e4f766af097df2caryclark@google.com 1991ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark/// \brief Check that the given template argument corresponds to the given 1992ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark/// template parameter. 1993ccec0f958ffc71a9986d236bc2eb335cb2111119caryclarkbool Sema::CheckTemplateArgument(NamedDecl *Param, 199407393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com const TemplateArgumentLoc &Arg, 199507393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com TemplateDecl *Template, 1996ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark SourceLocation TemplateLoc, 199707393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com SourceLocation RAngleLoc, 1998ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark TemplateArgumentListBuilder &Converted, 199907393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com CheckTemplateArgumentKind CTAK) { 2000dac1d17027dcaa5596885a9f333979418b35001ccaryclark // Check template type parameters. 2001ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) 200207393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com return CheckTemplateTypeArgument(TTP, Arg, Converted); 200307393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com 2004277c3f87656c44e0a651ed0dd56efa16c0ab07b4reed@google.com // Check non-type template parameters. 200507393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com if (NonTypeTemplateParmDecl *NTTP =dyn_cast<NonTypeTemplateParmDecl>(Param)) { 200607393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com // Do substitution on the type of the non-type template parameter 200707393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com // with the template arguments we've seen thus far. 200807393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com QualType NTTPType = NTTP->getType(); 200907393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com if (NTTPType->isDependentType()) { 201007393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com // Do substitution on the type of the non-type template parameter. 201107393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com InstantiatingTemplate Inst(*this, TemplateLoc, Template, 201207393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com NTTP, Converted.getFlatArguments(), 201307393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com Converted.flatSize(), 2014a4aced47281e085201a356ce888b92138846e9f6skia.committer@gmail.com SourceRange(TemplateLoc, RAngleLoc)); 201507e97fccd2d85076cd22ef411b0773ab92a18abecaryclark@google.com 201607e97fccd2d85076cd22ef411b0773ab92a18abecaryclark@google.com TemplateArgumentList TemplateArgs(Context, Converted, 201707393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com /*TakeArgs=*/false); 201807393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com NTTPType = SubstType(NTTPType, 201907393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com MultiLevelTemplateArgumentList(TemplateArgs), 202007393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com NTTP->getLocation(), 202107393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com NTTP->getDeclName()); 202207393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com // If that worked, check the non-type template parameter type 202307393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com // for validity. 202407393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com if (!NTTPType.isNull()) 202507393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com NTTPType = CheckNonTypeTemplateParameterType(NTTPType, 202607393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com NTTP->getLocation()); 2027ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark if (NTTPType.isNull()) 2028ccec0f958ffc71a9986d236bc2eb335cb2111119caryclark return true; 202907393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com } 2030 2031 switch (Arg.getArgument().getKind()) { 2032 case TemplateArgument::Null: 2033 assert(false && "Should never see a NULL template argument here"); 2034 return true; 2035 2036 case TemplateArgument::Expression: { 2037 Expr *E = Arg.getArgument().getAsExpr(); 2038 TemplateArgument Result; 2039 if (CheckTemplateArgument(NTTP, NTTPType, E, Result, CTAK)) 2040 return true; 2041 2042 Converted.Append(Result); 2043 break; 2044 } 2045 2046 case TemplateArgument::Declaration: 2047 case TemplateArgument::Integral: 2048 // We've already checked this template argument, so just copy 2049 // it to the list of converted arguments. 2050 Converted.Append(Arg.getArgument()); 2051 break; 2052 2053 case TemplateArgument::Template: 2054 // We were given a template template argument. It may not be ill-formed; 2055 // see below. 2056 if (DependentTemplateName *DTN 2057 = Arg.getArgument().getAsTemplate().getAsDependentTemplateName()) { 2058 // We have a template argument such as \c T::template X, which we 2059 // parsed as a template template argument. However, since we now 2060 // know that we need a non-type template argument, convert this 2061 // template name into an expression. 2062 Expr *E = DependentScopeDeclRefExpr::Create(Context, 2063 DTN->getQualifier(), 2064 Arg.getTemplateQualifierRange(), 2065 DTN->getIdentifier(), 2066 Arg.getTemplateNameLoc()); 2067 2068 TemplateArgument Result; 2069 if (CheckTemplateArgument(NTTP, NTTPType, E, Result)) 2070 return true; 2071 2072 Converted.Append(Result); 2073 break; 2074 } 2075 2076 // We have a template argument that actually does refer to a class 2077 // template, template alias, or template template parameter, and 2078 // therefore cannot be a non-type template argument. 2079 Diag(Arg.getLocation(), diag::err_template_arg_must_be_expr) 2080 << Arg.getSourceRange(); 2081 2082 Diag(Param->getLocation(), diag::note_template_param_here); 2083 return true; 2084 2085 case TemplateArgument::Type: { 2086 // We have a non-type template parameter but the template 2087 // argument is a type. 2088 2089 // C++ [temp.arg]p2: 2090 // In a template-argument, an ambiguity between a type-id and 2091 // an expression is resolved to a type-id, regardless of the 2092 // form of the corresponding template-parameter. 2093 // 2094 // We warn specifically about this case, since it can be rather 2095 // confusing for users. 2096 QualType T = Arg.getArgument().getAsType(); 2097 SourceRange SR = Arg.getSourceRange(); 2098 if (T->isFunctionType()) 2099 Diag(SR.getBegin(), diag::err_template_arg_nontype_ambig) << SR << T; 2100 else 2101 Diag(SR.getBegin(), diag::err_template_arg_must_be_expr) << SR; 2102 Diag(Param->getLocation(), diag::note_template_param_here); 2103 return true; 2104 } 2105 2106 case TemplateArgument::Pack: 2107 llvm_unreachable("Caller must expand template argument packs"); 2108 break; 2109 } 2110 2111 return false; 2112 } 2113 2114 2115 // Check template template parameters. 2116 TemplateTemplateParmDecl *TempParm = cast<TemplateTemplateParmDecl>(Param); 2117 2118 // Substitute into the template parameter list of the template 2119 // template parameter, since previously-supplied template arguments 2120 // may appear within the template template parameter. 2121 { 2122 // Set up a template instantiation context. 2123 LocalInstantiationScope Scope(*this); 2124 InstantiatingTemplate Inst(*this, TemplateLoc, Template, 2125 TempParm, Converted.getFlatArguments(), 2126 Converted.flatSize(), 2127 SourceRange(TemplateLoc, RAngleLoc)); 2128 2129 TemplateArgumentList TemplateArgs(Context, Converted, 2130 /*TakeArgs=*/false); 2131 TempParm = cast_or_null<TemplateTemplateParmDecl>( 2132 SubstDecl(TempParm, CurContext, 2133 MultiLevelTemplateArgumentList(TemplateArgs))); 2134 if (!TempParm) 2135 return true; 2136 2137 // FIXME: TempParam is leaked. 2138 } 2139 2140 switch (Arg.getArgument().getKind()) { 2141 case TemplateArgument::Null: 2142 assert(false && "Should never see a NULL template argument here"); 2143 return true; 2144 2145 case TemplateArgument::Template: 2146 if (CheckTemplateArgument(TempParm, Arg)) 2147 return true; 2148 2149 Converted.Append(Arg.getArgument()); 2150 break; 2151 2152 case TemplateArgument::Expression: 2153 case TemplateArgument::Type: 2154 // We have a template template parameter but the template 2155 // argument does not refer to a template. 2156 Diag(Arg.getLocation(), diag::err_template_arg_must_be_template); 2157 return true; 2158 2159 case TemplateArgument::Declaration: 2160 llvm_unreachable( 2161 "Declaration argument with template template parameter"); 2162 break; 2163 case TemplateArgument::Integral: 2164 llvm_unreachable( 2165 "Integral argument with template template parameter"); 2166 break; 2167 2168 case TemplateArgument::Pack: 2169 llvm_unreachable("Caller must expand template argument packs"); 2170 break; 2171 } 2172 2173 return false; 2174} 2175 2176/// \brief Check that the given template argument list is well-formed 2177/// for specializing the given template. 2178bool Sema::CheckTemplateArgumentList(TemplateDecl *Template, 2179 SourceLocation TemplateLoc, 2180 const TemplateArgumentListInfo &TemplateArgs, 2181 bool PartialTemplateArgs, 2182 TemplateArgumentListBuilder &Converted) { 2183 TemplateParameterList *Params = Template->getTemplateParameters(); 2184 unsigned NumParams = Params->size(); 2185 unsigned NumArgs = TemplateArgs.size(); 2186 bool Invalid = false; 2187 2188 SourceLocation RAngleLoc = TemplateArgs.getRAngleLoc(); 2189 2190 bool HasParameterPack = 2191 NumParams > 0 && Params->getParam(NumParams - 1)->isTemplateParameterPack(); 2192 2193 if ((NumArgs > NumParams && !HasParameterPack) || 2194 (NumArgs < Params->getMinRequiredArguments() && 2195 !PartialTemplateArgs)) { 2196 // FIXME: point at either the first arg beyond what we can handle, 2197 // or the '>', depending on whether we have too many or too few 2198 // arguments. 2199 SourceRange Range; 2200 if (NumArgs > NumParams) 2201 Range = SourceRange(TemplateArgs[NumParams].getLocation(), RAngleLoc); 2202 Diag(TemplateLoc, diag::err_template_arg_list_different_arity) 2203 << (NumArgs > NumParams) 2204 << (isa<ClassTemplateDecl>(Template)? 0 : 2205 isa<FunctionTemplateDecl>(Template)? 1 : 2206 isa<TemplateTemplateParmDecl>(Template)? 2 : 3) 2207 << Template << Range; 2208 Diag(Template->getLocation(), diag::note_template_decl_here) 2209 << Params->getSourceRange(); 2210 Invalid = true; 2211 } 2212 2213 // C++ [temp.arg]p1: 2214 // [...] The type and form of each template-argument specified in 2215 // a template-id shall match the type and form specified for the 2216 // corresponding parameter declared by the template in its 2217 // template-parameter-list. 2218 unsigned ArgIdx = 0; 2219 for (TemplateParameterList::iterator Param = Params->begin(), 2220 ParamEnd = Params->end(); 2221 Param != ParamEnd; ++Param, ++ArgIdx) { 2222 if (ArgIdx > NumArgs && PartialTemplateArgs) 2223 break; 2224 2225 // If we have a template parameter pack, check every remaining template 2226 // argument against that template parameter pack. 2227 if ((*Param)->isTemplateParameterPack()) { 2228 Converted.BeginPack(); 2229 for (; ArgIdx < NumArgs; ++ArgIdx) { 2230 if (CheckTemplateArgument(*Param, TemplateArgs[ArgIdx], Template, 2231 TemplateLoc, RAngleLoc, Converted)) { 2232 Invalid = true; 2233 break; 2234 } 2235 } 2236 Converted.EndPack(); 2237 continue; 2238 } 2239 2240 if (ArgIdx < NumArgs) { 2241 // Check the template argument we were given. 2242 if (CheckTemplateArgument(*Param, TemplateArgs[ArgIdx], Template, 2243 TemplateLoc, RAngleLoc, Converted)) 2244 return true; 2245 2246 continue; 2247 } 2248 2249 // We have a default template argument that we will use. 2250 TemplateArgumentLoc Arg; 2251 2252 // Retrieve the default template argument from the template 2253 // parameter. For each kind of template parameter, we substitute the 2254 // template arguments provided thus far and any "outer" template arguments 2255 // (when the template parameter was part of a nested template) into 2256 // the default argument. 2257 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*Param)) { 2258 if (!TTP->hasDefaultArgument()) { 2259 assert((Invalid || PartialTemplateArgs) && "Missing default argument"); 2260 break; 2261 } 2262 2263 TypeSourceInfo *ArgType = SubstDefaultTemplateArgument(*this, 2264 Template, 2265 TemplateLoc, 2266 RAngleLoc, 2267 TTP, 2268 Converted); 2269 if (!ArgType) 2270 return true; 2271 2272 Arg = TemplateArgumentLoc(TemplateArgument(ArgType->getType()), 2273 ArgType); 2274 } else if (NonTypeTemplateParmDecl *NTTP 2275 = dyn_cast<NonTypeTemplateParmDecl>(*Param)) { 2276 if (!NTTP->hasDefaultArgument()) { 2277 assert((Invalid || PartialTemplateArgs) && "Missing default argument"); 2278 break; 2279 } 2280 2281 Sema::OwningExprResult E = SubstDefaultTemplateArgument(*this, Template, 2282 TemplateLoc, 2283 RAngleLoc, 2284 NTTP, 2285 Converted); 2286 if (E.isInvalid()) 2287 return true; 2288 2289 Expr *Ex = E.takeAs<Expr>(); 2290 Arg = TemplateArgumentLoc(TemplateArgument(Ex), Ex); 2291 } else { 2292 TemplateTemplateParmDecl *TempParm 2293 = cast<TemplateTemplateParmDecl>(*Param); 2294 2295 if (!TempParm->hasDefaultArgument()) { 2296 assert((Invalid || PartialTemplateArgs) && "Missing default argument"); 2297 break; 2298 } 2299 2300 TemplateName Name = SubstDefaultTemplateArgument(*this, Template, 2301 TemplateLoc, 2302 RAngleLoc, 2303 TempParm, 2304 Converted); 2305 if (Name.isNull()) 2306 return true; 2307 2308 Arg = TemplateArgumentLoc(TemplateArgument(Name), 2309 TempParm->getDefaultArgument().getTemplateQualifierRange(), 2310 TempParm->getDefaultArgument().getTemplateNameLoc()); 2311 } 2312 2313 // Introduce an instantiation record that describes where we are using 2314 // the default template argument. 2315 InstantiatingTemplate Instantiating(*this, RAngleLoc, Template, *Param, 2316 Converted.getFlatArguments(), 2317 Converted.flatSize(), 2318 SourceRange(TemplateLoc, RAngleLoc)); 2319 2320 // Check the default template argument. 2321 if (CheckTemplateArgument(*Param, Arg, Template, TemplateLoc, 2322 RAngleLoc, Converted)) 2323 return true; 2324 } 2325 2326 return Invalid; 2327} 2328 2329/// \brief Check a template argument against its corresponding 2330/// template type parameter. 2331/// 2332/// This routine implements the semantics of C++ [temp.arg.type]. It 2333/// returns true if an error occurred, and false otherwise. 2334bool Sema::CheckTemplateArgument(TemplateTypeParmDecl *Param, 2335 TypeSourceInfo *ArgInfo) { 2336 assert(ArgInfo && "invalid TypeSourceInfo"); 2337 QualType Arg = ArgInfo->getType(); 2338 2339 // C++ [temp.arg.type]p2: 2340 // A local type, a type with no linkage, an unnamed type or a type 2341 // compounded from any of these types shall not be used as a 2342 // template-argument for a template type-parameter. 2343 // 2344 // FIXME: Perform the recursive and no-linkage type checks. 2345 const TagType *Tag = 0; 2346 if (const EnumType *EnumT = Arg->getAs<EnumType>()) 2347 Tag = EnumT; 2348 else if (const RecordType *RecordT = Arg->getAs<RecordType>()) 2349 Tag = RecordT; 2350 if (Tag && Tag->getDecl()->getDeclContext()->isFunctionOrMethod()) { 2351 SourceRange SR = ArgInfo->getTypeLoc().getFullSourceRange(); 2352 return Diag(SR.getBegin(), diag::err_template_arg_local_type) 2353 << QualType(Tag, 0) << SR; 2354 } else if (Tag && !Tag->getDecl()->getDeclName() && 2355 !Tag->getDecl()->getTypedefForAnonDecl()) { 2356 SourceRange SR = ArgInfo->getTypeLoc().getFullSourceRange(); 2357 Diag(SR.getBegin(), diag::err_template_arg_unnamed_type) << SR; 2358 Diag(Tag->getDecl()->getLocation(), diag::note_template_unnamed_type_here); 2359 return true; 2360 } else if (Context.hasSameUnqualifiedType(Arg, Context.OverloadTy)) { 2361 SourceRange SR = ArgInfo->getTypeLoc().getFullSourceRange(); 2362 return Diag(SR.getBegin(), diag::err_template_arg_overload_type) << SR; 2363 } 2364 2365 return false; 2366} 2367 2368/// \brief Checks whether the given template argument is the address 2369/// of an object or function according to C++ [temp.arg.nontype]p1. 2370static bool 2371CheckTemplateArgumentAddressOfObjectOrFunction(Sema &S, 2372 NonTypeTemplateParmDecl *Param, 2373 QualType ParamType, 2374 Expr *ArgIn, 2375 TemplateArgument &Converted) { 2376 bool Invalid = false; 2377 Expr *Arg = ArgIn; 2378 QualType ArgType = Arg->getType(); 2379 2380 // See through any implicit casts we added to fix the type. 2381 while (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(Arg)) 2382 Arg = Cast->getSubExpr(); 2383 2384 // C++ [temp.arg.nontype]p1: 2385 // 2386 // A template-argument for a non-type, non-template 2387 // template-parameter shall be one of: [...] 2388 // 2389 // -- the address of an object or function with external 2390 // linkage, including function templates and function 2391 // template-ids but excluding non-static class members, 2392 // expressed as & id-expression where the & is optional if 2393 // the name refers to a function or array, or if the 2394 // corresponding template-parameter is a reference; or 2395 DeclRefExpr *DRE = 0; 2396 2397 // Ignore (and complain about) any excess parentheses. 2398 while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) { 2399 if (!Invalid) { 2400 S.Diag(Arg->getSourceRange().getBegin(), 2401 diag::err_template_arg_extra_parens) 2402 << Arg->getSourceRange(); 2403 Invalid = true; 2404 } 2405 2406 Arg = Parens->getSubExpr(); 2407 } 2408 2409 bool AddressTaken = false; 2410 SourceLocation AddrOpLoc; 2411 if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) { 2412 if (UnOp->getOpcode() == UnaryOperator::AddrOf) { 2413 DRE = dyn_cast<DeclRefExpr>(UnOp->getSubExpr()); 2414 AddressTaken = true; 2415 AddrOpLoc = UnOp->getOperatorLoc(); 2416 } 2417 } else 2418 DRE = dyn_cast<DeclRefExpr>(Arg); 2419 2420 if (!DRE) { 2421 S.Diag(Arg->getLocStart(), diag::err_template_arg_not_decl_ref) 2422 << Arg->getSourceRange(); 2423 S.Diag(Param->getLocation(), diag::note_template_param_here); 2424 return true; 2425 } 2426 2427 // Stop checking the precise nature of the argument if it is value dependent, 2428 // it should be checked when instantiated. 2429 if (Arg->isValueDependent()) { 2430 Converted = TemplateArgument(ArgIn->Retain()); 2431 return false; 2432 } 2433 2434 if (!isa<ValueDecl>(DRE->getDecl())) { 2435 S.Diag(Arg->getSourceRange().getBegin(), 2436 diag::err_template_arg_not_object_or_func_form) 2437 << Arg->getSourceRange(); 2438 S.Diag(Param->getLocation(), diag::note_template_param_here); 2439 return true; 2440 } 2441 2442 NamedDecl *Entity = 0; 2443 2444 // Cannot refer to non-static data members 2445 if (FieldDecl *Field = dyn_cast<FieldDecl>(DRE->getDecl())) { 2446 S.Diag(Arg->getSourceRange().getBegin(), diag::err_template_arg_field) 2447 << Field << Arg->getSourceRange(); 2448 S.Diag(Param->getLocation(), diag::note_template_param_here); 2449 return true; 2450 } 2451 2452 // Cannot refer to non-static member functions 2453 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(DRE->getDecl())) 2454 if (!Method->isStatic()) { 2455 S.Diag(Arg->getSourceRange().getBegin(), diag::err_template_arg_method) 2456 << Method << Arg->getSourceRange(); 2457 S.Diag(Param->getLocation(), diag::note_template_param_here); 2458 return true; 2459 } 2460 2461 // Functions must have external linkage. 2462 if (FunctionDecl *Func = dyn_cast<FunctionDecl>(DRE->getDecl())) { 2463 if (!isExternalLinkage(Func->getLinkage())) { 2464 S.Diag(Arg->getSourceRange().getBegin(), 2465 diag::err_template_arg_function_not_extern) 2466 << Func << Arg->getSourceRange(); 2467 S.Diag(Func->getLocation(), diag::note_template_arg_internal_object) 2468 << true; 2469 return true; 2470 } 2471 2472 // Okay: we've named a function with external linkage. 2473 Entity = Func; 2474 2475 // If the template parameter has pointer type, the function decays. 2476 if (ParamType->isPointerType() && !AddressTaken) 2477 ArgType = S.Context.getPointerType(Func->getType()); 2478 else if (AddressTaken && ParamType->isReferenceType()) { 2479 // If we originally had an address-of operator, but the 2480 // parameter has reference type, complain and (if things look 2481 // like they will work) drop the address-of operator. 2482 if (!S.Context.hasSameUnqualifiedType(Func->getType(), 2483 ParamType.getNonReferenceType())) { 2484 S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) 2485 << ParamType; 2486 S.Diag(Param->getLocation(), diag::note_template_param_here); 2487 return true; 2488 } 2489 2490 S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) 2491 << ParamType 2492 << FixItHint::CreateRemoval(AddrOpLoc); 2493 S.Diag(Param->getLocation(), diag::note_template_param_here); 2494 2495 ArgType = Func->getType(); 2496 } 2497 } else if (VarDecl *Var = dyn_cast<VarDecl>(DRE->getDecl())) { 2498 if (!isExternalLinkage(Var->getLinkage())) { 2499 S.Diag(Arg->getSourceRange().getBegin(), 2500 diag::err_template_arg_object_not_extern) 2501 << Var << Arg->getSourceRange(); 2502 S.Diag(Var->getLocation(), diag::note_template_arg_internal_object) 2503 << true; 2504 return true; 2505 } 2506 2507 // A value of reference type is not an object. 2508 if (Var->getType()->isReferenceType()) { 2509 S.Diag(Arg->getSourceRange().getBegin(), 2510 diag::err_template_arg_reference_var) 2511 << Var->getType() << Arg->getSourceRange(); 2512 S.Diag(Param->getLocation(), diag::note_template_param_here); 2513 return true; 2514 } 2515 2516 // Okay: we've named an object with external linkage 2517 Entity = Var; 2518 2519 // If the template parameter has pointer type, we must have taken 2520 // the address of this object. 2521 if (ParamType->isReferenceType()) { 2522 if (AddressTaken) { 2523 // If we originally had an address-of operator, but the 2524 // parameter has reference type, complain and (if things look 2525 // like they will work) drop the address-of operator. 2526 if (!S.Context.hasSameUnqualifiedType(Var->getType(), 2527 ParamType.getNonReferenceType())) { 2528 S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) 2529 << ParamType; 2530 S.Diag(Param->getLocation(), diag::note_template_param_here); 2531 return true; 2532 } 2533 2534 S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) 2535 << ParamType 2536 << FixItHint::CreateRemoval(AddrOpLoc); 2537 S.Diag(Param->getLocation(), diag::note_template_param_here); 2538 2539 ArgType = Var->getType(); 2540 } 2541 } else if (!AddressTaken && ParamType->isPointerType()) { 2542 if (Var->getType()->isArrayType()) { 2543 // Array-to-pointer decay. 2544 ArgType = S.Context.getArrayDecayedType(Var->getType()); 2545 } else { 2546 // If the template parameter has pointer type but the address of 2547 // this object was not taken, complain and (possibly) recover by 2548 // taking the address of the entity. 2549 ArgType = S.Context.getPointerType(Var->getType()); 2550 if (!S.Context.hasSameUnqualifiedType(ArgType, ParamType)) { 2551 S.Diag(Arg->getLocStart(), diag::err_template_arg_not_address_of) 2552 << ParamType; 2553 S.Diag(Param->getLocation(), diag::note_template_param_here); 2554 return true; 2555 } 2556 2557 S.Diag(Arg->getLocStart(), diag::err_template_arg_not_address_of) 2558 << ParamType 2559 << FixItHint::CreateInsertion(Arg->getLocStart(), "&"); 2560 2561 S.Diag(Param->getLocation(), diag::note_template_param_here); 2562 } 2563 } 2564 } else { 2565 // We found something else, but we don't know specifically what it is. 2566 S.Diag(Arg->getSourceRange().getBegin(), 2567 diag::err_template_arg_not_object_or_func) 2568 << Arg->getSourceRange(); 2569 S.Diag(DRE->getDecl()->getLocation(), diag::note_template_arg_refers_here); 2570 return true; 2571 } 2572 2573 if (ParamType->isPointerType() && 2574 !ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType() && 2575 S.IsQualificationConversion(ArgType, ParamType)) { 2576 // For pointer-to-object types, qualification conversions are 2577 // permitted. 2578 } else { 2579 if (const ReferenceType *ParamRef = ParamType->getAs<ReferenceType>()) { 2580 if (!ParamRef->getPointeeType()->isFunctionType()) { 2581 // C++ [temp.arg.nontype]p5b3: 2582 // For a non-type template-parameter of type reference to 2583 // object, no conversions apply. The type referred to by the 2584 // reference may be more cv-qualified than the (otherwise 2585 // identical) type of the template- argument. The 2586 // template-parameter is bound directly to the 2587 // template-argument, which shall be an lvalue. 2588 2589 // FIXME: Other qualifiers? 2590 unsigned ParamQuals = ParamRef->getPointeeType().getCVRQualifiers(); 2591 unsigned ArgQuals = ArgType.getCVRQualifiers(); 2592 2593 if ((ParamQuals | ArgQuals) != ParamQuals) { 2594 S.Diag(Arg->getSourceRange().getBegin(), 2595 diag::err_template_arg_ref_bind_ignores_quals) 2596 << ParamType << Arg->getType() 2597 << Arg->getSourceRange(); 2598 S.Diag(Param->getLocation(), diag::note_template_param_here); 2599 return true; 2600 } 2601 } 2602 } 2603 2604 // At this point, the template argument refers to an object or 2605 // function with external linkage. We now need to check whether the 2606 // argument and parameter types are compatible. 2607 if (!S.Context.hasSameUnqualifiedType(ArgType, 2608 ParamType.getNonReferenceType())) { 2609 // We can't perform this conversion or binding. 2610 if (ParamType->isReferenceType()) 2611 S.Diag(Arg->getLocStart(), diag::err_template_arg_no_ref_bind) 2612 << ParamType << Arg->getType() << Arg->getSourceRange(); 2613 else 2614 S.Diag(Arg->getLocStart(), diag::err_template_arg_not_convertible) 2615 << Arg->getType() << ParamType << Arg->getSourceRange(); 2616 S.Diag(Param->getLocation(), diag::note_template_param_here); 2617 return true; 2618 } 2619 } 2620 2621 // Create the template argument. 2622 Converted = TemplateArgument(Entity->getCanonicalDecl()); 2623 S.MarkDeclarationReferenced(Arg->getLocStart(), Entity); 2624 return false; 2625} 2626 2627/// \brief Checks whether the given template argument is a pointer to 2628/// member constant according to C++ [temp.arg.nontype]p1. 2629bool Sema::CheckTemplateArgumentPointerToMember(Expr *Arg, 2630 TemplateArgument &Converted) { 2631 bool Invalid = false; 2632 2633 // See through any implicit casts we added to fix the type. 2634 while (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(Arg)) 2635 Arg = Cast->getSubExpr(); 2636 2637 // C++ [temp.arg.nontype]p1: 2638 // 2639 // A template-argument for a non-type, non-template 2640 // template-parameter shall be one of: [...] 2641 // 2642 // -- a pointer to member expressed as described in 5.3.1. 2643 DeclRefExpr *DRE = 0; 2644 2645 // Ignore (and complain about) any excess parentheses. 2646 while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) { 2647 if (!Invalid) { 2648 Diag(Arg->getSourceRange().getBegin(), 2649 diag::err_template_arg_extra_parens) 2650 << Arg->getSourceRange(); 2651 Invalid = true; 2652 } 2653 2654 Arg = Parens->getSubExpr(); 2655 } 2656 2657 // A pointer-to-member constant written &Class::member. 2658 if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) { 2659 if (UnOp->getOpcode() == UnaryOperator::AddrOf) { 2660 DRE = dyn_cast<DeclRefExpr>(UnOp->getSubExpr()); 2661 if (DRE && !DRE->getQualifier()) 2662 DRE = 0; 2663 } 2664 } 2665 // A constant of pointer-to-member type. 2666 else if ((DRE = dyn_cast<DeclRefExpr>(Arg))) { 2667 if (ValueDecl *VD = dyn_cast<ValueDecl>(DRE->getDecl())) { 2668 if (VD->getType()->isMemberPointerType()) { 2669 if (isa<NonTypeTemplateParmDecl>(VD) || 2670 (isa<VarDecl>(VD) && 2671 Context.getCanonicalType(VD->getType()).isConstQualified())) { 2672 if (Arg->isTypeDependent() || Arg->isValueDependent()) 2673 Converted = TemplateArgument(Arg->Retain()); 2674 else 2675 Converted = TemplateArgument(VD->getCanonicalDecl()); 2676 return Invalid; 2677 } 2678 } 2679 } 2680 2681 DRE = 0; 2682 } 2683 2684 if (!DRE) 2685 return Diag(Arg->getSourceRange().getBegin(), 2686 diag::err_template_arg_not_pointer_to_member_form) 2687 << Arg->getSourceRange(); 2688 2689 if (isa<FieldDecl>(DRE->getDecl()) || isa<CXXMethodDecl>(DRE->getDecl())) { 2690 assert((isa<FieldDecl>(DRE->getDecl()) || 2691 !cast<CXXMethodDecl>(DRE->getDecl())->isStatic()) && 2692 "Only non-static member pointers can make it here"); 2693 2694 // Okay: this is the address of a non-static member, and therefore 2695 // a member pointer constant. 2696 if (Arg->isTypeDependent() || Arg->isValueDependent()) 2697 Converted = TemplateArgument(Arg->Retain()); 2698 else 2699 Converted = TemplateArgument(DRE->getDecl()->getCanonicalDecl()); 2700 return Invalid; 2701 } 2702 2703 // We found something else, but we don't know specifically what it is. 2704 Diag(Arg->getSourceRange().getBegin(), 2705 diag::err_template_arg_not_pointer_to_member_form) 2706 << Arg->getSourceRange(); 2707 Diag(DRE->getDecl()->getLocation(), 2708 diag::note_template_arg_refers_here); 2709 return true; 2710} 2711 2712/// \brief Check a template argument against its corresponding 2713/// non-type template parameter. 2714/// 2715/// This routine implements the semantics of C++ [temp.arg.nontype]. 2716/// It returns true if an error occurred, and false otherwise. \p 2717/// InstantiatedParamType is the type of the non-type template 2718/// parameter after it has been instantiated. 2719/// 2720/// If no error was detected, Converted receives the converted template argument. 2721bool Sema::CheckTemplateArgument(NonTypeTemplateParmDecl *Param, 2722 QualType InstantiatedParamType, Expr *&Arg, 2723 TemplateArgument &Converted, 2724 CheckTemplateArgumentKind CTAK) { 2725 SourceLocation StartLoc = Arg->getSourceRange().getBegin(); 2726 2727 // If either the parameter has a dependent type or the argument is 2728 // type-dependent, there's nothing we can check now. 2729 if (InstantiatedParamType->isDependentType() || Arg->isTypeDependent()) { 2730 // FIXME: Produce a cloned, canonical expression? 2731 Converted = TemplateArgument(Arg); 2732 return false; 2733 } 2734 2735 // C++ [temp.arg.nontype]p5: 2736 // The following conversions are performed on each expression used 2737 // as a non-type template-argument. If a non-type 2738 // template-argument cannot be converted to the type of the 2739 // corresponding template-parameter then the program is 2740 // ill-formed. 2741 // 2742 // -- for a non-type template-parameter of integral or 2743 // enumeration type, integral promotions (4.5) and integral 2744 // conversions (4.7) are applied. 2745 QualType ParamType = InstantiatedParamType; 2746 QualType ArgType = Arg->getType(); 2747 if (ParamType->isIntegralType() || ParamType->isEnumeralType()) { 2748 // C++ [temp.arg.nontype]p1: 2749 // A template-argument for a non-type, non-template 2750 // template-parameter shall be one of: 2751 // 2752 // -- an integral constant-expression of integral or enumeration 2753 // type; or 2754 // -- the name of a non-type template-parameter; or 2755 SourceLocation NonConstantLoc; 2756 llvm::APSInt Value; 2757 if (!ArgType->isIntegralType() && !ArgType->isEnumeralType()) { 2758 Diag(Arg->getSourceRange().getBegin(), 2759 diag::err_template_arg_not_integral_or_enumeral) 2760 << ArgType << Arg->getSourceRange(); 2761 Diag(Param->getLocation(), diag::note_template_param_here); 2762 return true; 2763 } else if (!Arg->isValueDependent() && 2764 !Arg->isIntegerConstantExpr(Value, Context, &NonConstantLoc)) { 2765 Diag(NonConstantLoc, diag::err_template_arg_not_ice) 2766 << ArgType << Arg->getSourceRange(); 2767 return true; 2768 } 2769 2770 // From here on out, all we care about are the unqualified forms 2771 // of the parameter and argument types. 2772 ParamType = ParamType.getUnqualifiedType(); 2773 ArgType = ArgType.getUnqualifiedType(); 2774 2775 // Try to convert the argument to the parameter's type. 2776 if (Context.hasSameType(ParamType, ArgType)) { 2777 // Okay: no conversion necessary 2778 } else if (CTAK == CTAK_Deduced) { 2779 // C++ [temp.deduct.type]p17: 2780 // If, in the declaration of a function template with a non-type 2781 // template-parameter, the non-type template- parameter is used 2782 // in an expression in the function parameter-list and, if the 2783 // corresponding template-argument is deduced, the 2784 // template-argument type shall match the type of the 2785 // template-parameter exactly, except that a template-argument 2786 // deduced from an array bound may be of any integral type. 2787 Diag(StartLoc, diag::err_deduced_non_type_template_arg_type_mismatch) 2788 << ArgType << ParamType; 2789 Diag(Param->getLocation(), diag::note_template_param_here); 2790 return true; 2791 } else if (IsIntegralPromotion(Arg, ArgType, ParamType) || 2792 !ParamType->isEnumeralType()) { 2793 // This is an integral promotion or conversion. 2794 ImpCastExprToType(Arg, ParamType, CastExpr::CK_IntegralCast); 2795 } else { 2796 // We can't perform this conversion. 2797 Diag(Arg->getSourceRange().getBegin(), 2798 diag::err_template_arg_not_convertible) 2799 << Arg->getType() << InstantiatedParamType << Arg->getSourceRange(); 2800 Diag(Param->getLocation(), diag::note_template_param_here); 2801 return true; 2802 } 2803 2804 QualType IntegerType = Context.getCanonicalType(ParamType); 2805 if (const EnumType *Enum = IntegerType->getAs<EnumType>()) 2806 IntegerType = Context.getCanonicalType(Enum->getDecl()->getIntegerType()); 2807 2808 if (!Arg->isValueDependent()) { 2809 llvm::APSInt OldValue = Value; 2810 2811 // Coerce the template argument's value to the value it will have 2812 // based on the template parameter's type. 2813 unsigned AllowedBits = Context.getTypeSize(IntegerType); 2814 if (Value.getBitWidth() != AllowedBits) 2815 Value.extOrTrunc(AllowedBits); 2816 Value.setIsSigned(IntegerType->isSignedIntegerType()); 2817 2818 // Complain if an unsigned parameter received a negative value. 2819 if (IntegerType->isUnsignedIntegerType() 2820 && (OldValue.isSigned() && OldValue.isNegative())) { 2821 Diag(Arg->getSourceRange().getBegin(), diag::warn_template_arg_negative) 2822 << OldValue.toString(10) << Value.toString(10) << Param->getType() 2823 << Arg->getSourceRange(); 2824 Diag(Param->getLocation(), diag::note_template_param_here); 2825 } 2826 2827 // Complain if we overflowed the template parameter's type. 2828 unsigned RequiredBits; 2829 if (IntegerType->isUnsignedIntegerType()) 2830 RequiredBits = OldValue.getActiveBits(); 2831 else if (OldValue.isUnsigned()) 2832 RequiredBits = OldValue.getActiveBits() + 1; 2833 else 2834 RequiredBits = OldValue.getMinSignedBits(); 2835 if (RequiredBits > AllowedBits) { 2836 Diag(Arg->getSourceRange().getBegin(), 2837 diag::warn_template_arg_too_large) 2838 << OldValue.toString(10) << Value.toString(10) << Param->getType() 2839 << Arg->getSourceRange(); 2840 Diag(Param->getLocation(), diag::note_template_param_here); 2841 } 2842 } 2843 2844 // Add the value of this argument to the list of converted 2845 // arguments. We use the bitwidth and signedness of the template 2846 // parameter. 2847 if (Arg->isValueDependent()) { 2848 // The argument is value-dependent. Create a new 2849 // TemplateArgument with the converted expression. 2850 Converted = TemplateArgument(Arg); 2851 return false; 2852 } 2853 2854 Converted = TemplateArgument(Value, 2855 ParamType->isEnumeralType() ? ParamType 2856 : IntegerType); 2857 return false; 2858 } 2859 2860 DeclAccessPair FoundResult; // temporary for ResolveOverloadedFunction 2861 2862 // C++0x [temp.arg.nontype]p5 bullets 2, 4 and 6 permit conversion 2863 // from a template argument of type std::nullptr_t to a non-type 2864 // template parameter of type pointer to object, pointer to 2865 // function, or pointer-to-member, respectively. 2866 if (ArgType->isNullPtrType() && 2867 (ParamType->isPointerType() || ParamType->isMemberPointerType())) { 2868 Converted = TemplateArgument((NamedDecl *)0); 2869 return false; 2870 } 2871 2872 // Handle pointer-to-function, reference-to-function, and 2873 // pointer-to-member-function all in (roughly) the same way. 2874 if (// -- For a non-type template-parameter of type pointer to 2875 // function, only the function-to-pointer conversion (4.3) is 2876 // applied. If the template-argument represents a set of 2877 // overloaded functions (or a pointer to such), the matching 2878 // function is selected from the set (13.4). 2879 (ParamType->isPointerType() && 2880 ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType()) || 2881 // -- For a non-type template-parameter of type reference to 2882 // function, no conversions apply. If the template-argument 2883 // represents a set of overloaded functions, the matching 2884 // function is selected from the set (13.4). 2885 (ParamType->isReferenceType() && 2886 ParamType->getAs<ReferenceType>()->getPointeeType()->isFunctionType()) || 2887 // -- For a non-type template-parameter of type pointer to 2888 // member function, no conversions apply. If the 2889 // template-argument represents a set of overloaded member 2890 // functions, the matching member function is selected from 2891 // the set (13.4). 2892 (ParamType->isMemberPointerType() && 2893 ParamType->getAs<MemberPointerType>()->getPointeeType() 2894 ->isFunctionType())) { 2895 2896 if (Arg->getType() == Context.OverloadTy) { 2897 if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg, ParamType, 2898 true, 2899 FoundResult)) { 2900 if (DiagnoseUseOfDecl(Fn, Arg->getSourceRange().getBegin())) 2901 return true; 2902 2903 Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn); 2904 ArgType = Arg->getType(); 2905 } else 2906 return true; 2907 } 2908 2909 if (!ParamType->isMemberPointerType()) 2910 return CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param, 2911 ParamType, 2912 Arg, Converted); 2913 2914 if (IsQualificationConversion(ArgType, ParamType.getNonReferenceType())) { 2915 ImpCastExprToType(Arg, ParamType, CastExpr::CK_NoOp, 2916 Arg->isLvalue(Context) == Expr::LV_Valid); 2917 } else if (!Context.hasSameUnqualifiedType(ArgType, 2918 ParamType.getNonReferenceType())) { 2919 // We can't perform this conversion. 2920 Diag(Arg->getSourceRange().getBegin(), 2921 diag::err_template_arg_not_convertible) 2922 << Arg->getType() << InstantiatedParamType << Arg->getSourceRange(); 2923 Diag(Param->getLocation(), diag::note_template_param_here); 2924 return true; 2925 } 2926 2927 return CheckTemplateArgumentPointerToMember(Arg, Converted); 2928 } 2929 2930 if (ParamType->isPointerType()) { 2931 // -- for a non-type template-parameter of type pointer to 2932 // object, qualification conversions (4.4) and the 2933 // array-to-pointer conversion (4.2) are applied. 2934 // C++0x also allows a value of std::nullptr_t. 2935 assert(ParamType->getAs<PointerType>()->getPointeeType()->isObjectType() && 2936 "Only object pointers allowed here"); 2937 2938 return CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param, 2939 ParamType, 2940 Arg, Converted); 2941 } 2942 2943 if (const ReferenceType *ParamRefType = ParamType->getAs<ReferenceType>()) { 2944 // -- For a non-type template-parameter of type reference to 2945 // object, no conversions apply. The type referred to by the 2946 // reference may be more cv-qualified than the (otherwise 2947 // identical) type of the template-argument. The 2948 // template-parameter is bound directly to the 2949 // template-argument, which must be an lvalue. 2950 assert(ParamRefType->getPointeeType()->isObjectType() && 2951 "Only object references allowed here"); 2952 2953 if (Arg->getType() == Context.OverloadTy) { 2954 if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg, 2955 ParamRefType->getPointeeType(), 2956 true, 2957 FoundResult)) { 2958 if (DiagnoseUseOfDecl(Fn, Arg->getSourceRange().getBegin())) 2959 return true; 2960 2961 Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn); 2962 ArgType = Arg->getType(); 2963 } else 2964 return true; 2965 } 2966 2967 return CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param, 2968 ParamType, 2969 Arg, Converted); 2970 } 2971 2972 // -- For a non-type template-parameter of type pointer to data 2973 // member, qualification conversions (4.4) are applied. 2974 assert(ParamType->isMemberPointerType() && "Only pointers to members remain"); 2975 2976 if (Context.hasSameUnqualifiedType(ParamType, ArgType)) { 2977 // Types match exactly: nothing more to do here. 2978 } else if (IsQualificationConversion(ArgType, ParamType)) { 2979 ImpCastExprToType(Arg, ParamType, CastExpr::CK_NoOp, 2980 Arg->isLvalue(Context) == Expr::LV_Valid); 2981 } else { 2982 // We can't perform this conversion. 2983 Diag(Arg->getSourceRange().getBegin(), 2984 diag::err_template_arg_not_convertible) 2985 << Arg->getType() << InstantiatedParamType << Arg->getSourceRange(); 2986 Diag(Param->getLocation(), diag::note_template_param_here); 2987 return true; 2988 } 2989 2990 return CheckTemplateArgumentPointerToMember(Arg, Converted); 2991} 2992 2993/// \brief Check a template argument against its corresponding 2994/// template template parameter. 2995/// 2996/// This routine implements the semantics of C++ [temp.arg.template]. 2997/// It returns true if an error occurred, and false otherwise. 2998bool Sema::CheckTemplateArgument(TemplateTemplateParmDecl *Param, 2999 const TemplateArgumentLoc &Arg) { 3000 TemplateName Name = Arg.getArgument().getAsTemplate(); 3001 TemplateDecl *Template = Name.getAsTemplateDecl(); 3002 if (!Template) { 3003 // Any dependent template name is fine. 3004 assert(Name.isDependent() && "Non-dependent template isn't a declaration?"); 3005 return false; 3006 } 3007 3008 // C++ [temp.arg.template]p1: 3009 // A template-argument for a template template-parameter shall be 3010 // the name of a class template, expressed as id-expression. Only 3011 // primary class templates are considered when matching the 3012 // template template argument with the corresponding parameter; 3013 // partial specializations are not considered even if their 3014 // parameter lists match that of the template template parameter. 3015 // 3016 // Note that we also allow template template parameters here, which 3017 // will happen when we are dealing with, e.g., class template 3018 // partial specializations. 3019 if (!isa<ClassTemplateDecl>(Template) && 3020 !isa<TemplateTemplateParmDecl>(Template)) { 3021 assert(isa<FunctionTemplateDecl>(Template) && 3022 "Only function templates are possible here"); 3023 Diag(Arg.getLocation(), diag::err_template_arg_not_class_template); 3024 Diag(Template->getLocation(), diag::note_template_arg_refers_here_func) 3025 << Template; 3026 } 3027 3028 return !TemplateParameterListsAreEqual(Template->getTemplateParameters(), 3029 Param->getTemplateParameters(), 3030 true, 3031 TPL_TemplateTemplateArgumentMatch, 3032 Arg.getLocation()); 3033} 3034 3035/// \brief Given a non-type template argument that refers to a 3036/// declaration and the type of its corresponding non-type template 3037/// parameter, produce an expression that properly refers to that 3038/// declaration. 3039Sema::OwningExprResult 3040Sema::BuildExpressionFromDeclTemplateArgument(const TemplateArgument &Arg, 3041 QualType ParamType, 3042 SourceLocation Loc) { 3043 assert(Arg.getKind() == TemplateArgument::Declaration && 3044 "Only declaration template arguments permitted here"); 3045 ValueDecl *VD = cast<ValueDecl>(Arg.getAsDecl()); 3046 3047 if (VD->getDeclContext()->isRecord() && 3048 (isa<CXXMethodDecl>(VD) || isa<FieldDecl>(VD))) { 3049 // If the value is a class member, we might have a pointer-to-member. 3050 // Determine whether the non-type template template parameter is of 3051 // pointer-to-member type. If so, we need to build an appropriate 3052 // expression for a pointer-to-member, since a "normal" DeclRefExpr 3053 // would refer to the member itself. 3054 if (ParamType->isMemberPointerType()) { 3055 QualType ClassType 3056 = Context.getTypeDeclType(cast<RecordDecl>(VD->getDeclContext())); 3057 NestedNameSpecifier *Qualifier 3058 = NestedNameSpecifier::Create(Context, 0, false, ClassType.getTypePtr()); 3059 CXXScopeSpec SS; 3060 SS.setScopeRep(Qualifier); 3061 OwningExprResult RefExpr = BuildDeclRefExpr(VD, 3062 VD->getType().getNonReferenceType(), 3063 Loc, 3064 &SS); 3065 if (RefExpr.isInvalid()) 3066 return ExprError(); 3067 3068 RefExpr = CreateBuiltinUnaryOp(Loc, UnaryOperator::AddrOf, move(RefExpr)); 3069 3070 // We might need to perform a trailing qualification conversion, since 3071 // the element type on the parameter could be more qualified than the 3072 // element type in the expression we constructed. 3073 if (IsQualificationConversion(((Expr*) RefExpr.get())->getType(), 3074 ParamType.getUnqualifiedType())) { 3075 Expr *RefE = RefExpr.takeAs<Expr>(); 3076 ImpCastExprToType(RefE, ParamType.getUnqualifiedType(), 3077 CastExpr::CK_NoOp); 3078 RefExpr = Owned(RefE); 3079 } 3080 3081 assert(!RefExpr.isInvalid() && 3082 Context.hasSameType(((Expr*) RefExpr.get())->getType(), 3083 ParamType.getUnqualifiedType())); 3084 return move(RefExpr); 3085 } 3086 } 3087 3088 QualType T = VD->getType().getNonReferenceType(); 3089 if (ParamType->isPointerType()) { 3090 // When the non-type template parameter is a pointer, take the 3091 // address of the declaration. 3092 OwningExprResult RefExpr = BuildDeclRefExpr(VD, T, Loc); 3093 if (RefExpr.isInvalid()) 3094 return ExprError(); 3095 3096 if (T->isFunctionType() || T->isArrayType()) { 3097 // Decay functions and arrays. 3098 Expr *RefE = (Expr *)RefExpr.get(); 3099 DefaultFunctionArrayConversion(RefE); 3100 if (RefE != RefExpr.get()) { 3101 RefExpr.release(); 3102 RefExpr = Owned(RefE); 3103 } 3104 3105 return move(RefExpr); 3106 } 3107 3108 // Take the address of everything else 3109 return CreateBuiltinUnaryOp(Loc, UnaryOperator::AddrOf, move(RefExpr)); 3110 } 3111 3112 // If the non-type template parameter has reference type, qualify the 3113 // resulting declaration reference with the extra qualifiers on the 3114 // type that the reference refers to. 3115 if (const ReferenceType *TargetRef = ParamType->getAs<ReferenceType>()) 3116 T = Context.getQualifiedType(T, TargetRef->getPointeeType().getQualifiers()); 3117 3118 return BuildDeclRefExpr(VD, T, Loc); 3119} 3120 3121/// \brief Construct a new expression that refers to the given 3122/// integral template argument with the given source-location 3123/// information. 3124/// 3125/// This routine takes care of the mapping from an integral template 3126/// argument (which may have any integral type) to the appropriate 3127/// literal value. 3128Sema::OwningExprResult 3129Sema::BuildExpressionFromIntegralTemplateArgument(const TemplateArgument &Arg, 3130 SourceLocation Loc) { 3131 assert(Arg.getKind() == TemplateArgument::Integral && 3132 "Operation is only value for integral template arguments"); 3133 QualType T = Arg.getIntegralType(); 3134 if (T->isCharType() || T->isWideCharType()) 3135 return Owned(new (Context) CharacterLiteral( 3136 Arg.getAsIntegral()->getZExtValue(), 3137 T->isWideCharType(), 3138 T, 3139 Loc)); 3140 if (T->isBooleanType()) 3141 return Owned(new (Context) CXXBoolLiteralExpr( 3142 Arg.getAsIntegral()->getBoolValue(), 3143 T, 3144 Loc)); 3145 3146 return Owned(new (Context) IntegerLiteral(*Arg.getAsIntegral(), T, Loc)); 3147} 3148 3149 3150/// \brief Determine whether the given template parameter lists are 3151/// equivalent. 3152/// 3153/// \param New The new template parameter list, typically written in the 3154/// source code as part of a new template declaration. 3155/// 3156/// \param Old The old template parameter list, typically found via 3157/// name lookup of the template declared with this template parameter 3158/// list. 3159/// 3160/// \param Complain If true, this routine will produce a diagnostic if 3161/// the template parameter lists are not equivalent. 3162/// 3163/// \param Kind describes how we are to match the template parameter lists. 3164/// 3165/// \param TemplateArgLoc If this source location is valid, then we 3166/// are actually checking the template parameter list of a template 3167/// argument (New) against the template parameter list of its 3168/// corresponding template template parameter (Old). We produce 3169/// slightly different diagnostics in this scenario. 3170/// 3171/// \returns True if the template parameter lists are equal, false 3172/// otherwise. 3173bool 3174Sema::TemplateParameterListsAreEqual(TemplateParameterList *New, 3175 TemplateParameterList *Old, 3176 bool Complain, 3177 TemplateParameterListEqualKind Kind, 3178 SourceLocation TemplateArgLoc) { 3179 if (Old->size() != New->size()) { 3180 if (Complain) { 3181 unsigned NextDiag = diag::err_template_param_list_different_arity; 3182 if (TemplateArgLoc.isValid()) { 3183 Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch); 3184 NextDiag = diag::note_template_param_list_different_arity; 3185 } 3186 Diag(New->getTemplateLoc(), NextDiag) 3187 << (New->size() > Old->size()) 3188 << (Kind != TPL_TemplateMatch) 3189 << SourceRange(New->getTemplateLoc(), New->getRAngleLoc()); 3190 Diag(Old->getTemplateLoc(), diag::note_template_prev_declaration) 3191 << (Kind != TPL_TemplateMatch) 3192 << SourceRange(Old->getTemplateLoc(), Old->getRAngleLoc()); 3193 } 3194 3195 return false; 3196 } 3197 3198 for (TemplateParameterList::iterator OldParm = Old->begin(), 3199 OldParmEnd = Old->end(), NewParm = New->begin(); 3200 OldParm != OldParmEnd; ++OldParm, ++NewParm) { 3201 if ((*OldParm)->getKind() != (*NewParm)->getKind()) { 3202 if (Complain) { 3203 unsigned NextDiag = diag::err_template_param_different_kind; 3204 if (TemplateArgLoc.isValid()) { 3205 Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch); 3206 NextDiag = diag::note_template_param_different_kind; 3207 } 3208 Diag((*NewParm)->getLocation(), NextDiag) 3209 << (Kind != TPL_TemplateMatch); 3210 Diag((*OldParm)->getLocation(), diag::note_template_prev_declaration) 3211 << (Kind != TPL_TemplateMatch); 3212 } 3213 return false; 3214 } 3215 3216 if (isa<TemplateTypeParmDecl>(*OldParm)) { 3217 // Okay; all template type parameters are equivalent (since we 3218 // know we're at the same index). 3219 } else if (NonTypeTemplateParmDecl *OldNTTP 3220 = dyn_cast<NonTypeTemplateParmDecl>(*OldParm)) { 3221 // The types of non-type template parameters must agree. 3222 NonTypeTemplateParmDecl *NewNTTP 3223 = cast<NonTypeTemplateParmDecl>(*NewParm); 3224 3225 // If we are matching a template template argument to a template 3226 // template parameter and one of the non-type template parameter types 3227 // is dependent, then we must wait until template instantiation time 3228 // to actually compare the arguments. 3229 if (Kind == TPL_TemplateTemplateArgumentMatch && 3230 (OldNTTP->getType()->isDependentType() || 3231 NewNTTP->getType()->isDependentType())) 3232 continue; 3233 3234 if (Context.getCanonicalType(OldNTTP->getType()) != 3235 Context.getCanonicalType(NewNTTP->getType())) { 3236 if (Complain) { 3237 unsigned NextDiag = diag::err_template_nontype_parm_different_type; 3238 if (TemplateArgLoc.isValid()) { 3239 Diag(TemplateArgLoc, 3240 diag::err_template_arg_template_params_mismatch); 3241 NextDiag = diag::note_template_nontype_parm_different_type; 3242 } 3243 Diag(NewNTTP->getLocation(), NextDiag) 3244 << NewNTTP->getType() 3245 << (Kind != TPL_TemplateMatch); 3246 Diag(OldNTTP->getLocation(), 3247 diag::note_template_nontype_parm_prev_declaration) 3248 << OldNTTP->getType(); 3249 } 3250 return false; 3251 } 3252 } else { 3253 // The template parameter lists of template template 3254 // parameters must agree. 3255 assert(isa<TemplateTemplateParmDecl>(*OldParm) && 3256 "Only template template parameters handled here"); 3257 TemplateTemplateParmDecl *OldTTP 3258 = cast<TemplateTemplateParmDecl>(*OldParm); 3259 TemplateTemplateParmDecl *NewTTP 3260 = cast<TemplateTemplateParmDecl>(*NewParm); 3261 if (!TemplateParameterListsAreEqual(NewTTP->getTemplateParameters(), 3262 OldTTP->getTemplateParameters(), 3263 Complain, 3264 (Kind == TPL_TemplateMatch? TPL_TemplateTemplateParmMatch : Kind), 3265 TemplateArgLoc)) 3266 return false; 3267 } 3268 } 3269 3270 return true; 3271} 3272 3273/// \brief Check whether a template can be declared within this scope. 3274/// 3275/// If the template declaration is valid in this scope, returns 3276/// false. Otherwise, issues a diagnostic and returns true. 3277bool 3278Sema::CheckTemplateDeclScope(Scope *S, TemplateParameterList *TemplateParams) { 3279 // Find the nearest enclosing declaration scope. 3280 while ((S->getFlags() & Scope::DeclScope) == 0 || 3281 (S->getFlags() & Scope::TemplateParamScope) != 0) 3282 S = S->getParent(); 3283 3284 // C++ [temp]p2: 3285 // A template-declaration can appear only as a namespace scope or 3286 // class scope declaration. 3287 DeclContext *Ctx = static_cast<DeclContext *>(S->getEntity()); 3288 if (Ctx && isa<LinkageSpecDecl>(Ctx) && 3289 cast<LinkageSpecDecl>(Ctx)->getLanguage() != LinkageSpecDecl::lang_cxx) 3290 return Diag(TemplateParams->getTemplateLoc(), diag::err_template_linkage) 3291 << TemplateParams->getSourceRange(); 3292 3293 while (Ctx && isa<LinkageSpecDecl>(Ctx)) 3294 Ctx = Ctx->getParent(); 3295 3296 if (Ctx && (Ctx->isFileContext() || Ctx->isRecord())) 3297 return false; 3298 3299 return Diag(TemplateParams->getTemplateLoc(), 3300 diag::err_template_outside_namespace_or_class_scope) 3301 << TemplateParams->getSourceRange(); 3302} 3303 3304/// \brief Determine what kind of template specialization the given declaration 3305/// is. 3306static TemplateSpecializationKind getTemplateSpecializationKind(NamedDecl *D) { 3307 if (!D) 3308 return TSK_Undeclared; 3309 3310 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) 3311 return Record->getTemplateSpecializationKind(); 3312 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) 3313 return Function->getTemplateSpecializationKind(); 3314 if (VarDecl *Var = dyn_cast<VarDecl>(D)) 3315 return Var->getTemplateSpecializationKind(); 3316 3317 return TSK_Undeclared; 3318} 3319 3320/// \brief Check whether a specialization is well-formed in the current 3321/// context. 3322/// 3323/// This routine determines whether a template specialization can be declared 3324/// in the current context (C++ [temp.expl.spec]p2). 3325/// 3326/// \param S the semantic analysis object for which this check is being 3327/// performed. 3328/// 3329/// \param Specialized the entity being specialized or instantiated, which 3330/// may be a kind of template (class template, function template, etc.) or 3331/// a member of a class template (member function, static data member, 3332/// member class). 3333/// 3334/// \param PrevDecl the previous declaration of this entity, if any. 3335/// 3336/// \param Loc the location of the explicit specialization or instantiation of 3337/// this entity. 3338/// 3339/// \param IsPartialSpecialization whether this is a partial specialization of 3340/// a class template. 3341/// 3342/// \returns true if there was an error that we cannot recover from, false 3343/// otherwise. 3344static bool CheckTemplateSpecializationScope(Sema &S, 3345 NamedDecl *Specialized, 3346 NamedDecl *PrevDecl, 3347 SourceLocation Loc, 3348 bool IsPartialSpecialization) { 3349 // Keep these "kind" numbers in sync with the %select statements in the 3350 // various diagnostics emitted by this routine. 3351 int EntityKind = 0; 3352 bool isTemplateSpecialization = false; 3353 if (isa<ClassTemplateDecl>(Specialized)) { 3354 EntityKind = IsPartialSpecialization? 1 : 0; 3355 isTemplateSpecialization = true; 3356 } else if (isa<FunctionTemplateDecl>(Specialized)) { 3357 EntityKind = 2; 3358 isTemplateSpecialization = true; 3359 } else if (isa<CXXMethodDecl>(Specialized)) 3360 EntityKind = 3; 3361 else if (isa<VarDecl>(Specialized)) 3362 EntityKind = 4; 3363 else if (isa<RecordDecl>(Specialized)) 3364 EntityKind = 5; 3365 else { 3366 S.Diag(Loc, diag::err_template_spec_unknown_kind); 3367 S.Diag(Specialized->getLocation(), diag::note_specialized_entity); 3368 return true; 3369 } 3370 3371 // C++ [temp.expl.spec]p2: 3372 // An explicit specialization shall be declared in the namespace 3373 // of which the template is a member, or, for member templates, in 3374 // the namespace of which the enclosing class or enclosing class 3375 // template is a member. An explicit specialization of a member 3376 // function, member class or static data member of a class 3377 // template shall be declared in the namespace of which the class 3378 // template is a member. Such a declaration may also be a 3379 // definition. If the declaration is not a definition, the 3380 // specialization may be defined later in the name- space in which 3381 // the explicit specialization was declared, or in a namespace 3382 // that encloses the one in which the explicit specialization was 3383 // declared. 3384 if (S.CurContext->getLookupContext()->isFunctionOrMethod()) { 3385 S.Diag(Loc, diag::err_template_spec_decl_function_scope) 3386 << Specialized; 3387 return true; 3388 } 3389 3390 if (S.CurContext->isRecord() && !IsPartialSpecialization) { 3391 S.Diag(Loc, diag::err_template_spec_decl_class_scope) 3392 << Specialized; 3393 return true; 3394 } 3395 3396 // C++ [temp.class.spec]p6: 3397 // A class template partial specialization may be declared or redeclared 3398 // in any namespace scope in which its definition may be defined (14.5.1 3399 // and 14.5.2). 3400 bool ComplainedAboutScope = false; 3401 DeclContext *SpecializedContext 3402 = Specialized->getDeclContext()->getEnclosingNamespaceContext(); 3403 DeclContext *DC = S.CurContext->getEnclosingNamespaceContext(); 3404 if ((!PrevDecl || 3405 getTemplateSpecializationKind(PrevDecl) == TSK_Undeclared || 3406 getTemplateSpecializationKind(PrevDecl) == TSK_ImplicitInstantiation)){ 3407 // There is no prior declaration of this entity, so this 3408 // specialization must be in the same context as the template 3409 // itself. 3410 if (!DC->Equals(SpecializedContext)) { 3411 if (isa<TranslationUnitDecl>(SpecializedContext)) 3412 S.Diag(Loc, diag::err_template_spec_decl_out_of_scope_global) 3413 << EntityKind << Specialized; 3414 else if (isa<NamespaceDecl>(SpecializedContext)) 3415 S.Diag(Loc, diag::err_template_spec_decl_out_of_scope) 3416 << EntityKind << Specialized 3417 << cast<NamedDecl>(SpecializedContext); 3418 3419 S.Diag(Specialized->getLocation(), diag::note_specialized_entity); 3420 ComplainedAboutScope = true; 3421 } 3422 } 3423 3424 // Make sure that this redeclaration (or definition) occurs in an enclosing 3425 // namespace. 3426 // Note that HandleDeclarator() performs this check for explicit 3427 // specializations of function templates, static data members, and member 3428 // functions, so we skip the check here for those kinds of entities. 3429 // FIXME: HandleDeclarator's diagnostics aren't quite as good, though. 3430 // Should we refactor that check, so that it occurs later? 3431 if (!ComplainedAboutScope && !DC->Encloses(SpecializedContext) && 3432 !(isa<FunctionTemplateDecl>(Specialized) || isa<VarDecl>(Specialized) || 3433 isa<FunctionDecl>(Specialized))) { 3434 if (isa<TranslationUnitDecl>(SpecializedContext)) 3435 S.Diag(Loc, diag::err_template_spec_redecl_global_scope) 3436 << EntityKind << Specialized; 3437 else if (isa<NamespaceDecl>(SpecializedContext)) 3438 S.Diag(Loc, diag::err_template_spec_redecl_out_of_scope) 3439 << EntityKind << Specialized 3440 << cast<NamedDecl>(SpecializedContext); 3441 3442 S.Diag(Specialized->getLocation(), diag::note_specialized_entity); 3443 } 3444 3445 // FIXME: check for specialization-after-instantiation errors and such. 3446 3447 return false; 3448} 3449 3450/// \brief Check the non-type template arguments of a class template 3451/// partial specialization according to C++ [temp.class.spec]p9. 3452/// 3453/// \param TemplateParams the template parameters of the primary class 3454/// template. 3455/// 3456/// \param TemplateArg the template arguments of the class template 3457/// partial specialization. 3458/// 3459/// \param MirrorsPrimaryTemplate will be set true if the class 3460/// template partial specialization arguments are identical to the 3461/// implicit template arguments of the primary template. This is not 3462/// necessarily an error (C++0x), and it is left to the caller to diagnose 3463/// this condition when it is an error. 3464/// 3465/// \returns true if there was an error, false otherwise. 3466bool Sema::CheckClassTemplatePartialSpecializationArgs( 3467 TemplateParameterList *TemplateParams, 3468 const TemplateArgumentListBuilder &TemplateArgs, 3469 bool &MirrorsPrimaryTemplate) { 3470 // FIXME: the interface to this function will have to change to 3471 // accommodate variadic templates. 3472 MirrorsPrimaryTemplate = true; 3473 3474 const TemplateArgument *ArgList = TemplateArgs.getFlatArguments(); 3475 3476 for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) { 3477 // Determine whether the template argument list of the partial 3478 // specialization is identical to the implicit argument list of 3479 // the primary template. The caller may need to diagnostic this as 3480 // an error per C++ [temp.class.spec]p9b3. 3481 if (MirrorsPrimaryTemplate) { 3482 if (TemplateTypeParmDecl *TTP 3483 = dyn_cast<TemplateTypeParmDecl>(TemplateParams->getParam(I))) { 3484 if (Context.getCanonicalType(Context.getTypeDeclType(TTP)) != 3485 Context.getCanonicalType(ArgList[I].getAsType())) 3486 MirrorsPrimaryTemplate = false; 3487 } else if (TemplateTemplateParmDecl *TTP 3488 = dyn_cast<TemplateTemplateParmDecl>( 3489 TemplateParams->getParam(I))) { 3490 TemplateName Name = ArgList[I].getAsTemplate(); 3491 TemplateTemplateParmDecl *ArgDecl 3492 = dyn_cast_or_null<TemplateTemplateParmDecl>(Name.getAsTemplateDecl()); 3493 if (!ArgDecl || 3494 ArgDecl->getIndex() != TTP->getIndex() || 3495 ArgDecl->getDepth() != TTP->getDepth()) 3496 MirrorsPrimaryTemplate = false; 3497 } 3498 } 3499 3500 NonTypeTemplateParmDecl *Param 3501 = dyn_cast<NonTypeTemplateParmDecl>(TemplateParams->getParam(I)); 3502 if (!Param) { 3503 continue; 3504 } 3505 3506 Expr *ArgExpr = ArgList[I].getAsExpr(); 3507 if (!ArgExpr) { 3508 MirrorsPrimaryTemplate = false; 3509 continue; 3510 } 3511 3512 // C++ [temp.class.spec]p8: 3513 // A non-type argument is non-specialized if it is the name of a 3514 // non-type parameter. All other non-type arguments are 3515 // specialized. 3516 // 3517 // Below, we check the two conditions that only apply to 3518 // specialized non-type arguments, so skip any non-specialized 3519 // arguments. 3520 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ArgExpr)) 3521 if (NonTypeTemplateParmDecl *NTTP 3522 = dyn_cast<NonTypeTemplateParmDecl>(DRE->getDecl())) { 3523 if (MirrorsPrimaryTemplate && 3524 (Param->getIndex() != NTTP->getIndex() || 3525 Param->getDepth() != NTTP->getDepth())) 3526 MirrorsPrimaryTemplate = false; 3527 3528 continue; 3529 } 3530 3531 // C++ [temp.class.spec]p9: 3532 // Within the argument list of a class template partial 3533 // specialization, the following restrictions apply: 3534 // -- A partially specialized non-type argument expression 3535 // shall not involve a template parameter of the partial 3536 // specialization except when the argument expression is a 3537 // simple identifier. 3538 if (ArgExpr->isTypeDependent() || ArgExpr->isValueDependent()) { 3539 Diag(ArgExpr->getLocStart(), 3540 diag::err_dependent_non_type_arg_in_partial_spec) 3541 << ArgExpr->getSourceRange(); 3542 return true; 3543 } 3544 3545 // -- The type of a template parameter corresponding to a 3546 // specialized non-type argument shall not be dependent on a 3547 // parameter of the specialization. 3548 if (Param->getType()->isDependentType()) { 3549 Diag(ArgExpr->getLocStart(), 3550 diag::err_dependent_typed_non_type_arg_in_partial_spec) 3551 << Param->getType() 3552 << ArgExpr->getSourceRange(); 3553 Diag(Param->getLocation(), diag::note_template_param_here); 3554 return true; 3555 } 3556 3557 MirrorsPrimaryTemplate = false; 3558 } 3559 3560 return false; 3561} 3562 3563/// \brief Retrieve the previous declaration of the given declaration. 3564static NamedDecl *getPreviousDecl(NamedDecl *ND) { 3565 if (VarDecl *VD = dyn_cast<VarDecl>(ND)) 3566 return VD->getPreviousDeclaration(); 3567 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) 3568 return FD->getPreviousDeclaration(); 3569 if (TagDecl *TD = dyn_cast<TagDecl>(ND)) 3570 return TD->getPreviousDeclaration(); 3571 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(ND)) 3572 return TD->getPreviousDeclaration(); 3573 if (FunctionTemplateDecl *FTD = dyn_cast<FunctionTemplateDecl>(ND)) 3574 return FTD->getPreviousDeclaration(); 3575 if (ClassTemplateDecl *CTD = dyn_cast<ClassTemplateDecl>(ND)) 3576 return CTD->getPreviousDeclaration(); 3577 return 0; 3578} 3579 3580Sema::DeclResult 3581Sema::ActOnClassTemplateSpecialization(Scope *S, unsigned TagSpec, 3582 TagUseKind TUK, 3583 SourceLocation KWLoc, 3584 CXXScopeSpec &SS, 3585 TemplateTy TemplateD, 3586 SourceLocation TemplateNameLoc, 3587 SourceLocation LAngleLoc, 3588 ASTTemplateArgsPtr TemplateArgsIn, 3589 SourceLocation RAngleLoc, 3590 AttributeList *Attr, 3591 MultiTemplateParamsArg TemplateParameterLists) { 3592 assert(TUK != TUK_Reference && "References are not specializations"); 3593 3594 // Find the class template we're specializing 3595 TemplateName Name = TemplateD.getAsVal<TemplateName>(); 3596 ClassTemplateDecl *ClassTemplate 3597 = dyn_cast_or_null<ClassTemplateDecl>(Name.getAsTemplateDecl()); 3598 3599 if (!ClassTemplate) { 3600 Diag(TemplateNameLoc, diag::err_not_class_template_specialization) 3601 << (Name.getAsTemplateDecl() && 3602 isa<TemplateTemplateParmDecl>(Name.getAsTemplateDecl())); 3603 return true; 3604 } 3605 3606 bool isExplicitSpecialization = false; 3607 bool isPartialSpecialization = false; 3608 3609 // Check the validity of the template headers that introduce this 3610 // template. 3611 // FIXME: We probably shouldn't complain about these headers for 3612 // friend declarations. 3613 TemplateParameterList *TemplateParams 3614 = MatchTemplateParametersToScopeSpecifier(TemplateNameLoc, SS, 3615 (TemplateParameterList**)TemplateParameterLists.get(), 3616 TemplateParameterLists.size(), 3617 TUK == TUK_Friend, 3618 isExplicitSpecialization); 3619 if (TemplateParams && TemplateParams->size() > 0) { 3620 isPartialSpecialization = true; 3621 3622 // C++ [temp.class.spec]p10: 3623 // The template parameter list of a specialization shall not 3624 // contain default template argument values. 3625 for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) { 3626 Decl *Param = TemplateParams->getParam(I); 3627 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) { 3628 if (TTP->hasDefaultArgument()) { 3629 Diag(TTP->getDefaultArgumentLoc(), 3630 diag::err_default_arg_in_partial_spec); 3631 TTP->removeDefaultArgument(); 3632 } 3633 } else if (NonTypeTemplateParmDecl *NTTP 3634 = dyn_cast<NonTypeTemplateParmDecl>(Param)) { 3635 if (Expr *DefArg = NTTP->getDefaultArgument()) { 3636 Diag(NTTP->getDefaultArgumentLoc(), 3637 diag::err_default_arg_in_partial_spec) 3638 << DefArg->getSourceRange(); 3639 NTTP->setDefaultArgument(0); 3640 DefArg->Destroy(Context); 3641 } 3642 } else { 3643 TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(Param); 3644 if (TTP->hasDefaultArgument()) { 3645 Diag(TTP->getDefaultArgument().getLocation(), 3646 diag::err_default_arg_in_partial_spec) 3647 << TTP->getDefaultArgument().getSourceRange(); 3648 TTP->setDefaultArgument(TemplateArgumentLoc()); 3649 } 3650 } 3651 } 3652 } else if (TemplateParams) { 3653 if (TUK == TUK_Friend) 3654 Diag(KWLoc, diag::err_template_spec_friend) 3655 << FixItHint::CreateRemoval( 3656 SourceRange(TemplateParams->getTemplateLoc(), 3657 TemplateParams->getRAngleLoc())) 3658 << SourceRange(LAngleLoc, RAngleLoc); 3659 else 3660 isExplicitSpecialization = true; 3661 } else if (TUK != TUK_Friend) { 3662 Diag(KWLoc, diag::err_template_spec_needs_header) 3663 << FixItHint::CreateInsertion(KWLoc, "template<> "); 3664 isExplicitSpecialization = true; 3665 } 3666 3667 // Check that the specialization uses the same tag kind as the 3668 // original template. 3669 TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); 3670 assert(Kind != TTK_Enum && "Invalid enum tag in class template spec!"); 3671 if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(), 3672 Kind, KWLoc, 3673 *ClassTemplate->getIdentifier())) { 3674 Diag(KWLoc, diag::err_use_with_wrong_tag) 3675 << ClassTemplate 3676 << FixItHint::CreateReplacement(KWLoc, 3677 ClassTemplate->getTemplatedDecl()->getKindName()); 3678 Diag(ClassTemplate->getTemplatedDecl()->getLocation(), 3679 diag::note_previous_use); 3680 Kind = ClassTemplate->getTemplatedDecl()->getTagKind(); 3681 } 3682 3683 // Translate the parser's template argument list in our AST format. 3684 TemplateArgumentListInfo TemplateArgs; 3685 TemplateArgs.setLAngleLoc(LAngleLoc); 3686 TemplateArgs.setRAngleLoc(RAngleLoc); 3687 translateTemplateArguments(TemplateArgsIn, TemplateArgs); 3688 3689 // Check that the template argument list is well-formed for this 3690 // template. 3691 TemplateArgumentListBuilder Converted(ClassTemplate->getTemplateParameters(), 3692 TemplateArgs.size()); 3693 if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc, 3694 TemplateArgs, false, Converted)) 3695 return true; 3696 3697 assert((Converted.structuredSize() == 3698 ClassTemplate->getTemplateParameters()->size()) && 3699 "Converted template argument list is too short!"); 3700 3701 // Find the class template (partial) specialization declaration that 3702 // corresponds to these arguments. 3703 llvm::FoldingSetNodeID ID; 3704 if (isPartialSpecialization) { 3705 bool MirrorsPrimaryTemplate; 3706 if (CheckClassTemplatePartialSpecializationArgs( 3707 ClassTemplate->getTemplateParameters(), 3708 Converted, MirrorsPrimaryTemplate)) 3709 return true; 3710 3711 if (MirrorsPrimaryTemplate) { 3712 // C++ [temp.class.spec]p9b3: 3713 // 3714 // -- The argument list of the specialization shall not be identical 3715 // to the implicit argument list of the primary template. 3716 Diag(TemplateNameLoc, diag::err_partial_spec_args_match_primary_template) 3717 << (TUK == TUK_Definition) 3718 << FixItHint::CreateRemoval(SourceRange(LAngleLoc, RAngleLoc)); 3719 return CheckClassTemplate(S, TagSpec, TUK, KWLoc, SS, 3720 ClassTemplate->getIdentifier(), 3721 TemplateNameLoc, 3722 Attr, 3723 TemplateParams, 3724 AS_none); 3725 } 3726 3727 // FIXME: Diagnose friend partial specializations 3728 3729 if (!Name.isDependent() && 3730 !TemplateSpecializationType::anyDependentTemplateArguments( 3731 TemplateArgs.getArgumentArray(), 3732 TemplateArgs.size())) { 3733 Diag(TemplateNameLoc, diag::err_partial_spec_fully_specialized) 3734 << ClassTemplate->getDeclName(); 3735 isPartialSpecialization = false; 3736 } else { 3737 // FIXME: Template parameter list matters, too 3738 ClassTemplatePartialSpecializationDecl::Profile(ID, 3739 Converted.getFlatArguments(), 3740 Converted.flatSize(), 3741 Context); 3742 } 3743 } 3744 3745 if (!isPartialSpecialization) 3746 ClassTemplateSpecializationDecl::Profile(ID, 3747 Converted.getFlatArguments(), 3748 Converted.flatSize(), 3749 Context); 3750 void *InsertPos = 0; 3751 ClassTemplateSpecializationDecl *PrevDecl = 0; 3752 3753 if (isPartialSpecialization) 3754 PrevDecl 3755 = ClassTemplate->getPartialSpecializations().FindNodeOrInsertPos(ID, 3756 InsertPos); 3757 else 3758 PrevDecl 3759 = ClassTemplate->getSpecializations().FindNodeOrInsertPos(ID, InsertPos); 3760 3761 ClassTemplateSpecializationDecl *Specialization = 0; 3762 3763 // Check whether we can declare a class template specialization in 3764 // the current scope. 3765 if (TUK != TUK_Friend && 3766 CheckTemplateSpecializationScope(*this, ClassTemplate, PrevDecl, 3767 TemplateNameLoc, 3768 isPartialSpecialization)) 3769 return true; 3770 3771 // The canonical type 3772 QualType CanonType; 3773 if (PrevDecl && 3774 (PrevDecl->getSpecializationKind() == TSK_Undeclared || 3775 TUK == TUK_Friend)) { 3776 // Since the only prior class template specialization with these 3777 // arguments was referenced but not declared, or we're only 3778 // referencing this specialization as a friend, reuse that 3779 // declaration node as our own, updating its source location to 3780 // reflect our new declaration. 3781 Specialization = PrevDecl; 3782 Specialization->setLocation(TemplateNameLoc); 3783 PrevDecl = 0; 3784 CanonType = Context.getTypeDeclType(Specialization); 3785 } else if (isPartialSpecialization) { 3786 // Build the canonical type that describes the converted template 3787 // arguments of the class template partial specialization. 3788 TemplateName CanonTemplate = Context.getCanonicalTemplateName(Name); 3789 CanonType = Context.getTemplateSpecializationType(CanonTemplate, 3790 Converted.getFlatArguments(), 3791 Converted.flatSize()); 3792 3793 // Create a new class template partial specialization declaration node. 3794 ClassTemplatePartialSpecializationDecl *PrevPartial 3795 = cast_or_null<ClassTemplatePartialSpecializationDecl>(PrevDecl); 3796 unsigned SequenceNumber = PrevPartial? PrevPartial->getSequenceNumber() 3797 : ClassTemplate->getPartialSpecializations().size(); 3798 ClassTemplatePartialSpecializationDecl *Partial 3799 = ClassTemplatePartialSpecializationDecl::Create(Context, Kind, 3800 ClassTemplate->getDeclContext(), 3801 TemplateNameLoc, 3802 TemplateParams, 3803 ClassTemplate, 3804 Converted, 3805 TemplateArgs, 3806 CanonType, 3807 PrevPartial, 3808 SequenceNumber); 3809 SetNestedNameSpecifier(Partial, SS); 3810 3811 if (PrevPartial) { 3812 ClassTemplate->getPartialSpecializations().RemoveNode(PrevPartial); 3813 ClassTemplate->getPartialSpecializations().GetOrInsertNode(Partial); 3814 } else { 3815 ClassTemplate->getPartialSpecializations().InsertNode(Partial, InsertPos); 3816 } 3817 Specialization = Partial; 3818 3819 // If we are providing an explicit specialization of a member class 3820 // template specialization, make a note of that. 3821 if (PrevPartial && PrevPartial->getInstantiatedFromMember()) 3822 PrevPartial->setMemberSpecialization(); 3823 3824 // Check that all of the template parameters of the class template 3825 // partial specialization are deducible from the template 3826 // arguments. If not, this class template partial specialization 3827 // will never be used. 3828 llvm::SmallVector<bool, 8> DeducibleParams; 3829 DeducibleParams.resize(TemplateParams->size()); 3830 MarkUsedTemplateParameters(Partial->getTemplateArgs(), true, 3831 TemplateParams->getDepth(), 3832 DeducibleParams); 3833 unsigned NumNonDeducible = 0; 3834 for (unsigned I = 0, N = DeducibleParams.size(); I != N; ++I) 3835 if (!DeducibleParams[I]) 3836 ++NumNonDeducible; 3837 3838 if (NumNonDeducible) { 3839 Diag(TemplateNameLoc, diag::warn_partial_specs_not_deducible) 3840 << (NumNonDeducible > 1) 3841 << SourceRange(TemplateNameLoc, RAngleLoc); 3842 for (unsigned I = 0, N = DeducibleParams.size(); I != N; ++I) { 3843 if (!DeducibleParams[I]) { 3844 NamedDecl *Param = cast<NamedDecl>(TemplateParams->getParam(I)); 3845 if (Param->getDeclName()) 3846 Diag(Param->getLocation(), 3847 diag::note_partial_spec_unused_parameter) 3848 << Param->getDeclName(); 3849 else 3850 Diag(Param->getLocation(), 3851 diag::note_partial_spec_unused_parameter) 3852 << std::string("<anonymous>"); 3853 } 3854 } 3855 } 3856 } else { 3857 // Create a new class template specialization declaration node for 3858 // this explicit specialization or friend declaration. 3859 Specialization 3860 = ClassTemplateSpecializationDecl::Create(Context, Kind, 3861 ClassTemplate->getDeclContext(), 3862 TemplateNameLoc, 3863 ClassTemplate, 3864 Converted, 3865 PrevDecl); 3866 SetNestedNameSpecifier(Specialization, SS); 3867 3868 if (PrevDecl) { 3869 ClassTemplate->getSpecializations().RemoveNode(PrevDecl); 3870 ClassTemplate->getSpecializations().GetOrInsertNode(Specialization); 3871 } else { 3872 ClassTemplate->getSpecializations().InsertNode(Specialization, 3873 InsertPos); 3874 } 3875 3876 CanonType = Context.getTypeDeclType(Specialization); 3877 } 3878 3879 // C++ [temp.expl.spec]p6: 3880 // If a template, a member template or the member of a class template is 3881 // explicitly specialized then that specialization shall be declared 3882 // before the first use of that specialization that would cause an implicit 3883 // instantiation to take place, in every translation unit in which such a 3884 // use occurs; no diagnostic is required. 3885 if (PrevDecl && PrevDecl->getPointOfInstantiation().isValid()) { 3886 bool Okay = false; 3887 for (NamedDecl *Prev = PrevDecl; Prev; Prev = getPreviousDecl(Prev)) { 3888 // Is there any previous explicit specialization declaration? 3889 if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) { 3890 Okay = true; 3891 break; 3892 } 3893 } 3894 3895 if (!Okay) { 3896 SourceRange Range(TemplateNameLoc, RAngleLoc); 3897 Diag(TemplateNameLoc, diag::err_specialization_after_instantiation) 3898 << Context.getTypeDeclType(Specialization) << Range; 3899 3900 Diag(PrevDecl->getPointOfInstantiation(), 3901 diag::note_instantiation_required_here) 3902 << (PrevDecl->getTemplateSpecializationKind() 3903 != TSK_ImplicitInstantiation); 3904 return true; 3905 } 3906 } 3907 3908 // If this is not a friend, note that this is an explicit specialization. 3909 if (TUK != TUK_Friend) 3910 Specialization->setSpecializationKind(TSK_ExplicitSpecialization); 3911 3912 // Check that this isn't a redefinition of this specialization. 3913 if (TUK == TUK_Definition) { 3914 if (RecordDecl *Def = Specialization->getDefinition()) { 3915 SourceRange Range(TemplateNameLoc, RAngleLoc); 3916 Diag(TemplateNameLoc, diag::err_redefinition) 3917 << Context.getTypeDeclType(Specialization) << Range; 3918 Diag(Def->getLocation(), diag::note_previous_definition); 3919 Specialization->setInvalidDecl(); 3920 return true; 3921 } 3922 } 3923 3924 // Build the fully-sugared type for this class template 3925 // specialization as the user wrote in the specialization 3926 // itself. This means that we'll pretty-print the type retrieved 3927 // from the specialization's declaration the way that the user 3928 // actually wrote the specialization, rather than formatting the 3929 // name based on the "canonical" representation used to store the 3930 // template arguments in the specialization. 3931 TypeSourceInfo *WrittenTy 3932 = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc, 3933 TemplateArgs, CanonType); 3934 if (TUK != TUK_Friend) 3935 Specialization->setTypeAsWritten(WrittenTy); 3936 TemplateArgsIn.release(); 3937 3938 // C++ [temp.expl.spec]p9: 3939 // A template explicit specialization is in the scope of the 3940 // namespace in which the template was defined. 3941 // 3942 // We actually implement this paragraph where we set the semantic 3943 // context (in the creation of the ClassTemplateSpecializationDecl), 3944 // but we also maintain the lexical context where the actual 3945 // definition occurs. 3946 Specialization->setLexicalDeclContext(CurContext); 3947 3948 // We may be starting the definition of this specialization. 3949 if (TUK == TUK_Definition) 3950 Specialization->startDefinition(); 3951 3952 if (TUK == TUK_Friend) { 3953 FriendDecl *Friend = FriendDecl::Create(Context, CurContext, 3954 TemplateNameLoc, 3955 WrittenTy, 3956 /*FIXME:*/KWLoc); 3957 Friend->setAccess(AS_public); 3958 CurContext->addDecl(Friend); 3959 } else { 3960 // Add the specialization into its lexical context, so that it can 3961 // be seen when iterating through the list of declarations in that 3962 // context. However, specializations are not found by name lookup. 3963 CurContext->addDecl(Specialization); 3964 } 3965 return DeclPtrTy::make(Specialization); 3966} 3967 3968Sema::DeclPtrTy 3969Sema::ActOnTemplateDeclarator(Scope *S, 3970 MultiTemplateParamsArg TemplateParameterLists, 3971 Declarator &D) { 3972 return HandleDeclarator(S, D, move(TemplateParameterLists), false); 3973} 3974 3975Sema::DeclPtrTy 3976Sema::ActOnStartOfFunctionTemplateDef(Scope *FnBodyScope, 3977 MultiTemplateParamsArg TemplateParameterLists, 3978 Declarator &D) { 3979 assert(getCurFunctionDecl() == 0 && "Function parsing confused"); 3980 assert(D.getTypeObject(0).Kind == DeclaratorChunk::Function && 3981 "Not a function declarator!"); 3982 DeclaratorChunk::FunctionTypeInfo &FTI = D.getTypeObject(0).Fun; 3983 3984 if (FTI.hasPrototype) { 3985 // FIXME: Diagnose arguments without names in C. 3986 } 3987 3988 Scope *ParentScope = FnBodyScope->getParent(); 3989 3990 DeclPtrTy DP = HandleDeclarator(ParentScope, D, 3991 move(TemplateParameterLists), 3992 /*IsFunctionDefinition=*/true); 3993 if (FunctionTemplateDecl *FunctionTemplate 3994 = dyn_cast_or_null<FunctionTemplateDecl>(DP.getAs<Decl>())) 3995 return ActOnStartOfFunctionDef(FnBodyScope, 3996 DeclPtrTy::make(FunctionTemplate->getTemplatedDecl())); 3997 if (FunctionDecl *Function = dyn_cast_or_null<FunctionDecl>(DP.getAs<Decl>())) 3998 return ActOnStartOfFunctionDef(FnBodyScope, DeclPtrTy::make(Function)); 3999 return DeclPtrTy(); 4000} 4001 4002/// \brief Strips various properties off an implicit instantiation 4003/// that has just been explicitly specialized. 4004static void StripImplicitInstantiation(NamedDecl *D) { 4005 D->invalidateAttrs(); 4006 4007 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { 4008 FD->setInlineSpecified(false); 4009 } 4010} 4011 4012/// \brief Diagnose cases where we have an explicit template specialization 4013/// before/after an explicit template instantiation, producing diagnostics 4014/// for those cases where they are required and determining whether the 4015/// new specialization/instantiation will have any effect. 4016/// 4017/// \param NewLoc the location of the new explicit specialization or 4018/// instantiation. 4019/// 4020/// \param NewTSK the kind of the new explicit specialization or instantiation. 4021/// 4022/// \param PrevDecl the previous declaration of the entity. 4023/// 4024/// \param PrevTSK the kind of the old explicit specialization or instantiatin. 4025/// 4026/// \param PrevPointOfInstantiation if valid, indicates where the previus 4027/// declaration was instantiated (either implicitly or explicitly). 4028/// 4029/// \param SuppressNew will be set to true to indicate that the new 4030/// specialization or instantiation has no effect and should be ignored. 4031/// 4032/// \returns true if there was an error that should prevent the introduction of 4033/// the new declaration into the AST, false otherwise. 4034bool 4035Sema::CheckSpecializationInstantiationRedecl(SourceLocation NewLoc, 4036 TemplateSpecializationKind NewTSK, 4037 NamedDecl *PrevDecl, 4038 TemplateSpecializationKind PrevTSK, 4039 SourceLocation PrevPointOfInstantiation, 4040 bool &SuppressNew) { 4041 SuppressNew = false; 4042 4043 switch (NewTSK) { 4044 case TSK_Undeclared: 4045 case TSK_ImplicitInstantiation: 4046 assert(false && "Don't check implicit instantiations here"); 4047 return false; 4048 4049 case TSK_ExplicitSpecialization: 4050 switch (PrevTSK) { 4051 case TSK_Undeclared: 4052 case TSK_ExplicitSpecialization: 4053 // Okay, we're just specializing something that is either already 4054 // explicitly specialized or has merely been mentioned without any 4055 // instantiation. 4056 return false; 4057 4058 case TSK_ImplicitInstantiation: 4059 if (PrevPointOfInstantiation.isInvalid()) { 4060 // The declaration itself has not actually been instantiated, so it is 4061 // still okay to specialize it. 4062 StripImplicitInstantiation(PrevDecl); 4063 return false; 4064 } 4065 // Fall through 4066 4067 case TSK_ExplicitInstantiationDeclaration: 4068 case TSK_ExplicitInstantiationDefinition: 4069 assert((PrevTSK == TSK_ImplicitInstantiation || 4070 PrevPointOfInstantiation.isValid()) && 4071 "Explicit instantiation without point of instantiation?"); 4072 4073 // C++ [temp.expl.spec]p6: 4074 // If a template, a member template or the member of a class template 4075 // is explicitly specialized then that specialization shall be declared 4076 // before the first use of that specialization that would cause an 4077 // implicit instantiation to take place, in every translation unit in 4078 // which such a use occurs; no diagnostic is required. 4079 for (NamedDecl *Prev = PrevDecl; Prev; Prev = getPreviousDecl(Prev)) { 4080 // Is there any previous explicit specialization declaration? 4081 if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) 4082 return false; 4083 } 4084 4085 Diag(NewLoc, diag::err_specialization_after_instantiation) 4086 << PrevDecl; 4087 Diag(PrevPointOfInstantiation, diag::note_instantiation_required_here) 4088 << (PrevTSK != TSK_ImplicitInstantiation); 4089 4090 return true; 4091 } 4092 break; 4093 4094 case TSK_ExplicitInstantiationDeclaration: 4095 switch (PrevTSK) { 4096 case TSK_ExplicitInstantiationDeclaration: 4097 // This explicit instantiation declaration is redundant (that's okay). 4098 SuppressNew = true; 4099 return false; 4100 4101 case TSK_Undeclared: 4102 case TSK_ImplicitInstantiation: 4103 // We're explicitly instantiating something that may have already been 4104 // implicitly instantiated; that's fine. 4105 return false; 4106 4107 case TSK_ExplicitSpecialization: 4108 // C++0x [temp.explicit]p4: 4109 // For a given set of template parameters, if an explicit instantiation 4110 // of a template appears after a declaration of an explicit 4111 // specialization for that template, the explicit instantiation has no 4112 // effect. 4113 SuppressNew = true; 4114 return false; 4115 4116 case TSK_ExplicitInstantiationDefinition: 4117 // C++0x [temp.explicit]p10: 4118 // If an entity is the subject of both an explicit instantiation 4119 // declaration and an explicit instantiation definition in the same 4120 // translation unit, the definition shall follow the declaration. 4121 Diag(NewLoc, 4122 diag::err_explicit_instantiation_declaration_after_definition); 4123 Diag(PrevPointOfInstantiation, 4124 diag::note_explicit_instantiation_definition_here); 4125 assert(PrevPointOfInstantiation.isValid() && 4126 "Explicit instantiation without point of instantiation?"); 4127 SuppressNew = true; 4128 return false; 4129 } 4130 break; 4131 4132 case TSK_ExplicitInstantiationDefinition: 4133 switch (PrevTSK) { 4134 case TSK_Undeclared: 4135 case TSK_ImplicitInstantiation: 4136 // We're explicitly instantiating something that may have already been 4137 // implicitly instantiated; that's fine. 4138 return false; 4139 4140 case TSK_ExplicitSpecialization: 4141 // C++ DR 259, C++0x [temp.explicit]p4: 4142 // For a given set of template parameters, if an explicit 4143 // instantiation of a template appears after a declaration of 4144 // an explicit specialization for that template, the explicit 4145 // instantiation has no effect. 4146 // 4147 // In C++98/03 mode, we only give an extension warning here, because it 4148 // is not harmful to try to explicitly instantiate something that 4149 // has been explicitly specialized. 4150 if (!getLangOptions().CPlusPlus0x) { 4151 Diag(NewLoc, diag::ext_explicit_instantiation_after_specialization) 4152 << PrevDecl; 4153 Diag(PrevDecl->getLocation(), 4154 diag::note_previous_template_specialization); 4155 } 4156 SuppressNew = true; 4157 return false; 4158 4159 case TSK_ExplicitInstantiationDeclaration: 4160 // We're explicity instantiating a definition for something for which we 4161 // were previously asked to suppress instantiations. That's fine. 4162 return false; 4163 4164 case TSK_ExplicitInstantiationDefinition: 4165 // C++0x [temp.spec]p5: 4166 // For a given template and a given set of template-arguments, 4167 // - an explicit instantiation definition shall appear at most once 4168 // in a program, 4169 Diag(NewLoc, diag::err_explicit_instantiation_duplicate) 4170 << PrevDecl; 4171 Diag(PrevPointOfInstantiation, 4172 diag::note_previous_explicit_instantiation); 4173 SuppressNew = true; 4174 return false; 4175 } 4176 break; 4177 } 4178 4179 assert(false && "Missing specialization/instantiation case?"); 4180 4181 return false; 4182} 4183 4184/// \brief Perform semantic analysis for the given dependent function 4185/// template specialization. The only possible way to get a dependent 4186/// function template specialization is with a friend declaration, 4187/// like so: 4188/// 4189/// template <class T> void foo(T); 4190/// template <class T> class A { 4191/// friend void foo<>(T); 4192/// }; 4193/// 4194/// There really isn't any useful analysis we can do here, so we 4195/// just store the information. 4196bool 4197Sema::CheckDependentFunctionTemplateSpecialization(FunctionDecl *FD, 4198 const TemplateArgumentListInfo &ExplicitTemplateArgs, 4199 LookupResult &Previous) { 4200 // Remove anything from Previous that isn't a function template in 4201 // the correct context. 4202 DeclContext *FDLookupContext = FD->getDeclContext()->getLookupContext(); 4203 LookupResult::Filter F = Previous.makeFilter(); 4204 while (F.hasNext()) { 4205 NamedDecl *D = F.next()->getUnderlyingDecl(); 4206 if (!isa<FunctionTemplateDecl>(D) || 4207 !FDLookupContext->Equals(D->getDeclContext()->getLookupContext())) 4208 F.erase(); 4209 } 4210 F.done(); 4211 4212 // Should this be diagnosed here? 4213 if (Previous.empty()) return true; 4214 4215 FD->setDependentTemplateSpecialization(Context, Previous.asUnresolvedSet(), 4216 ExplicitTemplateArgs); 4217 return false; 4218} 4219 4220/// \brief Perform semantic analysis for the given function template 4221/// specialization. 4222/// 4223/// This routine performs all of the semantic analysis required for an 4224/// explicit function template specialization. On successful completion, 4225/// the function declaration \p FD will become a function template 4226/// specialization. 4227/// 4228/// \param FD the function declaration, which will be updated to become a 4229/// function template specialization. 4230/// 4231/// \param HasExplicitTemplateArgs whether any template arguments were 4232/// explicitly provided. 4233/// 4234/// \param LAngleLoc the location of the left angle bracket ('<'), if 4235/// template arguments were explicitly provided. 4236/// 4237/// \param ExplicitTemplateArgs the explicitly-provided template arguments, 4238/// if any. 4239/// 4240/// \param NumExplicitTemplateArgs the number of explicitly-provided template 4241/// arguments. This number may be zero even when HasExplicitTemplateArgs is 4242/// true as in, e.g., \c void sort<>(char*, char*); 4243/// 4244/// \param RAngleLoc the location of the right angle bracket ('>'), if 4245/// template arguments were explicitly provided. 4246/// 4247/// \param PrevDecl the set of declarations that 4248bool 4249Sema::CheckFunctionTemplateSpecialization(FunctionDecl *FD, 4250 const TemplateArgumentListInfo *ExplicitTemplateArgs, 4251 LookupResult &Previous) { 4252 // The set of function template specializations that could match this 4253 // explicit function template specialization. 4254 UnresolvedSet<8> Candidates; 4255 4256 DeclContext *FDLookupContext = FD->getDeclContext()->getLookupContext(); 4257 for (LookupResult::iterator I = Previous.begin(), E = Previous.end(); 4258 I != E; ++I) { 4259 NamedDecl *Ovl = (*I)->getUnderlyingDecl(); 4260 if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Ovl)) { 4261 // Only consider templates found within the same semantic lookup scope as 4262 // FD. 4263 if (!FDLookupContext->Equals(Ovl->getDeclContext()->getLookupContext())) 4264 continue; 4265 4266 // C++ [temp.expl.spec]p11: 4267 // A trailing template-argument can be left unspecified in the 4268 // template-id naming an explicit function template specialization 4269 // provided it can be deduced from the function argument type. 4270 // Perform template argument deduction to determine whether we may be 4271 // specializing this template. 4272 // FIXME: It is somewhat wasteful to build 4273 TemplateDeductionInfo Info(Context, FD->getLocation()); 4274 FunctionDecl *Specialization = 0; 4275 if (TemplateDeductionResult TDK 4276 = DeduceTemplateArguments(FunTmpl, ExplicitTemplateArgs, 4277 FD->getType(), 4278 Specialization, 4279 Info)) { 4280 // FIXME: Template argument deduction failed; record why it failed, so 4281 // that we can provide nifty diagnostics. 4282 (void)TDK; 4283 continue; 4284 } 4285 4286 // Record this candidate. 4287 Candidates.addDecl(Specialization, I.getAccess()); 4288 } 4289 } 4290 4291 // Find the most specialized function template. 4292 UnresolvedSetIterator Result 4293 = getMostSpecialized(Candidates.begin(), Candidates.end(), 4294 TPOC_Other, FD->getLocation(), 4295 PDiag(diag::err_function_template_spec_no_match) 4296 << FD->getDeclName(), 4297 PDiag(diag::err_function_template_spec_ambiguous) 4298 << FD->getDeclName() << (ExplicitTemplateArgs != 0), 4299 PDiag(diag::note_function_template_spec_matched)); 4300 if (Result == Candidates.end()) 4301 return true; 4302 4303 // Ignore access information; it doesn't figure into redeclaration checking. 4304 FunctionDecl *Specialization = cast<FunctionDecl>(*Result); 4305 Specialization->setLocation(FD->getLocation()); 4306 4307 // FIXME: Check if the prior specialization has a point of instantiation. 4308 // If so, we have run afoul of . 4309 4310 // If this is a friend declaration, then we're not really declaring 4311 // an explicit specialization. 4312 bool isFriend = (FD->getFriendObjectKind() != Decl::FOK_None); 4313 4314 // Check the scope of this explicit specialization. 4315 if (!isFriend && 4316 CheckTemplateSpecializationScope(*this, 4317 Specialization->getPrimaryTemplate(), 4318 Specialization, FD->getLocation(), 4319 false)) 4320 return true; 4321 4322 // C++ [temp.expl.spec]p6: 4323 // If a template, a member template or the member of a class template is 4324 // explicitly specialized then that specialization shall be declared 4325 // before the first use of that specialization that would cause an implicit 4326 // instantiation to take place, in every translation unit in which such a 4327 // use occurs; no diagnostic is required. 4328 FunctionTemplateSpecializationInfo *SpecInfo 4329 = Specialization->getTemplateSpecializationInfo(); 4330 assert(SpecInfo && "Function template specialization info missing?"); 4331 4332 bool SuppressNew = false; 4333 if (!isFriend && 4334 CheckSpecializationInstantiationRedecl(FD->getLocation(), 4335 TSK_ExplicitSpecialization, 4336 Specialization, 4337 SpecInfo->getTemplateSpecializationKind(), 4338 SpecInfo->getPointOfInstantiation(), 4339 SuppressNew)) 4340 return true; 4341 4342 // Mark the prior declaration as an explicit specialization, so that later 4343 // clients know that this is an explicit specialization. 4344 if (!isFriend) 4345 SpecInfo->setTemplateSpecializationKind(TSK_ExplicitSpecialization); 4346 4347 // Turn the given function declaration into a function template 4348 // specialization, with the template arguments from the previous 4349 // specialization. 4350 FD->setFunctionTemplateSpecialization(Specialization->getPrimaryTemplate(), 4351 new (Context) TemplateArgumentList( 4352 *Specialization->getTemplateSpecializationArgs()), 4353 /*InsertPos=*/0, 4354 SpecInfo->getTemplateSpecializationKind()); 4355 4356 // The "previous declaration" for this function template specialization is 4357 // the prior function template specialization. 4358 Previous.clear(); 4359 Previous.addDecl(Specialization); 4360 return false; 4361} 4362 4363/// \brief Perform semantic analysis for the given non-template member 4364/// specialization. 4365/// 4366/// This routine performs all of the semantic analysis required for an 4367/// explicit member function specialization. On successful completion, 4368/// the function declaration \p FD will become a member function 4369/// specialization. 4370/// 4371/// \param Member the member declaration, which will be updated to become a 4372/// specialization. 4373/// 4374/// \param Previous the set of declarations, one of which may be specialized 4375/// by this function specialization; the set will be modified to contain the 4376/// redeclared member. 4377bool 4378Sema::CheckMemberSpecialization(NamedDecl *Member, LookupResult &Previous) { 4379 assert(!isa<TemplateDecl>(Member) && "Only for non-template members"); 4380 4381 // Try to find the member we are instantiating. 4382 NamedDecl *Instantiation = 0; 4383 NamedDecl *InstantiatedFrom = 0; 4384 MemberSpecializationInfo *MSInfo = 0; 4385 4386 if (Previous.empty()) { 4387 // Nowhere to look anyway. 4388 } else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Member)) { 4389 for (LookupResult::iterator I = Previous.begin(), E = Previous.end(); 4390 I != E; ++I) { 4391 NamedDecl *D = (*I)->getUnderlyingDecl(); 4392 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) { 4393 if (Context.hasSameType(Function->getType(), Method->getType())) { 4394 Instantiation = Method; 4395 InstantiatedFrom = Method->getInstantiatedFromMemberFunction(); 4396 MSInfo = Method->getMemberSpecializationInfo(); 4397 break; 4398 } 4399 } 4400 } 4401 } else if (isa<VarDecl>(Member)) { 4402 VarDecl *PrevVar; 4403 if (Previous.isSingleResult() && 4404 (PrevVar = dyn_cast<VarDecl>(Previous.getFoundDecl()))) 4405 if (PrevVar->isStaticDataMember()) { 4406 Instantiation = PrevVar; 4407 InstantiatedFrom = PrevVar->getInstantiatedFromStaticDataMember(); 4408 MSInfo = PrevVar->getMemberSpecializationInfo(); 4409 } 4410 } else if (isa<RecordDecl>(Member)) { 4411 CXXRecordDecl *PrevRecord; 4412 if (Previous.isSingleResult() && 4413 (PrevRecord = dyn_cast<CXXRecordDecl>(Previous.getFoundDecl()))) { 4414 Instantiation = PrevRecord; 4415 InstantiatedFrom = PrevRecord->getInstantiatedFromMemberClass(); 4416 MSInfo = PrevRecord->getMemberSpecializationInfo(); 4417 } 4418 } 4419 4420 if (!Instantiation) { 4421 // There is no previous declaration that matches. Since member 4422 // specializations are always out-of-line, the caller will complain about 4423 // this mismatch later. 4424 return false; 4425 } 4426 4427 // If this is a friend, just bail out here before we start turning 4428 // things into explicit specializations. 4429 if (Member->getFriendObjectKind() != Decl::FOK_None) { 4430 // Preserve instantiation information. 4431 if (InstantiatedFrom && isa<CXXMethodDecl>(Member)) { 4432 cast<CXXMethodDecl>(Member)->setInstantiationOfMemberFunction( 4433 cast<CXXMethodDecl>(InstantiatedFrom), 4434 cast<CXXMethodDecl>(Instantiation)->getTemplateSpecializationKind()); 4435 } else if (InstantiatedFrom && isa<CXXRecordDecl>(Member)) { 4436 cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass( 4437 cast<CXXRecordDecl>(InstantiatedFrom), 4438 cast<CXXRecordDecl>(Instantiation)->getTemplateSpecializationKind()); 4439 } 4440 4441 Previous.clear(); 4442 Previous.addDecl(Instantiation); 4443 return false; 4444 } 4445 4446 // Make sure that this is a specialization of a member. 4447 if (!InstantiatedFrom) { 4448 Diag(Member->getLocation(), diag::err_spec_member_not_instantiated) 4449 << Member; 4450 Diag(Instantiation->getLocation(), diag::note_specialized_decl); 4451 return true; 4452 } 4453 4454 // C++ [temp.expl.spec]p6: 4455 // If a template, a member template or the member of a class template is 4456 // explicitly specialized then that spe- cialization shall be declared 4457 // before the first use of that specialization that would cause an implicit 4458 // instantiation to take place, in every translation unit in which such a 4459 // use occurs; no diagnostic is required. 4460 assert(MSInfo && "Member specialization info missing?"); 4461 4462 bool SuppressNew = false; 4463 if (CheckSpecializationInstantiationRedecl(Member->getLocation(), 4464 TSK_ExplicitSpecialization, 4465 Instantiation, 4466 MSInfo->getTemplateSpecializationKind(), 4467 MSInfo->getPointOfInstantiation(), 4468 SuppressNew)) 4469 return true; 4470 4471 // Check the scope of this explicit specialization. 4472 if (CheckTemplateSpecializationScope(*this, 4473 InstantiatedFrom, 4474 Instantiation, Member->getLocation(), 4475 false)) 4476 return true; 4477 4478 // Note that this is an explicit instantiation of a member. 4479 // the original declaration to note that it is an explicit specialization 4480 // (if it was previously an implicit instantiation). This latter step 4481 // makes bookkeeping easier. 4482 if (isa<FunctionDecl>(Member)) { 4483 FunctionDecl *InstantiationFunction = cast<FunctionDecl>(Instantiation); 4484 if (InstantiationFunction->getTemplateSpecializationKind() == 4485 TSK_ImplicitInstantiation) { 4486 InstantiationFunction->setTemplateSpecializationKind( 4487 TSK_ExplicitSpecialization); 4488 InstantiationFunction->setLocation(Member->getLocation()); 4489 } 4490 4491 cast<FunctionDecl>(Member)->setInstantiationOfMemberFunction( 4492 cast<CXXMethodDecl>(InstantiatedFrom), 4493 TSK_ExplicitSpecialization); 4494 } else if (isa<VarDecl>(Member)) { 4495 VarDecl *InstantiationVar = cast<VarDecl>(Instantiation); 4496 if (InstantiationVar->getTemplateSpecializationKind() == 4497 TSK_ImplicitInstantiation) { 4498 InstantiationVar->setTemplateSpecializationKind( 4499 TSK_ExplicitSpecialization); 4500 InstantiationVar->setLocation(Member->getLocation()); 4501 } 4502 4503 Context.setInstantiatedFromStaticDataMember(cast<VarDecl>(Member), 4504 cast<VarDecl>(InstantiatedFrom), 4505 TSK_ExplicitSpecialization); 4506 } else { 4507 assert(isa<CXXRecordDecl>(Member) && "Only member classes remain"); 4508 CXXRecordDecl *InstantiationClass = cast<CXXRecordDecl>(Instantiation); 4509 if (InstantiationClass->getTemplateSpecializationKind() == 4510 TSK_ImplicitInstantiation) { 4511 InstantiationClass->setTemplateSpecializationKind( 4512 TSK_ExplicitSpecialization); 4513 InstantiationClass->setLocation(Member->getLocation()); 4514 } 4515 4516 cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass( 4517 cast<CXXRecordDecl>(InstantiatedFrom), 4518 TSK_ExplicitSpecialization); 4519 } 4520 4521 // Save the caller the trouble of having to figure out which declaration 4522 // this specialization matches. 4523 Previous.clear(); 4524 Previous.addDecl(Instantiation); 4525 return false; 4526} 4527 4528/// \brief Check the scope of an explicit instantiation. 4529static void CheckExplicitInstantiationScope(Sema &S, NamedDecl *D, 4530 SourceLocation InstLoc, 4531 bool WasQualifiedName) { 4532 DeclContext *ExpectedContext 4533 = D->getDeclContext()->getEnclosingNamespaceContext()->getLookupContext(); 4534 DeclContext *CurContext = S.CurContext->getLookupContext(); 4535 4536 // C++0x [temp.explicit]p2: 4537 // An explicit instantiation shall appear in an enclosing namespace of its 4538 // template. 4539 // 4540 // This is DR275, which we do not retroactively apply to C++98/03. 4541 if (S.getLangOptions().CPlusPlus0x && 4542 !CurContext->Encloses(ExpectedContext)) { 4543 if (NamespaceDecl *NS = dyn_cast<NamespaceDecl>(ExpectedContext)) 4544 S.Diag(InstLoc, 4545 S.getLangOptions().CPlusPlus0x? 4546 diag::err_explicit_instantiation_out_of_scope 4547 : diag::warn_explicit_instantiation_out_of_scope_0x) 4548 << D << NS; 4549 else 4550 S.Diag(InstLoc, 4551 S.getLangOptions().CPlusPlus0x? 4552 diag::err_explicit_instantiation_must_be_global 4553 : diag::warn_explicit_instantiation_out_of_scope_0x) 4554 << D; 4555 S.Diag(D->getLocation(), diag::note_explicit_instantiation_here); 4556 return; 4557 } 4558 4559 // C++0x [temp.explicit]p2: 4560 // If the name declared in the explicit instantiation is an unqualified 4561 // name, the explicit instantiation shall appear in the namespace where 4562 // its template is declared or, if that namespace is inline (7.3.1), any 4563 // namespace from its enclosing namespace set. 4564 if (WasQualifiedName) 4565 return; 4566 4567 if (CurContext->Equals(ExpectedContext)) 4568 return; 4569 4570 S.Diag(InstLoc, 4571 S.getLangOptions().CPlusPlus0x? 4572 diag::err_explicit_instantiation_unqualified_wrong_namespace 4573 : diag::warn_explicit_instantiation_unqualified_wrong_namespace_0x) 4574 << D << ExpectedContext; 4575 S.Diag(D->getLocation(), diag::note_explicit_instantiation_here); 4576} 4577 4578/// \brief Determine whether the given scope specifier has a template-id in it. 4579static bool ScopeSpecifierHasTemplateId(const CXXScopeSpec &SS) { 4580 if (!SS.isSet()) 4581 return false; 4582 4583 // C++0x [temp.explicit]p2: 4584 // If the explicit instantiation is for a member function, a member class 4585 // or a static data member of a class template specialization, the name of 4586 // the class template specialization in the qualified-id for the member 4587 // name shall be a simple-template-id. 4588 // 4589 // C++98 has the same restriction, just worded differently. 4590 for (NestedNameSpecifier *NNS = (NestedNameSpecifier *)SS.getScopeRep(); 4591 NNS; NNS = NNS->getPrefix()) 4592 if (Type *T = NNS->getAsType()) 4593 if (isa<TemplateSpecializationType>(T)) 4594 return true; 4595 4596 return false; 4597} 4598 4599// Explicit instantiation of a class template specialization 4600Sema::DeclResult 4601Sema::ActOnExplicitInstantiation(Scope *S, 4602 SourceLocation ExternLoc, 4603 SourceLocation TemplateLoc, 4604 unsigned TagSpec, 4605 SourceLocation KWLoc, 4606 const CXXScopeSpec &SS, 4607 TemplateTy TemplateD, 4608 SourceLocation TemplateNameLoc, 4609 SourceLocation LAngleLoc, 4610 ASTTemplateArgsPtr TemplateArgsIn, 4611 SourceLocation RAngleLoc, 4612 AttributeList *Attr) { 4613 // Find the class template we're specializing 4614 TemplateName Name = TemplateD.getAsVal<TemplateName>(); 4615 ClassTemplateDecl *ClassTemplate 4616 = cast<ClassTemplateDecl>(Name.getAsTemplateDecl()); 4617 4618 // Check that the specialization uses the same tag kind as the 4619 // original template. 4620 TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); 4621 assert(Kind != TTK_Enum && 4622 "Invalid enum tag in class template explicit instantiation!"); 4623 if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(), 4624 Kind, KWLoc, 4625 *ClassTemplate->getIdentifier())) { 4626 Diag(KWLoc, diag::err_use_with_wrong_tag) 4627 << ClassTemplate 4628 << FixItHint::CreateReplacement(KWLoc, 4629 ClassTemplate->getTemplatedDecl()->getKindName()); 4630 Diag(ClassTemplate->getTemplatedDecl()->getLocation(), 4631 diag::note_previous_use); 4632 Kind = ClassTemplate->getTemplatedDecl()->getTagKind(); 4633 } 4634 4635 // C++0x [temp.explicit]p2: 4636 // There are two forms of explicit instantiation: an explicit instantiation 4637 // definition and an explicit instantiation declaration. An explicit 4638 // instantiation declaration begins with the extern keyword. [...] 4639 TemplateSpecializationKind TSK 4640 = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition 4641 : TSK_ExplicitInstantiationDeclaration; 4642 4643 // Translate the parser's template argument list in our AST format. 4644 TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); 4645 translateTemplateArguments(TemplateArgsIn, TemplateArgs); 4646 4647 // Check that the template argument list is well-formed for this 4648 // template. 4649 TemplateArgumentListBuilder Converted(ClassTemplate->getTemplateParameters(), 4650 TemplateArgs.size()); 4651 if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc, 4652 TemplateArgs, false, Converted)) 4653 return true; 4654 4655 assert((Converted.structuredSize() == 4656 ClassTemplate->getTemplateParameters()->size()) && 4657 "Converted template argument list is too short!"); 4658 4659 // Find the class template specialization declaration that 4660 // corresponds to these arguments. 4661 llvm::FoldingSetNodeID ID; 4662 ClassTemplateSpecializationDecl::Profile(ID, 4663 Converted.getFlatArguments(), 4664 Converted.flatSize(), 4665 Context); 4666 void *InsertPos = 0; 4667 ClassTemplateSpecializationDecl *PrevDecl 4668 = ClassTemplate->getSpecializations().FindNodeOrInsertPos(ID, InsertPos); 4669 4670 // C++0x [temp.explicit]p2: 4671 // [...] An explicit instantiation shall appear in an enclosing 4672 // namespace of its template. [...] 4673 // 4674 // This is C++ DR 275. 4675 CheckExplicitInstantiationScope(*this, ClassTemplate, TemplateNameLoc, 4676 SS.isSet()); 4677 4678 ClassTemplateSpecializationDecl *Specialization = 0; 4679 4680 bool ReusedDecl = false; 4681 if (PrevDecl) { 4682 bool SuppressNew = false; 4683 if (CheckSpecializationInstantiationRedecl(TemplateNameLoc, TSK, 4684 PrevDecl, 4685 PrevDecl->getSpecializationKind(), 4686 PrevDecl->getPointOfInstantiation(), 4687 SuppressNew)) 4688 return DeclPtrTy::make(PrevDecl); 4689 4690 if (SuppressNew) 4691 return DeclPtrTy::make(PrevDecl); 4692 4693 if (PrevDecl->getSpecializationKind() == TSK_ImplicitInstantiation || 4694 PrevDecl->getSpecializationKind() == TSK_Undeclared) { 4695 // Since the only prior class template specialization with these 4696 // arguments was referenced but not declared, reuse that 4697 // declaration node as our own, updating its source location to 4698 // reflect our new declaration. 4699 Specialization = PrevDecl; 4700 Specialization->setLocation(TemplateNameLoc); 4701 PrevDecl = 0; 4702 ReusedDecl = true; 4703 } 4704 } 4705 4706 if (!Specialization) { 4707 // Create a new class template specialization declaration node for 4708 // this explicit specialization. 4709 Specialization 4710 = ClassTemplateSpecializationDecl::Create(Context, Kind, 4711 ClassTemplate->getDeclContext(), 4712 TemplateNameLoc, 4713 ClassTemplate, 4714 Converted, PrevDecl); 4715 SetNestedNameSpecifier(Specialization, SS); 4716 4717 if (PrevDecl) { 4718 // Remove the previous declaration from the folding set, since we want 4719 // to introduce a new declaration. 4720 ClassTemplate->getSpecializations().RemoveNode(PrevDecl); 4721 ClassTemplate->getSpecializations().FindNodeOrInsertPos(ID, InsertPos); 4722 } 4723 4724 // Insert the new specialization. 4725 ClassTemplate->getSpecializations().InsertNode(Specialization, InsertPos); 4726 } 4727 4728 // Build the fully-sugared type for this explicit instantiation as 4729 // the user wrote in the explicit instantiation itself. This means 4730 // that we'll pretty-print the type retrieved from the 4731 // specialization's declaration the way that the user actually wrote 4732 // the explicit instantiation, rather than formatting the name based 4733 // on the "canonical" representation used to store the template 4734 // arguments in the specialization. 4735 TypeSourceInfo *WrittenTy 4736 = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc, 4737 TemplateArgs, 4738 Context.getTypeDeclType(Specialization)); 4739 Specialization->setTypeAsWritten(WrittenTy); 4740 TemplateArgsIn.release(); 4741 4742 if (!ReusedDecl) { 4743 // Add the explicit instantiation into its lexical context. However, 4744 // since explicit instantiations are never found by name lookup, we 4745 // just put it into the declaration context directly. 4746 Specialization->setLexicalDeclContext(CurContext); 4747 CurContext->addDecl(Specialization); 4748 } 4749 4750 // C++ [temp.explicit]p3: 4751 // A definition of a class template or class member template 4752 // shall be in scope at the point of the explicit instantiation of 4753 // the class template or class member template. 4754 // 4755 // This check comes when we actually try to perform the 4756 // instantiation. 4757 ClassTemplateSpecializationDecl *Def 4758 = cast_or_null<ClassTemplateSpecializationDecl>( 4759 Specialization->getDefinition()); 4760 if (!Def) 4761 InstantiateClassTemplateSpecialization(TemplateNameLoc, Specialization, TSK); 4762 else if (TSK == TSK_ExplicitInstantiationDefinition) 4763 MarkVTableUsed(TemplateNameLoc, Specialization, true); 4764 4765 // Instantiate the members of this class template specialization. 4766 Def = cast_or_null<ClassTemplateSpecializationDecl>( 4767 Specialization->getDefinition()); 4768 if (Def) { 4769 TemplateSpecializationKind Old_TSK = Def->getTemplateSpecializationKind(); 4770 4771 // Fix a TSK_ExplicitInstantiationDeclaration followed by a 4772 // TSK_ExplicitInstantiationDefinition 4773 if (Old_TSK == TSK_ExplicitInstantiationDeclaration && 4774 TSK == TSK_ExplicitInstantiationDefinition) 4775 Def->setTemplateSpecializationKind(TSK); 4776 4777 InstantiateClassTemplateSpecializationMembers(TemplateNameLoc, Def, TSK); 4778 } 4779 4780 return DeclPtrTy::make(Specialization); 4781} 4782 4783// Explicit instantiation of a member class of a class template. 4784Sema::DeclResult 4785Sema::ActOnExplicitInstantiation(Scope *S, 4786 SourceLocation ExternLoc, 4787 SourceLocation TemplateLoc, 4788 unsigned TagSpec, 4789 SourceLocation KWLoc, 4790 CXXScopeSpec &SS, 4791 IdentifierInfo *Name, 4792 SourceLocation NameLoc, 4793 AttributeList *Attr) { 4794 4795 bool Owned = false; 4796 bool IsDependent = false; 4797 DeclPtrTy TagD = ActOnTag(S, TagSpec, Action::TUK_Reference, 4798 KWLoc, SS, Name, NameLoc, Attr, AS_none, 4799 MultiTemplateParamsArg(*this, 0, 0), 4800 Owned, IsDependent); 4801 assert(!IsDependent && "explicit instantiation of dependent name not yet handled"); 4802 4803 if (!TagD) 4804 return true; 4805 4806 TagDecl *Tag = cast<TagDecl>(TagD.getAs<Decl>()); 4807 if (Tag->isEnum()) { 4808 Diag(TemplateLoc, diag::err_explicit_instantiation_enum) 4809 << Context.getTypeDeclType(Tag); 4810 return true; 4811 } 4812 4813 if (Tag->isInvalidDecl()) 4814 return true; 4815 4816 CXXRecordDecl *Record = cast<CXXRecordDecl>(Tag); 4817 CXXRecordDecl *Pattern = Record->getInstantiatedFromMemberClass(); 4818 if (!Pattern) { 4819 Diag(TemplateLoc, diag::err_explicit_instantiation_nontemplate_type) 4820 << Context.getTypeDeclType(Record); 4821 Diag(Record->getLocation(), diag::note_nontemplate_decl_here); 4822 return true; 4823 } 4824 4825 // C++0x [temp.explicit]p2: 4826 // If the explicit instantiation is for a class or member class, the 4827 // elaborated-type-specifier in the declaration shall include a 4828 // simple-template-id. 4829 // 4830 // C++98 has the same restriction, just worded differently. 4831 if (!ScopeSpecifierHasTemplateId(SS)) 4832 Diag(TemplateLoc, diag::err_explicit_instantiation_without_qualified_id) 4833 << Record << SS.getRange(); 4834 4835 // C++0x [temp.explicit]p2: 4836 // There are two forms of explicit instantiation: an explicit instantiation 4837 // definition and an explicit instantiation declaration. An explicit 4838 // instantiation declaration begins with the extern keyword. [...] 4839 TemplateSpecializationKind TSK 4840 = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition 4841 : TSK_ExplicitInstantiationDeclaration; 4842 4843 // C++0x [temp.explicit]p2: 4844 // [...] An explicit instantiation shall appear in an enclosing 4845 // namespace of its template. [...] 4846 // 4847 // This is C++ DR 275. 4848 CheckExplicitInstantiationScope(*this, Record, NameLoc, true); 4849 4850 // Verify that it is okay to explicitly instantiate here. 4851 CXXRecordDecl *PrevDecl 4852 = cast_or_null<CXXRecordDecl>(Record->getPreviousDeclaration()); 4853 if (!PrevDecl && Record->getDefinition()) 4854 PrevDecl = Record; 4855 if (PrevDecl) { 4856 MemberSpecializationInfo *MSInfo = PrevDecl->getMemberSpecializationInfo(); 4857 bool SuppressNew = false; 4858 assert(MSInfo && "No member specialization information?"); 4859 if (CheckSpecializationInstantiationRedecl(TemplateLoc, TSK, 4860 PrevDecl, 4861 MSInfo->getTemplateSpecializationKind(), 4862 MSInfo->getPointOfInstantiation(), 4863 SuppressNew)) 4864 return true; 4865 if (SuppressNew) 4866 return TagD; 4867 } 4868 4869 CXXRecordDecl *RecordDef 4870 = cast_or_null<CXXRecordDecl>(Record->getDefinition()); 4871 if (!RecordDef) { 4872 // C++ [temp.explicit]p3: 4873 // A definition of a member class of a class template shall be in scope 4874 // at the point of an explicit instantiation of the member class. 4875 CXXRecordDecl *Def 4876 = cast_or_null<CXXRecordDecl>(Pattern->getDefinition()); 4877 if (!Def) { 4878 Diag(TemplateLoc, diag::err_explicit_instantiation_undefined_member) 4879 << 0 << Record->getDeclName() << Record->getDeclContext(); 4880 Diag(Pattern->getLocation(), diag::note_forward_declaration) 4881 << Pattern; 4882 return true; 4883 } else { 4884 if (InstantiateClass(NameLoc, Record, Def, 4885 getTemplateInstantiationArgs(Record), 4886 TSK)) 4887 return true; 4888 4889 RecordDef = cast_or_null<CXXRecordDecl>(Record->getDefinition()); 4890 if (!RecordDef) 4891 return true; 4892 } 4893 } 4894 4895 // Instantiate all of the members of the class. 4896 InstantiateClassMembers(NameLoc, RecordDef, 4897 getTemplateInstantiationArgs(Record), TSK); 4898 4899 if (TSK == TSK_ExplicitInstantiationDefinition) 4900 MarkVTableUsed(NameLoc, RecordDef, true); 4901 4902 // FIXME: We don't have any representation for explicit instantiations of 4903 // member classes. Such a representation is not needed for compilation, but it 4904 // should be available for clients that want to see all of the declarations in 4905 // the source code. 4906 return TagD; 4907} 4908 4909Sema::DeclResult Sema::ActOnExplicitInstantiation(Scope *S, 4910 SourceLocation ExternLoc, 4911 SourceLocation TemplateLoc, 4912 Declarator &D) { 4913 // Explicit instantiations always require a name. 4914 DeclarationName Name = GetNameForDeclarator(D); 4915 if (!Name) { 4916 if (!D.isInvalidType()) 4917 Diag(D.getDeclSpec().getSourceRange().getBegin(), 4918 diag::err_explicit_instantiation_requires_name) 4919 << D.getDeclSpec().getSourceRange() 4920 << D.getSourceRange(); 4921 4922 return true; 4923 } 4924 4925 // The scope passed in may not be a decl scope. Zip up the scope tree until 4926 // we find one that is. 4927 while ((S->getFlags() & Scope::DeclScope) == 0 || 4928 (S->getFlags() & Scope::TemplateParamScope) != 0) 4929 S = S->getParent(); 4930 4931 // Determine the type of the declaration. 4932 QualType R = GetTypeForDeclarator(D, S, 0); 4933 if (R.isNull()) 4934 return true; 4935 4936 if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef) { 4937 // Cannot explicitly instantiate a typedef. 4938 Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_of_typedef) 4939 << Name; 4940 return true; 4941 } 4942 4943 // C++0x [temp.explicit]p1: 4944 // [...] An explicit instantiation of a function template shall not use the 4945 // inline or constexpr specifiers. 4946 // Presumably, this also applies to member functions of class templates as 4947 // well. 4948 if (D.getDeclSpec().isInlineSpecified() && getLangOptions().CPlusPlus0x) 4949 Diag(D.getDeclSpec().getInlineSpecLoc(), 4950 diag::err_explicit_instantiation_inline) 4951 <<FixItHint::CreateRemoval(D.getDeclSpec().getInlineSpecLoc()); 4952 4953 // FIXME: check for constexpr specifier. 4954 4955 // C++0x [temp.explicit]p2: 4956 // There are two forms of explicit instantiation: an explicit instantiation 4957 // definition and an explicit instantiation declaration. An explicit 4958 // instantiation declaration begins with the extern keyword. [...] 4959 TemplateSpecializationKind TSK 4960 = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition 4961 : TSK_ExplicitInstantiationDeclaration; 4962 4963 LookupResult Previous(*this, Name, D.getIdentifierLoc(), LookupOrdinaryName); 4964 LookupParsedName(Previous, S, &D.getCXXScopeSpec()); 4965 4966 if (!R->isFunctionType()) { 4967 // C++ [temp.explicit]p1: 4968 // A [...] static data member of a class template can be explicitly 4969 // instantiated from the member definition associated with its class 4970 // template. 4971 if (Previous.isAmbiguous()) 4972 return true; 4973 4974 VarDecl *Prev = Previous.getAsSingle<VarDecl>(); 4975 if (!Prev || !Prev->isStaticDataMember()) { 4976 // We expect to see a data data member here. 4977 Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_not_known) 4978 << Name; 4979 for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end(); 4980 P != PEnd; ++P) 4981 Diag((*P)->getLocation(), diag::note_explicit_instantiation_here); 4982 return true; 4983 } 4984 4985 if (!Prev->getInstantiatedFromStaticDataMember()) { 4986 // FIXME: Check for explicit specialization? 4987 Diag(D.getIdentifierLoc(), 4988 diag::err_explicit_instantiation_data_member_not_instantiated) 4989 << Prev; 4990 Diag(Prev->getLocation(), diag::note_explicit_instantiation_here); 4991 // FIXME: Can we provide a note showing where this was declared? 4992 return true; 4993 } 4994 4995 // C++0x [temp.explicit]p2: 4996 // If the explicit instantiation is for a member function, a member class 4997 // or a static data member of a class template specialization, the name of 4998 // the class template specialization in the qualified-id for the member 4999 // name shall be a simple-template-id. 5000 // 5001 // C++98 has the same restriction, just worded differently. 5002 if (!ScopeSpecifierHasTemplateId(D.getCXXScopeSpec())) 5003 Diag(D.getIdentifierLoc(), 5004 diag::err_explicit_instantiation_without_qualified_id) 5005 << Prev << D.getCXXScopeSpec().getRange(); 5006 5007 // Check the scope of this explicit instantiation. 5008 CheckExplicitInstantiationScope(*this, Prev, D.getIdentifierLoc(), true); 5009 5010 // Verify that it is okay to explicitly instantiate here. 5011 MemberSpecializationInfo *MSInfo = Prev->getMemberSpecializationInfo(); 5012 assert(MSInfo && "Missing static data member specialization info?"); 5013 bool SuppressNew = false; 5014 if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK, Prev, 5015 MSInfo->getTemplateSpecializationKind(), 5016 MSInfo->getPointOfInstantiation(), 5017 SuppressNew)) 5018 return true; 5019 if (SuppressNew) 5020 return DeclPtrTy(); 5021 5022 // Instantiate static data member. 5023 Prev->setTemplateSpecializationKind(TSK, D.getIdentifierLoc()); 5024 if (TSK == TSK_ExplicitInstantiationDefinition) 5025 InstantiateStaticDataMemberDefinition(D.getIdentifierLoc(), Prev, false, 5026 /*DefinitionRequired=*/true); 5027 5028 // FIXME: Create an ExplicitInstantiation node? 5029 return DeclPtrTy(); 5030 } 5031 5032 // If the declarator is a template-id, translate the parser's template 5033 // argument list into our AST format. 5034 bool HasExplicitTemplateArgs = false; 5035 TemplateArgumentListInfo TemplateArgs; 5036 if (D.getName().getKind() == UnqualifiedId::IK_TemplateId) { 5037 TemplateIdAnnotation *TemplateId = D.getName().TemplateId; 5038 TemplateArgs.setLAngleLoc(TemplateId->LAngleLoc); 5039 TemplateArgs.setRAngleLoc(TemplateId->RAngleLoc); 5040 ASTTemplateArgsPtr TemplateArgsPtr(*this, 5041 TemplateId->getTemplateArgs(), 5042 TemplateId->NumArgs); 5043 translateTemplateArguments(TemplateArgsPtr, TemplateArgs); 5044 HasExplicitTemplateArgs = true; 5045 TemplateArgsPtr.release(); 5046 } 5047 5048 // C++ [temp.explicit]p1: 5049 // A [...] function [...] can be explicitly instantiated from its template. 5050 // A member function [...] of a class template can be explicitly 5051 // instantiated from the member definition associated with its class 5052 // template. 5053 UnresolvedSet<8> Matches; 5054 for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end(); 5055 P != PEnd; ++P) { 5056 NamedDecl *Prev = *P; 5057 if (!HasExplicitTemplateArgs) { 5058 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Prev)) { 5059 if (Context.hasSameUnqualifiedType(Method->getType(), R)) { 5060 Matches.clear(); 5061 5062 Matches.addDecl(Method, P.getAccess()); 5063 if (Method->getTemplateSpecializationKind() == TSK_Undeclared) 5064 break; 5065 } 5066 } 5067 } 5068 5069 FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Prev); 5070 if (!FunTmpl) 5071 continue; 5072 5073 TemplateDeductionInfo Info(Context, D.getIdentifierLoc()); 5074 FunctionDecl *Specialization = 0; 5075 if (TemplateDeductionResult TDK 5076 = DeduceTemplateArguments(FunTmpl, 5077 (HasExplicitTemplateArgs ? &TemplateArgs : 0), 5078 R, Specialization, Info)) { 5079 // FIXME: Keep track of almost-matches? 5080 (void)TDK; 5081 continue; 5082 } 5083 5084 Matches.addDecl(Specialization, P.getAccess()); 5085 } 5086 5087 // Find the most specialized function template specialization. 5088 UnresolvedSetIterator Result 5089 = getMostSpecialized(Matches.begin(), Matches.end(), TPOC_Other, 5090 D.getIdentifierLoc(), 5091 PDiag(diag::err_explicit_instantiation_not_known) << Name, 5092 PDiag(diag::err_explicit_instantiation_ambiguous) << Name, 5093 PDiag(diag::note_explicit_instantiation_candidate)); 5094 5095 if (Result == Matches.end()) 5096 return true; 5097 5098 // Ignore access control bits, we don't need them for redeclaration checking. 5099 FunctionDecl *Specialization = cast<FunctionDecl>(*Result); 5100 5101 if (Specialization->getTemplateSpecializationKind() == TSK_Undeclared) { 5102 Diag(D.getIdentifierLoc(), 5103 diag::err_explicit_instantiation_member_function_not_instantiated) 5104 << Specialization 5105 << (Specialization->getTemplateSpecializationKind() == 5106 TSK_ExplicitSpecialization); 5107 Diag(Specialization->getLocation(), diag::note_explicit_instantiation_here); 5108 return true; 5109 } 5110 5111 FunctionDecl *PrevDecl = Specialization->getPreviousDeclaration(); 5112 if (!PrevDecl && Specialization->isThisDeclarationADefinition()) 5113 PrevDecl = Specialization; 5114 5115 if (PrevDecl) { 5116 bool SuppressNew = false; 5117 if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK, 5118 PrevDecl, 5119 PrevDecl->getTemplateSpecializationKind(), 5120 PrevDecl->getPointOfInstantiation(), 5121 SuppressNew)) 5122 return true; 5123 5124 // FIXME: We may still want to build some representation of this 5125 // explicit specialization. 5126 if (SuppressNew) 5127 return DeclPtrTy(); 5128 } 5129 5130 Specialization->setTemplateSpecializationKind(TSK, D.getIdentifierLoc()); 5131 5132 if (TSK == TSK_ExplicitInstantiationDefinition) 5133 InstantiateFunctionDefinition(D.getIdentifierLoc(), Specialization, 5134 false, /*DefinitionRequired=*/true); 5135 5136 // C++0x [temp.explicit]p2: 5137 // If the explicit instantiation is for a member function, a member class 5138 // or a static data member of a class template specialization, the name of 5139 // the class template specialization in the qualified-id for the member 5140 // name shall be a simple-template-id. 5141 // 5142 // C++98 has the same restriction, just worded differently. 5143 FunctionTemplateDecl *FunTmpl = Specialization->getPrimaryTemplate(); 5144 if (D.getName().getKind() != UnqualifiedId::IK_TemplateId && !FunTmpl && 5145 D.getCXXScopeSpec().isSet() && 5146 !ScopeSpecifierHasTemplateId(D.getCXXScopeSpec())) 5147 Diag(D.getIdentifierLoc(), 5148 diag::err_explicit_instantiation_without_qualified_id) 5149 << Specialization << D.getCXXScopeSpec().getRange(); 5150 5151 CheckExplicitInstantiationScope(*this, 5152 FunTmpl? (NamedDecl *)FunTmpl 5153 : Specialization->getInstantiatedFromMemberFunction(), 5154 D.getIdentifierLoc(), 5155 D.getCXXScopeSpec().isSet()); 5156 5157 // FIXME: Create some kind of ExplicitInstantiationDecl here. 5158 return DeclPtrTy(); 5159} 5160 5161Sema::TypeResult 5162Sema::ActOnDependentTag(Scope *S, unsigned TagSpec, TagUseKind TUK, 5163 const CXXScopeSpec &SS, IdentifierInfo *Name, 5164 SourceLocation TagLoc, SourceLocation NameLoc) { 5165 // This has to hold, because SS is expected to be defined. 5166 assert(Name && "Expected a name in a dependent tag"); 5167 5168 NestedNameSpecifier *NNS 5169 = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); 5170 if (!NNS) 5171 return true; 5172 5173 TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); 5174 5175 if (TUK == TUK_Declaration || TUK == TUK_Definition) { 5176 Diag(NameLoc, diag::err_dependent_tag_decl) 5177 << (TUK == TUK_Definition) << Kind << SS.getRange(); 5178 return true; 5179 } 5180 5181 ElaboratedTypeKeyword Kwd = TypeWithKeyword::getKeywordForTagTypeKind(Kind); 5182 return Context.getDependentNameType(Kwd, NNS, Name).getAsOpaquePtr(); 5183} 5184 5185static void FillTypeLoc(DependentNameTypeLoc TL, 5186 SourceLocation TypenameLoc, 5187 SourceRange QualifierRange) { 5188 // FIXME: typename, qualifier range 5189 TL.setNameLoc(TypenameLoc); 5190} 5191 5192static void FillTypeLoc(ElaboratedTypeLoc TL, 5193 SourceLocation TypenameLoc, 5194 SourceRange QualifierRange) { 5195 // FIXME: typename, qualifier range 5196 TL.setNameLoc(TypenameLoc); 5197} 5198 5199Sema::TypeResult 5200Sema::ActOnTypenameType(SourceLocation TypenameLoc, const CXXScopeSpec &SS, 5201 const IdentifierInfo &II, SourceLocation IdLoc) { 5202 NestedNameSpecifier *NNS 5203 = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); 5204 if (!NNS) 5205 return true; 5206 5207 QualType T = CheckTypenameType(ETK_Typename, NNS, II, 5208 SourceRange(TypenameLoc, IdLoc)); 5209 if (T.isNull()) 5210 return true; 5211 5212 TypeSourceInfo *TSI = Context.CreateTypeSourceInfo(T); 5213 if (isa<DependentNameType>(T)) { 5214 DependentNameTypeLoc TL = cast<DependentNameTypeLoc>(TSI->getTypeLoc()); 5215 // FIXME: fill inner type loc 5216 FillTypeLoc(TL, TypenameLoc, SS.getRange()); 5217 } else { 5218 ElaboratedTypeLoc TL = cast<ElaboratedTypeLoc>(TSI->getTypeLoc()); 5219 // FIXME: fill inner type loc 5220 FillTypeLoc(TL, TypenameLoc, SS.getRange()); 5221 } 5222 5223 return CreateLocInfoType(T, TSI).getAsOpaquePtr(); 5224} 5225 5226Sema::TypeResult 5227Sema::ActOnTypenameType(SourceLocation TypenameLoc, const CXXScopeSpec &SS, 5228 SourceLocation TemplateLoc, TypeTy *Ty) { 5229 QualType T = GetTypeFromParser(Ty); 5230 NestedNameSpecifier *NNS 5231 = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); 5232 const TemplateSpecializationType *TemplateId 5233 = T->getAs<TemplateSpecializationType>(); 5234 assert(TemplateId && "Expected a template specialization type"); 5235 5236 if (computeDeclContext(SS, false)) { 5237 // If we can compute a declaration context, then the "typename" 5238 // keyword was superfluous. Just build an ElaboratedType to keep 5239 // track of the nested-name-specifier. 5240 T = Context.getElaboratedType(ETK_Typename, NNS, T); 5241 TypeSourceInfo *TSI = Context.CreateTypeSourceInfo(T); 5242 ElaboratedTypeLoc TL = cast<ElaboratedTypeLoc>(TSI->getTypeLoc()); 5243 // FIXME: fill inner type loc 5244 FillTypeLoc(TL, TypenameLoc, SS.getRange()); 5245 return CreateLocInfoType(T, TSI).getAsOpaquePtr(); 5246 } 5247 5248 T = Context.getDependentNameType(ETK_Typename, NNS, TemplateId); 5249 TypeSourceInfo *TSI = Context.CreateTypeSourceInfo(T); 5250 DependentNameTypeLoc TL = cast<DependentNameTypeLoc>(TSI->getTypeLoc()); 5251 // FIXME: fill inner type loc 5252 FillTypeLoc(TL, TypenameLoc, SS.getRange()); 5253 return CreateLocInfoType(T, TSI).getAsOpaquePtr(); 5254} 5255 5256/// \brief Build the type that describes a C++ typename specifier, 5257/// e.g., "typename T::type". 5258QualType 5259Sema::CheckTypenameType(ElaboratedTypeKeyword Keyword, 5260 NestedNameSpecifier *NNS, const IdentifierInfo &II, 5261 SourceRange Range) { 5262 CXXScopeSpec SS; 5263 SS.setScopeRep(NNS); 5264 SS.setRange(Range); 5265 5266 DeclContext *Ctx = computeDeclContext(SS); 5267 if (!Ctx) { 5268 // If the nested-name-specifier is dependent and couldn't be 5269 // resolved to a type, build a typename type. 5270 assert(NNS->isDependent()); 5271 return Context.getDependentNameType(Keyword, NNS, &II); 5272 } 5273 5274 // If the nested-name-specifier refers to the current instantiation, 5275 // the "typename" keyword itself is superfluous. In C++03, the 5276 // program is actually ill-formed. However, DR 382 (in C++0x CD1) 5277 // allows such extraneous "typename" keywords, and we retroactively 5278 // apply this DR to C++03 code. In any case we continue. 5279 5280 if (RequireCompleteDeclContext(SS, Ctx)) 5281 return QualType(); 5282 5283 DeclarationName Name(&II); 5284 LookupResult Result(*this, Name, Range.getEnd(), LookupOrdinaryName); 5285 LookupQualifiedName(Result, Ctx); 5286 unsigned DiagID = 0; 5287 Decl *Referenced = 0; 5288 switch (Result.getResultKind()) { 5289 case LookupResult::NotFound: 5290 DiagID = diag::err_typename_nested_not_found; 5291 break; 5292 5293 case LookupResult::NotFoundInCurrentInstantiation: 5294 // Okay, it's a member of an unknown instantiation. 5295 return Context.getDependentNameType(Keyword, NNS, &II); 5296 5297 case LookupResult::Found: 5298 if (TypeDecl *Type = dyn_cast<TypeDecl>(Result.getFoundDecl())) { 5299 // We found a type. Build an ElaboratedType, since the 5300 // typename-specifier was just sugar. 5301 return Context.getElaboratedType(ETK_Typename, NNS, 5302 Context.getTypeDeclType(Type)); 5303 } 5304 5305 DiagID = diag::err_typename_nested_not_type; 5306 Referenced = Result.getFoundDecl(); 5307 break; 5308 5309 case LookupResult::FoundUnresolvedValue: 5310 llvm_unreachable("unresolved using decl in non-dependent context"); 5311 return QualType(); 5312 5313 case LookupResult::FoundOverloaded: 5314 DiagID = diag::err_typename_nested_not_type; 5315 Referenced = *Result.begin(); 5316 break; 5317 5318 case LookupResult::Ambiguous: 5319 return QualType(); 5320 } 5321 5322 // If we get here, it's because name lookup did not find a 5323 // type. Emit an appropriate diagnostic and return an error. 5324 Diag(Range.getEnd(), DiagID) << Range << Name << Ctx; 5325 if (Referenced) 5326 Diag(Referenced->getLocation(), diag::note_typename_refers_here) 5327 << Name; 5328 return QualType(); 5329} 5330 5331namespace { 5332 // See Sema::RebuildTypeInCurrentInstantiation 5333 class CurrentInstantiationRebuilder 5334 : public TreeTransform<CurrentInstantiationRebuilder> { 5335 SourceLocation Loc; 5336 DeclarationName Entity; 5337 5338 public: 5339 typedef TreeTransform<CurrentInstantiationRebuilder> inherited; 5340 5341 CurrentInstantiationRebuilder(Sema &SemaRef, 5342 SourceLocation Loc, 5343 DeclarationName Entity) 5344 : TreeTransform<CurrentInstantiationRebuilder>(SemaRef), 5345 Loc(Loc), Entity(Entity) { } 5346 5347 /// \brief Determine whether the given type \p T has already been 5348 /// transformed. 5349 /// 5350 /// For the purposes of type reconstruction, a type has already been 5351 /// transformed if it is NULL or if it is not dependent. 5352 bool AlreadyTransformed(QualType T) { 5353 return T.isNull() || !T->isDependentType(); 5354 } 5355 5356 /// \brief Returns the location of the entity whose type is being 5357 /// rebuilt. 5358 SourceLocation getBaseLocation() { return Loc; } 5359 5360 /// \brief Returns the name of the entity whose type is being rebuilt. 5361 DeclarationName getBaseEntity() { return Entity; } 5362 5363 /// \brief Sets the "base" location and entity when that 5364 /// information is known based on another transformation. 5365 void setBase(SourceLocation Loc, DeclarationName Entity) { 5366 this->Loc = Loc; 5367 this->Entity = Entity; 5368 } 5369 5370 /// \brief Transforms an expression by returning the expression itself 5371 /// (an identity function). 5372 /// 5373 /// FIXME: This is completely unsafe; we will need to actually clone the 5374 /// expressions. 5375 Sema::OwningExprResult TransformExpr(Expr *E) { 5376 return getSema().Owned(E->Retain()); 5377 } 5378 5379 /// \brief Transforms a typename type by determining whether the type now 5380 /// refers to a member of the current instantiation, and then 5381 /// type-checking and building an ElaboratedType (when possible). 5382 QualType TransformDependentNameType(TypeLocBuilder &TLB, 5383 DependentNameTypeLoc TL, 5384 QualType ObjectType); 5385 }; 5386} 5387 5388QualType 5389CurrentInstantiationRebuilder::TransformDependentNameType(TypeLocBuilder &TLB, 5390 DependentNameTypeLoc TL, 5391 QualType ObjectType) { 5392 DependentNameType *T = TL.getTypePtr(); 5393 5394 NestedNameSpecifier *NNS 5395 = TransformNestedNameSpecifier(T->getQualifier(), 5396 /*FIXME:*/SourceRange(getBaseLocation()), 5397 ObjectType); 5398 if (!NNS) 5399 return QualType(); 5400 5401 // If the nested-name-specifier did not change, and we cannot compute the 5402 // context corresponding to the nested-name-specifier, then this 5403 // typename type will not change; exit early. 5404 CXXScopeSpec SS; 5405 SS.setRange(SourceRange(getBaseLocation())); 5406 SS.setScopeRep(NNS); 5407 5408 QualType Result; 5409 if (NNS == T->getQualifier() && getSema().computeDeclContext(SS) == 0) 5410 Result = QualType(T, 0); 5411 5412 // Rebuild the typename type, which will probably turn into a 5413 // ElaboratedType. 5414 else if (const TemplateSpecializationType *TemplateId = T->getTemplateId()) { 5415 QualType NewTemplateId 5416 = TransformType(QualType(TemplateId, 0)); 5417 if (NewTemplateId.isNull()) 5418 return QualType(); 5419 5420 if (NNS == T->getQualifier() && 5421 NewTemplateId == QualType(TemplateId, 0)) 5422 Result = QualType(T, 0); 5423 else 5424 Result = getDerived().RebuildDependentNameType(T->getKeyword(), 5425 NNS, NewTemplateId); 5426 } else 5427 Result = getDerived().RebuildDependentNameType(T->getKeyword(), 5428 NNS, T->getIdentifier(), 5429 SourceRange(TL.getNameLoc())); 5430 5431 if (Result.isNull()) 5432 return QualType(); 5433 5434 DependentNameTypeLoc NewTL = TLB.push<DependentNameTypeLoc>(Result); 5435 NewTL.setNameLoc(TL.getNameLoc()); 5436 return Result; 5437} 5438 5439/// \brief Rebuilds a type within the context of the current instantiation. 5440/// 5441/// The type \p T is part of the type of an out-of-line member definition of 5442/// a class template (or class template partial specialization) that was parsed 5443/// and constructed before we entered the scope of the class template (or 5444/// partial specialization thereof). This routine will rebuild that type now 5445/// that we have entered the declarator's scope, which may produce different 5446/// canonical types, e.g., 5447/// 5448/// \code 5449/// template<typename T> 5450/// struct X { 5451/// typedef T* pointer; 5452/// pointer data(); 5453/// }; 5454/// 5455/// template<typename T> 5456/// typename X<T>::pointer X<T>::data() { ... } 5457/// \endcode 5458/// 5459/// Here, the type "typename X<T>::pointer" will be created as a DependentNameType, 5460/// since we do not know that we can look into X<T> when we parsed the type. 5461/// This function will rebuild the type, performing the lookup of "pointer" 5462/// in X<T> and returning an ElaboratedType whose canonical type is the same 5463/// as the canonical type of T*, allowing the return types of the out-of-line 5464/// definition and the declaration to match. 5465TypeSourceInfo *Sema::RebuildTypeInCurrentInstantiation(TypeSourceInfo *T, 5466 SourceLocation Loc, 5467 DeclarationName Name) { 5468 if (!T || !T->getType()->isDependentType()) 5469 return T; 5470 5471 CurrentInstantiationRebuilder Rebuilder(*this, Loc, Name); 5472 return Rebuilder.TransformType(T); 5473} 5474 5475bool Sema::RebuildNestedNameSpecifierInCurrentInstantiation(CXXScopeSpec &SS) { 5476 if (SS.isInvalid()) return true; 5477 5478 NestedNameSpecifier *NNS = static_cast<NestedNameSpecifier*>(SS.getScopeRep()); 5479 CurrentInstantiationRebuilder Rebuilder(*this, SS.getRange().getBegin(), 5480 DeclarationName()); 5481 NestedNameSpecifier *Rebuilt = 5482 Rebuilder.TransformNestedNameSpecifier(NNS, SS.getRange()); 5483 if (!Rebuilt) return true; 5484 5485 SS.setScopeRep(Rebuilt); 5486 return false; 5487} 5488 5489/// \brief Produces a formatted string that describes the binding of 5490/// template parameters to template arguments. 5491std::string 5492Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params, 5493 const TemplateArgumentList &Args) { 5494 // FIXME: For variadic templates, we'll need to get the structured list. 5495 return getTemplateArgumentBindingsText(Params, Args.getFlatArgumentList(), 5496 Args.flat_size()); 5497} 5498 5499std::string 5500Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params, 5501 const TemplateArgument *Args, 5502 unsigned NumArgs) { 5503 std::string Result; 5504 5505 if (!Params || Params->size() == 0 || NumArgs == 0) 5506 return Result; 5507 5508 for (unsigned I = 0, N = Params->size(); I != N; ++I) { 5509 if (I >= NumArgs) 5510 break; 5511 5512 if (I == 0) 5513 Result += "[with "; 5514 else 5515 Result += ", "; 5516 5517 if (const IdentifierInfo *Id = Params->getParam(I)->getIdentifier()) { 5518 Result += Id->getName(); 5519 } else { 5520 Result += '$'; 5521 Result += llvm::utostr(I); 5522 } 5523 5524 Result += " = "; 5525 5526 switch (Args[I].getKind()) { 5527 case TemplateArgument::Null: 5528 Result += "<no value>"; 5529 break; 5530 5531 case TemplateArgument::Type: { 5532 std::string TypeStr; 5533 Args[I].getAsType().getAsStringInternal(TypeStr, 5534 Context.PrintingPolicy); 5535 Result += TypeStr; 5536 break; 5537 } 5538 5539 case TemplateArgument::Declaration: { 5540 bool Unnamed = true; 5541 if (NamedDecl *ND = dyn_cast_or_null<NamedDecl>(Args[I].getAsDecl())) { 5542 if (ND->getDeclName()) { 5543 Unnamed = false; 5544 Result += ND->getNameAsString(); 5545 } 5546 } 5547 5548 if (Unnamed) { 5549 Result += "<anonymous>"; 5550 } 5551 break; 5552 } 5553 5554 case TemplateArgument::Template: { 5555 std::string Str; 5556 llvm::raw_string_ostream OS(Str); 5557 Args[I].getAsTemplate().print(OS, Context.PrintingPolicy); 5558 Result += OS.str(); 5559 break; 5560 } 5561 5562 case TemplateArgument::Integral: { 5563 Result += Args[I].getAsIntegral()->toString(10); 5564 break; 5565 } 5566 5567 case TemplateArgument::Expression: { 5568 // FIXME: This is non-optimal, since we're regurgitating the 5569 // expression we were given. 5570 std::string Str; 5571 { 5572 llvm::raw_string_ostream OS(Str); 5573 Args[I].getAsExpr()->printPretty(OS, Context, 0, 5574 Context.PrintingPolicy); 5575 } 5576 Result += Str; 5577 break; 5578 } 5579 5580 case TemplateArgument::Pack: 5581 // FIXME: Format template argument packs 5582 Result += "<template argument pack>"; 5583 break; 5584 } 5585 } 5586 5587 Result += ']'; 5588 return Result; 5589} 5590