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 | |
| 12 | #include "Sema.h" |
John McCall | 7d384dd | 2009-11-18 07:57:50 +0000 | [diff] [blame] | 13 | #include "Lookup.h" |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 14 | #include "TreeTransform.h" |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 15 | #include "clang/AST/ASTContext.h" |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 16 | #include "clang/AST/Expr.h" |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 17 | #include "clang/AST/ExprCXX.h" |
John McCall | 92b7f70 | 2010-03-11 07:50:04 +0000 | [diff] [blame] | 18 | #include "clang/AST/DeclFriend.h" |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 19 | #include "clang/AST/DeclTemplate.h" |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 20 | #include "clang/Parse/DeclSpec.h" |
Douglas Gregor | 314b97f | 2009-11-10 19:49:08 +0000 | [diff] [blame] | 21 | #include "clang/Parse/Template.h" |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 22 | #include "clang/Basic/LangOptions.h" |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 23 | #include "clang/Basic/PartialDiagnostic.h" |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/StringExtras.h" |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 25 | using namespace clang; |
| 26 | |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 27 | /// \brief Determine whether the declaration found is acceptable as the name |
| 28 | /// of a template and, if so, return that template declaration. Otherwise, |
| 29 | /// returns NULL. |
| 30 | static NamedDecl *isAcceptableTemplateName(ASTContext &Context, NamedDecl *D) { |
| 31 | if (!D) |
| 32 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 33 | |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 34 | if (isa<TemplateDecl>(D)) |
| 35 | return D; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 36 | |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 37 | if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) { |
| 38 | // C++ [temp.local]p1: |
| 39 | // Like normal (non-template) classes, class templates have an |
| 40 | // injected-class-name (Clause 9). The injected-class-name |
| 41 | // can be used with or without a template-argument-list. When |
| 42 | // it is used without a template-argument-list, it is |
| 43 | // equivalent to the injected-class-name followed by the |
| 44 | // template-parameters of the class template enclosed in |
| 45 | // <>. When it is used with a template-argument-list, it |
| 46 | // refers to the specified class template specialization, |
| 47 | // which could be the current specialization or another |
| 48 | // specialization. |
| 49 | if (Record->isInjectedClassName()) { |
Douglas Gregor | 542b548 | 2009-10-14 17:30:58 +0000 | [diff] [blame] | 50 | Record = cast<CXXRecordDecl>(Record->getDeclContext()); |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 51 | if (Record->getDescribedClassTemplate()) |
| 52 | return Record->getDescribedClassTemplate(); |
| 53 | |
| 54 | if (ClassTemplateSpecializationDecl *Spec |
| 55 | = dyn_cast<ClassTemplateSpecializationDecl>(Record)) |
| 56 | return Spec->getSpecializedTemplate(); |
| 57 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 58 | |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 59 | return 0; |
| 60 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 61 | |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 62 | return 0; |
| 63 | } |
| 64 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 65 | static void FilterAcceptableTemplateNames(ASTContext &C, LookupResult &R) { |
Douglas Gregor | 01e56ae | 2010-04-12 20:54:26 +0000 | [diff] [blame] | 66 | // The set of class templates we've already seen. |
| 67 | llvm::SmallPtrSet<ClassTemplateDecl *, 8> ClassTemplates; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 68 | LookupResult::Filter filter = R.makeFilter(); |
| 69 | while (filter.hasNext()) { |
| 70 | NamedDecl *Orig = filter.next(); |
| 71 | NamedDecl *Repl = isAcceptableTemplateName(C, Orig->getUnderlyingDecl()); |
| 72 | if (!Repl) |
| 73 | filter.erase(); |
Douglas Gregor | 01e56ae | 2010-04-12 20:54:26 +0000 | [diff] [blame] | 74 | else if (Repl != Orig) { |
| 75 | |
| 76 | // C++ [temp.local]p3: |
| 77 | // A lookup that finds an injected-class-name (10.2) can result in an |
| 78 | // ambiguity in certain cases (for example, if it is found in more than |
| 79 | // one base class). If all of the injected-class-names that are found |
| 80 | // refer to specializations of the same class template, and if the name |
| 81 | // is followed by a template-argument-list, the reference refers to the |
| 82 | // class template itself and not a specialization thereof, and is not |
| 83 | // ambiguous. |
| 84 | // |
| 85 | // FIXME: Will we eventually have to do the same for alias templates? |
| 86 | if (ClassTemplateDecl *ClassTmpl = dyn_cast<ClassTemplateDecl>(Repl)) |
| 87 | if (!ClassTemplates.insert(ClassTmpl)) { |
| 88 | filter.erase(); |
| 89 | continue; |
| 90 | } |
| 91 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 92 | filter.replace(Repl); |
Douglas Gregor | 01e56ae | 2010-04-12 20:54:26 +0000 | [diff] [blame] | 93 | } |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 94 | } |
| 95 | filter.done(); |
| 96 | } |
| 97 | |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 98 | TemplateNameKind Sema::isTemplateName(Scope *S, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 99 | CXXScopeSpec &SS, |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 100 | UnqualifiedId &Name, |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 101 | TypeTy *ObjectTypePtr, |
Douglas Gregor | 495c35d | 2009-08-25 22:51:20 +0000 | [diff] [blame] | 102 | bool EnteringContext, |
Douglas Gregor | 1fd6d44 | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 103 | TemplateTy &TemplateResult, |
| 104 | bool &MemberOfUnknownSpecialization) { |
Douglas Gregor | b862b8f | 2010-01-11 23:29:10 +0000 | [diff] [blame] | 105 | assert(getLangOptions().CPlusPlus && "No template names in C!"); |
| 106 | |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 107 | DeclarationName TName; |
Douglas Gregor | 1fd6d44 | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 108 | MemberOfUnknownSpecialization = false; |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 109 | |
| 110 | switch (Name.getKind()) { |
| 111 | case UnqualifiedId::IK_Identifier: |
| 112 | TName = DeclarationName(Name.Identifier); |
| 113 | break; |
| 114 | |
| 115 | case UnqualifiedId::IK_OperatorFunctionId: |
| 116 | TName = Context.DeclarationNames.getCXXOperatorName( |
| 117 | Name.OperatorFunctionId.Operator); |
| 118 | break; |
| 119 | |
Sean Hunt | e6252d1 | 2009-11-28 08:58:14 +0000 | [diff] [blame] | 120 | case UnqualifiedId::IK_LiteralOperatorId: |
Sean Hunt | 3e518bd | 2009-11-29 07:34:05 +0000 | [diff] [blame] | 121 | TName = Context.DeclarationNames.getCXXLiteralOperatorName(Name.Identifier); |
| 122 | break; |
Sean Hunt | e6252d1 | 2009-11-28 08:58:14 +0000 | [diff] [blame] | 123 | |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 124 | default: |
| 125 | return TNK_Non_template; |
| 126 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 127 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 128 | QualType ObjectType = QualType::getFromOpaquePtr(ObjectTypePtr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 129 | |
Douglas Gregor | bfea239 | 2009-12-31 08:11:17 +0000 | [diff] [blame] | 130 | LookupResult R(*this, TName, Name.getSourceRange().getBegin(), |
| 131 | LookupOrdinaryName); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 132 | R.suppressDiagnostics(); |
Douglas Gregor | 1fd6d44 | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 133 | LookupTemplateName(R, S, SS, ObjectType, EnteringContext, |
| 134 | MemberOfUnknownSpecialization); |
Douglas Gregor | 01e56ae | 2010-04-12 20:54:26 +0000 | [diff] [blame] | 135 | if (R.empty() || R.isAmbiguous()) |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 136 | return TNK_Non_template; |
| 137 | |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 138 | TemplateName Template; |
| 139 | TemplateNameKind TemplateKind; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 140 | |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 141 | unsigned ResultCount = R.end() - R.begin(); |
| 142 | if (ResultCount > 1) { |
| 143 | // We assume that we'll preserve the qualifier from a function |
| 144 | // template name in other ways. |
| 145 | Template = Context.getOverloadedTemplateName(R.begin(), R.end()); |
| 146 | TemplateKind = TNK_Function_template; |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 147 | } else { |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 148 | TemplateDecl *TD = cast<TemplateDecl>((*R.begin())->getUnderlyingDecl()); |
| 149 | |
| 150 | if (SS.isSet() && !SS.isInvalid()) { |
| 151 | NestedNameSpecifier *Qualifier |
| 152 | = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); |
| 153 | Template = Context.getQualifiedTemplateName(Qualifier, false, TD); |
| 154 | } else { |
| 155 | Template = TemplateName(TD); |
| 156 | } |
| 157 | |
| 158 | if (isa<FunctionTemplateDecl>(TD)) |
| 159 | TemplateKind = TNK_Function_template; |
| 160 | else { |
| 161 | assert(isa<ClassTemplateDecl>(TD) || isa<TemplateTemplateParmDecl>(TD)); |
| 162 | TemplateKind = TNK_Type_template; |
| 163 | } |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 164 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 165 | |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 166 | TemplateResult = TemplateTy::make(Template); |
| 167 | return TemplateKind; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 168 | } |
| 169 | |
Douglas Gregor | 84d0a19 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 170 | bool Sema::DiagnoseUnknownTemplateName(const IdentifierInfo &II, |
| 171 | SourceLocation IILoc, |
| 172 | Scope *S, |
| 173 | const CXXScopeSpec *SS, |
| 174 | TemplateTy &SuggestedTemplate, |
| 175 | TemplateNameKind &SuggestedKind) { |
| 176 | // We can't recover unless there's a dependent scope specifier preceding the |
| 177 | // template name. |
Douglas Gregor | d5ab9b0 | 2010-05-21 23:43:39 +0000 | [diff] [blame] | 178 | // FIXME: Typo correction? |
Douglas Gregor | 84d0a19 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 179 | if (!SS || !SS->isSet() || !isDependentScopeSpecifier(*SS) || |
| 180 | computeDeclContext(*SS)) |
| 181 | return false; |
| 182 | |
| 183 | // The code is missing a 'template' keyword prior to the dependent template |
| 184 | // name. |
| 185 | NestedNameSpecifier *Qualifier = (NestedNameSpecifier*)SS->getScopeRep(); |
| 186 | Diag(IILoc, diag::err_template_kw_missing) |
| 187 | << Qualifier << II.getName() |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 188 | << FixItHint::CreateInsertion(IILoc, "template "); |
Douglas Gregor | 84d0a19 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 189 | SuggestedTemplate |
| 190 | = TemplateTy::make(Context.getDependentTemplateName(Qualifier, &II)); |
| 191 | SuggestedKind = TNK_Dependent_template_name; |
| 192 | return true; |
| 193 | } |
| 194 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 195 | void Sema::LookupTemplateName(LookupResult &Found, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 196 | Scope *S, CXXScopeSpec &SS, |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 197 | QualType ObjectType, |
Douglas Gregor | 1fd6d44 | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 198 | bool EnteringContext, |
| 199 | bool &MemberOfUnknownSpecialization) { |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 200 | // Determine where to perform name lookup |
Douglas Gregor | 1fd6d44 | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 201 | MemberOfUnknownSpecialization = false; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 202 | DeclContext *LookupCtx = 0; |
| 203 | bool isDependent = false; |
| 204 | if (!ObjectType.isNull()) { |
| 205 | // This nested-name-specifier occurs in a member access expression, e.g., |
| 206 | // x->B::f, and we are looking into the type of the object. |
| 207 | assert(!SS.isSet() && "ObjectType and scope specifier cannot coexist"); |
| 208 | LookupCtx = computeDeclContext(ObjectType); |
| 209 | isDependent = ObjectType->isDependentType(); |
| 210 | assert((isDependent || !ObjectType->isIncompleteType()) && |
| 211 | "Caller should have completed object type"); |
| 212 | } else if (SS.isSet()) { |
| 213 | // This nested-name-specifier occurs after another nested-name-specifier, |
| 214 | // so long into the context associated with the prior nested-name-specifier. |
| 215 | LookupCtx = computeDeclContext(SS, EnteringContext); |
| 216 | isDependent = isDependentScopeSpecifier(SS); |
| 217 | |
| 218 | // The declaration context must be complete. |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 219 | if (LookupCtx && RequireCompleteDeclContext(SS, LookupCtx)) |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 220 | return; |
| 221 | } |
| 222 | |
| 223 | bool ObjectTypeSearchedInScope = false; |
| 224 | if (LookupCtx) { |
| 225 | // Perform "qualified" name lookup into the declaration context we |
| 226 | // computed, which is either the type of the base of a member access |
| 227 | // expression or the declaration context associated with a prior |
| 228 | // nested-name-specifier. |
| 229 | LookupQualifiedName(Found, LookupCtx); |
| 230 | |
| 231 | if (!ObjectType.isNull() && Found.empty()) { |
| 232 | // C++ [basic.lookup.classref]p1: |
| 233 | // In a class member access expression (5.2.5), if the . or -> token is |
| 234 | // immediately followed by an identifier followed by a <, the |
| 235 | // identifier must be looked up to determine whether the < is the |
| 236 | // beginning of a template argument list (14.2) or a less-than operator. |
| 237 | // The identifier is first looked up in the class of the object |
| 238 | // expression. If the identifier is not found, it is then looked up in |
| 239 | // the context of the entire postfix-expression and shall name a class |
| 240 | // or function template. |
| 241 | // |
| 242 | // FIXME: When we're instantiating a template, do we actually have to |
| 243 | // look in the scope of the template? Seems fishy... |
| 244 | if (S) LookupName(Found, S); |
| 245 | ObjectTypeSearchedInScope = true; |
| 246 | } |
| 247 | } else if (isDependent) { |
Douglas Gregor | 2e93388 | 2010-01-12 17:06:20 +0000 | [diff] [blame] | 248 | // We cannot look into a dependent object type or nested nme |
| 249 | // specifier. |
Douglas Gregor | 1fd6d44 | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 250 | MemberOfUnknownSpecialization = true; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 251 | return; |
| 252 | } else { |
| 253 | // Perform unqualified name lookup in the current scope. |
| 254 | LookupName(Found, S); |
| 255 | } |
| 256 | |
Douglas Gregor | 2e93388 | 2010-01-12 17:06:20 +0000 | [diff] [blame] | 257 | if (Found.empty() && !isDependent) { |
Douglas Gregor | bfea239 | 2009-12-31 08:11:17 +0000 | [diff] [blame] | 258 | // If we did not find any names, attempt to correct any typos. |
| 259 | DeclarationName Name = Found.getLookupName(); |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 260 | if (DeclarationName Corrected = CorrectTypo(Found, S, &SS, LookupCtx, |
| 261 | false, CTC_CXXCasts)) { |
Douglas Gregor | bfea239 | 2009-12-31 08:11:17 +0000 | [diff] [blame] | 262 | FilterAcceptableTemplateNames(Context, Found); |
| 263 | if (!Found.empty() && isa<TemplateDecl>(*Found.begin())) { |
| 264 | if (LookupCtx) |
| 265 | Diag(Found.getNameLoc(), diag::err_no_member_template_suggest) |
| 266 | << Name << LookupCtx << Found.getLookupName() << SS.getRange() |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 267 | << FixItHint::CreateReplacement(Found.getNameLoc(), |
Douglas Gregor | bfea239 | 2009-12-31 08:11:17 +0000 | [diff] [blame] | 268 | Found.getLookupName().getAsString()); |
| 269 | else |
| 270 | Diag(Found.getNameLoc(), diag::err_no_template_suggest) |
| 271 | << Name << Found.getLookupName() |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 272 | << FixItHint::CreateReplacement(Found.getNameLoc(), |
Douglas Gregor | bfea239 | 2009-12-31 08:11:17 +0000 | [diff] [blame] | 273 | Found.getLookupName().getAsString()); |
Douglas Gregor | 67dd1d4 | 2010-01-07 00:17:44 +0000 | [diff] [blame] | 274 | if (TemplateDecl *Template = Found.getAsSingle<TemplateDecl>()) |
| 275 | Diag(Template->getLocation(), diag::note_previous_decl) |
| 276 | << Template->getDeclName(); |
Douglas Gregor | bfea239 | 2009-12-31 08:11:17 +0000 | [diff] [blame] | 277 | } else |
| 278 | Found.clear(); |
| 279 | } else { |
| 280 | Found.clear(); |
| 281 | } |
| 282 | } |
| 283 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 284 | FilterAcceptableTemplateNames(Context, Found); |
| 285 | if (Found.empty()) |
| 286 | return; |
| 287 | |
| 288 | if (S && !ObjectType.isNull() && !ObjectTypeSearchedInScope) { |
| 289 | // C++ [basic.lookup.classref]p1: |
| 290 | // [...] If the lookup in the class of the object expression finds a |
| 291 | // template, the name is also looked up in the context of the entire |
| 292 | // postfix-expression and [...] |
| 293 | // |
| 294 | LookupResult FoundOuter(*this, Found.getLookupName(), Found.getNameLoc(), |
| 295 | LookupOrdinaryName); |
| 296 | LookupName(FoundOuter, S); |
| 297 | FilterAcceptableTemplateNames(Context, FoundOuter); |
Douglas Gregor | 01e56ae | 2010-04-12 20:54:26 +0000 | [diff] [blame] | 298 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 299 | if (FoundOuter.empty()) { |
| 300 | // - if the name is not found, the name found in the class of the |
| 301 | // object expression is used, otherwise |
| 302 | } else if (!FoundOuter.getAsSingle<ClassTemplateDecl>()) { |
| 303 | // - if the name is found in the context of the entire |
| 304 | // postfix-expression and does not name a class template, the name |
| 305 | // found in the class of the object expression is used, otherwise |
| 306 | } else { |
| 307 | // - if the name found is a class template, it must refer to the same |
| 308 | // entity as the one found in the class of the object expression, |
| 309 | // otherwise the program is ill-formed. |
| 310 | if (!Found.isSingleResult() || |
| 311 | Found.getFoundDecl()->getCanonicalDecl() |
| 312 | != FoundOuter.getFoundDecl()->getCanonicalDecl()) { |
| 313 | Diag(Found.getNameLoc(), |
Jeffrey Yasskin | 21d07e4 | 2010-06-05 01:39:57 +0000 | [diff] [blame] | 314 | diag::ext_nested_name_member_ref_lookup_ambiguous) |
| 315 | << Found.getLookupName() |
| 316 | << ObjectType; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 317 | Diag(Found.getRepresentativeDecl()->getLocation(), |
| 318 | diag::note_ambig_member_ref_object_type) |
| 319 | << ObjectType; |
| 320 | Diag(FoundOuter.getFoundDecl()->getLocation(), |
| 321 | diag::note_ambig_member_ref_scope); |
| 322 | |
| 323 | // Recover by taking the template that we found in the object |
| 324 | // expression's type. |
| 325 | } |
| 326 | } |
| 327 | } |
| 328 | } |
| 329 | |
John McCall | 2f841ba | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 330 | /// ActOnDependentIdExpression - Handle a dependent id-expression that |
| 331 | /// was just parsed. This is only possible with an explicit scope |
| 332 | /// specifier naming a dependent type. |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 333 | Sema::OwningExprResult |
| 334 | Sema::ActOnDependentIdExpression(const CXXScopeSpec &SS, |
| 335 | DeclarationName Name, |
| 336 | SourceLocation NameLoc, |
John McCall | 2f841ba | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 337 | bool isAddressOfOperand, |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 338 | const TemplateArgumentListInfo *TemplateArgs) { |
| 339 | NestedNameSpecifier *Qualifier |
| 340 | = static_cast<NestedNameSpecifier*>(SS.getScopeRep()); |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 341 | |
| 342 | DeclContext *DC = getFunctionLevelDeclContext(); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 343 | |
John McCall | 2f841ba | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 344 | if (!isAddressOfOperand && |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 345 | isa<CXXMethodDecl>(DC) && |
| 346 | cast<CXXMethodDecl>(DC)->isInstance()) { |
| 347 | QualType ThisType = cast<CXXMethodDecl>(DC)->getThisType(Context); |
John McCall | 2f841ba | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 348 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 349 | // Since the 'this' expression is synthesized, we don't need to |
| 350 | // perform the double-lookup check. |
| 351 | NamedDecl *FirstQualifierInScope = 0; |
| 352 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 353 | return Owned(CXXDependentScopeMemberExpr::Create(Context, |
| 354 | /*This*/ 0, ThisType, |
| 355 | /*IsArrow*/ true, |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 356 | /*Op*/ SourceLocation(), |
| 357 | Qualifier, SS.getRange(), |
| 358 | FirstQualifierInScope, |
| 359 | Name, NameLoc, |
| 360 | TemplateArgs)); |
| 361 | } |
| 362 | |
| 363 | return BuildDependentDeclRefExpr(SS, Name, NameLoc, TemplateArgs); |
| 364 | } |
| 365 | |
| 366 | Sema::OwningExprResult |
| 367 | Sema::BuildDependentDeclRefExpr(const CXXScopeSpec &SS, |
| 368 | DeclarationName Name, |
| 369 | SourceLocation NameLoc, |
| 370 | const TemplateArgumentListInfo *TemplateArgs) { |
| 371 | return Owned(DependentScopeDeclRefExpr::Create(Context, |
| 372 | static_cast<NestedNameSpecifier*>(SS.getScopeRep()), |
| 373 | SS.getRange(), |
| 374 | Name, NameLoc, |
| 375 | TemplateArgs)); |
Douglas Gregor | d6fb7ef | 2008-12-18 19:37:40 +0000 | [diff] [blame] | 376 | } |
| 377 | |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 378 | /// DiagnoseTemplateParameterShadow - Produce a diagnostic complaining |
| 379 | /// that the template parameter 'PrevDecl' is being shadowed by a new |
| 380 | /// declaration at location Loc. Returns true to indicate that this is |
| 381 | /// an error, and false otherwise. |
| 382 | bool Sema::DiagnoseTemplateParameterShadow(SourceLocation Loc, Decl *PrevDecl) { |
Douglas Gregor | f57172b | 2008-12-08 18:40:42 +0000 | [diff] [blame] | 383 | assert(PrevDecl->isTemplateParameter() && "Not a template parameter"); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 384 | |
| 385 | // Microsoft Visual C++ permits template parameters to be shadowed. |
| 386 | if (getLangOptions().Microsoft) |
| 387 | return false; |
| 388 | |
| 389 | // C++ [temp.local]p4: |
| 390 | // A template-parameter shall not be redeclared within its |
| 391 | // scope (including nested scopes). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 392 | Diag(Loc, diag::err_template_param_shadow) |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 393 | << cast<NamedDecl>(PrevDecl)->getDeclName(); |
| 394 | Diag(PrevDecl->getLocation(), diag::note_template_param_here); |
| 395 | return true; |
| 396 | } |
| 397 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 398 | /// AdjustDeclIfTemplate - If the given decl happens to be a template, reset |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 399 | /// the parameter D to reference the templated declaration and return a pointer |
| 400 | /// to the template declaration. Otherwise, do nothing to D and return null. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 401 | TemplateDecl *Sema::AdjustDeclIfTemplate(DeclPtrTy &D) { |
Douglas Gregor | 13d2d6c | 2009-10-06 21:27:51 +0000 | [diff] [blame] | 402 | if (TemplateDecl *Temp = dyn_cast_or_null<TemplateDecl>(D.getAs<Decl>())) { |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 403 | D = DeclPtrTy::make(Temp->getTemplatedDecl()); |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 404 | return Temp; |
| 405 | } |
| 406 | return 0; |
| 407 | } |
| 408 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 409 | static TemplateArgumentLoc translateTemplateArgument(Sema &SemaRef, |
| 410 | const ParsedTemplateArgument &Arg) { |
| 411 | |
| 412 | switch (Arg.getKind()) { |
| 413 | case ParsedTemplateArgument::Type: { |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 414 | TypeSourceInfo *DI; |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 415 | QualType T = SemaRef.GetTypeFromParser(Arg.getAsType(), &DI); |
| 416 | if (!DI) |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 417 | DI = SemaRef.Context.getTrivialTypeSourceInfo(T, Arg.getLocation()); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 418 | return TemplateArgumentLoc(TemplateArgument(T), DI); |
| 419 | } |
| 420 | |
| 421 | case ParsedTemplateArgument::NonType: { |
| 422 | Expr *E = static_cast<Expr *>(Arg.getAsExpr()); |
| 423 | return TemplateArgumentLoc(TemplateArgument(E), E); |
| 424 | } |
| 425 | |
| 426 | case ParsedTemplateArgument::Template: { |
| 427 | TemplateName Template |
| 428 | = TemplateName::getFromVoidPointer(Arg.getAsTemplate().get()); |
| 429 | return TemplateArgumentLoc(TemplateArgument(Template), |
| 430 | Arg.getScopeSpec().getRange(), |
| 431 | Arg.getLocation()); |
| 432 | } |
| 433 | } |
| 434 | |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 435 | llvm_unreachable("Unhandled parsed template argument"); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 436 | return TemplateArgumentLoc(); |
| 437 | } |
| 438 | |
| 439 | /// \brief Translates template arguments as provided by the parser |
| 440 | /// into template arguments used by semantic analysis. |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 441 | void Sema::translateTemplateArguments(const ASTTemplateArgsPtr &TemplateArgsIn, |
| 442 | TemplateArgumentListInfo &TemplateArgs) { |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 443 | for (unsigned I = 0, Last = TemplateArgsIn.size(); I != Last; ++I) |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 444 | TemplateArgs.addArgument(translateTemplateArgument(*this, |
| 445 | TemplateArgsIn[I])); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 446 | } |
| 447 | |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 448 | /// ActOnTypeParameter - Called when a C++ template type parameter |
| 449 | /// (e.g., "typename T") has been parsed. Typename specifies whether |
| 450 | /// the keyword "typename" was used to declare the type parameter |
| 451 | /// (otherwise, "class" was used), and KeyLoc is the location of the |
| 452 | /// "class" or "typename" keyword. ParamName is the name of the |
| 453 | /// parameter (NULL indicates an unnamed template parameter) and |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 454 | /// ParamName is the location of the parameter name (if any). |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 455 | /// If the type parameter has a default argument, it will be added |
| 456 | /// later via ActOnTypeParameterDefault. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 457 | Sema::DeclPtrTy Sema::ActOnTypeParameter(Scope *S, bool Typename, bool Ellipsis, |
Anders Carlsson | 941df7d | 2009-06-12 19:58:00 +0000 | [diff] [blame] | 458 | SourceLocation EllipsisLoc, |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 459 | SourceLocation KeyLoc, |
| 460 | IdentifierInfo *ParamName, |
| 461 | SourceLocation ParamNameLoc, |
| 462 | unsigned Depth, unsigned Position) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 463 | assert(S->isTemplateParamScope() && |
| 464 | "Template type parameter not in template parameter scope!"); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 465 | bool Invalid = false; |
| 466 | |
| 467 | if (ParamName) { |
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 468 | NamedDecl *PrevDecl = LookupSingleName(S, ParamName, ParamNameLoc, |
Douglas Gregor | c0b3964 | 2010-04-15 23:40:53 +0000 | [diff] [blame] | 469 | LookupOrdinaryName, |
| 470 | ForRedeclaration); |
Douglas Gregor | f57172b | 2008-12-08 18:40:42 +0000 | [diff] [blame] | 471 | if (PrevDecl && PrevDecl->isTemplateParameter()) |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 472 | Invalid = Invalid || DiagnoseTemplateParameterShadow(ParamNameLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 473 | PrevDecl); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 474 | } |
| 475 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 476 | SourceLocation Loc = ParamNameLoc; |
| 477 | if (!ParamName) |
| 478 | Loc = KeyLoc; |
| 479 | |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 480 | TemplateTypeParmDecl *Param |
John McCall | 7a9813c | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 481 | = TemplateTypeParmDecl::Create(Context, Context.getTranslationUnitDecl(), |
| 482 | Loc, Depth, Position, ParamName, Typename, |
Anders Carlsson | 6d845ae | 2009-06-12 22:23:22 +0000 | [diff] [blame] | 483 | Ellipsis); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 484 | if (Invalid) |
| 485 | Param->setInvalidDecl(); |
| 486 | |
| 487 | if (ParamName) { |
| 488 | // Add the template parameter into the current scope. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 489 | S->AddDecl(DeclPtrTy::make(Param)); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 490 | IdResolver.AddDecl(Param); |
| 491 | } |
| 492 | |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 493 | return DeclPtrTy::make(Param); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 494 | } |
| 495 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 496 | /// ActOnTypeParameterDefault - Adds a default argument (the type |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 497 | /// Default) to the given template type parameter (TypeParam). |
| 498 | void Sema::ActOnTypeParameterDefault(DeclPtrTy TypeParam, |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 499 | SourceLocation EqualLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 500 | SourceLocation DefaultLoc, |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 501 | TypeTy *DefaultT) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 502 | TemplateTypeParmDecl *Parm |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 503 | = cast<TemplateTypeParmDecl>(TypeParam.getAs<Decl>()); |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 504 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 505 | TypeSourceInfo *DefaultTInfo; |
| 506 | GetTypeFromParser(DefaultT, &DefaultTInfo); |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 507 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 508 | assert(DefaultTInfo && "expected source information for type"); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 509 | |
Anders Carlsson | 9c4c5c8 | 2009-06-12 22:30:13 +0000 | [diff] [blame] | 510 | // C++0x [temp.param]p9: |
| 511 | // A default template-argument may be specified for any kind of |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 512 | // template-parameter that is not a template parameter pack. |
Anders Carlsson | 9c4c5c8 | 2009-06-12 22:30:13 +0000 | [diff] [blame] | 513 | if (Parm->isParameterPack()) { |
| 514 | Diag(DefaultLoc, diag::err_template_param_pack_default_arg); |
Anders Carlsson | 9c4c5c8 | 2009-06-12 22:30:13 +0000 | [diff] [blame] | 515 | return; |
| 516 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 517 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 518 | // C++ [temp.param]p14: |
| 519 | // A template-parameter shall not be used in its own default argument. |
| 520 | // FIXME: Implement this check! Needs a recursive walk over the types. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 521 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 522 | // Check the template argument itself. |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 523 | if (CheckTemplateArgument(Parm, DefaultTInfo)) { |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 524 | Parm->setInvalidDecl(); |
| 525 | return; |
| 526 | } |
| 527 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 528 | Parm->setDefaultArgument(DefaultTInfo, false); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 529 | } |
| 530 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 531 | /// \brief Check that the type of a non-type template parameter is |
| 532 | /// well-formed. |
| 533 | /// |
| 534 | /// \returns the (possibly-promoted) parameter type if valid; |
| 535 | /// otherwise, produces a diagnostic and returns a NULL type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 536 | QualType |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 537 | Sema::CheckNonTypeTemplateParameterType(QualType T, SourceLocation Loc) { |
Douglas Gregor | a481ec4 | 2010-05-23 19:57:01 +0000 | [diff] [blame] | 538 | // We don't allow variably-modified types as the type of non-type template |
| 539 | // parameters. |
| 540 | if (T->isVariablyModifiedType()) { |
| 541 | Diag(Loc, diag::err_variably_modified_nontype_template_param) |
| 542 | << T; |
| 543 | return QualType(); |
| 544 | } |
| 545 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 546 | // C++ [temp.param]p4: |
| 547 | // |
| 548 | // A non-type template-parameter shall have one of the following |
| 549 | // (optionally cv-qualified) types: |
| 550 | // |
| 551 | // -- integral or enumeration type, |
| 552 | if (T->isIntegralType() || T->isEnumeralType() || |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 553 | // -- pointer to object or pointer to function, |
| 554 | (T->isPointerType() && |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 555 | (T->getAs<PointerType>()->getPointeeType()->isObjectType() || |
| 556 | T->getAs<PointerType>()->getPointeeType()->isFunctionType())) || |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 557 | // -- reference to object or reference to function, |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 558 | T->isReferenceType() || |
| 559 | // -- pointer to member. |
| 560 | T->isMemberPointerType() || |
| 561 | // If T is a dependent type, we can't do the check now, so we |
| 562 | // assume that it is well-formed. |
| 563 | T->isDependentType()) |
| 564 | return T; |
| 565 | // C++ [temp.param]p8: |
| 566 | // |
| 567 | // A non-type template-parameter of type "array of T" or |
| 568 | // "function returning T" is adjusted to be of type "pointer to |
| 569 | // T" or "pointer to function returning T", respectively. |
| 570 | else if (T->isArrayType()) |
| 571 | // FIXME: Keep the type prior to promotion? |
| 572 | return Context.getArrayDecayedType(T); |
| 573 | else if (T->isFunctionType()) |
| 574 | // FIXME: Keep the type prior to promotion? |
| 575 | return Context.getPointerType(T); |
Douglas Gregor | 0fddb97 | 2010-05-22 16:17:30 +0000 | [diff] [blame] | 576 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 577 | Diag(Loc, diag::err_template_nontype_parm_bad_type) |
| 578 | << T; |
| 579 | |
| 580 | return QualType(); |
| 581 | } |
| 582 | |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 583 | /// ActOnNonTypeTemplateParameter - Called when a C++ non-type |
| 584 | /// template parameter (e.g., "int Size" in "template<int Size> |
| 585 | /// class Array") has been parsed. S is the current scope and D is |
| 586 | /// the parsed declarator. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 587 | Sema::DeclPtrTy Sema::ActOnNonTypeTemplateParameter(Scope *S, Declarator &D, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 588 | unsigned Depth, |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 589 | unsigned Position) { |
John McCall | bf1a028 | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 590 | TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S); |
| 591 | QualType T = TInfo->getType(); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 592 | |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 593 | assert(S->isTemplateParamScope() && |
| 594 | "Non-type template parameter not in template parameter scope!"); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 595 | bool Invalid = false; |
| 596 | |
| 597 | IdentifierInfo *ParamName = D.getIdentifier(); |
| 598 | if (ParamName) { |
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 599 | NamedDecl *PrevDecl = LookupSingleName(S, ParamName, D.getIdentifierLoc(), |
Douglas Gregor | c0b3964 | 2010-04-15 23:40:53 +0000 | [diff] [blame] | 600 | LookupOrdinaryName, |
| 601 | ForRedeclaration); |
Douglas Gregor | f57172b | 2008-12-08 18:40:42 +0000 | [diff] [blame] | 602 | if (PrevDecl && PrevDecl->isTemplateParameter()) |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 603 | Invalid = Invalid || DiagnoseTemplateParameterShadow(D.getIdentifierLoc(), |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 604 | PrevDecl); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 605 | } |
| 606 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 607 | T = CheckNonTypeTemplateParameterType(T, D.getIdentifierLoc()); |
Douglas Gregor | ceef30c | 2009-03-09 16:46:39 +0000 | [diff] [blame] | 608 | if (T.isNull()) { |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 609 | T = Context.IntTy; // Recover with an 'int' type. |
Douglas Gregor | ceef30c | 2009-03-09 16:46:39 +0000 | [diff] [blame] | 610 | Invalid = true; |
| 611 | } |
Douglas Gregor | 5d290d5 | 2009-02-10 17:43:50 +0000 | [diff] [blame] | 612 | |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 613 | NonTypeTemplateParmDecl *Param |
John McCall | 7a9813c | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 614 | = NonTypeTemplateParmDecl::Create(Context, Context.getTranslationUnitDecl(), |
| 615 | D.getIdentifierLoc(), |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 616 | Depth, Position, ParamName, T, TInfo); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 617 | if (Invalid) |
| 618 | Param->setInvalidDecl(); |
| 619 | |
| 620 | if (D.getIdentifier()) { |
| 621 | // Add the template parameter into the current scope. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 622 | S->AddDecl(DeclPtrTy::make(Param)); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 623 | IdResolver.AddDecl(Param); |
| 624 | } |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 625 | return DeclPtrTy::make(Param); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 626 | } |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 627 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 628 | /// \brief Adds a default argument to the given non-type template |
| 629 | /// parameter. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 630 | void Sema::ActOnNonTypeTemplateParameterDefault(DeclPtrTy TemplateParamD, |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 631 | SourceLocation EqualLoc, |
| 632 | ExprArg DefaultE) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 633 | NonTypeTemplateParmDecl *TemplateParm |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 634 | = cast<NonTypeTemplateParmDecl>(TemplateParamD.getAs<Decl>()); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 635 | Expr *Default = static_cast<Expr *>(DefaultE.get()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 636 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 637 | // C++ [temp.param]p14: |
| 638 | // A template-parameter shall not be used in its own default argument. |
| 639 | // FIXME: Implement this check! Needs a recursive walk over the types. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 640 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 641 | // Check the well-formedness of the default template argument. |
Douglas Gregor | 02cbbd2 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 642 | TemplateArgument Converted; |
| 643 | if (CheckTemplateArgument(TemplateParm, TemplateParm->getType(), Default, |
| 644 | Converted)) { |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 645 | TemplateParm->setInvalidDecl(); |
| 646 | return; |
| 647 | } |
| 648 | |
Abramo Bagnara | d92f7a2 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 649 | TemplateParm->setDefaultArgument(DefaultE.takeAs<Expr>(), false); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 650 | } |
| 651 | |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 652 | |
| 653 | /// ActOnTemplateTemplateParameter - Called when a C++ template template |
| 654 | /// parameter (e.g. T in template <template <typename> class T> class array) |
| 655 | /// has been parsed. S is the current scope. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 656 | Sema::DeclPtrTy Sema::ActOnTemplateTemplateParameter(Scope* S, |
| 657 | SourceLocation TmpLoc, |
| 658 | TemplateParamsTy *Params, |
| 659 | IdentifierInfo *Name, |
| 660 | SourceLocation NameLoc, |
| 661 | unsigned Depth, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 662 | unsigned Position) { |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 663 | assert(S->isTemplateParamScope() && |
| 664 | "Template template parameter not in template parameter scope!"); |
| 665 | |
| 666 | // Construct the parameter object. |
| 667 | TemplateTemplateParmDecl *Param = |
John McCall | 7a9813c | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 668 | TemplateTemplateParmDecl::Create(Context, Context.getTranslationUnitDecl(), |
| 669 | TmpLoc, Depth, Position, Name, |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 670 | (TemplateParameterList*)Params); |
| 671 | |
| 672 | // Make sure the parameter is valid. |
| 673 | // FIXME: Decl object is not currently invalidated anywhere so this doesn't |
| 674 | // do anything yet. However, if the template parameter list or (eventual) |
| 675 | // default value is ever invalidated, that will propagate here. |
| 676 | bool Invalid = false; |
| 677 | if (Invalid) { |
| 678 | Param->setInvalidDecl(); |
| 679 | } |
| 680 | |
| 681 | // If the tt-param has a name, then link the identifier into the scope |
| 682 | // and lookup mechanisms. |
| 683 | if (Name) { |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 684 | S->AddDecl(DeclPtrTy::make(Param)); |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 685 | IdResolver.AddDecl(Param); |
| 686 | } |
| 687 | |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 688 | return DeclPtrTy::make(Param); |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 689 | } |
| 690 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 691 | /// \brief Adds a default argument to the given template template |
| 692 | /// parameter. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 693 | void Sema::ActOnTemplateTemplateParameterDefault(DeclPtrTy TemplateParamD, |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 694 | SourceLocation EqualLoc, |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 695 | const ParsedTemplateArgument &Default) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 696 | TemplateTemplateParmDecl *TemplateParm |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 697 | = cast<TemplateTemplateParmDecl>(TemplateParamD.getAs<Decl>()); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 698 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 699 | // C++ [temp.param]p14: |
| 700 | // A template-parameter shall not be used in its own default argument. |
| 701 | // FIXME: Implement this check! Needs a recursive walk over the types. |
| 702 | |
Douglas Gregor | 9148c3f | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 703 | // Check only that we have a template template argument. We don't want to |
| 704 | // try to check well-formedness now, because our template template parameter |
| 705 | // might have dependent types in its template parameters, which we wouldn't |
| 706 | // be able to match now. |
| 707 | // |
| 708 | // If none of the template template parameter's template arguments mention |
| 709 | // other template parameters, we could actually perform more checking here. |
| 710 | // However, it isn't worth doing. |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 711 | TemplateArgumentLoc DefaultArg = translateTemplateArgument(*this, Default); |
Douglas Gregor | 9148c3f | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 712 | if (DefaultArg.getArgument().getAsTemplate().isNull()) { |
| 713 | Diag(DefaultArg.getLocation(), diag::err_template_arg_not_class_template) |
| 714 | << DefaultArg.getSourceRange(); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 715 | return; |
| 716 | } |
Douglas Gregor | 9148c3f | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 717 | |
Abramo Bagnara | d92f7a2 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 718 | TemplateParm->setDefaultArgument(DefaultArg, false); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 719 | } |
| 720 | |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 721 | /// ActOnTemplateParameterList - Builds a TemplateParameterList that |
| 722 | /// contains the template parameters in Params/NumParams. |
| 723 | Sema::TemplateParamsTy * |
| 724 | Sema::ActOnTemplateParameterList(unsigned Depth, |
| 725 | SourceLocation ExportLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 726 | SourceLocation TemplateLoc, |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 727 | SourceLocation LAngleLoc, |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 728 | DeclPtrTy *Params, unsigned NumParams, |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 729 | SourceLocation RAngleLoc) { |
| 730 | if (ExportLoc.isValid()) |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 731 | Diag(ExportLoc, diag::warn_template_export_unsupported); |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 732 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 733 | return TemplateParameterList::Create(Context, TemplateLoc, LAngleLoc, |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 734 | (NamedDecl**)Params, NumParams, |
| 735 | RAngleLoc); |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 736 | } |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 737 | |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 738 | static void SetNestedNameSpecifier(TagDecl *T, const CXXScopeSpec &SS) { |
| 739 | if (SS.isSet()) |
| 740 | T->setQualifierInfo(static_cast<NestedNameSpecifier*>(SS.getScopeRep()), |
| 741 | SS.getRange()); |
| 742 | } |
| 743 | |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 744 | Sema::DeclResult |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 745 | Sema::CheckClassTemplate(Scope *S, unsigned TagSpec, TagUseKind TUK, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 746 | SourceLocation KWLoc, CXXScopeSpec &SS, |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 747 | IdentifierInfo *Name, SourceLocation NameLoc, |
| 748 | AttributeList *Attr, |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 749 | TemplateParameterList *TemplateParams, |
Anders Carlsson | 5aeccdb | 2009-03-26 00:52:18 +0000 | [diff] [blame] | 750 | AccessSpecifier AS) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 751 | assert(TemplateParams && TemplateParams->size() > 0 && |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 752 | "No template parameters"); |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 753 | assert(TUK != TUK_Reference && "Can only declare or define class templates"); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 754 | bool Invalid = false; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 755 | |
| 756 | // Check that we can declare a template here. |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 757 | if (CheckTemplateDeclScope(S, TemplateParams)) |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 758 | return true; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 759 | |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 760 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
| 761 | assert(Kind != TTK_Enum && "can't build template of enumerated type"); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 762 | |
| 763 | // There is no such thing as an unnamed class template. |
| 764 | if (!Name) { |
| 765 | Diag(KWLoc, diag::err_template_unnamed_class); |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 766 | return true; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 767 | } |
| 768 | |
| 769 | // Find any previous declaration with this name. |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 770 | DeclContext *SemanticContext; |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 771 | LookupResult Previous(*this, Name, NameLoc, LookupOrdinaryName, |
John McCall | 7d384dd | 2009-11-18 07:57:50 +0000 | [diff] [blame] | 772 | ForRedeclaration); |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 773 | if (SS.isNotEmpty() && !SS.isInvalid()) { |
| 774 | SemanticContext = computeDeclContext(SS, true); |
| 775 | if (!SemanticContext) { |
| 776 | // FIXME: Produce a reasonable diagnostic here |
| 777 | return true; |
| 778 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 779 | |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 780 | if (RequireCompleteDeclContext(SS, SemanticContext)) |
| 781 | return true; |
| 782 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 783 | LookupQualifiedName(Previous, SemanticContext); |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 784 | } else { |
| 785 | SemanticContext = CurContext; |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 786 | LookupName(Previous, S); |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 787 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 788 | |
Douglas Gregor | 57265e3 | 2010-04-12 16:00:01 +0000 | [diff] [blame] | 789 | if (Previous.isAmbiguous()) |
| 790 | return true; |
| 791 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 792 | NamedDecl *PrevDecl = 0; |
| 793 | if (Previous.begin() != Previous.end()) |
Douglas Gregor | 57265e3 | 2010-04-12 16:00:01 +0000 | [diff] [blame] | 794 | PrevDecl = (*Previous.begin())->getUnderlyingDecl(); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 795 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 796 | // If there is a previous declaration with the same name, check |
| 797 | // whether this is a valid redeclaration. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 798 | ClassTemplateDecl *PrevClassTemplate |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 799 | = dyn_cast_or_null<ClassTemplateDecl>(PrevDecl); |
Douglas Gregor | d7e5bdb | 2009-10-09 21:11:42 +0000 | [diff] [blame] | 800 | |
| 801 | // We may have found the injected-class-name of a class template, |
| 802 | // class template partial specialization, or class template specialization. |
| 803 | // In these cases, grab the template that is being defined or specialized. |
| 804 | if (!PrevClassTemplate && PrevDecl && isa<CXXRecordDecl>(PrevDecl) && |
| 805 | cast<CXXRecordDecl>(PrevDecl)->isInjectedClassName()) { |
| 806 | PrevDecl = cast<CXXRecordDecl>(PrevDecl->getDeclContext()); |
| 807 | PrevClassTemplate |
| 808 | = cast<CXXRecordDecl>(PrevDecl)->getDescribedClassTemplate(); |
| 809 | if (!PrevClassTemplate && isa<ClassTemplateSpecializationDecl>(PrevDecl)) { |
| 810 | PrevClassTemplate |
| 811 | = cast<ClassTemplateSpecializationDecl>(PrevDecl) |
| 812 | ->getSpecializedTemplate(); |
| 813 | } |
| 814 | } |
| 815 | |
John McCall | 65c4946 | 2009-12-18 11:25:59 +0000 | [diff] [blame] | 816 | if (TUK == TUK_Friend) { |
John McCall | e129d44 | 2009-12-17 23:21:11 +0000 | [diff] [blame] | 817 | // C++ [namespace.memdef]p3: |
| 818 | // [...] When looking for a prior declaration of a class or a function |
| 819 | // declared as a friend, and when the name of the friend class or |
| 820 | // function is neither a qualified name nor a template-id, scopes outside |
| 821 | // the innermost enclosing namespace scope are not considered. |
Douglas Gregor | c1c9df7 | 2010-04-18 17:37:40 +0000 | [diff] [blame] | 822 | if (!SS.isSet()) { |
| 823 | DeclContext *OutermostContext = CurContext; |
| 824 | while (!OutermostContext->isFileContext()) |
| 825 | OutermostContext = OutermostContext->getLookupParent(); |
John McCall | 65c4946 | 2009-12-18 11:25:59 +0000 | [diff] [blame] | 826 | |
Douglas Gregor | c1c9df7 | 2010-04-18 17:37:40 +0000 | [diff] [blame] | 827 | if (PrevDecl && |
| 828 | (OutermostContext->Equals(PrevDecl->getDeclContext()) || |
| 829 | OutermostContext->Encloses(PrevDecl->getDeclContext()))) { |
| 830 | SemanticContext = PrevDecl->getDeclContext(); |
| 831 | } else { |
| 832 | // Declarations in outer scopes don't matter. However, the outermost |
| 833 | // context we computed is the semantic context for our new |
| 834 | // declaration. |
| 835 | PrevDecl = PrevClassTemplate = 0; |
| 836 | SemanticContext = OutermostContext; |
| 837 | } |
John McCall | e129d44 | 2009-12-17 23:21:11 +0000 | [diff] [blame] | 838 | } |
Douglas Gregor | c1c9df7 | 2010-04-18 17:37:40 +0000 | [diff] [blame] | 839 | |
John McCall | e129d44 | 2009-12-17 23:21:11 +0000 | [diff] [blame] | 840 | if (CurContext->isDependentContext()) { |
| 841 | // If this is a dependent context, we don't want to link the friend |
| 842 | // class template to the template in scope, because that would perform |
| 843 | // checking of the template parameter lists that can't be performed |
| 844 | // until the outer context is instantiated. |
| 845 | PrevDecl = PrevClassTemplate = 0; |
| 846 | } |
| 847 | } else if (PrevDecl && !isDeclInScope(PrevDecl, SemanticContext, S)) |
| 848 | PrevDecl = PrevClassTemplate = 0; |
Douglas Gregor | 57265e3 | 2010-04-12 16:00:01 +0000 | [diff] [blame] | 849 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 850 | if (PrevClassTemplate) { |
| 851 | // Ensure that the template parameter lists are compatible. |
| 852 | if (!TemplateParameterListsAreEqual(TemplateParams, |
| 853 | PrevClassTemplate->getTemplateParameters(), |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 854 | /*Complain=*/true, |
| 855 | TPL_TemplateMatch)) |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 856 | return true; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 857 | |
| 858 | // C++ [temp.class]p4: |
| 859 | // In a redeclaration, partial specialization, explicit |
| 860 | // specialization or explicit instantiation of a class template, |
| 861 | // the class-key shall agree in kind with the original class |
| 862 | // template declaration (7.1.5.3). |
| 863 | RecordDecl *PrevRecordDecl = PrevClassTemplate->getTemplatedDecl(); |
Douglas Gregor | 501c5ce | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 864 | if (!isAcceptableTagRedeclaration(PrevRecordDecl, Kind, KWLoc, *Name)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 865 | Diag(KWLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 866 | << Name |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 867 | << FixItHint::CreateReplacement(KWLoc, PrevRecordDecl->getKindName()); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 868 | Diag(PrevRecordDecl->getLocation(), diag::note_previous_use); |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 869 | Kind = PrevRecordDecl->getTagKind(); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 870 | } |
| 871 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 872 | // Check for redefinition of this class template. |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 873 | if (TUK == TUK_Definition) { |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 874 | if (TagDecl *Def = PrevRecordDecl->getDefinition()) { |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 875 | Diag(NameLoc, diag::err_redefinition) << Name; |
| 876 | Diag(Def->getLocation(), diag::note_previous_definition); |
| 877 | // FIXME: Would it make sense to try to "forget" the previous |
| 878 | // definition, as part of error recovery? |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 879 | return true; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 880 | } |
| 881 | } |
| 882 | } else if (PrevDecl && PrevDecl->isTemplateParameter()) { |
| 883 | // Maybe we will complain about the shadowed template parameter. |
| 884 | DiagnoseTemplateParameterShadow(NameLoc, PrevDecl); |
| 885 | // Just pretend that we didn't see the previous declaration. |
| 886 | PrevDecl = 0; |
| 887 | } else if (PrevDecl) { |
| 888 | // C++ [temp]p5: |
| 889 | // A class template shall not have the same name as any other |
| 890 | // template, class, function, object, enumeration, enumerator, |
| 891 | // namespace, or type in the same scope (3.3), except as specified |
| 892 | // in (14.5.4). |
| 893 | Diag(NameLoc, diag::err_redefinition_different_kind) << Name; |
| 894 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 895 | return true; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 896 | } |
| 897 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 898 | // Check the template parameter list of this declaration, possibly |
| 899 | // merging in the template parameter list from the previous class |
| 900 | // template declaration. |
| 901 | if (CheckTemplateParameterList(TemplateParams, |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 902 | PrevClassTemplate? PrevClassTemplate->getTemplateParameters() : 0, |
| 903 | TPC_ClassTemplate)) |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 904 | Invalid = true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 905 | |
Douglas Gregor | 57265e3 | 2010-04-12 16:00:01 +0000 | [diff] [blame] | 906 | if (SS.isSet()) { |
| 907 | // If the name of the template was qualified, we must be defining the |
| 908 | // template out-of-line. |
| 909 | if (!SS.isInvalid() && !Invalid && !PrevClassTemplate && |
| 910 | !(TUK == TUK_Friend && CurContext->isDependentContext())) |
| 911 | Diag(NameLoc, diag::err_member_def_does_not_match) |
| 912 | << Name << SemanticContext << SS.getRange(); |
| 913 | } |
| 914 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 915 | CXXRecordDecl *NewClass = |
Douglas Gregor | 741dd9a | 2009-07-21 14:46:17 +0000 | [diff] [blame] | 916 | CXXRecordDecl::Create(Context, Kind, SemanticContext, NameLoc, Name, KWLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 917 | PrevClassTemplate? |
Douglas Gregor | aafc0cc | 2009-05-15 19:11:46 +0000 | [diff] [blame] | 918 | PrevClassTemplate->getTemplatedDecl() : 0, |
| 919 | /*DelayTypeCreation=*/true); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 920 | SetNestedNameSpecifier(NewClass, SS); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 921 | |
| 922 | ClassTemplateDecl *NewTemplate |
| 923 | = ClassTemplateDecl::Create(Context, SemanticContext, NameLoc, |
| 924 | DeclarationName(Name), TemplateParams, |
Douglas Gregor | 5953d8b | 2009-03-19 17:26:29 +0000 | [diff] [blame] | 925 | NewClass, PrevClassTemplate); |
Douglas Gregor | befc20e | 2009-03-26 00:10:35 +0000 | [diff] [blame] | 926 | NewClass->setDescribedClassTemplate(NewTemplate); |
| 927 | |
Douglas Gregor | aafc0cc | 2009-05-15 19:11:46 +0000 | [diff] [blame] | 928 | // Build the type for the class template declaration now. |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 929 | QualType T = NewTemplate->getInjectedClassNameSpecialization(Context); |
| 930 | T = Context.getInjectedClassNameType(NewClass, T); |
Douglas Gregor | aafc0cc | 2009-05-15 19:11:46 +0000 | [diff] [blame] | 931 | assert(T->isDependentType() && "Class template type is not dependent?"); |
| 932 | (void)T; |
| 933 | |
Douglas Gregor | fd056bc | 2009-10-13 16:30:37 +0000 | [diff] [blame] | 934 | // If we are providing an explicit specialization of a member that is a |
| 935 | // class template, make a note of that. |
| 936 | if (PrevClassTemplate && |
| 937 | PrevClassTemplate->getInstantiatedFromMemberTemplate()) |
| 938 | PrevClassTemplate->setMemberSpecialization(); |
| 939 | |
Anders Carlsson | 4cbe82c | 2009-03-26 01:24:28 +0000 | [diff] [blame] | 940 | // Set the access specifier. |
Douglas Gregor | d85bea2 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 941 | if (!Invalid && TUK != TUK_Friend) |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 942 | SetMemberAccessSpecifier(NewTemplate, PrevClassTemplate, AS); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 943 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 944 | // Set the lexical context of these templates |
| 945 | NewClass->setLexicalDeclContext(CurContext); |
| 946 | NewTemplate->setLexicalDeclContext(CurContext); |
| 947 | |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 948 | if (TUK == TUK_Definition) |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 949 | NewClass->startDefinition(); |
| 950 | |
| 951 | if (Attr) |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 952 | ProcessDeclAttributeList(S, NewClass, Attr); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 953 | |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 954 | if (TUK != TUK_Friend) |
| 955 | PushOnScopeChains(NewTemplate, S); |
| 956 | else { |
Douglas Gregor | d85bea2 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 957 | if (PrevClassTemplate && PrevClassTemplate->getAccess() != AS_none) { |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 958 | NewTemplate->setAccess(PrevClassTemplate->getAccess()); |
Douglas Gregor | d85bea2 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 959 | NewClass->setAccess(PrevClassTemplate->getAccess()); |
| 960 | } |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 961 | |
Douglas Gregor | d85bea2 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 962 | NewTemplate->setObjectOfFriendDecl(/* PreviouslyDeclared = */ |
| 963 | PrevClassTemplate != NULL); |
| 964 | |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 965 | // Friend templates are visible in fairly strange ways. |
| 966 | if (!CurContext->isDependentContext()) { |
| 967 | DeclContext *DC = SemanticContext->getLookupContext(); |
| 968 | DC->makeDeclVisibleInContext(NewTemplate, /* Recoverable = */ false); |
| 969 | if (Scope *EnclosingScope = getScopeForDeclContext(S, DC)) |
| 970 | PushOnScopeChains(NewTemplate, EnclosingScope, |
| 971 | /* AddToContext = */ false); |
| 972 | } |
Douglas Gregor | d85bea2 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 973 | |
| 974 | FriendDecl *Friend = FriendDecl::Create(Context, CurContext, |
| 975 | NewClass->getLocation(), |
| 976 | NewTemplate, |
| 977 | /*FIXME:*/NewClass->getLocation()); |
| 978 | Friend->setAccess(AS_public); |
| 979 | CurContext->addDecl(Friend); |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 980 | } |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 981 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 982 | if (Invalid) { |
| 983 | NewTemplate->setInvalidDecl(); |
| 984 | NewClass->setInvalidDecl(); |
| 985 | } |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 986 | return DeclPtrTy::make(NewTemplate); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 987 | } |
| 988 | |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 989 | /// \brief Diagnose the presence of a default template argument on a |
| 990 | /// template parameter, which is ill-formed in certain contexts. |
| 991 | /// |
| 992 | /// \returns true if the default template argument should be dropped. |
| 993 | static bool DiagnoseDefaultTemplateArgument(Sema &S, |
| 994 | Sema::TemplateParamListContext TPC, |
| 995 | SourceLocation ParamLoc, |
| 996 | SourceRange DefArgRange) { |
| 997 | switch (TPC) { |
| 998 | case Sema::TPC_ClassTemplate: |
| 999 | return false; |
| 1000 | |
| 1001 | case Sema::TPC_FunctionTemplate: |
| 1002 | // C++ [temp.param]p9: |
| 1003 | // A default template-argument shall not be specified in a |
| 1004 | // function template declaration or a function template |
| 1005 | // definition [...] |
| 1006 | // (This sentence is not in C++0x, per DR226). |
| 1007 | if (!S.getLangOptions().CPlusPlus0x) |
| 1008 | S.Diag(ParamLoc, |
| 1009 | diag::err_template_parameter_default_in_function_template) |
| 1010 | << DefArgRange; |
| 1011 | return false; |
| 1012 | |
| 1013 | case Sema::TPC_ClassTemplateMember: |
| 1014 | // C++0x [temp.param]p9: |
| 1015 | // A default template-argument shall not be specified in the |
| 1016 | // template-parameter-lists of the definition of a member of a |
| 1017 | // class template that appears outside of the member's class. |
| 1018 | S.Diag(ParamLoc, diag::err_template_parameter_default_template_member) |
| 1019 | << DefArgRange; |
| 1020 | return true; |
| 1021 | |
| 1022 | case Sema::TPC_FriendFunctionTemplate: |
| 1023 | // C++ [temp.param]p9: |
| 1024 | // A default template-argument shall not be specified in a |
| 1025 | // friend template declaration. |
| 1026 | S.Diag(ParamLoc, diag::err_template_parameter_default_friend_template) |
| 1027 | << DefArgRange; |
| 1028 | return true; |
| 1029 | |
| 1030 | // FIXME: C++0x [temp.param]p9 allows default template-arguments |
| 1031 | // for friend function templates if there is only a single |
| 1032 | // declaration (and it is a definition). Strange! |
| 1033 | } |
| 1034 | |
| 1035 | return false; |
| 1036 | } |
| 1037 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1038 | /// \brief Checks the validity of a template parameter list, possibly |
| 1039 | /// considering the template parameter list from a previous |
| 1040 | /// declaration. |
| 1041 | /// |
| 1042 | /// If an "old" template parameter list is provided, it must be |
| 1043 | /// equivalent (per TemplateParameterListsAreEqual) to the "new" |
| 1044 | /// template parameter list. |
| 1045 | /// |
| 1046 | /// \param NewParams Template parameter list for a new template |
| 1047 | /// declaration. This template parameter list will be updated with any |
| 1048 | /// default arguments that are carried through from the previous |
| 1049 | /// template parameter list. |
| 1050 | /// |
| 1051 | /// \param OldParams If provided, template parameter list from a |
| 1052 | /// previous declaration of the same template. Default template |
| 1053 | /// arguments will be merged from the old template parameter list to |
| 1054 | /// the new template parameter list. |
| 1055 | /// |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1056 | /// \param TPC Describes the context in which we are checking the given |
| 1057 | /// template parameter list. |
| 1058 | /// |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1059 | /// \returns true if an error occurred, false otherwise. |
| 1060 | bool Sema::CheckTemplateParameterList(TemplateParameterList *NewParams, |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1061 | TemplateParameterList *OldParams, |
| 1062 | TemplateParamListContext TPC) { |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1063 | bool Invalid = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1064 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1065 | // C++ [temp.param]p10: |
| 1066 | // The set of default template-arguments available for use with a |
| 1067 | // template declaration or definition is obtained by merging the |
| 1068 | // default arguments from the definition (if in scope) and all |
| 1069 | // declarations in scope in the same way default function |
| 1070 | // arguments are (8.3.6). |
| 1071 | bool SawDefaultArgument = false; |
| 1072 | SourceLocation PreviousDefaultArgLoc; |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 1073 | |
Anders Carlsson | 49d2557 | 2009-06-12 23:20:15 +0000 | [diff] [blame] | 1074 | bool SawParameterPack = false; |
| 1075 | SourceLocation ParameterPackLoc; |
| 1076 | |
Mike Stump | 1a35fde | 2009-02-11 23:03:27 +0000 | [diff] [blame] | 1077 | // Dummy initialization to avoid warnings. |
Douglas Gregor | 1bc6913 | 2009-02-11 20:46:19 +0000 | [diff] [blame] | 1078 | TemplateParameterList::iterator OldParam = NewParams->end(); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1079 | if (OldParams) |
| 1080 | OldParam = OldParams->begin(); |
| 1081 | |
| 1082 | for (TemplateParameterList::iterator NewParam = NewParams->begin(), |
| 1083 | NewParamEnd = NewParams->end(); |
| 1084 | NewParam != NewParamEnd; ++NewParam) { |
| 1085 | // Variables used to diagnose redundant default arguments |
| 1086 | bool RedundantDefaultArg = false; |
| 1087 | SourceLocation OldDefaultLoc; |
| 1088 | SourceLocation NewDefaultLoc; |
| 1089 | |
| 1090 | // Variables used to diagnose missing default arguments |
| 1091 | bool MissingDefaultArg = false; |
| 1092 | |
Anders Carlsson | 49d2557 | 2009-06-12 23:20:15 +0000 | [diff] [blame] | 1093 | // C++0x [temp.param]p11: |
| 1094 | // If a template parameter of a class template is a template parameter pack, |
| 1095 | // it must be the last template parameter. |
| 1096 | if (SawParameterPack) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1097 | Diag(ParameterPackLoc, |
Anders Carlsson | 49d2557 | 2009-06-12 23:20:15 +0000 | [diff] [blame] | 1098 | diag::err_template_param_pack_must_be_last_template_parameter); |
| 1099 | Invalid = true; |
| 1100 | } |
| 1101 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1102 | if (TemplateTypeParmDecl *NewTypeParm |
| 1103 | = dyn_cast<TemplateTypeParmDecl>(*NewParam)) { |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1104 | // Check the presence of a default argument here. |
| 1105 | if (NewTypeParm->hasDefaultArgument() && |
| 1106 | DiagnoseDefaultTemplateArgument(*this, TPC, |
| 1107 | NewTypeParm->getLocation(), |
| 1108 | NewTypeParm->getDefaultArgumentInfo()->getTypeLoc() |
Abramo Bagnara | bd054db | 2010-05-20 10:00:11 +0000 | [diff] [blame] | 1109 | .getSourceRange())) |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1110 | NewTypeParm->removeDefaultArgument(); |
| 1111 | |
| 1112 | // Merge default arguments for template type parameters. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1113 | TemplateTypeParmDecl *OldTypeParm |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1114 | = OldParams? cast<TemplateTypeParmDecl>(*OldParam) : 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1115 | |
Anders Carlsson | 49d2557 | 2009-06-12 23:20:15 +0000 | [diff] [blame] | 1116 | if (NewTypeParm->isParameterPack()) { |
| 1117 | assert(!NewTypeParm->hasDefaultArgument() && |
| 1118 | "Parameter packs can't have a default argument!"); |
| 1119 | SawParameterPack = true; |
| 1120 | ParameterPackLoc = NewTypeParm->getLocation(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1121 | } else if (OldTypeParm && OldTypeParm->hasDefaultArgument() && |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1122 | NewTypeParm->hasDefaultArgument()) { |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1123 | OldDefaultLoc = OldTypeParm->getDefaultArgumentLoc(); |
| 1124 | NewDefaultLoc = NewTypeParm->getDefaultArgumentLoc(); |
| 1125 | SawDefaultArgument = true; |
| 1126 | RedundantDefaultArg = true; |
| 1127 | PreviousDefaultArgLoc = NewDefaultLoc; |
| 1128 | } else if (OldTypeParm && OldTypeParm->hasDefaultArgument()) { |
| 1129 | // Merge the default argument from the old declaration to the |
| 1130 | // new declaration. |
| 1131 | SawDefaultArgument = true; |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1132 | NewTypeParm->setDefaultArgument(OldTypeParm->getDefaultArgumentInfo(), |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1133 | true); |
| 1134 | PreviousDefaultArgLoc = OldTypeParm->getDefaultArgumentLoc(); |
| 1135 | } else if (NewTypeParm->hasDefaultArgument()) { |
| 1136 | SawDefaultArgument = true; |
| 1137 | PreviousDefaultArgLoc = NewTypeParm->getDefaultArgumentLoc(); |
| 1138 | } else if (SawDefaultArgument) |
| 1139 | MissingDefaultArg = true; |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1140 | } else if (NonTypeTemplateParmDecl *NewNonTypeParm |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1141 | = dyn_cast<NonTypeTemplateParmDecl>(*NewParam)) { |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1142 | // Check the presence of a default argument here. |
| 1143 | if (NewNonTypeParm->hasDefaultArgument() && |
| 1144 | DiagnoseDefaultTemplateArgument(*this, TPC, |
| 1145 | NewNonTypeParm->getLocation(), |
| 1146 | NewNonTypeParm->getDefaultArgument()->getSourceRange())) { |
| 1147 | NewNonTypeParm->getDefaultArgument()->Destroy(Context); |
Abramo Bagnara | d92f7a2 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 1148 | NewNonTypeParm->removeDefaultArgument(); |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1149 | } |
| 1150 | |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1151 | // Merge default arguments for non-type template parameters |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1152 | NonTypeTemplateParmDecl *OldNonTypeParm |
| 1153 | = OldParams? cast<NonTypeTemplateParmDecl>(*OldParam) : 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1154 | if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument() && |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1155 | NewNonTypeParm->hasDefaultArgument()) { |
| 1156 | OldDefaultLoc = OldNonTypeParm->getDefaultArgumentLoc(); |
| 1157 | NewDefaultLoc = NewNonTypeParm->getDefaultArgumentLoc(); |
| 1158 | SawDefaultArgument = true; |
| 1159 | RedundantDefaultArg = true; |
| 1160 | PreviousDefaultArgLoc = NewDefaultLoc; |
| 1161 | } else if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument()) { |
| 1162 | // Merge the default argument from the old declaration to the |
| 1163 | // new declaration. |
| 1164 | SawDefaultArgument = true; |
| 1165 | // FIXME: We need to create a new kind of "default argument" |
| 1166 | // expression that points to a previous template template |
| 1167 | // parameter. |
| 1168 | NewNonTypeParm->setDefaultArgument( |
Abramo Bagnara | d92f7a2 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 1169 | OldNonTypeParm->getDefaultArgument(), |
| 1170 | /*Inherited=*/ true); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1171 | PreviousDefaultArgLoc = OldNonTypeParm->getDefaultArgumentLoc(); |
| 1172 | } else if (NewNonTypeParm->hasDefaultArgument()) { |
| 1173 | SawDefaultArgument = true; |
| 1174 | PreviousDefaultArgLoc = NewNonTypeParm->getDefaultArgumentLoc(); |
| 1175 | } else if (SawDefaultArgument) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1176 | MissingDefaultArg = true; |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1177 | } else { |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1178 | // Check the presence of a default argument here. |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1179 | TemplateTemplateParmDecl *NewTemplateParm |
| 1180 | = cast<TemplateTemplateParmDecl>(*NewParam); |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1181 | if (NewTemplateParm->hasDefaultArgument() && |
| 1182 | DiagnoseDefaultTemplateArgument(*this, TPC, |
| 1183 | NewTemplateParm->getLocation(), |
| 1184 | NewTemplateParm->getDefaultArgument().getSourceRange())) |
Abramo Bagnara | d92f7a2 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 1185 | NewTemplateParm->removeDefaultArgument(); |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1186 | |
| 1187 | // Merge default arguments for template template parameters |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1188 | TemplateTemplateParmDecl *OldTemplateParm |
| 1189 | = OldParams? cast<TemplateTemplateParmDecl>(*OldParam) : 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1190 | if (OldTemplateParm && OldTemplateParm->hasDefaultArgument() && |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1191 | NewTemplateParm->hasDefaultArgument()) { |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1192 | OldDefaultLoc = OldTemplateParm->getDefaultArgument().getLocation(); |
| 1193 | NewDefaultLoc = NewTemplateParm->getDefaultArgument().getLocation(); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1194 | SawDefaultArgument = true; |
| 1195 | RedundantDefaultArg = true; |
| 1196 | PreviousDefaultArgLoc = NewDefaultLoc; |
| 1197 | } else if (OldTemplateParm && OldTemplateParm->hasDefaultArgument()) { |
| 1198 | // Merge the default argument from the old declaration to the |
| 1199 | // new declaration. |
| 1200 | SawDefaultArgument = true; |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 1201 | // FIXME: We need to create a new kind of "default argument" expression |
| 1202 | // that points to a previous template template parameter. |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1203 | NewTemplateParm->setDefaultArgument( |
Abramo Bagnara | d92f7a2 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 1204 | OldTemplateParm->getDefaultArgument(), |
| 1205 | /*Inherited=*/ true); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1206 | PreviousDefaultArgLoc |
| 1207 | = OldTemplateParm->getDefaultArgument().getLocation(); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1208 | } else if (NewTemplateParm->hasDefaultArgument()) { |
| 1209 | SawDefaultArgument = true; |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1210 | PreviousDefaultArgLoc |
| 1211 | = NewTemplateParm->getDefaultArgument().getLocation(); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1212 | } else if (SawDefaultArgument) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1213 | MissingDefaultArg = true; |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1214 | } |
| 1215 | |
| 1216 | if (RedundantDefaultArg) { |
| 1217 | // C++ [temp.param]p12: |
| 1218 | // A template-parameter shall not be given default arguments |
| 1219 | // by two different declarations in the same scope. |
| 1220 | Diag(NewDefaultLoc, diag::err_template_param_default_arg_redefinition); |
| 1221 | Diag(OldDefaultLoc, diag::note_template_param_prev_default_arg); |
| 1222 | Invalid = true; |
| 1223 | } else if (MissingDefaultArg) { |
| 1224 | // C++ [temp.param]p11: |
| 1225 | // If a template-parameter has a default template-argument, |
| 1226 | // all subsequent template-parameters shall have a default |
| 1227 | // template-argument supplied. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1228 | Diag((*NewParam)->getLocation(), |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1229 | diag::err_template_param_default_arg_missing); |
| 1230 | Diag(PreviousDefaultArgLoc, diag::note_template_param_prev_default_arg); |
| 1231 | Invalid = true; |
| 1232 | } |
| 1233 | |
| 1234 | // If we have an old template parameter list that we're merging |
| 1235 | // in, move on to the next parameter. |
| 1236 | if (OldParams) |
| 1237 | ++OldParam; |
| 1238 | } |
| 1239 | |
| 1240 | return Invalid; |
| 1241 | } |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 1242 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1243 | /// \brief Match the given template parameter lists to the given scope |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1244 | /// specifier, returning the template parameter list that applies to the |
| 1245 | /// name. |
| 1246 | /// |
| 1247 | /// \param DeclStartLoc the start of the declaration that has a scope |
| 1248 | /// specifier or a template parameter list. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1249 | /// |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1250 | /// \param SS the scope specifier that will be matched to the given template |
| 1251 | /// parameter lists. This scope specifier precedes a qualified name that is |
| 1252 | /// being declared. |
| 1253 | /// |
| 1254 | /// \param ParamLists the template parameter lists, from the outermost to the |
| 1255 | /// innermost template parameter lists. |
| 1256 | /// |
| 1257 | /// \param NumParamLists the number of template parameter lists in ParamLists. |
| 1258 | /// |
John McCall | 77e8b11 | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 1259 | /// \param IsFriend Whether to apply the slightly different rules for |
| 1260 | /// matching template parameters to scope specifiers in friend |
| 1261 | /// declarations. |
| 1262 | /// |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 1263 | /// \param IsExplicitSpecialization will be set true if the entity being |
| 1264 | /// declared is an explicit specialization, false otherwise. |
| 1265 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1266 | /// \returns the template parameter list, if any, that corresponds to the |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1267 | /// name that is preceded by the scope specifier @p SS. This template |
| 1268 | /// parameter list may be have template parameters (if we're declaring a |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1269 | /// template) or may have no template parameters (if we're declaring a |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1270 | /// template specialization), or may be NULL (if we were's declaring isn't |
| 1271 | /// itself a template). |
| 1272 | TemplateParameterList * |
| 1273 | Sema::MatchTemplateParametersToScopeSpecifier(SourceLocation DeclStartLoc, |
| 1274 | const CXXScopeSpec &SS, |
| 1275 | TemplateParameterList **ParamLists, |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 1276 | unsigned NumParamLists, |
John McCall | 77e8b11 | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 1277 | bool IsFriend, |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 1278 | bool &IsExplicitSpecialization) { |
| 1279 | IsExplicitSpecialization = false; |
| 1280 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1281 | // Find the template-ids that occur within the nested-name-specifier. These |
| 1282 | // template-ids will match up with the template parameter lists. |
| 1283 | llvm::SmallVector<const TemplateSpecializationType *, 4> |
| 1284 | TemplateIdsInSpecifier; |
Douglas Gregor | 3ebd753 | 2009-11-23 12:11:45 +0000 | [diff] [blame] | 1285 | llvm::SmallVector<ClassTemplateSpecializationDecl *, 4> |
| 1286 | ExplicitSpecializationsInSpecifier; |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1287 | for (NestedNameSpecifier *NNS = (NestedNameSpecifier *)SS.getScopeRep(); |
| 1288 | NNS; NNS = NNS->getPrefix()) { |
John McCall | 4b2b02b | 2009-12-15 02:19:47 +0000 | [diff] [blame] | 1289 | const Type *T = NNS->getAsType(); |
| 1290 | if (!T) break; |
| 1291 | |
| 1292 | // C++0x [temp.expl.spec]p17: |
| 1293 | // A member or a member template may be nested within many |
| 1294 | // enclosing class templates. In an explicit specialization for |
| 1295 | // such a member, the member declaration shall be preceded by a |
| 1296 | // template<> for each enclosing class template that is |
| 1297 | // explicitly specialized. |
Douglas Gregor | fe33106 | 2010-02-13 05:23:25 +0000 | [diff] [blame] | 1298 | // |
| 1299 | // Following the existing practice of GNU and EDG, we allow a typedef of a |
| 1300 | // template specialization type. |
| 1301 | if (const TypedefType *TT = dyn_cast<TypedefType>(T)) |
| 1302 | T = TT->LookThroughTypedefs().getTypePtr(); |
John McCall | 4b2b02b | 2009-12-15 02:19:47 +0000 | [diff] [blame] | 1303 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1304 | if (const TemplateSpecializationType *SpecType |
Douglas Gregor | fe33106 | 2010-02-13 05:23:25 +0000 | [diff] [blame] | 1305 | = dyn_cast<TemplateSpecializationType>(T)) { |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1306 | TemplateDecl *Template = SpecType->getTemplateName().getAsTemplateDecl(); |
| 1307 | if (!Template) |
| 1308 | continue; // FIXME: should this be an error? probably... |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1309 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1310 | if (const RecordType *Record = SpecType->getAs<RecordType>()) { |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1311 | ClassTemplateSpecializationDecl *SpecDecl |
| 1312 | = cast<ClassTemplateSpecializationDecl>(Record->getDecl()); |
| 1313 | // If the nested name specifier refers to an explicit specialization, |
| 1314 | // we don't need a template<> header. |
Douglas Gregor | 3ebd753 | 2009-11-23 12:11:45 +0000 | [diff] [blame] | 1315 | if (SpecDecl->getSpecializationKind() == TSK_ExplicitSpecialization) { |
| 1316 | ExplicitSpecializationsInSpecifier.push_back(SpecDecl); |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1317 | continue; |
Douglas Gregor | 3ebd753 | 2009-11-23 12:11:45 +0000 | [diff] [blame] | 1318 | } |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1319 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1320 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1321 | TemplateIdsInSpecifier.push_back(SpecType); |
| 1322 | } |
| 1323 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1324 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1325 | // Reverse the list of template-ids in the scope specifier, so that we can |
| 1326 | // more easily match up the template-ids and the template parameter lists. |
| 1327 | std::reverse(TemplateIdsInSpecifier.begin(), TemplateIdsInSpecifier.end()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1328 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1329 | SourceLocation FirstTemplateLoc = DeclStartLoc; |
| 1330 | if (NumParamLists) |
| 1331 | FirstTemplateLoc = ParamLists[0]->getTemplateLoc(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1332 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1333 | // Match the template-ids found in the specifier to the template parameter |
| 1334 | // lists. |
| 1335 | unsigned Idx = 0; |
| 1336 | for (unsigned NumTemplateIds = TemplateIdsInSpecifier.size(); |
| 1337 | Idx != NumTemplateIds; ++Idx) { |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 1338 | QualType TemplateId = QualType(TemplateIdsInSpecifier[Idx], 0); |
| 1339 | bool DependentTemplateId = TemplateId->isDependentType(); |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1340 | if (Idx >= NumParamLists) { |
| 1341 | // We have a template-id without a corresponding template parameter |
| 1342 | // list. |
John McCall | 77e8b11 | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 1343 | |
| 1344 | // ...which is fine if this is a friend declaration. |
| 1345 | if (IsFriend) { |
| 1346 | IsExplicitSpecialization = true; |
| 1347 | break; |
| 1348 | } |
| 1349 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1350 | if (DependentTemplateId) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1351 | // FIXME: the location information here isn't great. |
| 1352 | Diag(SS.getRange().getBegin(), |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1353 | diag::err_template_spec_needs_template_parameters) |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 1354 | << TemplateId |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1355 | << SS.getRange(); |
| 1356 | } else { |
| 1357 | Diag(SS.getRange().getBegin(), diag::err_template_spec_needs_header) |
| 1358 | << SS.getRange() |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 1359 | << FixItHint::CreateInsertion(FirstTemplateLoc, "template<> "); |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 1360 | IsExplicitSpecialization = true; |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1361 | } |
| 1362 | return 0; |
| 1363 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1364 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1365 | // Check the template parameter list against its corresponding template-id. |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 1366 | if (DependentTemplateId) { |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 1367 | TemplateParameterList *ExpectedTemplateParams = 0; |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 1368 | |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 1369 | // Are there cases in (e.g.) friends where this won't match? |
| 1370 | if (const InjectedClassNameType *Injected |
| 1371 | = TemplateId->getAs<InjectedClassNameType>()) { |
| 1372 | CXXRecordDecl *Record = Injected->getDecl(); |
| 1373 | if (ClassTemplatePartialSpecializationDecl *Partial = |
| 1374 | dyn_cast<ClassTemplatePartialSpecializationDecl>(Record)) |
| 1375 | ExpectedTemplateParams = Partial->getTemplateParameters(); |
| 1376 | else |
| 1377 | ExpectedTemplateParams = Record->getDescribedClassTemplate() |
| 1378 | ->getTemplateParameters(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1379 | } |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1380 | |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 1381 | if (ExpectedTemplateParams) |
| 1382 | TemplateParameterListsAreEqual(ParamLists[Idx], |
| 1383 | ExpectedTemplateParams, |
| 1384 | true, TPL_TemplateMatch); |
| 1385 | |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1386 | CheckTemplateParameterList(ParamLists[Idx], 0, TPC_ClassTemplateMember); |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 1387 | } else if (ParamLists[Idx]->size() > 0) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1388 | Diag(ParamLists[Idx]->getTemplateLoc(), |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 1389 | diag::err_template_param_list_matches_nontemplate) |
| 1390 | << TemplateId |
| 1391 | << ParamLists[Idx]->getSourceRange(); |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 1392 | else |
| 1393 | IsExplicitSpecialization = true; |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1394 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1395 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1396 | // If there were at least as many template-ids as there were template |
| 1397 | // parameter lists, then there are no template parameter lists remaining for |
| 1398 | // the declaration itself. |
| 1399 | if (Idx >= NumParamLists) |
| 1400 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1401 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1402 | // If there were too many template parameter lists, complain about that now. |
| 1403 | if (Idx != NumParamLists - 1) { |
| 1404 | while (Idx < NumParamLists - 1) { |
Douglas Gregor | 3ebd753 | 2009-11-23 12:11:45 +0000 | [diff] [blame] | 1405 | bool isExplicitSpecHeader = ParamLists[Idx]->size() == 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1406 | Diag(ParamLists[Idx]->getTemplateLoc(), |
Douglas Gregor | 3ebd753 | 2009-11-23 12:11:45 +0000 | [diff] [blame] | 1407 | isExplicitSpecHeader? diag::warn_template_spec_extra_headers |
| 1408 | : diag::err_template_spec_extra_headers) |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1409 | << SourceRange(ParamLists[Idx]->getTemplateLoc(), |
| 1410 | ParamLists[Idx]->getRAngleLoc()); |
Douglas Gregor | 3ebd753 | 2009-11-23 12:11:45 +0000 | [diff] [blame] | 1411 | |
| 1412 | if (isExplicitSpecHeader && !ExplicitSpecializationsInSpecifier.empty()) { |
| 1413 | Diag(ExplicitSpecializationsInSpecifier.back()->getLocation(), |
| 1414 | diag::note_explicit_template_spec_does_not_need_header) |
| 1415 | << ExplicitSpecializationsInSpecifier.back(); |
| 1416 | ExplicitSpecializationsInSpecifier.pop_back(); |
| 1417 | } |
| 1418 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1419 | ++Idx; |
| 1420 | } |
| 1421 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1422 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1423 | // Return the last template parameter list, which corresponds to the |
| 1424 | // entity being declared. |
| 1425 | return ParamLists[NumParamLists - 1]; |
| 1426 | } |
| 1427 | |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1428 | QualType Sema::CheckTemplateIdType(TemplateName Name, |
| 1429 | SourceLocation TemplateLoc, |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1430 | const TemplateArgumentListInfo &TemplateArgs) { |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1431 | TemplateDecl *Template = Name.getAsTemplateDecl(); |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 1432 | if (!Template) { |
| 1433 | // The template name does not resolve to a template, so we just |
| 1434 | // build a dependent template-id type. |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1435 | return Context.getTemplateSpecializationType(Name, TemplateArgs); |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 1436 | } |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1437 | |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1438 | // Check that the template argument list is well-formed for this |
| 1439 | // template. |
Anders Carlsson | fb25052 | 2009-06-23 01:26:57 +0000 | [diff] [blame] | 1440 | TemplateArgumentListBuilder Converted(Template->getTemplateParameters(), |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1441 | TemplateArgs.size()); |
| 1442 | if (CheckTemplateArgumentList(Template, TemplateLoc, TemplateArgs, |
Douglas Gregor | 16134c6 | 2009-07-01 00:28:38 +0000 | [diff] [blame] | 1443 | false, Converted)) |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1444 | return QualType(); |
| 1445 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1446 | assert((Converted.structuredSize() == |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1447 | Template->getTemplateParameters()->size()) && |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1448 | "Converted template argument list is too short!"); |
| 1449 | |
| 1450 | QualType CanonType; |
| 1451 | |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 1452 | if (Name.isDependent() || |
| 1453 | TemplateSpecializationType::anyDependentTemplateArguments( |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1454 | TemplateArgs)) { |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1455 | // This class template specialization is a dependent |
| 1456 | // type. Therefore, its canonical type is another class template |
| 1457 | // specialization type that contains all of the converted |
| 1458 | // arguments in canonical form. This ensures that, e.g., A<T> and |
| 1459 | // A<T, T> have identical types when A is declared as: |
| 1460 | // |
| 1461 | // template<typename T, typename U = T> struct A; |
Douglas Gregor | 25a3ef7 | 2009-05-07 06:41:52 +0000 | [diff] [blame] | 1462 | TemplateName CanonName = Context.getCanonicalTemplateName(Name); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1463 | CanonType = Context.getTemplateSpecializationType(CanonName, |
Anders Carlsson | fb25052 | 2009-06-23 01:26:57 +0000 | [diff] [blame] | 1464 | Converted.getFlatArguments(), |
| 1465 | Converted.flatSize()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1466 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 1467 | // FIXME: CanonType is not actually the canonical type, and unfortunately |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1468 | // it is a TemplateSpecializationType that we will never use again. |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 1469 | // In the future, we need to teach getTemplateSpecializationType to only |
| 1470 | // build the canonical type and return that to us. |
| 1471 | CanonType = Context.getCanonicalType(CanonType); |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 1472 | |
| 1473 | // This might work out to be a current instantiation, in which |
| 1474 | // case the canonical type needs to be the InjectedClassNameType. |
| 1475 | // |
| 1476 | // TODO: in theory this could be a simple hashtable lookup; most |
| 1477 | // changes to CurContext don't change the set of current |
| 1478 | // instantiations. |
| 1479 | if (isa<ClassTemplateDecl>(Template)) { |
| 1480 | for (DeclContext *Ctx = CurContext; Ctx; Ctx = Ctx->getLookupParent()) { |
| 1481 | // If we get out to a namespace, we're done. |
| 1482 | if (Ctx->isFileContext()) break; |
| 1483 | |
| 1484 | // If this isn't a record, keep looking. |
| 1485 | CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Ctx); |
| 1486 | if (!Record) continue; |
| 1487 | |
| 1488 | // Look for one of the two cases with InjectedClassNameTypes |
| 1489 | // and check whether it's the same template. |
| 1490 | if (!isa<ClassTemplatePartialSpecializationDecl>(Record) && |
| 1491 | !Record->getDescribedClassTemplate()) |
| 1492 | continue; |
| 1493 | |
| 1494 | // Fetch the injected class name type and check whether its |
| 1495 | // injected type is equal to the type we just built. |
| 1496 | QualType ICNT = Context.getTypeDeclType(Record); |
| 1497 | QualType Injected = cast<InjectedClassNameType>(ICNT) |
| 1498 | ->getInjectedSpecializationType(); |
| 1499 | |
| 1500 | if (CanonType != Injected->getCanonicalTypeInternal()) |
| 1501 | continue; |
| 1502 | |
| 1503 | // If so, the canonical type of this TST is the injected |
| 1504 | // class name type of the record we just found. |
| 1505 | assert(ICNT.isCanonical()); |
| 1506 | CanonType = ICNT; |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 1507 | break; |
| 1508 | } |
| 1509 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1510 | } else if (ClassTemplateDecl *ClassTemplate |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1511 | = dyn_cast<ClassTemplateDecl>(Template)) { |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1512 | // Find the class template specialization declaration that |
| 1513 | // corresponds to these arguments. |
| 1514 | llvm::FoldingSetNodeID ID; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1515 | ClassTemplateSpecializationDecl::Profile(ID, |
Anders Carlsson | fb25052 | 2009-06-23 01:26:57 +0000 | [diff] [blame] | 1516 | Converted.getFlatArguments(), |
Douglas Gregor | 828e226 | 2009-07-29 16:09:57 +0000 | [diff] [blame] | 1517 | Converted.flatSize(), |
| 1518 | Context); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1519 | void *InsertPos = 0; |
| 1520 | ClassTemplateSpecializationDecl *Decl |
| 1521 | = ClassTemplate->getSpecializations().FindNodeOrInsertPos(ID, InsertPos); |
| 1522 | if (!Decl) { |
| 1523 | // This is the first time we have referenced this class template |
| 1524 | // specialization. Create the canonical declaration and add it to |
| 1525 | // the set of specializations. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1526 | Decl = ClassTemplateSpecializationDecl::Create(Context, |
Douglas Gregor | 13c8577 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 1527 | ClassTemplate->getTemplatedDecl()->getTagKind(), |
| 1528 | ClassTemplate->getDeclContext(), |
| 1529 | ClassTemplate->getLocation(), |
| 1530 | ClassTemplate, |
| 1531 | Converted, 0); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1532 | ClassTemplate->getSpecializations().InsertNode(Decl, InsertPos); |
| 1533 | Decl->setLexicalDeclContext(CurContext); |
| 1534 | } |
| 1535 | |
| 1536 | CanonType = Context.getTypeDeclType(Decl); |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 1537 | assert(isa<RecordType>(CanonType) && |
| 1538 | "type of non-dependent specialization is not a RecordType"); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1539 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1540 | |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1541 | // Build the fully-sugared type for this class template |
| 1542 | // specialization, which refers back to the class template |
| 1543 | // specialization we created or found. |
John McCall | 71d74bc | 2010-06-13 09:25:03 +0000 | [diff] [blame] | 1544 | return Context.getTemplateSpecializationType(Name, TemplateArgs, CanonType); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1545 | } |
| 1546 | |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 1547 | Action::TypeResult |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1548 | Sema::ActOnTemplateIdType(TemplateTy TemplateD, SourceLocation TemplateLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1549 | SourceLocation LAngleLoc, |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1550 | ASTTemplateArgsPtr TemplateArgsIn, |
John McCall | 6b2becf | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 1551 | SourceLocation RAngleLoc) { |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1552 | TemplateName Template = TemplateD.getAsVal<TemplateName>(); |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 1553 | |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1554 | // Translate the parser's template argument list in our AST format. |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1555 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
Douglas Gregor | 314b97f | 2009-11-10 19:49:08 +0000 | [diff] [blame] | 1556 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 1557 | |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1558 | QualType Result = CheckTemplateIdType(Template, TemplateLoc, TemplateArgs); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1559 | TemplateArgsIn.release(); |
Douglas Gregor | 31a19b6 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 1560 | |
| 1561 | if (Result.isNull()) |
| 1562 | return true; |
| 1563 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1564 | TypeSourceInfo *DI = Context.CreateTypeSourceInfo(Result); |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1565 | TemplateSpecializationTypeLoc TL |
| 1566 | = cast<TemplateSpecializationTypeLoc>(DI->getTypeLoc()); |
| 1567 | TL.setTemplateNameLoc(TemplateLoc); |
| 1568 | TL.setLAngleLoc(LAngleLoc); |
| 1569 | TL.setRAngleLoc(RAngleLoc); |
| 1570 | for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) |
| 1571 | TL.setArgLocInfo(i, TemplateArgs[i].getLocInfo()); |
| 1572 | |
| 1573 | return CreateLocInfoType(Result, DI).getAsOpaquePtr(); |
John McCall | 6b2becf | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 1574 | } |
John McCall | f1bbbb4 | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 1575 | |
John McCall | 6b2becf | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 1576 | Sema::TypeResult Sema::ActOnTagTemplateIdType(TypeResult TypeResult, |
| 1577 | TagUseKind TUK, |
| 1578 | DeclSpec::TST TagSpec, |
| 1579 | SourceLocation TagLoc) { |
| 1580 | if (TypeResult.isInvalid()) |
| 1581 | return Sema::TypeResult(); |
John McCall | f1bbbb4 | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 1582 | |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1583 | // FIXME: preserve source info, ideally without copying the DI. |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1584 | TypeSourceInfo *DI; |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1585 | QualType Type = GetTypeFromParser(TypeResult.get(), &DI); |
John McCall | f1bbbb4 | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 1586 | |
John McCall | 6b2becf | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 1587 | // Verify the tag specifier. |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 1588 | TagTypeKind TagKind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1589 | |
John McCall | 6b2becf | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 1590 | if (const RecordType *RT = Type->getAs<RecordType>()) { |
| 1591 | RecordDecl *D = RT->getDecl(); |
| 1592 | |
| 1593 | IdentifierInfo *Id = D->getIdentifier(); |
| 1594 | assert(Id && "templated class must have an identifier"); |
| 1595 | |
| 1596 | if (!isAcceptableTagRedeclaration(D, TagKind, TagLoc, *Id)) { |
| 1597 | Diag(TagLoc, diag::err_use_with_wrong_tag) |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 1598 | << Type |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 1599 | << FixItHint::CreateReplacement(SourceRange(TagLoc), D->getKindName()); |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 1600 | Diag(D->getLocation(), diag::note_previous_use); |
John McCall | f1bbbb4 | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 1601 | } |
| 1602 | } |
| 1603 | |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 1604 | ElaboratedTypeKeyword Keyword |
| 1605 | = TypeWithKeyword::getKeywordForTagTypeKind(TagKind); |
| 1606 | QualType ElabType = Context.getElaboratedType(Keyword, /*NNS=*/0, Type); |
John McCall | 6b2becf | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 1607 | |
| 1608 | return ElabType.getAsOpaquePtr(); |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 1609 | } |
| 1610 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1611 | Sema::OwningExprResult Sema::BuildTemplateIdExpr(const CXXScopeSpec &SS, |
| 1612 | LookupResult &R, |
| 1613 | bool RequiresADL, |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1614 | const TemplateArgumentListInfo &TemplateArgs) { |
Douglas Gregor | edce4dd | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 1615 | // FIXME: Can we do any checking at this point? I guess we could check the |
| 1616 | // template arguments that we have against the template name, if the template |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1617 | // 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] | 1618 | // though. |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1619 | |
| 1620 | // These should be filtered out by our callers. |
| 1621 | assert(!R.empty() && "empty lookup results when building templateid"); |
| 1622 | assert(!R.isAmbiguous() && "ambiguous lookup when building templateid"); |
| 1623 | |
| 1624 | NestedNameSpecifier *Qualifier = 0; |
| 1625 | SourceRange QualifierRange; |
| 1626 | if (SS.isSet()) { |
| 1627 | Qualifier = static_cast<NestedNameSpecifier*>(SS.getScopeRep()); |
| 1628 | QualifierRange = SS.getRange(); |
Douglas Gregor | a9e29aa | 2009-10-22 07:19:14 +0000 | [diff] [blame] | 1629 | } |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 1630 | |
| 1631 | // We don't want lookup warnings at this point. |
| 1632 | R.suppressDiagnostics(); |
Douglas Gregor | a9e29aa | 2009-10-22 07:19:14 +0000 | [diff] [blame] | 1633 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1634 | bool Dependent |
| 1635 | = UnresolvedLookupExpr::ComputeDependence(R.begin(), R.end(), |
| 1636 | &TemplateArgs); |
| 1637 | UnresolvedLookupExpr *ULE |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 1638 | = UnresolvedLookupExpr::Create(Context, Dependent, R.getNamingClass(), |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1639 | Qualifier, QualifierRange, |
| 1640 | R.getLookupName(), R.getNameLoc(), |
Douglas Gregor | 5a84dec | 2010-05-23 18:57:34 +0000 | [diff] [blame] | 1641 | RequiresADL, TemplateArgs, |
| 1642 | R.begin(), R.end()); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1643 | |
| 1644 | return Owned(ULE); |
Douglas Gregor | edce4dd | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 1645 | } |
| 1646 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1647 | // We actually only call this from template instantiation. |
| 1648 | Sema::OwningExprResult |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 1649 | Sema::BuildQualifiedTemplateIdExpr(CXXScopeSpec &SS, |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1650 | DeclarationName Name, |
| 1651 | SourceLocation NameLoc, |
| 1652 | const TemplateArgumentListInfo &TemplateArgs) { |
| 1653 | DeclContext *DC; |
| 1654 | if (!(DC = computeDeclContext(SS, false)) || |
| 1655 | DC->isDependentContext() || |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 1656 | RequireCompleteDeclContext(SS, DC)) |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1657 | return BuildDependentDeclRefExpr(SS, Name, NameLoc, &TemplateArgs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1658 | |
Douglas Gregor | 1fd6d44 | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 1659 | bool MemberOfUnknownSpecialization; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1660 | LookupResult R(*this, Name, NameLoc, LookupOrdinaryName); |
Douglas Gregor | 1fd6d44 | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 1661 | LookupTemplateName(R, (Scope*) 0, SS, QualType(), /*Entering*/ false, |
| 1662 | MemberOfUnknownSpecialization); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1663 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1664 | if (R.isAmbiguous()) |
| 1665 | return ExprError(); |
| 1666 | |
| 1667 | if (R.empty()) { |
| 1668 | Diag(NameLoc, diag::err_template_kw_refers_to_non_template) |
| 1669 | << Name << SS.getRange(); |
| 1670 | return ExprError(); |
| 1671 | } |
| 1672 | |
| 1673 | if (ClassTemplateDecl *Temp = R.getAsSingle<ClassTemplateDecl>()) { |
| 1674 | Diag(NameLoc, diag::err_template_kw_refers_to_class_template) |
| 1675 | << (NestedNameSpecifier*) SS.getScopeRep() << Name << SS.getRange(); |
| 1676 | Diag(Temp->getLocation(), diag::note_referenced_class_template); |
| 1677 | return ExprError(); |
| 1678 | } |
| 1679 | |
| 1680 | return BuildTemplateIdExpr(SS, R, /* ADL */ false, TemplateArgs); |
Douglas Gregor | edce4dd | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 1681 | } |
| 1682 | |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 1683 | /// \brief Form a dependent template name. |
| 1684 | /// |
| 1685 | /// This action forms a dependent template name given the template |
| 1686 | /// name and its (presumably dependent) scope specifier. For |
| 1687 | /// example, given "MetaFun::template apply", the scope specifier \p |
| 1688 | /// SS will be "MetaFun::", \p TemplateKWLoc contains the location |
| 1689 | /// of the "template" keyword, and "apply" is the \p Name. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1690 | Sema::TemplateTy |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 1691 | Sema::ActOnDependentTemplateName(SourceLocation TemplateKWLoc, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 1692 | CXXScopeSpec &SS, |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 1693 | UnqualifiedId &Name, |
Douglas Gregor | a481edb | 2009-11-20 23:39:24 +0000 | [diff] [blame] | 1694 | TypeTy *ObjectType, |
| 1695 | bool EnteringContext) { |
Douglas Gregor | 0707bc5 | 2010-01-19 16:01:07 +0000 | [diff] [blame] | 1696 | DeclContext *LookupCtx = 0; |
| 1697 | if (SS.isSet()) |
| 1698 | LookupCtx = computeDeclContext(SS, EnteringContext); |
| 1699 | if (!LookupCtx && ObjectType) |
| 1700 | LookupCtx = computeDeclContext(QualType::getFromOpaquePtr(ObjectType)); |
| 1701 | if (LookupCtx) { |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 1702 | // C++0x [temp.names]p5: |
| 1703 | // If a name prefixed by the keyword template is not the name of |
| 1704 | // a template, the program is ill-formed. [Note: the keyword |
| 1705 | // template may not be applied to non-template members of class |
| 1706 | // templates. -end note ] [ Note: as is the case with the |
| 1707 | // typename prefix, the template prefix is allowed in cases |
| 1708 | // where it is not strictly necessary; i.e., when the |
| 1709 | // nested-name-specifier or the expression on the left of the -> |
| 1710 | // or . is not dependent on a template-parameter, or the use |
| 1711 | // does not appear in the scope of a template. -end note] |
| 1712 | // |
| 1713 | // Note: C++03 was more strict here, because it banned the use of |
| 1714 | // the "template" keyword prior to a template-name that was not a |
| 1715 | // dependent name. C++ DR468 relaxed this requirement (the |
| 1716 | // "template" keyword is now permitted). We follow the C++0x |
Douglas Gregor | 732281d | 2010-06-14 22:07:54 +0000 | [diff] [blame^] | 1717 | // rules, even in C++03 mode with a warning, retroactively applying the DR. |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 1718 | TemplateTy Template; |
Douglas Gregor | 1fd6d44 | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 1719 | bool MemberOfUnknownSpecialization; |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 1720 | TemplateNameKind TNK = isTemplateName(0, SS, Name, ObjectType, |
Douglas Gregor | 1fd6d44 | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 1721 | EnteringContext, Template, |
| 1722 | MemberOfUnknownSpecialization); |
Douglas Gregor | 0707bc5 | 2010-01-19 16:01:07 +0000 | [diff] [blame] | 1723 | if (TNK == TNK_Non_template && LookupCtx->isDependentContext() && |
| 1724 | isa<CXXRecordDecl>(LookupCtx) && |
| 1725 | cast<CXXRecordDecl>(LookupCtx)->hasAnyDependentBases()) { |
Douglas Gregor | 9edad9b | 2010-01-14 17:47:39 +0000 | [diff] [blame] | 1726 | // This is a dependent template. |
| 1727 | } else if (TNK == TNK_Non_template) { |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 1728 | Diag(Name.getSourceRange().getBegin(), |
| 1729 | diag::err_template_kw_refers_to_non_template) |
| 1730 | << GetNameFromUnqualifiedId(Name) |
Douglas Gregor | 0278e12 | 2010-05-05 05:58:24 +0000 | [diff] [blame] | 1731 | << Name.getSourceRange() |
| 1732 | << TemplateKWLoc; |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 1733 | return TemplateTy(); |
Douglas Gregor | 9edad9b | 2010-01-14 17:47:39 +0000 | [diff] [blame] | 1734 | } else { |
| 1735 | // We found something; return it. |
Douglas Gregor | 732281d | 2010-06-14 22:07:54 +0000 | [diff] [blame^] | 1736 | if (ActiveTemplateInstantiations.empty() && |
| 1737 | !getLangOptions().CPlusPlus0x && |
| 1738 | !SS.isEmpty() && !isDependentScopeSpecifier(SS)) |
| 1739 | Diag(TemplateKWLoc.isValid()? TemplateKWLoc |
| 1740 | : Name.getSourceRange().getBegin(), |
| 1741 | diag::ext_template_nondependent) |
| 1742 | << SourceRange(Name.getSourceRange().getBegin()) |
| 1743 | << FixItHint::CreateRemoval(TemplateKWLoc); |
| 1744 | |
Douglas Gregor | 9edad9b | 2010-01-14 17:47:39 +0000 | [diff] [blame] | 1745 | return Template; |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 1746 | } |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 1747 | } |
| 1748 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1749 | NestedNameSpecifier *Qualifier |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 1750 | = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 1751 | |
| 1752 | switch (Name.getKind()) { |
| 1753 | case UnqualifiedId::IK_Identifier: |
| 1754 | return TemplateTy::make(Context.getDependentTemplateName(Qualifier, |
| 1755 | Name.Identifier)); |
| 1756 | |
Douglas Gregor | ca1bdd7 | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 1757 | case UnqualifiedId::IK_OperatorFunctionId: |
| 1758 | return TemplateTy::make(Context.getDependentTemplateName(Qualifier, |
| 1759 | Name.OperatorFunctionId.Operator)); |
Sean Hunt | e6252d1 | 2009-11-28 08:58:14 +0000 | [diff] [blame] | 1760 | |
| 1761 | case UnqualifiedId::IK_LiteralOperatorId: |
| 1762 | assert(false && "We don't support these; Parse shouldn't have allowed propagation"); |
| 1763 | |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 1764 | default: |
| 1765 | break; |
| 1766 | } |
| 1767 | |
| 1768 | Diag(Name.getSourceRange().getBegin(), |
| 1769 | diag::err_template_kw_refers_to_non_template) |
| 1770 | << GetNameFromUnqualifiedId(Name) |
Douglas Gregor | 0278e12 | 2010-05-05 05:58:24 +0000 | [diff] [blame] | 1771 | << Name.getSourceRange() |
| 1772 | << TemplateKWLoc; |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 1773 | return TemplateTy(); |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 1774 | } |
| 1775 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1776 | bool Sema::CheckTemplateTypeArgument(TemplateTypeParmDecl *Param, |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1777 | const TemplateArgumentLoc &AL, |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 1778 | TemplateArgumentListBuilder &Converted) { |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1779 | const TemplateArgument &Arg = AL.getArgument(); |
| 1780 | |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 1781 | // Check template type parameter. |
Jeffrey Yasskin | db88d8a | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 1782 | switch(Arg.getKind()) { |
| 1783 | case TemplateArgument::Type: |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 1784 | // C++ [temp.arg.type]p1: |
| 1785 | // A template-argument for a template-parameter which is a |
| 1786 | // type shall be a type-id. |
Jeffrey Yasskin | db88d8a | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 1787 | break; |
| 1788 | case TemplateArgument::Template: { |
| 1789 | // We have a template type parameter but the template argument |
| 1790 | // is a template without any arguments. |
| 1791 | SourceRange SR = AL.getSourceRange(); |
| 1792 | TemplateName Name = Arg.getAsTemplate(); |
| 1793 | Diag(SR.getBegin(), diag::err_template_missing_args) |
| 1794 | << Name << SR; |
| 1795 | if (TemplateDecl *Decl = Name.getAsTemplateDecl()) |
| 1796 | Diag(Decl->getLocation(), diag::note_template_decl_here); |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 1797 | |
Jeffrey Yasskin | db88d8a | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 1798 | return true; |
| 1799 | } |
| 1800 | default: { |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 1801 | // We have a template type parameter but the template argument |
| 1802 | // is not a type. |
John McCall | 828bff2 | 2009-10-29 18:45:58 +0000 | [diff] [blame] | 1803 | SourceRange SR = AL.getSourceRange(); |
| 1804 | Diag(SR.getBegin(), diag::err_template_arg_must_be_type) << SR; |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 1805 | Diag(Param->getLocation(), diag::note_template_param_here); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1806 | |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 1807 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1808 | } |
Jeffrey Yasskin | db88d8a | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 1809 | } |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 1810 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1811 | if (CheckTemplateArgument(Param, AL.getTypeSourceInfo())) |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 1812 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1813 | |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 1814 | // Add the converted template type argument. |
Anders Carlsson | fb25052 | 2009-06-23 01:26:57 +0000 | [diff] [blame] | 1815 | Converted.Append( |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1816 | TemplateArgument(Context.getCanonicalType(Arg.getAsType()))); |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 1817 | return false; |
| 1818 | } |
| 1819 | |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 1820 | /// \brief Substitute template arguments into the default template argument for |
| 1821 | /// the given template type parameter. |
| 1822 | /// |
| 1823 | /// \param SemaRef the semantic analysis object for which we are performing |
| 1824 | /// the substitution. |
| 1825 | /// |
| 1826 | /// \param Template the template that we are synthesizing template arguments |
| 1827 | /// for. |
| 1828 | /// |
| 1829 | /// \param TemplateLoc the location of the template name that started the |
| 1830 | /// template-id we are checking. |
| 1831 | /// |
| 1832 | /// \param RAngleLoc the location of the right angle bracket ('>') that |
| 1833 | /// terminates the template-id. |
| 1834 | /// |
| 1835 | /// \param Param the template template parameter whose default we are |
| 1836 | /// substituting into. |
| 1837 | /// |
| 1838 | /// \param Converted the list of template arguments provided for template |
| 1839 | /// parameters that precede \p Param in the template parameter list. |
| 1840 | /// |
| 1841 | /// \returns the substituted template argument, or NULL if an error occurred. |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1842 | static TypeSourceInfo * |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 1843 | SubstDefaultTemplateArgument(Sema &SemaRef, |
| 1844 | TemplateDecl *Template, |
| 1845 | SourceLocation TemplateLoc, |
| 1846 | SourceLocation RAngleLoc, |
| 1847 | TemplateTypeParmDecl *Param, |
| 1848 | TemplateArgumentListBuilder &Converted) { |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1849 | TypeSourceInfo *ArgType = Param->getDefaultArgumentInfo(); |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 1850 | |
| 1851 | // If the argument type is dependent, instantiate it now based |
| 1852 | // on the previously-computed template arguments. |
| 1853 | if (ArgType->getType()->isDependentType()) { |
| 1854 | TemplateArgumentList TemplateArgs(SemaRef.Context, Converted, |
| 1855 | /*TakeArgs=*/false); |
| 1856 | |
| 1857 | MultiLevelTemplateArgumentList AllTemplateArgs |
| 1858 | = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs); |
| 1859 | |
| 1860 | Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc, |
| 1861 | Template, Converted.getFlatArguments(), |
| 1862 | Converted.flatSize(), |
| 1863 | SourceRange(TemplateLoc, RAngleLoc)); |
| 1864 | |
| 1865 | ArgType = SemaRef.SubstType(ArgType, AllTemplateArgs, |
| 1866 | Param->getDefaultArgumentLoc(), |
| 1867 | Param->getDeclName()); |
| 1868 | } |
| 1869 | |
| 1870 | return ArgType; |
| 1871 | } |
| 1872 | |
| 1873 | /// \brief Substitute template arguments into the default template argument for |
| 1874 | /// the given non-type template parameter. |
| 1875 | /// |
| 1876 | /// \param SemaRef the semantic analysis object for which we are performing |
| 1877 | /// the substitution. |
| 1878 | /// |
| 1879 | /// \param Template the template that we are synthesizing template arguments |
| 1880 | /// for. |
| 1881 | /// |
| 1882 | /// \param TemplateLoc the location of the template name that started the |
| 1883 | /// template-id we are checking. |
| 1884 | /// |
| 1885 | /// \param RAngleLoc the location of the right angle bracket ('>') that |
| 1886 | /// terminates the template-id. |
| 1887 | /// |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1888 | /// \param Param the non-type template parameter whose default we are |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 1889 | /// substituting into. |
| 1890 | /// |
| 1891 | /// \param Converted the list of template arguments provided for template |
| 1892 | /// parameters that precede \p Param in the template parameter list. |
| 1893 | /// |
| 1894 | /// \returns the substituted template argument, or NULL if an error occurred. |
| 1895 | static Sema::OwningExprResult |
| 1896 | SubstDefaultTemplateArgument(Sema &SemaRef, |
| 1897 | TemplateDecl *Template, |
| 1898 | SourceLocation TemplateLoc, |
| 1899 | SourceLocation RAngleLoc, |
| 1900 | NonTypeTemplateParmDecl *Param, |
| 1901 | TemplateArgumentListBuilder &Converted) { |
| 1902 | TemplateArgumentList TemplateArgs(SemaRef.Context, Converted, |
| 1903 | /*TakeArgs=*/false); |
| 1904 | |
| 1905 | MultiLevelTemplateArgumentList AllTemplateArgs |
| 1906 | = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs); |
| 1907 | |
| 1908 | Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc, |
| 1909 | Template, Converted.getFlatArguments(), |
| 1910 | Converted.flatSize(), |
| 1911 | SourceRange(TemplateLoc, RAngleLoc)); |
| 1912 | |
| 1913 | return SemaRef.SubstExpr(Param->getDefaultArgument(), AllTemplateArgs); |
| 1914 | } |
| 1915 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1916 | /// \brief Substitute template arguments into the default template argument for |
| 1917 | /// the given template template parameter. |
| 1918 | /// |
| 1919 | /// \param SemaRef the semantic analysis object for which we are performing |
| 1920 | /// the substitution. |
| 1921 | /// |
| 1922 | /// \param Template the template that we are synthesizing template arguments |
| 1923 | /// for. |
| 1924 | /// |
| 1925 | /// \param TemplateLoc the location of the template name that started the |
| 1926 | /// template-id we are checking. |
| 1927 | /// |
| 1928 | /// \param RAngleLoc the location of the right angle bracket ('>') that |
| 1929 | /// terminates the template-id. |
| 1930 | /// |
| 1931 | /// \param Param the template template parameter whose default we are |
| 1932 | /// substituting into. |
| 1933 | /// |
| 1934 | /// \param Converted the list of template arguments provided for template |
| 1935 | /// parameters that precede \p Param in the template parameter list. |
| 1936 | /// |
| 1937 | /// \returns the substituted template argument, or NULL if an error occurred. |
| 1938 | static TemplateName |
| 1939 | SubstDefaultTemplateArgument(Sema &SemaRef, |
| 1940 | TemplateDecl *Template, |
| 1941 | SourceLocation TemplateLoc, |
| 1942 | SourceLocation RAngleLoc, |
| 1943 | TemplateTemplateParmDecl *Param, |
| 1944 | TemplateArgumentListBuilder &Converted) { |
| 1945 | TemplateArgumentList TemplateArgs(SemaRef.Context, Converted, |
| 1946 | /*TakeArgs=*/false); |
| 1947 | |
| 1948 | MultiLevelTemplateArgumentList AllTemplateArgs |
| 1949 | = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs); |
| 1950 | |
| 1951 | Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc, |
| 1952 | Template, Converted.getFlatArguments(), |
| 1953 | Converted.flatSize(), |
| 1954 | SourceRange(TemplateLoc, RAngleLoc)); |
| 1955 | |
| 1956 | return SemaRef.SubstTemplateName( |
| 1957 | Param->getDefaultArgument().getArgument().getAsTemplate(), |
| 1958 | Param->getDefaultArgument().getTemplateNameLoc(), |
| 1959 | AllTemplateArgs); |
| 1960 | } |
| 1961 | |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 1962 | /// \brief If the given template parameter has a default template |
| 1963 | /// argument, substitute into that default template argument and |
| 1964 | /// return the corresponding template argument. |
| 1965 | TemplateArgumentLoc |
| 1966 | Sema::SubstDefaultTemplateArgumentIfAvailable(TemplateDecl *Template, |
| 1967 | SourceLocation TemplateLoc, |
| 1968 | SourceLocation RAngleLoc, |
| 1969 | Decl *Param, |
| 1970 | TemplateArgumentListBuilder &Converted) { |
| 1971 | if (TemplateTypeParmDecl *TypeParm = dyn_cast<TemplateTypeParmDecl>(Param)) { |
| 1972 | if (!TypeParm->hasDefaultArgument()) |
| 1973 | return TemplateArgumentLoc(); |
| 1974 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1975 | TypeSourceInfo *DI = SubstDefaultTemplateArgument(*this, Template, |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 1976 | TemplateLoc, |
| 1977 | RAngleLoc, |
| 1978 | TypeParm, |
| 1979 | Converted); |
| 1980 | if (DI) |
| 1981 | return TemplateArgumentLoc(TemplateArgument(DI->getType()), DI); |
| 1982 | |
| 1983 | return TemplateArgumentLoc(); |
| 1984 | } |
| 1985 | |
| 1986 | if (NonTypeTemplateParmDecl *NonTypeParm |
| 1987 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
| 1988 | if (!NonTypeParm->hasDefaultArgument()) |
| 1989 | return TemplateArgumentLoc(); |
| 1990 | |
| 1991 | OwningExprResult Arg = SubstDefaultTemplateArgument(*this, Template, |
| 1992 | TemplateLoc, |
| 1993 | RAngleLoc, |
| 1994 | NonTypeParm, |
| 1995 | Converted); |
| 1996 | if (Arg.isInvalid()) |
| 1997 | return TemplateArgumentLoc(); |
| 1998 | |
| 1999 | Expr *ArgE = Arg.takeAs<Expr>(); |
| 2000 | return TemplateArgumentLoc(TemplateArgument(ArgE), ArgE); |
| 2001 | } |
| 2002 | |
| 2003 | TemplateTemplateParmDecl *TempTempParm |
| 2004 | = cast<TemplateTemplateParmDecl>(Param); |
| 2005 | if (!TempTempParm->hasDefaultArgument()) |
| 2006 | return TemplateArgumentLoc(); |
| 2007 | |
| 2008 | TemplateName TName = SubstDefaultTemplateArgument(*this, Template, |
| 2009 | TemplateLoc, |
| 2010 | RAngleLoc, |
| 2011 | TempTempParm, |
| 2012 | Converted); |
| 2013 | if (TName.isNull()) |
| 2014 | return TemplateArgumentLoc(); |
| 2015 | |
| 2016 | return TemplateArgumentLoc(TemplateArgument(TName), |
| 2017 | TempTempParm->getDefaultArgument().getTemplateQualifierRange(), |
| 2018 | TempTempParm->getDefaultArgument().getTemplateNameLoc()); |
| 2019 | } |
| 2020 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2021 | /// \brief Check that the given template argument corresponds to the given |
| 2022 | /// template parameter. |
| 2023 | bool Sema::CheckTemplateArgument(NamedDecl *Param, |
| 2024 | const TemplateArgumentLoc &Arg, |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2025 | TemplateDecl *Template, |
| 2026 | SourceLocation TemplateLoc, |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2027 | SourceLocation RAngleLoc, |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 2028 | TemplateArgumentListBuilder &Converted, |
| 2029 | CheckTemplateArgumentKind CTAK) { |
Douglas Gregor | d9e1530 | 2009-11-11 19:41:09 +0000 | [diff] [blame] | 2030 | // Check template type parameters. |
| 2031 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2032 | return CheckTemplateTypeArgument(TTP, Arg, Converted); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2033 | |
Douglas Gregor | d9e1530 | 2009-11-11 19:41:09 +0000 | [diff] [blame] | 2034 | // Check non-type template parameters. |
| 2035 | if (NonTypeTemplateParmDecl *NTTP =dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2036 | // Do substitution on the type of the non-type template parameter |
| 2037 | // with the template arguments we've seen thus far. |
| 2038 | QualType NTTPType = NTTP->getType(); |
| 2039 | if (NTTPType->isDependentType()) { |
| 2040 | // Do substitution on the type of the non-type template parameter. |
| 2041 | InstantiatingTemplate Inst(*this, TemplateLoc, Template, |
| 2042 | NTTP, Converted.getFlatArguments(), |
| 2043 | Converted.flatSize(), |
| 2044 | SourceRange(TemplateLoc, RAngleLoc)); |
| 2045 | |
| 2046 | TemplateArgumentList TemplateArgs(Context, Converted, |
| 2047 | /*TakeArgs=*/false); |
| 2048 | NTTPType = SubstType(NTTPType, |
| 2049 | MultiLevelTemplateArgumentList(TemplateArgs), |
| 2050 | NTTP->getLocation(), |
| 2051 | NTTP->getDeclName()); |
| 2052 | // If that worked, check the non-type template parameter type |
| 2053 | // for validity. |
| 2054 | if (!NTTPType.isNull()) |
| 2055 | NTTPType = CheckNonTypeTemplateParameterType(NTTPType, |
| 2056 | NTTP->getLocation()); |
| 2057 | if (NTTPType.isNull()) |
| 2058 | return true; |
| 2059 | } |
| 2060 | |
| 2061 | switch (Arg.getArgument().getKind()) { |
| 2062 | case TemplateArgument::Null: |
| 2063 | assert(false && "Should never see a NULL template argument here"); |
| 2064 | return true; |
| 2065 | |
| 2066 | case TemplateArgument::Expression: { |
| 2067 | Expr *E = Arg.getArgument().getAsExpr(); |
| 2068 | TemplateArgument Result; |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 2069 | if (CheckTemplateArgument(NTTP, NTTPType, E, Result, CTAK)) |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2070 | return true; |
| 2071 | |
| 2072 | Converted.Append(Result); |
| 2073 | break; |
| 2074 | } |
| 2075 | |
| 2076 | case TemplateArgument::Declaration: |
| 2077 | case TemplateArgument::Integral: |
| 2078 | // We've already checked this template argument, so just copy |
| 2079 | // it to the list of converted arguments. |
| 2080 | Converted.Append(Arg.getArgument()); |
| 2081 | break; |
| 2082 | |
| 2083 | case TemplateArgument::Template: |
| 2084 | // We were given a template template argument. It may not be ill-formed; |
| 2085 | // see below. |
| 2086 | if (DependentTemplateName *DTN |
| 2087 | = Arg.getArgument().getAsTemplate().getAsDependentTemplateName()) { |
| 2088 | // We have a template argument such as \c T::template X, which we |
| 2089 | // parsed as a template template argument. However, since we now |
| 2090 | // know that we need a non-type template argument, convert this |
| 2091 | // template name into an expression. |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2092 | Expr *E = DependentScopeDeclRefExpr::Create(Context, |
| 2093 | DTN->getQualifier(), |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2094 | Arg.getTemplateQualifierRange(), |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2095 | DTN->getIdentifier(), |
| 2096 | Arg.getTemplateNameLoc()); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2097 | |
| 2098 | TemplateArgument Result; |
| 2099 | if (CheckTemplateArgument(NTTP, NTTPType, E, Result)) |
| 2100 | return true; |
| 2101 | |
| 2102 | Converted.Append(Result); |
| 2103 | break; |
| 2104 | } |
| 2105 | |
| 2106 | // We have a template argument that actually does refer to a class |
| 2107 | // template, template alias, or template template parameter, and |
| 2108 | // therefore cannot be a non-type template argument. |
| 2109 | Diag(Arg.getLocation(), diag::err_template_arg_must_be_expr) |
| 2110 | << Arg.getSourceRange(); |
| 2111 | |
| 2112 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 2113 | return true; |
| 2114 | |
| 2115 | case TemplateArgument::Type: { |
| 2116 | // We have a non-type template parameter but the template |
| 2117 | // argument is a type. |
| 2118 | |
| 2119 | // C++ [temp.arg]p2: |
| 2120 | // In a template-argument, an ambiguity between a type-id and |
| 2121 | // an expression is resolved to a type-id, regardless of the |
| 2122 | // form of the corresponding template-parameter. |
| 2123 | // |
| 2124 | // We warn specifically about this case, since it can be rather |
| 2125 | // confusing for users. |
| 2126 | QualType T = Arg.getArgument().getAsType(); |
| 2127 | SourceRange SR = Arg.getSourceRange(); |
| 2128 | if (T->isFunctionType()) |
| 2129 | Diag(SR.getBegin(), diag::err_template_arg_nontype_ambig) << SR << T; |
| 2130 | else |
| 2131 | Diag(SR.getBegin(), diag::err_template_arg_must_be_expr) << SR; |
| 2132 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 2133 | return true; |
| 2134 | } |
| 2135 | |
| 2136 | case TemplateArgument::Pack: |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 2137 | llvm_unreachable("Caller must expand template argument packs"); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2138 | break; |
| 2139 | } |
| 2140 | |
| 2141 | return false; |
| 2142 | } |
| 2143 | |
| 2144 | |
| 2145 | // Check template template parameters. |
| 2146 | TemplateTemplateParmDecl *TempParm = cast<TemplateTemplateParmDecl>(Param); |
| 2147 | |
| 2148 | // Substitute into the template parameter list of the template |
| 2149 | // template parameter, since previously-supplied template arguments |
| 2150 | // may appear within the template template parameter. |
| 2151 | { |
| 2152 | // Set up a template instantiation context. |
| 2153 | LocalInstantiationScope Scope(*this); |
| 2154 | InstantiatingTemplate Inst(*this, TemplateLoc, Template, |
| 2155 | TempParm, Converted.getFlatArguments(), |
| 2156 | Converted.flatSize(), |
| 2157 | SourceRange(TemplateLoc, RAngleLoc)); |
| 2158 | |
| 2159 | TemplateArgumentList TemplateArgs(Context, Converted, |
| 2160 | /*TakeArgs=*/false); |
| 2161 | TempParm = cast_or_null<TemplateTemplateParmDecl>( |
| 2162 | SubstDecl(TempParm, CurContext, |
| 2163 | MultiLevelTemplateArgumentList(TemplateArgs))); |
| 2164 | if (!TempParm) |
| 2165 | return true; |
| 2166 | |
| 2167 | // FIXME: TempParam is leaked. |
| 2168 | } |
| 2169 | |
| 2170 | switch (Arg.getArgument().getKind()) { |
| 2171 | case TemplateArgument::Null: |
| 2172 | assert(false && "Should never see a NULL template argument here"); |
| 2173 | return true; |
| 2174 | |
| 2175 | case TemplateArgument::Template: |
| 2176 | if (CheckTemplateArgument(TempParm, Arg)) |
| 2177 | return true; |
| 2178 | |
| 2179 | Converted.Append(Arg.getArgument()); |
| 2180 | break; |
| 2181 | |
| 2182 | case TemplateArgument::Expression: |
| 2183 | case TemplateArgument::Type: |
| 2184 | // We have a template template parameter but the template |
| 2185 | // argument does not refer to a template. |
| 2186 | Diag(Arg.getLocation(), diag::err_template_arg_must_be_template); |
| 2187 | return true; |
| 2188 | |
| 2189 | case TemplateArgument::Declaration: |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 2190 | llvm_unreachable( |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2191 | "Declaration argument with template template parameter"); |
| 2192 | break; |
| 2193 | case TemplateArgument::Integral: |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 2194 | llvm_unreachable( |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2195 | "Integral argument with template template parameter"); |
| 2196 | break; |
| 2197 | |
| 2198 | case TemplateArgument::Pack: |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 2199 | llvm_unreachable("Caller must expand template argument packs"); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2200 | break; |
| 2201 | } |
| 2202 | |
| 2203 | return false; |
| 2204 | } |
| 2205 | |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2206 | /// \brief Check that the given template argument list is well-formed |
| 2207 | /// for specializing the given template. |
| 2208 | bool Sema::CheckTemplateArgumentList(TemplateDecl *Template, |
| 2209 | SourceLocation TemplateLoc, |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2210 | const TemplateArgumentListInfo &TemplateArgs, |
Douglas Gregor | 16134c6 | 2009-07-01 00:28:38 +0000 | [diff] [blame] | 2211 | bool PartialTemplateArgs, |
Anders Carlsson | 1c5976e | 2009-06-05 03:43:12 +0000 | [diff] [blame] | 2212 | TemplateArgumentListBuilder &Converted) { |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2213 | TemplateParameterList *Params = Template->getTemplateParameters(); |
| 2214 | unsigned NumParams = Params->size(); |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2215 | unsigned NumArgs = TemplateArgs.size(); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2216 | bool Invalid = false; |
| 2217 | |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2218 | SourceLocation RAngleLoc = TemplateArgs.getRAngleLoc(); |
| 2219 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2220 | bool HasParameterPack = |
Anders Carlsson | 0ceffb5 | 2009-06-13 02:08:00 +0000 | [diff] [blame] | 2221 | NumParams > 0 && Params->getParam(NumParams - 1)->isTemplateParameterPack(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2222 | |
Anders Carlsson | 0ceffb5 | 2009-06-13 02:08:00 +0000 | [diff] [blame] | 2223 | if ((NumArgs > NumParams && !HasParameterPack) || |
Douglas Gregor | 16134c6 | 2009-07-01 00:28:38 +0000 | [diff] [blame] | 2224 | (NumArgs < Params->getMinRequiredArguments() && |
| 2225 | !PartialTemplateArgs)) { |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2226 | // FIXME: point at either the first arg beyond what we can handle, |
| 2227 | // or the '>', depending on whether we have too many or too few |
| 2228 | // arguments. |
| 2229 | SourceRange Range; |
| 2230 | if (NumArgs > NumParams) |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2231 | Range = SourceRange(TemplateArgs[NumParams].getLocation(), RAngleLoc); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2232 | Diag(TemplateLoc, diag::err_template_arg_list_different_arity) |
| 2233 | << (NumArgs > NumParams) |
| 2234 | << (isa<ClassTemplateDecl>(Template)? 0 : |
| 2235 | isa<FunctionTemplateDecl>(Template)? 1 : |
| 2236 | isa<TemplateTemplateParmDecl>(Template)? 2 : 3) |
| 2237 | << Template << Range; |
Douglas Gregor | 62cb18d | 2009-02-11 18:16:40 +0000 | [diff] [blame] | 2238 | Diag(Template->getLocation(), diag::note_template_decl_here) |
| 2239 | << Params->getSourceRange(); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2240 | Invalid = true; |
| 2241 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2242 | |
| 2243 | // C++ [temp.arg]p1: |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2244 | // [...] The type and form of each template-argument specified in |
| 2245 | // a template-id shall match the type and form specified for the |
| 2246 | // corresponding parameter declared by the template in its |
| 2247 | // template-parameter-list. |
| 2248 | unsigned ArgIdx = 0; |
| 2249 | for (TemplateParameterList::iterator Param = Params->begin(), |
| 2250 | ParamEnd = Params->end(); |
| 2251 | Param != ParamEnd; ++Param, ++ArgIdx) { |
Douglas Gregor | 16134c6 | 2009-07-01 00:28:38 +0000 | [diff] [blame] | 2252 | if (ArgIdx > NumArgs && PartialTemplateArgs) |
| 2253 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2254 | |
Douglas Gregor | d9e1530 | 2009-11-11 19:41:09 +0000 | [diff] [blame] | 2255 | // If we have a template parameter pack, check every remaining template |
| 2256 | // argument against that template parameter pack. |
| 2257 | if ((*Param)->isTemplateParameterPack()) { |
| 2258 | Converted.BeginPack(); |
| 2259 | for (; ArgIdx < NumArgs; ++ArgIdx) { |
| 2260 | if (CheckTemplateArgument(*Param, TemplateArgs[ArgIdx], Template, |
| 2261 | TemplateLoc, RAngleLoc, Converted)) { |
| 2262 | Invalid = true; |
| 2263 | break; |
| 2264 | } |
| 2265 | } |
| 2266 | Converted.EndPack(); |
| 2267 | continue; |
| 2268 | } |
| 2269 | |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2270 | if (ArgIdx < NumArgs) { |
| 2271 | // Check the template argument we were given. |
| 2272 | if (CheckTemplateArgument(*Param, TemplateArgs[ArgIdx], Template, |
| 2273 | TemplateLoc, RAngleLoc, Converted)) |
| 2274 | return true; |
| 2275 | |
| 2276 | continue; |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 2277 | } |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2278 | |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2279 | // We have a default template argument that we will use. |
| 2280 | TemplateArgumentLoc Arg; |
| 2281 | |
| 2282 | // Retrieve the default template argument from the template |
| 2283 | // parameter. For each kind of template parameter, we substitute the |
| 2284 | // template arguments provided thus far and any "outer" template arguments |
| 2285 | // (when the template parameter was part of a nested template) into |
| 2286 | // the default argument. |
| 2287 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*Param)) { |
| 2288 | if (!TTP->hasDefaultArgument()) { |
| 2289 | assert((Invalid || PartialTemplateArgs) && "Missing default argument"); |
| 2290 | break; |
| 2291 | } |
| 2292 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2293 | TypeSourceInfo *ArgType = SubstDefaultTemplateArgument(*this, |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2294 | Template, |
| 2295 | TemplateLoc, |
| 2296 | RAngleLoc, |
| 2297 | TTP, |
| 2298 | Converted); |
| 2299 | if (!ArgType) |
| 2300 | return true; |
| 2301 | |
| 2302 | Arg = TemplateArgumentLoc(TemplateArgument(ArgType->getType()), |
| 2303 | ArgType); |
| 2304 | } else if (NonTypeTemplateParmDecl *NTTP |
| 2305 | = dyn_cast<NonTypeTemplateParmDecl>(*Param)) { |
| 2306 | if (!NTTP->hasDefaultArgument()) { |
| 2307 | assert((Invalid || PartialTemplateArgs) && "Missing default argument"); |
| 2308 | break; |
| 2309 | } |
| 2310 | |
| 2311 | Sema::OwningExprResult E = SubstDefaultTemplateArgument(*this, Template, |
| 2312 | TemplateLoc, |
| 2313 | RAngleLoc, |
| 2314 | NTTP, |
| 2315 | Converted); |
| 2316 | if (E.isInvalid()) |
| 2317 | return true; |
| 2318 | |
| 2319 | Expr *Ex = E.takeAs<Expr>(); |
| 2320 | Arg = TemplateArgumentLoc(TemplateArgument(Ex), Ex); |
| 2321 | } else { |
| 2322 | TemplateTemplateParmDecl *TempParm |
| 2323 | = cast<TemplateTemplateParmDecl>(*Param); |
| 2324 | |
| 2325 | if (!TempParm->hasDefaultArgument()) { |
| 2326 | assert((Invalid || PartialTemplateArgs) && "Missing default argument"); |
| 2327 | break; |
| 2328 | } |
| 2329 | |
| 2330 | TemplateName Name = SubstDefaultTemplateArgument(*this, Template, |
| 2331 | TemplateLoc, |
| 2332 | RAngleLoc, |
| 2333 | TempParm, |
| 2334 | Converted); |
| 2335 | if (Name.isNull()) |
| 2336 | return true; |
| 2337 | |
| 2338 | Arg = TemplateArgumentLoc(TemplateArgument(Name), |
| 2339 | TempParm->getDefaultArgument().getTemplateQualifierRange(), |
| 2340 | TempParm->getDefaultArgument().getTemplateNameLoc()); |
| 2341 | } |
| 2342 | |
| 2343 | // Introduce an instantiation record that describes where we are using |
| 2344 | // the default template argument. |
| 2345 | InstantiatingTemplate Instantiating(*this, RAngleLoc, Template, *Param, |
| 2346 | Converted.getFlatArguments(), |
| 2347 | Converted.flatSize(), |
| 2348 | SourceRange(TemplateLoc, RAngleLoc)); |
| 2349 | |
| 2350 | // Check the default template argument. |
Douglas Gregor | d9e1530 | 2009-11-11 19:41:09 +0000 | [diff] [blame] | 2351 | if (CheckTemplateArgument(*Param, Arg, Template, TemplateLoc, |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2352 | RAngleLoc, Converted)) |
| 2353 | return true; |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2354 | } |
| 2355 | |
| 2356 | return Invalid; |
| 2357 | } |
| 2358 | |
| 2359 | /// \brief Check a template argument against its corresponding |
| 2360 | /// template type parameter. |
| 2361 | /// |
| 2362 | /// This routine implements the semantics of C++ [temp.arg.type]. It |
| 2363 | /// returns true if an error occurred, and false otherwise. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2364 | bool Sema::CheckTemplateArgument(TemplateTypeParmDecl *Param, |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2365 | TypeSourceInfo *ArgInfo) { |
| 2366 | assert(ArgInfo && "invalid TypeSourceInfo"); |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2367 | QualType Arg = ArgInfo->getType(); |
| 2368 | |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2369 | // C++ [temp.arg.type]p2: |
| 2370 | // A local type, a type with no linkage, an unnamed type or a type |
| 2371 | // compounded from any of these types shall not be used as a |
| 2372 | // template-argument for a template type-parameter. |
| 2373 | // |
Douglas Gregor | 0fddb97 | 2010-05-22 16:17:30 +0000 | [diff] [blame] | 2374 | // FIXME: Perform the unnamed type check. |
| 2375 | SourceRange SR = ArgInfo->getTypeLoc().getSourceRange(); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2376 | const TagType *Tag = 0; |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2377 | if (const EnumType *EnumT = Arg->getAs<EnumType>()) |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2378 | Tag = EnumT; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2379 | else if (const RecordType *RecordT = Arg->getAs<RecordType>()) |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2380 | Tag = RecordT; |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2381 | if (Tag && Tag->getDecl()->getDeclContext()->isFunctionOrMethod()) { |
Abramo Bagnara | bd054db | 2010-05-20 10:00:11 +0000 | [diff] [blame] | 2382 | SourceRange SR = ArgInfo->getTypeLoc().getSourceRange(); |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2383 | return Diag(SR.getBegin(), diag::err_template_arg_local_type) |
| 2384 | << QualType(Tag, 0) << SR; |
| 2385 | } else if (Tag && !Tag->getDecl()->getDeclName() && |
Douglas Gregor | 9813753 | 2009-03-10 18:33:27 +0000 | [diff] [blame] | 2386 | !Tag->getDecl()->getTypedefForAnonDecl()) { |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2387 | Diag(SR.getBegin(), diag::err_template_arg_unnamed_type) << SR; |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2388 | Diag(Tag->getDecl()->getLocation(), diag::note_template_unnamed_type_here); |
| 2389 | return true; |
Douglas Gregor | 0fddb97 | 2010-05-22 16:17:30 +0000 | [diff] [blame] | 2390 | } else if (Arg->isVariablyModifiedType()) { |
| 2391 | Diag(SR.getBegin(), diag::err_variably_modified_template_arg) |
| 2392 | << Arg; |
| 2393 | return true; |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 2394 | } else if (Context.hasSameUnqualifiedType(Arg, Context.OverloadTy)) { |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 2395 | return Diag(SR.getBegin(), diag::err_template_arg_overload_type) << SR; |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2396 | } |
| 2397 | |
| 2398 | return false; |
| 2399 | } |
| 2400 | |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2401 | /// \brief Checks whether the given template argument is the address |
| 2402 | /// of an object or function according to C++ [temp.arg.nontype]p1. |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2403 | static bool |
| 2404 | CheckTemplateArgumentAddressOfObjectOrFunction(Sema &S, |
| 2405 | NonTypeTemplateParmDecl *Param, |
| 2406 | QualType ParamType, |
| 2407 | Expr *ArgIn, |
| 2408 | TemplateArgument &Converted) { |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2409 | bool Invalid = false; |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2410 | Expr *Arg = ArgIn; |
| 2411 | QualType ArgType = Arg->getType(); |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2412 | |
| 2413 | // See through any implicit casts we added to fix the type. |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 2414 | while (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(Arg)) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2415 | Arg = Cast->getSubExpr(); |
| 2416 | |
| 2417 | // C++ [temp.arg.nontype]p1: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2418 | // |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2419 | // A template-argument for a non-type, non-template |
| 2420 | // template-parameter shall be one of: [...] |
| 2421 | // |
| 2422 | // -- the address of an object or function with external |
| 2423 | // linkage, including function templates and function |
| 2424 | // template-ids but excluding non-static class members, |
| 2425 | // expressed as & id-expression where the & is optional if |
| 2426 | // the name refers to a function or array, or if the |
| 2427 | // corresponding template-parameter is a reference; or |
| 2428 | DeclRefExpr *DRE = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2429 | |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2430 | // Ignore (and complain about) any excess parentheses. |
| 2431 | while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) { |
| 2432 | if (!Invalid) { |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2433 | S.Diag(Arg->getSourceRange().getBegin(), |
| 2434 | diag::err_template_arg_extra_parens) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2435 | << Arg->getSourceRange(); |
| 2436 | Invalid = true; |
| 2437 | } |
| 2438 | |
| 2439 | Arg = Parens->getSubExpr(); |
| 2440 | } |
| 2441 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2442 | bool AddressTaken = false; |
| 2443 | SourceLocation AddrOpLoc; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2444 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) { |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2445 | if (UnOp->getOpcode() == UnaryOperator::AddrOf) { |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2446 | DRE = dyn_cast<DeclRefExpr>(UnOp->getSubExpr()); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2447 | AddressTaken = true; |
| 2448 | AddrOpLoc = UnOp->getOperatorLoc(); |
| 2449 | } |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2450 | } else |
| 2451 | DRE = dyn_cast<DeclRefExpr>(Arg); |
| 2452 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2453 | if (!DRE) { |
Douglas Gregor | 1a8cf73 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 2454 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_decl_ref) |
| 2455 | << Arg->getSourceRange(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2456 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 2457 | return true; |
| 2458 | } |
Chandler Carruth | 038cc39 | 2010-01-31 10:01:20 +0000 | [diff] [blame] | 2459 | |
| 2460 | // Stop checking the precise nature of the argument if it is value dependent, |
| 2461 | // it should be checked when instantiated. |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2462 | if (Arg->isValueDependent()) { |
| 2463 | Converted = TemplateArgument(ArgIn->Retain()); |
Chandler Carruth | 038cc39 | 2010-01-31 10:01:20 +0000 | [diff] [blame] | 2464 | return false; |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2465 | } |
Chandler Carruth | 038cc39 | 2010-01-31 10:01:20 +0000 | [diff] [blame] | 2466 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2467 | if (!isa<ValueDecl>(DRE->getDecl())) { |
| 2468 | S.Diag(Arg->getSourceRange().getBegin(), |
| 2469 | diag::err_template_arg_not_object_or_func_form) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2470 | << Arg->getSourceRange(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2471 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 2472 | return true; |
| 2473 | } |
| 2474 | |
| 2475 | NamedDecl *Entity = 0; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2476 | |
| 2477 | // Cannot refer to non-static data members |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2478 | if (FieldDecl *Field = dyn_cast<FieldDecl>(DRE->getDecl())) { |
| 2479 | S.Diag(Arg->getSourceRange().getBegin(), diag::err_template_arg_field) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2480 | << Field << Arg->getSourceRange(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2481 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 2482 | return true; |
| 2483 | } |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2484 | |
| 2485 | // Cannot refer to non-static member functions |
| 2486 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(DRE->getDecl())) |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2487 | if (!Method->isStatic()) { |
| 2488 | S.Diag(Arg->getSourceRange().getBegin(), diag::err_template_arg_method) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2489 | << Method << Arg->getSourceRange(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2490 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 2491 | return true; |
| 2492 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2493 | |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2494 | // Functions must have external linkage. |
| 2495 | if (FunctionDecl *Func = dyn_cast<FunctionDecl>(DRE->getDecl())) { |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 2496 | if (!isExternalLinkage(Func->getLinkage())) { |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2497 | S.Diag(Arg->getSourceRange().getBegin(), |
| 2498 | diag::err_template_arg_function_not_extern) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2499 | << Func << Arg->getSourceRange(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2500 | S.Diag(Func->getLocation(), diag::note_template_arg_internal_object) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2501 | << true; |
| 2502 | return true; |
| 2503 | } |
| 2504 | |
| 2505 | // Okay: we've named a function with external linkage. |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 2506 | Entity = Func; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2507 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2508 | // If the template parameter has pointer type, the function decays. |
| 2509 | if (ParamType->isPointerType() && !AddressTaken) |
| 2510 | ArgType = S.Context.getPointerType(Func->getType()); |
| 2511 | else if (AddressTaken && ParamType->isReferenceType()) { |
| 2512 | // If we originally had an address-of operator, but the |
| 2513 | // parameter has reference type, complain and (if things look |
| 2514 | // like they will work) drop the address-of operator. |
| 2515 | if (!S.Context.hasSameUnqualifiedType(Func->getType(), |
| 2516 | ParamType.getNonReferenceType())) { |
| 2517 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 2518 | << ParamType; |
| 2519 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 2520 | return true; |
| 2521 | } |
| 2522 | |
| 2523 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 2524 | << ParamType |
| 2525 | << FixItHint::CreateRemoval(AddrOpLoc); |
| 2526 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 2527 | |
| 2528 | ArgType = Func->getType(); |
| 2529 | } |
| 2530 | } else if (VarDecl *Var = dyn_cast<VarDecl>(DRE->getDecl())) { |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 2531 | if (!isExternalLinkage(Var->getLinkage())) { |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2532 | S.Diag(Arg->getSourceRange().getBegin(), |
| 2533 | diag::err_template_arg_object_not_extern) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2534 | << Var << Arg->getSourceRange(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2535 | S.Diag(Var->getLocation(), diag::note_template_arg_internal_object) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2536 | << true; |
| 2537 | return true; |
| 2538 | } |
| 2539 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2540 | // A value of reference type is not an object. |
| 2541 | if (Var->getType()->isReferenceType()) { |
| 2542 | S.Diag(Arg->getSourceRange().getBegin(), |
| 2543 | diag::err_template_arg_reference_var) |
| 2544 | << Var->getType() << Arg->getSourceRange(); |
| 2545 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 2546 | return true; |
| 2547 | } |
| 2548 | |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2549 | // Okay: we've named an object with external linkage |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 2550 | Entity = Var; |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2551 | |
| 2552 | // If the template parameter has pointer type, we must have taken |
| 2553 | // the address of this object. |
| 2554 | if (ParamType->isReferenceType()) { |
| 2555 | if (AddressTaken) { |
| 2556 | // If we originally had an address-of operator, but the |
| 2557 | // parameter has reference type, complain and (if things look |
| 2558 | // like they will work) drop the address-of operator. |
| 2559 | if (!S.Context.hasSameUnqualifiedType(Var->getType(), |
| 2560 | ParamType.getNonReferenceType())) { |
| 2561 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 2562 | << ParamType; |
| 2563 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 2564 | return true; |
| 2565 | } |
| 2566 | |
| 2567 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 2568 | << ParamType |
| 2569 | << FixItHint::CreateRemoval(AddrOpLoc); |
| 2570 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 2571 | |
| 2572 | ArgType = Var->getType(); |
| 2573 | } |
| 2574 | } else if (!AddressTaken && ParamType->isPointerType()) { |
| 2575 | if (Var->getType()->isArrayType()) { |
| 2576 | // Array-to-pointer decay. |
| 2577 | ArgType = S.Context.getArrayDecayedType(Var->getType()); |
| 2578 | } else { |
| 2579 | // If the template parameter has pointer type but the address of |
| 2580 | // this object was not taken, complain and (possibly) recover by |
| 2581 | // taking the address of the entity. |
| 2582 | ArgType = S.Context.getPointerType(Var->getType()); |
| 2583 | if (!S.Context.hasSameUnqualifiedType(ArgType, ParamType)) { |
| 2584 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_address_of) |
| 2585 | << ParamType; |
| 2586 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 2587 | return true; |
| 2588 | } |
| 2589 | |
| 2590 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_address_of) |
| 2591 | << ParamType |
| 2592 | << FixItHint::CreateInsertion(Arg->getLocStart(), "&"); |
| 2593 | |
| 2594 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 2595 | } |
| 2596 | } |
| 2597 | } else { |
| 2598 | // We found something else, but we don't know specifically what it is. |
| 2599 | S.Diag(Arg->getSourceRange().getBegin(), |
| 2600 | diag::err_template_arg_not_object_or_func) |
| 2601 | << Arg->getSourceRange(); |
| 2602 | S.Diag(DRE->getDecl()->getLocation(), diag::note_template_arg_refers_here); |
| 2603 | return true; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2604 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2605 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2606 | if (ParamType->isPointerType() && |
| 2607 | !ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType() && |
| 2608 | S.IsQualificationConversion(ArgType, ParamType)) { |
| 2609 | // For pointer-to-object types, qualification conversions are |
| 2610 | // permitted. |
| 2611 | } else { |
| 2612 | if (const ReferenceType *ParamRef = ParamType->getAs<ReferenceType>()) { |
| 2613 | if (!ParamRef->getPointeeType()->isFunctionType()) { |
| 2614 | // C++ [temp.arg.nontype]p5b3: |
| 2615 | // For a non-type template-parameter of type reference to |
| 2616 | // object, no conversions apply. The type referred to by the |
| 2617 | // reference may be more cv-qualified than the (otherwise |
| 2618 | // identical) type of the template- argument. The |
| 2619 | // template-parameter is bound directly to the |
| 2620 | // template-argument, which shall be an lvalue. |
| 2621 | |
| 2622 | // FIXME: Other qualifiers? |
| 2623 | unsigned ParamQuals = ParamRef->getPointeeType().getCVRQualifiers(); |
| 2624 | unsigned ArgQuals = ArgType.getCVRQualifiers(); |
| 2625 | |
| 2626 | if ((ParamQuals | ArgQuals) != ParamQuals) { |
| 2627 | S.Diag(Arg->getSourceRange().getBegin(), |
| 2628 | diag::err_template_arg_ref_bind_ignores_quals) |
| 2629 | << ParamType << Arg->getType() |
| 2630 | << Arg->getSourceRange(); |
| 2631 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 2632 | return true; |
| 2633 | } |
| 2634 | } |
| 2635 | } |
| 2636 | |
| 2637 | // At this point, the template argument refers to an object or |
| 2638 | // function with external linkage. We now need to check whether the |
| 2639 | // argument and parameter types are compatible. |
| 2640 | if (!S.Context.hasSameUnqualifiedType(ArgType, |
| 2641 | ParamType.getNonReferenceType())) { |
| 2642 | // We can't perform this conversion or binding. |
| 2643 | if (ParamType->isReferenceType()) |
| 2644 | S.Diag(Arg->getLocStart(), diag::err_template_arg_no_ref_bind) |
| 2645 | << ParamType << Arg->getType() << Arg->getSourceRange(); |
| 2646 | else |
| 2647 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_convertible) |
| 2648 | << Arg->getType() << ParamType << Arg->getSourceRange(); |
| 2649 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 2650 | return true; |
| 2651 | } |
| 2652 | } |
| 2653 | |
| 2654 | // Create the template argument. |
| 2655 | Converted = TemplateArgument(Entity->getCanonicalDecl()); |
Douglas Gregor | 77c13e0 | 2010-04-24 18:20:53 +0000 | [diff] [blame] | 2656 | S.MarkDeclarationReferenced(Arg->getLocStart(), Entity); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2657 | return false; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2658 | } |
| 2659 | |
| 2660 | /// \brief Checks whether the given template argument is a pointer to |
| 2661 | /// member constant according to C++ [temp.arg.nontype]p1. |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 2662 | bool Sema::CheckTemplateArgumentPointerToMember(Expr *Arg, |
| 2663 | TemplateArgument &Converted) { |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2664 | bool Invalid = false; |
| 2665 | |
| 2666 | // See through any implicit casts we added to fix the type. |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 2667 | while (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(Arg)) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2668 | Arg = Cast->getSubExpr(); |
| 2669 | |
| 2670 | // C++ [temp.arg.nontype]p1: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2671 | // |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2672 | // A template-argument for a non-type, non-template |
| 2673 | // template-parameter shall be one of: [...] |
| 2674 | // |
| 2675 | // -- a pointer to member expressed as described in 5.3.1. |
Douglas Gregor | a2813ce | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 2676 | DeclRefExpr *DRE = 0; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2677 | |
| 2678 | // Ignore (and complain about) any excess parentheses. |
| 2679 | while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) { |
| 2680 | if (!Invalid) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2681 | Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2682 | diag::err_template_arg_extra_parens) |
| 2683 | << Arg->getSourceRange(); |
| 2684 | Invalid = true; |
| 2685 | } |
| 2686 | |
| 2687 | Arg = Parens->getSubExpr(); |
| 2688 | } |
| 2689 | |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 2690 | // A pointer-to-member constant written &Class::member. |
| 2691 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) { |
Douglas Gregor | a2813ce | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 2692 | if (UnOp->getOpcode() == UnaryOperator::AddrOf) { |
| 2693 | DRE = dyn_cast<DeclRefExpr>(UnOp->getSubExpr()); |
| 2694 | if (DRE && !DRE->getQualifier()) |
| 2695 | DRE = 0; |
| 2696 | } |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 2697 | } |
| 2698 | // A constant of pointer-to-member type. |
| 2699 | else if ((DRE = dyn_cast<DeclRefExpr>(Arg))) { |
| 2700 | if (ValueDecl *VD = dyn_cast<ValueDecl>(DRE->getDecl())) { |
| 2701 | if (VD->getType()->isMemberPointerType()) { |
| 2702 | if (isa<NonTypeTemplateParmDecl>(VD) || |
| 2703 | (isa<VarDecl>(VD) && |
| 2704 | Context.getCanonicalType(VD->getType()).isConstQualified())) { |
| 2705 | if (Arg->isTypeDependent() || Arg->isValueDependent()) |
| 2706 | Converted = TemplateArgument(Arg->Retain()); |
| 2707 | else |
| 2708 | Converted = TemplateArgument(VD->getCanonicalDecl()); |
| 2709 | return Invalid; |
| 2710 | } |
| 2711 | } |
| 2712 | } |
| 2713 | |
| 2714 | DRE = 0; |
| 2715 | } |
| 2716 | |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2717 | if (!DRE) |
| 2718 | return Diag(Arg->getSourceRange().getBegin(), |
| 2719 | diag::err_template_arg_not_pointer_to_member_form) |
| 2720 | << Arg->getSourceRange(); |
| 2721 | |
| 2722 | if (isa<FieldDecl>(DRE->getDecl()) || isa<CXXMethodDecl>(DRE->getDecl())) { |
| 2723 | assert((isa<FieldDecl>(DRE->getDecl()) || |
| 2724 | !cast<CXXMethodDecl>(DRE->getDecl())->isStatic()) && |
| 2725 | "Only non-static member pointers can make it here"); |
| 2726 | |
| 2727 | // Okay: this is the address of a non-static member, and therefore |
| 2728 | // a member pointer constant. |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 2729 | if (Arg->isTypeDependent() || Arg->isValueDependent()) |
| 2730 | Converted = TemplateArgument(Arg->Retain()); |
| 2731 | else |
| 2732 | Converted = TemplateArgument(DRE->getDecl()->getCanonicalDecl()); |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2733 | return Invalid; |
| 2734 | } |
| 2735 | |
| 2736 | // 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] | 2737 | Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2738 | diag::err_template_arg_not_pointer_to_member_form) |
| 2739 | << Arg->getSourceRange(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2740 | Diag(DRE->getDecl()->getLocation(), |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2741 | diag::note_template_arg_refers_here); |
| 2742 | return true; |
| 2743 | } |
| 2744 | |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2745 | /// \brief Check a template argument against its corresponding |
| 2746 | /// non-type template parameter. |
| 2747 | /// |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 2748 | /// This routine implements the semantics of C++ [temp.arg.nontype]. |
| 2749 | /// It returns true if an error occurred, and false otherwise. \p |
| 2750 | /// InstantiatedParamType is the type of the non-type template |
| 2751 | /// parameter after it has been instantiated. |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 2752 | /// |
Douglas Gregor | 02cbbd2 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 2753 | /// If no error was detected, Converted receives the converted template argument. |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2754 | bool Sema::CheckTemplateArgument(NonTypeTemplateParmDecl *Param, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2755 | QualType InstantiatedParamType, Expr *&Arg, |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 2756 | TemplateArgument &Converted, |
| 2757 | CheckTemplateArgumentKind CTAK) { |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2758 | SourceLocation StartLoc = Arg->getSourceRange().getBegin(); |
| 2759 | |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 2760 | // If either the parameter has a dependent type or the argument is |
| 2761 | // type-dependent, there's nothing we can check now. |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2762 | if (InstantiatedParamType->isDependentType() || Arg->isTypeDependent()) { |
| 2763 | // FIXME: Produce a cloned, canonical expression? |
Douglas Gregor | 02cbbd2 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 2764 | Converted = TemplateArgument(Arg); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 2765 | return false; |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2766 | } |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 2767 | |
| 2768 | // C++ [temp.arg.nontype]p5: |
| 2769 | // The following conversions are performed on each expression used |
| 2770 | // as a non-type template-argument. If a non-type |
| 2771 | // template-argument cannot be converted to the type of the |
| 2772 | // corresponding template-parameter then the program is |
| 2773 | // ill-formed. |
| 2774 | // |
| 2775 | // -- for a non-type template-parameter of integral or |
| 2776 | // enumeration type, integral promotions (4.5) and integral |
| 2777 | // conversions (4.7) are applied. |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 2778 | QualType ParamType = InstantiatedParamType; |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 2779 | QualType ArgType = Arg->getType(); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 2780 | if (ParamType->isIntegralType() || ParamType->isEnumeralType()) { |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 2781 | // C++ [temp.arg.nontype]p1: |
| 2782 | // A template-argument for a non-type, non-template |
| 2783 | // template-parameter shall be one of: |
| 2784 | // |
| 2785 | // -- an integral constant-expression of integral or enumeration |
| 2786 | // type; or |
| 2787 | // -- the name of a non-type template-parameter; or |
| 2788 | SourceLocation NonConstantLoc; |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 2789 | llvm::APSInt Value; |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 2790 | if (!ArgType->isIntegralType() && !ArgType->isEnumeralType()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2791 | Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 2792 | diag::err_template_arg_not_integral_or_enumeral) |
| 2793 | << ArgType << Arg->getSourceRange(); |
| 2794 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 2795 | return true; |
| 2796 | } else if (!Arg->isValueDependent() && |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 2797 | !Arg->isIntegerConstantExpr(Value, Context, &NonConstantLoc)) { |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 2798 | Diag(NonConstantLoc, diag::err_template_arg_not_ice) |
| 2799 | << ArgType << Arg->getSourceRange(); |
| 2800 | return true; |
| 2801 | } |
| 2802 | |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 2803 | // From here on out, all we care about are the unqualified forms |
| 2804 | // of the parameter and argument types. |
| 2805 | ParamType = ParamType.getUnqualifiedType(); |
| 2806 | ArgType = ArgType.getUnqualifiedType(); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 2807 | |
| 2808 | // Try to convert the argument to the parameter's type. |
Douglas Gregor | ff52439 | 2009-11-04 21:50:46 +0000 | [diff] [blame] | 2809 | if (Context.hasSameType(ParamType, ArgType)) { |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 2810 | // Okay: no conversion necessary |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 2811 | } else if (CTAK == CTAK_Deduced) { |
| 2812 | // C++ [temp.deduct.type]p17: |
| 2813 | // If, in the declaration of a function template with a non-type |
| 2814 | // template-parameter, the non-type template- parameter is used |
| 2815 | // in an expression in the function parameter-list and, if the |
| 2816 | // corresponding template-argument is deduced, the |
| 2817 | // template-argument type shall match the type of the |
| 2818 | // template-parameter exactly, except that a template-argument |
| 2819 | // deduced from an array bound may be of any integral type. |
| 2820 | Diag(StartLoc, diag::err_deduced_non_type_template_arg_type_mismatch) |
| 2821 | << ArgType << ParamType; |
| 2822 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 2823 | return true; |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 2824 | } else if (IsIntegralPromotion(Arg, ArgType, ParamType) || |
| 2825 | !ParamType->isEnumeralType()) { |
| 2826 | // This is an integral promotion or conversion. |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 2827 | ImpCastExprToType(Arg, ParamType, CastExpr::CK_IntegralCast); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 2828 | } else { |
| 2829 | // We can't perform this conversion. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2830 | Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 2831 | diag::err_template_arg_not_convertible) |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 2832 | << Arg->getType() << InstantiatedParamType << Arg->getSourceRange(); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 2833 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 2834 | return true; |
| 2835 | } |
| 2836 | |
Douglas Gregor | f80a9d5 | 2009-03-14 00:20:21 +0000 | [diff] [blame] | 2837 | QualType IntegerType = Context.getCanonicalType(ParamType); |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2838 | if (const EnumType *Enum = IntegerType->getAs<EnumType>()) |
Douglas Gregor | 02cbbd2 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 2839 | IntegerType = Context.getCanonicalType(Enum->getDecl()->getIntegerType()); |
Douglas Gregor | f80a9d5 | 2009-03-14 00:20:21 +0000 | [diff] [blame] | 2840 | |
| 2841 | if (!Arg->isValueDependent()) { |
Douglas Gregor | 1a6e034 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 2842 | llvm::APSInt OldValue = Value; |
| 2843 | |
| 2844 | // Coerce the template argument's value to the value it will have |
| 2845 | // based on the template parameter's type. |
Douglas Gregor | 0d4fd8e | 2010-03-26 00:39:40 +0000 | [diff] [blame] | 2846 | unsigned AllowedBits = Context.getTypeSize(IntegerType); |
Douglas Gregor | 0d4fd8e | 2010-03-26 00:39:40 +0000 | [diff] [blame] | 2847 | if (Value.getBitWidth() != AllowedBits) |
| 2848 | Value.extOrTrunc(AllowedBits); |
| 2849 | Value.setIsSigned(IntegerType->isSignedIntegerType()); |
Douglas Gregor | 1a6e034 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 2850 | |
| 2851 | // Complain if an unsigned parameter received a negative value. |
| 2852 | if (IntegerType->isUnsignedIntegerType() |
| 2853 | && (OldValue.isSigned() && OldValue.isNegative())) { |
| 2854 | Diag(Arg->getSourceRange().getBegin(), diag::warn_template_arg_negative) |
| 2855 | << OldValue.toString(10) << Value.toString(10) << Param->getType() |
| 2856 | << Arg->getSourceRange(); |
| 2857 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 2858 | } |
| 2859 | |
| 2860 | // Complain if we overflowed the template parameter's type. |
| 2861 | unsigned RequiredBits; |
| 2862 | if (IntegerType->isUnsignedIntegerType()) |
| 2863 | RequiredBits = OldValue.getActiveBits(); |
| 2864 | else if (OldValue.isUnsigned()) |
| 2865 | RequiredBits = OldValue.getActiveBits() + 1; |
| 2866 | else |
| 2867 | RequiredBits = OldValue.getMinSignedBits(); |
| 2868 | if (RequiredBits > AllowedBits) { |
| 2869 | Diag(Arg->getSourceRange().getBegin(), |
| 2870 | diag::warn_template_arg_too_large) |
| 2871 | << OldValue.toString(10) << Value.toString(10) << Param->getType() |
| 2872 | << Arg->getSourceRange(); |
| 2873 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 2874 | } |
Douglas Gregor | f80a9d5 | 2009-03-14 00:20:21 +0000 | [diff] [blame] | 2875 | } |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 2876 | |
Douglas Gregor | 02cbbd2 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 2877 | // Add the value of this argument to the list of converted |
| 2878 | // arguments. We use the bitwidth and signedness of the template |
| 2879 | // parameter. |
| 2880 | if (Arg->isValueDependent()) { |
| 2881 | // The argument is value-dependent. Create a new |
| 2882 | // TemplateArgument with the converted expression. |
| 2883 | Converted = TemplateArgument(Arg); |
| 2884 | return false; |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 2885 | } |
| 2886 | |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2887 | Converted = TemplateArgument(Value, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2888 | ParamType->isEnumeralType() ? ParamType |
Douglas Gregor | 02cbbd2 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 2889 | : IntegerType); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 2890 | return false; |
| 2891 | } |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 2892 | |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2893 | DeclAccessPair FoundResult; // temporary for ResolveOverloadedFunction |
| 2894 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2895 | // C++0x [temp.arg.nontype]p5 bullets 2, 4 and 6 permit conversion |
| 2896 | // from a template argument of type std::nullptr_t to a non-type |
| 2897 | // template parameter of type pointer to object, pointer to |
| 2898 | // function, or pointer-to-member, respectively. |
| 2899 | if (ArgType->isNullPtrType() && |
| 2900 | (ParamType->isPointerType() || ParamType->isMemberPointerType())) { |
| 2901 | Converted = TemplateArgument((NamedDecl *)0); |
| 2902 | return false; |
| 2903 | } |
| 2904 | |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 2905 | // Handle pointer-to-function, reference-to-function, and |
| 2906 | // pointer-to-member-function all in (roughly) the same way. |
| 2907 | if (// -- For a non-type template-parameter of type pointer to |
| 2908 | // function, only the function-to-pointer conversion (4.3) is |
| 2909 | // applied. If the template-argument represents a set of |
| 2910 | // overloaded functions (or a pointer to such), the matching |
| 2911 | // function is selected from the set (13.4). |
| 2912 | (ParamType->isPointerType() && |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2913 | ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType()) || |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 2914 | // -- For a non-type template-parameter of type reference to |
| 2915 | // function, no conversions apply. If the template-argument |
| 2916 | // represents a set of overloaded functions, the matching |
| 2917 | // function is selected from the set (13.4). |
| 2918 | (ParamType->isReferenceType() && |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2919 | ParamType->getAs<ReferenceType>()->getPointeeType()->isFunctionType()) || |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 2920 | // -- For a non-type template-parameter of type pointer to |
| 2921 | // member function, no conversions apply. If the |
| 2922 | // template-argument represents a set of overloaded member |
| 2923 | // functions, the matching member function is selected from |
| 2924 | // the set (13.4). |
| 2925 | (ParamType->isMemberPointerType() && |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2926 | ParamType->getAs<MemberPointerType>()->getPointeeType() |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 2927 | ->isFunctionType())) { |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2928 | |
Douglas Gregor | 1a8cf73 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 2929 | if (Arg->getType() == Context.OverloadTy) { |
| 2930 | if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg, ParamType, |
| 2931 | true, |
| 2932 | FoundResult)) { |
| 2933 | if (DiagnoseUseOfDecl(Fn, Arg->getSourceRange().getBegin())) |
| 2934 | return true; |
| 2935 | |
| 2936 | Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn); |
| 2937 | ArgType = Arg->getType(); |
| 2938 | } else |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 2939 | return true; |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 2940 | } |
Douglas Gregor | 1a8cf73 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 2941 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2942 | if (!ParamType->isMemberPointerType()) |
| 2943 | return CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param, |
| 2944 | ParamType, |
| 2945 | Arg, Converted); |
| 2946 | |
| 2947 | if (IsQualificationConversion(ArgType, ParamType.getNonReferenceType())) { |
| 2948 | ImpCastExprToType(Arg, ParamType, CastExpr::CK_NoOp, |
| 2949 | Arg->isLvalue(Context) == Expr::LV_Valid); |
| 2950 | } else if (!Context.hasSameUnqualifiedType(ArgType, |
| 2951 | ParamType.getNonReferenceType())) { |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 2952 | // We can't perform this conversion. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2953 | Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 2954 | diag::err_template_arg_not_convertible) |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 2955 | << Arg->getType() << InstantiatedParamType << Arg->getSourceRange(); |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 2956 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 2957 | return true; |
| 2958 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2959 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2960 | return CheckTemplateArgumentPointerToMember(Arg, Converted); |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 2961 | } |
| 2962 | |
Chris Lattner | fe90de7 | 2009-02-20 21:37:53 +0000 | [diff] [blame] | 2963 | if (ParamType->isPointerType()) { |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 2964 | // -- for a non-type template-parameter of type pointer to |
| 2965 | // object, qualification conversions (4.4) and the |
| 2966 | // array-to-pointer conversion (4.2) are applied. |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 2967 | // C++0x also allows a value of std::nullptr_t. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2968 | assert(ParamType->getAs<PointerType>()->getPointeeType()->isObjectType() && |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 2969 | "Only object pointers allowed here"); |
Douglas Gregor | f684e6e | 2009-02-11 00:44:29 +0000 | [diff] [blame] | 2970 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2971 | return CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param, |
| 2972 | ParamType, |
| 2973 | Arg, Converted); |
Douglas Gregor | f684e6e | 2009-02-11 00:44:29 +0000 | [diff] [blame] | 2974 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2975 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2976 | if (const ReferenceType *ParamRefType = ParamType->getAs<ReferenceType>()) { |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 2977 | // -- For a non-type template-parameter of type reference to |
| 2978 | // object, no conversions apply. The type referred to by the |
| 2979 | // reference may be more cv-qualified than the (otherwise |
| 2980 | // identical) type of the template-argument. The |
| 2981 | // template-parameter is bound directly to the |
| 2982 | // template-argument, which must be an lvalue. |
Douglas Gregor | bad0e65 | 2009-03-24 20:32:41 +0000 | [diff] [blame] | 2983 | assert(ParamRefType->getPointeeType()->isObjectType() && |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 2984 | "Only object references allowed here"); |
Douglas Gregor | f684e6e | 2009-02-11 00:44:29 +0000 | [diff] [blame] | 2985 | |
Douglas Gregor | 1a8cf73 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 2986 | if (Arg->getType() == Context.OverloadTy) { |
| 2987 | if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg, |
| 2988 | ParamRefType->getPointeeType(), |
| 2989 | true, |
| 2990 | FoundResult)) { |
| 2991 | if (DiagnoseUseOfDecl(Fn, Arg->getSourceRange().getBegin())) |
| 2992 | return true; |
| 2993 | |
| 2994 | Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn); |
| 2995 | ArgType = Arg->getType(); |
| 2996 | } else |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2997 | return true; |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 2998 | } |
Douglas Gregor | 1a8cf73 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 2999 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3000 | return CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param, |
| 3001 | ParamType, |
| 3002 | Arg, Converted); |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 3003 | } |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 3004 | |
| 3005 | // -- For a non-type template-parameter of type pointer to data |
| 3006 | // member, qualification conversions (4.4) are applied. |
| 3007 | assert(ParamType->isMemberPointerType() && "Only pointers to members remain"); |
| 3008 | |
Douglas Gregor | 8e6563b | 2009-02-11 18:22:40 +0000 | [diff] [blame] | 3009 | if (Context.hasSameUnqualifiedType(ParamType, ArgType)) { |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 3010 | // Types match exactly: nothing more to do here. |
| 3011 | } else if (IsQualificationConversion(ArgType, ParamType)) { |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3012 | ImpCastExprToType(Arg, ParamType, CastExpr::CK_NoOp, |
| 3013 | Arg->isLvalue(Context) == Expr::LV_Valid); |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 3014 | } else { |
| 3015 | // We can't perform this conversion. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3016 | Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 3017 | diag::err_template_arg_not_convertible) |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 3018 | << Arg->getType() << InstantiatedParamType << Arg->getSourceRange(); |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 3019 | Diag(Param->getLocation(), diag::note_template_param_here); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3020 | return true; |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 3021 | } |
| 3022 | |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 3023 | return CheckTemplateArgumentPointerToMember(Arg, Converted); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3024 | } |
| 3025 | |
| 3026 | /// \brief Check a template argument against its corresponding |
| 3027 | /// template template parameter. |
| 3028 | /// |
| 3029 | /// This routine implements the semantics of C++ [temp.arg.template]. |
| 3030 | /// It returns true if an error occurred, and false otherwise. |
| 3031 | bool Sema::CheckTemplateArgument(TemplateTemplateParmDecl *Param, |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 3032 | const TemplateArgumentLoc &Arg) { |
| 3033 | TemplateName Name = Arg.getArgument().getAsTemplate(); |
| 3034 | TemplateDecl *Template = Name.getAsTemplateDecl(); |
| 3035 | if (!Template) { |
| 3036 | // Any dependent template name is fine. |
| 3037 | assert(Name.isDependent() && "Non-dependent template isn't a declaration?"); |
| 3038 | return false; |
| 3039 | } |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 3040 | |
| 3041 | // C++ [temp.arg.template]p1: |
| 3042 | // A template-argument for a template template-parameter shall be |
| 3043 | // the name of a class template, expressed as id-expression. Only |
| 3044 | // primary class templates are considered when matching the |
| 3045 | // template template argument with the corresponding parameter; |
| 3046 | // partial specializations are not considered even if their |
| 3047 | // parameter lists match that of the template template parameter. |
Douglas Gregor | ba1ecb5 | 2009-06-12 19:43:02 +0000 | [diff] [blame] | 3048 | // |
| 3049 | // Note that we also allow template template parameters here, which |
| 3050 | // will happen when we are dealing with, e.g., class template |
| 3051 | // partial specializations. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3052 | if (!isa<ClassTemplateDecl>(Template) && |
Douglas Gregor | ba1ecb5 | 2009-06-12 19:43:02 +0000 | [diff] [blame] | 3053 | !isa<TemplateTemplateParmDecl>(Template)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3054 | assert(isa<FunctionTemplateDecl>(Template) && |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 3055 | "Only function templates are possible here"); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 3056 | Diag(Arg.getLocation(), diag::err_template_arg_not_class_template); |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3057 | Diag(Template->getLocation(), diag::note_template_arg_refers_here_func) |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 3058 | << Template; |
| 3059 | } |
| 3060 | |
| 3061 | return !TemplateParameterListsAreEqual(Template->getTemplateParameters(), |
| 3062 | Param->getTemplateParameters(), |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 3063 | true, |
| 3064 | TPL_TemplateTemplateArgumentMatch, |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 3065 | Arg.getLocation()); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3066 | } |
| 3067 | |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3068 | /// \brief Given a non-type template argument that refers to a |
| 3069 | /// declaration and the type of its corresponding non-type template |
| 3070 | /// parameter, produce an expression that properly refers to that |
| 3071 | /// declaration. |
| 3072 | Sema::OwningExprResult |
| 3073 | Sema::BuildExpressionFromDeclTemplateArgument(const TemplateArgument &Arg, |
| 3074 | QualType ParamType, |
| 3075 | SourceLocation Loc) { |
| 3076 | assert(Arg.getKind() == TemplateArgument::Declaration && |
| 3077 | "Only declaration template arguments permitted here"); |
| 3078 | ValueDecl *VD = cast<ValueDecl>(Arg.getAsDecl()); |
| 3079 | |
| 3080 | if (VD->getDeclContext()->isRecord() && |
| 3081 | (isa<CXXMethodDecl>(VD) || isa<FieldDecl>(VD))) { |
| 3082 | // If the value is a class member, we might have a pointer-to-member. |
| 3083 | // Determine whether the non-type template template parameter is of |
| 3084 | // pointer-to-member type. If so, we need to build an appropriate |
| 3085 | // expression for a pointer-to-member, since a "normal" DeclRefExpr |
| 3086 | // would refer to the member itself. |
| 3087 | if (ParamType->isMemberPointerType()) { |
| 3088 | QualType ClassType |
| 3089 | = Context.getTypeDeclType(cast<RecordDecl>(VD->getDeclContext())); |
| 3090 | NestedNameSpecifier *Qualifier |
| 3091 | = NestedNameSpecifier::Create(Context, 0, false, ClassType.getTypePtr()); |
| 3092 | CXXScopeSpec SS; |
| 3093 | SS.setScopeRep(Qualifier); |
| 3094 | OwningExprResult RefExpr = BuildDeclRefExpr(VD, |
| 3095 | VD->getType().getNonReferenceType(), |
| 3096 | Loc, |
| 3097 | &SS); |
| 3098 | if (RefExpr.isInvalid()) |
| 3099 | return ExprError(); |
| 3100 | |
| 3101 | RefExpr = CreateBuiltinUnaryOp(Loc, UnaryOperator::AddrOf, move(RefExpr)); |
Douglas Gregor | c0c8300 | 2010-04-30 21:46:38 +0000 | [diff] [blame] | 3102 | |
| 3103 | // We might need to perform a trailing qualification conversion, since |
| 3104 | // the element type on the parameter could be more qualified than the |
| 3105 | // element type in the expression we constructed. |
| 3106 | if (IsQualificationConversion(((Expr*) RefExpr.get())->getType(), |
| 3107 | ParamType.getUnqualifiedType())) { |
| 3108 | Expr *RefE = RefExpr.takeAs<Expr>(); |
| 3109 | ImpCastExprToType(RefE, ParamType.getUnqualifiedType(), |
| 3110 | CastExpr::CK_NoOp); |
| 3111 | RefExpr = Owned(RefE); |
| 3112 | } |
| 3113 | |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3114 | assert(!RefExpr.isInvalid() && |
| 3115 | Context.hasSameType(((Expr*) RefExpr.get())->getType(), |
Douglas Gregor | c0c8300 | 2010-04-30 21:46:38 +0000 | [diff] [blame] | 3116 | ParamType.getUnqualifiedType())); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3117 | return move(RefExpr); |
| 3118 | } |
| 3119 | } |
| 3120 | |
| 3121 | QualType T = VD->getType().getNonReferenceType(); |
| 3122 | if (ParamType->isPointerType()) { |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3123 | // When the non-type template parameter is a pointer, take the |
| 3124 | // address of the declaration. |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3125 | OwningExprResult RefExpr = BuildDeclRefExpr(VD, T, Loc); |
| 3126 | if (RefExpr.isInvalid()) |
| 3127 | return ExprError(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3128 | |
| 3129 | if (T->isFunctionType() || T->isArrayType()) { |
| 3130 | // Decay functions and arrays. |
| 3131 | Expr *RefE = (Expr *)RefExpr.get(); |
| 3132 | DefaultFunctionArrayConversion(RefE); |
| 3133 | if (RefE != RefExpr.get()) { |
| 3134 | RefExpr.release(); |
| 3135 | RefExpr = Owned(RefE); |
| 3136 | } |
| 3137 | |
| 3138 | return move(RefExpr); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3139 | } |
| 3140 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3141 | // Take the address of everything else |
| 3142 | return CreateBuiltinUnaryOp(Loc, UnaryOperator::AddrOf, move(RefExpr)); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3143 | } |
| 3144 | |
| 3145 | // If the non-type template parameter has reference type, qualify the |
| 3146 | // resulting declaration reference with the extra qualifiers on the |
| 3147 | // type that the reference refers to. |
| 3148 | if (const ReferenceType *TargetRef = ParamType->getAs<ReferenceType>()) |
| 3149 | T = Context.getQualifiedType(T, TargetRef->getPointeeType().getQualifiers()); |
| 3150 | |
| 3151 | return BuildDeclRefExpr(VD, T, Loc); |
| 3152 | } |
| 3153 | |
| 3154 | /// \brief Construct a new expression that refers to the given |
| 3155 | /// integral template argument with the given source-location |
| 3156 | /// information. |
| 3157 | /// |
| 3158 | /// This routine takes care of the mapping from an integral template |
| 3159 | /// argument (which may have any integral type) to the appropriate |
| 3160 | /// literal value. |
| 3161 | Sema::OwningExprResult |
| 3162 | Sema::BuildExpressionFromIntegralTemplateArgument(const TemplateArgument &Arg, |
| 3163 | SourceLocation Loc) { |
| 3164 | assert(Arg.getKind() == TemplateArgument::Integral && |
| 3165 | "Operation is only value for integral template arguments"); |
| 3166 | QualType T = Arg.getIntegralType(); |
| 3167 | if (T->isCharType() || T->isWideCharType()) |
| 3168 | return Owned(new (Context) CharacterLiteral( |
| 3169 | Arg.getAsIntegral()->getZExtValue(), |
| 3170 | T->isWideCharType(), |
| 3171 | T, |
| 3172 | Loc)); |
| 3173 | if (T->isBooleanType()) |
| 3174 | return Owned(new (Context) CXXBoolLiteralExpr( |
| 3175 | Arg.getAsIntegral()->getBoolValue(), |
| 3176 | T, |
| 3177 | Loc)); |
| 3178 | |
| 3179 | return Owned(new (Context) IntegerLiteral(*Arg.getAsIntegral(), T, Loc)); |
| 3180 | } |
| 3181 | |
| 3182 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3183 | /// \brief Determine whether the given template parameter lists are |
| 3184 | /// equivalent. |
| 3185 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3186 | /// \param New The new template parameter list, typically written in the |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3187 | /// source code as part of a new template declaration. |
| 3188 | /// |
| 3189 | /// \param Old The old template parameter list, typically found via |
| 3190 | /// name lookup of the template declared with this template parameter |
| 3191 | /// list. |
| 3192 | /// |
| 3193 | /// \param Complain If true, this routine will produce a diagnostic if |
| 3194 | /// the template parameter lists are not equivalent. |
| 3195 | /// |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 3196 | /// \param Kind describes how we are to match the template parameter lists. |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 3197 | /// |
| 3198 | /// \param TemplateArgLoc If this source location is valid, then we |
| 3199 | /// are actually checking the template parameter list of a template |
| 3200 | /// argument (New) against the template parameter list of its |
| 3201 | /// corresponding template template parameter (Old). We produce |
| 3202 | /// slightly different diagnostics in this scenario. |
| 3203 | /// |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3204 | /// \returns True if the template parameter lists are equal, false |
| 3205 | /// otherwise. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3206 | bool |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3207 | Sema::TemplateParameterListsAreEqual(TemplateParameterList *New, |
| 3208 | TemplateParameterList *Old, |
| 3209 | bool Complain, |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 3210 | TemplateParameterListEqualKind Kind, |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 3211 | SourceLocation TemplateArgLoc) { |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3212 | if (Old->size() != New->size()) { |
| 3213 | if (Complain) { |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 3214 | unsigned NextDiag = diag::err_template_param_list_different_arity; |
| 3215 | if (TemplateArgLoc.isValid()) { |
| 3216 | Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch); |
| 3217 | NextDiag = diag::note_template_param_list_different_arity; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3218 | } |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 3219 | Diag(New->getTemplateLoc(), NextDiag) |
| 3220 | << (New->size() > Old->size()) |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 3221 | << (Kind != TPL_TemplateMatch) |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 3222 | << SourceRange(New->getTemplateLoc(), New->getRAngleLoc()); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3223 | Diag(Old->getTemplateLoc(), diag::note_template_prev_declaration) |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 3224 | << (Kind != TPL_TemplateMatch) |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3225 | << SourceRange(Old->getTemplateLoc(), Old->getRAngleLoc()); |
| 3226 | } |
| 3227 | |
| 3228 | return false; |
| 3229 | } |
| 3230 | |
| 3231 | for (TemplateParameterList::iterator OldParm = Old->begin(), |
| 3232 | OldParmEnd = Old->end(), NewParm = New->begin(); |
| 3233 | OldParm != OldParmEnd; ++OldParm, ++NewParm) { |
| 3234 | if ((*OldParm)->getKind() != (*NewParm)->getKind()) { |
Douglas Gregor | 34d1dc9 | 2009-06-24 16:50:40 +0000 | [diff] [blame] | 3235 | if (Complain) { |
| 3236 | unsigned NextDiag = diag::err_template_param_different_kind; |
| 3237 | if (TemplateArgLoc.isValid()) { |
| 3238 | Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch); |
| 3239 | NextDiag = diag::note_template_param_different_kind; |
| 3240 | } |
| 3241 | Diag((*NewParm)->getLocation(), NextDiag) |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 3242 | << (Kind != TPL_TemplateMatch); |
Douglas Gregor | 34d1dc9 | 2009-06-24 16:50:40 +0000 | [diff] [blame] | 3243 | Diag((*OldParm)->getLocation(), diag::note_template_prev_declaration) |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 3244 | << (Kind != TPL_TemplateMatch); |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 3245 | } |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3246 | return false; |
| 3247 | } |
| 3248 | |
Douglas Gregor | a417b87 | 2010-06-04 08:34:32 +0000 | [diff] [blame] | 3249 | if (TemplateTypeParmDecl *OldTTP |
| 3250 | = dyn_cast<TemplateTypeParmDecl>(*OldParm)) { |
| 3251 | // Template type parameters are equivalent if either both are template |
| 3252 | // type parameter packs or neither are (since we know we're at the same |
| 3253 | // index). |
| 3254 | TemplateTypeParmDecl *NewTTP = cast<TemplateTypeParmDecl>(*NewParm); |
| 3255 | if (OldTTP->isParameterPack() != NewTTP->isParameterPack()) { |
| 3256 | // FIXME: Implement the rules in C++0x [temp.arg.template]p5 that |
| 3257 | // allow one to match a template parameter pack in the template |
| 3258 | // parameter list of a template template parameter to one or more |
| 3259 | // template parameters in the template parameter list of the |
| 3260 | // corresponding template template argument. |
| 3261 | if (Complain) { |
| 3262 | unsigned NextDiag = diag::err_template_parameter_pack_non_pack; |
| 3263 | if (TemplateArgLoc.isValid()) { |
| 3264 | Diag(TemplateArgLoc, |
| 3265 | diag::err_template_arg_template_params_mismatch); |
| 3266 | NextDiag = diag::note_template_parameter_pack_non_pack; |
| 3267 | } |
| 3268 | Diag(NewTTP->getLocation(), NextDiag) |
| 3269 | << 0 << NewTTP->isParameterPack(); |
| 3270 | Diag(OldTTP->getLocation(), diag::note_template_parameter_pack_here) |
| 3271 | << 0 << OldTTP->isParameterPack(); |
| 3272 | } |
| 3273 | return false; |
| 3274 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3275 | } else if (NonTypeTemplateParmDecl *OldNTTP |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3276 | = dyn_cast<NonTypeTemplateParmDecl>(*OldParm)) { |
| 3277 | // The types of non-type template parameters must agree. |
| 3278 | NonTypeTemplateParmDecl *NewNTTP |
| 3279 | = cast<NonTypeTemplateParmDecl>(*NewParm); |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 3280 | |
| 3281 | // If we are matching a template template argument to a template |
| 3282 | // template parameter and one of the non-type template parameter types |
| 3283 | // is dependent, then we must wait until template instantiation time |
| 3284 | // to actually compare the arguments. |
| 3285 | if (Kind == TPL_TemplateTemplateArgumentMatch && |
| 3286 | (OldNTTP->getType()->isDependentType() || |
| 3287 | NewNTTP->getType()->isDependentType())) |
| 3288 | continue; |
| 3289 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3290 | if (Context.getCanonicalType(OldNTTP->getType()) != |
| 3291 | Context.getCanonicalType(NewNTTP->getType())) { |
| 3292 | if (Complain) { |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 3293 | unsigned NextDiag = diag::err_template_nontype_parm_different_type; |
| 3294 | if (TemplateArgLoc.isValid()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3295 | Diag(TemplateArgLoc, |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 3296 | diag::err_template_arg_template_params_mismatch); |
| 3297 | NextDiag = diag::note_template_nontype_parm_different_type; |
| 3298 | } |
| 3299 | Diag(NewNTTP->getLocation(), NextDiag) |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3300 | << NewNTTP->getType() |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 3301 | << (Kind != TPL_TemplateMatch); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3302 | Diag(OldNTTP->getLocation(), |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3303 | diag::note_template_nontype_parm_prev_declaration) |
| 3304 | << OldNTTP->getType(); |
| 3305 | } |
| 3306 | return false; |
| 3307 | } |
| 3308 | } else { |
| 3309 | // The template parameter lists of template template |
| 3310 | // parameters must agree. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3311 | assert(isa<TemplateTemplateParmDecl>(*OldParm) && |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3312 | "Only template template parameters handled here"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3313 | TemplateTemplateParmDecl *OldTTP |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3314 | = cast<TemplateTemplateParmDecl>(*OldParm); |
| 3315 | TemplateTemplateParmDecl *NewTTP |
| 3316 | = cast<TemplateTemplateParmDecl>(*NewParm); |
| 3317 | if (!TemplateParameterListsAreEqual(NewTTP->getTemplateParameters(), |
| 3318 | OldTTP->getTemplateParameters(), |
| 3319 | Complain, |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 3320 | (Kind == TPL_TemplateMatch? TPL_TemplateTemplateParmMatch : Kind), |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 3321 | TemplateArgLoc)) |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3322 | return false; |
| 3323 | } |
| 3324 | } |
| 3325 | |
| 3326 | return true; |
| 3327 | } |
| 3328 | |
| 3329 | /// \brief Check whether a template can be declared within this scope. |
| 3330 | /// |
| 3331 | /// If the template declaration is valid in this scope, returns |
| 3332 | /// false. Otherwise, issues a diagnostic and returns true. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3333 | bool |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 3334 | Sema::CheckTemplateDeclScope(Scope *S, TemplateParameterList *TemplateParams) { |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3335 | // Find the nearest enclosing declaration scope. |
| 3336 | while ((S->getFlags() & Scope::DeclScope) == 0 || |
| 3337 | (S->getFlags() & Scope::TemplateParamScope) != 0) |
| 3338 | S = S->getParent(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3339 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3340 | // C++ [temp]p2: |
| 3341 | // A template-declaration can appear only as a namespace scope or |
| 3342 | // class scope declaration. |
| 3343 | DeclContext *Ctx = static_cast<DeclContext *>(S->getEntity()); |
Eli Friedman | 1503f77 | 2009-07-31 01:43:05 +0000 | [diff] [blame] | 3344 | if (Ctx && isa<LinkageSpecDecl>(Ctx) && |
| 3345 | cast<LinkageSpecDecl>(Ctx)->getLanguage() != LinkageSpecDecl::lang_cxx) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3346 | return Diag(TemplateParams->getTemplateLoc(), diag::err_template_linkage) |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 3347 | << TemplateParams->getSourceRange(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3348 | |
Eli Friedman | 1503f77 | 2009-07-31 01:43:05 +0000 | [diff] [blame] | 3349 | while (Ctx && isa<LinkageSpecDecl>(Ctx)) |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3350 | Ctx = Ctx->getParent(); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3351 | |
| 3352 | if (Ctx && (Ctx->isFileContext() || Ctx->isRecord())) |
| 3353 | return false; |
| 3354 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3355 | return Diag(TemplateParams->getTemplateLoc(), |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 3356 | diag::err_template_outside_namespace_or_class_scope) |
| 3357 | << TemplateParams->getSourceRange(); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3358 | } |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3359 | |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3360 | /// \brief Determine what kind of template specialization the given declaration |
| 3361 | /// is. |
| 3362 | static TemplateSpecializationKind getTemplateSpecializationKind(NamedDecl *D) { |
| 3363 | if (!D) |
| 3364 | return TSK_Undeclared; |
| 3365 | |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 3366 | if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) |
| 3367 | return Record->getTemplateSpecializationKind(); |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3368 | if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) |
| 3369 | return Function->getTemplateSpecializationKind(); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 3370 | if (VarDecl *Var = dyn_cast<VarDecl>(D)) |
| 3371 | return Var->getTemplateSpecializationKind(); |
| 3372 | |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3373 | return TSK_Undeclared; |
| 3374 | } |
| 3375 | |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 3376 | /// \brief Check whether a specialization is well-formed in the current |
| 3377 | /// context. |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 3378 | /// |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 3379 | /// This routine determines whether a template specialization can be declared |
| 3380 | /// in the current context (C++ [temp.expl.spec]p2). |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3381 | /// |
| 3382 | /// \param S the semantic analysis object for which this check is being |
| 3383 | /// performed. |
| 3384 | /// |
| 3385 | /// \param Specialized the entity being specialized or instantiated, which |
| 3386 | /// may be a kind of template (class template, function template, etc.) or |
| 3387 | /// a member of a class template (member function, static data member, |
| 3388 | /// member class). |
| 3389 | /// |
| 3390 | /// \param PrevDecl the previous declaration of this entity, if any. |
| 3391 | /// |
| 3392 | /// \param Loc the location of the explicit specialization or instantiation of |
| 3393 | /// this entity. |
| 3394 | /// |
| 3395 | /// \param IsPartialSpecialization whether this is a partial specialization of |
| 3396 | /// a class template. |
| 3397 | /// |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3398 | /// \returns true if there was an error that we cannot recover from, false |
| 3399 | /// otherwise. |
| 3400 | static bool CheckTemplateSpecializationScope(Sema &S, |
| 3401 | NamedDecl *Specialized, |
| 3402 | NamedDecl *PrevDecl, |
| 3403 | SourceLocation Loc, |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 3404 | bool IsPartialSpecialization) { |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3405 | // Keep these "kind" numbers in sync with the %select statements in the |
| 3406 | // various diagnostics emitted by this routine. |
| 3407 | int EntityKind = 0; |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 3408 | bool isTemplateSpecialization = false; |
| 3409 | if (isa<ClassTemplateDecl>(Specialized)) { |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3410 | EntityKind = IsPartialSpecialization? 1 : 0; |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 3411 | isTemplateSpecialization = true; |
| 3412 | } else if (isa<FunctionTemplateDecl>(Specialized)) { |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3413 | EntityKind = 2; |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 3414 | isTemplateSpecialization = true; |
| 3415 | } else if (isa<CXXMethodDecl>(Specialized)) |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3416 | EntityKind = 3; |
| 3417 | else if (isa<VarDecl>(Specialized)) |
| 3418 | EntityKind = 4; |
| 3419 | else if (isa<RecordDecl>(Specialized)) |
| 3420 | EntityKind = 5; |
| 3421 | else { |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 3422 | S.Diag(Loc, diag::err_template_spec_unknown_kind); |
| 3423 | S.Diag(Specialized->getLocation(), diag::note_specialized_entity); |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3424 | return true; |
| 3425 | } |
| 3426 | |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 3427 | // C++ [temp.expl.spec]p2: |
| 3428 | // An explicit specialization shall be declared in the namespace |
| 3429 | // of which the template is a member, or, for member templates, in |
| 3430 | // the namespace of which the enclosing class or enclosing class |
| 3431 | // template is a member. An explicit specialization of a member |
| 3432 | // function, member class or static data member of a class |
| 3433 | // template shall be declared in the namespace of which the class |
| 3434 | // template is a member. Such a declaration may also be a |
| 3435 | // definition. If the declaration is not a definition, the |
| 3436 | // specialization may be defined later in the name- space in which |
| 3437 | // the explicit specialization was declared, or in a namespace |
| 3438 | // that encloses the one in which the explicit specialization was |
| 3439 | // declared. |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3440 | if (S.CurContext->getLookupContext()->isFunctionOrMethod()) { |
| 3441 | S.Diag(Loc, diag::err_template_spec_decl_function_scope) |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 3442 | << Specialized; |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 3443 | return true; |
| 3444 | } |
Douglas Gregor | 7974c3b | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 3445 | |
Douglas Gregor | 0a40747 | 2009-10-07 17:30:37 +0000 | [diff] [blame] | 3446 | if (S.CurContext->isRecord() && !IsPartialSpecialization) { |
| 3447 | S.Diag(Loc, diag::err_template_spec_decl_class_scope) |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 3448 | << Specialized; |
Douglas Gregor | 0a40747 | 2009-10-07 17:30:37 +0000 | [diff] [blame] | 3449 | return true; |
| 3450 | } |
| 3451 | |
Douglas Gregor | 7974c3b | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 3452 | // C++ [temp.class.spec]p6: |
| 3453 | // A class template partial specialization may be declared or redeclared |
| 3454 | // in any namespace scope in which its definition may be defined (14.5.1 |
| 3455 | // and 14.5.2). |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3456 | bool ComplainedAboutScope = false; |
Douglas Gregor | 7974c3b | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 3457 | DeclContext *SpecializedContext |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3458 | = Specialized->getDeclContext()->getEnclosingNamespaceContext(); |
Douglas Gregor | 7974c3b | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 3459 | DeclContext *DC = S.CurContext->getEnclosingNamespaceContext(); |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 3460 | if ((!PrevDecl || |
| 3461 | getTemplateSpecializationKind(PrevDecl) == TSK_Undeclared || |
| 3462 | getTemplateSpecializationKind(PrevDecl) == TSK_ImplicitInstantiation)){ |
| 3463 | // There is no prior declaration of this entity, so this |
| 3464 | // specialization must be in the same context as the template |
| 3465 | // itself. |
| 3466 | if (!DC->Equals(SpecializedContext)) { |
| 3467 | if (isa<TranslationUnitDecl>(SpecializedContext)) |
| 3468 | S.Diag(Loc, diag::err_template_spec_decl_out_of_scope_global) |
| 3469 | << EntityKind << Specialized; |
| 3470 | else if (isa<NamespaceDecl>(SpecializedContext)) |
| 3471 | S.Diag(Loc, diag::err_template_spec_decl_out_of_scope) |
| 3472 | << EntityKind << Specialized |
| 3473 | << cast<NamedDecl>(SpecializedContext); |
| 3474 | |
| 3475 | S.Diag(Specialized->getLocation(), diag::note_specialized_entity); |
| 3476 | ComplainedAboutScope = true; |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 3477 | } |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 3478 | } |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3479 | |
| 3480 | // Make sure that this redeclaration (or definition) occurs in an enclosing |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 3481 | // namespace. |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3482 | // Note that HandleDeclarator() performs this check for explicit |
| 3483 | // specializations of function templates, static data members, and member |
| 3484 | // functions, so we skip the check here for those kinds of entities. |
| 3485 | // FIXME: HandleDeclarator's diagnostics aren't quite as good, though. |
Douglas Gregor | 7974c3b | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 3486 | // Should we refactor that check, so that it occurs later? |
| 3487 | if (!ComplainedAboutScope && !DC->Encloses(SpecializedContext) && |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 3488 | !(isa<FunctionTemplateDecl>(Specialized) || isa<VarDecl>(Specialized) || |
| 3489 | isa<FunctionDecl>(Specialized))) { |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3490 | if (isa<TranslationUnitDecl>(SpecializedContext)) |
| 3491 | S.Diag(Loc, diag::err_template_spec_redecl_global_scope) |
| 3492 | << EntityKind << Specialized; |
| 3493 | else if (isa<NamespaceDecl>(SpecializedContext)) |
| 3494 | S.Diag(Loc, diag::err_template_spec_redecl_out_of_scope) |
| 3495 | << EntityKind << Specialized |
| 3496 | << cast<NamedDecl>(SpecializedContext); |
| 3497 | |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 3498 | S.Diag(Specialized->getLocation(), diag::note_specialized_entity); |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 3499 | } |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3500 | |
| 3501 | // FIXME: check for specialization-after-instantiation errors and such. |
| 3502 | |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 3503 | return false; |
| 3504 | } |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3505 | |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 3506 | /// \brief Check the non-type template arguments of a class template |
| 3507 | /// partial specialization according to C++ [temp.class.spec]p9. |
| 3508 | /// |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3509 | /// \param TemplateParams the template parameters of the primary class |
| 3510 | /// template. |
| 3511 | /// |
| 3512 | /// \param TemplateArg the template arguments of the class template |
| 3513 | /// partial specialization. |
| 3514 | /// |
| 3515 | /// \param MirrorsPrimaryTemplate will be set true if the class |
| 3516 | /// template partial specialization arguments are identical to the |
| 3517 | /// implicit template arguments of the primary template. This is not |
| 3518 | /// necessarily an error (C++0x), and it is left to the caller to diagnose |
| 3519 | /// this condition when it is an error. |
| 3520 | /// |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 3521 | /// \returns true if there was an error, false otherwise. |
| 3522 | bool Sema::CheckClassTemplatePartialSpecializationArgs( |
| 3523 | TemplateParameterList *TemplateParams, |
Anders Carlsson | 6360be7 | 2009-06-13 18:20:51 +0000 | [diff] [blame] | 3524 | const TemplateArgumentListBuilder &TemplateArgs, |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3525 | bool &MirrorsPrimaryTemplate) { |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 3526 | // FIXME: the interface to this function will have to change to |
| 3527 | // accommodate variadic templates. |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3528 | MirrorsPrimaryTemplate = true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3529 | |
Anders Carlsson | fb25052 | 2009-06-23 01:26:57 +0000 | [diff] [blame] | 3530 | const TemplateArgument *ArgList = TemplateArgs.getFlatArguments(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3531 | |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 3532 | for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) { |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3533 | // Determine whether the template argument list of the partial |
| 3534 | // specialization is identical to the implicit argument list of |
| 3535 | // the primary template. The caller may need to diagnostic this as |
| 3536 | // an error per C++ [temp.class.spec]p9b3. |
| 3537 | if (MirrorsPrimaryTemplate) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3538 | if (TemplateTypeParmDecl *TTP |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3539 | = dyn_cast<TemplateTypeParmDecl>(TemplateParams->getParam(I))) { |
| 3540 | if (Context.getCanonicalType(Context.getTypeDeclType(TTP)) != |
Anders Carlsson | 6360be7 | 2009-06-13 18:20:51 +0000 | [diff] [blame] | 3541 | Context.getCanonicalType(ArgList[I].getAsType())) |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3542 | MirrorsPrimaryTemplate = false; |
| 3543 | } else if (TemplateTemplateParmDecl *TTP |
| 3544 | = dyn_cast<TemplateTemplateParmDecl>( |
| 3545 | TemplateParams->getParam(I))) { |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 3546 | TemplateName Name = ArgList[I].getAsTemplate(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3547 | TemplateTemplateParmDecl *ArgDecl |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 3548 | = dyn_cast_or_null<TemplateTemplateParmDecl>(Name.getAsTemplateDecl()); |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3549 | if (!ArgDecl || |
| 3550 | ArgDecl->getIndex() != TTP->getIndex() || |
| 3551 | ArgDecl->getDepth() != TTP->getDepth()) |
| 3552 | MirrorsPrimaryTemplate = false; |
| 3553 | } |
| 3554 | } |
| 3555 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3556 | NonTypeTemplateParmDecl *Param |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 3557 | = dyn_cast<NonTypeTemplateParmDecl>(TemplateParams->getParam(I)); |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3558 | if (!Param) { |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 3559 | continue; |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3560 | } |
| 3561 | |
Anders Carlsson | 6360be7 | 2009-06-13 18:20:51 +0000 | [diff] [blame] | 3562 | Expr *ArgExpr = ArgList[I].getAsExpr(); |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3563 | if (!ArgExpr) { |
| 3564 | MirrorsPrimaryTemplate = false; |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 3565 | continue; |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3566 | } |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 3567 | |
| 3568 | // C++ [temp.class.spec]p8: |
| 3569 | // A non-type argument is non-specialized if it is the name of a |
| 3570 | // non-type parameter. All other non-type arguments are |
| 3571 | // specialized. |
| 3572 | // |
| 3573 | // Below, we check the two conditions that only apply to |
| 3574 | // specialized non-type arguments, so skip any non-specialized |
| 3575 | // arguments. |
| 3576 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ArgExpr)) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3577 | if (NonTypeTemplateParmDecl *NTTP |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3578 | = dyn_cast<NonTypeTemplateParmDecl>(DRE->getDecl())) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3579 | if (MirrorsPrimaryTemplate && |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3580 | (Param->getIndex() != NTTP->getIndex() || |
| 3581 | Param->getDepth() != NTTP->getDepth())) |
| 3582 | MirrorsPrimaryTemplate = false; |
| 3583 | |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 3584 | continue; |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3585 | } |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 3586 | |
| 3587 | // C++ [temp.class.spec]p9: |
| 3588 | // Within the argument list of a class template partial |
| 3589 | // specialization, the following restrictions apply: |
| 3590 | // -- A partially specialized non-type argument expression |
| 3591 | // shall not involve a template parameter of the partial |
| 3592 | // specialization except when the argument expression is a |
| 3593 | // simple identifier. |
| 3594 | if (ArgExpr->isTypeDependent() || ArgExpr->isValueDependent()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3595 | Diag(ArgExpr->getLocStart(), |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 3596 | diag::err_dependent_non_type_arg_in_partial_spec) |
| 3597 | << ArgExpr->getSourceRange(); |
| 3598 | return true; |
| 3599 | } |
| 3600 | |
| 3601 | // -- The type of a template parameter corresponding to a |
| 3602 | // specialized non-type argument shall not be dependent on a |
| 3603 | // parameter of the specialization. |
| 3604 | if (Param->getType()->isDependentType()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3605 | Diag(ArgExpr->getLocStart(), |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 3606 | diag::err_dependent_typed_non_type_arg_in_partial_spec) |
| 3607 | << Param->getType() |
| 3608 | << ArgExpr->getSourceRange(); |
| 3609 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 3610 | return true; |
| 3611 | } |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3612 | |
| 3613 | MirrorsPrimaryTemplate = false; |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 3614 | } |
| 3615 | |
| 3616 | return false; |
| 3617 | } |
| 3618 | |
Douglas Gregor | dc0a11c | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 3619 | /// \brief Retrieve the previous declaration of the given declaration. |
| 3620 | static NamedDecl *getPreviousDecl(NamedDecl *ND) { |
| 3621 | if (VarDecl *VD = dyn_cast<VarDecl>(ND)) |
| 3622 | return VD->getPreviousDeclaration(); |
| 3623 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) |
| 3624 | return FD->getPreviousDeclaration(); |
| 3625 | if (TagDecl *TD = dyn_cast<TagDecl>(ND)) |
| 3626 | return TD->getPreviousDeclaration(); |
| 3627 | if (TypedefDecl *TD = dyn_cast<TypedefDecl>(ND)) |
| 3628 | return TD->getPreviousDeclaration(); |
| 3629 | if (FunctionTemplateDecl *FTD = dyn_cast<FunctionTemplateDecl>(ND)) |
| 3630 | return FTD->getPreviousDeclaration(); |
| 3631 | if (ClassTemplateDecl *CTD = dyn_cast<ClassTemplateDecl>(ND)) |
| 3632 | return CTD->getPreviousDeclaration(); |
| 3633 | return 0; |
| 3634 | } |
| 3635 | |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 3636 | Sema::DeclResult |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 3637 | Sema::ActOnClassTemplateSpecialization(Scope *S, unsigned TagSpec, |
| 3638 | TagUseKind TUK, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3639 | SourceLocation KWLoc, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 3640 | CXXScopeSpec &SS, |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 3641 | TemplateTy TemplateD, |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3642 | SourceLocation TemplateNameLoc, |
| 3643 | SourceLocation LAngleLoc, |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 3644 | ASTTemplateArgsPtr TemplateArgsIn, |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3645 | SourceLocation RAngleLoc, |
| 3646 | AttributeList *Attr, |
| 3647 | MultiTemplateParamsArg TemplateParameterLists) { |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 3648 | assert(TUK != TUK_Reference && "References are not specializations"); |
John McCall | f1bbbb4 | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 3649 | |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3650 | // Find the class template we're specializing |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 3651 | TemplateName Name = TemplateD.getAsVal<TemplateName>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3652 | ClassTemplateDecl *ClassTemplate |
Douglas Gregor | 8b13c08 | 2009-11-12 00:46:20 +0000 | [diff] [blame] | 3653 | = dyn_cast_or_null<ClassTemplateDecl>(Name.getAsTemplateDecl()); |
| 3654 | |
| 3655 | if (!ClassTemplate) { |
| 3656 | Diag(TemplateNameLoc, diag::err_not_class_template_specialization) |
| 3657 | << (Name.getAsTemplateDecl() && |
| 3658 | isa<TemplateTemplateParmDecl>(Name.getAsTemplateDecl())); |
| 3659 | return true; |
| 3660 | } |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3661 | |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 3662 | bool isExplicitSpecialization = false; |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 3663 | bool isPartialSpecialization = false; |
| 3664 | |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 3665 | // Check the validity of the template headers that introduce this |
| 3666 | // template. |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 3667 | // FIXME: We probably shouldn't complain about these headers for |
| 3668 | // friend declarations. |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 3669 | TemplateParameterList *TemplateParams |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3670 | = MatchTemplateParametersToScopeSpecifier(TemplateNameLoc, SS, |
| 3671 | (TemplateParameterList**)TemplateParameterLists.get(), |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 3672 | TemplateParameterLists.size(), |
John McCall | 77e8b11 | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 3673 | TUK == TUK_Friend, |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 3674 | isExplicitSpecialization); |
Abramo Bagnara | 9b93488 | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 3675 | unsigned NumMatchedTemplateParamLists = TemplateParameterLists.size(); |
| 3676 | if (TemplateParams) |
| 3677 | --NumMatchedTemplateParamLists; |
| 3678 | |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 3679 | if (TemplateParams && TemplateParams->size() > 0) { |
| 3680 | isPartialSpecialization = true; |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 3681 | |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 3682 | // C++ [temp.class.spec]p10: |
| 3683 | // The template parameter list of a specialization shall not |
| 3684 | // contain default template argument values. |
| 3685 | for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) { |
| 3686 | Decl *Param = TemplateParams->getParam(I); |
| 3687 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) { |
| 3688 | if (TTP->hasDefaultArgument()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3689 | Diag(TTP->getDefaultArgumentLoc(), |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 3690 | diag::err_default_arg_in_partial_spec); |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 3691 | TTP->removeDefaultArgument(); |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 3692 | } |
| 3693 | } else if (NonTypeTemplateParmDecl *NTTP |
| 3694 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
| 3695 | if (Expr *DefArg = NTTP->getDefaultArgument()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3696 | Diag(NTTP->getDefaultArgumentLoc(), |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 3697 | diag::err_default_arg_in_partial_spec) |
| 3698 | << DefArg->getSourceRange(); |
Abramo Bagnara | d92f7a2 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 3699 | NTTP->removeDefaultArgument(); |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 3700 | DefArg->Destroy(Context); |
| 3701 | } |
| 3702 | } else { |
| 3703 | TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(Param); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 3704 | if (TTP->hasDefaultArgument()) { |
| 3705 | Diag(TTP->getDefaultArgument().getLocation(), |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 3706 | diag::err_default_arg_in_partial_spec) |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 3707 | << TTP->getDefaultArgument().getSourceRange(); |
Abramo Bagnara | d92f7a2 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 3708 | TTP->removeDefaultArgument(); |
Douglas Gregor | ba1ecb5 | 2009-06-12 19:43:02 +0000 | [diff] [blame] | 3709 | } |
| 3710 | } |
| 3711 | } |
Douglas Gregor | a735b20 | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 3712 | } else if (TemplateParams) { |
| 3713 | if (TUK == TUK_Friend) |
| 3714 | Diag(KWLoc, diag::err_template_spec_friend) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 3715 | << FixItHint::CreateRemoval( |
Douglas Gregor | a735b20 | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 3716 | SourceRange(TemplateParams->getTemplateLoc(), |
| 3717 | TemplateParams->getRAngleLoc())) |
| 3718 | << SourceRange(LAngleLoc, RAngleLoc); |
| 3719 | else |
| 3720 | isExplicitSpecialization = true; |
| 3721 | } else if (TUK != TUK_Friend) { |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 3722 | Diag(KWLoc, diag::err_template_spec_needs_header) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 3723 | << FixItHint::CreateInsertion(KWLoc, "template<> "); |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 3724 | isExplicitSpecialization = true; |
| 3725 | } |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 3726 | |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3727 | // Check that the specialization uses the same tag kind as the |
| 3728 | // original template. |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 3729 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
| 3730 | assert(Kind != TTK_Enum && "Invalid enum tag in class template spec!"); |
Douglas Gregor | 501c5ce | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 3731 | if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3732 | Kind, KWLoc, |
Douglas Gregor | 501c5ce | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 3733 | *ClassTemplate->getIdentifier())) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3734 | Diag(KWLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 3735 | << ClassTemplate |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 3736 | << FixItHint::CreateReplacement(KWLoc, |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 3737 | ClassTemplate->getTemplatedDecl()->getKindName()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3738 | Diag(ClassTemplate->getTemplatedDecl()->getLocation(), |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3739 | diag::note_previous_use); |
| 3740 | Kind = ClassTemplate->getTemplatedDecl()->getTagKind(); |
| 3741 | } |
| 3742 | |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 3743 | // Translate the parser's template argument list in our AST format. |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 3744 | TemplateArgumentListInfo TemplateArgs; |
| 3745 | TemplateArgs.setLAngleLoc(LAngleLoc); |
| 3746 | TemplateArgs.setRAngleLoc(RAngleLoc); |
Douglas Gregor | 314b97f | 2009-11-10 19:49:08 +0000 | [diff] [blame] | 3747 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 3748 | |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3749 | // Check that the template argument list is well-formed for this |
| 3750 | // template. |
Anders Carlsson | fb25052 | 2009-06-23 01:26:57 +0000 | [diff] [blame] | 3751 | TemplateArgumentListBuilder Converted(ClassTemplate->getTemplateParameters(), |
| 3752 | TemplateArgs.size()); |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 3753 | if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc, |
| 3754 | TemplateArgs, false, Converted)) |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 3755 | return true; |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3756 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3757 | assert((Converted.structuredSize() == |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3758 | ClassTemplate->getTemplateParameters()->size()) && |
| 3759 | "Converted template argument list is too short!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3760 | |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 3761 | // Find the class template (partial) specialization declaration that |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3762 | // corresponds to these arguments. |
| 3763 | llvm::FoldingSetNodeID ID; |
Douglas Gregor | ba1ecb5 | 2009-06-12 19:43:02 +0000 | [diff] [blame] | 3764 | if (isPartialSpecialization) { |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3765 | bool MirrorsPrimaryTemplate; |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 3766 | if (CheckClassTemplatePartialSpecializationArgs( |
| 3767 | ClassTemplate->getTemplateParameters(), |
Anders Carlsson | fb25052 | 2009-06-23 01:26:57 +0000 | [diff] [blame] | 3768 | Converted, MirrorsPrimaryTemplate)) |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 3769 | return true; |
| 3770 | |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3771 | if (MirrorsPrimaryTemplate) { |
| 3772 | // C++ [temp.class.spec]p9b3: |
| 3773 | // |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3774 | // -- The argument list of the specialization shall not be identical |
| 3775 | // to the implicit argument list of the primary template. |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3776 | Diag(TemplateNameLoc, diag::err_partial_spec_args_match_primary_template) |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 3777 | << (TUK == TUK_Definition) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 3778 | << FixItHint::CreateRemoval(SourceRange(LAngleLoc, RAngleLoc)); |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 3779 | return CheckClassTemplate(S, TagSpec, TUK, KWLoc, SS, |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3780 | ClassTemplate->getIdentifier(), |
| 3781 | TemplateNameLoc, |
| 3782 | Attr, |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 3783 | TemplateParams, |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3784 | AS_none); |
| 3785 | } |
| 3786 | |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 3787 | // FIXME: Diagnose friend partial specializations |
| 3788 | |
Douglas Gregor | de09096 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 3789 | if (!Name.isDependent() && |
| 3790 | !TemplateSpecializationType::anyDependentTemplateArguments( |
| 3791 | TemplateArgs.getArgumentArray(), |
| 3792 | TemplateArgs.size())) { |
| 3793 | Diag(TemplateNameLoc, diag::err_partial_spec_fully_specialized) |
| 3794 | << ClassTemplate->getDeclName(); |
| 3795 | isPartialSpecialization = false; |
| 3796 | } else { |
| 3797 | // FIXME: Template parameter list matters, too |
| 3798 | ClassTemplatePartialSpecializationDecl::Profile(ID, |
| 3799 | Converted.getFlatArguments(), |
| 3800 | Converted.flatSize(), |
| 3801 | Context); |
| 3802 | } |
| 3803 | } |
| 3804 | |
| 3805 | if (!isPartialSpecialization) |
Anders Carlsson | 1c5976e | 2009-06-05 03:43:12 +0000 | [diff] [blame] | 3806 | ClassTemplateSpecializationDecl::Profile(ID, |
Anders Carlsson | fb25052 | 2009-06-23 01:26:57 +0000 | [diff] [blame] | 3807 | Converted.getFlatArguments(), |
Douglas Gregor | 828e226 | 2009-07-29 16:09:57 +0000 | [diff] [blame] | 3808 | Converted.flatSize(), |
| 3809 | Context); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3810 | void *InsertPos = 0; |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 3811 | ClassTemplateSpecializationDecl *PrevDecl = 0; |
| 3812 | |
| 3813 | if (isPartialSpecialization) |
| 3814 | PrevDecl |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3815 | = ClassTemplate->getPartialSpecializations().FindNodeOrInsertPos(ID, |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 3816 | InsertPos); |
| 3817 | else |
| 3818 | PrevDecl |
| 3819 | = ClassTemplate->getSpecializations().FindNodeOrInsertPos(ID, InsertPos); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3820 | |
| 3821 | ClassTemplateSpecializationDecl *Specialization = 0; |
| 3822 | |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 3823 | // Check whether we can declare a class template specialization in |
| 3824 | // the current scope. |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 3825 | if (TUK != TUK_Friend && |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3826 | CheckTemplateSpecializationScope(*this, ClassTemplate, PrevDecl, |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 3827 | TemplateNameLoc, |
| 3828 | isPartialSpecialization)) |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 3829 | return true; |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 3830 | |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 3831 | // The canonical type |
| 3832 | QualType CanonType; |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 3833 | if (PrevDecl && |
| 3834 | (PrevDecl->getSpecializationKind() == TSK_Undeclared || |
Douglas Gregor | de09096 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 3835 | TUK == TUK_Friend)) { |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3836 | // Since the only prior class template specialization with these |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 3837 | // arguments was referenced but not declared, or we're only |
| 3838 | // referencing this specialization as a friend, reuse that |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3839 | // declaration node as our own, updating its source location to |
| 3840 | // reflect our new declaration. |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3841 | Specialization = PrevDecl; |
Douglas Gregor | 6bc9f7e | 2009-02-25 22:18:32 +0000 | [diff] [blame] | 3842 | Specialization->setLocation(TemplateNameLoc); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3843 | PrevDecl = 0; |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 3844 | CanonType = Context.getTypeDeclType(Specialization); |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 3845 | } else if (isPartialSpecialization) { |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 3846 | // Build the canonical type that describes the converted template |
| 3847 | // arguments of the class template partial specialization. |
Douglas Gregor | de09096 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 3848 | TemplateName CanonTemplate = Context.getCanonicalTemplateName(Name); |
| 3849 | CanonType = Context.getTemplateSpecializationType(CanonTemplate, |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 3850 | Converted.getFlatArguments(), |
| 3851 | Converted.flatSize()); |
| 3852 | |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 3853 | // Create a new class template partial specialization declaration node. |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 3854 | ClassTemplatePartialSpecializationDecl *PrevPartial |
| 3855 | = cast_or_null<ClassTemplatePartialSpecializationDecl>(PrevDecl); |
Douglas Gregor | dc60c1e | 2010-04-30 05:56:50 +0000 | [diff] [blame] | 3856 | unsigned SequenceNumber = PrevPartial? PrevPartial->getSequenceNumber() |
| 3857 | : ClassTemplate->getPartialSpecializations().size(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3858 | ClassTemplatePartialSpecializationDecl *Partial |
Douglas Gregor | 13c8577 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 3859 | = ClassTemplatePartialSpecializationDecl::Create(Context, Kind, |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 3860 | ClassTemplate->getDeclContext(), |
Anders Carlsson | 91fdf6f | 2009-06-05 04:06:48 +0000 | [diff] [blame] | 3861 | TemplateNameLoc, |
| 3862 | TemplateParams, |
| 3863 | ClassTemplate, |
Anders Carlsson | fb25052 | 2009-06-23 01:26:57 +0000 | [diff] [blame] | 3864 | Converted, |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 3865 | TemplateArgs, |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 3866 | CanonType, |
Douglas Gregor | dc60c1e | 2010-04-30 05:56:50 +0000 | [diff] [blame] | 3867 | PrevPartial, |
| 3868 | SequenceNumber); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 3869 | SetNestedNameSpecifier(Partial, SS); |
Abramo Bagnara | 9b93488 | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 3870 | if (NumMatchedTemplateParamLists > 0) { |
| 3871 | Partial->setTemplateParameterListsInfo(NumMatchedTemplateParamLists, |
| 3872 | (TemplateParameterList**) TemplateParameterLists.release()); |
| 3873 | } |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 3874 | |
| 3875 | if (PrevPartial) { |
| 3876 | ClassTemplate->getPartialSpecializations().RemoveNode(PrevPartial); |
| 3877 | ClassTemplate->getPartialSpecializations().GetOrInsertNode(Partial); |
| 3878 | } else { |
| 3879 | ClassTemplate->getPartialSpecializations().InsertNode(Partial, InsertPos); |
| 3880 | } |
| 3881 | Specialization = Partial; |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 3882 | |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 3883 | // If we are providing an explicit specialization of a member class |
| 3884 | // template specialization, make a note of that. |
| 3885 | if (PrevPartial && PrevPartial->getInstantiatedFromMember()) |
| 3886 | PrevPartial->setMemberSpecialization(); |
| 3887 | |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 3888 | // Check that all of the template parameters of the class template |
| 3889 | // partial specialization are deducible from the template |
| 3890 | // arguments. If not, this class template partial specialization |
| 3891 | // will never be used. |
| 3892 | llvm::SmallVector<bool, 8> DeducibleParams; |
| 3893 | DeducibleParams.resize(TemplateParams->size()); |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 3894 | MarkUsedTemplateParameters(Partial->getTemplateArgs(), true, |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 3895 | TemplateParams->getDepth(), |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 3896 | DeducibleParams); |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 3897 | unsigned NumNonDeducible = 0; |
| 3898 | for (unsigned I = 0, N = DeducibleParams.size(); I != N; ++I) |
| 3899 | if (!DeducibleParams[I]) |
| 3900 | ++NumNonDeducible; |
| 3901 | |
| 3902 | if (NumNonDeducible) { |
| 3903 | Diag(TemplateNameLoc, diag::warn_partial_specs_not_deducible) |
| 3904 | << (NumNonDeducible > 1) |
| 3905 | << SourceRange(TemplateNameLoc, RAngleLoc); |
| 3906 | for (unsigned I = 0, N = DeducibleParams.size(); I != N; ++I) { |
| 3907 | if (!DeducibleParams[I]) { |
| 3908 | NamedDecl *Param = cast<NamedDecl>(TemplateParams->getParam(I)); |
| 3909 | if (Param->getDeclName()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3910 | Diag(Param->getLocation(), |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 3911 | diag::note_partial_spec_unused_parameter) |
| 3912 | << Param->getDeclName(); |
| 3913 | else |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3914 | Diag(Param->getLocation(), |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 3915 | diag::note_partial_spec_unused_parameter) |
| 3916 | << std::string("<anonymous>"); |
| 3917 | } |
| 3918 | } |
| 3919 | } |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3920 | } else { |
| 3921 | // Create a new class template specialization declaration node for |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 3922 | // this explicit specialization or friend declaration. |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3923 | Specialization |
Douglas Gregor | 13c8577 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 3924 | = ClassTemplateSpecializationDecl::Create(Context, Kind, |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3925 | ClassTemplate->getDeclContext(), |
| 3926 | TemplateNameLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3927 | ClassTemplate, |
Anders Carlsson | fb25052 | 2009-06-23 01:26:57 +0000 | [diff] [blame] | 3928 | Converted, |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3929 | PrevDecl); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 3930 | SetNestedNameSpecifier(Specialization, SS); |
Abramo Bagnara | 9b93488 | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 3931 | if (NumMatchedTemplateParamLists > 0) { |
| 3932 | Specialization->setTemplateParameterListsInfo( |
| 3933 | NumMatchedTemplateParamLists, |
| 3934 | (TemplateParameterList**) TemplateParameterLists.release()); |
| 3935 | } |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3936 | |
| 3937 | if (PrevDecl) { |
| 3938 | ClassTemplate->getSpecializations().RemoveNode(PrevDecl); |
| 3939 | ClassTemplate->getSpecializations().GetOrInsertNode(Specialization); |
| 3940 | } else { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3941 | ClassTemplate->getSpecializations().InsertNode(Specialization, |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3942 | InsertPos); |
| 3943 | } |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 3944 | |
| 3945 | CanonType = Context.getTypeDeclType(Specialization); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3946 | } |
| 3947 | |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 3948 | // C++ [temp.expl.spec]p6: |
| 3949 | // If a template, a member template or the member of a class template is |
| 3950 | // explicitly specialized then that specialization shall be declared |
| 3951 | // before the first use of that specialization that would cause an implicit |
| 3952 | // instantiation to take place, in every translation unit in which such a |
| 3953 | // use occurs; no diagnostic is required. |
| 3954 | if (PrevDecl && PrevDecl->getPointOfInstantiation().isValid()) { |
Douglas Gregor | dc0a11c | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 3955 | bool Okay = false; |
| 3956 | for (NamedDecl *Prev = PrevDecl; Prev; Prev = getPreviousDecl(Prev)) { |
| 3957 | // Is there any previous explicit specialization declaration? |
| 3958 | if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) { |
| 3959 | Okay = true; |
| 3960 | break; |
| 3961 | } |
| 3962 | } |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 3963 | |
Douglas Gregor | dc0a11c | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 3964 | if (!Okay) { |
| 3965 | SourceRange Range(TemplateNameLoc, RAngleLoc); |
| 3966 | Diag(TemplateNameLoc, diag::err_specialization_after_instantiation) |
| 3967 | << Context.getTypeDeclType(Specialization) << Range; |
| 3968 | |
| 3969 | Diag(PrevDecl->getPointOfInstantiation(), |
| 3970 | diag::note_instantiation_required_here) |
| 3971 | << (PrevDecl->getTemplateSpecializationKind() |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 3972 | != TSK_ImplicitInstantiation); |
Douglas Gregor | dc0a11c | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 3973 | return true; |
| 3974 | } |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 3975 | } |
| 3976 | |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 3977 | // If this is not a friend, note that this is an explicit specialization. |
| 3978 | if (TUK != TUK_Friend) |
| 3979 | Specialization->setSpecializationKind(TSK_ExplicitSpecialization); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3980 | |
| 3981 | // Check that this isn't a redefinition of this specialization. |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 3982 | if (TUK == TUK_Definition) { |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 3983 | if (RecordDecl *Def = Specialization->getDefinition()) { |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3984 | SourceRange Range(TemplateNameLoc, RAngleLoc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3985 | Diag(TemplateNameLoc, diag::err_redefinition) |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 3986 | << Context.getTypeDeclType(Specialization) << Range; |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3987 | Diag(Def->getLocation(), diag::note_previous_definition); |
| 3988 | Specialization->setInvalidDecl(); |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 3989 | return true; |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3990 | } |
| 3991 | } |
| 3992 | |
Douglas Gregor | fc705b8 | 2009-02-26 22:19:44 +0000 | [diff] [blame] | 3993 | // Build the fully-sugared type for this class template |
| 3994 | // specialization as the user wrote in the specialization |
| 3995 | // itself. This means that we'll pretty-print the type retrieved |
| 3996 | // from the specialization's declaration the way that the user |
| 3997 | // actually wrote the specialization, rather than formatting the |
| 3998 | // name based on the "canonical" representation used to store the |
| 3999 | // template arguments in the specialization. |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 4000 | TypeSourceInfo *WrittenTy |
| 4001 | = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc, |
| 4002 | TemplateArgs, CanonType); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 4003 | if (TUK != TUK_Friend) { |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 4004 | Specialization->setTypeAsWritten(WrittenTy); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 4005 | Specialization->setTemplateKeywordLoc(KWLoc); |
| 4006 | } |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 4007 | TemplateArgsIn.release(); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4008 | |
Douglas Gregor | 6bc9f7e | 2009-02-25 22:18:32 +0000 | [diff] [blame] | 4009 | // C++ [temp.expl.spec]p9: |
| 4010 | // A template explicit specialization is in the scope of the |
| 4011 | // namespace in which the template was defined. |
| 4012 | // |
| 4013 | // We actually implement this paragraph where we set the semantic |
| 4014 | // context (in the creation of the ClassTemplateSpecializationDecl), |
| 4015 | // but we also maintain the lexical context where the actual |
| 4016 | // definition occurs. |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4017 | Specialization->setLexicalDeclContext(CurContext); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4018 | |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4019 | // We may be starting the definition of this specialization. |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 4020 | if (TUK == TUK_Definition) |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4021 | Specialization->startDefinition(); |
| 4022 | |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 4023 | if (TUK == TUK_Friend) { |
| 4024 | FriendDecl *Friend = FriendDecl::Create(Context, CurContext, |
| 4025 | TemplateNameLoc, |
John McCall | 32f2fb5 | 2010-03-25 18:04:51 +0000 | [diff] [blame] | 4026 | WrittenTy, |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 4027 | /*FIXME:*/KWLoc); |
| 4028 | Friend->setAccess(AS_public); |
| 4029 | CurContext->addDecl(Friend); |
| 4030 | } else { |
| 4031 | // Add the specialization into its lexical context, so that it can |
| 4032 | // be seen when iterating through the list of declarations in that |
| 4033 | // context. However, specializations are not found by name lookup. |
| 4034 | CurContext->addDecl(Specialization); |
| 4035 | } |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 4036 | return DeclPtrTy::make(Specialization); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4037 | } |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 4038 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4039 | Sema::DeclPtrTy |
| 4040 | Sema::ActOnTemplateDeclarator(Scope *S, |
Douglas Gregor | e542c86 | 2009-06-23 23:11:28 +0000 | [diff] [blame] | 4041 | MultiTemplateParamsArg TemplateParameterLists, |
| 4042 | Declarator &D) { |
| 4043 | return HandleDeclarator(S, D, move(TemplateParameterLists), false); |
| 4044 | } |
| 4045 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4046 | Sema::DeclPtrTy |
| 4047 | Sema::ActOnStartOfFunctionTemplateDef(Scope *FnBodyScope, |
Douglas Gregor | 52591bf | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 4048 | MultiTemplateParamsArg TemplateParameterLists, |
| 4049 | Declarator &D) { |
| 4050 | assert(getCurFunctionDecl() == 0 && "Function parsing confused"); |
| 4051 | assert(D.getTypeObject(0).Kind == DeclaratorChunk::Function && |
| 4052 | "Not a function declarator!"); |
| 4053 | DeclaratorChunk::FunctionTypeInfo &FTI = D.getTypeObject(0).Fun; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4054 | |
Douglas Gregor | 52591bf | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 4055 | if (FTI.hasPrototype) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4056 | // FIXME: Diagnose arguments without names in C. |
Douglas Gregor | 52591bf | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 4057 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4058 | |
Douglas Gregor | 52591bf | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 4059 | Scope *ParentScope = FnBodyScope->getParent(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4060 | |
| 4061 | DeclPtrTy DP = HandleDeclarator(ParentScope, D, |
Douglas Gregor | 52591bf | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 4062 | move(TemplateParameterLists), |
| 4063 | /*IsFunctionDefinition=*/true); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4064 | if (FunctionTemplateDecl *FunctionTemplate |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 4065 | = dyn_cast_or_null<FunctionTemplateDecl>(DP.getAs<Decl>())) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4066 | return ActOnStartOfFunctionDef(FnBodyScope, |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 4067 | DeclPtrTy::make(FunctionTemplate->getTemplatedDecl())); |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 4068 | if (FunctionDecl *Function = dyn_cast_or_null<FunctionDecl>(DP.getAs<Decl>())) |
| 4069 | return ActOnStartOfFunctionDef(FnBodyScope, DeclPtrTy::make(Function)); |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 4070 | return DeclPtrTy(); |
Douglas Gregor | 52591bf | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 4071 | } |
| 4072 | |
John McCall | 7504239 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 4073 | /// \brief Strips various properties off an implicit instantiation |
| 4074 | /// that has just been explicitly specialized. |
| 4075 | static void StripImplicitInstantiation(NamedDecl *D) { |
| 4076 | D->invalidateAttrs(); |
| 4077 | |
| 4078 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 4079 | FD->setInlineSpecified(false); |
| 4080 | } |
| 4081 | } |
| 4082 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4083 | /// \brief Diagnose cases where we have an explicit template specialization |
| 4084 | /// before/after an explicit template instantiation, producing diagnostics |
| 4085 | /// for those cases where they are required and determining whether the |
| 4086 | /// new specialization/instantiation will have any effect. |
| 4087 | /// |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4088 | /// \param NewLoc the location of the new explicit specialization or |
| 4089 | /// instantiation. |
| 4090 | /// |
| 4091 | /// \param NewTSK the kind of the new explicit specialization or instantiation. |
| 4092 | /// |
| 4093 | /// \param PrevDecl the previous declaration of the entity. |
| 4094 | /// |
| 4095 | /// \param PrevTSK the kind of the old explicit specialization or instantiatin. |
| 4096 | /// |
| 4097 | /// \param PrevPointOfInstantiation if valid, indicates where the previus |
| 4098 | /// declaration was instantiated (either implicitly or explicitly). |
| 4099 | /// |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 4100 | /// \param HasNoEffect will be set to true to indicate that the new |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4101 | /// specialization or instantiation has no effect and should be ignored. |
| 4102 | /// |
| 4103 | /// \returns true if there was an error that should prevent the introduction of |
| 4104 | /// the new declaration into the AST, false otherwise. |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 4105 | bool |
| 4106 | Sema::CheckSpecializationInstantiationRedecl(SourceLocation NewLoc, |
| 4107 | TemplateSpecializationKind NewTSK, |
| 4108 | NamedDecl *PrevDecl, |
| 4109 | TemplateSpecializationKind PrevTSK, |
| 4110 | SourceLocation PrevPointOfInstantiation, |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 4111 | bool &HasNoEffect) { |
| 4112 | HasNoEffect = false; |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4113 | |
| 4114 | switch (NewTSK) { |
| 4115 | case TSK_Undeclared: |
| 4116 | case TSK_ImplicitInstantiation: |
| 4117 | assert(false && "Don't check implicit instantiations here"); |
| 4118 | return false; |
| 4119 | |
| 4120 | case TSK_ExplicitSpecialization: |
| 4121 | switch (PrevTSK) { |
| 4122 | case TSK_Undeclared: |
| 4123 | case TSK_ExplicitSpecialization: |
| 4124 | // Okay, we're just specializing something that is either already |
| 4125 | // explicitly specialized or has merely been mentioned without any |
| 4126 | // instantiation. |
| 4127 | return false; |
| 4128 | |
| 4129 | case TSK_ImplicitInstantiation: |
| 4130 | if (PrevPointOfInstantiation.isInvalid()) { |
| 4131 | // The declaration itself has not actually been instantiated, so it is |
| 4132 | // still okay to specialize it. |
John McCall | 7504239 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 4133 | StripImplicitInstantiation(PrevDecl); |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4134 | return false; |
| 4135 | } |
| 4136 | // Fall through |
| 4137 | |
| 4138 | case TSK_ExplicitInstantiationDeclaration: |
| 4139 | case TSK_ExplicitInstantiationDefinition: |
| 4140 | assert((PrevTSK == TSK_ImplicitInstantiation || |
| 4141 | PrevPointOfInstantiation.isValid()) && |
| 4142 | "Explicit instantiation without point of instantiation?"); |
| 4143 | |
| 4144 | // C++ [temp.expl.spec]p6: |
| 4145 | // If a template, a member template or the member of a class template |
| 4146 | // is explicitly specialized then that specialization shall be declared |
| 4147 | // before the first use of that specialization that would cause an |
| 4148 | // implicit instantiation to take place, in every translation unit in |
| 4149 | // which such a use occurs; no diagnostic is required. |
Douglas Gregor | dc0a11c | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 4150 | for (NamedDecl *Prev = PrevDecl; Prev; Prev = getPreviousDecl(Prev)) { |
| 4151 | // Is there any previous explicit specialization declaration? |
| 4152 | if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) |
| 4153 | return false; |
| 4154 | } |
| 4155 | |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 4156 | Diag(NewLoc, diag::err_specialization_after_instantiation) |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4157 | << PrevDecl; |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 4158 | Diag(PrevPointOfInstantiation, diag::note_instantiation_required_here) |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4159 | << (PrevTSK != TSK_ImplicitInstantiation); |
| 4160 | |
| 4161 | return true; |
| 4162 | } |
| 4163 | break; |
| 4164 | |
| 4165 | case TSK_ExplicitInstantiationDeclaration: |
| 4166 | switch (PrevTSK) { |
| 4167 | case TSK_ExplicitInstantiationDeclaration: |
| 4168 | // This explicit instantiation declaration is redundant (that's okay). |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 4169 | HasNoEffect = true; |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4170 | return false; |
| 4171 | |
| 4172 | case TSK_Undeclared: |
| 4173 | case TSK_ImplicitInstantiation: |
| 4174 | // We're explicitly instantiating something that may have already been |
| 4175 | // implicitly instantiated; that's fine. |
| 4176 | return false; |
| 4177 | |
| 4178 | case TSK_ExplicitSpecialization: |
| 4179 | // C++0x [temp.explicit]p4: |
| 4180 | // For a given set of template parameters, if an explicit instantiation |
| 4181 | // of a template appears after a declaration of an explicit |
| 4182 | // specialization for that template, the explicit instantiation has no |
| 4183 | // effect. |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 4184 | HasNoEffect = true; |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4185 | return false; |
| 4186 | |
| 4187 | case TSK_ExplicitInstantiationDefinition: |
| 4188 | // C++0x [temp.explicit]p10: |
| 4189 | // If an entity is the subject of both an explicit instantiation |
| 4190 | // declaration and an explicit instantiation definition in the same |
| 4191 | // translation unit, the definition shall follow the declaration. |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 4192 | Diag(NewLoc, |
| 4193 | diag::err_explicit_instantiation_declaration_after_definition); |
| 4194 | Diag(PrevPointOfInstantiation, |
| 4195 | diag::note_explicit_instantiation_definition_here); |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4196 | assert(PrevPointOfInstantiation.isValid() && |
| 4197 | "Explicit instantiation without point of instantiation?"); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 4198 | HasNoEffect = true; |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4199 | return false; |
| 4200 | } |
| 4201 | break; |
| 4202 | |
| 4203 | case TSK_ExplicitInstantiationDefinition: |
| 4204 | switch (PrevTSK) { |
| 4205 | case TSK_Undeclared: |
| 4206 | case TSK_ImplicitInstantiation: |
| 4207 | // We're explicitly instantiating something that may have already been |
| 4208 | // implicitly instantiated; that's fine. |
| 4209 | return false; |
| 4210 | |
| 4211 | case TSK_ExplicitSpecialization: |
| 4212 | // C++ DR 259, C++0x [temp.explicit]p4: |
| 4213 | // For a given set of template parameters, if an explicit |
| 4214 | // instantiation of a template appears after a declaration of |
| 4215 | // an explicit specialization for that template, the explicit |
| 4216 | // instantiation has no effect. |
| 4217 | // |
| 4218 | // 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] | 4219 | // is not harmful to try to explicitly instantiate something that |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4220 | // has been explicitly specialized. |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 4221 | if (!getLangOptions().CPlusPlus0x) { |
| 4222 | Diag(NewLoc, diag::ext_explicit_instantiation_after_specialization) |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4223 | << PrevDecl; |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 4224 | Diag(PrevDecl->getLocation(), |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4225 | diag::note_previous_template_specialization); |
| 4226 | } |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 4227 | HasNoEffect = true; |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4228 | return false; |
| 4229 | |
| 4230 | case TSK_ExplicitInstantiationDeclaration: |
| 4231 | // We're explicity instantiating a definition for something for which we |
| 4232 | // were previously asked to suppress instantiations. That's fine. |
| 4233 | return false; |
| 4234 | |
| 4235 | case TSK_ExplicitInstantiationDefinition: |
| 4236 | // C++0x [temp.spec]p5: |
| 4237 | // For a given template and a given set of template-arguments, |
| 4238 | // - an explicit instantiation definition shall appear at most once |
| 4239 | // in a program, |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 4240 | Diag(NewLoc, diag::err_explicit_instantiation_duplicate) |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4241 | << PrevDecl; |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 4242 | Diag(PrevPointOfInstantiation, |
| 4243 | diag::note_previous_explicit_instantiation); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 4244 | HasNoEffect = true; |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4245 | return false; |
| 4246 | } |
| 4247 | break; |
| 4248 | } |
| 4249 | |
| 4250 | assert(false && "Missing specialization/instantiation case?"); |
| 4251 | |
| 4252 | return false; |
| 4253 | } |
| 4254 | |
John McCall | af2094e | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 4255 | /// \brief Perform semantic analysis for the given dependent function |
| 4256 | /// template specialization. The only possible way to get a dependent |
| 4257 | /// function template specialization is with a friend declaration, |
| 4258 | /// like so: |
| 4259 | /// |
| 4260 | /// template <class T> void foo(T); |
| 4261 | /// template <class T> class A { |
| 4262 | /// friend void foo<>(T); |
| 4263 | /// }; |
| 4264 | /// |
| 4265 | /// There really isn't any useful analysis we can do here, so we |
| 4266 | /// just store the information. |
| 4267 | bool |
| 4268 | Sema::CheckDependentFunctionTemplateSpecialization(FunctionDecl *FD, |
| 4269 | const TemplateArgumentListInfo &ExplicitTemplateArgs, |
| 4270 | LookupResult &Previous) { |
| 4271 | // Remove anything from Previous that isn't a function template in |
| 4272 | // the correct context. |
| 4273 | DeclContext *FDLookupContext = FD->getDeclContext()->getLookupContext(); |
| 4274 | LookupResult::Filter F = Previous.makeFilter(); |
| 4275 | while (F.hasNext()) { |
| 4276 | NamedDecl *D = F.next()->getUnderlyingDecl(); |
| 4277 | if (!isa<FunctionTemplateDecl>(D) || |
| 4278 | !FDLookupContext->Equals(D->getDeclContext()->getLookupContext())) |
| 4279 | F.erase(); |
| 4280 | } |
| 4281 | F.done(); |
| 4282 | |
| 4283 | // Should this be diagnosed here? |
| 4284 | if (Previous.empty()) return true; |
| 4285 | |
| 4286 | FD->setDependentTemplateSpecialization(Context, Previous.asUnresolvedSet(), |
| 4287 | ExplicitTemplateArgs); |
| 4288 | return false; |
| 4289 | } |
| 4290 | |
Abramo Bagnara | e03db98 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 4291 | /// \brief Perform semantic analysis for the given function template |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4292 | /// specialization. |
| 4293 | /// |
Abramo Bagnara | e03db98 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 4294 | /// This routine performs all of the semantic analysis required for an |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4295 | /// explicit function template specialization. On successful completion, |
| 4296 | /// the function declaration \p FD will become a function template |
| 4297 | /// specialization. |
| 4298 | /// |
| 4299 | /// \param FD the function declaration, which will be updated to become a |
| 4300 | /// function template specialization. |
| 4301 | /// |
Abramo Bagnara | e03db98 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 4302 | /// \param ExplicitTemplateArgs the explicitly-provided template arguments, |
| 4303 | /// if any. Note that this may be valid info even when 0 arguments are |
| 4304 | /// explicitly provided as in, e.g., \c void sort<>(char*, char*); |
| 4305 | /// as it anyway contains info on the angle brackets locations. |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4306 | /// |
Abramo Bagnara | e03db98 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 4307 | /// \param PrevDecl the set of declarations that may be specialized by |
| 4308 | /// this function specialization. |
| 4309 | bool |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4310 | Sema::CheckFunctionTemplateSpecialization(FunctionDecl *FD, |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 4311 | const TemplateArgumentListInfo *ExplicitTemplateArgs, |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 4312 | LookupResult &Previous) { |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4313 | // The set of function template specializations that could match this |
| 4314 | // explicit function template specialization. |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4315 | UnresolvedSet<8> Candidates; |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4316 | |
| 4317 | DeclContext *FDLookupContext = FD->getDeclContext()->getLookupContext(); |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 4318 | for (LookupResult::iterator I = Previous.begin(), E = Previous.end(); |
| 4319 | I != E; ++I) { |
| 4320 | NamedDecl *Ovl = (*I)->getUnderlyingDecl(); |
| 4321 | if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Ovl)) { |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4322 | // Only consider templates found within the same semantic lookup scope as |
| 4323 | // FD. |
| 4324 | if (!FDLookupContext->Equals(Ovl->getDeclContext()->getLookupContext())) |
| 4325 | continue; |
| 4326 | |
| 4327 | // C++ [temp.expl.spec]p11: |
| 4328 | // A trailing template-argument can be left unspecified in the |
| 4329 | // template-id naming an explicit function template specialization |
| 4330 | // provided it can be deduced from the function argument type. |
| 4331 | // Perform template argument deduction to determine whether we may be |
| 4332 | // specializing this template. |
| 4333 | // FIXME: It is somewhat wasteful to build |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 4334 | TemplateDeductionInfo Info(Context, FD->getLocation()); |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4335 | FunctionDecl *Specialization = 0; |
| 4336 | if (TemplateDeductionResult TDK |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 4337 | = DeduceTemplateArguments(FunTmpl, ExplicitTemplateArgs, |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4338 | FD->getType(), |
| 4339 | Specialization, |
| 4340 | Info)) { |
| 4341 | // FIXME: Template argument deduction failed; record why it failed, so |
| 4342 | // that we can provide nifty diagnostics. |
| 4343 | (void)TDK; |
| 4344 | continue; |
| 4345 | } |
| 4346 | |
| 4347 | // Record this candidate. |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4348 | Candidates.addDecl(Specialization, I.getAccess()); |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4349 | } |
| 4350 | } |
| 4351 | |
Douglas Gregor | c5df30f | 2009-09-26 03:41:46 +0000 | [diff] [blame] | 4352 | // Find the most specialized function template. |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4353 | UnresolvedSetIterator Result |
| 4354 | = getMostSpecialized(Candidates.begin(), Candidates.end(), |
| 4355 | TPOC_Other, FD->getLocation(), |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 4356 | PDiag(diag::err_function_template_spec_no_match) |
Douglas Gregor | c5df30f | 2009-09-26 03:41:46 +0000 | [diff] [blame] | 4357 | << FD->getDeclName(), |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 4358 | PDiag(diag::err_function_template_spec_ambiguous) |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 4359 | << FD->getDeclName() << (ExplicitTemplateArgs != 0), |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 4360 | PDiag(diag::note_function_template_spec_matched)); |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4361 | if (Result == Candidates.end()) |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4362 | return true; |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4363 | |
| 4364 | // Ignore access information; it doesn't figure into redeclaration checking. |
| 4365 | FunctionDecl *Specialization = cast<FunctionDecl>(*Result); |
Douglas Gregor | c42b652 | 2010-04-09 21:02:29 +0000 | [diff] [blame] | 4366 | Specialization->setLocation(FD->getLocation()); |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4367 | |
| 4368 | // FIXME: Check if the prior specialization has a point of instantiation. |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 4369 | // If so, we have run afoul of . |
John McCall | 7ad650f | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 4370 | |
| 4371 | // If this is a friend declaration, then we're not really declaring |
| 4372 | // an explicit specialization. |
| 4373 | bool isFriend = (FD->getFriendObjectKind() != Decl::FOK_None); |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4374 | |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4375 | // Check the scope of this explicit specialization. |
John McCall | 7ad650f | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 4376 | if (!isFriend && |
| 4377 | CheckTemplateSpecializationScope(*this, |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4378 | Specialization->getPrimaryTemplate(), |
| 4379 | Specialization, FD->getLocation(), |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4380 | false)) |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4381 | return true; |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 4382 | |
| 4383 | // C++ [temp.expl.spec]p6: |
| 4384 | // If a template, a member template or the member of a class template is |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 4385 | // explicitly specialized then that specialization shall be declared |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 4386 | // before the first use of that specialization that would cause an implicit |
| 4387 | // instantiation to take place, in every translation unit in which such a |
| 4388 | // use occurs; no diagnostic is required. |
| 4389 | FunctionTemplateSpecializationInfo *SpecInfo |
| 4390 | = Specialization->getTemplateSpecializationInfo(); |
| 4391 | assert(SpecInfo && "Function template specialization info missing?"); |
John McCall | 7504239 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 4392 | |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 4393 | bool HasNoEffect = false; |
John McCall | 7ad650f | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 4394 | if (!isFriend && |
| 4395 | CheckSpecializationInstantiationRedecl(FD->getLocation(), |
John McCall | 7504239 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 4396 | TSK_ExplicitSpecialization, |
| 4397 | Specialization, |
| 4398 | SpecInfo->getTemplateSpecializationKind(), |
| 4399 | SpecInfo->getPointOfInstantiation(), |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 4400 | HasNoEffect)) |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 4401 | return true; |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4402 | |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4403 | // Mark the prior declaration as an explicit specialization, so that later |
| 4404 | // clients know that this is an explicit specialization. |
John McCall | 7ad650f | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 4405 | if (!isFriend) |
| 4406 | SpecInfo->setTemplateSpecializationKind(TSK_ExplicitSpecialization); |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4407 | |
| 4408 | // Turn the given function declaration into a function template |
| 4409 | // specialization, with the template arguments from the previous |
| 4410 | // specialization. |
Abramo Bagnara | e03db98 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 4411 | // Take copies of (semantic and syntactic) template argument lists. |
| 4412 | const TemplateArgumentList* TemplArgs = new (Context) |
| 4413 | TemplateArgumentList(Specialization->getTemplateSpecializationArgs()); |
| 4414 | const TemplateArgumentListInfo* TemplArgsAsWritten = ExplicitTemplateArgs |
| 4415 | ? new (Context) TemplateArgumentListInfo(*ExplicitTemplateArgs) : 0; |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 4416 | FD->setFunctionTemplateSpecialization(Specialization->getPrimaryTemplate(), |
Abramo Bagnara | e03db98 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 4417 | TemplArgs, /*InsertPos=*/0, |
| 4418 | SpecInfo->getTemplateSpecializationKind(), |
| 4419 | TemplArgsAsWritten); |
| 4420 | |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4421 | // The "previous declaration" for this function template specialization is |
| 4422 | // the prior function template specialization. |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 4423 | Previous.clear(); |
| 4424 | Previous.addDecl(Specialization); |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4425 | return false; |
| 4426 | } |
| 4427 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4428 | /// \brief Perform semantic analysis for the given non-template member |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 4429 | /// specialization. |
| 4430 | /// |
| 4431 | /// This routine performs all of the semantic analysis required for an |
| 4432 | /// explicit member function specialization. On successful completion, |
| 4433 | /// the function declaration \p FD will become a member function |
| 4434 | /// specialization. |
| 4435 | /// |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4436 | /// \param Member the member declaration, which will be updated to become a |
| 4437 | /// specialization. |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 4438 | /// |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 4439 | /// \param Previous the set of declarations, one of which may be specialized |
| 4440 | /// by this function specialization; the set will be modified to contain the |
| 4441 | /// redeclared member. |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 4442 | bool |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 4443 | Sema::CheckMemberSpecialization(NamedDecl *Member, LookupResult &Previous) { |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4444 | assert(!isa<TemplateDecl>(Member) && "Only for non-template members"); |
John McCall | 77e8b11 | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 4445 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4446 | // Try to find the member we are instantiating. |
| 4447 | NamedDecl *Instantiation = 0; |
| 4448 | NamedDecl *InstantiatedFrom = 0; |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 4449 | MemberSpecializationInfo *MSInfo = 0; |
| 4450 | |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 4451 | if (Previous.empty()) { |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4452 | // Nowhere to look anyway. |
| 4453 | } else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Member)) { |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 4454 | for (LookupResult::iterator I = Previous.begin(), E = Previous.end(); |
| 4455 | I != E; ++I) { |
| 4456 | NamedDecl *D = (*I)->getUnderlyingDecl(); |
| 4457 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) { |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4458 | if (Context.hasSameType(Function->getType(), Method->getType())) { |
| 4459 | Instantiation = Method; |
| 4460 | InstantiatedFrom = Method->getInstantiatedFromMemberFunction(); |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 4461 | MSInfo = Method->getMemberSpecializationInfo(); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4462 | break; |
| 4463 | } |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 4464 | } |
| 4465 | } |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4466 | } else if (isa<VarDecl>(Member)) { |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 4467 | VarDecl *PrevVar; |
| 4468 | if (Previous.isSingleResult() && |
| 4469 | (PrevVar = dyn_cast<VarDecl>(Previous.getFoundDecl()))) |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4470 | if (PrevVar->isStaticDataMember()) { |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 4471 | Instantiation = PrevVar; |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4472 | InstantiatedFrom = PrevVar->getInstantiatedFromStaticDataMember(); |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 4473 | MSInfo = PrevVar->getMemberSpecializationInfo(); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4474 | } |
| 4475 | } else if (isa<RecordDecl>(Member)) { |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 4476 | CXXRecordDecl *PrevRecord; |
| 4477 | if (Previous.isSingleResult() && |
| 4478 | (PrevRecord = dyn_cast<CXXRecordDecl>(Previous.getFoundDecl()))) { |
| 4479 | Instantiation = PrevRecord; |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4480 | InstantiatedFrom = PrevRecord->getInstantiatedFromMemberClass(); |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 4481 | MSInfo = PrevRecord->getMemberSpecializationInfo(); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4482 | } |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 4483 | } |
| 4484 | |
| 4485 | if (!Instantiation) { |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4486 | // There is no previous declaration that matches. Since member |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 4487 | // specializations are always out-of-line, the caller will complain about |
| 4488 | // this mismatch later. |
| 4489 | return false; |
| 4490 | } |
John McCall | 77e8b11 | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 4491 | |
| 4492 | // If this is a friend, just bail out here before we start turning |
| 4493 | // things into explicit specializations. |
| 4494 | if (Member->getFriendObjectKind() != Decl::FOK_None) { |
| 4495 | // Preserve instantiation information. |
| 4496 | if (InstantiatedFrom && isa<CXXMethodDecl>(Member)) { |
| 4497 | cast<CXXMethodDecl>(Member)->setInstantiationOfMemberFunction( |
| 4498 | cast<CXXMethodDecl>(InstantiatedFrom), |
| 4499 | cast<CXXMethodDecl>(Instantiation)->getTemplateSpecializationKind()); |
| 4500 | } else if (InstantiatedFrom && isa<CXXRecordDecl>(Member)) { |
| 4501 | cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass( |
| 4502 | cast<CXXRecordDecl>(InstantiatedFrom), |
| 4503 | cast<CXXRecordDecl>(Instantiation)->getTemplateSpecializationKind()); |
| 4504 | } |
| 4505 | |
| 4506 | Previous.clear(); |
| 4507 | Previous.addDecl(Instantiation); |
| 4508 | return false; |
| 4509 | } |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 4510 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4511 | // Make sure that this is a specialization of a member. |
| 4512 | if (!InstantiatedFrom) { |
| 4513 | Diag(Member->getLocation(), diag::err_spec_member_not_instantiated) |
| 4514 | << Member; |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 4515 | Diag(Instantiation->getLocation(), diag::note_specialized_decl); |
| 4516 | return true; |
| 4517 | } |
| 4518 | |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 4519 | // C++ [temp.expl.spec]p6: |
| 4520 | // If a template, a member template or the member of a class template is |
| 4521 | // explicitly specialized then that spe- cialization shall be declared |
| 4522 | // before the first use of that specialization that would cause an implicit |
| 4523 | // instantiation to take place, in every translation unit in which such a |
| 4524 | // use occurs; no diagnostic is required. |
| 4525 | assert(MSInfo && "Member specialization info missing?"); |
John McCall | 7504239 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 4526 | |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 4527 | bool HasNoEffect = false; |
John McCall | 7504239 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 4528 | if (CheckSpecializationInstantiationRedecl(Member->getLocation(), |
| 4529 | TSK_ExplicitSpecialization, |
| 4530 | Instantiation, |
| 4531 | MSInfo->getTemplateSpecializationKind(), |
| 4532 | MSInfo->getPointOfInstantiation(), |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 4533 | HasNoEffect)) |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 4534 | return true; |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 4535 | |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 4536 | // Check the scope of this explicit specialization. |
| 4537 | if (CheckTemplateSpecializationScope(*this, |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4538 | InstantiatedFrom, |
| 4539 | Instantiation, Member->getLocation(), |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4540 | false)) |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 4541 | return true; |
Douglas Gregor | 2db3232 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 4542 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4543 | // Note that this is an explicit instantiation of a member. |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 4544 | // the original declaration to note that it is an explicit specialization |
| 4545 | // (if it was previously an implicit instantiation). This latter step |
| 4546 | // makes bookkeeping easier. |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4547 | if (isa<FunctionDecl>(Member)) { |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 4548 | FunctionDecl *InstantiationFunction = cast<FunctionDecl>(Instantiation); |
| 4549 | if (InstantiationFunction->getTemplateSpecializationKind() == |
| 4550 | TSK_ImplicitInstantiation) { |
| 4551 | InstantiationFunction->setTemplateSpecializationKind( |
| 4552 | TSK_ExplicitSpecialization); |
| 4553 | InstantiationFunction->setLocation(Member->getLocation()); |
| 4554 | } |
| 4555 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4556 | cast<FunctionDecl>(Member)->setInstantiationOfMemberFunction( |
| 4557 | cast<CXXMethodDecl>(InstantiatedFrom), |
| 4558 | TSK_ExplicitSpecialization); |
| 4559 | } else if (isa<VarDecl>(Member)) { |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 4560 | VarDecl *InstantiationVar = cast<VarDecl>(Instantiation); |
| 4561 | if (InstantiationVar->getTemplateSpecializationKind() == |
| 4562 | TSK_ImplicitInstantiation) { |
| 4563 | InstantiationVar->setTemplateSpecializationKind( |
| 4564 | TSK_ExplicitSpecialization); |
| 4565 | InstantiationVar->setLocation(Member->getLocation()); |
| 4566 | } |
| 4567 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4568 | Context.setInstantiatedFromStaticDataMember(cast<VarDecl>(Member), |
| 4569 | cast<VarDecl>(InstantiatedFrom), |
| 4570 | TSK_ExplicitSpecialization); |
| 4571 | } else { |
| 4572 | assert(isa<CXXRecordDecl>(Member) && "Only member classes remain"); |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 4573 | CXXRecordDecl *InstantiationClass = cast<CXXRecordDecl>(Instantiation); |
| 4574 | if (InstantiationClass->getTemplateSpecializationKind() == |
| 4575 | TSK_ImplicitInstantiation) { |
| 4576 | InstantiationClass->setTemplateSpecializationKind( |
| 4577 | TSK_ExplicitSpecialization); |
| 4578 | InstantiationClass->setLocation(Member->getLocation()); |
| 4579 | } |
| 4580 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4581 | cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass( |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 4582 | cast<CXXRecordDecl>(InstantiatedFrom), |
| 4583 | TSK_ExplicitSpecialization); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4584 | } |
| 4585 | |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 4586 | // Save the caller the trouble of having to figure out which declaration |
| 4587 | // this specialization matches. |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 4588 | Previous.clear(); |
| 4589 | Previous.addDecl(Instantiation); |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 4590 | return false; |
| 4591 | } |
| 4592 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 4593 | /// \brief Check the scope of an explicit instantiation. |
| 4594 | static void CheckExplicitInstantiationScope(Sema &S, NamedDecl *D, |
| 4595 | SourceLocation InstLoc, |
| 4596 | bool WasQualifiedName) { |
| 4597 | DeclContext *ExpectedContext |
| 4598 | = D->getDeclContext()->getEnclosingNamespaceContext()->getLookupContext(); |
| 4599 | DeclContext *CurContext = S.CurContext->getLookupContext(); |
| 4600 | |
| 4601 | // C++0x [temp.explicit]p2: |
| 4602 | // An explicit instantiation shall appear in an enclosing namespace of its |
| 4603 | // template. |
| 4604 | // |
| 4605 | // This is DR275, which we do not retroactively apply to C++98/03. |
| 4606 | if (S.getLangOptions().CPlusPlus0x && |
| 4607 | !CurContext->Encloses(ExpectedContext)) { |
| 4608 | if (NamespaceDecl *NS = dyn_cast<NamespaceDecl>(ExpectedContext)) |
Douglas Gregor | 2166beb | 2010-05-11 17:39:34 +0000 | [diff] [blame] | 4609 | S.Diag(InstLoc, |
| 4610 | S.getLangOptions().CPlusPlus0x? |
| 4611 | diag::err_explicit_instantiation_out_of_scope |
| 4612 | : diag::warn_explicit_instantiation_out_of_scope_0x) |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 4613 | << D << NS; |
| 4614 | else |
Douglas Gregor | 2166beb | 2010-05-11 17:39:34 +0000 | [diff] [blame] | 4615 | S.Diag(InstLoc, |
| 4616 | S.getLangOptions().CPlusPlus0x? |
| 4617 | diag::err_explicit_instantiation_must_be_global |
| 4618 | : diag::warn_explicit_instantiation_out_of_scope_0x) |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 4619 | << D; |
| 4620 | S.Diag(D->getLocation(), diag::note_explicit_instantiation_here); |
| 4621 | return; |
| 4622 | } |
| 4623 | |
| 4624 | // C++0x [temp.explicit]p2: |
| 4625 | // If the name declared in the explicit instantiation is an unqualified |
| 4626 | // name, the explicit instantiation shall appear in the namespace where |
| 4627 | // its template is declared or, if that namespace is inline (7.3.1), any |
| 4628 | // namespace from its enclosing namespace set. |
| 4629 | if (WasQualifiedName) |
| 4630 | return; |
| 4631 | |
| 4632 | if (CurContext->Equals(ExpectedContext)) |
| 4633 | return; |
| 4634 | |
Douglas Gregor | 2166beb | 2010-05-11 17:39:34 +0000 | [diff] [blame] | 4635 | S.Diag(InstLoc, |
| 4636 | S.getLangOptions().CPlusPlus0x? |
| 4637 | diag::err_explicit_instantiation_unqualified_wrong_namespace |
| 4638 | : diag::warn_explicit_instantiation_unqualified_wrong_namespace_0x) |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 4639 | << D << ExpectedContext; |
| 4640 | S.Diag(D->getLocation(), diag::note_explicit_instantiation_here); |
| 4641 | } |
| 4642 | |
| 4643 | /// \brief Determine whether the given scope specifier has a template-id in it. |
| 4644 | static bool ScopeSpecifierHasTemplateId(const CXXScopeSpec &SS) { |
| 4645 | if (!SS.isSet()) |
| 4646 | return false; |
| 4647 | |
| 4648 | // C++0x [temp.explicit]p2: |
| 4649 | // If the explicit instantiation is for a member function, a member class |
| 4650 | // or a static data member of a class template specialization, the name of |
| 4651 | // the class template specialization in the qualified-id for the member |
| 4652 | // name shall be a simple-template-id. |
| 4653 | // |
| 4654 | // C++98 has the same restriction, just worded differently. |
| 4655 | for (NestedNameSpecifier *NNS = (NestedNameSpecifier *)SS.getScopeRep(); |
| 4656 | NNS; NNS = NNS->getPrefix()) |
| 4657 | if (Type *T = NNS->getAsType()) |
| 4658 | if (isa<TemplateSpecializationType>(T)) |
| 4659 | return true; |
| 4660 | |
| 4661 | return false; |
| 4662 | } |
| 4663 | |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 4664 | // Explicit instantiation of a class template specialization |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4665 | Sema::DeclResult |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4666 | Sema::ActOnExplicitInstantiation(Scope *S, |
Douglas Gregor | 45f9655 | 2009-09-04 06:33:52 +0000 | [diff] [blame] | 4667 | SourceLocation ExternLoc, |
| 4668 | SourceLocation TemplateLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4669 | unsigned TagSpec, |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4670 | SourceLocation KWLoc, |
| 4671 | const CXXScopeSpec &SS, |
| 4672 | TemplateTy TemplateD, |
| 4673 | SourceLocation TemplateNameLoc, |
| 4674 | SourceLocation LAngleLoc, |
| 4675 | ASTTemplateArgsPtr TemplateArgsIn, |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4676 | SourceLocation RAngleLoc, |
| 4677 | AttributeList *Attr) { |
| 4678 | // Find the class template we're specializing |
| 4679 | TemplateName Name = TemplateD.getAsVal<TemplateName>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4680 | ClassTemplateDecl *ClassTemplate |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4681 | = cast<ClassTemplateDecl>(Name.getAsTemplateDecl()); |
| 4682 | |
| 4683 | // Check that the specialization uses the same tag kind as the |
| 4684 | // original template. |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 4685 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
| 4686 | assert(Kind != TTK_Enum && |
| 4687 | "Invalid enum tag in class template explicit instantiation!"); |
Douglas Gregor | 501c5ce | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 4688 | if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4689 | Kind, KWLoc, |
Douglas Gregor | 501c5ce | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 4690 | *ClassTemplate->getIdentifier())) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4691 | Diag(KWLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4692 | << ClassTemplate |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 4693 | << FixItHint::CreateReplacement(KWLoc, |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4694 | ClassTemplate->getTemplatedDecl()->getKindName()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4695 | Diag(ClassTemplate->getTemplatedDecl()->getLocation(), |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4696 | diag::note_previous_use); |
| 4697 | Kind = ClassTemplate->getTemplatedDecl()->getTagKind(); |
| 4698 | } |
| 4699 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 4700 | // C++0x [temp.explicit]p2: |
| 4701 | // There are two forms of explicit instantiation: an explicit instantiation |
| 4702 | // definition and an explicit instantiation declaration. An explicit |
| 4703 | // instantiation declaration begins with the extern keyword. [...] |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4704 | TemplateSpecializationKind TSK |
| 4705 | = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition |
| 4706 | : TSK_ExplicitInstantiationDeclaration; |
| 4707 | |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4708 | // Translate the parser's template argument list in our AST format. |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 4709 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
Douglas Gregor | 314b97f | 2009-11-10 19:49:08 +0000 | [diff] [blame] | 4710 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4711 | |
| 4712 | // Check that the template argument list is well-formed for this |
| 4713 | // template. |
Anders Carlsson | fb25052 | 2009-06-23 01:26:57 +0000 | [diff] [blame] | 4714 | TemplateArgumentListBuilder Converted(ClassTemplate->getTemplateParameters(), |
| 4715 | TemplateArgs.size()); |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 4716 | if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc, |
| 4717 | TemplateArgs, false, Converted)) |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4718 | return true; |
| 4719 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4720 | assert((Converted.structuredSize() == |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4721 | ClassTemplate->getTemplateParameters()->size()) && |
| 4722 | "Converted template argument list is too short!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4723 | |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4724 | // Find the class template specialization declaration that |
| 4725 | // corresponds to these arguments. |
| 4726 | llvm::FoldingSetNodeID ID; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4727 | ClassTemplateSpecializationDecl::Profile(ID, |
Anders Carlsson | fb25052 | 2009-06-23 01:26:57 +0000 | [diff] [blame] | 4728 | Converted.getFlatArguments(), |
Douglas Gregor | 828e226 | 2009-07-29 16:09:57 +0000 | [diff] [blame] | 4729 | Converted.flatSize(), |
| 4730 | Context); |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4731 | void *InsertPos = 0; |
| 4732 | ClassTemplateSpecializationDecl *PrevDecl |
| 4733 | = ClassTemplate->getSpecializations().FindNodeOrInsertPos(ID, InsertPos); |
| 4734 | |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 4735 | TemplateSpecializationKind PrevDecl_TSK |
| 4736 | = PrevDecl ? PrevDecl->getTemplateSpecializationKind() : TSK_Undeclared; |
| 4737 | |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4738 | // C++0x [temp.explicit]p2: |
| 4739 | // [...] An explicit instantiation shall appear in an enclosing |
| 4740 | // namespace of its template. [...] |
| 4741 | // |
| 4742 | // This is C++ DR 275. |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 4743 | CheckExplicitInstantiationScope(*this, ClassTemplate, TemplateNameLoc, |
| 4744 | SS.isSet()); |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4745 | |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4746 | ClassTemplateSpecializationDecl *Specialization = 0; |
| 4747 | |
Douglas Gregor | d78f598 | 2009-11-25 06:01:46 +0000 | [diff] [blame] | 4748 | bool ReusedDecl = false; |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 4749 | bool HasNoEffect = false; |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4750 | if (PrevDecl) { |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 4751 | if (CheckSpecializationInstantiationRedecl(TemplateNameLoc, TSK, |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 4752 | PrevDecl, PrevDecl_TSK, |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 4753 | PrevDecl->getPointOfInstantiation(), |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 4754 | HasNoEffect)) |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4755 | return DeclPtrTy::make(PrevDecl); |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4756 | |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 4757 | // Even though HasNoEffect == true means that this explicit instantiation |
| 4758 | // has no effect on semantics, we go on to put its syntax in the AST. |
| 4759 | |
| 4760 | if (PrevDecl_TSK == TSK_ImplicitInstantiation || |
| 4761 | PrevDecl_TSK == TSK_Undeclared) { |
Douglas Gregor | 52604ab | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 4762 | // Since the only prior class template specialization with these |
| 4763 | // arguments was referenced but not declared, reuse that |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 4764 | // declaration node as our own, updating the source location |
| 4765 | // for the template name to reflect our new declaration. |
| 4766 | // (Other source locations will be updated later.) |
Douglas Gregor | 52604ab | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 4767 | Specialization = PrevDecl; |
| 4768 | Specialization->setLocation(TemplateNameLoc); |
| 4769 | PrevDecl = 0; |
Douglas Gregor | d78f598 | 2009-11-25 06:01:46 +0000 | [diff] [blame] | 4770 | ReusedDecl = true; |
Douglas Gregor | 52604ab | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 4771 | } |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 4772 | } |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 4773 | |
Douglas Gregor | 52604ab | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 4774 | if (!Specialization) { |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4775 | // Create a new class template specialization declaration node for |
| 4776 | // this explicit specialization. |
| 4777 | Specialization |
Douglas Gregor | 13c8577 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 4778 | = ClassTemplateSpecializationDecl::Create(Context, Kind, |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4779 | ClassTemplate->getDeclContext(), |
| 4780 | TemplateNameLoc, |
| 4781 | ClassTemplate, |
Douglas Gregor | 52604ab | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 4782 | Converted, PrevDecl); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 4783 | SetNestedNameSpecifier(Specialization, SS); |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4784 | |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 4785 | if (!HasNoEffect) { |
| 4786 | if (PrevDecl) { |
| 4787 | // Remove the previous declaration from the folding set, since we want |
| 4788 | // to introduce a new declaration. |
| 4789 | ClassTemplate->getSpecializations().RemoveNode(PrevDecl); |
| 4790 | ClassTemplate->getSpecializations().FindNodeOrInsertPos(ID, InsertPos); |
| 4791 | } |
| 4792 | // Insert the new specialization. |
| 4793 | ClassTemplate->getSpecializations().InsertNode(Specialization, InsertPos); |
| 4794 | } |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4795 | } |
| 4796 | |
| 4797 | // Build the fully-sugared type for this explicit instantiation as |
| 4798 | // the user wrote in the explicit instantiation itself. This means |
| 4799 | // that we'll pretty-print the type retrieved from the |
| 4800 | // specialization's declaration the way that the user actually wrote |
| 4801 | // the explicit instantiation, rather than formatting the name based |
| 4802 | // on the "canonical" representation used to store the template |
| 4803 | // arguments in the specialization. |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 4804 | TypeSourceInfo *WrittenTy |
| 4805 | = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc, |
| 4806 | TemplateArgs, |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4807 | Context.getTypeDeclType(Specialization)); |
| 4808 | Specialization->setTypeAsWritten(WrittenTy); |
| 4809 | TemplateArgsIn.release(); |
| 4810 | |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 4811 | // Set source locations for keywords. |
| 4812 | Specialization->setExternLoc(ExternLoc); |
| 4813 | Specialization->setTemplateKeywordLoc(TemplateLoc); |
| 4814 | |
| 4815 | // Add the explicit instantiation into its lexical context. However, |
| 4816 | // since explicit instantiations are never found by name lookup, we |
| 4817 | // just put it into the declaration context directly. |
| 4818 | Specialization->setLexicalDeclContext(CurContext); |
| 4819 | CurContext->addDecl(Specialization); |
| 4820 | |
| 4821 | // Syntax is now OK, so return if it has no other effect on semantics. |
| 4822 | if (HasNoEffect) { |
| 4823 | // Set the template specialization kind. |
| 4824 | Specialization->setTemplateSpecializationKind(TSK); |
| 4825 | return DeclPtrTy::make(Specialization); |
Douglas Gregor | d78f598 | 2009-11-25 06:01:46 +0000 | [diff] [blame] | 4826 | } |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4827 | |
| 4828 | // C++ [temp.explicit]p3: |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4829 | // A definition of a class template or class member template |
| 4830 | // shall be in scope at the point of the explicit instantiation of |
| 4831 | // the class template or class member template. |
| 4832 | // |
| 4833 | // This check comes when we actually try to perform the |
| 4834 | // instantiation. |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 4835 | ClassTemplateSpecializationDecl *Def |
| 4836 | = cast_or_null<ClassTemplateSpecializationDecl>( |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 4837 | Specialization->getDefinition()); |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 4838 | if (!Def) |
Douglas Gregor | 972e6ce | 2009-10-27 06:26:26 +0000 | [diff] [blame] | 4839 | InstantiateClassTemplateSpecialization(TemplateNameLoc, Specialization, TSK); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 4840 | else if (TSK == TSK_ExplicitInstantiationDefinition) { |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 4841 | MarkVTableUsed(TemplateNameLoc, Specialization, true); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 4842 | Specialization->setPointOfInstantiation(Def->getPointOfInstantiation()); |
| 4843 | } |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 4844 | |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 4845 | // Instantiate the members of this class template specialization. |
| 4846 | Def = cast_or_null<ClassTemplateSpecializationDecl>( |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 4847 | Specialization->getDefinition()); |
Rafael Espindola | b0f65ca | 2010-03-22 23:12:48 +0000 | [diff] [blame] | 4848 | if (Def) { |
Rafael Espindola | f075b22 | 2010-03-23 19:55:22 +0000 | [diff] [blame] | 4849 | TemplateSpecializationKind Old_TSK = Def->getTemplateSpecializationKind(); |
| 4850 | |
| 4851 | // Fix a TSK_ExplicitInstantiationDeclaration followed by a |
| 4852 | // TSK_ExplicitInstantiationDefinition |
| 4853 | if (Old_TSK == TSK_ExplicitInstantiationDeclaration && |
| 4854 | TSK == TSK_ExplicitInstantiationDefinition) |
| 4855 | Def->setTemplateSpecializationKind(TSK); |
Rafael Espindola | b0f65ca | 2010-03-22 23:12:48 +0000 | [diff] [blame] | 4856 | |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 4857 | InstantiateClassTemplateSpecializationMembers(TemplateNameLoc, Def, TSK); |
Rafael Espindola | b0f65ca | 2010-03-22 23:12:48 +0000 | [diff] [blame] | 4858 | } |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4859 | |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 4860 | // Set the template specialization kind. |
| 4861 | Specialization->setTemplateSpecializationKind(TSK); |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4862 | return DeclPtrTy::make(Specialization); |
| 4863 | } |
| 4864 | |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 4865 | // Explicit instantiation of a member class of a class template. |
| 4866 | Sema::DeclResult |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4867 | Sema::ActOnExplicitInstantiation(Scope *S, |
Douglas Gregor | 45f9655 | 2009-09-04 06:33:52 +0000 | [diff] [blame] | 4868 | SourceLocation ExternLoc, |
| 4869 | SourceLocation TemplateLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4870 | unsigned TagSpec, |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 4871 | SourceLocation KWLoc, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 4872 | CXXScopeSpec &SS, |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 4873 | IdentifierInfo *Name, |
| 4874 | SourceLocation NameLoc, |
| 4875 | AttributeList *Attr) { |
| 4876 | |
Douglas Gregor | 402abb5 | 2009-05-28 23:31:59 +0000 | [diff] [blame] | 4877 | bool Owned = false; |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 4878 | bool IsDependent = false; |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 4879 | DeclPtrTy TagD = ActOnTag(S, TagSpec, Action::TUK_Reference, |
Douglas Gregor | 7cdbc58 | 2009-07-22 23:48:44 +0000 | [diff] [blame] | 4880 | KWLoc, SS, Name, NameLoc, Attr, AS_none, |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 4881 | MultiTemplateParamsArg(*this, 0, 0), |
| 4882 | Owned, IsDependent); |
| 4883 | assert(!IsDependent && "explicit instantiation of dependent name not yet handled"); |
| 4884 | |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 4885 | if (!TagD) |
| 4886 | return true; |
| 4887 | |
| 4888 | TagDecl *Tag = cast<TagDecl>(TagD.getAs<Decl>()); |
| 4889 | if (Tag->isEnum()) { |
| 4890 | Diag(TemplateLoc, diag::err_explicit_instantiation_enum) |
| 4891 | << Context.getTypeDeclType(Tag); |
| 4892 | return true; |
| 4893 | } |
| 4894 | |
Douglas Gregor | d0c8737 | 2009-05-27 17:30:49 +0000 | [diff] [blame] | 4895 | if (Tag->isInvalidDecl()) |
| 4896 | return true; |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 4897 | |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 4898 | CXXRecordDecl *Record = cast<CXXRecordDecl>(Tag); |
| 4899 | CXXRecordDecl *Pattern = Record->getInstantiatedFromMemberClass(); |
| 4900 | if (!Pattern) { |
| 4901 | Diag(TemplateLoc, diag::err_explicit_instantiation_nontemplate_type) |
| 4902 | << Context.getTypeDeclType(Record); |
| 4903 | Diag(Record->getLocation(), diag::note_nontemplate_decl_here); |
| 4904 | return true; |
| 4905 | } |
| 4906 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 4907 | // C++0x [temp.explicit]p2: |
| 4908 | // If the explicit instantiation is for a class or member class, the |
| 4909 | // elaborated-type-specifier in the declaration shall include a |
| 4910 | // simple-template-id. |
| 4911 | // |
| 4912 | // C++98 has the same restriction, just worded differently. |
| 4913 | if (!ScopeSpecifierHasTemplateId(SS)) |
| 4914 | Diag(TemplateLoc, diag::err_explicit_instantiation_without_qualified_id) |
| 4915 | << Record << SS.getRange(); |
| 4916 | |
| 4917 | // C++0x [temp.explicit]p2: |
| 4918 | // There are two forms of explicit instantiation: an explicit instantiation |
| 4919 | // definition and an explicit instantiation declaration. An explicit |
| 4920 | // instantiation declaration begins with the extern keyword. [...] |
Douglas Gregor | a74bbe2 | 2009-10-14 21:46:58 +0000 | [diff] [blame] | 4921 | TemplateSpecializationKind TSK |
| 4922 | = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition |
| 4923 | : TSK_ExplicitInstantiationDeclaration; |
| 4924 | |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 4925 | // C++0x [temp.explicit]p2: |
| 4926 | // [...] An explicit instantiation shall appear in an enclosing |
| 4927 | // namespace of its template. [...] |
| 4928 | // |
| 4929 | // This is C++ DR 275. |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 4930 | CheckExplicitInstantiationScope(*this, Record, NameLoc, true); |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4931 | |
| 4932 | // Verify that it is okay to explicitly instantiate here. |
Douglas Gregor | 583f33b | 2009-10-15 18:07:02 +0000 | [diff] [blame] | 4933 | CXXRecordDecl *PrevDecl |
| 4934 | = cast_or_null<CXXRecordDecl>(Record->getPreviousDeclaration()); |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 4935 | if (!PrevDecl && Record->getDefinition()) |
Douglas Gregor | 583f33b | 2009-10-15 18:07:02 +0000 | [diff] [blame] | 4936 | PrevDecl = Record; |
| 4937 | if (PrevDecl) { |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4938 | MemberSpecializationInfo *MSInfo = PrevDecl->getMemberSpecializationInfo(); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 4939 | bool HasNoEffect = false; |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4940 | assert(MSInfo && "No member specialization information?"); |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 4941 | if (CheckSpecializationInstantiationRedecl(TemplateLoc, TSK, |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4942 | PrevDecl, |
| 4943 | MSInfo->getTemplateSpecializationKind(), |
| 4944 | MSInfo->getPointOfInstantiation(), |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 4945 | HasNoEffect)) |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4946 | return true; |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 4947 | if (HasNoEffect) |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4948 | return TagD; |
| 4949 | } |
| 4950 | |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 4951 | CXXRecordDecl *RecordDef |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 4952 | = cast_or_null<CXXRecordDecl>(Record->getDefinition()); |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 4953 | if (!RecordDef) { |
Douglas Gregor | bf7643e | 2009-10-15 12:53:22 +0000 | [diff] [blame] | 4954 | // C++ [temp.explicit]p3: |
| 4955 | // A definition of a member class of a class template shall be in scope |
| 4956 | // at the point of an explicit instantiation of the member class. |
| 4957 | CXXRecordDecl *Def |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 4958 | = cast_or_null<CXXRecordDecl>(Pattern->getDefinition()); |
Douglas Gregor | bf7643e | 2009-10-15 12:53:22 +0000 | [diff] [blame] | 4959 | if (!Def) { |
Douglas Gregor | e2d3a3d | 2009-10-15 14:05:49 +0000 | [diff] [blame] | 4960 | Diag(TemplateLoc, diag::err_explicit_instantiation_undefined_member) |
| 4961 | << 0 << Record->getDeclName() << Record->getDeclContext(); |
Douglas Gregor | bf7643e | 2009-10-15 12:53:22 +0000 | [diff] [blame] | 4962 | Diag(Pattern->getLocation(), diag::note_forward_declaration) |
| 4963 | << Pattern; |
| 4964 | return true; |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 4965 | } else { |
| 4966 | if (InstantiateClass(NameLoc, Record, Def, |
| 4967 | getTemplateInstantiationArgs(Record), |
| 4968 | TSK)) |
| 4969 | return true; |
| 4970 | |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 4971 | RecordDef = cast_or_null<CXXRecordDecl>(Record->getDefinition()); |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 4972 | if (!RecordDef) |
| 4973 | return true; |
| 4974 | } |
| 4975 | } |
| 4976 | |
| 4977 | // Instantiate all of the members of the class. |
| 4978 | InstantiateClassMembers(NameLoc, RecordDef, |
| 4979 | getTemplateInstantiationArgs(Record), TSK); |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 4980 | |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 4981 | if (TSK == TSK_ExplicitInstantiationDefinition) |
| 4982 | MarkVTableUsed(NameLoc, RecordDef, true); |
| 4983 | |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 4984 | // FIXME: We don't have any representation for explicit instantiations of |
| 4985 | // member classes. Such a representation is not needed for compilation, but it |
| 4986 | // should be available for clients that want to see all of the declarations in |
| 4987 | // the source code. |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 4988 | return TagD; |
| 4989 | } |
| 4990 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4991 | Sema::DeclResult Sema::ActOnExplicitInstantiation(Scope *S, |
| 4992 | SourceLocation ExternLoc, |
| 4993 | SourceLocation TemplateLoc, |
| 4994 | Declarator &D) { |
| 4995 | // Explicit instantiations always require a name. |
| 4996 | DeclarationName Name = GetNameForDeclarator(D); |
| 4997 | if (!Name) { |
| 4998 | if (!D.isInvalidType()) |
| 4999 | Diag(D.getDeclSpec().getSourceRange().getBegin(), |
| 5000 | diag::err_explicit_instantiation_requires_name) |
| 5001 | << D.getDeclSpec().getSourceRange() |
| 5002 | << D.getSourceRange(); |
| 5003 | |
| 5004 | return true; |
| 5005 | } |
| 5006 | |
| 5007 | // The scope passed in may not be a decl scope. Zip up the scope tree until |
| 5008 | // we find one that is. |
| 5009 | while ((S->getFlags() & Scope::DeclScope) == 0 || |
| 5010 | (S->getFlags() & Scope::TemplateParamScope) != 0) |
| 5011 | S = S->getParent(); |
| 5012 | |
| 5013 | // Determine the type of the declaration. |
John McCall | bf1a028 | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 5014 | TypeSourceInfo *T = GetTypeForDeclarator(D, S); |
| 5015 | QualType R = T->getType(); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5016 | if (R.isNull()) |
| 5017 | return true; |
| 5018 | |
| 5019 | if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef) { |
| 5020 | // Cannot explicitly instantiate a typedef. |
| 5021 | Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_of_typedef) |
| 5022 | << Name; |
| 5023 | return true; |
| 5024 | } |
| 5025 | |
Douglas Gregor | 663b5a0 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 5026 | // C++0x [temp.explicit]p1: |
| 5027 | // [...] An explicit instantiation of a function template shall not use the |
| 5028 | // inline or constexpr specifiers. |
| 5029 | // Presumably, this also applies to member functions of class templates as |
| 5030 | // well. |
| 5031 | if (D.getDeclSpec().isInlineSpecified() && getLangOptions().CPlusPlus0x) |
| 5032 | Diag(D.getDeclSpec().getInlineSpecLoc(), |
| 5033 | diag::err_explicit_instantiation_inline) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 5034 | <<FixItHint::CreateRemoval(D.getDeclSpec().getInlineSpecLoc()); |
Douglas Gregor | 663b5a0 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 5035 | |
| 5036 | // FIXME: check for constexpr specifier. |
| 5037 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5038 | // C++0x [temp.explicit]p2: |
| 5039 | // There are two forms of explicit instantiation: an explicit instantiation |
| 5040 | // definition and an explicit instantiation declaration. An explicit |
| 5041 | // instantiation declaration begins with the extern keyword. [...] |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5042 | TemplateSpecializationKind TSK |
| 5043 | = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition |
| 5044 | : TSK_ExplicitInstantiationDeclaration; |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5045 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 5046 | LookupResult Previous(*this, Name, D.getIdentifierLoc(), LookupOrdinaryName); |
| 5047 | LookupParsedName(Previous, S, &D.getCXXScopeSpec()); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5048 | |
| 5049 | if (!R->isFunctionType()) { |
| 5050 | // C++ [temp.explicit]p1: |
| 5051 | // A [...] static data member of a class template can be explicitly |
| 5052 | // instantiated from the member definition associated with its class |
| 5053 | // template. |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 5054 | if (Previous.isAmbiguous()) |
| 5055 | return true; |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5056 | |
John McCall | 1bcee0a | 2009-12-02 08:25:40 +0000 | [diff] [blame] | 5057 | VarDecl *Prev = Previous.getAsSingle<VarDecl>(); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5058 | if (!Prev || !Prev->isStaticDataMember()) { |
| 5059 | // We expect to see a data data member here. |
| 5060 | Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_not_known) |
| 5061 | << Name; |
| 5062 | for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end(); |
| 5063 | P != PEnd; ++P) |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 5064 | Diag((*P)->getLocation(), diag::note_explicit_instantiation_here); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5065 | return true; |
| 5066 | } |
| 5067 | |
| 5068 | if (!Prev->getInstantiatedFromStaticDataMember()) { |
| 5069 | // FIXME: Check for explicit specialization? |
| 5070 | Diag(D.getIdentifierLoc(), |
| 5071 | diag::err_explicit_instantiation_data_member_not_instantiated) |
| 5072 | << Prev; |
| 5073 | Diag(Prev->getLocation(), diag::note_explicit_instantiation_here); |
| 5074 | // FIXME: Can we provide a note showing where this was declared? |
| 5075 | return true; |
| 5076 | } |
| 5077 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5078 | // C++0x [temp.explicit]p2: |
| 5079 | // If the explicit instantiation is for a member function, a member class |
| 5080 | // or a static data member of a class template specialization, the name of |
| 5081 | // the class template specialization in the qualified-id for the member |
| 5082 | // name shall be a simple-template-id. |
| 5083 | // |
| 5084 | // C++98 has the same restriction, just worded differently. |
| 5085 | if (!ScopeSpecifierHasTemplateId(D.getCXXScopeSpec())) |
| 5086 | Diag(D.getIdentifierLoc(), |
| 5087 | diag::err_explicit_instantiation_without_qualified_id) |
| 5088 | << Prev << D.getCXXScopeSpec().getRange(); |
| 5089 | |
| 5090 | // Check the scope of this explicit instantiation. |
| 5091 | CheckExplicitInstantiationScope(*this, Prev, D.getIdentifierLoc(), true); |
| 5092 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5093 | // Verify that it is okay to explicitly instantiate here. |
| 5094 | MemberSpecializationInfo *MSInfo = Prev->getMemberSpecializationInfo(); |
| 5095 | assert(MSInfo && "Missing static data member specialization info?"); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5096 | bool HasNoEffect = false; |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 5097 | if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK, Prev, |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5098 | MSInfo->getTemplateSpecializationKind(), |
| 5099 | MSInfo->getPointOfInstantiation(), |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5100 | HasNoEffect)) |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5101 | return true; |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5102 | if (HasNoEffect) |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5103 | return DeclPtrTy(); |
| 5104 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5105 | // Instantiate static data member. |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 5106 | Prev->setTemplateSpecializationKind(TSK, D.getIdentifierLoc()); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5107 | if (TSK == TSK_ExplicitInstantiationDefinition) |
Douglas Gregor | e2d3a3d | 2009-10-15 14:05:49 +0000 | [diff] [blame] | 5108 | InstantiateStaticDataMemberDefinition(D.getIdentifierLoc(), Prev, false, |
| 5109 | /*DefinitionRequired=*/true); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5110 | |
| 5111 | // FIXME: Create an ExplicitInstantiation node? |
| 5112 | return DeclPtrTy(); |
| 5113 | } |
| 5114 | |
Douglas Gregor | 0b60d9e | 2009-09-25 23:53:26 +0000 | [diff] [blame] | 5115 | // If the declarator is a template-id, translate the parser's template |
| 5116 | // argument list into our AST format. |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 5117 | bool HasExplicitTemplateArgs = false; |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 5118 | TemplateArgumentListInfo TemplateArgs; |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 5119 | if (D.getName().getKind() == UnqualifiedId::IK_TemplateId) { |
| 5120 | TemplateIdAnnotation *TemplateId = D.getName().TemplateId; |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 5121 | TemplateArgs.setLAngleLoc(TemplateId->LAngleLoc); |
| 5122 | TemplateArgs.setRAngleLoc(TemplateId->RAngleLoc); |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 5123 | ASTTemplateArgsPtr TemplateArgsPtr(*this, |
| 5124 | TemplateId->getTemplateArgs(), |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 5125 | TemplateId->NumArgs); |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 5126 | translateTemplateArguments(TemplateArgsPtr, TemplateArgs); |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 5127 | HasExplicitTemplateArgs = true; |
Douglas Gregor | b2f81cf | 2009-10-01 23:51:25 +0000 | [diff] [blame] | 5128 | TemplateArgsPtr.release(); |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 5129 | } |
Douglas Gregor | 0b60d9e | 2009-09-25 23:53:26 +0000 | [diff] [blame] | 5130 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5131 | // C++ [temp.explicit]p1: |
| 5132 | // A [...] function [...] can be explicitly instantiated from its template. |
| 5133 | // A member function [...] of a class template can be explicitly |
| 5134 | // instantiated from the member definition associated with its class |
| 5135 | // template. |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5136 | UnresolvedSet<8> Matches; |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5137 | for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end(); |
| 5138 | P != PEnd; ++P) { |
| 5139 | NamedDecl *Prev = *P; |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 5140 | if (!HasExplicitTemplateArgs) { |
| 5141 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Prev)) { |
| 5142 | if (Context.hasSameUnqualifiedType(Method->getType(), R)) { |
| 5143 | Matches.clear(); |
Douglas Gregor | 48026d2 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 5144 | |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5145 | Matches.addDecl(Method, P.getAccess()); |
Douglas Gregor | 48026d2 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 5146 | if (Method->getTemplateSpecializationKind() == TSK_Undeclared) |
| 5147 | break; |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 5148 | } |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5149 | } |
| 5150 | } |
| 5151 | |
| 5152 | FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Prev); |
| 5153 | if (!FunTmpl) |
| 5154 | continue; |
| 5155 | |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 5156 | TemplateDeductionInfo Info(Context, D.getIdentifierLoc()); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5157 | FunctionDecl *Specialization = 0; |
| 5158 | if (TemplateDeductionResult TDK |
Douglas Gregor | 48026d2 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 5159 | = DeduceTemplateArguments(FunTmpl, |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 5160 | (HasExplicitTemplateArgs ? &TemplateArgs : 0), |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5161 | R, Specialization, Info)) { |
| 5162 | // FIXME: Keep track of almost-matches? |
| 5163 | (void)TDK; |
| 5164 | continue; |
| 5165 | } |
| 5166 | |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5167 | Matches.addDecl(Specialization, P.getAccess()); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5168 | } |
| 5169 | |
| 5170 | // Find the most specialized function template specialization. |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5171 | UnresolvedSetIterator Result |
| 5172 | = getMostSpecialized(Matches.begin(), Matches.end(), TPOC_Other, |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5173 | D.getIdentifierLoc(), |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 5174 | PDiag(diag::err_explicit_instantiation_not_known) << Name, |
| 5175 | PDiag(diag::err_explicit_instantiation_ambiguous) << Name, |
| 5176 | PDiag(diag::note_explicit_instantiation_candidate)); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5177 | |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5178 | if (Result == Matches.end()) |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5179 | return true; |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5180 | |
| 5181 | // Ignore access control bits, we don't need them for redeclaration checking. |
| 5182 | FunctionDecl *Specialization = cast<FunctionDecl>(*Result); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5183 | |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 5184 | if (Specialization->getTemplateSpecializationKind() == TSK_Undeclared) { |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5185 | Diag(D.getIdentifierLoc(), |
| 5186 | diag::err_explicit_instantiation_member_function_not_instantiated) |
| 5187 | << Specialization |
| 5188 | << (Specialization->getTemplateSpecializationKind() == |
| 5189 | TSK_ExplicitSpecialization); |
| 5190 | Diag(Specialization->getLocation(), diag::note_explicit_instantiation_here); |
| 5191 | return true; |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 5192 | } |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5193 | |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 5194 | FunctionDecl *PrevDecl = Specialization->getPreviousDeclaration(); |
Douglas Gregor | 583f33b | 2009-10-15 18:07:02 +0000 | [diff] [blame] | 5195 | if (!PrevDecl && Specialization->isThisDeclarationADefinition()) |
| 5196 | PrevDecl = Specialization; |
| 5197 | |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 5198 | if (PrevDecl) { |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5199 | bool HasNoEffect = false; |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 5200 | if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK, |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 5201 | PrevDecl, |
| 5202 | PrevDecl->getTemplateSpecializationKind(), |
| 5203 | PrevDecl->getPointOfInstantiation(), |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5204 | HasNoEffect)) |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 5205 | return true; |
| 5206 | |
| 5207 | // FIXME: We may still want to build some representation of this |
| 5208 | // explicit specialization. |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5209 | if (HasNoEffect) |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 5210 | return DeclPtrTy(); |
| 5211 | } |
Anders Carlsson | 26d6e9d | 2009-11-24 05:34:41 +0000 | [diff] [blame] | 5212 | |
| 5213 | Specialization->setTemplateSpecializationKind(TSK, D.getIdentifierLoc()); |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 5214 | |
| 5215 | if (TSK == TSK_ExplicitInstantiationDefinition) |
| 5216 | InstantiateFunctionDefinition(D.getIdentifierLoc(), Specialization, |
| 5217 | false, /*DefinitionRequired=*/true); |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 5218 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5219 | // C++0x [temp.explicit]p2: |
| 5220 | // If the explicit instantiation is for a member function, a member class |
| 5221 | // or a static data member of a class template specialization, the name of |
| 5222 | // the class template specialization in the qualified-id for the member |
| 5223 | // name shall be a simple-template-id. |
| 5224 | // |
| 5225 | // C++98 has the same restriction, just worded differently. |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 5226 | FunctionTemplateDecl *FunTmpl = Specialization->getPrimaryTemplate(); |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 5227 | if (D.getName().getKind() != UnqualifiedId::IK_TemplateId && !FunTmpl && |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5228 | D.getCXXScopeSpec().isSet() && |
| 5229 | !ScopeSpecifierHasTemplateId(D.getCXXScopeSpec())) |
| 5230 | Diag(D.getIdentifierLoc(), |
| 5231 | diag::err_explicit_instantiation_without_qualified_id) |
| 5232 | << Specialization << D.getCXXScopeSpec().getRange(); |
| 5233 | |
| 5234 | CheckExplicitInstantiationScope(*this, |
| 5235 | FunTmpl? (NamedDecl *)FunTmpl |
| 5236 | : Specialization->getInstantiatedFromMemberFunction(), |
| 5237 | D.getIdentifierLoc(), |
| 5238 | D.getCXXScopeSpec().isSet()); |
| 5239 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5240 | // FIXME: Create some kind of ExplicitInstantiationDecl here. |
| 5241 | return DeclPtrTy(); |
| 5242 | } |
| 5243 | |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5244 | Sema::TypeResult |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 5245 | Sema::ActOnDependentTag(Scope *S, unsigned TagSpec, TagUseKind TUK, |
| 5246 | const CXXScopeSpec &SS, IdentifierInfo *Name, |
| 5247 | SourceLocation TagLoc, SourceLocation NameLoc) { |
| 5248 | // This has to hold, because SS is expected to be defined. |
| 5249 | assert(Name && "Expected a name in a dependent tag"); |
| 5250 | |
| 5251 | NestedNameSpecifier *NNS |
| 5252 | = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); |
| 5253 | if (!NNS) |
| 5254 | return true; |
| 5255 | |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 5256 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
Daniel Dunbar | 12c0ade | 2010-04-01 16:50:48 +0000 | [diff] [blame] | 5257 | |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 5258 | if (TUK == TUK_Declaration || TUK == TUK_Definition) { |
| 5259 | Diag(NameLoc, diag::err_dependent_tag_decl) |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 5260 | << (TUK == TUK_Definition) << Kind << SS.getRange(); |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 5261 | return true; |
| 5262 | } |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 5263 | |
| 5264 | ElaboratedTypeKeyword Kwd = TypeWithKeyword::getKeywordForTagTypeKind(Kind); |
| 5265 | return Context.getDependentNameType(Kwd, NNS, Name).getAsOpaquePtr(); |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 5266 | } |
| 5267 | |
| 5268 | Sema::TypeResult |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5269 | Sema::ActOnTypenameType(SourceLocation TypenameLoc, const CXXScopeSpec &SS, |
| 5270 | const IdentifierInfo &II, SourceLocation IdLoc) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5271 | NestedNameSpecifier *NNS |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5272 | = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); |
| 5273 | if (!NNS) |
| 5274 | return true; |
| 5275 | |
Douglas Gregor | 107de90 | 2010-04-24 15:35:55 +0000 | [diff] [blame] | 5276 | QualType T = CheckTypenameType(ETK_Typename, NNS, II, |
Abramo Bagnara | e4da7a0 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 5277 | TypenameLoc, SS.getRange(), IdLoc); |
Douglas Gregor | 31a19b6 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 5278 | if (T.isNull()) |
| 5279 | return true; |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 5280 | |
| 5281 | TypeSourceInfo *TSI = Context.CreateTypeSourceInfo(T); |
| 5282 | if (isa<DependentNameType>(T)) { |
| 5283 | DependentNameTypeLoc TL = cast<DependentNameTypeLoc>(TSI->getTypeLoc()); |
John McCall | 4e44983 | 2010-05-28 23:32:21 +0000 | [diff] [blame] | 5284 | TL.setKeywordLoc(TypenameLoc); |
| 5285 | TL.setQualifierRange(SS.getRange()); |
| 5286 | TL.setNameLoc(IdLoc); |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 5287 | } else { |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 5288 | ElaboratedTypeLoc TL = cast<ElaboratedTypeLoc>(TSI->getTypeLoc()); |
John McCall | 4e44983 | 2010-05-28 23:32:21 +0000 | [diff] [blame] | 5289 | TL.setKeywordLoc(TypenameLoc); |
| 5290 | TL.setQualifierRange(SS.getRange()); |
| 5291 | cast<TypeSpecTypeLoc>(TL.getNamedTypeLoc()).setNameLoc(IdLoc); |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 5292 | } |
| 5293 | |
| 5294 | return CreateLocInfoType(T, TSI).getAsOpaquePtr(); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5295 | } |
| 5296 | |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 5297 | Sema::TypeResult |
| 5298 | Sema::ActOnTypenameType(SourceLocation TypenameLoc, const CXXScopeSpec &SS, |
| 5299 | SourceLocation TemplateLoc, TypeTy *Ty) { |
John McCall | 4e44983 | 2010-05-28 23:32:21 +0000 | [diff] [blame] | 5300 | TypeSourceInfo *InnerTSI = 0; |
| 5301 | QualType T = GetTypeFromParser(Ty, &InnerTSI); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5302 | NestedNameSpecifier *NNS |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 5303 | = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); |
John McCall | 4e44983 | 2010-05-28 23:32:21 +0000 | [diff] [blame] | 5304 | |
| 5305 | assert(isa<TemplateSpecializationType>(T) && |
| 5306 | "Expected a template specialization type"); |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 5307 | |
Douglas Gregor | 6946baf | 2009-09-02 13:05:45 +0000 | [diff] [blame] | 5308 | if (computeDeclContext(SS, false)) { |
| 5309 | // If we can compute a declaration context, then the "typename" |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 5310 | // keyword was superfluous. Just build an ElaboratedType to keep |
Douglas Gregor | 6946baf | 2009-09-02 13:05:45 +0000 | [diff] [blame] | 5311 | // track of the nested-name-specifier. |
John McCall | 4e44983 | 2010-05-28 23:32:21 +0000 | [diff] [blame] | 5312 | |
| 5313 | // Push the inner type, preserving its source locations if possible. |
| 5314 | TypeLocBuilder Builder; |
| 5315 | if (InnerTSI) |
| 5316 | Builder.pushFullCopy(InnerTSI->getTypeLoc()); |
| 5317 | else |
| 5318 | Builder.push<TemplateSpecializationTypeLoc>(T).initialize(TemplateLoc); |
| 5319 | |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 5320 | T = Context.getElaboratedType(ETK_Typename, NNS, T); |
John McCall | 4e44983 | 2010-05-28 23:32:21 +0000 | [diff] [blame] | 5321 | ElaboratedTypeLoc TL = Builder.push<ElaboratedTypeLoc>(T); |
| 5322 | TL.setKeywordLoc(TypenameLoc); |
| 5323 | TL.setQualifierRange(SS.getRange()); |
| 5324 | |
| 5325 | TypeSourceInfo *TSI = Builder.getTypeSourceInfo(Context, T); |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 5326 | return CreateLocInfoType(T, TSI).getAsOpaquePtr(); |
Douglas Gregor | 6946baf | 2009-09-02 13:05:45 +0000 | [diff] [blame] | 5327 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5328 | |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 5329 | // TODO: it's really silly that we make a template specialization |
| 5330 | // type earlier only to drop it again here. |
| 5331 | TemplateSpecializationType *TST = cast<TemplateSpecializationType>(T); |
| 5332 | DependentTemplateName *DTN = |
| 5333 | TST->getTemplateName().getAsDependentTemplateName(); |
| 5334 | assert(DTN && "dependent template has non-dependent name?"); |
| 5335 | T = Context.getDependentTemplateSpecializationType(ETK_Typename, NNS, |
| 5336 | DTN->getIdentifier(), |
| 5337 | TST->getNumArgs(), |
| 5338 | TST->getArgs()); |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 5339 | TypeSourceInfo *TSI = Context.CreateTypeSourceInfo(T); |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 5340 | DependentTemplateSpecializationTypeLoc TL = |
| 5341 | cast<DependentTemplateSpecializationTypeLoc>(TSI->getTypeLoc()); |
| 5342 | if (InnerTSI) { |
| 5343 | TemplateSpecializationTypeLoc TSTL = |
| 5344 | cast<TemplateSpecializationTypeLoc>(InnerTSI->getTypeLoc()); |
| 5345 | TL.setLAngleLoc(TSTL.getLAngleLoc()); |
| 5346 | TL.setRAngleLoc(TSTL.getRAngleLoc()); |
| 5347 | for (unsigned I = 0, E = TST->getNumArgs(); I != E; ++I) |
| 5348 | TL.setArgLocInfo(I, TSTL.getArgLocInfo(I)); |
| 5349 | } else { |
| 5350 | TL.initializeLocal(SourceLocation()); |
| 5351 | } |
John McCall | 4e44983 | 2010-05-28 23:32:21 +0000 | [diff] [blame] | 5352 | TL.setKeywordLoc(TypenameLoc); |
| 5353 | TL.setQualifierRange(SS.getRange()); |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 5354 | return CreateLocInfoType(T, TSI).getAsOpaquePtr(); |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 5355 | } |
| 5356 | |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5357 | /// \brief Build the type that describes a C++ typename specifier, |
| 5358 | /// e.g., "typename T::type". |
| 5359 | QualType |
Douglas Gregor | 107de90 | 2010-04-24 15:35:55 +0000 | [diff] [blame] | 5360 | Sema::CheckTypenameType(ElaboratedTypeKeyword Keyword, |
| 5361 | NestedNameSpecifier *NNS, const IdentifierInfo &II, |
Abramo Bagnara | e4da7a0 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 5362 | SourceLocation KeywordLoc, SourceRange NNSRange, |
| 5363 | SourceLocation IILoc) { |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 5364 | CXXScopeSpec SS; |
| 5365 | SS.setScopeRep(NNS); |
Abramo Bagnara | e4da7a0 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 5366 | SS.setRange(NNSRange); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5367 | |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 5368 | DeclContext *Ctx = computeDeclContext(SS); |
| 5369 | if (!Ctx) { |
| 5370 | // If the nested-name-specifier is dependent and couldn't be |
| 5371 | // resolved to a type, build a typename type. |
| 5372 | assert(NNS->isDependent()); |
| 5373 | return Context.getDependentNameType(Keyword, NNS, &II); |
Douglas Gregor | 42af25f | 2009-05-11 19:58:34 +0000 | [diff] [blame] | 5374 | } |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5375 | |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 5376 | // If the nested-name-specifier refers to the current instantiation, |
| 5377 | // the "typename" keyword itself is superfluous. In C++03, the |
| 5378 | // program is actually ill-formed. However, DR 382 (in C++0x CD1) |
| 5379 | // allows such extraneous "typename" keywords, and we retroactively |
Douglas Gregor | 732281d | 2010-06-14 22:07:54 +0000 | [diff] [blame^] | 5380 | // 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] | 5381 | |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 5382 | if (RequireCompleteDeclContext(SS, Ctx)) |
| 5383 | return QualType(); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5384 | |
| 5385 | DeclarationName Name(&II); |
Abramo Bagnara | e4da7a0 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 5386 | LookupResult Result(*this, Name, IILoc, LookupOrdinaryName); |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 5387 | LookupQualifiedName(Result, Ctx); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5388 | unsigned DiagID = 0; |
| 5389 | Decl *Referenced = 0; |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 5390 | switch (Result.getResultKind()) { |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5391 | case LookupResult::NotFound: |
Douglas Gregor | 3f09327 | 2009-10-13 21:16:44 +0000 | [diff] [blame] | 5392 | DiagID = diag::err_typename_nested_not_found; |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5393 | break; |
Douglas Gregor | 7d3f576 | 2010-01-15 01:44:47 +0000 | [diff] [blame] | 5394 | |
| 5395 | case LookupResult::NotFoundInCurrentInstantiation: |
| 5396 | // Okay, it's a member of an unknown instantiation. |
Douglas Gregor | 107de90 | 2010-04-24 15:35:55 +0000 | [diff] [blame] | 5397 | return Context.getDependentNameType(Keyword, NNS, &II); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5398 | |
| 5399 | case LookupResult::Found: |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 5400 | if (TypeDecl *Type = dyn_cast<TypeDecl>(Result.getFoundDecl())) { |
Douglas Gregor | 732281d | 2010-06-14 22:07:54 +0000 | [diff] [blame^] | 5401 | if (ActiveTemplateInstantiations.empty() && |
| 5402 | !getLangOptions().CPlusPlus0x && !SS.isEmpty() && |
| 5403 | !isDependentScopeSpecifier(SS)) |
| 5404 | Diag(KeywordLoc.isValid()? KeywordLoc : IILoc, |
| 5405 | diag::ext_typename_nondependent) |
| 5406 | << SourceRange(IILoc) |
| 5407 | << FixItHint::CreateRemoval(KeywordLoc); |
| 5408 | |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 5409 | // We found a type. Build an ElaboratedType, since the |
| 5410 | // typename-specifier was just sugar. |
| 5411 | return Context.getElaboratedType(ETK_Typename, NNS, |
| 5412 | Context.getTypeDeclType(Type)); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5413 | } |
| 5414 | |
| 5415 | DiagID = diag::err_typename_nested_not_type; |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 5416 | Referenced = Result.getFoundDecl(); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5417 | break; |
| 5418 | |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 5419 | case LookupResult::FoundUnresolvedValue: |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 5420 | llvm_unreachable("unresolved using decl in non-dependent context"); |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 5421 | return QualType(); |
| 5422 | |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5423 | case LookupResult::FoundOverloaded: |
| 5424 | DiagID = diag::err_typename_nested_not_type; |
| 5425 | Referenced = *Result.begin(); |
| 5426 | break; |
| 5427 | |
John McCall | 6e24726 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 5428 | case LookupResult::Ambiguous: |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5429 | return QualType(); |
| 5430 | } |
| 5431 | |
| 5432 | // If we get here, it's because name lookup did not find a |
| 5433 | // type. Emit an appropriate diagnostic and return an error. |
Abramo Bagnara | e4da7a0 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 5434 | SourceRange FullRange(KeywordLoc.isValid() ? KeywordLoc : NNSRange.getBegin(), |
| 5435 | IILoc); |
| 5436 | Diag(IILoc, DiagID) << FullRange << Name << Ctx; |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5437 | if (Referenced) |
| 5438 | Diag(Referenced->getLocation(), diag::note_typename_refers_here) |
| 5439 | << Name; |
| 5440 | return QualType(); |
| 5441 | } |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 5442 | |
| 5443 | namespace { |
| 5444 | // See Sema::RebuildTypeInCurrentInstantiation |
Benjamin Kramer | 85b4521 | 2009-11-28 19:45:26 +0000 | [diff] [blame] | 5445 | class CurrentInstantiationRebuilder |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5446 | : public TreeTransform<CurrentInstantiationRebuilder> { |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 5447 | SourceLocation Loc; |
| 5448 | DeclarationName Entity; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5449 | |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 5450 | public: |
Douglas Gregor | 895162d | 2010-04-30 18:55:50 +0000 | [diff] [blame] | 5451 | typedef TreeTransform<CurrentInstantiationRebuilder> inherited; |
| 5452 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5453 | CurrentInstantiationRebuilder(Sema &SemaRef, |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 5454 | SourceLocation Loc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5455 | DeclarationName Entity) |
| 5456 | : TreeTransform<CurrentInstantiationRebuilder>(SemaRef), |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 5457 | Loc(Loc), Entity(Entity) { } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5458 | |
| 5459 | /// \brief Determine whether the given type \p T has already been |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 5460 | /// transformed. |
| 5461 | /// |
| 5462 | /// For the purposes of type reconstruction, a type has already been |
| 5463 | /// transformed if it is NULL or if it is not dependent. |
| 5464 | bool AlreadyTransformed(QualType T) { |
| 5465 | return T.isNull() || !T->isDependentType(); |
| 5466 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5467 | |
| 5468 | /// \brief Returns the location of the entity whose type is being |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 5469 | /// rebuilt. |
| 5470 | SourceLocation getBaseLocation() { return Loc; } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5471 | |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 5472 | /// \brief Returns the name of the entity whose type is being rebuilt. |
| 5473 | DeclarationName getBaseEntity() { return Entity; } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5474 | |
Douglas Gregor | 972e6ce | 2009-10-27 06:26:26 +0000 | [diff] [blame] | 5475 | /// \brief Sets the "base" location and entity when that |
| 5476 | /// information is known based on another transformation. |
| 5477 | void setBase(SourceLocation Loc, DeclarationName Entity) { |
| 5478 | this->Loc = Loc; |
| 5479 | this->Entity = Entity; |
| 5480 | } |
| 5481 | |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 5482 | /// \brief Transforms an expression by returning the expression itself |
| 5483 | /// (an identity function). |
| 5484 | /// |
| 5485 | /// FIXME: This is completely unsafe; we will need to actually clone the |
| 5486 | /// expressions. |
| 5487 | Sema::OwningExprResult TransformExpr(Expr *E) { |
Douglas Gregor | 895162d | 2010-04-30 18:55:50 +0000 | [diff] [blame] | 5488 | return getSema().Owned(E->Retain()); |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 5489 | } |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 5490 | }; |
| 5491 | } |
| 5492 | |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 5493 | /// \brief Rebuilds a type within the context of the current instantiation. |
| 5494 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5495 | /// 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] | 5496 | /// a class template (or class template partial specialization) that was parsed |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5497 | /// and constructed before we entered the scope of the class template (or |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 5498 | /// partial specialization thereof). This routine will rebuild that type now |
| 5499 | /// that we have entered the declarator's scope, which may produce different |
| 5500 | /// canonical types, e.g., |
| 5501 | /// |
| 5502 | /// \code |
| 5503 | /// template<typename T> |
| 5504 | /// struct X { |
| 5505 | /// typedef T* pointer; |
| 5506 | /// pointer data(); |
| 5507 | /// }; |
| 5508 | /// |
| 5509 | /// template<typename T> |
| 5510 | /// typename X<T>::pointer X<T>::data() { ... } |
| 5511 | /// \endcode |
| 5512 | /// |
Douglas Gregor | 4714c12 | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 5513 | /// 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] | 5514 | /// since we do not know that we can look into X<T> when we parsed the type. |
| 5515 | /// This function will rebuild the type, performing the lookup of "pointer" |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 5516 | /// 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] | 5517 | /// as the canonical type of T*, allowing the return types of the out-of-line |
| 5518 | /// definition and the declaration to match. |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 5519 | TypeSourceInfo *Sema::RebuildTypeInCurrentInstantiation(TypeSourceInfo *T, |
| 5520 | SourceLocation Loc, |
| 5521 | DeclarationName Name) { |
| 5522 | if (!T || !T->getType()->isDependentType()) |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 5523 | return T; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5524 | |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 5525 | CurrentInstantiationRebuilder Rebuilder(*this, Loc, Name); |
| 5526 | return Rebuilder.TransformType(T); |
Benjamin Kramer | 27ba2f0 | 2009-08-11 22:33:06 +0000 | [diff] [blame] | 5527 | } |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 5528 | |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 5529 | bool Sema::RebuildNestedNameSpecifierInCurrentInstantiation(CXXScopeSpec &SS) { |
| 5530 | if (SS.isInvalid()) return true; |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 5531 | |
| 5532 | NestedNameSpecifier *NNS = static_cast<NestedNameSpecifier*>(SS.getScopeRep()); |
| 5533 | CurrentInstantiationRebuilder Rebuilder(*this, SS.getRange().getBegin(), |
| 5534 | DeclarationName()); |
| 5535 | NestedNameSpecifier *Rebuilt = |
| 5536 | Rebuilder.TransformNestedNameSpecifier(NNS, SS.getRange()); |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 5537 | if (!Rebuilt) return true; |
| 5538 | |
| 5539 | SS.setScopeRep(Rebuilt); |
| 5540 | return false; |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 5541 | } |
| 5542 | |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 5543 | /// \brief Produces a formatted string that describes the binding of |
| 5544 | /// template parameters to template arguments. |
| 5545 | std::string |
| 5546 | Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params, |
| 5547 | const TemplateArgumentList &Args) { |
Douglas Gregor | 9148c3f | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 5548 | // FIXME: For variadic templates, we'll need to get the structured list. |
| 5549 | return getTemplateArgumentBindingsText(Params, Args.getFlatArgumentList(), |
| 5550 | Args.flat_size()); |
| 5551 | } |
| 5552 | |
| 5553 | std::string |
| 5554 | Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params, |
| 5555 | const TemplateArgument *Args, |
| 5556 | unsigned NumArgs) { |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 5557 | std::string Result; |
| 5558 | |
Douglas Gregor | 9148c3f | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 5559 | if (!Params || Params->size() == 0 || NumArgs == 0) |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 5560 | return Result; |
| 5561 | |
| 5562 | for (unsigned I = 0, N = Params->size(); I != N; ++I) { |
Douglas Gregor | 9148c3f | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 5563 | if (I >= NumArgs) |
| 5564 | break; |
| 5565 | |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 5566 | if (I == 0) |
| 5567 | Result += "[with "; |
| 5568 | else |
| 5569 | Result += ", "; |
| 5570 | |
| 5571 | if (const IdentifierInfo *Id = Params->getParam(I)->getIdentifier()) { |
| 5572 | Result += Id->getName(); |
| 5573 | } else { |
| 5574 | Result += '$'; |
| 5575 | Result += llvm::utostr(I); |
| 5576 | } |
| 5577 | |
| 5578 | Result += " = "; |
| 5579 | |
| 5580 | switch (Args[I].getKind()) { |
| 5581 | case TemplateArgument::Null: |
| 5582 | Result += "<no value>"; |
| 5583 | break; |
| 5584 | |
| 5585 | case TemplateArgument::Type: { |
| 5586 | std::string TypeStr; |
| 5587 | Args[I].getAsType().getAsStringInternal(TypeStr, |
| 5588 | Context.PrintingPolicy); |
| 5589 | Result += TypeStr; |
| 5590 | break; |
| 5591 | } |
| 5592 | |
| 5593 | case TemplateArgument::Declaration: { |
| 5594 | bool Unnamed = true; |
| 5595 | if (NamedDecl *ND = dyn_cast_or_null<NamedDecl>(Args[I].getAsDecl())) { |
| 5596 | if (ND->getDeclName()) { |
| 5597 | Unnamed = false; |
| 5598 | Result += ND->getNameAsString(); |
| 5599 | } |
| 5600 | } |
| 5601 | |
| 5602 | if (Unnamed) { |
| 5603 | Result += "<anonymous>"; |
| 5604 | } |
| 5605 | break; |
| 5606 | } |
| 5607 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 5608 | case TemplateArgument::Template: { |
| 5609 | std::string Str; |
| 5610 | llvm::raw_string_ostream OS(Str); |
| 5611 | Args[I].getAsTemplate().print(OS, Context.PrintingPolicy); |
| 5612 | Result += OS.str(); |
| 5613 | break; |
| 5614 | } |
| 5615 | |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 5616 | case TemplateArgument::Integral: { |
| 5617 | Result += Args[I].getAsIntegral()->toString(10); |
| 5618 | break; |
| 5619 | } |
| 5620 | |
| 5621 | case TemplateArgument::Expression: { |
Douglas Gregor | 77e2c67 | 2010-04-29 04:55:13 +0000 | [diff] [blame] | 5622 | // FIXME: This is non-optimal, since we're regurgitating the |
| 5623 | // expression we were given. |
| 5624 | std::string Str; |
| 5625 | { |
| 5626 | llvm::raw_string_ostream OS(Str); |
| 5627 | Args[I].getAsExpr()->printPretty(OS, Context, 0, |
| 5628 | Context.PrintingPolicy); |
| 5629 | } |
| 5630 | Result += Str; |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 5631 | break; |
| 5632 | } |
| 5633 | |
| 5634 | case TemplateArgument::Pack: |
| 5635 | // FIXME: Format template argument packs |
| 5636 | Result += "<template argument pack>"; |
| 5637 | break; |
| 5638 | } |
| 5639 | } |
| 5640 | |
| 5641 | Result += ']'; |
| 5642 | return Result; |
| 5643 | } |