Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 1 | //===------- SemaTemplate.cpp - Semantic Analysis for C++ Templates -------===/ |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 7 | //===----------------------------------------------------------------------===/ |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 8 | // |
| 9 | // This file implements semantic analysis for C++ templates. |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 10 | //===----------------------------------------------------------------------===/ |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 11 | |
John McCall | 2d88708 | 2010-08-25 22:03:47 +0000 | [diff] [blame] | 12 | #include "clang/Sema/SemaInternal.h" |
Douglas Gregor | e737f50 | 2010-08-12 20:07:10 +0000 | [diff] [blame] | 13 | #include "clang/Sema/Lookup.h" |
John McCall | 5f1e094 | 2010-08-24 08:50:51 +0000 | [diff] [blame] | 14 | #include "clang/Sema/Scope.h" |
John McCall | 7cd088e | 2010-08-24 07:21:54 +0000 | [diff] [blame] | 15 | #include "clang/Sema/Template.h" |
John McCall | 2a7fb27 | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 16 | #include "clang/Sema/TemplateDeduction.h" |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 17 | #include "TreeTransform.h" |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 18 | #include "clang/AST/ASTContext.h" |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 19 | #include "clang/AST/Expr.h" |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 20 | #include "clang/AST/ExprCXX.h" |
John McCall | 92b7f70 | 2010-03-11 07:50:04 +0000 | [diff] [blame] | 21 | #include "clang/AST/DeclFriend.h" |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 22 | #include "clang/AST/DeclTemplate.h" |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 23 | #include "clang/AST/RecursiveASTVisitor.h" |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 24 | #include "clang/AST/TypeVisitor.h" |
John McCall | 1951085 | 2010-08-20 18:27:03 +0000 | [diff] [blame] | 25 | #include "clang/Sema/DeclSpec.h" |
| 26 | #include "clang/Sema/ParsedTemplate.h" |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 27 | #include "clang/Basic/LangOptions.h" |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 28 | #include "clang/Basic/PartialDiagnostic.h" |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 29 | #include "llvm/ADT/StringExtras.h" |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 30 | using namespace clang; |
John McCall | 2a7fb27 | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 31 | using namespace sema; |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 32 | |
John McCall | 78b8105 | 2010-11-10 02:40:36 +0000 | [diff] [blame] | 33 | // Exported for use by Parser. |
| 34 | SourceRange |
| 35 | clang::getTemplateParamsRange(TemplateParameterList const * const *Ps, |
| 36 | unsigned N) { |
| 37 | if (!N) return SourceRange(); |
| 38 | return SourceRange(Ps[0]->getTemplateLoc(), Ps[N-1]->getRAngleLoc()); |
| 39 | } |
| 40 | |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 41 | /// \brief Determine whether the declaration found is acceptable as the name |
| 42 | /// of a template and, if so, return that template declaration. Otherwise, |
| 43 | /// returns NULL. |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 44 | static NamedDecl *isAcceptableTemplateName(ASTContext &Context, |
| 45 | NamedDecl *Orig) { |
| 46 | NamedDecl *D = Orig->getUnderlyingDecl(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 47 | |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 48 | if (isa<TemplateDecl>(D)) |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 49 | return Orig; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 50 | |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 51 | if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) { |
| 52 | // C++ [temp.local]p1: |
| 53 | // Like normal (non-template) classes, class templates have an |
| 54 | // injected-class-name (Clause 9). The injected-class-name |
| 55 | // can be used with or without a template-argument-list. When |
| 56 | // it is used without a template-argument-list, it is |
| 57 | // equivalent to the injected-class-name followed by the |
| 58 | // template-parameters of the class template enclosed in |
| 59 | // <>. When it is used with a template-argument-list, it |
| 60 | // refers to the specified class template specialization, |
| 61 | // which could be the current specialization or another |
| 62 | // specialization. |
| 63 | if (Record->isInjectedClassName()) { |
Douglas Gregor | 542b548 | 2009-10-14 17:30:58 +0000 | [diff] [blame] | 64 | Record = cast<CXXRecordDecl>(Record->getDeclContext()); |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 65 | if (Record->getDescribedClassTemplate()) |
| 66 | return Record->getDescribedClassTemplate(); |
| 67 | |
| 68 | if (ClassTemplateSpecializationDecl *Spec |
| 69 | = dyn_cast<ClassTemplateSpecializationDecl>(Record)) |
| 70 | return Spec->getSpecializedTemplate(); |
| 71 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 72 | |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 73 | return 0; |
| 74 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 75 | |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 76 | return 0; |
| 77 | } |
| 78 | |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 79 | void Sema::FilterAcceptableTemplateNames(LookupResult &R) { |
Douglas Gregor | 01e56ae | 2010-04-12 20:54:26 +0000 | [diff] [blame] | 80 | // The set of class templates we've already seen. |
| 81 | llvm::SmallPtrSet<ClassTemplateDecl *, 8> ClassTemplates; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 82 | LookupResult::Filter filter = R.makeFilter(); |
| 83 | while (filter.hasNext()) { |
| 84 | NamedDecl *Orig = filter.next(); |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 85 | NamedDecl *Repl = isAcceptableTemplateName(Context, Orig); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 86 | if (!Repl) |
| 87 | filter.erase(); |
Douglas Gregor | 01e56ae | 2010-04-12 20:54:26 +0000 | [diff] [blame] | 88 | else if (Repl != Orig) { |
| 89 | |
| 90 | // C++ [temp.local]p3: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 91 | // A lookup that finds an injected-class-name (10.2) can result in an |
Douglas Gregor | 01e56ae | 2010-04-12 20:54:26 +0000 | [diff] [blame] | 92 | // ambiguity in certain cases (for example, if it is found in more than |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 93 | // one base class). If all of the injected-class-names that are found |
| 94 | // refer to specializations of the same class template, and if the name |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 95 | // is used as a template-name, the reference refers to the class |
| 96 | // template itself and not a specialization thereof, and is not |
Douglas Gregor | 01e56ae | 2010-04-12 20:54:26 +0000 | [diff] [blame] | 97 | // ambiguous. |
Douglas Gregor | 01e56ae | 2010-04-12 20:54:26 +0000 | [diff] [blame] | 98 | if (ClassTemplateDecl *ClassTmpl = dyn_cast<ClassTemplateDecl>(Repl)) |
| 99 | if (!ClassTemplates.insert(ClassTmpl)) { |
| 100 | filter.erase(); |
| 101 | continue; |
| 102 | } |
John McCall | 8ba6691 | 2010-08-13 07:02:08 +0000 | [diff] [blame] | 103 | |
| 104 | // FIXME: we promote access to public here as a workaround to |
| 105 | // the fact that LookupResult doesn't let us remember that we |
| 106 | // found this template through a particular injected class name, |
| 107 | // which means we end up doing nasty things to the invariants. |
| 108 | // Pretending that access is public is *much* safer. |
| 109 | filter.replace(Repl, AS_public); |
Douglas Gregor | 01e56ae | 2010-04-12 20:54:26 +0000 | [diff] [blame] | 110 | } |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 111 | } |
| 112 | filter.done(); |
| 113 | } |
| 114 | |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 115 | bool Sema::hasAnyAcceptableTemplateNames(LookupResult &R) { |
| 116 | for (LookupResult::iterator I = R.begin(), IEnd = R.end(); I != IEnd; ++I) |
| 117 | if (isAcceptableTemplateName(Context, *I)) |
| 118 | return true; |
| 119 | |
Douglas Gregor | 3b88735 | 2011-04-27 04:48:22 +0000 | [diff] [blame] | 120 | return false; |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 121 | } |
| 122 | |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 123 | TemplateNameKind Sema::isTemplateName(Scope *S, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 124 | CXXScopeSpec &SS, |
Abramo Bagnara | 7c15353 | 2010-08-06 12:11:11 +0000 | [diff] [blame] | 125 | bool hasTemplateKeyword, |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 126 | UnqualifiedId &Name, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 127 | ParsedType ObjectTypePtr, |
Douglas Gregor | 495c35d | 2009-08-25 22:51:20 +0000 | [diff] [blame] | 128 | bool EnteringContext, |
Douglas Gregor | 1fd6d44 | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 129 | TemplateTy &TemplateResult, |
| 130 | bool &MemberOfUnknownSpecialization) { |
Douglas Gregor | b862b8f | 2010-01-11 23:29:10 +0000 | [diff] [blame] | 131 | assert(getLangOptions().CPlusPlus && "No template names in C!"); |
| 132 | |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 133 | DeclarationName TName; |
Douglas Gregor | 1fd6d44 | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 134 | MemberOfUnknownSpecialization = false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 135 | |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 136 | switch (Name.getKind()) { |
| 137 | case UnqualifiedId::IK_Identifier: |
| 138 | TName = DeclarationName(Name.Identifier); |
| 139 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 140 | |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 141 | case UnqualifiedId::IK_OperatorFunctionId: |
| 142 | TName = Context.DeclarationNames.getCXXOperatorName( |
| 143 | Name.OperatorFunctionId.Operator); |
| 144 | break; |
| 145 | |
Sean Hunt | e6252d1 | 2009-11-28 08:58:14 +0000 | [diff] [blame] | 146 | case UnqualifiedId::IK_LiteralOperatorId: |
Sean Hunt | 3e518bd | 2009-11-29 07:34:05 +0000 | [diff] [blame] | 147 | TName = Context.DeclarationNames.getCXXLiteralOperatorName(Name.Identifier); |
| 148 | break; |
Sean Hunt | e6252d1 | 2009-11-28 08:58:14 +0000 | [diff] [blame] | 149 | |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 150 | default: |
| 151 | return TNK_Non_template; |
| 152 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 153 | |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 154 | QualType ObjectType = ObjectTypePtr.get(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 155 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 156 | LookupResult R(*this, TName, Name.getSourceRange().getBegin(), |
Douglas Gregor | bfea239 | 2009-12-31 08:11:17 +0000 | [diff] [blame] | 157 | LookupOrdinaryName); |
Douglas Gregor | 1fd6d44 | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 158 | LookupTemplateName(R, S, SS, ObjectType, EnteringContext, |
| 159 | MemberOfUnknownSpecialization); |
John McCall | 67d22fb | 2010-08-28 20:17:00 +0000 | [diff] [blame] | 160 | if (R.empty()) return TNK_Non_template; |
| 161 | if (R.isAmbiguous()) { |
| 162 | // Suppress diagnostics; we'll redo this lookup later. |
John McCall | b859206 | 2010-08-13 02:23:42 +0000 | [diff] [blame] | 163 | R.suppressDiagnostics(); |
John McCall | 67d22fb | 2010-08-28 20:17:00 +0000 | [diff] [blame] | 164 | |
| 165 | // FIXME: we might have ambiguous templates, in which case we |
| 166 | // should at least parse them properly! |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 167 | return TNK_Non_template; |
John McCall | b859206 | 2010-08-13 02:23:42 +0000 | [diff] [blame] | 168 | } |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 169 | |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 170 | TemplateName Template; |
| 171 | TemplateNameKind TemplateKind; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 172 | |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 173 | unsigned ResultCount = R.end() - R.begin(); |
| 174 | if (ResultCount > 1) { |
| 175 | // We assume that we'll preserve the qualifier from a function |
| 176 | // template name in other ways. |
| 177 | Template = Context.getOverloadedTemplateName(R.begin(), R.end()); |
| 178 | TemplateKind = TNK_Function_template; |
John McCall | b859206 | 2010-08-13 02:23:42 +0000 | [diff] [blame] | 179 | |
| 180 | // We'll do this lookup again later. |
| 181 | R.suppressDiagnostics(); |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 182 | } else { |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 183 | TemplateDecl *TD = cast<TemplateDecl>((*R.begin())->getUnderlyingDecl()); |
| 184 | |
| 185 | if (SS.isSet() && !SS.isInvalid()) { |
| 186 | NestedNameSpecifier *Qualifier |
| 187 | = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); |
Abramo Bagnara | 7c15353 | 2010-08-06 12:11:11 +0000 | [diff] [blame] | 188 | Template = Context.getQualifiedTemplateName(Qualifier, |
| 189 | hasTemplateKeyword, TD); |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 190 | } else { |
| 191 | Template = TemplateName(TD); |
| 192 | } |
| 193 | |
John McCall | b859206 | 2010-08-13 02:23:42 +0000 | [diff] [blame] | 194 | if (isa<FunctionTemplateDecl>(TD)) { |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 195 | TemplateKind = TNK_Function_template; |
John McCall | b859206 | 2010-08-13 02:23:42 +0000 | [diff] [blame] | 196 | |
| 197 | // We'll do this lookup again later. |
| 198 | R.suppressDiagnostics(); |
| 199 | } else { |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 200 | assert(isa<ClassTemplateDecl>(TD) || isa<TemplateTemplateParmDecl>(TD) || |
| 201 | isa<TypeAliasTemplateDecl>(TD)); |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 202 | TemplateKind = TNK_Type_template; |
| 203 | } |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 204 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 205 | |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 206 | TemplateResult = TemplateTy::make(Template); |
| 207 | return TemplateKind; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 208 | } |
| 209 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 210 | bool Sema::DiagnoseUnknownTemplateName(const IdentifierInfo &II, |
Douglas Gregor | 84d0a19 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 211 | SourceLocation IILoc, |
| 212 | Scope *S, |
| 213 | const CXXScopeSpec *SS, |
| 214 | TemplateTy &SuggestedTemplate, |
| 215 | TemplateNameKind &SuggestedKind) { |
| 216 | // We can't recover unless there's a dependent scope specifier preceding the |
| 217 | // template name. |
Douglas Gregor | d5ab9b0 | 2010-05-21 23:43:39 +0000 | [diff] [blame] | 218 | // FIXME: Typo correction? |
Douglas Gregor | 84d0a19 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 219 | if (!SS || !SS->isSet() || !isDependentScopeSpecifier(*SS) || |
| 220 | computeDeclContext(*SS)) |
| 221 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 222 | |
Douglas Gregor | 84d0a19 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 223 | // The code is missing a 'template' keyword prior to the dependent template |
| 224 | // name. |
| 225 | NestedNameSpecifier *Qualifier = (NestedNameSpecifier*)SS->getScopeRep(); |
| 226 | Diag(IILoc, diag::err_template_kw_missing) |
| 227 | << Qualifier << II.getName() |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 228 | << FixItHint::CreateInsertion(IILoc, "template "); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 229 | SuggestedTemplate |
Douglas Gregor | 84d0a19 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 230 | = TemplateTy::make(Context.getDependentTemplateName(Qualifier, &II)); |
| 231 | SuggestedKind = TNK_Dependent_template_name; |
| 232 | return true; |
| 233 | } |
| 234 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 235 | void Sema::LookupTemplateName(LookupResult &Found, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 236 | Scope *S, CXXScopeSpec &SS, |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 237 | QualType ObjectType, |
Douglas Gregor | 1fd6d44 | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 238 | bool EnteringContext, |
| 239 | bool &MemberOfUnknownSpecialization) { |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 240 | // Determine where to perform name lookup |
Douglas Gregor | 1fd6d44 | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 241 | MemberOfUnknownSpecialization = false; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 242 | DeclContext *LookupCtx = 0; |
| 243 | bool isDependent = false; |
| 244 | if (!ObjectType.isNull()) { |
| 245 | // This nested-name-specifier occurs in a member access expression, e.g., |
| 246 | // x->B::f, and we are looking into the type of the object. |
| 247 | assert(!SS.isSet() && "ObjectType and scope specifier cannot coexist"); |
| 248 | LookupCtx = computeDeclContext(ObjectType); |
| 249 | isDependent = ObjectType->isDependentType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 250 | assert((isDependent || !ObjectType->isIncompleteType()) && |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 251 | "Caller should have completed object type"); |
| 252 | } else if (SS.isSet()) { |
| 253 | // This nested-name-specifier occurs after another nested-name-specifier, |
| 254 | // so long into the context associated with the prior nested-name-specifier. |
| 255 | LookupCtx = computeDeclContext(SS, EnteringContext); |
| 256 | isDependent = isDependentScopeSpecifier(SS); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 257 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 258 | // The declaration context must be complete. |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 259 | if (LookupCtx && RequireCompleteDeclContext(SS, LookupCtx)) |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 260 | return; |
| 261 | } |
| 262 | |
| 263 | bool ObjectTypeSearchedInScope = false; |
| 264 | if (LookupCtx) { |
| 265 | // Perform "qualified" name lookup into the declaration context we |
| 266 | // computed, which is either the type of the base of a member access |
| 267 | // expression or the declaration context associated with a prior |
| 268 | // nested-name-specifier. |
| 269 | LookupQualifiedName(Found, LookupCtx); |
| 270 | |
| 271 | if (!ObjectType.isNull() && Found.empty()) { |
| 272 | // C++ [basic.lookup.classref]p1: |
| 273 | // In a class member access expression (5.2.5), if the . or -> token is |
| 274 | // immediately followed by an identifier followed by a <, the |
| 275 | // identifier must be looked up to determine whether the < is the |
| 276 | // beginning of a template argument list (14.2) or a less-than operator. |
| 277 | // The identifier is first looked up in the class of the object |
| 278 | // expression. If the identifier is not found, it is then looked up in |
| 279 | // the context of the entire postfix-expression and shall name a class |
| 280 | // or function template. |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 281 | if (S) LookupName(Found, S); |
| 282 | ObjectTypeSearchedInScope = true; |
| 283 | } |
Douglas Gregor | f9f97a0 | 2010-07-16 16:54:17 +0000 | [diff] [blame] | 284 | } else if (isDependent && (!S || ObjectType.isNull())) { |
Douglas Gregor | 2e93388 | 2010-01-12 17:06:20 +0000 | [diff] [blame] | 285 | // We cannot look into a dependent object type or nested nme |
| 286 | // specifier. |
Douglas Gregor | 1fd6d44 | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 287 | MemberOfUnknownSpecialization = true; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 288 | return; |
| 289 | } else { |
| 290 | // Perform unqualified name lookup in the current scope. |
| 291 | LookupName(Found, S); |
| 292 | } |
| 293 | |
Douglas Gregor | 2e93388 | 2010-01-12 17:06:20 +0000 | [diff] [blame] | 294 | if (Found.empty() && !isDependent) { |
Douglas Gregor | bfea239 | 2009-12-31 08:11:17 +0000 | [diff] [blame] | 295 | // If we did not find any names, attempt to correct any typos. |
| 296 | DeclarationName Name = Found.getLookupName(); |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 297 | Found.clear(); |
| 298 | if (TypoCorrection Corrected = CorrectTypo(Found.getLookupNameInfo(), |
| 299 | Found.getLookupKind(), S, &SS, |
| 300 | LookupCtx, false, |
| 301 | CTC_CXXCasts)) { |
| 302 | Found.setLookupName(Corrected.getCorrection()); |
| 303 | if (Corrected.getCorrectionDecl()) |
| 304 | Found.addDecl(Corrected.getCorrectionDecl()); |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 305 | FilterAcceptableTemplateNames(Found); |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 306 | if (!Found.empty()) { |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 307 | std::string CorrectedStr(Corrected.getAsString(getLangOptions())); |
| 308 | std::string CorrectedQuotedStr(Corrected.getQuoted(getLangOptions())); |
Douglas Gregor | bfea239 | 2009-12-31 08:11:17 +0000 | [diff] [blame] | 309 | if (LookupCtx) |
| 310 | Diag(Found.getNameLoc(), diag::err_no_member_template_suggest) |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 311 | << Name << LookupCtx << CorrectedQuotedStr << SS.getRange() |
| 312 | << FixItHint::CreateReplacement(Found.getNameLoc(), CorrectedStr); |
Douglas Gregor | bfea239 | 2009-12-31 08:11:17 +0000 | [diff] [blame] | 313 | else |
| 314 | Diag(Found.getNameLoc(), diag::err_no_template_suggest) |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 315 | << Name << CorrectedQuotedStr |
| 316 | << FixItHint::CreateReplacement(Found.getNameLoc(), CorrectedStr); |
Douglas Gregor | 67dd1d4 | 2010-01-07 00:17:44 +0000 | [diff] [blame] | 317 | if (TemplateDecl *Template = Found.getAsSingle<TemplateDecl>()) |
| 318 | Diag(Template->getLocation(), diag::note_previous_decl) |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 319 | << CorrectedQuotedStr; |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 320 | } |
Douglas Gregor | bfea239 | 2009-12-31 08:11:17 +0000 | [diff] [blame] | 321 | } else { |
Douglas Gregor | 12eb5d6 | 2010-06-29 19:27:42 +0000 | [diff] [blame] | 322 | Found.setLookupName(Name); |
Douglas Gregor | bfea239 | 2009-12-31 08:11:17 +0000 | [diff] [blame] | 323 | } |
| 324 | } |
| 325 | |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 326 | FilterAcceptableTemplateNames(Found); |
Douglas Gregor | f9f97a0 | 2010-07-16 16:54:17 +0000 | [diff] [blame] | 327 | if (Found.empty()) { |
| 328 | if (isDependent) |
| 329 | MemberOfUnknownSpecialization = true; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 330 | return; |
Douglas Gregor | f9f97a0 | 2010-07-16 16:54:17 +0000 | [diff] [blame] | 331 | } |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 332 | |
| 333 | if (S && !ObjectType.isNull() && !ObjectTypeSearchedInScope) { |
| 334 | // C++ [basic.lookup.classref]p1: |
| 335 | // [...] If the lookup in the class of the object expression finds a |
| 336 | // template, the name is also looked up in the context of the entire |
| 337 | // postfix-expression and [...] |
| 338 | // |
| 339 | LookupResult FoundOuter(*this, Found.getLookupName(), Found.getNameLoc(), |
| 340 | LookupOrdinaryName); |
| 341 | LookupName(FoundOuter, S); |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 342 | FilterAcceptableTemplateNames(FoundOuter); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 343 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 344 | if (FoundOuter.empty()) { |
| 345 | // - if the name is not found, the name found in the class of the |
| 346 | // object expression is used, otherwise |
Douglas Gregor | a6d1e76 | 2011-08-10 21:59:45 +0000 | [diff] [blame] | 347 | } else if (!FoundOuter.getAsSingle<ClassTemplateDecl>() || |
| 348 | FoundOuter.isAmbiguous()) { |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 349 | // - if the name is found in the context of the entire |
| 350 | // postfix-expression and does not name a class template, the name |
| 351 | // found in the class of the object expression is used, otherwise |
Douglas Gregor | a6d1e76 | 2011-08-10 21:59:45 +0000 | [diff] [blame] | 352 | FoundOuter.clear(); |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 353 | } else if (!Found.isSuppressingDiagnostics()) { |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 354 | // - if the name found is a class template, it must refer to the same |
| 355 | // entity as the one found in the class of the object expression, |
| 356 | // otherwise the program is ill-formed. |
| 357 | if (!Found.isSingleResult() || |
| 358 | Found.getFoundDecl()->getCanonicalDecl() |
| 359 | != FoundOuter.getFoundDecl()->getCanonicalDecl()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 360 | Diag(Found.getNameLoc(), |
Jeffrey Yasskin | 21d07e4 | 2010-06-05 01:39:57 +0000 | [diff] [blame] | 361 | diag::ext_nested_name_member_ref_lookup_ambiguous) |
| 362 | << Found.getLookupName() |
| 363 | << ObjectType; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 364 | Diag(Found.getRepresentativeDecl()->getLocation(), |
| 365 | diag::note_ambig_member_ref_object_type) |
| 366 | << ObjectType; |
| 367 | Diag(FoundOuter.getFoundDecl()->getLocation(), |
| 368 | diag::note_ambig_member_ref_scope); |
| 369 | |
| 370 | // Recover by taking the template that we found in the object |
| 371 | // expression's type. |
| 372 | } |
| 373 | } |
| 374 | } |
| 375 | } |
| 376 | |
John McCall | 2f841ba | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 377 | /// ActOnDependentIdExpression - Handle a dependent id-expression that |
| 378 | /// was just parsed. This is only possible with an explicit scope |
| 379 | /// specifier naming a dependent type. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 380 | ExprResult |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 381 | Sema::ActOnDependentIdExpression(const CXXScopeSpec &SS, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 382 | const DeclarationNameInfo &NameInfo, |
John McCall | 2f841ba | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 383 | bool isAddressOfOperand, |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 384 | const TemplateArgumentListInfo *TemplateArgs) { |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 385 | DeclContext *DC = getFunctionLevelDeclContext(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 386 | |
John McCall | 2f841ba | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 387 | if (!isAddressOfOperand && |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 388 | isa<CXXMethodDecl>(DC) && |
| 389 | cast<CXXMethodDecl>(DC)->isInstance()) { |
| 390 | QualType ThisType = cast<CXXMethodDecl>(DC)->getThisType(Context); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 391 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 392 | // Since the 'this' expression is synthesized, we don't need to |
| 393 | // perform the double-lookup check. |
| 394 | NamedDecl *FirstQualifierInScope = 0; |
| 395 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 396 | return Owned(CXXDependentScopeMemberExpr::Create(Context, |
| 397 | /*This*/ 0, ThisType, |
| 398 | /*IsArrow*/ true, |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 399 | /*Op*/ SourceLocation(), |
Douglas Gregor | 7c3179c | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 400 | SS.getWithLocInContext(Context), |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 401 | FirstQualifierInScope, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 402 | NameInfo, |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 403 | TemplateArgs)); |
| 404 | } |
| 405 | |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 406 | return BuildDependentDeclRefExpr(SS, NameInfo, TemplateArgs); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 407 | } |
| 408 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 409 | ExprResult |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 410 | Sema::BuildDependentDeclRefExpr(const CXXScopeSpec &SS, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 411 | const DeclarationNameInfo &NameInfo, |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 412 | const TemplateArgumentListInfo *TemplateArgs) { |
| 413 | return Owned(DependentScopeDeclRefExpr::Create(Context, |
Douglas Gregor | 00cf3cc | 2011-02-25 20:49:16 +0000 | [diff] [blame] | 414 | SS.getWithLocInContext(Context), |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 415 | NameInfo, |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 416 | TemplateArgs)); |
Douglas Gregor | d6fb7ef | 2008-12-18 19:37:40 +0000 | [diff] [blame] | 417 | } |
| 418 | |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 419 | /// DiagnoseTemplateParameterShadow - Produce a diagnostic complaining |
| 420 | /// that the template parameter 'PrevDecl' is being shadowed by a new |
| 421 | /// declaration at location Loc. Returns true to indicate that this is |
| 422 | /// an error, and false otherwise. |
| 423 | bool Sema::DiagnoseTemplateParameterShadow(SourceLocation Loc, Decl *PrevDecl) { |
Douglas Gregor | f57172b | 2008-12-08 18:40:42 +0000 | [diff] [blame] | 424 | assert(PrevDecl->isTemplateParameter() && "Not a template parameter"); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 425 | |
| 426 | // Microsoft Visual C++ permits template parameters to be shadowed. |
| 427 | if (getLangOptions().Microsoft) |
| 428 | return false; |
| 429 | |
| 430 | // C++ [temp.local]p4: |
| 431 | // A template-parameter shall not be redeclared within its |
| 432 | // scope (including nested scopes). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 433 | Diag(Loc, diag::err_template_param_shadow) |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 434 | << cast<NamedDecl>(PrevDecl)->getDeclName(); |
| 435 | Diag(PrevDecl->getLocation(), diag::note_template_param_here); |
| 436 | return true; |
| 437 | } |
| 438 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 439 | /// AdjustDeclIfTemplate - If the given decl happens to be a template, reset |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 440 | /// the parameter D to reference the templated declaration and return a pointer |
| 441 | /// to the template declaration. Otherwise, do nothing to D and return null. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 442 | TemplateDecl *Sema::AdjustDeclIfTemplate(Decl *&D) { |
| 443 | if (TemplateDecl *Temp = dyn_cast_or_null<TemplateDecl>(D)) { |
| 444 | D = Temp->getTemplatedDecl(); |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 445 | return Temp; |
| 446 | } |
| 447 | return 0; |
| 448 | } |
| 449 | |
Douglas Gregor | ba68eca | 2011-01-05 17:40:24 +0000 | [diff] [blame] | 450 | ParsedTemplateArgument ParsedTemplateArgument::getTemplatePackExpansion( |
| 451 | SourceLocation EllipsisLoc) const { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 452 | assert(Kind == Template && |
Douglas Gregor | ba68eca | 2011-01-05 17:40:24 +0000 | [diff] [blame] | 453 | "Only template template arguments can be pack expansions here"); |
| 454 | assert(getAsTemplate().get().containsUnexpandedParameterPack() && |
| 455 | "Template template argument pack expansion without packs"); |
| 456 | ParsedTemplateArgument Result(*this); |
| 457 | Result.EllipsisLoc = EllipsisLoc; |
| 458 | return Result; |
| 459 | } |
| 460 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 461 | static TemplateArgumentLoc translateTemplateArgument(Sema &SemaRef, |
| 462 | const ParsedTemplateArgument &Arg) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 463 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 464 | switch (Arg.getKind()) { |
| 465 | case ParsedTemplateArgument::Type: { |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 466 | TypeSourceInfo *DI; |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 467 | QualType T = SemaRef.GetTypeFromParser(Arg.getAsType(), &DI); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 468 | if (!DI) |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 469 | DI = SemaRef.Context.getTrivialTypeSourceInfo(T, Arg.getLocation()); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 470 | return TemplateArgumentLoc(TemplateArgument(T), DI); |
| 471 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 472 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 473 | case ParsedTemplateArgument::NonType: { |
| 474 | Expr *E = static_cast<Expr *>(Arg.getAsExpr()); |
| 475 | return TemplateArgumentLoc(TemplateArgument(E), E); |
| 476 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 477 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 478 | case ParsedTemplateArgument::Template: { |
John McCall | 2b5289b | 2010-08-23 07:28:44 +0000 | [diff] [blame] | 479 | TemplateName Template = Arg.getAsTemplate().get(); |
Douglas Gregor | 2be29f4 | 2011-01-14 23:41:42 +0000 | [diff] [blame] | 480 | TemplateArgument TArg; |
| 481 | if (Arg.getEllipsisLoc().isValid()) |
| 482 | TArg = TemplateArgument(Template, llvm::Optional<unsigned int>()); |
| 483 | else |
| 484 | TArg = Template; |
| 485 | return TemplateArgumentLoc(TArg, |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 486 | Arg.getScopeSpec().getWithLocInContext( |
| 487 | SemaRef.Context), |
Douglas Gregor | ba68eca | 2011-01-05 17:40:24 +0000 | [diff] [blame] | 488 | Arg.getLocation(), |
| 489 | Arg.getEllipsisLoc()); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 490 | } |
| 491 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 492 | |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 493 | llvm_unreachable("Unhandled parsed template argument"); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 494 | return TemplateArgumentLoc(); |
| 495 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 496 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 497 | /// \brief Translates template arguments as provided by the parser |
| 498 | /// into template arguments used by semantic analysis. |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 499 | void Sema::translateTemplateArguments(const ASTTemplateArgsPtr &TemplateArgsIn, |
| 500 | TemplateArgumentListInfo &TemplateArgs) { |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 501 | for (unsigned I = 0, Last = TemplateArgsIn.size(); I != Last; ++I) |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 502 | TemplateArgs.addArgument(translateTemplateArgument(*this, |
| 503 | TemplateArgsIn[I])); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 504 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 505 | |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 506 | /// ActOnTypeParameter - Called when a C++ template type parameter |
| 507 | /// (e.g., "typename T") has been parsed. Typename specifies whether |
| 508 | /// the keyword "typename" was used to declare the type parameter |
| 509 | /// (otherwise, "class" was used), and KeyLoc is the location of the |
| 510 | /// "class" or "typename" keyword. ParamName is the name of the |
| 511 | /// parameter (NULL indicates an unnamed template parameter) and |
Chandler Carruth | 4fb86f8 | 2011-05-01 00:51:33 +0000 | [diff] [blame] | 512 | /// ParamNameLoc is the location of the parameter name (if any). |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 513 | /// If the type parameter has a default argument, it will be added |
| 514 | /// later via ActOnTypeParameterDefault. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 515 | Decl *Sema::ActOnTypeParameter(Scope *S, bool Typename, bool Ellipsis, |
| 516 | SourceLocation EllipsisLoc, |
| 517 | SourceLocation KeyLoc, |
| 518 | IdentifierInfo *ParamName, |
| 519 | SourceLocation ParamNameLoc, |
| 520 | unsigned Depth, unsigned Position, |
| 521 | SourceLocation EqualLoc, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 522 | ParsedType DefaultArg) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 523 | assert(S->isTemplateParamScope() && |
| 524 | "Template type parameter not in template parameter scope!"); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 525 | bool Invalid = false; |
| 526 | |
| 527 | if (ParamName) { |
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 528 | NamedDecl *PrevDecl = LookupSingleName(S, ParamName, ParamNameLoc, |
Douglas Gregor | c0b3964 | 2010-04-15 23:40:53 +0000 | [diff] [blame] | 529 | LookupOrdinaryName, |
| 530 | ForRedeclaration); |
Douglas Gregor | f57172b | 2008-12-08 18:40:42 +0000 | [diff] [blame] | 531 | if (PrevDecl && PrevDecl->isTemplateParameter()) |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 532 | Invalid = Invalid || DiagnoseTemplateParameterShadow(ParamNameLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 533 | PrevDecl); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 534 | } |
| 535 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 536 | SourceLocation Loc = ParamNameLoc; |
| 537 | if (!ParamName) |
| 538 | Loc = KeyLoc; |
| 539 | |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 540 | TemplateTypeParmDecl *Param |
John McCall | 7a9813c | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 541 | = TemplateTypeParmDecl::Create(Context, Context.getTranslationUnitDecl(), |
Abramo Bagnara | 344577e | 2011-03-06 15:48:19 +0000 | [diff] [blame] | 542 | KeyLoc, Loc, Depth, Position, ParamName, |
| 543 | Typename, Ellipsis); |
Douglas Gregor | 9a299e0 | 2011-03-04 17:52:15 +0000 | [diff] [blame] | 544 | Param->setAccess(AS_public); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 545 | if (Invalid) |
| 546 | Param->setInvalidDecl(); |
| 547 | |
| 548 | if (ParamName) { |
| 549 | // Add the template parameter into the current scope. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 550 | S->AddDecl(Param); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 551 | IdResolver.AddDecl(Param); |
| 552 | } |
| 553 | |
Douglas Gregor | 61c4d28 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 554 | // C++0x [temp.param]p9: |
| 555 | // A default template-argument may be specified for any kind of |
| 556 | // template-parameter that is not a template parameter pack. |
| 557 | if (DefaultArg && Ellipsis) { |
| 558 | Diag(EqualLoc, diag::err_template_param_pack_default_arg); |
| 559 | DefaultArg = ParsedType(); |
| 560 | } |
| 561 | |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 562 | // Handle the default argument, if provided. |
| 563 | if (DefaultArg) { |
| 564 | TypeSourceInfo *DefaultTInfo; |
| 565 | GetTypeFromParser(DefaultArg, &DefaultTInfo); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 566 | |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 567 | assert(DefaultTInfo && "expected source information for type"); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 568 | |
Douglas Gregor | 6f52675 | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 569 | // Check for unexpanded parameter packs. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 570 | if (DiagnoseUnexpandedParameterPack(Loc, DefaultTInfo, |
Douglas Gregor | 6f52675 | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 571 | UPPC_DefaultArgument)) |
| 572 | return Param; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 573 | |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 574 | // Check the template argument itself. |
| 575 | if (CheckTemplateArgument(Param, DefaultTInfo)) { |
| 576 | Param->setInvalidDecl(); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 577 | return Param; |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 578 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 579 | |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 580 | Param->setDefaultArgument(DefaultTInfo, false); |
| 581 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 582 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 583 | return Param; |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 584 | } |
| 585 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 586 | /// \brief Check that the type of a non-type template parameter is |
| 587 | /// well-formed. |
| 588 | /// |
| 589 | /// \returns the (possibly-promoted) parameter type if valid; |
| 590 | /// otherwise, produces a diagnostic and returns a NULL type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 591 | QualType |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 592 | Sema::CheckNonTypeTemplateParameterType(QualType T, SourceLocation Loc) { |
Douglas Gregor | a481ec4 | 2010-05-23 19:57:01 +0000 | [diff] [blame] | 593 | // We don't allow variably-modified types as the type of non-type template |
| 594 | // parameters. |
| 595 | if (T->isVariablyModifiedType()) { |
| 596 | Diag(Loc, diag::err_variably_modified_nontype_template_param) |
| 597 | << T; |
| 598 | return QualType(); |
| 599 | } |
| 600 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 601 | // C++ [temp.param]p4: |
| 602 | // |
| 603 | // A non-type template-parameter shall have one of the following |
| 604 | // (optionally cv-qualified) types: |
| 605 | // |
| 606 | // -- integral or enumeration type, |
Douglas Gregor | 2ade35e | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 607 | if (T->isIntegralOrEnumerationType() || |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 608 | // -- pointer to object or pointer to function, |
Eli Friedman | 1357869 | 2010-08-05 02:49:48 +0000 | [diff] [blame] | 609 | T->isPointerType() || |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 610 | // -- reference to object or reference to function, |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 611 | T->isReferenceType() || |
Douglas Gregor | 84ee2ee | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 612 | // -- pointer to member, |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 613 | T->isMemberPointerType() || |
Douglas Gregor | 84ee2ee | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 614 | // -- std::nullptr_t. |
| 615 | T->isNullPtrType() || |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 616 | // If T is a dependent type, we can't do the check now, so we |
| 617 | // assume that it is well-formed. |
| 618 | T->isDependentType()) |
| 619 | return T; |
| 620 | // C++ [temp.param]p8: |
| 621 | // |
| 622 | // A non-type template-parameter of type "array of T" or |
| 623 | // "function returning T" is adjusted to be of type "pointer to |
| 624 | // T" or "pointer to function returning T", respectively. |
| 625 | else if (T->isArrayType()) |
| 626 | // FIXME: Keep the type prior to promotion? |
| 627 | return Context.getArrayDecayedType(T); |
| 628 | else if (T->isFunctionType()) |
| 629 | // FIXME: Keep the type prior to promotion? |
| 630 | return Context.getPointerType(T); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 631 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 632 | Diag(Loc, diag::err_template_nontype_parm_bad_type) |
| 633 | << T; |
| 634 | |
| 635 | return QualType(); |
| 636 | } |
| 637 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 638 | Decl *Sema::ActOnNonTypeTemplateParameter(Scope *S, Declarator &D, |
| 639 | unsigned Depth, |
| 640 | unsigned Position, |
| 641 | SourceLocation EqualLoc, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 642 | Expr *Default) { |
John McCall | bf1a028 | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 643 | TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S); |
| 644 | QualType T = TInfo->getType(); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 645 | |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 646 | assert(S->isTemplateParamScope() && |
| 647 | "Non-type template parameter not in template parameter scope!"); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 648 | bool Invalid = false; |
| 649 | |
| 650 | IdentifierInfo *ParamName = D.getIdentifier(); |
| 651 | if (ParamName) { |
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 652 | NamedDecl *PrevDecl = LookupSingleName(S, ParamName, D.getIdentifierLoc(), |
Douglas Gregor | c0b3964 | 2010-04-15 23:40:53 +0000 | [diff] [blame] | 653 | LookupOrdinaryName, |
| 654 | ForRedeclaration); |
Douglas Gregor | f57172b | 2008-12-08 18:40:42 +0000 | [diff] [blame] | 655 | if (PrevDecl && PrevDecl->isTemplateParameter()) |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 656 | Invalid = Invalid || DiagnoseTemplateParameterShadow(D.getIdentifierLoc(), |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 657 | PrevDecl); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 658 | } |
| 659 | |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 660 | T = CheckNonTypeTemplateParameterType(T, D.getIdentifierLoc()); |
| 661 | if (T.isNull()) { |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 662 | T = Context.IntTy; // Recover with an 'int' type. |
Douglas Gregor | ceef30c | 2009-03-09 16:46:39 +0000 | [diff] [blame] | 663 | Invalid = true; |
| 664 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 665 | |
Douglas Gregor | 10738d3 | 2010-12-23 23:51:58 +0000 | [diff] [blame] | 666 | bool IsParameterPack = D.hasEllipsis(); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 667 | NonTypeTemplateParmDecl *Param |
John McCall | 7a9813c | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 668 | = NonTypeTemplateParmDecl::Create(Context, Context.getTranslationUnitDecl(), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 669 | D.getSourceRange().getBegin(), |
John McCall | 7a9813c | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 670 | D.getIdentifierLoc(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 671 | Depth, Position, ParamName, T, |
Douglas Gregor | 10738d3 | 2010-12-23 23:51:58 +0000 | [diff] [blame] | 672 | IsParameterPack, TInfo); |
Douglas Gregor | 9a299e0 | 2011-03-04 17:52:15 +0000 | [diff] [blame] | 673 | Param->setAccess(AS_public); |
| 674 | |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 675 | if (Invalid) |
| 676 | Param->setInvalidDecl(); |
| 677 | |
| 678 | if (D.getIdentifier()) { |
| 679 | // Add the template parameter into the current scope. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 680 | S->AddDecl(Param); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 681 | IdResolver.AddDecl(Param); |
| 682 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 683 | |
Douglas Gregor | 61c4d28 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 684 | // C++0x [temp.param]p9: |
| 685 | // A default template-argument may be specified for any kind of |
| 686 | // template-parameter that is not a template parameter pack. |
| 687 | if (Default && IsParameterPack) { |
| 688 | Diag(EqualLoc, diag::err_template_param_pack_default_arg); |
| 689 | Default = 0; |
| 690 | } |
| 691 | |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 692 | // Check the well-formedness of the default template argument, if provided. |
Douglas Gregor | 10738d3 | 2010-12-23 23:51:58 +0000 | [diff] [blame] | 693 | if (Default) { |
Douglas Gregor | 6f52675 | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 694 | // Check for unexpanded parameter packs. |
| 695 | if (DiagnoseUnexpandedParameterPack(Default, UPPC_DefaultArgument)) |
| 696 | return Param; |
| 697 | |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 698 | TemplateArgument Converted; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 699 | ExprResult DefaultRes = CheckTemplateArgument(Param, Param->getType(), Default, Converted); |
| 700 | if (DefaultRes.isInvalid()) { |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 701 | Param->setInvalidDecl(); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 702 | return Param; |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 703 | } |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 704 | Default = DefaultRes.take(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 705 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 706 | Param->setDefaultArgument(Default, false); |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 707 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 708 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 709 | return Param; |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 710 | } |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 711 | |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 712 | /// ActOnTemplateTemplateParameter - Called when a C++ template template |
| 713 | /// parameter (e.g. T in template <template <typename> class T> class array) |
| 714 | /// has been parsed. S is the current scope. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 715 | Decl *Sema::ActOnTemplateTemplateParameter(Scope* S, |
| 716 | SourceLocation TmpLoc, |
| 717 | TemplateParamsTy *Params, |
Douglas Gregor | 61c4d28 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 718 | SourceLocation EllipsisLoc, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 719 | IdentifierInfo *Name, |
| 720 | SourceLocation NameLoc, |
| 721 | unsigned Depth, |
| 722 | unsigned Position, |
| 723 | SourceLocation EqualLoc, |
Douglas Gregor | 61c4d28 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 724 | ParsedTemplateArgument Default) { |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 725 | assert(S->isTemplateParamScope() && |
| 726 | "Template template parameter not in template parameter scope!"); |
| 727 | |
| 728 | // Construct the parameter object. |
Douglas Gregor | 61c4d28 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 729 | bool IsParameterPack = EllipsisLoc.isValid(); |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 730 | TemplateTemplateParmDecl *Param = |
John McCall | 7a9813c | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 731 | TemplateTemplateParmDecl::Create(Context, Context.getTranslationUnitDecl(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 732 | NameLoc.isInvalid()? TmpLoc : NameLoc, |
| 733 | Depth, Position, IsParameterPack, |
Douglas Gregor | 61c4d28 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 734 | Name, Params); |
Douglas Gregor | 9a299e0 | 2011-03-04 17:52:15 +0000 | [diff] [blame] | 735 | Param->setAccess(AS_public); |
| 736 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 737 | // If the template template parameter has a name, then link the identifier |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 738 | // into the scope and lookup mechanisms. |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 739 | if (Name) { |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 740 | S->AddDecl(Param); |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 741 | IdResolver.AddDecl(Param); |
| 742 | } |
| 743 | |
Douglas Gregor | 6f52675 | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 744 | if (Params->size() == 0) { |
| 745 | Diag(Param->getLocation(), diag::err_template_template_parm_no_parms) |
| 746 | << SourceRange(Params->getLAngleLoc(), Params->getRAngleLoc()); |
| 747 | Param->setInvalidDecl(); |
| 748 | } |
| 749 | |
Douglas Gregor | 61c4d28 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 750 | // C++0x [temp.param]p9: |
| 751 | // A default template-argument may be specified for any kind of |
| 752 | // template-parameter that is not a template parameter pack. |
| 753 | if (IsParameterPack && !Default.isInvalid()) { |
| 754 | Diag(EqualLoc, diag::err_template_param_pack_default_arg); |
| 755 | Default = ParsedTemplateArgument(); |
| 756 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 757 | |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 758 | if (!Default.isInvalid()) { |
| 759 | // Check only that we have a template template argument. We don't want to |
| 760 | // try to check well-formedness now, because our template template parameter |
| 761 | // might have dependent types in its template parameters, which we wouldn't |
| 762 | // be able to match now. |
| 763 | // |
| 764 | // If none of the template template parameter's template arguments mention |
| 765 | // other template parameters, we could actually perform more checking here. |
| 766 | // However, it isn't worth doing. |
| 767 | TemplateArgumentLoc DefaultArg = translateTemplateArgument(*this, Default); |
| 768 | if (DefaultArg.getArgument().getAsTemplate().isNull()) { |
| 769 | Diag(DefaultArg.getLocation(), diag::err_template_arg_not_class_template) |
| 770 | << DefaultArg.getSourceRange(); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 771 | return Param; |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 772 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 773 | |
Douglas Gregor | 6f52675 | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 774 | // Check for unexpanded parameter packs. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 775 | if (DiagnoseUnexpandedParameterPack(DefaultArg.getLocation(), |
Douglas Gregor | 6f52675 | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 776 | DefaultArg.getArgument().getAsTemplate(), |
| 777 | UPPC_DefaultArgument)) |
| 778 | return Param; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 779 | |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 780 | Param->setDefaultArgument(DefaultArg, false); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 781 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 782 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 783 | return Param; |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 784 | } |
| 785 | |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 786 | /// ActOnTemplateParameterList - Builds a TemplateParameterList that |
| 787 | /// contains the template parameters in Params/NumParams. |
| 788 | Sema::TemplateParamsTy * |
| 789 | Sema::ActOnTemplateParameterList(unsigned Depth, |
| 790 | SourceLocation ExportLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 791 | SourceLocation TemplateLoc, |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 792 | SourceLocation LAngleLoc, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 793 | Decl **Params, unsigned NumParams, |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 794 | SourceLocation RAngleLoc) { |
| 795 | if (ExportLoc.isValid()) |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 796 | Diag(ExportLoc, diag::warn_template_export_unsupported); |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 797 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 798 | return TemplateParameterList::Create(Context, TemplateLoc, LAngleLoc, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 799 | (NamedDecl**)Params, NumParams, |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 800 | RAngleLoc); |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 801 | } |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 802 | |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 803 | static void SetNestedNameSpecifier(TagDecl *T, const CXXScopeSpec &SS) { |
| 804 | if (SS.isSet()) |
Douglas Gregor | c22b5ff | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 805 | T->setQualifierInfo(SS.getWithLocInContext(T->getASTContext())); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 806 | } |
| 807 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 808 | DeclResult |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 809 | Sema::CheckClassTemplate(Scope *S, unsigned TagSpec, TagUseKind TUK, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 810 | SourceLocation KWLoc, CXXScopeSpec &SS, |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 811 | IdentifierInfo *Name, SourceLocation NameLoc, |
| 812 | AttributeList *Attr, |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 813 | TemplateParameterList *TemplateParams, |
Abramo Bagnara | c57c17d | 2011-03-10 13:28:31 +0000 | [diff] [blame] | 814 | AccessSpecifier AS, |
| 815 | unsigned NumOuterTemplateParamLists, |
| 816 | TemplateParameterList** OuterTemplateParamLists) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 817 | assert(TemplateParams && TemplateParams->size() > 0 && |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 818 | "No template parameters"); |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 819 | assert(TUK != TUK_Reference && "Can only declare or define class templates"); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 820 | bool Invalid = false; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 821 | |
| 822 | // Check that we can declare a template here. |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 823 | if (CheckTemplateDeclScope(S, TemplateParams)) |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 824 | return true; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 825 | |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 826 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
| 827 | assert(Kind != TTK_Enum && "can't build template of enumerated type"); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 828 | |
| 829 | // There is no such thing as an unnamed class template. |
| 830 | if (!Name) { |
| 831 | Diag(KWLoc, diag::err_template_unnamed_class); |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 832 | return true; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 833 | } |
| 834 | |
| 835 | // Find any previous declaration with this name. |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 836 | DeclContext *SemanticContext; |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 837 | LookupResult Previous(*this, Name, NameLoc, LookupOrdinaryName, |
John McCall | 7d384dd | 2009-11-18 07:57:50 +0000 | [diff] [blame] | 838 | ForRedeclaration); |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 839 | if (SS.isNotEmpty() && !SS.isInvalid()) { |
| 840 | SemanticContext = computeDeclContext(SS, true); |
| 841 | if (!SemanticContext) { |
| 842 | // FIXME: Produce a reasonable diagnostic here |
| 843 | return true; |
| 844 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 845 | |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 846 | if (RequireCompleteDeclContext(SS, SemanticContext)) |
| 847 | return true; |
| 848 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 849 | LookupQualifiedName(Previous, SemanticContext); |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 850 | } else { |
| 851 | SemanticContext = CurContext; |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 852 | LookupName(Previous, S); |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 853 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 854 | |
Douglas Gregor | 57265e3 | 2010-04-12 16:00:01 +0000 | [diff] [blame] | 855 | if (Previous.isAmbiguous()) |
| 856 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 857 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 858 | NamedDecl *PrevDecl = 0; |
| 859 | if (Previous.begin() != Previous.end()) |
Douglas Gregor | 57265e3 | 2010-04-12 16:00:01 +0000 | [diff] [blame] | 860 | PrevDecl = (*Previous.begin())->getUnderlyingDecl(); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 861 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 862 | // If there is a previous declaration with the same name, check |
| 863 | // whether this is a valid redeclaration. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 864 | ClassTemplateDecl *PrevClassTemplate |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 865 | = dyn_cast_or_null<ClassTemplateDecl>(PrevDecl); |
Douglas Gregor | d7e5bdb | 2009-10-09 21:11:42 +0000 | [diff] [blame] | 866 | |
| 867 | // We may have found the injected-class-name of a class template, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 868 | // class template partial specialization, or class template specialization. |
Douglas Gregor | d7e5bdb | 2009-10-09 21:11:42 +0000 | [diff] [blame] | 869 | // In these cases, grab the template that is being defined or specialized. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 870 | if (!PrevClassTemplate && PrevDecl && isa<CXXRecordDecl>(PrevDecl) && |
Douglas Gregor | d7e5bdb | 2009-10-09 21:11:42 +0000 | [diff] [blame] | 871 | cast<CXXRecordDecl>(PrevDecl)->isInjectedClassName()) { |
| 872 | PrevDecl = cast<CXXRecordDecl>(PrevDecl->getDeclContext()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 873 | PrevClassTemplate |
Douglas Gregor | d7e5bdb | 2009-10-09 21:11:42 +0000 | [diff] [blame] | 874 | = cast<CXXRecordDecl>(PrevDecl)->getDescribedClassTemplate(); |
| 875 | if (!PrevClassTemplate && isa<ClassTemplateSpecializationDecl>(PrevDecl)) { |
| 876 | PrevClassTemplate |
| 877 | = cast<ClassTemplateSpecializationDecl>(PrevDecl) |
| 878 | ->getSpecializedTemplate(); |
| 879 | } |
| 880 | } |
| 881 | |
John McCall | 65c4946 | 2009-12-18 11:25:59 +0000 | [diff] [blame] | 882 | if (TUK == TUK_Friend) { |
John McCall | e129d44 | 2009-12-17 23:21:11 +0000 | [diff] [blame] | 883 | // C++ [namespace.memdef]p3: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 884 | // [...] When looking for a prior declaration of a class or a function |
| 885 | // declared as a friend, and when the name of the friend class or |
John McCall | e129d44 | 2009-12-17 23:21:11 +0000 | [diff] [blame] | 886 | // function is neither a qualified name nor a template-id, scopes outside |
| 887 | // the innermost enclosing namespace scope are not considered. |
Douglas Gregor | c1c9df7 | 2010-04-18 17:37:40 +0000 | [diff] [blame] | 888 | if (!SS.isSet()) { |
| 889 | DeclContext *OutermostContext = CurContext; |
| 890 | while (!OutermostContext->isFileContext()) |
| 891 | OutermostContext = OutermostContext->getLookupParent(); |
John McCall | 65c4946 | 2009-12-18 11:25:59 +0000 | [diff] [blame] | 892 | |
Douglas Gregor | c1c9df7 | 2010-04-18 17:37:40 +0000 | [diff] [blame] | 893 | if (PrevDecl && |
| 894 | (OutermostContext->Equals(PrevDecl->getDeclContext()) || |
| 895 | OutermostContext->Encloses(PrevDecl->getDeclContext()))) { |
| 896 | SemanticContext = PrevDecl->getDeclContext(); |
| 897 | } else { |
| 898 | // Declarations in outer scopes don't matter. However, the outermost |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 899 | // context we computed is the semantic context for our new |
Douglas Gregor | c1c9df7 | 2010-04-18 17:37:40 +0000 | [diff] [blame] | 900 | // declaration. |
| 901 | PrevDecl = PrevClassTemplate = 0; |
| 902 | SemanticContext = OutermostContext; |
| 903 | } |
John McCall | e129d44 | 2009-12-17 23:21:11 +0000 | [diff] [blame] | 904 | } |
Douglas Gregor | c1c9df7 | 2010-04-18 17:37:40 +0000 | [diff] [blame] | 905 | |
John McCall | e129d44 | 2009-12-17 23:21:11 +0000 | [diff] [blame] | 906 | if (CurContext->isDependentContext()) { |
| 907 | // If this is a dependent context, we don't want to link the friend |
| 908 | // class template to the template in scope, because that would perform |
| 909 | // checking of the template parameter lists that can't be performed |
| 910 | // until the outer context is instantiated. |
| 911 | PrevDecl = PrevClassTemplate = 0; |
| 912 | } |
| 913 | } else if (PrevDecl && !isDeclInScope(PrevDecl, SemanticContext, S)) |
| 914 | PrevDecl = PrevClassTemplate = 0; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 915 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 916 | if (PrevClassTemplate) { |
| 917 | // Ensure that the template parameter lists are compatible. |
| 918 | if (!TemplateParameterListsAreEqual(TemplateParams, |
| 919 | PrevClassTemplate->getTemplateParameters(), |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 920 | /*Complain=*/true, |
| 921 | TPL_TemplateMatch)) |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 922 | return true; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 923 | |
| 924 | // C++ [temp.class]p4: |
| 925 | // In a redeclaration, partial specialization, explicit |
| 926 | // specialization or explicit instantiation of a class template, |
| 927 | // the class-key shall agree in kind with the original class |
| 928 | // template declaration (7.1.5.3). |
| 929 | RecordDecl *PrevRecordDecl = PrevClassTemplate->getTemplatedDecl(); |
Richard Trieu | bbf34c0 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 930 | if (!isAcceptableTagRedeclaration(PrevRecordDecl, Kind, |
| 931 | TUK == TUK_Definition, KWLoc, *Name)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 932 | Diag(KWLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 933 | << Name |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 934 | << FixItHint::CreateReplacement(KWLoc, PrevRecordDecl->getKindName()); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 935 | Diag(PrevRecordDecl->getLocation(), diag::note_previous_use); |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 936 | Kind = PrevRecordDecl->getTagKind(); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 937 | } |
| 938 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 939 | // Check for redefinition of this class template. |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 940 | if (TUK == TUK_Definition) { |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 941 | if (TagDecl *Def = PrevRecordDecl->getDefinition()) { |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 942 | Diag(NameLoc, diag::err_redefinition) << Name; |
| 943 | Diag(Def->getLocation(), diag::note_previous_definition); |
| 944 | // FIXME: Would it make sense to try to "forget" the previous |
| 945 | // definition, as part of error recovery? |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 946 | return true; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 947 | } |
| 948 | } |
| 949 | } else if (PrevDecl && PrevDecl->isTemplateParameter()) { |
| 950 | // Maybe we will complain about the shadowed template parameter. |
| 951 | DiagnoseTemplateParameterShadow(NameLoc, PrevDecl); |
| 952 | // Just pretend that we didn't see the previous declaration. |
| 953 | PrevDecl = 0; |
| 954 | } else if (PrevDecl) { |
| 955 | // C++ [temp]p5: |
| 956 | // A class template shall not have the same name as any other |
| 957 | // template, class, function, object, enumeration, enumerator, |
| 958 | // namespace, or type in the same scope (3.3), except as specified |
| 959 | // in (14.5.4). |
| 960 | Diag(NameLoc, diag::err_redefinition_different_kind) << Name; |
| 961 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 962 | return true; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 963 | } |
| 964 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 965 | // Check the template parameter list of this declaration, possibly |
| 966 | // merging in the template parameter list from the previous class |
| 967 | // template declaration. |
| 968 | if (CheckTemplateParameterList(TemplateParams, |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 969 | PrevClassTemplate? PrevClassTemplate->getTemplateParameters() : 0, |
Douglas Gregor | d89d86f | 2011-02-04 04:20:44 +0000 | [diff] [blame] | 970 | (SS.isSet() && SemanticContext && |
Douglas Gregor | 461bf2e | 2011-02-04 12:22:53 +0000 | [diff] [blame] | 971 | SemanticContext->isRecord() && |
| 972 | SemanticContext->isDependentContext()) |
Douglas Gregor | d89d86f | 2011-02-04 04:20:44 +0000 | [diff] [blame] | 973 | ? TPC_ClassTemplateMember |
| 974 | : TPC_ClassTemplate)) |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 975 | Invalid = true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 976 | |
Douglas Gregor | 57265e3 | 2010-04-12 16:00:01 +0000 | [diff] [blame] | 977 | if (SS.isSet()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 978 | // If the name of the template was qualified, we must be defining the |
Douglas Gregor | 57265e3 | 2010-04-12 16:00:01 +0000 | [diff] [blame] | 979 | // template out-of-line. |
| 980 | if (!SS.isInvalid() && !Invalid && !PrevClassTemplate && |
| 981 | !(TUK == TUK_Friend && CurContext->isDependentContext())) |
| 982 | Diag(NameLoc, diag::err_member_def_does_not_match) |
| 983 | << Name << SemanticContext << SS.getRange(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 984 | } |
| 985 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 986 | CXXRecordDecl *NewClass = |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 987 | CXXRecordDecl::Create(Context, Kind, SemanticContext, KWLoc, NameLoc, Name, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 988 | PrevClassTemplate? |
Douglas Gregor | aafc0cc | 2009-05-15 19:11:46 +0000 | [diff] [blame] | 989 | PrevClassTemplate->getTemplatedDecl() : 0, |
| 990 | /*DelayTypeCreation=*/true); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 991 | SetNestedNameSpecifier(NewClass, SS); |
Abramo Bagnara | c57c17d | 2011-03-10 13:28:31 +0000 | [diff] [blame] | 992 | if (NumOuterTemplateParamLists > 0) |
| 993 | NewClass->setTemplateParameterListsInfo(Context, |
| 994 | NumOuterTemplateParamLists, |
| 995 | OuterTemplateParamLists); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 996 | |
| 997 | ClassTemplateDecl *NewTemplate |
| 998 | = ClassTemplateDecl::Create(Context, SemanticContext, NameLoc, |
| 999 | DeclarationName(Name), TemplateParams, |
Douglas Gregor | 5953d8b | 2009-03-19 17:26:29 +0000 | [diff] [blame] | 1000 | NewClass, PrevClassTemplate); |
Douglas Gregor | befc20e | 2009-03-26 00:10:35 +0000 | [diff] [blame] | 1001 | NewClass->setDescribedClassTemplate(NewTemplate); |
| 1002 | |
Douglas Gregor | aafc0cc | 2009-05-15 19:11:46 +0000 | [diff] [blame] | 1003 | // Build the type for the class template declaration now. |
Douglas Gregor | 24bae92 | 2010-07-08 18:37:38 +0000 | [diff] [blame] | 1004 | QualType T = NewTemplate->getInjectedClassNameSpecialization(); |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 1005 | T = Context.getInjectedClassNameType(NewClass, T); |
Douglas Gregor | aafc0cc | 2009-05-15 19:11:46 +0000 | [diff] [blame] | 1006 | assert(T->isDependentType() && "Class template type is not dependent?"); |
| 1007 | (void)T; |
| 1008 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1009 | // If we are providing an explicit specialization of a member that is a |
Douglas Gregor | fd056bc | 2009-10-13 16:30:37 +0000 | [diff] [blame] | 1010 | // class template, make a note of that. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1011 | if (PrevClassTemplate && |
Douglas Gregor | fd056bc | 2009-10-13 16:30:37 +0000 | [diff] [blame] | 1012 | PrevClassTemplate->getInstantiatedFromMemberTemplate()) |
| 1013 | PrevClassTemplate->setMemberSpecialization(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1014 | |
Anders Carlsson | 4cbe82c | 2009-03-26 01:24:28 +0000 | [diff] [blame] | 1015 | // Set the access specifier. |
Douglas Gregor | d85bea2 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 1016 | if (!Invalid && TUK != TUK_Friend) |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1017 | SetMemberAccessSpecifier(NewTemplate, PrevClassTemplate, AS); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1018 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1019 | // Set the lexical context of these templates |
| 1020 | NewClass->setLexicalDeclContext(CurContext); |
| 1021 | NewTemplate->setLexicalDeclContext(CurContext); |
| 1022 | |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 1023 | if (TUK == TUK_Definition) |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1024 | NewClass->startDefinition(); |
| 1025 | |
| 1026 | if (Attr) |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 1027 | ProcessDeclAttributeList(S, NewClass, Attr); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1028 | |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1029 | if (TUK != TUK_Friend) |
| 1030 | PushOnScopeChains(NewTemplate, S); |
| 1031 | else { |
Douglas Gregor | d85bea2 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 1032 | if (PrevClassTemplate && PrevClassTemplate->getAccess() != AS_none) { |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1033 | NewTemplate->setAccess(PrevClassTemplate->getAccess()); |
Douglas Gregor | d85bea2 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 1034 | NewClass->setAccess(PrevClassTemplate->getAccess()); |
| 1035 | } |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1036 | |
Douglas Gregor | d85bea2 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 1037 | NewTemplate->setObjectOfFriendDecl(/* PreviouslyDeclared = */ |
| 1038 | PrevClassTemplate != NULL); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1039 | |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1040 | // Friend templates are visible in fairly strange ways. |
| 1041 | if (!CurContext->isDependentContext()) { |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 1042 | DeclContext *DC = SemanticContext->getRedeclContext(); |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1043 | DC->makeDeclVisibleInContext(NewTemplate, /* Recoverable = */ false); |
| 1044 | if (Scope *EnclosingScope = getScopeForDeclContext(S, DC)) |
| 1045 | PushOnScopeChains(NewTemplate, EnclosingScope, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1046 | /* AddToContext = */ false); |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1047 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1048 | |
Douglas Gregor | d85bea2 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 1049 | FriendDecl *Friend = FriendDecl::Create(Context, CurContext, |
| 1050 | NewClass->getLocation(), |
| 1051 | NewTemplate, |
| 1052 | /*FIXME:*/NewClass->getLocation()); |
| 1053 | Friend->setAccess(AS_public); |
| 1054 | CurContext->addDecl(Friend); |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1055 | } |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1056 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1057 | if (Invalid) { |
| 1058 | NewTemplate->setInvalidDecl(); |
| 1059 | NewClass->setInvalidDecl(); |
| 1060 | } |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1061 | return NewTemplate; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1062 | } |
| 1063 | |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1064 | /// \brief Diagnose the presence of a default template argument on a |
| 1065 | /// template parameter, which is ill-formed in certain contexts. |
| 1066 | /// |
| 1067 | /// \returns true if the default template argument should be dropped. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1068 | static bool DiagnoseDefaultTemplateArgument(Sema &S, |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1069 | Sema::TemplateParamListContext TPC, |
| 1070 | SourceLocation ParamLoc, |
| 1071 | SourceRange DefArgRange) { |
| 1072 | switch (TPC) { |
| 1073 | case Sema::TPC_ClassTemplate: |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 1074 | case Sema::TPC_TypeAliasTemplate: |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1075 | return false; |
| 1076 | |
| 1077 | case Sema::TPC_FunctionTemplate: |
Douglas Gregor | d89d86f | 2011-02-04 04:20:44 +0000 | [diff] [blame] | 1078 | case Sema::TPC_FriendFunctionTemplateDefinition: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1079 | // C++ [temp.param]p9: |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1080 | // A default template-argument shall not be specified in a |
| 1081 | // function template declaration or a function template |
| 1082 | // definition [...] |
Douglas Gregor | d89d86f | 2011-02-04 04:20:44 +0000 | [diff] [blame] | 1083 | // If a friend function template declaration specifies a default |
| 1084 | // template-argument, that declaration shall be a definition and shall be |
| 1085 | // the only declaration of the function template in the translation unit. |
| 1086 | // (C++98/03 doesn't have this wording; see DR226). |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1087 | if (!S.getLangOptions().CPlusPlus0x) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1088 | S.Diag(ParamLoc, |
Douglas Gregor | ee5d21f | 2011-02-04 03:57:22 +0000 | [diff] [blame] | 1089 | diag::ext_template_parameter_default_in_function_template) |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1090 | << DefArgRange; |
| 1091 | return false; |
| 1092 | |
| 1093 | case Sema::TPC_ClassTemplateMember: |
| 1094 | // C++0x [temp.param]p9: |
| 1095 | // A default template-argument shall not be specified in the |
| 1096 | // template-parameter-lists of the definition of a member of a |
| 1097 | // class template that appears outside of the member's class. |
| 1098 | S.Diag(ParamLoc, diag::err_template_parameter_default_template_member) |
| 1099 | << DefArgRange; |
| 1100 | return true; |
| 1101 | |
| 1102 | case Sema::TPC_FriendFunctionTemplate: |
| 1103 | // C++ [temp.param]p9: |
| 1104 | // A default template-argument shall not be specified in a |
| 1105 | // friend template declaration. |
| 1106 | S.Diag(ParamLoc, diag::err_template_parameter_default_friend_template) |
| 1107 | << DefArgRange; |
| 1108 | return true; |
| 1109 | |
| 1110 | // FIXME: C++0x [temp.param]p9 allows default template-arguments |
| 1111 | // for friend function templates if there is only a single |
| 1112 | // declaration (and it is a definition). Strange! |
| 1113 | } |
| 1114 | |
| 1115 | return false; |
| 1116 | } |
| 1117 | |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1118 | /// \brief Check for unexpanded parameter packs within the template parameters |
| 1119 | /// of a template template parameter, recursively. |
Benjamin Kramer | da57f3e | 2011-03-26 12:38:21 +0000 | [diff] [blame] | 1120 | static bool DiagnoseUnexpandedParameterPacks(Sema &S, |
| 1121 | TemplateTemplateParmDecl *TTP) { |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1122 | TemplateParameterList *Params = TTP->getTemplateParameters(); |
| 1123 | for (unsigned I = 0, N = Params->size(); I != N; ++I) { |
| 1124 | NamedDecl *P = Params->getParam(I); |
| 1125 | if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(P)) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1126 | if (S.DiagnoseUnexpandedParameterPack(NTTP->getLocation(), |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1127 | NTTP->getTypeSourceInfo(), |
| 1128 | Sema::UPPC_NonTypeTemplateParameterType)) |
| 1129 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1130 | |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1131 | continue; |
| 1132 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1133 | |
| 1134 | if (TemplateTemplateParmDecl *InnerTTP |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1135 | = dyn_cast<TemplateTemplateParmDecl>(P)) |
| 1136 | if (DiagnoseUnexpandedParameterPacks(S, InnerTTP)) |
| 1137 | return true; |
| 1138 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1139 | |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1140 | return false; |
| 1141 | } |
| 1142 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1143 | /// \brief Checks the validity of a template parameter list, possibly |
| 1144 | /// considering the template parameter list from a previous |
| 1145 | /// declaration. |
| 1146 | /// |
| 1147 | /// If an "old" template parameter list is provided, it must be |
| 1148 | /// equivalent (per TemplateParameterListsAreEqual) to the "new" |
| 1149 | /// template parameter list. |
| 1150 | /// |
| 1151 | /// \param NewParams Template parameter list for a new template |
| 1152 | /// declaration. This template parameter list will be updated with any |
| 1153 | /// default arguments that are carried through from the previous |
| 1154 | /// template parameter list. |
| 1155 | /// |
| 1156 | /// \param OldParams If provided, template parameter list from a |
| 1157 | /// previous declaration of the same template. Default template |
| 1158 | /// arguments will be merged from the old template parameter list to |
| 1159 | /// the new template parameter list. |
| 1160 | /// |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1161 | /// \param TPC Describes the context in which we are checking the given |
| 1162 | /// template parameter list. |
| 1163 | /// |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1164 | /// \returns true if an error occurred, false otherwise. |
| 1165 | bool Sema::CheckTemplateParameterList(TemplateParameterList *NewParams, |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1166 | TemplateParameterList *OldParams, |
| 1167 | TemplateParamListContext TPC) { |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1168 | bool Invalid = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1169 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1170 | // C++ [temp.param]p10: |
| 1171 | // The set of default template-arguments available for use with a |
| 1172 | // template declaration or definition is obtained by merging the |
| 1173 | // default arguments from the definition (if in scope) and all |
| 1174 | // declarations in scope in the same way default function |
| 1175 | // arguments are (8.3.6). |
| 1176 | bool SawDefaultArgument = false; |
| 1177 | SourceLocation PreviousDefaultArgLoc; |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 1178 | |
Anders Carlsson | 49d2557 | 2009-06-12 23:20:15 +0000 | [diff] [blame] | 1179 | bool SawParameterPack = false; |
| 1180 | SourceLocation ParameterPackLoc; |
| 1181 | |
Mike Stump | 1a35fde | 2009-02-11 23:03:27 +0000 | [diff] [blame] | 1182 | // Dummy initialization to avoid warnings. |
Douglas Gregor | 1bc6913 | 2009-02-11 20:46:19 +0000 | [diff] [blame] | 1183 | TemplateParameterList::iterator OldParam = NewParams->end(); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1184 | if (OldParams) |
| 1185 | OldParam = OldParams->begin(); |
| 1186 | |
Douglas Gregor | fd1a8fd | 2011-01-27 01:40:17 +0000 | [diff] [blame] | 1187 | bool RemoveDefaultArguments = false; |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1188 | for (TemplateParameterList::iterator NewParam = NewParams->begin(), |
| 1189 | NewParamEnd = NewParams->end(); |
| 1190 | NewParam != NewParamEnd; ++NewParam) { |
| 1191 | // Variables used to diagnose redundant default arguments |
| 1192 | bool RedundantDefaultArg = false; |
| 1193 | SourceLocation OldDefaultLoc; |
| 1194 | SourceLocation NewDefaultLoc; |
| 1195 | |
| 1196 | // Variables used to diagnose missing default arguments |
| 1197 | bool MissingDefaultArg = false; |
| 1198 | |
Anders Carlsson | 49d2557 | 2009-06-12 23:20:15 +0000 | [diff] [blame] | 1199 | // C++0x [temp.param]p11: |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 1200 | // If a template parameter of a primary class template or alias template |
| 1201 | // is a template parameter pack, it shall be the last template parameter. |
| 1202 | if (SawParameterPack && |
| 1203 | (TPC == TPC_ClassTemplate || TPC == TPC_TypeAliasTemplate)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1204 | Diag(ParameterPackLoc, |
Anders Carlsson | 49d2557 | 2009-06-12 23:20:15 +0000 | [diff] [blame] | 1205 | diag::err_template_param_pack_must_be_last_template_parameter); |
| 1206 | Invalid = true; |
| 1207 | } |
| 1208 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1209 | if (TemplateTypeParmDecl *NewTypeParm |
| 1210 | = dyn_cast<TemplateTypeParmDecl>(*NewParam)) { |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1211 | // Check the presence of a default argument here. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1212 | if (NewTypeParm->hasDefaultArgument() && |
| 1213 | DiagnoseDefaultTemplateArgument(*this, TPC, |
| 1214 | NewTypeParm->getLocation(), |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1215 | NewTypeParm->getDefaultArgumentInfo()->getTypeLoc() |
Abramo Bagnara | bd054db | 2010-05-20 10:00:11 +0000 | [diff] [blame] | 1216 | .getSourceRange())) |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1217 | NewTypeParm->removeDefaultArgument(); |
| 1218 | |
| 1219 | // Merge default arguments for template type parameters. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1220 | TemplateTypeParmDecl *OldTypeParm |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1221 | = OldParams? cast<TemplateTypeParmDecl>(*OldParam) : 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1222 | |
Anders Carlsson | 49d2557 | 2009-06-12 23:20:15 +0000 | [diff] [blame] | 1223 | if (NewTypeParm->isParameterPack()) { |
| 1224 | assert(!NewTypeParm->hasDefaultArgument() && |
| 1225 | "Parameter packs can't have a default argument!"); |
| 1226 | SawParameterPack = true; |
| 1227 | ParameterPackLoc = NewTypeParm->getLocation(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1228 | } else if (OldTypeParm && OldTypeParm->hasDefaultArgument() && |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1229 | NewTypeParm->hasDefaultArgument()) { |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1230 | OldDefaultLoc = OldTypeParm->getDefaultArgumentLoc(); |
| 1231 | NewDefaultLoc = NewTypeParm->getDefaultArgumentLoc(); |
| 1232 | SawDefaultArgument = true; |
| 1233 | RedundantDefaultArg = true; |
| 1234 | PreviousDefaultArgLoc = NewDefaultLoc; |
| 1235 | } else if (OldTypeParm && OldTypeParm->hasDefaultArgument()) { |
| 1236 | // Merge the default argument from the old declaration to the |
| 1237 | // new declaration. |
| 1238 | SawDefaultArgument = true; |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1239 | NewTypeParm->setDefaultArgument(OldTypeParm->getDefaultArgumentInfo(), |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1240 | true); |
| 1241 | PreviousDefaultArgLoc = OldTypeParm->getDefaultArgumentLoc(); |
| 1242 | } else if (NewTypeParm->hasDefaultArgument()) { |
| 1243 | SawDefaultArgument = true; |
| 1244 | PreviousDefaultArgLoc = NewTypeParm->getDefaultArgumentLoc(); |
| 1245 | } else if (SawDefaultArgument) |
| 1246 | MissingDefaultArg = true; |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1247 | } else if (NonTypeTemplateParmDecl *NewNonTypeParm |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1248 | = dyn_cast<NonTypeTemplateParmDecl>(*NewParam)) { |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1249 | // Check for unexpanded parameter packs. |
| 1250 | if (DiagnoseUnexpandedParameterPack(NewNonTypeParm->getLocation(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1251 | NewNonTypeParm->getTypeSourceInfo(), |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1252 | UPPC_NonTypeTemplateParameterType)) { |
| 1253 | Invalid = true; |
| 1254 | continue; |
| 1255 | } |
| 1256 | |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1257 | // Check the presence of a default argument here. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1258 | if (NewNonTypeParm->hasDefaultArgument() && |
| 1259 | DiagnoseDefaultTemplateArgument(*this, TPC, |
| 1260 | NewNonTypeParm->getLocation(), |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1261 | NewNonTypeParm->getDefaultArgument()->getSourceRange())) { |
Abramo Bagnara | d92f7a2 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 1262 | NewNonTypeParm->removeDefaultArgument(); |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1263 | } |
| 1264 | |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1265 | // Merge default arguments for non-type template parameters |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1266 | NonTypeTemplateParmDecl *OldNonTypeParm |
| 1267 | = OldParams? cast<NonTypeTemplateParmDecl>(*OldParam) : 0; |
Douglas Gregor | 1ed6476 | 2011-01-05 16:19:19 +0000 | [diff] [blame] | 1268 | if (NewNonTypeParm->isParameterPack()) { |
| 1269 | assert(!NewNonTypeParm->hasDefaultArgument() && |
| 1270 | "Parameter packs can't have a default argument!"); |
| 1271 | SawParameterPack = true; |
| 1272 | ParameterPackLoc = NewNonTypeParm->getLocation(); |
| 1273 | } else if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument() && |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1274 | NewNonTypeParm->hasDefaultArgument()) { |
| 1275 | OldDefaultLoc = OldNonTypeParm->getDefaultArgumentLoc(); |
| 1276 | NewDefaultLoc = NewNonTypeParm->getDefaultArgumentLoc(); |
| 1277 | SawDefaultArgument = true; |
| 1278 | RedundantDefaultArg = true; |
| 1279 | PreviousDefaultArgLoc = NewDefaultLoc; |
| 1280 | } else if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument()) { |
| 1281 | // Merge the default argument from the old declaration to the |
| 1282 | // new declaration. |
| 1283 | SawDefaultArgument = true; |
| 1284 | // FIXME: We need to create a new kind of "default argument" |
Douglas Gregor | 61c4d28 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 1285 | // expression that points to a previous non-type template |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1286 | // parameter. |
| 1287 | NewNonTypeParm->setDefaultArgument( |
Abramo Bagnara | d92f7a2 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 1288 | OldNonTypeParm->getDefaultArgument(), |
| 1289 | /*Inherited=*/ true); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1290 | PreviousDefaultArgLoc = OldNonTypeParm->getDefaultArgumentLoc(); |
| 1291 | } else if (NewNonTypeParm->hasDefaultArgument()) { |
| 1292 | SawDefaultArgument = true; |
| 1293 | PreviousDefaultArgLoc = NewNonTypeParm->getDefaultArgumentLoc(); |
| 1294 | } else if (SawDefaultArgument) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1295 | MissingDefaultArg = true; |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1296 | } else { |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1297 | // Check the presence of a default argument here. |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1298 | TemplateTemplateParmDecl *NewTemplateParm |
| 1299 | = cast<TemplateTemplateParmDecl>(*NewParam); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1300 | |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1301 | // Check for unexpanded parameter packs, recursively. |
| 1302 | if (DiagnoseUnexpandedParameterPacks(*this, NewTemplateParm)) { |
| 1303 | Invalid = true; |
| 1304 | continue; |
| 1305 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1306 | |
| 1307 | if (NewTemplateParm->hasDefaultArgument() && |
| 1308 | DiagnoseDefaultTemplateArgument(*this, TPC, |
| 1309 | NewTemplateParm->getLocation(), |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1310 | NewTemplateParm->getDefaultArgument().getSourceRange())) |
Abramo Bagnara | d92f7a2 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 1311 | NewTemplateParm->removeDefaultArgument(); |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1312 | |
| 1313 | // Merge default arguments for template template parameters |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1314 | TemplateTemplateParmDecl *OldTemplateParm |
| 1315 | = OldParams? cast<TemplateTemplateParmDecl>(*OldParam) : 0; |
Douglas Gregor | 1ed6476 | 2011-01-05 16:19:19 +0000 | [diff] [blame] | 1316 | if (NewTemplateParm->isParameterPack()) { |
| 1317 | assert(!NewTemplateParm->hasDefaultArgument() && |
| 1318 | "Parameter packs can't have a default argument!"); |
| 1319 | SawParameterPack = true; |
| 1320 | ParameterPackLoc = NewTemplateParm->getLocation(); |
| 1321 | } else if (OldTemplateParm && OldTemplateParm->hasDefaultArgument() && |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1322 | NewTemplateParm->hasDefaultArgument()) { |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1323 | OldDefaultLoc = OldTemplateParm->getDefaultArgument().getLocation(); |
| 1324 | NewDefaultLoc = NewTemplateParm->getDefaultArgument().getLocation(); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1325 | SawDefaultArgument = true; |
| 1326 | RedundantDefaultArg = true; |
| 1327 | PreviousDefaultArgLoc = NewDefaultLoc; |
| 1328 | } else if (OldTemplateParm && OldTemplateParm->hasDefaultArgument()) { |
| 1329 | // Merge the default argument from the old declaration to the |
| 1330 | // new declaration. |
| 1331 | SawDefaultArgument = true; |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 1332 | // FIXME: We need to create a new kind of "default argument" expression |
| 1333 | // that points to a previous template template parameter. |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1334 | NewTemplateParm->setDefaultArgument( |
Abramo Bagnara | d92f7a2 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 1335 | OldTemplateParm->getDefaultArgument(), |
| 1336 | /*Inherited=*/ true); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1337 | PreviousDefaultArgLoc |
| 1338 | = OldTemplateParm->getDefaultArgument().getLocation(); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1339 | } else if (NewTemplateParm->hasDefaultArgument()) { |
| 1340 | SawDefaultArgument = true; |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1341 | PreviousDefaultArgLoc |
| 1342 | = NewTemplateParm->getDefaultArgument().getLocation(); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1343 | } else if (SawDefaultArgument) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1344 | MissingDefaultArg = true; |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1345 | } |
| 1346 | |
| 1347 | if (RedundantDefaultArg) { |
| 1348 | // C++ [temp.param]p12: |
| 1349 | // A template-parameter shall not be given default arguments |
| 1350 | // by two different declarations in the same scope. |
| 1351 | Diag(NewDefaultLoc, diag::err_template_param_default_arg_redefinition); |
| 1352 | Diag(OldDefaultLoc, diag::note_template_param_prev_default_arg); |
| 1353 | Invalid = true; |
Douglas Gregor | ee5d21f | 2011-02-04 03:57:22 +0000 | [diff] [blame] | 1354 | } else if (MissingDefaultArg && TPC != TPC_FunctionTemplate) { |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1355 | // C++ [temp.param]p11: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1356 | // If a template-parameter of a class template has a default |
| 1357 | // template-argument, each subsequent template-parameter shall either |
Douglas Gregor | b49e415 | 2011-01-05 16:21:17 +0000 | [diff] [blame] | 1358 | // have a default template-argument supplied or be a template parameter |
| 1359 | // pack. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1360 | Diag((*NewParam)->getLocation(), |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1361 | diag::err_template_param_default_arg_missing); |
| 1362 | Diag(PreviousDefaultArgLoc, diag::note_template_param_prev_default_arg); |
| 1363 | Invalid = true; |
Douglas Gregor | fd1a8fd | 2011-01-27 01:40:17 +0000 | [diff] [blame] | 1364 | RemoveDefaultArguments = true; |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1365 | } |
| 1366 | |
| 1367 | // If we have an old template parameter list that we're merging |
| 1368 | // in, move on to the next parameter. |
| 1369 | if (OldParams) |
| 1370 | ++OldParam; |
| 1371 | } |
| 1372 | |
Douglas Gregor | fd1a8fd | 2011-01-27 01:40:17 +0000 | [diff] [blame] | 1373 | // We were missing some default arguments at the end of the list, so remove |
| 1374 | // all of the default arguments. |
| 1375 | if (RemoveDefaultArguments) { |
| 1376 | for (TemplateParameterList::iterator NewParam = NewParams->begin(), |
| 1377 | NewParamEnd = NewParams->end(); |
| 1378 | NewParam != NewParamEnd; ++NewParam) { |
| 1379 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*NewParam)) |
| 1380 | TTP->removeDefaultArgument(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1381 | else if (NonTypeTemplateParmDecl *NTTP |
Douglas Gregor | fd1a8fd | 2011-01-27 01:40:17 +0000 | [diff] [blame] | 1382 | = dyn_cast<NonTypeTemplateParmDecl>(*NewParam)) |
| 1383 | NTTP->removeDefaultArgument(); |
| 1384 | else |
| 1385 | cast<TemplateTemplateParmDecl>(*NewParam)->removeDefaultArgument(); |
| 1386 | } |
| 1387 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1388 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1389 | return Invalid; |
| 1390 | } |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 1391 | |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1392 | namespace { |
| 1393 | |
| 1394 | /// A class which looks for a use of a certain level of template |
| 1395 | /// parameter. |
| 1396 | struct DependencyChecker : RecursiveASTVisitor<DependencyChecker> { |
| 1397 | typedef RecursiveASTVisitor<DependencyChecker> super; |
| 1398 | |
| 1399 | unsigned Depth; |
| 1400 | bool Match; |
| 1401 | |
| 1402 | DependencyChecker(TemplateParameterList *Params) : Match(false) { |
| 1403 | NamedDecl *ND = Params->getParam(0); |
| 1404 | if (TemplateTypeParmDecl *PD = dyn_cast<TemplateTypeParmDecl>(ND)) { |
| 1405 | Depth = PD->getDepth(); |
| 1406 | } else if (NonTypeTemplateParmDecl *PD = |
| 1407 | dyn_cast<NonTypeTemplateParmDecl>(ND)) { |
| 1408 | Depth = PD->getDepth(); |
| 1409 | } else { |
| 1410 | Depth = cast<TemplateTemplateParmDecl>(ND)->getDepth(); |
| 1411 | } |
| 1412 | } |
| 1413 | |
| 1414 | bool Matches(unsigned ParmDepth) { |
| 1415 | if (ParmDepth >= Depth) { |
| 1416 | Match = true; |
| 1417 | return true; |
| 1418 | } |
| 1419 | return false; |
| 1420 | } |
| 1421 | |
| 1422 | bool VisitTemplateTypeParmType(const TemplateTypeParmType *T) { |
| 1423 | return !Matches(T->getDepth()); |
| 1424 | } |
| 1425 | |
| 1426 | bool TraverseTemplateName(TemplateName N) { |
| 1427 | if (TemplateTemplateParmDecl *PD = |
| 1428 | dyn_cast_or_null<TemplateTemplateParmDecl>(N.getAsTemplateDecl())) |
| 1429 | if (Matches(PD->getDepth())) return false; |
| 1430 | return super::TraverseTemplateName(N); |
| 1431 | } |
| 1432 | |
| 1433 | bool VisitDeclRefExpr(DeclRefExpr *E) { |
| 1434 | if (NonTypeTemplateParmDecl *PD = |
| 1435 | dyn_cast<NonTypeTemplateParmDecl>(E->getDecl())) { |
| 1436 | if (PD->getDepth() == Depth) { |
| 1437 | Match = true; |
| 1438 | return false; |
| 1439 | } |
| 1440 | } |
| 1441 | return super::VisitDeclRefExpr(E); |
| 1442 | } |
Douglas Gregor | 18c8339 | 2011-05-13 00:34:01 +0000 | [diff] [blame] | 1443 | |
| 1444 | bool TraverseInjectedClassNameType(const InjectedClassNameType *T) { |
| 1445 | return TraverseType(T->getInjectedSpecializationType()); |
| 1446 | } |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1447 | }; |
| 1448 | } |
| 1449 | |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1450 | /// Determines whether a given type depends on the given parameter |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1451 | /// list. |
| 1452 | static bool |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1453 | DependsOnTemplateParameters(QualType T, TemplateParameterList *Params) { |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1454 | DependencyChecker Checker(Params); |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1455 | Checker.TraverseType(T); |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1456 | return Checker.Match; |
| 1457 | } |
| 1458 | |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1459 | // Find the source range corresponding to the named type in the given |
| 1460 | // nested-name-specifier, if any. |
| 1461 | static SourceRange getRangeOfTypeInNestedNameSpecifier(ASTContext &Context, |
| 1462 | QualType T, |
| 1463 | const CXXScopeSpec &SS) { |
| 1464 | NestedNameSpecifierLoc NNSLoc(SS.getScopeRep(), SS.location_data()); |
| 1465 | while (NestedNameSpecifier *NNS = NNSLoc.getNestedNameSpecifier()) { |
| 1466 | if (const Type *CurType = NNS->getAsType()) { |
| 1467 | if (Context.hasSameUnqualifiedType(T, QualType(CurType, 0))) |
| 1468 | return NNSLoc.getTypeLoc().getSourceRange(); |
| 1469 | } else |
| 1470 | break; |
| 1471 | |
| 1472 | NNSLoc = NNSLoc.getPrefix(); |
| 1473 | } |
| 1474 | |
| 1475 | return SourceRange(); |
| 1476 | } |
| 1477 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1478 | /// \brief Match the given template parameter lists to the given scope |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1479 | /// specifier, returning the template parameter list that applies to the |
| 1480 | /// name. |
| 1481 | /// |
| 1482 | /// \param DeclStartLoc the start of the declaration that has a scope |
| 1483 | /// specifier or a template parameter list. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1484 | /// |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1485 | /// \param DeclLoc The location of the declaration itself. |
| 1486 | /// |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1487 | /// \param SS the scope specifier that will be matched to the given template |
| 1488 | /// parameter lists. This scope specifier precedes a qualified name that is |
| 1489 | /// being declared. |
| 1490 | /// |
| 1491 | /// \param ParamLists the template parameter lists, from the outermost to the |
| 1492 | /// innermost template parameter lists. |
| 1493 | /// |
| 1494 | /// \param NumParamLists the number of template parameter lists in ParamLists. |
| 1495 | /// |
John McCall | 77e8b11 | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 1496 | /// \param IsFriend Whether to apply the slightly different rules for |
| 1497 | /// matching template parameters to scope specifiers in friend |
| 1498 | /// declarations. |
| 1499 | /// |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 1500 | /// \param IsExplicitSpecialization will be set true if the entity being |
| 1501 | /// declared is an explicit specialization, false otherwise. |
| 1502 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1503 | /// \returns the template parameter list, if any, that corresponds to the |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1504 | /// name that is preceded by the scope specifier @p SS. This template |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 1505 | /// parameter list may have template parameters (if we're declaring a |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1506 | /// template) or may have no template parameters (if we're declaring a |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 1507 | /// template specialization), or may be NULL (if what we're declaring isn't |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1508 | /// itself a template). |
| 1509 | TemplateParameterList * |
| 1510 | Sema::MatchTemplateParametersToScopeSpecifier(SourceLocation DeclStartLoc, |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1511 | SourceLocation DeclLoc, |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1512 | const CXXScopeSpec &SS, |
| 1513 | TemplateParameterList **ParamLists, |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 1514 | unsigned NumParamLists, |
John McCall | 77e8b11 | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 1515 | bool IsFriend, |
Douglas Gregor | 0167f3c | 2010-07-14 23:14:12 +0000 | [diff] [blame] | 1516 | bool &IsExplicitSpecialization, |
| 1517 | bool &Invalid) { |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 1518 | IsExplicitSpecialization = false; |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1519 | Invalid = false; |
| 1520 | |
| 1521 | // The sequence of nested types to which we will match up the template |
| 1522 | // parameter lists. We first build this list by starting with the type named |
| 1523 | // by the nested-name-specifier and walking out until we run out of types. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1524 | SmallVector<QualType, 4> NestedTypes; |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1525 | QualType T; |
Douglas Gregor | 714c992 | 2011-05-15 17:27:27 +0000 | [diff] [blame] | 1526 | if (SS.getScopeRep()) { |
| 1527 | if (CXXRecordDecl *Record |
| 1528 | = dyn_cast_or_null<CXXRecordDecl>(computeDeclContext(SS, true))) |
| 1529 | T = Context.getTypeDeclType(Record); |
| 1530 | else |
| 1531 | T = QualType(SS.getScopeRep()->getAsType(), 0); |
| 1532 | } |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1533 | |
| 1534 | // If we found an explicit specialization that prevents us from needing |
| 1535 | // 'template<>' headers, this will be set to the location of that |
| 1536 | // explicit specialization. |
| 1537 | SourceLocation ExplicitSpecLoc; |
| 1538 | |
| 1539 | while (!T.isNull()) { |
| 1540 | NestedTypes.push_back(T); |
| 1541 | |
| 1542 | // Retrieve the parent of a record type. |
| 1543 | if (CXXRecordDecl *Record = T->getAsCXXRecordDecl()) { |
| 1544 | // If this type is an explicit specialization, we're done. |
| 1545 | if (ClassTemplateSpecializationDecl *Spec |
| 1546 | = dyn_cast<ClassTemplateSpecializationDecl>(Record)) { |
| 1547 | if (!isa<ClassTemplatePartialSpecializationDecl>(Spec) && |
| 1548 | Spec->getSpecializationKind() == TSK_ExplicitSpecialization) { |
| 1549 | ExplicitSpecLoc = Spec->getLocation(); |
| 1550 | break; |
Douglas Gregor | 3ebd753 | 2009-11-23 12:11:45 +0000 | [diff] [blame] | 1551 | } |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1552 | } else if (Record->getTemplateSpecializationKind() |
| 1553 | == TSK_ExplicitSpecialization) { |
| 1554 | ExplicitSpecLoc = Record->getLocation(); |
John McCall | 77e8b11 | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 1555 | break; |
| 1556 | } |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1557 | |
| 1558 | if (TypeDecl *Parent = dyn_cast<TypeDecl>(Record->getParent())) |
| 1559 | T = Context.getTypeDeclType(Parent); |
| 1560 | else |
| 1561 | T = QualType(); |
| 1562 | continue; |
| 1563 | } |
| 1564 | |
| 1565 | if (const TemplateSpecializationType *TST |
| 1566 | = T->getAs<TemplateSpecializationType>()) { |
| 1567 | if (TemplateDecl *Template = TST->getTemplateName().getAsTemplateDecl()) { |
| 1568 | if (TypeDecl *Parent = dyn_cast<TypeDecl>(Template->getDeclContext())) |
| 1569 | T = Context.getTypeDeclType(Parent); |
| 1570 | else |
| 1571 | T = QualType(); |
| 1572 | continue; |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1573 | } |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1574 | } |
| 1575 | |
| 1576 | // Look one step prior in a dependent template specialization type. |
| 1577 | if (const DependentTemplateSpecializationType *DependentTST |
| 1578 | = T->getAs<DependentTemplateSpecializationType>()) { |
| 1579 | if (NestedNameSpecifier *NNS = DependentTST->getQualifier()) |
| 1580 | T = QualType(NNS->getAsType(), 0); |
| 1581 | else |
| 1582 | T = QualType(); |
| 1583 | continue; |
| 1584 | } |
| 1585 | |
| 1586 | // Look one step prior in a dependent name type. |
| 1587 | if (const DependentNameType *DependentName = T->getAs<DependentNameType>()){ |
| 1588 | if (NestedNameSpecifier *NNS = DependentName->getQualifier()) |
| 1589 | T = QualType(NNS->getAsType(), 0); |
| 1590 | else |
| 1591 | T = QualType(); |
| 1592 | continue; |
| 1593 | } |
| 1594 | |
| 1595 | // Retrieve the parent of an enumeration type. |
| 1596 | if (const EnumType *EnumT = T->getAs<EnumType>()) { |
| 1597 | // FIXME: Forward-declared enums require a TSK_ExplicitSpecialization |
| 1598 | // check here. |
| 1599 | EnumDecl *Enum = EnumT->getDecl(); |
| 1600 | |
| 1601 | // Get to the parent type. |
| 1602 | if (TypeDecl *Parent = dyn_cast<TypeDecl>(Enum->getParent())) |
| 1603 | T = Context.getTypeDeclType(Parent); |
| 1604 | else |
| 1605 | T = QualType(); |
| 1606 | continue; |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1607 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1608 | |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1609 | T = QualType(); |
| 1610 | } |
| 1611 | // Reverse the nested types list, since we want to traverse from the outermost |
| 1612 | // to the innermost while checking template-parameter-lists. |
| 1613 | std::reverse(NestedTypes.begin(), NestedTypes.end()); |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 1614 | |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1615 | // C++0x [temp.expl.spec]p17: |
| 1616 | // A member or a member template may be nested within many |
| 1617 | // enclosing class templates. In an explicit specialization for |
| 1618 | // such a member, the member declaration shall be preceded by a |
| 1619 | // template<> for each enclosing class template that is |
| 1620 | // explicitly specialized. |
Douglas Gregor | 89b9f10 | 2011-06-06 15:22:55 +0000 | [diff] [blame] | 1621 | bool SawNonEmptyTemplateParameterList = false; |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1622 | unsigned ParamIdx = 0; |
| 1623 | for (unsigned TypeIdx = 0, NumTypes = NestedTypes.size(); TypeIdx != NumTypes; |
| 1624 | ++TypeIdx) { |
| 1625 | T = NestedTypes[TypeIdx]; |
| 1626 | |
| 1627 | // Whether we expect a 'template<>' header. |
| 1628 | bool NeedEmptyTemplateHeader = false; |
| 1629 | |
| 1630 | // Whether we expect a template header with parameters. |
| 1631 | bool NeedNonemptyTemplateHeader = false; |
| 1632 | |
| 1633 | // For a dependent type, the set of template parameters that we |
| 1634 | // expect to see. |
| 1635 | TemplateParameterList *ExpectedTemplateParams = 0; |
| 1636 | |
Douglas Gregor | 175c5bb | 2011-05-11 23:26:17 +0000 | [diff] [blame] | 1637 | // C++0x [temp.expl.spec]p15: |
| 1638 | // A member or a member template may be nested within many enclosing |
| 1639 | // class templates. In an explicit specialization for such a member, the |
| 1640 | // member declaration shall be preceded by a template<> for each |
| 1641 | // enclosing class template that is explicitly specialized. |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1642 | if (CXXRecordDecl *Record = T->getAsCXXRecordDecl()) { |
| 1643 | if (ClassTemplatePartialSpecializationDecl *Partial |
| 1644 | = dyn_cast<ClassTemplatePartialSpecializationDecl>(Record)) { |
| 1645 | ExpectedTemplateParams = Partial->getTemplateParameters(); |
| 1646 | NeedNonemptyTemplateHeader = true; |
| 1647 | } else if (Record->isDependentType()) { |
| 1648 | if (Record->getDescribedClassTemplate()) { |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 1649 | ExpectedTemplateParams = Record->getDescribedClassTemplate() |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1650 | ->getTemplateParameters(); |
| 1651 | NeedNonemptyTemplateHeader = true; |
| 1652 | } |
| 1653 | } else if (ClassTemplateSpecializationDecl *Spec |
| 1654 | = dyn_cast<ClassTemplateSpecializationDecl>(Record)) { |
| 1655 | // C++0x [temp.expl.spec]p4: |
| 1656 | // Members of an explicitly specialized class template are defined |
| 1657 | // in the same manner as members of normal classes, and not using |
| 1658 | // the template<> syntax. |
| 1659 | if (Spec->getSpecializationKind() != TSK_ExplicitSpecialization) |
| 1660 | NeedEmptyTemplateHeader = true; |
| 1661 | else |
Douglas Gregor | 95ea450 | 2011-06-01 22:37:07 +0000 | [diff] [blame] | 1662 | continue; |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1663 | } else if (Record->getTemplateSpecializationKind()) { |
| 1664 | if (Record->getTemplateSpecializationKind() |
Douglas Gregor | 175c5bb | 2011-05-11 23:26:17 +0000 | [diff] [blame] | 1665 | != TSK_ExplicitSpecialization && |
| 1666 | TypeIdx == NumTypes - 1) |
| 1667 | IsExplicitSpecialization = true; |
| 1668 | |
| 1669 | continue; |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1670 | } |
| 1671 | } else if (const TemplateSpecializationType *TST |
| 1672 | = T->getAs<TemplateSpecializationType>()) { |
| 1673 | if (TemplateDecl *Template = TST->getTemplateName().getAsTemplateDecl()) { |
| 1674 | ExpectedTemplateParams = Template->getTemplateParameters(); |
| 1675 | NeedNonemptyTemplateHeader = true; |
| 1676 | } |
| 1677 | } else if (T->getAs<DependentTemplateSpecializationType>()) { |
| 1678 | // FIXME: We actually could/should check the template arguments here |
| 1679 | // against the corresponding template parameter list. |
| 1680 | NeedNonemptyTemplateHeader = false; |
| 1681 | } |
| 1682 | |
Douglas Gregor | 89b9f10 | 2011-06-06 15:22:55 +0000 | [diff] [blame] | 1683 | // C++ [temp.expl.spec]p16: |
| 1684 | // In an explicit specialization declaration for a member of a class |
| 1685 | // template or a member template that ap- pears in namespace scope, the |
| 1686 | // member template and some of its enclosing class templates may remain |
| 1687 | // unspecialized, except that the declaration shall not explicitly |
| 1688 | // specialize a class member template if its en- closing class templates |
| 1689 | // are not explicitly specialized as well. |
| 1690 | if (ParamIdx < NumParamLists) { |
| 1691 | if (ParamLists[ParamIdx]->size() == 0) { |
| 1692 | if (SawNonEmptyTemplateParameterList) { |
| 1693 | Diag(DeclLoc, diag::err_specialize_member_of_template) |
| 1694 | << ParamLists[ParamIdx]->getSourceRange(); |
| 1695 | Invalid = true; |
| 1696 | IsExplicitSpecialization = false; |
| 1697 | return 0; |
| 1698 | } |
| 1699 | } else |
| 1700 | SawNonEmptyTemplateParameterList = true; |
| 1701 | } |
| 1702 | |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1703 | if (NeedEmptyTemplateHeader) { |
| 1704 | // If we're on the last of the types, and we need a 'template<>' header |
| 1705 | // here, then it's an explicit specialization. |
| 1706 | if (TypeIdx == NumTypes - 1) |
| 1707 | IsExplicitSpecialization = true; |
| 1708 | |
| 1709 | if (ParamIdx < NumParamLists) { |
| 1710 | if (ParamLists[ParamIdx]->size() > 0) { |
| 1711 | // The header has template parameters when it shouldn't. Complain. |
| 1712 | Diag(ParamLists[ParamIdx]->getTemplateLoc(), |
| 1713 | diag::err_template_param_list_matches_nontemplate) |
| 1714 | << T |
| 1715 | << SourceRange(ParamLists[ParamIdx]->getLAngleLoc(), |
| 1716 | ParamLists[ParamIdx]->getRAngleLoc()) |
| 1717 | << getRangeOfTypeInNestedNameSpecifier(Context, T, SS); |
| 1718 | Invalid = true; |
| 1719 | return 0; |
| 1720 | } |
| 1721 | |
| 1722 | // Consume this template header. |
| 1723 | ++ParamIdx; |
| 1724 | continue; |
| 1725 | } |
| 1726 | |
| 1727 | if (!IsFriend) { |
| 1728 | // We don't have a template header, but we should. |
| 1729 | SourceLocation ExpectedTemplateLoc; |
| 1730 | if (NumParamLists > 0) |
| 1731 | ExpectedTemplateLoc = ParamLists[0]->getTemplateLoc(); |
| 1732 | else |
| 1733 | ExpectedTemplateLoc = DeclStartLoc; |
| 1734 | |
| 1735 | Diag(DeclLoc, diag::err_template_spec_needs_header) |
| 1736 | << getRangeOfTypeInNestedNameSpecifier(Context, T, SS) |
| 1737 | << FixItHint::CreateInsertion(ExpectedTemplateLoc, "template<> "); |
| 1738 | } |
| 1739 | |
| 1740 | continue; |
| 1741 | } |
| 1742 | |
| 1743 | if (NeedNonemptyTemplateHeader) { |
| 1744 | // In friend declarations we can have template-ids which don't |
| 1745 | // depend on the corresponding template parameter lists. But |
| 1746 | // assume that empty parameter lists are supposed to match this |
| 1747 | // template-id. |
| 1748 | if (IsFriend && T->isDependentType()) { |
| 1749 | if (ParamIdx < NumParamLists && |
| 1750 | DependsOnTemplateParameters(T, ParamLists[ParamIdx])) |
| 1751 | ExpectedTemplateParams = 0; |
| 1752 | else |
| 1753 | continue; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1754 | } |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1755 | |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1756 | if (ParamIdx < NumParamLists) { |
| 1757 | // Check the template parameter list, if we can. |
| 1758 | if (ExpectedTemplateParams && |
| 1759 | !TemplateParameterListsAreEqual(ParamLists[ParamIdx], |
| 1760 | ExpectedTemplateParams, |
| 1761 | true, TPL_TemplateMatch)) |
| 1762 | Invalid = true; |
| 1763 | |
| 1764 | if (!Invalid && |
| 1765 | CheckTemplateParameterList(ParamLists[ParamIdx], 0, |
| 1766 | TPC_ClassTemplateMember)) |
| 1767 | Invalid = true; |
| 1768 | |
| 1769 | ++ParamIdx; |
| 1770 | continue; |
| 1771 | } |
| 1772 | |
| 1773 | Diag(DeclLoc, diag::err_template_spec_needs_template_parameters) |
| 1774 | << T |
| 1775 | << getRangeOfTypeInNestedNameSpecifier(Context, T, SS); |
| 1776 | Invalid = true; |
| 1777 | continue; |
| 1778 | } |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1779 | } |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1780 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1781 | // If there were at least as many template-ids as there were template |
| 1782 | // parameter lists, then there are no template parameter lists remaining for |
| 1783 | // the declaration itself. |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1784 | if (ParamIdx >= NumParamLists) |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1785 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1786 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1787 | // If there were too many template parameter lists, complain about that now. |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1788 | if (ParamIdx < NumParamLists - 1) { |
| 1789 | bool HasAnyExplicitSpecHeader = false; |
| 1790 | bool AllExplicitSpecHeaders = true; |
| 1791 | for (unsigned I = ParamIdx; I != NumParamLists - 1; ++I) { |
| 1792 | if (ParamLists[I]->size() == 0) |
| 1793 | HasAnyExplicitSpecHeader = true; |
| 1794 | else |
| 1795 | AllExplicitSpecHeaders = false; |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1796 | } |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1797 | |
| 1798 | Diag(ParamLists[ParamIdx]->getTemplateLoc(), |
| 1799 | AllExplicitSpecHeaders? diag::warn_template_spec_extra_headers |
| 1800 | : diag::err_template_spec_extra_headers) |
| 1801 | << SourceRange(ParamLists[ParamIdx]->getTemplateLoc(), |
| 1802 | ParamLists[NumParamLists - 2]->getRAngleLoc()); |
| 1803 | |
| 1804 | // If there was a specialization somewhere, such that 'template<>' is |
| 1805 | // not required, and there were any 'template<>' headers, note where the |
| 1806 | // specialization occurred. |
| 1807 | if (ExplicitSpecLoc.isValid() && HasAnyExplicitSpecHeader) |
| 1808 | Diag(ExplicitSpecLoc, |
| 1809 | diag::note_explicit_template_spec_does_not_need_header) |
| 1810 | << NestedTypes.back(); |
| 1811 | |
| 1812 | // We have a template parameter list with no corresponding scope, which |
| 1813 | // means that the resulting template declaration can't be instantiated |
| 1814 | // properly (we'll end up with dependent nodes when we shouldn't). |
| 1815 | if (!AllExplicitSpecHeaders) |
| 1816 | Invalid = true; |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1817 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1818 | |
Douglas Gregor | 89b9f10 | 2011-06-06 15:22:55 +0000 | [diff] [blame] | 1819 | // C++ [temp.expl.spec]p16: |
| 1820 | // In an explicit specialization declaration for a member of a class |
| 1821 | // template or a member template that ap- pears in namespace scope, the |
| 1822 | // member template and some of its enclosing class templates may remain |
| 1823 | // unspecialized, except that the declaration shall not explicitly |
| 1824 | // specialize a class member template if its en- closing class templates |
| 1825 | // are not explicitly specialized as well. |
| 1826 | if (ParamLists[NumParamLists - 1]->size() == 0 && |
| 1827 | SawNonEmptyTemplateParameterList) { |
| 1828 | Diag(DeclLoc, diag::err_specialize_member_of_template) |
| 1829 | << ParamLists[ParamIdx]->getSourceRange(); |
| 1830 | Invalid = true; |
| 1831 | IsExplicitSpecialization = false; |
| 1832 | return 0; |
| 1833 | } |
| 1834 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1835 | // Return the last template parameter list, which corresponds to the |
| 1836 | // entity being declared. |
| 1837 | return ParamLists[NumParamLists - 1]; |
| 1838 | } |
| 1839 | |
Douglas Gregor | 6cd9d4a | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 1840 | void Sema::NoteAllFoundTemplates(TemplateName Name) { |
| 1841 | if (TemplateDecl *Template = Name.getAsTemplateDecl()) { |
| 1842 | Diag(Template->getLocation(), diag::note_template_declared_here) |
| 1843 | << (isa<FunctionTemplateDecl>(Template)? 0 |
| 1844 | : isa<ClassTemplateDecl>(Template)? 1 |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 1845 | : isa<TypeAliasTemplateDecl>(Template)? 2 |
| 1846 | : 3) |
Douglas Gregor | 6cd9d4a | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 1847 | << Template->getDeclName(); |
| 1848 | return; |
| 1849 | } |
| 1850 | |
| 1851 | if (OverloadedTemplateStorage *OST = Name.getAsOverloadedTemplate()) { |
| 1852 | for (OverloadedTemplateStorage::iterator I = OST->begin(), |
| 1853 | IEnd = OST->end(); |
| 1854 | I != IEnd; ++I) |
| 1855 | Diag((*I)->getLocation(), diag::note_template_declared_here) |
| 1856 | << 0 << (*I)->getDeclName(); |
| 1857 | |
| 1858 | return; |
| 1859 | } |
| 1860 | } |
| 1861 | |
| 1862 | |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1863 | QualType Sema::CheckTemplateIdType(TemplateName Name, |
| 1864 | SourceLocation TemplateLoc, |
Douglas Gregor | 6771423 | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 1865 | TemplateArgumentListInfo &TemplateArgs) { |
John McCall | 1460604 | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 1866 | DependentTemplateName *DTN |
| 1867 | = Name.getUnderlying().getAsDependentTemplateName(); |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 1868 | if (DTN && DTN->isIdentifier()) |
| 1869 | // When building a template-id where the template-name is dependent, |
| 1870 | // assume the template is a type template. Either our assumption is |
| 1871 | // correct, or the code is ill-formed and will be diagnosed when the |
| 1872 | // dependent name is substituted. |
| 1873 | return Context.getDependentTemplateSpecializationType(ETK_None, |
| 1874 | DTN->getQualifier(), |
| 1875 | DTN->getIdentifier(), |
| 1876 | TemplateArgs); |
| 1877 | |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1878 | TemplateDecl *Template = Name.getAsTemplateDecl(); |
Douglas Gregor | 6cd9d4a | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 1879 | if (!Template || isa<FunctionTemplateDecl>(Template)) { |
| 1880 | // We might have a substituted template template parameter pack. If so, |
| 1881 | // build a template specialization type for it. |
| 1882 | if (Name.getAsSubstTemplateTemplateParmPack()) |
| 1883 | return Context.getTemplateSpecializationType(Name, TemplateArgs); |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 1884 | |
Douglas Gregor | 6cd9d4a | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 1885 | Diag(TemplateLoc, diag::err_template_id_not_a_type) |
| 1886 | << Name; |
| 1887 | NoteAllFoundTemplates(Name); |
| 1888 | return QualType(); |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 1889 | } |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1890 | |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1891 | // Check that the template argument list is well-formed for this |
| 1892 | // template. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1893 | SmallVector<TemplateArgument, 4> Converted; |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1894 | if (CheckTemplateArgumentList(Template, TemplateLoc, TemplateArgs, |
Douglas Gregor | 16134c6 | 2009-07-01 00:28:38 +0000 | [diff] [blame] | 1895 | false, Converted)) |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1896 | return QualType(); |
| 1897 | |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 1898 | assert((Converted.size() == Template->getTemplateParameters()->size()) && |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1899 | "Converted template argument list is too short!"); |
| 1900 | |
| 1901 | QualType CanonType; |
| 1902 | |
Douglas Gregor | 561f812 | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 1903 | bool InstantiationDependent = false; |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 1904 | if (TypeAliasTemplateDecl *AliasTemplate |
| 1905 | = dyn_cast<TypeAliasTemplateDecl>(Template)) { |
| 1906 | // Find the canonical type for this type alias template specialization. |
| 1907 | TypeAliasDecl *Pattern = AliasTemplate->getTemplatedDecl(); |
| 1908 | if (Pattern->isInvalidDecl()) |
| 1909 | return QualType(); |
| 1910 | |
| 1911 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
| 1912 | Converted.data(), Converted.size()); |
| 1913 | |
| 1914 | // Only substitute for the innermost template argument list. |
| 1915 | MultiLevelTemplateArgumentList TemplateArgLists; |
Richard Smith | 1804174 | 2011-05-14 15:04:18 +0000 | [diff] [blame] | 1916 | TemplateArgLists.addOuterTemplateArguments(&TemplateArgs); |
Richard Smith | aff37b4 | 2011-05-12 00:06:17 +0000 | [diff] [blame] | 1917 | unsigned Depth = AliasTemplate->getTemplateParameters()->getDepth(); |
| 1918 | for (unsigned I = 0; I < Depth; ++I) |
| 1919 | TemplateArgLists.addOuterTemplateArguments(0, 0); |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 1920 | |
| 1921 | InstantiatingTemplate Inst(*this, TemplateLoc, Template); |
| 1922 | CanonType = SubstType(Pattern->getUnderlyingType(), |
| 1923 | TemplateArgLists, AliasTemplate->getLocation(), |
| 1924 | AliasTemplate->getDeclName()); |
| 1925 | if (CanonType.isNull()) |
| 1926 | return QualType(); |
| 1927 | } else if (Name.isDependent() || |
| 1928 | TemplateSpecializationType::anyDependentTemplateArguments( |
Douglas Gregor | 561f812 | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 1929 | TemplateArgs, InstantiationDependent)) { |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1930 | // This class template specialization is a dependent |
| 1931 | // type. Therefore, its canonical type is another class template |
| 1932 | // specialization type that contains all of the converted |
| 1933 | // arguments in canonical form. This ensures that, e.g., A<T> and |
| 1934 | // A<T, T> have identical types when A is declared as: |
| 1935 | // |
| 1936 | // template<typename T, typename U = T> struct A; |
Douglas Gregor | 25a3ef7 | 2009-05-07 06:41:52 +0000 | [diff] [blame] | 1937 | TemplateName CanonName = Context.getCanonicalTemplateName(Name); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1938 | CanonType = Context.getTemplateSpecializationType(CanonName, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 1939 | Converted.data(), |
| 1940 | Converted.size()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1941 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 1942 | // FIXME: CanonType is not actually the canonical type, and unfortunately |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1943 | // it is a TemplateSpecializationType that we will never use again. |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 1944 | // In the future, we need to teach getTemplateSpecializationType to only |
| 1945 | // build the canonical type and return that to us. |
| 1946 | CanonType = Context.getCanonicalType(CanonType); |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 1947 | |
| 1948 | // This might work out to be a current instantiation, in which |
| 1949 | // case the canonical type needs to be the InjectedClassNameType. |
| 1950 | // |
| 1951 | // TODO: in theory this could be a simple hashtable lookup; most |
| 1952 | // changes to CurContext don't change the set of current |
| 1953 | // instantiations. |
| 1954 | if (isa<ClassTemplateDecl>(Template)) { |
| 1955 | for (DeclContext *Ctx = CurContext; Ctx; Ctx = Ctx->getLookupParent()) { |
| 1956 | // If we get out to a namespace, we're done. |
| 1957 | if (Ctx->isFileContext()) break; |
| 1958 | |
| 1959 | // If this isn't a record, keep looking. |
| 1960 | CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Ctx); |
| 1961 | if (!Record) continue; |
| 1962 | |
| 1963 | // Look for one of the two cases with InjectedClassNameTypes |
| 1964 | // and check whether it's the same template. |
| 1965 | if (!isa<ClassTemplatePartialSpecializationDecl>(Record) && |
| 1966 | !Record->getDescribedClassTemplate()) |
| 1967 | continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1968 | |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 1969 | // Fetch the injected class name type and check whether its |
| 1970 | // injected type is equal to the type we just built. |
| 1971 | QualType ICNT = Context.getTypeDeclType(Record); |
| 1972 | QualType Injected = cast<InjectedClassNameType>(ICNT) |
| 1973 | ->getInjectedSpecializationType(); |
| 1974 | |
| 1975 | if (CanonType != Injected->getCanonicalTypeInternal()) |
| 1976 | continue; |
| 1977 | |
| 1978 | // If so, the canonical type of this TST is the injected |
| 1979 | // class name type of the record we just found. |
| 1980 | assert(ICNT.isCanonical()); |
| 1981 | CanonType = ICNT; |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 1982 | break; |
| 1983 | } |
| 1984 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1985 | } else if (ClassTemplateDecl *ClassTemplate |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1986 | = dyn_cast<ClassTemplateDecl>(Template)) { |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1987 | // Find the class template specialization declaration that |
| 1988 | // corresponds to these arguments. |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1989 | void *InsertPos = 0; |
| 1990 | ClassTemplateSpecializationDecl *Decl |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1991 | = ClassTemplate->findSpecialization(Converted.data(), Converted.size(), |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 1992 | InsertPos); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1993 | if (!Decl) { |
| 1994 | // This is the first time we have referenced this class template |
| 1995 | // specialization. Create the canonical declaration and add it to |
| 1996 | // the set of specializations. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1997 | Decl = ClassTemplateSpecializationDecl::Create(Context, |
Douglas Gregor | 13c8577 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 1998 | ClassTemplate->getTemplatedDecl()->getTagKind(), |
| 1999 | ClassTemplate->getDeclContext(), |
| 2000 | ClassTemplate->getLocation(), |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 2001 | ClassTemplate->getLocation(), |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2002 | ClassTemplate, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2003 | Converted.data(), |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2004 | Converted.size(), 0); |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 2005 | ClassTemplate->AddSpecialization(Decl, InsertPos); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2006 | Decl->setLexicalDeclContext(CurContext); |
| 2007 | } |
| 2008 | |
| 2009 | CanonType = Context.getTypeDeclType(Decl); |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 2010 | assert(isa<RecordType>(CanonType) && |
| 2011 | "type of non-dependent specialization is not a RecordType"); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2012 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2013 | |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2014 | // Build the fully-sugared type for this class template |
| 2015 | // specialization, which refers back to the class template |
| 2016 | // specialization we created or found. |
John McCall | 71d74bc | 2010-06-13 09:25:03 +0000 | [diff] [blame] | 2017 | return Context.getTemplateSpecializationType(Name, TemplateArgs, CanonType); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2018 | } |
| 2019 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2020 | TypeResult |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2021 | Sema::ActOnTemplateIdType(CXXScopeSpec &SS, |
| 2022 | TemplateTy TemplateD, SourceLocation TemplateLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2023 | SourceLocation LAngleLoc, |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 2024 | ASTTemplateArgsPtr TemplateArgsIn, |
John McCall | 6b2becf | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 2025 | SourceLocation RAngleLoc) { |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2026 | if (SS.isInvalid()) |
| 2027 | return true; |
| 2028 | |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 2029 | TemplateName Template = TemplateD.getAsVal<TemplateName>(); |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 2030 | |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2031 | // Translate the parser's template argument list in our AST format. |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2032 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
Douglas Gregor | 314b97f | 2009-11-10 19:49:08 +0000 | [diff] [blame] | 2033 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2034 | |
Douglas Gregor | a88f09f | 2011-02-28 17:23:35 +0000 | [diff] [blame] | 2035 | if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) { |
| 2036 | QualType T = Context.getDependentTemplateSpecializationType(ETK_None, |
| 2037 | DTN->getQualifier(), |
| 2038 | DTN->getIdentifier(), |
| 2039 | TemplateArgs); |
| 2040 | |
| 2041 | // Build type-source information. |
| 2042 | TypeLocBuilder TLB; |
| 2043 | DependentTemplateSpecializationTypeLoc SpecTL |
| 2044 | = TLB.push<DependentTemplateSpecializationTypeLoc>(T); |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2045 | SpecTL.setKeywordLoc(SourceLocation()); |
Douglas Gregor | a88f09f | 2011-02-28 17:23:35 +0000 | [diff] [blame] | 2046 | SpecTL.setNameLoc(TemplateLoc); |
| 2047 | SpecTL.setLAngleLoc(LAngleLoc); |
| 2048 | SpecTL.setRAngleLoc(RAngleLoc); |
Douglas Gregor | 94fdffa | 2011-03-01 20:11:18 +0000 | [diff] [blame] | 2049 | SpecTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
Douglas Gregor | a88f09f | 2011-02-28 17:23:35 +0000 | [diff] [blame] | 2050 | for (unsigned I = 0, N = SpecTL.getNumArgs(); I != N; ++I) |
| 2051 | SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo()); |
| 2052 | return CreateParsedType(T, TLB.getTypeSourceInfo(Context, T)); |
| 2053 | } |
| 2054 | |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2055 | QualType Result = CheckTemplateIdType(Template, TemplateLoc, TemplateArgs); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2056 | TemplateArgsIn.release(); |
Douglas Gregor | 31a19b6 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 2057 | |
| 2058 | if (Result.isNull()) |
| 2059 | return true; |
| 2060 | |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2061 | // Build type-source information. |
| 2062 | TypeLocBuilder TLB; |
| 2063 | TemplateSpecializationTypeLoc SpecTL |
| 2064 | = TLB.push<TemplateSpecializationTypeLoc>(Result); |
| 2065 | SpecTL.setTemplateNameLoc(TemplateLoc); |
| 2066 | SpecTL.setLAngleLoc(LAngleLoc); |
| 2067 | SpecTL.setRAngleLoc(RAngleLoc); |
| 2068 | for (unsigned i = 0, e = SpecTL.getNumArgs(); i != e; ++i) |
| 2069 | SpecTL.setArgLocInfo(i, TemplateArgs[i].getLocInfo()); |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2070 | |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2071 | if (SS.isNotEmpty()) { |
| 2072 | // Create an elaborated-type-specifier containing the nested-name-specifier. |
| 2073 | Result = Context.getElaboratedType(ETK_None, SS.getScopeRep(), Result); |
| 2074 | ElaboratedTypeLoc ElabTL = TLB.push<ElaboratedTypeLoc>(Result); |
| 2075 | ElabTL.setKeywordLoc(SourceLocation()); |
| 2076 | ElabTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 2077 | } |
| 2078 | |
| 2079 | return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result)); |
John McCall | 6b2becf | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 2080 | } |
John McCall | f1bbbb4 | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 2081 | |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2082 | TypeResult Sema::ActOnTagTemplateIdType(TagUseKind TUK, |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2083 | TypeSpecifierType TagSpec, |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2084 | SourceLocation TagLoc, |
| 2085 | CXXScopeSpec &SS, |
| 2086 | TemplateTy TemplateD, |
| 2087 | SourceLocation TemplateLoc, |
| 2088 | SourceLocation LAngleLoc, |
| 2089 | ASTTemplateArgsPtr TemplateArgsIn, |
| 2090 | SourceLocation RAngleLoc) { |
| 2091 | TemplateName Template = TemplateD.getAsVal<TemplateName>(); |
| 2092 | |
| 2093 | // Translate the parser's template argument list in our AST format. |
| 2094 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
| 2095 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
| 2096 | |
| 2097 | // Determine the tag kind |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2098 | TagTypeKind TagKind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2099 | ElaboratedTypeKeyword Keyword |
| 2100 | = TypeWithKeyword::getKeywordForTagTypeKind(TagKind); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2101 | |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2102 | if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) { |
| 2103 | QualType T = Context.getDependentTemplateSpecializationType(Keyword, |
| 2104 | DTN->getQualifier(), |
| 2105 | DTN->getIdentifier(), |
| 2106 | TemplateArgs); |
| 2107 | |
| 2108 | // Build type-source information. |
| 2109 | TypeLocBuilder TLB; |
| 2110 | DependentTemplateSpecializationTypeLoc SpecTL |
| 2111 | = TLB.push<DependentTemplateSpecializationTypeLoc>(T); |
| 2112 | SpecTL.setKeywordLoc(TagLoc); |
| 2113 | SpecTL.setNameLoc(TemplateLoc); |
| 2114 | SpecTL.setLAngleLoc(LAngleLoc); |
| 2115 | SpecTL.setRAngleLoc(RAngleLoc); |
| 2116 | SpecTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 2117 | for (unsigned I = 0, N = SpecTL.getNumArgs(); I != N; ++I) |
| 2118 | SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo()); |
| 2119 | return CreateParsedType(T, TLB.getTypeSourceInfo(Context, T)); |
| 2120 | } |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2121 | |
| 2122 | if (TypeAliasTemplateDecl *TAT = |
| 2123 | dyn_cast_or_null<TypeAliasTemplateDecl>(Template.getAsTemplateDecl())) { |
| 2124 | // C++0x [dcl.type.elab]p2: |
| 2125 | // If the identifier resolves to a typedef-name or the simple-template-id |
| 2126 | // resolves to an alias template specialization, the |
| 2127 | // elaborated-type-specifier is ill-formed. |
| 2128 | Diag(TemplateLoc, diag::err_tag_reference_non_tag) << 4; |
| 2129 | Diag(TAT->getLocation(), diag::note_declared_at); |
| 2130 | } |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2131 | |
| 2132 | QualType Result = CheckTemplateIdType(Template, TemplateLoc, TemplateArgs); |
| 2133 | if (Result.isNull()) |
| 2134 | return TypeResult(); |
| 2135 | |
| 2136 | // Check the tag kind |
| 2137 | if (const RecordType *RT = Result->getAs<RecordType>()) { |
John McCall | 6b2becf | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 2138 | RecordDecl *D = RT->getDecl(); |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2139 | |
John McCall | 6b2becf | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 2140 | IdentifierInfo *Id = D->getIdentifier(); |
| 2141 | assert(Id && "templated class must have an identifier"); |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2142 | |
Richard Trieu | bbf34c0 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 2143 | if (!isAcceptableTagRedeclaration(D, TagKind, TUK == TUK_Definition, |
| 2144 | TagLoc, *Id)) { |
John McCall | 6b2becf | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 2145 | Diag(TagLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2146 | << Result |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 2147 | << FixItHint::CreateReplacement(SourceRange(TagLoc), D->getKindName()); |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 2148 | Diag(D->getLocation(), diag::note_previous_use); |
John McCall | f1bbbb4 | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 2149 | } |
| 2150 | } |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2151 | |
| 2152 | // Provide source-location information for the template specialization. |
| 2153 | TypeLocBuilder TLB; |
| 2154 | TemplateSpecializationTypeLoc SpecTL |
| 2155 | = TLB.push<TemplateSpecializationTypeLoc>(Result); |
| 2156 | SpecTL.setTemplateNameLoc(TemplateLoc); |
| 2157 | SpecTL.setLAngleLoc(LAngleLoc); |
| 2158 | SpecTL.setRAngleLoc(RAngleLoc); |
| 2159 | for (unsigned i = 0, e = SpecTL.getNumArgs(); i != e; ++i) |
| 2160 | SpecTL.setArgLocInfo(i, TemplateArgs[i].getLocInfo()); |
John McCall | f1bbbb4 | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 2161 | |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2162 | // Construct an elaborated type containing the nested-name-specifier (if any) |
| 2163 | // and keyword. |
| 2164 | Result = Context.getElaboratedType(Keyword, SS.getScopeRep(), Result); |
| 2165 | ElaboratedTypeLoc ElabTL = TLB.push<ElaboratedTypeLoc>(Result); |
| 2166 | ElabTL.setKeywordLoc(TagLoc); |
| 2167 | ElabTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 2168 | return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result)); |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 2169 | } |
| 2170 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2171 | ExprResult Sema::BuildTemplateIdExpr(const CXXScopeSpec &SS, |
Douglas Gregor | 4c9be89 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 2172 | LookupResult &R, |
| 2173 | bool RequiresADL, |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2174 | const TemplateArgumentListInfo &TemplateArgs) { |
Douglas Gregor | edce4dd | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 2175 | // FIXME: Can we do any checking at this point? I guess we could check the |
| 2176 | // template arguments that we have against the template name, if the template |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2177 | // name refers to a single template. That's not a terribly common case, |
Douglas Gregor | edce4dd | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 2178 | // though. |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 2179 | // foo<int> could identify a single function unambiguously |
| 2180 | // This approach does NOT work, since f<int>(1); |
| 2181 | // gets resolved prior to resorting to overload resolution |
| 2182 | // i.e., template<class T> void f(double); |
| 2183 | // vs template<class T, class U> void f(U); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2184 | |
| 2185 | // These should be filtered out by our callers. |
| 2186 | assert(!R.empty() && "empty lookup results when building templateid"); |
| 2187 | assert(!R.isAmbiguous() && "ambiguous lookup when building templateid"); |
| 2188 | |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 2189 | // We don't want lookup warnings at this point. |
| 2190 | R.suppressDiagnostics(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2191 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2192 | UnresolvedLookupExpr *ULE |
Douglas Gregor | bebbe0d | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 2193 | = UnresolvedLookupExpr::Create(Context, R.getNamingClass(), |
Douglas Gregor | 4c9be89 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 2194 | SS.getWithLocInContext(Context), |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2195 | R.getLookupNameInfo(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2196 | RequiresADL, TemplateArgs, |
Douglas Gregor | 5a84dec | 2010-05-23 18:57:34 +0000 | [diff] [blame] | 2197 | R.begin(), R.end()); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2198 | |
| 2199 | return Owned(ULE); |
Douglas Gregor | edce4dd | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 2200 | } |
| 2201 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2202 | // We actually only call this from template instantiation. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2203 | ExprResult |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 2204 | Sema::BuildQualifiedTemplateIdExpr(CXXScopeSpec &SS, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2205 | const DeclarationNameInfo &NameInfo, |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2206 | const TemplateArgumentListInfo &TemplateArgs) { |
| 2207 | DeclContext *DC; |
| 2208 | if (!(DC = computeDeclContext(SS, false)) || |
| 2209 | DC->isDependentContext() || |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 2210 | RequireCompleteDeclContext(SS, DC)) |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2211 | return BuildDependentDeclRefExpr(SS, NameInfo, &TemplateArgs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2212 | |
Douglas Gregor | 1fd6d44 | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 2213 | bool MemberOfUnknownSpecialization; |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2214 | LookupResult R(*this, NameInfo, LookupOrdinaryName); |
Douglas Gregor | 1fd6d44 | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 2215 | LookupTemplateName(R, (Scope*) 0, SS, QualType(), /*Entering*/ false, |
| 2216 | MemberOfUnknownSpecialization); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2217 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2218 | if (R.isAmbiguous()) |
| 2219 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2220 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2221 | if (R.empty()) { |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2222 | Diag(NameInfo.getLoc(), diag::err_template_kw_refers_to_non_template) |
| 2223 | << NameInfo.getName() << SS.getRange(); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2224 | return ExprError(); |
| 2225 | } |
| 2226 | |
| 2227 | if (ClassTemplateDecl *Temp = R.getAsSingle<ClassTemplateDecl>()) { |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2228 | Diag(NameInfo.getLoc(), diag::err_template_kw_refers_to_class_template) |
| 2229 | << (NestedNameSpecifier*) SS.getScopeRep() |
| 2230 | << NameInfo.getName() << SS.getRange(); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2231 | Diag(Temp->getLocation(), diag::note_referenced_class_template); |
| 2232 | return ExprError(); |
| 2233 | } |
| 2234 | |
| 2235 | return BuildTemplateIdExpr(SS, R, /* ADL */ false, TemplateArgs); |
Douglas Gregor | edce4dd | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 2236 | } |
| 2237 | |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 2238 | /// \brief Form a dependent template name. |
| 2239 | /// |
| 2240 | /// This action forms a dependent template name given the template |
| 2241 | /// name and its (presumably dependent) scope specifier. For |
| 2242 | /// example, given "MetaFun::template apply", the scope specifier \p |
| 2243 | /// SS will be "MetaFun::", \p TemplateKWLoc contains the location |
| 2244 | /// of the "template" keyword, and "apply" is the \p Name. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2245 | TemplateNameKind Sema::ActOnDependentTemplateName(Scope *S, |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2246 | SourceLocation TemplateKWLoc, |
| 2247 | CXXScopeSpec &SS, |
| 2248 | UnqualifiedId &Name, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2249 | ParsedType ObjectType, |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2250 | bool EnteringContext, |
| 2251 | TemplateTy &Result) { |
Douglas Gregor | 1a15dae | 2010-06-16 22:31:08 +0000 | [diff] [blame] | 2252 | if (TemplateKWLoc.isValid() && S && !S->getTemplateParamParent() && |
| 2253 | !getLangOptions().CPlusPlus0x) |
| 2254 | Diag(TemplateKWLoc, diag::ext_template_outside_of_template) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2255 | << FixItHint::CreateRemoval(TemplateKWLoc); |
| 2256 | |
Douglas Gregor | 0707bc5 | 2010-01-19 16:01:07 +0000 | [diff] [blame] | 2257 | DeclContext *LookupCtx = 0; |
| 2258 | if (SS.isSet()) |
| 2259 | LookupCtx = computeDeclContext(SS, EnteringContext); |
| 2260 | if (!LookupCtx && ObjectType) |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2261 | LookupCtx = computeDeclContext(ObjectType.get()); |
Douglas Gregor | 0707bc5 | 2010-01-19 16:01:07 +0000 | [diff] [blame] | 2262 | if (LookupCtx) { |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 2263 | // C++0x [temp.names]p5: |
| 2264 | // If a name prefixed by the keyword template is not the name of |
| 2265 | // a template, the program is ill-formed. [Note: the keyword |
| 2266 | // template may not be applied to non-template members of class |
| 2267 | // templates. -end note ] [ Note: as is the case with the |
| 2268 | // typename prefix, the template prefix is allowed in cases |
| 2269 | // where it is not strictly necessary; i.e., when the |
| 2270 | // nested-name-specifier or the expression on the left of the -> |
| 2271 | // or . is not dependent on a template-parameter, or the use |
| 2272 | // does not appear in the scope of a template. -end note] |
| 2273 | // |
| 2274 | // Note: C++03 was more strict here, because it banned the use of |
| 2275 | // the "template" keyword prior to a template-name that was not a |
| 2276 | // dependent name. C++ DR468 relaxed this requirement (the |
| 2277 | // "template" keyword is now permitted). We follow the C++0x |
Douglas Gregor | 732281d | 2010-06-14 22:07:54 +0000 | [diff] [blame] | 2278 | // rules, even in C++03 mode with a warning, retroactively applying the DR. |
Douglas Gregor | 1fd6d44 | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 2279 | bool MemberOfUnknownSpecialization; |
Abramo Bagnara | 7c15353 | 2010-08-06 12:11:11 +0000 | [diff] [blame] | 2280 | TemplateNameKind TNK = isTemplateName(0, SS, TemplateKWLoc.isValid(), Name, |
| 2281 | ObjectType, EnteringContext, Result, |
Douglas Gregor | 1fd6d44 | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 2282 | MemberOfUnknownSpecialization); |
Douglas Gregor | 0707bc5 | 2010-01-19 16:01:07 +0000 | [diff] [blame] | 2283 | if (TNK == TNK_Non_template && LookupCtx->isDependentContext() && |
| 2284 | isa<CXXRecordDecl>(LookupCtx) && |
Douglas Gregor | d078bd2 | 2011-03-11 23:27:41 +0000 | [diff] [blame] | 2285 | (!cast<CXXRecordDecl>(LookupCtx)->hasDefinition() || |
| 2286 | cast<CXXRecordDecl>(LookupCtx)->hasAnyDependentBases())) { |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2287 | // This is a dependent template. Handle it below. |
Douglas Gregor | 9edad9b | 2010-01-14 17:47:39 +0000 | [diff] [blame] | 2288 | } else if (TNK == TNK_Non_template) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2289 | Diag(Name.getSourceRange().getBegin(), |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 2290 | diag::err_template_kw_refers_to_non_template) |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2291 | << GetNameFromUnqualifiedId(Name).getName() |
Douglas Gregor | 0278e12 | 2010-05-05 05:58:24 +0000 | [diff] [blame] | 2292 | << Name.getSourceRange() |
| 2293 | << TemplateKWLoc; |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2294 | return TNK_Non_template; |
Douglas Gregor | 9edad9b | 2010-01-14 17:47:39 +0000 | [diff] [blame] | 2295 | } else { |
| 2296 | // We found something; return it. |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2297 | return TNK; |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 2298 | } |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 2299 | } |
| 2300 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2301 | NestedNameSpecifier *Qualifier |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 2302 | = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2303 | |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 2304 | switch (Name.getKind()) { |
| 2305 | case UnqualifiedId::IK_Identifier: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2306 | Result = TemplateTy::make(Context.getDependentTemplateName(Qualifier, |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2307 | Name.Identifier)); |
| 2308 | return TNK_Dependent_template_name; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2309 | |
Douglas Gregor | ca1bdd7 | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 2310 | case UnqualifiedId::IK_OperatorFunctionId: |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2311 | Result = TemplateTy::make(Context.getDependentTemplateName(Qualifier, |
Douglas Gregor | ca1bdd7 | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 2312 | Name.OperatorFunctionId.Operator)); |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2313 | return TNK_Dependent_template_name; |
Sean Hunt | e6252d1 | 2009-11-28 08:58:14 +0000 | [diff] [blame] | 2314 | |
| 2315 | case UnqualifiedId::IK_LiteralOperatorId: |
| 2316 | assert(false && "We don't support these; Parse shouldn't have allowed propagation"); |
| 2317 | |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 2318 | default: |
| 2319 | break; |
| 2320 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2321 | |
| 2322 | Diag(Name.getSourceRange().getBegin(), |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 2323 | diag::err_template_kw_refers_to_non_template) |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2324 | << GetNameFromUnqualifiedId(Name).getName() |
Douglas Gregor | 0278e12 | 2010-05-05 05:58:24 +0000 | [diff] [blame] | 2325 | << Name.getSourceRange() |
| 2326 | << TemplateKWLoc; |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2327 | return TNK_Non_template; |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 2328 | } |
| 2329 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2330 | bool Sema::CheckTemplateTypeArgument(TemplateTypeParmDecl *Param, |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2331 | const TemplateArgumentLoc &AL, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2332 | SmallVectorImpl<TemplateArgument> &Converted) { |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2333 | const TemplateArgument &Arg = AL.getArgument(); |
| 2334 | |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 2335 | // Check template type parameter. |
Jeffrey Yasskin | db88d8a | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 2336 | switch(Arg.getKind()) { |
| 2337 | case TemplateArgument::Type: |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 2338 | // C++ [temp.arg.type]p1: |
| 2339 | // A template-argument for a template-parameter which is a |
| 2340 | // type shall be a type-id. |
Jeffrey Yasskin | db88d8a | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 2341 | break; |
| 2342 | case TemplateArgument::Template: { |
| 2343 | // We have a template type parameter but the template argument |
| 2344 | // is a template without any arguments. |
| 2345 | SourceRange SR = AL.getSourceRange(); |
| 2346 | TemplateName Name = Arg.getAsTemplate(); |
| 2347 | Diag(SR.getBegin(), diag::err_template_missing_args) |
| 2348 | << Name << SR; |
| 2349 | if (TemplateDecl *Decl = Name.getAsTemplateDecl()) |
| 2350 | Diag(Decl->getLocation(), diag::note_template_decl_here); |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 2351 | |
Jeffrey Yasskin | db88d8a | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 2352 | return true; |
| 2353 | } |
| 2354 | default: { |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 2355 | // We have a template type parameter but the template argument |
| 2356 | // is not a type. |
John McCall | 828bff2 | 2009-10-29 18:45:58 +0000 | [diff] [blame] | 2357 | SourceRange SR = AL.getSourceRange(); |
| 2358 | Diag(SR.getBegin(), diag::err_template_arg_must_be_type) << SR; |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 2359 | Diag(Param->getLocation(), diag::note_template_param_here); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2360 | |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 2361 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2362 | } |
Jeffrey Yasskin | db88d8a | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 2363 | } |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 2364 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2365 | if (CheckTemplateArgument(Param, AL.getTypeSourceInfo())) |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 2366 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2367 | |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 2368 | // Add the converted template type argument. |
Douglas Gregor | e559ca1 | 2011-06-17 22:11:49 +0000 | [diff] [blame] | 2369 | QualType ArgType = Context.getCanonicalType(Arg.getAsType()); |
| 2370 | |
| 2371 | // Objective-C ARC: |
| 2372 | // If an explicitly-specified template argument type is a lifetime type |
| 2373 | // with no lifetime qualifier, the __strong lifetime qualifier is inferred. |
| 2374 | if (getLangOptions().ObjCAutoRefCount && |
| 2375 | ArgType->isObjCLifetimeType() && |
| 2376 | !ArgType.getObjCLifetime()) { |
| 2377 | Qualifiers Qs; |
| 2378 | Qs.setObjCLifetime(Qualifiers::OCL_Strong); |
| 2379 | ArgType = Context.getQualifiedType(ArgType, Qs); |
| 2380 | } |
| 2381 | |
| 2382 | Converted.push_back(TemplateArgument(ArgType)); |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 2383 | return false; |
| 2384 | } |
| 2385 | |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2386 | /// \brief Substitute template arguments into the default template argument for |
| 2387 | /// the given template type parameter. |
| 2388 | /// |
| 2389 | /// \param SemaRef the semantic analysis object for which we are performing |
| 2390 | /// the substitution. |
| 2391 | /// |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2392 | /// \param Template the template that we are synthesizing template arguments |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2393 | /// for. |
| 2394 | /// |
| 2395 | /// \param TemplateLoc the location of the template name that started the |
| 2396 | /// template-id we are checking. |
| 2397 | /// |
| 2398 | /// \param RAngleLoc the location of the right angle bracket ('>') that |
| 2399 | /// terminates the template-id. |
| 2400 | /// |
| 2401 | /// \param Param the template template parameter whose default we are |
| 2402 | /// substituting into. |
| 2403 | /// |
| 2404 | /// \param Converted the list of template arguments provided for template |
| 2405 | /// parameters that precede \p Param in the template parameter list. |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2406 | /// \returns the substituted template argument, or NULL if an error occurred. |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2407 | static TypeSourceInfo * |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2408 | SubstDefaultTemplateArgument(Sema &SemaRef, |
| 2409 | TemplateDecl *Template, |
| 2410 | SourceLocation TemplateLoc, |
| 2411 | SourceLocation RAngleLoc, |
| 2412 | TemplateTypeParmDecl *Param, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2413 | SmallVectorImpl<TemplateArgument> &Converted) { |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2414 | TypeSourceInfo *ArgType = Param->getDefaultArgumentInfo(); |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2415 | |
| 2416 | // If the argument type is dependent, instantiate it now based |
| 2417 | // on the previously-computed template arguments. |
| 2418 | if (ArgType->getType()->isDependentType()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2419 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2420 | Converted.data(), Converted.size()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2421 | |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2422 | MultiLevelTemplateArgumentList AllTemplateArgs |
| 2423 | = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs); |
| 2424 | |
| 2425 | Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2426 | Template, Converted.data(), |
| 2427 | Converted.size(), |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2428 | SourceRange(TemplateLoc, RAngleLoc)); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2429 | |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2430 | ArgType = SemaRef.SubstType(ArgType, AllTemplateArgs, |
| 2431 | Param->getDefaultArgumentLoc(), |
| 2432 | Param->getDeclName()); |
| 2433 | } |
| 2434 | |
| 2435 | return ArgType; |
| 2436 | } |
| 2437 | |
| 2438 | /// \brief Substitute template arguments into the default template argument for |
| 2439 | /// the given non-type template parameter. |
| 2440 | /// |
| 2441 | /// \param SemaRef the semantic analysis object for which we are performing |
| 2442 | /// the substitution. |
| 2443 | /// |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2444 | /// \param Template the template that we are synthesizing template arguments |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2445 | /// for. |
| 2446 | /// |
| 2447 | /// \param TemplateLoc the location of the template name that started the |
| 2448 | /// template-id we are checking. |
| 2449 | /// |
| 2450 | /// \param RAngleLoc the location of the right angle bracket ('>') that |
| 2451 | /// terminates the template-id. |
| 2452 | /// |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2453 | /// \param Param the non-type template parameter whose default we are |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2454 | /// substituting into. |
| 2455 | /// |
| 2456 | /// \param Converted the list of template arguments provided for template |
| 2457 | /// parameters that precede \p Param in the template parameter list. |
| 2458 | /// |
| 2459 | /// \returns the substituted template argument, or NULL if an error occurred. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2460 | static ExprResult |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2461 | SubstDefaultTemplateArgument(Sema &SemaRef, |
| 2462 | TemplateDecl *Template, |
| 2463 | SourceLocation TemplateLoc, |
| 2464 | SourceLocation RAngleLoc, |
| 2465 | NonTypeTemplateParmDecl *Param, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2466 | SmallVectorImpl<TemplateArgument> &Converted) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2467 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2468 | Converted.data(), Converted.size()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2469 | |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2470 | MultiLevelTemplateArgumentList AllTemplateArgs |
| 2471 | = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2472 | |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2473 | Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2474 | Template, Converted.data(), |
| 2475 | Converted.size(), |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2476 | SourceRange(TemplateLoc, RAngleLoc)); |
| 2477 | |
| 2478 | return SemaRef.SubstExpr(Param->getDefaultArgument(), AllTemplateArgs); |
| 2479 | } |
| 2480 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2481 | /// \brief Substitute template arguments into the default template argument for |
| 2482 | /// the given template template parameter. |
| 2483 | /// |
| 2484 | /// \param SemaRef the semantic analysis object for which we are performing |
| 2485 | /// the substitution. |
| 2486 | /// |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2487 | /// \param Template the template that we are synthesizing template arguments |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2488 | /// for. |
| 2489 | /// |
| 2490 | /// \param TemplateLoc the location of the template name that started the |
| 2491 | /// template-id we are checking. |
| 2492 | /// |
| 2493 | /// \param RAngleLoc the location of the right angle bracket ('>') that |
| 2494 | /// terminates the template-id. |
| 2495 | /// |
| 2496 | /// \param Param the template template parameter whose default we are |
| 2497 | /// substituting into. |
| 2498 | /// |
| 2499 | /// \param Converted the list of template arguments provided for template |
| 2500 | /// parameters that precede \p Param in the template parameter list. |
| 2501 | /// |
Douglas Gregor | 1d752d7 | 2011-03-02 18:46:51 +0000 | [diff] [blame] | 2502 | /// \param QualifierLoc Will be set to the nested-name-specifier (with |
| 2503 | /// source-location information) that precedes the template name. |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2504 | /// |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2505 | /// \returns the substituted template argument, or NULL if an error occurred. |
| 2506 | static TemplateName |
| 2507 | SubstDefaultTemplateArgument(Sema &SemaRef, |
| 2508 | TemplateDecl *Template, |
| 2509 | SourceLocation TemplateLoc, |
| 2510 | SourceLocation RAngleLoc, |
| 2511 | TemplateTemplateParmDecl *Param, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2512 | SmallVectorImpl<TemplateArgument> &Converted, |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2513 | NestedNameSpecifierLoc &QualifierLoc) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2514 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2515 | Converted.data(), Converted.size()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2516 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2517 | MultiLevelTemplateArgumentList AllTemplateArgs |
| 2518 | = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2519 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2520 | Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2521 | Template, Converted.data(), |
| 2522 | Converted.size(), |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2523 | SourceRange(TemplateLoc, RAngleLoc)); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2524 | |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2525 | // Substitute into the nested-name-specifier first, |
Douglas Gregor | 1d752d7 | 2011-03-02 18:46:51 +0000 | [diff] [blame] | 2526 | QualifierLoc = Param->getDefaultArgument().getTemplateQualifierLoc(); |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2527 | if (QualifierLoc) { |
| 2528 | QualifierLoc = SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc, |
| 2529 | AllTemplateArgs); |
| 2530 | if (!QualifierLoc) |
| 2531 | return TemplateName(); |
| 2532 | } |
| 2533 | |
Douglas Gregor | 1d752d7 | 2011-03-02 18:46:51 +0000 | [diff] [blame] | 2534 | return SemaRef.SubstTemplateName(QualifierLoc, |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2535 | Param->getDefaultArgument().getArgument().getAsTemplate(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2536 | Param->getDefaultArgument().getTemplateNameLoc(), |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2537 | AllTemplateArgs); |
| 2538 | } |
| 2539 | |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2540 | /// \brief If the given template parameter has a default template |
| 2541 | /// argument, substitute into that default template argument and |
| 2542 | /// return the corresponding template argument. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2543 | TemplateArgumentLoc |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2544 | Sema::SubstDefaultTemplateArgumentIfAvailable(TemplateDecl *Template, |
| 2545 | SourceLocation TemplateLoc, |
| 2546 | SourceLocation RAngleLoc, |
| 2547 | Decl *Param, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2548 | SmallVectorImpl<TemplateArgument> &Converted) { |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2549 | if (TemplateTypeParmDecl *TypeParm = dyn_cast<TemplateTypeParmDecl>(Param)) { |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2550 | if (!TypeParm->hasDefaultArgument()) |
| 2551 | return TemplateArgumentLoc(); |
| 2552 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2553 | TypeSourceInfo *DI = SubstDefaultTemplateArgument(*this, Template, |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2554 | TemplateLoc, |
| 2555 | RAngleLoc, |
| 2556 | TypeParm, |
| 2557 | Converted); |
| 2558 | if (DI) |
| 2559 | return TemplateArgumentLoc(TemplateArgument(DI->getType()), DI); |
| 2560 | |
| 2561 | return TemplateArgumentLoc(); |
| 2562 | } |
| 2563 | |
| 2564 | if (NonTypeTemplateParmDecl *NonTypeParm |
| 2565 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
| 2566 | if (!NonTypeParm->hasDefaultArgument()) |
| 2567 | return TemplateArgumentLoc(); |
| 2568 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2569 | ExprResult Arg = SubstDefaultTemplateArgument(*this, Template, |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2570 | TemplateLoc, |
| 2571 | RAngleLoc, |
| 2572 | NonTypeParm, |
| 2573 | Converted); |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2574 | if (Arg.isInvalid()) |
| 2575 | return TemplateArgumentLoc(); |
| 2576 | |
| 2577 | Expr *ArgE = Arg.takeAs<Expr>(); |
| 2578 | return TemplateArgumentLoc(TemplateArgument(ArgE), ArgE); |
| 2579 | } |
| 2580 | |
| 2581 | TemplateTemplateParmDecl *TempTempParm |
| 2582 | = cast<TemplateTemplateParmDecl>(Param); |
| 2583 | if (!TempTempParm->hasDefaultArgument()) |
| 2584 | return TemplateArgumentLoc(); |
| 2585 | |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2586 | |
Douglas Gregor | 1d752d7 | 2011-03-02 18:46:51 +0000 | [diff] [blame] | 2587 | NestedNameSpecifierLoc QualifierLoc; |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2588 | TemplateName TName = SubstDefaultTemplateArgument(*this, Template, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2589 | TemplateLoc, |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2590 | RAngleLoc, |
| 2591 | TempTempParm, |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2592 | Converted, |
| 2593 | QualifierLoc); |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2594 | if (TName.isNull()) |
| 2595 | return TemplateArgumentLoc(); |
| 2596 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2597 | return TemplateArgumentLoc(TemplateArgument(TName), |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2598 | TempTempParm->getDefaultArgument().getTemplateQualifierLoc(), |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2599 | TempTempParm->getDefaultArgument().getTemplateNameLoc()); |
| 2600 | } |
| 2601 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2602 | /// \brief Check that the given template argument corresponds to the given |
| 2603 | /// template parameter. |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2604 | /// |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2605 | /// \param Param The template parameter against which the argument will be |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2606 | /// checked. |
| 2607 | /// |
| 2608 | /// \param Arg The template argument. |
| 2609 | /// |
| 2610 | /// \param Template The template in which the template argument resides. |
| 2611 | /// |
| 2612 | /// \param TemplateLoc The location of the template name for the template |
| 2613 | /// whose argument list we're matching. |
| 2614 | /// |
| 2615 | /// \param RAngleLoc The location of the right angle bracket ('>') that closes |
| 2616 | /// the template argument list. |
| 2617 | /// |
| 2618 | /// \param ArgumentPackIndex The index into the argument pack where this |
| 2619 | /// argument will be placed. Only valid if the parameter is a parameter pack. |
| 2620 | /// |
| 2621 | /// \param Converted The checked, converted argument will be added to the |
| 2622 | /// end of this small vector. |
| 2623 | /// |
| 2624 | /// \param CTAK Describes how we arrived at this particular template argument: |
| 2625 | /// explicitly written, deduced, etc. |
| 2626 | /// |
| 2627 | /// \returns true on error, false otherwise. |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2628 | bool Sema::CheckTemplateArgument(NamedDecl *Param, |
| 2629 | const TemplateArgumentLoc &Arg, |
Douglas Gregor | 54c53cc | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2630 | NamedDecl *Template, |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2631 | SourceLocation TemplateLoc, |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2632 | SourceLocation RAngleLoc, |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2633 | unsigned ArgumentPackIndex, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2634 | SmallVectorImpl<TemplateArgument> &Converted, |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 2635 | CheckTemplateArgumentKind CTAK) { |
Douglas Gregor | d9e1530 | 2009-11-11 19:41:09 +0000 | [diff] [blame] | 2636 | // Check template type parameters. |
| 2637 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2638 | return CheckTemplateTypeArgument(TTP, Arg, Converted); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2639 | |
Douglas Gregor | d9e1530 | 2009-11-11 19:41:09 +0000 | [diff] [blame] | 2640 | // Check non-type template parameters. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2641 | if (NonTypeTemplateParmDecl *NTTP =dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2642 | // Do substitution on the type of the non-type template parameter |
Peter Collingbourne | 9f6f6a1 | 2010-12-10 17:08:53 +0000 | [diff] [blame] | 2643 | // with the template arguments we've seen thus far. But if the |
| 2644 | // template has a dependent context then we cannot substitute yet. |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2645 | QualType NTTPType = NTTP->getType(); |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2646 | if (NTTP->isParameterPack() && NTTP->isExpandedParameterPack()) |
| 2647 | NTTPType = NTTP->getExpansionType(ArgumentPackIndex); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2648 | |
Peter Collingbourne | 9f6f6a1 | 2010-12-10 17:08:53 +0000 | [diff] [blame] | 2649 | if (NTTPType->isDependentType() && |
| 2650 | !isa<TemplateTemplateParmDecl>(Template) && |
| 2651 | !Template->getDeclContext()->isDependentContext()) { |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2652 | // Do substitution on the type of the non-type template parameter. |
| 2653 | InstantiatingTemplate Inst(*this, TemplateLoc, Template, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2654 | NTTP, Converted.data(), Converted.size(), |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2655 | SourceRange(TemplateLoc, RAngleLoc)); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2656 | |
| 2657 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2658 | Converted.data(), Converted.size()); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2659 | NTTPType = SubstType(NTTPType, |
| 2660 | MultiLevelTemplateArgumentList(TemplateArgs), |
| 2661 | NTTP->getLocation(), |
| 2662 | NTTP->getDeclName()); |
| 2663 | // If that worked, check the non-type template parameter type |
| 2664 | // for validity. |
| 2665 | if (!NTTPType.isNull()) |
| 2666 | NTTPType = CheckNonTypeTemplateParameterType(NTTPType, |
| 2667 | NTTP->getLocation()); |
| 2668 | if (NTTPType.isNull()) |
| 2669 | return true; |
| 2670 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2671 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2672 | switch (Arg.getArgument().getKind()) { |
| 2673 | case TemplateArgument::Null: |
| 2674 | assert(false && "Should never see a NULL template argument here"); |
| 2675 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2676 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2677 | case TemplateArgument::Expression: { |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2678 | TemplateArgument Result; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2679 | ExprResult Res = |
| 2680 | CheckTemplateArgument(NTTP, NTTPType, Arg.getArgument().getAsExpr(), |
| 2681 | Result, CTAK); |
| 2682 | if (Res.isInvalid()) |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2683 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2684 | |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2685 | Converted.push_back(Result); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2686 | break; |
| 2687 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2688 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2689 | case TemplateArgument::Declaration: |
| 2690 | case TemplateArgument::Integral: |
| 2691 | // We've already checked this template argument, so just copy |
| 2692 | // it to the list of converted arguments. |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2693 | Converted.push_back(Arg.getArgument()); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2694 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2695 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2696 | case TemplateArgument::Template: |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 2697 | case TemplateArgument::TemplateExpansion: |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2698 | // We were given a template template argument. It may not be ill-formed; |
| 2699 | // see below. |
| 2700 | if (DependentTemplateName *DTN |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 2701 | = Arg.getArgument().getAsTemplateOrTemplatePattern() |
| 2702 | .getAsDependentTemplateName()) { |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2703 | // We have a template argument such as \c T::template X, which we |
| 2704 | // parsed as a template template argument. However, since we now |
| 2705 | // know that we need a non-type template argument, convert this |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2706 | // template name into an expression. |
| 2707 | |
| 2708 | DeclarationNameInfo NameInfo(DTN->getIdentifier(), |
| 2709 | Arg.getTemplateNameLoc()); |
| 2710 | |
Douglas Gregor | 00cf3cc | 2011-02-25 20:49:16 +0000 | [diff] [blame] | 2711 | CXXScopeSpec SS; |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2712 | SS.Adopt(Arg.getTemplateQualifierLoc()); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2713 | ExprResult E = Owned(DependentScopeDeclRefExpr::Create(Context, |
Douglas Gregor | 00cf3cc | 2011-02-25 20:49:16 +0000 | [diff] [blame] | 2714 | SS.getWithLocInContext(Context), |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2715 | NameInfo)); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2716 | |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 2717 | // If we parsed the template argument as a pack expansion, create a |
| 2718 | // pack expansion expression. |
| 2719 | if (Arg.getArgument().getKind() == TemplateArgument::TemplateExpansion){ |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2720 | E = ActOnPackExpansion(E.take(), Arg.getTemplateEllipsisLoc()); |
| 2721 | if (E.isInvalid()) |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 2722 | return true; |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 2723 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2724 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2725 | TemplateArgument Result; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2726 | E = CheckTemplateArgument(NTTP, NTTPType, E.take(), Result); |
| 2727 | if (E.isInvalid()) |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2728 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2729 | |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2730 | Converted.push_back(Result); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2731 | break; |
| 2732 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2733 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2734 | // We have a template argument that actually does refer to a class |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2735 | // template, alias template, or template template parameter, and |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2736 | // therefore cannot be a non-type template argument. |
| 2737 | Diag(Arg.getLocation(), diag::err_template_arg_must_be_expr) |
| 2738 | << Arg.getSourceRange(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2739 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2740 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 2741 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2742 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2743 | case TemplateArgument::Type: { |
| 2744 | // We have a non-type template parameter but the template |
| 2745 | // argument is a type. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2746 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2747 | // C++ [temp.arg]p2: |
| 2748 | // In a template-argument, an ambiguity between a type-id and |
| 2749 | // an expression is resolved to a type-id, regardless of the |
| 2750 | // form of the corresponding template-parameter. |
| 2751 | // |
| 2752 | // We warn specifically about this case, since it can be rather |
| 2753 | // confusing for users. |
| 2754 | QualType T = Arg.getArgument().getAsType(); |
| 2755 | SourceRange SR = Arg.getSourceRange(); |
| 2756 | if (T->isFunctionType()) |
| 2757 | Diag(SR.getBegin(), diag::err_template_arg_nontype_ambig) << SR << T; |
| 2758 | else |
| 2759 | Diag(SR.getBegin(), diag::err_template_arg_must_be_expr) << SR; |
| 2760 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 2761 | return true; |
| 2762 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2763 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2764 | case TemplateArgument::Pack: |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 2765 | llvm_unreachable("Caller must expand template argument packs"); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2766 | break; |
| 2767 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2768 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2769 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2770 | } |
| 2771 | |
| 2772 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2773 | // Check template template parameters. |
| 2774 | TemplateTemplateParmDecl *TempParm = cast<TemplateTemplateParmDecl>(Param); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2775 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2776 | // Substitute into the template parameter list of the template |
| 2777 | // template parameter, since previously-supplied template arguments |
| 2778 | // may appear within the template template parameter. |
| 2779 | { |
| 2780 | // Set up a template instantiation context. |
| 2781 | LocalInstantiationScope Scope(*this); |
| 2782 | InstantiatingTemplate Inst(*this, TemplateLoc, Template, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2783 | TempParm, Converted.data(), Converted.size(), |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2784 | SourceRange(TemplateLoc, RAngleLoc)); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2785 | |
| 2786 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2787 | Converted.data(), Converted.size()); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2788 | TempParm = cast_or_null<TemplateTemplateParmDecl>( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2789 | SubstDecl(TempParm, CurContext, |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2790 | MultiLevelTemplateArgumentList(TemplateArgs))); |
| 2791 | if (!TempParm) |
| 2792 | return true; |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2793 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2794 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2795 | switch (Arg.getArgument().getKind()) { |
| 2796 | case TemplateArgument::Null: |
| 2797 | assert(false && "Should never see a NULL template argument here"); |
| 2798 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2799 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2800 | case TemplateArgument::Template: |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 2801 | case TemplateArgument::TemplateExpansion: |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2802 | if (CheckTemplateArgument(TempParm, Arg)) |
| 2803 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2804 | |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2805 | Converted.push_back(Arg.getArgument()); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2806 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2807 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2808 | case TemplateArgument::Expression: |
| 2809 | case TemplateArgument::Type: |
| 2810 | // We have a template template parameter but the template |
| 2811 | // argument does not refer to a template. |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2812 | Diag(Arg.getLocation(), diag::err_template_arg_must_be_template) |
| 2813 | << getLangOptions().CPlusPlus0x; |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2814 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2815 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2816 | case TemplateArgument::Declaration: |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 2817 | llvm_unreachable( |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2818 | "Declaration argument with template template parameter"); |
| 2819 | break; |
| 2820 | case TemplateArgument::Integral: |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 2821 | llvm_unreachable( |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2822 | "Integral argument with template template parameter"); |
| 2823 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2824 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2825 | case TemplateArgument::Pack: |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 2826 | llvm_unreachable("Caller must expand template argument packs"); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2827 | break; |
| 2828 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2829 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2830 | return false; |
| 2831 | } |
| 2832 | |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2833 | /// \brief Check that the given template argument list is well-formed |
| 2834 | /// for specializing the given template. |
| 2835 | bool Sema::CheckTemplateArgumentList(TemplateDecl *Template, |
| 2836 | SourceLocation TemplateLoc, |
Douglas Gregor | 6771423 | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 2837 | TemplateArgumentListInfo &TemplateArgs, |
Douglas Gregor | 16134c6 | 2009-07-01 00:28:38 +0000 | [diff] [blame] | 2838 | bool PartialTemplateArgs, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2839 | SmallVectorImpl<TemplateArgument> &Converted) { |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2840 | TemplateParameterList *Params = Template->getTemplateParameters(); |
| 2841 | unsigned NumParams = Params->size(); |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2842 | unsigned NumArgs = TemplateArgs.size(); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2843 | bool Invalid = false; |
| 2844 | |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2845 | SourceLocation RAngleLoc = TemplateArgs.getRAngleLoc(); |
| 2846 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2847 | bool HasParameterPack = |
Anders Carlsson | 0ceffb5 | 2009-06-13 02:08:00 +0000 | [diff] [blame] | 2848 | NumParams > 0 && Params->getParam(NumParams - 1)->isTemplateParameterPack(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2849 | |
Anders Carlsson | 0ceffb5 | 2009-06-13 02:08:00 +0000 | [diff] [blame] | 2850 | if ((NumArgs > NumParams && !HasParameterPack) || |
Douglas Gregor | 16134c6 | 2009-07-01 00:28:38 +0000 | [diff] [blame] | 2851 | (NumArgs < Params->getMinRequiredArguments() && |
| 2852 | !PartialTemplateArgs)) { |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2853 | // FIXME: point at either the first arg beyond what we can handle, |
| 2854 | // or the '>', depending on whether we have too many or too few |
| 2855 | // arguments. |
| 2856 | SourceRange Range; |
| 2857 | if (NumArgs > NumParams) |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2858 | Range = SourceRange(TemplateArgs[NumParams].getLocation(), RAngleLoc); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2859 | Diag(TemplateLoc, diag::err_template_arg_list_different_arity) |
| 2860 | << (NumArgs > NumParams) |
| 2861 | << (isa<ClassTemplateDecl>(Template)? 0 : |
| 2862 | isa<FunctionTemplateDecl>(Template)? 1 : |
| 2863 | isa<TemplateTemplateParmDecl>(Template)? 2 : 3) |
| 2864 | << Template << Range; |
Douglas Gregor | 62cb18d | 2009-02-11 18:16:40 +0000 | [diff] [blame] | 2865 | Diag(Template->getLocation(), diag::note_template_decl_here) |
| 2866 | << Params->getSourceRange(); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2867 | Invalid = true; |
| 2868 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2869 | |
| 2870 | // C++ [temp.arg]p1: |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2871 | // [...] The type and form of each template-argument specified in |
| 2872 | // a template-id shall match the type and form specified for the |
| 2873 | // corresponding parameter declared by the template in its |
| 2874 | // template-parameter-list. |
Douglas Gregor | 6771423 | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 2875 | bool isTemplateTemplateParameter = isa<TemplateTemplateParmDecl>(Template); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2876 | SmallVector<TemplateArgument, 2> ArgumentPack; |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 2877 | TemplateParameterList::iterator Param = Params->begin(), |
| 2878 | ParamEnd = Params->end(); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2879 | unsigned ArgIdx = 0; |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 2880 | LocalInstantiationScope InstScope(*this, true); |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 2881 | while (Param != ParamEnd) { |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2882 | if (ArgIdx < NumArgs) { |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2883 | // If we have an expanded parameter pack, make sure we don't have too |
| 2884 | // many arguments. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2885 | if (NonTypeTemplateParmDecl *NTTP |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2886 | = dyn_cast<NonTypeTemplateParmDecl>(*Param)) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2887 | if (NTTP->isExpandedParameterPack() && |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2888 | ArgumentPack.size() >= NTTP->getNumExpansionTypes()) { |
| 2889 | Diag(TemplateLoc, diag::err_template_arg_list_different_arity) |
| 2890 | << true |
| 2891 | << (isa<ClassTemplateDecl>(Template)? 0 : |
| 2892 | isa<FunctionTemplateDecl>(Template)? 1 : |
| 2893 | isa<TemplateTemplateParmDecl>(Template)? 2 : 3) |
| 2894 | << Template; |
| 2895 | Diag(Template->getLocation(), diag::note_template_decl_here) |
| 2896 | << Params->getSourceRange(); |
| 2897 | return true; |
| 2898 | } |
| 2899 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2900 | |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2901 | // Check the template argument we were given. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2902 | if (CheckTemplateArgument(*Param, TemplateArgs[ArgIdx], Template, |
| 2903 | TemplateLoc, RAngleLoc, |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2904 | ArgumentPack.size(), Converted)) |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2905 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2906 | |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 2907 | if ((*Param)->isTemplateParameterPack()) { |
| 2908 | // The template parameter was a template parameter pack, so take the |
| 2909 | // deduced argument and place it on the argument pack. Note that we |
| 2910 | // stay on the same template parameter so that we can deduce more |
| 2911 | // arguments. |
| 2912 | ArgumentPack.push_back(Converted.back()); |
| 2913 | Converted.pop_back(); |
| 2914 | } else { |
| 2915 | // Move to the next template parameter. |
| 2916 | ++Param; |
| 2917 | } |
| 2918 | ++ArgIdx; |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2919 | continue; |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 2920 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2921 | |
Douglas Gregor | 8735b29 | 2011-06-03 02:59:40 +0000 | [diff] [blame] | 2922 | // If we're checking a partial template argument list, we're done. |
| 2923 | if (PartialTemplateArgs) { |
| 2924 | if ((*Param)->isTemplateParameterPack() && !ArgumentPack.empty()) |
| 2925 | Converted.push_back(TemplateArgument::CreatePackCopy(Context, |
| 2926 | ArgumentPack.data(), |
| 2927 | ArgumentPack.size())); |
| 2928 | |
| 2929 | return Invalid; |
| 2930 | } |
| 2931 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2932 | // If we have a template parameter pack with no more corresponding |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 2933 | // arguments, just break out now and we'll fill in the argument pack below. |
| 2934 | if ((*Param)->isTemplateParameterPack()) |
| 2935 | break; |
Douglas Gregor | f968d83 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 2936 | |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2937 | // We have a default template argument that we will use. |
| 2938 | TemplateArgumentLoc Arg; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2939 | |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2940 | // Retrieve the default template argument from the template |
| 2941 | // parameter. For each kind of template parameter, we substitute the |
| 2942 | // template arguments provided thus far and any "outer" template arguments |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2943 | // (when the template parameter was part of a nested template) into |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2944 | // the default argument. |
| 2945 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*Param)) { |
| 2946 | if (!TTP->hasDefaultArgument()) { |
Douglas Gregor | 8735b29 | 2011-06-03 02:59:40 +0000 | [diff] [blame] | 2947 | assert(Invalid && "Missing default argument"); |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2948 | break; |
| 2949 | } |
| 2950 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2951 | TypeSourceInfo *ArgType = SubstDefaultTemplateArgument(*this, |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2952 | Template, |
| 2953 | TemplateLoc, |
| 2954 | RAngleLoc, |
| 2955 | TTP, |
| 2956 | Converted); |
| 2957 | if (!ArgType) |
| 2958 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2959 | |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2960 | Arg = TemplateArgumentLoc(TemplateArgument(ArgType->getType()), |
| 2961 | ArgType); |
| 2962 | } else if (NonTypeTemplateParmDecl *NTTP |
| 2963 | = dyn_cast<NonTypeTemplateParmDecl>(*Param)) { |
| 2964 | if (!NTTP->hasDefaultArgument()) { |
Douglas Gregor | 8735b29 | 2011-06-03 02:59:40 +0000 | [diff] [blame] | 2965 | assert(Invalid && "Missing default argument"); |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2966 | break; |
| 2967 | } |
| 2968 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2969 | ExprResult E = SubstDefaultTemplateArgument(*this, Template, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2970 | TemplateLoc, |
| 2971 | RAngleLoc, |
| 2972 | NTTP, |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2973 | Converted); |
| 2974 | if (E.isInvalid()) |
| 2975 | return true; |
| 2976 | |
| 2977 | Expr *Ex = E.takeAs<Expr>(); |
| 2978 | Arg = TemplateArgumentLoc(TemplateArgument(Ex), Ex); |
| 2979 | } else { |
| 2980 | TemplateTemplateParmDecl *TempParm |
| 2981 | = cast<TemplateTemplateParmDecl>(*Param); |
| 2982 | |
| 2983 | if (!TempParm->hasDefaultArgument()) { |
Douglas Gregor | 8735b29 | 2011-06-03 02:59:40 +0000 | [diff] [blame] | 2984 | assert(Invalid && "Missing default argument"); |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2985 | break; |
| 2986 | } |
| 2987 | |
Douglas Gregor | 1d752d7 | 2011-03-02 18:46:51 +0000 | [diff] [blame] | 2988 | NestedNameSpecifierLoc QualifierLoc; |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2989 | TemplateName Name = SubstDefaultTemplateArgument(*this, Template, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2990 | TemplateLoc, |
| 2991 | RAngleLoc, |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2992 | TempParm, |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2993 | Converted, |
| 2994 | QualifierLoc); |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2995 | if (Name.isNull()) |
| 2996 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2997 | |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2998 | Arg = TemplateArgumentLoc(TemplateArgument(Name), QualifierLoc, |
| 2999 | TempParm->getDefaultArgument().getTemplateNameLoc()); |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3000 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3001 | |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3002 | // Introduce an instantiation record that describes where we are using |
| 3003 | // the default template argument. |
| 3004 | InstantiatingTemplate Instantiating(*this, RAngleLoc, Template, *Param, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 3005 | Converted.data(), Converted.size(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3006 | SourceRange(TemplateLoc, RAngleLoc)); |
| 3007 | |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3008 | // Check the default template argument. |
Douglas Gregor | d9e1530 | 2009-11-11 19:41:09 +0000 | [diff] [blame] | 3009 | if (CheckTemplateArgument(*Param, Arg, Template, TemplateLoc, |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 3010 | RAngleLoc, 0, Converted)) |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3011 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3012 | |
Douglas Gregor | 6771423 | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 3013 | // Core issue 150 (assumed resolution): if this is a template template |
| 3014 | // parameter, keep track of the default template arguments from the |
| 3015 | // template definition. |
| 3016 | if (isTemplateTemplateParameter) |
| 3017 | TemplateArgs.addArgument(Arg); |
| 3018 | |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 3019 | // Move to the next template parameter and argument. |
| 3020 | ++Param; |
| 3021 | ++ArgIdx; |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3022 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3023 | |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 3024 | // Form argument packs for each of the parameter packs remaining. |
| 3025 | while (Param != ParamEnd) { |
Douglas Gregor | d373119 | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 3026 | // If we're checking a partial list of template arguments, don't fill |
| 3027 | // in arguments for non-template parameter packs. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3028 | |
| 3029 | if ((*Param)->isTemplateParameterPack()) { |
Douglas Gregor | 8735b29 | 2011-06-03 02:59:40 +0000 | [diff] [blame] | 3030 | if (ArgumentPack.empty()) |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 3031 | Converted.push_back(TemplateArgument(0, 0)); |
Douglas Gregor | 203e6a3 | 2011-01-11 23:09:57 +0000 | [diff] [blame] | 3032 | else { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3033 | Converted.push_back(TemplateArgument::CreatePackCopy(Context, |
| 3034 | ArgumentPack.data(), |
Douglas Gregor | 203e6a3 | 2011-01-11 23:09:57 +0000 | [diff] [blame] | 3035 | ArgumentPack.size())); |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 3036 | ArgumentPack.clear(); |
| 3037 | } |
| 3038 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3039 | |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 3040 | ++Param; |
| 3041 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3042 | |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3043 | return Invalid; |
| 3044 | } |
| 3045 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3046 | namespace { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3047 | class UnnamedLocalNoLinkageFinder |
| 3048 | : public TypeVisitor<UnnamedLocalNoLinkageFinder, bool> |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3049 | { |
| 3050 | Sema &S; |
| 3051 | SourceRange SR; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3052 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3053 | typedef TypeVisitor<UnnamedLocalNoLinkageFinder, bool> inherited; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3054 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3055 | public: |
| 3056 | UnnamedLocalNoLinkageFinder(Sema &S, SourceRange SR) : S(S), SR(SR) { } |
| 3057 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3058 | bool Visit(QualType T) { |
| 3059 | return inherited::Visit(T.getTypePtr()); |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3060 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3061 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3062 | #define TYPE(Class, Parent) \ |
| 3063 | bool Visit##Class##Type(const Class##Type *); |
| 3064 | #define ABSTRACT_TYPE(Class, Parent) \ |
| 3065 | bool Visit##Class##Type(const Class##Type *) { return false; } |
| 3066 | #define NON_CANONICAL_TYPE(Class, Parent) \ |
| 3067 | bool Visit##Class##Type(const Class##Type *) { return false; } |
| 3068 | #include "clang/AST/TypeNodes.def" |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3069 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3070 | bool VisitTagDecl(const TagDecl *Tag); |
| 3071 | bool VisitNestedNameSpecifier(NestedNameSpecifier *NNS); |
| 3072 | }; |
| 3073 | } |
| 3074 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3075 | bool UnnamedLocalNoLinkageFinder::VisitBuiltinType(const BuiltinType*) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3076 | return false; |
| 3077 | } |
| 3078 | |
| 3079 | bool UnnamedLocalNoLinkageFinder::VisitComplexType(const ComplexType* T) { |
| 3080 | return Visit(T->getElementType()); |
| 3081 | } |
| 3082 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3083 | bool UnnamedLocalNoLinkageFinder::VisitPointerType(const PointerType* T) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3084 | return Visit(T->getPointeeType()); |
| 3085 | } |
| 3086 | |
| 3087 | bool UnnamedLocalNoLinkageFinder::VisitBlockPointerType( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3088 | const BlockPointerType* T) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3089 | return Visit(T->getPointeeType()); |
| 3090 | } |
| 3091 | |
| 3092 | bool UnnamedLocalNoLinkageFinder::VisitLValueReferenceType( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3093 | const LValueReferenceType* T) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3094 | return Visit(T->getPointeeType()); |
| 3095 | } |
| 3096 | |
| 3097 | bool UnnamedLocalNoLinkageFinder::VisitRValueReferenceType( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3098 | const RValueReferenceType* T) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3099 | return Visit(T->getPointeeType()); |
| 3100 | } |
| 3101 | |
| 3102 | bool UnnamedLocalNoLinkageFinder::VisitMemberPointerType( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3103 | const MemberPointerType* T) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3104 | return Visit(T->getPointeeType()) || Visit(QualType(T->getClass(), 0)); |
| 3105 | } |
| 3106 | |
| 3107 | bool UnnamedLocalNoLinkageFinder::VisitConstantArrayType( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3108 | const ConstantArrayType* T) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3109 | return Visit(T->getElementType()); |
| 3110 | } |
| 3111 | |
| 3112 | bool UnnamedLocalNoLinkageFinder::VisitIncompleteArrayType( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3113 | const IncompleteArrayType* T) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3114 | return Visit(T->getElementType()); |
| 3115 | } |
| 3116 | |
| 3117 | bool UnnamedLocalNoLinkageFinder::VisitVariableArrayType( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3118 | const VariableArrayType* T) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3119 | return Visit(T->getElementType()); |
| 3120 | } |
| 3121 | |
| 3122 | bool UnnamedLocalNoLinkageFinder::VisitDependentSizedArrayType( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3123 | const DependentSizedArrayType* T) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3124 | return Visit(T->getElementType()); |
| 3125 | } |
| 3126 | |
| 3127 | bool UnnamedLocalNoLinkageFinder::VisitDependentSizedExtVectorType( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3128 | const DependentSizedExtVectorType* T) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3129 | return Visit(T->getElementType()); |
| 3130 | } |
| 3131 | |
| 3132 | bool UnnamedLocalNoLinkageFinder::VisitVectorType(const VectorType* T) { |
| 3133 | return Visit(T->getElementType()); |
| 3134 | } |
| 3135 | |
| 3136 | bool UnnamedLocalNoLinkageFinder::VisitExtVectorType(const ExtVectorType* T) { |
| 3137 | return Visit(T->getElementType()); |
| 3138 | } |
| 3139 | |
| 3140 | bool UnnamedLocalNoLinkageFinder::VisitFunctionProtoType( |
| 3141 | const FunctionProtoType* T) { |
| 3142 | for (FunctionProtoType::arg_type_iterator A = T->arg_type_begin(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3143 | AEnd = T->arg_type_end(); |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3144 | A != AEnd; ++A) { |
| 3145 | if (Visit(*A)) |
| 3146 | return true; |
| 3147 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3148 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3149 | return Visit(T->getResultType()); |
| 3150 | } |
| 3151 | |
| 3152 | bool UnnamedLocalNoLinkageFinder::VisitFunctionNoProtoType( |
| 3153 | const FunctionNoProtoType* T) { |
| 3154 | return Visit(T->getResultType()); |
| 3155 | } |
| 3156 | |
| 3157 | bool UnnamedLocalNoLinkageFinder::VisitUnresolvedUsingType( |
| 3158 | const UnresolvedUsingType*) { |
| 3159 | return false; |
| 3160 | } |
| 3161 | |
| 3162 | bool UnnamedLocalNoLinkageFinder::VisitTypeOfExprType(const TypeOfExprType*) { |
| 3163 | return false; |
| 3164 | } |
| 3165 | |
| 3166 | bool UnnamedLocalNoLinkageFinder::VisitTypeOfType(const TypeOfType* T) { |
| 3167 | return Visit(T->getUnderlyingType()); |
| 3168 | } |
| 3169 | |
| 3170 | bool UnnamedLocalNoLinkageFinder::VisitDecltypeType(const DecltypeType*) { |
| 3171 | return false; |
| 3172 | } |
| 3173 | |
Sean Hunt | ca63c20 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 3174 | bool UnnamedLocalNoLinkageFinder::VisitUnaryTransformType( |
| 3175 | const UnaryTransformType*) { |
| 3176 | return false; |
| 3177 | } |
| 3178 | |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 3179 | bool UnnamedLocalNoLinkageFinder::VisitAutoType(const AutoType *T) { |
| 3180 | return Visit(T->getDeducedType()); |
| 3181 | } |
| 3182 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3183 | bool UnnamedLocalNoLinkageFinder::VisitRecordType(const RecordType* T) { |
| 3184 | return VisitTagDecl(T->getDecl()); |
| 3185 | } |
| 3186 | |
| 3187 | bool UnnamedLocalNoLinkageFinder::VisitEnumType(const EnumType* T) { |
| 3188 | return VisitTagDecl(T->getDecl()); |
| 3189 | } |
| 3190 | |
| 3191 | bool UnnamedLocalNoLinkageFinder::VisitTemplateTypeParmType( |
| 3192 | const TemplateTypeParmType*) { |
| 3193 | return false; |
| 3194 | } |
| 3195 | |
Douglas Gregor | c3069d6 | 2011-01-14 02:55:32 +0000 | [diff] [blame] | 3196 | bool UnnamedLocalNoLinkageFinder::VisitSubstTemplateTypeParmPackType( |
| 3197 | const SubstTemplateTypeParmPackType *) { |
| 3198 | return false; |
| 3199 | } |
| 3200 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3201 | bool UnnamedLocalNoLinkageFinder::VisitTemplateSpecializationType( |
| 3202 | const TemplateSpecializationType*) { |
| 3203 | return false; |
| 3204 | } |
| 3205 | |
| 3206 | bool UnnamedLocalNoLinkageFinder::VisitInjectedClassNameType( |
| 3207 | const InjectedClassNameType* T) { |
| 3208 | return VisitTagDecl(T->getDecl()); |
| 3209 | } |
| 3210 | |
| 3211 | bool UnnamedLocalNoLinkageFinder::VisitDependentNameType( |
| 3212 | const DependentNameType* T) { |
| 3213 | return VisitNestedNameSpecifier(T->getQualifier()); |
| 3214 | } |
| 3215 | |
| 3216 | bool UnnamedLocalNoLinkageFinder::VisitDependentTemplateSpecializationType( |
| 3217 | const DependentTemplateSpecializationType* T) { |
| 3218 | return VisitNestedNameSpecifier(T->getQualifier()); |
| 3219 | } |
| 3220 | |
Douglas Gregor | 7536dd5 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 3221 | bool UnnamedLocalNoLinkageFinder::VisitPackExpansionType( |
| 3222 | const PackExpansionType* T) { |
| 3223 | return Visit(T->getPattern()); |
| 3224 | } |
| 3225 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3226 | bool UnnamedLocalNoLinkageFinder::VisitObjCObjectType(const ObjCObjectType *) { |
| 3227 | return false; |
| 3228 | } |
| 3229 | |
| 3230 | bool UnnamedLocalNoLinkageFinder::VisitObjCInterfaceType( |
| 3231 | const ObjCInterfaceType *) { |
| 3232 | return false; |
| 3233 | } |
| 3234 | |
| 3235 | bool UnnamedLocalNoLinkageFinder::VisitObjCObjectPointerType( |
| 3236 | const ObjCObjectPointerType *) { |
| 3237 | return false; |
| 3238 | } |
| 3239 | |
| 3240 | bool UnnamedLocalNoLinkageFinder::VisitTagDecl(const TagDecl *Tag) { |
| 3241 | if (Tag->getDeclContext()->isFunctionOrMethod()) { |
| 3242 | S.Diag(SR.getBegin(), diag::ext_template_arg_local_type) |
| 3243 | << S.Context.getTypeDeclType(Tag) << SR; |
| 3244 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3245 | } |
| 3246 | |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 3247 | if (!Tag->getDeclName() && !Tag->getTypedefNameForAnonDecl()) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3248 | S.Diag(SR.getBegin(), diag::ext_template_arg_unnamed_type) << SR; |
| 3249 | S.Diag(Tag->getLocation(), diag::note_template_unnamed_type_here); |
| 3250 | return true; |
| 3251 | } |
| 3252 | |
| 3253 | return false; |
| 3254 | } |
| 3255 | |
| 3256 | bool UnnamedLocalNoLinkageFinder::VisitNestedNameSpecifier( |
| 3257 | NestedNameSpecifier *NNS) { |
| 3258 | if (NNS->getPrefix() && VisitNestedNameSpecifier(NNS->getPrefix())) |
| 3259 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3260 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3261 | switch (NNS->getKind()) { |
| 3262 | case NestedNameSpecifier::Identifier: |
| 3263 | case NestedNameSpecifier::Namespace: |
Douglas Gregor | 14aba76 | 2011-02-24 02:36:08 +0000 | [diff] [blame] | 3264 | case NestedNameSpecifier::NamespaceAlias: |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3265 | case NestedNameSpecifier::Global: |
| 3266 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3267 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3268 | case NestedNameSpecifier::TypeSpec: |
| 3269 | case NestedNameSpecifier::TypeSpecWithTemplate: |
| 3270 | return Visit(QualType(NNS->getAsType(), 0)); |
| 3271 | } |
Fariborz Jahanian | 7b1ec6c | 2010-10-13 16:19:16 +0000 | [diff] [blame] | 3272 | return false; |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3273 | } |
| 3274 | |
| 3275 | |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3276 | /// \brief Check a template argument against its corresponding |
| 3277 | /// template type parameter. |
| 3278 | /// |
| 3279 | /// This routine implements the semantics of C++ [temp.arg.type]. It |
| 3280 | /// returns true if an error occurred, and false otherwise. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3281 | bool Sema::CheckTemplateArgument(TemplateTypeParmDecl *Param, |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3282 | TypeSourceInfo *ArgInfo) { |
| 3283 | assert(ArgInfo && "invalid TypeSourceInfo"); |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 3284 | QualType Arg = ArgInfo->getType(); |
Douglas Gregor | 0fddb97 | 2010-05-22 16:17:30 +0000 | [diff] [blame] | 3285 | SourceRange SR = ArgInfo->getTypeLoc().getSourceRange(); |
Chandler Carruth | 17fb855 | 2010-09-03 21:12:34 +0000 | [diff] [blame] | 3286 | |
| 3287 | if (Arg->isVariablyModifiedType()) { |
| 3288 | return Diag(SR.getBegin(), diag::err_variably_modified_template_arg) << Arg; |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 3289 | } else if (Context.hasSameUnqualifiedType(Arg, Context.OverloadTy)) { |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 3290 | return Diag(SR.getBegin(), diag::err_template_arg_overload_type) << SR; |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3291 | } |
| 3292 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3293 | // C++03 [temp.arg.type]p2: |
| 3294 | // A local type, a type with no linkage, an unnamed type or a type |
| 3295 | // compounded from any of these types shall not be used as a |
| 3296 | // template-argument for a template type-parameter. |
| 3297 | // |
| 3298 | // C++0x allows these, and even in C++03 we allow them as an extension with |
| 3299 | // a warning. |
Douglas Gregor | db4d4bb | 2010-10-13 18:05:20 +0000 | [diff] [blame] | 3300 | if (!LangOpts.CPlusPlus0x && Arg->hasUnnamedOrLocalType()) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3301 | UnnamedLocalNoLinkageFinder Finder(*this, SR); |
| 3302 | (void)Finder.Visit(Context.getCanonicalType(Arg)); |
| 3303 | } |
| 3304 | |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3305 | return false; |
| 3306 | } |
| 3307 | |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3308 | /// \brief Checks whether the given template argument is the address |
| 3309 | /// of an object or function according to C++ [temp.arg.nontype]p1. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3310 | static bool |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3311 | CheckTemplateArgumentAddressOfObjectOrFunction(Sema &S, |
| 3312 | NonTypeTemplateParmDecl *Param, |
| 3313 | QualType ParamType, |
| 3314 | Expr *ArgIn, |
| 3315 | TemplateArgument &Converted) { |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3316 | bool Invalid = false; |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3317 | Expr *Arg = ArgIn; |
| 3318 | QualType ArgType = Arg->getType(); |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3319 | |
| 3320 | // See through any implicit casts we added to fix the type. |
John McCall | 91a5755 | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 3321 | Arg = Arg->IgnoreImpCasts(); |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3322 | |
| 3323 | // C++ [temp.arg.nontype]p1: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3324 | // |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3325 | // A template-argument for a non-type, non-template |
| 3326 | // template-parameter shall be one of: [...] |
| 3327 | // |
| 3328 | // -- the address of an object or function with external |
| 3329 | // linkage, including function templates and function |
| 3330 | // template-ids but excluding non-static class members, |
| 3331 | // expressed as & id-expression where the & is optional if |
| 3332 | // the name refers to a function or array, or if the |
| 3333 | // corresponding template-parameter is a reference; or |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3334 | |
Abramo Bagnara | 2c5399f | 2010-09-13 06:06:58 +0000 | [diff] [blame] | 3335 | // In C++98/03 mode, give an extension warning on any extra parentheses. |
| 3336 | // See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#773 |
| 3337 | bool ExtraParens = false; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3338 | while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) { |
Abramo Bagnara | 2c5399f | 2010-09-13 06:06:58 +0000 | [diff] [blame] | 3339 | if (!Invalid && !ExtraParens && !S.getLangOptions().CPlusPlus0x) { |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3340 | S.Diag(Arg->getSourceRange().getBegin(), |
Abramo Bagnara | 2c5399f | 2010-09-13 06:06:58 +0000 | [diff] [blame] | 3341 | diag::ext_template_arg_extra_parens) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3342 | << Arg->getSourceRange(); |
Abramo Bagnara | 2c5399f | 2010-09-13 06:06:58 +0000 | [diff] [blame] | 3343 | ExtraParens = true; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3344 | } |
| 3345 | |
| 3346 | Arg = Parens->getSubExpr(); |
| 3347 | } |
| 3348 | |
John McCall | 91a5755 | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 3349 | while (SubstNonTypeTemplateParmExpr *subst = |
| 3350 | dyn_cast<SubstNonTypeTemplateParmExpr>(Arg)) |
| 3351 | Arg = subst->getReplacement()->IgnoreImpCasts(); |
| 3352 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3353 | bool AddressTaken = false; |
| 3354 | SourceLocation AddrOpLoc; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3355 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3356 | if (UnOp->getOpcode() == UO_AddrOf) { |
John McCall | 91a5755 | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 3357 | Arg = UnOp->getSubExpr(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3358 | AddressTaken = true; |
| 3359 | AddrOpLoc = UnOp->getOperatorLoc(); |
| 3360 | } |
Francois Pichet | a343a41 | 2011-04-29 09:08:14 +0000 | [diff] [blame] | 3361 | } |
John McCall | 91a5755 | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 3362 | |
| 3363 | if (S.getLangOptions().Microsoft && isa<CXXUuidofExpr>(Arg)) { |
| 3364 | Converted = TemplateArgument(ArgIn); |
| 3365 | return false; |
| 3366 | } |
| 3367 | |
| 3368 | while (SubstNonTypeTemplateParmExpr *subst = |
| 3369 | dyn_cast<SubstNonTypeTemplateParmExpr>(Arg)) |
| 3370 | Arg = subst->getReplacement()->IgnoreImpCasts(); |
| 3371 | |
| 3372 | DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Arg); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3373 | if (!DRE) { |
Douglas Gregor | 1a8cf73 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 3374 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_decl_ref) |
| 3375 | << Arg->getSourceRange(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3376 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3377 | return true; |
| 3378 | } |
Chandler Carruth | 038cc39 | 2010-01-31 10:01:20 +0000 | [diff] [blame] | 3379 | |
| 3380 | // Stop checking the precise nature of the argument if it is value dependent, |
| 3381 | // it should be checked when instantiated. |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3382 | if (Arg->isValueDependent()) { |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 3383 | Converted = TemplateArgument(ArgIn); |
Chandler Carruth | 038cc39 | 2010-01-31 10:01:20 +0000 | [diff] [blame] | 3384 | return false; |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3385 | } |
Chandler Carruth | 038cc39 | 2010-01-31 10:01:20 +0000 | [diff] [blame] | 3386 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3387 | if (!isa<ValueDecl>(DRE->getDecl())) { |
| 3388 | S.Diag(Arg->getSourceRange().getBegin(), |
| 3389 | diag::err_template_arg_not_object_or_func_form) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3390 | << Arg->getSourceRange(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3391 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3392 | return true; |
| 3393 | } |
| 3394 | |
| 3395 | NamedDecl *Entity = 0; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3396 | |
| 3397 | // Cannot refer to non-static data members |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3398 | if (FieldDecl *Field = dyn_cast<FieldDecl>(DRE->getDecl())) { |
| 3399 | S.Diag(Arg->getSourceRange().getBegin(), diag::err_template_arg_field) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3400 | << Field << Arg->getSourceRange(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3401 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3402 | return true; |
| 3403 | } |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3404 | |
| 3405 | // Cannot refer to non-static member functions |
| 3406 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(DRE->getDecl())) |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3407 | if (!Method->isStatic()) { |
| 3408 | S.Diag(Arg->getSourceRange().getBegin(), diag::err_template_arg_method) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3409 | << Method << Arg->getSourceRange(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3410 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3411 | return true; |
| 3412 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3413 | |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3414 | // Functions must have external linkage. |
| 3415 | if (FunctionDecl *Func = dyn_cast<FunctionDecl>(DRE->getDecl())) { |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 3416 | if (!isExternalLinkage(Func->getLinkage())) { |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3417 | S.Diag(Arg->getSourceRange().getBegin(), |
| 3418 | diag::err_template_arg_function_not_extern) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3419 | << Func << Arg->getSourceRange(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3420 | S.Diag(Func->getLocation(), diag::note_template_arg_internal_object) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3421 | << true; |
| 3422 | return true; |
| 3423 | } |
| 3424 | |
| 3425 | // Okay: we've named a function with external linkage. |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 3426 | Entity = Func; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3427 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3428 | // If the template parameter has pointer type, the function decays. |
| 3429 | if (ParamType->isPointerType() && !AddressTaken) |
| 3430 | ArgType = S.Context.getPointerType(Func->getType()); |
| 3431 | else if (AddressTaken && ParamType->isReferenceType()) { |
| 3432 | // If we originally had an address-of operator, but the |
| 3433 | // parameter has reference type, complain and (if things look |
| 3434 | // like they will work) drop the address-of operator. |
| 3435 | if (!S.Context.hasSameUnqualifiedType(Func->getType(), |
| 3436 | ParamType.getNonReferenceType())) { |
| 3437 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 3438 | << ParamType; |
| 3439 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3440 | return true; |
| 3441 | } |
| 3442 | |
| 3443 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 3444 | << ParamType |
| 3445 | << FixItHint::CreateRemoval(AddrOpLoc); |
| 3446 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3447 | |
| 3448 | ArgType = Func->getType(); |
| 3449 | } |
| 3450 | } else if (VarDecl *Var = dyn_cast<VarDecl>(DRE->getDecl())) { |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 3451 | if (!isExternalLinkage(Var->getLinkage())) { |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3452 | S.Diag(Arg->getSourceRange().getBegin(), |
| 3453 | diag::err_template_arg_object_not_extern) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3454 | << Var << Arg->getSourceRange(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3455 | S.Diag(Var->getLocation(), diag::note_template_arg_internal_object) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3456 | << true; |
| 3457 | return true; |
| 3458 | } |
| 3459 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3460 | // A value of reference type is not an object. |
| 3461 | if (Var->getType()->isReferenceType()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3462 | S.Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3463 | diag::err_template_arg_reference_var) |
| 3464 | << Var->getType() << Arg->getSourceRange(); |
| 3465 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3466 | return true; |
| 3467 | } |
| 3468 | |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3469 | // Okay: we've named an object with external linkage |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 3470 | Entity = Var; |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3471 | |
| 3472 | // If the template parameter has pointer type, we must have taken |
| 3473 | // the address of this object. |
| 3474 | if (ParamType->isReferenceType()) { |
| 3475 | if (AddressTaken) { |
| 3476 | // If we originally had an address-of operator, but the |
| 3477 | // parameter has reference type, complain and (if things look |
| 3478 | // like they will work) drop the address-of operator. |
| 3479 | if (!S.Context.hasSameUnqualifiedType(Var->getType(), |
| 3480 | ParamType.getNonReferenceType())) { |
| 3481 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 3482 | << ParamType; |
| 3483 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3484 | return true; |
| 3485 | } |
| 3486 | |
| 3487 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 3488 | << ParamType |
| 3489 | << FixItHint::CreateRemoval(AddrOpLoc); |
| 3490 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3491 | |
| 3492 | ArgType = Var->getType(); |
| 3493 | } |
| 3494 | } else if (!AddressTaken && ParamType->isPointerType()) { |
| 3495 | if (Var->getType()->isArrayType()) { |
| 3496 | // Array-to-pointer decay. |
| 3497 | ArgType = S.Context.getArrayDecayedType(Var->getType()); |
| 3498 | } else { |
| 3499 | // If the template parameter has pointer type but the address of |
| 3500 | // this object was not taken, complain and (possibly) recover by |
| 3501 | // taking the address of the entity. |
| 3502 | ArgType = S.Context.getPointerType(Var->getType()); |
| 3503 | if (!S.Context.hasSameUnqualifiedType(ArgType, ParamType)) { |
| 3504 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_address_of) |
| 3505 | << ParamType; |
| 3506 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3507 | return true; |
| 3508 | } |
| 3509 | |
| 3510 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_address_of) |
| 3511 | << ParamType |
| 3512 | << FixItHint::CreateInsertion(Arg->getLocStart(), "&"); |
| 3513 | |
| 3514 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3515 | } |
| 3516 | } |
| 3517 | } else { |
| 3518 | // We found something else, but we don't know specifically what it is. |
| 3519 | S.Diag(Arg->getSourceRange().getBegin(), |
| 3520 | diag::err_template_arg_not_object_or_func) |
| 3521 | << Arg->getSourceRange(); |
| 3522 | S.Diag(DRE->getDecl()->getLocation(), diag::note_template_arg_refers_here); |
| 3523 | return true; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3524 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3525 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3526 | bool ObjCLifetimeConversion; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3527 | if (ParamType->isPointerType() && |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3528 | !ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType() && |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3529 | S.IsQualificationConversion(ArgType, ParamType, false, |
| 3530 | ObjCLifetimeConversion)) { |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3531 | // For pointer-to-object types, qualification conversions are |
| 3532 | // permitted. |
| 3533 | } else { |
| 3534 | if (const ReferenceType *ParamRef = ParamType->getAs<ReferenceType>()) { |
| 3535 | if (!ParamRef->getPointeeType()->isFunctionType()) { |
| 3536 | // C++ [temp.arg.nontype]p5b3: |
| 3537 | // For a non-type template-parameter of type reference to |
| 3538 | // object, no conversions apply. The type referred to by the |
| 3539 | // reference may be more cv-qualified than the (otherwise |
| 3540 | // identical) type of the template- argument. The |
| 3541 | // template-parameter is bound directly to the |
| 3542 | // template-argument, which shall be an lvalue. |
| 3543 | |
| 3544 | // FIXME: Other qualifiers? |
| 3545 | unsigned ParamQuals = ParamRef->getPointeeType().getCVRQualifiers(); |
| 3546 | unsigned ArgQuals = ArgType.getCVRQualifiers(); |
| 3547 | |
| 3548 | if ((ParamQuals | ArgQuals) != ParamQuals) { |
| 3549 | S.Diag(Arg->getSourceRange().getBegin(), |
| 3550 | diag::err_template_arg_ref_bind_ignores_quals) |
| 3551 | << ParamType << Arg->getType() |
| 3552 | << Arg->getSourceRange(); |
| 3553 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3554 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3555 | } |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3556 | } |
| 3557 | } |
| 3558 | |
| 3559 | // At this point, the template argument refers to an object or |
| 3560 | // function with external linkage. We now need to check whether the |
| 3561 | // argument and parameter types are compatible. |
| 3562 | if (!S.Context.hasSameUnqualifiedType(ArgType, |
| 3563 | ParamType.getNonReferenceType())) { |
| 3564 | // We can't perform this conversion or binding. |
| 3565 | if (ParamType->isReferenceType()) |
| 3566 | S.Diag(Arg->getLocStart(), diag::err_template_arg_no_ref_bind) |
John McCall | 91a5755 | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 3567 | << ParamType << ArgIn->getType() << Arg->getSourceRange(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3568 | else |
| 3569 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_convertible) |
John McCall | 91a5755 | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 3570 | << ArgIn->getType() << ParamType << Arg->getSourceRange(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3571 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3572 | return true; |
| 3573 | } |
| 3574 | } |
| 3575 | |
| 3576 | // Create the template argument. |
| 3577 | Converted = TemplateArgument(Entity->getCanonicalDecl()); |
Douglas Gregor | 77c13e0 | 2010-04-24 18:20:53 +0000 | [diff] [blame] | 3578 | S.MarkDeclarationReferenced(Arg->getLocStart(), Entity); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3579 | return false; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3580 | } |
| 3581 | |
| 3582 | /// \brief Checks whether the given template argument is a pointer to |
| 3583 | /// member constant according to C++ [temp.arg.nontype]p1. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3584 | bool Sema::CheckTemplateArgumentPointerToMember(Expr *Arg, |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 3585 | TemplateArgument &Converted) { |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3586 | bool Invalid = false; |
| 3587 | |
| 3588 | // See through any implicit casts we added to fix the type. |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 3589 | while (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(Arg)) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3590 | Arg = Cast->getSubExpr(); |
| 3591 | |
| 3592 | // C++ [temp.arg.nontype]p1: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3593 | // |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3594 | // A template-argument for a non-type, non-template |
| 3595 | // template-parameter shall be one of: [...] |
| 3596 | // |
| 3597 | // -- a pointer to member expressed as described in 5.3.1. |
Douglas Gregor | a2813ce | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 3598 | DeclRefExpr *DRE = 0; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3599 | |
Abramo Bagnara | 2c5399f | 2010-09-13 06:06:58 +0000 | [diff] [blame] | 3600 | // In C++98/03 mode, give an extension warning on any extra parentheses. |
| 3601 | // See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#773 |
| 3602 | bool ExtraParens = false; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3603 | while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) { |
Abramo Bagnara | 2c5399f | 2010-09-13 06:06:58 +0000 | [diff] [blame] | 3604 | if (!Invalid && !ExtraParens && !getLangOptions().CPlusPlus0x) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3605 | Diag(Arg->getSourceRange().getBegin(), |
Abramo Bagnara | 2c5399f | 2010-09-13 06:06:58 +0000 | [diff] [blame] | 3606 | diag::ext_template_arg_extra_parens) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3607 | << Arg->getSourceRange(); |
Abramo Bagnara | 2c5399f | 2010-09-13 06:06:58 +0000 | [diff] [blame] | 3608 | ExtraParens = true; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3609 | } |
| 3610 | |
| 3611 | Arg = Parens->getSubExpr(); |
| 3612 | } |
| 3613 | |
John McCall | 91a5755 | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 3614 | while (SubstNonTypeTemplateParmExpr *subst = |
| 3615 | dyn_cast<SubstNonTypeTemplateParmExpr>(Arg)) |
| 3616 | Arg = subst->getReplacement()->IgnoreImpCasts(); |
| 3617 | |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 3618 | // A pointer-to-member constant written &Class::member. |
| 3619 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3620 | if (UnOp->getOpcode() == UO_AddrOf) { |
Douglas Gregor | a2813ce | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 3621 | DRE = dyn_cast<DeclRefExpr>(UnOp->getSubExpr()); |
| 3622 | if (DRE && !DRE->getQualifier()) |
| 3623 | DRE = 0; |
| 3624 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3625 | } |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 3626 | // A constant of pointer-to-member type. |
| 3627 | else if ((DRE = dyn_cast<DeclRefExpr>(Arg))) { |
| 3628 | if (ValueDecl *VD = dyn_cast<ValueDecl>(DRE->getDecl())) { |
| 3629 | if (VD->getType()->isMemberPointerType()) { |
| 3630 | if (isa<NonTypeTemplateParmDecl>(VD) || |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3631 | (isa<VarDecl>(VD) && |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 3632 | Context.getCanonicalType(VD->getType()).isConstQualified())) { |
| 3633 | if (Arg->isTypeDependent() || Arg->isValueDependent()) |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 3634 | Converted = TemplateArgument(Arg); |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 3635 | else |
| 3636 | Converted = TemplateArgument(VD->getCanonicalDecl()); |
| 3637 | return Invalid; |
| 3638 | } |
| 3639 | } |
| 3640 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3641 | |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 3642 | DRE = 0; |
| 3643 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3644 | |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3645 | if (!DRE) |
| 3646 | return Diag(Arg->getSourceRange().getBegin(), |
| 3647 | diag::err_template_arg_not_pointer_to_member_form) |
| 3648 | << Arg->getSourceRange(); |
| 3649 | |
| 3650 | if (isa<FieldDecl>(DRE->getDecl()) || isa<CXXMethodDecl>(DRE->getDecl())) { |
| 3651 | assert((isa<FieldDecl>(DRE->getDecl()) || |
| 3652 | !cast<CXXMethodDecl>(DRE->getDecl())->isStatic()) && |
| 3653 | "Only non-static member pointers can make it here"); |
| 3654 | |
| 3655 | // Okay: this is the address of a non-static member, and therefore |
| 3656 | // a member pointer constant. |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 3657 | if (Arg->isTypeDependent() || Arg->isValueDependent()) |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 3658 | Converted = TemplateArgument(Arg); |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 3659 | else |
| 3660 | Converted = TemplateArgument(DRE->getDecl()->getCanonicalDecl()); |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3661 | return Invalid; |
| 3662 | } |
| 3663 | |
| 3664 | // We found something else, but we don't know specifically what it is. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3665 | Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3666 | diag::err_template_arg_not_pointer_to_member_form) |
| 3667 | << Arg->getSourceRange(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3668 | Diag(DRE->getDecl()->getLocation(), |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3669 | diag::note_template_arg_refers_here); |
| 3670 | return true; |
| 3671 | } |
| 3672 | |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3673 | /// \brief Check a template argument against its corresponding |
| 3674 | /// non-type template parameter. |
| 3675 | /// |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 3676 | /// This routine implements the semantics of C++ [temp.arg.nontype]. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3677 | /// If an error occurred, it returns ExprError(); otherwise, it |
| 3678 | /// returns the converted template argument. \p |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 3679 | /// InstantiatedParamType is the type of the non-type template |
| 3680 | /// parameter after it has been instantiated. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3681 | ExprResult Sema::CheckTemplateArgument(NonTypeTemplateParmDecl *Param, |
| 3682 | QualType InstantiatedParamType, Expr *Arg, |
| 3683 | TemplateArgument &Converted, |
| 3684 | CheckTemplateArgumentKind CTAK) { |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 3685 | SourceLocation StartLoc = Arg->getSourceRange().getBegin(); |
| 3686 | |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3687 | // If either the parameter has a dependent type or the argument is |
| 3688 | // type-dependent, there's nothing we can check now. |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 3689 | if (InstantiatedParamType->isDependentType() || Arg->isTypeDependent()) { |
| 3690 | // FIXME: Produce a cloned, canonical expression? |
Douglas Gregor | 02cbbd2 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 3691 | Converted = TemplateArgument(Arg); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3692 | return Owned(Arg); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 3693 | } |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3694 | |
| 3695 | // C++ [temp.arg.nontype]p5: |
| 3696 | // The following conversions are performed on each expression used |
| 3697 | // as a non-type template-argument. If a non-type |
| 3698 | // template-argument cannot be converted to the type of the |
| 3699 | // corresponding template-parameter then the program is |
| 3700 | // ill-formed. |
| 3701 | // |
| 3702 | // -- for a non-type template-parameter of integral or |
| 3703 | // enumeration type, integral promotions (4.5) and integral |
| 3704 | // conversions (4.7) are applied. |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 3705 | QualType ParamType = InstantiatedParamType; |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 3706 | QualType ArgType = Arg->getType(); |
Douglas Gregor | 2ade35e | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 3707 | if (ParamType->isIntegralOrEnumerationType()) { |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3708 | // C++ [temp.arg.nontype]p1: |
| 3709 | // A template-argument for a non-type, non-template |
| 3710 | // template-parameter shall be one of: |
| 3711 | // |
| 3712 | // -- an integral constant-expression of integral or enumeration |
| 3713 | // type; or |
| 3714 | // -- the name of a non-type template-parameter; or |
| 3715 | SourceLocation NonConstantLoc; |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 3716 | llvm::APSInt Value; |
Douglas Gregor | 2ade35e | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 3717 | if (!ArgType->isIntegralOrEnumerationType()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3718 | Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3719 | diag::err_template_arg_not_integral_or_enumeral) |
| 3720 | << ArgType << Arg->getSourceRange(); |
| 3721 | Diag(Param->getLocation(), diag::note_template_param_here); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3722 | return ExprError(); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3723 | } else if (!Arg->isValueDependent() && |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 3724 | !Arg->isIntegerConstantExpr(Value, Context, &NonConstantLoc)) { |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3725 | Diag(NonConstantLoc, diag::err_template_arg_not_ice) |
| 3726 | << ArgType << Arg->getSourceRange(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3727 | return ExprError(); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3728 | } |
| 3729 | |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3730 | // From here on out, all we care about are the unqualified forms |
| 3731 | // of the parameter and argument types. |
| 3732 | ParamType = ParamType.getUnqualifiedType(); |
| 3733 | ArgType = ArgType.getUnqualifiedType(); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3734 | |
| 3735 | // Try to convert the argument to the parameter's type. |
Douglas Gregor | ff52439 | 2009-11-04 21:50:46 +0000 | [diff] [blame] | 3736 | if (Context.hasSameType(ParamType, ArgType)) { |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3737 | // Okay: no conversion necessary |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3738 | } else if (CTAK == CTAK_Deduced) { |
| 3739 | // C++ [temp.deduct.type]p17: |
| 3740 | // If, in the declaration of a function template with a non-type |
| 3741 | // template-parameter, the non-type template- parameter is used |
| 3742 | // in an expression in the function parameter-list and, if the |
| 3743 | // corresponding template-argument is deduced, the |
| 3744 | // template-argument type shall match the type of the |
| 3745 | // template-parameter exactly, except that a template-argument |
| 3746 | // deduced from an array bound may be of any integral type. |
| 3747 | Diag(StartLoc, diag::err_deduced_non_type_template_arg_type_mismatch) |
| 3748 | << ArgType << ParamType; |
| 3749 | Diag(Param->getLocation(), diag::note_template_param_here); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3750 | return ExprError(); |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3751 | } else if (ParamType->isBooleanType()) { |
| 3752 | // This is an integral-to-boolean conversion. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3753 | Arg = ImpCastExprToType(Arg, ParamType, CK_IntegralToBoolean).take(); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3754 | } else if (IsIntegralPromotion(Arg, ArgType, ParamType) || |
| 3755 | !ParamType->isEnumeralType()) { |
| 3756 | // This is an integral promotion or conversion. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3757 | Arg = ImpCastExprToType(Arg, ParamType, CK_IntegralCast).take(); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3758 | } else { |
| 3759 | // We can't perform this conversion. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3760 | Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3761 | diag::err_template_arg_not_convertible) |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 3762 | << Arg->getType() << InstantiatedParamType << Arg->getSourceRange(); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3763 | Diag(Param->getLocation(), diag::note_template_param_here); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3764 | return ExprError(); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3765 | } |
| 3766 | |
Douglas Gregor | c746937 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 3767 | // Add the value of this argument to the list of converted |
| 3768 | // arguments. We use the bitwidth and signedness of the template |
| 3769 | // parameter. |
| 3770 | if (Arg->isValueDependent()) { |
| 3771 | // The argument is value-dependent. Create a new |
| 3772 | // TemplateArgument with the converted expression. |
| 3773 | Converted = TemplateArgument(Arg); |
| 3774 | return Owned(Arg); |
| 3775 | } |
| 3776 | |
Douglas Gregor | f80a9d5 | 2009-03-14 00:20:21 +0000 | [diff] [blame] | 3777 | QualType IntegerType = Context.getCanonicalType(ParamType); |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3778 | if (const EnumType *Enum = IntegerType->getAs<EnumType>()) |
Douglas Gregor | 02cbbd2 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 3779 | IntegerType = Context.getCanonicalType(Enum->getDecl()->getIntegerType()); |
Douglas Gregor | f80a9d5 | 2009-03-14 00:20:21 +0000 | [diff] [blame] | 3780 | |
Douglas Gregor | c746937 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 3781 | if (ParamType->isBooleanType()) { |
| 3782 | // Value must be zero or one. |
| 3783 | Value = Value != 0; |
| 3784 | unsigned AllowedBits = Context.getTypeSize(IntegerType); |
| 3785 | if (Value.getBitWidth() != AllowedBits) |
| 3786 | Value = Value.extOrTrunc(AllowedBits); |
Douglas Gregor | 575a1c9 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 3787 | Value.setIsSigned(IntegerType->isSignedIntegerOrEnumerationType()); |
Douglas Gregor | c746937 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 3788 | } else { |
Douglas Gregor | 1a6e034 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 3789 | llvm::APSInt OldValue = Value; |
Douglas Gregor | c746937 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 3790 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3791 | // Coerce the template argument's value to the value it will have |
Douglas Gregor | 1a6e034 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 3792 | // based on the template parameter's type. |
Douglas Gregor | 0d4fd8e | 2010-03-26 00:39:40 +0000 | [diff] [blame] | 3793 | unsigned AllowedBits = Context.getTypeSize(IntegerType); |
Douglas Gregor | 0d4fd8e | 2010-03-26 00:39:40 +0000 | [diff] [blame] | 3794 | if (Value.getBitWidth() != AllowedBits) |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 3795 | Value = Value.extOrTrunc(AllowedBits); |
Douglas Gregor | 575a1c9 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 3796 | Value.setIsSigned(IntegerType->isSignedIntegerOrEnumerationType()); |
Douglas Gregor | c746937 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 3797 | |
Douglas Gregor | 1a6e034 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 3798 | // Complain if an unsigned parameter received a negative value. |
Douglas Gregor | 575a1c9 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 3799 | if (IntegerType->isUnsignedIntegerOrEnumerationType() |
Douglas Gregor | c746937 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 3800 | && (OldValue.isSigned() && OldValue.isNegative())) { |
Douglas Gregor | 1a6e034 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 3801 | Diag(Arg->getSourceRange().getBegin(), diag::warn_template_arg_negative) |
| 3802 | << OldValue.toString(10) << Value.toString(10) << Param->getType() |
| 3803 | << Arg->getSourceRange(); |
| 3804 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 3805 | } |
Douglas Gregor | c746937 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 3806 | |
Douglas Gregor | 1a6e034 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 3807 | // Complain if we overflowed the template parameter's type. |
| 3808 | unsigned RequiredBits; |
Douglas Gregor | 575a1c9 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 3809 | if (IntegerType->isUnsignedIntegerOrEnumerationType()) |
Douglas Gregor | 1a6e034 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 3810 | RequiredBits = OldValue.getActiveBits(); |
| 3811 | else if (OldValue.isUnsigned()) |
| 3812 | RequiredBits = OldValue.getActiveBits() + 1; |
| 3813 | else |
| 3814 | RequiredBits = OldValue.getMinSignedBits(); |
| 3815 | if (RequiredBits > AllowedBits) { |
| 3816 | Diag(Arg->getSourceRange().getBegin(), |
| 3817 | diag::warn_template_arg_too_large) |
| 3818 | << OldValue.toString(10) << Value.toString(10) << Param->getType() |
| 3819 | << Arg->getSourceRange(); |
| 3820 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 3821 | } |
Douglas Gregor | f80a9d5 | 2009-03-14 00:20:21 +0000 | [diff] [blame] | 3822 | } |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 3823 | |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 3824 | Converted = TemplateArgument(Value, |
Douglas Gregor | 6b63f55 | 2011-08-09 01:55:14 +0000 | [diff] [blame] | 3825 | ParamType->isEnumeralType() |
| 3826 | ? Context.getCanonicalType(ParamType) |
| 3827 | : IntegerType); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3828 | return Owned(Arg); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3829 | } |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 3830 | |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 3831 | DeclAccessPair FoundResult; // temporary for ResolveOverloadedFunction |
| 3832 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3833 | // C++0x [temp.arg.nontype]p5 bullets 2, 4 and 6 permit conversion |
| 3834 | // from a template argument of type std::nullptr_t to a non-type |
| 3835 | // template parameter of type pointer to object, pointer to |
| 3836 | // function, or pointer-to-member, respectively. |
Douglas Gregor | 84ee2ee | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 3837 | if (ArgType->isNullPtrType()) { |
| 3838 | if (ParamType->isPointerType() || ParamType->isMemberPointerType()) { |
| 3839 | Converted = TemplateArgument((NamedDecl *)0); |
| 3840 | return Owned(Arg); |
| 3841 | } |
| 3842 | |
| 3843 | if (ParamType->isNullPtrType()) { |
| 3844 | llvm::APSInt Zero(Context.getTypeSize(Context.NullPtrTy), true); |
| 3845 | Converted = TemplateArgument(Zero, Context.NullPtrTy); |
| 3846 | return Owned(Arg); |
| 3847 | } |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3848 | } |
| 3849 | |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 3850 | // Handle pointer-to-function, reference-to-function, and |
| 3851 | // pointer-to-member-function all in (roughly) the same way. |
| 3852 | if (// -- For a non-type template-parameter of type pointer to |
| 3853 | // function, only the function-to-pointer conversion (4.3) is |
| 3854 | // applied. If the template-argument represents a set of |
| 3855 | // overloaded functions (or a pointer to such), the matching |
| 3856 | // function is selected from the set (13.4). |
| 3857 | (ParamType->isPointerType() && |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3858 | ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType()) || |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 3859 | // -- For a non-type template-parameter of type reference to |
| 3860 | // function, no conversions apply. If the template-argument |
| 3861 | // represents a set of overloaded functions, the matching |
| 3862 | // function is selected from the set (13.4). |
| 3863 | (ParamType->isReferenceType() && |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3864 | ParamType->getAs<ReferenceType>()->getPointeeType()->isFunctionType()) || |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 3865 | // -- For a non-type template-parameter of type pointer to |
| 3866 | // member function, no conversions apply. If the |
| 3867 | // template-argument represents a set of overloaded member |
| 3868 | // functions, the matching member function is selected from |
| 3869 | // the set (13.4). |
| 3870 | (ParamType->isMemberPointerType() && |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3871 | ParamType->getAs<MemberPointerType>()->getPointeeType() |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 3872 | ->isFunctionType())) { |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3873 | |
Douglas Gregor | 1a8cf73 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 3874 | if (Arg->getType() == Context.OverloadTy) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3875 | if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg, ParamType, |
Douglas Gregor | 1a8cf73 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 3876 | true, |
| 3877 | FoundResult)) { |
| 3878 | if (DiagnoseUseOfDecl(Fn, Arg->getSourceRange().getBegin())) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3879 | return ExprError(); |
Douglas Gregor | 1a8cf73 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 3880 | |
| 3881 | Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn); |
| 3882 | ArgType = Arg->getType(); |
| 3883 | } else |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3884 | return ExprError(); |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 3885 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3886 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3887 | if (!ParamType->isMemberPointerType()) { |
| 3888 | if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param, |
| 3889 | ParamType, |
| 3890 | Arg, Converted)) |
| 3891 | return ExprError(); |
| 3892 | return Owned(Arg); |
| 3893 | } |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3894 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3895 | bool ObjCLifetimeConversion; |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 3896 | if (IsQualificationConversion(ArgType, ParamType.getNonReferenceType(), |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3897 | false, ObjCLifetimeConversion)) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3898 | Arg = ImpCastExprToType(Arg, ParamType, CK_NoOp, CastCategory(Arg)).take(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3899 | } else if (!Context.hasSameUnqualifiedType(ArgType, |
| 3900 | ParamType.getNonReferenceType())) { |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 3901 | // We can't perform this conversion. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3902 | Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 3903 | diag::err_template_arg_not_convertible) |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 3904 | << Arg->getType() << InstantiatedParamType << Arg->getSourceRange(); |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 3905 | Diag(Param->getLocation(), diag::note_template_param_here); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3906 | return ExprError(); |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 3907 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3908 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3909 | if (CheckTemplateArgumentPointerToMember(Arg, Converted)) |
| 3910 | return ExprError(); |
| 3911 | return Owned(Arg); |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 3912 | } |
| 3913 | |
Chris Lattner | fe90de7 | 2009-02-20 21:37:53 +0000 | [diff] [blame] | 3914 | if (ParamType->isPointerType()) { |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 3915 | // -- for a non-type template-parameter of type pointer to |
| 3916 | // object, qualification conversions (4.4) and the |
| 3917 | // array-to-pointer conversion (4.2) are applied. |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 3918 | // C++0x also allows a value of std::nullptr_t. |
Eli Friedman | 1357869 | 2010-08-05 02:49:48 +0000 | [diff] [blame] | 3919 | assert(ParamType->getPointeeType()->isIncompleteOrObjectType() && |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 3920 | "Only object pointers allowed here"); |
Douglas Gregor | f684e6e | 2009-02-11 00:44:29 +0000 | [diff] [blame] | 3921 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3922 | if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param, |
| 3923 | ParamType, |
| 3924 | Arg, Converted)) |
| 3925 | return ExprError(); |
| 3926 | return Owned(Arg); |
Douglas Gregor | f684e6e | 2009-02-11 00:44:29 +0000 | [diff] [blame] | 3927 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3928 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3929 | if (const ReferenceType *ParamRefType = ParamType->getAs<ReferenceType>()) { |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 3930 | // -- For a non-type template-parameter of type reference to |
| 3931 | // object, no conversions apply. The type referred to by the |
| 3932 | // reference may be more cv-qualified than the (otherwise |
| 3933 | // identical) type of the template-argument. The |
| 3934 | // template-parameter is bound directly to the |
| 3935 | // template-argument, which must be an lvalue. |
Eli Friedman | 1357869 | 2010-08-05 02:49:48 +0000 | [diff] [blame] | 3936 | assert(ParamRefType->getPointeeType()->isIncompleteOrObjectType() && |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 3937 | "Only object references allowed here"); |
Douglas Gregor | f684e6e | 2009-02-11 00:44:29 +0000 | [diff] [blame] | 3938 | |
Douglas Gregor | 1a8cf73 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 3939 | if (Arg->getType() == Context.OverloadTy) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3940 | if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg, |
| 3941 | ParamRefType->getPointeeType(), |
Douglas Gregor | 1a8cf73 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 3942 | true, |
| 3943 | FoundResult)) { |
| 3944 | if (DiagnoseUseOfDecl(Fn, Arg->getSourceRange().getBegin())) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3945 | return ExprError(); |
Douglas Gregor | 1a8cf73 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 3946 | |
| 3947 | Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn); |
| 3948 | ArgType = Arg->getType(); |
| 3949 | } else |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3950 | return ExprError(); |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 3951 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3952 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3953 | if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param, |
| 3954 | ParamType, |
| 3955 | Arg, Converted)) |
| 3956 | return ExprError(); |
| 3957 | return Owned(Arg); |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 3958 | } |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 3959 | |
| 3960 | // -- For a non-type template-parameter of type pointer to data |
| 3961 | // member, qualification conversions (4.4) are applied. |
| 3962 | assert(ParamType->isMemberPointerType() && "Only pointers to members remain"); |
| 3963 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3964 | bool ObjCLifetimeConversion; |
Douglas Gregor | 8e6563b | 2009-02-11 18:22:40 +0000 | [diff] [blame] | 3965 | if (Context.hasSameUnqualifiedType(ParamType, ArgType)) { |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 3966 | // Types match exactly: nothing more to do here. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3967 | } else if (IsQualificationConversion(ArgType, ParamType, false, |
| 3968 | ObjCLifetimeConversion)) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3969 | Arg = ImpCastExprToType(Arg, ParamType, CK_NoOp, CastCategory(Arg)).take(); |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 3970 | } else { |
| 3971 | // We can't perform this conversion. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3972 | Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 3973 | diag::err_template_arg_not_convertible) |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 3974 | << Arg->getType() << InstantiatedParamType << Arg->getSourceRange(); |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 3975 | Diag(Param->getLocation(), diag::note_template_param_here); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3976 | return ExprError(); |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 3977 | } |
| 3978 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3979 | if (CheckTemplateArgumentPointerToMember(Arg, Converted)) |
| 3980 | return ExprError(); |
| 3981 | return Owned(Arg); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3982 | } |
| 3983 | |
| 3984 | /// \brief Check a template argument against its corresponding |
| 3985 | /// template template parameter. |
| 3986 | /// |
| 3987 | /// This routine implements the semantics of C++ [temp.arg.template]. |
| 3988 | /// It returns true if an error occurred, and false otherwise. |
| 3989 | bool Sema::CheckTemplateArgument(TemplateTemplateParmDecl *Param, |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 3990 | const TemplateArgumentLoc &Arg) { |
| 3991 | TemplateName Name = Arg.getArgument().getAsTemplate(); |
| 3992 | TemplateDecl *Template = Name.getAsTemplateDecl(); |
| 3993 | if (!Template) { |
| 3994 | // Any dependent template name is fine. |
| 3995 | assert(Name.isDependent() && "Non-dependent template isn't a declaration?"); |
| 3996 | return false; |
| 3997 | } |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 3998 | |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3999 | // C++0x [temp.arg.template]p1: |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 4000 | // A template-argument for a template template-parameter shall be |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 4001 | // the name of a class template or an alias template, expressed as an |
| 4002 | // id-expression. When the template-argument names a class template, only |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 4003 | // primary class templates are considered when matching the |
| 4004 | // template template argument with the corresponding parameter; |
| 4005 | // partial specializations are not considered even if their |
| 4006 | // parameter lists match that of the template template parameter. |
Douglas Gregor | ba1ecb5 | 2009-06-12 19:43:02 +0000 | [diff] [blame] | 4007 | // |
| 4008 | // Note that we also allow template template parameters here, which |
| 4009 | // will happen when we are dealing with, e.g., class template |
| 4010 | // partial specializations. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4011 | if (!isa<ClassTemplateDecl>(Template) && |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 4012 | !isa<TemplateTemplateParmDecl>(Template) && |
| 4013 | !isa<TypeAliasTemplateDecl>(Template)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4014 | assert(isa<FunctionTemplateDecl>(Template) && |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 4015 | "Only function templates are possible here"); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 4016 | Diag(Arg.getLocation(), diag::err_template_arg_not_class_template); |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 4017 | Diag(Template->getLocation(), diag::note_template_arg_refers_here_func) |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 4018 | << Template; |
| 4019 | } |
| 4020 | |
| 4021 | return !TemplateParameterListsAreEqual(Template->getTemplateParameters(), |
| 4022 | Param->getTemplateParameters(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4023 | true, |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 4024 | TPL_TemplateTemplateArgumentMatch, |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 4025 | Arg.getLocation()); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 4026 | } |
| 4027 | |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4028 | /// \brief Given a non-type template argument that refers to a |
| 4029 | /// declaration and the type of its corresponding non-type template |
| 4030 | /// parameter, produce an expression that properly refers to that |
| 4031 | /// declaration. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4032 | ExprResult |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4033 | Sema::BuildExpressionFromDeclTemplateArgument(const TemplateArgument &Arg, |
| 4034 | QualType ParamType, |
| 4035 | SourceLocation Loc) { |
| 4036 | assert(Arg.getKind() == TemplateArgument::Declaration && |
| 4037 | "Only declaration template arguments permitted here"); |
| 4038 | ValueDecl *VD = cast<ValueDecl>(Arg.getAsDecl()); |
| 4039 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4040 | if (VD->getDeclContext()->isRecord() && |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4041 | (isa<CXXMethodDecl>(VD) || isa<FieldDecl>(VD))) { |
| 4042 | // If the value is a class member, we might have a pointer-to-member. |
| 4043 | // Determine whether the non-type template template parameter is of |
| 4044 | // pointer-to-member type. If so, we need to build an appropriate |
| 4045 | // expression for a pointer-to-member, since a "normal" DeclRefExpr |
| 4046 | // would refer to the member itself. |
| 4047 | if (ParamType->isMemberPointerType()) { |
| 4048 | QualType ClassType |
| 4049 | = Context.getTypeDeclType(cast<RecordDecl>(VD->getDeclContext())); |
| 4050 | NestedNameSpecifier *Qualifier |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4051 | = NestedNameSpecifier::Create(Context, 0, false, |
| 4052 | ClassType.getTypePtr()); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4053 | CXXScopeSpec SS; |
Douglas Gregor | c34348a | 2011-02-24 17:54:50 +0000 | [diff] [blame] | 4054 | SS.MakeTrivial(Context, Qualifier, Loc); |
John McCall | dfa1edb | 2010-11-23 20:48:44 +0000 | [diff] [blame] | 4055 | |
| 4056 | // The actual value-ness of this is unimportant, but for |
| 4057 | // internal consistency's sake, references to instance methods |
| 4058 | // are r-values. |
| 4059 | ExprValueKind VK = VK_LValue; |
| 4060 | if (isa<CXXMethodDecl>(VD) && cast<CXXMethodDecl>(VD)->isInstance()) |
| 4061 | VK = VK_RValue; |
| 4062 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4063 | ExprResult RefExpr = BuildDeclRefExpr(VD, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4064 | VD->getType().getNonReferenceType(), |
John McCall | dfa1edb | 2010-11-23 20:48:44 +0000 | [diff] [blame] | 4065 | VK, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4066 | Loc, |
| 4067 | &SS); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4068 | if (RefExpr.isInvalid()) |
| 4069 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4070 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4071 | RefExpr = CreateBuiltinUnaryOp(Loc, UO_AddrOf, RefExpr.get()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4072 | |
Douglas Gregor | c0c8300 | 2010-04-30 21:46:38 +0000 | [diff] [blame] | 4073 | // We might need to perform a trailing qualification conversion, since |
| 4074 | // the element type on the parameter could be more qualified than the |
| 4075 | // element type in the expression we constructed. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4076 | bool ObjCLifetimeConversion; |
Douglas Gregor | c0c8300 | 2010-04-30 21:46:38 +0000 | [diff] [blame] | 4077 | if (IsQualificationConversion(((Expr*) RefExpr.get())->getType(), |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4078 | ParamType.getUnqualifiedType(), false, |
| 4079 | ObjCLifetimeConversion)) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4080 | RefExpr = ImpCastExprToType(RefExpr.take(), ParamType.getUnqualifiedType(), CK_NoOp); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4081 | |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4082 | assert(!RefExpr.isInvalid() && |
| 4083 | Context.hasSameType(((Expr*) RefExpr.get())->getType(), |
Douglas Gregor | c0c8300 | 2010-04-30 21:46:38 +0000 | [diff] [blame] | 4084 | ParamType.getUnqualifiedType())); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4085 | return move(RefExpr); |
| 4086 | } |
| 4087 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4088 | |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4089 | QualType T = VD->getType().getNonReferenceType(); |
| 4090 | if (ParamType->isPointerType()) { |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4091 | // When the non-type template parameter is a pointer, take the |
| 4092 | // address of the declaration. |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4093 | ExprResult RefExpr = BuildDeclRefExpr(VD, T, VK_LValue, Loc); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4094 | if (RefExpr.isInvalid()) |
| 4095 | return ExprError(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4096 | |
| 4097 | if (T->isFunctionType() || T->isArrayType()) { |
| 4098 | // Decay functions and arrays. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4099 | RefExpr = DefaultFunctionArrayConversion(RefExpr.take()); |
| 4100 | if (RefExpr.isInvalid()) |
| 4101 | return ExprError(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4102 | |
| 4103 | return move(RefExpr); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4104 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4105 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4106 | // Take the address of everything else |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4107 | return CreateBuiltinUnaryOp(Loc, UO_AddrOf, RefExpr.get()); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4108 | } |
| 4109 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4110 | ExprValueKind VK = VK_RValue; |
| 4111 | |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4112 | // If the non-type template parameter has reference type, qualify the |
| 4113 | // resulting declaration reference with the extra qualifiers on the |
| 4114 | // type that the reference refers to. |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4115 | if (const ReferenceType *TargetRef = ParamType->getAs<ReferenceType>()) { |
| 4116 | VK = VK_LValue; |
| 4117 | T = Context.getQualifiedType(T, |
| 4118 | TargetRef->getPointeeType().getQualifiers()); |
| 4119 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4120 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4121 | return BuildDeclRefExpr(VD, T, VK, Loc); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4122 | } |
| 4123 | |
| 4124 | /// \brief Construct a new expression that refers to the given |
| 4125 | /// integral template argument with the given source-location |
| 4126 | /// information. |
| 4127 | /// |
| 4128 | /// This routine takes care of the mapping from an integral template |
| 4129 | /// argument (which may have any integral type) to the appropriate |
| 4130 | /// literal value. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4131 | ExprResult |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4132 | Sema::BuildExpressionFromIntegralTemplateArgument(const TemplateArgument &Arg, |
| 4133 | SourceLocation Loc) { |
| 4134 | assert(Arg.getKind() == TemplateArgument::Integral && |
Douglas Gregor | d373119 | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 4135 | "Operation is only valid for integral template arguments"); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4136 | QualType T = Arg.getIntegralType(); |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 4137 | if (T->isAnyCharacterType()) { |
| 4138 | CharacterLiteral::CharacterKind Kind; |
| 4139 | if (T->isWideCharType()) |
| 4140 | Kind = CharacterLiteral::Wide; |
| 4141 | else if (T->isChar16Type()) |
| 4142 | Kind = CharacterLiteral::UTF16; |
| 4143 | else if (T->isChar32Type()) |
| 4144 | Kind = CharacterLiteral::UTF32; |
| 4145 | else |
| 4146 | Kind = CharacterLiteral::Ascii; |
| 4147 | |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4148 | return Owned(new (Context) CharacterLiteral( |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 4149 | Arg.getAsIntegral()->getZExtValue(), |
| 4150 | Kind, T, Loc)); |
| 4151 | } |
| 4152 | |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4153 | if (T->isBooleanType()) |
| 4154 | return Owned(new (Context) CXXBoolLiteralExpr( |
| 4155 | Arg.getAsIntegral()->getBoolValue(), |
Chris Lattner | 223de24 | 2011-04-25 20:37:58 +0000 | [diff] [blame] | 4156 | T, Loc)); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4157 | |
Douglas Gregor | 84ee2ee | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 4158 | if (T->isNullPtrType()) |
| 4159 | return Owned(new (Context) CXXNullPtrLiteralExpr(Context.NullPtrTy, Loc)); |
| 4160 | |
Chris Lattner | 223de24 | 2011-04-25 20:37:58 +0000 | [diff] [blame] | 4161 | // If this is an enum type that we're instantiating, we need to use an integer |
| 4162 | // type the same size as the enumerator. We don't want to build an |
| 4163 | // IntegerLiteral with enum type. |
Peter Collingbourne | fb7b363 | 2010-12-15 15:06:14 +0000 | [diff] [blame] | 4164 | QualType BT; |
| 4165 | if (const EnumType *ET = T->getAs<EnumType>()) |
Chris Lattner | 223de24 | 2011-04-25 20:37:58 +0000 | [diff] [blame] | 4166 | BT = ET->getDecl()->getIntegerType(); |
Peter Collingbourne | fb7b363 | 2010-12-15 15:06:14 +0000 | [diff] [blame] | 4167 | else |
| 4168 | BT = T; |
| 4169 | |
John McCall | 4e9272d | 2011-07-15 07:47:58 +0000 | [diff] [blame] | 4170 | Expr *E = IntegerLiteral::Create(Context, *Arg.getAsIntegral(), BT, Loc); |
| 4171 | if (T->isEnumeralType()) { |
| 4172 | // FIXME: This is a hack. We need a better way to handle substituted |
| 4173 | // non-type template parameters. |
| 4174 | E = CStyleCastExpr::Create(Context, T, VK_RValue, CK_IntegralCast, E, 0, |
| 4175 | Context.getTrivialTypeSourceInfo(T, Loc), |
| 4176 | Loc, Loc); |
| 4177 | } |
| 4178 | |
| 4179 | return Owned(E); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4180 | } |
| 4181 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4182 | /// \brief Match two template parameters within template parameter lists. |
| 4183 | static bool MatchTemplateParameterKind(Sema &S, NamedDecl *New, NamedDecl *Old, |
| 4184 | bool Complain, |
| 4185 | Sema::TemplateParameterListEqualKind Kind, |
| 4186 | SourceLocation TemplateArgLoc) { |
| 4187 | // Check the actual kind (type, non-type, template). |
| 4188 | if (Old->getKind() != New->getKind()) { |
| 4189 | if (Complain) { |
| 4190 | unsigned NextDiag = diag::err_template_param_different_kind; |
| 4191 | if (TemplateArgLoc.isValid()) { |
| 4192 | S.Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch); |
| 4193 | NextDiag = diag::note_template_param_different_kind; |
| 4194 | } |
| 4195 | S.Diag(New->getLocation(), NextDiag) |
| 4196 | << (Kind != Sema::TPL_TemplateMatch); |
| 4197 | S.Diag(Old->getLocation(), diag::note_template_prev_declaration) |
| 4198 | << (Kind != Sema::TPL_TemplateMatch); |
| 4199 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4200 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4201 | return false; |
| 4202 | } |
| 4203 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4204 | // Check that both are parameter packs are neither are parameter packs. |
| 4205 | // However, if we are matching a template template argument to a |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4206 | // template template parameter, the template template parameter can have |
| 4207 | // a parameter pack where the template template argument does not. |
| 4208 | if (Old->isTemplateParameterPack() != New->isTemplateParameterPack() && |
| 4209 | !(Kind == Sema::TPL_TemplateTemplateArgumentMatch && |
| 4210 | Old->isTemplateParameterPack())) { |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4211 | if (Complain) { |
| 4212 | unsigned NextDiag = diag::err_template_parameter_pack_non_pack; |
| 4213 | if (TemplateArgLoc.isValid()) { |
| 4214 | S.Diag(TemplateArgLoc, |
| 4215 | diag::err_template_arg_template_params_mismatch); |
| 4216 | NextDiag = diag::note_template_parameter_pack_non_pack; |
| 4217 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4218 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4219 | unsigned ParamKind = isa<TemplateTypeParmDecl>(New)? 0 |
| 4220 | : isa<NonTypeTemplateParmDecl>(New)? 1 |
| 4221 | : 2; |
| 4222 | S.Diag(New->getLocation(), NextDiag) |
| 4223 | << ParamKind << New->isParameterPack(); |
| 4224 | S.Diag(Old->getLocation(), diag::note_template_parameter_pack_here) |
| 4225 | << ParamKind << Old->isParameterPack(); |
| 4226 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4227 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4228 | return false; |
| 4229 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4230 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4231 | // For non-type template parameters, check the type of the parameter. |
| 4232 | if (NonTypeTemplateParmDecl *OldNTTP |
| 4233 | = dyn_cast<NonTypeTemplateParmDecl>(Old)) { |
| 4234 | NonTypeTemplateParmDecl *NewNTTP = cast<NonTypeTemplateParmDecl>(New); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4235 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4236 | // If we are matching a template template argument to a template |
| 4237 | // template parameter and one of the non-type template parameter types |
| 4238 | // is dependent, then we must wait until template instantiation time |
| 4239 | // to actually compare the arguments. |
| 4240 | if (Kind == Sema::TPL_TemplateTemplateArgumentMatch && |
| 4241 | (OldNTTP->getType()->isDependentType() || |
| 4242 | NewNTTP->getType()->isDependentType())) |
| 4243 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4244 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4245 | if (!S.Context.hasSameType(OldNTTP->getType(), NewNTTP->getType())) { |
| 4246 | if (Complain) { |
| 4247 | unsigned NextDiag = diag::err_template_nontype_parm_different_type; |
| 4248 | if (TemplateArgLoc.isValid()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4249 | S.Diag(TemplateArgLoc, |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4250 | diag::err_template_arg_template_params_mismatch); |
| 4251 | NextDiag = diag::note_template_nontype_parm_different_type; |
| 4252 | } |
| 4253 | S.Diag(NewNTTP->getLocation(), NextDiag) |
| 4254 | << NewNTTP->getType() |
| 4255 | << (Kind != Sema::TPL_TemplateMatch); |
| 4256 | S.Diag(OldNTTP->getLocation(), |
| 4257 | diag::note_template_nontype_parm_prev_declaration) |
| 4258 | << OldNTTP->getType(); |
| 4259 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4260 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4261 | return false; |
| 4262 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4263 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4264 | return true; |
| 4265 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4266 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4267 | // For template template parameters, check the template parameter types. |
| 4268 | // The template parameter lists of template template |
| 4269 | // parameters must agree. |
| 4270 | if (TemplateTemplateParmDecl *OldTTP |
| 4271 | = dyn_cast<TemplateTemplateParmDecl>(Old)) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4272 | TemplateTemplateParmDecl *NewTTP = cast<TemplateTemplateParmDecl>(New); |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4273 | return S.TemplateParameterListsAreEqual(NewTTP->getTemplateParameters(), |
| 4274 | OldTTP->getTemplateParameters(), |
| 4275 | Complain, |
| 4276 | (Kind == Sema::TPL_TemplateMatch |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4277 | ? Sema::TPL_TemplateTemplateParmMatch |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4278 | : Kind), |
| 4279 | TemplateArgLoc); |
| 4280 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4281 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4282 | return true; |
| 4283 | } |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4284 | |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4285 | /// \brief Diagnose a known arity mismatch when comparing template argument |
| 4286 | /// lists. |
| 4287 | static |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4288 | void DiagnoseTemplateParameterListArityMismatch(Sema &S, |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4289 | TemplateParameterList *New, |
| 4290 | TemplateParameterList *Old, |
| 4291 | Sema::TemplateParameterListEqualKind Kind, |
| 4292 | SourceLocation TemplateArgLoc) { |
| 4293 | unsigned NextDiag = diag::err_template_param_list_different_arity; |
| 4294 | if (TemplateArgLoc.isValid()) { |
| 4295 | S.Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch); |
| 4296 | NextDiag = diag::note_template_param_list_different_arity; |
| 4297 | } |
| 4298 | S.Diag(New->getTemplateLoc(), NextDiag) |
| 4299 | << (New->size() > Old->size()) |
| 4300 | << (Kind != Sema::TPL_TemplateMatch) |
| 4301 | << SourceRange(New->getTemplateLoc(), New->getRAngleLoc()); |
| 4302 | S.Diag(Old->getTemplateLoc(), diag::note_template_prev_declaration) |
| 4303 | << (Kind != Sema::TPL_TemplateMatch) |
| 4304 | << SourceRange(Old->getTemplateLoc(), Old->getRAngleLoc()); |
| 4305 | } |
| 4306 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4307 | /// \brief Determine whether the given template parameter lists are |
| 4308 | /// equivalent. |
| 4309 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4310 | /// \param New The new template parameter list, typically written in the |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4311 | /// source code as part of a new template declaration. |
| 4312 | /// |
| 4313 | /// \param Old The old template parameter list, typically found via |
| 4314 | /// name lookup of the template declared with this template parameter |
| 4315 | /// list. |
| 4316 | /// |
| 4317 | /// \param Complain If true, this routine will produce a diagnostic if |
| 4318 | /// the template parameter lists are not equivalent. |
| 4319 | /// |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 4320 | /// \param Kind describes how we are to match the template parameter lists. |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 4321 | /// |
| 4322 | /// \param TemplateArgLoc If this source location is valid, then we |
| 4323 | /// are actually checking the template parameter list of a template |
| 4324 | /// argument (New) against the template parameter list of its |
| 4325 | /// corresponding template template parameter (Old). We produce |
| 4326 | /// slightly different diagnostics in this scenario. |
| 4327 | /// |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4328 | /// \returns True if the template parameter lists are equal, false |
| 4329 | /// otherwise. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4330 | bool |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4331 | Sema::TemplateParameterListsAreEqual(TemplateParameterList *New, |
| 4332 | TemplateParameterList *Old, |
| 4333 | bool Complain, |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 4334 | TemplateParameterListEqualKind Kind, |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 4335 | SourceLocation TemplateArgLoc) { |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4336 | if (Old->size() != New->size() && Kind != TPL_TemplateTemplateArgumentMatch) { |
| 4337 | if (Complain) |
| 4338 | DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind, |
| 4339 | TemplateArgLoc); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4340 | |
| 4341 | return false; |
| 4342 | } |
| 4343 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4344 | // C++0x [temp.arg.template]p3: |
| 4345 | // A template-argument matches a template template-parameter (call it P) |
NAKAMURA Takumi | 0099530 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 4346 | // when each of the template parameters in the template-parameter-list of |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 4347 | // the template-argument's corresponding class template or alias template |
NAKAMURA Takumi | 0099530 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 4348 | // (call it A) matches the corresponding template parameter in the |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4349 | // template-parameter-list of P. [...] |
| 4350 | TemplateParameterList::iterator NewParm = New->begin(); |
| 4351 | TemplateParameterList::iterator NewParmEnd = New->end(); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4352 | for (TemplateParameterList::iterator OldParm = Old->begin(), |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4353 | OldParmEnd = Old->end(); |
| 4354 | OldParm != OldParmEnd; ++OldParm) { |
Douglas Gregor | c421f54 | 2011-01-13 18:47:47 +0000 | [diff] [blame] | 4355 | if (Kind != TPL_TemplateTemplateArgumentMatch || |
| 4356 | !(*OldParm)->isTemplateParameterPack()) { |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4357 | if (NewParm == NewParmEnd) { |
| 4358 | if (Complain) |
| 4359 | DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind, |
| 4360 | TemplateArgLoc); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4361 | |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4362 | return false; |
| 4363 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4364 | |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4365 | if (!MatchTemplateParameterKind(*this, *NewParm, *OldParm, Complain, |
| 4366 | Kind, TemplateArgLoc)) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4367 | return false; |
| 4368 | |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4369 | ++NewParm; |
| 4370 | continue; |
| 4371 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4372 | |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4373 | // C++0x [temp.arg.template]p3: |
NAKAMURA Takumi | 0099530 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 4374 | // [...] When P's template- parameter-list contains a template parameter |
| 4375 | // pack (14.5.3), the template parameter pack will match zero or more |
| 4376 | // template parameters or template parameter packs in the |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4377 | // template-parameter-list of A with the same type and form as the |
| 4378 | // template parameter pack in P (ignoring whether those template |
| 4379 | // parameters are template parameter packs). |
| 4380 | for (; NewParm != NewParmEnd; ++NewParm) { |
| 4381 | if (!MatchTemplateParameterKind(*this, *NewParm, *OldParm, Complain, |
| 4382 | Kind, TemplateArgLoc)) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4383 | return false; |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4384 | } |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4385 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4386 | |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4387 | // Make sure we exhausted all of the arguments. |
| 4388 | if (NewParm != NewParmEnd) { |
| 4389 | if (Complain) |
| 4390 | DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind, |
| 4391 | TemplateArgLoc); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4392 | |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4393 | return false; |
| 4394 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4395 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4396 | return true; |
| 4397 | } |
| 4398 | |
| 4399 | /// \brief Check whether a template can be declared within this scope. |
| 4400 | /// |
| 4401 | /// If the template declaration is valid in this scope, returns |
| 4402 | /// false. Otherwise, issues a diagnostic and returns true. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4403 | bool |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4404 | Sema::CheckTemplateDeclScope(Scope *S, TemplateParameterList *TemplateParams) { |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4405 | // Find the nearest enclosing declaration scope. |
| 4406 | while ((S->getFlags() & Scope::DeclScope) == 0 || |
| 4407 | (S->getFlags() & Scope::TemplateParamScope) != 0) |
| 4408 | S = S->getParent(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4409 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4410 | // C++ [temp]p2: |
| 4411 | // A template-declaration can appear only as a namespace scope or |
| 4412 | // class scope declaration. |
| 4413 | DeclContext *Ctx = static_cast<DeclContext *>(S->getEntity()); |
Eli Friedman | 1503f77 | 2009-07-31 01:43:05 +0000 | [diff] [blame] | 4414 | if (Ctx && isa<LinkageSpecDecl>(Ctx) && |
| 4415 | cast<LinkageSpecDecl>(Ctx)->getLanguage() != LinkageSpecDecl::lang_cxx) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4416 | return Diag(TemplateParams->getTemplateLoc(), diag::err_template_linkage) |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4417 | << TemplateParams->getSourceRange(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4418 | |
Eli Friedman | 1503f77 | 2009-07-31 01:43:05 +0000 | [diff] [blame] | 4419 | while (Ctx && isa<LinkageSpecDecl>(Ctx)) |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4420 | Ctx = Ctx->getParent(); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4421 | |
| 4422 | if (Ctx && (Ctx->isFileContext() || Ctx->isRecord())) |
| 4423 | return false; |
| 4424 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4425 | return Diag(TemplateParams->getTemplateLoc(), |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4426 | diag::err_template_outside_namespace_or_class_scope) |
| 4427 | << TemplateParams->getSourceRange(); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4428 | } |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4429 | |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4430 | /// \brief Determine what kind of template specialization the given declaration |
| 4431 | /// is. |
| 4432 | static TemplateSpecializationKind getTemplateSpecializationKind(NamedDecl *D) { |
| 4433 | if (!D) |
| 4434 | return TSK_Undeclared; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4435 | |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 4436 | if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) |
| 4437 | return Record->getTemplateSpecializationKind(); |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4438 | if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) |
| 4439 | return Function->getTemplateSpecializationKind(); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4440 | if (VarDecl *Var = dyn_cast<VarDecl>(D)) |
| 4441 | return Var->getTemplateSpecializationKind(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4442 | |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4443 | return TSK_Undeclared; |
| 4444 | } |
| 4445 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4446 | /// \brief Check whether a specialization is well-formed in the current |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4447 | /// context. |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4448 | /// |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4449 | /// This routine determines whether a template specialization can be declared |
| 4450 | /// in the current context (C++ [temp.expl.spec]p2). |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4451 | /// |
| 4452 | /// \param S the semantic analysis object for which this check is being |
| 4453 | /// performed. |
| 4454 | /// |
| 4455 | /// \param Specialized the entity being specialized or instantiated, which |
| 4456 | /// may be a kind of template (class template, function template, etc.) or |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4457 | /// a member of a class template (member function, static data member, |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4458 | /// member class). |
| 4459 | /// |
| 4460 | /// \param PrevDecl the previous declaration of this entity, if any. |
| 4461 | /// |
| 4462 | /// \param Loc the location of the explicit specialization or instantiation of |
| 4463 | /// this entity. |
| 4464 | /// |
| 4465 | /// \param IsPartialSpecialization whether this is a partial specialization of |
| 4466 | /// a class template. |
| 4467 | /// |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4468 | /// \returns true if there was an error that we cannot recover from, false |
| 4469 | /// otherwise. |
| 4470 | static bool CheckTemplateSpecializationScope(Sema &S, |
| 4471 | NamedDecl *Specialized, |
| 4472 | NamedDecl *PrevDecl, |
| 4473 | SourceLocation Loc, |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4474 | bool IsPartialSpecialization) { |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4475 | // Keep these "kind" numbers in sync with the %select statements in the |
| 4476 | // various diagnostics emitted by this routine. |
| 4477 | int EntityKind = 0; |
Ted Kremenek | fe62b06 | 2011-01-14 22:31:36 +0000 | [diff] [blame] | 4478 | if (isa<ClassTemplateDecl>(Specialized)) |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4479 | EntityKind = IsPartialSpecialization? 1 : 0; |
Ted Kremenek | fe62b06 | 2011-01-14 22:31:36 +0000 | [diff] [blame] | 4480 | else if (isa<FunctionTemplateDecl>(Specialized)) |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4481 | EntityKind = 2; |
Ted Kremenek | fe62b06 | 2011-01-14 22:31:36 +0000 | [diff] [blame] | 4482 | else if (isa<CXXMethodDecl>(Specialized)) |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4483 | EntityKind = 3; |
| 4484 | else if (isa<VarDecl>(Specialized)) |
| 4485 | EntityKind = 4; |
| 4486 | else if (isa<RecordDecl>(Specialized)) |
| 4487 | EntityKind = 5; |
| 4488 | else { |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4489 | S.Diag(Loc, diag::err_template_spec_unknown_kind); |
| 4490 | S.Diag(Specialized->getLocation(), diag::note_specialized_entity); |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4491 | return true; |
| 4492 | } |
| 4493 | |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4494 | // C++ [temp.expl.spec]p2: |
| 4495 | // An explicit specialization shall be declared in the namespace |
| 4496 | // of which the template is a member, or, for member templates, in |
| 4497 | // the namespace of which the enclosing class or enclosing class |
| 4498 | // template is a member. An explicit specialization of a member |
| 4499 | // function, member class or static data member of a class |
| 4500 | // template shall be declared in the namespace of which the class |
| 4501 | // template is a member. Such a declaration may also be a |
| 4502 | // definition. If the declaration is not a definition, the |
| 4503 | // specialization may be defined later in the name- space in which |
| 4504 | // the explicit specialization was declared, or in a namespace |
| 4505 | // that encloses the one in which the explicit specialization was |
| 4506 | // declared. |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 4507 | if (S.CurContext->getRedeclContext()->isFunctionOrMethod()) { |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4508 | S.Diag(Loc, diag::err_template_spec_decl_function_scope) |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4509 | << Specialized; |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4510 | return true; |
| 4511 | } |
Douglas Gregor | 7974c3b | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 4512 | |
Douglas Gregor | 0a40747 | 2009-10-07 17:30:37 +0000 | [diff] [blame] | 4513 | if (S.CurContext->isRecord() && !IsPartialSpecialization) { |
Francois Pichet | af0f4d0 | 2011-08-14 03:52:19 +0000 | [diff] [blame^] | 4514 | if (S.getLangOptions().Microsoft) { |
| 4515 | // Do not warn for class scope explicit specialization during |
| 4516 | // instantiation, warning was already emitted during pattern |
| 4517 | // semantic analysis. |
| 4518 | if (!S.ActiveTemplateInstantiations.size()) |
| 4519 | S.Diag(Loc, diag::ext_function_specialization_in_class) |
| 4520 | << Specialized; |
| 4521 | } else { |
| 4522 | S.Diag(Loc, diag::err_template_spec_decl_class_scope) |
| 4523 | << Specialized; |
| 4524 | return true; |
| 4525 | } |
Douglas Gregor | 0a40747 | 2009-10-07 17:30:37 +0000 | [diff] [blame] | 4526 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4527 | |
Douglas Gregor | 7974c3b | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 4528 | // C++ [temp.class.spec]p6: |
| 4529 | // A class template partial specialization may be declared or redeclared |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4530 | // in any namespace scope in which its definition may be defined (14.5.1 |
| 4531 | // and 14.5.2). |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4532 | bool ComplainedAboutScope = false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4533 | DeclContext *SpecializedContext |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4534 | = Specialized->getDeclContext()->getEnclosingNamespaceContext(); |
Douglas Gregor | 7974c3b | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 4535 | DeclContext *DC = S.CurContext->getEnclosingNamespaceContext(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4536 | if ((!PrevDecl || |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4537 | getTemplateSpecializationKind(PrevDecl) == TSK_Undeclared || |
| 4538 | getTemplateSpecializationKind(PrevDecl) == TSK_ImplicitInstantiation)){ |
Douglas Gregor | 121dc9a | 2010-09-12 05:08:28 +0000 | [diff] [blame] | 4539 | // C++ [temp.exp.spec]p2: |
| 4540 | // An explicit specialization shall be declared in the namespace of which |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4541 | // the template is a member, or, for member templates, in the namespace |
Douglas Gregor | 121dc9a | 2010-09-12 05:08:28 +0000 | [diff] [blame] | 4542 | // of which the enclosing class or enclosing class template is a member. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4543 | // An explicit specialization of a member function, member class or |
| 4544 | // static data member of a class template shall be declared in the |
Douglas Gregor | 121dc9a | 2010-09-12 05:08:28 +0000 | [diff] [blame] | 4545 | // namespace of which the class template is a member. |
| 4546 | // |
| 4547 | // C++0x [temp.expl.spec]p2: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4548 | // An explicit specialization shall be declared in a namespace enclosing |
Douglas Gregor | 121dc9a | 2010-09-12 05:08:28 +0000 | [diff] [blame] | 4549 | // the specialized template. |
| 4550 | if (!DC->InEnclosingNamespaceSetOf(SpecializedContext) && |
| 4551 | !(S.getLangOptions().CPlusPlus0x && DC->Encloses(SpecializedContext))) { |
Douglas Gregor | a4d5de5 | 2010-09-12 05:24:55 +0000 | [diff] [blame] | 4552 | bool IsCPlusPlus0xExtension |
| 4553 | = !S.getLangOptions().CPlusPlus0x && DC->Encloses(SpecializedContext); |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4554 | if (isa<TranslationUnitDecl>(SpecializedContext)) |
Douglas Gregor | a4d5de5 | 2010-09-12 05:24:55 +0000 | [diff] [blame] | 4555 | S.Diag(Loc, IsCPlusPlus0xExtension |
| 4556 | ? diag::ext_template_spec_decl_out_of_scope_global |
| 4557 | : diag::err_template_spec_decl_out_of_scope_global) |
| 4558 | << EntityKind << Specialized; |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4559 | else if (isa<NamespaceDecl>(SpecializedContext)) |
Douglas Gregor | a4d5de5 | 2010-09-12 05:24:55 +0000 | [diff] [blame] | 4560 | S.Diag(Loc, IsCPlusPlus0xExtension |
| 4561 | ? diag::ext_template_spec_decl_out_of_scope |
| 4562 | : diag::err_template_spec_decl_out_of_scope) |
| 4563 | << EntityKind << Specialized |
| 4564 | << cast<NamedDecl>(SpecializedContext); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4565 | |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4566 | S.Diag(Specialized->getLocation(), diag::note_specialized_entity); |
| 4567 | ComplainedAboutScope = true; |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4568 | } |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4569 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4570 | |
| 4571 | // Make sure that this redeclaration (or definition) occurs in an enclosing |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4572 | // namespace. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4573 | // Note that HandleDeclarator() performs this check for explicit |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4574 | // specializations of function templates, static data members, and member |
| 4575 | // functions, so we skip the check here for those kinds of entities. |
| 4576 | // FIXME: HandleDeclarator's diagnostics aren't quite as good, though. |
Douglas Gregor | 7974c3b | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 4577 | // Should we refactor that check, so that it occurs later? |
| 4578 | if (!ComplainedAboutScope && !DC->Encloses(SpecializedContext) && |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4579 | !(isa<FunctionTemplateDecl>(Specialized) || isa<VarDecl>(Specialized) || |
| 4580 | isa<FunctionDecl>(Specialized))) { |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4581 | if (isa<TranslationUnitDecl>(SpecializedContext)) |
| 4582 | S.Diag(Loc, diag::err_template_spec_redecl_global_scope) |
| 4583 | << EntityKind << Specialized; |
| 4584 | else if (isa<NamespaceDecl>(SpecializedContext)) |
| 4585 | S.Diag(Loc, diag::err_template_spec_redecl_out_of_scope) |
| 4586 | << EntityKind << Specialized |
| 4587 | << cast<NamedDecl>(SpecializedContext); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4588 | |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4589 | S.Diag(Specialized->getLocation(), diag::note_specialized_entity); |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4590 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4591 | |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4592 | // FIXME: check for specialization-after-instantiation errors and such. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4593 | |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4594 | return false; |
| 4595 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4596 | |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4597 | /// \brief Subroutine of Sema::CheckClassTemplatePartialSpecializationArgs |
| 4598 | /// that checks non-type template partial specialization arguments. |
| 4599 | static bool CheckNonTypeClassTemplatePartialSpecializationArgs(Sema &S, |
| 4600 | NonTypeTemplateParmDecl *Param, |
| 4601 | const TemplateArgument *Args, |
| 4602 | unsigned NumArgs) { |
| 4603 | for (unsigned I = 0; I != NumArgs; ++I) { |
| 4604 | if (Args[I].getKind() == TemplateArgument::Pack) { |
| 4605 | if (CheckNonTypeClassTemplatePartialSpecializationArgs(S, Param, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4606 | Args[I].pack_begin(), |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4607 | Args[I].pack_size())) |
| 4608 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4609 | |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4610 | continue; |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4611 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4612 | |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4613 | Expr *ArgExpr = Args[I].getAsExpr(); |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 4614 | if (!ArgExpr) { |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4615 | continue; |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 4616 | } |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4617 | |
Douglas Gregor | 7a21fd4 | 2011-01-03 21:37:45 +0000 | [diff] [blame] | 4618 | // We can have a pack expansion of any of the bullets below. |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4619 | if (PackExpansionExpr *Expansion = dyn_cast<PackExpansionExpr>(ArgExpr)) |
| 4620 | ArgExpr = Expansion->getPattern(); |
Douglas Gregor | 54c53cc | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 4621 | |
| 4622 | // Strip off any implicit casts we added as part of type checking. |
| 4623 | while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(ArgExpr)) |
| 4624 | ArgExpr = ICE->getSubExpr(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4625 | |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4626 | // C++ [temp.class.spec]p8: |
| 4627 | // A non-type argument is non-specialized if it is the name of a |
| 4628 | // non-type parameter. All other non-type arguments are |
| 4629 | // specialized. |
| 4630 | // |
| 4631 | // Below, we check the two conditions that only apply to |
| 4632 | // specialized non-type arguments, so skip any non-specialized |
| 4633 | // arguments. |
| 4634 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ArgExpr)) |
Douglas Gregor | 54c53cc | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 4635 | if (isa<NonTypeTemplateParmDecl>(DRE->getDecl())) |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4636 | continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4637 | |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4638 | // C++ [temp.class.spec]p9: |
| 4639 | // Within the argument list of a class template partial |
| 4640 | // specialization, the following restrictions apply: |
| 4641 | // -- A partially specialized non-type argument expression |
| 4642 | // shall not involve a template parameter of the partial |
| 4643 | // specialization except when the argument expression is a |
| 4644 | // simple identifier. |
| 4645 | if (ArgExpr->isTypeDependent() || ArgExpr->isValueDependent()) { |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4646 | S.Diag(ArgExpr->getLocStart(), |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4647 | diag::err_dependent_non_type_arg_in_partial_spec) |
| 4648 | << ArgExpr->getSourceRange(); |
| 4649 | return true; |
| 4650 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4651 | |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4652 | // -- The type of a template parameter corresponding to a |
| 4653 | // specialized non-type argument shall not be dependent on a |
| 4654 | // parameter of the specialization. |
| 4655 | if (Param->getType()->isDependentType()) { |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4656 | S.Diag(ArgExpr->getLocStart(), |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4657 | diag::err_dependent_typed_non_type_arg_in_partial_spec) |
| 4658 | << Param->getType() |
| 4659 | << ArgExpr->getSourceRange(); |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4660 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4661 | return true; |
| 4662 | } |
| 4663 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4664 | |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4665 | return false; |
| 4666 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4667 | |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4668 | /// \brief Check the non-type template arguments of a class template |
| 4669 | /// partial specialization according to C++ [temp.class.spec]p9. |
| 4670 | /// |
| 4671 | /// \param TemplateParams the template parameters of the primary class |
| 4672 | /// template. |
| 4673 | /// |
| 4674 | /// \param TemplateArg the template arguments of the class template |
| 4675 | /// partial specialization. |
| 4676 | /// |
| 4677 | /// \returns true if there was an error, false otherwise. |
| 4678 | static bool CheckClassTemplatePartialSpecializationArgs(Sema &S, |
| 4679 | TemplateParameterList *TemplateParams, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4680 | SmallVectorImpl<TemplateArgument> &TemplateArgs) { |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4681 | const TemplateArgument *ArgList = TemplateArgs.data(); |
| 4682 | |
| 4683 | for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) { |
| 4684 | NonTypeTemplateParmDecl *Param |
| 4685 | = dyn_cast<NonTypeTemplateParmDecl>(TemplateParams->getParam(I)); |
| 4686 | if (!Param) |
| 4687 | continue; |
| 4688 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4689 | if (CheckNonTypeClassTemplatePartialSpecializationArgs(S, Param, |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4690 | &ArgList[I], 1)) |
| 4691 | return true; |
| 4692 | } |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4693 | |
| 4694 | return false; |
| 4695 | } |
| 4696 | |
Douglas Gregor | dc0a11c | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 4697 | /// \brief Retrieve the previous declaration of the given declaration. |
| 4698 | static NamedDecl *getPreviousDecl(NamedDecl *ND) { |
| 4699 | if (VarDecl *VD = dyn_cast<VarDecl>(ND)) |
| 4700 | return VD->getPreviousDeclaration(); |
| 4701 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) |
| 4702 | return FD->getPreviousDeclaration(); |
| 4703 | if (TagDecl *TD = dyn_cast<TagDecl>(ND)) |
| 4704 | return TD->getPreviousDeclaration(); |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 4705 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(ND)) |
Douglas Gregor | dc0a11c | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 4706 | return TD->getPreviousDeclaration(); |
| 4707 | if (FunctionTemplateDecl *FTD = dyn_cast<FunctionTemplateDecl>(ND)) |
| 4708 | return FTD->getPreviousDeclaration(); |
| 4709 | if (ClassTemplateDecl *CTD = dyn_cast<ClassTemplateDecl>(ND)) |
| 4710 | return CTD->getPreviousDeclaration(); |
| 4711 | return 0; |
| 4712 | } |
| 4713 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4714 | DeclResult |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 4715 | Sema::ActOnClassTemplateSpecialization(Scope *S, unsigned TagSpec, |
| 4716 | TagUseKind TUK, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4717 | SourceLocation KWLoc, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 4718 | CXXScopeSpec &SS, |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 4719 | TemplateTy TemplateD, |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4720 | SourceLocation TemplateNameLoc, |
| 4721 | SourceLocation LAngleLoc, |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 4722 | ASTTemplateArgsPtr TemplateArgsIn, |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4723 | SourceLocation RAngleLoc, |
| 4724 | AttributeList *Attr, |
| 4725 | MultiTemplateParamsArg TemplateParameterLists) { |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 4726 | assert(TUK != TUK_Reference && "References are not specializations"); |
John McCall | f1bbbb4 | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 4727 | |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 4728 | // NOTE: KWLoc is the location of the tag keyword. This will instead |
| 4729 | // store the location of the outermost template keyword in the declaration. |
| 4730 | SourceLocation TemplateKWLoc = TemplateParameterLists.size() > 0 |
| 4731 | ? TemplateParameterLists.get()[0]->getTemplateLoc() : SourceLocation(); |
| 4732 | |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4733 | // Find the class template we're specializing |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 4734 | TemplateName Name = TemplateD.getAsVal<TemplateName>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4735 | ClassTemplateDecl *ClassTemplate |
Douglas Gregor | 8b13c08 | 2009-11-12 00:46:20 +0000 | [diff] [blame] | 4736 | = dyn_cast_or_null<ClassTemplateDecl>(Name.getAsTemplateDecl()); |
| 4737 | |
| 4738 | if (!ClassTemplate) { |
| 4739 | Diag(TemplateNameLoc, diag::err_not_class_template_specialization) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4740 | << (Name.getAsTemplateDecl() && |
Douglas Gregor | 8b13c08 | 2009-11-12 00:46:20 +0000 | [diff] [blame] | 4741 | isa<TemplateTemplateParmDecl>(Name.getAsTemplateDecl())); |
| 4742 | return true; |
| 4743 | } |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4744 | |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 4745 | bool isExplicitSpecialization = false; |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 4746 | bool isPartialSpecialization = false; |
| 4747 | |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4748 | // Check the validity of the template headers that introduce this |
| 4749 | // template. |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 4750 | // FIXME: We probably shouldn't complain about these headers for |
| 4751 | // friend declarations. |
Douglas Gregor | 0167f3c | 2010-07-14 23:14:12 +0000 | [diff] [blame] | 4752 | bool Invalid = false; |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4753 | TemplateParameterList *TemplateParams |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 4754 | = MatchTemplateParametersToScopeSpecifier(TemplateNameLoc, |
| 4755 | TemplateNameLoc, |
| 4756 | SS, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4757 | (TemplateParameterList**)TemplateParameterLists.get(), |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 4758 | TemplateParameterLists.size(), |
John McCall | 77e8b11 | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 4759 | TUK == TUK_Friend, |
Douglas Gregor | 0167f3c | 2010-07-14 23:14:12 +0000 | [diff] [blame] | 4760 | isExplicitSpecialization, |
| 4761 | Invalid); |
| 4762 | if (Invalid) |
| 4763 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4764 | |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4765 | if (TemplateParams && TemplateParams->size() > 0) { |
| 4766 | isPartialSpecialization = true; |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4767 | |
Douglas Gregor | b0ee93c | 2010-12-21 08:14:57 +0000 | [diff] [blame] | 4768 | if (TUK == TUK_Friend) { |
| 4769 | Diag(KWLoc, diag::err_partial_specialization_friend) |
| 4770 | << SourceRange(LAngleLoc, RAngleLoc); |
| 4771 | return true; |
| 4772 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4773 | |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4774 | // C++ [temp.class.spec]p10: |
| 4775 | // The template parameter list of a specialization shall not |
| 4776 | // contain default template argument values. |
| 4777 | for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) { |
| 4778 | Decl *Param = TemplateParams->getParam(I); |
| 4779 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) { |
| 4780 | if (TTP->hasDefaultArgument()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4781 | Diag(TTP->getDefaultArgumentLoc(), |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4782 | diag::err_default_arg_in_partial_spec); |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 4783 | TTP->removeDefaultArgument(); |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4784 | } |
| 4785 | } else if (NonTypeTemplateParmDecl *NTTP |
| 4786 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
| 4787 | if (Expr *DefArg = NTTP->getDefaultArgument()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4788 | Diag(NTTP->getDefaultArgumentLoc(), |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4789 | diag::err_default_arg_in_partial_spec) |
| 4790 | << DefArg->getSourceRange(); |
Abramo Bagnara | d92f7a2 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 4791 | NTTP->removeDefaultArgument(); |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4792 | } |
| 4793 | } else { |
| 4794 | TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(Param); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 4795 | if (TTP->hasDefaultArgument()) { |
| 4796 | Diag(TTP->getDefaultArgument().getLocation(), |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4797 | diag::err_default_arg_in_partial_spec) |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 4798 | << TTP->getDefaultArgument().getSourceRange(); |
Abramo Bagnara | d92f7a2 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 4799 | TTP->removeDefaultArgument(); |
Douglas Gregor | ba1ecb5 | 2009-06-12 19:43:02 +0000 | [diff] [blame] | 4800 | } |
| 4801 | } |
| 4802 | } |
Douglas Gregor | a735b20 | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 4803 | } else if (TemplateParams) { |
| 4804 | if (TUK == TUK_Friend) |
| 4805 | Diag(KWLoc, diag::err_template_spec_friend) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 4806 | << FixItHint::CreateRemoval( |
Douglas Gregor | a735b20 | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 4807 | SourceRange(TemplateParams->getTemplateLoc(), |
| 4808 | TemplateParams->getRAngleLoc())) |
| 4809 | << SourceRange(LAngleLoc, RAngleLoc); |
| 4810 | else |
| 4811 | isExplicitSpecialization = true; |
| 4812 | } else if (TUK != TUK_Friend) { |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4813 | Diag(KWLoc, diag::err_template_spec_needs_header) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 4814 | << FixItHint::CreateInsertion(KWLoc, "template<> "); |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 4815 | isExplicitSpecialization = true; |
| 4816 | } |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4817 | |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4818 | // Check that the specialization uses the same tag kind as the |
| 4819 | // original template. |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 4820 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
| 4821 | assert(Kind != TTK_Enum && "Invalid enum tag in class template spec!"); |
Douglas Gregor | 501c5ce | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 4822 | if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(), |
Richard Trieu | bbf34c0 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 4823 | Kind, TUK == TUK_Definition, KWLoc, |
Douglas Gregor | 501c5ce | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 4824 | *ClassTemplate->getIdentifier())) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4825 | Diag(KWLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 4826 | << ClassTemplate |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 4827 | << FixItHint::CreateReplacement(KWLoc, |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 4828 | ClassTemplate->getTemplatedDecl()->getKindName()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4829 | Diag(ClassTemplate->getTemplatedDecl()->getLocation(), |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4830 | diag::note_previous_use); |
| 4831 | Kind = ClassTemplate->getTemplatedDecl()->getTagKind(); |
| 4832 | } |
| 4833 | |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 4834 | // Translate the parser's template argument list in our AST format. |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 4835 | TemplateArgumentListInfo TemplateArgs; |
| 4836 | TemplateArgs.setLAngleLoc(LAngleLoc); |
| 4837 | TemplateArgs.setRAngleLoc(RAngleLoc); |
Douglas Gregor | 314b97f | 2009-11-10 19:49:08 +0000 | [diff] [blame] | 4838 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 4839 | |
Douglas Gregor | 925910d | 2011-01-03 20:35:03 +0000 | [diff] [blame] | 4840 | // Check for unexpanded parameter packs in any of the template arguments. |
| 4841 | for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4842 | if (DiagnoseUnexpandedParameterPack(TemplateArgs[I], |
Douglas Gregor | 925910d | 2011-01-03 20:35:03 +0000 | [diff] [blame] | 4843 | UPPC_PartialSpecialization)) |
| 4844 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4845 | |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4846 | // Check that the template argument list is well-formed for this |
| 4847 | // template. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4848 | SmallVector<TemplateArgument, 4> Converted; |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 4849 | if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc, |
| 4850 | TemplateArgs, false, Converted)) |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 4851 | return true; |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4852 | |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 4853 | assert((Converted.size() == ClassTemplate->getTemplateParameters()->size()) && |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4854 | "Converted template argument list is too short!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4855 | |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 4856 | // Find the class template (partial) specialization declaration that |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4857 | // corresponds to these arguments. |
Douglas Gregor | ba1ecb5 | 2009-06-12 19:43:02 +0000 | [diff] [blame] | 4858 | if (isPartialSpecialization) { |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4859 | if (CheckClassTemplatePartialSpecializationArgs(*this, |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4860 | ClassTemplate->getTemplateParameters(), |
Douglas Gregor | b9c6631 | 2010-12-23 17:13:55 +0000 | [diff] [blame] | 4861 | Converted)) |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4862 | return true; |
| 4863 | |
Douglas Gregor | 561f812 | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 4864 | bool InstantiationDependent; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4865 | if (!Name.isDependent() && |
Douglas Gregor | de09096 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 4866 | !TemplateSpecializationType::anyDependentTemplateArguments( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4867 | TemplateArgs.getArgumentArray(), |
Douglas Gregor | 561f812 | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 4868 | TemplateArgs.size(), |
| 4869 | InstantiationDependent)) { |
Douglas Gregor | de09096 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 4870 | Diag(TemplateNameLoc, diag::err_partial_spec_fully_specialized) |
| 4871 | << ClassTemplate->getDeclName(); |
| 4872 | isPartialSpecialization = false; |
Douglas Gregor | de09096 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 4873 | } |
| 4874 | } |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 4875 | |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4876 | void *InsertPos = 0; |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 4877 | ClassTemplateSpecializationDecl *PrevDecl = 0; |
| 4878 | |
| 4879 | if (isPartialSpecialization) |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 4880 | // FIXME: Template parameter list matters, too |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 4881 | PrevDecl |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 4882 | = ClassTemplate->findPartialSpecialization(Converted.data(), |
| 4883 | Converted.size(), |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 4884 | InsertPos); |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 4885 | else |
| 4886 | PrevDecl |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 4887 | = ClassTemplate->findSpecialization(Converted.data(), |
| 4888 | Converted.size(), InsertPos); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4889 | |
| 4890 | ClassTemplateSpecializationDecl *Specialization = 0; |
| 4891 | |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4892 | // Check whether we can declare a class template specialization in |
| 4893 | // the current scope. |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 4894 | if (TUK != TUK_Friend && |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4895 | CheckTemplateSpecializationScope(*this, ClassTemplate, PrevDecl, |
| 4896 | TemplateNameLoc, |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4897 | isPartialSpecialization)) |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 4898 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4899 | |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 4900 | // The canonical type |
| 4901 | QualType CanonType; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4902 | if (PrevDecl && |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 4903 | (PrevDecl->getSpecializationKind() == TSK_Undeclared || |
Douglas Gregor | de09096 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 4904 | TUK == TUK_Friend)) { |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4905 | // Since the only prior class template specialization with these |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 4906 | // arguments was referenced but not declared, or we're only |
| 4907 | // referencing this specialization as a friend, reuse that |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 4908 | // declaration node as our own, updating its source location and |
| 4909 | // the list of outer template parameters to reflect our new declaration. |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4910 | Specialization = PrevDecl; |
Douglas Gregor | 6bc9f7e | 2009-02-25 22:18:32 +0000 | [diff] [blame] | 4911 | Specialization->setLocation(TemplateNameLoc); |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 4912 | if (TemplateParameterLists.size() > 0) { |
| 4913 | Specialization->setTemplateParameterListsInfo(Context, |
| 4914 | TemplateParameterLists.size(), |
| 4915 | (TemplateParameterList**) TemplateParameterLists.release()); |
| 4916 | } |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4917 | PrevDecl = 0; |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 4918 | CanonType = Context.getTypeDeclType(Specialization); |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 4919 | } else if (isPartialSpecialization) { |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 4920 | // Build the canonical type that describes the converted template |
| 4921 | // arguments of the class template partial specialization. |
Douglas Gregor | de09096 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 4922 | TemplateName CanonTemplate = Context.getCanonicalTemplateName(Name); |
| 4923 | CanonType = Context.getTemplateSpecializationType(CanonTemplate, |
Douglas Gregor | b9c6631 | 2010-12-23 17:13:55 +0000 | [diff] [blame] | 4924 | Converted.data(), |
| 4925 | Converted.size()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4926 | |
| 4927 | if (Context.hasSameType(CanonType, |
Douglas Gregor | b9c6631 | 2010-12-23 17:13:55 +0000 | [diff] [blame] | 4928 | ClassTemplate->getInjectedClassNameSpecialization())) { |
| 4929 | // C++ [temp.class.spec]p9b3: |
| 4930 | // |
| 4931 | // -- The argument list of the specialization shall not be identical |
| 4932 | // to the implicit argument list of the primary template. |
| 4933 | Diag(TemplateNameLoc, diag::err_partial_spec_args_match_primary_template) |
| 4934 | << (TUK == TUK_Definition) |
| 4935 | << FixItHint::CreateRemoval(SourceRange(LAngleLoc, RAngleLoc)); |
| 4936 | return CheckClassTemplate(S, TagSpec, TUK, KWLoc, SS, |
| 4937 | ClassTemplate->getIdentifier(), |
| 4938 | TemplateNameLoc, |
| 4939 | Attr, |
| 4940 | TemplateParams, |
Abramo Bagnara | c57c17d | 2011-03-10 13:28:31 +0000 | [diff] [blame] | 4941 | AS_none, |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 4942 | TemplateParameterLists.size() - 1, |
Abramo Bagnara | c57c17d | 2011-03-10 13:28:31 +0000 | [diff] [blame] | 4943 | (TemplateParameterList**) TemplateParameterLists.release()); |
Douglas Gregor | b9c6631 | 2010-12-23 17:13:55 +0000 | [diff] [blame] | 4944 | } |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 4945 | |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 4946 | // Create a new class template partial specialization declaration node. |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 4947 | ClassTemplatePartialSpecializationDecl *PrevPartial |
| 4948 | = cast_or_null<ClassTemplatePartialSpecializationDecl>(PrevDecl); |
Douglas Gregor | dc60c1e | 2010-04-30 05:56:50 +0000 | [diff] [blame] | 4949 | unsigned SequenceNumber = PrevPartial? PrevPartial->getSequenceNumber() |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 4950 | : ClassTemplate->getNextPartialSpecSequenceNumber(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4951 | ClassTemplatePartialSpecializationDecl *Partial |
Douglas Gregor | 13c8577 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 4952 | = ClassTemplatePartialSpecializationDecl::Create(Context, Kind, |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 4953 | ClassTemplate->getDeclContext(), |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 4954 | KWLoc, TemplateNameLoc, |
Anders Carlsson | 91fdf6f | 2009-06-05 04:06:48 +0000 | [diff] [blame] | 4955 | TemplateParams, |
| 4956 | ClassTemplate, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 4957 | Converted.data(), |
| 4958 | Converted.size(), |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 4959 | TemplateArgs, |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 4960 | CanonType, |
Douglas Gregor | dc60c1e | 2010-04-30 05:56:50 +0000 | [diff] [blame] | 4961 | PrevPartial, |
| 4962 | SequenceNumber); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 4963 | SetNestedNameSpecifier(Partial, SS); |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 4964 | if (TemplateParameterLists.size() > 1 && SS.isSet()) { |
Douglas Gregor | c722ea4 | 2010-06-15 17:44:38 +0000 | [diff] [blame] | 4965 | Partial->setTemplateParameterListsInfo(Context, |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 4966 | TemplateParameterLists.size() - 1, |
Abramo Bagnara | 9b93488 | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 4967 | (TemplateParameterList**) TemplateParameterLists.release()); |
| 4968 | } |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 4969 | |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 4970 | if (!PrevPartial) |
| 4971 | ClassTemplate->AddPartialSpecialization(Partial, InsertPos); |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 4972 | Specialization = Partial; |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4973 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4974 | // If we are providing an explicit specialization of a member class |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4975 | // template specialization, make a note of that. |
| 4976 | if (PrevPartial && PrevPartial->getInstantiatedFromMember()) |
| 4977 | PrevPartial->setMemberSpecialization(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4978 | |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4979 | // Check that all of the template parameters of the class template |
| 4980 | // partial specialization are deducible from the template |
| 4981 | // arguments. If not, this class template partial specialization |
| 4982 | // will never be used. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4983 | SmallVector<bool, 8> DeducibleParams; |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4984 | DeducibleParams.resize(TemplateParams->size()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4985 | MarkUsedTemplateParameters(Partial->getTemplateArgs(), true, |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4986 | TemplateParams->getDepth(), |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4987 | DeducibleParams); |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4988 | unsigned NumNonDeducible = 0; |
| 4989 | for (unsigned I = 0, N = DeducibleParams.size(); I != N; ++I) |
| 4990 | if (!DeducibleParams[I]) |
| 4991 | ++NumNonDeducible; |
| 4992 | |
| 4993 | if (NumNonDeducible) { |
| 4994 | Diag(TemplateNameLoc, diag::warn_partial_specs_not_deducible) |
| 4995 | << (NumNonDeducible > 1) |
| 4996 | << SourceRange(TemplateNameLoc, RAngleLoc); |
| 4997 | for (unsigned I = 0, N = DeducibleParams.size(); I != N; ++I) { |
| 4998 | if (!DeducibleParams[I]) { |
| 4999 | NamedDecl *Param = cast<NamedDecl>(TemplateParams->getParam(I)); |
| 5000 | if (Param->getDeclName()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5001 | Diag(Param->getLocation(), |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5002 | diag::note_partial_spec_unused_parameter) |
| 5003 | << Param->getDeclName(); |
| 5004 | else |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5005 | Diag(Param->getLocation(), |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5006 | diag::note_partial_spec_unused_parameter) |
Benjamin Kramer | 476d8b8 | 2010-08-11 14:47:12 +0000 | [diff] [blame] | 5007 | << "<anonymous>"; |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5008 | } |
| 5009 | } |
| 5010 | } |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5011 | } else { |
| 5012 | // Create a new class template specialization declaration node for |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 5013 | // this explicit specialization or friend declaration. |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5014 | Specialization |
Douglas Gregor | 13c8577 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 5015 | = ClassTemplateSpecializationDecl::Create(Context, Kind, |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5016 | ClassTemplate->getDeclContext(), |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 5017 | KWLoc, TemplateNameLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5018 | ClassTemplate, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 5019 | Converted.data(), |
| 5020 | Converted.size(), |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5021 | PrevDecl); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 5022 | SetNestedNameSpecifier(Specialization, SS); |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 5023 | if (TemplateParameterLists.size() > 0) { |
Douglas Gregor | c722ea4 | 2010-06-15 17:44:38 +0000 | [diff] [blame] | 5024 | Specialization->setTemplateParameterListsInfo(Context, |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 5025 | TemplateParameterLists.size(), |
Abramo Bagnara | 9b93488 | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 5026 | (TemplateParameterList**) TemplateParameterLists.release()); |
| 5027 | } |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5028 | |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 5029 | if (!PrevDecl) |
| 5030 | ClassTemplate->AddSpecialization(Specialization, InsertPos); |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 5031 | |
| 5032 | CanonType = Context.getTypeDeclType(Specialization); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5033 | } |
| 5034 | |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5035 | // C++ [temp.expl.spec]p6: |
| 5036 | // If a template, a member template or the member of a class template is |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5037 | // explicitly specialized then that specialization shall be declared |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5038 | // before the first use of that specialization that would cause an implicit |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5039 | // instantiation to take place, in every translation unit in which such a |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5040 | // use occurs; no diagnostic is required. |
| 5041 | if (PrevDecl && PrevDecl->getPointOfInstantiation().isValid()) { |
Douglas Gregor | dc0a11c | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 5042 | bool Okay = false; |
| 5043 | for (NamedDecl *Prev = PrevDecl; Prev; Prev = getPreviousDecl(Prev)) { |
| 5044 | // Is there any previous explicit specialization declaration? |
| 5045 | if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) { |
| 5046 | Okay = true; |
| 5047 | break; |
| 5048 | } |
| 5049 | } |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5050 | |
Douglas Gregor | dc0a11c | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 5051 | if (!Okay) { |
| 5052 | SourceRange Range(TemplateNameLoc, RAngleLoc); |
| 5053 | Diag(TemplateNameLoc, diag::err_specialization_after_instantiation) |
| 5054 | << Context.getTypeDeclType(Specialization) << Range; |
| 5055 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5056 | Diag(PrevDecl->getPointOfInstantiation(), |
Douglas Gregor | dc0a11c | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 5057 | diag::note_instantiation_required_here) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5058 | << (PrevDecl->getTemplateSpecializationKind() |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5059 | != TSK_ImplicitInstantiation); |
Douglas Gregor | dc0a11c | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 5060 | return true; |
| 5061 | } |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5062 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5063 | |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 5064 | // If this is not a friend, note that this is an explicit specialization. |
| 5065 | if (TUK != TUK_Friend) |
| 5066 | Specialization->setSpecializationKind(TSK_ExplicitSpecialization); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5067 | |
| 5068 | // Check that this isn't a redefinition of this specialization. |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 5069 | if (TUK == TUK_Definition) { |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 5070 | if (RecordDecl *Def = Specialization->getDefinition()) { |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5071 | SourceRange Range(TemplateNameLoc, RAngleLoc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5072 | Diag(TemplateNameLoc, diag::err_redefinition) |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 5073 | << Context.getTypeDeclType(Specialization) << Range; |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5074 | Diag(Def->getLocation(), diag::note_previous_definition); |
| 5075 | Specialization->setInvalidDecl(); |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 5076 | return true; |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5077 | } |
| 5078 | } |
| 5079 | |
John McCall | 7f1b987 | 2010-12-18 03:30:47 +0000 | [diff] [blame] | 5080 | if (Attr) |
| 5081 | ProcessDeclAttributeList(S, Specialization, Attr); |
| 5082 | |
Douglas Gregor | fc705b8 | 2009-02-26 22:19:44 +0000 | [diff] [blame] | 5083 | // Build the fully-sugared type for this class template |
| 5084 | // specialization as the user wrote in the specialization |
| 5085 | // itself. This means that we'll pretty-print the type retrieved |
| 5086 | // from the specialization's declaration the way that the user |
| 5087 | // actually wrote the specialization, rather than formatting the |
| 5088 | // name based on the "canonical" representation used to store the |
| 5089 | // template arguments in the specialization. |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 5090 | TypeSourceInfo *WrittenTy |
| 5091 | = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc, |
| 5092 | TemplateArgs, CanonType); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5093 | if (TUK != TUK_Friend) { |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 5094 | Specialization->setTypeAsWritten(WrittenTy); |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 5095 | Specialization->setTemplateKeywordLoc(TemplateKWLoc); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5096 | } |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 5097 | TemplateArgsIn.release(); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5098 | |
Douglas Gregor | 6bc9f7e | 2009-02-25 22:18:32 +0000 | [diff] [blame] | 5099 | // C++ [temp.expl.spec]p9: |
| 5100 | // A template explicit specialization is in the scope of the |
| 5101 | // namespace in which the template was defined. |
| 5102 | // |
| 5103 | // We actually implement this paragraph where we set the semantic |
| 5104 | // context (in the creation of the ClassTemplateSpecializationDecl), |
| 5105 | // but we also maintain the lexical context where the actual |
| 5106 | // definition occurs. |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5107 | Specialization->setLexicalDeclContext(CurContext); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5108 | |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5109 | // We may be starting the definition of this specialization. |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 5110 | if (TUK == TUK_Definition) |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5111 | Specialization->startDefinition(); |
| 5112 | |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 5113 | if (TUK == TUK_Friend) { |
| 5114 | FriendDecl *Friend = FriendDecl::Create(Context, CurContext, |
| 5115 | TemplateNameLoc, |
John McCall | 32f2fb5 | 2010-03-25 18:04:51 +0000 | [diff] [blame] | 5116 | WrittenTy, |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 5117 | /*FIXME:*/KWLoc); |
| 5118 | Friend->setAccess(AS_public); |
| 5119 | CurContext->addDecl(Friend); |
| 5120 | } else { |
| 5121 | // Add the specialization into its lexical context, so that it can |
| 5122 | // be seen when iterating through the list of declarations in that |
| 5123 | // context. However, specializations are not found by name lookup. |
| 5124 | CurContext->addDecl(Specialization); |
| 5125 | } |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5126 | return Specialization; |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5127 | } |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5128 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5129 | Decl *Sema::ActOnTemplateDeclarator(Scope *S, |
Douglas Gregor | e542c86 | 2009-06-23 23:11:28 +0000 | [diff] [blame] | 5130 | MultiTemplateParamsArg TemplateParameterLists, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5131 | Declarator &D) { |
Douglas Gregor | e542c86 | 2009-06-23 23:11:28 +0000 | [diff] [blame] | 5132 | return HandleDeclarator(S, D, move(TemplateParameterLists), false); |
| 5133 | } |
| 5134 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5135 | Decl *Sema::ActOnStartOfFunctionTemplateDef(Scope *FnBodyScope, |
Douglas Gregor | 52591bf | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 5136 | MultiTemplateParamsArg TemplateParameterLists, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5137 | Declarator &D) { |
Douglas Gregor | 52591bf | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 5138 | assert(getCurFunctionDecl() == 0 && "Function parsing confused"); |
Abramo Bagnara | 075f8f1 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 5139 | DeclaratorChunk::FunctionTypeInfo &FTI = D.getFunctionTypeInfo(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5140 | |
Douglas Gregor | 52591bf | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 5141 | if (FTI.hasPrototype) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5142 | // FIXME: Diagnose arguments without names in C. |
Douglas Gregor | 52591bf | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 5143 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5144 | |
Douglas Gregor | 52591bf | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 5145 | Scope *ParentScope = FnBodyScope->getParent(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5146 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5147 | Decl *DP = HandleDeclarator(ParentScope, D, |
| 5148 | move(TemplateParameterLists), |
| 5149 | /*IsFunctionDefinition=*/true); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5150 | if (FunctionTemplateDecl *FunctionTemplate |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5151 | = dyn_cast_or_null<FunctionTemplateDecl>(DP)) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5152 | return ActOnStartOfFunctionDef(FnBodyScope, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5153 | FunctionTemplate->getTemplatedDecl()); |
| 5154 | if (FunctionDecl *Function = dyn_cast_or_null<FunctionDecl>(DP)) |
| 5155 | return ActOnStartOfFunctionDef(FnBodyScope, Function); |
| 5156 | return 0; |
Douglas Gregor | 52591bf | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 5157 | } |
| 5158 | |
John McCall | 7504239 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 5159 | /// \brief Strips various properties off an implicit instantiation |
| 5160 | /// that has just been explicitly specialized. |
| 5161 | static void StripImplicitInstantiation(NamedDecl *D) { |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 5162 | D->dropAttrs(); |
John McCall | 7504239 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 5163 | |
| 5164 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 5165 | FD->setInlineSpecified(false); |
| 5166 | } |
| 5167 | } |
| 5168 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5169 | /// \brief Diagnose cases where we have an explicit template specialization |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5170 | /// before/after an explicit template instantiation, producing diagnostics |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5171 | /// for those cases where they are required and determining whether the |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5172 | /// new specialization/instantiation will have any effect. |
| 5173 | /// |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5174 | /// \param NewLoc the location of the new explicit specialization or |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5175 | /// instantiation. |
| 5176 | /// |
| 5177 | /// \param NewTSK the kind of the new explicit specialization or instantiation. |
| 5178 | /// |
| 5179 | /// \param PrevDecl the previous declaration of the entity. |
| 5180 | /// |
| 5181 | /// \param PrevTSK the kind of the old explicit specialization or instantiatin. |
| 5182 | /// |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5183 | /// \param PrevPointOfInstantiation if valid, indicates where the previus |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5184 | /// declaration was instantiated (either implicitly or explicitly). |
| 5185 | /// |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5186 | /// \param HasNoEffect will be set to true to indicate that the new |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5187 | /// specialization or instantiation has no effect and should be ignored. |
| 5188 | /// |
| 5189 | /// \returns true if there was an error that should prevent the introduction of |
| 5190 | /// the new declaration into the AST, false otherwise. |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 5191 | bool |
| 5192 | Sema::CheckSpecializationInstantiationRedecl(SourceLocation NewLoc, |
| 5193 | TemplateSpecializationKind NewTSK, |
| 5194 | NamedDecl *PrevDecl, |
| 5195 | TemplateSpecializationKind PrevTSK, |
| 5196 | SourceLocation PrevPointOfInstantiation, |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5197 | bool &HasNoEffect) { |
| 5198 | HasNoEffect = false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5199 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5200 | switch (NewTSK) { |
| 5201 | case TSK_Undeclared: |
| 5202 | case TSK_ImplicitInstantiation: |
| 5203 | assert(false && "Don't check implicit instantiations here"); |
| 5204 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5205 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5206 | case TSK_ExplicitSpecialization: |
| 5207 | switch (PrevTSK) { |
| 5208 | case TSK_Undeclared: |
| 5209 | case TSK_ExplicitSpecialization: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5210 | // Okay, we're just specializing something that is either already |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5211 | // explicitly specialized or has merely been mentioned without any |
| 5212 | // instantiation. |
| 5213 | return false; |
| 5214 | |
| 5215 | case TSK_ImplicitInstantiation: |
| 5216 | if (PrevPointOfInstantiation.isInvalid()) { |
| 5217 | // The declaration itself has not actually been instantiated, so it is |
| 5218 | // still okay to specialize it. |
John McCall | 7504239 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 5219 | StripImplicitInstantiation(PrevDecl); |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5220 | return false; |
| 5221 | } |
| 5222 | // Fall through |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5223 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5224 | case TSK_ExplicitInstantiationDeclaration: |
| 5225 | case TSK_ExplicitInstantiationDefinition: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5226 | assert((PrevTSK == TSK_ImplicitInstantiation || |
| 5227 | PrevPointOfInstantiation.isValid()) && |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5228 | "Explicit instantiation without point of instantiation?"); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5229 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5230 | // C++ [temp.expl.spec]p6: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5231 | // If a template, a member template or the member of a class template |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5232 | // is explicitly specialized then that specialization shall be declared |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5233 | // before the first use of that specialization that would cause an |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5234 | // implicit instantiation to take place, in every translation unit in |
| 5235 | // which such a use occurs; no diagnostic is required. |
Douglas Gregor | dc0a11c | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 5236 | for (NamedDecl *Prev = PrevDecl; Prev; Prev = getPreviousDecl(Prev)) { |
| 5237 | // Is there any previous explicit specialization declaration? |
| 5238 | if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) |
| 5239 | return false; |
| 5240 | } |
| 5241 | |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 5242 | Diag(NewLoc, diag::err_specialization_after_instantiation) |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5243 | << PrevDecl; |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 5244 | Diag(PrevPointOfInstantiation, diag::note_instantiation_required_here) |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5245 | << (PrevTSK != TSK_ImplicitInstantiation); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5246 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5247 | return true; |
| 5248 | } |
| 5249 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5250 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5251 | case TSK_ExplicitInstantiationDeclaration: |
| 5252 | switch (PrevTSK) { |
| 5253 | case TSK_ExplicitInstantiationDeclaration: |
| 5254 | // This explicit instantiation declaration is redundant (that's okay). |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5255 | HasNoEffect = true; |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5256 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5257 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5258 | case TSK_Undeclared: |
| 5259 | case TSK_ImplicitInstantiation: |
| 5260 | // We're explicitly instantiating something that may have already been |
| 5261 | // implicitly instantiated; that's fine. |
| 5262 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5263 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5264 | case TSK_ExplicitSpecialization: |
| 5265 | // C++0x [temp.explicit]p4: |
| 5266 | // For a given set of template parameters, if an explicit instantiation |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5267 | // of a template appears after a declaration of an explicit |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5268 | // specialization for that template, the explicit instantiation has no |
| 5269 | // effect. |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5270 | HasNoEffect = true; |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5271 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5272 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5273 | case TSK_ExplicitInstantiationDefinition: |
| 5274 | // C++0x [temp.explicit]p10: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5275 | // If an entity is the subject of both an explicit instantiation |
| 5276 | // declaration and an explicit instantiation definition in the same |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5277 | // translation unit, the definition shall follow the declaration. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5278 | Diag(NewLoc, |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 5279 | diag::err_explicit_instantiation_declaration_after_definition); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5280 | Diag(PrevPointOfInstantiation, |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 5281 | diag::note_explicit_instantiation_definition_here); |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5282 | assert(PrevPointOfInstantiation.isValid() && |
| 5283 | "Explicit instantiation without point of instantiation?"); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5284 | HasNoEffect = true; |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5285 | return false; |
| 5286 | } |
| 5287 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5288 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5289 | case TSK_ExplicitInstantiationDefinition: |
| 5290 | switch (PrevTSK) { |
| 5291 | case TSK_Undeclared: |
| 5292 | case TSK_ImplicitInstantiation: |
| 5293 | // We're explicitly instantiating something that may have already been |
| 5294 | // implicitly instantiated; that's fine. |
| 5295 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5296 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5297 | case TSK_ExplicitSpecialization: |
| 5298 | // C++ DR 259, C++0x [temp.explicit]p4: |
| 5299 | // For a given set of template parameters, if an explicit |
| 5300 | // instantiation of a template appears after a declaration of |
| 5301 | // an explicit specialization for that template, the explicit |
| 5302 | // instantiation has no effect. |
| 5303 | // |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5304 | // In C++98/03 mode, we only give an extension warning here, because it |
Douglas Gregor | c42b652 | 2010-04-09 21:02:29 +0000 | [diff] [blame] | 5305 | // is not harmful to try to explicitly instantiate something that |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5306 | // has been explicitly specialized. |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 5307 | if (!getLangOptions().CPlusPlus0x) { |
| 5308 | Diag(NewLoc, diag::ext_explicit_instantiation_after_specialization) |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5309 | << PrevDecl; |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 5310 | Diag(PrevDecl->getLocation(), |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5311 | diag::note_previous_template_specialization); |
| 5312 | } |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5313 | HasNoEffect = true; |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5314 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5315 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5316 | case TSK_ExplicitInstantiationDeclaration: |
| 5317 | // We're explicity instantiating a definition for something for which we |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5318 | // were previously asked to suppress instantiations. That's fine. |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5319 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5320 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5321 | case TSK_ExplicitInstantiationDefinition: |
| 5322 | // C++0x [temp.spec]p5: |
| 5323 | // For a given template and a given set of template-arguments, |
| 5324 | // - an explicit instantiation definition shall appear at most once |
| 5325 | // in a program, |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 5326 | Diag(NewLoc, diag::err_explicit_instantiation_duplicate) |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5327 | << PrevDecl; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5328 | Diag(PrevPointOfInstantiation, |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 5329 | diag::note_previous_explicit_instantiation); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5330 | HasNoEffect = true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5331 | return false; |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5332 | } |
| 5333 | break; |
| 5334 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5335 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5336 | assert(false && "Missing specialization/instantiation case?"); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5337 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5338 | return false; |
| 5339 | } |
| 5340 | |
John McCall | af2094e | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 5341 | /// \brief Perform semantic analysis for the given dependent function |
| 5342 | /// template specialization. The only possible way to get a dependent |
| 5343 | /// function template specialization is with a friend declaration, |
| 5344 | /// like so: |
| 5345 | /// |
| 5346 | /// template <class T> void foo(T); |
| 5347 | /// template <class T> class A { |
| 5348 | /// friend void foo<>(T); |
| 5349 | /// }; |
| 5350 | /// |
| 5351 | /// There really isn't any useful analysis we can do here, so we |
| 5352 | /// just store the information. |
| 5353 | bool |
| 5354 | Sema::CheckDependentFunctionTemplateSpecialization(FunctionDecl *FD, |
| 5355 | const TemplateArgumentListInfo &ExplicitTemplateArgs, |
| 5356 | LookupResult &Previous) { |
| 5357 | // Remove anything from Previous that isn't a function template in |
| 5358 | // the correct context. |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 5359 | DeclContext *FDLookupContext = FD->getDeclContext()->getRedeclContext(); |
John McCall | af2094e | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 5360 | LookupResult::Filter F = Previous.makeFilter(); |
| 5361 | while (F.hasNext()) { |
| 5362 | NamedDecl *D = F.next()->getUnderlyingDecl(); |
| 5363 | if (!isa<FunctionTemplateDecl>(D) || |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 5364 | !FDLookupContext->InEnclosingNamespaceSetOf( |
| 5365 | D->getDeclContext()->getRedeclContext())) |
John McCall | af2094e | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 5366 | F.erase(); |
| 5367 | } |
| 5368 | F.done(); |
| 5369 | |
| 5370 | // Should this be diagnosed here? |
| 5371 | if (Previous.empty()) return true; |
| 5372 | |
| 5373 | FD->setDependentTemplateSpecialization(Context, Previous.asUnresolvedSet(), |
| 5374 | ExplicitTemplateArgs); |
| 5375 | return false; |
| 5376 | } |
| 5377 | |
Abramo Bagnara | e03db98 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 5378 | /// \brief Perform semantic analysis for the given function template |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5379 | /// specialization. |
| 5380 | /// |
Abramo Bagnara | e03db98 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 5381 | /// This routine performs all of the semantic analysis required for an |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5382 | /// explicit function template specialization. On successful completion, |
| 5383 | /// the function declaration \p FD will become a function template |
| 5384 | /// specialization. |
| 5385 | /// |
| 5386 | /// \param FD the function declaration, which will be updated to become a |
| 5387 | /// function template specialization. |
| 5388 | /// |
Abramo Bagnara | e03db98 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 5389 | /// \param ExplicitTemplateArgs the explicitly-provided template arguments, |
| 5390 | /// if any. Note that this may be valid info even when 0 arguments are |
| 5391 | /// explicitly provided as in, e.g., \c void sort<>(char*, char*); |
| 5392 | /// as it anyway contains info on the angle brackets locations. |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5393 | /// |
Francois Pichet | 59e7c56 | 2011-07-08 06:21:47 +0000 | [diff] [blame] | 5394 | /// \param Previous the set of declarations that may be specialized by |
Abramo Bagnara | e03db98 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 5395 | /// this function specialization. |
| 5396 | bool |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5397 | Sema::CheckFunctionTemplateSpecialization(FunctionDecl *FD, |
Douglas Gregor | 6771423 | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 5398 | TemplateArgumentListInfo *ExplicitTemplateArgs, |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5399 | LookupResult &Previous) { |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5400 | // The set of function template specializations that could match this |
| 5401 | // explicit function template specialization. |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5402 | UnresolvedSet<8> Candidates; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5403 | |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 5404 | DeclContext *FDLookupContext = FD->getDeclContext()->getRedeclContext(); |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5405 | for (LookupResult::iterator I = Previous.begin(), E = Previous.end(); |
| 5406 | I != E; ++I) { |
| 5407 | NamedDecl *Ovl = (*I)->getUnderlyingDecl(); |
| 5408 | if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Ovl)) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5409 | // Only consider templates found within the same semantic lookup scope as |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5410 | // FD. |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 5411 | if (!FDLookupContext->InEnclosingNamespaceSetOf( |
| 5412 | Ovl->getDeclContext()->getRedeclContext())) |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5413 | continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5414 | |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5415 | // C++ [temp.expl.spec]p11: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5416 | // A trailing template-argument can be left unspecified in the |
| 5417 | // template-id naming an explicit function template specialization |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5418 | // provided it can be deduced from the function argument type. |
| 5419 | // Perform template argument deduction to determine whether we may be |
| 5420 | // specializing this template. |
| 5421 | // FIXME: It is somewhat wasteful to build |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 5422 | TemplateDeductionInfo Info(Context, FD->getLocation()); |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5423 | FunctionDecl *Specialization = 0; |
| 5424 | if (TemplateDeductionResult TDK |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 5425 | = DeduceTemplateArguments(FunTmpl, ExplicitTemplateArgs, |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5426 | FD->getType(), |
| 5427 | Specialization, |
| 5428 | Info)) { |
| 5429 | // FIXME: Template argument deduction failed; record why it failed, so |
| 5430 | // that we can provide nifty diagnostics. |
| 5431 | (void)TDK; |
| 5432 | continue; |
| 5433 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5434 | |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5435 | // Record this candidate. |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5436 | Candidates.addDecl(Specialization, I.getAccess()); |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5437 | } |
| 5438 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5439 | |
Douglas Gregor | c5df30f | 2009-09-26 03:41:46 +0000 | [diff] [blame] | 5440 | // Find the most specialized function template. |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5441 | UnresolvedSetIterator Result |
| 5442 | = getMostSpecialized(Candidates.begin(), Candidates.end(), |
Douglas Gregor | 5c7bf42 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 5443 | TPOC_Other, 0, FD->getLocation(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5444 | PDiag(diag::err_function_template_spec_no_match) |
Douglas Gregor | c5df30f | 2009-09-26 03:41:46 +0000 | [diff] [blame] | 5445 | << FD->getDeclName(), |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 5446 | PDiag(diag::err_function_template_spec_ambiguous) |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 5447 | << FD->getDeclName() << (ExplicitTemplateArgs != 0), |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 5448 | PDiag(diag::note_function_template_spec_matched)); |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5449 | if (Result == Candidates.end()) |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5450 | return true; |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5451 | |
| 5452 | // Ignore access information; it doesn't figure into redeclaration checking. |
| 5453 | FunctionDecl *Specialization = cast<FunctionDecl>(*Result); |
Abramo Bagnara | abfb405 | 2011-03-04 17:20:30 +0000 | [diff] [blame] | 5454 | |
| 5455 | FunctionTemplateSpecializationInfo *SpecInfo |
| 5456 | = Specialization->getTemplateSpecializationInfo(); |
| 5457 | assert(SpecInfo && "Function template specialization info missing?"); |
Francois Pichet | 59e7c56 | 2011-07-08 06:21:47 +0000 | [diff] [blame] | 5458 | |
| 5459 | // Note: do not overwrite location info if previous template |
| 5460 | // specialization kind was explicit. |
| 5461 | TemplateSpecializationKind TSK = SpecInfo->getTemplateSpecializationKind(); |
| 5462 | if (TSK == TSK_Undeclared || TSK == TSK_ImplicitInstantiation) |
| 5463 | Specialization->setLocation(FD->getLocation()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5464 | |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5465 | // FIXME: Check if the prior specialization has a point of instantiation. |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5466 | // If so, we have run afoul of . |
John McCall | 7ad650f | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 5467 | |
| 5468 | // If this is a friend declaration, then we're not really declaring |
| 5469 | // an explicit specialization. |
| 5470 | bool isFriend = (FD->getFriendObjectKind() != Decl::FOK_None); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5471 | |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5472 | // Check the scope of this explicit specialization. |
John McCall | 7ad650f | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 5473 | if (!isFriend && |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5474 | CheckTemplateSpecializationScope(*this, |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5475 | Specialization->getPrimaryTemplate(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5476 | Specialization, FD->getLocation(), |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 5477 | false)) |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5478 | return true; |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5479 | |
| 5480 | // C++ [temp.expl.spec]p6: |
| 5481 | // If a template, a member template or the member of a class template is |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5482 | // explicitly specialized then that specialization shall be declared |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5483 | // before the first use of that specialization that would cause an implicit |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5484 | // instantiation to take place, in every translation unit in which such a |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5485 | // use occurs; no diagnostic is required. |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5486 | bool HasNoEffect = false; |
John McCall | 7ad650f | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 5487 | if (!isFriend && |
| 5488 | CheckSpecializationInstantiationRedecl(FD->getLocation(), |
John McCall | 7504239 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 5489 | TSK_ExplicitSpecialization, |
| 5490 | Specialization, |
| 5491 | SpecInfo->getTemplateSpecializationKind(), |
| 5492 | SpecInfo->getPointOfInstantiation(), |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5493 | HasNoEffect)) |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5494 | return true; |
Douglas Gregor | e885e18 | 2011-05-21 18:53:30 +0000 | [diff] [blame] | 5495 | |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5496 | // Mark the prior declaration as an explicit specialization, so that later |
| 5497 | // clients know that this is an explicit specialization. |
Argyrios Kyrtzidis | bbc6454 | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 5498 | if (!isFriend) { |
John McCall | 7ad650f | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 5499 | SpecInfo->setTemplateSpecializationKind(TSK_ExplicitSpecialization); |
Argyrios Kyrtzidis | bbc6454 | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 5500 | MarkUnusedFileScopedDecl(Specialization); |
| 5501 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5502 | |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5503 | // Turn the given function declaration into a function template |
| 5504 | // specialization, with the template arguments from the previous |
| 5505 | // specialization. |
Abramo Bagnara | e03db98 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 5506 | // Take copies of (semantic and syntactic) template argument lists. |
| 5507 | const TemplateArgumentList* TemplArgs = new (Context) |
| 5508 | TemplateArgumentList(Specialization->getTemplateSpecializationArgs()); |
| 5509 | const TemplateArgumentListInfo* TemplArgsAsWritten = ExplicitTemplateArgs |
| 5510 | ? new (Context) TemplateArgumentListInfo(*ExplicitTemplateArgs) : 0; |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 5511 | FD->setFunctionTemplateSpecialization(Specialization->getPrimaryTemplate(), |
Abramo Bagnara | e03db98 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 5512 | TemplArgs, /*InsertPos=*/0, |
| 5513 | SpecInfo->getTemplateSpecializationKind(), |
| 5514 | TemplArgsAsWritten); |
Douglas Gregor | e885e18 | 2011-05-21 18:53:30 +0000 | [diff] [blame] | 5515 | FD->setStorageClass(Specialization->getStorageClass()); |
| 5516 | |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5517 | // The "previous declaration" for this function template specialization is |
| 5518 | // the prior function template specialization. |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5519 | Previous.clear(); |
| 5520 | Previous.addDecl(Specialization); |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5521 | return false; |
| 5522 | } |
| 5523 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5524 | /// \brief Perform semantic analysis for the given non-template member |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5525 | /// specialization. |
| 5526 | /// |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5527 | /// This routine performs all of the semantic analysis required for an |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5528 | /// explicit member function specialization. On successful completion, |
| 5529 | /// the function declaration \p FD will become a member function |
| 5530 | /// specialization. |
| 5531 | /// |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5532 | /// \param Member the member declaration, which will be updated to become a |
| 5533 | /// specialization. |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5534 | /// |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5535 | /// \param Previous the set of declarations, one of which may be specialized |
| 5536 | /// by this function specialization; the set will be modified to contain the |
| 5537 | /// redeclared member. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5538 | bool |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5539 | Sema::CheckMemberSpecialization(NamedDecl *Member, LookupResult &Previous) { |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5540 | assert(!isa<TemplateDecl>(Member) && "Only for non-template members"); |
John McCall | 77e8b11 | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 5541 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5542 | // Try to find the member we are instantiating. |
| 5543 | NamedDecl *Instantiation = 0; |
| 5544 | NamedDecl *InstantiatedFrom = 0; |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5545 | MemberSpecializationInfo *MSInfo = 0; |
| 5546 | |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5547 | if (Previous.empty()) { |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5548 | // Nowhere to look anyway. |
| 5549 | } else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Member)) { |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5550 | for (LookupResult::iterator I = Previous.begin(), E = Previous.end(); |
| 5551 | I != E; ++I) { |
| 5552 | NamedDecl *D = (*I)->getUnderlyingDecl(); |
| 5553 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) { |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5554 | if (Context.hasSameType(Function->getType(), Method->getType())) { |
| 5555 | Instantiation = Method; |
| 5556 | InstantiatedFrom = Method->getInstantiatedFromMemberFunction(); |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5557 | MSInfo = Method->getMemberSpecializationInfo(); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5558 | break; |
| 5559 | } |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5560 | } |
| 5561 | } |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5562 | } else if (isa<VarDecl>(Member)) { |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5563 | VarDecl *PrevVar; |
| 5564 | if (Previous.isSingleResult() && |
| 5565 | (PrevVar = dyn_cast<VarDecl>(Previous.getFoundDecl()))) |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5566 | if (PrevVar->isStaticDataMember()) { |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5567 | Instantiation = PrevVar; |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5568 | InstantiatedFrom = PrevVar->getInstantiatedFromStaticDataMember(); |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5569 | MSInfo = PrevVar->getMemberSpecializationInfo(); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5570 | } |
| 5571 | } else if (isa<RecordDecl>(Member)) { |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5572 | CXXRecordDecl *PrevRecord; |
| 5573 | if (Previous.isSingleResult() && |
| 5574 | (PrevRecord = dyn_cast<CXXRecordDecl>(Previous.getFoundDecl()))) { |
| 5575 | Instantiation = PrevRecord; |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5576 | InstantiatedFrom = PrevRecord->getInstantiatedFromMemberClass(); |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5577 | MSInfo = PrevRecord->getMemberSpecializationInfo(); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5578 | } |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5579 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5580 | |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5581 | if (!Instantiation) { |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5582 | // There is no previous declaration that matches. Since member |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5583 | // specializations are always out-of-line, the caller will complain about |
| 5584 | // this mismatch later. |
| 5585 | return false; |
| 5586 | } |
John McCall | 77e8b11 | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 5587 | |
| 5588 | // If this is a friend, just bail out here before we start turning |
| 5589 | // things into explicit specializations. |
| 5590 | if (Member->getFriendObjectKind() != Decl::FOK_None) { |
| 5591 | // Preserve instantiation information. |
| 5592 | if (InstantiatedFrom && isa<CXXMethodDecl>(Member)) { |
| 5593 | cast<CXXMethodDecl>(Member)->setInstantiationOfMemberFunction( |
| 5594 | cast<CXXMethodDecl>(InstantiatedFrom), |
| 5595 | cast<CXXMethodDecl>(Instantiation)->getTemplateSpecializationKind()); |
| 5596 | } else if (InstantiatedFrom && isa<CXXRecordDecl>(Member)) { |
| 5597 | cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass( |
| 5598 | cast<CXXRecordDecl>(InstantiatedFrom), |
| 5599 | cast<CXXRecordDecl>(Instantiation)->getTemplateSpecializationKind()); |
| 5600 | } |
| 5601 | |
| 5602 | Previous.clear(); |
| 5603 | Previous.addDecl(Instantiation); |
| 5604 | return false; |
| 5605 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5606 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5607 | // Make sure that this is a specialization of a member. |
| 5608 | if (!InstantiatedFrom) { |
| 5609 | Diag(Member->getLocation(), diag::err_spec_member_not_instantiated) |
| 5610 | << Member; |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5611 | Diag(Instantiation->getLocation(), diag::note_specialized_decl); |
| 5612 | return true; |
| 5613 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5614 | |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5615 | // C++ [temp.expl.spec]p6: |
| 5616 | // If a template, a member template or the member of a class template is |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5617 | // explicitly specialized then that spe- cialization shall be declared |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5618 | // before the first use of that specialization that would cause an implicit |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5619 | // instantiation to take place, in every translation unit in which such a |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5620 | // use occurs; no diagnostic is required. |
| 5621 | assert(MSInfo && "Member specialization info missing?"); |
John McCall | 7504239 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 5622 | |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5623 | bool HasNoEffect = false; |
John McCall | 7504239 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 5624 | if (CheckSpecializationInstantiationRedecl(Member->getLocation(), |
| 5625 | TSK_ExplicitSpecialization, |
| 5626 | Instantiation, |
| 5627 | MSInfo->getTemplateSpecializationKind(), |
| 5628 | MSInfo->getPointOfInstantiation(), |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5629 | HasNoEffect)) |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5630 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5631 | |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5632 | // Check the scope of this explicit specialization. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5633 | if (CheckTemplateSpecializationScope(*this, |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5634 | InstantiatedFrom, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5635 | Instantiation, Member->getLocation(), |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 5636 | false)) |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5637 | return true; |
Douglas Gregor | 2db3232 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 5638 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5639 | // Note that this is an explicit instantiation of a member. |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 5640 | // the original declaration to note that it is an explicit specialization |
| 5641 | // (if it was previously an implicit instantiation). This latter step |
| 5642 | // makes bookkeeping easier. |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5643 | if (isa<FunctionDecl>(Member)) { |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 5644 | FunctionDecl *InstantiationFunction = cast<FunctionDecl>(Instantiation); |
| 5645 | if (InstantiationFunction->getTemplateSpecializationKind() == |
| 5646 | TSK_ImplicitInstantiation) { |
| 5647 | InstantiationFunction->setTemplateSpecializationKind( |
| 5648 | TSK_ExplicitSpecialization); |
| 5649 | InstantiationFunction->setLocation(Member->getLocation()); |
| 5650 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5651 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5652 | cast<FunctionDecl>(Member)->setInstantiationOfMemberFunction( |
| 5653 | cast<CXXMethodDecl>(InstantiatedFrom), |
| 5654 | TSK_ExplicitSpecialization); |
Argyrios Kyrtzidis | bbc6454 | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 5655 | MarkUnusedFileScopedDecl(InstantiationFunction); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5656 | } else if (isa<VarDecl>(Member)) { |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 5657 | VarDecl *InstantiationVar = cast<VarDecl>(Instantiation); |
| 5658 | if (InstantiationVar->getTemplateSpecializationKind() == |
| 5659 | TSK_ImplicitInstantiation) { |
| 5660 | InstantiationVar->setTemplateSpecializationKind( |
| 5661 | TSK_ExplicitSpecialization); |
| 5662 | InstantiationVar->setLocation(Member->getLocation()); |
| 5663 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5664 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5665 | Context.setInstantiatedFromStaticDataMember(cast<VarDecl>(Member), |
| 5666 | cast<VarDecl>(InstantiatedFrom), |
| 5667 | TSK_ExplicitSpecialization); |
Argyrios Kyrtzidis | bbc6454 | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 5668 | MarkUnusedFileScopedDecl(InstantiationVar); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5669 | } else { |
| 5670 | assert(isa<CXXRecordDecl>(Member) && "Only member classes remain"); |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 5671 | CXXRecordDecl *InstantiationClass = cast<CXXRecordDecl>(Instantiation); |
| 5672 | if (InstantiationClass->getTemplateSpecializationKind() == |
| 5673 | TSK_ImplicitInstantiation) { |
| 5674 | InstantiationClass->setTemplateSpecializationKind( |
| 5675 | TSK_ExplicitSpecialization); |
| 5676 | InstantiationClass->setLocation(Member->getLocation()); |
| 5677 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5678 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5679 | cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass( |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 5680 | cast<CXXRecordDecl>(InstantiatedFrom), |
| 5681 | TSK_ExplicitSpecialization); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5682 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5683 | |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5684 | // Save the caller the trouble of having to figure out which declaration |
| 5685 | // this specialization matches. |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5686 | Previous.clear(); |
| 5687 | Previous.addDecl(Instantiation); |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5688 | return false; |
| 5689 | } |
| 5690 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5691 | /// \brief Check the scope of an explicit instantiation. |
Douglas Gregor | 669eed8 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 5692 | /// |
| 5693 | /// \returns true if a serious error occurs, false otherwise. |
| 5694 | static bool CheckExplicitInstantiationScope(Sema &S, NamedDecl *D, |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5695 | SourceLocation InstLoc, |
| 5696 | bool WasQualifiedName) { |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 5697 | DeclContext *OrigContext= D->getDeclContext()->getEnclosingNamespaceContext(); |
| 5698 | DeclContext *CurContext = S.CurContext->getRedeclContext(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5699 | |
Douglas Gregor | 669eed8 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 5700 | if (CurContext->isRecord()) { |
| 5701 | S.Diag(InstLoc, diag::err_explicit_instantiation_in_class) |
| 5702 | << D; |
| 5703 | return true; |
| 5704 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5705 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5706 | // C++0x [temp.explicit]p2: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5707 | // An explicit instantiation shall appear in an enclosing namespace of its |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5708 | // template. |
| 5709 | // |
| 5710 | // This is DR275, which we do not retroactively apply to C++98/03. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5711 | if (S.getLangOptions().CPlusPlus0x && |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 5712 | !CurContext->Encloses(OrigContext)) { |
| 5713 | if (NamespaceDecl *NS = dyn_cast<NamespaceDecl>(OrigContext)) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5714 | S.Diag(InstLoc, |
| 5715 | S.getLangOptions().CPlusPlus0x? |
Douglas Gregor | 2166beb | 2010-05-11 17:39:34 +0000 | [diff] [blame] | 5716 | diag::err_explicit_instantiation_out_of_scope |
| 5717 | : diag::warn_explicit_instantiation_out_of_scope_0x) |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5718 | << D << NS; |
| 5719 | else |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5720 | S.Diag(InstLoc, |
Douglas Gregor | 2166beb | 2010-05-11 17:39:34 +0000 | [diff] [blame] | 5721 | S.getLangOptions().CPlusPlus0x? |
| 5722 | diag::err_explicit_instantiation_must_be_global |
| 5723 | : diag::warn_explicit_instantiation_out_of_scope_0x) |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5724 | << D; |
| 5725 | S.Diag(D->getLocation(), diag::note_explicit_instantiation_here); |
Douglas Gregor | 669eed8 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 5726 | return false; |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5727 | } |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 5728 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5729 | // C++0x [temp.explicit]p2: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5730 | // If the name declared in the explicit instantiation is an unqualified |
| 5731 | // name, the explicit instantiation shall appear in the namespace where |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5732 | // its template is declared or, if that namespace is inline (7.3.1), any |
| 5733 | // namespace from its enclosing namespace set. |
| 5734 | if (WasQualifiedName) |
Douglas Gregor | 669eed8 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 5735 | return false; |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 5736 | |
| 5737 | if (CurContext->InEnclosingNamespaceSetOf(OrigContext)) |
Douglas Gregor | 669eed8 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 5738 | return false; |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 5739 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5740 | S.Diag(InstLoc, |
Douglas Gregor | 2166beb | 2010-05-11 17:39:34 +0000 | [diff] [blame] | 5741 | S.getLangOptions().CPlusPlus0x? |
| 5742 | diag::err_explicit_instantiation_unqualified_wrong_namespace |
| 5743 | : diag::warn_explicit_instantiation_unqualified_wrong_namespace_0x) |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 5744 | << D << OrigContext; |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5745 | S.Diag(D->getLocation(), diag::note_explicit_instantiation_here); |
Douglas Gregor | 669eed8 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 5746 | return false; |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5747 | } |
| 5748 | |
| 5749 | /// \brief Determine whether the given scope specifier has a template-id in it. |
| 5750 | static bool ScopeSpecifierHasTemplateId(const CXXScopeSpec &SS) { |
| 5751 | if (!SS.isSet()) |
| 5752 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5753 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5754 | // C++0x [temp.explicit]p2: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5755 | // If the explicit instantiation is for a member function, a member class |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5756 | // or a static data member of a class template specialization, the name of |
| 5757 | // the class template specialization in the qualified-id for the member |
| 5758 | // name shall be a simple-template-id. |
| 5759 | // |
| 5760 | // C++98 has the same restriction, just worded differently. |
| 5761 | for (NestedNameSpecifier *NNS = (NestedNameSpecifier *)SS.getScopeRep(); |
| 5762 | NNS; NNS = NNS->getPrefix()) |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 5763 | if (const Type *T = NNS->getAsType()) |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5764 | if (isa<TemplateSpecializationType>(T)) |
| 5765 | return true; |
| 5766 | |
| 5767 | return false; |
| 5768 | } |
| 5769 | |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 5770 | // Explicit instantiation of a class template specialization |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5771 | DeclResult |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5772 | Sema::ActOnExplicitInstantiation(Scope *S, |
Douglas Gregor | 45f9655 | 2009-09-04 06:33:52 +0000 | [diff] [blame] | 5773 | SourceLocation ExternLoc, |
| 5774 | SourceLocation TemplateLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5775 | unsigned TagSpec, |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5776 | SourceLocation KWLoc, |
| 5777 | const CXXScopeSpec &SS, |
| 5778 | TemplateTy TemplateD, |
| 5779 | SourceLocation TemplateNameLoc, |
| 5780 | SourceLocation LAngleLoc, |
| 5781 | ASTTemplateArgsPtr TemplateArgsIn, |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5782 | SourceLocation RAngleLoc, |
| 5783 | AttributeList *Attr) { |
| 5784 | // Find the class template we're specializing |
| 5785 | TemplateName Name = TemplateD.getAsVal<TemplateName>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5786 | ClassTemplateDecl *ClassTemplate |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5787 | = cast<ClassTemplateDecl>(Name.getAsTemplateDecl()); |
| 5788 | |
| 5789 | // Check that the specialization uses the same tag kind as the |
| 5790 | // original template. |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 5791 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
| 5792 | assert(Kind != TTK_Enum && |
| 5793 | "Invalid enum tag in class template explicit instantiation!"); |
Douglas Gregor | 501c5ce | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 5794 | if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(), |
Richard Trieu | bbf34c0 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 5795 | Kind, /*isDefinition*/false, KWLoc, |
Douglas Gregor | 501c5ce | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 5796 | *ClassTemplate->getIdentifier())) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5797 | Diag(KWLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5798 | << ClassTemplate |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 5799 | << FixItHint::CreateReplacement(KWLoc, |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5800 | ClassTemplate->getTemplatedDecl()->getKindName()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5801 | Diag(ClassTemplate->getTemplatedDecl()->getLocation(), |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5802 | diag::note_previous_use); |
| 5803 | Kind = ClassTemplate->getTemplatedDecl()->getTagKind(); |
| 5804 | } |
| 5805 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5806 | // C++0x [temp.explicit]p2: |
| 5807 | // There are two forms of explicit instantiation: an explicit instantiation |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5808 | // definition and an explicit instantiation declaration. An explicit |
| 5809 | // instantiation declaration begins with the extern keyword. [...] |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5810 | TemplateSpecializationKind TSK |
| 5811 | = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition |
| 5812 | : TSK_ExplicitInstantiationDeclaration; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5813 | |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5814 | // Translate the parser's template argument list in our AST format. |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 5815 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
Douglas Gregor | 314b97f | 2009-11-10 19:49:08 +0000 | [diff] [blame] | 5816 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5817 | |
| 5818 | // Check that the template argument list is well-formed for this |
| 5819 | // template. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5820 | SmallVector<TemplateArgument, 4> Converted; |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 5821 | if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc, |
| 5822 | TemplateArgs, false, Converted)) |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5823 | return true; |
| 5824 | |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 5825 | assert((Converted.size() == ClassTemplate->getTemplateParameters()->size()) && |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5826 | "Converted template argument list is too short!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5827 | |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5828 | // Find the class template specialization declaration that |
| 5829 | // corresponds to these arguments. |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5830 | void *InsertPos = 0; |
| 5831 | ClassTemplateSpecializationDecl *PrevDecl |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 5832 | = ClassTemplate->findSpecialization(Converted.data(), |
| 5833 | Converted.size(), InsertPos); |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5834 | |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5835 | TemplateSpecializationKind PrevDecl_TSK |
| 5836 | = PrevDecl ? PrevDecl->getTemplateSpecializationKind() : TSK_Undeclared; |
| 5837 | |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5838 | // C++0x [temp.explicit]p2: |
| 5839 | // [...] An explicit instantiation shall appear in an enclosing |
| 5840 | // namespace of its template. [...] |
| 5841 | // |
| 5842 | // This is C++ DR 275. |
Douglas Gregor | 669eed8 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 5843 | if (CheckExplicitInstantiationScope(*this, ClassTemplate, TemplateNameLoc, |
| 5844 | SS.isSet())) |
| 5845 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5846 | |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5847 | ClassTemplateSpecializationDecl *Specialization = 0; |
| 5848 | |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5849 | bool HasNoEffect = false; |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5850 | if (PrevDecl) { |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 5851 | if (CheckSpecializationInstantiationRedecl(TemplateNameLoc, TSK, |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5852 | PrevDecl, PrevDecl_TSK, |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 5853 | PrevDecl->getPointOfInstantiation(), |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5854 | HasNoEffect)) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5855 | return PrevDecl; |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5856 | |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5857 | // Even though HasNoEffect == true means that this explicit instantiation |
| 5858 | // has no effect on semantics, we go on to put its syntax in the AST. |
| 5859 | |
| 5860 | if (PrevDecl_TSK == TSK_ImplicitInstantiation || |
| 5861 | PrevDecl_TSK == TSK_Undeclared) { |
Douglas Gregor | 52604ab | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 5862 | // Since the only prior class template specialization with these |
| 5863 | // arguments was referenced but not declared, reuse that |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5864 | // declaration node as our own, updating the source location |
| 5865 | // for the template name to reflect our new declaration. |
| 5866 | // (Other source locations will be updated later.) |
Douglas Gregor | 52604ab | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 5867 | Specialization = PrevDecl; |
| 5868 | Specialization->setLocation(TemplateNameLoc); |
| 5869 | PrevDecl = 0; |
| 5870 | } |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 5871 | } |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5872 | |
Douglas Gregor | 52604ab | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 5873 | if (!Specialization) { |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5874 | // Create a new class template specialization declaration node for |
| 5875 | // this explicit specialization. |
| 5876 | Specialization |
Douglas Gregor | 13c8577 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 5877 | = ClassTemplateSpecializationDecl::Create(Context, Kind, |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5878 | ClassTemplate->getDeclContext(), |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 5879 | KWLoc, TemplateNameLoc, |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5880 | ClassTemplate, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 5881 | Converted.data(), |
| 5882 | Converted.size(), |
| 5883 | PrevDecl); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 5884 | SetNestedNameSpecifier(Specialization, SS); |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5885 | |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 5886 | if (!HasNoEffect && !PrevDecl) { |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5887 | // Insert the new specialization. |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 5888 | ClassTemplate->AddSpecialization(Specialization, InsertPos); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5889 | } |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5890 | } |
| 5891 | |
| 5892 | // Build the fully-sugared type for this explicit instantiation as |
| 5893 | // the user wrote in the explicit instantiation itself. This means |
| 5894 | // that we'll pretty-print the type retrieved from the |
| 5895 | // specialization's declaration the way that the user actually wrote |
| 5896 | // the explicit instantiation, rather than formatting the name based |
| 5897 | // on the "canonical" representation used to store the template |
| 5898 | // arguments in the specialization. |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 5899 | TypeSourceInfo *WrittenTy |
| 5900 | = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc, |
| 5901 | TemplateArgs, |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5902 | Context.getTypeDeclType(Specialization)); |
| 5903 | Specialization->setTypeAsWritten(WrittenTy); |
| 5904 | TemplateArgsIn.release(); |
| 5905 | |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5906 | // Set source locations for keywords. |
| 5907 | Specialization->setExternLoc(ExternLoc); |
| 5908 | Specialization->setTemplateKeywordLoc(TemplateLoc); |
| 5909 | |
| 5910 | // Add the explicit instantiation into its lexical context. However, |
| 5911 | // since explicit instantiations are never found by name lookup, we |
| 5912 | // just put it into the declaration context directly. |
| 5913 | Specialization->setLexicalDeclContext(CurContext); |
| 5914 | CurContext->addDecl(Specialization); |
| 5915 | |
| 5916 | // Syntax is now OK, so return if it has no other effect on semantics. |
| 5917 | if (HasNoEffect) { |
| 5918 | // Set the template specialization kind. |
| 5919 | Specialization->setTemplateSpecializationKind(TSK); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5920 | return Specialization; |
Douglas Gregor | d78f598 | 2009-11-25 06:01:46 +0000 | [diff] [blame] | 5921 | } |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5922 | |
| 5923 | // C++ [temp.explicit]p3: |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5924 | // A definition of a class template or class member template |
| 5925 | // shall be in scope at the point of the explicit instantiation of |
| 5926 | // the class template or class member template. |
| 5927 | // |
| 5928 | // This check comes when we actually try to perform the |
| 5929 | // instantiation. |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 5930 | ClassTemplateSpecializationDecl *Def |
| 5931 | = cast_or_null<ClassTemplateSpecializationDecl>( |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 5932 | Specialization->getDefinition()); |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 5933 | if (!Def) |
Douglas Gregor | 972e6ce | 2009-10-27 06:26:26 +0000 | [diff] [blame] | 5934 | InstantiateClassTemplateSpecialization(TemplateNameLoc, Specialization, TSK); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5935 | else if (TSK == TSK_ExplicitInstantiationDefinition) { |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 5936 | MarkVTableUsed(TemplateNameLoc, Specialization, true); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5937 | Specialization->setPointOfInstantiation(Def->getPointOfInstantiation()); |
| 5938 | } |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 5939 | |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 5940 | // Instantiate the members of this class template specialization. |
| 5941 | Def = cast_or_null<ClassTemplateSpecializationDecl>( |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 5942 | Specialization->getDefinition()); |
Rafael Espindola | b0f65ca | 2010-03-22 23:12:48 +0000 | [diff] [blame] | 5943 | if (Def) { |
Rafael Espindola | f075b22 | 2010-03-23 19:55:22 +0000 | [diff] [blame] | 5944 | TemplateSpecializationKind Old_TSK = Def->getTemplateSpecializationKind(); |
| 5945 | |
| 5946 | // Fix a TSK_ExplicitInstantiationDeclaration followed by a |
| 5947 | // TSK_ExplicitInstantiationDefinition |
| 5948 | if (Old_TSK == TSK_ExplicitInstantiationDeclaration && |
| 5949 | TSK == TSK_ExplicitInstantiationDefinition) |
| 5950 | Def->setTemplateSpecializationKind(TSK); |
Rafael Espindola | b0f65ca | 2010-03-22 23:12:48 +0000 | [diff] [blame] | 5951 | |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 5952 | InstantiateClassTemplateSpecializationMembers(TemplateNameLoc, Def, TSK); |
Rafael Espindola | b0f65ca | 2010-03-22 23:12:48 +0000 | [diff] [blame] | 5953 | } |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5954 | |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5955 | // Set the template specialization kind. |
| 5956 | Specialization->setTemplateSpecializationKind(TSK); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5957 | return Specialization; |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5958 | } |
| 5959 | |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 5960 | // Explicit instantiation of a member class of a class template. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5961 | DeclResult |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5962 | Sema::ActOnExplicitInstantiation(Scope *S, |
Douglas Gregor | 45f9655 | 2009-09-04 06:33:52 +0000 | [diff] [blame] | 5963 | SourceLocation ExternLoc, |
| 5964 | SourceLocation TemplateLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5965 | unsigned TagSpec, |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 5966 | SourceLocation KWLoc, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 5967 | CXXScopeSpec &SS, |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 5968 | IdentifierInfo *Name, |
| 5969 | SourceLocation NameLoc, |
| 5970 | AttributeList *Attr) { |
| 5971 | |
Douglas Gregor | 402abb5 | 2009-05-28 23:31:59 +0000 | [diff] [blame] | 5972 | bool Owned = false; |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 5973 | bool IsDependent = false; |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5974 | Decl *TagD = ActOnTag(S, TagSpec, Sema::TUK_Reference, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5975 | KWLoc, SS, Name, NameLoc, Attr, AS_none, |
| 5976 | MultiTemplateParamsArg(*this, 0, 0), |
Abramo Bagnara | a88cefd | 2010-12-03 18:54:17 +0000 | [diff] [blame] | 5977 | Owned, IsDependent, false, false, |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 5978 | TypeResult()); |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 5979 | assert(!IsDependent && "explicit instantiation of dependent name not yet handled"); |
| 5980 | |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 5981 | if (!TagD) |
| 5982 | return true; |
| 5983 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5984 | TagDecl *Tag = cast<TagDecl>(TagD); |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 5985 | if (Tag->isEnum()) { |
| 5986 | Diag(TemplateLoc, diag::err_explicit_instantiation_enum) |
| 5987 | << Context.getTypeDeclType(Tag); |
| 5988 | return true; |
| 5989 | } |
| 5990 | |
Douglas Gregor | d0c8737 | 2009-05-27 17:30:49 +0000 | [diff] [blame] | 5991 | if (Tag->isInvalidDecl()) |
| 5992 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5993 | |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 5994 | CXXRecordDecl *Record = cast<CXXRecordDecl>(Tag); |
| 5995 | CXXRecordDecl *Pattern = Record->getInstantiatedFromMemberClass(); |
| 5996 | if (!Pattern) { |
| 5997 | Diag(TemplateLoc, diag::err_explicit_instantiation_nontemplate_type) |
| 5998 | << Context.getTypeDeclType(Record); |
| 5999 | Diag(Record->getLocation(), diag::note_nontemplate_decl_here); |
| 6000 | return true; |
| 6001 | } |
| 6002 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6003 | // C++0x [temp.explicit]p2: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6004 | // If the explicit instantiation is for a class or member class, the |
| 6005 | // elaborated-type-specifier in the declaration shall include a |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6006 | // simple-template-id. |
| 6007 | // |
| 6008 | // C++98 has the same restriction, just worded differently. |
| 6009 | if (!ScopeSpecifierHasTemplateId(SS)) |
Douglas Gregor | a2dd828 | 2010-06-16 16:26:47 +0000 | [diff] [blame] | 6010 | Diag(TemplateLoc, diag::ext_explicit_instantiation_without_qualified_id) |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6011 | << Record << SS.getRange(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6012 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6013 | // C++0x [temp.explicit]p2: |
| 6014 | // There are two forms of explicit instantiation: an explicit instantiation |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6015 | // definition and an explicit instantiation declaration. An explicit |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6016 | // instantiation declaration begins with the extern keyword. [...] |
Douglas Gregor | a74bbe2 | 2009-10-14 21:46:58 +0000 | [diff] [blame] | 6017 | TemplateSpecializationKind TSK |
| 6018 | = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition |
| 6019 | : TSK_ExplicitInstantiationDeclaration; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6020 | |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 6021 | // C++0x [temp.explicit]p2: |
| 6022 | // [...] An explicit instantiation shall appear in an enclosing |
| 6023 | // namespace of its template. [...] |
| 6024 | // |
| 6025 | // This is C++ DR 275. |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6026 | CheckExplicitInstantiationScope(*this, Record, NameLoc, true); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6027 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6028 | // Verify that it is okay to explicitly instantiate here. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6029 | CXXRecordDecl *PrevDecl |
Douglas Gregor | 583f33b | 2009-10-15 18:07:02 +0000 | [diff] [blame] | 6030 | = cast_or_null<CXXRecordDecl>(Record->getPreviousDeclaration()); |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 6031 | if (!PrevDecl && Record->getDefinition()) |
Douglas Gregor | 583f33b | 2009-10-15 18:07:02 +0000 | [diff] [blame] | 6032 | PrevDecl = Record; |
| 6033 | if (PrevDecl) { |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6034 | MemberSpecializationInfo *MSInfo = PrevDecl->getMemberSpecializationInfo(); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6035 | bool HasNoEffect = false; |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6036 | assert(MSInfo && "No member specialization information?"); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6037 | if (CheckSpecializationInstantiationRedecl(TemplateLoc, TSK, |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6038 | PrevDecl, |
| 6039 | MSInfo->getTemplateSpecializationKind(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6040 | MSInfo->getPointOfInstantiation(), |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6041 | HasNoEffect)) |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6042 | return true; |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6043 | if (HasNoEffect) |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6044 | return TagD; |
| 6045 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6046 | |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 6047 | CXXRecordDecl *RecordDef |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 6048 | = cast_or_null<CXXRecordDecl>(Record->getDefinition()); |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 6049 | if (!RecordDef) { |
Douglas Gregor | bf7643e | 2009-10-15 12:53:22 +0000 | [diff] [blame] | 6050 | // C++ [temp.explicit]p3: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6051 | // A definition of a member class of a class template shall be in scope |
Douglas Gregor | bf7643e | 2009-10-15 12:53:22 +0000 | [diff] [blame] | 6052 | // at the point of an explicit instantiation of the member class. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6053 | CXXRecordDecl *Def |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 6054 | = cast_or_null<CXXRecordDecl>(Pattern->getDefinition()); |
Douglas Gregor | bf7643e | 2009-10-15 12:53:22 +0000 | [diff] [blame] | 6055 | if (!Def) { |
Douglas Gregor | e2d3a3d | 2009-10-15 14:05:49 +0000 | [diff] [blame] | 6056 | Diag(TemplateLoc, diag::err_explicit_instantiation_undefined_member) |
| 6057 | << 0 << Record->getDeclName() << Record->getDeclContext(); |
Douglas Gregor | bf7643e | 2009-10-15 12:53:22 +0000 | [diff] [blame] | 6058 | Diag(Pattern->getLocation(), diag::note_forward_declaration) |
| 6059 | << Pattern; |
| 6060 | return true; |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 6061 | } else { |
| 6062 | if (InstantiateClass(NameLoc, Record, Def, |
| 6063 | getTemplateInstantiationArgs(Record), |
| 6064 | TSK)) |
| 6065 | return true; |
| 6066 | |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 6067 | RecordDef = cast_or_null<CXXRecordDecl>(Record->getDefinition()); |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 6068 | if (!RecordDef) |
| 6069 | return true; |
| 6070 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6071 | } |
| 6072 | |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 6073 | // Instantiate all of the members of the class. |
| 6074 | InstantiateClassMembers(NameLoc, RecordDef, |
| 6075 | getTemplateInstantiationArgs(Record), TSK); |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 6076 | |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 6077 | if (TSK == TSK_ExplicitInstantiationDefinition) |
| 6078 | MarkVTableUsed(NameLoc, RecordDef, true); |
| 6079 | |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 6080 | // FIXME: We don't have any representation for explicit instantiations of |
| 6081 | // member classes. Such a representation is not needed for compilation, but it |
| 6082 | // should be available for clients that want to see all of the declarations in |
| 6083 | // the source code. |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 6084 | return TagD; |
| 6085 | } |
| 6086 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 6087 | DeclResult Sema::ActOnExplicitInstantiation(Scope *S, |
| 6088 | SourceLocation ExternLoc, |
| 6089 | SourceLocation TemplateLoc, |
| 6090 | Declarator &D) { |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6091 | // Explicit instantiations always require a name. |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 6092 | // TODO: check if/when DNInfo should replace Name. |
| 6093 | DeclarationNameInfo NameInfo = GetNameForDeclarator(D); |
| 6094 | DeclarationName Name = NameInfo.getName(); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6095 | if (!Name) { |
| 6096 | if (!D.isInvalidType()) |
| 6097 | Diag(D.getDeclSpec().getSourceRange().getBegin(), |
| 6098 | diag::err_explicit_instantiation_requires_name) |
| 6099 | << D.getDeclSpec().getSourceRange() |
| 6100 | << D.getSourceRange(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6101 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6102 | return true; |
| 6103 | } |
| 6104 | |
| 6105 | // The scope passed in may not be a decl scope. Zip up the scope tree until |
| 6106 | // we find one that is. |
| 6107 | while ((S->getFlags() & Scope::DeclScope) == 0 || |
| 6108 | (S->getFlags() & Scope::TemplateParamScope) != 0) |
| 6109 | S = S->getParent(); |
| 6110 | |
| 6111 | // Determine the type of the declaration. |
John McCall | bf1a028 | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 6112 | TypeSourceInfo *T = GetTypeForDeclarator(D, S); |
| 6113 | QualType R = T->getType(); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6114 | if (R.isNull()) |
| 6115 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6116 | |
Douglas Gregor | e885e18 | 2011-05-21 18:53:30 +0000 | [diff] [blame] | 6117 | // C++ [dcl.stc]p1: |
| 6118 | // A storage-class-specifier shall not be specified in [...] an explicit |
| 6119 | // instantiation (14.7.2) directive. |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6120 | if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef) { |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6121 | Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_of_typedef) |
| 6122 | << Name; |
| 6123 | return true; |
Douglas Gregor | e885e18 | 2011-05-21 18:53:30 +0000 | [diff] [blame] | 6124 | } else if (D.getDeclSpec().getStorageClassSpec() |
| 6125 | != DeclSpec::SCS_unspecified) { |
| 6126 | // Complain about then remove the storage class specifier. |
| 6127 | Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_storage_class) |
| 6128 | << FixItHint::CreateRemoval(D.getDeclSpec().getStorageClassSpecLoc()); |
| 6129 | |
| 6130 | D.getMutableDeclSpec().ClearStorageClassSpecs(); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6131 | } |
| 6132 | |
Douglas Gregor | 663b5a0 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 6133 | // C++0x [temp.explicit]p1: |
| 6134 | // [...] An explicit instantiation of a function template shall not use the |
| 6135 | // inline or constexpr specifiers. |
| 6136 | // Presumably, this also applies to member functions of class templates as |
| 6137 | // well. |
| 6138 | if (D.getDeclSpec().isInlineSpecified() && getLangOptions().CPlusPlus0x) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6139 | Diag(D.getDeclSpec().getInlineSpecLoc(), |
Douglas Gregor | 663b5a0 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 6140 | diag::err_explicit_instantiation_inline) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 6141 | <<FixItHint::CreateRemoval(D.getDeclSpec().getInlineSpecLoc()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6142 | |
Douglas Gregor | 663b5a0 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 6143 | // FIXME: check for constexpr specifier. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6144 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6145 | // C++0x [temp.explicit]p2: |
| 6146 | // There are two forms of explicit instantiation: an explicit instantiation |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6147 | // definition and an explicit instantiation declaration. An explicit |
| 6148 | // instantiation declaration begins with the extern keyword. [...] |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6149 | TemplateSpecializationKind TSK |
| 6150 | = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition |
| 6151 | : TSK_ExplicitInstantiationDeclaration; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6152 | |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 6153 | LookupResult Previous(*this, NameInfo, LookupOrdinaryName); |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 6154 | LookupParsedName(Previous, S, &D.getCXXScopeSpec()); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6155 | |
| 6156 | if (!R->isFunctionType()) { |
| 6157 | // C++ [temp.explicit]p1: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6158 | // A [...] static data member of a class template can be explicitly |
| 6159 | // instantiated from the member definition associated with its class |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6160 | // template. |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 6161 | if (Previous.isAmbiguous()) |
| 6162 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6163 | |
John McCall | 1bcee0a | 2009-12-02 08:25:40 +0000 | [diff] [blame] | 6164 | VarDecl *Prev = Previous.getAsSingle<VarDecl>(); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6165 | if (!Prev || !Prev->isStaticDataMember()) { |
| 6166 | // We expect to see a data data member here. |
| 6167 | Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_not_known) |
| 6168 | << Name; |
| 6169 | for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end(); |
| 6170 | P != PEnd; ++P) |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 6171 | Diag((*P)->getLocation(), diag::note_explicit_instantiation_here); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6172 | return true; |
| 6173 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6174 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6175 | if (!Prev->getInstantiatedFromStaticDataMember()) { |
| 6176 | // FIXME: Check for explicit specialization? |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6177 | Diag(D.getIdentifierLoc(), |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6178 | diag::err_explicit_instantiation_data_member_not_instantiated) |
| 6179 | << Prev; |
| 6180 | Diag(Prev->getLocation(), diag::note_explicit_instantiation_here); |
| 6181 | // FIXME: Can we provide a note showing where this was declared? |
| 6182 | return true; |
| 6183 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6184 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6185 | // C++0x [temp.explicit]p2: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6186 | // If the explicit instantiation is for a member function, a member class |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6187 | // or a static data member of a class template specialization, the name of |
| 6188 | // the class template specialization in the qualified-id for the member |
| 6189 | // name shall be a simple-template-id. |
| 6190 | // |
| 6191 | // C++98 has the same restriction, just worded differently. |
| 6192 | if (!ScopeSpecifierHasTemplateId(D.getCXXScopeSpec())) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6193 | Diag(D.getIdentifierLoc(), |
Douglas Gregor | a2dd828 | 2010-06-16 16:26:47 +0000 | [diff] [blame] | 6194 | diag::ext_explicit_instantiation_without_qualified_id) |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6195 | << Prev << D.getCXXScopeSpec().getRange(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6196 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6197 | // Check the scope of this explicit instantiation. |
| 6198 | CheckExplicitInstantiationScope(*this, Prev, D.getIdentifierLoc(), true); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6199 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6200 | // Verify that it is okay to explicitly instantiate here. |
| 6201 | MemberSpecializationInfo *MSInfo = Prev->getMemberSpecializationInfo(); |
| 6202 | assert(MSInfo && "Missing static data member specialization info?"); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6203 | bool HasNoEffect = false; |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 6204 | if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK, Prev, |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6205 | MSInfo->getTemplateSpecializationKind(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6206 | MSInfo->getPointOfInstantiation(), |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6207 | HasNoEffect)) |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6208 | return true; |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6209 | if (HasNoEffect) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6210 | return (Decl*) 0; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6211 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6212 | // Instantiate static data member. |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 6213 | Prev->setTemplateSpecializationKind(TSK, D.getIdentifierLoc()); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6214 | if (TSK == TSK_ExplicitInstantiationDefinition) |
Chandler Carruth | 58e390e | 2010-08-25 08:27:02 +0000 | [diff] [blame] | 6215 | InstantiateStaticDataMemberDefinition(D.getIdentifierLoc(), Prev); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6216 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6217 | // FIXME: Create an ExplicitInstantiation node? |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6218 | return (Decl*) 0; |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6219 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6220 | |
| 6221 | // If the declarator is a template-id, translate the parser's template |
Douglas Gregor | 0b60d9e | 2009-09-25 23:53:26 +0000 | [diff] [blame] | 6222 | // argument list into our AST format. |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 6223 | bool HasExplicitTemplateArgs = false; |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 6224 | TemplateArgumentListInfo TemplateArgs; |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 6225 | if (D.getName().getKind() == UnqualifiedId::IK_TemplateId) { |
| 6226 | TemplateIdAnnotation *TemplateId = D.getName().TemplateId; |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 6227 | TemplateArgs.setLAngleLoc(TemplateId->LAngleLoc); |
| 6228 | TemplateArgs.setRAngleLoc(TemplateId->RAngleLoc); |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 6229 | ASTTemplateArgsPtr TemplateArgsPtr(*this, |
| 6230 | TemplateId->getTemplateArgs(), |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 6231 | TemplateId->NumArgs); |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 6232 | translateTemplateArguments(TemplateArgsPtr, TemplateArgs); |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 6233 | HasExplicitTemplateArgs = true; |
Douglas Gregor | b2f81cf | 2009-10-01 23:51:25 +0000 | [diff] [blame] | 6234 | TemplateArgsPtr.release(); |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 6235 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6236 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6237 | // C++ [temp.explicit]p1: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6238 | // A [...] function [...] can be explicitly instantiated from its template. |
| 6239 | // A member function [...] of a class template can be explicitly |
| 6240 | // instantiated from the member definition associated with its class |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6241 | // template. |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6242 | UnresolvedSet<8> Matches; |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6243 | for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end(); |
| 6244 | P != PEnd; ++P) { |
| 6245 | NamedDecl *Prev = *P; |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 6246 | if (!HasExplicitTemplateArgs) { |
| 6247 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Prev)) { |
| 6248 | if (Context.hasSameUnqualifiedType(Method->getType(), R)) { |
| 6249 | Matches.clear(); |
Douglas Gregor | 48026d2 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 6250 | |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6251 | Matches.addDecl(Method, P.getAccess()); |
Douglas Gregor | 48026d2 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 6252 | if (Method->getTemplateSpecializationKind() == TSK_Undeclared) |
| 6253 | break; |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 6254 | } |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6255 | } |
| 6256 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6257 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6258 | FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Prev); |
| 6259 | if (!FunTmpl) |
| 6260 | continue; |
| 6261 | |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 6262 | TemplateDeductionInfo Info(Context, D.getIdentifierLoc()); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6263 | FunctionDecl *Specialization = 0; |
| 6264 | if (TemplateDeductionResult TDK |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6265 | = DeduceTemplateArguments(FunTmpl, |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 6266 | (HasExplicitTemplateArgs ? &TemplateArgs : 0), |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6267 | R, Specialization, Info)) { |
| 6268 | // FIXME: Keep track of almost-matches? |
| 6269 | (void)TDK; |
| 6270 | continue; |
| 6271 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6272 | |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6273 | Matches.addDecl(Specialization, P.getAccess()); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6274 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6275 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6276 | // Find the most specialized function template specialization. |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6277 | UnresolvedSetIterator Result |
Douglas Gregor | 5c7bf42 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 6278 | = getMostSpecialized(Matches.begin(), Matches.end(), TPOC_Other, 0, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6279 | D.getIdentifierLoc(), |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 6280 | PDiag(diag::err_explicit_instantiation_not_known) << Name, |
| 6281 | PDiag(diag::err_explicit_instantiation_ambiguous) << Name, |
| 6282 | PDiag(diag::note_explicit_instantiation_candidate)); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6283 | |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6284 | if (Result == Matches.end()) |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6285 | return true; |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6286 | |
| 6287 | // Ignore access control bits, we don't need them for redeclaration checking. |
| 6288 | FunctionDecl *Specialization = cast<FunctionDecl>(*Result); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6289 | |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 6290 | if (Specialization->getTemplateSpecializationKind() == TSK_Undeclared) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6291 | Diag(D.getIdentifierLoc(), |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6292 | diag::err_explicit_instantiation_member_function_not_instantiated) |
| 6293 | << Specialization |
| 6294 | << (Specialization->getTemplateSpecializationKind() == |
| 6295 | TSK_ExplicitSpecialization); |
| 6296 | Diag(Specialization->getLocation(), diag::note_explicit_instantiation_here); |
| 6297 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6298 | } |
| 6299 | |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 6300 | FunctionDecl *PrevDecl = Specialization->getPreviousDeclaration(); |
Douglas Gregor | 583f33b | 2009-10-15 18:07:02 +0000 | [diff] [blame] | 6301 | if (!PrevDecl && Specialization->isThisDeclarationADefinition()) |
| 6302 | PrevDecl = Specialization; |
| 6303 | |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 6304 | if (PrevDecl) { |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6305 | bool HasNoEffect = false; |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 6306 | if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6307 | PrevDecl, |
| 6308 | PrevDecl->getTemplateSpecializationKind(), |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 6309 | PrevDecl->getPointOfInstantiation(), |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6310 | HasNoEffect)) |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 6311 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6312 | |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 6313 | // FIXME: We may still want to build some representation of this |
| 6314 | // explicit specialization. |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6315 | if (HasNoEffect) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6316 | return (Decl*) 0; |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 6317 | } |
Anders Carlsson | 26d6e9d | 2009-11-24 05:34:41 +0000 | [diff] [blame] | 6318 | |
| 6319 | Specialization->setTemplateSpecializationKind(TSK, D.getIdentifierLoc()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6320 | |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 6321 | if (TSK == TSK_ExplicitInstantiationDefinition) |
Chandler Carruth | 58e390e | 2010-08-25 08:27:02 +0000 | [diff] [blame] | 6322 | InstantiateFunctionDefinition(D.getIdentifierLoc(), Specialization); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6323 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6324 | // C++0x [temp.explicit]p2: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6325 | // If the explicit instantiation is for a member function, a member class |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6326 | // or a static data member of a class template specialization, the name of |
| 6327 | // the class template specialization in the qualified-id for the member |
| 6328 | // name shall be a simple-template-id. |
| 6329 | // |
| 6330 | // C++98 has the same restriction, just worded differently. |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 6331 | FunctionTemplateDecl *FunTmpl = Specialization->getPrimaryTemplate(); |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 6332 | if (D.getName().getKind() != UnqualifiedId::IK_TemplateId && !FunTmpl && |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6333 | D.getCXXScopeSpec().isSet() && |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6334 | !ScopeSpecifierHasTemplateId(D.getCXXScopeSpec())) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6335 | Diag(D.getIdentifierLoc(), |
Douglas Gregor | a2dd828 | 2010-06-16 16:26:47 +0000 | [diff] [blame] | 6336 | diag::ext_explicit_instantiation_without_qualified_id) |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6337 | << Specialization << D.getCXXScopeSpec().getRange(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6338 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6339 | CheckExplicitInstantiationScope(*this, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6340 | FunTmpl? (NamedDecl *)FunTmpl |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6341 | : Specialization->getInstantiatedFromMemberFunction(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6342 | D.getIdentifierLoc(), |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6343 | D.getCXXScopeSpec().isSet()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6344 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6345 | // FIXME: Create some kind of ExplicitInstantiationDecl here. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6346 | return (Decl*) 0; |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6347 | } |
| 6348 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 6349 | TypeResult |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 6350 | Sema::ActOnDependentTag(Scope *S, unsigned TagSpec, TagUseKind TUK, |
| 6351 | const CXXScopeSpec &SS, IdentifierInfo *Name, |
| 6352 | SourceLocation TagLoc, SourceLocation NameLoc) { |
| 6353 | // This has to hold, because SS is expected to be defined. |
| 6354 | assert(Name && "Expected a name in a dependent tag"); |
| 6355 | |
| 6356 | NestedNameSpecifier *NNS |
| 6357 | = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); |
| 6358 | if (!NNS) |
| 6359 | return true; |
| 6360 | |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 6361 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
Daniel Dunbar | 12c0ade | 2010-04-01 16:50:48 +0000 | [diff] [blame] | 6362 | |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 6363 | if (TUK == TUK_Declaration || TUK == TUK_Definition) { |
| 6364 | Diag(NameLoc, diag::err_dependent_tag_decl) |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 6365 | << (TUK == TUK_Definition) << Kind << SS.getRange(); |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 6366 | return true; |
| 6367 | } |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 6368 | |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 6369 | // Create the resulting type. |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 6370 | ElaboratedTypeKeyword Kwd = TypeWithKeyword::getKeywordForTagTypeKind(Kind); |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 6371 | QualType Result = Context.getDependentNameType(Kwd, NNS, Name); |
| 6372 | |
| 6373 | // Create type-source location information for this type. |
| 6374 | TypeLocBuilder TLB; |
| 6375 | DependentNameTypeLoc TL = TLB.push<DependentNameTypeLoc>(Result); |
| 6376 | TL.setKeywordLoc(TagLoc); |
| 6377 | TL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 6378 | TL.setNameLoc(NameLoc); |
| 6379 | return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result)); |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 6380 | } |
| 6381 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 6382 | TypeResult |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6383 | Sema::ActOnTypenameType(Scope *S, SourceLocation TypenameLoc, |
| 6384 | const CXXScopeSpec &SS, const IdentifierInfo &II, |
Douglas Gregor | 1a15dae | 2010-06-16 22:31:08 +0000 | [diff] [blame] | 6385 | SourceLocation IdLoc) { |
Douglas Gregor | e29425b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 6386 | if (SS.isInvalid()) |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6387 | return true; |
Douglas Gregor | e29425b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 6388 | |
Douglas Gregor | 1a15dae | 2010-06-16 22:31:08 +0000 | [diff] [blame] | 6389 | if (TypenameLoc.isValid() && S && !S->getTemplateParamParent() && |
| 6390 | !getLangOptions().CPlusPlus0x) |
| 6391 | Diag(TypenameLoc, diag::ext_typename_outside_of_template) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6392 | << FixItHint::CreateRemoval(TypenameLoc); |
| 6393 | |
Douglas Gregor | 2494dd0 | 2011-03-01 01:34:45 +0000 | [diff] [blame] | 6394 | NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context); |
Douglas Gregor | 9e87687 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 6395 | QualType T = CheckTypenameType(TypenameLoc.isValid()? ETK_Typename : ETK_None, |
| 6396 | TypenameLoc, QualifierLoc, II, IdLoc); |
Douglas Gregor | 31a19b6 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 6397 | if (T.isNull()) |
| 6398 | return true; |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 6399 | |
| 6400 | TypeSourceInfo *TSI = Context.CreateTypeSourceInfo(T); |
| 6401 | if (isa<DependentNameType>(T)) { |
| 6402 | DependentNameTypeLoc TL = cast<DependentNameTypeLoc>(TSI->getTypeLoc()); |
John McCall | 4e44983 | 2010-05-28 23:32:21 +0000 | [diff] [blame] | 6403 | TL.setKeywordLoc(TypenameLoc); |
Douglas Gregor | 2494dd0 | 2011-03-01 01:34:45 +0000 | [diff] [blame] | 6404 | TL.setQualifierLoc(QualifierLoc); |
John McCall | 4e44983 | 2010-05-28 23:32:21 +0000 | [diff] [blame] | 6405 | TL.setNameLoc(IdLoc); |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 6406 | } else { |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 6407 | ElaboratedTypeLoc TL = cast<ElaboratedTypeLoc>(TSI->getTypeLoc()); |
John McCall | 4e44983 | 2010-05-28 23:32:21 +0000 | [diff] [blame] | 6408 | TL.setKeywordLoc(TypenameLoc); |
Douglas Gregor | 9e87687 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 6409 | TL.setQualifierLoc(QualifierLoc); |
John McCall | 4e44983 | 2010-05-28 23:32:21 +0000 | [diff] [blame] | 6410 | cast<TypeSpecTypeLoc>(TL.getNamedTypeLoc()).setNameLoc(IdLoc); |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 6411 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6412 | |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 6413 | return CreateParsedType(T, TSI); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6414 | } |
| 6415 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 6416 | TypeResult |
Douglas Gregor | a02411e | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 6417 | Sema::ActOnTypenameType(Scope *S, SourceLocation TypenameLoc, |
| 6418 | const CXXScopeSpec &SS, |
| 6419 | SourceLocation TemplateLoc, |
| 6420 | TemplateTy TemplateIn, |
| 6421 | SourceLocation TemplateNameLoc, |
| 6422 | SourceLocation LAngleLoc, |
| 6423 | ASTTemplateArgsPtr TemplateArgsIn, |
| 6424 | SourceLocation RAngleLoc) { |
Douglas Gregor | 1a15dae | 2010-06-16 22:31:08 +0000 | [diff] [blame] | 6425 | if (TypenameLoc.isValid() && S && !S->getTemplateParamParent() && |
| 6426 | !getLangOptions().CPlusPlus0x) |
| 6427 | Diag(TypenameLoc, diag::ext_typename_outside_of_template) |
Douglas Gregor | a02411e | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 6428 | << FixItHint::CreateRemoval(TypenameLoc); |
| 6429 | |
| 6430 | // Translate the parser's template argument list in our AST format. |
| 6431 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
| 6432 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
| 6433 | |
| 6434 | TemplateName Template = TemplateIn.get(); |
Douglas Gregor | ef24c4b | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 6435 | if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) { |
| 6436 | // Construct a dependent template specialization type. |
| 6437 | assert(DTN && "dependent template has non-dependent name?"); |
| 6438 | assert(DTN->getQualifier() |
| 6439 | == static_cast<NestedNameSpecifier*>(SS.getScopeRep())); |
| 6440 | QualType T = Context.getDependentTemplateSpecializationType(ETK_Typename, |
| 6441 | DTN->getQualifier(), |
| 6442 | DTN->getIdentifier(), |
| 6443 | TemplateArgs); |
Douglas Gregor | a02411e | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 6444 | |
Douglas Gregor | ef24c4b | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 6445 | // Create source-location information for this type. |
John McCall | 4e44983 | 2010-05-28 23:32:21 +0000 | [diff] [blame] | 6446 | TypeLocBuilder Builder; |
Douglas Gregor | ef24c4b | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 6447 | DependentTemplateSpecializationTypeLoc SpecTL |
| 6448 | = Builder.push<DependentTemplateSpecializationTypeLoc>(T); |
Douglas Gregor | a02411e | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 6449 | SpecTL.setLAngleLoc(LAngleLoc); |
| 6450 | SpecTL.setRAngleLoc(RAngleLoc); |
Douglas Gregor | ef24c4b | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 6451 | SpecTL.setKeywordLoc(TypenameLoc); |
Douglas Gregor | 94fdffa | 2011-03-01 20:11:18 +0000 | [diff] [blame] | 6452 | SpecTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
Douglas Gregor | ef24c4b | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 6453 | SpecTL.setNameLoc(TemplateNameLoc); |
Douglas Gregor | a02411e | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 6454 | for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) |
| 6455 | SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo()); |
Douglas Gregor | ef24c4b | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 6456 | return CreateParsedType(T, Builder.getTypeSourceInfo(Context, T)); |
Douglas Gregor | 6946baf | 2009-09-02 13:05:45 +0000 | [diff] [blame] | 6457 | } |
Douglas Gregor | a02411e | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 6458 | |
Douglas Gregor | ef24c4b | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 6459 | QualType T = CheckTemplateIdType(Template, TemplateNameLoc, TemplateArgs); |
| 6460 | if (T.isNull()) |
| 6461 | return true; |
Douglas Gregor | a02411e | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 6462 | |
Douglas Gregor | ef24c4b | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 6463 | // Provide source-location information for the template specialization |
| 6464 | // type. |
Douglas Gregor | a02411e | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 6465 | TypeLocBuilder Builder; |
Douglas Gregor | ef24c4b | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 6466 | TemplateSpecializationTypeLoc SpecTL |
| 6467 | = Builder.push<TemplateSpecializationTypeLoc>(T); |
| 6468 | |
| 6469 | // FIXME: No place to set the location of the 'template' keyword! |
Douglas Gregor | a02411e | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 6470 | SpecTL.setLAngleLoc(LAngleLoc); |
| 6471 | SpecTL.setRAngleLoc(RAngleLoc); |
Douglas Gregor | ef24c4b | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 6472 | SpecTL.setTemplateNameLoc(TemplateNameLoc); |
Douglas Gregor | a02411e | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 6473 | for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) |
| 6474 | SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo()); |
| 6475 | |
Douglas Gregor | ef24c4b | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 6476 | T = Context.getElaboratedType(ETK_Typename, SS.getScopeRep(), T); |
| 6477 | ElaboratedTypeLoc TL = Builder.push<ElaboratedTypeLoc>(T); |
| 6478 | TL.setKeywordLoc(TypenameLoc); |
Douglas Gregor | 9e87687 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 6479 | TL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 6480 | |
Douglas Gregor | ef24c4b | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 6481 | TypeSourceInfo *TSI = Builder.getTypeSourceInfo(Context, T); |
| 6482 | return CreateParsedType(T, TSI); |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 6483 | } |
| 6484 | |
Douglas Gregor | a02411e | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 6485 | |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6486 | /// \brief Build the type that describes a C++ typename specifier, |
| 6487 | /// e.g., "typename T::type". |
| 6488 | QualType |
Douglas Gregor | e29425b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 6489 | Sema::CheckTypenameType(ElaboratedTypeKeyword Keyword, |
| 6490 | SourceLocation KeywordLoc, |
| 6491 | NestedNameSpecifierLoc QualifierLoc, |
| 6492 | const IdentifierInfo &II, |
Abramo Bagnara | e4da7a0 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 6493 | SourceLocation IILoc) { |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 6494 | CXXScopeSpec SS; |
Douglas Gregor | e29425b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 6495 | SS.Adopt(QualifierLoc); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6496 | |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 6497 | DeclContext *Ctx = computeDeclContext(SS); |
| 6498 | if (!Ctx) { |
| 6499 | // If the nested-name-specifier is dependent and couldn't be |
| 6500 | // resolved to a type, build a typename type. |
Douglas Gregor | e29425b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 6501 | assert(QualifierLoc.getNestedNameSpecifier()->isDependent()); |
| 6502 | return Context.getDependentNameType(Keyword, |
| 6503 | QualifierLoc.getNestedNameSpecifier(), |
| 6504 | &II); |
Douglas Gregor | 42af25f | 2009-05-11 19:58:34 +0000 | [diff] [blame] | 6505 | } |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6506 | |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 6507 | // If the nested-name-specifier refers to the current instantiation, |
| 6508 | // the "typename" keyword itself is superfluous. In C++03, the |
| 6509 | // program is actually ill-formed. However, DR 382 (in C++0x CD1) |
| 6510 | // allows such extraneous "typename" keywords, and we retroactively |
Douglas Gregor | 732281d | 2010-06-14 22:07:54 +0000 | [diff] [blame] | 6511 | // apply this DR to C++03 code with only a warning. In any case we continue. |
Douglas Gregor | 42af25f | 2009-05-11 19:58:34 +0000 | [diff] [blame] | 6512 | |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 6513 | if (RequireCompleteDeclContext(SS, Ctx)) |
| 6514 | return QualType(); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6515 | |
| 6516 | DeclarationName Name(&II); |
Abramo Bagnara | e4da7a0 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 6517 | LookupResult Result(*this, Name, IILoc, LookupOrdinaryName); |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 6518 | LookupQualifiedName(Result, Ctx); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6519 | unsigned DiagID = 0; |
| 6520 | Decl *Referenced = 0; |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 6521 | switch (Result.getResultKind()) { |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6522 | case LookupResult::NotFound: |
Douglas Gregor | 3f09327 | 2009-10-13 21:16:44 +0000 | [diff] [blame] | 6523 | DiagID = diag::err_typename_nested_not_found; |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6524 | break; |
Douglas Gregor | d954504 | 2010-12-09 00:06:27 +0000 | [diff] [blame] | 6525 | |
| 6526 | case LookupResult::FoundUnresolvedValue: { |
| 6527 | // We found a using declaration that is a value. Most likely, the using |
| 6528 | // declaration itself is meant to have the 'typename' keyword. |
Douglas Gregor | e29425b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 6529 | SourceRange FullRange(KeywordLoc.isValid() ? KeywordLoc : SS.getBeginLoc(), |
Douglas Gregor | d954504 | 2010-12-09 00:06:27 +0000 | [diff] [blame] | 6530 | IILoc); |
| 6531 | Diag(IILoc, diag::err_typename_refers_to_using_value_decl) |
| 6532 | << Name << Ctx << FullRange; |
| 6533 | if (UnresolvedUsingValueDecl *Using |
| 6534 | = dyn_cast<UnresolvedUsingValueDecl>(Result.getRepresentativeDecl())){ |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 6535 | SourceLocation Loc = Using->getQualifierLoc().getBeginLoc(); |
Douglas Gregor | d954504 | 2010-12-09 00:06:27 +0000 | [diff] [blame] | 6536 | Diag(Loc, diag::note_using_value_decl_missing_typename) |
| 6537 | << FixItHint::CreateInsertion(Loc, "typename "); |
| 6538 | } |
| 6539 | } |
| 6540 | // Fall through to create a dependent typename type, from which we can recover |
| 6541 | // better. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6542 | |
Douglas Gregor | 7d3f576 | 2010-01-15 01:44:47 +0000 | [diff] [blame] | 6543 | case LookupResult::NotFoundInCurrentInstantiation: |
| 6544 | // Okay, it's a member of an unknown instantiation. |
Douglas Gregor | e29425b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 6545 | return Context.getDependentNameType(Keyword, |
| 6546 | QualifierLoc.getNestedNameSpecifier(), |
| 6547 | &II); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6548 | |
| 6549 | case LookupResult::Found: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6550 | if (TypeDecl *Type = dyn_cast<TypeDecl>(Result.getFoundDecl())) { |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 6551 | // We found a type. Build an ElaboratedType, since the |
| 6552 | // typename-specifier was just sugar. |
Douglas Gregor | e29425b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 6553 | return Context.getElaboratedType(ETK_Typename, |
| 6554 | QualifierLoc.getNestedNameSpecifier(), |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 6555 | Context.getTypeDeclType(Type)); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6556 | } |
| 6557 | |
| 6558 | DiagID = diag::err_typename_nested_not_type; |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 6559 | Referenced = Result.getFoundDecl(); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6560 | break; |
| 6561 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6562 | |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 6563 | llvm_unreachable("unresolved using decl in non-dependent context"); |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 6564 | return QualType(); |
| 6565 | |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6566 | case LookupResult::FoundOverloaded: |
| 6567 | DiagID = diag::err_typename_nested_not_type; |
| 6568 | Referenced = *Result.begin(); |
| 6569 | break; |
| 6570 | |
John McCall | 6e24726 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 6571 | case LookupResult::Ambiguous: |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6572 | return QualType(); |
| 6573 | } |
| 6574 | |
| 6575 | // If we get here, it's because name lookup did not find a |
| 6576 | // type. Emit an appropriate diagnostic and return an error. |
Douglas Gregor | e29425b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 6577 | SourceRange FullRange(KeywordLoc.isValid() ? KeywordLoc : SS.getBeginLoc(), |
Abramo Bagnara | e4da7a0 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 6578 | IILoc); |
| 6579 | Diag(IILoc, DiagID) << FullRange << Name << Ctx; |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6580 | if (Referenced) |
| 6581 | Diag(Referenced->getLocation(), diag::note_typename_refers_here) |
| 6582 | << Name; |
| 6583 | return QualType(); |
| 6584 | } |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6585 | |
| 6586 | namespace { |
| 6587 | // See Sema::RebuildTypeInCurrentInstantiation |
Benjamin Kramer | 85b4521 | 2009-11-28 19:45:26 +0000 | [diff] [blame] | 6588 | class CurrentInstantiationRebuilder |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6589 | : public TreeTransform<CurrentInstantiationRebuilder> { |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6590 | SourceLocation Loc; |
| 6591 | DeclarationName Entity; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6592 | |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6593 | public: |
Douglas Gregor | 895162d | 2010-04-30 18:55:50 +0000 | [diff] [blame] | 6594 | typedef TreeTransform<CurrentInstantiationRebuilder> inherited; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6595 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6596 | CurrentInstantiationRebuilder(Sema &SemaRef, |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6597 | SourceLocation Loc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6598 | DeclarationName Entity) |
| 6599 | : TreeTransform<CurrentInstantiationRebuilder>(SemaRef), |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6600 | Loc(Loc), Entity(Entity) { } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6601 | |
| 6602 | /// \brief Determine whether the given type \p T has already been |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6603 | /// transformed. |
| 6604 | /// |
| 6605 | /// For the purposes of type reconstruction, a type has already been |
| 6606 | /// transformed if it is NULL or if it is not dependent. |
| 6607 | bool AlreadyTransformed(QualType T) { |
| 6608 | return T.isNull() || !T->isDependentType(); |
| 6609 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6610 | |
| 6611 | /// \brief Returns the location of the entity whose type is being |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6612 | /// rebuilt. |
| 6613 | SourceLocation getBaseLocation() { return Loc; } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6614 | |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6615 | /// \brief Returns the name of the entity whose type is being rebuilt. |
| 6616 | DeclarationName getBaseEntity() { return Entity; } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6617 | |
Douglas Gregor | 972e6ce | 2009-10-27 06:26:26 +0000 | [diff] [blame] | 6618 | /// \brief Sets the "base" location and entity when that |
| 6619 | /// information is known based on another transformation. |
| 6620 | void setBase(SourceLocation Loc, DeclarationName Entity) { |
| 6621 | this->Loc = Loc; |
| 6622 | this->Entity = Entity; |
| 6623 | } |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6624 | }; |
| 6625 | } |
| 6626 | |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6627 | /// \brief Rebuilds a type within the context of the current instantiation. |
| 6628 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6629 | /// The type \p T is part of the type of an out-of-line member definition of |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6630 | /// a class template (or class template partial specialization) that was parsed |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6631 | /// and constructed before we entered the scope of the class template (or |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6632 | /// partial specialization thereof). This routine will rebuild that type now |
| 6633 | /// that we have entered the declarator's scope, which may produce different |
| 6634 | /// canonical types, e.g., |
| 6635 | /// |
| 6636 | /// \code |
| 6637 | /// template<typename T> |
| 6638 | /// struct X { |
| 6639 | /// typedef T* pointer; |
| 6640 | /// pointer data(); |
| 6641 | /// }; |
| 6642 | /// |
| 6643 | /// template<typename T> |
| 6644 | /// typename X<T>::pointer X<T>::data() { ... } |
| 6645 | /// \endcode |
| 6646 | /// |
Douglas Gregor | 4714c12 | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 6647 | /// Here, the type "typename X<T>::pointer" will be created as a DependentNameType, |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6648 | /// since we do not know that we can look into X<T> when we parsed the type. |
| 6649 | /// This function will rebuild the type, performing the lookup of "pointer" |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 6650 | /// in X<T> and returning an ElaboratedType whose canonical type is the same |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6651 | /// as the canonical type of T*, allowing the return types of the out-of-line |
| 6652 | /// definition and the declaration to match. |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 6653 | TypeSourceInfo *Sema::RebuildTypeInCurrentInstantiation(TypeSourceInfo *T, |
| 6654 | SourceLocation Loc, |
| 6655 | DeclarationName Name) { |
| 6656 | if (!T || !T->getType()->isDependentType()) |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6657 | return T; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6658 | |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6659 | CurrentInstantiationRebuilder Rebuilder(*this, Loc, Name); |
| 6660 | return Rebuilder.TransformType(T); |
Benjamin Kramer | 27ba2f0 | 2009-08-11 22:33:06 +0000 | [diff] [blame] | 6661 | } |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6662 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 6663 | ExprResult Sema::RebuildExprInCurrentInstantiation(Expr *E) { |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 6664 | CurrentInstantiationRebuilder Rebuilder(*this, E->getExprLoc(), |
| 6665 | DeclarationName()); |
| 6666 | return Rebuilder.TransformExpr(E); |
| 6667 | } |
| 6668 | |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 6669 | bool Sema::RebuildNestedNameSpecifierInCurrentInstantiation(CXXScopeSpec &SS) { |
Douglas Gregor | 7e38494 | 2011-02-25 16:07:42 +0000 | [diff] [blame] | 6670 | if (SS.isInvalid()) |
| 6671 | return true; |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 6672 | |
Douglas Gregor | 7e38494 | 2011-02-25 16:07:42 +0000 | [diff] [blame] | 6673 | NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context); |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 6674 | CurrentInstantiationRebuilder Rebuilder(*this, SS.getRange().getBegin(), |
| 6675 | DeclarationName()); |
Douglas Gregor | 7e38494 | 2011-02-25 16:07:42 +0000 | [diff] [blame] | 6676 | NestedNameSpecifierLoc Rebuilt |
| 6677 | = Rebuilder.TransformNestedNameSpecifierLoc(QualifierLoc); |
| 6678 | if (!Rebuilt) |
| 6679 | return true; |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 6680 | |
Douglas Gregor | 7e38494 | 2011-02-25 16:07:42 +0000 | [diff] [blame] | 6681 | SS.Adopt(Rebuilt); |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 6682 | return false; |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 6683 | } |
| 6684 | |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6685 | /// \brief Produces a formatted string that describes the binding of |
| 6686 | /// template parameters to template arguments. |
| 6687 | std::string |
| 6688 | Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params, |
| 6689 | const TemplateArgumentList &Args) { |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 6690 | return getTemplateArgumentBindingsText(Params, Args.data(), Args.size()); |
Douglas Gregor | 9148c3f | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 6691 | } |
| 6692 | |
| 6693 | std::string |
| 6694 | Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params, |
| 6695 | const TemplateArgument *Args, |
| 6696 | unsigned NumArgs) { |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 6697 | llvm::SmallString<128> Str; |
| 6698 | llvm::raw_svector_ostream Out(Str); |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6699 | |
Douglas Gregor | 9148c3f | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 6700 | if (!Params || Params->size() == 0 || NumArgs == 0) |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 6701 | return std::string(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6702 | |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6703 | for (unsigned I = 0, N = Params->size(); I != N; ++I) { |
Douglas Gregor | 9148c3f | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 6704 | if (I >= NumArgs) |
| 6705 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6706 | |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6707 | if (I == 0) |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 6708 | Out << "[with "; |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6709 | else |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 6710 | Out << ", "; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6711 | |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6712 | if (const IdentifierInfo *Id = Params->getParam(I)->getIdentifier()) { |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 6713 | Out << Id->getName(); |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6714 | } else { |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 6715 | Out << '$' << I; |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6716 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6717 | |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 6718 | Out << " = "; |
| 6719 | Args[I].print(Context.PrintingPolicy, Out); |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6720 | } |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 6721 | |
| 6722 | Out << ']'; |
| 6723 | return Out.str(); |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6724 | } |
Francois Pichet | 8387e2a | 2011-04-22 22:18:13 +0000 | [diff] [blame] | 6725 | |
| 6726 | void Sema::MarkAsLateParsedTemplate(FunctionDecl *FD, bool Flag) { |
| 6727 | if (!FD) |
| 6728 | return; |
| 6729 | FD->setLateTemplateParsed(Flag); |
| 6730 | } |
| 6731 | |
| 6732 | bool Sema::IsInsideALocalClassWithinATemplateFunction() { |
| 6733 | DeclContext *DC = CurContext; |
| 6734 | |
| 6735 | while (DC) { |
| 6736 | if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(CurContext)) { |
| 6737 | const FunctionDecl *FD = RD->isLocalClass(); |
| 6738 | return (FD && FD->getTemplatedKind() != FunctionDecl::TK_NonTemplate); |
| 6739 | } else if (DC->isTranslationUnit() || DC->isNamespace()) |
| 6740 | return false; |
| 6741 | |
| 6742 | DC = DC->getParent(); |
| 6743 | } |
| 6744 | return false; |
| 6745 | } |