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" |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 18 | #include "clang/AST/DeclTemplate.h" |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 19 | #include "clang/Parse/DeclSpec.h" |
Douglas Gregor | 314b97f | 2009-11-10 19:49:08 +0000 | [diff] [blame] | 20 | #include "clang/Parse/Template.h" |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 21 | #include "clang/Basic/LangOptions.h" |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 22 | #include "clang/Basic/PartialDiagnostic.h" |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/StringExtras.h" |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 24 | using namespace clang; |
| 25 | |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 26 | /// \brief Determine whether the declaration found is acceptable as the name |
| 27 | /// of a template and, if so, return that template declaration. Otherwise, |
| 28 | /// returns NULL. |
| 29 | static NamedDecl *isAcceptableTemplateName(ASTContext &Context, NamedDecl *D) { |
| 30 | if (!D) |
| 31 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 32 | |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 33 | if (isa<TemplateDecl>(D)) |
| 34 | return D; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 35 | |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 36 | if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) { |
| 37 | // C++ [temp.local]p1: |
| 38 | // Like normal (non-template) classes, class templates have an |
| 39 | // injected-class-name (Clause 9). The injected-class-name |
| 40 | // can be used with or without a template-argument-list. When |
| 41 | // it is used without a template-argument-list, it is |
| 42 | // equivalent to the injected-class-name followed by the |
| 43 | // template-parameters of the class template enclosed in |
| 44 | // <>. When it is used with a template-argument-list, it |
| 45 | // refers to the specified class template specialization, |
| 46 | // which could be the current specialization or another |
| 47 | // specialization. |
| 48 | if (Record->isInjectedClassName()) { |
Douglas Gregor | 542b548 | 2009-10-14 17:30:58 +0000 | [diff] [blame] | 49 | Record = cast<CXXRecordDecl>(Record->getDeclContext()); |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 50 | if (Record->getDescribedClassTemplate()) |
| 51 | return Record->getDescribedClassTemplate(); |
| 52 | |
| 53 | if (ClassTemplateSpecializationDecl *Spec |
| 54 | = dyn_cast<ClassTemplateSpecializationDecl>(Record)) |
| 55 | return Spec->getSpecializedTemplate(); |
| 56 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 57 | |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 58 | return 0; |
| 59 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 60 | |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 61 | return 0; |
| 62 | } |
| 63 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 64 | static void FilterAcceptableTemplateNames(ASTContext &C, LookupResult &R) { |
| 65 | LookupResult::Filter filter = R.makeFilter(); |
| 66 | while (filter.hasNext()) { |
| 67 | NamedDecl *Orig = filter.next(); |
| 68 | NamedDecl *Repl = isAcceptableTemplateName(C, Orig->getUnderlyingDecl()); |
| 69 | if (!Repl) |
| 70 | filter.erase(); |
| 71 | else if (Repl != Orig) |
| 72 | filter.replace(Repl); |
| 73 | } |
| 74 | filter.done(); |
| 75 | } |
| 76 | |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 77 | TemplateNameKind Sema::isTemplateName(Scope *S, |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 78 | const CXXScopeSpec &SS, |
| 79 | UnqualifiedId &Name, |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 80 | TypeTy *ObjectTypePtr, |
Douglas Gregor | 495c35d | 2009-08-25 22:51:20 +0000 | [diff] [blame] | 81 | bool EnteringContext, |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 82 | TemplateTy &TemplateResult) { |
Douglas Gregor | b862b8f | 2010-01-11 23:29:10 +0000 | [diff] [blame] | 83 | assert(getLangOptions().CPlusPlus && "No template names in C!"); |
| 84 | |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 85 | DeclarationName TName; |
| 86 | |
| 87 | switch (Name.getKind()) { |
| 88 | case UnqualifiedId::IK_Identifier: |
| 89 | TName = DeclarationName(Name.Identifier); |
| 90 | break; |
| 91 | |
| 92 | case UnqualifiedId::IK_OperatorFunctionId: |
| 93 | TName = Context.DeclarationNames.getCXXOperatorName( |
| 94 | Name.OperatorFunctionId.Operator); |
| 95 | break; |
| 96 | |
Sean Hunt | e6252d1 | 2009-11-28 08:58:14 +0000 | [diff] [blame] | 97 | case UnqualifiedId::IK_LiteralOperatorId: |
Sean Hunt | 3e518bd | 2009-11-29 07:34:05 +0000 | [diff] [blame] | 98 | TName = Context.DeclarationNames.getCXXLiteralOperatorName(Name.Identifier); |
| 99 | break; |
Sean Hunt | e6252d1 | 2009-11-28 08:58:14 +0000 | [diff] [blame] | 100 | |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 101 | default: |
| 102 | return TNK_Non_template; |
| 103 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 104 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 105 | QualType ObjectType = QualType::getFromOpaquePtr(ObjectTypePtr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 106 | |
Douglas Gregor | bfea239 | 2009-12-31 08:11:17 +0000 | [diff] [blame] | 107 | LookupResult R(*this, TName, Name.getSourceRange().getBegin(), |
| 108 | LookupOrdinaryName); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 109 | R.suppressDiagnostics(); |
| 110 | LookupTemplateName(R, S, SS, ObjectType, EnteringContext); |
| 111 | if (R.empty()) |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 112 | return TNK_Non_template; |
| 113 | |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 114 | TemplateName Template; |
| 115 | TemplateNameKind TemplateKind; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 116 | |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 117 | unsigned ResultCount = R.end() - R.begin(); |
| 118 | if (ResultCount > 1) { |
| 119 | // We assume that we'll preserve the qualifier from a function |
| 120 | // template name in other ways. |
| 121 | Template = Context.getOverloadedTemplateName(R.begin(), R.end()); |
| 122 | TemplateKind = TNK_Function_template; |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 123 | } else { |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 124 | TemplateDecl *TD = cast<TemplateDecl>((*R.begin())->getUnderlyingDecl()); |
| 125 | |
| 126 | if (SS.isSet() && !SS.isInvalid()) { |
| 127 | NestedNameSpecifier *Qualifier |
| 128 | = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); |
| 129 | Template = Context.getQualifiedTemplateName(Qualifier, false, TD); |
| 130 | } else { |
| 131 | Template = TemplateName(TD); |
| 132 | } |
| 133 | |
| 134 | if (isa<FunctionTemplateDecl>(TD)) |
| 135 | TemplateKind = TNK_Function_template; |
| 136 | else { |
| 137 | assert(isa<ClassTemplateDecl>(TD) || isa<TemplateTemplateParmDecl>(TD)); |
| 138 | TemplateKind = TNK_Type_template; |
| 139 | } |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 140 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 141 | |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 142 | TemplateResult = TemplateTy::make(Template); |
| 143 | return TemplateKind; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 144 | } |
| 145 | |
Douglas Gregor | 84d0a19 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 146 | bool Sema::DiagnoseUnknownTemplateName(const IdentifierInfo &II, |
| 147 | SourceLocation IILoc, |
| 148 | Scope *S, |
| 149 | const CXXScopeSpec *SS, |
| 150 | TemplateTy &SuggestedTemplate, |
| 151 | TemplateNameKind &SuggestedKind) { |
| 152 | // We can't recover unless there's a dependent scope specifier preceding the |
| 153 | // template name. |
| 154 | if (!SS || !SS->isSet() || !isDependentScopeSpecifier(*SS) || |
| 155 | computeDeclContext(*SS)) |
| 156 | return false; |
| 157 | |
| 158 | // The code is missing a 'template' keyword prior to the dependent template |
| 159 | // name. |
| 160 | NestedNameSpecifier *Qualifier = (NestedNameSpecifier*)SS->getScopeRep(); |
| 161 | Diag(IILoc, diag::err_template_kw_missing) |
| 162 | << Qualifier << II.getName() |
| 163 | << CodeModificationHint::CreateInsertion(IILoc, "template "); |
| 164 | SuggestedTemplate |
| 165 | = TemplateTy::make(Context.getDependentTemplateName(Qualifier, &II)); |
| 166 | SuggestedKind = TNK_Dependent_template_name; |
| 167 | return true; |
| 168 | } |
| 169 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 170 | void Sema::LookupTemplateName(LookupResult &Found, |
| 171 | Scope *S, const CXXScopeSpec &SS, |
| 172 | QualType ObjectType, |
| 173 | bool EnteringContext) { |
| 174 | // Determine where to perform name lookup |
| 175 | DeclContext *LookupCtx = 0; |
| 176 | bool isDependent = false; |
| 177 | if (!ObjectType.isNull()) { |
| 178 | // This nested-name-specifier occurs in a member access expression, e.g., |
| 179 | // x->B::f, and we are looking into the type of the object. |
| 180 | assert(!SS.isSet() && "ObjectType and scope specifier cannot coexist"); |
| 181 | LookupCtx = computeDeclContext(ObjectType); |
| 182 | isDependent = ObjectType->isDependentType(); |
| 183 | assert((isDependent || !ObjectType->isIncompleteType()) && |
| 184 | "Caller should have completed object type"); |
| 185 | } else if (SS.isSet()) { |
| 186 | // This nested-name-specifier occurs after another nested-name-specifier, |
| 187 | // so long into the context associated with the prior nested-name-specifier. |
| 188 | LookupCtx = computeDeclContext(SS, EnteringContext); |
| 189 | isDependent = isDependentScopeSpecifier(SS); |
| 190 | |
| 191 | // The declaration context must be complete. |
| 192 | if (LookupCtx && RequireCompleteDeclContext(SS)) |
| 193 | return; |
| 194 | } |
| 195 | |
| 196 | bool ObjectTypeSearchedInScope = false; |
| 197 | if (LookupCtx) { |
| 198 | // Perform "qualified" name lookup into the declaration context we |
| 199 | // computed, which is either the type of the base of a member access |
| 200 | // expression or the declaration context associated with a prior |
| 201 | // nested-name-specifier. |
| 202 | LookupQualifiedName(Found, LookupCtx); |
| 203 | |
| 204 | if (!ObjectType.isNull() && Found.empty()) { |
| 205 | // C++ [basic.lookup.classref]p1: |
| 206 | // In a class member access expression (5.2.5), if the . or -> token is |
| 207 | // immediately followed by an identifier followed by a <, the |
| 208 | // identifier must be looked up to determine whether the < is the |
| 209 | // beginning of a template argument list (14.2) or a less-than operator. |
| 210 | // The identifier is first looked up in the class of the object |
| 211 | // expression. If the identifier is not found, it is then looked up in |
| 212 | // the context of the entire postfix-expression and shall name a class |
| 213 | // or function template. |
| 214 | // |
| 215 | // FIXME: When we're instantiating a template, do we actually have to |
| 216 | // look in the scope of the template? Seems fishy... |
| 217 | if (S) LookupName(Found, S); |
| 218 | ObjectTypeSearchedInScope = true; |
| 219 | } |
| 220 | } else if (isDependent) { |
Douglas Gregor | 2e93388 | 2010-01-12 17:06:20 +0000 | [diff] [blame] | 221 | // We cannot look into a dependent object type or nested nme |
| 222 | // specifier. |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 223 | return; |
| 224 | } else { |
| 225 | // Perform unqualified name lookup in the current scope. |
| 226 | LookupName(Found, S); |
| 227 | } |
| 228 | |
| 229 | // FIXME: Cope with ambiguous name-lookup results. |
| 230 | assert(!Found.isAmbiguous() && |
| 231 | "Cannot handle template name-lookup ambiguities"); |
| 232 | |
Douglas Gregor | 2e93388 | 2010-01-12 17:06:20 +0000 | [diff] [blame] | 233 | if (Found.empty() && !isDependent) { |
Douglas Gregor | bfea239 | 2009-12-31 08:11:17 +0000 | [diff] [blame] | 234 | // If we did not find any names, attempt to correct any typos. |
| 235 | DeclarationName Name = Found.getLookupName(); |
| 236 | if (CorrectTypo(Found, S, &SS, LookupCtx)) { |
| 237 | FilterAcceptableTemplateNames(Context, Found); |
| 238 | if (!Found.empty() && isa<TemplateDecl>(*Found.begin())) { |
| 239 | if (LookupCtx) |
| 240 | Diag(Found.getNameLoc(), diag::err_no_member_template_suggest) |
| 241 | << Name << LookupCtx << Found.getLookupName() << SS.getRange() |
| 242 | << CodeModificationHint::CreateReplacement(Found.getNameLoc(), |
| 243 | Found.getLookupName().getAsString()); |
| 244 | else |
| 245 | Diag(Found.getNameLoc(), diag::err_no_template_suggest) |
| 246 | << Name << Found.getLookupName() |
| 247 | << CodeModificationHint::CreateReplacement(Found.getNameLoc(), |
| 248 | Found.getLookupName().getAsString()); |
Douglas Gregor | 67dd1d4 | 2010-01-07 00:17:44 +0000 | [diff] [blame] | 249 | if (TemplateDecl *Template = Found.getAsSingle<TemplateDecl>()) |
| 250 | Diag(Template->getLocation(), diag::note_previous_decl) |
| 251 | << Template->getDeclName(); |
Douglas Gregor | bfea239 | 2009-12-31 08:11:17 +0000 | [diff] [blame] | 252 | } else |
| 253 | Found.clear(); |
| 254 | } else { |
| 255 | Found.clear(); |
| 256 | } |
| 257 | } |
| 258 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 259 | FilterAcceptableTemplateNames(Context, Found); |
| 260 | if (Found.empty()) |
| 261 | return; |
| 262 | |
| 263 | if (S && !ObjectType.isNull() && !ObjectTypeSearchedInScope) { |
| 264 | // C++ [basic.lookup.classref]p1: |
| 265 | // [...] If the lookup in the class of the object expression finds a |
| 266 | // template, the name is also looked up in the context of the entire |
| 267 | // postfix-expression and [...] |
| 268 | // |
| 269 | LookupResult FoundOuter(*this, Found.getLookupName(), Found.getNameLoc(), |
| 270 | LookupOrdinaryName); |
| 271 | LookupName(FoundOuter, S); |
| 272 | FilterAcceptableTemplateNames(Context, FoundOuter); |
| 273 | // FIXME: Handle ambiguities in this lookup better |
| 274 | |
| 275 | if (FoundOuter.empty()) { |
| 276 | // - if the name is not found, the name found in the class of the |
| 277 | // object expression is used, otherwise |
| 278 | } else if (!FoundOuter.getAsSingle<ClassTemplateDecl>()) { |
| 279 | // - if the name is found in the context of the entire |
| 280 | // postfix-expression and does not name a class template, the name |
| 281 | // found in the class of the object expression is used, otherwise |
| 282 | } else { |
| 283 | // - if the name found is a class template, it must refer to the same |
| 284 | // entity as the one found in the class of the object expression, |
| 285 | // otherwise the program is ill-formed. |
| 286 | if (!Found.isSingleResult() || |
| 287 | Found.getFoundDecl()->getCanonicalDecl() |
| 288 | != FoundOuter.getFoundDecl()->getCanonicalDecl()) { |
| 289 | Diag(Found.getNameLoc(), |
| 290 | diag::err_nested_name_member_ref_lookup_ambiguous) |
| 291 | << Found.getLookupName(); |
| 292 | Diag(Found.getRepresentativeDecl()->getLocation(), |
| 293 | diag::note_ambig_member_ref_object_type) |
| 294 | << ObjectType; |
| 295 | Diag(FoundOuter.getFoundDecl()->getLocation(), |
| 296 | diag::note_ambig_member_ref_scope); |
| 297 | |
| 298 | // Recover by taking the template that we found in the object |
| 299 | // expression's type. |
| 300 | } |
| 301 | } |
| 302 | } |
| 303 | } |
| 304 | |
John McCall | 2f841ba | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 305 | /// ActOnDependentIdExpression - Handle a dependent id-expression that |
| 306 | /// was just parsed. This is only possible with an explicit scope |
| 307 | /// specifier naming a dependent type. |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 308 | Sema::OwningExprResult |
| 309 | Sema::ActOnDependentIdExpression(const CXXScopeSpec &SS, |
| 310 | DeclarationName Name, |
| 311 | SourceLocation NameLoc, |
John McCall | 2f841ba | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 312 | bool isAddressOfOperand, |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 313 | const TemplateArgumentListInfo *TemplateArgs) { |
| 314 | NestedNameSpecifier *Qualifier |
| 315 | = static_cast<NestedNameSpecifier*>(SS.getScopeRep()); |
| 316 | |
John McCall | 2f841ba | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 317 | if (!isAddressOfOperand && |
| 318 | isa<CXXMethodDecl>(CurContext) && |
| 319 | cast<CXXMethodDecl>(CurContext)->isInstance()) { |
| 320 | QualType ThisType = cast<CXXMethodDecl>(CurContext)->getThisType(Context); |
| 321 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 322 | // Since the 'this' expression is synthesized, we don't need to |
| 323 | // perform the double-lookup check. |
| 324 | NamedDecl *FirstQualifierInScope = 0; |
| 325 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 326 | return Owned(CXXDependentScopeMemberExpr::Create(Context, |
| 327 | /*This*/ 0, ThisType, |
| 328 | /*IsArrow*/ true, |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 329 | /*Op*/ SourceLocation(), |
| 330 | Qualifier, SS.getRange(), |
| 331 | FirstQualifierInScope, |
| 332 | Name, NameLoc, |
| 333 | TemplateArgs)); |
| 334 | } |
| 335 | |
| 336 | return BuildDependentDeclRefExpr(SS, Name, NameLoc, TemplateArgs); |
| 337 | } |
| 338 | |
| 339 | Sema::OwningExprResult |
| 340 | Sema::BuildDependentDeclRefExpr(const CXXScopeSpec &SS, |
| 341 | DeclarationName Name, |
| 342 | SourceLocation NameLoc, |
| 343 | const TemplateArgumentListInfo *TemplateArgs) { |
| 344 | return Owned(DependentScopeDeclRefExpr::Create(Context, |
| 345 | static_cast<NestedNameSpecifier*>(SS.getScopeRep()), |
| 346 | SS.getRange(), |
| 347 | Name, NameLoc, |
| 348 | TemplateArgs)); |
Douglas Gregor | d6fb7ef | 2008-12-18 19:37:40 +0000 | [diff] [blame] | 349 | } |
| 350 | |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 351 | /// DiagnoseTemplateParameterShadow - Produce a diagnostic complaining |
| 352 | /// that the template parameter 'PrevDecl' is being shadowed by a new |
| 353 | /// declaration at location Loc. Returns true to indicate that this is |
| 354 | /// an error, and false otherwise. |
| 355 | bool Sema::DiagnoseTemplateParameterShadow(SourceLocation Loc, Decl *PrevDecl) { |
Douglas Gregor | f57172b | 2008-12-08 18:40:42 +0000 | [diff] [blame] | 356 | assert(PrevDecl->isTemplateParameter() && "Not a template parameter"); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 357 | |
| 358 | // Microsoft Visual C++ permits template parameters to be shadowed. |
| 359 | if (getLangOptions().Microsoft) |
| 360 | return false; |
| 361 | |
| 362 | // C++ [temp.local]p4: |
| 363 | // A template-parameter shall not be redeclared within its |
| 364 | // scope (including nested scopes). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 365 | Diag(Loc, diag::err_template_param_shadow) |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 366 | << cast<NamedDecl>(PrevDecl)->getDeclName(); |
| 367 | Diag(PrevDecl->getLocation(), diag::note_template_param_here); |
| 368 | return true; |
| 369 | } |
| 370 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 371 | /// AdjustDeclIfTemplate - If the given decl happens to be a template, reset |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 372 | /// the parameter D to reference the templated declaration and return a pointer |
| 373 | /// to the template declaration. Otherwise, do nothing to D and return null. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 374 | TemplateDecl *Sema::AdjustDeclIfTemplate(DeclPtrTy &D) { |
Douglas Gregor | 13d2d6c | 2009-10-06 21:27:51 +0000 | [diff] [blame] | 375 | if (TemplateDecl *Temp = dyn_cast_or_null<TemplateDecl>(D.getAs<Decl>())) { |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 376 | D = DeclPtrTy::make(Temp->getTemplatedDecl()); |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 377 | return Temp; |
| 378 | } |
| 379 | return 0; |
| 380 | } |
| 381 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 382 | static TemplateArgumentLoc translateTemplateArgument(Sema &SemaRef, |
| 383 | const ParsedTemplateArgument &Arg) { |
| 384 | |
| 385 | switch (Arg.getKind()) { |
| 386 | case ParsedTemplateArgument::Type: { |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 387 | TypeSourceInfo *DI; |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 388 | QualType T = SemaRef.GetTypeFromParser(Arg.getAsType(), &DI); |
| 389 | if (!DI) |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 390 | DI = SemaRef.Context.getTrivialTypeSourceInfo(T, Arg.getLocation()); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 391 | return TemplateArgumentLoc(TemplateArgument(T), DI); |
| 392 | } |
| 393 | |
| 394 | case ParsedTemplateArgument::NonType: { |
| 395 | Expr *E = static_cast<Expr *>(Arg.getAsExpr()); |
| 396 | return TemplateArgumentLoc(TemplateArgument(E), E); |
| 397 | } |
| 398 | |
| 399 | case ParsedTemplateArgument::Template: { |
| 400 | TemplateName Template |
| 401 | = TemplateName::getFromVoidPointer(Arg.getAsTemplate().get()); |
| 402 | return TemplateArgumentLoc(TemplateArgument(Template), |
| 403 | Arg.getScopeSpec().getRange(), |
| 404 | Arg.getLocation()); |
| 405 | } |
| 406 | } |
| 407 | |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 408 | llvm_unreachable("Unhandled parsed template argument"); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 409 | return TemplateArgumentLoc(); |
| 410 | } |
| 411 | |
| 412 | /// \brief Translates template arguments as provided by the parser |
| 413 | /// into template arguments used by semantic analysis. |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 414 | void Sema::translateTemplateArguments(const ASTTemplateArgsPtr &TemplateArgsIn, |
| 415 | TemplateArgumentListInfo &TemplateArgs) { |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 416 | for (unsigned I = 0, Last = TemplateArgsIn.size(); I != Last; ++I) |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 417 | TemplateArgs.addArgument(translateTemplateArgument(*this, |
| 418 | TemplateArgsIn[I])); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 419 | } |
| 420 | |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 421 | /// ActOnTypeParameter - Called when a C++ template type parameter |
| 422 | /// (e.g., "typename T") has been parsed. Typename specifies whether |
| 423 | /// the keyword "typename" was used to declare the type parameter |
| 424 | /// (otherwise, "class" was used), and KeyLoc is the location of the |
| 425 | /// "class" or "typename" keyword. ParamName is the name of the |
| 426 | /// parameter (NULL indicates an unnamed template parameter) and |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 427 | /// ParamName is the location of the parameter name (if any). |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 428 | /// If the type parameter has a default argument, it will be added |
| 429 | /// later via ActOnTypeParameterDefault. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 430 | Sema::DeclPtrTy Sema::ActOnTypeParameter(Scope *S, bool Typename, bool Ellipsis, |
Anders Carlsson | 941df7d | 2009-06-12 19:58:00 +0000 | [diff] [blame] | 431 | SourceLocation EllipsisLoc, |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 432 | SourceLocation KeyLoc, |
| 433 | IdentifierInfo *ParamName, |
| 434 | SourceLocation ParamNameLoc, |
| 435 | unsigned Depth, unsigned Position) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 436 | assert(S->isTemplateParamScope() && |
| 437 | "Template type parameter not in template parameter scope!"); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 438 | bool Invalid = false; |
| 439 | |
| 440 | if (ParamName) { |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 441 | NamedDecl *PrevDecl = LookupSingleName(S, ParamName, LookupTagName); |
Douglas Gregor | f57172b | 2008-12-08 18:40:42 +0000 | [diff] [blame] | 442 | if (PrevDecl && PrevDecl->isTemplateParameter()) |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 443 | Invalid = Invalid || DiagnoseTemplateParameterShadow(ParamNameLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 444 | PrevDecl); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 445 | } |
| 446 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 447 | SourceLocation Loc = ParamNameLoc; |
| 448 | if (!ParamName) |
| 449 | Loc = KeyLoc; |
| 450 | |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 451 | TemplateTypeParmDecl *Param |
John McCall | 7a9813c | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 452 | = TemplateTypeParmDecl::Create(Context, Context.getTranslationUnitDecl(), |
| 453 | Loc, Depth, Position, ParamName, Typename, |
Anders Carlsson | 6d845ae | 2009-06-12 22:23:22 +0000 | [diff] [blame] | 454 | Ellipsis); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 455 | if (Invalid) |
| 456 | Param->setInvalidDecl(); |
| 457 | |
| 458 | if (ParamName) { |
| 459 | // Add the template parameter into the current scope. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 460 | S->AddDecl(DeclPtrTy::make(Param)); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 461 | IdResolver.AddDecl(Param); |
| 462 | } |
| 463 | |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 464 | return DeclPtrTy::make(Param); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 465 | } |
| 466 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 467 | /// ActOnTypeParameterDefault - Adds a default argument (the type |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 468 | /// Default) to the given template type parameter (TypeParam). |
| 469 | void Sema::ActOnTypeParameterDefault(DeclPtrTy TypeParam, |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 470 | SourceLocation EqualLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 471 | SourceLocation DefaultLoc, |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 472 | TypeTy *DefaultT) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 473 | TemplateTypeParmDecl *Parm |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 474 | = cast<TemplateTypeParmDecl>(TypeParam.getAs<Decl>()); |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 475 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 476 | TypeSourceInfo *DefaultTInfo; |
| 477 | GetTypeFromParser(DefaultT, &DefaultTInfo); |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 478 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 479 | assert(DefaultTInfo && "expected source information for type"); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 480 | |
Anders Carlsson | 9c4c5c8 | 2009-06-12 22:30:13 +0000 | [diff] [blame] | 481 | // C++0x [temp.param]p9: |
| 482 | // A default template-argument may be specified for any kind of |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 483 | // template-parameter that is not a template parameter pack. |
Anders Carlsson | 9c4c5c8 | 2009-06-12 22:30:13 +0000 | [diff] [blame] | 484 | if (Parm->isParameterPack()) { |
| 485 | Diag(DefaultLoc, diag::err_template_param_pack_default_arg); |
Anders Carlsson | 9c4c5c8 | 2009-06-12 22:30:13 +0000 | [diff] [blame] | 486 | return; |
| 487 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 488 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 489 | // C++ [temp.param]p14: |
| 490 | // A template-parameter shall not be used in its own default argument. |
| 491 | // FIXME: Implement this check! Needs a recursive walk over the types. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 492 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 493 | // Check the template argument itself. |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 494 | if (CheckTemplateArgument(Parm, DefaultTInfo)) { |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 495 | Parm->setInvalidDecl(); |
| 496 | return; |
| 497 | } |
| 498 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 499 | Parm->setDefaultArgument(DefaultTInfo, false); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 500 | } |
| 501 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 502 | /// \brief Check that the type of a non-type template parameter is |
| 503 | /// well-formed. |
| 504 | /// |
| 505 | /// \returns the (possibly-promoted) parameter type if valid; |
| 506 | /// otherwise, produces a diagnostic and returns a NULL type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 507 | QualType |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 508 | Sema::CheckNonTypeTemplateParameterType(QualType T, SourceLocation Loc) { |
| 509 | // C++ [temp.param]p4: |
| 510 | // |
| 511 | // A non-type template-parameter shall have one of the following |
| 512 | // (optionally cv-qualified) types: |
| 513 | // |
| 514 | // -- integral or enumeration type, |
| 515 | if (T->isIntegralType() || T->isEnumeralType() || |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 516 | // -- pointer to object or pointer to function, |
| 517 | (T->isPointerType() && |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 518 | (T->getAs<PointerType>()->getPointeeType()->isObjectType() || |
| 519 | T->getAs<PointerType>()->getPointeeType()->isFunctionType())) || |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 520 | // -- reference to object or reference to function, |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 521 | T->isReferenceType() || |
| 522 | // -- pointer to member. |
| 523 | T->isMemberPointerType() || |
| 524 | // If T is a dependent type, we can't do the check now, so we |
| 525 | // assume that it is well-formed. |
| 526 | T->isDependentType()) |
| 527 | return T; |
| 528 | // C++ [temp.param]p8: |
| 529 | // |
| 530 | // A non-type template-parameter of type "array of T" or |
| 531 | // "function returning T" is adjusted to be of type "pointer to |
| 532 | // T" or "pointer to function returning T", respectively. |
| 533 | else if (T->isArrayType()) |
| 534 | // FIXME: Keep the type prior to promotion? |
| 535 | return Context.getArrayDecayedType(T); |
| 536 | else if (T->isFunctionType()) |
| 537 | // FIXME: Keep the type prior to promotion? |
| 538 | return Context.getPointerType(T); |
| 539 | |
| 540 | Diag(Loc, diag::err_template_nontype_parm_bad_type) |
| 541 | << T; |
| 542 | |
| 543 | return QualType(); |
| 544 | } |
| 545 | |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 546 | /// ActOnNonTypeTemplateParameter - Called when a C++ non-type |
| 547 | /// template parameter (e.g., "int Size" in "template<int Size> |
| 548 | /// class Array") has been parsed. S is the current scope and D is |
| 549 | /// the parsed declarator. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 550 | Sema::DeclPtrTy Sema::ActOnNonTypeTemplateParameter(Scope *S, Declarator &D, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 551 | unsigned Depth, |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 552 | unsigned Position) { |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 553 | TypeSourceInfo *TInfo = 0; |
| 554 | QualType T = GetTypeForDeclarator(D, S, &TInfo); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 555 | |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 556 | assert(S->isTemplateParamScope() && |
| 557 | "Non-type template parameter not in template parameter scope!"); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 558 | bool Invalid = false; |
| 559 | |
| 560 | IdentifierInfo *ParamName = D.getIdentifier(); |
| 561 | if (ParamName) { |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 562 | NamedDecl *PrevDecl = LookupSingleName(S, ParamName, LookupTagName); |
Douglas Gregor | f57172b | 2008-12-08 18:40:42 +0000 | [diff] [blame] | 563 | if (PrevDecl && PrevDecl->isTemplateParameter()) |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 564 | Invalid = Invalid || DiagnoseTemplateParameterShadow(D.getIdentifierLoc(), |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 565 | PrevDecl); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 566 | } |
| 567 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 568 | T = CheckNonTypeTemplateParameterType(T, D.getIdentifierLoc()); |
Douglas Gregor | ceef30c | 2009-03-09 16:46:39 +0000 | [diff] [blame] | 569 | if (T.isNull()) { |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 570 | T = Context.IntTy; // Recover with an 'int' type. |
Douglas Gregor | ceef30c | 2009-03-09 16:46:39 +0000 | [diff] [blame] | 571 | Invalid = true; |
| 572 | } |
Douglas Gregor | 5d290d5 | 2009-02-10 17:43:50 +0000 | [diff] [blame] | 573 | |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 574 | NonTypeTemplateParmDecl *Param |
John McCall | 7a9813c | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 575 | = NonTypeTemplateParmDecl::Create(Context, Context.getTranslationUnitDecl(), |
| 576 | D.getIdentifierLoc(), |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 577 | Depth, Position, ParamName, T, TInfo); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 578 | if (Invalid) |
| 579 | Param->setInvalidDecl(); |
| 580 | |
| 581 | if (D.getIdentifier()) { |
| 582 | // Add the template parameter into the current scope. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 583 | S->AddDecl(DeclPtrTy::make(Param)); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 584 | IdResolver.AddDecl(Param); |
| 585 | } |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 586 | return DeclPtrTy::make(Param); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 587 | } |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 588 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 589 | /// \brief Adds a default argument to the given non-type template |
| 590 | /// parameter. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 591 | void Sema::ActOnNonTypeTemplateParameterDefault(DeclPtrTy TemplateParamD, |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 592 | SourceLocation EqualLoc, |
| 593 | ExprArg DefaultE) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 594 | NonTypeTemplateParmDecl *TemplateParm |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 595 | = cast<NonTypeTemplateParmDecl>(TemplateParamD.getAs<Decl>()); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 596 | Expr *Default = static_cast<Expr *>(DefaultE.get()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 597 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 598 | // C++ [temp.param]p14: |
| 599 | // A template-parameter shall not be used in its own default argument. |
| 600 | // FIXME: Implement this check! Needs a recursive walk over the types. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 601 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 602 | // Check the well-formedness of the default template argument. |
Douglas Gregor | 02cbbd2 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 603 | TemplateArgument Converted; |
| 604 | if (CheckTemplateArgument(TemplateParm, TemplateParm->getType(), Default, |
| 605 | Converted)) { |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 606 | TemplateParm->setInvalidDecl(); |
| 607 | return; |
| 608 | } |
| 609 | |
Anders Carlsson | e9146f2 | 2009-05-01 19:49:17 +0000 | [diff] [blame] | 610 | TemplateParm->setDefaultArgument(DefaultE.takeAs<Expr>()); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 611 | } |
| 612 | |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 613 | |
| 614 | /// ActOnTemplateTemplateParameter - Called when a C++ template template |
| 615 | /// parameter (e.g. T in template <template <typename> class T> class array) |
| 616 | /// has been parsed. S is the current scope. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 617 | Sema::DeclPtrTy Sema::ActOnTemplateTemplateParameter(Scope* S, |
| 618 | SourceLocation TmpLoc, |
| 619 | TemplateParamsTy *Params, |
| 620 | IdentifierInfo *Name, |
| 621 | SourceLocation NameLoc, |
| 622 | unsigned Depth, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 623 | unsigned Position) { |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 624 | assert(S->isTemplateParamScope() && |
| 625 | "Template template parameter not in template parameter scope!"); |
| 626 | |
| 627 | // Construct the parameter object. |
| 628 | TemplateTemplateParmDecl *Param = |
John McCall | 7a9813c | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 629 | TemplateTemplateParmDecl::Create(Context, Context.getTranslationUnitDecl(), |
| 630 | TmpLoc, Depth, Position, Name, |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 631 | (TemplateParameterList*)Params); |
| 632 | |
| 633 | // Make sure the parameter is valid. |
| 634 | // FIXME: Decl object is not currently invalidated anywhere so this doesn't |
| 635 | // do anything yet. However, if the template parameter list or (eventual) |
| 636 | // default value is ever invalidated, that will propagate here. |
| 637 | bool Invalid = false; |
| 638 | if (Invalid) { |
| 639 | Param->setInvalidDecl(); |
| 640 | } |
| 641 | |
| 642 | // If the tt-param has a name, then link the identifier into the scope |
| 643 | // and lookup mechanisms. |
| 644 | if (Name) { |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 645 | S->AddDecl(DeclPtrTy::make(Param)); |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 646 | IdResolver.AddDecl(Param); |
| 647 | } |
| 648 | |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 649 | return DeclPtrTy::make(Param); |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 650 | } |
| 651 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 652 | /// \brief Adds a default argument to the given template template |
| 653 | /// parameter. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 654 | void Sema::ActOnTemplateTemplateParameterDefault(DeclPtrTy TemplateParamD, |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 655 | SourceLocation EqualLoc, |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 656 | const ParsedTemplateArgument &Default) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 657 | TemplateTemplateParmDecl *TemplateParm |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 658 | = cast<TemplateTemplateParmDecl>(TemplateParamD.getAs<Decl>()); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 659 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 660 | // C++ [temp.param]p14: |
| 661 | // A template-parameter shall not be used in its own default argument. |
| 662 | // FIXME: Implement this check! Needs a recursive walk over the types. |
| 663 | |
Douglas Gregor | 9148c3f | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 664 | // Check only that we have a template template argument. We don't want to |
| 665 | // try to check well-formedness now, because our template template parameter |
| 666 | // might have dependent types in its template parameters, which we wouldn't |
| 667 | // be able to match now. |
| 668 | // |
| 669 | // If none of the template template parameter's template arguments mention |
| 670 | // other template parameters, we could actually perform more checking here. |
| 671 | // However, it isn't worth doing. |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 672 | TemplateArgumentLoc DefaultArg = translateTemplateArgument(*this, Default); |
Douglas Gregor | 9148c3f | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 673 | if (DefaultArg.getArgument().getAsTemplate().isNull()) { |
| 674 | Diag(DefaultArg.getLocation(), diag::err_template_arg_not_class_template) |
| 675 | << DefaultArg.getSourceRange(); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 676 | return; |
| 677 | } |
Douglas Gregor | 9148c3f | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 678 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 679 | TemplateParm->setDefaultArgument(DefaultArg); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 680 | } |
| 681 | |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 682 | /// ActOnTemplateParameterList - Builds a TemplateParameterList that |
| 683 | /// contains the template parameters in Params/NumParams. |
| 684 | Sema::TemplateParamsTy * |
| 685 | Sema::ActOnTemplateParameterList(unsigned Depth, |
| 686 | SourceLocation ExportLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 687 | SourceLocation TemplateLoc, |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 688 | SourceLocation LAngleLoc, |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 689 | DeclPtrTy *Params, unsigned NumParams, |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 690 | SourceLocation RAngleLoc) { |
| 691 | if (ExportLoc.isValid()) |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 692 | Diag(ExportLoc, diag::warn_template_export_unsupported); |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 693 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 694 | return TemplateParameterList::Create(Context, TemplateLoc, LAngleLoc, |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 695 | (NamedDecl**)Params, NumParams, |
| 696 | RAngleLoc); |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 697 | } |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 698 | |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 699 | Sema::DeclResult |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 700 | Sema::CheckClassTemplate(Scope *S, unsigned TagSpec, TagUseKind TUK, |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 701 | SourceLocation KWLoc, const CXXScopeSpec &SS, |
| 702 | IdentifierInfo *Name, SourceLocation NameLoc, |
| 703 | AttributeList *Attr, |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 704 | TemplateParameterList *TemplateParams, |
Anders Carlsson | 5aeccdb | 2009-03-26 00:52:18 +0000 | [diff] [blame] | 705 | AccessSpecifier AS) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 706 | assert(TemplateParams && TemplateParams->size() > 0 && |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 707 | "No template parameters"); |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 708 | assert(TUK != TUK_Reference && "Can only declare or define class templates"); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 709 | bool Invalid = false; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 710 | |
| 711 | // Check that we can declare a template here. |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 712 | if (CheckTemplateDeclScope(S, TemplateParams)) |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 713 | return true; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 714 | |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 715 | TagDecl::TagKind Kind = TagDecl::getTagKindForTypeSpec(TagSpec); |
| 716 | assert(Kind != TagDecl::TK_enum && "can't build template of enumerated type"); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 717 | |
| 718 | // There is no such thing as an unnamed class template. |
| 719 | if (!Name) { |
| 720 | Diag(KWLoc, diag::err_template_unnamed_class); |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 721 | return true; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 722 | } |
| 723 | |
| 724 | // Find any previous declaration with this name. |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 725 | DeclContext *SemanticContext; |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 726 | LookupResult Previous(*this, Name, NameLoc, LookupOrdinaryName, |
John McCall | 7d384dd | 2009-11-18 07:57:50 +0000 | [diff] [blame] | 727 | ForRedeclaration); |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 728 | if (SS.isNotEmpty() && !SS.isInvalid()) { |
Douglas Gregor | f0510d4 | 2009-10-12 23:11:44 +0000 | [diff] [blame] | 729 | if (RequireCompleteDeclContext(SS)) |
| 730 | return true; |
| 731 | |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 732 | SemanticContext = computeDeclContext(SS, true); |
| 733 | if (!SemanticContext) { |
| 734 | // FIXME: Produce a reasonable diagnostic here |
| 735 | return true; |
| 736 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 737 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 738 | LookupQualifiedName(Previous, SemanticContext); |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 739 | } else { |
| 740 | SemanticContext = CurContext; |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 741 | LookupName(Previous, S); |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 742 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 743 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 744 | assert(!Previous.isAmbiguous() && "Ambiguity in class template redecl?"); |
| 745 | NamedDecl *PrevDecl = 0; |
| 746 | if (Previous.begin() != Previous.end()) |
| 747 | PrevDecl = *Previous.begin(); |
| 748 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 749 | // If there is a previous declaration with the same name, check |
| 750 | // whether this is a valid redeclaration. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 751 | ClassTemplateDecl *PrevClassTemplate |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 752 | = dyn_cast_or_null<ClassTemplateDecl>(PrevDecl); |
Douglas Gregor | d7e5bdb | 2009-10-09 21:11:42 +0000 | [diff] [blame] | 753 | |
| 754 | // We may have found the injected-class-name of a class template, |
| 755 | // class template partial specialization, or class template specialization. |
| 756 | // In these cases, grab the template that is being defined or specialized. |
| 757 | if (!PrevClassTemplate && PrevDecl && isa<CXXRecordDecl>(PrevDecl) && |
| 758 | cast<CXXRecordDecl>(PrevDecl)->isInjectedClassName()) { |
| 759 | PrevDecl = cast<CXXRecordDecl>(PrevDecl->getDeclContext()); |
| 760 | PrevClassTemplate |
| 761 | = cast<CXXRecordDecl>(PrevDecl)->getDescribedClassTemplate(); |
| 762 | if (!PrevClassTemplate && isa<ClassTemplateSpecializationDecl>(PrevDecl)) { |
| 763 | PrevClassTemplate |
| 764 | = cast<ClassTemplateSpecializationDecl>(PrevDecl) |
| 765 | ->getSpecializedTemplate(); |
| 766 | } |
| 767 | } |
| 768 | |
John McCall | 65c4946 | 2009-12-18 11:25:59 +0000 | [diff] [blame] | 769 | if (TUK == TUK_Friend) { |
John McCall | e129d44 | 2009-12-17 23:21:11 +0000 | [diff] [blame] | 770 | // C++ [namespace.memdef]p3: |
| 771 | // [...] When looking for a prior declaration of a class or a function |
| 772 | // declared as a friend, and when the name of the friend class or |
| 773 | // function is neither a qualified name nor a template-id, scopes outside |
| 774 | // the innermost enclosing namespace scope are not considered. |
| 775 | DeclContext *OutermostContext = CurContext; |
| 776 | while (!OutermostContext->isFileContext()) |
| 777 | OutermostContext = OutermostContext->getLookupParent(); |
John McCall | 65c4946 | 2009-12-18 11:25:59 +0000 | [diff] [blame] | 778 | |
| 779 | if (PrevDecl && |
| 780 | (OutermostContext->Equals(PrevDecl->getDeclContext()) || |
| 781 | OutermostContext->Encloses(PrevDecl->getDeclContext()))) { |
John McCall | e129d44 | 2009-12-17 23:21:11 +0000 | [diff] [blame] | 782 | SemanticContext = PrevDecl->getDeclContext(); |
| 783 | } else { |
| 784 | // Declarations in outer scopes don't matter. However, the outermost |
| 785 | // context we computed is the semantic context for our new |
| 786 | // declaration. |
| 787 | PrevDecl = PrevClassTemplate = 0; |
| 788 | SemanticContext = OutermostContext; |
| 789 | } |
| 790 | |
| 791 | if (CurContext->isDependentContext()) { |
| 792 | // If this is a dependent context, we don't want to link the friend |
| 793 | // class template to the template in scope, because that would perform |
| 794 | // checking of the template parameter lists that can't be performed |
| 795 | // until the outer context is instantiated. |
| 796 | PrevDecl = PrevClassTemplate = 0; |
| 797 | } |
| 798 | } else if (PrevDecl && !isDeclInScope(PrevDecl, SemanticContext, S)) |
| 799 | PrevDecl = PrevClassTemplate = 0; |
| 800 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 801 | if (PrevClassTemplate) { |
| 802 | // Ensure that the template parameter lists are compatible. |
| 803 | if (!TemplateParameterListsAreEqual(TemplateParams, |
| 804 | PrevClassTemplate->getTemplateParameters(), |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 805 | /*Complain=*/true, |
| 806 | TPL_TemplateMatch)) |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 807 | return true; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 808 | |
| 809 | // C++ [temp.class]p4: |
| 810 | // In a redeclaration, partial specialization, explicit |
| 811 | // specialization or explicit instantiation of a class template, |
| 812 | // the class-key shall agree in kind with the original class |
| 813 | // template declaration (7.1.5.3). |
| 814 | RecordDecl *PrevRecordDecl = PrevClassTemplate->getTemplatedDecl(); |
Douglas Gregor | 501c5ce | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 815 | if (!isAcceptableTagRedeclaration(PrevRecordDecl, Kind, KWLoc, *Name)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 816 | Diag(KWLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 817 | << Name |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 818 | << CodeModificationHint::CreateReplacement(KWLoc, |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 819 | PrevRecordDecl->getKindName()); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 820 | Diag(PrevRecordDecl->getLocation(), diag::note_previous_use); |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 821 | Kind = PrevRecordDecl->getTagKind(); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 822 | } |
| 823 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 824 | // Check for redefinition of this class template. |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 825 | if (TUK == TUK_Definition) { |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 826 | if (TagDecl *Def = PrevRecordDecl->getDefinition(Context)) { |
| 827 | Diag(NameLoc, diag::err_redefinition) << Name; |
| 828 | Diag(Def->getLocation(), diag::note_previous_definition); |
| 829 | // FIXME: Would it make sense to try to "forget" the previous |
| 830 | // definition, as part of error recovery? |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 831 | return true; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 832 | } |
| 833 | } |
| 834 | } else if (PrevDecl && PrevDecl->isTemplateParameter()) { |
| 835 | // Maybe we will complain about the shadowed template parameter. |
| 836 | DiagnoseTemplateParameterShadow(NameLoc, PrevDecl); |
| 837 | // Just pretend that we didn't see the previous declaration. |
| 838 | PrevDecl = 0; |
| 839 | } else if (PrevDecl) { |
| 840 | // C++ [temp]p5: |
| 841 | // A class template shall not have the same name as any other |
| 842 | // template, class, function, object, enumeration, enumerator, |
| 843 | // namespace, or type in the same scope (3.3), except as specified |
| 844 | // in (14.5.4). |
| 845 | Diag(NameLoc, diag::err_redefinition_different_kind) << Name; |
| 846 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 847 | return true; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 848 | } |
| 849 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 850 | // Check the template parameter list of this declaration, possibly |
| 851 | // merging in the template parameter list from the previous class |
| 852 | // template declaration. |
| 853 | if (CheckTemplateParameterList(TemplateParams, |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 854 | PrevClassTemplate? PrevClassTemplate->getTemplateParameters() : 0, |
| 855 | TPC_ClassTemplate)) |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 856 | Invalid = true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 857 | |
Douglas Gregor | 7da97d0 | 2009-05-10 22:57:19 +0000 | [diff] [blame] | 858 | // FIXME: If we had a scope specifier, we better have a previous template |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 859 | // declaration! |
| 860 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 861 | CXXRecordDecl *NewClass = |
Douglas Gregor | 741dd9a | 2009-07-21 14:46:17 +0000 | [diff] [blame] | 862 | CXXRecordDecl::Create(Context, Kind, SemanticContext, NameLoc, Name, KWLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 863 | PrevClassTemplate? |
Douglas Gregor | aafc0cc | 2009-05-15 19:11:46 +0000 | [diff] [blame] | 864 | PrevClassTemplate->getTemplatedDecl() : 0, |
| 865 | /*DelayTypeCreation=*/true); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 866 | |
| 867 | ClassTemplateDecl *NewTemplate |
| 868 | = ClassTemplateDecl::Create(Context, SemanticContext, NameLoc, |
| 869 | DeclarationName(Name), TemplateParams, |
Douglas Gregor | 5953d8b | 2009-03-19 17:26:29 +0000 | [diff] [blame] | 870 | NewClass, PrevClassTemplate); |
Douglas Gregor | befc20e | 2009-03-26 00:10:35 +0000 | [diff] [blame] | 871 | NewClass->setDescribedClassTemplate(NewTemplate); |
| 872 | |
Douglas Gregor | aafc0cc | 2009-05-15 19:11:46 +0000 | [diff] [blame] | 873 | // Build the type for the class template declaration now. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 874 | QualType T = |
| 875 | Context.getTypeDeclType(NewClass, |
| 876 | PrevClassTemplate? |
| 877 | PrevClassTemplate->getTemplatedDecl() : 0); |
Douglas Gregor | aafc0cc | 2009-05-15 19:11:46 +0000 | [diff] [blame] | 878 | assert(T->isDependentType() && "Class template type is not dependent?"); |
| 879 | (void)T; |
| 880 | |
Douglas Gregor | fd056bc | 2009-10-13 16:30:37 +0000 | [diff] [blame] | 881 | // If we are providing an explicit specialization of a member that is a |
| 882 | // class template, make a note of that. |
| 883 | if (PrevClassTemplate && |
| 884 | PrevClassTemplate->getInstantiatedFromMemberTemplate()) |
| 885 | PrevClassTemplate->setMemberSpecialization(); |
| 886 | |
Anders Carlsson | 4cbe82c | 2009-03-26 01:24:28 +0000 | [diff] [blame] | 887 | // Set the access specifier. |
Douglas Gregor | d85bea2 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 888 | if (!Invalid && TUK != TUK_Friend) |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 889 | SetMemberAccessSpecifier(NewTemplate, PrevClassTemplate, AS); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 890 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 891 | // Set the lexical context of these templates |
| 892 | NewClass->setLexicalDeclContext(CurContext); |
| 893 | NewTemplate->setLexicalDeclContext(CurContext); |
| 894 | |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 895 | if (TUK == TUK_Definition) |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 896 | NewClass->startDefinition(); |
| 897 | |
| 898 | if (Attr) |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 899 | ProcessDeclAttributeList(S, NewClass, Attr); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 900 | |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 901 | if (TUK != TUK_Friend) |
| 902 | PushOnScopeChains(NewTemplate, S); |
| 903 | else { |
Douglas Gregor | d85bea2 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 904 | if (PrevClassTemplate && PrevClassTemplate->getAccess() != AS_none) { |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 905 | NewTemplate->setAccess(PrevClassTemplate->getAccess()); |
Douglas Gregor | d85bea2 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 906 | NewClass->setAccess(PrevClassTemplate->getAccess()); |
| 907 | } |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 908 | |
Douglas Gregor | d85bea2 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 909 | NewTemplate->setObjectOfFriendDecl(/* PreviouslyDeclared = */ |
| 910 | PrevClassTemplate != NULL); |
| 911 | |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 912 | // Friend templates are visible in fairly strange ways. |
| 913 | if (!CurContext->isDependentContext()) { |
| 914 | DeclContext *DC = SemanticContext->getLookupContext(); |
| 915 | DC->makeDeclVisibleInContext(NewTemplate, /* Recoverable = */ false); |
| 916 | if (Scope *EnclosingScope = getScopeForDeclContext(S, DC)) |
| 917 | PushOnScopeChains(NewTemplate, EnclosingScope, |
| 918 | /* AddToContext = */ false); |
| 919 | } |
Douglas Gregor | d85bea2 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 920 | |
| 921 | FriendDecl *Friend = FriendDecl::Create(Context, CurContext, |
| 922 | NewClass->getLocation(), |
| 923 | NewTemplate, |
| 924 | /*FIXME:*/NewClass->getLocation()); |
| 925 | Friend->setAccess(AS_public); |
| 926 | CurContext->addDecl(Friend); |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 927 | } |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 928 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 929 | if (Invalid) { |
| 930 | NewTemplate->setInvalidDecl(); |
| 931 | NewClass->setInvalidDecl(); |
| 932 | } |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 933 | return DeclPtrTy::make(NewTemplate); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 934 | } |
| 935 | |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 936 | /// \brief Diagnose the presence of a default template argument on a |
| 937 | /// template parameter, which is ill-formed in certain contexts. |
| 938 | /// |
| 939 | /// \returns true if the default template argument should be dropped. |
| 940 | static bool DiagnoseDefaultTemplateArgument(Sema &S, |
| 941 | Sema::TemplateParamListContext TPC, |
| 942 | SourceLocation ParamLoc, |
| 943 | SourceRange DefArgRange) { |
| 944 | switch (TPC) { |
| 945 | case Sema::TPC_ClassTemplate: |
| 946 | return false; |
| 947 | |
| 948 | case Sema::TPC_FunctionTemplate: |
| 949 | // C++ [temp.param]p9: |
| 950 | // A default template-argument shall not be specified in a |
| 951 | // function template declaration or a function template |
| 952 | // definition [...] |
| 953 | // (This sentence is not in C++0x, per DR226). |
| 954 | if (!S.getLangOptions().CPlusPlus0x) |
| 955 | S.Diag(ParamLoc, |
| 956 | diag::err_template_parameter_default_in_function_template) |
| 957 | << DefArgRange; |
| 958 | return false; |
| 959 | |
| 960 | case Sema::TPC_ClassTemplateMember: |
| 961 | // C++0x [temp.param]p9: |
| 962 | // A default template-argument shall not be specified in the |
| 963 | // template-parameter-lists of the definition of a member of a |
| 964 | // class template that appears outside of the member's class. |
| 965 | S.Diag(ParamLoc, diag::err_template_parameter_default_template_member) |
| 966 | << DefArgRange; |
| 967 | return true; |
| 968 | |
| 969 | case Sema::TPC_FriendFunctionTemplate: |
| 970 | // C++ [temp.param]p9: |
| 971 | // A default template-argument shall not be specified in a |
| 972 | // friend template declaration. |
| 973 | S.Diag(ParamLoc, diag::err_template_parameter_default_friend_template) |
| 974 | << DefArgRange; |
| 975 | return true; |
| 976 | |
| 977 | // FIXME: C++0x [temp.param]p9 allows default template-arguments |
| 978 | // for friend function templates if there is only a single |
| 979 | // declaration (and it is a definition). Strange! |
| 980 | } |
| 981 | |
| 982 | return false; |
| 983 | } |
| 984 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 985 | /// \brief Checks the validity of a template parameter list, possibly |
| 986 | /// considering the template parameter list from a previous |
| 987 | /// declaration. |
| 988 | /// |
| 989 | /// If an "old" template parameter list is provided, it must be |
| 990 | /// equivalent (per TemplateParameterListsAreEqual) to the "new" |
| 991 | /// template parameter list. |
| 992 | /// |
| 993 | /// \param NewParams Template parameter list for a new template |
| 994 | /// declaration. This template parameter list will be updated with any |
| 995 | /// default arguments that are carried through from the previous |
| 996 | /// template parameter list. |
| 997 | /// |
| 998 | /// \param OldParams If provided, template parameter list from a |
| 999 | /// previous declaration of the same template. Default template |
| 1000 | /// arguments will be merged from the old template parameter list to |
| 1001 | /// the new template parameter list. |
| 1002 | /// |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1003 | /// \param TPC Describes the context in which we are checking the given |
| 1004 | /// template parameter list. |
| 1005 | /// |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1006 | /// \returns true if an error occurred, false otherwise. |
| 1007 | bool Sema::CheckTemplateParameterList(TemplateParameterList *NewParams, |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1008 | TemplateParameterList *OldParams, |
| 1009 | TemplateParamListContext TPC) { |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1010 | bool Invalid = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1011 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1012 | // C++ [temp.param]p10: |
| 1013 | // The set of default template-arguments available for use with a |
| 1014 | // template declaration or definition is obtained by merging the |
| 1015 | // default arguments from the definition (if in scope) and all |
| 1016 | // declarations in scope in the same way default function |
| 1017 | // arguments are (8.3.6). |
| 1018 | bool SawDefaultArgument = false; |
| 1019 | SourceLocation PreviousDefaultArgLoc; |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 1020 | |
Anders Carlsson | 49d2557 | 2009-06-12 23:20:15 +0000 | [diff] [blame] | 1021 | bool SawParameterPack = false; |
| 1022 | SourceLocation ParameterPackLoc; |
| 1023 | |
Mike Stump | 1a35fde | 2009-02-11 23:03:27 +0000 | [diff] [blame] | 1024 | // Dummy initialization to avoid warnings. |
Douglas Gregor | 1bc6913 | 2009-02-11 20:46:19 +0000 | [diff] [blame] | 1025 | TemplateParameterList::iterator OldParam = NewParams->end(); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1026 | if (OldParams) |
| 1027 | OldParam = OldParams->begin(); |
| 1028 | |
| 1029 | for (TemplateParameterList::iterator NewParam = NewParams->begin(), |
| 1030 | NewParamEnd = NewParams->end(); |
| 1031 | NewParam != NewParamEnd; ++NewParam) { |
| 1032 | // Variables used to diagnose redundant default arguments |
| 1033 | bool RedundantDefaultArg = false; |
| 1034 | SourceLocation OldDefaultLoc; |
| 1035 | SourceLocation NewDefaultLoc; |
| 1036 | |
| 1037 | // Variables used to diagnose missing default arguments |
| 1038 | bool MissingDefaultArg = false; |
| 1039 | |
Anders Carlsson | 49d2557 | 2009-06-12 23:20:15 +0000 | [diff] [blame] | 1040 | // C++0x [temp.param]p11: |
| 1041 | // If a template parameter of a class template is a template parameter pack, |
| 1042 | // it must be the last template parameter. |
| 1043 | if (SawParameterPack) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1044 | Diag(ParameterPackLoc, |
Anders Carlsson | 49d2557 | 2009-06-12 23:20:15 +0000 | [diff] [blame] | 1045 | diag::err_template_param_pack_must_be_last_template_parameter); |
| 1046 | Invalid = true; |
| 1047 | } |
| 1048 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1049 | if (TemplateTypeParmDecl *NewTypeParm |
| 1050 | = dyn_cast<TemplateTypeParmDecl>(*NewParam)) { |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1051 | // Check the presence of a default argument here. |
| 1052 | if (NewTypeParm->hasDefaultArgument() && |
| 1053 | DiagnoseDefaultTemplateArgument(*this, TPC, |
| 1054 | NewTypeParm->getLocation(), |
| 1055 | NewTypeParm->getDefaultArgumentInfo()->getTypeLoc() |
| 1056 | .getFullSourceRange())) |
| 1057 | NewTypeParm->removeDefaultArgument(); |
| 1058 | |
| 1059 | // Merge default arguments for template type parameters. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1060 | TemplateTypeParmDecl *OldTypeParm |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1061 | = OldParams? cast<TemplateTypeParmDecl>(*OldParam) : 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1062 | |
Anders Carlsson | 49d2557 | 2009-06-12 23:20:15 +0000 | [diff] [blame] | 1063 | if (NewTypeParm->isParameterPack()) { |
| 1064 | assert(!NewTypeParm->hasDefaultArgument() && |
| 1065 | "Parameter packs can't have a default argument!"); |
| 1066 | SawParameterPack = true; |
| 1067 | ParameterPackLoc = NewTypeParm->getLocation(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1068 | } else if (OldTypeParm && OldTypeParm->hasDefaultArgument() && |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1069 | NewTypeParm->hasDefaultArgument()) { |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1070 | OldDefaultLoc = OldTypeParm->getDefaultArgumentLoc(); |
| 1071 | NewDefaultLoc = NewTypeParm->getDefaultArgumentLoc(); |
| 1072 | SawDefaultArgument = true; |
| 1073 | RedundantDefaultArg = true; |
| 1074 | PreviousDefaultArgLoc = NewDefaultLoc; |
| 1075 | } else if (OldTypeParm && OldTypeParm->hasDefaultArgument()) { |
| 1076 | // Merge the default argument from the old declaration to the |
| 1077 | // new declaration. |
| 1078 | SawDefaultArgument = true; |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1079 | NewTypeParm->setDefaultArgument(OldTypeParm->getDefaultArgumentInfo(), |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1080 | true); |
| 1081 | PreviousDefaultArgLoc = OldTypeParm->getDefaultArgumentLoc(); |
| 1082 | } else if (NewTypeParm->hasDefaultArgument()) { |
| 1083 | SawDefaultArgument = true; |
| 1084 | PreviousDefaultArgLoc = NewTypeParm->getDefaultArgumentLoc(); |
| 1085 | } else if (SawDefaultArgument) |
| 1086 | MissingDefaultArg = true; |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1087 | } else if (NonTypeTemplateParmDecl *NewNonTypeParm |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1088 | = dyn_cast<NonTypeTemplateParmDecl>(*NewParam)) { |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1089 | // Check the presence of a default argument here. |
| 1090 | if (NewNonTypeParm->hasDefaultArgument() && |
| 1091 | DiagnoseDefaultTemplateArgument(*this, TPC, |
| 1092 | NewNonTypeParm->getLocation(), |
| 1093 | NewNonTypeParm->getDefaultArgument()->getSourceRange())) { |
| 1094 | NewNonTypeParm->getDefaultArgument()->Destroy(Context); |
| 1095 | NewNonTypeParm->setDefaultArgument(0); |
| 1096 | } |
| 1097 | |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1098 | // Merge default arguments for non-type template parameters |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1099 | NonTypeTemplateParmDecl *OldNonTypeParm |
| 1100 | = OldParams? cast<NonTypeTemplateParmDecl>(*OldParam) : 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1101 | if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument() && |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1102 | NewNonTypeParm->hasDefaultArgument()) { |
| 1103 | OldDefaultLoc = OldNonTypeParm->getDefaultArgumentLoc(); |
| 1104 | NewDefaultLoc = NewNonTypeParm->getDefaultArgumentLoc(); |
| 1105 | SawDefaultArgument = true; |
| 1106 | RedundantDefaultArg = true; |
| 1107 | PreviousDefaultArgLoc = NewDefaultLoc; |
| 1108 | } else if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument()) { |
| 1109 | // Merge the default argument from the old declaration to the |
| 1110 | // new declaration. |
| 1111 | SawDefaultArgument = true; |
| 1112 | // FIXME: We need to create a new kind of "default argument" |
| 1113 | // expression that points to a previous template template |
| 1114 | // parameter. |
| 1115 | NewNonTypeParm->setDefaultArgument( |
| 1116 | OldNonTypeParm->getDefaultArgument()); |
| 1117 | PreviousDefaultArgLoc = OldNonTypeParm->getDefaultArgumentLoc(); |
| 1118 | } else if (NewNonTypeParm->hasDefaultArgument()) { |
| 1119 | SawDefaultArgument = true; |
| 1120 | PreviousDefaultArgLoc = NewNonTypeParm->getDefaultArgumentLoc(); |
| 1121 | } else if (SawDefaultArgument) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1122 | MissingDefaultArg = true; |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1123 | } else { |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1124 | // Check the presence of a default argument here. |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1125 | TemplateTemplateParmDecl *NewTemplateParm |
| 1126 | = cast<TemplateTemplateParmDecl>(*NewParam); |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1127 | if (NewTemplateParm->hasDefaultArgument() && |
| 1128 | DiagnoseDefaultTemplateArgument(*this, TPC, |
| 1129 | NewTemplateParm->getLocation(), |
| 1130 | NewTemplateParm->getDefaultArgument().getSourceRange())) |
| 1131 | NewTemplateParm->setDefaultArgument(TemplateArgumentLoc()); |
| 1132 | |
| 1133 | // Merge default arguments for template template parameters |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1134 | TemplateTemplateParmDecl *OldTemplateParm |
| 1135 | = OldParams? cast<TemplateTemplateParmDecl>(*OldParam) : 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1136 | if (OldTemplateParm && OldTemplateParm->hasDefaultArgument() && |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1137 | NewTemplateParm->hasDefaultArgument()) { |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1138 | OldDefaultLoc = OldTemplateParm->getDefaultArgument().getLocation(); |
| 1139 | NewDefaultLoc = NewTemplateParm->getDefaultArgument().getLocation(); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1140 | SawDefaultArgument = true; |
| 1141 | RedundantDefaultArg = true; |
| 1142 | PreviousDefaultArgLoc = NewDefaultLoc; |
| 1143 | } else if (OldTemplateParm && OldTemplateParm->hasDefaultArgument()) { |
| 1144 | // Merge the default argument from the old declaration to the |
| 1145 | // new declaration. |
| 1146 | SawDefaultArgument = true; |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 1147 | // FIXME: We need to create a new kind of "default argument" expression |
| 1148 | // that points to a previous template template parameter. |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1149 | NewTemplateParm->setDefaultArgument( |
| 1150 | OldTemplateParm->getDefaultArgument()); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1151 | PreviousDefaultArgLoc |
| 1152 | = OldTemplateParm->getDefaultArgument().getLocation(); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1153 | } else if (NewTemplateParm->hasDefaultArgument()) { |
| 1154 | SawDefaultArgument = true; |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1155 | PreviousDefaultArgLoc |
| 1156 | = NewTemplateParm->getDefaultArgument().getLocation(); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1157 | } else if (SawDefaultArgument) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1158 | MissingDefaultArg = true; |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1159 | } |
| 1160 | |
| 1161 | if (RedundantDefaultArg) { |
| 1162 | // C++ [temp.param]p12: |
| 1163 | // A template-parameter shall not be given default arguments |
| 1164 | // by two different declarations in the same scope. |
| 1165 | Diag(NewDefaultLoc, diag::err_template_param_default_arg_redefinition); |
| 1166 | Diag(OldDefaultLoc, diag::note_template_param_prev_default_arg); |
| 1167 | Invalid = true; |
| 1168 | } else if (MissingDefaultArg) { |
| 1169 | // C++ [temp.param]p11: |
| 1170 | // If a template-parameter has a default template-argument, |
| 1171 | // all subsequent template-parameters shall have a default |
| 1172 | // template-argument supplied. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1173 | Diag((*NewParam)->getLocation(), |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1174 | diag::err_template_param_default_arg_missing); |
| 1175 | Diag(PreviousDefaultArgLoc, diag::note_template_param_prev_default_arg); |
| 1176 | Invalid = true; |
| 1177 | } |
| 1178 | |
| 1179 | // If we have an old template parameter list that we're merging |
| 1180 | // in, move on to the next parameter. |
| 1181 | if (OldParams) |
| 1182 | ++OldParam; |
| 1183 | } |
| 1184 | |
| 1185 | return Invalid; |
| 1186 | } |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 1187 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1188 | /// \brief Match the given template parameter lists to the given scope |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1189 | /// specifier, returning the template parameter list that applies to the |
| 1190 | /// name. |
| 1191 | /// |
| 1192 | /// \param DeclStartLoc the start of the declaration that has a scope |
| 1193 | /// specifier or a template parameter list. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1194 | /// |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1195 | /// \param SS the scope specifier that will be matched to the given template |
| 1196 | /// parameter lists. This scope specifier precedes a qualified name that is |
| 1197 | /// being declared. |
| 1198 | /// |
| 1199 | /// \param ParamLists the template parameter lists, from the outermost to the |
| 1200 | /// innermost template parameter lists. |
| 1201 | /// |
| 1202 | /// \param NumParamLists the number of template parameter lists in ParamLists. |
| 1203 | /// |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 1204 | /// \param IsExplicitSpecialization will be set true if the entity being |
| 1205 | /// declared is an explicit specialization, false otherwise. |
| 1206 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1207 | /// \returns the template parameter list, if any, that corresponds to the |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1208 | /// name that is preceded by the scope specifier @p SS. This template |
| 1209 | /// parameter list may be have template parameters (if we're declaring a |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1210 | /// template) or may have no template parameters (if we're declaring a |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1211 | /// template specialization), or may be NULL (if we were's declaring isn't |
| 1212 | /// itself a template). |
| 1213 | TemplateParameterList * |
| 1214 | Sema::MatchTemplateParametersToScopeSpecifier(SourceLocation DeclStartLoc, |
| 1215 | const CXXScopeSpec &SS, |
| 1216 | TemplateParameterList **ParamLists, |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 1217 | unsigned NumParamLists, |
| 1218 | bool &IsExplicitSpecialization) { |
| 1219 | IsExplicitSpecialization = false; |
| 1220 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1221 | // Find the template-ids that occur within the nested-name-specifier. These |
| 1222 | // template-ids will match up with the template parameter lists. |
| 1223 | llvm::SmallVector<const TemplateSpecializationType *, 4> |
| 1224 | TemplateIdsInSpecifier; |
Douglas Gregor | 3ebd753 | 2009-11-23 12:11:45 +0000 | [diff] [blame] | 1225 | llvm::SmallVector<ClassTemplateSpecializationDecl *, 4> |
| 1226 | ExplicitSpecializationsInSpecifier; |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1227 | for (NestedNameSpecifier *NNS = (NestedNameSpecifier *)SS.getScopeRep(); |
| 1228 | NNS; NNS = NNS->getPrefix()) { |
John McCall | 4b2b02b | 2009-12-15 02:19:47 +0000 | [diff] [blame] | 1229 | const Type *T = NNS->getAsType(); |
| 1230 | if (!T) break; |
| 1231 | |
| 1232 | // C++0x [temp.expl.spec]p17: |
| 1233 | // A member or a member template may be nested within many |
| 1234 | // enclosing class templates. In an explicit specialization for |
| 1235 | // such a member, the member declaration shall be preceded by a |
| 1236 | // template<> for each enclosing class template that is |
| 1237 | // explicitly specialized. |
| 1238 | // We interpret this as forbidding typedefs of template |
| 1239 | // specializations in the scope specifiers of out-of-line decls. |
| 1240 | if (const TypedefType *TT = dyn_cast<TypedefType>(T)) { |
| 1241 | const Type *UnderlyingT = TT->LookThroughTypedefs().getTypePtr(); |
| 1242 | if (isa<TemplateSpecializationType>(UnderlyingT)) |
| 1243 | // FIXME: better source location information. |
| 1244 | Diag(DeclStartLoc, diag::err_typedef_in_def_scope) << QualType(T,0); |
| 1245 | T = UnderlyingT; |
| 1246 | } |
| 1247 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1248 | if (const TemplateSpecializationType *SpecType |
John McCall | 4b2b02b | 2009-12-15 02:19:47 +0000 | [diff] [blame] | 1249 | = dyn_cast<TemplateSpecializationType>(T)) { |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1250 | TemplateDecl *Template = SpecType->getTemplateName().getAsTemplateDecl(); |
| 1251 | if (!Template) |
| 1252 | continue; // FIXME: should this be an error? probably... |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1253 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1254 | if (const RecordType *Record = SpecType->getAs<RecordType>()) { |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1255 | ClassTemplateSpecializationDecl *SpecDecl |
| 1256 | = cast<ClassTemplateSpecializationDecl>(Record->getDecl()); |
| 1257 | // If the nested name specifier refers to an explicit specialization, |
| 1258 | // we don't need a template<> header. |
Douglas Gregor | 3ebd753 | 2009-11-23 12:11:45 +0000 | [diff] [blame] | 1259 | if (SpecDecl->getSpecializationKind() == TSK_ExplicitSpecialization) { |
| 1260 | ExplicitSpecializationsInSpecifier.push_back(SpecDecl); |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1261 | continue; |
Douglas Gregor | 3ebd753 | 2009-11-23 12:11:45 +0000 | [diff] [blame] | 1262 | } |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1263 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1264 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1265 | TemplateIdsInSpecifier.push_back(SpecType); |
| 1266 | } |
| 1267 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1268 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1269 | // Reverse the list of template-ids in the scope specifier, so that we can |
| 1270 | // more easily match up the template-ids and the template parameter lists. |
| 1271 | std::reverse(TemplateIdsInSpecifier.begin(), TemplateIdsInSpecifier.end()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1272 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1273 | SourceLocation FirstTemplateLoc = DeclStartLoc; |
| 1274 | if (NumParamLists) |
| 1275 | FirstTemplateLoc = ParamLists[0]->getTemplateLoc(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1276 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1277 | // Match the template-ids found in the specifier to the template parameter |
| 1278 | // lists. |
| 1279 | unsigned Idx = 0; |
| 1280 | for (unsigned NumTemplateIds = TemplateIdsInSpecifier.size(); |
| 1281 | Idx != NumTemplateIds; ++Idx) { |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 1282 | QualType TemplateId = QualType(TemplateIdsInSpecifier[Idx], 0); |
| 1283 | bool DependentTemplateId = TemplateId->isDependentType(); |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1284 | if (Idx >= NumParamLists) { |
| 1285 | // We have a template-id without a corresponding template parameter |
| 1286 | // list. |
| 1287 | if (DependentTemplateId) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1288 | // FIXME: the location information here isn't great. |
| 1289 | Diag(SS.getRange().getBegin(), |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1290 | diag::err_template_spec_needs_template_parameters) |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 1291 | << TemplateId |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1292 | << SS.getRange(); |
| 1293 | } else { |
| 1294 | Diag(SS.getRange().getBegin(), diag::err_template_spec_needs_header) |
| 1295 | << SS.getRange() |
| 1296 | << CodeModificationHint::CreateInsertion(FirstTemplateLoc, |
| 1297 | "template<> "); |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 1298 | IsExplicitSpecialization = true; |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1299 | } |
| 1300 | return 0; |
| 1301 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1302 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1303 | // Check the template parameter list against its corresponding template-id. |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 1304 | if (DependentTemplateId) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1305 | TemplateDecl *Template |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 1306 | = TemplateIdsInSpecifier[Idx]->getTemplateName().getAsTemplateDecl(); |
| 1307 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1308 | if (ClassTemplateDecl *ClassTemplate |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 1309 | = dyn_cast<ClassTemplateDecl>(Template)) { |
| 1310 | TemplateParameterList *ExpectedTemplateParams = 0; |
| 1311 | // Is this template-id naming the primary template? |
| 1312 | if (Context.hasSameType(TemplateId, |
| 1313 | ClassTemplate->getInjectedClassNameType(Context))) |
| 1314 | ExpectedTemplateParams = ClassTemplate->getTemplateParameters(); |
| 1315 | // ... or a partial specialization? |
| 1316 | else if (ClassTemplatePartialSpecializationDecl *PartialSpec |
| 1317 | = ClassTemplate->findPartialSpecialization(TemplateId)) |
| 1318 | ExpectedTemplateParams = PartialSpec->getTemplateParameters(); |
| 1319 | |
| 1320 | if (ExpectedTemplateParams) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1321 | TemplateParameterListsAreEqual(ParamLists[Idx], |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 1322 | ExpectedTemplateParams, |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 1323 | true, TPL_TemplateMatch); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1324 | } |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1325 | |
| 1326 | CheckTemplateParameterList(ParamLists[Idx], 0, TPC_ClassTemplateMember); |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 1327 | } else if (ParamLists[Idx]->size() > 0) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1328 | Diag(ParamLists[Idx]->getTemplateLoc(), |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 1329 | diag::err_template_param_list_matches_nontemplate) |
| 1330 | << TemplateId |
| 1331 | << ParamLists[Idx]->getSourceRange(); |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 1332 | else |
| 1333 | IsExplicitSpecialization = true; |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1334 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1335 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1336 | // If there were at least as many template-ids as there were template |
| 1337 | // parameter lists, then there are no template parameter lists remaining for |
| 1338 | // the declaration itself. |
| 1339 | if (Idx >= NumParamLists) |
| 1340 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1341 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1342 | // If there were too many template parameter lists, complain about that now. |
| 1343 | if (Idx != NumParamLists - 1) { |
| 1344 | while (Idx < NumParamLists - 1) { |
Douglas Gregor | 3ebd753 | 2009-11-23 12:11:45 +0000 | [diff] [blame] | 1345 | bool isExplicitSpecHeader = ParamLists[Idx]->size() == 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1346 | Diag(ParamLists[Idx]->getTemplateLoc(), |
Douglas Gregor | 3ebd753 | 2009-11-23 12:11:45 +0000 | [diff] [blame] | 1347 | isExplicitSpecHeader? diag::warn_template_spec_extra_headers |
| 1348 | : diag::err_template_spec_extra_headers) |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1349 | << SourceRange(ParamLists[Idx]->getTemplateLoc(), |
| 1350 | ParamLists[Idx]->getRAngleLoc()); |
Douglas Gregor | 3ebd753 | 2009-11-23 12:11:45 +0000 | [diff] [blame] | 1351 | |
| 1352 | if (isExplicitSpecHeader && !ExplicitSpecializationsInSpecifier.empty()) { |
| 1353 | Diag(ExplicitSpecializationsInSpecifier.back()->getLocation(), |
| 1354 | diag::note_explicit_template_spec_does_not_need_header) |
| 1355 | << ExplicitSpecializationsInSpecifier.back(); |
| 1356 | ExplicitSpecializationsInSpecifier.pop_back(); |
| 1357 | } |
| 1358 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1359 | ++Idx; |
| 1360 | } |
| 1361 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1362 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1363 | // Return the last template parameter list, which corresponds to the |
| 1364 | // entity being declared. |
| 1365 | return ParamLists[NumParamLists - 1]; |
| 1366 | } |
| 1367 | |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1368 | QualType Sema::CheckTemplateIdType(TemplateName Name, |
| 1369 | SourceLocation TemplateLoc, |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1370 | const TemplateArgumentListInfo &TemplateArgs) { |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1371 | TemplateDecl *Template = Name.getAsTemplateDecl(); |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 1372 | if (!Template) { |
| 1373 | // The template name does not resolve to a template, so we just |
| 1374 | // build a dependent template-id type. |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1375 | return Context.getTemplateSpecializationType(Name, TemplateArgs); |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 1376 | } |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1377 | |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1378 | // Check that the template argument list is well-formed for this |
| 1379 | // template. |
Anders Carlsson | fb25052 | 2009-06-23 01:26:57 +0000 | [diff] [blame] | 1380 | TemplateArgumentListBuilder Converted(Template->getTemplateParameters(), |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1381 | TemplateArgs.size()); |
| 1382 | if (CheckTemplateArgumentList(Template, TemplateLoc, TemplateArgs, |
Douglas Gregor | 16134c6 | 2009-07-01 00:28:38 +0000 | [diff] [blame] | 1383 | false, Converted)) |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1384 | return QualType(); |
| 1385 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1386 | assert((Converted.structuredSize() == |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1387 | Template->getTemplateParameters()->size()) && |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1388 | "Converted template argument list is too short!"); |
| 1389 | |
| 1390 | QualType CanonType; |
| 1391 | |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 1392 | if (Name.isDependent() || |
| 1393 | TemplateSpecializationType::anyDependentTemplateArguments( |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1394 | TemplateArgs)) { |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1395 | // This class template specialization is a dependent |
| 1396 | // type. Therefore, its canonical type is another class template |
| 1397 | // specialization type that contains all of the converted |
| 1398 | // arguments in canonical form. This ensures that, e.g., A<T> and |
| 1399 | // A<T, T> have identical types when A is declared as: |
| 1400 | // |
| 1401 | // template<typename T, typename U = T> struct A; |
Douglas Gregor | 25a3ef7 | 2009-05-07 06:41:52 +0000 | [diff] [blame] | 1402 | TemplateName CanonName = Context.getCanonicalTemplateName(Name); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1403 | CanonType = Context.getTemplateSpecializationType(CanonName, |
Anders Carlsson | fb25052 | 2009-06-23 01:26:57 +0000 | [diff] [blame] | 1404 | Converted.getFlatArguments(), |
| 1405 | Converted.flatSize()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1406 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 1407 | // FIXME: CanonType is not actually the canonical type, and unfortunately |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1408 | // it is a TemplateSpecializationType that we will never use again. |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 1409 | // In the future, we need to teach getTemplateSpecializationType to only |
| 1410 | // build the canonical type and return that to us. |
| 1411 | CanonType = Context.getCanonicalType(CanonType); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1412 | } else if (ClassTemplateDecl *ClassTemplate |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1413 | = dyn_cast<ClassTemplateDecl>(Template)) { |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1414 | // Find the class template specialization declaration that |
| 1415 | // corresponds to these arguments. |
| 1416 | llvm::FoldingSetNodeID ID; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1417 | ClassTemplateSpecializationDecl::Profile(ID, |
Anders Carlsson | fb25052 | 2009-06-23 01:26:57 +0000 | [diff] [blame] | 1418 | Converted.getFlatArguments(), |
Douglas Gregor | 828e226 | 2009-07-29 16:09:57 +0000 | [diff] [blame] | 1419 | Converted.flatSize(), |
| 1420 | Context); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1421 | void *InsertPos = 0; |
| 1422 | ClassTemplateSpecializationDecl *Decl |
| 1423 | = ClassTemplate->getSpecializations().FindNodeOrInsertPos(ID, InsertPos); |
| 1424 | if (!Decl) { |
| 1425 | // This is the first time we have referenced this class template |
| 1426 | // specialization. Create the canonical declaration and add it to |
| 1427 | // the set of specializations. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1428 | Decl = ClassTemplateSpecializationDecl::Create(Context, |
Anders Carlsson | 1c5976e | 2009-06-05 03:43:12 +0000 | [diff] [blame] | 1429 | ClassTemplate->getDeclContext(), |
John McCall | 9cc7807 | 2009-09-11 07:25:08 +0000 | [diff] [blame] | 1430 | ClassTemplate->getLocation(), |
Anders Carlsson | 1c5976e | 2009-06-05 03:43:12 +0000 | [diff] [blame] | 1431 | ClassTemplate, |
Anders Carlsson | fb25052 | 2009-06-23 01:26:57 +0000 | [diff] [blame] | 1432 | Converted, 0); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1433 | ClassTemplate->getSpecializations().InsertNode(Decl, InsertPos); |
| 1434 | Decl->setLexicalDeclContext(CurContext); |
| 1435 | } |
| 1436 | |
| 1437 | CanonType = Context.getTypeDeclType(Decl); |
| 1438 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1439 | |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1440 | // Build the fully-sugared type for this class template |
| 1441 | // specialization, which refers back to the class template |
| 1442 | // specialization we created or found. |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1443 | return Context.getTemplateSpecializationType(Name, TemplateArgs, CanonType); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1444 | } |
| 1445 | |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 1446 | Action::TypeResult |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1447 | Sema::ActOnTemplateIdType(TemplateTy TemplateD, SourceLocation TemplateLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1448 | SourceLocation LAngleLoc, |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1449 | ASTTemplateArgsPtr TemplateArgsIn, |
John McCall | 6b2becf | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 1450 | SourceLocation RAngleLoc) { |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1451 | TemplateName Template = TemplateD.getAsVal<TemplateName>(); |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 1452 | |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1453 | // Translate the parser's template argument list in our AST format. |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1454 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
Douglas Gregor | 314b97f | 2009-11-10 19:49:08 +0000 | [diff] [blame] | 1455 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 1456 | |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1457 | QualType Result = CheckTemplateIdType(Template, TemplateLoc, TemplateArgs); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1458 | TemplateArgsIn.release(); |
Douglas Gregor | 31a19b6 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 1459 | |
| 1460 | if (Result.isNull()) |
| 1461 | return true; |
| 1462 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1463 | TypeSourceInfo *DI = Context.CreateTypeSourceInfo(Result); |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1464 | TemplateSpecializationTypeLoc TL |
| 1465 | = cast<TemplateSpecializationTypeLoc>(DI->getTypeLoc()); |
| 1466 | TL.setTemplateNameLoc(TemplateLoc); |
| 1467 | TL.setLAngleLoc(LAngleLoc); |
| 1468 | TL.setRAngleLoc(RAngleLoc); |
| 1469 | for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) |
| 1470 | TL.setArgLocInfo(i, TemplateArgs[i].getLocInfo()); |
| 1471 | |
| 1472 | return CreateLocInfoType(Result, DI).getAsOpaquePtr(); |
John McCall | 6b2becf | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 1473 | } |
John McCall | f1bbbb4 | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 1474 | |
John McCall | 6b2becf | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 1475 | Sema::TypeResult Sema::ActOnTagTemplateIdType(TypeResult TypeResult, |
| 1476 | TagUseKind TUK, |
| 1477 | DeclSpec::TST TagSpec, |
| 1478 | SourceLocation TagLoc) { |
| 1479 | if (TypeResult.isInvalid()) |
| 1480 | return Sema::TypeResult(); |
John McCall | f1bbbb4 | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 1481 | |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1482 | // FIXME: preserve source info, ideally without copying the DI. |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1483 | TypeSourceInfo *DI; |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1484 | QualType Type = GetTypeFromParser(TypeResult.get(), &DI); |
John McCall | f1bbbb4 | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 1485 | |
John McCall | 6b2becf | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 1486 | // Verify the tag specifier. |
| 1487 | TagDecl::TagKind TagKind = TagDecl::getTagKindForTypeSpec(TagSpec); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1488 | |
John McCall | 6b2becf | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 1489 | if (const RecordType *RT = Type->getAs<RecordType>()) { |
| 1490 | RecordDecl *D = RT->getDecl(); |
| 1491 | |
| 1492 | IdentifierInfo *Id = D->getIdentifier(); |
| 1493 | assert(Id && "templated class must have an identifier"); |
| 1494 | |
| 1495 | if (!isAcceptableTagRedeclaration(D, TagKind, TagLoc, *Id)) { |
| 1496 | Diag(TagLoc, diag::err_use_with_wrong_tag) |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 1497 | << Type |
John McCall | 6b2becf | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 1498 | << CodeModificationHint::CreateReplacement(SourceRange(TagLoc), |
| 1499 | D->getKindName()); |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 1500 | Diag(D->getLocation(), diag::note_previous_use); |
John McCall | f1bbbb4 | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 1501 | } |
| 1502 | } |
| 1503 | |
John McCall | 6b2becf | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 1504 | QualType ElabType = Context.getElaboratedType(Type, TagKind); |
| 1505 | |
| 1506 | return ElabType.getAsOpaquePtr(); |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 1507 | } |
| 1508 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1509 | Sema::OwningExprResult Sema::BuildTemplateIdExpr(const CXXScopeSpec &SS, |
| 1510 | LookupResult &R, |
| 1511 | bool RequiresADL, |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1512 | const TemplateArgumentListInfo &TemplateArgs) { |
Douglas Gregor | edce4dd | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 1513 | // FIXME: Can we do any checking at this point? I guess we could check the |
| 1514 | // template arguments that we have against the template name, if the template |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1515 | // 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] | 1516 | // though. |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1517 | |
| 1518 | // These should be filtered out by our callers. |
| 1519 | assert(!R.empty() && "empty lookup results when building templateid"); |
| 1520 | assert(!R.isAmbiguous() && "ambiguous lookup when building templateid"); |
| 1521 | |
| 1522 | NestedNameSpecifier *Qualifier = 0; |
| 1523 | SourceRange QualifierRange; |
| 1524 | if (SS.isSet()) { |
| 1525 | Qualifier = static_cast<NestedNameSpecifier*>(SS.getScopeRep()); |
| 1526 | QualifierRange = SS.getRange(); |
Douglas Gregor | a9e29aa | 2009-10-22 07:19:14 +0000 | [diff] [blame] | 1527 | } |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 1528 | |
| 1529 | // We don't want lookup warnings at this point. |
| 1530 | R.suppressDiagnostics(); |
Douglas Gregor | a9e29aa | 2009-10-22 07:19:14 +0000 | [diff] [blame] | 1531 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1532 | bool Dependent |
| 1533 | = UnresolvedLookupExpr::ComputeDependence(R.begin(), R.end(), |
| 1534 | &TemplateArgs); |
| 1535 | UnresolvedLookupExpr *ULE |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 1536 | = UnresolvedLookupExpr::Create(Context, Dependent, R.getNamingClass(), |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1537 | Qualifier, QualifierRange, |
| 1538 | R.getLookupName(), R.getNameLoc(), |
| 1539 | RequiresADL, TemplateArgs); |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 1540 | ULE->addDecls(R.begin(), R.end()); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1541 | |
| 1542 | return Owned(ULE); |
Douglas Gregor | edce4dd | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 1543 | } |
| 1544 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1545 | // We actually only call this from template instantiation. |
| 1546 | Sema::OwningExprResult |
| 1547 | Sema::BuildQualifiedTemplateIdExpr(const CXXScopeSpec &SS, |
| 1548 | DeclarationName Name, |
| 1549 | SourceLocation NameLoc, |
| 1550 | const TemplateArgumentListInfo &TemplateArgs) { |
| 1551 | DeclContext *DC; |
| 1552 | if (!(DC = computeDeclContext(SS, false)) || |
| 1553 | DC->isDependentContext() || |
| 1554 | RequireCompleteDeclContext(SS)) |
| 1555 | return BuildDependentDeclRefExpr(SS, Name, NameLoc, &TemplateArgs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1556 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1557 | LookupResult R(*this, Name, NameLoc, LookupOrdinaryName); |
| 1558 | LookupTemplateName(R, (Scope*) 0, SS, QualType(), /*Entering*/ false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1559 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1560 | if (R.isAmbiguous()) |
| 1561 | return ExprError(); |
| 1562 | |
| 1563 | if (R.empty()) { |
| 1564 | Diag(NameLoc, diag::err_template_kw_refers_to_non_template) |
| 1565 | << Name << SS.getRange(); |
| 1566 | return ExprError(); |
| 1567 | } |
| 1568 | |
| 1569 | if (ClassTemplateDecl *Temp = R.getAsSingle<ClassTemplateDecl>()) { |
| 1570 | Diag(NameLoc, diag::err_template_kw_refers_to_class_template) |
| 1571 | << (NestedNameSpecifier*) SS.getScopeRep() << Name << SS.getRange(); |
| 1572 | Diag(Temp->getLocation(), diag::note_referenced_class_template); |
| 1573 | return ExprError(); |
| 1574 | } |
| 1575 | |
| 1576 | return BuildTemplateIdExpr(SS, R, /* ADL */ false, TemplateArgs); |
Douglas Gregor | edce4dd | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 1577 | } |
| 1578 | |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 1579 | /// \brief Form a dependent template name. |
| 1580 | /// |
| 1581 | /// This action forms a dependent template name given the template |
| 1582 | /// name and its (presumably dependent) scope specifier. For |
| 1583 | /// example, given "MetaFun::template apply", the scope specifier \p |
| 1584 | /// SS will be "MetaFun::", \p TemplateKWLoc contains the location |
| 1585 | /// of the "template" keyword, and "apply" is the \p Name. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1586 | Sema::TemplateTy |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 1587 | Sema::ActOnDependentTemplateName(SourceLocation TemplateKWLoc, |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 1588 | const CXXScopeSpec &SS, |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 1589 | UnqualifiedId &Name, |
Douglas Gregor | a481edb | 2009-11-20 23:39:24 +0000 | [diff] [blame] | 1590 | TypeTy *ObjectType, |
| 1591 | bool EnteringContext) { |
Douglas Gregor | 0707bc5 | 2010-01-19 16:01:07 +0000 | [diff] [blame] | 1592 | DeclContext *LookupCtx = 0; |
| 1593 | if (SS.isSet()) |
| 1594 | LookupCtx = computeDeclContext(SS, EnteringContext); |
| 1595 | if (!LookupCtx && ObjectType) |
| 1596 | LookupCtx = computeDeclContext(QualType::getFromOpaquePtr(ObjectType)); |
| 1597 | if (LookupCtx) { |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 1598 | // C++0x [temp.names]p5: |
| 1599 | // If a name prefixed by the keyword template is not the name of |
| 1600 | // a template, the program is ill-formed. [Note: the keyword |
| 1601 | // template may not be applied to non-template members of class |
| 1602 | // templates. -end note ] [ Note: as is the case with the |
| 1603 | // typename prefix, the template prefix is allowed in cases |
| 1604 | // where it is not strictly necessary; i.e., when the |
| 1605 | // nested-name-specifier or the expression on the left of the -> |
| 1606 | // or . is not dependent on a template-parameter, or the use |
| 1607 | // does not appear in the scope of a template. -end note] |
| 1608 | // |
| 1609 | // Note: C++03 was more strict here, because it banned the use of |
| 1610 | // the "template" keyword prior to a template-name that was not a |
| 1611 | // dependent name. C++ DR468 relaxed this requirement (the |
| 1612 | // "template" keyword is now permitted). We follow the C++0x |
| 1613 | // rules, even in C++03 mode, retroactively applying the DR. |
| 1614 | TemplateTy Template; |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 1615 | TemplateNameKind TNK = isTemplateName(0, SS, Name, ObjectType, |
Douglas Gregor | a481edb | 2009-11-20 23:39:24 +0000 | [diff] [blame] | 1616 | EnteringContext, Template); |
Douglas Gregor | 0707bc5 | 2010-01-19 16:01:07 +0000 | [diff] [blame] | 1617 | if (TNK == TNK_Non_template && LookupCtx->isDependentContext() && |
| 1618 | isa<CXXRecordDecl>(LookupCtx) && |
| 1619 | cast<CXXRecordDecl>(LookupCtx)->hasAnyDependentBases()) { |
Douglas Gregor | 9edad9b | 2010-01-14 17:47:39 +0000 | [diff] [blame] | 1620 | // This is a dependent template. |
| 1621 | } else if (TNK == TNK_Non_template) { |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 1622 | Diag(Name.getSourceRange().getBegin(), |
| 1623 | diag::err_template_kw_refers_to_non_template) |
| 1624 | << GetNameFromUnqualifiedId(Name) |
| 1625 | << Name.getSourceRange(); |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 1626 | return TemplateTy(); |
Douglas Gregor | 9edad9b | 2010-01-14 17:47:39 +0000 | [diff] [blame] | 1627 | } else { |
| 1628 | // We found something; return it. |
| 1629 | return Template; |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 1630 | } |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 1631 | } |
| 1632 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1633 | NestedNameSpecifier *Qualifier |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 1634 | = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 1635 | |
| 1636 | switch (Name.getKind()) { |
| 1637 | case UnqualifiedId::IK_Identifier: |
| 1638 | return TemplateTy::make(Context.getDependentTemplateName(Qualifier, |
| 1639 | Name.Identifier)); |
| 1640 | |
Douglas Gregor | ca1bdd7 | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 1641 | case UnqualifiedId::IK_OperatorFunctionId: |
| 1642 | return TemplateTy::make(Context.getDependentTemplateName(Qualifier, |
| 1643 | Name.OperatorFunctionId.Operator)); |
Sean Hunt | e6252d1 | 2009-11-28 08:58:14 +0000 | [diff] [blame] | 1644 | |
| 1645 | case UnqualifiedId::IK_LiteralOperatorId: |
| 1646 | assert(false && "We don't support these; Parse shouldn't have allowed propagation"); |
| 1647 | |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 1648 | default: |
| 1649 | break; |
| 1650 | } |
| 1651 | |
| 1652 | Diag(Name.getSourceRange().getBegin(), |
| 1653 | diag::err_template_kw_refers_to_non_template) |
| 1654 | << GetNameFromUnqualifiedId(Name) |
| 1655 | << Name.getSourceRange(); |
| 1656 | return TemplateTy(); |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 1657 | } |
| 1658 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1659 | bool Sema::CheckTemplateTypeArgument(TemplateTypeParmDecl *Param, |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1660 | const TemplateArgumentLoc &AL, |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 1661 | TemplateArgumentListBuilder &Converted) { |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1662 | const TemplateArgument &Arg = AL.getArgument(); |
| 1663 | |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 1664 | // Check template type parameter. |
| 1665 | if (Arg.getKind() != TemplateArgument::Type) { |
| 1666 | // C++ [temp.arg.type]p1: |
| 1667 | // A template-argument for a template-parameter which is a |
| 1668 | // type shall be a type-id. |
| 1669 | |
| 1670 | // We have a template type parameter but the template argument |
| 1671 | // is not a type. |
John McCall | 828bff2 | 2009-10-29 18:45:58 +0000 | [diff] [blame] | 1672 | SourceRange SR = AL.getSourceRange(); |
| 1673 | Diag(SR.getBegin(), diag::err_template_arg_must_be_type) << SR; |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 1674 | Diag(Param->getLocation(), diag::note_template_param_here); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1675 | |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 1676 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1677 | } |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 1678 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1679 | if (CheckTemplateArgument(Param, AL.getTypeSourceInfo())) |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 1680 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1681 | |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 1682 | // Add the converted template type argument. |
Anders Carlsson | fb25052 | 2009-06-23 01:26:57 +0000 | [diff] [blame] | 1683 | Converted.Append( |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1684 | TemplateArgument(Context.getCanonicalType(Arg.getAsType()))); |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 1685 | return false; |
| 1686 | } |
| 1687 | |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 1688 | /// \brief Substitute template arguments into the default template argument for |
| 1689 | /// the given template type parameter. |
| 1690 | /// |
| 1691 | /// \param SemaRef the semantic analysis object for which we are performing |
| 1692 | /// the substitution. |
| 1693 | /// |
| 1694 | /// \param Template the template that we are synthesizing template arguments |
| 1695 | /// for. |
| 1696 | /// |
| 1697 | /// \param TemplateLoc the location of the template name that started the |
| 1698 | /// template-id we are checking. |
| 1699 | /// |
| 1700 | /// \param RAngleLoc the location of the right angle bracket ('>') that |
| 1701 | /// terminates the template-id. |
| 1702 | /// |
| 1703 | /// \param Param the template template parameter whose default we are |
| 1704 | /// substituting into. |
| 1705 | /// |
| 1706 | /// \param Converted the list of template arguments provided for template |
| 1707 | /// parameters that precede \p Param in the template parameter list. |
| 1708 | /// |
| 1709 | /// \returns the substituted template argument, or NULL if an error occurred. |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1710 | static TypeSourceInfo * |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 1711 | SubstDefaultTemplateArgument(Sema &SemaRef, |
| 1712 | TemplateDecl *Template, |
| 1713 | SourceLocation TemplateLoc, |
| 1714 | SourceLocation RAngleLoc, |
| 1715 | TemplateTypeParmDecl *Param, |
| 1716 | TemplateArgumentListBuilder &Converted) { |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1717 | TypeSourceInfo *ArgType = Param->getDefaultArgumentInfo(); |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 1718 | |
| 1719 | // If the argument type is dependent, instantiate it now based |
| 1720 | // on the previously-computed template arguments. |
| 1721 | if (ArgType->getType()->isDependentType()) { |
| 1722 | TemplateArgumentList TemplateArgs(SemaRef.Context, Converted, |
| 1723 | /*TakeArgs=*/false); |
| 1724 | |
| 1725 | MultiLevelTemplateArgumentList AllTemplateArgs |
| 1726 | = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs); |
| 1727 | |
| 1728 | Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc, |
| 1729 | Template, Converted.getFlatArguments(), |
| 1730 | Converted.flatSize(), |
| 1731 | SourceRange(TemplateLoc, RAngleLoc)); |
| 1732 | |
| 1733 | ArgType = SemaRef.SubstType(ArgType, AllTemplateArgs, |
| 1734 | Param->getDefaultArgumentLoc(), |
| 1735 | Param->getDeclName()); |
| 1736 | } |
| 1737 | |
| 1738 | return ArgType; |
| 1739 | } |
| 1740 | |
| 1741 | /// \brief Substitute template arguments into the default template argument for |
| 1742 | /// the given non-type template parameter. |
| 1743 | /// |
| 1744 | /// \param SemaRef the semantic analysis object for which we are performing |
| 1745 | /// the substitution. |
| 1746 | /// |
| 1747 | /// \param Template the template that we are synthesizing template arguments |
| 1748 | /// for. |
| 1749 | /// |
| 1750 | /// \param TemplateLoc the location of the template name that started the |
| 1751 | /// template-id we are checking. |
| 1752 | /// |
| 1753 | /// \param RAngleLoc the location of the right angle bracket ('>') that |
| 1754 | /// terminates the template-id. |
| 1755 | /// |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1756 | /// \param Param the non-type template parameter whose default we are |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 1757 | /// substituting into. |
| 1758 | /// |
| 1759 | /// \param Converted the list of template arguments provided for template |
| 1760 | /// parameters that precede \p Param in the template parameter list. |
| 1761 | /// |
| 1762 | /// \returns the substituted template argument, or NULL if an error occurred. |
| 1763 | static Sema::OwningExprResult |
| 1764 | SubstDefaultTemplateArgument(Sema &SemaRef, |
| 1765 | TemplateDecl *Template, |
| 1766 | SourceLocation TemplateLoc, |
| 1767 | SourceLocation RAngleLoc, |
| 1768 | NonTypeTemplateParmDecl *Param, |
| 1769 | TemplateArgumentListBuilder &Converted) { |
| 1770 | TemplateArgumentList TemplateArgs(SemaRef.Context, Converted, |
| 1771 | /*TakeArgs=*/false); |
| 1772 | |
| 1773 | MultiLevelTemplateArgumentList AllTemplateArgs |
| 1774 | = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs); |
| 1775 | |
| 1776 | Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc, |
| 1777 | Template, Converted.getFlatArguments(), |
| 1778 | Converted.flatSize(), |
| 1779 | SourceRange(TemplateLoc, RAngleLoc)); |
| 1780 | |
| 1781 | return SemaRef.SubstExpr(Param->getDefaultArgument(), AllTemplateArgs); |
| 1782 | } |
| 1783 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1784 | /// \brief Substitute template arguments into the default template argument for |
| 1785 | /// the given template template parameter. |
| 1786 | /// |
| 1787 | /// \param SemaRef the semantic analysis object for which we are performing |
| 1788 | /// the substitution. |
| 1789 | /// |
| 1790 | /// \param Template the template that we are synthesizing template arguments |
| 1791 | /// for. |
| 1792 | /// |
| 1793 | /// \param TemplateLoc the location of the template name that started the |
| 1794 | /// template-id we are checking. |
| 1795 | /// |
| 1796 | /// \param RAngleLoc the location of the right angle bracket ('>') that |
| 1797 | /// terminates the template-id. |
| 1798 | /// |
| 1799 | /// \param Param the template template parameter whose default we are |
| 1800 | /// substituting into. |
| 1801 | /// |
| 1802 | /// \param Converted the list of template arguments provided for template |
| 1803 | /// parameters that precede \p Param in the template parameter list. |
| 1804 | /// |
| 1805 | /// \returns the substituted template argument, or NULL if an error occurred. |
| 1806 | static TemplateName |
| 1807 | SubstDefaultTemplateArgument(Sema &SemaRef, |
| 1808 | TemplateDecl *Template, |
| 1809 | SourceLocation TemplateLoc, |
| 1810 | SourceLocation RAngleLoc, |
| 1811 | TemplateTemplateParmDecl *Param, |
| 1812 | TemplateArgumentListBuilder &Converted) { |
| 1813 | TemplateArgumentList TemplateArgs(SemaRef.Context, Converted, |
| 1814 | /*TakeArgs=*/false); |
| 1815 | |
| 1816 | MultiLevelTemplateArgumentList AllTemplateArgs |
| 1817 | = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs); |
| 1818 | |
| 1819 | Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc, |
| 1820 | Template, Converted.getFlatArguments(), |
| 1821 | Converted.flatSize(), |
| 1822 | SourceRange(TemplateLoc, RAngleLoc)); |
| 1823 | |
| 1824 | return SemaRef.SubstTemplateName( |
| 1825 | Param->getDefaultArgument().getArgument().getAsTemplate(), |
| 1826 | Param->getDefaultArgument().getTemplateNameLoc(), |
| 1827 | AllTemplateArgs); |
| 1828 | } |
| 1829 | |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 1830 | /// \brief If the given template parameter has a default template |
| 1831 | /// argument, substitute into that default template argument and |
| 1832 | /// return the corresponding template argument. |
| 1833 | TemplateArgumentLoc |
| 1834 | Sema::SubstDefaultTemplateArgumentIfAvailable(TemplateDecl *Template, |
| 1835 | SourceLocation TemplateLoc, |
| 1836 | SourceLocation RAngleLoc, |
| 1837 | Decl *Param, |
| 1838 | TemplateArgumentListBuilder &Converted) { |
| 1839 | if (TemplateTypeParmDecl *TypeParm = dyn_cast<TemplateTypeParmDecl>(Param)) { |
| 1840 | if (!TypeParm->hasDefaultArgument()) |
| 1841 | return TemplateArgumentLoc(); |
| 1842 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1843 | TypeSourceInfo *DI = SubstDefaultTemplateArgument(*this, Template, |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 1844 | TemplateLoc, |
| 1845 | RAngleLoc, |
| 1846 | TypeParm, |
| 1847 | Converted); |
| 1848 | if (DI) |
| 1849 | return TemplateArgumentLoc(TemplateArgument(DI->getType()), DI); |
| 1850 | |
| 1851 | return TemplateArgumentLoc(); |
| 1852 | } |
| 1853 | |
| 1854 | if (NonTypeTemplateParmDecl *NonTypeParm |
| 1855 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
| 1856 | if (!NonTypeParm->hasDefaultArgument()) |
| 1857 | return TemplateArgumentLoc(); |
| 1858 | |
| 1859 | OwningExprResult Arg = SubstDefaultTemplateArgument(*this, Template, |
| 1860 | TemplateLoc, |
| 1861 | RAngleLoc, |
| 1862 | NonTypeParm, |
| 1863 | Converted); |
| 1864 | if (Arg.isInvalid()) |
| 1865 | return TemplateArgumentLoc(); |
| 1866 | |
| 1867 | Expr *ArgE = Arg.takeAs<Expr>(); |
| 1868 | return TemplateArgumentLoc(TemplateArgument(ArgE), ArgE); |
| 1869 | } |
| 1870 | |
| 1871 | TemplateTemplateParmDecl *TempTempParm |
| 1872 | = cast<TemplateTemplateParmDecl>(Param); |
| 1873 | if (!TempTempParm->hasDefaultArgument()) |
| 1874 | return TemplateArgumentLoc(); |
| 1875 | |
| 1876 | TemplateName TName = SubstDefaultTemplateArgument(*this, Template, |
| 1877 | TemplateLoc, |
| 1878 | RAngleLoc, |
| 1879 | TempTempParm, |
| 1880 | Converted); |
| 1881 | if (TName.isNull()) |
| 1882 | return TemplateArgumentLoc(); |
| 1883 | |
| 1884 | return TemplateArgumentLoc(TemplateArgument(TName), |
| 1885 | TempTempParm->getDefaultArgument().getTemplateQualifierRange(), |
| 1886 | TempTempParm->getDefaultArgument().getTemplateNameLoc()); |
| 1887 | } |
| 1888 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 1889 | /// \brief Check that the given template argument corresponds to the given |
| 1890 | /// template parameter. |
| 1891 | bool Sema::CheckTemplateArgument(NamedDecl *Param, |
| 1892 | const TemplateArgumentLoc &Arg, |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 1893 | TemplateDecl *Template, |
| 1894 | SourceLocation TemplateLoc, |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 1895 | SourceLocation RAngleLoc, |
| 1896 | TemplateArgumentListBuilder &Converted) { |
Douglas Gregor | d9e1530 | 2009-11-11 19:41:09 +0000 | [diff] [blame] | 1897 | // Check template type parameters. |
| 1898 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 1899 | return CheckTemplateTypeArgument(TTP, Arg, Converted); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 1900 | |
Douglas Gregor | d9e1530 | 2009-11-11 19:41:09 +0000 | [diff] [blame] | 1901 | // Check non-type template parameters. |
| 1902 | if (NonTypeTemplateParmDecl *NTTP =dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 1903 | // Do substitution on the type of the non-type template parameter |
| 1904 | // with the template arguments we've seen thus far. |
| 1905 | QualType NTTPType = NTTP->getType(); |
| 1906 | if (NTTPType->isDependentType()) { |
| 1907 | // Do substitution on the type of the non-type template parameter. |
| 1908 | InstantiatingTemplate Inst(*this, TemplateLoc, Template, |
| 1909 | NTTP, Converted.getFlatArguments(), |
| 1910 | Converted.flatSize(), |
| 1911 | SourceRange(TemplateLoc, RAngleLoc)); |
| 1912 | |
| 1913 | TemplateArgumentList TemplateArgs(Context, Converted, |
| 1914 | /*TakeArgs=*/false); |
| 1915 | NTTPType = SubstType(NTTPType, |
| 1916 | MultiLevelTemplateArgumentList(TemplateArgs), |
| 1917 | NTTP->getLocation(), |
| 1918 | NTTP->getDeclName()); |
| 1919 | // If that worked, check the non-type template parameter type |
| 1920 | // for validity. |
| 1921 | if (!NTTPType.isNull()) |
| 1922 | NTTPType = CheckNonTypeTemplateParameterType(NTTPType, |
| 1923 | NTTP->getLocation()); |
| 1924 | if (NTTPType.isNull()) |
| 1925 | return true; |
| 1926 | } |
| 1927 | |
| 1928 | switch (Arg.getArgument().getKind()) { |
| 1929 | case TemplateArgument::Null: |
| 1930 | assert(false && "Should never see a NULL template argument here"); |
| 1931 | return true; |
| 1932 | |
| 1933 | case TemplateArgument::Expression: { |
| 1934 | Expr *E = Arg.getArgument().getAsExpr(); |
| 1935 | TemplateArgument Result; |
| 1936 | if (CheckTemplateArgument(NTTP, NTTPType, E, Result)) |
| 1937 | return true; |
| 1938 | |
| 1939 | Converted.Append(Result); |
| 1940 | break; |
| 1941 | } |
| 1942 | |
| 1943 | case TemplateArgument::Declaration: |
| 1944 | case TemplateArgument::Integral: |
| 1945 | // We've already checked this template argument, so just copy |
| 1946 | // it to the list of converted arguments. |
| 1947 | Converted.Append(Arg.getArgument()); |
| 1948 | break; |
| 1949 | |
| 1950 | case TemplateArgument::Template: |
| 1951 | // We were given a template template argument. It may not be ill-formed; |
| 1952 | // see below. |
| 1953 | if (DependentTemplateName *DTN |
| 1954 | = Arg.getArgument().getAsTemplate().getAsDependentTemplateName()) { |
| 1955 | // We have a template argument such as \c T::template X, which we |
| 1956 | // parsed as a template template argument. However, since we now |
| 1957 | // know that we need a non-type template argument, convert this |
| 1958 | // template name into an expression. |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1959 | Expr *E = DependentScopeDeclRefExpr::Create(Context, |
| 1960 | DTN->getQualifier(), |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 1961 | Arg.getTemplateQualifierRange(), |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1962 | DTN->getIdentifier(), |
| 1963 | Arg.getTemplateNameLoc()); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 1964 | |
| 1965 | TemplateArgument Result; |
| 1966 | if (CheckTemplateArgument(NTTP, NTTPType, E, Result)) |
| 1967 | return true; |
| 1968 | |
| 1969 | Converted.Append(Result); |
| 1970 | break; |
| 1971 | } |
| 1972 | |
| 1973 | // We have a template argument that actually does refer to a class |
| 1974 | // template, template alias, or template template parameter, and |
| 1975 | // therefore cannot be a non-type template argument. |
| 1976 | Diag(Arg.getLocation(), diag::err_template_arg_must_be_expr) |
| 1977 | << Arg.getSourceRange(); |
| 1978 | |
| 1979 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 1980 | return true; |
| 1981 | |
| 1982 | case TemplateArgument::Type: { |
| 1983 | // We have a non-type template parameter but the template |
| 1984 | // argument is a type. |
| 1985 | |
| 1986 | // C++ [temp.arg]p2: |
| 1987 | // In a template-argument, an ambiguity between a type-id and |
| 1988 | // an expression is resolved to a type-id, regardless of the |
| 1989 | // form of the corresponding template-parameter. |
| 1990 | // |
| 1991 | // We warn specifically about this case, since it can be rather |
| 1992 | // confusing for users. |
| 1993 | QualType T = Arg.getArgument().getAsType(); |
| 1994 | SourceRange SR = Arg.getSourceRange(); |
| 1995 | if (T->isFunctionType()) |
| 1996 | Diag(SR.getBegin(), diag::err_template_arg_nontype_ambig) << SR << T; |
| 1997 | else |
| 1998 | Diag(SR.getBegin(), diag::err_template_arg_must_be_expr) << SR; |
| 1999 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 2000 | return true; |
| 2001 | } |
| 2002 | |
| 2003 | case TemplateArgument::Pack: |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 2004 | llvm_unreachable("Caller must expand template argument packs"); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2005 | break; |
| 2006 | } |
| 2007 | |
| 2008 | return false; |
| 2009 | } |
| 2010 | |
| 2011 | |
| 2012 | // Check template template parameters. |
| 2013 | TemplateTemplateParmDecl *TempParm = cast<TemplateTemplateParmDecl>(Param); |
| 2014 | |
| 2015 | // Substitute into the template parameter list of the template |
| 2016 | // template parameter, since previously-supplied template arguments |
| 2017 | // may appear within the template template parameter. |
| 2018 | { |
| 2019 | // Set up a template instantiation context. |
| 2020 | LocalInstantiationScope Scope(*this); |
| 2021 | InstantiatingTemplate Inst(*this, TemplateLoc, Template, |
| 2022 | TempParm, Converted.getFlatArguments(), |
| 2023 | Converted.flatSize(), |
| 2024 | SourceRange(TemplateLoc, RAngleLoc)); |
| 2025 | |
| 2026 | TemplateArgumentList TemplateArgs(Context, Converted, |
| 2027 | /*TakeArgs=*/false); |
| 2028 | TempParm = cast_or_null<TemplateTemplateParmDecl>( |
| 2029 | SubstDecl(TempParm, CurContext, |
| 2030 | MultiLevelTemplateArgumentList(TemplateArgs))); |
| 2031 | if (!TempParm) |
| 2032 | return true; |
| 2033 | |
| 2034 | // FIXME: TempParam is leaked. |
| 2035 | } |
| 2036 | |
| 2037 | switch (Arg.getArgument().getKind()) { |
| 2038 | case TemplateArgument::Null: |
| 2039 | assert(false && "Should never see a NULL template argument here"); |
| 2040 | return true; |
| 2041 | |
| 2042 | case TemplateArgument::Template: |
| 2043 | if (CheckTemplateArgument(TempParm, Arg)) |
| 2044 | return true; |
| 2045 | |
| 2046 | Converted.Append(Arg.getArgument()); |
| 2047 | break; |
| 2048 | |
| 2049 | case TemplateArgument::Expression: |
| 2050 | case TemplateArgument::Type: |
| 2051 | // We have a template template parameter but the template |
| 2052 | // argument does not refer to a template. |
| 2053 | Diag(Arg.getLocation(), diag::err_template_arg_must_be_template); |
| 2054 | return true; |
| 2055 | |
| 2056 | case TemplateArgument::Declaration: |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 2057 | llvm_unreachable( |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2058 | "Declaration argument with template template parameter"); |
| 2059 | break; |
| 2060 | case TemplateArgument::Integral: |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 2061 | llvm_unreachable( |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2062 | "Integral argument with template template parameter"); |
| 2063 | break; |
| 2064 | |
| 2065 | case TemplateArgument::Pack: |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 2066 | llvm_unreachable("Caller must expand template argument packs"); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2067 | break; |
| 2068 | } |
| 2069 | |
| 2070 | return false; |
| 2071 | } |
| 2072 | |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2073 | /// \brief Check that the given template argument list is well-formed |
| 2074 | /// for specializing the given template. |
| 2075 | bool Sema::CheckTemplateArgumentList(TemplateDecl *Template, |
| 2076 | SourceLocation TemplateLoc, |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2077 | const TemplateArgumentListInfo &TemplateArgs, |
Douglas Gregor | 16134c6 | 2009-07-01 00:28:38 +0000 | [diff] [blame] | 2078 | bool PartialTemplateArgs, |
Anders Carlsson | 1c5976e | 2009-06-05 03:43:12 +0000 | [diff] [blame] | 2079 | TemplateArgumentListBuilder &Converted) { |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2080 | TemplateParameterList *Params = Template->getTemplateParameters(); |
| 2081 | unsigned NumParams = Params->size(); |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2082 | unsigned NumArgs = TemplateArgs.size(); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2083 | bool Invalid = false; |
| 2084 | |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2085 | SourceLocation RAngleLoc = TemplateArgs.getRAngleLoc(); |
| 2086 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2087 | bool HasParameterPack = |
Anders Carlsson | 0ceffb5 | 2009-06-13 02:08:00 +0000 | [diff] [blame] | 2088 | NumParams > 0 && Params->getParam(NumParams - 1)->isTemplateParameterPack(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2089 | |
Anders Carlsson | 0ceffb5 | 2009-06-13 02:08:00 +0000 | [diff] [blame] | 2090 | if ((NumArgs > NumParams && !HasParameterPack) || |
Douglas Gregor | 16134c6 | 2009-07-01 00:28:38 +0000 | [diff] [blame] | 2091 | (NumArgs < Params->getMinRequiredArguments() && |
| 2092 | !PartialTemplateArgs)) { |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2093 | // FIXME: point at either the first arg beyond what we can handle, |
| 2094 | // or the '>', depending on whether we have too many or too few |
| 2095 | // arguments. |
| 2096 | SourceRange Range; |
| 2097 | if (NumArgs > NumParams) |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2098 | Range = SourceRange(TemplateArgs[NumParams].getLocation(), RAngleLoc); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2099 | Diag(TemplateLoc, diag::err_template_arg_list_different_arity) |
| 2100 | << (NumArgs > NumParams) |
| 2101 | << (isa<ClassTemplateDecl>(Template)? 0 : |
| 2102 | isa<FunctionTemplateDecl>(Template)? 1 : |
| 2103 | isa<TemplateTemplateParmDecl>(Template)? 2 : 3) |
| 2104 | << Template << Range; |
Douglas Gregor | 62cb18d | 2009-02-11 18:16:40 +0000 | [diff] [blame] | 2105 | Diag(Template->getLocation(), diag::note_template_decl_here) |
| 2106 | << Params->getSourceRange(); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2107 | Invalid = true; |
| 2108 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2109 | |
| 2110 | // C++ [temp.arg]p1: |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2111 | // [...] The type and form of each template-argument specified in |
| 2112 | // a template-id shall match the type and form specified for the |
| 2113 | // corresponding parameter declared by the template in its |
| 2114 | // template-parameter-list. |
| 2115 | unsigned ArgIdx = 0; |
| 2116 | for (TemplateParameterList::iterator Param = Params->begin(), |
| 2117 | ParamEnd = Params->end(); |
| 2118 | Param != ParamEnd; ++Param, ++ArgIdx) { |
Douglas Gregor | 16134c6 | 2009-07-01 00:28:38 +0000 | [diff] [blame] | 2119 | if (ArgIdx > NumArgs && PartialTemplateArgs) |
| 2120 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2121 | |
Douglas Gregor | d9e1530 | 2009-11-11 19:41:09 +0000 | [diff] [blame] | 2122 | // If we have a template parameter pack, check every remaining template |
| 2123 | // argument against that template parameter pack. |
| 2124 | if ((*Param)->isTemplateParameterPack()) { |
| 2125 | Converted.BeginPack(); |
| 2126 | for (; ArgIdx < NumArgs; ++ArgIdx) { |
| 2127 | if (CheckTemplateArgument(*Param, TemplateArgs[ArgIdx], Template, |
| 2128 | TemplateLoc, RAngleLoc, Converted)) { |
| 2129 | Invalid = true; |
| 2130 | break; |
| 2131 | } |
| 2132 | } |
| 2133 | Converted.EndPack(); |
| 2134 | continue; |
| 2135 | } |
| 2136 | |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2137 | if (ArgIdx < NumArgs) { |
| 2138 | // Check the template argument we were given. |
| 2139 | if (CheckTemplateArgument(*Param, TemplateArgs[ArgIdx], Template, |
| 2140 | TemplateLoc, RAngleLoc, Converted)) |
| 2141 | return true; |
| 2142 | |
| 2143 | continue; |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 2144 | } |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2145 | |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2146 | // We have a default template argument that we will use. |
| 2147 | TemplateArgumentLoc Arg; |
| 2148 | |
| 2149 | // Retrieve the default template argument from the template |
| 2150 | // parameter. For each kind of template parameter, we substitute the |
| 2151 | // template arguments provided thus far and any "outer" template arguments |
| 2152 | // (when the template parameter was part of a nested template) into |
| 2153 | // the default argument. |
| 2154 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*Param)) { |
| 2155 | if (!TTP->hasDefaultArgument()) { |
| 2156 | assert((Invalid || PartialTemplateArgs) && "Missing default argument"); |
| 2157 | break; |
| 2158 | } |
| 2159 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2160 | TypeSourceInfo *ArgType = SubstDefaultTemplateArgument(*this, |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2161 | Template, |
| 2162 | TemplateLoc, |
| 2163 | RAngleLoc, |
| 2164 | TTP, |
| 2165 | Converted); |
| 2166 | if (!ArgType) |
| 2167 | return true; |
| 2168 | |
| 2169 | Arg = TemplateArgumentLoc(TemplateArgument(ArgType->getType()), |
| 2170 | ArgType); |
| 2171 | } else if (NonTypeTemplateParmDecl *NTTP |
| 2172 | = dyn_cast<NonTypeTemplateParmDecl>(*Param)) { |
| 2173 | if (!NTTP->hasDefaultArgument()) { |
| 2174 | assert((Invalid || PartialTemplateArgs) && "Missing default argument"); |
| 2175 | break; |
| 2176 | } |
| 2177 | |
| 2178 | Sema::OwningExprResult E = SubstDefaultTemplateArgument(*this, Template, |
| 2179 | TemplateLoc, |
| 2180 | RAngleLoc, |
| 2181 | NTTP, |
| 2182 | Converted); |
| 2183 | if (E.isInvalid()) |
| 2184 | return true; |
| 2185 | |
| 2186 | Expr *Ex = E.takeAs<Expr>(); |
| 2187 | Arg = TemplateArgumentLoc(TemplateArgument(Ex), Ex); |
| 2188 | } else { |
| 2189 | TemplateTemplateParmDecl *TempParm |
| 2190 | = cast<TemplateTemplateParmDecl>(*Param); |
| 2191 | |
| 2192 | if (!TempParm->hasDefaultArgument()) { |
| 2193 | assert((Invalid || PartialTemplateArgs) && "Missing default argument"); |
| 2194 | break; |
| 2195 | } |
| 2196 | |
| 2197 | TemplateName Name = SubstDefaultTemplateArgument(*this, Template, |
| 2198 | TemplateLoc, |
| 2199 | RAngleLoc, |
| 2200 | TempParm, |
| 2201 | Converted); |
| 2202 | if (Name.isNull()) |
| 2203 | return true; |
| 2204 | |
| 2205 | Arg = TemplateArgumentLoc(TemplateArgument(Name), |
| 2206 | TempParm->getDefaultArgument().getTemplateQualifierRange(), |
| 2207 | TempParm->getDefaultArgument().getTemplateNameLoc()); |
| 2208 | } |
| 2209 | |
| 2210 | // Introduce an instantiation record that describes where we are using |
| 2211 | // the default template argument. |
| 2212 | InstantiatingTemplate Instantiating(*this, RAngleLoc, Template, *Param, |
| 2213 | Converted.getFlatArguments(), |
| 2214 | Converted.flatSize(), |
| 2215 | SourceRange(TemplateLoc, RAngleLoc)); |
| 2216 | |
| 2217 | // Check the default template argument. |
Douglas Gregor | d9e1530 | 2009-11-11 19:41:09 +0000 | [diff] [blame] | 2218 | if (CheckTemplateArgument(*Param, Arg, Template, TemplateLoc, |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2219 | RAngleLoc, Converted)) |
| 2220 | return true; |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2221 | } |
| 2222 | |
| 2223 | return Invalid; |
| 2224 | } |
| 2225 | |
| 2226 | /// \brief Check a template argument against its corresponding |
| 2227 | /// template type parameter. |
| 2228 | /// |
| 2229 | /// This routine implements the semantics of C++ [temp.arg.type]. It |
| 2230 | /// returns true if an error occurred, and false otherwise. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2231 | bool Sema::CheckTemplateArgument(TemplateTypeParmDecl *Param, |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2232 | TypeSourceInfo *ArgInfo) { |
| 2233 | assert(ArgInfo && "invalid TypeSourceInfo"); |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2234 | QualType Arg = ArgInfo->getType(); |
| 2235 | |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2236 | // C++ [temp.arg.type]p2: |
| 2237 | // A local type, a type with no linkage, an unnamed type or a type |
| 2238 | // compounded from any of these types shall not be used as a |
| 2239 | // template-argument for a template type-parameter. |
| 2240 | // |
| 2241 | // FIXME: Perform the recursive and no-linkage type checks. |
| 2242 | const TagType *Tag = 0; |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2243 | if (const EnumType *EnumT = Arg->getAs<EnumType>()) |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2244 | Tag = EnumT; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2245 | else if (const RecordType *RecordT = Arg->getAs<RecordType>()) |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2246 | Tag = RecordT; |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2247 | if (Tag && Tag->getDecl()->getDeclContext()->isFunctionOrMethod()) { |
| 2248 | SourceRange SR = ArgInfo->getTypeLoc().getFullSourceRange(); |
| 2249 | return Diag(SR.getBegin(), diag::err_template_arg_local_type) |
| 2250 | << QualType(Tag, 0) << SR; |
| 2251 | } else if (Tag && !Tag->getDecl()->getDeclName() && |
Douglas Gregor | 9813753 | 2009-03-10 18:33:27 +0000 | [diff] [blame] | 2252 | !Tag->getDecl()->getTypedefForAnonDecl()) { |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2253 | SourceRange SR = ArgInfo->getTypeLoc().getFullSourceRange(); |
| 2254 | Diag(SR.getBegin(), diag::err_template_arg_unnamed_type) << SR; |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2255 | Diag(Tag->getDecl()->getLocation(), diag::note_template_unnamed_type_here); |
| 2256 | return true; |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 2257 | } else if (Context.hasSameUnqualifiedType(Arg, Context.OverloadTy)) { |
| 2258 | SourceRange SR = ArgInfo->getTypeLoc().getFullSourceRange(); |
| 2259 | return Diag(SR.getBegin(), diag::err_template_arg_overload_type) << SR; |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2260 | } |
| 2261 | |
| 2262 | return false; |
| 2263 | } |
| 2264 | |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2265 | /// \brief Checks whether the given template argument is the address |
| 2266 | /// of an object or function according to C++ [temp.arg.nontype]p1. |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 2267 | bool Sema::CheckTemplateArgumentAddressOfObjectOrFunction(Expr *Arg, |
| 2268 | NamedDecl *&Entity) { |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2269 | bool Invalid = false; |
| 2270 | |
| 2271 | // See through any implicit casts we added to fix the type. |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 2272 | while (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(Arg)) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2273 | Arg = Cast->getSubExpr(); |
| 2274 | |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 2275 | // C++0x allows nullptr, and there's no further checking to be done for that. |
| 2276 | if (Arg->getType()->isNullPtrType()) |
| 2277 | return false; |
| 2278 | |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2279 | // C++ [temp.arg.nontype]p1: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2280 | // |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2281 | // A template-argument for a non-type, non-template |
| 2282 | // template-parameter shall be one of: [...] |
| 2283 | // |
| 2284 | // -- the address of an object or function with external |
| 2285 | // linkage, including function templates and function |
| 2286 | // template-ids but excluding non-static class members, |
| 2287 | // expressed as & id-expression where the & is optional if |
| 2288 | // the name refers to a function or array, or if the |
| 2289 | // corresponding template-parameter is a reference; or |
| 2290 | DeclRefExpr *DRE = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2291 | |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2292 | // Ignore (and complain about) any excess parentheses. |
| 2293 | while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) { |
| 2294 | if (!Invalid) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2295 | Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2296 | diag::err_template_arg_extra_parens) |
| 2297 | << Arg->getSourceRange(); |
| 2298 | Invalid = true; |
| 2299 | } |
| 2300 | |
| 2301 | Arg = Parens->getSubExpr(); |
| 2302 | } |
| 2303 | |
| 2304 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) { |
| 2305 | if (UnOp->getOpcode() == UnaryOperator::AddrOf) |
| 2306 | DRE = dyn_cast<DeclRefExpr>(UnOp->getSubExpr()); |
| 2307 | } else |
| 2308 | DRE = dyn_cast<DeclRefExpr>(Arg); |
| 2309 | |
Chandler Carruth | 038cc39 | 2010-01-31 10:01:20 +0000 | [diff] [blame] | 2310 | if (!DRE) |
| 2311 | return Diag(Arg->getSourceRange().getBegin(), |
| 2312 | diag::err_template_arg_not_decl_ref) |
| 2313 | << Arg->getSourceRange(); |
| 2314 | |
| 2315 | // Stop checking the precise nature of the argument if it is value dependent, |
| 2316 | // it should be checked when instantiated. |
| 2317 | if (Arg->isValueDependent()) |
| 2318 | return false; |
| 2319 | |
| 2320 | if (!isa<ValueDecl>(DRE->getDecl())) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2321 | return Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2322 | diag::err_template_arg_not_object_or_func_form) |
| 2323 | << Arg->getSourceRange(); |
| 2324 | |
| 2325 | // Cannot refer to non-static data members |
| 2326 | if (FieldDecl *Field = dyn_cast<FieldDecl>(DRE->getDecl())) |
| 2327 | return Diag(Arg->getSourceRange().getBegin(), diag::err_template_arg_field) |
| 2328 | << Field << Arg->getSourceRange(); |
| 2329 | |
| 2330 | // Cannot refer to non-static member functions |
| 2331 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(DRE->getDecl())) |
| 2332 | if (!Method->isStatic()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2333 | return Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2334 | diag::err_template_arg_method) |
| 2335 | << Method << Arg->getSourceRange(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2336 | |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2337 | // Functions must have external linkage. |
| 2338 | if (FunctionDecl *Func = dyn_cast<FunctionDecl>(DRE->getDecl())) { |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 2339 | if (!isExternalLinkage(Func->getLinkage())) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2340 | Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2341 | diag::err_template_arg_function_not_extern) |
| 2342 | << Func << Arg->getSourceRange(); |
| 2343 | Diag(Func->getLocation(), diag::note_template_arg_internal_object) |
| 2344 | << true; |
| 2345 | return true; |
| 2346 | } |
| 2347 | |
| 2348 | // Okay: we've named a function with external linkage. |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 2349 | Entity = Func; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2350 | return Invalid; |
| 2351 | } |
| 2352 | |
| 2353 | if (VarDecl *Var = dyn_cast<VarDecl>(DRE->getDecl())) { |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 2354 | if (!isExternalLinkage(Var->getLinkage())) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2355 | Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2356 | diag::err_template_arg_object_not_extern) |
| 2357 | << Var << Arg->getSourceRange(); |
| 2358 | Diag(Var->getLocation(), diag::note_template_arg_internal_object) |
| 2359 | << true; |
| 2360 | return true; |
| 2361 | } |
| 2362 | |
| 2363 | // Okay: we've named an object with external linkage |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 2364 | Entity = Var; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2365 | return Invalid; |
| 2366 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2367 | |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2368 | // 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] | 2369 | Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2370 | diag::err_template_arg_not_object_or_func) |
| 2371 | << Arg->getSourceRange(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2372 | Diag(DRE->getDecl()->getLocation(), |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2373 | diag::note_template_arg_refers_here); |
| 2374 | return true; |
| 2375 | } |
| 2376 | |
| 2377 | /// \brief Checks whether the given template argument is a pointer to |
| 2378 | /// member constant according to C++ [temp.arg.nontype]p1. |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 2379 | bool Sema::CheckTemplateArgumentPointerToMember(Expr *Arg, |
| 2380 | TemplateArgument &Converted) { |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2381 | bool Invalid = false; |
| 2382 | |
| 2383 | // See through any implicit casts we added to fix the type. |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 2384 | while (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(Arg)) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2385 | Arg = Cast->getSubExpr(); |
| 2386 | |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 2387 | // C++0x allows nullptr, and there's no further checking to be done for that. |
| 2388 | if (Arg->getType()->isNullPtrType()) |
| 2389 | return false; |
| 2390 | |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2391 | // C++ [temp.arg.nontype]p1: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2392 | // |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2393 | // A template-argument for a non-type, non-template |
| 2394 | // template-parameter shall be one of: [...] |
| 2395 | // |
| 2396 | // -- a pointer to member expressed as described in 5.3.1. |
Douglas Gregor | a2813ce | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 2397 | DeclRefExpr *DRE = 0; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2398 | |
| 2399 | // Ignore (and complain about) any excess parentheses. |
| 2400 | while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) { |
| 2401 | if (!Invalid) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2402 | Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2403 | diag::err_template_arg_extra_parens) |
| 2404 | << Arg->getSourceRange(); |
| 2405 | Invalid = true; |
| 2406 | } |
| 2407 | |
| 2408 | Arg = Parens->getSubExpr(); |
| 2409 | } |
| 2410 | |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 2411 | // A pointer-to-member constant written &Class::member. |
| 2412 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) { |
Douglas Gregor | a2813ce | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 2413 | if (UnOp->getOpcode() == UnaryOperator::AddrOf) { |
| 2414 | DRE = dyn_cast<DeclRefExpr>(UnOp->getSubExpr()); |
| 2415 | if (DRE && !DRE->getQualifier()) |
| 2416 | DRE = 0; |
| 2417 | } |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 2418 | } |
| 2419 | // A constant of pointer-to-member type. |
| 2420 | else if ((DRE = dyn_cast<DeclRefExpr>(Arg))) { |
| 2421 | if (ValueDecl *VD = dyn_cast<ValueDecl>(DRE->getDecl())) { |
| 2422 | if (VD->getType()->isMemberPointerType()) { |
| 2423 | if (isa<NonTypeTemplateParmDecl>(VD) || |
| 2424 | (isa<VarDecl>(VD) && |
| 2425 | Context.getCanonicalType(VD->getType()).isConstQualified())) { |
| 2426 | if (Arg->isTypeDependent() || Arg->isValueDependent()) |
| 2427 | Converted = TemplateArgument(Arg->Retain()); |
| 2428 | else |
| 2429 | Converted = TemplateArgument(VD->getCanonicalDecl()); |
| 2430 | return Invalid; |
| 2431 | } |
| 2432 | } |
| 2433 | } |
| 2434 | |
| 2435 | DRE = 0; |
| 2436 | } |
| 2437 | |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2438 | if (!DRE) |
| 2439 | return Diag(Arg->getSourceRange().getBegin(), |
| 2440 | diag::err_template_arg_not_pointer_to_member_form) |
| 2441 | << Arg->getSourceRange(); |
| 2442 | |
| 2443 | if (isa<FieldDecl>(DRE->getDecl()) || isa<CXXMethodDecl>(DRE->getDecl())) { |
| 2444 | assert((isa<FieldDecl>(DRE->getDecl()) || |
| 2445 | !cast<CXXMethodDecl>(DRE->getDecl())->isStatic()) && |
| 2446 | "Only non-static member pointers can make it here"); |
| 2447 | |
| 2448 | // Okay: this is the address of a non-static member, and therefore |
| 2449 | // a member pointer constant. |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 2450 | if (Arg->isTypeDependent() || Arg->isValueDependent()) |
| 2451 | Converted = TemplateArgument(Arg->Retain()); |
| 2452 | else |
| 2453 | Converted = TemplateArgument(DRE->getDecl()->getCanonicalDecl()); |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2454 | return Invalid; |
| 2455 | } |
| 2456 | |
| 2457 | // 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] | 2458 | Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2459 | diag::err_template_arg_not_pointer_to_member_form) |
| 2460 | << Arg->getSourceRange(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2461 | Diag(DRE->getDecl()->getLocation(), |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2462 | diag::note_template_arg_refers_here); |
| 2463 | return true; |
| 2464 | } |
| 2465 | |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2466 | /// \brief Check a template argument against its corresponding |
| 2467 | /// non-type template parameter. |
| 2468 | /// |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 2469 | /// This routine implements the semantics of C++ [temp.arg.nontype]. |
| 2470 | /// It returns true if an error occurred, and false otherwise. \p |
| 2471 | /// InstantiatedParamType is the type of the non-type template |
| 2472 | /// parameter after it has been instantiated. |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 2473 | /// |
Douglas Gregor | 02cbbd2 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 2474 | /// If no error was detected, Converted receives the converted template argument. |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2475 | bool Sema::CheckTemplateArgument(NonTypeTemplateParmDecl *Param, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2476 | QualType InstantiatedParamType, Expr *&Arg, |
Douglas Gregor | 02cbbd2 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 2477 | TemplateArgument &Converted) { |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2478 | SourceLocation StartLoc = Arg->getSourceRange().getBegin(); |
| 2479 | |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 2480 | // If either the parameter has a dependent type or the argument is |
| 2481 | // type-dependent, there's nothing we can check now. |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 2482 | // FIXME: Add template argument to Converted! |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2483 | if (InstantiatedParamType->isDependentType() || Arg->isTypeDependent()) { |
| 2484 | // FIXME: Produce a cloned, canonical expression? |
Douglas Gregor | 02cbbd2 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 2485 | Converted = TemplateArgument(Arg); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 2486 | return false; |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2487 | } |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 2488 | |
| 2489 | // C++ [temp.arg.nontype]p5: |
| 2490 | // The following conversions are performed on each expression used |
| 2491 | // as a non-type template-argument. If a non-type |
| 2492 | // template-argument cannot be converted to the type of the |
| 2493 | // corresponding template-parameter then the program is |
| 2494 | // ill-formed. |
| 2495 | // |
| 2496 | // -- for a non-type template-parameter of integral or |
| 2497 | // enumeration type, integral promotions (4.5) and integral |
| 2498 | // conversions (4.7) are applied. |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 2499 | QualType ParamType = InstantiatedParamType; |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 2500 | QualType ArgType = Arg->getType(); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 2501 | if (ParamType->isIntegralType() || ParamType->isEnumeralType()) { |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 2502 | // C++ [temp.arg.nontype]p1: |
| 2503 | // A template-argument for a non-type, non-template |
| 2504 | // template-parameter shall be one of: |
| 2505 | // |
| 2506 | // -- an integral constant-expression of integral or enumeration |
| 2507 | // type; or |
| 2508 | // -- the name of a non-type template-parameter; or |
| 2509 | SourceLocation NonConstantLoc; |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 2510 | llvm::APSInt Value; |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 2511 | if (!ArgType->isIntegralType() && !ArgType->isEnumeralType()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2512 | Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 2513 | diag::err_template_arg_not_integral_or_enumeral) |
| 2514 | << ArgType << Arg->getSourceRange(); |
| 2515 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 2516 | return true; |
| 2517 | } else if (!Arg->isValueDependent() && |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 2518 | !Arg->isIntegerConstantExpr(Value, Context, &NonConstantLoc)) { |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 2519 | Diag(NonConstantLoc, diag::err_template_arg_not_ice) |
| 2520 | << ArgType << Arg->getSourceRange(); |
| 2521 | return true; |
| 2522 | } |
| 2523 | |
| 2524 | // FIXME: We need some way to more easily get the unqualified form |
| 2525 | // of the types without going all the way to the |
| 2526 | // canonical type. |
| 2527 | if (Context.getCanonicalType(ParamType).getCVRQualifiers()) |
| 2528 | ParamType = Context.getCanonicalType(ParamType).getUnqualifiedType(); |
| 2529 | if (Context.getCanonicalType(ArgType).getCVRQualifiers()) |
| 2530 | ArgType = Context.getCanonicalType(ArgType).getUnqualifiedType(); |
| 2531 | |
| 2532 | // Try to convert the argument to the parameter's type. |
Douglas Gregor | ff52439 | 2009-11-04 21:50:46 +0000 | [diff] [blame] | 2533 | if (Context.hasSameType(ParamType, ArgType)) { |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 2534 | // Okay: no conversion necessary |
| 2535 | } else if (IsIntegralPromotion(Arg, ArgType, ParamType) || |
| 2536 | !ParamType->isEnumeralType()) { |
| 2537 | // This is an integral promotion or conversion. |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 2538 | ImpCastExprToType(Arg, ParamType, CastExpr::CK_IntegralCast); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 2539 | } else { |
| 2540 | // We can't perform this conversion. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2541 | Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 2542 | diag::err_template_arg_not_convertible) |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 2543 | << Arg->getType() << InstantiatedParamType << Arg->getSourceRange(); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 2544 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 2545 | return true; |
| 2546 | } |
| 2547 | |
Douglas Gregor | f80a9d5 | 2009-03-14 00:20:21 +0000 | [diff] [blame] | 2548 | QualType IntegerType = Context.getCanonicalType(ParamType); |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2549 | if (const EnumType *Enum = IntegerType->getAs<EnumType>()) |
Douglas Gregor | 02cbbd2 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 2550 | IntegerType = Context.getCanonicalType(Enum->getDecl()->getIntegerType()); |
Douglas Gregor | f80a9d5 | 2009-03-14 00:20:21 +0000 | [diff] [blame] | 2551 | |
| 2552 | if (!Arg->isValueDependent()) { |
| 2553 | // Check that an unsigned parameter does not receive a negative |
| 2554 | // value. |
| 2555 | if (IntegerType->isUnsignedIntegerType() |
| 2556 | && (Value.isSigned() && Value.isNegative())) { |
| 2557 | Diag(Arg->getSourceRange().getBegin(), diag::err_template_arg_negative) |
| 2558 | << Value.toString(10) << Param->getType() |
| 2559 | << Arg->getSourceRange(); |
| 2560 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 2561 | return true; |
| 2562 | } |
| 2563 | |
| 2564 | // Check that we don't overflow the template parameter type. |
| 2565 | unsigned AllowedBits = Context.getTypeSize(IntegerType); |
Eli Friedman | 29f89f6 | 2009-12-23 18:44:58 +0000 | [diff] [blame] | 2566 | unsigned RequiredBits; |
| 2567 | if (IntegerType->isUnsignedIntegerType()) |
| 2568 | RequiredBits = Value.getActiveBits(); |
| 2569 | else if (Value.isUnsigned()) |
| 2570 | RequiredBits = Value.getActiveBits() + 1; |
| 2571 | else |
| 2572 | RequiredBits = Value.getMinSignedBits(); |
| 2573 | if (RequiredBits > AllowedBits) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2574 | Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | f80a9d5 | 2009-03-14 00:20:21 +0000 | [diff] [blame] | 2575 | diag::err_template_arg_too_large) |
| 2576 | << Value.toString(10) << Param->getType() |
| 2577 | << Arg->getSourceRange(); |
| 2578 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 2579 | return true; |
| 2580 | } |
| 2581 | |
| 2582 | if (Value.getBitWidth() != AllowedBits) |
| 2583 | Value.extOrTrunc(AllowedBits); |
| 2584 | Value.setIsSigned(IntegerType->isSignedIntegerType()); |
| 2585 | } |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 2586 | |
Douglas Gregor | 02cbbd2 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 2587 | // Add the value of this argument to the list of converted |
| 2588 | // arguments. We use the bitwidth and signedness of the template |
| 2589 | // parameter. |
| 2590 | if (Arg->isValueDependent()) { |
| 2591 | // The argument is value-dependent. Create a new |
| 2592 | // TemplateArgument with the converted expression. |
| 2593 | Converted = TemplateArgument(Arg); |
| 2594 | return false; |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 2595 | } |
| 2596 | |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2597 | Converted = TemplateArgument(Value, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2598 | ParamType->isEnumeralType() ? ParamType |
Douglas Gregor | 02cbbd2 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 2599 | : IntegerType); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 2600 | return false; |
| 2601 | } |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 2602 | |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 2603 | // Handle pointer-to-function, reference-to-function, and |
| 2604 | // pointer-to-member-function all in (roughly) the same way. |
| 2605 | if (// -- For a non-type template-parameter of type pointer to |
| 2606 | // function, only the function-to-pointer conversion (4.3) is |
| 2607 | // applied. If the template-argument represents a set of |
| 2608 | // overloaded functions (or a pointer to such), the matching |
| 2609 | // function is selected from the set (13.4). |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 2610 | // In C++0x, any std::nullptr_t value can be converted. |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 2611 | (ParamType->isPointerType() && |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2612 | ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType()) || |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 2613 | // -- For a non-type template-parameter of type reference to |
| 2614 | // function, no conversions apply. If the template-argument |
| 2615 | // represents a set of overloaded functions, the matching |
| 2616 | // function is selected from the set (13.4). |
| 2617 | (ParamType->isReferenceType() && |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2618 | ParamType->getAs<ReferenceType>()->getPointeeType()->isFunctionType()) || |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 2619 | // -- For a non-type template-parameter of type pointer to |
| 2620 | // member function, no conversions apply. If the |
| 2621 | // template-argument represents a set of overloaded member |
| 2622 | // functions, the matching member function is selected from |
| 2623 | // the set (13.4). |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 2624 | // Again, C++0x allows a std::nullptr_t value. |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 2625 | (ParamType->isMemberPointerType() && |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2626 | ParamType->getAs<MemberPointerType>()->getPointeeType() |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 2627 | ->isFunctionType())) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2628 | if (Context.hasSameUnqualifiedType(ArgType, |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2629 | ParamType.getNonReferenceType())) { |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 2630 | // We don't have to do anything: the types already match. |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 2631 | } else if (ArgType->isNullPtrType() && (ParamType->isPointerType() || |
| 2632 | ParamType->isMemberPointerType())) { |
| 2633 | ArgType = ParamType; |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 2634 | if (ParamType->isMemberPointerType()) |
| 2635 | ImpCastExprToType(Arg, ParamType, CastExpr::CK_NullToMemberPointer); |
| 2636 | else |
| 2637 | ImpCastExprToType(Arg, ParamType, CastExpr::CK_BitCast); |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 2638 | } else if (ArgType->isFunctionType() && ParamType->isPointerType()) { |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 2639 | ArgType = Context.getPointerType(ArgType); |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 2640 | ImpCastExprToType(Arg, ArgType, CastExpr::CK_FunctionToPointerDecay); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2641 | } else if (FunctionDecl *Fn |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 2642 | = ResolveAddressOfOverloadedFunction(Arg, ParamType, true)) { |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 2643 | if (DiagnoseUseOfDecl(Fn, Arg->getSourceRange().getBegin())) |
| 2644 | return true; |
| 2645 | |
Anders Carlsson | 96ad533 | 2009-10-21 17:16:23 +0000 | [diff] [blame] | 2646 | Arg = FixOverloadedFunctionReference(Arg, Fn); |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 2647 | ArgType = Arg->getType(); |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 2648 | if (ArgType->isFunctionType() && ParamType->isPointerType()) { |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 2649 | ArgType = Context.getPointerType(Arg->getType()); |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 2650 | ImpCastExprToType(Arg, ArgType, CastExpr::CK_FunctionToPointerDecay); |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 2651 | } |
| 2652 | } |
| 2653 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2654 | if (!Context.hasSameUnqualifiedType(ArgType, |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2655 | ParamType.getNonReferenceType())) { |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 2656 | // We can't perform this conversion. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2657 | Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 2658 | diag::err_template_arg_not_convertible) |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 2659 | << Arg->getType() << InstantiatedParamType << Arg->getSourceRange(); |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 2660 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 2661 | return true; |
| 2662 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2663 | |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 2664 | if (ParamType->isMemberPointerType()) |
| 2665 | return CheckTemplateArgumentPointerToMember(Arg, Converted); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2666 | |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 2667 | NamedDecl *Entity = 0; |
| 2668 | if (CheckTemplateArgumentAddressOfObjectOrFunction(Arg, Entity)) |
| 2669 | return true; |
| 2670 | |
Chandler Carruth | 038cc39 | 2010-01-31 10:01:20 +0000 | [diff] [blame] | 2671 | if (Arg->isValueDependent()) { |
| 2672 | Converted = TemplateArgument(Arg); |
| 2673 | } else { |
| 2674 | if (Entity) |
| 2675 | Entity = cast<NamedDecl>(Entity->getCanonicalDecl()); |
| 2676 | Converted = TemplateArgument(Entity); |
| 2677 | } |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 2678 | return false; |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 2679 | } |
| 2680 | |
Chris Lattner | fe90de7 | 2009-02-20 21:37:53 +0000 | [diff] [blame] | 2681 | if (ParamType->isPointerType()) { |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 2682 | // -- for a non-type template-parameter of type pointer to |
| 2683 | // object, qualification conversions (4.4) and the |
| 2684 | // array-to-pointer conversion (4.2) are applied. |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 2685 | // C++0x also allows a value of std::nullptr_t. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2686 | assert(ParamType->getAs<PointerType>()->getPointeeType()->isObjectType() && |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 2687 | "Only object pointers allowed here"); |
Douglas Gregor | f684e6e | 2009-02-11 00:44:29 +0000 | [diff] [blame] | 2688 | |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 2689 | if (ArgType->isNullPtrType()) { |
| 2690 | ArgType = ParamType; |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 2691 | ImpCastExprToType(Arg, ParamType, CastExpr::CK_BitCast); |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 2692 | } else if (ArgType->isArrayType()) { |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 2693 | ArgType = Context.getArrayDecayedType(ArgType); |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 2694 | ImpCastExprToType(Arg, ArgType, CastExpr::CK_ArrayToPointerDecay); |
Douglas Gregor | f684e6e | 2009-02-11 00:44:29 +0000 | [diff] [blame] | 2695 | } |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 2696 | |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 2697 | if (IsQualificationConversion(ArgType, ParamType)) { |
| 2698 | ArgType = ParamType; |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 2699 | ImpCastExprToType(Arg, ParamType, CastExpr::CK_NoOp); |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 2700 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2701 | |
Douglas Gregor | 8e6563b | 2009-02-11 18:22:40 +0000 | [diff] [blame] | 2702 | if (!Context.hasSameUnqualifiedType(ArgType, ParamType)) { |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 2703 | // We can't perform this conversion. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2704 | Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 2705 | diag::err_template_arg_not_convertible) |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 2706 | << Arg->getType() << InstantiatedParamType << Arg->getSourceRange(); |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 2707 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 2708 | return true; |
| 2709 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2710 | |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 2711 | NamedDecl *Entity = 0; |
| 2712 | if (CheckTemplateArgumentAddressOfObjectOrFunction(Arg, Entity)) |
| 2713 | return true; |
| 2714 | |
Chandler Carruth | 038cc39 | 2010-01-31 10:01:20 +0000 | [diff] [blame] | 2715 | if (Arg->isValueDependent()) { |
| 2716 | Converted = TemplateArgument(Arg); |
| 2717 | } else { |
| 2718 | if (Entity) |
| 2719 | Entity = cast<NamedDecl>(Entity->getCanonicalDecl()); |
| 2720 | Converted = TemplateArgument(Entity); |
| 2721 | } |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 2722 | return false; |
Douglas Gregor | f684e6e | 2009-02-11 00:44:29 +0000 | [diff] [blame] | 2723 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2724 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2725 | if (const ReferenceType *ParamRefType = ParamType->getAs<ReferenceType>()) { |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 2726 | // -- For a non-type template-parameter of type reference to |
| 2727 | // object, no conversions apply. The type referred to by the |
| 2728 | // reference may be more cv-qualified than the (otherwise |
| 2729 | // identical) type of the template-argument. The |
| 2730 | // template-parameter is bound directly to the |
| 2731 | // template-argument, which must be an lvalue. |
Douglas Gregor | bad0e65 | 2009-03-24 20:32:41 +0000 | [diff] [blame] | 2732 | assert(ParamRefType->getPointeeType()->isObjectType() && |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 2733 | "Only object references allowed here"); |
Douglas Gregor | f684e6e | 2009-02-11 00:44:29 +0000 | [diff] [blame] | 2734 | |
Chandler Carruth | 5147fa6 | 2010-02-03 09:37:33 +0000 | [diff] [blame^] | 2735 | QualType ReferredType = ParamRefType->getPointeeType(); |
| 2736 | if (!Context.hasSameUnqualifiedType(ReferredType, ArgType)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2737 | Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 2738 | diag::err_template_arg_no_ref_bind) |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 2739 | << InstantiatedParamType << Arg->getType() |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 2740 | << Arg->getSourceRange(); |
| 2741 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 2742 | return true; |
| 2743 | } |
| 2744 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2745 | unsigned ParamQuals |
Chandler Carruth | 5147fa6 | 2010-02-03 09:37:33 +0000 | [diff] [blame^] | 2746 | = Context.getCanonicalType(ReferredType).getCVRQualifiers(); |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 2747 | unsigned ArgQuals = Context.getCanonicalType(ArgType).getCVRQualifiers(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2748 | |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 2749 | if ((ParamQuals | ArgQuals) != ParamQuals) { |
| 2750 | Diag(Arg->getSourceRange().getBegin(), |
| 2751 | diag::err_template_arg_ref_bind_ignores_quals) |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 2752 | << InstantiatedParamType << Arg->getType() |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 2753 | << Arg->getSourceRange(); |
| 2754 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 2755 | return true; |
| 2756 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2757 | |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 2758 | NamedDecl *Entity = 0; |
| 2759 | if (CheckTemplateArgumentAddressOfObjectOrFunction(Arg, Entity)) |
| 2760 | return true; |
| 2761 | |
Chandler Carruth | 038cc39 | 2010-01-31 10:01:20 +0000 | [diff] [blame] | 2762 | if (Arg->isValueDependent()) { |
| 2763 | Converted = TemplateArgument(Arg); |
| 2764 | } else { |
| 2765 | Entity = cast<NamedDecl>(Entity->getCanonicalDecl()); |
| 2766 | Converted = TemplateArgument(Entity); |
| 2767 | } |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 2768 | return false; |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 2769 | } |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 2770 | |
| 2771 | // -- For a non-type template-parameter of type pointer to data |
| 2772 | // member, qualification conversions (4.4) are applied. |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 2773 | // C++0x allows std::nullptr_t values. |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 2774 | assert(ParamType->isMemberPointerType() && "Only pointers to members remain"); |
| 2775 | |
Douglas Gregor | 8e6563b | 2009-02-11 18:22:40 +0000 | [diff] [blame] | 2776 | if (Context.hasSameUnqualifiedType(ParamType, ArgType)) { |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 2777 | // Types match exactly: nothing more to do here. |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 2778 | } else if (ArgType->isNullPtrType()) { |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 2779 | ImpCastExprToType(Arg, ParamType, CastExpr::CK_NullToMemberPointer); |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 2780 | } else if (IsQualificationConversion(ArgType, ParamType)) { |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 2781 | ImpCastExprToType(Arg, ParamType, CastExpr::CK_NoOp); |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 2782 | } else { |
| 2783 | // We can't perform this conversion. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2784 | Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 2785 | diag::err_template_arg_not_convertible) |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 2786 | << Arg->getType() << InstantiatedParamType << Arg->getSourceRange(); |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 2787 | Diag(Param->getLocation(), diag::note_template_param_here); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2788 | return true; |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 2789 | } |
| 2790 | |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 2791 | return CheckTemplateArgumentPointerToMember(Arg, Converted); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2792 | } |
| 2793 | |
| 2794 | /// \brief Check a template argument against its corresponding |
| 2795 | /// template template parameter. |
| 2796 | /// |
| 2797 | /// This routine implements the semantics of C++ [temp.arg.template]. |
| 2798 | /// It returns true if an error occurred, and false otherwise. |
| 2799 | bool Sema::CheckTemplateArgument(TemplateTemplateParmDecl *Param, |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2800 | const TemplateArgumentLoc &Arg) { |
| 2801 | TemplateName Name = Arg.getArgument().getAsTemplate(); |
| 2802 | TemplateDecl *Template = Name.getAsTemplateDecl(); |
| 2803 | if (!Template) { |
| 2804 | // Any dependent template name is fine. |
| 2805 | assert(Name.isDependent() && "Non-dependent template isn't a declaration?"); |
| 2806 | return false; |
| 2807 | } |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 2808 | |
| 2809 | // C++ [temp.arg.template]p1: |
| 2810 | // A template-argument for a template template-parameter shall be |
| 2811 | // the name of a class template, expressed as id-expression. Only |
| 2812 | // primary class templates are considered when matching the |
| 2813 | // template template argument with the corresponding parameter; |
| 2814 | // partial specializations are not considered even if their |
| 2815 | // parameter lists match that of the template template parameter. |
Douglas Gregor | ba1ecb5 | 2009-06-12 19:43:02 +0000 | [diff] [blame] | 2816 | // |
| 2817 | // Note that we also allow template template parameters here, which |
| 2818 | // will happen when we are dealing with, e.g., class template |
| 2819 | // partial specializations. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2820 | if (!isa<ClassTemplateDecl>(Template) && |
Douglas Gregor | ba1ecb5 | 2009-06-12 19:43:02 +0000 | [diff] [blame] | 2821 | !isa<TemplateTemplateParmDecl>(Template)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2822 | assert(isa<FunctionTemplateDecl>(Template) && |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 2823 | "Only function templates are possible here"); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2824 | Diag(Arg.getLocation(), diag::err_template_arg_not_class_template); |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 2825 | Diag(Template->getLocation(), diag::note_template_arg_refers_here_func) |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 2826 | << Template; |
| 2827 | } |
| 2828 | |
| 2829 | return !TemplateParameterListsAreEqual(Template->getTemplateParameters(), |
| 2830 | Param->getTemplateParameters(), |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 2831 | true, |
| 2832 | TPL_TemplateTemplateArgumentMatch, |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2833 | Arg.getLocation()); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2834 | } |
| 2835 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 2836 | /// \brief Determine whether the given template parameter lists are |
| 2837 | /// equivalent. |
| 2838 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2839 | /// \param New The new template parameter list, typically written in the |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 2840 | /// source code as part of a new template declaration. |
| 2841 | /// |
| 2842 | /// \param Old The old template parameter list, typically found via |
| 2843 | /// name lookup of the template declared with this template parameter |
| 2844 | /// list. |
| 2845 | /// |
| 2846 | /// \param Complain If true, this routine will produce a diagnostic if |
| 2847 | /// the template parameter lists are not equivalent. |
| 2848 | /// |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 2849 | /// \param Kind describes how we are to match the template parameter lists. |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 2850 | /// |
| 2851 | /// \param TemplateArgLoc If this source location is valid, then we |
| 2852 | /// are actually checking the template parameter list of a template |
| 2853 | /// argument (New) against the template parameter list of its |
| 2854 | /// corresponding template template parameter (Old). We produce |
| 2855 | /// slightly different diagnostics in this scenario. |
| 2856 | /// |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 2857 | /// \returns True if the template parameter lists are equal, false |
| 2858 | /// otherwise. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2859 | bool |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 2860 | Sema::TemplateParameterListsAreEqual(TemplateParameterList *New, |
| 2861 | TemplateParameterList *Old, |
| 2862 | bool Complain, |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 2863 | TemplateParameterListEqualKind Kind, |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 2864 | SourceLocation TemplateArgLoc) { |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 2865 | if (Old->size() != New->size()) { |
| 2866 | if (Complain) { |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 2867 | unsigned NextDiag = diag::err_template_param_list_different_arity; |
| 2868 | if (TemplateArgLoc.isValid()) { |
| 2869 | Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch); |
| 2870 | NextDiag = diag::note_template_param_list_different_arity; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2871 | } |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 2872 | Diag(New->getTemplateLoc(), NextDiag) |
| 2873 | << (New->size() > Old->size()) |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 2874 | << (Kind != TPL_TemplateMatch) |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 2875 | << SourceRange(New->getTemplateLoc(), New->getRAngleLoc()); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 2876 | Diag(Old->getTemplateLoc(), diag::note_template_prev_declaration) |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 2877 | << (Kind != TPL_TemplateMatch) |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 2878 | << SourceRange(Old->getTemplateLoc(), Old->getRAngleLoc()); |
| 2879 | } |
| 2880 | |
| 2881 | return false; |
| 2882 | } |
| 2883 | |
| 2884 | for (TemplateParameterList::iterator OldParm = Old->begin(), |
| 2885 | OldParmEnd = Old->end(), NewParm = New->begin(); |
| 2886 | OldParm != OldParmEnd; ++OldParm, ++NewParm) { |
| 2887 | if ((*OldParm)->getKind() != (*NewParm)->getKind()) { |
Douglas Gregor | 34d1dc9 | 2009-06-24 16:50:40 +0000 | [diff] [blame] | 2888 | if (Complain) { |
| 2889 | unsigned NextDiag = diag::err_template_param_different_kind; |
| 2890 | if (TemplateArgLoc.isValid()) { |
| 2891 | Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch); |
| 2892 | NextDiag = diag::note_template_param_different_kind; |
| 2893 | } |
| 2894 | Diag((*NewParm)->getLocation(), NextDiag) |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 2895 | << (Kind != TPL_TemplateMatch); |
Douglas Gregor | 34d1dc9 | 2009-06-24 16:50:40 +0000 | [diff] [blame] | 2896 | Diag((*OldParm)->getLocation(), diag::note_template_prev_declaration) |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 2897 | << (Kind != TPL_TemplateMatch); |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 2898 | } |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 2899 | return false; |
| 2900 | } |
| 2901 | |
| 2902 | if (isa<TemplateTypeParmDecl>(*OldParm)) { |
| 2903 | // Okay; all template type parameters are equivalent (since we |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 2904 | // know we're at the same index). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2905 | } else if (NonTypeTemplateParmDecl *OldNTTP |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 2906 | = dyn_cast<NonTypeTemplateParmDecl>(*OldParm)) { |
| 2907 | // The types of non-type template parameters must agree. |
| 2908 | NonTypeTemplateParmDecl *NewNTTP |
| 2909 | = cast<NonTypeTemplateParmDecl>(*NewParm); |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 2910 | |
| 2911 | // If we are matching a template template argument to a template |
| 2912 | // template parameter and one of the non-type template parameter types |
| 2913 | // is dependent, then we must wait until template instantiation time |
| 2914 | // to actually compare the arguments. |
| 2915 | if (Kind == TPL_TemplateTemplateArgumentMatch && |
| 2916 | (OldNTTP->getType()->isDependentType() || |
| 2917 | NewNTTP->getType()->isDependentType())) |
| 2918 | continue; |
| 2919 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 2920 | if (Context.getCanonicalType(OldNTTP->getType()) != |
| 2921 | Context.getCanonicalType(NewNTTP->getType())) { |
| 2922 | if (Complain) { |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 2923 | unsigned NextDiag = diag::err_template_nontype_parm_different_type; |
| 2924 | if (TemplateArgLoc.isValid()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2925 | Diag(TemplateArgLoc, |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 2926 | diag::err_template_arg_template_params_mismatch); |
| 2927 | NextDiag = diag::note_template_nontype_parm_different_type; |
| 2928 | } |
| 2929 | Diag(NewNTTP->getLocation(), NextDiag) |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 2930 | << NewNTTP->getType() |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 2931 | << (Kind != TPL_TemplateMatch); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2932 | Diag(OldNTTP->getLocation(), |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 2933 | diag::note_template_nontype_parm_prev_declaration) |
| 2934 | << OldNTTP->getType(); |
| 2935 | } |
| 2936 | return false; |
| 2937 | } |
| 2938 | } else { |
| 2939 | // The template parameter lists of template template |
| 2940 | // parameters must agree. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2941 | assert(isa<TemplateTemplateParmDecl>(*OldParm) && |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 2942 | "Only template template parameters handled here"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2943 | TemplateTemplateParmDecl *OldTTP |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 2944 | = cast<TemplateTemplateParmDecl>(*OldParm); |
| 2945 | TemplateTemplateParmDecl *NewTTP |
| 2946 | = cast<TemplateTemplateParmDecl>(*NewParm); |
| 2947 | if (!TemplateParameterListsAreEqual(NewTTP->getTemplateParameters(), |
| 2948 | OldTTP->getTemplateParameters(), |
| 2949 | Complain, |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 2950 | (Kind == TPL_TemplateMatch? TPL_TemplateTemplateParmMatch : Kind), |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 2951 | TemplateArgLoc)) |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 2952 | return false; |
| 2953 | } |
| 2954 | } |
| 2955 | |
| 2956 | return true; |
| 2957 | } |
| 2958 | |
| 2959 | /// \brief Check whether a template can be declared within this scope. |
| 2960 | /// |
| 2961 | /// If the template declaration is valid in this scope, returns |
| 2962 | /// false. Otherwise, issues a diagnostic and returns true. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2963 | bool |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 2964 | Sema::CheckTemplateDeclScope(Scope *S, TemplateParameterList *TemplateParams) { |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 2965 | // Find the nearest enclosing declaration scope. |
| 2966 | while ((S->getFlags() & Scope::DeclScope) == 0 || |
| 2967 | (S->getFlags() & Scope::TemplateParamScope) != 0) |
| 2968 | S = S->getParent(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2969 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 2970 | // C++ [temp]p2: |
| 2971 | // A template-declaration can appear only as a namespace scope or |
| 2972 | // class scope declaration. |
| 2973 | DeclContext *Ctx = static_cast<DeclContext *>(S->getEntity()); |
Eli Friedman | 1503f77 | 2009-07-31 01:43:05 +0000 | [diff] [blame] | 2974 | if (Ctx && isa<LinkageSpecDecl>(Ctx) && |
| 2975 | cast<LinkageSpecDecl>(Ctx)->getLanguage() != LinkageSpecDecl::lang_cxx) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2976 | return Diag(TemplateParams->getTemplateLoc(), diag::err_template_linkage) |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 2977 | << TemplateParams->getSourceRange(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2978 | |
Eli Friedman | 1503f77 | 2009-07-31 01:43:05 +0000 | [diff] [blame] | 2979 | while (Ctx && isa<LinkageSpecDecl>(Ctx)) |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 2980 | Ctx = Ctx->getParent(); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 2981 | |
| 2982 | if (Ctx && (Ctx->isFileContext() || Ctx->isRecord())) |
| 2983 | return false; |
| 2984 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2985 | return Diag(TemplateParams->getTemplateLoc(), |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 2986 | diag::err_template_outside_namespace_or_class_scope) |
| 2987 | << TemplateParams->getSourceRange(); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 2988 | } |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 2989 | |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 2990 | /// \brief Determine what kind of template specialization the given declaration |
| 2991 | /// is. |
| 2992 | static TemplateSpecializationKind getTemplateSpecializationKind(NamedDecl *D) { |
| 2993 | if (!D) |
| 2994 | return TSK_Undeclared; |
| 2995 | |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 2996 | if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) |
| 2997 | return Record->getTemplateSpecializationKind(); |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 2998 | if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) |
| 2999 | return Function->getTemplateSpecializationKind(); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 3000 | if (VarDecl *Var = dyn_cast<VarDecl>(D)) |
| 3001 | return Var->getTemplateSpecializationKind(); |
| 3002 | |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3003 | return TSK_Undeclared; |
| 3004 | } |
| 3005 | |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 3006 | /// \brief Check whether a specialization is well-formed in the current |
| 3007 | /// context. |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 3008 | /// |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 3009 | /// This routine determines whether a template specialization can be declared |
| 3010 | /// in the current context (C++ [temp.expl.spec]p2). |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3011 | /// |
| 3012 | /// \param S the semantic analysis object for which this check is being |
| 3013 | /// performed. |
| 3014 | /// |
| 3015 | /// \param Specialized the entity being specialized or instantiated, which |
| 3016 | /// may be a kind of template (class template, function template, etc.) or |
| 3017 | /// a member of a class template (member function, static data member, |
| 3018 | /// member class). |
| 3019 | /// |
| 3020 | /// \param PrevDecl the previous declaration of this entity, if any. |
| 3021 | /// |
| 3022 | /// \param Loc the location of the explicit specialization or instantiation of |
| 3023 | /// this entity. |
| 3024 | /// |
| 3025 | /// \param IsPartialSpecialization whether this is a partial specialization of |
| 3026 | /// a class template. |
| 3027 | /// |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3028 | /// \returns true if there was an error that we cannot recover from, false |
| 3029 | /// otherwise. |
| 3030 | static bool CheckTemplateSpecializationScope(Sema &S, |
| 3031 | NamedDecl *Specialized, |
| 3032 | NamedDecl *PrevDecl, |
| 3033 | SourceLocation Loc, |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 3034 | bool IsPartialSpecialization) { |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3035 | // Keep these "kind" numbers in sync with the %select statements in the |
| 3036 | // various diagnostics emitted by this routine. |
| 3037 | int EntityKind = 0; |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 3038 | bool isTemplateSpecialization = false; |
| 3039 | if (isa<ClassTemplateDecl>(Specialized)) { |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3040 | EntityKind = IsPartialSpecialization? 1 : 0; |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 3041 | isTemplateSpecialization = true; |
| 3042 | } else if (isa<FunctionTemplateDecl>(Specialized)) { |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3043 | EntityKind = 2; |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 3044 | isTemplateSpecialization = true; |
| 3045 | } else if (isa<CXXMethodDecl>(Specialized)) |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3046 | EntityKind = 3; |
| 3047 | else if (isa<VarDecl>(Specialized)) |
| 3048 | EntityKind = 4; |
| 3049 | else if (isa<RecordDecl>(Specialized)) |
| 3050 | EntityKind = 5; |
| 3051 | else { |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 3052 | S.Diag(Loc, diag::err_template_spec_unknown_kind); |
| 3053 | S.Diag(Specialized->getLocation(), diag::note_specialized_entity); |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3054 | return true; |
| 3055 | } |
| 3056 | |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 3057 | // C++ [temp.expl.spec]p2: |
| 3058 | // An explicit specialization shall be declared in the namespace |
| 3059 | // of which the template is a member, or, for member templates, in |
| 3060 | // the namespace of which the enclosing class or enclosing class |
| 3061 | // template is a member. An explicit specialization of a member |
| 3062 | // function, member class or static data member of a class |
| 3063 | // template shall be declared in the namespace of which the class |
| 3064 | // template is a member. Such a declaration may also be a |
| 3065 | // definition. If the declaration is not a definition, the |
| 3066 | // specialization may be defined later in the name- space in which |
| 3067 | // the explicit specialization was declared, or in a namespace |
| 3068 | // that encloses the one in which the explicit specialization was |
| 3069 | // declared. |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3070 | if (S.CurContext->getLookupContext()->isFunctionOrMethod()) { |
| 3071 | S.Diag(Loc, diag::err_template_spec_decl_function_scope) |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 3072 | << Specialized; |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 3073 | return true; |
| 3074 | } |
Douglas Gregor | 7974c3b | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 3075 | |
Douglas Gregor | 0a40747 | 2009-10-07 17:30:37 +0000 | [diff] [blame] | 3076 | if (S.CurContext->isRecord() && !IsPartialSpecialization) { |
| 3077 | S.Diag(Loc, diag::err_template_spec_decl_class_scope) |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 3078 | << Specialized; |
Douglas Gregor | 0a40747 | 2009-10-07 17:30:37 +0000 | [diff] [blame] | 3079 | return true; |
| 3080 | } |
| 3081 | |
Douglas Gregor | 7974c3b | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 3082 | // C++ [temp.class.spec]p6: |
| 3083 | // A class template partial specialization may be declared or redeclared |
| 3084 | // in any namespace scope in which its definition may be defined (14.5.1 |
| 3085 | // and 14.5.2). |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3086 | bool ComplainedAboutScope = false; |
Douglas Gregor | 7974c3b | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 3087 | DeclContext *SpecializedContext |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3088 | = Specialized->getDeclContext()->getEnclosingNamespaceContext(); |
Douglas Gregor | 7974c3b | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 3089 | DeclContext *DC = S.CurContext->getEnclosingNamespaceContext(); |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 3090 | if ((!PrevDecl || |
| 3091 | getTemplateSpecializationKind(PrevDecl) == TSK_Undeclared || |
| 3092 | getTemplateSpecializationKind(PrevDecl) == TSK_ImplicitInstantiation)){ |
| 3093 | // There is no prior declaration of this entity, so this |
| 3094 | // specialization must be in the same context as the template |
| 3095 | // itself. |
| 3096 | if (!DC->Equals(SpecializedContext)) { |
| 3097 | if (isa<TranslationUnitDecl>(SpecializedContext)) |
| 3098 | S.Diag(Loc, diag::err_template_spec_decl_out_of_scope_global) |
| 3099 | << EntityKind << Specialized; |
| 3100 | else if (isa<NamespaceDecl>(SpecializedContext)) |
| 3101 | S.Diag(Loc, diag::err_template_spec_decl_out_of_scope) |
| 3102 | << EntityKind << Specialized |
| 3103 | << cast<NamedDecl>(SpecializedContext); |
| 3104 | |
| 3105 | S.Diag(Specialized->getLocation(), diag::note_specialized_entity); |
| 3106 | ComplainedAboutScope = true; |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 3107 | } |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 3108 | } |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3109 | |
| 3110 | // Make sure that this redeclaration (or definition) occurs in an enclosing |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 3111 | // namespace. |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3112 | // Note that HandleDeclarator() performs this check for explicit |
| 3113 | // specializations of function templates, static data members, and member |
| 3114 | // functions, so we skip the check here for those kinds of entities. |
| 3115 | // FIXME: HandleDeclarator's diagnostics aren't quite as good, though. |
Douglas Gregor | 7974c3b | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 3116 | // Should we refactor that check, so that it occurs later? |
| 3117 | if (!ComplainedAboutScope && !DC->Encloses(SpecializedContext) && |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 3118 | !(isa<FunctionTemplateDecl>(Specialized) || isa<VarDecl>(Specialized) || |
| 3119 | isa<FunctionDecl>(Specialized))) { |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3120 | if (isa<TranslationUnitDecl>(SpecializedContext)) |
| 3121 | S.Diag(Loc, diag::err_template_spec_redecl_global_scope) |
| 3122 | << EntityKind << Specialized; |
| 3123 | else if (isa<NamespaceDecl>(SpecializedContext)) |
| 3124 | S.Diag(Loc, diag::err_template_spec_redecl_out_of_scope) |
| 3125 | << EntityKind << Specialized |
| 3126 | << cast<NamedDecl>(SpecializedContext); |
| 3127 | |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 3128 | S.Diag(Specialized->getLocation(), diag::note_specialized_entity); |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 3129 | } |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3130 | |
| 3131 | // FIXME: check for specialization-after-instantiation errors and such. |
| 3132 | |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 3133 | return false; |
| 3134 | } |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3135 | |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 3136 | /// \brief Check the non-type template arguments of a class template |
| 3137 | /// partial specialization according to C++ [temp.class.spec]p9. |
| 3138 | /// |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3139 | /// \param TemplateParams the template parameters of the primary class |
| 3140 | /// template. |
| 3141 | /// |
| 3142 | /// \param TemplateArg the template arguments of the class template |
| 3143 | /// partial specialization. |
| 3144 | /// |
| 3145 | /// \param MirrorsPrimaryTemplate will be set true if the class |
| 3146 | /// template partial specialization arguments are identical to the |
| 3147 | /// implicit template arguments of the primary template. This is not |
| 3148 | /// necessarily an error (C++0x), and it is left to the caller to diagnose |
| 3149 | /// this condition when it is an error. |
| 3150 | /// |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 3151 | /// \returns true if there was an error, false otherwise. |
| 3152 | bool Sema::CheckClassTemplatePartialSpecializationArgs( |
| 3153 | TemplateParameterList *TemplateParams, |
Anders Carlsson | 6360be7 | 2009-06-13 18:20:51 +0000 | [diff] [blame] | 3154 | const TemplateArgumentListBuilder &TemplateArgs, |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3155 | bool &MirrorsPrimaryTemplate) { |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 3156 | // FIXME: the interface to this function will have to change to |
| 3157 | // accommodate variadic templates. |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3158 | MirrorsPrimaryTemplate = true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3159 | |
Anders Carlsson | fb25052 | 2009-06-23 01:26:57 +0000 | [diff] [blame] | 3160 | const TemplateArgument *ArgList = TemplateArgs.getFlatArguments(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3161 | |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 3162 | for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) { |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3163 | // Determine whether the template argument list of the partial |
| 3164 | // specialization is identical to the implicit argument list of |
| 3165 | // the primary template. The caller may need to diagnostic this as |
| 3166 | // an error per C++ [temp.class.spec]p9b3. |
| 3167 | if (MirrorsPrimaryTemplate) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3168 | if (TemplateTypeParmDecl *TTP |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3169 | = dyn_cast<TemplateTypeParmDecl>(TemplateParams->getParam(I))) { |
| 3170 | if (Context.getCanonicalType(Context.getTypeDeclType(TTP)) != |
Anders Carlsson | 6360be7 | 2009-06-13 18:20:51 +0000 | [diff] [blame] | 3171 | Context.getCanonicalType(ArgList[I].getAsType())) |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3172 | MirrorsPrimaryTemplate = false; |
| 3173 | } else if (TemplateTemplateParmDecl *TTP |
| 3174 | = dyn_cast<TemplateTemplateParmDecl>( |
| 3175 | TemplateParams->getParam(I))) { |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 3176 | TemplateName Name = ArgList[I].getAsTemplate(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3177 | TemplateTemplateParmDecl *ArgDecl |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 3178 | = dyn_cast_or_null<TemplateTemplateParmDecl>(Name.getAsTemplateDecl()); |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3179 | if (!ArgDecl || |
| 3180 | ArgDecl->getIndex() != TTP->getIndex() || |
| 3181 | ArgDecl->getDepth() != TTP->getDepth()) |
| 3182 | MirrorsPrimaryTemplate = false; |
| 3183 | } |
| 3184 | } |
| 3185 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3186 | NonTypeTemplateParmDecl *Param |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 3187 | = dyn_cast<NonTypeTemplateParmDecl>(TemplateParams->getParam(I)); |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3188 | if (!Param) { |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 3189 | continue; |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3190 | } |
| 3191 | |
Anders Carlsson | 6360be7 | 2009-06-13 18:20:51 +0000 | [diff] [blame] | 3192 | Expr *ArgExpr = ArgList[I].getAsExpr(); |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3193 | if (!ArgExpr) { |
| 3194 | MirrorsPrimaryTemplate = false; |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 3195 | continue; |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3196 | } |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 3197 | |
| 3198 | // C++ [temp.class.spec]p8: |
| 3199 | // A non-type argument is non-specialized if it is the name of a |
| 3200 | // non-type parameter. All other non-type arguments are |
| 3201 | // specialized. |
| 3202 | // |
| 3203 | // Below, we check the two conditions that only apply to |
| 3204 | // specialized non-type arguments, so skip any non-specialized |
| 3205 | // arguments. |
| 3206 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ArgExpr)) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3207 | if (NonTypeTemplateParmDecl *NTTP |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3208 | = dyn_cast<NonTypeTemplateParmDecl>(DRE->getDecl())) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3209 | if (MirrorsPrimaryTemplate && |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3210 | (Param->getIndex() != NTTP->getIndex() || |
| 3211 | Param->getDepth() != NTTP->getDepth())) |
| 3212 | MirrorsPrimaryTemplate = false; |
| 3213 | |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 3214 | continue; |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3215 | } |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 3216 | |
| 3217 | // C++ [temp.class.spec]p9: |
| 3218 | // Within the argument list of a class template partial |
| 3219 | // specialization, the following restrictions apply: |
| 3220 | // -- A partially specialized non-type argument expression |
| 3221 | // shall not involve a template parameter of the partial |
| 3222 | // specialization except when the argument expression is a |
| 3223 | // simple identifier. |
| 3224 | if (ArgExpr->isTypeDependent() || ArgExpr->isValueDependent()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3225 | Diag(ArgExpr->getLocStart(), |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 3226 | diag::err_dependent_non_type_arg_in_partial_spec) |
| 3227 | << ArgExpr->getSourceRange(); |
| 3228 | return true; |
| 3229 | } |
| 3230 | |
| 3231 | // -- The type of a template parameter corresponding to a |
| 3232 | // specialized non-type argument shall not be dependent on a |
| 3233 | // parameter of the specialization. |
| 3234 | if (Param->getType()->isDependentType()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3235 | Diag(ArgExpr->getLocStart(), |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 3236 | diag::err_dependent_typed_non_type_arg_in_partial_spec) |
| 3237 | << Param->getType() |
| 3238 | << ArgExpr->getSourceRange(); |
| 3239 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 3240 | return true; |
| 3241 | } |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3242 | |
| 3243 | MirrorsPrimaryTemplate = false; |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 3244 | } |
| 3245 | |
| 3246 | return false; |
| 3247 | } |
| 3248 | |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 3249 | Sema::DeclResult |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 3250 | Sema::ActOnClassTemplateSpecialization(Scope *S, unsigned TagSpec, |
| 3251 | TagUseKind TUK, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3252 | SourceLocation KWLoc, |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3253 | const CXXScopeSpec &SS, |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 3254 | TemplateTy TemplateD, |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3255 | SourceLocation TemplateNameLoc, |
| 3256 | SourceLocation LAngleLoc, |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 3257 | ASTTemplateArgsPtr TemplateArgsIn, |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3258 | SourceLocation RAngleLoc, |
| 3259 | AttributeList *Attr, |
| 3260 | MultiTemplateParamsArg TemplateParameterLists) { |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 3261 | assert(TUK != TUK_Reference && "References are not specializations"); |
John McCall | f1bbbb4 | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 3262 | |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3263 | // Find the class template we're specializing |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 3264 | TemplateName Name = TemplateD.getAsVal<TemplateName>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3265 | ClassTemplateDecl *ClassTemplate |
Douglas Gregor | 8b13c08 | 2009-11-12 00:46:20 +0000 | [diff] [blame] | 3266 | = dyn_cast_or_null<ClassTemplateDecl>(Name.getAsTemplateDecl()); |
| 3267 | |
| 3268 | if (!ClassTemplate) { |
| 3269 | Diag(TemplateNameLoc, diag::err_not_class_template_specialization) |
| 3270 | << (Name.getAsTemplateDecl() && |
| 3271 | isa<TemplateTemplateParmDecl>(Name.getAsTemplateDecl())); |
| 3272 | return true; |
| 3273 | } |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3274 | |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 3275 | bool isExplicitSpecialization = false; |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 3276 | bool isPartialSpecialization = false; |
| 3277 | |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 3278 | // Check the validity of the template headers that introduce this |
| 3279 | // template. |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 3280 | // FIXME: We probably shouldn't complain about these headers for |
| 3281 | // friend declarations. |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 3282 | TemplateParameterList *TemplateParams |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3283 | = MatchTemplateParametersToScopeSpecifier(TemplateNameLoc, SS, |
| 3284 | (TemplateParameterList**)TemplateParameterLists.get(), |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 3285 | TemplateParameterLists.size(), |
| 3286 | isExplicitSpecialization); |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 3287 | if (TemplateParams && TemplateParams->size() > 0) { |
| 3288 | isPartialSpecialization = true; |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 3289 | |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 3290 | // C++ [temp.class.spec]p10: |
| 3291 | // The template parameter list of a specialization shall not |
| 3292 | // contain default template argument values. |
| 3293 | for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) { |
| 3294 | Decl *Param = TemplateParams->getParam(I); |
| 3295 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) { |
| 3296 | if (TTP->hasDefaultArgument()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3297 | Diag(TTP->getDefaultArgumentLoc(), |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 3298 | diag::err_default_arg_in_partial_spec); |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 3299 | TTP->removeDefaultArgument(); |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 3300 | } |
| 3301 | } else if (NonTypeTemplateParmDecl *NTTP |
| 3302 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
| 3303 | if (Expr *DefArg = NTTP->getDefaultArgument()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3304 | Diag(NTTP->getDefaultArgumentLoc(), |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 3305 | diag::err_default_arg_in_partial_spec) |
| 3306 | << DefArg->getSourceRange(); |
| 3307 | NTTP->setDefaultArgument(0); |
| 3308 | DefArg->Destroy(Context); |
| 3309 | } |
| 3310 | } else { |
| 3311 | TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(Param); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 3312 | if (TTP->hasDefaultArgument()) { |
| 3313 | Diag(TTP->getDefaultArgument().getLocation(), |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 3314 | diag::err_default_arg_in_partial_spec) |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 3315 | << TTP->getDefaultArgument().getSourceRange(); |
| 3316 | TTP->setDefaultArgument(TemplateArgumentLoc()); |
Douglas Gregor | ba1ecb5 | 2009-06-12 19:43:02 +0000 | [diff] [blame] | 3317 | } |
| 3318 | } |
| 3319 | } |
Douglas Gregor | a735b20 | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 3320 | } else if (TemplateParams) { |
| 3321 | if (TUK == TUK_Friend) |
| 3322 | Diag(KWLoc, diag::err_template_spec_friend) |
| 3323 | << CodeModificationHint::CreateRemoval( |
| 3324 | SourceRange(TemplateParams->getTemplateLoc(), |
| 3325 | TemplateParams->getRAngleLoc())) |
| 3326 | << SourceRange(LAngleLoc, RAngleLoc); |
| 3327 | else |
| 3328 | isExplicitSpecialization = true; |
| 3329 | } else if (TUK != TUK_Friend) { |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 3330 | Diag(KWLoc, diag::err_template_spec_needs_header) |
| 3331 | << CodeModificationHint::CreateInsertion(KWLoc, "template<> "); |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 3332 | isExplicitSpecialization = true; |
| 3333 | } |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 3334 | |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3335 | // Check that the specialization uses the same tag kind as the |
| 3336 | // original template. |
| 3337 | TagDecl::TagKind Kind; |
| 3338 | switch (TagSpec) { |
| 3339 | default: assert(0 && "Unknown tag type!"); |
| 3340 | case DeclSpec::TST_struct: Kind = TagDecl::TK_struct; break; |
| 3341 | case DeclSpec::TST_union: Kind = TagDecl::TK_union; break; |
| 3342 | case DeclSpec::TST_class: Kind = TagDecl::TK_class; break; |
| 3343 | } |
Douglas Gregor | 501c5ce | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 3344 | if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3345 | Kind, KWLoc, |
Douglas Gregor | 501c5ce | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 3346 | *ClassTemplate->getIdentifier())) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3347 | Diag(KWLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 3348 | << ClassTemplate |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3349 | << CodeModificationHint::CreateReplacement(KWLoc, |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 3350 | ClassTemplate->getTemplatedDecl()->getKindName()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3351 | Diag(ClassTemplate->getTemplatedDecl()->getLocation(), |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3352 | diag::note_previous_use); |
| 3353 | Kind = ClassTemplate->getTemplatedDecl()->getTagKind(); |
| 3354 | } |
| 3355 | |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 3356 | // Translate the parser's template argument list in our AST format. |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 3357 | TemplateArgumentListInfo TemplateArgs; |
| 3358 | TemplateArgs.setLAngleLoc(LAngleLoc); |
| 3359 | TemplateArgs.setRAngleLoc(RAngleLoc); |
Douglas Gregor | 314b97f | 2009-11-10 19:49:08 +0000 | [diff] [blame] | 3360 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 3361 | |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3362 | // Check that the template argument list is well-formed for this |
| 3363 | // template. |
Anders Carlsson | fb25052 | 2009-06-23 01:26:57 +0000 | [diff] [blame] | 3364 | TemplateArgumentListBuilder Converted(ClassTemplate->getTemplateParameters(), |
| 3365 | TemplateArgs.size()); |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 3366 | if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc, |
| 3367 | TemplateArgs, false, Converted)) |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 3368 | return true; |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3369 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3370 | assert((Converted.structuredSize() == |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3371 | ClassTemplate->getTemplateParameters()->size()) && |
| 3372 | "Converted template argument list is too short!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3373 | |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 3374 | // Find the class template (partial) specialization declaration that |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3375 | // corresponds to these arguments. |
| 3376 | llvm::FoldingSetNodeID ID; |
Douglas Gregor | ba1ecb5 | 2009-06-12 19:43:02 +0000 | [diff] [blame] | 3377 | if (isPartialSpecialization) { |
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 | if (CheckClassTemplatePartialSpecializationArgs( |
| 3380 | ClassTemplate->getTemplateParameters(), |
Anders Carlsson | fb25052 | 2009-06-23 01:26:57 +0000 | [diff] [blame] | 3381 | Converted, MirrorsPrimaryTemplate)) |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 3382 | return true; |
| 3383 | |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3384 | if (MirrorsPrimaryTemplate) { |
| 3385 | // C++ [temp.class.spec]p9b3: |
| 3386 | // |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3387 | // -- The argument list of the specialization shall not be identical |
| 3388 | // to the implicit argument list of the primary template. |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3389 | Diag(TemplateNameLoc, diag::err_partial_spec_args_match_primary_template) |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 3390 | << (TUK == TUK_Definition) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3391 | << CodeModificationHint::CreateRemoval(SourceRange(LAngleLoc, |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3392 | RAngleLoc)); |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 3393 | return CheckClassTemplate(S, TagSpec, TUK, KWLoc, SS, |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3394 | ClassTemplate->getIdentifier(), |
| 3395 | TemplateNameLoc, |
| 3396 | Attr, |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 3397 | TemplateParams, |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3398 | AS_none); |
| 3399 | } |
| 3400 | |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 3401 | // FIXME: Diagnose friend partial specializations |
| 3402 | |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 3403 | // FIXME: Template parameter list matters, too |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3404 | ClassTemplatePartialSpecializationDecl::Profile(ID, |
Anders Carlsson | fb25052 | 2009-06-23 01:26:57 +0000 | [diff] [blame] | 3405 | Converted.getFlatArguments(), |
Douglas Gregor | 828e226 | 2009-07-29 16:09:57 +0000 | [diff] [blame] | 3406 | Converted.flatSize(), |
| 3407 | Context); |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 3408 | } else |
Anders Carlsson | 1c5976e | 2009-06-05 03:43:12 +0000 | [diff] [blame] | 3409 | ClassTemplateSpecializationDecl::Profile(ID, |
Anders Carlsson | fb25052 | 2009-06-23 01:26:57 +0000 | [diff] [blame] | 3410 | Converted.getFlatArguments(), |
Douglas Gregor | 828e226 | 2009-07-29 16:09:57 +0000 | [diff] [blame] | 3411 | Converted.flatSize(), |
| 3412 | Context); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3413 | void *InsertPos = 0; |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 3414 | ClassTemplateSpecializationDecl *PrevDecl = 0; |
| 3415 | |
| 3416 | if (isPartialSpecialization) |
| 3417 | PrevDecl |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3418 | = ClassTemplate->getPartialSpecializations().FindNodeOrInsertPos(ID, |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 3419 | InsertPos); |
| 3420 | else |
| 3421 | PrevDecl |
| 3422 | = ClassTemplate->getSpecializations().FindNodeOrInsertPos(ID, InsertPos); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3423 | |
| 3424 | ClassTemplateSpecializationDecl *Specialization = 0; |
| 3425 | |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 3426 | // Check whether we can declare a class template specialization in |
| 3427 | // the current scope. |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 3428 | if (TUK != TUK_Friend && |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3429 | CheckTemplateSpecializationScope(*this, ClassTemplate, PrevDecl, |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 3430 | TemplateNameLoc, |
| 3431 | isPartialSpecialization)) |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 3432 | return true; |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 3433 | |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 3434 | // The canonical type |
| 3435 | QualType CanonType; |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 3436 | if (PrevDecl && |
| 3437 | (PrevDecl->getSpecializationKind() == TSK_Undeclared || |
| 3438 | TUK == TUK_Friend)) { |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3439 | // Since the only prior class template specialization with these |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 3440 | // arguments was referenced but not declared, or we're only |
| 3441 | // referencing this specialization as a friend, reuse that |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3442 | // declaration node as our own, updating its source location to |
| 3443 | // reflect our new declaration. |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3444 | Specialization = PrevDecl; |
Douglas Gregor | 6bc9f7e | 2009-02-25 22:18:32 +0000 | [diff] [blame] | 3445 | Specialization->setLocation(TemplateNameLoc); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3446 | PrevDecl = 0; |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 3447 | CanonType = Context.getTypeDeclType(Specialization); |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 3448 | } else if (isPartialSpecialization) { |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 3449 | // Build the canonical type that describes the converted template |
| 3450 | // arguments of the class template partial specialization. |
| 3451 | CanonType = Context.getTemplateSpecializationType( |
| 3452 | TemplateName(ClassTemplate), |
| 3453 | Converted.getFlatArguments(), |
| 3454 | Converted.flatSize()); |
| 3455 | |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 3456 | // Create a new class template partial specialization declaration node. |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 3457 | ClassTemplatePartialSpecializationDecl *PrevPartial |
| 3458 | = cast_or_null<ClassTemplatePartialSpecializationDecl>(PrevDecl); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3459 | ClassTemplatePartialSpecializationDecl *Partial |
| 3460 | = ClassTemplatePartialSpecializationDecl::Create(Context, |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 3461 | ClassTemplate->getDeclContext(), |
Anders Carlsson | 91fdf6f | 2009-06-05 04:06:48 +0000 | [diff] [blame] | 3462 | TemplateNameLoc, |
| 3463 | TemplateParams, |
| 3464 | ClassTemplate, |
Anders Carlsson | fb25052 | 2009-06-23 01:26:57 +0000 | [diff] [blame] | 3465 | Converted, |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 3466 | TemplateArgs, |
Anders Carlsson | 91fdf6f | 2009-06-05 04:06:48 +0000 | [diff] [blame] | 3467 | PrevPartial); |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 3468 | |
| 3469 | if (PrevPartial) { |
| 3470 | ClassTemplate->getPartialSpecializations().RemoveNode(PrevPartial); |
| 3471 | ClassTemplate->getPartialSpecializations().GetOrInsertNode(Partial); |
| 3472 | } else { |
| 3473 | ClassTemplate->getPartialSpecializations().InsertNode(Partial, InsertPos); |
| 3474 | } |
| 3475 | Specialization = Partial; |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 3476 | |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 3477 | // If we are providing an explicit specialization of a member class |
| 3478 | // template specialization, make a note of that. |
| 3479 | if (PrevPartial && PrevPartial->getInstantiatedFromMember()) |
| 3480 | PrevPartial->setMemberSpecialization(); |
| 3481 | |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 3482 | // Check that all of the template parameters of the class template |
| 3483 | // partial specialization are deducible from the template |
| 3484 | // arguments. If not, this class template partial specialization |
| 3485 | // will never be used. |
| 3486 | llvm::SmallVector<bool, 8> DeducibleParams; |
| 3487 | DeducibleParams.resize(TemplateParams->size()); |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 3488 | MarkUsedTemplateParameters(Partial->getTemplateArgs(), true, |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 3489 | TemplateParams->getDepth(), |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 3490 | DeducibleParams); |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 3491 | unsigned NumNonDeducible = 0; |
| 3492 | for (unsigned I = 0, N = DeducibleParams.size(); I != N; ++I) |
| 3493 | if (!DeducibleParams[I]) |
| 3494 | ++NumNonDeducible; |
| 3495 | |
| 3496 | if (NumNonDeducible) { |
| 3497 | Diag(TemplateNameLoc, diag::warn_partial_specs_not_deducible) |
| 3498 | << (NumNonDeducible > 1) |
| 3499 | << SourceRange(TemplateNameLoc, RAngleLoc); |
| 3500 | for (unsigned I = 0, N = DeducibleParams.size(); I != N; ++I) { |
| 3501 | if (!DeducibleParams[I]) { |
| 3502 | NamedDecl *Param = cast<NamedDecl>(TemplateParams->getParam(I)); |
| 3503 | if (Param->getDeclName()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3504 | Diag(Param->getLocation(), |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 3505 | diag::note_partial_spec_unused_parameter) |
| 3506 | << Param->getDeclName(); |
| 3507 | else |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3508 | Diag(Param->getLocation(), |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 3509 | diag::note_partial_spec_unused_parameter) |
| 3510 | << std::string("<anonymous>"); |
| 3511 | } |
| 3512 | } |
| 3513 | } |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3514 | } else { |
| 3515 | // Create a new class template specialization declaration node for |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 3516 | // this explicit specialization or friend declaration. |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3517 | Specialization |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3518 | = ClassTemplateSpecializationDecl::Create(Context, |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3519 | ClassTemplate->getDeclContext(), |
| 3520 | TemplateNameLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3521 | ClassTemplate, |
Anders Carlsson | fb25052 | 2009-06-23 01:26:57 +0000 | [diff] [blame] | 3522 | Converted, |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3523 | PrevDecl); |
| 3524 | |
| 3525 | if (PrevDecl) { |
| 3526 | ClassTemplate->getSpecializations().RemoveNode(PrevDecl); |
| 3527 | ClassTemplate->getSpecializations().GetOrInsertNode(Specialization); |
| 3528 | } else { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3529 | ClassTemplate->getSpecializations().InsertNode(Specialization, |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3530 | InsertPos); |
| 3531 | } |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 3532 | |
| 3533 | CanonType = Context.getTypeDeclType(Specialization); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3534 | } |
| 3535 | |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 3536 | // C++ [temp.expl.spec]p6: |
| 3537 | // If a template, a member template or the member of a class template is |
| 3538 | // explicitly specialized then that specialization shall be declared |
| 3539 | // before the first use of that specialization that would cause an implicit |
| 3540 | // instantiation to take place, in every translation unit in which such a |
| 3541 | // use occurs; no diagnostic is required. |
| 3542 | if (PrevDecl && PrevDecl->getPointOfInstantiation().isValid()) { |
| 3543 | SourceRange Range(TemplateNameLoc, RAngleLoc); |
| 3544 | Diag(TemplateNameLoc, diag::err_specialization_after_instantiation) |
| 3545 | << Context.getTypeDeclType(Specialization) << Range; |
| 3546 | |
| 3547 | Diag(PrevDecl->getPointOfInstantiation(), |
| 3548 | diag::note_instantiation_required_here) |
| 3549 | << (PrevDecl->getTemplateSpecializationKind() |
| 3550 | != TSK_ImplicitInstantiation); |
| 3551 | return true; |
| 3552 | } |
| 3553 | |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 3554 | // If this is not a friend, note that this is an explicit specialization. |
| 3555 | if (TUK != TUK_Friend) |
| 3556 | Specialization->setSpecializationKind(TSK_ExplicitSpecialization); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3557 | |
| 3558 | // Check that this isn't a redefinition of this specialization. |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 3559 | if (TUK == TUK_Definition) { |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3560 | if (RecordDecl *Def = Specialization->getDefinition(Context)) { |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3561 | SourceRange Range(TemplateNameLoc, RAngleLoc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3562 | Diag(TemplateNameLoc, diag::err_redefinition) |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 3563 | << Context.getTypeDeclType(Specialization) << Range; |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3564 | Diag(Def->getLocation(), diag::note_previous_definition); |
| 3565 | Specialization->setInvalidDecl(); |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 3566 | return true; |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3567 | } |
| 3568 | } |
| 3569 | |
Douglas Gregor | fc705b8 | 2009-02-26 22:19:44 +0000 | [diff] [blame] | 3570 | // Build the fully-sugared type for this class template |
| 3571 | // specialization as the user wrote in the specialization |
| 3572 | // itself. This means that we'll pretty-print the type retrieved |
| 3573 | // from the specialization's declaration the way that the user |
| 3574 | // actually wrote the specialization, rather than formatting the |
| 3575 | // name based on the "canonical" representation used to store the |
| 3576 | // template arguments in the specialization. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3577 | QualType WrittenTy |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 3578 | = Context.getTemplateSpecializationType(Name, TemplateArgs, CanonType); |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 3579 | if (TUK != TUK_Friend) |
| 3580 | Specialization->setTypeAsWritten(WrittenTy); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 3581 | TemplateArgsIn.release(); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3582 | |
Douglas Gregor | 6bc9f7e | 2009-02-25 22:18:32 +0000 | [diff] [blame] | 3583 | // C++ [temp.expl.spec]p9: |
| 3584 | // A template explicit specialization is in the scope of the |
| 3585 | // namespace in which the template was defined. |
| 3586 | // |
| 3587 | // We actually implement this paragraph where we set the semantic |
| 3588 | // context (in the creation of the ClassTemplateSpecializationDecl), |
| 3589 | // but we also maintain the lexical context where the actual |
| 3590 | // definition occurs. |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3591 | Specialization->setLexicalDeclContext(CurContext); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3592 | |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3593 | // We may be starting the definition of this specialization. |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 3594 | if (TUK == TUK_Definition) |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3595 | Specialization->startDefinition(); |
| 3596 | |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 3597 | if (TUK == TUK_Friend) { |
| 3598 | FriendDecl *Friend = FriendDecl::Create(Context, CurContext, |
| 3599 | TemplateNameLoc, |
| 3600 | WrittenTy.getTypePtr(), |
| 3601 | /*FIXME:*/KWLoc); |
| 3602 | Friend->setAccess(AS_public); |
| 3603 | CurContext->addDecl(Friend); |
| 3604 | } else { |
| 3605 | // Add the specialization into its lexical context, so that it can |
| 3606 | // be seen when iterating through the list of declarations in that |
| 3607 | // context. However, specializations are not found by name lookup. |
| 3608 | CurContext->addDecl(Specialization); |
| 3609 | } |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 3610 | return DeclPtrTy::make(Specialization); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3611 | } |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3612 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3613 | Sema::DeclPtrTy |
| 3614 | Sema::ActOnTemplateDeclarator(Scope *S, |
Douglas Gregor | e542c86 | 2009-06-23 23:11:28 +0000 | [diff] [blame] | 3615 | MultiTemplateParamsArg TemplateParameterLists, |
| 3616 | Declarator &D) { |
| 3617 | return HandleDeclarator(S, D, move(TemplateParameterLists), false); |
| 3618 | } |
| 3619 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3620 | Sema::DeclPtrTy |
| 3621 | Sema::ActOnStartOfFunctionTemplateDef(Scope *FnBodyScope, |
Douglas Gregor | 52591bf | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 3622 | MultiTemplateParamsArg TemplateParameterLists, |
| 3623 | Declarator &D) { |
| 3624 | assert(getCurFunctionDecl() == 0 && "Function parsing confused"); |
| 3625 | assert(D.getTypeObject(0).Kind == DeclaratorChunk::Function && |
| 3626 | "Not a function declarator!"); |
| 3627 | DeclaratorChunk::FunctionTypeInfo &FTI = D.getTypeObject(0).Fun; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3628 | |
Douglas Gregor | 52591bf | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 3629 | if (FTI.hasPrototype) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3630 | // FIXME: Diagnose arguments without names in C. |
Douglas Gregor | 52591bf | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 3631 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3632 | |
Douglas Gregor | 52591bf | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 3633 | Scope *ParentScope = FnBodyScope->getParent(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3634 | |
| 3635 | DeclPtrTy DP = HandleDeclarator(ParentScope, D, |
Douglas Gregor | 52591bf | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 3636 | move(TemplateParameterLists), |
| 3637 | /*IsFunctionDefinition=*/true); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3638 | if (FunctionTemplateDecl *FunctionTemplate |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 3639 | = dyn_cast_or_null<FunctionTemplateDecl>(DP.getAs<Decl>())) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3640 | return ActOnStartOfFunctionDef(FnBodyScope, |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3641 | DeclPtrTy::make(FunctionTemplate->getTemplatedDecl())); |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 3642 | if (FunctionDecl *Function = dyn_cast_or_null<FunctionDecl>(DP.getAs<Decl>())) |
| 3643 | return ActOnStartOfFunctionDef(FnBodyScope, DeclPtrTy::make(Function)); |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3644 | return DeclPtrTy(); |
Douglas Gregor | 52591bf | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 3645 | } |
| 3646 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 3647 | /// \brief Diagnose cases where we have an explicit template specialization |
| 3648 | /// before/after an explicit template instantiation, producing diagnostics |
| 3649 | /// for those cases where they are required and determining whether the |
| 3650 | /// new specialization/instantiation will have any effect. |
| 3651 | /// |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 3652 | /// \param NewLoc the location of the new explicit specialization or |
| 3653 | /// instantiation. |
| 3654 | /// |
| 3655 | /// \param NewTSK the kind of the new explicit specialization or instantiation. |
| 3656 | /// |
| 3657 | /// \param PrevDecl the previous declaration of the entity. |
| 3658 | /// |
| 3659 | /// \param PrevTSK the kind of the old explicit specialization or instantiatin. |
| 3660 | /// |
| 3661 | /// \param PrevPointOfInstantiation if valid, indicates where the previus |
| 3662 | /// declaration was instantiated (either implicitly or explicitly). |
| 3663 | /// |
| 3664 | /// \param SuppressNew will be set to true to indicate that the new |
| 3665 | /// specialization or instantiation has no effect and should be ignored. |
| 3666 | /// |
| 3667 | /// \returns true if there was an error that should prevent the introduction of |
| 3668 | /// the new declaration into the AST, false otherwise. |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 3669 | bool |
| 3670 | Sema::CheckSpecializationInstantiationRedecl(SourceLocation NewLoc, |
| 3671 | TemplateSpecializationKind NewTSK, |
| 3672 | NamedDecl *PrevDecl, |
| 3673 | TemplateSpecializationKind PrevTSK, |
| 3674 | SourceLocation PrevPointOfInstantiation, |
| 3675 | bool &SuppressNew) { |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 3676 | SuppressNew = false; |
| 3677 | |
| 3678 | switch (NewTSK) { |
| 3679 | case TSK_Undeclared: |
| 3680 | case TSK_ImplicitInstantiation: |
| 3681 | assert(false && "Don't check implicit instantiations here"); |
| 3682 | return false; |
| 3683 | |
| 3684 | case TSK_ExplicitSpecialization: |
| 3685 | switch (PrevTSK) { |
| 3686 | case TSK_Undeclared: |
| 3687 | case TSK_ExplicitSpecialization: |
| 3688 | // Okay, we're just specializing something that is either already |
| 3689 | // explicitly specialized or has merely been mentioned without any |
| 3690 | // instantiation. |
| 3691 | return false; |
| 3692 | |
| 3693 | case TSK_ImplicitInstantiation: |
| 3694 | if (PrevPointOfInstantiation.isInvalid()) { |
| 3695 | // The declaration itself has not actually been instantiated, so it is |
| 3696 | // still okay to specialize it. |
| 3697 | return false; |
| 3698 | } |
| 3699 | // Fall through |
| 3700 | |
| 3701 | case TSK_ExplicitInstantiationDeclaration: |
| 3702 | case TSK_ExplicitInstantiationDefinition: |
| 3703 | assert((PrevTSK == TSK_ImplicitInstantiation || |
| 3704 | PrevPointOfInstantiation.isValid()) && |
| 3705 | "Explicit instantiation without point of instantiation?"); |
| 3706 | |
| 3707 | // C++ [temp.expl.spec]p6: |
| 3708 | // If a template, a member template or the member of a class template |
| 3709 | // is explicitly specialized then that specialization shall be declared |
| 3710 | // before the first use of that specialization that would cause an |
| 3711 | // implicit instantiation to take place, in every translation unit in |
| 3712 | // which such a use occurs; no diagnostic is required. |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 3713 | Diag(NewLoc, diag::err_specialization_after_instantiation) |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 3714 | << PrevDecl; |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 3715 | Diag(PrevPointOfInstantiation, diag::note_instantiation_required_here) |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 3716 | << (PrevTSK != TSK_ImplicitInstantiation); |
| 3717 | |
| 3718 | return true; |
| 3719 | } |
| 3720 | break; |
| 3721 | |
| 3722 | case TSK_ExplicitInstantiationDeclaration: |
| 3723 | switch (PrevTSK) { |
| 3724 | case TSK_ExplicitInstantiationDeclaration: |
| 3725 | // This explicit instantiation declaration is redundant (that's okay). |
| 3726 | SuppressNew = true; |
| 3727 | return false; |
| 3728 | |
| 3729 | case TSK_Undeclared: |
| 3730 | case TSK_ImplicitInstantiation: |
| 3731 | // We're explicitly instantiating something that may have already been |
| 3732 | // implicitly instantiated; that's fine. |
| 3733 | return false; |
| 3734 | |
| 3735 | case TSK_ExplicitSpecialization: |
| 3736 | // C++0x [temp.explicit]p4: |
| 3737 | // For a given set of template parameters, if an explicit instantiation |
| 3738 | // of a template appears after a declaration of an explicit |
| 3739 | // specialization for that template, the explicit instantiation has no |
| 3740 | // effect. |
| 3741 | return false; |
| 3742 | |
| 3743 | case TSK_ExplicitInstantiationDefinition: |
| 3744 | // C++0x [temp.explicit]p10: |
| 3745 | // If an entity is the subject of both an explicit instantiation |
| 3746 | // declaration and an explicit instantiation definition in the same |
| 3747 | // translation unit, the definition shall follow the declaration. |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 3748 | Diag(NewLoc, |
| 3749 | diag::err_explicit_instantiation_declaration_after_definition); |
| 3750 | Diag(PrevPointOfInstantiation, |
| 3751 | diag::note_explicit_instantiation_definition_here); |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 3752 | assert(PrevPointOfInstantiation.isValid() && |
| 3753 | "Explicit instantiation without point of instantiation?"); |
| 3754 | SuppressNew = true; |
| 3755 | return false; |
| 3756 | } |
| 3757 | break; |
| 3758 | |
| 3759 | case TSK_ExplicitInstantiationDefinition: |
| 3760 | switch (PrevTSK) { |
| 3761 | case TSK_Undeclared: |
| 3762 | case TSK_ImplicitInstantiation: |
| 3763 | // We're explicitly instantiating something that may have already been |
| 3764 | // implicitly instantiated; that's fine. |
| 3765 | return false; |
| 3766 | |
| 3767 | case TSK_ExplicitSpecialization: |
| 3768 | // C++ DR 259, C++0x [temp.explicit]p4: |
| 3769 | // For a given set of template parameters, if an explicit |
| 3770 | // instantiation of a template appears after a declaration of |
| 3771 | // an explicit specialization for that template, the explicit |
| 3772 | // instantiation has no effect. |
| 3773 | // |
| 3774 | // In C++98/03 mode, we only give an extension warning here, because it |
| 3775 | // is not not harmful to try to explicitly instantiate something that |
| 3776 | // has been explicitly specialized. |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 3777 | if (!getLangOptions().CPlusPlus0x) { |
| 3778 | Diag(NewLoc, diag::ext_explicit_instantiation_after_specialization) |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 3779 | << PrevDecl; |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 3780 | Diag(PrevDecl->getLocation(), |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 3781 | diag::note_previous_template_specialization); |
| 3782 | } |
| 3783 | SuppressNew = true; |
| 3784 | return false; |
| 3785 | |
| 3786 | case TSK_ExplicitInstantiationDeclaration: |
| 3787 | // We're explicity instantiating a definition for something for which we |
| 3788 | // were previously asked to suppress instantiations. That's fine. |
| 3789 | return false; |
| 3790 | |
| 3791 | case TSK_ExplicitInstantiationDefinition: |
| 3792 | // C++0x [temp.spec]p5: |
| 3793 | // For a given template and a given set of template-arguments, |
| 3794 | // - an explicit instantiation definition shall appear at most once |
| 3795 | // in a program, |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 3796 | Diag(NewLoc, diag::err_explicit_instantiation_duplicate) |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 3797 | << PrevDecl; |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 3798 | Diag(PrevPointOfInstantiation, |
| 3799 | diag::note_previous_explicit_instantiation); |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 3800 | SuppressNew = true; |
| 3801 | return false; |
| 3802 | } |
| 3803 | break; |
| 3804 | } |
| 3805 | |
| 3806 | assert(false && "Missing specialization/instantiation case?"); |
| 3807 | |
| 3808 | return false; |
| 3809 | } |
| 3810 | |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 3811 | /// \brief Perform semantic analysis for the given function template |
| 3812 | /// specialization. |
| 3813 | /// |
| 3814 | /// This routine performs all of the semantic analysis required for an |
| 3815 | /// explicit function template specialization. On successful completion, |
| 3816 | /// the function declaration \p FD will become a function template |
| 3817 | /// specialization. |
| 3818 | /// |
| 3819 | /// \param FD the function declaration, which will be updated to become a |
| 3820 | /// function template specialization. |
| 3821 | /// |
| 3822 | /// \param HasExplicitTemplateArgs whether any template arguments were |
| 3823 | /// explicitly provided. |
| 3824 | /// |
| 3825 | /// \param LAngleLoc the location of the left angle bracket ('<'), if |
| 3826 | /// template arguments were explicitly provided. |
| 3827 | /// |
| 3828 | /// \param ExplicitTemplateArgs the explicitly-provided template arguments, |
| 3829 | /// if any. |
| 3830 | /// |
| 3831 | /// \param NumExplicitTemplateArgs the number of explicitly-provided template |
| 3832 | /// arguments. This number may be zero even when HasExplicitTemplateArgs is |
| 3833 | /// true as in, e.g., \c void sort<>(char*, char*); |
| 3834 | /// |
| 3835 | /// \param RAngleLoc the location of the right angle bracket ('>'), if |
| 3836 | /// template arguments were explicitly provided. |
| 3837 | /// |
| 3838 | /// \param PrevDecl the set of declarations that |
| 3839 | bool |
| 3840 | Sema::CheckFunctionTemplateSpecialization(FunctionDecl *FD, |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 3841 | const TemplateArgumentListInfo *ExplicitTemplateArgs, |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 3842 | LookupResult &Previous) { |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 3843 | // The set of function template specializations that could match this |
| 3844 | // explicit function template specialization. |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 3845 | UnresolvedSet<8> Candidates; |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 3846 | |
| 3847 | DeclContext *FDLookupContext = FD->getDeclContext()->getLookupContext(); |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 3848 | for (LookupResult::iterator I = Previous.begin(), E = Previous.end(); |
| 3849 | I != E; ++I) { |
| 3850 | NamedDecl *Ovl = (*I)->getUnderlyingDecl(); |
| 3851 | if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Ovl)) { |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 3852 | // Only consider templates found within the same semantic lookup scope as |
| 3853 | // FD. |
| 3854 | if (!FDLookupContext->Equals(Ovl->getDeclContext()->getLookupContext())) |
| 3855 | continue; |
| 3856 | |
| 3857 | // C++ [temp.expl.spec]p11: |
| 3858 | // A trailing template-argument can be left unspecified in the |
| 3859 | // template-id naming an explicit function template specialization |
| 3860 | // provided it can be deduced from the function argument type. |
| 3861 | // Perform template argument deduction to determine whether we may be |
| 3862 | // specializing this template. |
| 3863 | // FIXME: It is somewhat wasteful to build |
| 3864 | TemplateDeductionInfo Info(Context); |
| 3865 | FunctionDecl *Specialization = 0; |
| 3866 | if (TemplateDeductionResult TDK |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 3867 | = DeduceTemplateArguments(FunTmpl, ExplicitTemplateArgs, |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 3868 | FD->getType(), |
| 3869 | Specialization, |
| 3870 | Info)) { |
| 3871 | // FIXME: Template argument deduction failed; record why it failed, so |
| 3872 | // that we can provide nifty diagnostics. |
| 3873 | (void)TDK; |
| 3874 | continue; |
| 3875 | } |
| 3876 | |
| 3877 | // Record this candidate. |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 3878 | Candidates.addDecl(Specialization, I.getAccess()); |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 3879 | } |
| 3880 | } |
| 3881 | |
Douglas Gregor | c5df30f | 2009-09-26 03:41:46 +0000 | [diff] [blame] | 3882 | // Find the most specialized function template. |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 3883 | UnresolvedSetIterator Result |
| 3884 | = getMostSpecialized(Candidates.begin(), Candidates.end(), |
| 3885 | TPOC_Other, FD->getLocation(), |
Douglas Gregor | c5df30f | 2009-09-26 03:41:46 +0000 | [diff] [blame] | 3886 | PartialDiagnostic(diag::err_function_template_spec_no_match) |
| 3887 | << FD->getDeclName(), |
| 3888 | PartialDiagnostic(diag::err_function_template_spec_ambiguous) |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 3889 | << FD->getDeclName() << (ExplicitTemplateArgs != 0), |
Douglas Gregor | c5df30f | 2009-09-26 03:41:46 +0000 | [diff] [blame] | 3890 | PartialDiagnostic(diag::note_function_template_spec_matched)); |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 3891 | if (Result == Candidates.end()) |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 3892 | return true; |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 3893 | |
| 3894 | // Ignore access information; it doesn't figure into redeclaration checking. |
| 3895 | FunctionDecl *Specialization = cast<FunctionDecl>(*Result); |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 3896 | |
| 3897 | // FIXME: Check if the prior specialization has a point of instantiation. |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 3898 | // If so, we have run afoul of . |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 3899 | |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3900 | // Check the scope of this explicit specialization. |
| 3901 | if (CheckTemplateSpecializationScope(*this, |
| 3902 | Specialization->getPrimaryTemplate(), |
| 3903 | Specialization, FD->getLocation(), |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 3904 | false)) |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3905 | return true; |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 3906 | |
| 3907 | // C++ [temp.expl.spec]p6: |
| 3908 | // 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] | 3909 | // explicitly specialized then that specialization shall be declared |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 3910 | // before the first use of that specialization that would cause an implicit |
| 3911 | // instantiation to take place, in every translation unit in which such a |
| 3912 | // use occurs; no diagnostic is required. |
| 3913 | FunctionTemplateSpecializationInfo *SpecInfo |
| 3914 | = Specialization->getTemplateSpecializationInfo(); |
| 3915 | assert(SpecInfo && "Function template specialization info missing?"); |
| 3916 | if (SpecInfo->getPointOfInstantiation().isValid()) { |
| 3917 | Diag(FD->getLocation(), diag::err_specialization_after_instantiation) |
| 3918 | << FD; |
| 3919 | Diag(SpecInfo->getPointOfInstantiation(), |
| 3920 | diag::note_instantiation_required_here) |
| 3921 | << (Specialization->getTemplateSpecializationKind() |
| 3922 | != TSK_ImplicitInstantiation); |
| 3923 | return true; |
| 3924 | } |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3925 | |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 3926 | // Mark the prior declaration as an explicit specialization, so that later |
| 3927 | // clients know that this is an explicit specialization. |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 3928 | SpecInfo->setTemplateSpecializationKind(TSK_ExplicitSpecialization); |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 3929 | |
| 3930 | // Turn the given function declaration into a function template |
| 3931 | // specialization, with the template arguments from the previous |
| 3932 | // specialization. |
| 3933 | FD->setFunctionTemplateSpecialization(Context, |
| 3934 | Specialization->getPrimaryTemplate(), |
| 3935 | new (Context) TemplateArgumentList( |
| 3936 | *Specialization->getTemplateSpecializationArgs()), |
| 3937 | /*InsertPos=*/0, |
| 3938 | TSK_ExplicitSpecialization); |
| 3939 | |
| 3940 | // The "previous declaration" for this function template specialization is |
| 3941 | // the prior function template specialization. |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 3942 | Previous.clear(); |
| 3943 | Previous.addDecl(Specialization); |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 3944 | return false; |
| 3945 | } |
| 3946 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 3947 | /// \brief Perform semantic analysis for the given non-template member |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 3948 | /// specialization. |
| 3949 | /// |
| 3950 | /// This routine performs all of the semantic analysis required for an |
| 3951 | /// explicit member function specialization. On successful completion, |
| 3952 | /// the function declaration \p FD will become a member function |
| 3953 | /// specialization. |
| 3954 | /// |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 3955 | /// \param Member the member declaration, which will be updated to become a |
| 3956 | /// specialization. |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 3957 | /// |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 3958 | /// \param Previous the set of declarations, one of which may be specialized |
| 3959 | /// by this function specialization; the set will be modified to contain the |
| 3960 | /// redeclared member. |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 3961 | bool |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 3962 | Sema::CheckMemberSpecialization(NamedDecl *Member, LookupResult &Previous) { |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 3963 | assert(!isa<TemplateDecl>(Member) && "Only for non-template members"); |
| 3964 | |
| 3965 | // Try to find the member we are instantiating. |
| 3966 | NamedDecl *Instantiation = 0; |
| 3967 | NamedDecl *InstantiatedFrom = 0; |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 3968 | MemberSpecializationInfo *MSInfo = 0; |
| 3969 | |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 3970 | if (Previous.empty()) { |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 3971 | // Nowhere to look anyway. |
| 3972 | } else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Member)) { |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 3973 | for (LookupResult::iterator I = Previous.begin(), E = Previous.end(); |
| 3974 | I != E; ++I) { |
| 3975 | NamedDecl *D = (*I)->getUnderlyingDecl(); |
| 3976 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) { |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 3977 | if (Context.hasSameType(Function->getType(), Method->getType())) { |
| 3978 | Instantiation = Method; |
| 3979 | InstantiatedFrom = Method->getInstantiatedFromMemberFunction(); |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 3980 | MSInfo = Method->getMemberSpecializationInfo(); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 3981 | break; |
| 3982 | } |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 3983 | } |
| 3984 | } |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 3985 | } else if (isa<VarDecl>(Member)) { |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 3986 | VarDecl *PrevVar; |
| 3987 | if (Previous.isSingleResult() && |
| 3988 | (PrevVar = dyn_cast<VarDecl>(Previous.getFoundDecl()))) |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 3989 | if (PrevVar->isStaticDataMember()) { |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 3990 | Instantiation = PrevVar; |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 3991 | InstantiatedFrom = PrevVar->getInstantiatedFromStaticDataMember(); |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 3992 | MSInfo = PrevVar->getMemberSpecializationInfo(); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 3993 | } |
| 3994 | } else if (isa<RecordDecl>(Member)) { |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 3995 | CXXRecordDecl *PrevRecord; |
| 3996 | if (Previous.isSingleResult() && |
| 3997 | (PrevRecord = dyn_cast<CXXRecordDecl>(Previous.getFoundDecl()))) { |
| 3998 | Instantiation = PrevRecord; |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 3999 | InstantiatedFrom = PrevRecord->getInstantiatedFromMemberClass(); |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 4000 | MSInfo = PrevRecord->getMemberSpecializationInfo(); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4001 | } |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 4002 | } |
| 4003 | |
| 4004 | if (!Instantiation) { |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4005 | // There is no previous declaration that matches. Since member |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 4006 | // specializations are always out-of-line, the caller will complain about |
| 4007 | // this mismatch later. |
| 4008 | return false; |
| 4009 | } |
| 4010 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4011 | // Make sure that this is a specialization of a member. |
| 4012 | if (!InstantiatedFrom) { |
| 4013 | Diag(Member->getLocation(), diag::err_spec_member_not_instantiated) |
| 4014 | << Member; |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 4015 | Diag(Instantiation->getLocation(), diag::note_specialized_decl); |
| 4016 | return true; |
| 4017 | } |
| 4018 | |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 4019 | // C++ [temp.expl.spec]p6: |
| 4020 | // If a template, a member template or the member of a class template is |
| 4021 | // explicitly specialized then that spe- cialization shall be declared |
| 4022 | // before the first use of that specialization that would cause an implicit |
| 4023 | // instantiation to take place, in every translation unit in which such a |
| 4024 | // use occurs; no diagnostic is required. |
| 4025 | assert(MSInfo && "Member specialization info missing?"); |
| 4026 | if (MSInfo->getPointOfInstantiation().isValid()) { |
| 4027 | Diag(Member->getLocation(), diag::err_specialization_after_instantiation) |
| 4028 | << Member; |
| 4029 | Diag(MSInfo->getPointOfInstantiation(), |
| 4030 | diag::note_instantiation_required_here) |
| 4031 | << (MSInfo->getTemplateSpecializationKind() != TSK_ImplicitInstantiation); |
| 4032 | return true; |
| 4033 | } |
| 4034 | |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 4035 | // Check the scope of this explicit specialization. |
| 4036 | if (CheckTemplateSpecializationScope(*this, |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4037 | InstantiatedFrom, |
| 4038 | Instantiation, Member->getLocation(), |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4039 | false)) |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 4040 | return true; |
Douglas Gregor | 2db3232 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 4041 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4042 | // Note that this is an explicit instantiation of a member. |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 4043 | // the original declaration to note that it is an explicit specialization |
| 4044 | // (if it was previously an implicit instantiation). This latter step |
| 4045 | // makes bookkeeping easier. |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4046 | if (isa<FunctionDecl>(Member)) { |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 4047 | FunctionDecl *InstantiationFunction = cast<FunctionDecl>(Instantiation); |
| 4048 | if (InstantiationFunction->getTemplateSpecializationKind() == |
| 4049 | TSK_ImplicitInstantiation) { |
| 4050 | InstantiationFunction->setTemplateSpecializationKind( |
| 4051 | TSK_ExplicitSpecialization); |
| 4052 | InstantiationFunction->setLocation(Member->getLocation()); |
| 4053 | } |
| 4054 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4055 | cast<FunctionDecl>(Member)->setInstantiationOfMemberFunction( |
| 4056 | cast<CXXMethodDecl>(InstantiatedFrom), |
| 4057 | TSK_ExplicitSpecialization); |
| 4058 | } else if (isa<VarDecl>(Member)) { |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 4059 | VarDecl *InstantiationVar = cast<VarDecl>(Instantiation); |
| 4060 | if (InstantiationVar->getTemplateSpecializationKind() == |
| 4061 | TSK_ImplicitInstantiation) { |
| 4062 | InstantiationVar->setTemplateSpecializationKind( |
| 4063 | TSK_ExplicitSpecialization); |
| 4064 | InstantiationVar->setLocation(Member->getLocation()); |
| 4065 | } |
| 4066 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4067 | Context.setInstantiatedFromStaticDataMember(cast<VarDecl>(Member), |
| 4068 | cast<VarDecl>(InstantiatedFrom), |
| 4069 | TSK_ExplicitSpecialization); |
| 4070 | } else { |
| 4071 | assert(isa<CXXRecordDecl>(Member) && "Only member classes remain"); |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 4072 | CXXRecordDecl *InstantiationClass = cast<CXXRecordDecl>(Instantiation); |
| 4073 | if (InstantiationClass->getTemplateSpecializationKind() == |
| 4074 | TSK_ImplicitInstantiation) { |
| 4075 | InstantiationClass->setTemplateSpecializationKind( |
| 4076 | TSK_ExplicitSpecialization); |
| 4077 | InstantiationClass->setLocation(Member->getLocation()); |
| 4078 | } |
| 4079 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4080 | cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass( |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 4081 | cast<CXXRecordDecl>(InstantiatedFrom), |
| 4082 | TSK_ExplicitSpecialization); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4083 | } |
| 4084 | |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 4085 | // Save the caller the trouble of having to figure out which declaration |
| 4086 | // this specialization matches. |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 4087 | Previous.clear(); |
| 4088 | Previous.addDecl(Instantiation); |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 4089 | return false; |
| 4090 | } |
| 4091 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 4092 | /// \brief Check the scope of an explicit instantiation. |
| 4093 | static void CheckExplicitInstantiationScope(Sema &S, NamedDecl *D, |
| 4094 | SourceLocation InstLoc, |
| 4095 | bool WasQualifiedName) { |
| 4096 | DeclContext *ExpectedContext |
| 4097 | = D->getDeclContext()->getEnclosingNamespaceContext()->getLookupContext(); |
| 4098 | DeclContext *CurContext = S.CurContext->getLookupContext(); |
| 4099 | |
| 4100 | // C++0x [temp.explicit]p2: |
| 4101 | // An explicit instantiation shall appear in an enclosing namespace of its |
| 4102 | // template. |
| 4103 | // |
| 4104 | // This is DR275, which we do not retroactively apply to C++98/03. |
| 4105 | if (S.getLangOptions().CPlusPlus0x && |
| 4106 | !CurContext->Encloses(ExpectedContext)) { |
| 4107 | if (NamespaceDecl *NS = dyn_cast<NamespaceDecl>(ExpectedContext)) |
| 4108 | S.Diag(InstLoc, diag::err_explicit_instantiation_out_of_scope) |
| 4109 | << D << NS; |
| 4110 | else |
| 4111 | S.Diag(InstLoc, diag::err_explicit_instantiation_must_be_global) |
| 4112 | << D; |
| 4113 | S.Diag(D->getLocation(), diag::note_explicit_instantiation_here); |
| 4114 | return; |
| 4115 | } |
| 4116 | |
| 4117 | // C++0x [temp.explicit]p2: |
| 4118 | // If the name declared in the explicit instantiation is an unqualified |
| 4119 | // name, the explicit instantiation shall appear in the namespace where |
| 4120 | // its template is declared or, if that namespace is inline (7.3.1), any |
| 4121 | // namespace from its enclosing namespace set. |
| 4122 | if (WasQualifiedName) |
| 4123 | return; |
| 4124 | |
| 4125 | if (CurContext->Equals(ExpectedContext)) |
| 4126 | return; |
| 4127 | |
| 4128 | S.Diag(InstLoc, diag::err_explicit_instantiation_unqualified_wrong_namespace) |
| 4129 | << D << ExpectedContext; |
| 4130 | S.Diag(D->getLocation(), diag::note_explicit_instantiation_here); |
| 4131 | } |
| 4132 | |
| 4133 | /// \brief Determine whether the given scope specifier has a template-id in it. |
| 4134 | static bool ScopeSpecifierHasTemplateId(const CXXScopeSpec &SS) { |
| 4135 | if (!SS.isSet()) |
| 4136 | return false; |
| 4137 | |
| 4138 | // C++0x [temp.explicit]p2: |
| 4139 | // If the explicit instantiation is for a member function, a member class |
| 4140 | // or a static data member of a class template specialization, the name of |
| 4141 | // the class template specialization in the qualified-id for the member |
| 4142 | // name shall be a simple-template-id. |
| 4143 | // |
| 4144 | // C++98 has the same restriction, just worded differently. |
| 4145 | for (NestedNameSpecifier *NNS = (NestedNameSpecifier *)SS.getScopeRep(); |
| 4146 | NNS; NNS = NNS->getPrefix()) |
| 4147 | if (Type *T = NNS->getAsType()) |
| 4148 | if (isa<TemplateSpecializationType>(T)) |
| 4149 | return true; |
| 4150 | |
| 4151 | return false; |
| 4152 | } |
| 4153 | |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 4154 | // Explicit instantiation of a class template specialization |
Douglas Gregor | 45f9655 | 2009-09-04 06:33:52 +0000 | [diff] [blame] | 4155 | // FIXME: Implement extern template semantics |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4156 | Sema::DeclResult |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4157 | Sema::ActOnExplicitInstantiation(Scope *S, |
Douglas Gregor | 45f9655 | 2009-09-04 06:33:52 +0000 | [diff] [blame] | 4158 | SourceLocation ExternLoc, |
| 4159 | SourceLocation TemplateLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4160 | unsigned TagSpec, |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4161 | SourceLocation KWLoc, |
| 4162 | const CXXScopeSpec &SS, |
| 4163 | TemplateTy TemplateD, |
| 4164 | SourceLocation TemplateNameLoc, |
| 4165 | SourceLocation LAngleLoc, |
| 4166 | ASTTemplateArgsPtr TemplateArgsIn, |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4167 | SourceLocation RAngleLoc, |
| 4168 | AttributeList *Attr) { |
| 4169 | // Find the class template we're specializing |
| 4170 | TemplateName Name = TemplateD.getAsVal<TemplateName>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4171 | ClassTemplateDecl *ClassTemplate |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4172 | = cast<ClassTemplateDecl>(Name.getAsTemplateDecl()); |
| 4173 | |
| 4174 | // Check that the specialization uses the same tag kind as the |
| 4175 | // original template. |
| 4176 | TagDecl::TagKind Kind; |
| 4177 | switch (TagSpec) { |
| 4178 | default: assert(0 && "Unknown tag type!"); |
| 4179 | case DeclSpec::TST_struct: Kind = TagDecl::TK_struct; break; |
| 4180 | case DeclSpec::TST_union: Kind = TagDecl::TK_union; break; |
| 4181 | case DeclSpec::TST_class: Kind = TagDecl::TK_class; break; |
| 4182 | } |
Douglas Gregor | 501c5ce | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 4183 | if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4184 | Kind, KWLoc, |
Douglas Gregor | 501c5ce | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 4185 | *ClassTemplate->getIdentifier())) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4186 | Diag(KWLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4187 | << ClassTemplate |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4188 | << CodeModificationHint::CreateReplacement(KWLoc, |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4189 | ClassTemplate->getTemplatedDecl()->getKindName()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4190 | Diag(ClassTemplate->getTemplatedDecl()->getLocation(), |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4191 | diag::note_previous_use); |
| 4192 | Kind = ClassTemplate->getTemplatedDecl()->getTagKind(); |
| 4193 | } |
| 4194 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 4195 | // C++0x [temp.explicit]p2: |
| 4196 | // There are two forms of explicit instantiation: an explicit instantiation |
| 4197 | // definition and an explicit instantiation declaration. An explicit |
| 4198 | // instantiation declaration begins with the extern keyword. [...] |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4199 | TemplateSpecializationKind TSK |
| 4200 | = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition |
| 4201 | : TSK_ExplicitInstantiationDeclaration; |
| 4202 | |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4203 | // Translate the parser's template argument list in our AST format. |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 4204 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
Douglas Gregor | 314b97f | 2009-11-10 19:49:08 +0000 | [diff] [blame] | 4205 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4206 | |
| 4207 | // Check that the template argument list is well-formed for this |
| 4208 | // template. |
Anders Carlsson | fb25052 | 2009-06-23 01:26:57 +0000 | [diff] [blame] | 4209 | TemplateArgumentListBuilder Converted(ClassTemplate->getTemplateParameters(), |
| 4210 | TemplateArgs.size()); |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 4211 | if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc, |
| 4212 | TemplateArgs, false, Converted)) |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4213 | return true; |
| 4214 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4215 | assert((Converted.structuredSize() == |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4216 | ClassTemplate->getTemplateParameters()->size()) && |
| 4217 | "Converted template argument list is too short!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4218 | |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4219 | // Find the class template specialization declaration that |
| 4220 | // corresponds to these arguments. |
| 4221 | llvm::FoldingSetNodeID ID; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4222 | ClassTemplateSpecializationDecl::Profile(ID, |
Anders Carlsson | fb25052 | 2009-06-23 01:26:57 +0000 | [diff] [blame] | 4223 | Converted.getFlatArguments(), |
Douglas Gregor | 828e226 | 2009-07-29 16:09:57 +0000 | [diff] [blame] | 4224 | Converted.flatSize(), |
| 4225 | Context); |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4226 | void *InsertPos = 0; |
| 4227 | ClassTemplateSpecializationDecl *PrevDecl |
| 4228 | = ClassTemplate->getSpecializations().FindNodeOrInsertPos(ID, InsertPos); |
| 4229 | |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4230 | // C++0x [temp.explicit]p2: |
| 4231 | // [...] An explicit instantiation shall appear in an enclosing |
| 4232 | // namespace of its template. [...] |
| 4233 | // |
| 4234 | // This is C++ DR 275. |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 4235 | CheckExplicitInstantiationScope(*this, ClassTemplate, TemplateNameLoc, |
| 4236 | SS.isSet()); |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4237 | |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4238 | ClassTemplateSpecializationDecl *Specialization = 0; |
| 4239 | |
Douglas Gregor | d78f598 | 2009-11-25 06:01:46 +0000 | [diff] [blame] | 4240 | bool ReusedDecl = false; |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4241 | if (PrevDecl) { |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 4242 | bool SuppressNew = false; |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 4243 | if (CheckSpecializationInstantiationRedecl(TemplateNameLoc, TSK, |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 4244 | PrevDecl, |
| 4245 | PrevDecl->getSpecializationKind(), |
| 4246 | PrevDecl->getPointOfInstantiation(), |
| 4247 | SuppressNew)) |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4248 | return DeclPtrTy::make(PrevDecl); |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4249 | |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 4250 | if (SuppressNew) |
Douglas Gregor | 52604ab | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 4251 | return DeclPtrTy::make(PrevDecl); |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 4252 | |
Douglas Gregor | 52604ab | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 4253 | if (PrevDecl->getSpecializationKind() == TSK_ImplicitInstantiation || |
| 4254 | PrevDecl->getSpecializationKind() == TSK_Undeclared) { |
| 4255 | // Since the only prior class template specialization with these |
| 4256 | // arguments was referenced but not declared, reuse that |
| 4257 | // declaration node as our own, updating its source location to |
| 4258 | // reflect our new declaration. |
| 4259 | Specialization = PrevDecl; |
| 4260 | Specialization->setLocation(TemplateNameLoc); |
| 4261 | PrevDecl = 0; |
Douglas Gregor | d78f598 | 2009-11-25 06:01:46 +0000 | [diff] [blame] | 4262 | ReusedDecl = true; |
Douglas Gregor | 52604ab | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 4263 | } |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 4264 | } |
Douglas Gregor | 52604ab | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 4265 | |
| 4266 | if (!Specialization) { |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4267 | // Create a new class template specialization declaration node for |
| 4268 | // this explicit specialization. |
| 4269 | Specialization |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4270 | = ClassTemplateSpecializationDecl::Create(Context, |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4271 | ClassTemplate->getDeclContext(), |
| 4272 | TemplateNameLoc, |
| 4273 | ClassTemplate, |
Douglas Gregor | 52604ab | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 4274 | Converted, PrevDecl); |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4275 | |
Douglas Gregor | 52604ab | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 4276 | if (PrevDecl) { |
| 4277 | // Remove the previous declaration from the folding set, since we want |
| 4278 | // to introduce a new declaration. |
| 4279 | ClassTemplate->getSpecializations().RemoveNode(PrevDecl); |
| 4280 | ClassTemplate->getSpecializations().FindNodeOrInsertPos(ID, InsertPos); |
| 4281 | } |
| 4282 | |
| 4283 | // Insert the new specialization. |
| 4284 | ClassTemplate->getSpecializations().InsertNode(Specialization, InsertPos); |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4285 | } |
| 4286 | |
| 4287 | // Build the fully-sugared type for this explicit instantiation as |
| 4288 | // the user wrote in the explicit instantiation itself. This means |
| 4289 | // that we'll pretty-print the type retrieved from the |
| 4290 | // specialization's declaration the way that the user actually wrote |
| 4291 | // the explicit instantiation, rather than formatting the name based |
| 4292 | // on the "canonical" representation used to store the template |
| 4293 | // arguments in the specialization. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4294 | QualType WrittenTy |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 4295 | = Context.getTemplateSpecializationType(Name, TemplateArgs, |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4296 | Context.getTypeDeclType(Specialization)); |
| 4297 | Specialization->setTypeAsWritten(WrittenTy); |
| 4298 | TemplateArgsIn.release(); |
| 4299 | |
Douglas Gregor | d78f598 | 2009-11-25 06:01:46 +0000 | [diff] [blame] | 4300 | if (!ReusedDecl) { |
| 4301 | // Add the explicit instantiation into its lexical context. However, |
| 4302 | // since explicit instantiations are never found by name lookup, we |
| 4303 | // just put it into the declaration context directly. |
| 4304 | Specialization->setLexicalDeclContext(CurContext); |
| 4305 | CurContext->addDecl(Specialization); |
| 4306 | } |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4307 | |
| 4308 | // C++ [temp.explicit]p3: |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4309 | // A definition of a class template or class member template |
| 4310 | // shall be in scope at the point of the explicit instantiation of |
| 4311 | // the class template or class member template. |
| 4312 | // |
| 4313 | // This check comes when we actually try to perform the |
| 4314 | // instantiation. |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 4315 | ClassTemplateSpecializationDecl *Def |
| 4316 | = cast_or_null<ClassTemplateSpecializationDecl>( |
| 4317 | Specialization->getDefinition(Context)); |
| 4318 | if (!Def) |
Douglas Gregor | 972e6ce | 2009-10-27 06:26:26 +0000 | [diff] [blame] | 4319 | InstantiateClassTemplateSpecialization(TemplateNameLoc, Specialization, TSK); |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 4320 | |
| 4321 | // Instantiate the members of this class template specialization. |
| 4322 | Def = cast_or_null<ClassTemplateSpecializationDecl>( |
| 4323 | Specialization->getDefinition(Context)); |
| 4324 | if (Def) |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 4325 | InstantiateClassTemplateSpecializationMembers(TemplateNameLoc, Def, TSK); |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4326 | |
| 4327 | return DeclPtrTy::make(Specialization); |
| 4328 | } |
| 4329 | |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 4330 | // Explicit instantiation of a member class of a class template. |
| 4331 | Sema::DeclResult |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4332 | Sema::ActOnExplicitInstantiation(Scope *S, |
Douglas Gregor | 45f9655 | 2009-09-04 06:33:52 +0000 | [diff] [blame] | 4333 | SourceLocation ExternLoc, |
| 4334 | SourceLocation TemplateLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4335 | unsigned TagSpec, |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 4336 | SourceLocation KWLoc, |
| 4337 | const CXXScopeSpec &SS, |
| 4338 | IdentifierInfo *Name, |
| 4339 | SourceLocation NameLoc, |
| 4340 | AttributeList *Attr) { |
| 4341 | |
Douglas Gregor | 402abb5 | 2009-05-28 23:31:59 +0000 | [diff] [blame] | 4342 | bool Owned = false; |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 4343 | bool IsDependent = false; |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 4344 | DeclPtrTy TagD = ActOnTag(S, TagSpec, Action::TUK_Reference, |
Douglas Gregor | 7cdbc58 | 2009-07-22 23:48:44 +0000 | [diff] [blame] | 4345 | KWLoc, SS, Name, NameLoc, Attr, AS_none, |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 4346 | MultiTemplateParamsArg(*this, 0, 0), |
| 4347 | Owned, IsDependent); |
| 4348 | assert(!IsDependent && "explicit instantiation of dependent name not yet handled"); |
| 4349 | |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 4350 | if (!TagD) |
| 4351 | return true; |
| 4352 | |
| 4353 | TagDecl *Tag = cast<TagDecl>(TagD.getAs<Decl>()); |
| 4354 | if (Tag->isEnum()) { |
| 4355 | Diag(TemplateLoc, diag::err_explicit_instantiation_enum) |
| 4356 | << Context.getTypeDeclType(Tag); |
| 4357 | return true; |
| 4358 | } |
| 4359 | |
Douglas Gregor | d0c8737 | 2009-05-27 17:30:49 +0000 | [diff] [blame] | 4360 | if (Tag->isInvalidDecl()) |
| 4361 | return true; |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 4362 | |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 4363 | CXXRecordDecl *Record = cast<CXXRecordDecl>(Tag); |
| 4364 | CXXRecordDecl *Pattern = Record->getInstantiatedFromMemberClass(); |
| 4365 | if (!Pattern) { |
| 4366 | Diag(TemplateLoc, diag::err_explicit_instantiation_nontemplate_type) |
| 4367 | << Context.getTypeDeclType(Record); |
| 4368 | Diag(Record->getLocation(), diag::note_nontemplate_decl_here); |
| 4369 | return true; |
| 4370 | } |
| 4371 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 4372 | // C++0x [temp.explicit]p2: |
| 4373 | // If the explicit instantiation is for a class or member class, the |
| 4374 | // elaborated-type-specifier in the declaration shall include a |
| 4375 | // simple-template-id. |
| 4376 | // |
| 4377 | // C++98 has the same restriction, just worded differently. |
| 4378 | if (!ScopeSpecifierHasTemplateId(SS)) |
| 4379 | Diag(TemplateLoc, diag::err_explicit_instantiation_without_qualified_id) |
| 4380 | << Record << SS.getRange(); |
| 4381 | |
| 4382 | // C++0x [temp.explicit]p2: |
| 4383 | // There are two forms of explicit instantiation: an explicit instantiation |
| 4384 | // definition and an explicit instantiation declaration. An explicit |
| 4385 | // instantiation declaration begins with the extern keyword. [...] |
Douglas Gregor | a74bbe2 | 2009-10-14 21:46:58 +0000 | [diff] [blame] | 4386 | TemplateSpecializationKind TSK |
| 4387 | = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition |
| 4388 | : TSK_ExplicitInstantiationDeclaration; |
| 4389 | |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 4390 | // C++0x [temp.explicit]p2: |
| 4391 | // [...] An explicit instantiation shall appear in an enclosing |
| 4392 | // namespace of its template. [...] |
| 4393 | // |
| 4394 | // This is C++ DR 275. |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 4395 | CheckExplicitInstantiationScope(*this, Record, NameLoc, true); |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4396 | |
| 4397 | // Verify that it is okay to explicitly instantiate here. |
Douglas Gregor | 583f33b | 2009-10-15 18:07:02 +0000 | [diff] [blame] | 4398 | CXXRecordDecl *PrevDecl |
| 4399 | = cast_or_null<CXXRecordDecl>(Record->getPreviousDeclaration()); |
| 4400 | if (!PrevDecl && Record->getDefinition(Context)) |
| 4401 | PrevDecl = Record; |
| 4402 | if (PrevDecl) { |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4403 | MemberSpecializationInfo *MSInfo = PrevDecl->getMemberSpecializationInfo(); |
| 4404 | bool SuppressNew = false; |
| 4405 | assert(MSInfo && "No member specialization information?"); |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 4406 | if (CheckSpecializationInstantiationRedecl(TemplateLoc, TSK, |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4407 | PrevDecl, |
| 4408 | MSInfo->getTemplateSpecializationKind(), |
| 4409 | MSInfo->getPointOfInstantiation(), |
| 4410 | SuppressNew)) |
| 4411 | return true; |
| 4412 | if (SuppressNew) |
| 4413 | return TagD; |
| 4414 | } |
| 4415 | |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 4416 | CXXRecordDecl *RecordDef |
| 4417 | = cast_or_null<CXXRecordDecl>(Record->getDefinition(Context)); |
| 4418 | if (!RecordDef) { |
Douglas Gregor | bf7643e | 2009-10-15 12:53:22 +0000 | [diff] [blame] | 4419 | // C++ [temp.explicit]p3: |
| 4420 | // A definition of a member class of a class template shall be in scope |
| 4421 | // at the point of an explicit instantiation of the member class. |
| 4422 | CXXRecordDecl *Def |
| 4423 | = cast_or_null<CXXRecordDecl>(Pattern->getDefinition(Context)); |
| 4424 | if (!Def) { |
Douglas Gregor | e2d3a3d | 2009-10-15 14:05:49 +0000 | [diff] [blame] | 4425 | Diag(TemplateLoc, diag::err_explicit_instantiation_undefined_member) |
| 4426 | << 0 << Record->getDeclName() << Record->getDeclContext(); |
Douglas Gregor | bf7643e | 2009-10-15 12:53:22 +0000 | [diff] [blame] | 4427 | Diag(Pattern->getLocation(), diag::note_forward_declaration) |
| 4428 | << Pattern; |
| 4429 | return true; |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 4430 | } else { |
| 4431 | if (InstantiateClass(NameLoc, Record, Def, |
| 4432 | getTemplateInstantiationArgs(Record), |
| 4433 | TSK)) |
| 4434 | return true; |
| 4435 | |
| 4436 | RecordDef = cast_or_null<CXXRecordDecl>(Record->getDefinition(Context)); |
| 4437 | if (!RecordDef) |
| 4438 | return true; |
| 4439 | } |
| 4440 | } |
| 4441 | |
| 4442 | // Instantiate all of the members of the class. |
| 4443 | InstantiateClassMembers(NameLoc, RecordDef, |
| 4444 | getTemplateInstantiationArgs(Record), TSK); |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 4445 | |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 4446 | // FIXME: We don't have any representation for explicit instantiations of |
| 4447 | // member classes. Such a representation is not needed for compilation, but it |
| 4448 | // should be available for clients that want to see all of the declarations in |
| 4449 | // the source code. |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 4450 | return TagD; |
| 4451 | } |
| 4452 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4453 | Sema::DeclResult Sema::ActOnExplicitInstantiation(Scope *S, |
| 4454 | SourceLocation ExternLoc, |
| 4455 | SourceLocation TemplateLoc, |
| 4456 | Declarator &D) { |
| 4457 | // Explicit instantiations always require a name. |
| 4458 | DeclarationName Name = GetNameForDeclarator(D); |
| 4459 | if (!Name) { |
| 4460 | if (!D.isInvalidType()) |
| 4461 | Diag(D.getDeclSpec().getSourceRange().getBegin(), |
| 4462 | diag::err_explicit_instantiation_requires_name) |
| 4463 | << D.getDeclSpec().getSourceRange() |
| 4464 | << D.getSourceRange(); |
| 4465 | |
| 4466 | return true; |
| 4467 | } |
| 4468 | |
| 4469 | // The scope passed in may not be a decl scope. Zip up the scope tree until |
| 4470 | // we find one that is. |
| 4471 | while ((S->getFlags() & Scope::DeclScope) == 0 || |
| 4472 | (S->getFlags() & Scope::TemplateParamScope) != 0) |
| 4473 | S = S->getParent(); |
| 4474 | |
| 4475 | // Determine the type of the declaration. |
| 4476 | QualType R = GetTypeForDeclarator(D, S, 0); |
| 4477 | if (R.isNull()) |
| 4478 | return true; |
| 4479 | |
| 4480 | if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef) { |
| 4481 | // Cannot explicitly instantiate a typedef. |
| 4482 | Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_of_typedef) |
| 4483 | << Name; |
| 4484 | return true; |
| 4485 | } |
| 4486 | |
Douglas Gregor | 663b5a0 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 4487 | // C++0x [temp.explicit]p1: |
| 4488 | // [...] An explicit instantiation of a function template shall not use the |
| 4489 | // inline or constexpr specifiers. |
| 4490 | // Presumably, this also applies to member functions of class templates as |
| 4491 | // well. |
| 4492 | if (D.getDeclSpec().isInlineSpecified() && getLangOptions().CPlusPlus0x) |
| 4493 | Diag(D.getDeclSpec().getInlineSpecLoc(), |
| 4494 | diag::err_explicit_instantiation_inline) |
Chris Lattner | 29d9c1a | 2009-12-06 17:36:05 +0000 | [diff] [blame] | 4495 | <<CodeModificationHint::CreateRemoval(D.getDeclSpec().getInlineSpecLoc()); |
Douglas Gregor | 663b5a0 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 4496 | |
| 4497 | // FIXME: check for constexpr specifier. |
| 4498 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 4499 | // C++0x [temp.explicit]p2: |
| 4500 | // There are two forms of explicit instantiation: an explicit instantiation |
| 4501 | // definition and an explicit instantiation declaration. An explicit |
| 4502 | // instantiation declaration begins with the extern keyword. [...] |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4503 | TemplateSpecializationKind TSK |
| 4504 | = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition |
| 4505 | : TSK_ExplicitInstantiationDeclaration; |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 4506 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 4507 | LookupResult Previous(*this, Name, D.getIdentifierLoc(), LookupOrdinaryName); |
| 4508 | LookupParsedName(Previous, S, &D.getCXXScopeSpec()); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4509 | |
| 4510 | if (!R->isFunctionType()) { |
| 4511 | // C++ [temp.explicit]p1: |
| 4512 | // A [...] static data member of a class template can be explicitly |
| 4513 | // instantiated from the member definition associated with its class |
| 4514 | // template. |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 4515 | if (Previous.isAmbiguous()) |
| 4516 | return true; |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4517 | |
John McCall | 1bcee0a | 2009-12-02 08:25:40 +0000 | [diff] [blame] | 4518 | VarDecl *Prev = Previous.getAsSingle<VarDecl>(); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4519 | if (!Prev || !Prev->isStaticDataMember()) { |
| 4520 | // We expect to see a data data member here. |
| 4521 | Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_not_known) |
| 4522 | << Name; |
| 4523 | for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end(); |
| 4524 | P != PEnd; ++P) |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 4525 | Diag((*P)->getLocation(), diag::note_explicit_instantiation_here); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4526 | return true; |
| 4527 | } |
| 4528 | |
| 4529 | if (!Prev->getInstantiatedFromStaticDataMember()) { |
| 4530 | // FIXME: Check for explicit specialization? |
| 4531 | Diag(D.getIdentifierLoc(), |
| 4532 | diag::err_explicit_instantiation_data_member_not_instantiated) |
| 4533 | << Prev; |
| 4534 | Diag(Prev->getLocation(), diag::note_explicit_instantiation_here); |
| 4535 | // FIXME: Can we provide a note showing where this was declared? |
| 4536 | return true; |
| 4537 | } |
| 4538 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 4539 | // C++0x [temp.explicit]p2: |
| 4540 | // If the explicit instantiation is for a member function, a member class |
| 4541 | // or a static data member of a class template specialization, the name of |
| 4542 | // the class template specialization in the qualified-id for the member |
| 4543 | // name shall be a simple-template-id. |
| 4544 | // |
| 4545 | // C++98 has the same restriction, just worded differently. |
| 4546 | if (!ScopeSpecifierHasTemplateId(D.getCXXScopeSpec())) |
| 4547 | Diag(D.getIdentifierLoc(), |
| 4548 | diag::err_explicit_instantiation_without_qualified_id) |
| 4549 | << Prev << D.getCXXScopeSpec().getRange(); |
| 4550 | |
| 4551 | // Check the scope of this explicit instantiation. |
| 4552 | CheckExplicitInstantiationScope(*this, Prev, D.getIdentifierLoc(), true); |
| 4553 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4554 | // Verify that it is okay to explicitly instantiate here. |
| 4555 | MemberSpecializationInfo *MSInfo = Prev->getMemberSpecializationInfo(); |
| 4556 | assert(MSInfo && "Missing static data member specialization info?"); |
| 4557 | bool SuppressNew = false; |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 4558 | if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK, Prev, |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4559 | MSInfo->getTemplateSpecializationKind(), |
| 4560 | MSInfo->getPointOfInstantiation(), |
| 4561 | SuppressNew)) |
| 4562 | return true; |
| 4563 | if (SuppressNew) |
| 4564 | return DeclPtrTy(); |
| 4565 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4566 | // Instantiate static data member. |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 4567 | Prev->setTemplateSpecializationKind(TSK, D.getIdentifierLoc()); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4568 | if (TSK == TSK_ExplicitInstantiationDefinition) |
Douglas Gregor | e2d3a3d | 2009-10-15 14:05:49 +0000 | [diff] [blame] | 4569 | InstantiateStaticDataMemberDefinition(D.getIdentifierLoc(), Prev, false, |
| 4570 | /*DefinitionRequired=*/true); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4571 | |
| 4572 | // FIXME: Create an ExplicitInstantiation node? |
| 4573 | return DeclPtrTy(); |
| 4574 | } |
| 4575 | |
Douglas Gregor | 0b60d9e | 2009-09-25 23:53:26 +0000 | [diff] [blame] | 4576 | // If the declarator is a template-id, translate the parser's template |
| 4577 | // argument list into our AST format. |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 4578 | bool HasExplicitTemplateArgs = false; |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 4579 | TemplateArgumentListInfo TemplateArgs; |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 4580 | if (D.getName().getKind() == UnqualifiedId::IK_TemplateId) { |
| 4581 | TemplateIdAnnotation *TemplateId = D.getName().TemplateId; |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 4582 | TemplateArgs.setLAngleLoc(TemplateId->LAngleLoc); |
| 4583 | TemplateArgs.setRAngleLoc(TemplateId->RAngleLoc); |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 4584 | ASTTemplateArgsPtr TemplateArgsPtr(*this, |
| 4585 | TemplateId->getTemplateArgs(), |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 4586 | TemplateId->NumArgs); |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 4587 | translateTemplateArguments(TemplateArgsPtr, TemplateArgs); |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 4588 | HasExplicitTemplateArgs = true; |
Douglas Gregor | b2f81cf | 2009-10-01 23:51:25 +0000 | [diff] [blame] | 4589 | TemplateArgsPtr.release(); |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 4590 | } |
Douglas Gregor | 0b60d9e | 2009-09-25 23:53:26 +0000 | [diff] [blame] | 4591 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4592 | // C++ [temp.explicit]p1: |
| 4593 | // A [...] function [...] can be explicitly instantiated from its template. |
| 4594 | // A member function [...] of a class template can be explicitly |
| 4595 | // instantiated from the member definition associated with its class |
| 4596 | // template. |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4597 | UnresolvedSet<8> Matches; |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4598 | for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end(); |
| 4599 | P != PEnd; ++P) { |
| 4600 | NamedDecl *Prev = *P; |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 4601 | if (!HasExplicitTemplateArgs) { |
| 4602 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Prev)) { |
| 4603 | if (Context.hasSameUnqualifiedType(Method->getType(), R)) { |
| 4604 | Matches.clear(); |
Douglas Gregor | 48026d2 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 4605 | |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4606 | Matches.addDecl(Method, P.getAccess()); |
Douglas Gregor | 48026d2 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 4607 | if (Method->getTemplateSpecializationKind() == TSK_Undeclared) |
| 4608 | break; |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 4609 | } |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4610 | } |
| 4611 | } |
| 4612 | |
| 4613 | FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Prev); |
| 4614 | if (!FunTmpl) |
| 4615 | continue; |
| 4616 | |
| 4617 | TemplateDeductionInfo Info(Context); |
| 4618 | FunctionDecl *Specialization = 0; |
| 4619 | if (TemplateDeductionResult TDK |
Douglas Gregor | 48026d2 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 4620 | = DeduceTemplateArguments(FunTmpl, |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 4621 | (HasExplicitTemplateArgs ? &TemplateArgs : 0), |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4622 | R, Specialization, Info)) { |
| 4623 | // FIXME: Keep track of almost-matches? |
| 4624 | (void)TDK; |
| 4625 | continue; |
| 4626 | } |
| 4627 | |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4628 | Matches.addDecl(Specialization, P.getAccess()); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4629 | } |
| 4630 | |
| 4631 | // Find the most specialized function template specialization. |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4632 | UnresolvedSetIterator Result |
| 4633 | = getMostSpecialized(Matches.begin(), Matches.end(), TPOC_Other, |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4634 | D.getIdentifierLoc(), |
| 4635 | PartialDiagnostic(diag::err_explicit_instantiation_not_known) << Name, |
| 4636 | PartialDiagnostic(diag::err_explicit_instantiation_ambiguous) << Name, |
| 4637 | PartialDiagnostic(diag::note_explicit_instantiation_candidate)); |
| 4638 | |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4639 | if (Result == Matches.end()) |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4640 | return true; |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4641 | |
| 4642 | // Ignore access control bits, we don't need them for redeclaration checking. |
| 4643 | FunctionDecl *Specialization = cast<FunctionDecl>(*Result); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4644 | |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 4645 | if (Specialization->getTemplateSpecializationKind() == TSK_Undeclared) { |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4646 | Diag(D.getIdentifierLoc(), |
| 4647 | diag::err_explicit_instantiation_member_function_not_instantiated) |
| 4648 | << Specialization |
| 4649 | << (Specialization->getTemplateSpecializationKind() == |
| 4650 | TSK_ExplicitSpecialization); |
| 4651 | Diag(Specialization->getLocation(), diag::note_explicit_instantiation_here); |
| 4652 | return true; |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 4653 | } |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 4654 | |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 4655 | FunctionDecl *PrevDecl = Specialization->getPreviousDeclaration(); |
Douglas Gregor | 583f33b | 2009-10-15 18:07:02 +0000 | [diff] [blame] | 4656 | if (!PrevDecl && Specialization->isThisDeclarationADefinition()) |
| 4657 | PrevDecl = Specialization; |
| 4658 | |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 4659 | if (PrevDecl) { |
| 4660 | bool SuppressNew = false; |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 4661 | if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK, |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 4662 | PrevDecl, |
| 4663 | PrevDecl->getTemplateSpecializationKind(), |
| 4664 | PrevDecl->getPointOfInstantiation(), |
| 4665 | SuppressNew)) |
| 4666 | return true; |
| 4667 | |
| 4668 | // FIXME: We may still want to build some representation of this |
| 4669 | // explicit specialization. |
| 4670 | if (SuppressNew) |
| 4671 | return DeclPtrTy(); |
| 4672 | } |
Anders Carlsson | 26d6e9d | 2009-11-24 05:34:41 +0000 | [diff] [blame] | 4673 | |
| 4674 | Specialization->setTemplateSpecializationKind(TSK, D.getIdentifierLoc()); |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 4675 | |
| 4676 | if (TSK == TSK_ExplicitInstantiationDefinition) |
| 4677 | InstantiateFunctionDefinition(D.getIdentifierLoc(), Specialization, |
| 4678 | false, /*DefinitionRequired=*/true); |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 4679 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 4680 | // C++0x [temp.explicit]p2: |
| 4681 | // If the explicit instantiation is for a member function, a member class |
| 4682 | // or a static data member of a class template specialization, the name of |
| 4683 | // the class template specialization in the qualified-id for the member |
| 4684 | // name shall be a simple-template-id. |
| 4685 | // |
| 4686 | // C++98 has the same restriction, just worded differently. |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 4687 | FunctionTemplateDecl *FunTmpl = Specialization->getPrimaryTemplate(); |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 4688 | if (D.getName().getKind() != UnqualifiedId::IK_TemplateId && !FunTmpl && |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 4689 | D.getCXXScopeSpec().isSet() && |
| 4690 | !ScopeSpecifierHasTemplateId(D.getCXXScopeSpec())) |
| 4691 | Diag(D.getIdentifierLoc(), |
| 4692 | diag::err_explicit_instantiation_without_qualified_id) |
| 4693 | << Specialization << D.getCXXScopeSpec().getRange(); |
| 4694 | |
| 4695 | CheckExplicitInstantiationScope(*this, |
| 4696 | FunTmpl? (NamedDecl *)FunTmpl |
| 4697 | : Specialization->getInstantiatedFromMemberFunction(), |
| 4698 | D.getIdentifierLoc(), |
| 4699 | D.getCXXScopeSpec().isSet()); |
| 4700 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4701 | // FIXME: Create some kind of ExplicitInstantiationDecl here. |
| 4702 | return DeclPtrTy(); |
| 4703 | } |
| 4704 | |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 4705 | Sema::TypeResult |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 4706 | Sema::ActOnDependentTag(Scope *S, unsigned TagSpec, TagUseKind TUK, |
| 4707 | const CXXScopeSpec &SS, IdentifierInfo *Name, |
| 4708 | SourceLocation TagLoc, SourceLocation NameLoc) { |
| 4709 | // This has to hold, because SS is expected to be defined. |
| 4710 | assert(Name && "Expected a name in a dependent tag"); |
| 4711 | |
| 4712 | NestedNameSpecifier *NNS |
| 4713 | = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); |
| 4714 | if (!NNS) |
| 4715 | return true; |
| 4716 | |
| 4717 | QualType T = CheckTypenameType(NNS, *Name, SourceRange(TagLoc, NameLoc)); |
| 4718 | if (T.isNull()) |
| 4719 | return true; |
| 4720 | |
| 4721 | TagDecl::TagKind TagKind = TagDecl::getTagKindForTypeSpec(TagSpec); |
| 4722 | QualType ElabType = Context.getElaboratedType(T, TagKind); |
| 4723 | |
| 4724 | return ElabType.getAsOpaquePtr(); |
| 4725 | } |
| 4726 | |
| 4727 | Sema::TypeResult |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 4728 | Sema::ActOnTypenameType(SourceLocation TypenameLoc, const CXXScopeSpec &SS, |
| 4729 | const IdentifierInfo &II, SourceLocation IdLoc) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4730 | NestedNameSpecifier *NNS |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 4731 | = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); |
| 4732 | if (!NNS) |
| 4733 | return true; |
| 4734 | |
| 4735 | QualType T = CheckTypenameType(NNS, II, SourceRange(TypenameLoc, IdLoc)); |
Douglas Gregor | 31a19b6 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 4736 | if (T.isNull()) |
| 4737 | return true; |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 4738 | return T.getAsOpaquePtr(); |
| 4739 | } |
| 4740 | |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 4741 | Sema::TypeResult |
| 4742 | Sema::ActOnTypenameType(SourceLocation TypenameLoc, const CXXScopeSpec &SS, |
| 4743 | SourceLocation TemplateLoc, TypeTy *Ty) { |
Argyrios Kyrtzidis | e866190 | 2009-08-19 01:28:28 +0000 | [diff] [blame] | 4744 | QualType T = GetTypeFromParser(Ty); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4745 | NestedNameSpecifier *NNS |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 4746 | = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4747 | const TemplateSpecializationType *TemplateId |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4748 | = T->getAs<TemplateSpecializationType>(); |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 4749 | assert(TemplateId && "Expected a template specialization type"); |
| 4750 | |
Douglas Gregor | 6946baf | 2009-09-02 13:05:45 +0000 | [diff] [blame] | 4751 | if (computeDeclContext(SS, false)) { |
| 4752 | // If we can compute a declaration context, then the "typename" |
| 4753 | // keyword was superfluous. Just build a QualifiedNameType to keep |
| 4754 | // track of the nested-name-specifier. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4755 | |
Douglas Gregor | 6946baf | 2009-09-02 13:05:45 +0000 | [diff] [blame] | 4756 | // FIXME: Note that the QualifiedNameType had the "typename" keyword! |
| 4757 | return Context.getQualifiedNameType(NNS, T).getAsOpaquePtr(); |
| 4758 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4759 | |
Douglas Gregor | 6946baf | 2009-09-02 13:05:45 +0000 | [diff] [blame] | 4760 | return Context.getTypenameType(NNS, TemplateId).getAsOpaquePtr(); |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 4761 | } |
| 4762 | |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 4763 | /// \brief Build the type that describes a C++ typename specifier, |
| 4764 | /// e.g., "typename T::type". |
| 4765 | QualType |
| 4766 | Sema::CheckTypenameType(NestedNameSpecifier *NNS, const IdentifierInfo &II, |
| 4767 | SourceRange Range) { |
Douglas Gregor | 42af25f | 2009-05-11 19:58:34 +0000 | [diff] [blame] | 4768 | CXXRecordDecl *CurrentInstantiation = 0; |
| 4769 | if (NNS->isDependent()) { |
| 4770 | CurrentInstantiation = getCurrentInstantiationOf(NNS); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 4771 | |
Douglas Gregor | 42af25f | 2009-05-11 19:58:34 +0000 | [diff] [blame] | 4772 | // If the nested-name-specifier does not refer to the current |
| 4773 | // instantiation, then build a typename type. |
| 4774 | if (!CurrentInstantiation) |
| 4775 | return Context.getTypenameType(NNS, &II); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4776 | |
Douglas Gregor | de18d12 | 2009-09-02 13:12:51 +0000 | [diff] [blame] | 4777 | // The nested-name-specifier refers to the current instantiation, so the |
| 4778 | // "typename" keyword itself is superfluous. In C++03, the program is |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4779 | // actually ill-formed. However, DR 382 (in C++0x CD1) allows such |
Douglas Gregor | de18d12 | 2009-09-02 13:12:51 +0000 | [diff] [blame] | 4780 | // extraneous "typename" keywords, and we retroactively apply this DR to |
| 4781 | // C++03 code. |
Douglas Gregor | 42af25f | 2009-05-11 19:58:34 +0000 | [diff] [blame] | 4782 | } |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 4783 | |
Douglas Gregor | 42af25f | 2009-05-11 19:58:34 +0000 | [diff] [blame] | 4784 | DeclContext *Ctx = 0; |
| 4785 | |
| 4786 | if (CurrentInstantiation) |
| 4787 | Ctx = CurrentInstantiation; |
| 4788 | else { |
| 4789 | CXXScopeSpec SS; |
| 4790 | SS.setScopeRep(NNS); |
| 4791 | SS.setRange(Range); |
| 4792 | if (RequireCompleteDeclContext(SS)) |
| 4793 | return QualType(); |
| 4794 | |
| 4795 | Ctx = computeDeclContext(SS); |
| 4796 | } |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 4797 | assert(Ctx && "No declaration context?"); |
| 4798 | |
| 4799 | DeclarationName Name(&II); |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 4800 | LookupResult Result(*this, Name, Range.getEnd(), LookupOrdinaryName); |
| 4801 | LookupQualifiedName(Result, Ctx); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 4802 | unsigned DiagID = 0; |
| 4803 | Decl *Referenced = 0; |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 4804 | switch (Result.getResultKind()) { |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 4805 | case LookupResult::NotFound: |
Douglas Gregor | 3f09327 | 2009-10-13 21:16:44 +0000 | [diff] [blame] | 4806 | DiagID = diag::err_typename_nested_not_found; |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 4807 | break; |
Douglas Gregor | 7d3f576 | 2010-01-15 01:44:47 +0000 | [diff] [blame] | 4808 | |
| 4809 | case LookupResult::NotFoundInCurrentInstantiation: |
| 4810 | // Okay, it's a member of an unknown instantiation. |
| 4811 | return Context.getTypenameType(NNS, &II); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 4812 | |
| 4813 | case LookupResult::Found: |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 4814 | if (TypeDecl *Type = dyn_cast<TypeDecl>(Result.getFoundDecl())) { |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 4815 | // We found a type. Build a QualifiedNameType, since the |
| 4816 | // typename-specifier was just sugar. FIXME: Tell |
| 4817 | // QualifiedNameType that it has a "typename" prefix. |
| 4818 | return Context.getQualifiedNameType(NNS, Context.getTypeDeclType(Type)); |
| 4819 | } |
| 4820 | |
| 4821 | DiagID = diag::err_typename_nested_not_type; |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 4822 | Referenced = Result.getFoundDecl(); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 4823 | break; |
| 4824 | |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 4825 | case LookupResult::FoundUnresolvedValue: |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 4826 | llvm_unreachable("unresolved using decl in non-dependent context"); |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 4827 | return QualType(); |
| 4828 | |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 4829 | case LookupResult::FoundOverloaded: |
| 4830 | DiagID = diag::err_typename_nested_not_type; |
| 4831 | Referenced = *Result.begin(); |
| 4832 | break; |
| 4833 | |
John McCall | 6e24726 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 4834 | case LookupResult::Ambiguous: |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 4835 | return QualType(); |
| 4836 | } |
| 4837 | |
| 4838 | // If we get here, it's because name lookup did not find a |
| 4839 | // type. Emit an appropriate diagnostic and return an error. |
Douglas Gregor | 3f09327 | 2009-10-13 21:16:44 +0000 | [diff] [blame] | 4840 | Diag(Range.getEnd(), DiagID) << Range << Name << Ctx; |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 4841 | if (Referenced) |
| 4842 | Diag(Referenced->getLocation(), diag::note_typename_refers_here) |
| 4843 | << Name; |
| 4844 | return QualType(); |
| 4845 | } |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 4846 | |
| 4847 | namespace { |
| 4848 | // See Sema::RebuildTypeInCurrentInstantiation |
Benjamin Kramer | 85b4521 | 2009-11-28 19:45:26 +0000 | [diff] [blame] | 4849 | class CurrentInstantiationRebuilder |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4850 | : public TreeTransform<CurrentInstantiationRebuilder> { |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 4851 | SourceLocation Loc; |
| 4852 | DeclarationName Entity; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4853 | |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 4854 | public: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4855 | CurrentInstantiationRebuilder(Sema &SemaRef, |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 4856 | SourceLocation Loc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4857 | DeclarationName Entity) |
| 4858 | : TreeTransform<CurrentInstantiationRebuilder>(SemaRef), |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 4859 | Loc(Loc), Entity(Entity) { } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4860 | |
| 4861 | /// \brief Determine whether the given type \p T has already been |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 4862 | /// transformed. |
| 4863 | /// |
| 4864 | /// For the purposes of type reconstruction, a type has already been |
| 4865 | /// transformed if it is NULL or if it is not dependent. |
| 4866 | bool AlreadyTransformed(QualType T) { |
| 4867 | return T.isNull() || !T->isDependentType(); |
| 4868 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4869 | |
| 4870 | /// \brief Returns the location of the entity whose type is being |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 4871 | /// rebuilt. |
| 4872 | SourceLocation getBaseLocation() { return Loc; } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4873 | |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 4874 | /// \brief Returns the name of the entity whose type is being rebuilt. |
| 4875 | DeclarationName getBaseEntity() { return Entity; } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4876 | |
Douglas Gregor | 972e6ce | 2009-10-27 06:26:26 +0000 | [diff] [blame] | 4877 | /// \brief Sets the "base" location and entity when that |
| 4878 | /// information is known based on another transformation. |
| 4879 | void setBase(SourceLocation Loc, DeclarationName Entity) { |
| 4880 | this->Loc = Loc; |
| 4881 | this->Entity = Entity; |
| 4882 | } |
| 4883 | |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 4884 | /// \brief Transforms an expression by returning the expression itself |
| 4885 | /// (an identity function). |
| 4886 | /// |
| 4887 | /// FIXME: This is completely unsafe; we will need to actually clone the |
| 4888 | /// expressions. |
| 4889 | Sema::OwningExprResult TransformExpr(Expr *E) { |
| 4890 | return getSema().Owned(E); |
| 4891 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4892 | |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 4893 | /// \brief Transforms a typename type by determining whether the type now |
| 4894 | /// refers to a member of the current instantiation, and then |
| 4895 | /// type-checking and building a QualifiedNameType (when possible). |
John McCall | a2becad | 2009-10-21 00:40:46 +0000 | [diff] [blame] | 4896 | QualType TransformTypenameType(TypeLocBuilder &TLB, TypenameTypeLoc TL); |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 4897 | }; |
| 4898 | } |
| 4899 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4900 | QualType |
John McCall | a2becad | 2009-10-21 00:40:46 +0000 | [diff] [blame] | 4901 | CurrentInstantiationRebuilder::TransformTypenameType(TypeLocBuilder &TLB, |
| 4902 | TypenameTypeLoc TL) { |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 4903 | TypenameType *T = TL.getTypePtr(); |
John McCall | a2becad | 2009-10-21 00:40:46 +0000 | [diff] [blame] | 4904 | |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 4905 | NestedNameSpecifier *NNS |
| 4906 | = TransformNestedNameSpecifier(T->getQualifier(), |
| 4907 | /*FIXME:*/SourceRange(getBaseLocation())); |
| 4908 | if (!NNS) |
| 4909 | return QualType(); |
| 4910 | |
| 4911 | // If the nested-name-specifier did not change, and we cannot compute the |
| 4912 | // context corresponding to the nested-name-specifier, then this |
| 4913 | // typename type will not change; exit early. |
| 4914 | CXXScopeSpec SS; |
| 4915 | SS.setRange(SourceRange(getBaseLocation())); |
| 4916 | SS.setScopeRep(NNS); |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 4917 | |
| 4918 | QualType Result; |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 4919 | if (NNS == T->getQualifier() && getSema().computeDeclContext(SS) == 0) |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 4920 | Result = QualType(T, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4921 | |
| 4922 | // Rebuild the typename type, which will probably turn into a |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 4923 | // QualifiedNameType. |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 4924 | else if (const TemplateSpecializationType *TemplateId = T->getTemplateId()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4925 | QualType NewTemplateId |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 4926 | = TransformType(QualType(TemplateId, 0)); |
| 4927 | if (NewTemplateId.isNull()) |
| 4928 | return QualType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4929 | |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 4930 | if (NNS == T->getQualifier() && |
| 4931 | NewTemplateId == QualType(TemplateId, 0)) |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 4932 | Result = QualType(T, 0); |
| 4933 | else |
| 4934 | Result = getDerived().RebuildTypenameType(NNS, NewTemplateId); |
| 4935 | } else |
| 4936 | Result = getDerived().RebuildTypenameType(NNS, T->getIdentifier(), |
| 4937 | SourceRange(TL.getNameLoc())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4938 | |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 4939 | TypenameTypeLoc NewTL = TLB.push<TypenameTypeLoc>(Result); |
| 4940 | NewTL.setNameLoc(TL.getNameLoc()); |
| 4941 | return Result; |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 4942 | } |
| 4943 | |
| 4944 | /// \brief Rebuilds a type within the context of the current instantiation. |
| 4945 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4946 | /// 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] | 4947 | /// a class template (or class template partial specialization) that was parsed |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4948 | /// and constructed before we entered the scope of the class template (or |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 4949 | /// partial specialization thereof). This routine will rebuild that type now |
| 4950 | /// that we have entered the declarator's scope, which may produce different |
| 4951 | /// canonical types, e.g., |
| 4952 | /// |
| 4953 | /// \code |
| 4954 | /// template<typename T> |
| 4955 | /// struct X { |
| 4956 | /// typedef T* pointer; |
| 4957 | /// pointer data(); |
| 4958 | /// }; |
| 4959 | /// |
| 4960 | /// template<typename T> |
| 4961 | /// typename X<T>::pointer X<T>::data() { ... } |
| 4962 | /// \endcode |
| 4963 | /// |
| 4964 | /// Here, the type "typename X<T>::pointer" will be created as a TypenameType, |
| 4965 | /// since we do not know that we can look into X<T> when we parsed the type. |
| 4966 | /// This function will rebuild the type, performing the lookup of "pointer" |
| 4967 | /// in X<T> and returning a QualifiedNameType whose canonical type is the same |
| 4968 | /// as the canonical type of T*, allowing the return types of the out-of-line |
| 4969 | /// definition and the declaration to match. |
| 4970 | QualType Sema::RebuildTypeInCurrentInstantiation(QualType T, SourceLocation Loc, |
| 4971 | DeclarationName Name) { |
| 4972 | if (T.isNull() || !T->isDependentType()) |
| 4973 | return T; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4974 | |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 4975 | CurrentInstantiationRebuilder Rebuilder(*this, Loc, Name); |
| 4976 | return Rebuilder.TransformType(T); |
Benjamin Kramer | 27ba2f0 | 2009-08-11 22:33:06 +0000 | [diff] [blame] | 4977 | } |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 4978 | |
| 4979 | /// \brief Produces a formatted string that describes the binding of |
| 4980 | /// template parameters to template arguments. |
| 4981 | std::string |
| 4982 | Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params, |
| 4983 | const TemplateArgumentList &Args) { |
Douglas Gregor | 9148c3f | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 4984 | // FIXME: For variadic templates, we'll need to get the structured list. |
| 4985 | return getTemplateArgumentBindingsText(Params, Args.getFlatArgumentList(), |
| 4986 | Args.flat_size()); |
| 4987 | } |
| 4988 | |
| 4989 | std::string |
| 4990 | Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params, |
| 4991 | const TemplateArgument *Args, |
| 4992 | unsigned NumArgs) { |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 4993 | std::string Result; |
| 4994 | |
Douglas Gregor | 9148c3f | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 4995 | if (!Params || Params->size() == 0 || NumArgs == 0) |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 4996 | return Result; |
| 4997 | |
| 4998 | for (unsigned I = 0, N = Params->size(); I != N; ++I) { |
Douglas Gregor | 9148c3f | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 4999 | if (I >= NumArgs) |
| 5000 | break; |
| 5001 | |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 5002 | if (I == 0) |
| 5003 | Result += "[with "; |
| 5004 | else |
| 5005 | Result += ", "; |
| 5006 | |
| 5007 | if (const IdentifierInfo *Id = Params->getParam(I)->getIdentifier()) { |
| 5008 | Result += Id->getName(); |
| 5009 | } else { |
| 5010 | Result += '$'; |
| 5011 | Result += llvm::utostr(I); |
| 5012 | } |
| 5013 | |
| 5014 | Result += " = "; |
| 5015 | |
| 5016 | switch (Args[I].getKind()) { |
| 5017 | case TemplateArgument::Null: |
| 5018 | Result += "<no value>"; |
| 5019 | break; |
| 5020 | |
| 5021 | case TemplateArgument::Type: { |
| 5022 | std::string TypeStr; |
| 5023 | Args[I].getAsType().getAsStringInternal(TypeStr, |
| 5024 | Context.PrintingPolicy); |
| 5025 | Result += TypeStr; |
| 5026 | break; |
| 5027 | } |
| 5028 | |
| 5029 | case TemplateArgument::Declaration: { |
| 5030 | bool Unnamed = true; |
| 5031 | if (NamedDecl *ND = dyn_cast_or_null<NamedDecl>(Args[I].getAsDecl())) { |
| 5032 | if (ND->getDeclName()) { |
| 5033 | Unnamed = false; |
| 5034 | Result += ND->getNameAsString(); |
| 5035 | } |
| 5036 | } |
| 5037 | |
| 5038 | if (Unnamed) { |
| 5039 | Result += "<anonymous>"; |
| 5040 | } |
| 5041 | break; |
| 5042 | } |
| 5043 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 5044 | case TemplateArgument::Template: { |
| 5045 | std::string Str; |
| 5046 | llvm::raw_string_ostream OS(Str); |
| 5047 | Args[I].getAsTemplate().print(OS, Context.PrintingPolicy); |
| 5048 | Result += OS.str(); |
| 5049 | break; |
| 5050 | } |
| 5051 | |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 5052 | case TemplateArgument::Integral: { |
| 5053 | Result += Args[I].getAsIntegral()->toString(10); |
| 5054 | break; |
| 5055 | } |
| 5056 | |
| 5057 | case TemplateArgument::Expression: { |
| 5058 | assert(false && "No expressions in deduced template arguments!"); |
| 5059 | Result += "<expression>"; |
| 5060 | break; |
| 5061 | } |
| 5062 | |
| 5063 | case TemplateArgument::Pack: |
| 5064 | // FIXME: Format template argument packs |
| 5065 | Result += "<template argument pack>"; |
| 5066 | break; |
| 5067 | } |
| 5068 | } |
| 5069 | |
| 5070 | Result += ']'; |
| 5071 | return Result; |
| 5072 | } |