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) { |
| 66 | LookupResult::Filter filter = R.makeFilter(); |
| 67 | while (filter.hasNext()) { |
| 68 | NamedDecl *Orig = filter.next(); |
| 69 | NamedDecl *Repl = isAcceptableTemplateName(C, Orig->getUnderlyingDecl()); |
| 70 | if (!Repl) |
| 71 | filter.erase(); |
| 72 | else if (Repl != Orig) |
| 73 | filter.replace(Repl); |
| 74 | } |
| 75 | filter.done(); |
| 76 | } |
| 77 | |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 78 | TemplateNameKind Sema::isTemplateName(Scope *S, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 79 | CXXScopeSpec &SS, |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 80 | UnqualifiedId &Name, |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 81 | TypeTy *ObjectTypePtr, |
Douglas Gregor | 495c35d | 2009-08-25 22:51:20 +0000 | [diff] [blame] | 82 | bool EnteringContext, |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 83 | TemplateTy &TemplateResult) { |
Douglas Gregor | b862b8f | 2010-01-11 23:29:10 +0000 | [diff] [blame] | 84 | assert(getLangOptions().CPlusPlus && "No template names in C!"); |
| 85 | |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 86 | DeclarationName TName; |
| 87 | |
| 88 | switch (Name.getKind()) { |
| 89 | case UnqualifiedId::IK_Identifier: |
| 90 | TName = DeclarationName(Name.Identifier); |
| 91 | break; |
| 92 | |
| 93 | case UnqualifiedId::IK_OperatorFunctionId: |
| 94 | TName = Context.DeclarationNames.getCXXOperatorName( |
| 95 | Name.OperatorFunctionId.Operator); |
| 96 | break; |
| 97 | |
Sean Hunt | e6252d1 | 2009-11-28 08:58:14 +0000 | [diff] [blame] | 98 | case UnqualifiedId::IK_LiteralOperatorId: |
Sean Hunt | 3e518bd | 2009-11-29 07:34:05 +0000 | [diff] [blame] | 99 | TName = Context.DeclarationNames.getCXXLiteralOperatorName(Name.Identifier); |
| 100 | break; |
Sean Hunt | e6252d1 | 2009-11-28 08:58:14 +0000 | [diff] [blame] | 101 | |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 102 | default: |
| 103 | return TNK_Non_template; |
| 104 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 105 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 106 | QualType ObjectType = QualType::getFromOpaquePtr(ObjectTypePtr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 107 | |
Douglas Gregor | bfea239 | 2009-12-31 08:11:17 +0000 | [diff] [blame] | 108 | LookupResult R(*this, TName, Name.getSourceRange().getBegin(), |
| 109 | LookupOrdinaryName); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 110 | R.suppressDiagnostics(); |
| 111 | LookupTemplateName(R, S, SS, ObjectType, EnteringContext); |
| 112 | if (R.empty()) |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 113 | return TNK_Non_template; |
| 114 | |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 115 | TemplateName Template; |
| 116 | TemplateNameKind TemplateKind; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 117 | |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 118 | unsigned ResultCount = R.end() - R.begin(); |
| 119 | if (ResultCount > 1) { |
| 120 | // We assume that we'll preserve the qualifier from a function |
| 121 | // template name in other ways. |
| 122 | Template = Context.getOverloadedTemplateName(R.begin(), R.end()); |
| 123 | TemplateKind = TNK_Function_template; |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 124 | } else { |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 125 | TemplateDecl *TD = cast<TemplateDecl>((*R.begin())->getUnderlyingDecl()); |
| 126 | |
| 127 | if (SS.isSet() && !SS.isInvalid()) { |
| 128 | NestedNameSpecifier *Qualifier |
| 129 | = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); |
| 130 | Template = Context.getQualifiedTemplateName(Qualifier, false, TD); |
| 131 | } else { |
| 132 | Template = TemplateName(TD); |
| 133 | } |
| 134 | |
| 135 | if (isa<FunctionTemplateDecl>(TD)) |
| 136 | TemplateKind = TNK_Function_template; |
| 137 | else { |
| 138 | assert(isa<ClassTemplateDecl>(TD) || isa<TemplateTemplateParmDecl>(TD)); |
| 139 | TemplateKind = TNK_Type_template; |
| 140 | } |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 141 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 142 | |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 143 | TemplateResult = TemplateTy::make(Template); |
| 144 | return TemplateKind; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 145 | } |
| 146 | |
Douglas Gregor | 84d0a19 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 147 | bool Sema::DiagnoseUnknownTemplateName(const IdentifierInfo &II, |
| 148 | SourceLocation IILoc, |
| 149 | Scope *S, |
| 150 | const CXXScopeSpec *SS, |
| 151 | TemplateTy &SuggestedTemplate, |
| 152 | TemplateNameKind &SuggestedKind) { |
| 153 | // We can't recover unless there's a dependent scope specifier preceding the |
| 154 | // template name. |
| 155 | if (!SS || !SS->isSet() || !isDependentScopeSpecifier(*SS) || |
| 156 | computeDeclContext(*SS)) |
| 157 | return false; |
| 158 | |
| 159 | // The code is missing a 'template' keyword prior to the dependent template |
| 160 | // name. |
| 161 | NestedNameSpecifier *Qualifier = (NestedNameSpecifier*)SS->getScopeRep(); |
| 162 | Diag(IILoc, diag::err_template_kw_missing) |
| 163 | << Qualifier << II.getName() |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 164 | << FixItHint::CreateInsertion(IILoc, "template "); |
Douglas Gregor | 84d0a19 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 165 | SuggestedTemplate |
| 166 | = TemplateTy::make(Context.getDependentTemplateName(Qualifier, &II)); |
| 167 | SuggestedKind = TNK_Dependent_template_name; |
| 168 | return true; |
| 169 | } |
| 170 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 171 | void Sema::LookupTemplateName(LookupResult &Found, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 172 | Scope *S, CXXScopeSpec &SS, |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 173 | QualType ObjectType, |
| 174 | bool EnteringContext) { |
| 175 | // Determine where to perform name lookup |
| 176 | DeclContext *LookupCtx = 0; |
| 177 | bool isDependent = false; |
| 178 | if (!ObjectType.isNull()) { |
| 179 | // This nested-name-specifier occurs in a member access expression, e.g., |
| 180 | // x->B::f, and we are looking into the type of the object. |
| 181 | assert(!SS.isSet() && "ObjectType and scope specifier cannot coexist"); |
| 182 | LookupCtx = computeDeclContext(ObjectType); |
| 183 | isDependent = ObjectType->isDependentType(); |
| 184 | assert((isDependent || !ObjectType->isIncompleteType()) && |
| 185 | "Caller should have completed object type"); |
| 186 | } else if (SS.isSet()) { |
| 187 | // This nested-name-specifier occurs after another nested-name-specifier, |
| 188 | // so long into the context associated with the prior nested-name-specifier. |
| 189 | LookupCtx = computeDeclContext(SS, EnteringContext); |
| 190 | isDependent = isDependentScopeSpecifier(SS); |
| 191 | |
| 192 | // The declaration context must be complete. |
| 193 | if (LookupCtx && RequireCompleteDeclContext(SS)) |
| 194 | return; |
| 195 | } |
| 196 | |
| 197 | bool ObjectTypeSearchedInScope = false; |
| 198 | if (LookupCtx) { |
| 199 | // Perform "qualified" name lookup into the declaration context we |
| 200 | // computed, which is either the type of the base of a member access |
| 201 | // expression or the declaration context associated with a prior |
| 202 | // nested-name-specifier. |
| 203 | LookupQualifiedName(Found, LookupCtx); |
| 204 | |
| 205 | if (!ObjectType.isNull() && Found.empty()) { |
| 206 | // C++ [basic.lookup.classref]p1: |
| 207 | // In a class member access expression (5.2.5), if the . or -> token is |
| 208 | // immediately followed by an identifier followed by a <, the |
| 209 | // identifier must be looked up to determine whether the < is the |
| 210 | // beginning of a template argument list (14.2) or a less-than operator. |
| 211 | // The identifier is first looked up in the class of the object |
| 212 | // expression. If the identifier is not found, it is then looked up in |
| 213 | // the context of the entire postfix-expression and shall name a class |
| 214 | // or function template. |
| 215 | // |
| 216 | // FIXME: When we're instantiating a template, do we actually have to |
| 217 | // look in the scope of the template? Seems fishy... |
| 218 | if (S) LookupName(Found, S); |
| 219 | ObjectTypeSearchedInScope = true; |
| 220 | } |
| 221 | } else if (isDependent) { |
Douglas Gregor | 2e93388 | 2010-01-12 17:06:20 +0000 | [diff] [blame] | 222 | // We cannot look into a dependent object type or nested nme |
| 223 | // specifier. |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 224 | return; |
| 225 | } else { |
| 226 | // Perform unqualified name lookup in the current scope. |
| 227 | LookupName(Found, S); |
| 228 | } |
| 229 | |
| 230 | // FIXME: Cope with ambiguous name-lookup results. |
| 231 | assert(!Found.isAmbiguous() && |
| 232 | "Cannot handle template name-lookup ambiguities"); |
| 233 | |
Douglas Gregor | 2e93388 | 2010-01-12 17:06:20 +0000 | [diff] [blame] | 234 | if (Found.empty() && !isDependent) { |
Douglas Gregor | bfea239 | 2009-12-31 08:11:17 +0000 | [diff] [blame] | 235 | // If we did not find any names, attempt to correct any typos. |
| 236 | DeclarationName Name = Found.getLookupName(); |
| 237 | if (CorrectTypo(Found, S, &SS, LookupCtx)) { |
| 238 | FilterAcceptableTemplateNames(Context, Found); |
| 239 | if (!Found.empty() && isa<TemplateDecl>(*Found.begin())) { |
| 240 | if (LookupCtx) |
| 241 | Diag(Found.getNameLoc(), diag::err_no_member_template_suggest) |
| 242 | << Name << LookupCtx << Found.getLookupName() << SS.getRange() |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 243 | << FixItHint::CreateReplacement(Found.getNameLoc(), |
Douglas Gregor | bfea239 | 2009-12-31 08:11:17 +0000 | [diff] [blame] | 244 | Found.getLookupName().getAsString()); |
| 245 | else |
| 246 | Diag(Found.getNameLoc(), diag::err_no_template_suggest) |
| 247 | << Name << Found.getLookupName() |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 248 | << FixItHint::CreateReplacement(Found.getNameLoc(), |
Douglas Gregor | bfea239 | 2009-12-31 08:11:17 +0000 | [diff] [blame] | 249 | Found.getLookupName().getAsString()); |
Douglas Gregor | 67dd1d4 | 2010-01-07 00:17:44 +0000 | [diff] [blame] | 250 | if (TemplateDecl *Template = Found.getAsSingle<TemplateDecl>()) |
| 251 | Diag(Template->getLocation(), diag::note_previous_decl) |
| 252 | << Template->getDeclName(); |
Douglas Gregor | bfea239 | 2009-12-31 08:11:17 +0000 | [diff] [blame] | 253 | } else |
| 254 | Found.clear(); |
| 255 | } else { |
| 256 | Found.clear(); |
| 257 | } |
| 258 | } |
| 259 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 260 | FilterAcceptableTemplateNames(Context, Found); |
| 261 | if (Found.empty()) |
| 262 | return; |
| 263 | |
| 264 | if (S && !ObjectType.isNull() && !ObjectTypeSearchedInScope) { |
| 265 | // C++ [basic.lookup.classref]p1: |
| 266 | // [...] If the lookup in the class of the object expression finds a |
| 267 | // template, the name is also looked up in the context of the entire |
| 268 | // postfix-expression and [...] |
| 269 | // |
| 270 | LookupResult FoundOuter(*this, Found.getLookupName(), Found.getNameLoc(), |
| 271 | LookupOrdinaryName); |
| 272 | LookupName(FoundOuter, S); |
| 273 | FilterAcceptableTemplateNames(Context, FoundOuter); |
| 274 | // FIXME: Handle ambiguities in this lookup better |
| 275 | |
| 276 | if (FoundOuter.empty()) { |
| 277 | // - if the name is not found, the name found in the class of the |
| 278 | // object expression is used, otherwise |
| 279 | } else if (!FoundOuter.getAsSingle<ClassTemplateDecl>()) { |
| 280 | // - if the name is found in the context of the entire |
| 281 | // postfix-expression and does not name a class template, the name |
| 282 | // found in the class of the object expression is used, otherwise |
| 283 | } else { |
| 284 | // - if the name found is a class template, it must refer to the same |
| 285 | // entity as the one found in the class of the object expression, |
| 286 | // otherwise the program is ill-formed. |
| 287 | if (!Found.isSingleResult() || |
| 288 | Found.getFoundDecl()->getCanonicalDecl() |
| 289 | != FoundOuter.getFoundDecl()->getCanonicalDecl()) { |
| 290 | Diag(Found.getNameLoc(), |
| 291 | diag::err_nested_name_member_ref_lookup_ambiguous) |
| 292 | << Found.getLookupName(); |
| 293 | Diag(Found.getRepresentativeDecl()->getLocation(), |
| 294 | diag::note_ambig_member_ref_object_type) |
| 295 | << ObjectType; |
| 296 | Diag(FoundOuter.getFoundDecl()->getLocation(), |
| 297 | diag::note_ambig_member_ref_scope); |
| 298 | |
| 299 | // Recover by taking the template that we found in the object |
| 300 | // expression's type. |
| 301 | } |
| 302 | } |
| 303 | } |
| 304 | } |
| 305 | |
John McCall | 2f841ba | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 306 | /// ActOnDependentIdExpression - Handle a dependent id-expression that |
| 307 | /// was just parsed. This is only possible with an explicit scope |
| 308 | /// specifier naming a dependent type. |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 309 | Sema::OwningExprResult |
| 310 | Sema::ActOnDependentIdExpression(const CXXScopeSpec &SS, |
| 311 | DeclarationName Name, |
| 312 | SourceLocation NameLoc, |
John McCall | 2f841ba | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 313 | bool isAddressOfOperand, |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 314 | const TemplateArgumentListInfo *TemplateArgs) { |
| 315 | NestedNameSpecifier *Qualifier |
| 316 | = static_cast<NestedNameSpecifier*>(SS.getScopeRep()); |
| 317 | |
John McCall | 2f841ba | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 318 | if (!isAddressOfOperand && |
| 319 | isa<CXXMethodDecl>(CurContext) && |
| 320 | cast<CXXMethodDecl>(CurContext)->isInstance()) { |
| 321 | QualType ThisType = cast<CXXMethodDecl>(CurContext)->getThisType(Context); |
| 322 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 323 | // Since the 'this' expression is synthesized, we don't need to |
| 324 | // perform the double-lookup check. |
| 325 | NamedDecl *FirstQualifierInScope = 0; |
| 326 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 327 | return Owned(CXXDependentScopeMemberExpr::Create(Context, |
| 328 | /*This*/ 0, ThisType, |
| 329 | /*IsArrow*/ true, |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 330 | /*Op*/ SourceLocation(), |
| 331 | Qualifier, SS.getRange(), |
| 332 | FirstQualifierInScope, |
| 333 | Name, NameLoc, |
| 334 | TemplateArgs)); |
| 335 | } |
| 336 | |
| 337 | return BuildDependentDeclRefExpr(SS, Name, NameLoc, TemplateArgs); |
| 338 | } |
| 339 | |
| 340 | Sema::OwningExprResult |
| 341 | Sema::BuildDependentDeclRefExpr(const CXXScopeSpec &SS, |
| 342 | DeclarationName Name, |
| 343 | SourceLocation NameLoc, |
| 344 | const TemplateArgumentListInfo *TemplateArgs) { |
| 345 | return Owned(DependentScopeDeclRefExpr::Create(Context, |
| 346 | static_cast<NestedNameSpecifier*>(SS.getScopeRep()), |
| 347 | SS.getRange(), |
| 348 | Name, NameLoc, |
| 349 | TemplateArgs)); |
Douglas Gregor | d6fb7ef | 2008-12-18 19:37:40 +0000 | [diff] [blame] | 350 | } |
| 351 | |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 352 | /// DiagnoseTemplateParameterShadow - Produce a diagnostic complaining |
| 353 | /// that the template parameter 'PrevDecl' is being shadowed by a new |
| 354 | /// declaration at location Loc. Returns true to indicate that this is |
| 355 | /// an error, and false otherwise. |
| 356 | bool Sema::DiagnoseTemplateParameterShadow(SourceLocation Loc, Decl *PrevDecl) { |
Douglas Gregor | f57172b | 2008-12-08 18:40:42 +0000 | [diff] [blame] | 357 | assert(PrevDecl->isTemplateParameter() && "Not a template parameter"); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 358 | |
| 359 | // Microsoft Visual C++ permits template parameters to be shadowed. |
| 360 | if (getLangOptions().Microsoft) |
| 361 | return false; |
| 362 | |
| 363 | // C++ [temp.local]p4: |
| 364 | // A template-parameter shall not be redeclared within its |
| 365 | // scope (including nested scopes). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 366 | Diag(Loc, diag::err_template_param_shadow) |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 367 | << cast<NamedDecl>(PrevDecl)->getDeclName(); |
| 368 | Diag(PrevDecl->getLocation(), diag::note_template_param_here); |
| 369 | return true; |
| 370 | } |
| 371 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 372 | /// AdjustDeclIfTemplate - If the given decl happens to be a template, reset |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 373 | /// the parameter D to reference the templated declaration and return a pointer |
| 374 | /// to the template declaration. Otherwise, do nothing to D and return null. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 375 | TemplateDecl *Sema::AdjustDeclIfTemplate(DeclPtrTy &D) { |
Douglas Gregor | 13d2d6c | 2009-10-06 21:27:51 +0000 | [diff] [blame] | 376 | if (TemplateDecl *Temp = dyn_cast_or_null<TemplateDecl>(D.getAs<Decl>())) { |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 377 | D = DeclPtrTy::make(Temp->getTemplatedDecl()); |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 378 | return Temp; |
| 379 | } |
| 380 | return 0; |
| 381 | } |
| 382 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 383 | static TemplateArgumentLoc translateTemplateArgument(Sema &SemaRef, |
| 384 | const ParsedTemplateArgument &Arg) { |
| 385 | |
| 386 | switch (Arg.getKind()) { |
| 387 | case ParsedTemplateArgument::Type: { |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 388 | TypeSourceInfo *DI; |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 389 | QualType T = SemaRef.GetTypeFromParser(Arg.getAsType(), &DI); |
| 390 | if (!DI) |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 391 | DI = SemaRef.Context.getTrivialTypeSourceInfo(T, Arg.getLocation()); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 392 | return TemplateArgumentLoc(TemplateArgument(T), DI); |
| 393 | } |
| 394 | |
| 395 | case ParsedTemplateArgument::NonType: { |
| 396 | Expr *E = static_cast<Expr *>(Arg.getAsExpr()); |
| 397 | return TemplateArgumentLoc(TemplateArgument(E), E); |
| 398 | } |
| 399 | |
| 400 | case ParsedTemplateArgument::Template: { |
| 401 | TemplateName Template |
| 402 | = TemplateName::getFromVoidPointer(Arg.getAsTemplate().get()); |
| 403 | return TemplateArgumentLoc(TemplateArgument(Template), |
| 404 | Arg.getScopeSpec().getRange(), |
| 405 | Arg.getLocation()); |
| 406 | } |
| 407 | } |
| 408 | |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 409 | llvm_unreachable("Unhandled parsed template argument"); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 410 | return TemplateArgumentLoc(); |
| 411 | } |
| 412 | |
| 413 | /// \brief Translates template arguments as provided by the parser |
| 414 | /// into template arguments used by semantic analysis. |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 415 | void Sema::translateTemplateArguments(const ASTTemplateArgsPtr &TemplateArgsIn, |
| 416 | TemplateArgumentListInfo &TemplateArgs) { |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 417 | for (unsigned I = 0, Last = TemplateArgsIn.size(); I != Last; ++I) |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 418 | TemplateArgs.addArgument(translateTemplateArgument(*this, |
| 419 | TemplateArgsIn[I])); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 420 | } |
| 421 | |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 422 | /// ActOnTypeParameter - Called when a C++ template type parameter |
| 423 | /// (e.g., "typename T") has been parsed. Typename specifies whether |
| 424 | /// the keyword "typename" was used to declare the type parameter |
| 425 | /// (otherwise, "class" was used), and KeyLoc is the location of the |
| 426 | /// "class" or "typename" keyword. ParamName is the name of the |
| 427 | /// parameter (NULL indicates an unnamed template parameter) and |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 428 | /// ParamName is the location of the parameter name (if any). |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 429 | /// If the type parameter has a default argument, it will be added |
| 430 | /// later via ActOnTypeParameterDefault. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 431 | Sema::DeclPtrTy Sema::ActOnTypeParameter(Scope *S, bool Typename, bool Ellipsis, |
Anders Carlsson | 941df7d | 2009-06-12 19:58:00 +0000 | [diff] [blame] | 432 | SourceLocation EllipsisLoc, |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 433 | SourceLocation KeyLoc, |
| 434 | IdentifierInfo *ParamName, |
| 435 | SourceLocation ParamNameLoc, |
| 436 | unsigned Depth, unsigned Position) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 437 | assert(S->isTemplateParamScope() && |
| 438 | "Template type parameter not in template parameter scope!"); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 439 | bool Invalid = false; |
| 440 | |
| 441 | if (ParamName) { |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 442 | NamedDecl *PrevDecl = LookupSingleName(S, ParamName, LookupTagName); |
Douglas Gregor | f57172b | 2008-12-08 18:40:42 +0000 | [diff] [blame] | 443 | if (PrevDecl && PrevDecl->isTemplateParameter()) |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 444 | Invalid = Invalid || DiagnoseTemplateParameterShadow(ParamNameLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 445 | PrevDecl); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 446 | } |
| 447 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 448 | SourceLocation Loc = ParamNameLoc; |
| 449 | if (!ParamName) |
| 450 | Loc = KeyLoc; |
| 451 | |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 452 | TemplateTypeParmDecl *Param |
John McCall | 7a9813c | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 453 | = TemplateTypeParmDecl::Create(Context, Context.getTranslationUnitDecl(), |
| 454 | Loc, Depth, Position, ParamName, Typename, |
Anders Carlsson | 6d845ae | 2009-06-12 22:23:22 +0000 | [diff] [blame] | 455 | Ellipsis); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 456 | if (Invalid) |
| 457 | Param->setInvalidDecl(); |
| 458 | |
| 459 | if (ParamName) { |
| 460 | // Add the template parameter into the current scope. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 461 | S->AddDecl(DeclPtrTy::make(Param)); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 462 | IdResolver.AddDecl(Param); |
| 463 | } |
| 464 | |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 465 | return DeclPtrTy::make(Param); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 466 | } |
| 467 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 468 | /// ActOnTypeParameterDefault - Adds a default argument (the type |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 469 | /// Default) to the given template type parameter (TypeParam). |
| 470 | void Sema::ActOnTypeParameterDefault(DeclPtrTy TypeParam, |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 471 | SourceLocation EqualLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 472 | SourceLocation DefaultLoc, |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 473 | TypeTy *DefaultT) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 474 | TemplateTypeParmDecl *Parm |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 475 | = cast<TemplateTypeParmDecl>(TypeParam.getAs<Decl>()); |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 476 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 477 | TypeSourceInfo *DefaultTInfo; |
| 478 | GetTypeFromParser(DefaultT, &DefaultTInfo); |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 479 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 480 | assert(DefaultTInfo && "expected source information for type"); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 481 | |
Anders Carlsson | 9c4c5c8 | 2009-06-12 22:30:13 +0000 | [diff] [blame] | 482 | // C++0x [temp.param]p9: |
| 483 | // A default template-argument may be specified for any kind of |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 484 | // template-parameter that is not a template parameter pack. |
Anders Carlsson | 9c4c5c8 | 2009-06-12 22:30:13 +0000 | [diff] [blame] | 485 | if (Parm->isParameterPack()) { |
| 486 | Diag(DefaultLoc, diag::err_template_param_pack_default_arg); |
Anders Carlsson | 9c4c5c8 | 2009-06-12 22:30:13 +0000 | [diff] [blame] | 487 | return; |
| 488 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 489 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 490 | // C++ [temp.param]p14: |
| 491 | // A template-parameter shall not be used in its own default argument. |
| 492 | // FIXME: Implement this check! Needs a recursive walk over the types. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 493 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 494 | // Check the template argument itself. |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 495 | if (CheckTemplateArgument(Parm, DefaultTInfo)) { |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 496 | Parm->setInvalidDecl(); |
| 497 | return; |
| 498 | } |
| 499 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 500 | Parm->setDefaultArgument(DefaultTInfo, false); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 501 | } |
| 502 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 503 | /// \brief Check that the type of a non-type template parameter is |
| 504 | /// well-formed. |
| 505 | /// |
| 506 | /// \returns the (possibly-promoted) parameter type if valid; |
| 507 | /// otherwise, produces a diagnostic and returns a NULL type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 508 | QualType |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 509 | Sema::CheckNonTypeTemplateParameterType(QualType T, SourceLocation Loc) { |
| 510 | // C++ [temp.param]p4: |
| 511 | // |
| 512 | // A non-type template-parameter shall have one of the following |
| 513 | // (optionally cv-qualified) types: |
| 514 | // |
| 515 | // -- integral or enumeration type, |
| 516 | if (T->isIntegralType() || T->isEnumeralType() || |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 517 | // -- pointer to object or pointer to function, |
| 518 | (T->isPointerType() && |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 519 | (T->getAs<PointerType>()->getPointeeType()->isObjectType() || |
| 520 | T->getAs<PointerType>()->getPointeeType()->isFunctionType())) || |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 521 | // -- reference to object or reference to function, |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 522 | T->isReferenceType() || |
| 523 | // -- pointer to member. |
| 524 | T->isMemberPointerType() || |
| 525 | // If T is a dependent type, we can't do the check now, so we |
| 526 | // assume that it is well-formed. |
| 527 | T->isDependentType()) |
| 528 | return T; |
| 529 | // C++ [temp.param]p8: |
| 530 | // |
| 531 | // A non-type template-parameter of type "array of T" or |
| 532 | // "function returning T" is adjusted to be of type "pointer to |
| 533 | // T" or "pointer to function returning T", respectively. |
| 534 | else if (T->isArrayType()) |
| 535 | // FIXME: Keep the type prior to promotion? |
| 536 | return Context.getArrayDecayedType(T); |
| 537 | else if (T->isFunctionType()) |
| 538 | // FIXME: Keep the type prior to promotion? |
| 539 | return Context.getPointerType(T); |
| 540 | |
| 541 | Diag(Loc, diag::err_template_nontype_parm_bad_type) |
| 542 | << T; |
| 543 | |
| 544 | return QualType(); |
| 545 | } |
| 546 | |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 547 | /// ActOnNonTypeTemplateParameter - Called when a C++ non-type |
| 548 | /// template parameter (e.g., "int Size" in "template<int Size> |
| 549 | /// class Array") has been parsed. S is the current scope and D is |
| 550 | /// the parsed declarator. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 551 | Sema::DeclPtrTy Sema::ActOnNonTypeTemplateParameter(Scope *S, Declarator &D, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 552 | unsigned Depth, |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 553 | unsigned Position) { |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 554 | TypeSourceInfo *TInfo = 0; |
| 555 | QualType T = GetTypeForDeclarator(D, S, &TInfo); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 556 | |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 557 | assert(S->isTemplateParamScope() && |
| 558 | "Non-type template parameter not in template parameter scope!"); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 559 | bool Invalid = false; |
| 560 | |
| 561 | IdentifierInfo *ParamName = D.getIdentifier(); |
| 562 | if (ParamName) { |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 563 | NamedDecl *PrevDecl = LookupSingleName(S, ParamName, LookupTagName); |
Douglas Gregor | f57172b | 2008-12-08 18:40:42 +0000 | [diff] [blame] | 564 | if (PrevDecl && PrevDecl->isTemplateParameter()) |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 565 | Invalid = Invalid || DiagnoseTemplateParameterShadow(D.getIdentifierLoc(), |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 566 | PrevDecl); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 567 | } |
| 568 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 569 | T = CheckNonTypeTemplateParameterType(T, D.getIdentifierLoc()); |
Douglas Gregor | ceef30c | 2009-03-09 16:46:39 +0000 | [diff] [blame] | 570 | if (T.isNull()) { |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 571 | T = Context.IntTy; // Recover with an 'int' type. |
Douglas Gregor | ceef30c | 2009-03-09 16:46:39 +0000 | [diff] [blame] | 572 | Invalid = true; |
| 573 | } |
Douglas Gregor | 5d290d5 | 2009-02-10 17:43:50 +0000 | [diff] [blame] | 574 | |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 575 | NonTypeTemplateParmDecl *Param |
John McCall | 7a9813c | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 576 | = NonTypeTemplateParmDecl::Create(Context, Context.getTranslationUnitDecl(), |
| 577 | D.getIdentifierLoc(), |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 578 | Depth, Position, ParamName, T, TInfo); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 579 | if (Invalid) |
| 580 | Param->setInvalidDecl(); |
| 581 | |
| 582 | if (D.getIdentifier()) { |
| 583 | // Add the template parameter into the current scope. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 584 | S->AddDecl(DeclPtrTy::make(Param)); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 585 | IdResolver.AddDecl(Param); |
| 586 | } |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 587 | return DeclPtrTy::make(Param); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 588 | } |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 589 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 590 | /// \brief Adds a default argument to the given non-type template |
| 591 | /// parameter. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 592 | void Sema::ActOnNonTypeTemplateParameterDefault(DeclPtrTy TemplateParamD, |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 593 | SourceLocation EqualLoc, |
| 594 | ExprArg DefaultE) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 595 | NonTypeTemplateParmDecl *TemplateParm |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 596 | = cast<NonTypeTemplateParmDecl>(TemplateParamD.getAs<Decl>()); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 597 | Expr *Default = static_cast<Expr *>(DefaultE.get()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 598 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 599 | // C++ [temp.param]p14: |
| 600 | // A template-parameter shall not be used in its own default argument. |
| 601 | // FIXME: Implement this check! Needs a recursive walk over the types. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 602 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 603 | // Check the well-formedness of the default template argument. |
Douglas Gregor | 02cbbd2 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 604 | TemplateArgument Converted; |
| 605 | if (CheckTemplateArgument(TemplateParm, TemplateParm->getType(), Default, |
| 606 | Converted)) { |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 607 | TemplateParm->setInvalidDecl(); |
| 608 | return; |
| 609 | } |
| 610 | |
Anders Carlsson | e9146f2 | 2009-05-01 19:49:17 +0000 | [diff] [blame] | 611 | TemplateParm->setDefaultArgument(DefaultE.takeAs<Expr>()); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 612 | } |
| 613 | |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 614 | |
| 615 | /// ActOnTemplateTemplateParameter - Called when a C++ template template |
| 616 | /// parameter (e.g. T in template <template <typename> class T> class array) |
| 617 | /// has been parsed. S is the current scope. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 618 | Sema::DeclPtrTy Sema::ActOnTemplateTemplateParameter(Scope* S, |
| 619 | SourceLocation TmpLoc, |
| 620 | TemplateParamsTy *Params, |
| 621 | IdentifierInfo *Name, |
| 622 | SourceLocation NameLoc, |
| 623 | unsigned Depth, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 624 | unsigned Position) { |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 625 | assert(S->isTemplateParamScope() && |
| 626 | "Template template parameter not in template parameter scope!"); |
| 627 | |
| 628 | // Construct the parameter object. |
| 629 | TemplateTemplateParmDecl *Param = |
John McCall | 7a9813c | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 630 | TemplateTemplateParmDecl::Create(Context, Context.getTranslationUnitDecl(), |
| 631 | TmpLoc, Depth, Position, Name, |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 632 | (TemplateParameterList*)Params); |
| 633 | |
| 634 | // Make sure the parameter is valid. |
| 635 | // FIXME: Decl object is not currently invalidated anywhere so this doesn't |
| 636 | // do anything yet. However, if the template parameter list or (eventual) |
| 637 | // default value is ever invalidated, that will propagate here. |
| 638 | bool Invalid = false; |
| 639 | if (Invalid) { |
| 640 | Param->setInvalidDecl(); |
| 641 | } |
| 642 | |
| 643 | // If the tt-param has a name, then link the identifier into the scope |
| 644 | // and lookup mechanisms. |
| 645 | if (Name) { |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 646 | S->AddDecl(DeclPtrTy::make(Param)); |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 647 | IdResolver.AddDecl(Param); |
| 648 | } |
| 649 | |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 650 | return DeclPtrTy::make(Param); |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 651 | } |
| 652 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 653 | /// \brief Adds a default argument to the given template template |
| 654 | /// parameter. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 655 | void Sema::ActOnTemplateTemplateParameterDefault(DeclPtrTy TemplateParamD, |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 656 | SourceLocation EqualLoc, |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 657 | const ParsedTemplateArgument &Default) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 658 | TemplateTemplateParmDecl *TemplateParm |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 659 | = cast<TemplateTemplateParmDecl>(TemplateParamD.getAs<Decl>()); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 660 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 661 | // C++ [temp.param]p14: |
| 662 | // A template-parameter shall not be used in its own default argument. |
| 663 | // FIXME: Implement this check! Needs a recursive walk over the types. |
| 664 | |
Douglas Gregor | 9148c3f | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 665 | // Check only that we have a template template argument. We don't want to |
| 666 | // try to check well-formedness now, because our template template parameter |
| 667 | // might have dependent types in its template parameters, which we wouldn't |
| 668 | // be able to match now. |
| 669 | // |
| 670 | // If none of the template template parameter's template arguments mention |
| 671 | // other template parameters, we could actually perform more checking here. |
| 672 | // However, it isn't worth doing. |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 673 | TemplateArgumentLoc DefaultArg = translateTemplateArgument(*this, Default); |
Douglas Gregor | 9148c3f | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 674 | if (DefaultArg.getArgument().getAsTemplate().isNull()) { |
| 675 | Diag(DefaultArg.getLocation(), diag::err_template_arg_not_class_template) |
| 676 | << DefaultArg.getSourceRange(); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 677 | return; |
| 678 | } |
Douglas Gregor | 9148c3f | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 679 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 680 | TemplateParm->setDefaultArgument(DefaultArg); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 681 | } |
| 682 | |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 683 | /// ActOnTemplateParameterList - Builds a TemplateParameterList that |
| 684 | /// contains the template parameters in Params/NumParams. |
| 685 | Sema::TemplateParamsTy * |
| 686 | Sema::ActOnTemplateParameterList(unsigned Depth, |
| 687 | SourceLocation ExportLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 688 | SourceLocation TemplateLoc, |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 689 | SourceLocation LAngleLoc, |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 690 | DeclPtrTy *Params, unsigned NumParams, |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 691 | SourceLocation RAngleLoc) { |
| 692 | if (ExportLoc.isValid()) |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 693 | Diag(ExportLoc, diag::warn_template_export_unsupported); |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 694 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 695 | return TemplateParameterList::Create(Context, TemplateLoc, LAngleLoc, |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 696 | (NamedDecl**)Params, NumParams, |
| 697 | RAngleLoc); |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 698 | } |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 699 | |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 700 | static void SetNestedNameSpecifier(TagDecl *T, const CXXScopeSpec &SS) { |
| 701 | if (SS.isSet()) |
| 702 | T->setQualifierInfo(static_cast<NestedNameSpecifier*>(SS.getScopeRep()), |
| 703 | SS.getRange()); |
| 704 | } |
| 705 | |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 706 | Sema::DeclResult |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 707 | Sema::CheckClassTemplate(Scope *S, unsigned TagSpec, TagUseKind TUK, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 708 | SourceLocation KWLoc, CXXScopeSpec &SS, |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 709 | IdentifierInfo *Name, SourceLocation NameLoc, |
| 710 | AttributeList *Attr, |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 711 | TemplateParameterList *TemplateParams, |
Anders Carlsson | 5aeccdb | 2009-03-26 00:52:18 +0000 | [diff] [blame] | 712 | AccessSpecifier AS) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 713 | assert(TemplateParams && TemplateParams->size() > 0 && |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 714 | "No template parameters"); |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 715 | assert(TUK != TUK_Reference && "Can only declare or define class templates"); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 716 | bool Invalid = false; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 717 | |
| 718 | // Check that we can declare a template here. |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 719 | if (CheckTemplateDeclScope(S, TemplateParams)) |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 720 | return true; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 721 | |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 722 | TagDecl::TagKind Kind = TagDecl::getTagKindForTypeSpec(TagSpec); |
| 723 | assert(Kind != TagDecl::TK_enum && "can't build template of enumerated type"); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 724 | |
| 725 | // There is no such thing as an unnamed class template. |
| 726 | if (!Name) { |
| 727 | Diag(KWLoc, diag::err_template_unnamed_class); |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 728 | return true; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 729 | } |
| 730 | |
| 731 | // Find any previous declaration with this name. |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 732 | DeclContext *SemanticContext; |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 733 | LookupResult Previous(*this, Name, NameLoc, LookupOrdinaryName, |
John McCall | 7d384dd | 2009-11-18 07:57:50 +0000 | [diff] [blame] | 734 | ForRedeclaration); |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 735 | if (SS.isNotEmpty() && !SS.isInvalid()) { |
Douglas Gregor | f0510d4 | 2009-10-12 23:11:44 +0000 | [diff] [blame] | 736 | if (RequireCompleteDeclContext(SS)) |
| 737 | return true; |
| 738 | |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 739 | SemanticContext = computeDeclContext(SS, true); |
| 740 | if (!SemanticContext) { |
| 741 | // FIXME: Produce a reasonable diagnostic here |
| 742 | return true; |
| 743 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 744 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 745 | LookupQualifiedName(Previous, SemanticContext); |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 746 | } else { |
| 747 | SemanticContext = CurContext; |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 748 | LookupName(Previous, S); |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 749 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 750 | |
Douglas Gregor | 57265e3 | 2010-04-12 16:00:01 +0000 | [diff] [blame] | 751 | if (Previous.isAmbiguous()) |
| 752 | return true; |
| 753 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 754 | NamedDecl *PrevDecl = 0; |
| 755 | if (Previous.begin() != Previous.end()) |
Douglas Gregor | 57265e3 | 2010-04-12 16:00:01 +0000 | [diff] [blame] | 756 | PrevDecl = (*Previous.begin())->getUnderlyingDecl(); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 757 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 758 | // If there is a previous declaration with the same name, check |
| 759 | // whether this is a valid redeclaration. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 760 | ClassTemplateDecl *PrevClassTemplate |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 761 | = dyn_cast_or_null<ClassTemplateDecl>(PrevDecl); |
Douglas Gregor | d7e5bdb | 2009-10-09 21:11:42 +0000 | [diff] [blame] | 762 | |
| 763 | // We may have found the injected-class-name of a class template, |
| 764 | // class template partial specialization, or class template specialization. |
| 765 | // In these cases, grab the template that is being defined or specialized. |
| 766 | if (!PrevClassTemplate && PrevDecl && isa<CXXRecordDecl>(PrevDecl) && |
| 767 | cast<CXXRecordDecl>(PrevDecl)->isInjectedClassName()) { |
| 768 | PrevDecl = cast<CXXRecordDecl>(PrevDecl->getDeclContext()); |
| 769 | PrevClassTemplate |
| 770 | = cast<CXXRecordDecl>(PrevDecl)->getDescribedClassTemplate(); |
| 771 | if (!PrevClassTemplate && isa<ClassTemplateSpecializationDecl>(PrevDecl)) { |
| 772 | PrevClassTemplate |
| 773 | = cast<ClassTemplateSpecializationDecl>(PrevDecl) |
| 774 | ->getSpecializedTemplate(); |
| 775 | } |
| 776 | } |
| 777 | |
John McCall | 65c4946 | 2009-12-18 11:25:59 +0000 | [diff] [blame] | 778 | if (TUK == TUK_Friend) { |
John McCall | e129d44 | 2009-12-17 23:21:11 +0000 | [diff] [blame] | 779 | // C++ [namespace.memdef]p3: |
| 780 | // [...] When looking for a prior declaration of a class or a function |
| 781 | // declared as a friend, and when the name of the friend class or |
| 782 | // function is neither a qualified name nor a template-id, scopes outside |
| 783 | // the innermost enclosing namespace scope are not considered. |
| 784 | DeclContext *OutermostContext = CurContext; |
| 785 | while (!OutermostContext->isFileContext()) |
| 786 | OutermostContext = OutermostContext->getLookupParent(); |
John McCall | 65c4946 | 2009-12-18 11:25:59 +0000 | [diff] [blame] | 787 | |
| 788 | if (PrevDecl && |
| 789 | (OutermostContext->Equals(PrevDecl->getDeclContext()) || |
| 790 | OutermostContext->Encloses(PrevDecl->getDeclContext()))) { |
John McCall | e129d44 | 2009-12-17 23:21:11 +0000 | [diff] [blame] | 791 | SemanticContext = PrevDecl->getDeclContext(); |
| 792 | } else { |
| 793 | // Declarations in outer scopes don't matter. However, the outermost |
| 794 | // context we computed is the semantic context for our new |
| 795 | // declaration. |
| 796 | PrevDecl = PrevClassTemplate = 0; |
| 797 | SemanticContext = OutermostContext; |
| 798 | } |
| 799 | |
| 800 | if (CurContext->isDependentContext()) { |
| 801 | // If this is a dependent context, we don't want to link the friend |
| 802 | // class template to the template in scope, because that would perform |
| 803 | // checking of the template parameter lists that can't be performed |
| 804 | // until the outer context is instantiated. |
| 805 | PrevDecl = PrevClassTemplate = 0; |
| 806 | } |
| 807 | } else if (PrevDecl && !isDeclInScope(PrevDecl, SemanticContext, S)) |
| 808 | PrevDecl = PrevClassTemplate = 0; |
Douglas Gregor | 57265e3 | 2010-04-12 16:00:01 +0000 | [diff] [blame] | 809 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 810 | if (PrevClassTemplate) { |
| 811 | // Ensure that the template parameter lists are compatible. |
| 812 | if (!TemplateParameterListsAreEqual(TemplateParams, |
| 813 | PrevClassTemplate->getTemplateParameters(), |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 814 | /*Complain=*/true, |
| 815 | TPL_TemplateMatch)) |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 816 | return true; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 817 | |
| 818 | // C++ [temp.class]p4: |
| 819 | // In a redeclaration, partial specialization, explicit |
| 820 | // specialization or explicit instantiation of a class template, |
| 821 | // the class-key shall agree in kind with the original class |
| 822 | // template declaration (7.1.5.3). |
| 823 | RecordDecl *PrevRecordDecl = PrevClassTemplate->getTemplatedDecl(); |
Douglas Gregor | 501c5ce | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 824 | if (!isAcceptableTagRedeclaration(PrevRecordDecl, Kind, KWLoc, *Name)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 825 | Diag(KWLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 826 | << Name |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 827 | << FixItHint::CreateReplacement(KWLoc, PrevRecordDecl->getKindName()); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 828 | Diag(PrevRecordDecl->getLocation(), diag::note_previous_use); |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 829 | Kind = PrevRecordDecl->getTagKind(); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 830 | } |
| 831 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 832 | // Check for redefinition of this class template. |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 833 | if (TUK == TUK_Definition) { |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 834 | if (TagDecl *Def = PrevRecordDecl->getDefinition()) { |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 835 | Diag(NameLoc, diag::err_redefinition) << Name; |
| 836 | Diag(Def->getLocation(), diag::note_previous_definition); |
| 837 | // FIXME: Would it make sense to try to "forget" the previous |
| 838 | // definition, as part of error recovery? |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 839 | return true; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 840 | } |
| 841 | } |
| 842 | } else if (PrevDecl && PrevDecl->isTemplateParameter()) { |
| 843 | // Maybe we will complain about the shadowed template parameter. |
| 844 | DiagnoseTemplateParameterShadow(NameLoc, PrevDecl); |
| 845 | // Just pretend that we didn't see the previous declaration. |
| 846 | PrevDecl = 0; |
| 847 | } else if (PrevDecl) { |
| 848 | // C++ [temp]p5: |
| 849 | // A class template shall not have the same name as any other |
| 850 | // template, class, function, object, enumeration, enumerator, |
| 851 | // namespace, or type in the same scope (3.3), except as specified |
| 852 | // in (14.5.4). |
| 853 | Diag(NameLoc, diag::err_redefinition_different_kind) << Name; |
| 854 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 855 | return true; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 856 | } |
| 857 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 858 | // Check the template parameter list of this declaration, possibly |
| 859 | // merging in the template parameter list from the previous class |
| 860 | // template declaration. |
| 861 | if (CheckTemplateParameterList(TemplateParams, |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 862 | PrevClassTemplate? PrevClassTemplate->getTemplateParameters() : 0, |
| 863 | TPC_ClassTemplate)) |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 864 | Invalid = true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 865 | |
Douglas Gregor | 57265e3 | 2010-04-12 16:00:01 +0000 | [diff] [blame] | 866 | if (SS.isSet()) { |
| 867 | // If the name of the template was qualified, we must be defining the |
| 868 | // template out-of-line. |
| 869 | if (!SS.isInvalid() && !Invalid && !PrevClassTemplate && |
| 870 | !(TUK == TUK_Friend && CurContext->isDependentContext())) |
| 871 | Diag(NameLoc, diag::err_member_def_does_not_match) |
| 872 | << Name << SemanticContext << SS.getRange(); |
| 873 | } |
| 874 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 875 | CXXRecordDecl *NewClass = |
Douglas Gregor | 741dd9a | 2009-07-21 14:46:17 +0000 | [diff] [blame] | 876 | CXXRecordDecl::Create(Context, Kind, SemanticContext, NameLoc, Name, KWLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 877 | PrevClassTemplate? |
Douglas Gregor | aafc0cc | 2009-05-15 19:11:46 +0000 | [diff] [blame] | 878 | PrevClassTemplate->getTemplatedDecl() : 0, |
| 879 | /*DelayTypeCreation=*/true); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 880 | SetNestedNameSpecifier(NewClass, SS); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 881 | |
| 882 | ClassTemplateDecl *NewTemplate |
| 883 | = ClassTemplateDecl::Create(Context, SemanticContext, NameLoc, |
| 884 | DeclarationName(Name), TemplateParams, |
Douglas Gregor | 5953d8b | 2009-03-19 17:26:29 +0000 | [diff] [blame] | 885 | NewClass, PrevClassTemplate); |
Douglas Gregor | befc20e | 2009-03-26 00:10:35 +0000 | [diff] [blame] | 886 | NewClass->setDescribedClassTemplate(NewTemplate); |
| 887 | |
Douglas Gregor | aafc0cc | 2009-05-15 19:11:46 +0000 | [diff] [blame] | 888 | // Build the type for the class template declaration now. |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 889 | QualType T = NewTemplate->getInjectedClassNameSpecialization(Context); |
| 890 | T = Context.getInjectedClassNameType(NewClass, T); |
Douglas Gregor | aafc0cc | 2009-05-15 19:11:46 +0000 | [diff] [blame] | 891 | assert(T->isDependentType() && "Class template type is not dependent?"); |
| 892 | (void)T; |
| 893 | |
Douglas Gregor | fd056bc | 2009-10-13 16:30:37 +0000 | [diff] [blame] | 894 | // If we are providing an explicit specialization of a member that is a |
| 895 | // class template, make a note of that. |
| 896 | if (PrevClassTemplate && |
| 897 | PrevClassTemplate->getInstantiatedFromMemberTemplate()) |
| 898 | PrevClassTemplate->setMemberSpecialization(); |
| 899 | |
Anders Carlsson | 4cbe82c | 2009-03-26 01:24:28 +0000 | [diff] [blame] | 900 | // Set the access specifier. |
Douglas Gregor | d85bea2 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 901 | if (!Invalid && TUK != TUK_Friend) |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 902 | SetMemberAccessSpecifier(NewTemplate, PrevClassTemplate, AS); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 903 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 904 | // Set the lexical context of these templates |
| 905 | NewClass->setLexicalDeclContext(CurContext); |
| 906 | NewTemplate->setLexicalDeclContext(CurContext); |
| 907 | |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 908 | if (TUK == TUK_Definition) |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 909 | NewClass->startDefinition(); |
| 910 | |
| 911 | if (Attr) |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 912 | ProcessDeclAttributeList(S, NewClass, Attr); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 913 | |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 914 | if (TUK != TUK_Friend) |
| 915 | PushOnScopeChains(NewTemplate, S); |
| 916 | else { |
Douglas Gregor | d85bea2 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 917 | if (PrevClassTemplate && PrevClassTemplate->getAccess() != AS_none) { |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 918 | NewTemplate->setAccess(PrevClassTemplate->getAccess()); |
Douglas Gregor | d85bea2 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 919 | NewClass->setAccess(PrevClassTemplate->getAccess()); |
| 920 | } |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 921 | |
Douglas Gregor | d85bea2 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 922 | NewTemplate->setObjectOfFriendDecl(/* PreviouslyDeclared = */ |
| 923 | PrevClassTemplate != NULL); |
| 924 | |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 925 | // Friend templates are visible in fairly strange ways. |
| 926 | if (!CurContext->isDependentContext()) { |
| 927 | DeclContext *DC = SemanticContext->getLookupContext(); |
| 928 | DC->makeDeclVisibleInContext(NewTemplate, /* Recoverable = */ false); |
| 929 | if (Scope *EnclosingScope = getScopeForDeclContext(S, DC)) |
| 930 | PushOnScopeChains(NewTemplate, EnclosingScope, |
| 931 | /* AddToContext = */ false); |
| 932 | } |
Douglas Gregor | d85bea2 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 933 | |
| 934 | FriendDecl *Friend = FriendDecl::Create(Context, CurContext, |
| 935 | NewClass->getLocation(), |
| 936 | NewTemplate, |
| 937 | /*FIXME:*/NewClass->getLocation()); |
| 938 | Friend->setAccess(AS_public); |
| 939 | CurContext->addDecl(Friend); |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 940 | } |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 941 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 942 | if (Invalid) { |
| 943 | NewTemplate->setInvalidDecl(); |
| 944 | NewClass->setInvalidDecl(); |
| 945 | } |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 946 | return DeclPtrTy::make(NewTemplate); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 947 | } |
| 948 | |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 949 | /// \brief Diagnose the presence of a default template argument on a |
| 950 | /// template parameter, which is ill-formed in certain contexts. |
| 951 | /// |
| 952 | /// \returns true if the default template argument should be dropped. |
| 953 | static bool DiagnoseDefaultTemplateArgument(Sema &S, |
| 954 | Sema::TemplateParamListContext TPC, |
| 955 | SourceLocation ParamLoc, |
| 956 | SourceRange DefArgRange) { |
| 957 | switch (TPC) { |
| 958 | case Sema::TPC_ClassTemplate: |
| 959 | return false; |
| 960 | |
| 961 | case Sema::TPC_FunctionTemplate: |
| 962 | // C++ [temp.param]p9: |
| 963 | // A default template-argument shall not be specified in a |
| 964 | // function template declaration or a function template |
| 965 | // definition [...] |
| 966 | // (This sentence is not in C++0x, per DR226). |
| 967 | if (!S.getLangOptions().CPlusPlus0x) |
| 968 | S.Diag(ParamLoc, |
| 969 | diag::err_template_parameter_default_in_function_template) |
| 970 | << DefArgRange; |
| 971 | return false; |
| 972 | |
| 973 | case Sema::TPC_ClassTemplateMember: |
| 974 | // C++0x [temp.param]p9: |
| 975 | // A default template-argument shall not be specified in the |
| 976 | // template-parameter-lists of the definition of a member of a |
| 977 | // class template that appears outside of the member's class. |
| 978 | S.Diag(ParamLoc, diag::err_template_parameter_default_template_member) |
| 979 | << DefArgRange; |
| 980 | return true; |
| 981 | |
| 982 | case Sema::TPC_FriendFunctionTemplate: |
| 983 | // C++ [temp.param]p9: |
| 984 | // A default template-argument shall not be specified in a |
| 985 | // friend template declaration. |
| 986 | S.Diag(ParamLoc, diag::err_template_parameter_default_friend_template) |
| 987 | << DefArgRange; |
| 988 | return true; |
| 989 | |
| 990 | // FIXME: C++0x [temp.param]p9 allows default template-arguments |
| 991 | // for friend function templates if there is only a single |
| 992 | // declaration (and it is a definition). Strange! |
| 993 | } |
| 994 | |
| 995 | return false; |
| 996 | } |
| 997 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 998 | /// \brief Checks the validity of a template parameter list, possibly |
| 999 | /// considering the template parameter list from a previous |
| 1000 | /// declaration. |
| 1001 | /// |
| 1002 | /// If an "old" template parameter list is provided, it must be |
| 1003 | /// equivalent (per TemplateParameterListsAreEqual) to the "new" |
| 1004 | /// template parameter list. |
| 1005 | /// |
| 1006 | /// \param NewParams Template parameter list for a new template |
| 1007 | /// declaration. This template parameter list will be updated with any |
| 1008 | /// default arguments that are carried through from the previous |
| 1009 | /// template parameter list. |
| 1010 | /// |
| 1011 | /// \param OldParams If provided, template parameter list from a |
| 1012 | /// previous declaration of the same template. Default template |
| 1013 | /// arguments will be merged from the old template parameter list to |
| 1014 | /// the new template parameter list. |
| 1015 | /// |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1016 | /// \param TPC Describes the context in which we are checking the given |
| 1017 | /// template parameter list. |
| 1018 | /// |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1019 | /// \returns true if an error occurred, false otherwise. |
| 1020 | bool Sema::CheckTemplateParameterList(TemplateParameterList *NewParams, |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1021 | TemplateParameterList *OldParams, |
| 1022 | TemplateParamListContext TPC) { |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1023 | bool Invalid = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1024 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1025 | // C++ [temp.param]p10: |
| 1026 | // The set of default template-arguments available for use with a |
| 1027 | // template declaration or definition is obtained by merging the |
| 1028 | // default arguments from the definition (if in scope) and all |
| 1029 | // declarations in scope in the same way default function |
| 1030 | // arguments are (8.3.6). |
| 1031 | bool SawDefaultArgument = false; |
| 1032 | SourceLocation PreviousDefaultArgLoc; |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 1033 | |
Anders Carlsson | 49d2557 | 2009-06-12 23:20:15 +0000 | [diff] [blame] | 1034 | bool SawParameterPack = false; |
| 1035 | SourceLocation ParameterPackLoc; |
| 1036 | |
Mike Stump | 1a35fde | 2009-02-11 23:03:27 +0000 | [diff] [blame] | 1037 | // Dummy initialization to avoid warnings. |
Douglas Gregor | 1bc6913 | 2009-02-11 20:46:19 +0000 | [diff] [blame] | 1038 | TemplateParameterList::iterator OldParam = NewParams->end(); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1039 | if (OldParams) |
| 1040 | OldParam = OldParams->begin(); |
| 1041 | |
| 1042 | for (TemplateParameterList::iterator NewParam = NewParams->begin(), |
| 1043 | NewParamEnd = NewParams->end(); |
| 1044 | NewParam != NewParamEnd; ++NewParam) { |
| 1045 | // Variables used to diagnose redundant default arguments |
| 1046 | bool RedundantDefaultArg = false; |
| 1047 | SourceLocation OldDefaultLoc; |
| 1048 | SourceLocation NewDefaultLoc; |
| 1049 | |
| 1050 | // Variables used to diagnose missing default arguments |
| 1051 | bool MissingDefaultArg = false; |
| 1052 | |
Anders Carlsson | 49d2557 | 2009-06-12 23:20:15 +0000 | [diff] [blame] | 1053 | // C++0x [temp.param]p11: |
| 1054 | // If a template parameter of a class template is a template parameter pack, |
| 1055 | // it must be the last template parameter. |
| 1056 | if (SawParameterPack) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1057 | Diag(ParameterPackLoc, |
Anders Carlsson | 49d2557 | 2009-06-12 23:20:15 +0000 | [diff] [blame] | 1058 | diag::err_template_param_pack_must_be_last_template_parameter); |
| 1059 | Invalid = true; |
| 1060 | } |
| 1061 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1062 | if (TemplateTypeParmDecl *NewTypeParm |
| 1063 | = dyn_cast<TemplateTypeParmDecl>(*NewParam)) { |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1064 | // Check the presence of a default argument here. |
| 1065 | if (NewTypeParm->hasDefaultArgument() && |
| 1066 | DiagnoseDefaultTemplateArgument(*this, TPC, |
| 1067 | NewTypeParm->getLocation(), |
| 1068 | NewTypeParm->getDefaultArgumentInfo()->getTypeLoc() |
| 1069 | .getFullSourceRange())) |
| 1070 | NewTypeParm->removeDefaultArgument(); |
| 1071 | |
| 1072 | // Merge default arguments for template type parameters. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1073 | TemplateTypeParmDecl *OldTypeParm |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1074 | = OldParams? cast<TemplateTypeParmDecl>(*OldParam) : 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1075 | |
Anders Carlsson | 49d2557 | 2009-06-12 23:20:15 +0000 | [diff] [blame] | 1076 | if (NewTypeParm->isParameterPack()) { |
| 1077 | assert(!NewTypeParm->hasDefaultArgument() && |
| 1078 | "Parameter packs can't have a default argument!"); |
| 1079 | SawParameterPack = true; |
| 1080 | ParameterPackLoc = NewTypeParm->getLocation(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1081 | } else if (OldTypeParm && OldTypeParm->hasDefaultArgument() && |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1082 | NewTypeParm->hasDefaultArgument()) { |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1083 | OldDefaultLoc = OldTypeParm->getDefaultArgumentLoc(); |
| 1084 | NewDefaultLoc = NewTypeParm->getDefaultArgumentLoc(); |
| 1085 | SawDefaultArgument = true; |
| 1086 | RedundantDefaultArg = true; |
| 1087 | PreviousDefaultArgLoc = NewDefaultLoc; |
| 1088 | } else if (OldTypeParm && OldTypeParm->hasDefaultArgument()) { |
| 1089 | // Merge the default argument from the old declaration to the |
| 1090 | // new declaration. |
| 1091 | SawDefaultArgument = true; |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1092 | NewTypeParm->setDefaultArgument(OldTypeParm->getDefaultArgumentInfo(), |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1093 | true); |
| 1094 | PreviousDefaultArgLoc = OldTypeParm->getDefaultArgumentLoc(); |
| 1095 | } else if (NewTypeParm->hasDefaultArgument()) { |
| 1096 | SawDefaultArgument = true; |
| 1097 | PreviousDefaultArgLoc = NewTypeParm->getDefaultArgumentLoc(); |
| 1098 | } else if (SawDefaultArgument) |
| 1099 | MissingDefaultArg = true; |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1100 | } else if (NonTypeTemplateParmDecl *NewNonTypeParm |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1101 | = dyn_cast<NonTypeTemplateParmDecl>(*NewParam)) { |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1102 | // Check the presence of a default argument here. |
| 1103 | if (NewNonTypeParm->hasDefaultArgument() && |
| 1104 | DiagnoseDefaultTemplateArgument(*this, TPC, |
| 1105 | NewNonTypeParm->getLocation(), |
| 1106 | NewNonTypeParm->getDefaultArgument()->getSourceRange())) { |
| 1107 | NewNonTypeParm->getDefaultArgument()->Destroy(Context); |
| 1108 | NewNonTypeParm->setDefaultArgument(0); |
| 1109 | } |
| 1110 | |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1111 | // Merge default arguments for non-type template parameters |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1112 | NonTypeTemplateParmDecl *OldNonTypeParm |
| 1113 | = OldParams? cast<NonTypeTemplateParmDecl>(*OldParam) : 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1114 | if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument() && |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1115 | NewNonTypeParm->hasDefaultArgument()) { |
| 1116 | OldDefaultLoc = OldNonTypeParm->getDefaultArgumentLoc(); |
| 1117 | NewDefaultLoc = NewNonTypeParm->getDefaultArgumentLoc(); |
| 1118 | SawDefaultArgument = true; |
| 1119 | RedundantDefaultArg = true; |
| 1120 | PreviousDefaultArgLoc = NewDefaultLoc; |
| 1121 | } else if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument()) { |
| 1122 | // Merge the default argument from the old declaration to the |
| 1123 | // new declaration. |
| 1124 | SawDefaultArgument = true; |
| 1125 | // FIXME: We need to create a new kind of "default argument" |
| 1126 | // expression that points to a previous template template |
| 1127 | // parameter. |
| 1128 | NewNonTypeParm->setDefaultArgument( |
| 1129 | OldNonTypeParm->getDefaultArgument()); |
| 1130 | PreviousDefaultArgLoc = OldNonTypeParm->getDefaultArgumentLoc(); |
| 1131 | } else if (NewNonTypeParm->hasDefaultArgument()) { |
| 1132 | SawDefaultArgument = true; |
| 1133 | PreviousDefaultArgLoc = NewNonTypeParm->getDefaultArgumentLoc(); |
| 1134 | } else if (SawDefaultArgument) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1135 | MissingDefaultArg = true; |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1136 | } else { |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1137 | // Check the presence of a default argument here. |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1138 | TemplateTemplateParmDecl *NewTemplateParm |
| 1139 | = cast<TemplateTemplateParmDecl>(*NewParam); |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1140 | if (NewTemplateParm->hasDefaultArgument() && |
| 1141 | DiagnoseDefaultTemplateArgument(*this, TPC, |
| 1142 | NewTemplateParm->getLocation(), |
| 1143 | NewTemplateParm->getDefaultArgument().getSourceRange())) |
| 1144 | NewTemplateParm->setDefaultArgument(TemplateArgumentLoc()); |
| 1145 | |
| 1146 | // Merge default arguments for template template parameters |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1147 | TemplateTemplateParmDecl *OldTemplateParm |
| 1148 | = OldParams? cast<TemplateTemplateParmDecl>(*OldParam) : 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1149 | if (OldTemplateParm && OldTemplateParm->hasDefaultArgument() && |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1150 | NewTemplateParm->hasDefaultArgument()) { |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1151 | OldDefaultLoc = OldTemplateParm->getDefaultArgument().getLocation(); |
| 1152 | NewDefaultLoc = NewTemplateParm->getDefaultArgument().getLocation(); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1153 | SawDefaultArgument = true; |
| 1154 | RedundantDefaultArg = true; |
| 1155 | PreviousDefaultArgLoc = NewDefaultLoc; |
| 1156 | } else if (OldTemplateParm && OldTemplateParm->hasDefaultArgument()) { |
| 1157 | // Merge the default argument from the old declaration to the |
| 1158 | // new declaration. |
| 1159 | SawDefaultArgument = true; |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 1160 | // FIXME: We need to create a new kind of "default argument" expression |
| 1161 | // that points to a previous template template parameter. |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1162 | NewTemplateParm->setDefaultArgument( |
| 1163 | OldTemplateParm->getDefaultArgument()); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1164 | PreviousDefaultArgLoc |
| 1165 | = OldTemplateParm->getDefaultArgument().getLocation(); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1166 | } else if (NewTemplateParm->hasDefaultArgument()) { |
| 1167 | SawDefaultArgument = true; |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1168 | PreviousDefaultArgLoc |
| 1169 | = NewTemplateParm->getDefaultArgument().getLocation(); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1170 | } else if (SawDefaultArgument) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1171 | MissingDefaultArg = true; |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1172 | } |
| 1173 | |
| 1174 | if (RedundantDefaultArg) { |
| 1175 | // C++ [temp.param]p12: |
| 1176 | // A template-parameter shall not be given default arguments |
| 1177 | // by two different declarations in the same scope. |
| 1178 | Diag(NewDefaultLoc, diag::err_template_param_default_arg_redefinition); |
| 1179 | Diag(OldDefaultLoc, diag::note_template_param_prev_default_arg); |
| 1180 | Invalid = true; |
| 1181 | } else if (MissingDefaultArg) { |
| 1182 | // C++ [temp.param]p11: |
| 1183 | // If a template-parameter has a default template-argument, |
| 1184 | // all subsequent template-parameters shall have a default |
| 1185 | // template-argument supplied. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1186 | Diag((*NewParam)->getLocation(), |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1187 | diag::err_template_param_default_arg_missing); |
| 1188 | Diag(PreviousDefaultArgLoc, diag::note_template_param_prev_default_arg); |
| 1189 | Invalid = true; |
| 1190 | } |
| 1191 | |
| 1192 | // If we have an old template parameter list that we're merging |
| 1193 | // in, move on to the next parameter. |
| 1194 | if (OldParams) |
| 1195 | ++OldParam; |
| 1196 | } |
| 1197 | |
| 1198 | return Invalid; |
| 1199 | } |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 1200 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1201 | /// \brief Match the given template parameter lists to the given scope |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1202 | /// specifier, returning the template parameter list that applies to the |
| 1203 | /// name. |
| 1204 | /// |
| 1205 | /// \param DeclStartLoc the start of the declaration that has a scope |
| 1206 | /// specifier or a template parameter list. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1207 | /// |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1208 | /// \param SS the scope specifier that will be matched to the given template |
| 1209 | /// parameter lists. This scope specifier precedes a qualified name that is |
| 1210 | /// being declared. |
| 1211 | /// |
| 1212 | /// \param ParamLists the template parameter lists, from the outermost to the |
| 1213 | /// innermost template parameter lists. |
| 1214 | /// |
| 1215 | /// \param NumParamLists the number of template parameter lists in ParamLists. |
| 1216 | /// |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 1217 | /// \param IsExplicitSpecialization will be set true if the entity being |
| 1218 | /// declared is an explicit specialization, false otherwise. |
| 1219 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1220 | /// \returns the template parameter list, if any, that corresponds to the |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1221 | /// name that is preceded by the scope specifier @p SS. This template |
| 1222 | /// parameter list may be have template parameters (if we're declaring a |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1223 | /// template) or may have no template parameters (if we're declaring a |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1224 | /// template specialization), or may be NULL (if we were's declaring isn't |
| 1225 | /// itself a template). |
| 1226 | TemplateParameterList * |
| 1227 | Sema::MatchTemplateParametersToScopeSpecifier(SourceLocation DeclStartLoc, |
| 1228 | const CXXScopeSpec &SS, |
| 1229 | TemplateParameterList **ParamLists, |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 1230 | unsigned NumParamLists, |
| 1231 | bool &IsExplicitSpecialization) { |
| 1232 | IsExplicitSpecialization = false; |
| 1233 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1234 | // Find the template-ids that occur within the nested-name-specifier. These |
| 1235 | // template-ids will match up with the template parameter lists. |
| 1236 | llvm::SmallVector<const TemplateSpecializationType *, 4> |
| 1237 | TemplateIdsInSpecifier; |
Douglas Gregor | 3ebd753 | 2009-11-23 12:11:45 +0000 | [diff] [blame] | 1238 | llvm::SmallVector<ClassTemplateSpecializationDecl *, 4> |
| 1239 | ExplicitSpecializationsInSpecifier; |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1240 | for (NestedNameSpecifier *NNS = (NestedNameSpecifier *)SS.getScopeRep(); |
| 1241 | NNS; NNS = NNS->getPrefix()) { |
John McCall | 4b2b02b | 2009-12-15 02:19:47 +0000 | [diff] [blame] | 1242 | const Type *T = NNS->getAsType(); |
| 1243 | if (!T) break; |
| 1244 | |
| 1245 | // C++0x [temp.expl.spec]p17: |
| 1246 | // A member or a member template may be nested within many |
| 1247 | // enclosing class templates. In an explicit specialization for |
| 1248 | // such a member, the member declaration shall be preceded by a |
| 1249 | // template<> for each enclosing class template that is |
| 1250 | // explicitly specialized. |
Douglas Gregor | fe33106 | 2010-02-13 05:23:25 +0000 | [diff] [blame] | 1251 | // |
| 1252 | // Following the existing practice of GNU and EDG, we allow a typedef of a |
| 1253 | // template specialization type. |
| 1254 | if (const TypedefType *TT = dyn_cast<TypedefType>(T)) |
| 1255 | T = TT->LookThroughTypedefs().getTypePtr(); |
John McCall | 4b2b02b | 2009-12-15 02:19:47 +0000 | [diff] [blame] | 1256 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1257 | if (const TemplateSpecializationType *SpecType |
Douglas Gregor | fe33106 | 2010-02-13 05:23:25 +0000 | [diff] [blame] | 1258 | = dyn_cast<TemplateSpecializationType>(T)) { |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1259 | TemplateDecl *Template = SpecType->getTemplateName().getAsTemplateDecl(); |
| 1260 | if (!Template) |
| 1261 | continue; // FIXME: should this be an error? probably... |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1262 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1263 | if (const RecordType *Record = SpecType->getAs<RecordType>()) { |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1264 | ClassTemplateSpecializationDecl *SpecDecl |
| 1265 | = cast<ClassTemplateSpecializationDecl>(Record->getDecl()); |
| 1266 | // If the nested name specifier refers to an explicit specialization, |
| 1267 | // we don't need a template<> header. |
Douglas Gregor | 3ebd753 | 2009-11-23 12:11:45 +0000 | [diff] [blame] | 1268 | if (SpecDecl->getSpecializationKind() == TSK_ExplicitSpecialization) { |
| 1269 | ExplicitSpecializationsInSpecifier.push_back(SpecDecl); |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1270 | continue; |
Douglas Gregor | 3ebd753 | 2009-11-23 12:11:45 +0000 | [diff] [blame] | 1271 | } |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1272 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1273 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1274 | TemplateIdsInSpecifier.push_back(SpecType); |
| 1275 | } |
| 1276 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1277 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1278 | // Reverse the list of template-ids in the scope specifier, so that we can |
| 1279 | // more easily match up the template-ids and the template parameter lists. |
| 1280 | std::reverse(TemplateIdsInSpecifier.begin(), TemplateIdsInSpecifier.end()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1281 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1282 | SourceLocation FirstTemplateLoc = DeclStartLoc; |
| 1283 | if (NumParamLists) |
| 1284 | FirstTemplateLoc = ParamLists[0]->getTemplateLoc(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1285 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1286 | // Match the template-ids found in the specifier to the template parameter |
| 1287 | // lists. |
| 1288 | unsigned Idx = 0; |
| 1289 | for (unsigned NumTemplateIds = TemplateIdsInSpecifier.size(); |
| 1290 | Idx != NumTemplateIds; ++Idx) { |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 1291 | QualType TemplateId = QualType(TemplateIdsInSpecifier[Idx], 0); |
| 1292 | bool DependentTemplateId = TemplateId->isDependentType(); |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1293 | if (Idx >= NumParamLists) { |
| 1294 | // We have a template-id without a corresponding template parameter |
| 1295 | // list. |
| 1296 | if (DependentTemplateId) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1297 | // FIXME: the location information here isn't great. |
| 1298 | Diag(SS.getRange().getBegin(), |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1299 | diag::err_template_spec_needs_template_parameters) |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 1300 | << TemplateId |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1301 | << SS.getRange(); |
| 1302 | } else { |
| 1303 | Diag(SS.getRange().getBegin(), diag::err_template_spec_needs_header) |
| 1304 | << SS.getRange() |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 1305 | << FixItHint::CreateInsertion(FirstTemplateLoc, "template<> "); |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 1306 | IsExplicitSpecialization = true; |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1307 | } |
| 1308 | return 0; |
| 1309 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1310 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1311 | // Check the template parameter list against its corresponding template-id. |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 1312 | if (DependentTemplateId) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1313 | TemplateDecl *Template |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 1314 | = TemplateIdsInSpecifier[Idx]->getTemplateName().getAsTemplateDecl(); |
| 1315 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1316 | if (ClassTemplateDecl *ClassTemplate |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 1317 | = dyn_cast<ClassTemplateDecl>(Template)) { |
| 1318 | TemplateParameterList *ExpectedTemplateParams = 0; |
| 1319 | // Is this template-id naming the primary template? |
| 1320 | if (Context.hasSameType(TemplateId, |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 1321 | ClassTemplate->getInjectedClassNameSpecialization(Context))) |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 1322 | ExpectedTemplateParams = ClassTemplate->getTemplateParameters(); |
| 1323 | // ... or a partial specialization? |
| 1324 | else if (ClassTemplatePartialSpecializationDecl *PartialSpec |
| 1325 | = ClassTemplate->findPartialSpecialization(TemplateId)) |
| 1326 | ExpectedTemplateParams = PartialSpec->getTemplateParameters(); |
| 1327 | |
| 1328 | if (ExpectedTemplateParams) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1329 | TemplateParameterListsAreEqual(ParamLists[Idx], |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 1330 | ExpectedTemplateParams, |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 1331 | true, TPL_TemplateMatch); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1332 | } |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1333 | |
| 1334 | CheckTemplateParameterList(ParamLists[Idx], 0, TPC_ClassTemplateMember); |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 1335 | } else if (ParamLists[Idx]->size() > 0) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1336 | Diag(ParamLists[Idx]->getTemplateLoc(), |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 1337 | diag::err_template_param_list_matches_nontemplate) |
| 1338 | << TemplateId |
| 1339 | << ParamLists[Idx]->getSourceRange(); |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 1340 | else |
| 1341 | IsExplicitSpecialization = true; |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1342 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1343 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1344 | // If there were at least as many template-ids as there were template |
| 1345 | // parameter lists, then there are no template parameter lists remaining for |
| 1346 | // the declaration itself. |
| 1347 | if (Idx >= NumParamLists) |
| 1348 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1349 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1350 | // If there were too many template parameter lists, complain about that now. |
| 1351 | if (Idx != NumParamLists - 1) { |
| 1352 | while (Idx < NumParamLists - 1) { |
Douglas Gregor | 3ebd753 | 2009-11-23 12:11:45 +0000 | [diff] [blame] | 1353 | bool isExplicitSpecHeader = ParamLists[Idx]->size() == 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1354 | Diag(ParamLists[Idx]->getTemplateLoc(), |
Douglas Gregor | 3ebd753 | 2009-11-23 12:11:45 +0000 | [diff] [blame] | 1355 | isExplicitSpecHeader? diag::warn_template_spec_extra_headers |
| 1356 | : diag::err_template_spec_extra_headers) |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1357 | << SourceRange(ParamLists[Idx]->getTemplateLoc(), |
| 1358 | ParamLists[Idx]->getRAngleLoc()); |
Douglas Gregor | 3ebd753 | 2009-11-23 12:11:45 +0000 | [diff] [blame] | 1359 | |
| 1360 | if (isExplicitSpecHeader && !ExplicitSpecializationsInSpecifier.empty()) { |
| 1361 | Diag(ExplicitSpecializationsInSpecifier.back()->getLocation(), |
| 1362 | diag::note_explicit_template_spec_does_not_need_header) |
| 1363 | << ExplicitSpecializationsInSpecifier.back(); |
| 1364 | ExplicitSpecializationsInSpecifier.pop_back(); |
| 1365 | } |
| 1366 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1367 | ++Idx; |
| 1368 | } |
| 1369 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1370 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1371 | // Return the last template parameter list, which corresponds to the |
| 1372 | // entity being declared. |
| 1373 | return ParamLists[NumParamLists - 1]; |
| 1374 | } |
| 1375 | |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1376 | QualType Sema::CheckTemplateIdType(TemplateName Name, |
| 1377 | SourceLocation TemplateLoc, |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1378 | const TemplateArgumentListInfo &TemplateArgs) { |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1379 | TemplateDecl *Template = Name.getAsTemplateDecl(); |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 1380 | if (!Template) { |
| 1381 | // The template name does not resolve to a template, so we just |
| 1382 | // build a dependent template-id type. |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1383 | return Context.getTemplateSpecializationType(Name, TemplateArgs); |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 1384 | } |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1385 | |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1386 | // Check that the template argument list is well-formed for this |
| 1387 | // template. |
Anders Carlsson | fb25052 | 2009-06-23 01:26:57 +0000 | [diff] [blame] | 1388 | TemplateArgumentListBuilder Converted(Template->getTemplateParameters(), |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1389 | TemplateArgs.size()); |
| 1390 | if (CheckTemplateArgumentList(Template, TemplateLoc, TemplateArgs, |
Douglas Gregor | 16134c6 | 2009-07-01 00:28:38 +0000 | [diff] [blame] | 1391 | false, Converted)) |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1392 | return QualType(); |
| 1393 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1394 | assert((Converted.structuredSize() == |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1395 | Template->getTemplateParameters()->size()) && |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1396 | "Converted template argument list is too short!"); |
| 1397 | |
| 1398 | QualType CanonType; |
| 1399 | |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 1400 | if (Name.isDependent() || |
| 1401 | TemplateSpecializationType::anyDependentTemplateArguments( |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1402 | TemplateArgs)) { |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1403 | // This class template specialization is a dependent |
| 1404 | // type. Therefore, its canonical type is another class template |
| 1405 | // specialization type that contains all of the converted |
| 1406 | // arguments in canonical form. This ensures that, e.g., A<T> and |
| 1407 | // A<T, T> have identical types when A is declared as: |
| 1408 | // |
| 1409 | // template<typename T, typename U = T> struct A; |
Douglas Gregor | 25a3ef7 | 2009-05-07 06:41:52 +0000 | [diff] [blame] | 1410 | TemplateName CanonName = Context.getCanonicalTemplateName(Name); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1411 | CanonType = Context.getTemplateSpecializationType(CanonName, |
Anders Carlsson | fb25052 | 2009-06-23 01:26:57 +0000 | [diff] [blame] | 1412 | Converted.getFlatArguments(), |
| 1413 | Converted.flatSize()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1414 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 1415 | // FIXME: CanonType is not actually the canonical type, and unfortunately |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1416 | // it is a TemplateSpecializationType that we will never use again. |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 1417 | // In the future, we need to teach getTemplateSpecializationType to only |
| 1418 | // build the canonical type and return that to us. |
| 1419 | CanonType = Context.getCanonicalType(CanonType); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1420 | } else if (ClassTemplateDecl *ClassTemplate |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1421 | = dyn_cast<ClassTemplateDecl>(Template)) { |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1422 | // Find the class template specialization declaration that |
| 1423 | // corresponds to these arguments. |
| 1424 | llvm::FoldingSetNodeID ID; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1425 | ClassTemplateSpecializationDecl::Profile(ID, |
Anders Carlsson | fb25052 | 2009-06-23 01:26:57 +0000 | [diff] [blame] | 1426 | Converted.getFlatArguments(), |
Douglas Gregor | 828e226 | 2009-07-29 16:09:57 +0000 | [diff] [blame] | 1427 | Converted.flatSize(), |
| 1428 | Context); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1429 | void *InsertPos = 0; |
| 1430 | ClassTemplateSpecializationDecl *Decl |
| 1431 | = ClassTemplate->getSpecializations().FindNodeOrInsertPos(ID, InsertPos); |
| 1432 | if (!Decl) { |
| 1433 | // This is the first time we have referenced this class template |
| 1434 | // specialization. Create the canonical declaration and add it to |
| 1435 | // the set of specializations. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1436 | Decl = ClassTemplateSpecializationDecl::Create(Context, |
Anders Carlsson | 1c5976e | 2009-06-05 03:43:12 +0000 | [diff] [blame] | 1437 | ClassTemplate->getDeclContext(), |
John McCall | 9cc7807 | 2009-09-11 07:25:08 +0000 | [diff] [blame] | 1438 | ClassTemplate->getLocation(), |
Anders Carlsson | 1c5976e | 2009-06-05 03:43:12 +0000 | [diff] [blame] | 1439 | ClassTemplate, |
Anders Carlsson | fb25052 | 2009-06-23 01:26:57 +0000 | [diff] [blame] | 1440 | Converted, 0); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1441 | ClassTemplate->getSpecializations().InsertNode(Decl, InsertPos); |
| 1442 | Decl->setLexicalDeclContext(CurContext); |
| 1443 | } |
| 1444 | |
| 1445 | CanonType = Context.getTypeDeclType(Decl); |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 1446 | assert(isa<RecordType>(CanonType) && |
| 1447 | "type of non-dependent specialization is not a RecordType"); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1448 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1449 | |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1450 | // Build the fully-sugared type for this class template |
| 1451 | // specialization, which refers back to the class template |
| 1452 | // specialization we created or found. |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1453 | return Context.getTemplateSpecializationType(Name, TemplateArgs, CanonType); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1454 | } |
| 1455 | |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 1456 | Action::TypeResult |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1457 | Sema::ActOnTemplateIdType(TemplateTy TemplateD, SourceLocation TemplateLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1458 | SourceLocation LAngleLoc, |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1459 | ASTTemplateArgsPtr TemplateArgsIn, |
John McCall | 6b2becf | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 1460 | SourceLocation RAngleLoc) { |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1461 | TemplateName Template = TemplateD.getAsVal<TemplateName>(); |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 1462 | |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1463 | // Translate the parser's template argument list in our AST format. |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1464 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
Douglas Gregor | 314b97f | 2009-11-10 19:49:08 +0000 | [diff] [blame] | 1465 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 1466 | |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1467 | QualType Result = CheckTemplateIdType(Template, TemplateLoc, TemplateArgs); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1468 | TemplateArgsIn.release(); |
Douglas Gregor | 31a19b6 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 1469 | |
| 1470 | if (Result.isNull()) |
| 1471 | return true; |
| 1472 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1473 | TypeSourceInfo *DI = Context.CreateTypeSourceInfo(Result); |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1474 | TemplateSpecializationTypeLoc TL |
| 1475 | = cast<TemplateSpecializationTypeLoc>(DI->getTypeLoc()); |
| 1476 | TL.setTemplateNameLoc(TemplateLoc); |
| 1477 | TL.setLAngleLoc(LAngleLoc); |
| 1478 | TL.setRAngleLoc(RAngleLoc); |
| 1479 | for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) |
| 1480 | TL.setArgLocInfo(i, TemplateArgs[i].getLocInfo()); |
| 1481 | |
| 1482 | return CreateLocInfoType(Result, DI).getAsOpaquePtr(); |
John McCall | 6b2becf | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 1483 | } |
John McCall | f1bbbb4 | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 1484 | |
John McCall | 6b2becf | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 1485 | Sema::TypeResult Sema::ActOnTagTemplateIdType(TypeResult TypeResult, |
| 1486 | TagUseKind TUK, |
| 1487 | DeclSpec::TST TagSpec, |
| 1488 | SourceLocation TagLoc) { |
| 1489 | if (TypeResult.isInvalid()) |
| 1490 | return Sema::TypeResult(); |
John McCall | f1bbbb4 | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 1491 | |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1492 | // FIXME: preserve source info, ideally without copying the DI. |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1493 | TypeSourceInfo *DI; |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1494 | QualType Type = GetTypeFromParser(TypeResult.get(), &DI); |
John McCall | f1bbbb4 | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 1495 | |
John McCall | 6b2becf | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 1496 | // Verify the tag specifier. |
| 1497 | TagDecl::TagKind TagKind = TagDecl::getTagKindForTypeSpec(TagSpec); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1498 | |
John McCall | 6b2becf | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 1499 | if (const RecordType *RT = Type->getAs<RecordType>()) { |
| 1500 | RecordDecl *D = RT->getDecl(); |
| 1501 | |
| 1502 | IdentifierInfo *Id = D->getIdentifier(); |
| 1503 | assert(Id && "templated class must have an identifier"); |
| 1504 | |
| 1505 | if (!isAcceptableTagRedeclaration(D, TagKind, TagLoc, *Id)) { |
| 1506 | Diag(TagLoc, diag::err_use_with_wrong_tag) |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 1507 | << Type |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 1508 | << FixItHint::CreateReplacement(SourceRange(TagLoc), D->getKindName()); |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 1509 | Diag(D->getLocation(), diag::note_previous_use); |
John McCall | f1bbbb4 | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 1510 | } |
| 1511 | } |
| 1512 | |
John McCall | 6b2becf | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 1513 | QualType ElabType = Context.getElaboratedType(Type, TagKind); |
| 1514 | |
| 1515 | return ElabType.getAsOpaquePtr(); |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 1516 | } |
| 1517 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1518 | Sema::OwningExprResult Sema::BuildTemplateIdExpr(const CXXScopeSpec &SS, |
| 1519 | LookupResult &R, |
| 1520 | bool RequiresADL, |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1521 | const TemplateArgumentListInfo &TemplateArgs) { |
Douglas Gregor | edce4dd | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 1522 | // FIXME: Can we do any checking at this point? I guess we could check the |
| 1523 | // template arguments that we have against the template name, if the template |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1524 | // 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] | 1525 | // though. |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1526 | |
| 1527 | // These should be filtered out by our callers. |
| 1528 | assert(!R.empty() && "empty lookup results when building templateid"); |
| 1529 | assert(!R.isAmbiguous() && "ambiguous lookup when building templateid"); |
| 1530 | |
| 1531 | NestedNameSpecifier *Qualifier = 0; |
| 1532 | SourceRange QualifierRange; |
| 1533 | if (SS.isSet()) { |
| 1534 | Qualifier = static_cast<NestedNameSpecifier*>(SS.getScopeRep()); |
| 1535 | QualifierRange = SS.getRange(); |
Douglas Gregor | a9e29aa | 2009-10-22 07:19:14 +0000 | [diff] [blame] | 1536 | } |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 1537 | |
| 1538 | // We don't want lookup warnings at this point. |
| 1539 | R.suppressDiagnostics(); |
Douglas Gregor | a9e29aa | 2009-10-22 07:19:14 +0000 | [diff] [blame] | 1540 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1541 | bool Dependent |
| 1542 | = UnresolvedLookupExpr::ComputeDependence(R.begin(), R.end(), |
| 1543 | &TemplateArgs); |
| 1544 | UnresolvedLookupExpr *ULE |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 1545 | = UnresolvedLookupExpr::Create(Context, Dependent, R.getNamingClass(), |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1546 | Qualifier, QualifierRange, |
| 1547 | R.getLookupName(), R.getNameLoc(), |
| 1548 | RequiresADL, TemplateArgs); |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 1549 | ULE->addDecls(R.begin(), R.end()); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1550 | |
| 1551 | return Owned(ULE); |
Douglas Gregor | edce4dd | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 1552 | } |
| 1553 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1554 | // We actually only call this from template instantiation. |
| 1555 | Sema::OwningExprResult |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 1556 | Sema::BuildQualifiedTemplateIdExpr(CXXScopeSpec &SS, |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1557 | DeclarationName Name, |
| 1558 | SourceLocation NameLoc, |
| 1559 | const TemplateArgumentListInfo &TemplateArgs) { |
| 1560 | DeclContext *DC; |
| 1561 | if (!(DC = computeDeclContext(SS, false)) || |
| 1562 | DC->isDependentContext() || |
| 1563 | RequireCompleteDeclContext(SS)) |
| 1564 | return BuildDependentDeclRefExpr(SS, Name, NameLoc, &TemplateArgs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1565 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1566 | LookupResult R(*this, Name, NameLoc, LookupOrdinaryName); |
| 1567 | LookupTemplateName(R, (Scope*) 0, SS, QualType(), /*Entering*/ false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1568 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1569 | if (R.isAmbiguous()) |
| 1570 | return ExprError(); |
| 1571 | |
| 1572 | if (R.empty()) { |
| 1573 | Diag(NameLoc, diag::err_template_kw_refers_to_non_template) |
| 1574 | << Name << SS.getRange(); |
| 1575 | return ExprError(); |
| 1576 | } |
| 1577 | |
| 1578 | if (ClassTemplateDecl *Temp = R.getAsSingle<ClassTemplateDecl>()) { |
| 1579 | Diag(NameLoc, diag::err_template_kw_refers_to_class_template) |
| 1580 | << (NestedNameSpecifier*) SS.getScopeRep() << Name << SS.getRange(); |
| 1581 | Diag(Temp->getLocation(), diag::note_referenced_class_template); |
| 1582 | return ExprError(); |
| 1583 | } |
| 1584 | |
| 1585 | return BuildTemplateIdExpr(SS, R, /* ADL */ false, TemplateArgs); |
Douglas Gregor | edce4dd | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 1586 | } |
| 1587 | |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 1588 | /// \brief Form a dependent template name. |
| 1589 | /// |
| 1590 | /// This action forms a dependent template name given the template |
| 1591 | /// name and its (presumably dependent) scope specifier. For |
| 1592 | /// example, given "MetaFun::template apply", the scope specifier \p |
| 1593 | /// SS will be "MetaFun::", \p TemplateKWLoc contains the location |
| 1594 | /// of the "template" keyword, and "apply" is the \p Name. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1595 | Sema::TemplateTy |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 1596 | Sema::ActOnDependentTemplateName(SourceLocation TemplateKWLoc, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 1597 | CXXScopeSpec &SS, |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 1598 | UnqualifiedId &Name, |
Douglas Gregor | a481edb | 2009-11-20 23:39:24 +0000 | [diff] [blame] | 1599 | TypeTy *ObjectType, |
| 1600 | bool EnteringContext) { |
Douglas Gregor | 0707bc5 | 2010-01-19 16:01:07 +0000 | [diff] [blame] | 1601 | DeclContext *LookupCtx = 0; |
| 1602 | if (SS.isSet()) |
| 1603 | LookupCtx = computeDeclContext(SS, EnteringContext); |
| 1604 | if (!LookupCtx && ObjectType) |
| 1605 | LookupCtx = computeDeclContext(QualType::getFromOpaquePtr(ObjectType)); |
| 1606 | if (LookupCtx) { |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 1607 | // C++0x [temp.names]p5: |
| 1608 | // If a name prefixed by the keyword template is not the name of |
| 1609 | // a template, the program is ill-formed. [Note: the keyword |
| 1610 | // template may not be applied to non-template members of class |
| 1611 | // templates. -end note ] [ Note: as is the case with the |
| 1612 | // typename prefix, the template prefix is allowed in cases |
| 1613 | // where it is not strictly necessary; i.e., when the |
| 1614 | // nested-name-specifier or the expression on the left of the -> |
| 1615 | // or . is not dependent on a template-parameter, or the use |
| 1616 | // does not appear in the scope of a template. -end note] |
| 1617 | // |
| 1618 | // Note: C++03 was more strict here, because it banned the use of |
| 1619 | // the "template" keyword prior to a template-name that was not a |
| 1620 | // dependent name. C++ DR468 relaxed this requirement (the |
| 1621 | // "template" keyword is now permitted). We follow the C++0x |
| 1622 | // rules, even in C++03 mode, retroactively applying the DR. |
| 1623 | TemplateTy Template; |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 1624 | TemplateNameKind TNK = isTemplateName(0, SS, Name, ObjectType, |
Douglas Gregor | a481edb | 2009-11-20 23:39:24 +0000 | [diff] [blame] | 1625 | EnteringContext, Template); |
Douglas Gregor | 0707bc5 | 2010-01-19 16:01:07 +0000 | [diff] [blame] | 1626 | if (TNK == TNK_Non_template && LookupCtx->isDependentContext() && |
| 1627 | isa<CXXRecordDecl>(LookupCtx) && |
| 1628 | cast<CXXRecordDecl>(LookupCtx)->hasAnyDependentBases()) { |
Douglas Gregor | 9edad9b | 2010-01-14 17:47:39 +0000 | [diff] [blame] | 1629 | // This is a dependent template. |
| 1630 | } else if (TNK == TNK_Non_template) { |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 1631 | Diag(Name.getSourceRange().getBegin(), |
| 1632 | diag::err_template_kw_refers_to_non_template) |
| 1633 | << GetNameFromUnqualifiedId(Name) |
| 1634 | << Name.getSourceRange(); |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 1635 | return TemplateTy(); |
Douglas Gregor | 9edad9b | 2010-01-14 17:47:39 +0000 | [diff] [blame] | 1636 | } else { |
| 1637 | // We found something; return it. |
| 1638 | return Template; |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 1639 | } |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 1640 | } |
| 1641 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1642 | NestedNameSpecifier *Qualifier |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 1643 | = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 1644 | |
| 1645 | switch (Name.getKind()) { |
| 1646 | case UnqualifiedId::IK_Identifier: |
| 1647 | return TemplateTy::make(Context.getDependentTemplateName(Qualifier, |
| 1648 | Name.Identifier)); |
| 1649 | |
Douglas Gregor | ca1bdd7 | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 1650 | case UnqualifiedId::IK_OperatorFunctionId: |
| 1651 | return TemplateTy::make(Context.getDependentTemplateName(Qualifier, |
| 1652 | Name.OperatorFunctionId.Operator)); |
Sean Hunt | e6252d1 | 2009-11-28 08:58:14 +0000 | [diff] [blame] | 1653 | |
| 1654 | case UnqualifiedId::IK_LiteralOperatorId: |
| 1655 | assert(false && "We don't support these; Parse shouldn't have allowed propagation"); |
| 1656 | |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 1657 | default: |
| 1658 | break; |
| 1659 | } |
| 1660 | |
| 1661 | Diag(Name.getSourceRange().getBegin(), |
| 1662 | diag::err_template_kw_refers_to_non_template) |
| 1663 | << GetNameFromUnqualifiedId(Name) |
| 1664 | << Name.getSourceRange(); |
| 1665 | return TemplateTy(); |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 1666 | } |
| 1667 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1668 | bool Sema::CheckTemplateTypeArgument(TemplateTypeParmDecl *Param, |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1669 | const TemplateArgumentLoc &AL, |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 1670 | TemplateArgumentListBuilder &Converted) { |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1671 | const TemplateArgument &Arg = AL.getArgument(); |
| 1672 | |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 1673 | // Check template type parameter. |
Jeffrey Yasskin | db88d8a | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 1674 | switch(Arg.getKind()) { |
| 1675 | case TemplateArgument::Type: |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 1676 | // C++ [temp.arg.type]p1: |
| 1677 | // A template-argument for a template-parameter which is a |
| 1678 | // type shall be a type-id. |
Jeffrey Yasskin | db88d8a | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 1679 | break; |
| 1680 | case TemplateArgument::Template: { |
| 1681 | // We have a template type parameter but the template argument |
| 1682 | // is a template without any arguments. |
| 1683 | SourceRange SR = AL.getSourceRange(); |
| 1684 | TemplateName Name = Arg.getAsTemplate(); |
| 1685 | Diag(SR.getBegin(), diag::err_template_missing_args) |
| 1686 | << Name << SR; |
| 1687 | if (TemplateDecl *Decl = Name.getAsTemplateDecl()) |
| 1688 | Diag(Decl->getLocation(), diag::note_template_decl_here); |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 1689 | |
Jeffrey Yasskin | db88d8a | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 1690 | return true; |
| 1691 | } |
| 1692 | default: { |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 1693 | // We have a template type parameter but the template argument |
| 1694 | // is not a type. |
John McCall | 828bff2 | 2009-10-29 18:45:58 +0000 | [diff] [blame] | 1695 | SourceRange SR = AL.getSourceRange(); |
| 1696 | Diag(SR.getBegin(), diag::err_template_arg_must_be_type) << SR; |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 1697 | Diag(Param->getLocation(), diag::note_template_param_here); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1698 | |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 1699 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1700 | } |
Jeffrey Yasskin | db88d8a | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 1701 | } |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 1702 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1703 | if (CheckTemplateArgument(Param, AL.getTypeSourceInfo())) |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 1704 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1705 | |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 1706 | // Add the converted template type argument. |
Anders Carlsson | fb25052 | 2009-06-23 01:26:57 +0000 | [diff] [blame] | 1707 | Converted.Append( |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1708 | TemplateArgument(Context.getCanonicalType(Arg.getAsType()))); |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 1709 | return false; |
| 1710 | } |
| 1711 | |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 1712 | /// \brief Substitute template arguments into the default template argument for |
| 1713 | /// the given template type parameter. |
| 1714 | /// |
| 1715 | /// \param SemaRef the semantic analysis object for which we are performing |
| 1716 | /// the substitution. |
| 1717 | /// |
| 1718 | /// \param Template the template that we are synthesizing template arguments |
| 1719 | /// for. |
| 1720 | /// |
| 1721 | /// \param TemplateLoc the location of the template name that started the |
| 1722 | /// template-id we are checking. |
| 1723 | /// |
| 1724 | /// \param RAngleLoc the location of the right angle bracket ('>') that |
| 1725 | /// terminates the template-id. |
| 1726 | /// |
| 1727 | /// \param Param the template template parameter whose default we are |
| 1728 | /// substituting into. |
| 1729 | /// |
| 1730 | /// \param Converted the list of template arguments provided for template |
| 1731 | /// parameters that precede \p Param in the template parameter list. |
| 1732 | /// |
| 1733 | /// \returns the substituted template argument, or NULL if an error occurred. |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1734 | static TypeSourceInfo * |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 1735 | SubstDefaultTemplateArgument(Sema &SemaRef, |
| 1736 | TemplateDecl *Template, |
| 1737 | SourceLocation TemplateLoc, |
| 1738 | SourceLocation RAngleLoc, |
| 1739 | TemplateTypeParmDecl *Param, |
| 1740 | TemplateArgumentListBuilder &Converted) { |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1741 | TypeSourceInfo *ArgType = Param->getDefaultArgumentInfo(); |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 1742 | |
| 1743 | // If the argument type is dependent, instantiate it now based |
| 1744 | // on the previously-computed template arguments. |
| 1745 | if (ArgType->getType()->isDependentType()) { |
| 1746 | TemplateArgumentList TemplateArgs(SemaRef.Context, Converted, |
| 1747 | /*TakeArgs=*/false); |
| 1748 | |
| 1749 | MultiLevelTemplateArgumentList AllTemplateArgs |
| 1750 | = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs); |
| 1751 | |
| 1752 | Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc, |
| 1753 | Template, Converted.getFlatArguments(), |
| 1754 | Converted.flatSize(), |
| 1755 | SourceRange(TemplateLoc, RAngleLoc)); |
| 1756 | |
| 1757 | ArgType = SemaRef.SubstType(ArgType, AllTemplateArgs, |
| 1758 | Param->getDefaultArgumentLoc(), |
| 1759 | Param->getDeclName()); |
| 1760 | } |
| 1761 | |
| 1762 | return ArgType; |
| 1763 | } |
| 1764 | |
| 1765 | /// \brief Substitute template arguments into the default template argument for |
| 1766 | /// the given non-type template parameter. |
| 1767 | /// |
| 1768 | /// \param SemaRef the semantic analysis object for which we are performing |
| 1769 | /// the substitution. |
| 1770 | /// |
| 1771 | /// \param Template the template that we are synthesizing template arguments |
| 1772 | /// for. |
| 1773 | /// |
| 1774 | /// \param TemplateLoc the location of the template name that started the |
| 1775 | /// template-id we are checking. |
| 1776 | /// |
| 1777 | /// \param RAngleLoc the location of the right angle bracket ('>') that |
| 1778 | /// terminates the template-id. |
| 1779 | /// |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1780 | /// \param Param the non-type template parameter whose default we are |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 1781 | /// substituting into. |
| 1782 | /// |
| 1783 | /// \param Converted the list of template arguments provided for template |
| 1784 | /// parameters that precede \p Param in the template parameter list. |
| 1785 | /// |
| 1786 | /// \returns the substituted template argument, or NULL if an error occurred. |
| 1787 | static Sema::OwningExprResult |
| 1788 | SubstDefaultTemplateArgument(Sema &SemaRef, |
| 1789 | TemplateDecl *Template, |
| 1790 | SourceLocation TemplateLoc, |
| 1791 | SourceLocation RAngleLoc, |
| 1792 | NonTypeTemplateParmDecl *Param, |
| 1793 | TemplateArgumentListBuilder &Converted) { |
| 1794 | TemplateArgumentList TemplateArgs(SemaRef.Context, Converted, |
| 1795 | /*TakeArgs=*/false); |
| 1796 | |
| 1797 | MultiLevelTemplateArgumentList AllTemplateArgs |
| 1798 | = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs); |
| 1799 | |
| 1800 | Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc, |
| 1801 | Template, Converted.getFlatArguments(), |
| 1802 | Converted.flatSize(), |
| 1803 | SourceRange(TemplateLoc, RAngleLoc)); |
| 1804 | |
| 1805 | return SemaRef.SubstExpr(Param->getDefaultArgument(), AllTemplateArgs); |
| 1806 | } |
| 1807 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1808 | /// \brief Substitute template arguments into the default template argument for |
| 1809 | /// the given template template parameter. |
| 1810 | /// |
| 1811 | /// \param SemaRef the semantic analysis object for which we are performing |
| 1812 | /// the substitution. |
| 1813 | /// |
| 1814 | /// \param Template the template that we are synthesizing template arguments |
| 1815 | /// for. |
| 1816 | /// |
| 1817 | /// \param TemplateLoc the location of the template name that started the |
| 1818 | /// template-id we are checking. |
| 1819 | /// |
| 1820 | /// \param RAngleLoc the location of the right angle bracket ('>') that |
| 1821 | /// terminates the template-id. |
| 1822 | /// |
| 1823 | /// \param Param the template template parameter whose default we are |
| 1824 | /// substituting into. |
| 1825 | /// |
| 1826 | /// \param Converted the list of template arguments provided for template |
| 1827 | /// parameters that precede \p Param in the template parameter list. |
| 1828 | /// |
| 1829 | /// \returns the substituted template argument, or NULL if an error occurred. |
| 1830 | static TemplateName |
| 1831 | SubstDefaultTemplateArgument(Sema &SemaRef, |
| 1832 | TemplateDecl *Template, |
| 1833 | SourceLocation TemplateLoc, |
| 1834 | SourceLocation RAngleLoc, |
| 1835 | TemplateTemplateParmDecl *Param, |
| 1836 | TemplateArgumentListBuilder &Converted) { |
| 1837 | TemplateArgumentList TemplateArgs(SemaRef.Context, Converted, |
| 1838 | /*TakeArgs=*/false); |
| 1839 | |
| 1840 | MultiLevelTemplateArgumentList AllTemplateArgs |
| 1841 | = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs); |
| 1842 | |
| 1843 | Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc, |
| 1844 | Template, Converted.getFlatArguments(), |
| 1845 | Converted.flatSize(), |
| 1846 | SourceRange(TemplateLoc, RAngleLoc)); |
| 1847 | |
| 1848 | return SemaRef.SubstTemplateName( |
| 1849 | Param->getDefaultArgument().getArgument().getAsTemplate(), |
| 1850 | Param->getDefaultArgument().getTemplateNameLoc(), |
| 1851 | AllTemplateArgs); |
| 1852 | } |
| 1853 | |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 1854 | /// \brief If the given template parameter has a default template |
| 1855 | /// argument, substitute into that default template argument and |
| 1856 | /// return the corresponding template argument. |
| 1857 | TemplateArgumentLoc |
| 1858 | Sema::SubstDefaultTemplateArgumentIfAvailable(TemplateDecl *Template, |
| 1859 | SourceLocation TemplateLoc, |
| 1860 | SourceLocation RAngleLoc, |
| 1861 | Decl *Param, |
| 1862 | TemplateArgumentListBuilder &Converted) { |
| 1863 | if (TemplateTypeParmDecl *TypeParm = dyn_cast<TemplateTypeParmDecl>(Param)) { |
| 1864 | if (!TypeParm->hasDefaultArgument()) |
| 1865 | return TemplateArgumentLoc(); |
| 1866 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1867 | TypeSourceInfo *DI = SubstDefaultTemplateArgument(*this, Template, |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 1868 | TemplateLoc, |
| 1869 | RAngleLoc, |
| 1870 | TypeParm, |
| 1871 | Converted); |
| 1872 | if (DI) |
| 1873 | return TemplateArgumentLoc(TemplateArgument(DI->getType()), DI); |
| 1874 | |
| 1875 | return TemplateArgumentLoc(); |
| 1876 | } |
| 1877 | |
| 1878 | if (NonTypeTemplateParmDecl *NonTypeParm |
| 1879 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
| 1880 | if (!NonTypeParm->hasDefaultArgument()) |
| 1881 | return TemplateArgumentLoc(); |
| 1882 | |
| 1883 | OwningExprResult Arg = SubstDefaultTemplateArgument(*this, Template, |
| 1884 | TemplateLoc, |
| 1885 | RAngleLoc, |
| 1886 | NonTypeParm, |
| 1887 | Converted); |
| 1888 | if (Arg.isInvalid()) |
| 1889 | return TemplateArgumentLoc(); |
| 1890 | |
| 1891 | Expr *ArgE = Arg.takeAs<Expr>(); |
| 1892 | return TemplateArgumentLoc(TemplateArgument(ArgE), ArgE); |
| 1893 | } |
| 1894 | |
| 1895 | TemplateTemplateParmDecl *TempTempParm |
| 1896 | = cast<TemplateTemplateParmDecl>(Param); |
| 1897 | if (!TempTempParm->hasDefaultArgument()) |
| 1898 | return TemplateArgumentLoc(); |
| 1899 | |
| 1900 | TemplateName TName = SubstDefaultTemplateArgument(*this, Template, |
| 1901 | TemplateLoc, |
| 1902 | RAngleLoc, |
| 1903 | TempTempParm, |
| 1904 | Converted); |
| 1905 | if (TName.isNull()) |
| 1906 | return TemplateArgumentLoc(); |
| 1907 | |
| 1908 | return TemplateArgumentLoc(TemplateArgument(TName), |
| 1909 | TempTempParm->getDefaultArgument().getTemplateQualifierRange(), |
| 1910 | TempTempParm->getDefaultArgument().getTemplateNameLoc()); |
| 1911 | } |
| 1912 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 1913 | /// \brief Check that the given template argument corresponds to the given |
| 1914 | /// template parameter. |
| 1915 | bool Sema::CheckTemplateArgument(NamedDecl *Param, |
| 1916 | const TemplateArgumentLoc &Arg, |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 1917 | TemplateDecl *Template, |
| 1918 | SourceLocation TemplateLoc, |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 1919 | SourceLocation RAngleLoc, |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 1920 | TemplateArgumentListBuilder &Converted, |
| 1921 | CheckTemplateArgumentKind CTAK) { |
Douglas Gregor | d9e1530 | 2009-11-11 19:41:09 +0000 | [diff] [blame] | 1922 | // Check template type parameters. |
| 1923 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 1924 | return CheckTemplateTypeArgument(TTP, Arg, Converted); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 1925 | |
Douglas Gregor | d9e1530 | 2009-11-11 19:41:09 +0000 | [diff] [blame] | 1926 | // Check non-type template parameters. |
| 1927 | if (NonTypeTemplateParmDecl *NTTP =dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 1928 | // Do substitution on the type of the non-type template parameter |
| 1929 | // with the template arguments we've seen thus far. |
| 1930 | QualType NTTPType = NTTP->getType(); |
| 1931 | if (NTTPType->isDependentType()) { |
| 1932 | // Do substitution on the type of the non-type template parameter. |
| 1933 | InstantiatingTemplate Inst(*this, TemplateLoc, Template, |
| 1934 | NTTP, Converted.getFlatArguments(), |
| 1935 | Converted.flatSize(), |
| 1936 | SourceRange(TemplateLoc, RAngleLoc)); |
| 1937 | |
| 1938 | TemplateArgumentList TemplateArgs(Context, Converted, |
| 1939 | /*TakeArgs=*/false); |
| 1940 | NTTPType = SubstType(NTTPType, |
| 1941 | MultiLevelTemplateArgumentList(TemplateArgs), |
| 1942 | NTTP->getLocation(), |
| 1943 | NTTP->getDeclName()); |
| 1944 | // If that worked, check the non-type template parameter type |
| 1945 | // for validity. |
| 1946 | if (!NTTPType.isNull()) |
| 1947 | NTTPType = CheckNonTypeTemplateParameterType(NTTPType, |
| 1948 | NTTP->getLocation()); |
| 1949 | if (NTTPType.isNull()) |
| 1950 | return true; |
| 1951 | } |
| 1952 | |
| 1953 | switch (Arg.getArgument().getKind()) { |
| 1954 | case TemplateArgument::Null: |
| 1955 | assert(false && "Should never see a NULL template argument here"); |
| 1956 | return true; |
| 1957 | |
| 1958 | case TemplateArgument::Expression: { |
| 1959 | Expr *E = Arg.getArgument().getAsExpr(); |
| 1960 | TemplateArgument Result; |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 1961 | if (CheckTemplateArgument(NTTP, NTTPType, E, Result, CTAK)) |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 1962 | return true; |
| 1963 | |
| 1964 | Converted.Append(Result); |
| 1965 | break; |
| 1966 | } |
| 1967 | |
| 1968 | case TemplateArgument::Declaration: |
| 1969 | case TemplateArgument::Integral: |
| 1970 | // We've already checked this template argument, so just copy |
| 1971 | // it to the list of converted arguments. |
| 1972 | Converted.Append(Arg.getArgument()); |
| 1973 | break; |
| 1974 | |
| 1975 | case TemplateArgument::Template: |
| 1976 | // We were given a template template argument. It may not be ill-formed; |
| 1977 | // see below. |
| 1978 | if (DependentTemplateName *DTN |
| 1979 | = Arg.getArgument().getAsTemplate().getAsDependentTemplateName()) { |
| 1980 | // We have a template argument such as \c T::template X, which we |
| 1981 | // parsed as a template template argument. However, since we now |
| 1982 | // know that we need a non-type template argument, convert this |
| 1983 | // template name into an expression. |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1984 | Expr *E = DependentScopeDeclRefExpr::Create(Context, |
| 1985 | DTN->getQualifier(), |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 1986 | Arg.getTemplateQualifierRange(), |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1987 | DTN->getIdentifier(), |
| 1988 | Arg.getTemplateNameLoc()); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 1989 | |
| 1990 | TemplateArgument Result; |
| 1991 | if (CheckTemplateArgument(NTTP, NTTPType, E, Result)) |
| 1992 | return true; |
| 1993 | |
| 1994 | Converted.Append(Result); |
| 1995 | break; |
| 1996 | } |
| 1997 | |
| 1998 | // We have a template argument that actually does refer to a class |
| 1999 | // template, template alias, or template template parameter, and |
| 2000 | // therefore cannot be a non-type template argument. |
| 2001 | Diag(Arg.getLocation(), diag::err_template_arg_must_be_expr) |
| 2002 | << Arg.getSourceRange(); |
| 2003 | |
| 2004 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 2005 | return true; |
| 2006 | |
| 2007 | case TemplateArgument::Type: { |
| 2008 | // We have a non-type template parameter but the template |
| 2009 | // argument is a type. |
| 2010 | |
| 2011 | // C++ [temp.arg]p2: |
| 2012 | // In a template-argument, an ambiguity between a type-id and |
| 2013 | // an expression is resolved to a type-id, regardless of the |
| 2014 | // form of the corresponding template-parameter. |
| 2015 | // |
| 2016 | // We warn specifically about this case, since it can be rather |
| 2017 | // confusing for users. |
| 2018 | QualType T = Arg.getArgument().getAsType(); |
| 2019 | SourceRange SR = Arg.getSourceRange(); |
| 2020 | if (T->isFunctionType()) |
| 2021 | Diag(SR.getBegin(), diag::err_template_arg_nontype_ambig) << SR << T; |
| 2022 | else |
| 2023 | Diag(SR.getBegin(), diag::err_template_arg_must_be_expr) << SR; |
| 2024 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 2025 | return true; |
| 2026 | } |
| 2027 | |
| 2028 | case TemplateArgument::Pack: |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 2029 | llvm_unreachable("Caller must expand template argument packs"); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2030 | break; |
| 2031 | } |
| 2032 | |
| 2033 | return false; |
| 2034 | } |
| 2035 | |
| 2036 | |
| 2037 | // Check template template parameters. |
| 2038 | TemplateTemplateParmDecl *TempParm = cast<TemplateTemplateParmDecl>(Param); |
| 2039 | |
| 2040 | // Substitute into the template parameter list of the template |
| 2041 | // template parameter, since previously-supplied template arguments |
| 2042 | // may appear within the template template parameter. |
| 2043 | { |
| 2044 | // Set up a template instantiation context. |
| 2045 | LocalInstantiationScope Scope(*this); |
| 2046 | InstantiatingTemplate Inst(*this, TemplateLoc, Template, |
| 2047 | TempParm, Converted.getFlatArguments(), |
| 2048 | Converted.flatSize(), |
| 2049 | SourceRange(TemplateLoc, RAngleLoc)); |
| 2050 | |
| 2051 | TemplateArgumentList TemplateArgs(Context, Converted, |
| 2052 | /*TakeArgs=*/false); |
| 2053 | TempParm = cast_or_null<TemplateTemplateParmDecl>( |
| 2054 | SubstDecl(TempParm, CurContext, |
| 2055 | MultiLevelTemplateArgumentList(TemplateArgs))); |
| 2056 | if (!TempParm) |
| 2057 | return true; |
| 2058 | |
| 2059 | // FIXME: TempParam is leaked. |
| 2060 | } |
| 2061 | |
| 2062 | switch (Arg.getArgument().getKind()) { |
| 2063 | case TemplateArgument::Null: |
| 2064 | assert(false && "Should never see a NULL template argument here"); |
| 2065 | return true; |
| 2066 | |
| 2067 | case TemplateArgument::Template: |
| 2068 | if (CheckTemplateArgument(TempParm, Arg)) |
| 2069 | return true; |
| 2070 | |
| 2071 | Converted.Append(Arg.getArgument()); |
| 2072 | break; |
| 2073 | |
| 2074 | case TemplateArgument::Expression: |
| 2075 | case TemplateArgument::Type: |
| 2076 | // We have a template template parameter but the template |
| 2077 | // argument does not refer to a template. |
| 2078 | Diag(Arg.getLocation(), diag::err_template_arg_must_be_template); |
| 2079 | return true; |
| 2080 | |
| 2081 | case TemplateArgument::Declaration: |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 2082 | llvm_unreachable( |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2083 | "Declaration argument with template template parameter"); |
| 2084 | break; |
| 2085 | case TemplateArgument::Integral: |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 2086 | llvm_unreachable( |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2087 | "Integral argument with template template parameter"); |
| 2088 | break; |
| 2089 | |
| 2090 | case TemplateArgument::Pack: |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 2091 | llvm_unreachable("Caller must expand template argument packs"); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2092 | break; |
| 2093 | } |
| 2094 | |
| 2095 | return false; |
| 2096 | } |
| 2097 | |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2098 | /// \brief Check that the given template argument list is well-formed |
| 2099 | /// for specializing the given template. |
| 2100 | bool Sema::CheckTemplateArgumentList(TemplateDecl *Template, |
| 2101 | SourceLocation TemplateLoc, |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2102 | const TemplateArgumentListInfo &TemplateArgs, |
Douglas Gregor | 16134c6 | 2009-07-01 00:28:38 +0000 | [diff] [blame] | 2103 | bool PartialTemplateArgs, |
Anders Carlsson | 1c5976e | 2009-06-05 03:43:12 +0000 | [diff] [blame] | 2104 | TemplateArgumentListBuilder &Converted) { |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2105 | TemplateParameterList *Params = Template->getTemplateParameters(); |
| 2106 | unsigned NumParams = Params->size(); |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2107 | unsigned NumArgs = TemplateArgs.size(); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2108 | bool Invalid = false; |
| 2109 | |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2110 | SourceLocation RAngleLoc = TemplateArgs.getRAngleLoc(); |
| 2111 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2112 | bool HasParameterPack = |
Anders Carlsson | 0ceffb5 | 2009-06-13 02:08:00 +0000 | [diff] [blame] | 2113 | NumParams > 0 && Params->getParam(NumParams - 1)->isTemplateParameterPack(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2114 | |
Anders Carlsson | 0ceffb5 | 2009-06-13 02:08:00 +0000 | [diff] [blame] | 2115 | if ((NumArgs > NumParams && !HasParameterPack) || |
Douglas Gregor | 16134c6 | 2009-07-01 00:28:38 +0000 | [diff] [blame] | 2116 | (NumArgs < Params->getMinRequiredArguments() && |
| 2117 | !PartialTemplateArgs)) { |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2118 | // FIXME: point at either the first arg beyond what we can handle, |
| 2119 | // or the '>', depending on whether we have too many or too few |
| 2120 | // arguments. |
| 2121 | SourceRange Range; |
| 2122 | if (NumArgs > NumParams) |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2123 | Range = SourceRange(TemplateArgs[NumParams].getLocation(), RAngleLoc); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2124 | Diag(TemplateLoc, diag::err_template_arg_list_different_arity) |
| 2125 | << (NumArgs > NumParams) |
| 2126 | << (isa<ClassTemplateDecl>(Template)? 0 : |
| 2127 | isa<FunctionTemplateDecl>(Template)? 1 : |
| 2128 | isa<TemplateTemplateParmDecl>(Template)? 2 : 3) |
| 2129 | << Template << Range; |
Douglas Gregor | 62cb18d | 2009-02-11 18:16:40 +0000 | [diff] [blame] | 2130 | Diag(Template->getLocation(), diag::note_template_decl_here) |
| 2131 | << Params->getSourceRange(); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2132 | Invalid = true; |
| 2133 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2134 | |
| 2135 | // C++ [temp.arg]p1: |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2136 | // [...] The type and form of each template-argument specified in |
| 2137 | // a template-id shall match the type and form specified for the |
| 2138 | // corresponding parameter declared by the template in its |
| 2139 | // template-parameter-list. |
| 2140 | unsigned ArgIdx = 0; |
| 2141 | for (TemplateParameterList::iterator Param = Params->begin(), |
| 2142 | ParamEnd = Params->end(); |
| 2143 | Param != ParamEnd; ++Param, ++ArgIdx) { |
Douglas Gregor | 16134c6 | 2009-07-01 00:28:38 +0000 | [diff] [blame] | 2144 | if (ArgIdx > NumArgs && PartialTemplateArgs) |
| 2145 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2146 | |
Douglas Gregor | d9e1530 | 2009-11-11 19:41:09 +0000 | [diff] [blame] | 2147 | // If we have a template parameter pack, check every remaining template |
| 2148 | // argument against that template parameter pack. |
| 2149 | if ((*Param)->isTemplateParameterPack()) { |
| 2150 | Converted.BeginPack(); |
| 2151 | for (; ArgIdx < NumArgs; ++ArgIdx) { |
| 2152 | if (CheckTemplateArgument(*Param, TemplateArgs[ArgIdx], Template, |
| 2153 | TemplateLoc, RAngleLoc, Converted)) { |
| 2154 | Invalid = true; |
| 2155 | break; |
| 2156 | } |
| 2157 | } |
| 2158 | Converted.EndPack(); |
| 2159 | continue; |
| 2160 | } |
| 2161 | |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2162 | if (ArgIdx < NumArgs) { |
| 2163 | // Check the template argument we were given. |
| 2164 | if (CheckTemplateArgument(*Param, TemplateArgs[ArgIdx], Template, |
| 2165 | TemplateLoc, RAngleLoc, Converted)) |
| 2166 | return true; |
| 2167 | |
| 2168 | continue; |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 2169 | } |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2170 | |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2171 | // We have a default template argument that we will use. |
| 2172 | TemplateArgumentLoc Arg; |
| 2173 | |
| 2174 | // Retrieve the default template argument from the template |
| 2175 | // parameter. For each kind of template parameter, we substitute the |
| 2176 | // template arguments provided thus far and any "outer" template arguments |
| 2177 | // (when the template parameter was part of a nested template) into |
| 2178 | // the default argument. |
| 2179 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*Param)) { |
| 2180 | if (!TTP->hasDefaultArgument()) { |
| 2181 | assert((Invalid || PartialTemplateArgs) && "Missing default argument"); |
| 2182 | break; |
| 2183 | } |
| 2184 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2185 | TypeSourceInfo *ArgType = SubstDefaultTemplateArgument(*this, |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2186 | Template, |
| 2187 | TemplateLoc, |
| 2188 | RAngleLoc, |
| 2189 | TTP, |
| 2190 | Converted); |
| 2191 | if (!ArgType) |
| 2192 | return true; |
| 2193 | |
| 2194 | Arg = TemplateArgumentLoc(TemplateArgument(ArgType->getType()), |
| 2195 | ArgType); |
| 2196 | } else if (NonTypeTemplateParmDecl *NTTP |
| 2197 | = dyn_cast<NonTypeTemplateParmDecl>(*Param)) { |
| 2198 | if (!NTTP->hasDefaultArgument()) { |
| 2199 | assert((Invalid || PartialTemplateArgs) && "Missing default argument"); |
| 2200 | break; |
| 2201 | } |
| 2202 | |
| 2203 | Sema::OwningExprResult E = SubstDefaultTemplateArgument(*this, Template, |
| 2204 | TemplateLoc, |
| 2205 | RAngleLoc, |
| 2206 | NTTP, |
| 2207 | Converted); |
| 2208 | if (E.isInvalid()) |
| 2209 | return true; |
| 2210 | |
| 2211 | Expr *Ex = E.takeAs<Expr>(); |
| 2212 | Arg = TemplateArgumentLoc(TemplateArgument(Ex), Ex); |
| 2213 | } else { |
| 2214 | TemplateTemplateParmDecl *TempParm |
| 2215 | = cast<TemplateTemplateParmDecl>(*Param); |
| 2216 | |
| 2217 | if (!TempParm->hasDefaultArgument()) { |
| 2218 | assert((Invalid || PartialTemplateArgs) && "Missing default argument"); |
| 2219 | break; |
| 2220 | } |
| 2221 | |
| 2222 | TemplateName Name = SubstDefaultTemplateArgument(*this, Template, |
| 2223 | TemplateLoc, |
| 2224 | RAngleLoc, |
| 2225 | TempParm, |
| 2226 | Converted); |
| 2227 | if (Name.isNull()) |
| 2228 | return true; |
| 2229 | |
| 2230 | Arg = TemplateArgumentLoc(TemplateArgument(Name), |
| 2231 | TempParm->getDefaultArgument().getTemplateQualifierRange(), |
| 2232 | TempParm->getDefaultArgument().getTemplateNameLoc()); |
| 2233 | } |
| 2234 | |
| 2235 | // Introduce an instantiation record that describes where we are using |
| 2236 | // the default template argument. |
| 2237 | InstantiatingTemplate Instantiating(*this, RAngleLoc, Template, *Param, |
| 2238 | Converted.getFlatArguments(), |
| 2239 | Converted.flatSize(), |
| 2240 | SourceRange(TemplateLoc, RAngleLoc)); |
| 2241 | |
| 2242 | // Check the default template argument. |
Douglas Gregor | d9e1530 | 2009-11-11 19:41:09 +0000 | [diff] [blame] | 2243 | if (CheckTemplateArgument(*Param, Arg, Template, TemplateLoc, |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2244 | RAngleLoc, Converted)) |
| 2245 | return true; |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2246 | } |
| 2247 | |
| 2248 | return Invalid; |
| 2249 | } |
| 2250 | |
| 2251 | /// \brief Check a template argument against its corresponding |
| 2252 | /// template type parameter. |
| 2253 | /// |
| 2254 | /// This routine implements the semantics of C++ [temp.arg.type]. It |
| 2255 | /// returns true if an error occurred, and false otherwise. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2256 | bool Sema::CheckTemplateArgument(TemplateTypeParmDecl *Param, |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2257 | TypeSourceInfo *ArgInfo) { |
| 2258 | assert(ArgInfo && "invalid TypeSourceInfo"); |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2259 | QualType Arg = ArgInfo->getType(); |
| 2260 | |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2261 | // C++ [temp.arg.type]p2: |
| 2262 | // A local type, a type with no linkage, an unnamed type or a type |
| 2263 | // compounded from any of these types shall not be used as a |
| 2264 | // template-argument for a template type-parameter. |
| 2265 | // |
| 2266 | // FIXME: Perform the recursive and no-linkage type checks. |
| 2267 | const TagType *Tag = 0; |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2268 | if (const EnumType *EnumT = Arg->getAs<EnumType>()) |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2269 | Tag = EnumT; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2270 | else if (const RecordType *RecordT = Arg->getAs<RecordType>()) |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2271 | Tag = RecordT; |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2272 | if (Tag && Tag->getDecl()->getDeclContext()->isFunctionOrMethod()) { |
| 2273 | SourceRange SR = ArgInfo->getTypeLoc().getFullSourceRange(); |
| 2274 | return Diag(SR.getBegin(), diag::err_template_arg_local_type) |
| 2275 | << QualType(Tag, 0) << SR; |
| 2276 | } else if (Tag && !Tag->getDecl()->getDeclName() && |
Douglas Gregor | 9813753 | 2009-03-10 18:33:27 +0000 | [diff] [blame] | 2277 | !Tag->getDecl()->getTypedefForAnonDecl()) { |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2278 | SourceRange SR = ArgInfo->getTypeLoc().getFullSourceRange(); |
| 2279 | Diag(SR.getBegin(), diag::err_template_arg_unnamed_type) << SR; |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2280 | Diag(Tag->getDecl()->getLocation(), diag::note_template_unnamed_type_here); |
| 2281 | return true; |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 2282 | } else if (Context.hasSameUnqualifiedType(Arg, Context.OverloadTy)) { |
| 2283 | SourceRange SR = ArgInfo->getTypeLoc().getFullSourceRange(); |
| 2284 | return Diag(SR.getBegin(), diag::err_template_arg_overload_type) << SR; |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2285 | } |
| 2286 | |
| 2287 | return false; |
| 2288 | } |
| 2289 | |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2290 | /// \brief Checks whether the given template argument is the address |
| 2291 | /// of an object or function according to C++ [temp.arg.nontype]p1. |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2292 | static bool |
| 2293 | CheckTemplateArgumentAddressOfObjectOrFunction(Sema &S, |
| 2294 | NonTypeTemplateParmDecl *Param, |
| 2295 | QualType ParamType, |
| 2296 | Expr *ArgIn, |
| 2297 | TemplateArgument &Converted) { |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2298 | bool Invalid = false; |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2299 | Expr *Arg = ArgIn; |
| 2300 | QualType ArgType = Arg->getType(); |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2301 | |
| 2302 | // See through any implicit casts we added to fix the type. |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 2303 | while (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(Arg)) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2304 | Arg = Cast->getSubExpr(); |
| 2305 | |
| 2306 | // C++ [temp.arg.nontype]p1: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2307 | // |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2308 | // A template-argument for a non-type, non-template |
| 2309 | // template-parameter shall be one of: [...] |
| 2310 | // |
| 2311 | // -- the address of an object or function with external |
| 2312 | // linkage, including function templates and function |
| 2313 | // template-ids but excluding non-static class members, |
| 2314 | // expressed as & id-expression where the & is optional if |
| 2315 | // the name refers to a function or array, or if the |
| 2316 | // corresponding template-parameter is a reference; or |
| 2317 | DeclRefExpr *DRE = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2318 | |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2319 | // Ignore (and complain about) any excess parentheses. |
| 2320 | while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) { |
| 2321 | if (!Invalid) { |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2322 | S.Diag(Arg->getSourceRange().getBegin(), |
| 2323 | diag::err_template_arg_extra_parens) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2324 | << Arg->getSourceRange(); |
| 2325 | Invalid = true; |
| 2326 | } |
| 2327 | |
| 2328 | Arg = Parens->getSubExpr(); |
| 2329 | } |
| 2330 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2331 | bool AddressTaken = false; |
| 2332 | SourceLocation AddrOpLoc; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2333 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) { |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2334 | if (UnOp->getOpcode() == UnaryOperator::AddrOf) { |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2335 | DRE = dyn_cast<DeclRefExpr>(UnOp->getSubExpr()); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2336 | AddressTaken = true; |
| 2337 | AddrOpLoc = UnOp->getOperatorLoc(); |
| 2338 | } |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2339 | } else |
| 2340 | DRE = dyn_cast<DeclRefExpr>(Arg); |
| 2341 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2342 | if (!DRE) { |
| 2343 | if (S.Context.hasSameUnqualifiedType(ArgType, S.Context.OverloadTy)) { |
| 2344 | S.Diag(Arg->getLocStart(), |
| 2345 | diag::err_template_arg_unresolved_overloaded_function) |
| 2346 | << ParamType << Arg->getSourceRange(); |
| 2347 | } else { |
| 2348 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_decl_ref) |
| 2349 | << Arg->getSourceRange(); |
| 2350 | } |
| 2351 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 2352 | return true; |
| 2353 | } |
Chandler Carruth | 038cc39 | 2010-01-31 10:01:20 +0000 | [diff] [blame] | 2354 | |
| 2355 | // Stop checking the precise nature of the argument if it is value dependent, |
| 2356 | // it should be checked when instantiated. |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2357 | if (Arg->isValueDependent()) { |
| 2358 | Converted = TemplateArgument(ArgIn->Retain()); |
Chandler Carruth | 038cc39 | 2010-01-31 10:01:20 +0000 | [diff] [blame] | 2359 | return false; |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2360 | } |
Chandler Carruth | 038cc39 | 2010-01-31 10:01:20 +0000 | [diff] [blame] | 2361 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2362 | if (!isa<ValueDecl>(DRE->getDecl())) { |
| 2363 | S.Diag(Arg->getSourceRange().getBegin(), |
| 2364 | diag::err_template_arg_not_object_or_func_form) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2365 | << Arg->getSourceRange(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2366 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 2367 | return true; |
| 2368 | } |
| 2369 | |
| 2370 | NamedDecl *Entity = 0; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2371 | |
| 2372 | // Cannot refer to non-static data members |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2373 | if (FieldDecl *Field = dyn_cast<FieldDecl>(DRE->getDecl())) { |
| 2374 | S.Diag(Arg->getSourceRange().getBegin(), diag::err_template_arg_field) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2375 | << Field << Arg->getSourceRange(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2376 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 2377 | return true; |
| 2378 | } |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2379 | |
| 2380 | // Cannot refer to non-static member functions |
| 2381 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(DRE->getDecl())) |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2382 | if (!Method->isStatic()) { |
| 2383 | S.Diag(Arg->getSourceRange().getBegin(), diag::err_template_arg_method) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2384 | << Method << Arg->getSourceRange(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2385 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 2386 | return true; |
| 2387 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2388 | |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2389 | // Functions must have external linkage. |
| 2390 | if (FunctionDecl *Func = dyn_cast<FunctionDecl>(DRE->getDecl())) { |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 2391 | if (!isExternalLinkage(Func->getLinkage())) { |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2392 | S.Diag(Arg->getSourceRange().getBegin(), |
| 2393 | diag::err_template_arg_function_not_extern) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2394 | << Func << Arg->getSourceRange(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2395 | S.Diag(Func->getLocation(), diag::note_template_arg_internal_object) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2396 | << true; |
| 2397 | return true; |
| 2398 | } |
| 2399 | |
| 2400 | // Okay: we've named a function with external linkage. |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 2401 | Entity = Func; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2402 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2403 | // If the template parameter has pointer type, the function decays. |
| 2404 | if (ParamType->isPointerType() && !AddressTaken) |
| 2405 | ArgType = S.Context.getPointerType(Func->getType()); |
| 2406 | else if (AddressTaken && ParamType->isReferenceType()) { |
| 2407 | // If we originally had an address-of operator, but the |
| 2408 | // parameter has reference type, complain and (if things look |
| 2409 | // like they will work) drop the address-of operator. |
| 2410 | if (!S.Context.hasSameUnqualifiedType(Func->getType(), |
| 2411 | ParamType.getNonReferenceType())) { |
| 2412 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 2413 | << ParamType; |
| 2414 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 2415 | return true; |
| 2416 | } |
| 2417 | |
| 2418 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 2419 | << ParamType |
| 2420 | << FixItHint::CreateRemoval(AddrOpLoc); |
| 2421 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 2422 | |
| 2423 | ArgType = Func->getType(); |
| 2424 | } |
| 2425 | } else if (VarDecl *Var = dyn_cast<VarDecl>(DRE->getDecl())) { |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 2426 | if (!isExternalLinkage(Var->getLinkage())) { |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2427 | S.Diag(Arg->getSourceRange().getBegin(), |
| 2428 | diag::err_template_arg_object_not_extern) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2429 | << Var << Arg->getSourceRange(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2430 | S.Diag(Var->getLocation(), diag::note_template_arg_internal_object) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2431 | << true; |
| 2432 | return true; |
| 2433 | } |
| 2434 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2435 | // A value of reference type is not an object. |
| 2436 | if (Var->getType()->isReferenceType()) { |
| 2437 | S.Diag(Arg->getSourceRange().getBegin(), |
| 2438 | diag::err_template_arg_reference_var) |
| 2439 | << Var->getType() << Arg->getSourceRange(); |
| 2440 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 2441 | return true; |
| 2442 | } |
| 2443 | |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2444 | // Okay: we've named an object with external linkage |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 2445 | Entity = Var; |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2446 | |
| 2447 | // If the template parameter has pointer type, we must have taken |
| 2448 | // the address of this object. |
| 2449 | if (ParamType->isReferenceType()) { |
| 2450 | if (AddressTaken) { |
| 2451 | // If we originally had an address-of operator, but the |
| 2452 | // parameter has reference type, complain and (if things look |
| 2453 | // like they will work) drop the address-of operator. |
| 2454 | if (!S.Context.hasSameUnqualifiedType(Var->getType(), |
| 2455 | ParamType.getNonReferenceType())) { |
| 2456 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 2457 | << ParamType; |
| 2458 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 2459 | return true; |
| 2460 | } |
| 2461 | |
| 2462 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 2463 | << ParamType |
| 2464 | << FixItHint::CreateRemoval(AddrOpLoc); |
| 2465 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 2466 | |
| 2467 | ArgType = Var->getType(); |
| 2468 | } |
| 2469 | } else if (!AddressTaken && ParamType->isPointerType()) { |
| 2470 | if (Var->getType()->isArrayType()) { |
| 2471 | // Array-to-pointer decay. |
| 2472 | ArgType = S.Context.getArrayDecayedType(Var->getType()); |
| 2473 | } else { |
| 2474 | // If the template parameter has pointer type but the address of |
| 2475 | // this object was not taken, complain and (possibly) recover by |
| 2476 | // taking the address of the entity. |
| 2477 | ArgType = S.Context.getPointerType(Var->getType()); |
| 2478 | if (!S.Context.hasSameUnqualifiedType(ArgType, ParamType)) { |
| 2479 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_address_of) |
| 2480 | << ParamType; |
| 2481 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 2482 | return true; |
| 2483 | } |
| 2484 | |
| 2485 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_address_of) |
| 2486 | << ParamType |
| 2487 | << FixItHint::CreateInsertion(Arg->getLocStart(), "&"); |
| 2488 | |
| 2489 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 2490 | } |
| 2491 | } |
| 2492 | } else { |
| 2493 | // We found something else, but we don't know specifically what it is. |
| 2494 | S.Diag(Arg->getSourceRange().getBegin(), |
| 2495 | diag::err_template_arg_not_object_or_func) |
| 2496 | << Arg->getSourceRange(); |
| 2497 | S.Diag(DRE->getDecl()->getLocation(), diag::note_template_arg_refers_here); |
| 2498 | return true; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2499 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2500 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2501 | if (ParamType->isPointerType() && |
| 2502 | !ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType() && |
| 2503 | S.IsQualificationConversion(ArgType, ParamType)) { |
| 2504 | // For pointer-to-object types, qualification conversions are |
| 2505 | // permitted. |
| 2506 | } else { |
| 2507 | if (const ReferenceType *ParamRef = ParamType->getAs<ReferenceType>()) { |
| 2508 | if (!ParamRef->getPointeeType()->isFunctionType()) { |
| 2509 | // C++ [temp.arg.nontype]p5b3: |
| 2510 | // For a non-type template-parameter of type reference to |
| 2511 | // object, no conversions apply. The type referred to by the |
| 2512 | // reference may be more cv-qualified than the (otherwise |
| 2513 | // identical) type of the template- argument. The |
| 2514 | // template-parameter is bound directly to the |
| 2515 | // template-argument, which shall be an lvalue. |
| 2516 | |
| 2517 | // FIXME: Other qualifiers? |
| 2518 | unsigned ParamQuals = ParamRef->getPointeeType().getCVRQualifiers(); |
| 2519 | unsigned ArgQuals = ArgType.getCVRQualifiers(); |
| 2520 | |
| 2521 | if ((ParamQuals | ArgQuals) != ParamQuals) { |
| 2522 | S.Diag(Arg->getSourceRange().getBegin(), |
| 2523 | diag::err_template_arg_ref_bind_ignores_quals) |
| 2524 | << ParamType << Arg->getType() |
| 2525 | << Arg->getSourceRange(); |
| 2526 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 2527 | return true; |
| 2528 | } |
| 2529 | } |
| 2530 | } |
| 2531 | |
| 2532 | // At this point, the template argument refers to an object or |
| 2533 | // function with external linkage. We now need to check whether the |
| 2534 | // argument and parameter types are compatible. |
| 2535 | if (!S.Context.hasSameUnqualifiedType(ArgType, |
| 2536 | ParamType.getNonReferenceType())) { |
| 2537 | // We can't perform this conversion or binding. |
| 2538 | if (ParamType->isReferenceType()) |
| 2539 | S.Diag(Arg->getLocStart(), diag::err_template_arg_no_ref_bind) |
| 2540 | << ParamType << Arg->getType() << Arg->getSourceRange(); |
| 2541 | else |
| 2542 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_convertible) |
| 2543 | << Arg->getType() << ParamType << Arg->getSourceRange(); |
| 2544 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 2545 | return true; |
| 2546 | } |
| 2547 | } |
| 2548 | |
| 2549 | // Create the template argument. |
| 2550 | Converted = TemplateArgument(Entity->getCanonicalDecl()); |
| 2551 | return false; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2552 | } |
| 2553 | |
| 2554 | /// \brief Checks whether the given template argument is a pointer to |
| 2555 | /// member constant according to C++ [temp.arg.nontype]p1. |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 2556 | bool Sema::CheckTemplateArgumentPointerToMember(Expr *Arg, |
| 2557 | TemplateArgument &Converted) { |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2558 | bool Invalid = false; |
| 2559 | |
| 2560 | // See through any implicit casts we added to fix the type. |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 2561 | while (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(Arg)) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2562 | Arg = Cast->getSubExpr(); |
| 2563 | |
| 2564 | // C++ [temp.arg.nontype]p1: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2565 | // |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2566 | // A template-argument for a non-type, non-template |
| 2567 | // template-parameter shall be one of: [...] |
| 2568 | // |
| 2569 | // -- a pointer to member expressed as described in 5.3.1. |
Douglas Gregor | a2813ce | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 2570 | DeclRefExpr *DRE = 0; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2571 | |
| 2572 | // Ignore (and complain about) any excess parentheses. |
| 2573 | while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) { |
| 2574 | if (!Invalid) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2575 | Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2576 | diag::err_template_arg_extra_parens) |
| 2577 | << Arg->getSourceRange(); |
| 2578 | Invalid = true; |
| 2579 | } |
| 2580 | |
| 2581 | Arg = Parens->getSubExpr(); |
| 2582 | } |
| 2583 | |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 2584 | // A pointer-to-member constant written &Class::member. |
| 2585 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) { |
Douglas Gregor | a2813ce | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 2586 | if (UnOp->getOpcode() == UnaryOperator::AddrOf) { |
| 2587 | DRE = dyn_cast<DeclRefExpr>(UnOp->getSubExpr()); |
| 2588 | if (DRE && !DRE->getQualifier()) |
| 2589 | DRE = 0; |
| 2590 | } |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 2591 | } |
| 2592 | // A constant of pointer-to-member type. |
| 2593 | else if ((DRE = dyn_cast<DeclRefExpr>(Arg))) { |
| 2594 | if (ValueDecl *VD = dyn_cast<ValueDecl>(DRE->getDecl())) { |
| 2595 | if (VD->getType()->isMemberPointerType()) { |
| 2596 | if (isa<NonTypeTemplateParmDecl>(VD) || |
| 2597 | (isa<VarDecl>(VD) && |
| 2598 | Context.getCanonicalType(VD->getType()).isConstQualified())) { |
| 2599 | if (Arg->isTypeDependent() || Arg->isValueDependent()) |
| 2600 | Converted = TemplateArgument(Arg->Retain()); |
| 2601 | else |
| 2602 | Converted = TemplateArgument(VD->getCanonicalDecl()); |
| 2603 | return Invalid; |
| 2604 | } |
| 2605 | } |
| 2606 | } |
| 2607 | |
| 2608 | DRE = 0; |
| 2609 | } |
| 2610 | |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2611 | if (!DRE) |
| 2612 | return Diag(Arg->getSourceRange().getBegin(), |
| 2613 | diag::err_template_arg_not_pointer_to_member_form) |
| 2614 | << Arg->getSourceRange(); |
| 2615 | |
| 2616 | if (isa<FieldDecl>(DRE->getDecl()) || isa<CXXMethodDecl>(DRE->getDecl())) { |
| 2617 | assert((isa<FieldDecl>(DRE->getDecl()) || |
| 2618 | !cast<CXXMethodDecl>(DRE->getDecl())->isStatic()) && |
| 2619 | "Only non-static member pointers can make it here"); |
| 2620 | |
| 2621 | // Okay: this is the address of a non-static member, and therefore |
| 2622 | // a member pointer constant. |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 2623 | if (Arg->isTypeDependent() || Arg->isValueDependent()) |
| 2624 | Converted = TemplateArgument(Arg->Retain()); |
| 2625 | else |
| 2626 | Converted = TemplateArgument(DRE->getDecl()->getCanonicalDecl()); |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2627 | return Invalid; |
| 2628 | } |
| 2629 | |
| 2630 | // 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] | 2631 | Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2632 | diag::err_template_arg_not_pointer_to_member_form) |
| 2633 | << Arg->getSourceRange(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2634 | Diag(DRE->getDecl()->getLocation(), |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2635 | diag::note_template_arg_refers_here); |
| 2636 | return true; |
| 2637 | } |
| 2638 | |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2639 | /// \brief Check a template argument against its corresponding |
| 2640 | /// non-type template parameter. |
| 2641 | /// |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 2642 | /// This routine implements the semantics of C++ [temp.arg.nontype]. |
| 2643 | /// It returns true if an error occurred, and false otherwise. \p |
| 2644 | /// InstantiatedParamType is the type of the non-type template |
| 2645 | /// parameter after it has been instantiated. |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 2646 | /// |
Douglas Gregor | 02cbbd2 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 2647 | /// If no error was detected, Converted receives the converted template argument. |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2648 | bool Sema::CheckTemplateArgument(NonTypeTemplateParmDecl *Param, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2649 | QualType InstantiatedParamType, Expr *&Arg, |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 2650 | TemplateArgument &Converted, |
| 2651 | CheckTemplateArgumentKind CTAK) { |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2652 | SourceLocation StartLoc = Arg->getSourceRange().getBegin(); |
| 2653 | |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 2654 | // If either the parameter has a dependent type or the argument is |
| 2655 | // type-dependent, there's nothing we can check now. |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2656 | if (InstantiatedParamType->isDependentType() || Arg->isTypeDependent()) { |
| 2657 | // FIXME: Produce a cloned, canonical expression? |
Douglas Gregor | 02cbbd2 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 2658 | Converted = TemplateArgument(Arg); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 2659 | return false; |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2660 | } |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 2661 | |
| 2662 | // C++ [temp.arg.nontype]p5: |
| 2663 | // The following conversions are performed on each expression used |
| 2664 | // as a non-type template-argument. If a non-type |
| 2665 | // template-argument cannot be converted to the type of the |
| 2666 | // corresponding template-parameter then the program is |
| 2667 | // ill-formed. |
| 2668 | // |
| 2669 | // -- for a non-type template-parameter of integral or |
| 2670 | // enumeration type, integral promotions (4.5) and integral |
| 2671 | // conversions (4.7) are applied. |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 2672 | QualType ParamType = InstantiatedParamType; |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 2673 | QualType ArgType = Arg->getType(); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 2674 | if (ParamType->isIntegralType() || ParamType->isEnumeralType()) { |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 2675 | // C++ [temp.arg.nontype]p1: |
| 2676 | // A template-argument for a non-type, non-template |
| 2677 | // template-parameter shall be one of: |
| 2678 | // |
| 2679 | // -- an integral constant-expression of integral or enumeration |
| 2680 | // type; or |
| 2681 | // -- the name of a non-type template-parameter; or |
| 2682 | SourceLocation NonConstantLoc; |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 2683 | llvm::APSInt Value; |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 2684 | if (!ArgType->isIntegralType() && !ArgType->isEnumeralType()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2685 | Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 2686 | diag::err_template_arg_not_integral_or_enumeral) |
| 2687 | << ArgType << Arg->getSourceRange(); |
| 2688 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 2689 | return true; |
| 2690 | } else if (!Arg->isValueDependent() && |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 2691 | !Arg->isIntegerConstantExpr(Value, Context, &NonConstantLoc)) { |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 2692 | Diag(NonConstantLoc, diag::err_template_arg_not_ice) |
| 2693 | << ArgType << Arg->getSourceRange(); |
| 2694 | return true; |
| 2695 | } |
| 2696 | |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 2697 | // From here on out, all we care about are the unqualified forms |
| 2698 | // of the parameter and argument types. |
| 2699 | ParamType = ParamType.getUnqualifiedType(); |
| 2700 | ArgType = ArgType.getUnqualifiedType(); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 2701 | |
| 2702 | // Try to convert the argument to the parameter's type. |
Douglas Gregor | ff52439 | 2009-11-04 21:50:46 +0000 | [diff] [blame] | 2703 | if (Context.hasSameType(ParamType, ArgType)) { |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 2704 | // Okay: no conversion necessary |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 2705 | } else if (CTAK == CTAK_Deduced) { |
| 2706 | // C++ [temp.deduct.type]p17: |
| 2707 | // If, in the declaration of a function template with a non-type |
| 2708 | // template-parameter, the non-type template- parameter is used |
| 2709 | // in an expression in the function parameter-list and, if the |
| 2710 | // corresponding template-argument is deduced, the |
| 2711 | // template-argument type shall match the type of the |
| 2712 | // template-parameter exactly, except that a template-argument |
| 2713 | // deduced from an array bound may be of any integral type. |
| 2714 | Diag(StartLoc, diag::err_deduced_non_type_template_arg_type_mismatch) |
| 2715 | << ArgType << ParamType; |
| 2716 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 2717 | return true; |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 2718 | } else if (IsIntegralPromotion(Arg, ArgType, ParamType) || |
| 2719 | !ParamType->isEnumeralType()) { |
| 2720 | // This is an integral promotion or conversion. |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 2721 | ImpCastExprToType(Arg, ParamType, CastExpr::CK_IntegralCast); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 2722 | } else { |
| 2723 | // We can't perform this conversion. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2724 | Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 2725 | diag::err_template_arg_not_convertible) |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 2726 | << Arg->getType() << InstantiatedParamType << Arg->getSourceRange(); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 2727 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 2728 | return true; |
| 2729 | } |
| 2730 | |
Douglas Gregor | f80a9d5 | 2009-03-14 00:20:21 +0000 | [diff] [blame] | 2731 | QualType IntegerType = Context.getCanonicalType(ParamType); |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2732 | if (const EnumType *Enum = IntegerType->getAs<EnumType>()) |
Douglas Gregor | 02cbbd2 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 2733 | IntegerType = Context.getCanonicalType(Enum->getDecl()->getIntegerType()); |
Douglas Gregor | f80a9d5 | 2009-03-14 00:20:21 +0000 | [diff] [blame] | 2734 | |
| 2735 | if (!Arg->isValueDependent()) { |
Douglas Gregor | 1a6e034 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 2736 | llvm::APSInt OldValue = Value; |
| 2737 | |
| 2738 | // Coerce the template argument's value to the value it will have |
| 2739 | // based on the template parameter's type. |
Douglas Gregor | 0d4fd8e | 2010-03-26 00:39:40 +0000 | [diff] [blame] | 2740 | unsigned AllowedBits = Context.getTypeSize(IntegerType); |
Douglas Gregor | 0d4fd8e | 2010-03-26 00:39:40 +0000 | [diff] [blame] | 2741 | if (Value.getBitWidth() != AllowedBits) |
| 2742 | Value.extOrTrunc(AllowedBits); |
| 2743 | Value.setIsSigned(IntegerType->isSignedIntegerType()); |
Douglas Gregor | 1a6e034 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 2744 | |
| 2745 | // Complain if an unsigned parameter received a negative value. |
| 2746 | if (IntegerType->isUnsignedIntegerType() |
| 2747 | && (OldValue.isSigned() && OldValue.isNegative())) { |
| 2748 | Diag(Arg->getSourceRange().getBegin(), diag::warn_template_arg_negative) |
| 2749 | << OldValue.toString(10) << Value.toString(10) << Param->getType() |
| 2750 | << Arg->getSourceRange(); |
| 2751 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 2752 | } |
| 2753 | |
| 2754 | // Complain if we overflowed the template parameter's type. |
| 2755 | unsigned RequiredBits; |
| 2756 | if (IntegerType->isUnsignedIntegerType()) |
| 2757 | RequiredBits = OldValue.getActiveBits(); |
| 2758 | else if (OldValue.isUnsigned()) |
| 2759 | RequiredBits = OldValue.getActiveBits() + 1; |
| 2760 | else |
| 2761 | RequiredBits = OldValue.getMinSignedBits(); |
| 2762 | if (RequiredBits > AllowedBits) { |
| 2763 | Diag(Arg->getSourceRange().getBegin(), |
| 2764 | diag::warn_template_arg_too_large) |
| 2765 | << OldValue.toString(10) << Value.toString(10) << Param->getType() |
| 2766 | << Arg->getSourceRange(); |
| 2767 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 2768 | } |
Douglas Gregor | f80a9d5 | 2009-03-14 00:20:21 +0000 | [diff] [blame] | 2769 | } |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 2770 | |
Douglas Gregor | 02cbbd2 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 2771 | // Add the value of this argument to the list of converted |
| 2772 | // arguments. We use the bitwidth and signedness of the template |
| 2773 | // parameter. |
| 2774 | if (Arg->isValueDependent()) { |
| 2775 | // The argument is value-dependent. Create a new |
| 2776 | // TemplateArgument with the converted expression. |
| 2777 | Converted = TemplateArgument(Arg); |
| 2778 | return false; |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 2779 | } |
| 2780 | |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2781 | Converted = TemplateArgument(Value, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2782 | ParamType->isEnumeralType() ? ParamType |
Douglas Gregor | 02cbbd2 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 2783 | : IntegerType); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 2784 | return false; |
| 2785 | } |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 2786 | |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2787 | DeclAccessPair FoundResult; // temporary for ResolveOverloadedFunction |
| 2788 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2789 | // C++0x [temp.arg.nontype]p5 bullets 2, 4 and 6 permit conversion |
| 2790 | // from a template argument of type std::nullptr_t to a non-type |
| 2791 | // template parameter of type pointer to object, pointer to |
| 2792 | // function, or pointer-to-member, respectively. |
| 2793 | if (ArgType->isNullPtrType() && |
| 2794 | (ParamType->isPointerType() || ParamType->isMemberPointerType())) { |
| 2795 | Converted = TemplateArgument((NamedDecl *)0); |
| 2796 | return false; |
| 2797 | } |
| 2798 | |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 2799 | // Handle pointer-to-function, reference-to-function, and |
| 2800 | // pointer-to-member-function all in (roughly) the same way. |
| 2801 | if (// -- For a non-type template-parameter of type pointer to |
| 2802 | // function, only the function-to-pointer conversion (4.3) is |
| 2803 | // applied. If the template-argument represents a set of |
| 2804 | // overloaded functions (or a pointer to such), the matching |
| 2805 | // function is selected from the set (13.4). |
| 2806 | (ParamType->isPointerType() && |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2807 | ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType()) || |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 2808 | // -- For a non-type template-parameter of type reference to |
| 2809 | // function, no conversions apply. If the template-argument |
| 2810 | // represents a set of overloaded functions, the matching |
| 2811 | // function is selected from the set (13.4). |
| 2812 | (ParamType->isReferenceType() && |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2813 | ParamType->getAs<ReferenceType>()->getPointeeType()->isFunctionType()) || |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 2814 | // -- For a non-type template-parameter of type pointer to |
| 2815 | // member function, no conversions apply. If the |
| 2816 | // template-argument represents a set of overloaded member |
| 2817 | // functions, the matching member function is selected from |
| 2818 | // the set (13.4). |
| 2819 | (ParamType->isMemberPointerType() && |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2820 | ParamType->getAs<MemberPointerType>()->getPointeeType() |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 2821 | ->isFunctionType())) { |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2822 | |
| 2823 | if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg, ParamType, |
| 2824 | true, |
| 2825 | FoundResult)) { |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 2826 | if (DiagnoseUseOfDecl(Fn, Arg->getSourceRange().getBegin())) |
| 2827 | return true; |
| 2828 | |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2829 | Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn); |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 2830 | ArgType = Arg->getType(); |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 2831 | } |
| 2832 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2833 | if (!ParamType->isMemberPointerType()) |
| 2834 | return CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param, |
| 2835 | ParamType, |
| 2836 | Arg, Converted); |
| 2837 | |
| 2838 | if (IsQualificationConversion(ArgType, ParamType.getNonReferenceType())) { |
| 2839 | ImpCastExprToType(Arg, ParamType, CastExpr::CK_NoOp, |
| 2840 | Arg->isLvalue(Context) == Expr::LV_Valid); |
| 2841 | } else if (!Context.hasSameUnqualifiedType(ArgType, |
| 2842 | ParamType.getNonReferenceType())) { |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 2843 | // We can't perform this conversion. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2844 | Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 2845 | diag::err_template_arg_not_convertible) |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 2846 | << Arg->getType() << InstantiatedParamType << Arg->getSourceRange(); |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 2847 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 2848 | return true; |
| 2849 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2850 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2851 | return CheckTemplateArgumentPointerToMember(Arg, Converted); |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 2852 | } |
| 2853 | |
Chris Lattner | fe90de7 | 2009-02-20 21:37:53 +0000 | [diff] [blame] | 2854 | if (ParamType->isPointerType()) { |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 2855 | // -- for a non-type template-parameter of type pointer to |
| 2856 | // object, qualification conversions (4.4) and the |
| 2857 | // array-to-pointer conversion (4.2) are applied. |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 2858 | // C++0x also allows a value of std::nullptr_t. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2859 | assert(ParamType->getAs<PointerType>()->getPointeeType()->isObjectType() && |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 2860 | "Only object pointers allowed here"); |
Douglas Gregor | f684e6e | 2009-02-11 00:44:29 +0000 | [diff] [blame] | 2861 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2862 | return CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param, |
| 2863 | ParamType, |
| 2864 | Arg, Converted); |
Douglas Gregor | f684e6e | 2009-02-11 00:44:29 +0000 | [diff] [blame] | 2865 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2866 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2867 | if (const ReferenceType *ParamRefType = ParamType->getAs<ReferenceType>()) { |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 2868 | // -- For a non-type template-parameter of type reference to |
| 2869 | // object, no conversions apply. The type referred to by the |
| 2870 | // reference may be more cv-qualified than the (otherwise |
| 2871 | // identical) type of the template-argument. The |
| 2872 | // template-parameter is bound directly to the |
| 2873 | // template-argument, which must be an lvalue. |
Douglas Gregor | bad0e65 | 2009-03-24 20:32:41 +0000 | [diff] [blame] | 2874 | assert(ParamRefType->getPointeeType()->isObjectType() && |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 2875 | "Only object references allowed here"); |
Douglas Gregor | f684e6e | 2009-02-11 00:44:29 +0000 | [diff] [blame] | 2876 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2877 | if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg, |
| 2878 | ParamRefType->getPointeeType(), |
| 2879 | true, |
| 2880 | FoundResult)) { |
| 2881 | if (DiagnoseUseOfDecl(Fn, Arg->getSourceRange().getBegin())) |
| 2882 | return true; |
| 2883 | |
| 2884 | Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn); |
| 2885 | ArgType = Arg->getType(); |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 2886 | } |
| 2887 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2888 | return CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param, |
| 2889 | ParamType, |
| 2890 | Arg, Converted); |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 2891 | } |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 2892 | |
| 2893 | // -- For a non-type template-parameter of type pointer to data |
| 2894 | // member, qualification conversions (4.4) are applied. |
| 2895 | assert(ParamType->isMemberPointerType() && "Only pointers to members remain"); |
| 2896 | |
Douglas Gregor | 8e6563b | 2009-02-11 18:22:40 +0000 | [diff] [blame] | 2897 | if (Context.hasSameUnqualifiedType(ParamType, ArgType)) { |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 2898 | // Types match exactly: nothing more to do here. |
| 2899 | } else if (IsQualificationConversion(ArgType, ParamType)) { |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2900 | ImpCastExprToType(Arg, ParamType, CastExpr::CK_NoOp, |
| 2901 | Arg->isLvalue(Context) == Expr::LV_Valid); |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 2902 | } else { |
| 2903 | // We can't perform this conversion. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2904 | Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 2905 | diag::err_template_arg_not_convertible) |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 2906 | << Arg->getType() << InstantiatedParamType << Arg->getSourceRange(); |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 2907 | Diag(Param->getLocation(), diag::note_template_param_here); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2908 | return true; |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 2909 | } |
| 2910 | |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 2911 | return CheckTemplateArgumentPointerToMember(Arg, Converted); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2912 | } |
| 2913 | |
| 2914 | /// \brief Check a template argument against its corresponding |
| 2915 | /// template template parameter. |
| 2916 | /// |
| 2917 | /// This routine implements the semantics of C++ [temp.arg.template]. |
| 2918 | /// It returns true if an error occurred, and false otherwise. |
| 2919 | bool Sema::CheckTemplateArgument(TemplateTemplateParmDecl *Param, |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2920 | const TemplateArgumentLoc &Arg) { |
| 2921 | TemplateName Name = Arg.getArgument().getAsTemplate(); |
| 2922 | TemplateDecl *Template = Name.getAsTemplateDecl(); |
| 2923 | if (!Template) { |
| 2924 | // Any dependent template name is fine. |
| 2925 | assert(Name.isDependent() && "Non-dependent template isn't a declaration?"); |
| 2926 | return false; |
| 2927 | } |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 2928 | |
| 2929 | // C++ [temp.arg.template]p1: |
| 2930 | // A template-argument for a template template-parameter shall be |
| 2931 | // the name of a class template, expressed as id-expression. Only |
| 2932 | // primary class templates are considered when matching the |
| 2933 | // template template argument with the corresponding parameter; |
| 2934 | // partial specializations are not considered even if their |
| 2935 | // parameter lists match that of the template template parameter. |
Douglas Gregor | ba1ecb5 | 2009-06-12 19:43:02 +0000 | [diff] [blame] | 2936 | // |
| 2937 | // Note that we also allow template template parameters here, which |
| 2938 | // will happen when we are dealing with, e.g., class template |
| 2939 | // partial specializations. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2940 | if (!isa<ClassTemplateDecl>(Template) && |
Douglas Gregor | ba1ecb5 | 2009-06-12 19:43:02 +0000 | [diff] [blame] | 2941 | !isa<TemplateTemplateParmDecl>(Template)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2942 | assert(isa<FunctionTemplateDecl>(Template) && |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 2943 | "Only function templates are possible here"); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2944 | Diag(Arg.getLocation(), diag::err_template_arg_not_class_template); |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 2945 | Diag(Template->getLocation(), diag::note_template_arg_refers_here_func) |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 2946 | << Template; |
| 2947 | } |
| 2948 | |
| 2949 | return !TemplateParameterListsAreEqual(Template->getTemplateParameters(), |
| 2950 | Param->getTemplateParameters(), |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 2951 | true, |
| 2952 | TPL_TemplateTemplateArgumentMatch, |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2953 | Arg.getLocation()); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2954 | } |
| 2955 | |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 2956 | /// \brief Given a non-type template argument that refers to a |
| 2957 | /// declaration and the type of its corresponding non-type template |
| 2958 | /// parameter, produce an expression that properly refers to that |
| 2959 | /// declaration. |
| 2960 | Sema::OwningExprResult |
| 2961 | Sema::BuildExpressionFromDeclTemplateArgument(const TemplateArgument &Arg, |
| 2962 | QualType ParamType, |
| 2963 | SourceLocation Loc) { |
| 2964 | assert(Arg.getKind() == TemplateArgument::Declaration && |
| 2965 | "Only declaration template arguments permitted here"); |
| 2966 | ValueDecl *VD = cast<ValueDecl>(Arg.getAsDecl()); |
| 2967 | |
| 2968 | if (VD->getDeclContext()->isRecord() && |
| 2969 | (isa<CXXMethodDecl>(VD) || isa<FieldDecl>(VD))) { |
| 2970 | // If the value is a class member, we might have a pointer-to-member. |
| 2971 | // Determine whether the non-type template template parameter is of |
| 2972 | // pointer-to-member type. If so, we need to build an appropriate |
| 2973 | // expression for a pointer-to-member, since a "normal" DeclRefExpr |
| 2974 | // would refer to the member itself. |
| 2975 | if (ParamType->isMemberPointerType()) { |
| 2976 | QualType ClassType |
| 2977 | = Context.getTypeDeclType(cast<RecordDecl>(VD->getDeclContext())); |
| 2978 | NestedNameSpecifier *Qualifier |
| 2979 | = NestedNameSpecifier::Create(Context, 0, false, ClassType.getTypePtr()); |
| 2980 | CXXScopeSpec SS; |
| 2981 | SS.setScopeRep(Qualifier); |
| 2982 | OwningExprResult RefExpr = BuildDeclRefExpr(VD, |
| 2983 | VD->getType().getNonReferenceType(), |
| 2984 | Loc, |
| 2985 | &SS); |
| 2986 | if (RefExpr.isInvalid()) |
| 2987 | return ExprError(); |
| 2988 | |
| 2989 | RefExpr = CreateBuiltinUnaryOp(Loc, UnaryOperator::AddrOf, move(RefExpr)); |
| 2990 | assert(!RefExpr.isInvalid() && |
| 2991 | Context.hasSameType(((Expr*) RefExpr.get())->getType(), |
| 2992 | ParamType)); |
| 2993 | return move(RefExpr); |
| 2994 | } |
| 2995 | } |
| 2996 | |
| 2997 | QualType T = VD->getType().getNonReferenceType(); |
| 2998 | if (ParamType->isPointerType()) { |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2999 | // When the non-type template parameter is a pointer, take the |
| 3000 | // address of the declaration. |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3001 | OwningExprResult RefExpr = BuildDeclRefExpr(VD, T, Loc); |
| 3002 | if (RefExpr.isInvalid()) |
| 3003 | return ExprError(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3004 | |
| 3005 | if (T->isFunctionType() || T->isArrayType()) { |
| 3006 | // Decay functions and arrays. |
| 3007 | Expr *RefE = (Expr *)RefExpr.get(); |
| 3008 | DefaultFunctionArrayConversion(RefE); |
| 3009 | if (RefE != RefExpr.get()) { |
| 3010 | RefExpr.release(); |
| 3011 | RefExpr = Owned(RefE); |
| 3012 | } |
| 3013 | |
| 3014 | return move(RefExpr); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3015 | } |
| 3016 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3017 | // Take the address of everything else |
| 3018 | return CreateBuiltinUnaryOp(Loc, UnaryOperator::AddrOf, move(RefExpr)); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3019 | } |
| 3020 | |
| 3021 | // If the non-type template parameter has reference type, qualify the |
| 3022 | // resulting declaration reference with the extra qualifiers on the |
| 3023 | // type that the reference refers to. |
| 3024 | if (const ReferenceType *TargetRef = ParamType->getAs<ReferenceType>()) |
| 3025 | T = Context.getQualifiedType(T, TargetRef->getPointeeType().getQualifiers()); |
| 3026 | |
| 3027 | return BuildDeclRefExpr(VD, T, Loc); |
| 3028 | } |
| 3029 | |
| 3030 | /// \brief Construct a new expression that refers to the given |
| 3031 | /// integral template argument with the given source-location |
| 3032 | /// information. |
| 3033 | /// |
| 3034 | /// This routine takes care of the mapping from an integral template |
| 3035 | /// argument (which may have any integral type) to the appropriate |
| 3036 | /// literal value. |
| 3037 | Sema::OwningExprResult |
| 3038 | Sema::BuildExpressionFromIntegralTemplateArgument(const TemplateArgument &Arg, |
| 3039 | SourceLocation Loc) { |
| 3040 | assert(Arg.getKind() == TemplateArgument::Integral && |
| 3041 | "Operation is only value for integral template arguments"); |
| 3042 | QualType T = Arg.getIntegralType(); |
| 3043 | if (T->isCharType() || T->isWideCharType()) |
| 3044 | return Owned(new (Context) CharacterLiteral( |
| 3045 | Arg.getAsIntegral()->getZExtValue(), |
| 3046 | T->isWideCharType(), |
| 3047 | T, |
| 3048 | Loc)); |
| 3049 | if (T->isBooleanType()) |
| 3050 | return Owned(new (Context) CXXBoolLiteralExpr( |
| 3051 | Arg.getAsIntegral()->getBoolValue(), |
| 3052 | T, |
| 3053 | Loc)); |
| 3054 | |
| 3055 | return Owned(new (Context) IntegerLiteral(*Arg.getAsIntegral(), T, Loc)); |
| 3056 | } |
| 3057 | |
| 3058 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3059 | /// \brief Determine whether the given template parameter lists are |
| 3060 | /// equivalent. |
| 3061 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3062 | /// \param New The new template parameter list, typically written in the |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3063 | /// source code as part of a new template declaration. |
| 3064 | /// |
| 3065 | /// \param Old The old template parameter list, typically found via |
| 3066 | /// name lookup of the template declared with this template parameter |
| 3067 | /// list. |
| 3068 | /// |
| 3069 | /// \param Complain If true, this routine will produce a diagnostic if |
| 3070 | /// the template parameter lists are not equivalent. |
| 3071 | /// |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 3072 | /// \param Kind describes how we are to match the template parameter lists. |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 3073 | /// |
| 3074 | /// \param TemplateArgLoc If this source location is valid, then we |
| 3075 | /// are actually checking the template parameter list of a template |
| 3076 | /// argument (New) against the template parameter list of its |
| 3077 | /// corresponding template template parameter (Old). We produce |
| 3078 | /// slightly different diagnostics in this scenario. |
| 3079 | /// |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3080 | /// \returns True if the template parameter lists are equal, false |
| 3081 | /// otherwise. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3082 | bool |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3083 | Sema::TemplateParameterListsAreEqual(TemplateParameterList *New, |
| 3084 | TemplateParameterList *Old, |
| 3085 | bool Complain, |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 3086 | TemplateParameterListEqualKind Kind, |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 3087 | SourceLocation TemplateArgLoc) { |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3088 | if (Old->size() != New->size()) { |
| 3089 | if (Complain) { |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 3090 | unsigned NextDiag = diag::err_template_param_list_different_arity; |
| 3091 | if (TemplateArgLoc.isValid()) { |
| 3092 | Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch); |
| 3093 | NextDiag = diag::note_template_param_list_different_arity; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3094 | } |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 3095 | Diag(New->getTemplateLoc(), NextDiag) |
| 3096 | << (New->size() > Old->size()) |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 3097 | << (Kind != TPL_TemplateMatch) |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 3098 | << SourceRange(New->getTemplateLoc(), New->getRAngleLoc()); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3099 | Diag(Old->getTemplateLoc(), diag::note_template_prev_declaration) |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 3100 | << (Kind != TPL_TemplateMatch) |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3101 | << SourceRange(Old->getTemplateLoc(), Old->getRAngleLoc()); |
| 3102 | } |
| 3103 | |
| 3104 | return false; |
| 3105 | } |
| 3106 | |
| 3107 | for (TemplateParameterList::iterator OldParm = Old->begin(), |
| 3108 | OldParmEnd = Old->end(), NewParm = New->begin(); |
| 3109 | OldParm != OldParmEnd; ++OldParm, ++NewParm) { |
| 3110 | if ((*OldParm)->getKind() != (*NewParm)->getKind()) { |
Douglas Gregor | 34d1dc9 | 2009-06-24 16:50:40 +0000 | [diff] [blame] | 3111 | if (Complain) { |
| 3112 | unsigned NextDiag = diag::err_template_param_different_kind; |
| 3113 | if (TemplateArgLoc.isValid()) { |
| 3114 | Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch); |
| 3115 | NextDiag = diag::note_template_param_different_kind; |
| 3116 | } |
| 3117 | Diag((*NewParm)->getLocation(), NextDiag) |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 3118 | << (Kind != TPL_TemplateMatch); |
Douglas Gregor | 34d1dc9 | 2009-06-24 16:50:40 +0000 | [diff] [blame] | 3119 | Diag((*OldParm)->getLocation(), diag::note_template_prev_declaration) |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 3120 | << (Kind != TPL_TemplateMatch); |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 3121 | } |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3122 | return false; |
| 3123 | } |
| 3124 | |
| 3125 | if (isa<TemplateTypeParmDecl>(*OldParm)) { |
| 3126 | // Okay; all template type parameters are equivalent (since we |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 3127 | // know we're at the same index). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3128 | } else if (NonTypeTemplateParmDecl *OldNTTP |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3129 | = dyn_cast<NonTypeTemplateParmDecl>(*OldParm)) { |
| 3130 | // The types of non-type template parameters must agree. |
| 3131 | NonTypeTemplateParmDecl *NewNTTP |
| 3132 | = cast<NonTypeTemplateParmDecl>(*NewParm); |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 3133 | |
| 3134 | // If we are matching a template template argument to a template |
| 3135 | // template parameter and one of the non-type template parameter types |
| 3136 | // is dependent, then we must wait until template instantiation time |
| 3137 | // to actually compare the arguments. |
| 3138 | if (Kind == TPL_TemplateTemplateArgumentMatch && |
| 3139 | (OldNTTP->getType()->isDependentType() || |
| 3140 | NewNTTP->getType()->isDependentType())) |
| 3141 | continue; |
| 3142 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3143 | if (Context.getCanonicalType(OldNTTP->getType()) != |
| 3144 | Context.getCanonicalType(NewNTTP->getType())) { |
| 3145 | if (Complain) { |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 3146 | unsigned NextDiag = diag::err_template_nontype_parm_different_type; |
| 3147 | if (TemplateArgLoc.isValid()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3148 | Diag(TemplateArgLoc, |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 3149 | diag::err_template_arg_template_params_mismatch); |
| 3150 | NextDiag = diag::note_template_nontype_parm_different_type; |
| 3151 | } |
| 3152 | Diag(NewNTTP->getLocation(), NextDiag) |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3153 | << NewNTTP->getType() |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 3154 | << (Kind != TPL_TemplateMatch); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3155 | Diag(OldNTTP->getLocation(), |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3156 | diag::note_template_nontype_parm_prev_declaration) |
| 3157 | << OldNTTP->getType(); |
| 3158 | } |
| 3159 | return false; |
| 3160 | } |
| 3161 | } else { |
| 3162 | // The template parameter lists of template template |
| 3163 | // parameters must agree. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3164 | assert(isa<TemplateTemplateParmDecl>(*OldParm) && |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3165 | "Only template template parameters handled here"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3166 | TemplateTemplateParmDecl *OldTTP |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3167 | = cast<TemplateTemplateParmDecl>(*OldParm); |
| 3168 | TemplateTemplateParmDecl *NewTTP |
| 3169 | = cast<TemplateTemplateParmDecl>(*NewParm); |
| 3170 | if (!TemplateParameterListsAreEqual(NewTTP->getTemplateParameters(), |
| 3171 | OldTTP->getTemplateParameters(), |
| 3172 | Complain, |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 3173 | (Kind == TPL_TemplateMatch? TPL_TemplateTemplateParmMatch : Kind), |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 3174 | TemplateArgLoc)) |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3175 | return false; |
| 3176 | } |
| 3177 | } |
| 3178 | |
| 3179 | return true; |
| 3180 | } |
| 3181 | |
| 3182 | /// \brief Check whether a template can be declared within this scope. |
| 3183 | /// |
| 3184 | /// If the template declaration is valid in this scope, returns |
| 3185 | /// false. Otherwise, issues a diagnostic and returns true. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3186 | bool |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 3187 | Sema::CheckTemplateDeclScope(Scope *S, TemplateParameterList *TemplateParams) { |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3188 | // Find the nearest enclosing declaration scope. |
| 3189 | while ((S->getFlags() & Scope::DeclScope) == 0 || |
| 3190 | (S->getFlags() & Scope::TemplateParamScope) != 0) |
| 3191 | S = S->getParent(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3192 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3193 | // C++ [temp]p2: |
| 3194 | // A template-declaration can appear only as a namespace scope or |
| 3195 | // class scope declaration. |
| 3196 | DeclContext *Ctx = static_cast<DeclContext *>(S->getEntity()); |
Eli Friedman | 1503f77 | 2009-07-31 01:43:05 +0000 | [diff] [blame] | 3197 | if (Ctx && isa<LinkageSpecDecl>(Ctx) && |
| 3198 | cast<LinkageSpecDecl>(Ctx)->getLanguage() != LinkageSpecDecl::lang_cxx) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3199 | return Diag(TemplateParams->getTemplateLoc(), diag::err_template_linkage) |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 3200 | << TemplateParams->getSourceRange(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3201 | |
Eli Friedman | 1503f77 | 2009-07-31 01:43:05 +0000 | [diff] [blame] | 3202 | while (Ctx && isa<LinkageSpecDecl>(Ctx)) |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3203 | Ctx = Ctx->getParent(); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3204 | |
| 3205 | if (Ctx && (Ctx->isFileContext() || Ctx->isRecord())) |
| 3206 | return false; |
| 3207 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3208 | return Diag(TemplateParams->getTemplateLoc(), |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 3209 | diag::err_template_outside_namespace_or_class_scope) |
| 3210 | << TemplateParams->getSourceRange(); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3211 | } |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3212 | |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3213 | /// \brief Determine what kind of template specialization the given declaration |
| 3214 | /// is. |
| 3215 | static TemplateSpecializationKind getTemplateSpecializationKind(NamedDecl *D) { |
| 3216 | if (!D) |
| 3217 | return TSK_Undeclared; |
| 3218 | |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 3219 | if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) |
| 3220 | return Record->getTemplateSpecializationKind(); |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3221 | if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) |
| 3222 | return Function->getTemplateSpecializationKind(); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 3223 | if (VarDecl *Var = dyn_cast<VarDecl>(D)) |
| 3224 | return Var->getTemplateSpecializationKind(); |
| 3225 | |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3226 | return TSK_Undeclared; |
| 3227 | } |
| 3228 | |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 3229 | /// \brief Check whether a specialization is well-formed in the current |
| 3230 | /// context. |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 3231 | /// |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 3232 | /// This routine determines whether a template specialization can be declared |
| 3233 | /// in the current context (C++ [temp.expl.spec]p2). |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3234 | /// |
| 3235 | /// \param S the semantic analysis object for which this check is being |
| 3236 | /// performed. |
| 3237 | /// |
| 3238 | /// \param Specialized the entity being specialized or instantiated, which |
| 3239 | /// may be a kind of template (class template, function template, etc.) or |
| 3240 | /// a member of a class template (member function, static data member, |
| 3241 | /// member class). |
| 3242 | /// |
| 3243 | /// \param PrevDecl the previous declaration of this entity, if any. |
| 3244 | /// |
| 3245 | /// \param Loc the location of the explicit specialization or instantiation of |
| 3246 | /// this entity. |
| 3247 | /// |
| 3248 | /// \param IsPartialSpecialization whether this is a partial specialization of |
| 3249 | /// a class template. |
| 3250 | /// |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3251 | /// \returns true if there was an error that we cannot recover from, false |
| 3252 | /// otherwise. |
| 3253 | static bool CheckTemplateSpecializationScope(Sema &S, |
| 3254 | NamedDecl *Specialized, |
| 3255 | NamedDecl *PrevDecl, |
| 3256 | SourceLocation Loc, |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 3257 | bool IsPartialSpecialization) { |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3258 | // Keep these "kind" numbers in sync with the %select statements in the |
| 3259 | // various diagnostics emitted by this routine. |
| 3260 | int EntityKind = 0; |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 3261 | bool isTemplateSpecialization = false; |
| 3262 | if (isa<ClassTemplateDecl>(Specialized)) { |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3263 | EntityKind = IsPartialSpecialization? 1 : 0; |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 3264 | isTemplateSpecialization = true; |
| 3265 | } else if (isa<FunctionTemplateDecl>(Specialized)) { |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3266 | EntityKind = 2; |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 3267 | isTemplateSpecialization = true; |
| 3268 | } else if (isa<CXXMethodDecl>(Specialized)) |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3269 | EntityKind = 3; |
| 3270 | else if (isa<VarDecl>(Specialized)) |
| 3271 | EntityKind = 4; |
| 3272 | else if (isa<RecordDecl>(Specialized)) |
| 3273 | EntityKind = 5; |
| 3274 | else { |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 3275 | S.Diag(Loc, diag::err_template_spec_unknown_kind); |
| 3276 | S.Diag(Specialized->getLocation(), diag::note_specialized_entity); |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3277 | return true; |
| 3278 | } |
| 3279 | |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 3280 | // C++ [temp.expl.spec]p2: |
| 3281 | // An explicit specialization shall be declared in the namespace |
| 3282 | // of which the template is a member, or, for member templates, in |
| 3283 | // the namespace of which the enclosing class or enclosing class |
| 3284 | // template is a member. An explicit specialization of a member |
| 3285 | // function, member class or static data member of a class |
| 3286 | // template shall be declared in the namespace of which the class |
| 3287 | // template is a member. Such a declaration may also be a |
| 3288 | // definition. If the declaration is not a definition, the |
| 3289 | // specialization may be defined later in the name- space in which |
| 3290 | // the explicit specialization was declared, or in a namespace |
| 3291 | // that encloses the one in which the explicit specialization was |
| 3292 | // declared. |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3293 | if (S.CurContext->getLookupContext()->isFunctionOrMethod()) { |
| 3294 | S.Diag(Loc, diag::err_template_spec_decl_function_scope) |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 3295 | << Specialized; |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 3296 | return true; |
| 3297 | } |
Douglas Gregor | 7974c3b | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 3298 | |
Douglas Gregor | 0a40747 | 2009-10-07 17:30:37 +0000 | [diff] [blame] | 3299 | if (S.CurContext->isRecord() && !IsPartialSpecialization) { |
| 3300 | S.Diag(Loc, diag::err_template_spec_decl_class_scope) |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 3301 | << Specialized; |
Douglas Gregor | 0a40747 | 2009-10-07 17:30:37 +0000 | [diff] [blame] | 3302 | return true; |
| 3303 | } |
| 3304 | |
Douglas Gregor | 7974c3b | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 3305 | // C++ [temp.class.spec]p6: |
| 3306 | // A class template partial specialization may be declared or redeclared |
| 3307 | // in any namespace scope in which its definition may be defined (14.5.1 |
| 3308 | // and 14.5.2). |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3309 | bool ComplainedAboutScope = false; |
Douglas Gregor | 7974c3b | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 3310 | DeclContext *SpecializedContext |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3311 | = Specialized->getDeclContext()->getEnclosingNamespaceContext(); |
Douglas Gregor | 7974c3b | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 3312 | DeclContext *DC = S.CurContext->getEnclosingNamespaceContext(); |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 3313 | if ((!PrevDecl || |
| 3314 | getTemplateSpecializationKind(PrevDecl) == TSK_Undeclared || |
| 3315 | getTemplateSpecializationKind(PrevDecl) == TSK_ImplicitInstantiation)){ |
| 3316 | // There is no prior declaration of this entity, so this |
| 3317 | // specialization must be in the same context as the template |
| 3318 | // itself. |
| 3319 | if (!DC->Equals(SpecializedContext)) { |
| 3320 | if (isa<TranslationUnitDecl>(SpecializedContext)) |
| 3321 | S.Diag(Loc, diag::err_template_spec_decl_out_of_scope_global) |
| 3322 | << EntityKind << Specialized; |
| 3323 | else if (isa<NamespaceDecl>(SpecializedContext)) |
| 3324 | S.Diag(Loc, diag::err_template_spec_decl_out_of_scope) |
| 3325 | << EntityKind << Specialized |
| 3326 | << cast<NamedDecl>(SpecializedContext); |
| 3327 | |
| 3328 | S.Diag(Specialized->getLocation(), diag::note_specialized_entity); |
| 3329 | ComplainedAboutScope = true; |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 3330 | } |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 3331 | } |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3332 | |
| 3333 | // Make sure that this redeclaration (or definition) occurs in an enclosing |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 3334 | // namespace. |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3335 | // Note that HandleDeclarator() performs this check for explicit |
| 3336 | // specializations of function templates, static data members, and member |
| 3337 | // functions, so we skip the check here for those kinds of entities. |
| 3338 | // FIXME: HandleDeclarator's diagnostics aren't quite as good, though. |
Douglas Gregor | 7974c3b | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 3339 | // Should we refactor that check, so that it occurs later? |
| 3340 | if (!ComplainedAboutScope && !DC->Encloses(SpecializedContext) && |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 3341 | !(isa<FunctionTemplateDecl>(Specialized) || isa<VarDecl>(Specialized) || |
| 3342 | isa<FunctionDecl>(Specialized))) { |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3343 | if (isa<TranslationUnitDecl>(SpecializedContext)) |
| 3344 | S.Diag(Loc, diag::err_template_spec_redecl_global_scope) |
| 3345 | << EntityKind << Specialized; |
| 3346 | else if (isa<NamespaceDecl>(SpecializedContext)) |
| 3347 | S.Diag(Loc, diag::err_template_spec_redecl_out_of_scope) |
| 3348 | << EntityKind << Specialized |
| 3349 | << cast<NamedDecl>(SpecializedContext); |
| 3350 | |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 3351 | S.Diag(Specialized->getLocation(), diag::note_specialized_entity); |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 3352 | } |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3353 | |
| 3354 | // FIXME: check for specialization-after-instantiation errors and such. |
| 3355 | |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 3356 | return false; |
| 3357 | } |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3358 | |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 3359 | /// \brief Check the non-type template arguments of a class template |
| 3360 | /// partial specialization according to C++ [temp.class.spec]p9. |
| 3361 | /// |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3362 | /// \param TemplateParams the template parameters of the primary class |
| 3363 | /// template. |
| 3364 | /// |
| 3365 | /// \param TemplateArg the template arguments of the class template |
| 3366 | /// partial specialization. |
| 3367 | /// |
| 3368 | /// \param MirrorsPrimaryTemplate will be set true if the class |
| 3369 | /// template partial specialization arguments are identical to the |
| 3370 | /// implicit template arguments of the primary template. This is not |
| 3371 | /// necessarily an error (C++0x), and it is left to the caller to diagnose |
| 3372 | /// this condition when it is an error. |
| 3373 | /// |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 3374 | /// \returns true if there was an error, false otherwise. |
| 3375 | bool Sema::CheckClassTemplatePartialSpecializationArgs( |
| 3376 | TemplateParameterList *TemplateParams, |
Anders Carlsson | 6360be7 | 2009-06-13 18:20:51 +0000 | [diff] [blame] | 3377 | const TemplateArgumentListBuilder &TemplateArgs, |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3378 | bool &MirrorsPrimaryTemplate) { |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 3379 | // FIXME: the interface to this function will have to change to |
| 3380 | // accommodate variadic templates. |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3381 | MirrorsPrimaryTemplate = true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3382 | |
Anders Carlsson | fb25052 | 2009-06-23 01:26:57 +0000 | [diff] [blame] | 3383 | const TemplateArgument *ArgList = TemplateArgs.getFlatArguments(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3384 | |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 3385 | for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) { |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3386 | // Determine whether the template argument list of the partial |
| 3387 | // specialization is identical to the implicit argument list of |
| 3388 | // the primary template. The caller may need to diagnostic this as |
| 3389 | // an error per C++ [temp.class.spec]p9b3. |
| 3390 | if (MirrorsPrimaryTemplate) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3391 | if (TemplateTypeParmDecl *TTP |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3392 | = dyn_cast<TemplateTypeParmDecl>(TemplateParams->getParam(I))) { |
| 3393 | if (Context.getCanonicalType(Context.getTypeDeclType(TTP)) != |
Anders Carlsson | 6360be7 | 2009-06-13 18:20:51 +0000 | [diff] [blame] | 3394 | Context.getCanonicalType(ArgList[I].getAsType())) |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3395 | MirrorsPrimaryTemplate = false; |
| 3396 | } else if (TemplateTemplateParmDecl *TTP |
| 3397 | = dyn_cast<TemplateTemplateParmDecl>( |
| 3398 | TemplateParams->getParam(I))) { |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 3399 | TemplateName Name = ArgList[I].getAsTemplate(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3400 | TemplateTemplateParmDecl *ArgDecl |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 3401 | = dyn_cast_or_null<TemplateTemplateParmDecl>(Name.getAsTemplateDecl()); |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3402 | if (!ArgDecl || |
| 3403 | ArgDecl->getIndex() != TTP->getIndex() || |
| 3404 | ArgDecl->getDepth() != TTP->getDepth()) |
| 3405 | MirrorsPrimaryTemplate = false; |
| 3406 | } |
| 3407 | } |
| 3408 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3409 | NonTypeTemplateParmDecl *Param |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 3410 | = dyn_cast<NonTypeTemplateParmDecl>(TemplateParams->getParam(I)); |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3411 | if (!Param) { |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 3412 | continue; |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3413 | } |
| 3414 | |
Anders Carlsson | 6360be7 | 2009-06-13 18:20:51 +0000 | [diff] [blame] | 3415 | Expr *ArgExpr = ArgList[I].getAsExpr(); |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3416 | if (!ArgExpr) { |
| 3417 | MirrorsPrimaryTemplate = false; |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 3418 | continue; |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3419 | } |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 3420 | |
| 3421 | // C++ [temp.class.spec]p8: |
| 3422 | // A non-type argument is non-specialized if it is the name of a |
| 3423 | // non-type parameter. All other non-type arguments are |
| 3424 | // specialized. |
| 3425 | // |
| 3426 | // Below, we check the two conditions that only apply to |
| 3427 | // specialized non-type arguments, so skip any non-specialized |
| 3428 | // arguments. |
| 3429 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ArgExpr)) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3430 | if (NonTypeTemplateParmDecl *NTTP |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3431 | = dyn_cast<NonTypeTemplateParmDecl>(DRE->getDecl())) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3432 | if (MirrorsPrimaryTemplate && |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3433 | (Param->getIndex() != NTTP->getIndex() || |
| 3434 | Param->getDepth() != NTTP->getDepth())) |
| 3435 | MirrorsPrimaryTemplate = false; |
| 3436 | |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 3437 | continue; |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3438 | } |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 3439 | |
| 3440 | // C++ [temp.class.spec]p9: |
| 3441 | // Within the argument list of a class template partial |
| 3442 | // specialization, the following restrictions apply: |
| 3443 | // -- A partially specialized non-type argument expression |
| 3444 | // shall not involve a template parameter of the partial |
| 3445 | // specialization except when the argument expression is a |
| 3446 | // simple identifier. |
| 3447 | if (ArgExpr->isTypeDependent() || ArgExpr->isValueDependent()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3448 | Diag(ArgExpr->getLocStart(), |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 3449 | diag::err_dependent_non_type_arg_in_partial_spec) |
| 3450 | << ArgExpr->getSourceRange(); |
| 3451 | return true; |
| 3452 | } |
| 3453 | |
| 3454 | // -- The type of a template parameter corresponding to a |
| 3455 | // specialized non-type argument shall not be dependent on a |
| 3456 | // parameter of the specialization. |
| 3457 | if (Param->getType()->isDependentType()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3458 | Diag(ArgExpr->getLocStart(), |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 3459 | diag::err_dependent_typed_non_type_arg_in_partial_spec) |
| 3460 | << Param->getType() |
| 3461 | << ArgExpr->getSourceRange(); |
| 3462 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 3463 | return true; |
| 3464 | } |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3465 | |
| 3466 | MirrorsPrimaryTemplate = false; |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 3467 | } |
| 3468 | |
| 3469 | return false; |
| 3470 | } |
| 3471 | |
Douglas Gregor | dc0a11c | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 3472 | /// \brief Retrieve the previous declaration of the given declaration. |
| 3473 | static NamedDecl *getPreviousDecl(NamedDecl *ND) { |
| 3474 | if (VarDecl *VD = dyn_cast<VarDecl>(ND)) |
| 3475 | return VD->getPreviousDeclaration(); |
| 3476 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) |
| 3477 | return FD->getPreviousDeclaration(); |
| 3478 | if (TagDecl *TD = dyn_cast<TagDecl>(ND)) |
| 3479 | return TD->getPreviousDeclaration(); |
| 3480 | if (TypedefDecl *TD = dyn_cast<TypedefDecl>(ND)) |
| 3481 | return TD->getPreviousDeclaration(); |
| 3482 | if (FunctionTemplateDecl *FTD = dyn_cast<FunctionTemplateDecl>(ND)) |
| 3483 | return FTD->getPreviousDeclaration(); |
| 3484 | if (ClassTemplateDecl *CTD = dyn_cast<ClassTemplateDecl>(ND)) |
| 3485 | return CTD->getPreviousDeclaration(); |
| 3486 | return 0; |
| 3487 | } |
| 3488 | |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 3489 | Sema::DeclResult |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 3490 | Sema::ActOnClassTemplateSpecialization(Scope *S, unsigned TagSpec, |
| 3491 | TagUseKind TUK, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3492 | SourceLocation KWLoc, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 3493 | CXXScopeSpec &SS, |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 3494 | TemplateTy TemplateD, |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3495 | SourceLocation TemplateNameLoc, |
| 3496 | SourceLocation LAngleLoc, |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 3497 | ASTTemplateArgsPtr TemplateArgsIn, |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3498 | SourceLocation RAngleLoc, |
| 3499 | AttributeList *Attr, |
| 3500 | MultiTemplateParamsArg TemplateParameterLists) { |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 3501 | assert(TUK != TUK_Reference && "References are not specializations"); |
John McCall | f1bbbb4 | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 3502 | |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3503 | // Find the class template we're specializing |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 3504 | TemplateName Name = TemplateD.getAsVal<TemplateName>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3505 | ClassTemplateDecl *ClassTemplate |
Douglas Gregor | 8b13c08 | 2009-11-12 00:46:20 +0000 | [diff] [blame] | 3506 | = dyn_cast_or_null<ClassTemplateDecl>(Name.getAsTemplateDecl()); |
| 3507 | |
| 3508 | if (!ClassTemplate) { |
| 3509 | Diag(TemplateNameLoc, diag::err_not_class_template_specialization) |
| 3510 | << (Name.getAsTemplateDecl() && |
| 3511 | isa<TemplateTemplateParmDecl>(Name.getAsTemplateDecl())); |
| 3512 | return true; |
| 3513 | } |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3514 | |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 3515 | bool isExplicitSpecialization = false; |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 3516 | bool isPartialSpecialization = false; |
| 3517 | |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 3518 | // Check the validity of the template headers that introduce this |
| 3519 | // template. |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 3520 | // FIXME: We probably shouldn't complain about these headers for |
| 3521 | // friend declarations. |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 3522 | TemplateParameterList *TemplateParams |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3523 | = MatchTemplateParametersToScopeSpecifier(TemplateNameLoc, SS, |
| 3524 | (TemplateParameterList**)TemplateParameterLists.get(), |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 3525 | TemplateParameterLists.size(), |
| 3526 | isExplicitSpecialization); |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 3527 | if (TemplateParams && TemplateParams->size() > 0) { |
| 3528 | isPartialSpecialization = true; |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 3529 | |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 3530 | // C++ [temp.class.spec]p10: |
| 3531 | // The template parameter list of a specialization shall not |
| 3532 | // contain default template argument values. |
| 3533 | for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) { |
| 3534 | Decl *Param = TemplateParams->getParam(I); |
| 3535 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) { |
| 3536 | if (TTP->hasDefaultArgument()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3537 | Diag(TTP->getDefaultArgumentLoc(), |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 3538 | diag::err_default_arg_in_partial_spec); |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 3539 | TTP->removeDefaultArgument(); |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 3540 | } |
| 3541 | } else if (NonTypeTemplateParmDecl *NTTP |
| 3542 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
| 3543 | if (Expr *DefArg = NTTP->getDefaultArgument()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3544 | Diag(NTTP->getDefaultArgumentLoc(), |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 3545 | diag::err_default_arg_in_partial_spec) |
| 3546 | << DefArg->getSourceRange(); |
| 3547 | NTTP->setDefaultArgument(0); |
| 3548 | DefArg->Destroy(Context); |
| 3549 | } |
| 3550 | } else { |
| 3551 | TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(Param); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 3552 | if (TTP->hasDefaultArgument()) { |
| 3553 | Diag(TTP->getDefaultArgument().getLocation(), |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 3554 | diag::err_default_arg_in_partial_spec) |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 3555 | << TTP->getDefaultArgument().getSourceRange(); |
| 3556 | TTP->setDefaultArgument(TemplateArgumentLoc()); |
Douglas Gregor | ba1ecb5 | 2009-06-12 19:43:02 +0000 | [diff] [blame] | 3557 | } |
| 3558 | } |
| 3559 | } |
Douglas Gregor | a735b20 | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 3560 | } else if (TemplateParams) { |
| 3561 | if (TUK == TUK_Friend) |
| 3562 | Diag(KWLoc, diag::err_template_spec_friend) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 3563 | << FixItHint::CreateRemoval( |
Douglas Gregor | a735b20 | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 3564 | SourceRange(TemplateParams->getTemplateLoc(), |
| 3565 | TemplateParams->getRAngleLoc())) |
| 3566 | << SourceRange(LAngleLoc, RAngleLoc); |
| 3567 | else |
| 3568 | isExplicitSpecialization = true; |
| 3569 | } else if (TUK != TUK_Friend) { |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 3570 | Diag(KWLoc, diag::err_template_spec_needs_header) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 3571 | << FixItHint::CreateInsertion(KWLoc, "template<> "); |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 3572 | isExplicitSpecialization = true; |
| 3573 | } |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 3574 | |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3575 | // Check that the specialization uses the same tag kind as the |
| 3576 | // original template. |
| 3577 | TagDecl::TagKind Kind; |
| 3578 | switch (TagSpec) { |
| 3579 | default: assert(0 && "Unknown tag type!"); |
| 3580 | case DeclSpec::TST_struct: Kind = TagDecl::TK_struct; break; |
| 3581 | case DeclSpec::TST_union: Kind = TagDecl::TK_union; break; |
| 3582 | case DeclSpec::TST_class: Kind = TagDecl::TK_class; break; |
| 3583 | } |
Douglas Gregor | 501c5ce | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 3584 | if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3585 | Kind, KWLoc, |
Douglas Gregor | 501c5ce | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 3586 | *ClassTemplate->getIdentifier())) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3587 | Diag(KWLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 3588 | << ClassTemplate |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 3589 | << FixItHint::CreateReplacement(KWLoc, |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 3590 | ClassTemplate->getTemplatedDecl()->getKindName()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3591 | Diag(ClassTemplate->getTemplatedDecl()->getLocation(), |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3592 | diag::note_previous_use); |
| 3593 | Kind = ClassTemplate->getTemplatedDecl()->getTagKind(); |
| 3594 | } |
| 3595 | |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 3596 | // Translate the parser's template argument list in our AST format. |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 3597 | TemplateArgumentListInfo TemplateArgs; |
| 3598 | TemplateArgs.setLAngleLoc(LAngleLoc); |
| 3599 | TemplateArgs.setRAngleLoc(RAngleLoc); |
Douglas Gregor | 314b97f | 2009-11-10 19:49:08 +0000 | [diff] [blame] | 3600 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 3601 | |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3602 | // Check that the template argument list is well-formed for this |
| 3603 | // template. |
Anders Carlsson | fb25052 | 2009-06-23 01:26:57 +0000 | [diff] [blame] | 3604 | TemplateArgumentListBuilder Converted(ClassTemplate->getTemplateParameters(), |
| 3605 | TemplateArgs.size()); |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 3606 | if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc, |
| 3607 | TemplateArgs, false, Converted)) |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 3608 | return true; |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3609 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3610 | assert((Converted.structuredSize() == |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3611 | ClassTemplate->getTemplateParameters()->size()) && |
| 3612 | "Converted template argument list is too short!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3613 | |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 3614 | // Find the class template (partial) specialization declaration that |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3615 | // corresponds to these arguments. |
| 3616 | llvm::FoldingSetNodeID ID; |
Douglas Gregor | ba1ecb5 | 2009-06-12 19:43:02 +0000 | [diff] [blame] | 3617 | if (isPartialSpecialization) { |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3618 | bool MirrorsPrimaryTemplate; |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 3619 | if (CheckClassTemplatePartialSpecializationArgs( |
| 3620 | ClassTemplate->getTemplateParameters(), |
Anders Carlsson | fb25052 | 2009-06-23 01:26:57 +0000 | [diff] [blame] | 3621 | Converted, MirrorsPrimaryTemplate)) |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 3622 | return true; |
| 3623 | |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3624 | if (MirrorsPrimaryTemplate) { |
| 3625 | // C++ [temp.class.spec]p9b3: |
| 3626 | // |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3627 | // -- The argument list of the specialization shall not be identical |
| 3628 | // to the implicit argument list of the primary template. |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3629 | Diag(TemplateNameLoc, diag::err_partial_spec_args_match_primary_template) |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 3630 | << (TUK == TUK_Definition) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 3631 | << FixItHint::CreateRemoval(SourceRange(LAngleLoc, RAngleLoc)); |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 3632 | return CheckClassTemplate(S, TagSpec, TUK, KWLoc, SS, |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3633 | ClassTemplate->getIdentifier(), |
| 3634 | TemplateNameLoc, |
| 3635 | Attr, |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 3636 | TemplateParams, |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3637 | AS_none); |
| 3638 | } |
| 3639 | |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 3640 | // FIXME: Diagnose friend partial specializations |
| 3641 | |
Douglas Gregor | de09096 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 3642 | if (!Name.isDependent() && |
| 3643 | !TemplateSpecializationType::anyDependentTemplateArguments( |
| 3644 | TemplateArgs.getArgumentArray(), |
| 3645 | TemplateArgs.size())) { |
| 3646 | Diag(TemplateNameLoc, diag::err_partial_spec_fully_specialized) |
| 3647 | << ClassTemplate->getDeclName(); |
| 3648 | isPartialSpecialization = false; |
| 3649 | } else { |
| 3650 | // FIXME: Template parameter list matters, too |
| 3651 | ClassTemplatePartialSpecializationDecl::Profile(ID, |
| 3652 | Converted.getFlatArguments(), |
| 3653 | Converted.flatSize(), |
| 3654 | Context); |
| 3655 | } |
| 3656 | } |
| 3657 | |
| 3658 | if (!isPartialSpecialization) |
Anders Carlsson | 1c5976e | 2009-06-05 03:43:12 +0000 | [diff] [blame] | 3659 | ClassTemplateSpecializationDecl::Profile(ID, |
Anders Carlsson | fb25052 | 2009-06-23 01:26:57 +0000 | [diff] [blame] | 3660 | Converted.getFlatArguments(), |
Douglas Gregor | 828e226 | 2009-07-29 16:09:57 +0000 | [diff] [blame] | 3661 | Converted.flatSize(), |
| 3662 | Context); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3663 | void *InsertPos = 0; |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 3664 | ClassTemplateSpecializationDecl *PrevDecl = 0; |
| 3665 | |
| 3666 | if (isPartialSpecialization) |
| 3667 | PrevDecl |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3668 | = ClassTemplate->getPartialSpecializations().FindNodeOrInsertPos(ID, |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 3669 | InsertPos); |
| 3670 | else |
| 3671 | PrevDecl |
| 3672 | = ClassTemplate->getSpecializations().FindNodeOrInsertPos(ID, InsertPos); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3673 | |
| 3674 | ClassTemplateSpecializationDecl *Specialization = 0; |
| 3675 | |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 3676 | // Check whether we can declare a class template specialization in |
| 3677 | // the current scope. |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 3678 | if (TUK != TUK_Friend && |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3679 | CheckTemplateSpecializationScope(*this, ClassTemplate, PrevDecl, |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 3680 | TemplateNameLoc, |
| 3681 | isPartialSpecialization)) |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 3682 | return true; |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 3683 | |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 3684 | // The canonical type |
| 3685 | QualType CanonType; |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 3686 | if (PrevDecl && |
| 3687 | (PrevDecl->getSpecializationKind() == TSK_Undeclared || |
Douglas Gregor | de09096 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 3688 | TUK == TUK_Friend)) { |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3689 | // Since the only prior class template specialization with these |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 3690 | // arguments was referenced but not declared, or we're only |
| 3691 | // referencing this specialization as a friend, reuse that |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3692 | // declaration node as our own, updating its source location to |
| 3693 | // reflect our new declaration. |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3694 | Specialization = PrevDecl; |
Douglas Gregor | 6bc9f7e | 2009-02-25 22:18:32 +0000 | [diff] [blame] | 3695 | Specialization->setLocation(TemplateNameLoc); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3696 | PrevDecl = 0; |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 3697 | CanonType = Context.getTypeDeclType(Specialization); |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 3698 | } else if (isPartialSpecialization) { |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 3699 | // Build the canonical type that describes the converted template |
| 3700 | // arguments of the class template partial specialization. |
Douglas Gregor | de09096 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 3701 | TemplateName CanonTemplate = Context.getCanonicalTemplateName(Name); |
| 3702 | CanonType = Context.getTemplateSpecializationType(CanonTemplate, |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 3703 | Converted.getFlatArguments(), |
| 3704 | Converted.flatSize()); |
| 3705 | |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 3706 | // Create a new class template partial specialization declaration node. |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 3707 | ClassTemplatePartialSpecializationDecl *PrevPartial |
| 3708 | = cast_or_null<ClassTemplatePartialSpecializationDecl>(PrevDecl); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3709 | ClassTemplatePartialSpecializationDecl *Partial |
| 3710 | = ClassTemplatePartialSpecializationDecl::Create(Context, |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 3711 | ClassTemplate->getDeclContext(), |
Anders Carlsson | 91fdf6f | 2009-06-05 04:06:48 +0000 | [diff] [blame] | 3712 | TemplateNameLoc, |
| 3713 | TemplateParams, |
| 3714 | ClassTemplate, |
Anders Carlsson | fb25052 | 2009-06-23 01:26:57 +0000 | [diff] [blame] | 3715 | Converted, |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 3716 | TemplateArgs, |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 3717 | CanonType, |
Anders Carlsson | 91fdf6f | 2009-06-05 04:06:48 +0000 | [diff] [blame] | 3718 | PrevPartial); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 3719 | SetNestedNameSpecifier(Partial, SS); |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 3720 | |
| 3721 | if (PrevPartial) { |
| 3722 | ClassTemplate->getPartialSpecializations().RemoveNode(PrevPartial); |
| 3723 | ClassTemplate->getPartialSpecializations().GetOrInsertNode(Partial); |
| 3724 | } else { |
| 3725 | ClassTemplate->getPartialSpecializations().InsertNode(Partial, InsertPos); |
| 3726 | } |
| 3727 | Specialization = Partial; |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 3728 | |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 3729 | // If we are providing an explicit specialization of a member class |
| 3730 | // template specialization, make a note of that. |
| 3731 | if (PrevPartial && PrevPartial->getInstantiatedFromMember()) |
| 3732 | PrevPartial->setMemberSpecialization(); |
| 3733 | |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 3734 | // Check that all of the template parameters of the class template |
| 3735 | // partial specialization are deducible from the template |
| 3736 | // arguments. If not, this class template partial specialization |
| 3737 | // will never be used. |
| 3738 | llvm::SmallVector<bool, 8> DeducibleParams; |
| 3739 | DeducibleParams.resize(TemplateParams->size()); |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 3740 | MarkUsedTemplateParameters(Partial->getTemplateArgs(), true, |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 3741 | TemplateParams->getDepth(), |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 3742 | DeducibleParams); |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 3743 | unsigned NumNonDeducible = 0; |
| 3744 | for (unsigned I = 0, N = DeducibleParams.size(); I != N; ++I) |
| 3745 | if (!DeducibleParams[I]) |
| 3746 | ++NumNonDeducible; |
| 3747 | |
| 3748 | if (NumNonDeducible) { |
| 3749 | Diag(TemplateNameLoc, diag::warn_partial_specs_not_deducible) |
| 3750 | << (NumNonDeducible > 1) |
| 3751 | << SourceRange(TemplateNameLoc, RAngleLoc); |
| 3752 | for (unsigned I = 0, N = DeducibleParams.size(); I != N; ++I) { |
| 3753 | if (!DeducibleParams[I]) { |
| 3754 | NamedDecl *Param = cast<NamedDecl>(TemplateParams->getParam(I)); |
| 3755 | if (Param->getDeclName()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3756 | Diag(Param->getLocation(), |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 3757 | diag::note_partial_spec_unused_parameter) |
| 3758 | << Param->getDeclName(); |
| 3759 | else |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3760 | Diag(Param->getLocation(), |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 3761 | diag::note_partial_spec_unused_parameter) |
| 3762 | << std::string("<anonymous>"); |
| 3763 | } |
| 3764 | } |
| 3765 | } |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3766 | } else { |
| 3767 | // Create a new class template specialization declaration node for |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 3768 | // this explicit specialization or friend declaration. |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3769 | Specialization |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3770 | = ClassTemplateSpecializationDecl::Create(Context, |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3771 | ClassTemplate->getDeclContext(), |
| 3772 | TemplateNameLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3773 | ClassTemplate, |
Anders Carlsson | fb25052 | 2009-06-23 01:26:57 +0000 | [diff] [blame] | 3774 | Converted, |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3775 | PrevDecl); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 3776 | SetNestedNameSpecifier(Specialization, SS); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3777 | |
| 3778 | if (PrevDecl) { |
| 3779 | ClassTemplate->getSpecializations().RemoveNode(PrevDecl); |
| 3780 | ClassTemplate->getSpecializations().GetOrInsertNode(Specialization); |
| 3781 | } else { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3782 | ClassTemplate->getSpecializations().InsertNode(Specialization, |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3783 | InsertPos); |
| 3784 | } |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 3785 | |
| 3786 | CanonType = Context.getTypeDeclType(Specialization); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3787 | } |
| 3788 | |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 3789 | // C++ [temp.expl.spec]p6: |
| 3790 | // If a template, a member template or the member of a class template is |
| 3791 | // explicitly specialized then that specialization shall be declared |
| 3792 | // before the first use of that specialization that would cause an implicit |
| 3793 | // instantiation to take place, in every translation unit in which such a |
| 3794 | // use occurs; no diagnostic is required. |
| 3795 | if (PrevDecl && PrevDecl->getPointOfInstantiation().isValid()) { |
Douglas Gregor | dc0a11c | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 3796 | bool Okay = false; |
| 3797 | for (NamedDecl *Prev = PrevDecl; Prev; Prev = getPreviousDecl(Prev)) { |
| 3798 | // Is there any previous explicit specialization declaration? |
| 3799 | if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) { |
| 3800 | Okay = true; |
| 3801 | break; |
| 3802 | } |
| 3803 | } |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 3804 | |
Douglas Gregor | dc0a11c | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 3805 | if (!Okay) { |
| 3806 | SourceRange Range(TemplateNameLoc, RAngleLoc); |
| 3807 | Diag(TemplateNameLoc, diag::err_specialization_after_instantiation) |
| 3808 | << Context.getTypeDeclType(Specialization) << Range; |
| 3809 | |
| 3810 | Diag(PrevDecl->getPointOfInstantiation(), |
| 3811 | diag::note_instantiation_required_here) |
| 3812 | << (PrevDecl->getTemplateSpecializationKind() |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 3813 | != TSK_ImplicitInstantiation); |
Douglas Gregor | dc0a11c | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 3814 | return true; |
| 3815 | } |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 3816 | } |
| 3817 | |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 3818 | // If this is not a friend, note that this is an explicit specialization. |
| 3819 | if (TUK != TUK_Friend) |
| 3820 | Specialization->setSpecializationKind(TSK_ExplicitSpecialization); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3821 | |
| 3822 | // Check that this isn't a redefinition of this specialization. |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 3823 | if (TUK == TUK_Definition) { |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 3824 | if (RecordDecl *Def = Specialization->getDefinition()) { |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3825 | SourceRange Range(TemplateNameLoc, RAngleLoc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3826 | Diag(TemplateNameLoc, diag::err_redefinition) |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 3827 | << Context.getTypeDeclType(Specialization) << Range; |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3828 | Diag(Def->getLocation(), diag::note_previous_definition); |
| 3829 | Specialization->setInvalidDecl(); |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 3830 | return true; |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3831 | } |
| 3832 | } |
| 3833 | |
Douglas Gregor | fc705b8 | 2009-02-26 22:19:44 +0000 | [diff] [blame] | 3834 | // Build the fully-sugared type for this class template |
| 3835 | // specialization as the user wrote in the specialization |
| 3836 | // itself. This means that we'll pretty-print the type retrieved |
| 3837 | // from the specialization's declaration the way that the user |
| 3838 | // actually wrote the specialization, rather than formatting the |
| 3839 | // name based on the "canonical" representation used to store the |
| 3840 | // template arguments in the specialization. |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 3841 | TypeSourceInfo *WrittenTy |
| 3842 | = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc, |
| 3843 | TemplateArgs, CanonType); |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 3844 | if (TUK != TUK_Friend) |
| 3845 | Specialization->setTypeAsWritten(WrittenTy); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 3846 | TemplateArgsIn.release(); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3847 | |
Douglas Gregor | 6bc9f7e | 2009-02-25 22:18:32 +0000 | [diff] [blame] | 3848 | // C++ [temp.expl.spec]p9: |
| 3849 | // A template explicit specialization is in the scope of the |
| 3850 | // namespace in which the template was defined. |
| 3851 | // |
| 3852 | // We actually implement this paragraph where we set the semantic |
| 3853 | // context (in the creation of the ClassTemplateSpecializationDecl), |
| 3854 | // but we also maintain the lexical context where the actual |
| 3855 | // definition occurs. |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3856 | Specialization->setLexicalDeclContext(CurContext); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3857 | |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3858 | // We may be starting the definition of this specialization. |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 3859 | if (TUK == TUK_Definition) |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3860 | Specialization->startDefinition(); |
| 3861 | |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 3862 | if (TUK == TUK_Friend) { |
| 3863 | FriendDecl *Friend = FriendDecl::Create(Context, CurContext, |
| 3864 | TemplateNameLoc, |
John McCall | 32f2fb5 | 2010-03-25 18:04:51 +0000 | [diff] [blame] | 3865 | WrittenTy, |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 3866 | /*FIXME:*/KWLoc); |
| 3867 | Friend->setAccess(AS_public); |
| 3868 | CurContext->addDecl(Friend); |
| 3869 | } else { |
| 3870 | // Add the specialization into its lexical context, so that it can |
| 3871 | // be seen when iterating through the list of declarations in that |
| 3872 | // context. However, specializations are not found by name lookup. |
| 3873 | CurContext->addDecl(Specialization); |
| 3874 | } |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 3875 | return DeclPtrTy::make(Specialization); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3876 | } |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3877 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3878 | Sema::DeclPtrTy |
| 3879 | Sema::ActOnTemplateDeclarator(Scope *S, |
Douglas Gregor | e542c86 | 2009-06-23 23:11:28 +0000 | [diff] [blame] | 3880 | MultiTemplateParamsArg TemplateParameterLists, |
| 3881 | Declarator &D) { |
| 3882 | return HandleDeclarator(S, D, move(TemplateParameterLists), false); |
| 3883 | } |
| 3884 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3885 | Sema::DeclPtrTy |
| 3886 | Sema::ActOnStartOfFunctionTemplateDef(Scope *FnBodyScope, |
Douglas Gregor | 52591bf | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 3887 | MultiTemplateParamsArg TemplateParameterLists, |
| 3888 | Declarator &D) { |
| 3889 | assert(getCurFunctionDecl() == 0 && "Function parsing confused"); |
| 3890 | assert(D.getTypeObject(0).Kind == DeclaratorChunk::Function && |
| 3891 | "Not a function declarator!"); |
| 3892 | DeclaratorChunk::FunctionTypeInfo &FTI = D.getTypeObject(0).Fun; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3893 | |
Douglas Gregor | 52591bf | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 3894 | if (FTI.hasPrototype) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3895 | // FIXME: Diagnose arguments without names in C. |
Douglas Gregor | 52591bf | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 3896 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3897 | |
Douglas Gregor | 52591bf | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 3898 | Scope *ParentScope = FnBodyScope->getParent(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3899 | |
| 3900 | DeclPtrTy DP = HandleDeclarator(ParentScope, D, |
Douglas Gregor | 52591bf | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 3901 | move(TemplateParameterLists), |
| 3902 | /*IsFunctionDefinition=*/true); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3903 | if (FunctionTemplateDecl *FunctionTemplate |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 3904 | = dyn_cast_or_null<FunctionTemplateDecl>(DP.getAs<Decl>())) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3905 | return ActOnStartOfFunctionDef(FnBodyScope, |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3906 | DeclPtrTy::make(FunctionTemplate->getTemplatedDecl())); |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 3907 | if (FunctionDecl *Function = dyn_cast_or_null<FunctionDecl>(DP.getAs<Decl>())) |
| 3908 | return ActOnStartOfFunctionDef(FnBodyScope, DeclPtrTy::make(Function)); |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3909 | return DeclPtrTy(); |
Douglas Gregor | 52591bf | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 3910 | } |
| 3911 | |
John McCall | 7504239 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 3912 | /// \brief Strips various properties off an implicit instantiation |
| 3913 | /// that has just been explicitly specialized. |
| 3914 | static void StripImplicitInstantiation(NamedDecl *D) { |
| 3915 | D->invalidateAttrs(); |
| 3916 | |
| 3917 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 3918 | FD->setInlineSpecified(false); |
| 3919 | } |
| 3920 | } |
| 3921 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 3922 | /// \brief Diagnose cases where we have an explicit template specialization |
| 3923 | /// before/after an explicit template instantiation, producing diagnostics |
| 3924 | /// for those cases where they are required and determining whether the |
| 3925 | /// new specialization/instantiation will have any effect. |
| 3926 | /// |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 3927 | /// \param NewLoc the location of the new explicit specialization or |
| 3928 | /// instantiation. |
| 3929 | /// |
| 3930 | /// \param NewTSK the kind of the new explicit specialization or instantiation. |
| 3931 | /// |
| 3932 | /// \param PrevDecl the previous declaration of the entity. |
| 3933 | /// |
| 3934 | /// \param PrevTSK the kind of the old explicit specialization or instantiatin. |
| 3935 | /// |
| 3936 | /// \param PrevPointOfInstantiation if valid, indicates where the previus |
| 3937 | /// declaration was instantiated (either implicitly or explicitly). |
| 3938 | /// |
| 3939 | /// \param SuppressNew will be set to true to indicate that the new |
| 3940 | /// specialization or instantiation has no effect and should be ignored. |
| 3941 | /// |
| 3942 | /// \returns true if there was an error that should prevent the introduction of |
| 3943 | /// the new declaration into the AST, false otherwise. |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 3944 | bool |
| 3945 | Sema::CheckSpecializationInstantiationRedecl(SourceLocation NewLoc, |
| 3946 | TemplateSpecializationKind NewTSK, |
| 3947 | NamedDecl *PrevDecl, |
| 3948 | TemplateSpecializationKind PrevTSK, |
| 3949 | SourceLocation PrevPointOfInstantiation, |
| 3950 | bool &SuppressNew) { |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 3951 | SuppressNew = false; |
| 3952 | |
| 3953 | switch (NewTSK) { |
| 3954 | case TSK_Undeclared: |
| 3955 | case TSK_ImplicitInstantiation: |
| 3956 | assert(false && "Don't check implicit instantiations here"); |
| 3957 | return false; |
| 3958 | |
| 3959 | case TSK_ExplicitSpecialization: |
| 3960 | switch (PrevTSK) { |
| 3961 | case TSK_Undeclared: |
| 3962 | case TSK_ExplicitSpecialization: |
| 3963 | // Okay, we're just specializing something that is either already |
| 3964 | // explicitly specialized or has merely been mentioned without any |
| 3965 | // instantiation. |
| 3966 | return false; |
| 3967 | |
| 3968 | case TSK_ImplicitInstantiation: |
| 3969 | if (PrevPointOfInstantiation.isInvalid()) { |
| 3970 | // The declaration itself has not actually been instantiated, so it is |
| 3971 | // still okay to specialize it. |
John McCall | 7504239 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 3972 | StripImplicitInstantiation(PrevDecl); |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 3973 | return false; |
| 3974 | } |
| 3975 | // Fall through |
| 3976 | |
| 3977 | case TSK_ExplicitInstantiationDeclaration: |
| 3978 | case TSK_ExplicitInstantiationDefinition: |
| 3979 | assert((PrevTSK == TSK_ImplicitInstantiation || |
| 3980 | PrevPointOfInstantiation.isValid()) && |
| 3981 | "Explicit instantiation without point of instantiation?"); |
| 3982 | |
| 3983 | // C++ [temp.expl.spec]p6: |
| 3984 | // If a template, a member template or the member of a class template |
| 3985 | // is explicitly specialized then that specialization shall be declared |
| 3986 | // before the first use of that specialization that would cause an |
| 3987 | // implicit instantiation to take place, in every translation unit in |
| 3988 | // which such a use occurs; no diagnostic is required. |
Douglas Gregor | dc0a11c | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 3989 | for (NamedDecl *Prev = PrevDecl; Prev; Prev = getPreviousDecl(Prev)) { |
| 3990 | // Is there any previous explicit specialization declaration? |
| 3991 | if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) |
| 3992 | return false; |
| 3993 | } |
| 3994 | |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 3995 | Diag(NewLoc, diag::err_specialization_after_instantiation) |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 3996 | << PrevDecl; |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 3997 | Diag(PrevPointOfInstantiation, diag::note_instantiation_required_here) |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 3998 | << (PrevTSK != TSK_ImplicitInstantiation); |
| 3999 | |
| 4000 | return true; |
| 4001 | } |
| 4002 | break; |
| 4003 | |
| 4004 | case TSK_ExplicitInstantiationDeclaration: |
| 4005 | switch (PrevTSK) { |
| 4006 | case TSK_ExplicitInstantiationDeclaration: |
| 4007 | // This explicit instantiation declaration is redundant (that's okay). |
| 4008 | SuppressNew = true; |
| 4009 | return false; |
| 4010 | |
| 4011 | case TSK_Undeclared: |
| 4012 | case TSK_ImplicitInstantiation: |
| 4013 | // We're explicitly instantiating something that may have already been |
| 4014 | // implicitly instantiated; that's fine. |
| 4015 | return false; |
| 4016 | |
| 4017 | case TSK_ExplicitSpecialization: |
| 4018 | // C++0x [temp.explicit]p4: |
| 4019 | // For a given set of template parameters, if an explicit instantiation |
| 4020 | // of a template appears after a declaration of an explicit |
| 4021 | // specialization for that template, the explicit instantiation has no |
| 4022 | // effect. |
John McCall | e97c32f | 2010-03-02 23:09:38 +0000 | [diff] [blame] | 4023 | SuppressNew = true; |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4024 | return false; |
| 4025 | |
| 4026 | case TSK_ExplicitInstantiationDefinition: |
| 4027 | // C++0x [temp.explicit]p10: |
| 4028 | // If an entity is the subject of both an explicit instantiation |
| 4029 | // declaration and an explicit instantiation definition in the same |
| 4030 | // translation unit, the definition shall follow the declaration. |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 4031 | Diag(NewLoc, |
| 4032 | diag::err_explicit_instantiation_declaration_after_definition); |
| 4033 | Diag(PrevPointOfInstantiation, |
| 4034 | diag::note_explicit_instantiation_definition_here); |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4035 | assert(PrevPointOfInstantiation.isValid() && |
| 4036 | "Explicit instantiation without point of instantiation?"); |
| 4037 | SuppressNew = true; |
| 4038 | return false; |
| 4039 | } |
| 4040 | break; |
| 4041 | |
| 4042 | case TSK_ExplicitInstantiationDefinition: |
| 4043 | switch (PrevTSK) { |
| 4044 | case TSK_Undeclared: |
| 4045 | case TSK_ImplicitInstantiation: |
| 4046 | // We're explicitly instantiating something that may have already been |
| 4047 | // implicitly instantiated; that's fine. |
| 4048 | return false; |
| 4049 | |
| 4050 | case TSK_ExplicitSpecialization: |
| 4051 | // C++ DR 259, C++0x [temp.explicit]p4: |
| 4052 | // For a given set of template parameters, if an explicit |
| 4053 | // instantiation of a template appears after a declaration of |
| 4054 | // an explicit specialization for that template, the explicit |
| 4055 | // instantiation has no effect. |
| 4056 | // |
| 4057 | // 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] | 4058 | // is not harmful to try to explicitly instantiate something that |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4059 | // has been explicitly specialized. |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 4060 | if (!getLangOptions().CPlusPlus0x) { |
| 4061 | Diag(NewLoc, diag::ext_explicit_instantiation_after_specialization) |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4062 | << PrevDecl; |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 4063 | Diag(PrevDecl->getLocation(), |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4064 | diag::note_previous_template_specialization); |
| 4065 | } |
| 4066 | SuppressNew = true; |
| 4067 | return false; |
| 4068 | |
| 4069 | case TSK_ExplicitInstantiationDeclaration: |
| 4070 | // We're explicity instantiating a definition for something for which we |
| 4071 | // were previously asked to suppress instantiations. That's fine. |
| 4072 | return false; |
| 4073 | |
| 4074 | case TSK_ExplicitInstantiationDefinition: |
| 4075 | // C++0x [temp.spec]p5: |
| 4076 | // For a given template and a given set of template-arguments, |
| 4077 | // - an explicit instantiation definition shall appear at most once |
| 4078 | // in a program, |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 4079 | Diag(NewLoc, diag::err_explicit_instantiation_duplicate) |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4080 | << PrevDecl; |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 4081 | Diag(PrevPointOfInstantiation, |
| 4082 | diag::note_previous_explicit_instantiation); |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4083 | SuppressNew = true; |
| 4084 | return false; |
| 4085 | } |
| 4086 | break; |
| 4087 | } |
| 4088 | |
| 4089 | assert(false && "Missing specialization/instantiation case?"); |
| 4090 | |
| 4091 | return false; |
| 4092 | } |
| 4093 | |
John McCall | af2094e | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 4094 | /// \brief Perform semantic analysis for the given dependent function |
| 4095 | /// template specialization. The only possible way to get a dependent |
| 4096 | /// function template specialization is with a friend declaration, |
| 4097 | /// like so: |
| 4098 | /// |
| 4099 | /// template <class T> void foo(T); |
| 4100 | /// template <class T> class A { |
| 4101 | /// friend void foo<>(T); |
| 4102 | /// }; |
| 4103 | /// |
| 4104 | /// There really isn't any useful analysis we can do here, so we |
| 4105 | /// just store the information. |
| 4106 | bool |
| 4107 | Sema::CheckDependentFunctionTemplateSpecialization(FunctionDecl *FD, |
| 4108 | const TemplateArgumentListInfo &ExplicitTemplateArgs, |
| 4109 | LookupResult &Previous) { |
| 4110 | // Remove anything from Previous that isn't a function template in |
| 4111 | // the correct context. |
| 4112 | DeclContext *FDLookupContext = FD->getDeclContext()->getLookupContext(); |
| 4113 | LookupResult::Filter F = Previous.makeFilter(); |
| 4114 | while (F.hasNext()) { |
| 4115 | NamedDecl *D = F.next()->getUnderlyingDecl(); |
| 4116 | if (!isa<FunctionTemplateDecl>(D) || |
| 4117 | !FDLookupContext->Equals(D->getDeclContext()->getLookupContext())) |
| 4118 | F.erase(); |
| 4119 | } |
| 4120 | F.done(); |
| 4121 | |
| 4122 | // Should this be diagnosed here? |
| 4123 | if (Previous.empty()) return true; |
| 4124 | |
| 4125 | FD->setDependentTemplateSpecialization(Context, Previous.asUnresolvedSet(), |
| 4126 | ExplicitTemplateArgs); |
| 4127 | return false; |
| 4128 | } |
| 4129 | |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4130 | /// \brief Perform semantic analysis for the given function template |
| 4131 | /// specialization. |
| 4132 | /// |
| 4133 | /// This routine performs all of the semantic analysis required for an |
| 4134 | /// explicit function template specialization. On successful completion, |
| 4135 | /// the function declaration \p FD will become a function template |
| 4136 | /// specialization. |
| 4137 | /// |
| 4138 | /// \param FD the function declaration, which will be updated to become a |
| 4139 | /// function template specialization. |
| 4140 | /// |
| 4141 | /// \param HasExplicitTemplateArgs whether any template arguments were |
| 4142 | /// explicitly provided. |
| 4143 | /// |
| 4144 | /// \param LAngleLoc the location of the left angle bracket ('<'), if |
| 4145 | /// template arguments were explicitly provided. |
| 4146 | /// |
| 4147 | /// \param ExplicitTemplateArgs the explicitly-provided template arguments, |
| 4148 | /// if any. |
| 4149 | /// |
| 4150 | /// \param NumExplicitTemplateArgs the number of explicitly-provided template |
| 4151 | /// arguments. This number may be zero even when HasExplicitTemplateArgs is |
| 4152 | /// true as in, e.g., \c void sort<>(char*, char*); |
| 4153 | /// |
| 4154 | /// \param RAngleLoc the location of the right angle bracket ('>'), if |
| 4155 | /// template arguments were explicitly provided. |
| 4156 | /// |
| 4157 | /// \param PrevDecl the set of declarations that |
| 4158 | bool |
| 4159 | Sema::CheckFunctionTemplateSpecialization(FunctionDecl *FD, |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 4160 | const TemplateArgumentListInfo *ExplicitTemplateArgs, |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 4161 | LookupResult &Previous) { |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4162 | // The set of function template specializations that could match this |
| 4163 | // explicit function template specialization. |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4164 | UnresolvedSet<8> Candidates; |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4165 | |
| 4166 | DeclContext *FDLookupContext = FD->getDeclContext()->getLookupContext(); |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 4167 | for (LookupResult::iterator I = Previous.begin(), E = Previous.end(); |
| 4168 | I != E; ++I) { |
| 4169 | NamedDecl *Ovl = (*I)->getUnderlyingDecl(); |
| 4170 | if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Ovl)) { |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4171 | // Only consider templates found within the same semantic lookup scope as |
| 4172 | // FD. |
| 4173 | if (!FDLookupContext->Equals(Ovl->getDeclContext()->getLookupContext())) |
| 4174 | continue; |
| 4175 | |
| 4176 | // C++ [temp.expl.spec]p11: |
| 4177 | // A trailing template-argument can be left unspecified in the |
| 4178 | // template-id naming an explicit function template specialization |
| 4179 | // provided it can be deduced from the function argument type. |
| 4180 | // Perform template argument deduction to determine whether we may be |
| 4181 | // specializing this template. |
| 4182 | // FIXME: It is somewhat wasteful to build |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 4183 | TemplateDeductionInfo Info(Context, FD->getLocation()); |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4184 | FunctionDecl *Specialization = 0; |
| 4185 | if (TemplateDeductionResult TDK |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 4186 | = DeduceTemplateArguments(FunTmpl, ExplicitTemplateArgs, |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4187 | FD->getType(), |
| 4188 | Specialization, |
| 4189 | Info)) { |
| 4190 | // FIXME: Template argument deduction failed; record why it failed, so |
| 4191 | // that we can provide nifty diagnostics. |
| 4192 | (void)TDK; |
| 4193 | continue; |
| 4194 | } |
| 4195 | |
| 4196 | // Record this candidate. |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4197 | Candidates.addDecl(Specialization, I.getAccess()); |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4198 | } |
| 4199 | } |
| 4200 | |
Douglas Gregor | c5df30f | 2009-09-26 03:41:46 +0000 | [diff] [blame] | 4201 | // Find the most specialized function template. |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4202 | UnresolvedSetIterator Result |
| 4203 | = getMostSpecialized(Candidates.begin(), Candidates.end(), |
| 4204 | TPOC_Other, FD->getLocation(), |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 4205 | PDiag(diag::err_function_template_spec_no_match) |
Douglas Gregor | c5df30f | 2009-09-26 03:41:46 +0000 | [diff] [blame] | 4206 | << FD->getDeclName(), |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 4207 | PDiag(diag::err_function_template_spec_ambiguous) |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 4208 | << FD->getDeclName() << (ExplicitTemplateArgs != 0), |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 4209 | PDiag(diag::note_function_template_spec_matched)); |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4210 | if (Result == Candidates.end()) |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4211 | return true; |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4212 | |
| 4213 | // Ignore access information; it doesn't figure into redeclaration checking. |
| 4214 | FunctionDecl *Specialization = cast<FunctionDecl>(*Result); |
Douglas Gregor | c42b652 | 2010-04-09 21:02:29 +0000 | [diff] [blame] | 4215 | Specialization->setLocation(FD->getLocation()); |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4216 | |
| 4217 | // FIXME: Check if the prior specialization has a point of instantiation. |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 4218 | // If so, we have run afoul of . |
John McCall | 7ad650f | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 4219 | |
| 4220 | // If this is a friend declaration, then we're not really declaring |
| 4221 | // an explicit specialization. |
| 4222 | bool isFriend = (FD->getFriendObjectKind() != Decl::FOK_None); |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4223 | |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4224 | // Check the scope of this explicit specialization. |
John McCall | 7ad650f | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 4225 | if (!isFriend && |
| 4226 | CheckTemplateSpecializationScope(*this, |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4227 | Specialization->getPrimaryTemplate(), |
| 4228 | Specialization, FD->getLocation(), |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4229 | false)) |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4230 | return true; |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 4231 | |
| 4232 | // C++ [temp.expl.spec]p6: |
| 4233 | // 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] | 4234 | // explicitly specialized then that specialization shall be declared |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 4235 | // before the first use of that specialization that would cause an implicit |
| 4236 | // instantiation to take place, in every translation unit in which such a |
| 4237 | // use occurs; no diagnostic is required. |
| 4238 | FunctionTemplateSpecializationInfo *SpecInfo |
| 4239 | = Specialization->getTemplateSpecializationInfo(); |
| 4240 | assert(SpecInfo && "Function template specialization info missing?"); |
John McCall | 7504239 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 4241 | |
| 4242 | bool SuppressNew = false; |
John McCall | 7ad650f | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 4243 | if (!isFriend && |
| 4244 | CheckSpecializationInstantiationRedecl(FD->getLocation(), |
John McCall | 7504239 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 4245 | TSK_ExplicitSpecialization, |
| 4246 | Specialization, |
| 4247 | SpecInfo->getTemplateSpecializationKind(), |
| 4248 | SpecInfo->getPointOfInstantiation(), |
| 4249 | SuppressNew)) |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 4250 | return true; |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4251 | |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4252 | // Mark the prior declaration as an explicit specialization, so that later |
| 4253 | // clients know that this is an explicit specialization. |
John McCall | 7ad650f | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 4254 | if (!isFriend) |
| 4255 | SpecInfo->setTemplateSpecializationKind(TSK_ExplicitSpecialization); |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4256 | |
| 4257 | // Turn the given function declaration into a function template |
| 4258 | // specialization, with the template arguments from the previous |
| 4259 | // specialization. |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 4260 | FD->setFunctionTemplateSpecialization(Specialization->getPrimaryTemplate(), |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4261 | new (Context) TemplateArgumentList( |
| 4262 | *Specialization->getTemplateSpecializationArgs()), |
| 4263 | /*InsertPos=*/0, |
John McCall | 7ad650f | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 4264 | SpecInfo->getTemplateSpecializationKind()); |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4265 | |
| 4266 | // The "previous declaration" for this function template specialization is |
| 4267 | // the prior function template specialization. |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 4268 | Previous.clear(); |
| 4269 | Previous.addDecl(Specialization); |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4270 | return false; |
| 4271 | } |
| 4272 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4273 | /// \brief Perform semantic analysis for the given non-template member |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 4274 | /// specialization. |
| 4275 | /// |
| 4276 | /// This routine performs all of the semantic analysis required for an |
| 4277 | /// explicit member function specialization. On successful completion, |
| 4278 | /// the function declaration \p FD will become a member function |
| 4279 | /// specialization. |
| 4280 | /// |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4281 | /// \param Member the member declaration, which will be updated to become a |
| 4282 | /// specialization. |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 4283 | /// |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 4284 | /// \param Previous the set of declarations, one of which may be specialized |
| 4285 | /// by this function specialization; the set will be modified to contain the |
| 4286 | /// redeclared member. |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 4287 | bool |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 4288 | Sema::CheckMemberSpecialization(NamedDecl *Member, LookupResult &Previous) { |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4289 | assert(!isa<TemplateDecl>(Member) && "Only for non-template members"); |
| 4290 | |
| 4291 | // Try to find the member we are instantiating. |
| 4292 | NamedDecl *Instantiation = 0; |
| 4293 | NamedDecl *InstantiatedFrom = 0; |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 4294 | MemberSpecializationInfo *MSInfo = 0; |
| 4295 | |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 4296 | if (Previous.empty()) { |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4297 | // Nowhere to look anyway. |
| 4298 | } else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Member)) { |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 4299 | for (LookupResult::iterator I = Previous.begin(), E = Previous.end(); |
| 4300 | I != E; ++I) { |
| 4301 | NamedDecl *D = (*I)->getUnderlyingDecl(); |
| 4302 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) { |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4303 | if (Context.hasSameType(Function->getType(), Method->getType())) { |
| 4304 | Instantiation = Method; |
| 4305 | InstantiatedFrom = Method->getInstantiatedFromMemberFunction(); |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 4306 | MSInfo = Method->getMemberSpecializationInfo(); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4307 | break; |
| 4308 | } |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 4309 | } |
| 4310 | } |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4311 | } else if (isa<VarDecl>(Member)) { |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 4312 | VarDecl *PrevVar; |
| 4313 | if (Previous.isSingleResult() && |
| 4314 | (PrevVar = dyn_cast<VarDecl>(Previous.getFoundDecl()))) |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4315 | if (PrevVar->isStaticDataMember()) { |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 4316 | Instantiation = PrevVar; |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4317 | InstantiatedFrom = PrevVar->getInstantiatedFromStaticDataMember(); |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 4318 | MSInfo = PrevVar->getMemberSpecializationInfo(); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4319 | } |
| 4320 | } else if (isa<RecordDecl>(Member)) { |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 4321 | CXXRecordDecl *PrevRecord; |
| 4322 | if (Previous.isSingleResult() && |
| 4323 | (PrevRecord = dyn_cast<CXXRecordDecl>(Previous.getFoundDecl()))) { |
| 4324 | Instantiation = PrevRecord; |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4325 | InstantiatedFrom = PrevRecord->getInstantiatedFromMemberClass(); |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 4326 | MSInfo = PrevRecord->getMemberSpecializationInfo(); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4327 | } |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 4328 | } |
| 4329 | |
| 4330 | if (!Instantiation) { |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4331 | // There is no previous declaration that matches. Since member |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 4332 | // specializations are always out-of-line, the caller will complain about |
| 4333 | // this mismatch later. |
| 4334 | return false; |
| 4335 | } |
| 4336 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4337 | // Make sure that this is a specialization of a member. |
| 4338 | if (!InstantiatedFrom) { |
| 4339 | Diag(Member->getLocation(), diag::err_spec_member_not_instantiated) |
| 4340 | << Member; |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 4341 | Diag(Instantiation->getLocation(), diag::note_specialized_decl); |
| 4342 | return true; |
| 4343 | } |
| 4344 | |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 4345 | // C++ [temp.expl.spec]p6: |
| 4346 | // If a template, a member template or the member of a class template is |
| 4347 | // explicitly specialized then that spe- cialization shall be declared |
| 4348 | // before the first use of that specialization that would cause an implicit |
| 4349 | // instantiation to take place, in every translation unit in which such a |
| 4350 | // use occurs; no diagnostic is required. |
| 4351 | assert(MSInfo && "Member specialization info missing?"); |
John McCall | 7504239 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 4352 | |
| 4353 | bool SuppressNew = false; |
| 4354 | if (CheckSpecializationInstantiationRedecl(Member->getLocation(), |
| 4355 | TSK_ExplicitSpecialization, |
| 4356 | Instantiation, |
| 4357 | MSInfo->getTemplateSpecializationKind(), |
| 4358 | MSInfo->getPointOfInstantiation(), |
| 4359 | SuppressNew)) |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 4360 | return true; |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 4361 | |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 4362 | // Check the scope of this explicit specialization. |
| 4363 | if (CheckTemplateSpecializationScope(*this, |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4364 | InstantiatedFrom, |
| 4365 | Instantiation, Member->getLocation(), |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4366 | false)) |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 4367 | return true; |
Douglas Gregor | 2db3232 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 4368 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4369 | // Note that this is an explicit instantiation of a member. |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 4370 | // the original declaration to note that it is an explicit specialization |
| 4371 | // (if it was previously an implicit instantiation). This latter step |
| 4372 | // makes bookkeeping easier. |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4373 | if (isa<FunctionDecl>(Member)) { |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 4374 | FunctionDecl *InstantiationFunction = cast<FunctionDecl>(Instantiation); |
| 4375 | if (InstantiationFunction->getTemplateSpecializationKind() == |
| 4376 | TSK_ImplicitInstantiation) { |
| 4377 | InstantiationFunction->setTemplateSpecializationKind( |
| 4378 | TSK_ExplicitSpecialization); |
| 4379 | InstantiationFunction->setLocation(Member->getLocation()); |
| 4380 | } |
| 4381 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4382 | cast<FunctionDecl>(Member)->setInstantiationOfMemberFunction( |
| 4383 | cast<CXXMethodDecl>(InstantiatedFrom), |
| 4384 | TSK_ExplicitSpecialization); |
| 4385 | } else if (isa<VarDecl>(Member)) { |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 4386 | VarDecl *InstantiationVar = cast<VarDecl>(Instantiation); |
| 4387 | if (InstantiationVar->getTemplateSpecializationKind() == |
| 4388 | TSK_ImplicitInstantiation) { |
| 4389 | InstantiationVar->setTemplateSpecializationKind( |
| 4390 | TSK_ExplicitSpecialization); |
| 4391 | InstantiationVar->setLocation(Member->getLocation()); |
| 4392 | } |
| 4393 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4394 | Context.setInstantiatedFromStaticDataMember(cast<VarDecl>(Member), |
| 4395 | cast<VarDecl>(InstantiatedFrom), |
| 4396 | TSK_ExplicitSpecialization); |
| 4397 | } else { |
| 4398 | assert(isa<CXXRecordDecl>(Member) && "Only member classes remain"); |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 4399 | CXXRecordDecl *InstantiationClass = cast<CXXRecordDecl>(Instantiation); |
| 4400 | if (InstantiationClass->getTemplateSpecializationKind() == |
| 4401 | TSK_ImplicitInstantiation) { |
| 4402 | InstantiationClass->setTemplateSpecializationKind( |
| 4403 | TSK_ExplicitSpecialization); |
| 4404 | InstantiationClass->setLocation(Member->getLocation()); |
| 4405 | } |
| 4406 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4407 | cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass( |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 4408 | cast<CXXRecordDecl>(InstantiatedFrom), |
| 4409 | TSK_ExplicitSpecialization); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4410 | } |
| 4411 | |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 4412 | // Save the caller the trouble of having to figure out which declaration |
| 4413 | // this specialization matches. |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 4414 | Previous.clear(); |
| 4415 | Previous.addDecl(Instantiation); |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 4416 | return false; |
| 4417 | } |
| 4418 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 4419 | /// \brief Check the scope of an explicit instantiation. |
| 4420 | static void CheckExplicitInstantiationScope(Sema &S, NamedDecl *D, |
| 4421 | SourceLocation InstLoc, |
| 4422 | bool WasQualifiedName) { |
| 4423 | DeclContext *ExpectedContext |
| 4424 | = D->getDeclContext()->getEnclosingNamespaceContext()->getLookupContext(); |
| 4425 | DeclContext *CurContext = S.CurContext->getLookupContext(); |
| 4426 | |
| 4427 | // C++0x [temp.explicit]p2: |
| 4428 | // An explicit instantiation shall appear in an enclosing namespace of its |
| 4429 | // template. |
| 4430 | // |
| 4431 | // This is DR275, which we do not retroactively apply to C++98/03. |
| 4432 | if (S.getLangOptions().CPlusPlus0x && |
| 4433 | !CurContext->Encloses(ExpectedContext)) { |
| 4434 | if (NamespaceDecl *NS = dyn_cast<NamespaceDecl>(ExpectedContext)) |
| 4435 | S.Diag(InstLoc, diag::err_explicit_instantiation_out_of_scope) |
| 4436 | << D << NS; |
| 4437 | else |
| 4438 | S.Diag(InstLoc, diag::err_explicit_instantiation_must_be_global) |
| 4439 | << D; |
| 4440 | S.Diag(D->getLocation(), diag::note_explicit_instantiation_here); |
| 4441 | return; |
| 4442 | } |
| 4443 | |
| 4444 | // C++0x [temp.explicit]p2: |
| 4445 | // If the name declared in the explicit instantiation is an unqualified |
| 4446 | // name, the explicit instantiation shall appear in the namespace where |
| 4447 | // its template is declared or, if that namespace is inline (7.3.1), any |
| 4448 | // namespace from its enclosing namespace set. |
| 4449 | if (WasQualifiedName) |
| 4450 | return; |
| 4451 | |
| 4452 | if (CurContext->Equals(ExpectedContext)) |
| 4453 | return; |
| 4454 | |
| 4455 | S.Diag(InstLoc, diag::err_explicit_instantiation_unqualified_wrong_namespace) |
| 4456 | << D << ExpectedContext; |
| 4457 | S.Diag(D->getLocation(), diag::note_explicit_instantiation_here); |
| 4458 | } |
| 4459 | |
| 4460 | /// \brief Determine whether the given scope specifier has a template-id in it. |
| 4461 | static bool ScopeSpecifierHasTemplateId(const CXXScopeSpec &SS) { |
| 4462 | if (!SS.isSet()) |
| 4463 | return false; |
| 4464 | |
| 4465 | // C++0x [temp.explicit]p2: |
| 4466 | // If the explicit instantiation is for a member function, a member class |
| 4467 | // or a static data member of a class template specialization, the name of |
| 4468 | // the class template specialization in the qualified-id for the member |
| 4469 | // name shall be a simple-template-id. |
| 4470 | // |
| 4471 | // C++98 has the same restriction, just worded differently. |
| 4472 | for (NestedNameSpecifier *NNS = (NestedNameSpecifier *)SS.getScopeRep(); |
| 4473 | NNS; NNS = NNS->getPrefix()) |
| 4474 | if (Type *T = NNS->getAsType()) |
| 4475 | if (isa<TemplateSpecializationType>(T)) |
| 4476 | return true; |
| 4477 | |
| 4478 | return false; |
| 4479 | } |
| 4480 | |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 4481 | // Explicit instantiation of a class template specialization |
Douglas Gregor | 45f9655 | 2009-09-04 06:33:52 +0000 | [diff] [blame] | 4482 | // FIXME: Implement extern template semantics |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4483 | Sema::DeclResult |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4484 | Sema::ActOnExplicitInstantiation(Scope *S, |
Douglas Gregor | 45f9655 | 2009-09-04 06:33:52 +0000 | [diff] [blame] | 4485 | SourceLocation ExternLoc, |
| 4486 | SourceLocation TemplateLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4487 | unsigned TagSpec, |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4488 | SourceLocation KWLoc, |
| 4489 | const CXXScopeSpec &SS, |
| 4490 | TemplateTy TemplateD, |
| 4491 | SourceLocation TemplateNameLoc, |
| 4492 | SourceLocation LAngleLoc, |
| 4493 | ASTTemplateArgsPtr TemplateArgsIn, |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4494 | SourceLocation RAngleLoc, |
| 4495 | AttributeList *Attr) { |
| 4496 | // Find the class template we're specializing |
| 4497 | TemplateName Name = TemplateD.getAsVal<TemplateName>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4498 | ClassTemplateDecl *ClassTemplate |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4499 | = cast<ClassTemplateDecl>(Name.getAsTemplateDecl()); |
| 4500 | |
| 4501 | // Check that the specialization uses the same tag kind as the |
| 4502 | // original template. |
| 4503 | TagDecl::TagKind Kind; |
| 4504 | switch (TagSpec) { |
| 4505 | default: assert(0 && "Unknown tag type!"); |
| 4506 | case DeclSpec::TST_struct: Kind = TagDecl::TK_struct; break; |
| 4507 | case DeclSpec::TST_union: Kind = TagDecl::TK_union; break; |
| 4508 | case DeclSpec::TST_class: Kind = TagDecl::TK_class; break; |
| 4509 | } |
Douglas Gregor | 501c5ce | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 4510 | if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4511 | Kind, KWLoc, |
Douglas Gregor | 501c5ce | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 4512 | *ClassTemplate->getIdentifier())) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4513 | Diag(KWLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4514 | << ClassTemplate |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 4515 | << FixItHint::CreateReplacement(KWLoc, |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4516 | ClassTemplate->getTemplatedDecl()->getKindName()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4517 | Diag(ClassTemplate->getTemplatedDecl()->getLocation(), |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4518 | diag::note_previous_use); |
| 4519 | Kind = ClassTemplate->getTemplatedDecl()->getTagKind(); |
| 4520 | } |
| 4521 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 4522 | // C++0x [temp.explicit]p2: |
| 4523 | // There are two forms of explicit instantiation: an explicit instantiation |
| 4524 | // definition and an explicit instantiation declaration. An explicit |
| 4525 | // instantiation declaration begins with the extern keyword. [...] |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4526 | TemplateSpecializationKind TSK |
| 4527 | = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition |
| 4528 | : TSK_ExplicitInstantiationDeclaration; |
| 4529 | |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4530 | // Translate the parser's template argument list in our AST format. |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 4531 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
Douglas Gregor | 314b97f | 2009-11-10 19:49:08 +0000 | [diff] [blame] | 4532 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4533 | |
| 4534 | // Check that the template argument list is well-formed for this |
| 4535 | // template. |
Anders Carlsson | fb25052 | 2009-06-23 01:26:57 +0000 | [diff] [blame] | 4536 | TemplateArgumentListBuilder Converted(ClassTemplate->getTemplateParameters(), |
| 4537 | TemplateArgs.size()); |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 4538 | if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc, |
| 4539 | TemplateArgs, false, Converted)) |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4540 | return true; |
| 4541 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4542 | assert((Converted.structuredSize() == |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4543 | ClassTemplate->getTemplateParameters()->size()) && |
| 4544 | "Converted template argument list is too short!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4545 | |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4546 | // Find the class template specialization declaration that |
| 4547 | // corresponds to these arguments. |
| 4548 | llvm::FoldingSetNodeID ID; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4549 | ClassTemplateSpecializationDecl::Profile(ID, |
Anders Carlsson | fb25052 | 2009-06-23 01:26:57 +0000 | [diff] [blame] | 4550 | Converted.getFlatArguments(), |
Douglas Gregor | 828e226 | 2009-07-29 16:09:57 +0000 | [diff] [blame] | 4551 | Converted.flatSize(), |
| 4552 | Context); |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4553 | void *InsertPos = 0; |
| 4554 | ClassTemplateSpecializationDecl *PrevDecl |
| 4555 | = ClassTemplate->getSpecializations().FindNodeOrInsertPos(ID, InsertPos); |
| 4556 | |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4557 | // C++0x [temp.explicit]p2: |
| 4558 | // [...] An explicit instantiation shall appear in an enclosing |
| 4559 | // namespace of its template. [...] |
| 4560 | // |
| 4561 | // This is C++ DR 275. |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 4562 | CheckExplicitInstantiationScope(*this, ClassTemplate, TemplateNameLoc, |
| 4563 | SS.isSet()); |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4564 | |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4565 | ClassTemplateSpecializationDecl *Specialization = 0; |
| 4566 | |
Douglas Gregor | d78f598 | 2009-11-25 06:01:46 +0000 | [diff] [blame] | 4567 | bool ReusedDecl = false; |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4568 | if (PrevDecl) { |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 4569 | bool SuppressNew = false; |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 4570 | if (CheckSpecializationInstantiationRedecl(TemplateNameLoc, TSK, |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 4571 | PrevDecl, |
| 4572 | PrevDecl->getSpecializationKind(), |
| 4573 | PrevDecl->getPointOfInstantiation(), |
| 4574 | SuppressNew)) |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4575 | return DeclPtrTy::make(PrevDecl); |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4576 | |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 4577 | if (SuppressNew) |
Douglas Gregor | 52604ab | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 4578 | return DeclPtrTy::make(PrevDecl); |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 4579 | |
Douglas Gregor | 52604ab | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 4580 | if (PrevDecl->getSpecializationKind() == TSK_ImplicitInstantiation || |
| 4581 | PrevDecl->getSpecializationKind() == TSK_Undeclared) { |
| 4582 | // Since the only prior class template specialization with these |
| 4583 | // arguments was referenced but not declared, reuse that |
| 4584 | // declaration node as our own, updating its source location to |
| 4585 | // reflect our new declaration. |
| 4586 | Specialization = PrevDecl; |
| 4587 | Specialization->setLocation(TemplateNameLoc); |
| 4588 | PrevDecl = 0; |
Douglas Gregor | d78f598 | 2009-11-25 06:01:46 +0000 | [diff] [blame] | 4589 | ReusedDecl = true; |
Douglas Gregor | 52604ab | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 4590 | } |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 4591 | } |
Douglas Gregor | 52604ab | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 4592 | |
| 4593 | if (!Specialization) { |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4594 | // Create a new class template specialization declaration node for |
| 4595 | // this explicit specialization. |
| 4596 | Specialization |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4597 | = ClassTemplateSpecializationDecl::Create(Context, |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4598 | ClassTemplate->getDeclContext(), |
| 4599 | TemplateNameLoc, |
| 4600 | ClassTemplate, |
Douglas Gregor | 52604ab | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 4601 | Converted, PrevDecl); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 4602 | SetNestedNameSpecifier(Specialization, SS); |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4603 | |
Douglas Gregor | 52604ab | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 4604 | if (PrevDecl) { |
| 4605 | // Remove the previous declaration from the folding set, since we want |
| 4606 | // to introduce a new declaration. |
| 4607 | ClassTemplate->getSpecializations().RemoveNode(PrevDecl); |
| 4608 | ClassTemplate->getSpecializations().FindNodeOrInsertPos(ID, InsertPos); |
| 4609 | } |
| 4610 | |
| 4611 | // Insert the new specialization. |
| 4612 | ClassTemplate->getSpecializations().InsertNode(Specialization, InsertPos); |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4613 | } |
| 4614 | |
| 4615 | // Build the fully-sugared type for this explicit instantiation as |
| 4616 | // the user wrote in the explicit instantiation itself. This means |
| 4617 | // that we'll pretty-print the type retrieved from the |
| 4618 | // specialization's declaration the way that the user actually wrote |
| 4619 | // the explicit instantiation, rather than formatting the name based |
| 4620 | // on the "canonical" representation used to store the template |
| 4621 | // arguments in the specialization. |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 4622 | TypeSourceInfo *WrittenTy |
| 4623 | = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc, |
| 4624 | TemplateArgs, |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4625 | Context.getTypeDeclType(Specialization)); |
| 4626 | Specialization->setTypeAsWritten(WrittenTy); |
| 4627 | TemplateArgsIn.release(); |
| 4628 | |
Douglas Gregor | d78f598 | 2009-11-25 06:01:46 +0000 | [diff] [blame] | 4629 | if (!ReusedDecl) { |
| 4630 | // Add the explicit instantiation into its lexical context. However, |
| 4631 | // since explicit instantiations are never found by name lookup, we |
| 4632 | // just put it into the declaration context directly. |
| 4633 | Specialization->setLexicalDeclContext(CurContext); |
| 4634 | CurContext->addDecl(Specialization); |
| 4635 | } |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4636 | |
| 4637 | // C++ [temp.explicit]p3: |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4638 | // A definition of a class template or class member template |
| 4639 | // shall be in scope at the point of the explicit instantiation of |
| 4640 | // the class template or class member template. |
| 4641 | // |
| 4642 | // This check comes when we actually try to perform the |
| 4643 | // instantiation. |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 4644 | ClassTemplateSpecializationDecl *Def |
| 4645 | = cast_or_null<ClassTemplateSpecializationDecl>( |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 4646 | Specialization->getDefinition()); |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 4647 | if (!Def) |
Douglas Gregor | 972e6ce | 2009-10-27 06:26:26 +0000 | [diff] [blame] | 4648 | InstantiateClassTemplateSpecialization(TemplateNameLoc, Specialization, TSK); |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 4649 | |
| 4650 | // Instantiate the members of this class template specialization. |
| 4651 | Def = cast_or_null<ClassTemplateSpecializationDecl>( |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 4652 | Specialization->getDefinition()); |
Rafael Espindola | b0f65ca | 2010-03-22 23:12:48 +0000 | [diff] [blame] | 4653 | if (Def) { |
Rafael Espindola | f075b22 | 2010-03-23 19:55:22 +0000 | [diff] [blame] | 4654 | TemplateSpecializationKind Old_TSK = Def->getTemplateSpecializationKind(); |
| 4655 | |
| 4656 | // Fix a TSK_ExplicitInstantiationDeclaration followed by a |
| 4657 | // TSK_ExplicitInstantiationDefinition |
| 4658 | if (Old_TSK == TSK_ExplicitInstantiationDeclaration && |
| 4659 | TSK == TSK_ExplicitInstantiationDefinition) |
| 4660 | Def->setTemplateSpecializationKind(TSK); |
Rafael Espindola | b0f65ca | 2010-03-22 23:12:48 +0000 | [diff] [blame] | 4661 | |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 4662 | InstantiateClassTemplateSpecializationMembers(TemplateNameLoc, Def, TSK); |
Rafael Espindola | b0f65ca | 2010-03-22 23:12:48 +0000 | [diff] [blame] | 4663 | } |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4664 | |
| 4665 | return DeclPtrTy::make(Specialization); |
| 4666 | } |
| 4667 | |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 4668 | // Explicit instantiation of a member class of a class template. |
| 4669 | Sema::DeclResult |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4670 | Sema::ActOnExplicitInstantiation(Scope *S, |
Douglas Gregor | 45f9655 | 2009-09-04 06:33:52 +0000 | [diff] [blame] | 4671 | SourceLocation ExternLoc, |
| 4672 | SourceLocation TemplateLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4673 | unsigned TagSpec, |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 4674 | SourceLocation KWLoc, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 4675 | CXXScopeSpec &SS, |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 4676 | IdentifierInfo *Name, |
| 4677 | SourceLocation NameLoc, |
| 4678 | AttributeList *Attr) { |
| 4679 | |
Douglas Gregor | 402abb5 | 2009-05-28 23:31:59 +0000 | [diff] [blame] | 4680 | bool Owned = false; |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 4681 | bool IsDependent = false; |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 4682 | DeclPtrTy TagD = ActOnTag(S, TagSpec, Action::TUK_Reference, |
Douglas Gregor | 7cdbc58 | 2009-07-22 23:48:44 +0000 | [diff] [blame] | 4683 | KWLoc, SS, Name, NameLoc, Attr, AS_none, |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 4684 | MultiTemplateParamsArg(*this, 0, 0), |
| 4685 | Owned, IsDependent); |
| 4686 | assert(!IsDependent && "explicit instantiation of dependent name not yet handled"); |
| 4687 | |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 4688 | if (!TagD) |
| 4689 | return true; |
| 4690 | |
| 4691 | TagDecl *Tag = cast<TagDecl>(TagD.getAs<Decl>()); |
| 4692 | if (Tag->isEnum()) { |
| 4693 | Diag(TemplateLoc, diag::err_explicit_instantiation_enum) |
| 4694 | << Context.getTypeDeclType(Tag); |
| 4695 | return true; |
| 4696 | } |
| 4697 | |
Douglas Gregor | d0c8737 | 2009-05-27 17:30:49 +0000 | [diff] [blame] | 4698 | if (Tag->isInvalidDecl()) |
| 4699 | return true; |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 4700 | |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 4701 | CXXRecordDecl *Record = cast<CXXRecordDecl>(Tag); |
| 4702 | CXXRecordDecl *Pattern = Record->getInstantiatedFromMemberClass(); |
| 4703 | if (!Pattern) { |
| 4704 | Diag(TemplateLoc, diag::err_explicit_instantiation_nontemplate_type) |
| 4705 | << Context.getTypeDeclType(Record); |
| 4706 | Diag(Record->getLocation(), diag::note_nontemplate_decl_here); |
| 4707 | return true; |
| 4708 | } |
| 4709 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 4710 | // C++0x [temp.explicit]p2: |
| 4711 | // If the explicit instantiation is for a class or member class, the |
| 4712 | // elaborated-type-specifier in the declaration shall include a |
| 4713 | // simple-template-id. |
| 4714 | // |
| 4715 | // C++98 has the same restriction, just worded differently. |
| 4716 | if (!ScopeSpecifierHasTemplateId(SS)) |
| 4717 | Diag(TemplateLoc, diag::err_explicit_instantiation_without_qualified_id) |
| 4718 | << Record << SS.getRange(); |
| 4719 | |
| 4720 | // C++0x [temp.explicit]p2: |
| 4721 | // There are two forms of explicit instantiation: an explicit instantiation |
| 4722 | // definition and an explicit instantiation declaration. An explicit |
| 4723 | // instantiation declaration begins with the extern keyword. [...] |
Douglas Gregor | a74bbe2 | 2009-10-14 21:46:58 +0000 | [diff] [blame] | 4724 | TemplateSpecializationKind TSK |
| 4725 | = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition |
| 4726 | : TSK_ExplicitInstantiationDeclaration; |
| 4727 | |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 4728 | // C++0x [temp.explicit]p2: |
| 4729 | // [...] An explicit instantiation shall appear in an enclosing |
| 4730 | // namespace of its template. [...] |
| 4731 | // |
| 4732 | // This is C++ DR 275. |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 4733 | CheckExplicitInstantiationScope(*this, Record, NameLoc, true); |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4734 | |
| 4735 | // Verify that it is okay to explicitly instantiate here. |
Douglas Gregor | 583f33b | 2009-10-15 18:07:02 +0000 | [diff] [blame] | 4736 | CXXRecordDecl *PrevDecl |
| 4737 | = cast_or_null<CXXRecordDecl>(Record->getPreviousDeclaration()); |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 4738 | if (!PrevDecl && Record->getDefinition()) |
Douglas Gregor | 583f33b | 2009-10-15 18:07:02 +0000 | [diff] [blame] | 4739 | PrevDecl = Record; |
| 4740 | if (PrevDecl) { |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4741 | MemberSpecializationInfo *MSInfo = PrevDecl->getMemberSpecializationInfo(); |
| 4742 | bool SuppressNew = false; |
| 4743 | assert(MSInfo && "No member specialization information?"); |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 4744 | if (CheckSpecializationInstantiationRedecl(TemplateLoc, TSK, |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4745 | PrevDecl, |
| 4746 | MSInfo->getTemplateSpecializationKind(), |
| 4747 | MSInfo->getPointOfInstantiation(), |
| 4748 | SuppressNew)) |
| 4749 | return true; |
| 4750 | if (SuppressNew) |
| 4751 | return TagD; |
| 4752 | } |
| 4753 | |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 4754 | CXXRecordDecl *RecordDef |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 4755 | = cast_or_null<CXXRecordDecl>(Record->getDefinition()); |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 4756 | if (!RecordDef) { |
Douglas Gregor | bf7643e | 2009-10-15 12:53:22 +0000 | [diff] [blame] | 4757 | // C++ [temp.explicit]p3: |
| 4758 | // A definition of a member class of a class template shall be in scope |
| 4759 | // at the point of an explicit instantiation of the member class. |
| 4760 | CXXRecordDecl *Def |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 4761 | = cast_or_null<CXXRecordDecl>(Pattern->getDefinition()); |
Douglas Gregor | bf7643e | 2009-10-15 12:53:22 +0000 | [diff] [blame] | 4762 | if (!Def) { |
Douglas Gregor | e2d3a3d | 2009-10-15 14:05:49 +0000 | [diff] [blame] | 4763 | Diag(TemplateLoc, diag::err_explicit_instantiation_undefined_member) |
| 4764 | << 0 << Record->getDeclName() << Record->getDeclContext(); |
Douglas Gregor | bf7643e | 2009-10-15 12:53:22 +0000 | [diff] [blame] | 4765 | Diag(Pattern->getLocation(), diag::note_forward_declaration) |
| 4766 | << Pattern; |
| 4767 | return true; |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 4768 | } else { |
| 4769 | if (InstantiateClass(NameLoc, Record, Def, |
| 4770 | getTemplateInstantiationArgs(Record), |
| 4771 | TSK)) |
| 4772 | return true; |
| 4773 | |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 4774 | RecordDef = cast_or_null<CXXRecordDecl>(Record->getDefinition()); |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 4775 | if (!RecordDef) |
| 4776 | return true; |
| 4777 | } |
| 4778 | } |
| 4779 | |
| 4780 | // Instantiate all of the members of the class. |
| 4781 | InstantiateClassMembers(NameLoc, RecordDef, |
| 4782 | getTemplateInstantiationArgs(Record), TSK); |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 4783 | |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 4784 | // FIXME: We don't have any representation for explicit instantiations of |
| 4785 | // member classes. Such a representation is not needed for compilation, but it |
| 4786 | // should be available for clients that want to see all of the declarations in |
| 4787 | // the source code. |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 4788 | return TagD; |
| 4789 | } |
| 4790 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4791 | Sema::DeclResult Sema::ActOnExplicitInstantiation(Scope *S, |
| 4792 | SourceLocation ExternLoc, |
| 4793 | SourceLocation TemplateLoc, |
| 4794 | Declarator &D) { |
| 4795 | // Explicit instantiations always require a name. |
| 4796 | DeclarationName Name = GetNameForDeclarator(D); |
| 4797 | if (!Name) { |
| 4798 | if (!D.isInvalidType()) |
| 4799 | Diag(D.getDeclSpec().getSourceRange().getBegin(), |
| 4800 | diag::err_explicit_instantiation_requires_name) |
| 4801 | << D.getDeclSpec().getSourceRange() |
| 4802 | << D.getSourceRange(); |
| 4803 | |
| 4804 | return true; |
| 4805 | } |
| 4806 | |
| 4807 | // The scope passed in may not be a decl scope. Zip up the scope tree until |
| 4808 | // we find one that is. |
| 4809 | while ((S->getFlags() & Scope::DeclScope) == 0 || |
| 4810 | (S->getFlags() & Scope::TemplateParamScope) != 0) |
| 4811 | S = S->getParent(); |
| 4812 | |
| 4813 | // Determine the type of the declaration. |
| 4814 | QualType R = GetTypeForDeclarator(D, S, 0); |
| 4815 | if (R.isNull()) |
| 4816 | return true; |
| 4817 | |
| 4818 | if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef) { |
| 4819 | // Cannot explicitly instantiate a typedef. |
| 4820 | Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_of_typedef) |
| 4821 | << Name; |
| 4822 | return true; |
| 4823 | } |
| 4824 | |
Douglas Gregor | 663b5a0 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 4825 | // C++0x [temp.explicit]p1: |
| 4826 | // [...] An explicit instantiation of a function template shall not use the |
| 4827 | // inline or constexpr specifiers. |
| 4828 | // Presumably, this also applies to member functions of class templates as |
| 4829 | // well. |
| 4830 | if (D.getDeclSpec().isInlineSpecified() && getLangOptions().CPlusPlus0x) |
| 4831 | Diag(D.getDeclSpec().getInlineSpecLoc(), |
| 4832 | diag::err_explicit_instantiation_inline) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 4833 | <<FixItHint::CreateRemoval(D.getDeclSpec().getInlineSpecLoc()); |
Douglas Gregor | 663b5a0 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 4834 | |
| 4835 | // FIXME: check for constexpr specifier. |
| 4836 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 4837 | // C++0x [temp.explicit]p2: |
| 4838 | // There are two forms of explicit instantiation: an explicit instantiation |
| 4839 | // definition and an explicit instantiation declaration. An explicit |
| 4840 | // instantiation declaration begins with the extern keyword. [...] |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4841 | TemplateSpecializationKind TSK |
| 4842 | = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition |
| 4843 | : TSK_ExplicitInstantiationDeclaration; |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 4844 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 4845 | LookupResult Previous(*this, Name, D.getIdentifierLoc(), LookupOrdinaryName); |
| 4846 | LookupParsedName(Previous, S, &D.getCXXScopeSpec()); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4847 | |
| 4848 | if (!R->isFunctionType()) { |
| 4849 | // C++ [temp.explicit]p1: |
| 4850 | // A [...] static data member of a class template can be explicitly |
| 4851 | // instantiated from the member definition associated with its class |
| 4852 | // template. |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 4853 | if (Previous.isAmbiguous()) |
| 4854 | return true; |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4855 | |
John McCall | 1bcee0a | 2009-12-02 08:25:40 +0000 | [diff] [blame] | 4856 | VarDecl *Prev = Previous.getAsSingle<VarDecl>(); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4857 | if (!Prev || !Prev->isStaticDataMember()) { |
| 4858 | // We expect to see a data data member here. |
| 4859 | Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_not_known) |
| 4860 | << Name; |
| 4861 | for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end(); |
| 4862 | P != PEnd; ++P) |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 4863 | Diag((*P)->getLocation(), diag::note_explicit_instantiation_here); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4864 | return true; |
| 4865 | } |
| 4866 | |
| 4867 | if (!Prev->getInstantiatedFromStaticDataMember()) { |
| 4868 | // FIXME: Check for explicit specialization? |
| 4869 | Diag(D.getIdentifierLoc(), |
| 4870 | diag::err_explicit_instantiation_data_member_not_instantiated) |
| 4871 | << Prev; |
| 4872 | Diag(Prev->getLocation(), diag::note_explicit_instantiation_here); |
| 4873 | // FIXME: Can we provide a note showing where this was declared? |
| 4874 | return true; |
| 4875 | } |
| 4876 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 4877 | // C++0x [temp.explicit]p2: |
| 4878 | // If the explicit instantiation is for a member function, a member class |
| 4879 | // or a static data member of a class template specialization, the name of |
| 4880 | // the class template specialization in the qualified-id for the member |
| 4881 | // name shall be a simple-template-id. |
| 4882 | // |
| 4883 | // C++98 has the same restriction, just worded differently. |
| 4884 | if (!ScopeSpecifierHasTemplateId(D.getCXXScopeSpec())) |
| 4885 | Diag(D.getIdentifierLoc(), |
| 4886 | diag::err_explicit_instantiation_without_qualified_id) |
| 4887 | << Prev << D.getCXXScopeSpec().getRange(); |
| 4888 | |
| 4889 | // Check the scope of this explicit instantiation. |
| 4890 | CheckExplicitInstantiationScope(*this, Prev, D.getIdentifierLoc(), true); |
| 4891 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4892 | // Verify that it is okay to explicitly instantiate here. |
| 4893 | MemberSpecializationInfo *MSInfo = Prev->getMemberSpecializationInfo(); |
| 4894 | assert(MSInfo && "Missing static data member specialization info?"); |
| 4895 | bool SuppressNew = false; |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 4896 | if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK, Prev, |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4897 | MSInfo->getTemplateSpecializationKind(), |
| 4898 | MSInfo->getPointOfInstantiation(), |
| 4899 | SuppressNew)) |
| 4900 | return true; |
| 4901 | if (SuppressNew) |
| 4902 | return DeclPtrTy(); |
| 4903 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4904 | // Instantiate static data member. |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 4905 | Prev->setTemplateSpecializationKind(TSK, D.getIdentifierLoc()); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4906 | if (TSK == TSK_ExplicitInstantiationDefinition) |
Douglas Gregor | e2d3a3d | 2009-10-15 14:05:49 +0000 | [diff] [blame] | 4907 | InstantiateStaticDataMemberDefinition(D.getIdentifierLoc(), Prev, false, |
| 4908 | /*DefinitionRequired=*/true); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4909 | |
| 4910 | // FIXME: Create an ExplicitInstantiation node? |
| 4911 | return DeclPtrTy(); |
| 4912 | } |
| 4913 | |
Douglas Gregor | 0b60d9e | 2009-09-25 23:53:26 +0000 | [diff] [blame] | 4914 | // If the declarator is a template-id, translate the parser's template |
| 4915 | // argument list into our AST format. |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 4916 | bool HasExplicitTemplateArgs = false; |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 4917 | TemplateArgumentListInfo TemplateArgs; |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 4918 | if (D.getName().getKind() == UnqualifiedId::IK_TemplateId) { |
| 4919 | TemplateIdAnnotation *TemplateId = D.getName().TemplateId; |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 4920 | TemplateArgs.setLAngleLoc(TemplateId->LAngleLoc); |
| 4921 | TemplateArgs.setRAngleLoc(TemplateId->RAngleLoc); |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 4922 | ASTTemplateArgsPtr TemplateArgsPtr(*this, |
| 4923 | TemplateId->getTemplateArgs(), |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 4924 | TemplateId->NumArgs); |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 4925 | translateTemplateArguments(TemplateArgsPtr, TemplateArgs); |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 4926 | HasExplicitTemplateArgs = true; |
Douglas Gregor | b2f81cf | 2009-10-01 23:51:25 +0000 | [diff] [blame] | 4927 | TemplateArgsPtr.release(); |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 4928 | } |
Douglas Gregor | 0b60d9e | 2009-09-25 23:53:26 +0000 | [diff] [blame] | 4929 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4930 | // C++ [temp.explicit]p1: |
| 4931 | // A [...] function [...] can be explicitly instantiated from its template. |
| 4932 | // A member function [...] of a class template can be explicitly |
| 4933 | // instantiated from the member definition associated with its class |
| 4934 | // template. |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4935 | UnresolvedSet<8> Matches; |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4936 | for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end(); |
| 4937 | P != PEnd; ++P) { |
| 4938 | NamedDecl *Prev = *P; |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 4939 | if (!HasExplicitTemplateArgs) { |
| 4940 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Prev)) { |
| 4941 | if (Context.hasSameUnqualifiedType(Method->getType(), R)) { |
| 4942 | Matches.clear(); |
Douglas Gregor | 48026d2 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 4943 | |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4944 | Matches.addDecl(Method, P.getAccess()); |
Douglas Gregor | 48026d2 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 4945 | if (Method->getTemplateSpecializationKind() == TSK_Undeclared) |
| 4946 | break; |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 4947 | } |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4948 | } |
| 4949 | } |
| 4950 | |
| 4951 | FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Prev); |
| 4952 | if (!FunTmpl) |
| 4953 | continue; |
| 4954 | |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 4955 | TemplateDeductionInfo Info(Context, D.getIdentifierLoc()); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4956 | FunctionDecl *Specialization = 0; |
| 4957 | if (TemplateDeductionResult TDK |
Douglas Gregor | 48026d2 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 4958 | = DeduceTemplateArguments(FunTmpl, |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 4959 | (HasExplicitTemplateArgs ? &TemplateArgs : 0), |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4960 | R, Specialization, Info)) { |
| 4961 | // FIXME: Keep track of almost-matches? |
| 4962 | (void)TDK; |
| 4963 | continue; |
| 4964 | } |
| 4965 | |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4966 | Matches.addDecl(Specialization, P.getAccess()); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4967 | } |
| 4968 | |
| 4969 | // Find the most specialized function template specialization. |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4970 | UnresolvedSetIterator Result |
| 4971 | = getMostSpecialized(Matches.begin(), Matches.end(), TPOC_Other, |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4972 | D.getIdentifierLoc(), |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 4973 | PDiag(diag::err_explicit_instantiation_not_known) << Name, |
| 4974 | PDiag(diag::err_explicit_instantiation_ambiguous) << Name, |
| 4975 | PDiag(diag::note_explicit_instantiation_candidate)); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4976 | |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4977 | if (Result == Matches.end()) |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4978 | return true; |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4979 | |
| 4980 | // Ignore access control bits, we don't need them for redeclaration checking. |
| 4981 | FunctionDecl *Specialization = cast<FunctionDecl>(*Result); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4982 | |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 4983 | if (Specialization->getTemplateSpecializationKind() == TSK_Undeclared) { |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4984 | Diag(D.getIdentifierLoc(), |
| 4985 | diag::err_explicit_instantiation_member_function_not_instantiated) |
| 4986 | << Specialization |
| 4987 | << (Specialization->getTemplateSpecializationKind() == |
| 4988 | TSK_ExplicitSpecialization); |
| 4989 | Diag(Specialization->getLocation(), diag::note_explicit_instantiation_here); |
| 4990 | return true; |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 4991 | } |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 4992 | |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 4993 | FunctionDecl *PrevDecl = Specialization->getPreviousDeclaration(); |
Douglas Gregor | 583f33b | 2009-10-15 18:07:02 +0000 | [diff] [blame] | 4994 | if (!PrevDecl && Specialization->isThisDeclarationADefinition()) |
| 4995 | PrevDecl = Specialization; |
| 4996 | |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 4997 | if (PrevDecl) { |
| 4998 | bool SuppressNew = false; |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 4999 | if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK, |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 5000 | PrevDecl, |
| 5001 | PrevDecl->getTemplateSpecializationKind(), |
| 5002 | PrevDecl->getPointOfInstantiation(), |
| 5003 | SuppressNew)) |
| 5004 | return true; |
| 5005 | |
| 5006 | // FIXME: We may still want to build some representation of this |
| 5007 | // explicit specialization. |
| 5008 | if (SuppressNew) |
| 5009 | return DeclPtrTy(); |
| 5010 | } |
Anders Carlsson | 26d6e9d | 2009-11-24 05:34:41 +0000 | [diff] [blame] | 5011 | |
| 5012 | Specialization->setTemplateSpecializationKind(TSK, D.getIdentifierLoc()); |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 5013 | |
| 5014 | if (TSK == TSK_ExplicitInstantiationDefinition) |
| 5015 | InstantiateFunctionDefinition(D.getIdentifierLoc(), Specialization, |
| 5016 | false, /*DefinitionRequired=*/true); |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 5017 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5018 | // C++0x [temp.explicit]p2: |
| 5019 | // If the explicit instantiation is for a member function, a member class |
| 5020 | // or a static data member of a class template specialization, the name of |
| 5021 | // the class template specialization in the qualified-id for the member |
| 5022 | // name shall be a simple-template-id. |
| 5023 | // |
| 5024 | // C++98 has the same restriction, just worded differently. |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 5025 | FunctionTemplateDecl *FunTmpl = Specialization->getPrimaryTemplate(); |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 5026 | if (D.getName().getKind() != UnqualifiedId::IK_TemplateId && !FunTmpl && |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5027 | D.getCXXScopeSpec().isSet() && |
| 5028 | !ScopeSpecifierHasTemplateId(D.getCXXScopeSpec())) |
| 5029 | Diag(D.getIdentifierLoc(), |
| 5030 | diag::err_explicit_instantiation_without_qualified_id) |
| 5031 | << Specialization << D.getCXXScopeSpec().getRange(); |
| 5032 | |
| 5033 | CheckExplicitInstantiationScope(*this, |
| 5034 | FunTmpl? (NamedDecl *)FunTmpl |
| 5035 | : Specialization->getInstantiatedFromMemberFunction(), |
| 5036 | D.getIdentifierLoc(), |
| 5037 | D.getCXXScopeSpec().isSet()); |
| 5038 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5039 | // FIXME: Create some kind of ExplicitInstantiationDecl here. |
| 5040 | return DeclPtrTy(); |
| 5041 | } |
| 5042 | |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5043 | Sema::TypeResult |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 5044 | Sema::ActOnDependentTag(Scope *S, unsigned TagSpec, TagUseKind TUK, |
| 5045 | const CXXScopeSpec &SS, IdentifierInfo *Name, |
| 5046 | SourceLocation TagLoc, SourceLocation NameLoc) { |
| 5047 | // This has to hold, because SS is expected to be defined. |
| 5048 | assert(Name && "Expected a name in a dependent tag"); |
| 5049 | |
| 5050 | NestedNameSpecifier *NNS |
| 5051 | = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); |
| 5052 | if (!NNS) |
| 5053 | return true; |
| 5054 | |
Daniel Dunbar | 12c0ade | 2010-04-01 16:50:48 +0000 | [diff] [blame] | 5055 | ElaboratedTypeKeyword Keyword = ETK_None; |
Douglas Gregor | 4033642 | 2010-03-31 22:19:08 +0000 | [diff] [blame] | 5056 | switch (TagDecl::getTagKindForTypeSpec(TagSpec)) { |
| 5057 | case TagDecl::TK_struct: Keyword = ETK_Struct; break; |
| 5058 | case TagDecl::TK_class: Keyword = ETK_Class; break; |
| 5059 | case TagDecl::TK_union: Keyword = ETK_Union; break; |
| 5060 | case TagDecl::TK_enum: Keyword = ETK_Enum; break; |
| 5061 | } |
Daniel Dunbar | 12c0ade | 2010-04-01 16:50:48 +0000 | [diff] [blame] | 5062 | assert(Keyword != ETK_None && "Invalid tag kind!"); |
| 5063 | |
Douglas Gregor | 4033642 | 2010-03-31 22:19:08 +0000 | [diff] [blame] | 5064 | return Context.getDependentNameType(Keyword, NNS, Name).getAsOpaquePtr(); |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 5065 | } |
| 5066 | |
| 5067 | Sema::TypeResult |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5068 | Sema::ActOnTypenameType(SourceLocation TypenameLoc, const CXXScopeSpec &SS, |
| 5069 | const IdentifierInfo &II, SourceLocation IdLoc) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5070 | NestedNameSpecifier *NNS |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5071 | = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); |
| 5072 | if (!NNS) |
| 5073 | return true; |
| 5074 | |
| 5075 | QualType T = CheckTypenameType(NNS, II, SourceRange(TypenameLoc, IdLoc)); |
Douglas Gregor | 31a19b6 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 5076 | if (T.isNull()) |
| 5077 | return true; |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5078 | return T.getAsOpaquePtr(); |
| 5079 | } |
| 5080 | |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 5081 | Sema::TypeResult |
| 5082 | Sema::ActOnTypenameType(SourceLocation TypenameLoc, const CXXScopeSpec &SS, |
| 5083 | SourceLocation TemplateLoc, TypeTy *Ty) { |
Argyrios Kyrtzidis | e866190 | 2009-08-19 01:28:28 +0000 | [diff] [blame] | 5084 | QualType T = GetTypeFromParser(Ty); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5085 | NestedNameSpecifier *NNS |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 5086 | = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5087 | const TemplateSpecializationType *TemplateId |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5088 | = T->getAs<TemplateSpecializationType>(); |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 5089 | assert(TemplateId && "Expected a template specialization type"); |
| 5090 | |
Douglas Gregor | 6946baf | 2009-09-02 13:05:45 +0000 | [diff] [blame] | 5091 | if (computeDeclContext(SS, false)) { |
| 5092 | // If we can compute a declaration context, then the "typename" |
| 5093 | // keyword was superfluous. Just build a QualifiedNameType to keep |
| 5094 | // track of the nested-name-specifier. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5095 | |
Douglas Gregor | 6946baf | 2009-09-02 13:05:45 +0000 | [diff] [blame] | 5096 | // FIXME: Note that the QualifiedNameType had the "typename" keyword! |
| 5097 | return Context.getQualifiedNameType(NNS, T).getAsOpaquePtr(); |
| 5098 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5099 | |
Douglas Gregor | 4a2023f | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 5100 | return Context.getDependentNameType(ETK_Typename, NNS, TemplateId) |
| 5101 | .getAsOpaquePtr(); |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 5102 | } |
| 5103 | |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5104 | /// \brief Build the type that describes a C++ typename specifier, |
| 5105 | /// e.g., "typename T::type". |
| 5106 | QualType |
| 5107 | Sema::CheckTypenameType(NestedNameSpecifier *NNS, const IdentifierInfo &II, |
| 5108 | SourceRange Range) { |
Douglas Gregor | 42af25f | 2009-05-11 19:58:34 +0000 | [diff] [blame] | 5109 | CXXRecordDecl *CurrentInstantiation = 0; |
| 5110 | if (NNS->isDependent()) { |
| 5111 | CurrentInstantiation = getCurrentInstantiationOf(NNS); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5112 | |
Douglas Gregor | 42af25f | 2009-05-11 19:58:34 +0000 | [diff] [blame] | 5113 | // If the nested-name-specifier does not refer to the current |
| 5114 | // instantiation, then build a typename type. |
| 5115 | if (!CurrentInstantiation) |
Douglas Gregor | 4a2023f | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 5116 | return Context.getDependentNameType(ETK_Typename, NNS, &II); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5117 | |
Douglas Gregor | de18d12 | 2009-09-02 13:12:51 +0000 | [diff] [blame] | 5118 | // The nested-name-specifier refers to the current instantiation, so the |
| 5119 | // "typename" keyword itself is superfluous. In C++03, the program is |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5120 | // actually ill-formed. However, DR 382 (in C++0x CD1) allows such |
Douglas Gregor | de18d12 | 2009-09-02 13:12:51 +0000 | [diff] [blame] | 5121 | // extraneous "typename" keywords, and we retroactively apply this DR to |
| 5122 | // C++03 code. |
Douglas Gregor | 42af25f | 2009-05-11 19:58:34 +0000 | [diff] [blame] | 5123 | } |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5124 | |
Douglas Gregor | 42af25f | 2009-05-11 19:58:34 +0000 | [diff] [blame] | 5125 | DeclContext *Ctx = 0; |
| 5126 | |
| 5127 | if (CurrentInstantiation) |
| 5128 | Ctx = CurrentInstantiation; |
| 5129 | else { |
| 5130 | CXXScopeSpec SS; |
| 5131 | SS.setScopeRep(NNS); |
| 5132 | SS.setRange(Range); |
| 5133 | if (RequireCompleteDeclContext(SS)) |
| 5134 | return QualType(); |
| 5135 | |
| 5136 | Ctx = computeDeclContext(SS); |
| 5137 | } |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5138 | assert(Ctx && "No declaration context?"); |
| 5139 | |
| 5140 | DeclarationName Name(&II); |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 5141 | LookupResult Result(*this, Name, Range.getEnd(), LookupOrdinaryName); |
| 5142 | LookupQualifiedName(Result, Ctx); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5143 | unsigned DiagID = 0; |
| 5144 | Decl *Referenced = 0; |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 5145 | switch (Result.getResultKind()) { |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5146 | case LookupResult::NotFound: |
Douglas Gregor | 3f09327 | 2009-10-13 21:16:44 +0000 | [diff] [blame] | 5147 | DiagID = diag::err_typename_nested_not_found; |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5148 | break; |
Douglas Gregor | 7d3f576 | 2010-01-15 01:44:47 +0000 | [diff] [blame] | 5149 | |
| 5150 | case LookupResult::NotFoundInCurrentInstantiation: |
| 5151 | // Okay, it's a member of an unknown instantiation. |
Douglas Gregor | 4a2023f | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 5152 | return Context.getDependentNameType(ETK_Typename, NNS, &II); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5153 | |
| 5154 | case LookupResult::Found: |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 5155 | if (TypeDecl *Type = dyn_cast<TypeDecl>(Result.getFoundDecl())) { |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5156 | // We found a type. Build a QualifiedNameType, since the |
| 5157 | // typename-specifier was just sugar. FIXME: Tell |
| 5158 | // QualifiedNameType that it has a "typename" prefix. |
| 5159 | return Context.getQualifiedNameType(NNS, Context.getTypeDeclType(Type)); |
| 5160 | } |
| 5161 | |
| 5162 | DiagID = diag::err_typename_nested_not_type; |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 5163 | Referenced = Result.getFoundDecl(); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5164 | break; |
| 5165 | |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 5166 | case LookupResult::FoundUnresolvedValue: |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 5167 | llvm_unreachable("unresolved using decl in non-dependent context"); |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 5168 | return QualType(); |
| 5169 | |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5170 | case LookupResult::FoundOverloaded: |
| 5171 | DiagID = diag::err_typename_nested_not_type; |
| 5172 | Referenced = *Result.begin(); |
| 5173 | break; |
| 5174 | |
John McCall | 6e24726 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 5175 | case LookupResult::Ambiguous: |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5176 | return QualType(); |
| 5177 | } |
| 5178 | |
| 5179 | // If we get here, it's because name lookup did not find a |
| 5180 | // type. Emit an appropriate diagnostic and return an error. |
Douglas Gregor | 3f09327 | 2009-10-13 21:16:44 +0000 | [diff] [blame] | 5181 | Diag(Range.getEnd(), DiagID) << Range << Name << Ctx; |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5182 | if (Referenced) |
| 5183 | Diag(Referenced->getLocation(), diag::note_typename_refers_here) |
| 5184 | << Name; |
| 5185 | return QualType(); |
| 5186 | } |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 5187 | |
| 5188 | namespace { |
| 5189 | // See Sema::RebuildTypeInCurrentInstantiation |
Benjamin Kramer | 85b4521 | 2009-11-28 19:45:26 +0000 | [diff] [blame] | 5190 | class CurrentInstantiationRebuilder |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5191 | : public TreeTransform<CurrentInstantiationRebuilder> { |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 5192 | SourceLocation Loc; |
| 5193 | DeclarationName Entity; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5194 | |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 5195 | public: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5196 | CurrentInstantiationRebuilder(Sema &SemaRef, |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 5197 | SourceLocation Loc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5198 | DeclarationName Entity) |
| 5199 | : TreeTransform<CurrentInstantiationRebuilder>(SemaRef), |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 5200 | Loc(Loc), Entity(Entity) { } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5201 | |
| 5202 | /// \brief Determine whether the given type \p T has already been |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 5203 | /// transformed. |
| 5204 | /// |
| 5205 | /// For the purposes of type reconstruction, a type has already been |
| 5206 | /// transformed if it is NULL or if it is not dependent. |
| 5207 | bool AlreadyTransformed(QualType T) { |
| 5208 | return T.isNull() || !T->isDependentType(); |
| 5209 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5210 | |
| 5211 | /// \brief Returns the location of the entity whose type is being |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 5212 | /// rebuilt. |
| 5213 | SourceLocation getBaseLocation() { return Loc; } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5214 | |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 5215 | /// \brief Returns the name of the entity whose type is being rebuilt. |
| 5216 | DeclarationName getBaseEntity() { return Entity; } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5217 | |
Douglas Gregor | 972e6ce | 2009-10-27 06:26:26 +0000 | [diff] [blame] | 5218 | /// \brief Sets the "base" location and entity when that |
| 5219 | /// information is known based on another transformation. |
| 5220 | void setBase(SourceLocation Loc, DeclarationName Entity) { |
| 5221 | this->Loc = Loc; |
| 5222 | this->Entity = Entity; |
| 5223 | } |
| 5224 | |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 5225 | /// \brief Transforms an expression by returning the expression itself |
| 5226 | /// (an identity function). |
| 5227 | /// |
| 5228 | /// FIXME: This is completely unsafe; we will need to actually clone the |
| 5229 | /// expressions. |
| 5230 | Sema::OwningExprResult TransformExpr(Expr *E) { |
| 5231 | return getSema().Owned(E); |
| 5232 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5233 | |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 5234 | /// \brief Transforms a typename type by determining whether the type now |
| 5235 | /// refers to a member of the current instantiation, and then |
| 5236 | /// type-checking and building a QualifiedNameType (when possible). |
Douglas Gregor | 4714c12 | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 5237 | QualType TransformDependentNameType(TypeLocBuilder &TLB, DependentNameTypeLoc TL, |
Douglas Gregor | 124b878 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 5238 | QualType ObjectType); |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 5239 | }; |
| 5240 | } |
| 5241 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5242 | QualType |
Douglas Gregor | 4714c12 | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 5243 | CurrentInstantiationRebuilder::TransformDependentNameType(TypeLocBuilder &TLB, |
| 5244 | DependentNameTypeLoc TL, |
Douglas Gregor | 124b878 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 5245 | QualType ObjectType) { |
Douglas Gregor | 4714c12 | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 5246 | DependentNameType *T = TL.getTypePtr(); |
John McCall | a2becad | 2009-10-21 00:40:46 +0000 | [diff] [blame] | 5247 | |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 5248 | NestedNameSpecifier *NNS |
| 5249 | = TransformNestedNameSpecifier(T->getQualifier(), |
Douglas Gregor | 124b878 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 5250 | /*FIXME:*/SourceRange(getBaseLocation()), |
| 5251 | ObjectType); |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 5252 | if (!NNS) |
| 5253 | return QualType(); |
| 5254 | |
| 5255 | // If the nested-name-specifier did not change, and we cannot compute the |
| 5256 | // context corresponding to the nested-name-specifier, then this |
| 5257 | // typename type will not change; exit early. |
| 5258 | CXXScopeSpec SS; |
| 5259 | SS.setRange(SourceRange(getBaseLocation())); |
| 5260 | SS.setScopeRep(NNS); |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 5261 | |
| 5262 | QualType Result; |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 5263 | if (NNS == T->getQualifier() && getSema().computeDeclContext(SS) == 0) |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 5264 | Result = QualType(T, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5265 | |
| 5266 | // Rebuild the typename type, which will probably turn into a |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 5267 | // QualifiedNameType. |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 5268 | else if (const TemplateSpecializationType *TemplateId = T->getTemplateId()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5269 | QualType NewTemplateId |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 5270 | = TransformType(QualType(TemplateId, 0)); |
| 5271 | if (NewTemplateId.isNull()) |
| 5272 | return QualType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5273 | |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 5274 | if (NNS == T->getQualifier() && |
| 5275 | NewTemplateId == QualType(TemplateId, 0)) |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 5276 | Result = QualType(T, 0); |
| 5277 | else |
Douglas Gregor | 4a2023f | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 5278 | Result = getDerived().RebuildDependentNameType(T->getKeyword(), |
| 5279 | NNS, NewTemplateId); |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 5280 | } else |
Douglas Gregor | 4a2023f | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 5281 | Result = getDerived().RebuildDependentNameType(T->getKeyword(), |
| 5282 | NNS, T->getIdentifier(), |
| 5283 | SourceRange(TL.getNameLoc())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5284 | |
Douglas Gregor | a50ce32 | 2010-03-07 23:26:22 +0000 | [diff] [blame] | 5285 | if (Result.isNull()) |
| 5286 | return QualType(); |
| 5287 | |
Douglas Gregor | 4714c12 | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 5288 | DependentNameTypeLoc NewTL = TLB.push<DependentNameTypeLoc>(Result); |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 5289 | NewTL.setNameLoc(TL.getNameLoc()); |
| 5290 | return Result; |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 5291 | } |
| 5292 | |
| 5293 | /// \brief Rebuilds a type within the context of the current instantiation. |
| 5294 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5295 | /// 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] | 5296 | /// a class template (or class template partial specialization) that was parsed |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5297 | /// and constructed before we entered the scope of the class template (or |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 5298 | /// partial specialization thereof). This routine will rebuild that type now |
| 5299 | /// that we have entered the declarator's scope, which may produce different |
| 5300 | /// canonical types, e.g., |
| 5301 | /// |
| 5302 | /// \code |
| 5303 | /// template<typename T> |
| 5304 | /// struct X { |
| 5305 | /// typedef T* pointer; |
| 5306 | /// pointer data(); |
| 5307 | /// }; |
| 5308 | /// |
| 5309 | /// template<typename T> |
| 5310 | /// typename X<T>::pointer X<T>::data() { ... } |
| 5311 | /// \endcode |
| 5312 | /// |
Douglas Gregor | 4714c12 | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 5313 | /// 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] | 5314 | /// since we do not know that we can look into X<T> when we parsed the type. |
| 5315 | /// This function will rebuild the type, performing the lookup of "pointer" |
| 5316 | /// in X<T> and returning a QualifiedNameType whose canonical type is the same |
| 5317 | /// as the canonical type of T*, allowing the return types of the out-of-line |
| 5318 | /// definition and the declaration to match. |
| 5319 | QualType Sema::RebuildTypeInCurrentInstantiation(QualType T, SourceLocation Loc, |
| 5320 | DeclarationName Name) { |
| 5321 | if (T.isNull() || !T->isDependentType()) |
| 5322 | return T; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5323 | |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 5324 | CurrentInstantiationRebuilder Rebuilder(*this, Loc, Name); |
| 5325 | return Rebuilder.TransformType(T); |
Benjamin Kramer | 27ba2f0 | 2009-08-11 22:33:06 +0000 | [diff] [blame] | 5326 | } |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 5327 | |
| 5328 | /// \brief Produces a formatted string that describes the binding of |
| 5329 | /// template parameters to template arguments. |
| 5330 | std::string |
| 5331 | Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params, |
| 5332 | const TemplateArgumentList &Args) { |
Douglas Gregor | 9148c3f | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 5333 | // FIXME: For variadic templates, we'll need to get the structured list. |
| 5334 | return getTemplateArgumentBindingsText(Params, Args.getFlatArgumentList(), |
| 5335 | Args.flat_size()); |
| 5336 | } |
| 5337 | |
| 5338 | std::string |
| 5339 | Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params, |
| 5340 | const TemplateArgument *Args, |
| 5341 | unsigned NumArgs) { |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 5342 | std::string Result; |
| 5343 | |
Douglas Gregor | 9148c3f | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 5344 | if (!Params || Params->size() == 0 || NumArgs == 0) |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 5345 | return Result; |
| 5346 | |
| 5347 | for (unsigned I = 0, N = Params->size(); I != N; ++I) { |
Douglas Gregor | 9148c3f | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 5348 | if (I >= NumArgs) |
| 5349 | break; |
| 5350 | |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 5351 | if (I == 0) |
| 5352 | Result += "[with "; |
| 5353 | else |
| 5354 | Result += ", "; |
| 5355 | |
| 5356 | if (const IdentifierInfo *Id = Params->getParam(I)->getIdentifier()) { |
| 5357 | Result += Id->getName(); |
| 5358 | } else { |
| 5359 | Result += '$'; |
| 5360 | Result += llvm::utostr(I); |
| 5361 | } |
| 5362 | |
| 5363 | Result += " = "; |
| 5364 | |
| 5365 | switch (Args[I].getKind()) { |
| 5366 | case TemplateArgument::Null: |
| 5367 | Result += "<no value>"; |
| 5368 | break; |
| 5369 | |
| 5370 | case TemplateArgument::Type: { |
| 5371 | std::string TypeStr; |
| 5372 | Args[I].getAsType().getAsStringInternal(TypeStr, |
| 5373 | Context.PrintingPolicy); |
| 5374 | Result += TypeStr; |
| 5375 | break; |
| 5376 | } |
| 5377 | |
| 5378 | case TemplateArgument::Declaration: { |
| 5379 | bool Unnamed = true; |
| 5380 | if (NamedDecl *ND = dyn_cast_or_null<NamedDecl>(Args[I].getAsDecl())) { |
| 5381 | if (ND->getDeclName()) { |
| 5382 | Unnamed = false; |
| 5383 | Result += ND->getNameAsString(); |
| 5384 | } |
| 5385 | } |
| 5386 | |
| 5387 | if (Unnamed) { |
| 5388 | Result += "<anonymous>"; |
| 5389 | } |
| 5390 | break; |
| 5391 | } |
| 5392 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 5393 | case TemplateArgument::Template: { |
| 5394 | std::string Str; |
| 5395 | llvm::raw_string_ostream OS(Str); |
| 5396 | Args[I].getAsTemplate().print(OS, Context.PrintingPolicy); |
| 5397 | Result += OS.str(); |
| 5398 | break; |
| 5399 | } |
| 5400 | |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 5401 | case TemplateArgument::Integral: { |
| 5402 | Result += Args[I].getAsIntegral()->toString(10); |
| 5403 | break; |
| 5404 | } |
| 5405 | |
| 5406 | case TemplateArgument::Expression: { |
| 5407 | assert(false && "No expressions in deduced template arguments!"); |
| 5408 | Result += "<expression>"; |
| 5409 | break; |
| 5410 | } |
| 5411 | |
| 5412 | case TemplateArgument::Pack: |
| 5413 | // FIXME: Format template argument packs |
| 5414 | Result += "<template argument pack>"; |
| 5415 | break; |
| 5416 | } |
| 5417 | } |
| 5418 | |
| 5419 | Result += ']'; |
| 5420 | return Result; |
| 5421 | } |