Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 1 | //===------- SemaTemplate.cpp - Semantic Analysis for C++ Templates -------===/ |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 7 | //===----------------------------------------------------------------------===/ |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 8 | // |
| 9 | // This file implements semantic analysis for C++ templates. |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 10 | //===----------------------------------------------------------------------===/ |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 11 | |
| 12 | #include "Sema.h" |
John McCall | 7d384dd | 2009-11-18 07:57:50 +0000 | [diff] [blame] | 13 | #include "Lookup.h" |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 14 | #include "TreeTransform.h" |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 15 | #include "clang/AST/ASTContext.h" |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 16 | #include "clang/AST/Expr.h" |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 17 | #include "clang/AST/ExprCXX.h" |
John McCall | 92b7f70 | 2010-03-11 07:50:04 +0000 | [diff] [blame] | 18 | #include "clang/AST/DeclFriend.h" |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 19 | #include "clang/AST/DeclTemplate.h" |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 20 | #include "clang/Parse/DeclSpec.h" |
Douglas Gregor | 314b97f | 2009-11-10 19:49:08 +0000 | [diff] [blame] | 21 | #include "clang/Parse/Template.h" |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 22 | #include "clang/Basic/LangOptions.h" |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 23 | #include "clang/Basic/PartialDiagnostic.h" |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/StringExtras.h" |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 25 | using namespace clang; |
| 26 | |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 27 | /// \brief Determine whether the declaration found is acceptable as the name |
| 28 | /// of a template and, if so, return that template declaration. Otherwise, |
| 29 | /// returns NULL. |
| 30 | static NamedDecl *isAcceptableTemplateName(ASTContext &Context, NamedDecl *D) { |
| 31 | if (!D) |
| 32 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 33 | |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 34 | if (isa<TemplateDecl>(D)) |
| 35 | return D; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 36 | |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 37 | if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) { |
| 38 | // C++ [temp.local]p1: |
| 39 | // Like normal (non-template) classes, class templates have an |
| 40 | // injected-class-name (Clause 9). The injected-class-name |
| 41 | // can be used with or without a template-argument-list. When |
| 42 | // it is used without a template-argument-list, it is |
| 43 | // equivalent to the injected-class-name followed by the |
| 44 | // template-parameters of the class template enclosed in |
| 45 | // <>. When it is used with a template-argument-list, it |
| 46 | // refers to the specified class template specialization, |
| 47 | // which could be the current specialization or another |
| 48 | // specialization. |
| 49 | if (Record->isInjectedClassName()) { |
Douglas Gregor | 542b548 | 2009-10-14 17:30:58 +0000 | [diff] [blame] | 50 | Record = cast<CXXRecordDecl>(Record->getDeclContext()); |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 51 | if (Record->getDescribedClassTemplate()) |
| 52 | return Record->getDescribedClassTemplate(); |
| 53 | |
| 54 | if (ClassTemplateSpecializationDecl *Spec |
| 55 | = dyn_cast<ClassTemplateSpecializationDecl>(Record)) |
| 56 | return Spec->getSpecializedTemplate(); |
| 57 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 58 | |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 59 | return 0; |
| 60 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 61 | |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 62 | return 0; |
| 63 | } |
| 64 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 65 | static void FilterAcceptableTemplateNames(ASTContext &C, LookupResult &R) { |
| 66 | LookupResult::Filter filter = R.makeFilter(); |
| 67 | while (filter.hasNext()) { |
| 68 | NamedDecl *Orig = filter.next(); |
| 69 | NamedDecl *Repl = isAcceptableTemplateName(C, Orig->getUnderlyingDecl()); |
| 70 | if (!Repl) |
| 71 | filter.erase(); |
| 72 | else if (Repl != Orig) |
| 73 | filter.replace(Repl); |
| 74 | } |
| 75 | filter.done(); |
| 76 | } |
| 77 | |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 78 | TemplateNameKind Sema::isTemplateName(Scope *S, |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 79 | const CXXScopeSpec &SS, |
| 80 | UnqualifiedId &Name, |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 81 | TypeTy *ObjectTypePtr, |
Douglas Gregor | 495c35d | 2009-08-25 22:51:20 +0000 | [diff] [blame] | 82 | bool EnteringContext, |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 83 | TemplateTy &TemplateResult) { |
Douglas Gregor | b862b8f | 2010-01-11 23:29:10 +0000 | [diff] [blame] | 84 | assert(getLangOptions().CPlusPlus && "No template names in C!"); |
| 85 | |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 86 | DeclarationName TName; |
| 87 | |
| 88 | switch (Name.getKind()) { |
| 89 | case UnqualifiedId::IK_Identifier: |
| 90 | TName = DeclarationName(Name.Identifier); |
| 91 | break; |
| 92 | |
| 93 | case UnqualifiedId::IK_OperatorFunctionId: |
| 94 | TName = Context.DeclarationNames.getCXXOperatorName( |
| 95 | Name.OperatorFunctionId.Operator); |
| 96 | break; |
| 97 | |
Sean Hunt | e6252d1 | 2009-11-28 08:58:14 +0000 | [diff] [blame] | 98 | case UnqualifiedId::IK_LiteralOperatorId: |
Sean Hunt | 3e518bd | 2009-11-29 07:34:05 +0000 | [diff] [blame] | 99 | TName = Context.DeclarationNames.getCXXLiteralOperatorName(Name.Identifier); |
| 100 | break; |
Sean Hunt | e6252d1 | 2009-11-28 08:58:14 +0000 | [diff] [blame] | 101 | |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 102 | default: |
| 103 | return TNK_Non_template; |
| 104 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 105 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 106 | QualType ObjectType = QualType::getFromOpaquePtr(ObjectTypePtr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 107 | |
Douglas Gregor | bfea239 | 2009-12-31 08:11:17 +0000 | [diff] [blame] | 108 | LookupResult R(*this, TName, Name.getSourceRange().getBegin(), |
| 109 | LookupOrdinaryName); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 110 | R.suppressDiagnostics(); |
| 111 | LookupTemplateName(R, S, SS, ObjectType, EnteringContext); |
| 112 | if (R.empty()) |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 113 | return TNK_Non_template; |
| 114 | |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 115 | TemplateName Template; |
| 116 | TemplateNameKind TemplateKind; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 117 | |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 118 | unsigned ResultCount = R.end() - R.begin(); |
| 119 | if (ResultCount > 1) { |
| 120 | // We assume that we'll preserve the qualifier from a function |
| 121 | // template name in other ways. |
| 122 | Template = Context.getOverloadedTemplateName(R.begin(), R.end()); |
| 123 | TemplateKind = TNK_Function_template; |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 124 | } else { |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 125 | TemplateDecl *TD = cast<TemplateDecl>((*R.begin())->getUnderlyingDecl()); |
| 126 | |
| 127 | if (SS.isSet() && !SS.isInvalid()) { |
| 128 | NestedNameSpecifier *Qualifier |
| 129 | = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); |
| 130 | Template = Context.getQualifiedTemplateName(Qualifier, false, TD); |
| 131 | } else { |
| 132 | Template = TemplateName(TD); |
| 133 | } |
| 134 | |
| 135 | if (isa<FunctionTemplateDecl>(TD)) |
| 136 | TemplateKind = TNK_Function_template; |
| 137 | else { |
| 138 | assert(isa<ClassTemplateDecl>(TD) || isa<TemplateTemplateParmDecl>(TD)); |
| 139 | TemplateKind = TNK_Type_template; |
| 140 | } |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 141 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 142 | |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 143 | TemplateResult = TemplateTy::make(Template); |
| 144 | return TemplateKind; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 145 | } |
| 146 | |
Douglas Gregor | 84d0a19 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 147 | bool Sema::DiagnoseUnknownTemplateName(const IdentifierInfo &II, |
| 148 | SourceLocation IILoc, |
| 149 | Scope *S, |
| 150 | const CXXScopeSpec *SS, |
| 151 | TemplateTy &SuggestedTemplate, |
| 152 | TemplateNameKind &SuggestedKind) { |
| 153 | // We can't recover unless there's a dependent scope specifier preceding the |
| 154 | // template name. |
| 155 | if (!SS || !SS->isSet() || !isDependentScopeSpecifier(*SS) || |
| 156 | computeDeclContext(*SS)) |
| 157 | return false; |
| 158 | |
| 159 | // The code is missing a 'template' keyword prior to the dependent template |
| 160 | // name. |
| 161 | NestedNameSpecifier *Qualifier = (NestedNameSpecifier*)SS->getScopeRep(); |
| 162 | Diag(IILoc, diag::err_template_kw_missing) |
| 163 | << Qualifier << II.getName() |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 164 | << FixItHint::CreateInsertion(IILoc, "template "); |
Douglas Gregor | 84d0a19 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 165 | SuggestedTemplate |
| 166 | = TemplateTy::make(Context.getDependentTemplateName(Qualifier, &II)); |
| 167 | SuggestedKind = TNK_Dependent_template_name; |
| 168 | return true; |
| 169 | } |
| 170 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 171 | void Sema::LookupTemplateName(LookupResult &Found, |
| 172 | Scope *S, const CXXScopeSpec &SS, |
| 173 | QualType ObjectType, |
| 174 | bool EnteringContext) { |
| 175 | // Determine where to perform name lookup |
| 176 | DeclContext *LookupCtx = 0; |
| 177 | bool isDependent = false; |
| 178 | if (!ObjectType.isNull()) { |
| 179 | // This nested-name-specifier occurs in a member access expression, e.g., |
| 180 | // x->B::f, and we are looking into the type of the object. |
| 181 | assert(!SS.isSet() && "ObjectType and scope specifier cannot coexist"); |
| 182 | LookupCtx = computeDeclContext(ObjectType); |
| 183 | isDependent = ObjectType->isDependentType(); |
| 184 | assert((isDependent || !ObjectType->isIncompleteType()) && |
| 185 | "Caller should have completed object type"); |
| 186 | } else if (SS.isSet()) { |
| 187 | // This nested-name-specifier occurs after another nested-name-specifier, |
| 188 | // so long into the context associated with the prior nested-name-specifier. |
| 189 | LookupCtx = computeDeclContext(SS, EnteringContext); |
| 190 | isDependent = isDependentScopeSpecifier(SS); |
| 191 | |
| 192 | // The declaration context must be complete. |
| 193 | if (LookupCtx && RequireCompleteDeclContext(SS)) |
| 194 | return; |
| 195 | } |
| 196 | |
| 197 | bool ObjectTypeSearchedInScope = false; |
| 198 | if (LookupCtx) { |
| 199 | // Perform "qualified" name lookup into the declaration context we |
| 200 | // computed, which is either the type of the base of a member access |
| 201 | // expression or the declaration context associated with a prior |
| 202 | // nested-name-specifier. |
| 203 | LookupQualifiedName(Found, LookupCtx); |
| 204 | |
| 205 | if (!ObjectType.isNull() && Found.empty()) { |
| 206 | // C++ [basic.lookup.classref]p1: |
| 207 | // In a class member access expression (5.2.5), if the . or -> token is |
| 208 | // immediately followed by an identifier followed by a <, the |
| 209 | // identifier must be looked up to determine whether the < is the |
| 210 | // beginning of a template argument list (14.2) or a less-than operator. |
| 211 | // The identifier is first looked up in the class of the object |
| 212 | // expression. If the identifier is not found, it is then looked up in |
| 213 | // the context of the entire postfix-expression and shall name a class |
| 214 | // or function template. |
| 215 | // |
| 216 | // FIXME: When we're instantiating a template, do we actually have to |
| 217 | // look in the scope of the template? Seems fishy... |
| 218 | if (S) LookupName(Found, S); |
| 219 | ObjectTypeSearchedInScope = true; |
| 220 | } |
| 221 | } else if (isDependent) { |
Douglas Gregor | 2e93388 | 2010-01-12 17:06:20 +0000 | [diff] [blame] | 222 | // We cannot look into a dependent object type or nested nme |
| 223 | // specifier. |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 224 | return; |
| 225 | } else { |
| 226 | // Perform unqualified name lookup in the current scope. |
| 227 | LookupName(Found, S); |
| 228 | } |
| 229 | |
| 230 | // FIXME: Cope with ambiguous name-lookup results. |
| 231 | assert(!Found.isAmbiguous() && |
| 232 | "Cannot handle template name-lookup ambiguities"); |
| 233 | |
Douglas Gregor | 2e93388 | 2010-01-12 17:06:20 +0000 | [diff] [blame] | 234 | if (Found.empty() && !isDependent) { |
Douglas Gregor | bfea239 | 2009-12-31 08:11:17 +0000 | [diff] [blame] | 235 | // If we did not find any names, attempt to correct any typos. |
| 236 | DeclarationName Name = Found.getLookupName(); |
| 237 | if (CorrectTypo(Found, S, &SS, LookupCtx)) { |
| 238 | FilterAcceptableTemplateNames(Context, Found); |
| 239 | if (!Found.empty() && isa<TemplateDecl>(*Found.begin())) { |
| 240 | if (LookupCtx) |
| 241 | Diag(Found.getNameLoc(), diag::err_no_member_template_suggest) |
| 242 | << Name << LookupCtx << Found.getLookupName() << SS.getRange() |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 243 | << FixItHint::CreateReplacement(Found.getNameLoc(), |
Douglas Gregor | bfea239 | 2009-12-31 08:11:17 +0000 | [diff] [blame] | 244 | Found.getLookupName().getAsString()); |
| 245 | else |
| 246 | Diag(Found.getNameLoc(), diag::err_no_template_suggest) |
| 247 | << Name << Found.getLookupName() |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 248 | << FixItHint::CreateReplacement(Found.getNameLoc(), |
Douglas Gregor | bfea239 | 2009-12-31 08:11:17 +0000 | [diff] [blame] | 249 | Found.getLookupName().getAsString()); |
Douglas Gregor | 67dd1d4 | 2010-01-07 00:17:44 +0000 | [diff] [blame] | 250 | if (TemplateDecl *Template = Found.getAsSingle<TemplateDecl>()) |
| 251 | Diag(Template->getLocation(), diag::note_previous_decl) |
| 252 | << Template->getDeclName(); |
Douglas Gregor | bfea239 | 2009-12-31 08:11:17 +0000 | [diff] [blame] | 253 | } else |
| 254 | Found.clear(); |
| 255 | } else { |
| 256 | Found.clear(); |
| 257 | } |
| 258 | } |
| 259 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 260 | FilterAcceptableTemplateNames(Context, Found); |
| 261 | if (Found.empty()) |
| 262 | return; |
| 263 | |
| 264 | if (S && !ObjectType.isNull() && !ObjectTypeSearchedInScope) { |
| 265 | // C++ [basic.lookup.classref]p1: |
| 266 | // [...] If the lookup in the class of the object expression finds a |
| 267 | // template, the name is also looked up in the context of the entire |
| 268 | // postfix-expression and [...] |
| 269 | // |
| 270 | LookupResult FoundOuter(*this, Found.getLookupName(), Found.getNameLoc(), |
| 271 | LookupOrdinaryName); |
| 272 | LookupName(FoundOuter, S); |
| 273 | FilterAcceptableTemplateNames(Context, FoundOuter); |
| 274 | // FIXME: Handle ambiguities in this lookup better |
| 275 | |
| 276 | if (FoundOuter.empty()) { |
| 277 | // - if the name is not found, the name found in the class of the |
| 278 | // object expression is used, otherwise |
| 279 | } else if (!FoundOuter.getAsSingle<ClassTemplateDecl>()) { |
| 280 | // - if the name is found in the context of the entire |
| 281 | // postfix-expression and does not name a class template, the name |
| 282 | // found in the class of the object expression is used, otherwise |
| 283 | } else { |
| 284 | // - if the name found is a class template, it must refer to the same |
| 285 | // entity as the one found in the class of the object expression, |
| 286 | // otherwise the program is ill-formed. |
| 287 | if (!Found.isSingleResult() || |
| 288 | Found.getFoundDecl()->getCanonicalDecl() |
| 289 | != FoundOuter.getFoundDecl()->getCanonicalDecl()) { |
| 290 | Diag(Found.getNameLoc(), |
| 291 | diag::err_nested_name_member_ref_lookup_ambiguous) |
| 292 | << Found.getLookupName(); |
| 293 | Diag(Found.getRepresentativeDecl()->getLocation(), |
| 294 | diag::note_ambig_member_ref_object_type) |
| 295 | << ObjectType; |
| 296 | Diag(FoundOuter.getFoundDecl()->getLocation(), |
| 297 | diag::note_ambig_member_ref_scope); |
| 298 | |
| 299 | // Recover by taking the template that we found in the object |
| 300 | // expression's type. |
| 301 | } |
| 302 | } |
| 303 | } |
| 304 | } |
| 305 | |
John McCall | 2f841ba | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 306 | /// ActOnDependentIdExpression - Handle a dependent id-expression that |
| 307 | /// was just parsed. This is only possible with an explicit scope |
| 308 | /// specifier naming a dependent type. |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 309 | Sema::OwningExprResult |
| 310 | Sema::ActOnDependentIdExpression(const CXXScopeSpec &SS, |
| 311 | DeclarationName Name, |
| 312 | SourceLocation NameLoc, |
John McCall | 2f841ba | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 313 | bool isAddressOfOperand, |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 314 | const TemplateArgumentListInfo *TemplateArgs) { |
| 315 | NestedNameSpecifier *Qualifier |
| 316 | = static_cast<NestedNameSpecifier*>(SS.getScopeRep()); |
| 317 | |
John McCall | 2f841ba | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 318 | if (!isAddressOfOperand && |
| 319 | isa<CXXMethodDecl>(CurContext) && |
| 320 | cast<CXXMethodDecl>(CurContext)->isInstance()) { |
| 321 | QualType ThisType = cast<CXXMethodDecl>(CurContext)->getThisType(Context); |
| 322 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 323 | // Since the 'this' expression is synthesized, we don't need to |
| 324 | // perform the double-lookup check. |
| 325 | NamedDecl *FirstQualifierInScope = 0; |
| 326 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 327 | return Owned(CXXDependentScopeMemberExpr::Create(Context, |
| 328 | /*This*/ 0, ThisType, |
| 329 | /*IsArrow*/ true, |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 330 | /*Op*/ SourceLocation(), |
| 331 | Qualifier, SS.getRange(), |
| 332 | FirstQualifierInScope, |
| 333 | Name, NameLoc, |
| 334 | TemplateArgs)); |
| 335 | } |
| 336 | |
| 337 | return BuildDependentDeclRefExpr(SS, Name, NameLoc, TemplateArgs); |
| 338 | } |
| 339 | |
| 340 | Sema::OwningExprResult |
| 341 | Sema::BuildDependentDeclRefExpr(const CXXScopeSpec &SS, |
| 342 | DeclarationName Name, |
| 343 | SourceLocation NameLoc, |
| 344 | const TemplateArgumentListInfo *TemplateArgs) { |
| 345 | return Owned(DependentScopeDeclRefExpr::Create(Context, |
| 346 | static_cast<NestedNameSpecifier*>(SS.getScopeRep()), |
| 347 | SS.getRange(), |
| 348 | Name, NameLoc, |
| 349 | TemplateArgs)); |
Douglas Gregor | d6fb7ef | 2008-12-18 19:37:40 +0000 | [diff] [blame] | 350 | } |
| 351 | |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 352 | /// DiagnoseTemplateParameterShadow - Produce a diagnostic complaining |
| 353 | /// that the template parameter 'PrevDecl' is being shadowed by a new |
| 354 | /// declaration at location Loc. Returns true to indicate that this is |
| 355 | /// an error, and false otherwise. |
| 356 | bool Sema::DiagnoseTemplateParameterShadow(SourceLocation Loc, Decl *PrevDecl) { |
Douglas Gregor | f57172b | 2008-12-08 18:40:42 +0000 | [diff] [blame] | 357 | assert(PrevDecl->isTemplateParameter() && "Not a template parameter"); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 358 | |
| 359 | // Microsoft Visual C++ permits template parameters to be shadowed. |
| 360 | if (getLangOptions().Microsoft) |
| 361 | return false; |
| 362 | |
| 363 | // C++ [temp.local]p4: |
| 364 | // A template-parameter shall not be redeclared within its |
| 365 | // scope (including nested scopes). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 366 | Diag(Loc, diag::err_template_param_shadow) |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 367 | << cast<NamedDecl>(PrevDecl)->getDeclName(); |
| 368 | Diag(PrevDecl->getLocation(), diag::note_template_param_here); |
| 369 | return true; |
| 370 | } |
| 371 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 372 | /// AdjustDeclIfTemplate - If the given decl happens to be a template, reset |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 373 | /// the parameter D to reference the templated declaration and return a pointer |
| 374 | /// to the template declaration. Otherwise, do nothing to D and return null. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 375 | TemplateDecl *Sema::AdjustDeclIfTemplate(DeclPtrTy &D) { |
Douglas Gregor | 13d2d6c | 2009-10-06 21:27:51 +0000 | [diff] [blame] | 376 | if (TemplateDecl *Temp = dyn_cast_or_null<TemplateDecl>(D.getAs<Decl>())) { |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 377 | D = DeclPtrTy::make(Temp->getTemplatedDecl()); |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 378 | return Temp; |
| 379 | } |
| 380 | return 0; |
| 381 | } |
| 382 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 383 | static TemplateArgumentLoc translateTemplateArgument(Sema &SemaRef, |
| 384 | const ParsedTemplateArgument &Arg) { |
| 385 | |
| 386 | switch (Arg.getKind()) { |
| 387 | case ParsedTemplateArgument::Type: { |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 388 | TypeSourceInfo *DI; |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 389 | QualType T = SemaRef.GetTypeFromParser(Arg.getAsType(), &DI); |
| 390 | if (!DI) |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 391 | DI = SemaRef.Context.getTrivialTypeSourceInfo(T, Arg.getLocation()); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 392 | return TemplateArgumentLoc(TemplateArgument(T), DI); |
| 393 | } |
| 394 | |
| 395 | case ParsedTemplateArgument::NonType: { |
| 396 | Expr *E = static_cast<Expr *>(Arg.getAsExpr()); |
| 397 | return TemplateArgumentLoc(TemplateArgument(E), E); |
| 398 | } |
| 399 | |
| 400 | case ParsedTemplateArgument::Template: { |
| 401 | TemplateName Template |
| 402 | = TemplateName::getFromVoidPointer(Arg.getAsTemplate().get()); |
| 403 | return TemplateArgumentLoc(TemplateArgument(Template), |
| 404 | Arg.getScopeSpec().getRange(), |
| 405 | Arg.getLocation()); |
| 406 | } |
| 407 | } |
| 408 | |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 409 | llvm_unreachable("Unhandled parsed template argument"); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 410 | return TemplateArgumentLoc(); |
| 411 | } |
| 412 | |
| 413 | /// \brief Translates template arguments as provided by the parser |
| 414 | /// into template arguments used by semantic analysis. |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 415 | void Sema::translateTemplateArguments(const ASTTemplateArgsPtr &TemplateArgsIn, |
| 416 | TemplateArgumentListInfo &TemplateArgs) { |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 417 | for (unsigned I = 0, Last = TemplateArgsIn.size(); I != Last; ++I) |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 418 | TemplateArgs.addArgument(translateTemplateArgument(*this, |
| 419 | TemplateArgsIn[I])); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 420 | } |
| 421 | |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 422 | /// ActOnTypeParameter - Called when a C++ template type parameter |
| 423 | /// (e.g., "typename T") has been parsed. Typename specifies whether |
| 424 | /// the keyword "typename" was used to declare the type parameter |
| 425 | /// (otherwise, "class" was used), and KeyLoc is the location of the |
| 426 | /// "class" or "typename" keyword. ParamName is the name of the |
| 427 | /// parameter (NULL indicates an unnamed template parameter) and |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 428 | /// ParamName is the location of the parameter name (if any). |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 429 | /// If the type parameter has a default argument, it will be added |
| 430 | /// later via ActOnTypeParameterDefault. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 431 | Sema::DeclPtrTy Sema::ActOnTypeParameter(Scope *S, bool Typename, bool Ellipsis, |
Anders Carlsson | 941df7d | 2009-06-12 19:58:00 +0000 | [diff] [blame] | 432 | SourceLocation EllipsisLoc, |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 433 | SourceLocation KeyLoc, |
| 434 | IdentifierInfo *ParamName, |
| 435 | SourceLocation ParamNameLoc, |
| 436 | unsigned Depth, unsigned Position) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 437 | assert(S->isTemplateParamScope() && |
| 438 | "Template type parameter not in template parameter scope!"); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 439 | bool Invalid = false; |
| 440 | |
| 441 | if (ParamName) { |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 442 | NamedDecl *PrevDecl = LookupSingleName(S, ParamName, LookupTagName); |
Douglas Gregor | f57172b | 2008-12-08 18:40:42 +0000 | [diff] [blame] | 443 | if (PrevDecl && PrevDecl->isTemplateParameter()) |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 444 | Invalid = Invalid || DiagnoseTemplateParameterShadow(ParamNameLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 445 | PrevDecl); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 446 | } |
| 447 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 448 | SourceLocation Loc = ParamNameLoc; |
| 449 | if (!ParamName) |
| 450 | Loc = KeyLoc; |
| 451 | |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 452 | TemplateTypeParmDecl *Param |
John McCall | 7a9813c | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 453 | = TemplateTypeParmDecl::Create(Context, Context.getTranslationUnitDecl(), |
| 454 | Loc, Depth, Position, ParamName, Typename, |
Anders Carlsson | 6d845ae | 2009-06-12 22:23:22 +0000 | [diff] [blame] | 455 | Ellipsis); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 456 | if (Invalid) |
| 457 | Param->setInvalidDecl(); |
| 458 | |
| 459 | if (ParamName) { |
| 460 | // Add the template parameter into the current scope. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 461 | S->AddDecl(DeclPtrTy::make(Param)); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 462 | IdResolver.AddDecl(Param); |
| 463 | } |
| 464 | |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 465 | return DeclPtrTy::make(Param); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 466 | } |
| 467 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 468 | /// ActOnTypeParameterDefault - Adds a default argument (the type |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 469 | /// Default) to the given template type parameter (TypeParam). |
| 470 | void Sema::ActOnTypeParameterDefault(DeclPtrTy TypeParam, |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 471 | SourceLocation EqualLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 472 | SourceLocation DefaultLoc, |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 473 | TypeTy *DefaultT) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 474 | TemplateTypeParmDecl *Parm |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 475 | = cast<TemplateTypeParmDecl>(TypeParam.getAs<Decl>()); |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 476 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 477 | TypeSourceInfo *DefaultTInfo; |
| 478 | GetTypeFromParser(DefaultT, &DefaultTInfo); |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 479 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 480 | assert(DefaultTInfo && "expected source information for type"); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 481 | |
Anders Carlsson | 9c4c5c8 | 2009-06-12 22:30:13 +0000 | [diff] [blame] | 482 | // C++0x [temp.param]p9: |
| 483 | // A default template-argument may be specified for any kind of |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 484 | // template-parameter that is not a template parameter pack. |
Anders Carlsson | 9c4c5c8 | 2009-06-12 22:30:13 +0000 | [diff] [blame] | 485 | if (Parm->isParameterPack()) { |
| 486 | Diag(DefaultLoc, diag::err_template_param_pack_default_arg); |
Anders Carlsson | 9c4c5c8 | 2009-06-12 22:30:13 +0000 | [diff] [blame] | 487 | return; |
| 488 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 489 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 490 | // C++ [temp.param]p14: |
| 491 | // A template-parameter shall not be used in its own default argument. |
| 492 | // FIXME: Implement this check! Needs a recursive walk over the types. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 493 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 494 | // Check the template argument itself. |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 495 | if (CheckTemplateArgument(Parm, DefaultTInfo)) { |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 496 | Parm->setInvalidDecl(); |
| 497 | return; |
| 498 | } |
| 499 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 500 | Parm->setDefaultArgument(DefaultTInfo, false); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 501 | } |
| 502 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 503 | /// \brief Check that the type of a non-type template parameter is |
| 504 | /// well-formed. |
| 505 | /// |
| 506 | /// \returns the (possibly-promoted) parameter type if valid; |
| 507 | /// otherwise, produces a diagnostic and returns a NULL type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 508 | QualType |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 509 | Sema::CheckNonTypeTemplateParameterType(QualType T, SourceLocation Loc) { |
| 510 | // C++ [temp.param]p4: |
| 511 | // |
| 512 | // A non-type template-parameter shall have one of the following |
| 513 | // (optionally cv-qualified) types: |
| 514 | // |
| 515 | // -- integral or enumeration type, |
| 516 | if (T->isIntegralType() || T->isEnumeralType() || |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 517 | // -- pointer to object or pointer to function, |
| 518 | (T->isPointerType() && |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 519 | (T->getAs<PointerType>()->getPointeeType()->isObjectType() || |
| 520 | T->getAs<PointerType>()->getPointeeType()->isFunctionType())) || |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 521 | // -- reference to object or reference to function, |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 522 | T->isReferenceType() || |
| 523 | // -- pointer to member. |
| 524 | T->isMemberPointerType() || |
| 525 | // If T is a dependent type, we can't do the check now, so we |
| 526 | // assume that it is well-formed. |
| 527 | T->isDependentType()) |
| 528 | return T; |
| 529 | // C++ [temp.param]p8: |
| 530 | // |
| 531 | // A non-type template-parameter of type "array of T" or |
| 532 | // "function returning T" is adjusted to be of type "pointer to |
| 533 | // T" or "pointer to function returning T", respectively. |
| 534 | else if (T->isArrayType()) |
| 535 | // FIXME: Keep the type prior to promotion? |
| 536 | return Context.getArrayDecayedType(T); |
| 537 | else if (T->isFunctionType()) |
| 538 | // FIXME: Keep the type prior to promotion? |
| 539 | return Context.getPointerType(T); |
| 540 | |
| 541 | Diag(Loc, diag::err_template_nontype_parm_bad_type) |
| 542 | << T; |
| 543 | |
| 544 | return QualType(); |
| 545 | } |
| 546 | |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 547 | /// ActOnNonTypeTemplateParameter - Called when a C++ non-type |
| 548 | /// template parameter (e.g., "int Size" in "template<int Size> |
| 549 | /// class Array") has been parsed. S is the current scope and D is |
| 550 | /// the parsed declarator. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 551 | Sema::DeclPtrTy Sema::ActOnNonTypeTemplateParameter(Scope *S, Declarator &D, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 552 | unsigned Depth, |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 553 | unsigned Position) { |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 554 | TypeSourceInfo *TInfo = 0; |
| 555 | QualType T = GetTypeForDeclarator(D, S, &TInfo); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 556 | |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 557 | assert(S->isTemplateParamScope() && |
| 558 | "Non-type template parameter not in template parameter scope!"); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 559 | bool Invalid = false; |
| 560 | |
| 561 | IdentifierInfo *ParamName = D.getIdentifier(); |
| 562 | if (ParamName) { |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 563 | NamedDecl *PrevDecl = LookupSingleName(S, ParamName, LookupTagName); |
Douglas Gregor | f57172b | 2008-12-08 18:40:42 +0000 | [diff] [blame] | 564 | if (PrevDecl && PrevDecl->isTemplateParameter()) |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 565 | Invalid = Invalid || DiagnoseTemplateParameterShadow(D.getIdentifierLoc(), |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 566 | PrevDecl); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 567 | } |
| 568 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 569 | T = CheckNonTypeTemplateParameterType(T, D.getIdentifierLoc()); |
Douglas Gregor | ceef30c | 2009-03-09 16:46:39 +0000 | [diff] [blame] | 570 | if (T.isNull()) { |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 571 | T = Context.IntTy; // Recover with an 'int' type. |
Douglas Gregor | ceef30c | 2009-03-09 16:46:39 +0000 | [diff] [blame] | 572 | Invalid = true; |
| 573 | } |
Douglas Gregor | 5d290d5 | 2009-02-10 17:43:50 +0000 | [diff] [blame] | 574 | |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 575 | NonTypeTemplateParmDecl *Param |
John McCall | 7a9813c | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 576 | = NonTypeTemplateParmDecl::Create(Context, Context.getTranslationUnitDecl(), |
| 577 | D.getIdentifierLoc(), |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 578 | Depth, Position, ParamName, T, TInfo); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 579 | if (Invalid) |
| 580 | Param->setInvalidDecl(); |
| 581 | |
| 582 | if (D.getIdentifier()) { |
| 583 | // Add the template parameter into the current scope. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 584 | S->AddDecl(DeclPtrTy::make(Param)); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 585 | IdResolver.AddDecl(Param); |
| 586 | } |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 587 | return DeclPtrTy::make(Param); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 588 | } |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 589 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 590 | /// \brief Adds a default argument to the given non-type template |
| 591 | /// parameter. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 592 | void Sema::ActOnNonTypeTemplateParameterDefault(DeclPtrTy TemplateParamD, |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 593 | SourceLocation EqualLoc, |
| 594 | ExprArg DefaultE) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 595 | NonTypeTemplateParmDecl *TemplateParm |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 596 | = cast<NonTypeTemplateParmDecl>(TemplateParamD.getAs<Decl>()); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 597 | Expr *Default = static_cast<Expr *>(DefaultE.get()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 598 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 599 | // C++ [temp.param]p14: |
| 600 | // A template-parameter shall not be used in its own default argument. |
| 601 | // FIXME: Implement this check! Needs a recursive walk over the types. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 602 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 603 | // Check the well-formedness of the default template argument. |
Douglas Gregor | 02cbbd2 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 604 | TemplateArgument Converted; |
| 605 | if (CheckTemplateArgument(TemplateParm, TemplateParm->getType(), Default, |
| 606 | Converted)) { |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 607 | TemplateParm->setInvalidDecl(); |
| 608 | return; |
| 609 | } |
| 610 | |
Anders Carlsson | e9146f2 | 2009-05-01 19:49:17 +0000 | [diff] [blame] | 611 | TemplateParm->setDefaultArgument(DefaultE.takeAs<Expr>()); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 612 | } |
| 613 | |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 614 | |
| 615 | /// ActOnTemplateTemplateParameter - Called when a C++ template template |
| 616 | /// parameter (e.g. T in template <template <typename> class T> class array) |
| 617 | /// has been parsed. S is the current scope. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 618 | Sema::DeclPtrTy Sema::ActOnTemplateTemplateParameter(Scope* S, |
| 619 | SourceLocation TmpLoc, |
| 620 | TemplateParamsTy *Params, |
| 621 | IdentifierInfo *Name, |
| 622 | SourceLocation NameLoc, |
| 623 | unsigned Depth, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 624 | unsigned Position) { |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 625 | assert(S->isTemplateParamScope() && |
| 626 | "Template template parameter not in template parameter scope!"); |
| 627 | |
| 628 | // Construct the parameter object. |
| 629 | TemplateTemplateParmDecl *Param = |
John McCall | 7a9813c | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 630 | TemplateTemplateParmDecl::Create(Context, Context.getTranslationUnitDecl(), |
| 631 | TmpLoc, Depth, Position, Name, |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 632 | (TemplateParameterList*)Params); |
| 633 | |
| 634 | // Make sure the parameter is valid. |
| 635 | // FIXME: Decl object is not currently invalidated anywhere so this doesn't |
| 636 | // do anything yet. However, if the template parameter list or (eventual) |
| 637 | // default value is ever invalidated, that will propagate here. |
| 638 | bool Invalid = false; |
| 639 | if (Invalid) { |
| 640 | Param->setInvalidDecl(); |
| 641 | } |
| 642 | |
| 643 | // If the tt-param has a name, then link the identifier into the scope |
| 644 | // and lookup mechanisms. |
| 645 | if (Name) { |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 646 | S->AddDecl(DeclPtrTy::make(Param)); |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 647 | IdResolver.AddDecl(Param); |
| 648 | } |
| 649 | |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 650 | return DeclPtrTy::make(Param); |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 651 | } |
| 652 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 653 | /// \brief Adds a default argument to the given template template |
| 654 | /// parameter. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 655 | void Sema::ActOnTemplateTemplateParameterDefault(DeclPtrTy TemplateParamD, |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 656 | SourceLocation EqualLoc, |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 657 | const ParsedTemplateArgument &Default) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 658 | TemplateTemplateParmDecl *TemplateParm |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 659 | = cast<TemplateTemplateParmDecl>(TemplateParamD.getAs<Decl>()); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 660 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 661 | // C++ [temp.param]p14: |
| 662 | // A template-parameter shall not be used in its own default argument. |
| 663 | // FIXME: Implement this check! Needs a recursive walk over the types. |
| 664 | |
Douglas Gregor | 9148c3f | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 665 | // Check only that we have a template template argument. We don't want to |
| 666 | // try to check well-formedness now, because our template template parameter |
| 667 | // might have dependent types in its template parameters, which we wouldn't |
| 668 | // be able to match now. |
| 669 | // |
| 670 | // If none of the template template parameter's template arguments mention |
| 671 | // other template parameters, we could actually perform more checking here. |
| 672 | // However, it isn't worth doing. |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 673 | TemplateArgumentLoc DefaultArg = translateTemplateArgument(*this, Default); |
Douglas Gregor | 9148c3f | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 674 | if (DefaultArg.getArgument().getAsTemplate().isNull()) { |
| 675 | Diag(DefaultArg.getLocation(), diag::err_template_arg_not_class_template) |
| 676 | << DefaultArg.getSourceRange(); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 677 | return; |
| 678 | } |
Douglas Gregor | 9148c3f | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 679 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 680 | TemplateParm->setDefaultArgument(DefaultArg); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 681 | } |
| 682 | |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 683 | /// ActOnTemplateParameterList - Builds a TemplateParameterList that |
| 684 | /// contains the template parameters in Params/NumParams. |
| 685 | Sema::TemplateParamsTy * |
| 686 | Sema::ActOnTemplateParameterList(unsigned Depth, |
| 687 | SourceLocation ExportLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 688 | SourceLocation TemplateLoc, |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 689 | SourceLocation LAngleLoc, |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 690 | DeclPtrTy *Params, unsigned NumParams, |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 691 | SourceLocation RAngleLoc) { |
| 692 | if (ExportLoc.isValid()) |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 693 | Diag(ExportLoc, diag::warn_template_export_unsupported); |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 694 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 695 | return TemplateParameterList::Create(Context, TemplateLoc, LAngleLoc, |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 696 | (NamedDecl**)Params, NumParams, |
| 697 | RAngleLoc); |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 698 | } |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 699 | |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 700 | static void SetNestedNameSpecifier(TagDecl *T, const CXXScopeSpec &SS) { |
| 701 | if (SS.isSet()) |
| 702 | T->setQualifierInfo(static_cast<NestedNameSpecifier*>(SS.getScopeRep()), |
| 703 | SS.getRange()); |
| 704 | } |
| 705 | |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 706 | Sema::DeclResult |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 707 | Sema::CheckClassTemplate(Scope *S, unsigned TagSpec, TagUseKind TUK, |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 708 | SourceLocation KWLoc, const CXXScopeSpec &SS, |
| 709 | IdentifierInfo *Name, SourceLocation NameLoc, |
| 710 | AttributeList *Attr, |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 711 | TemplateParameterList *TemplateParams, |
Anders Carlsson | 5aeccdb | 2009-03-26 00:52:18 +0000 | [diff] [blame] | 712 | AccessSpecifier AS) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 713 | assert(TemplateParams && TemplateParams->size() > 0 && |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 714 | "No template parameters"); |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 715 | assert(TUK != TUK_Reference && "Can only declare or define class templates"); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 716 | bool Invalid = false; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 717 | |
| 718 | // Check that we can declare a template here. |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 719 | if (CheckTemplateDeclScope(S, TemplateParams)) |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 720 | return true; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 721 | |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 722 | TagDecl::TagKind Kind = TagDecl::getTagKindForTypeSpec(TagSpec); |
| 723 | assert(Kind != TagDecl::TK_enum && "can't build template of enumerated type"); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 724 | |
| 725 | // There is no such thing as an unnamed class template. |
| 726 | if (!Name) { |
| 727 | Diag(KWLoc, diag::err_template_unnamed_class); |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 728 | return true; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 729 | } |
| 730 | |
| 731 | // Find any previous declaration with this name. |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 732 | DeclContext *SemanticContext; |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 733 | LookupResult Previous(*this, Name, NameLoc, LookupOrdinaryName, |
John McCall | 7d384dd | 2009-11-18 07:57:50 +0000 | [diff] [blame] | 734 | ForRedeclaration); |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 735 | if (SS.isNotEmpty() && !SS.isInvalid()) { |
Douglas Gregor | f0510d4 | 2009-10-12 23:11:44 +0000 | [diff] [blame] | 736 | if (RequireCompleteDeclContext(SS)) |
| 737 | return true; |
| 738 | |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 739 | SemanticContext = computeDeclContext(SS, true); |
| 740 | if (!SemanticContext) { |
| 741 | // FIXME: Produce a reasonable diagnostic here |
| 742 | return true; |
| 743 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 744 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 745 | LookupQualifiedName(Previous, SemanticContext); |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 746 | } else { |
| 747 | SemanticContext = CurContext; |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 748 | LookupName(Previous, S); |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 749 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 750 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 751 | assert(!Previous.isAmbiguous() && "Ambiguity in class template redecl?"); |
| 752 | NamedDecl *PrevDecl = 0; |
| 753 | if (Previous.begin() != Previous.end()) |
| 754 | PrevDecl = *Previous.begin(); |
| 755 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 756 | // If there is a previous declaration with the same name, check |
| 757 | // whether this is a valid redeclaration. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 758 | ClassTemplateDecl *PrevClassTemplate |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 759 | = dyn_cast_or_null<ClassTemplateDecl>(PrevDecl); |
Douglas Gregor | d7e5bdb | 2009-10-09 21:11:42 +0000 | [diff] [blame] | 760 | |
| 761 | // We may have found the injected-class-name of a class template, |
| 762 | // class template partial specialization, or class template specialization. |
| 763 | // In these cases, grab the template that is being defined or specialized. |
| 764 | if (!PrevClassTemplate && PrevDecl && isa<CXXRecordDecl>(PrevDecl) && |
| 765 | cast<CXXRecordDecl>(PrevDecl)->isInjectedClassName()) { |
| 766 | PrevDecl = cast<CXXRecordDecl>(PrevDecl->getDeclContext()); |
| 767 | PrevClassTemplate |
| 768 | = cast<CXXRecordDecl>(PrevDecl)->getDescribedClassTemplate(); |
| 769 | if (!PrevClassTemplate && isa<ClassTemplateSpecializationDecl>(PrevDecl)) { |
| 770 | PrevClassTemplate |
| 771 | = cast<ClassTemplateSpecializationDecl>(PrevDecl) |
| 772 | ->getSpecializedTemplate(); |
| 773 | } |
| 774 | } |
| 775 | |
John McCall | 65c4946 | 2009-12-18 11:25:59 +0000 | [diff] [blame] | 776 | if (TUK == TUK_Friend) { |
John McCall | e129d44 | 2009-12-17 23:21:11 +0000 | [diff] [blame] | 777 | // C++ [namespace.memdef]p3: |
| 778 | // [...] When looking for a prior declaration of a class or a function |
| 779 | // declared as a friend, and when the name of the friend class or |
| 780 | // function is neither a qualified name nor a template-id, scopes outside |
| 781 | // the innermost enclosing namespace scope are not considered. |
| 782 | DeclContext *OutermostContext = CurContext; |
| 783 | while (!OutermostContext->isFileContext()) |
| 784 | OutermostContext = OutermostContext->getLookupParent(); |
John McCall | 65c4946 | 2009-12-18 11:25:59 +0000 | [diff] [blame] | 785 | |
| 786 | if (PrevDecl && |
| 787 | (OutermostContext->Equals(PrevDecl->getDeclContext()) || |
| 788 | OutermostContext->Encloses(PrevDecl->getDeclContext()))) { |
John McCall | e129d44 | 2009-12-17 23:21:11 +0000 | [diff] [blame] | 789 | SemanticContext = PrevDecl->getDeclContext(); |
| 790 | } else { |
| 791 | // Declarations in outer scopes don't matter. However, the outermost |
| 792 | // context we computed is the semantic context for our new |
| 793 | // declaration. |
| 794 | PrevDecl = PrevClassTemplate = 0; |
| 795 | SemanticContext = OutermostContext; |
| 796 | } |
| 797 | |
| 798 | if (CurContext->isDependentContext()) { |
| 799 | // If this is a dependent context, we don't want to link the friend |
| 800 | // class template to the template in scope, because that would perform |
| 801 | // checking of the template parameter lists that can't be performed |
| 802 | // until the outer context is instantiated. |
| 803 | PrevDecl = PrevClassTemplate = 0; |
| 804 | } |
| 805 | } else if (PrevDecl && !isDeclInScope(PrevDecl, SemanticContext, S)) |
| 806 | PrevDecl = PrevClassTemplate = 0; |
| 807 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 808 | if (PrevClassTemplate) { |
| 809 | // Ensure that the template parameter lists are compatible. |
| 810 | if (!TemplateParameterListsAreEqual(TemplateParams, |
| 811 | PrevClassTemplate->getTemplateParameters(), |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 812 | /*Complain=*/true, |
| 813 | TPL_TemplateMatch)) |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 814 | return true; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 815 | |
| 816 | // C++ [temp.class]p4: |
| 817 | // In a redeclaration, partial specialization, explicit |
| 818 | // specialization or explicit instantiation of a class template, |
| 819 | // the class-key shall agree in kind with the original class |
| 820 | // template declaration (7.1.5.3). |
| 821 | RecordDecl *PrevRecordDecl = PrevClassTemplate->getTemplatedDecl(); |
Douglas Gregor | 501c5ce | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 822 | if (!isAcceptableTagRedeclaration(PrevRecordDecl, Kind, KWLoc, *Name)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 823 | Diag(KWLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 824 | << Name |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 825 | << FixItHint::CreateReplacement(KWLoc, PrevRecordDecl->getKindName()); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 826 | Diag(PrevRecordDecl->getLocation(), diag::note_previous_use); |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 827 | Kind = PrevRecordDecl->getTagKind(); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 828 | } |
| 829 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 830 | // Check for redefinition of this class template. |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 831 | if (TUK == TUK_Definition) { |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 832 | if (TagDecl *Def = PrevRecordDecl->getDefinition()) { |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 833 | Diag(NameLoc, diag::err_redefinition) << Name; |
| 834 | Diag(Def->getLocation(), diag::note_previous_definition); |
| 835 | // FIXME: Would it make sense to try to "forget" the previous |
| 836 | // definition, as part of error recovery? |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 837 | return true; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 838 | } |
| 839 | } |
| 840 | } else if (PrevDecl && PrevDecl->isTemplateParameter()) { |
| 841 | // Maybe we will complain about the shadowed template parameter. |
| 842 | DiagnoseTemplateParameterShadow(NameLoc, PrevDecl); |
| 843 | // Just pretend that we didn't see the previous declaration. |
| 844 | PrevDecl = 0; |
| 845 | } else if (PrevDecl) { |
| 846 | // C++ [temp]p5: |
| 847 | // A class template shall not have the same name as any other |
| 848 | // template, class, function, object, enumeration, enumerator, |
| 849 | // namespace, or type in the same scope (3.3), except as specified |
| 850 | // in (14.5.4). |
| 851 | Diag(NameLoc, diag::err_redefinition_different_kind) << Name; |
| 852 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 853 | return true; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 854 | } |
| 855 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 856 | // Check the template parameter list of this declaration, possibly |
| 857 | // merging in the template parameter list from the previous class |
| 858 | // template declaration. |
| 859 | if (CheckTemplateParameterList(TemplateParams, |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 860 | PrevClassTemplate? PrevClassTemplate->getTemplateParameters() : 0, |
| 861 | TPC_ClassTemplate)) |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 862 | Invalid = true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 863 | |
Douglas Gregor | 7da97d0 | 2009-05-10 22:57:19 +0000 | [diff] [blame] | 864 | // 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] | 865 | // declaration! |
| 866 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 867 | CXXRecordDecl *NewClass = |
Douglas Gregor | 741dd9a | 2009-07-21 14:46:17 +0000 | [diff] [blame] | 868 | CXXRecordDecl::Create(Context, Kind, SemanticContext, NameLoc, Name, KWLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 869 | PrevClassTemplate? |
Douglas Gregor | aafc0cc | 2009-05-15 19:11:46 +0000 | [diff] [blame] | 870 | PrevClassTemplate->getTemplatedDecl() : 0, |
| 871 | /*DelayTypeCreation=*/true); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 872 | SetNestedNameSpecifier(NewClass, SS); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 873 | |
| 874 | ClassTemplateDecl *NewTemplate |
| 875 | = ClassTemplateDecl::Create(Context, SemanticContext, NameLoc, |
| 876 | DeclarationName(Name), TemplateParams, |
Douglas Gregor | 5953d8b | 2009-03-19 17:26:29 +0000 | [diff] [blame] | 877 | NewClass, PrevClassTemplate); |
Douglas Gregor | befc20e | 2009-03-26 00:10:35 +0000 | [diff] [blame] | 878 | NewClass->setDescribedClassTemplate(NewTemplate); |
| 879 | |
Douglas Gregor | aafc0cc | 2009-05-15 19:11:46 +0000 | [diff] [blame] | 880 | // Build the type for the class template declaration now. |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 881 | QualType T = NewTemplate->getInjectedClassNameSpecialization(Context); |
| 882 | T = Context.getInjectedClassNameType(NewClass, T); |
Douglas Gregor | aafc0cc | 2009-05-15 19:11:46 +0000 | [diff] [blame] | 883 | assert(T->isDependentType() && "Class template type is not dependent?"); |
| 884 | (void)T; |
| 885 | |
Douglas Gregor | fd056bc | 2009-10-13 16:30:37 +0000 | [diff] [blame] | 886 | // If we are providing an explicit specialization of a member that is a |
| 887 | // class template, make a note of that. |
| 888 | if (PrevClassTemplate && |
| 889 | PrevClassTemplate->getInstantiatedFromMemberTemplate()) |
| 890 | PrevClassTemplate->setMemberSpecialization(); |
| 891 | |
Anders Carlsson | 4cbe82c | 2009-03-26 01:24:28 +0000 | [diff] [blame] | 892 | // Set the access specifier. |
Douglas Gregor | d85bea2 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 893 | if (!Invalid && TUK != TUK_Friend) |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 894 | SetMemberAccessSpecifier(NewTemplate, PrevClassTemplate, AS); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 895 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 896 | // Set the lexical context of these templates |
| 897 | NewClass->setLexicalDeclContext(CurContext); |
| 898 | NewTemplate->setLexicalDeclContext(CurContext); |
| 899 | |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 900 | if (TUK == TUK_Definition) |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 901 | NewClass->startDefinition(); |
| 902 | |
| 903 | if (Attr) |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 904 | ProcessDeclAttributeList(S, NewClass, Attr); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 905 | |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 906 | if (TUK != TUK_Friend) |
| 907 | PushOnScopeChains(NewTemplate, S); |
| 908 | else { |
Douglas Gregor | d85bea2 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 909 | if (PrevClassTemplate && PrevClassTemplate->getAccess() != AS_none) { |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 910 | NewTemplate->setAccess(PrevClassTemplate->getAccess()); |
Douglas Gregor | d85bea2 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 911 | NewClass->setAccess(PrevClassTemplate->getAccess()); |
| 912 | } |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 913 | |
Douglas Gregor | d85bea2 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 914 | NewTemplate->setObjectOfFriendDecl(/* PreviouslyDeclared = */ |
| 915 | PrevClassTemplate != NULL); |
| 916 | |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 917 | // Friend templates are visible in fairly strange ways. |
| 918 | if (!CurContext->isDependentContext()) { |
| 919 | DeclContext *DC = SemanticContext->getLookupContext(); |
| 920 | DC->makeDeclVisibleInContext(NewTemplate, /* Recoverable = */ false); |
| 921 | if (Scope *EnclosingScope = getScopeForDeclContext(S, DC)) |
| 922 | PushOnScopeChains(NewTemplate, EnclosingScope, |
| 923 | /* AddToContext = */ false); |
| 924 | } |
Douglas Gregor | d85bea2 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 925 | |
| 926 | FriendDecl *Friend = FriendDecl::Create(Context, CurContext, |
| 927 | NewClass->getLocation(), |
| 928 | NewTemplate, |
| 929 | /*FIXME:*/NewClass->getLocation()); |
| 930 | Friend->setAccess(AS_public); |
| 931 | CurContext->addDecl(Friend); |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 932 | } |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 933 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 934 | if (Invalid) { |
| 935 | NewTemplate->setInvalidDecl(); |
| 936 | NewClass->setInvalidDecl(); |
| 937 | } |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 938 | return DeclPtrTy::make(NewTemplate); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 939 | } |
| 940 | |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 941 | /// \brief Diagnose the presence of a default template argument on a |
| 942 | /// template parameter, which is ill-formed in certain contexts. |
| 943 | /// |
| 944 | /// \returns true if the default template argument should be dropped. |
| 945 | static bool DiagnoseDefaultTemplateArgument(Sema &S, |
| 946 | Sema::TemplateParamListContext TPC, |
| 947 | SourceLocation ParamLoc, |
| 948 | SourceRange DefArgRange) { |
| 949 | switch (TPC) { |
| 950 | case Sema::TPC_ClassTemplate: |
| 951 | return false; |
| 952 | |
| 953 | case Sema::TPC_FunctionTemplate: |
| 954 | // C++ [temp.param]p9: |
| 955 | // A default template-argument shall not be specified in a |
| 956 | // function template declaration or a function template |
| 957 | // definition [...] |
| 958 | // (This sentence is not in C++0x, per DR226). |
| 959 | if (!S.getLangOptions().CPlusPlus0x) |
| 960 | S.Diag(ParamLoc, |
| 961 | diag::err_template_parameter_default_in_function_template) |
| 962 | << DefArgRange; |
| 963 | return false; |
| 964 | |
| 965 | case Sema::TPC_ClassTemplateMember: |
| 966 | // C++0x [temp.param]p9: |
| 967 | // A default template-argument shall not be specified in the |
| 968 | // template-parameter-lists of the definition of a member of a |
| 969 | // class template that appears outside of the member's class. |
| 970 | S.Diag(ParamLoc, diag::err_template_parameter_default_template_member) |
| 971 | << DefArgRange; |
| 972 | return true; |
| 973 | |
| 974 | case Sema::TPC_FriendFunctionTemplate: |
| 975 | // C++ [temp.param]p9: |
| 976 | // A default template-argument shall not be specified in a |
| 977 | // friend template declaration. |
| 978 | S.Diag(ParamLoc, diag::err_template_parameter_default_friend_template) |
| 979 | << DefArgRange; |
| 980 | return true; |
| 981 | |
| 982 | // FIXME: C++0x [temp.param]p9 allows default template-arguments |
| 983 | // for friend function templates if there is only a single |
| 984 | // declaration (and it is a definition). Strange! |
| 985 | } |
| 986 | |
| 987 | return false; |
| 988 | } |
| 989 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 990 | /// \brief Checks the validity of a template parameter list, possibly |
| 991 | /// considering the template parameter list from a previous |
| 992 | /// declaration. |
| 993 | /// |
| 994 | /// If an "old" template parameter list is provided, it must be |
| 995 | /// equivalent (per TemplateParameterListsAreEqual) to the "new" |
| 996 | /// template parameter list. |
| 997 | /// |
| 998 | /// \param NewParams Template parameter list for a new template |
| 999 | /// declaration. This template parameter list will be updated with any |
| 1000 | /// default arguments that are carried through from the previous |
| 1001 | /// template parameter list. |
| 1002 | /// |
| 1003 | /// \param OldParams If provided, template parameter list from a |
| 1004 | /// previous declaration of the same template. Default template |
| 1005 | /// arguments will be merged from the old template parameter list to |
| 1006 | /// the new template parameter list. |
| 1007 | /// |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1008 | /// \param TPC Describes the context in which we are checking the given |
| 1009 | /// template parameter list. |
| 1010 | /// |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1011 | /// \returns true if an error occurred, false otherwise. |
| 1012 | bool Sema::CheckTemplateParameterList(TemplateParameterList *NewParams, |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1013 | TemplateParameterList *OldParams, |
| 1014 | TemplateParamListContext TPC) { |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1015 | bool Invalid = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1016 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1017 | // C++ [temp.param]p10: |
| 1018 | // The set of default template-arguments available for use with a |
| 1019 | // template declaration or definition is obtained by merging the |
| 1020 | // default arguments from the definition (if in scope) and all |
| 1021 | // declarations in scope in the same way default function |
| 1022 | // arguments are (8.3.6). |
| 1023 | bool SawDefaultArgument = false; |
| 1024 | SourceLocation PreviousDefaultArgLoc; |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 1025 | |
Anders Carlsson | 49d2557 | 2009-06-12 23:20:15 +0000 | [diff] [blame] | 1026 | bool SawParameterPack = false; |
| 1027 | SourceLocation ParameterPackLoc; |
| 1028 | |
Mike Stump | 1a35fde | 2009-02-11 23:03:27 +0000 | [diff] [blame] | 1029 | // Dummy initialization to avoid warnings. |
Douglas Gregor | 1bc6913 | 2009-02-11 20:46:19 +0000 | [diff] [blame] | 1030 | TemplateParameterList::iterator OldParam = NewParams->end(); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1031 | if (OldParams) |
| 1032 | OldParam = OldParams->begin(); |
| 1033 | |
| 1034 | for (TemplateParameterList::iterator NewParam = NewParams->begin(), |
| 1035 | NewParamEnd = NewParams->end(); |
| 1036 | NewParam != NewParamEnd; ++NewParam) { |
| 1037 | // Variables used to diagnose redundant default arguments |
| 1038 | bool RedundantDefaultArg = false; |
| 1039 | SourceLocation OldDefaultLoc; |
| 1040 | SourceLocation NewDefaultLoc; |
| 1041 | |
| 1042 | // Variables used to diagnose missing default arguments |
| 1043 | bool MissingDefaultArg = false; |
| 1044 | |
Anders Carlsson | 49d2557 | 2009-06-12 23:20:15 +0000 | [diff] [blame] | 1045 | // C++0x [temp.param]p11: |
| 1046 | // If a template parameter of a class template is a template parameter pack, |
| 1047 | // it must be the last template parameter. |
| 1048 | if (SawParameterPack) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1049 | Diag(ParameterPackLoc, |
Anders Carlsson | 49d2557 | 2009-06-12 23:20:15 +0000 | [diff] [blame] | 1050 | diag::err_template_param_pack_must_be_last_template_parameter); |
| 1051 | Invalid = true; |
| 1052 | } |
| 1053 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1054 | if (TemplateTypeParmDecl *NewTypeParm |
| 1055 | = dyn_cast<TemplateTypeParmDecl>(*NewParam)) { |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1056 | // Check the presence of a default argument here. |
| 1057 | if (NewTypeParm->hasDefaultArgument() && |
| 1058 | DiagnoseDefaultTemplateArgument(*this, TPC, |
| 1059 | NewTypeParm->getLocation(), |
| 1060 | NewTypeParm->getDefaultArgumentInfo()->getTypeLoc() |
| 1061 | .getFullSourceRange())) |
| 1062 | NewTypeParm->removeDefaultArgument(); |
| 1063 | |
| 1064 | // Merge default arguments for template type parameters. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1065 | TemplateTypeParmDecl *OldTypeParm |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1066 | = OldParams? cast<TemplateTypeParmDecl>(*OldParam) : 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1067 | |
Anders Carlsson | 49d2557 | 2009-06-12 23:20:15 +0000 | [diff] [blame] | 1068 | if (NewTypeParm->isParameterPack()) { |
| 1069 | assert(!NewTypeParm->hasDefaultArgument() && |
| 1070 | "Parameter packs can't have a default argument!"); |
| 1071 | SawParameterPack = true; |
| 1072 | ParameterPackLoc = NewTypeParm->getLocation(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1073 | } else if (OldTypeParm && OldTypeParm->hasDefaultArgument() && |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1074 | NewTypeParm->hasDefaultArgument()) { |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1075 | OldDefaultLoc = OldTypeParm->getDefaultArgumentLoc(); |
| 1076 | NewDefaultLoc = NewTypeParm->getDefaultArgumentLoc(); |
| 1077 | SawDefaultArgument = true; |
| 1078 | RedundantDefaultArg = true; |
| 1079 | PreviousDefaultArgLoc = NewDefaultLoc; |
| 1080 | } else if (OldTypeParm && OldTypeParm->hasDefaultArgument()) { |
| 1081 | // Merge the default argument from the old declaration to the |
| 1082 | // new declaration. |
| 1083 | SawDefaultArgument = true; |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1084 | NewTypeParm->setDefaultArgument(OldTypeParm->getDefaultArgumentInfo(), |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1085 | true); |
| 1086 | PreviousDefaultArgLoc = OldTypeParm->getDefaultArgumentLoc(); |
| 1087 | } else if (NewTypeParm->hasDefaultArgument()) { |
| 1088 | SawDefaultArgument = true; |
| 1089 | PreviousDefaultArgLoc = NewTypeParm->getDefaultArgumentLoc(); |
| 1090 | } else if (SawDefaultArgument) |
| 1091 | MissingDefaultArg = true; |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1092 | } else if (NonTypeTemplateParmDecl *NewNonTypeParm |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1093 | = dyn_cast<NonTypeTemplateParmDecl>(*NewParam)) { |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1094 | // Check the presence of a default argument here. |
| 1095 | if (NewNonTypeParm->hasDefaultArgument() && |
| 1096 | DiagnoseDefaultTemplateArgument(*this, TPC, |
| 1097 | NewNonTypeParm->getLocation(), |
| 1098 | NewNonTypeParm->getDefaultArgument()->getSourceRange())) { |
| 1099 | NewNonTypeParm->getDefaultArgument()->Destroy(Context); |
| 1100 | NewNonTypeParm->setDefaultArgument(0); |
| 1101 | } |
| 1102 | |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1103 | // Merge default arguments for non-type template parameters |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1104 | NonTypeTemplateParmDecl *OldNonTypeParm |
| 1105 | = OldParams? cast<NonTypeTemplateParmDecl>(*OldParam) : 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1106 | if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument() && |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1107 | NewNonTypeParm->hasDefaultArgument()) { |
| 1108 | OldDefaultLoc = OldNonTypeParm->getDefaultArgumentLoc(); |
| 1109 | NewDefaultLoc = NewNonTypeParm->getDefaultArgumentLoc(); |
| 1110 | SawDefaultArgument = true; |
| 1111 | RedundantDefaultArg = true; |
| 1112 | PreviousDefaultArgLoc = NewDefaultLoc; |
| 1113 | } else if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument()) { |
| 1114 | // Merge the default argument from the old declaration to the |
| 1115 | // new declaration. |
| 1116 | SawDefaultArgument = true; |
| 1117 | // FIXME: We need to create a new kind of "default argument" |
| 1118 | // expression that points to a previous template template |
| 1119 | // parameter. |
| 1120 | NewNonTypeParm->setDefaultArgument( |
| 1121 | OldNonTypeParm->getDefaultArgument()); |
| 1122 | PreviousDefaultArgLoc = OldNonTypeParm->getDefaultArgumentLoc(); |
| 1123 | } else if (NewNonTypeParm->hasDefaultArgument()) { |
| 1124 | SawDefaultArgument = true; |
| 1125 | PreviousDefaultArgLoc = NewNonTypeParm->getDefaultArgumentLoc(); |
| 1126 | } else if (SawDefaultArgument) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1127 | MissingDefaultArg = true; |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1128 | } else { |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1129 | // Check the presence of a default argument here. |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1130 | TemplateTemplateParmDecl *NewTemplateParm |
| 1131 | = cast<TemplateTemplateParmDecl>(*NewParam); |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1132 | if (NewTemplateParm->hasDefaultArgument() && |
| 1133 | DiagnoseDefaultTemplateArgument(*this, TPC, |
| 1134 | NewTemplateParm->getLocation(), |
| 1135 | NewTemplateParm->getDefaultArgument().getSourceRange())) |
| 1136 | NewTemplateParm->setDefaultArgument(TemplateArgumentLoc()); |
| 1137 | |
| 1138 | // Merge default arguments for template template parameters |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1139 | TemplateTemplateParmDecl *OldTemplateParm |
| 1140 | = OldParams? cast<TemplateTemplateParmDecl>(*OldParam) : 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1141 | if (OldTemplateParm && OldTemplateParm->hasDefaultArgument() && |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1142 | NewTemplateParm->hasDefaultArgument()) { |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1143 | OldDefaultLoc = OldTemplateParm->getDefaultArgument().getLocation(); |
| 1144 | NewDefaultLoc = NewTemplateParm->getDefaultArgument().getLocation(); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1145 | SawDefaultArgument = true; |
| 1146 | RedundantDefaultArg = true; |
| 1147 | PreviousDefaultArgLoc = NewDefaultLoc; |
| 1148 | } else if (OldTemplateParm && OldTemplateParm->hasDefaultArgument()) { |
| 1149 | // Merge the default argument from the old declaration to the |
| 1150 | // new declaration. |
| 1151 | SawDefaultArgument = true; |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 1152 | // FIXME: We need to create a new kind of "default argument" expression |
| 1153 | // that points to a previous template template parameter. |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1154 | NewTemplateParm->setDefaultArgument( |
| 1155 | OldTemplateParm->getDefaultArgument()); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1156 | PreviousDefaultArgLoc |
| 1157 | = OldTemplateParm->getDefaultArgument().getLocation(); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1158 | } else if (NewTemplateParm->hasDefaultArgument()) { |
| 1159 | SawDefaultArgument = true; |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1160 | PreviousDefaultArgLoc |
| 1161 | = NewTemplateParm->getDefaultArgument().getLocation(); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1162 | } else if (SawDefaultArgument) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1163 | MissingDefaultArg = true; |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1164 | } |
| 1165 | |
| 1166 | if (RedundantDefaultArg) { |
| 1167 | // C++ [temp.param]p12: |
| 1168 | // A template-parameter shall not be given default arguments |
| 1169 | // by two different declarations in the same scope. |
| 1170 | Diag(NewDefaultLoc, diag::err_template_param_default_arg_redefinition); |
| 1171 | Diag(OldDefaultLoc, diag::note_template_param_prev_default_arg); |
| 1172 | Invalid = true; |
| 1173 | } else if (MissingDefaultArg) { |
| 1174 | // C++ [temp.param]p11: |
| 1175 | // If a template-parameter has a default template-argument, |
| 1176 | // all subsequent template-parameters shall have a default |
| 1177 | // template-argument supplied. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1178 | Diag((*NewParam)->getLocation(), |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1179 | diag::err_template_param_default_arg_missing); |
| 1180 | Diag(PreviousDefaultArgLoc, diag::note_template_param_prev_default_arg); |
| 1181 | Invalid = true; |
| 1182 | } |
| 1183 | |
| 1184 | // If we have an old template parameter list that we're merging |
| 1185 | // in, move on to the next parameter. |
| 1186 | if (OldParams) |
| 1187 | ++OldParam; |
| 1188 | } |
| 1189 | |
| 1190 | return Invalid; |
| 1191 | } |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 1192 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1193 | /// \brief Match the given template parameter lists to the given scope |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1194 | /// specifier, returning the template parameter list that applies to the |
| 1195 | /// name. |
| 1196 | /// |
| 1197 | /// \param DeclStartLoc the start of the declaration that has a scope |
| 1198 | /// specifier or a template parameter list. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1199 | /// |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1200 | /// \param SS the scope specifier that will be matched to the given template |
| 1201 | /// parameter lists. This scope specifier precedes a qualified name that is |
| 1202 | /// being declared. |
| 1203 | /// |
| 1204 | /// \param ParamLists the template parameter lists, from the outermost to the |
| 1205 | /// innermost template parameter lists. |
| 1206 | /// |
| 1207 | /// \param NumParamLists the number of template parameter lists in ParamLists. |
| 1208 | /// |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 1209 | /// \param IsExplicitSpecialization will be set true if the entity being |
| 1210 | /// declared is an explicit specialization, false otherwise. |
| 1211 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1212 | /// \returns the template parameter list, if any, that corresponds to the |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1213 | /// name that is preceded by the scope specifier @p SS. This template |
| 1214 | /// parameter list may be have template parameters (if we're declaring a |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1215 | /// template) or may have no template parameters (if we're declaring a |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1216 | /// template specialization), or may be NULL (if we were's declaring isn't |
| 1217 | /// itself a template). |
| 1218 | TemplateParameterList * |
| 1219 | Sema::MatchTemplateParametersToScopeSpecifier(SourceLocation DeclStartLoc, |
| 1220 | const CXXScopeSpec &SS, |
| 1221 | TemplateParameterList **ParamLists, |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 1222 | unsigned NumParamLists, |
| 1223 | bool &IsExplicitSpecialization) { |
| 1224 | IsExplicitSpecialization = false; |
| 1225 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1226 | // Find the template-ids that occur within the nested-name-specifier. These |
| 1227 | // template-ids will match up with the template parameter lists. |
| 1228 | llvm::SmallVector<const TemplateSpecializationType *, 4> |
| 1229 | TemplateIdsInSpecifier; |
Douglas Gregor | 3ebd753 | 2009-11-23 12:11:45 +0000 | [diff] [blame] | 1230 | llvm::SmallVector<ClassTemplateSpecializationDecl *, 4> |
| 1231 | ExplicitSpecializationsInSpecifier; |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1232 | for (NestedNameSpecifier *NNS = (NestedNameSpecifier *)SS.getScopeRep(); |
| 1233 | NNS; NNS = NNS->getPrefix()) { |
John McCall | 4b2b02b | 2009-12-15 02:19:47 +0000 | [diff] [blame] | 1234 | const Type *T = NNS->getAsType(); |
| 1235 | if (!T) break; |
| 1236 | |
| 1237 | // C++0x [temp.expl.spec]p17: |
| 1238 | // A member or a member template may be nested within many |
| 1239 | // enclosing class templates. In an explicit specialization for |
| 1240 | // such a member, the member declaration shall be preceded by a |
| 1241 | // template<> for each enclosing class template that is |
| 1242 | // explicitly specialized. |
Douglas Gregor | fe33106 | 2010-02-13 05:23:25 +0000 | [diff] [blame] | 1243 | // |
| 1244 | // Following the existing practice of GNU and EDG, we allow a typedef of a |
| 1245 | // template specialization type. |
| 1246 | if (const TypedefType *TT = dyn_cast<TypedefType>(T)) |
| 1247 | T = TT->LookThroughTypedefs().getTypePtr(); |
John McCall | 4b2b02b | 2009-12-15 02:19:47 +0000 | [diff] [blame] | 1248 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1249 | if (const TemplateSpecializationType *SpecType |
Douglas Gregor | fe33106 | 2010-02-13 05:23:25 +0000 | [diff] [blame] | 1250 | = dyn_cast<TemplateSpecializationType>(T)) { |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1251 | TemplateDecl *Template = SpecType->getTemplateName().getAsTemplateDecl(); |
| 1252 | if (!Template) |
| 1253 | continue; // FIXME: should this be an error? probably... |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1254 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1255 | if (const RecordType *Record = SpecType->getAs<RecordType>()) { |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1256 | ClassTemplateSpecializationDecl *SpecDecl |
| 1257 | = cast<ClassTemplateSpecializationDecl>(Record->getDecl()); |
| 1258 | // If the nested name specifier refers to an explicit specialization, |
| 1259 | // we don't need a template<> header. |
Douglas Gregor | 3ebd753 | 2009-11-23 12:11:45 +0000 | [diff] [blame] | 1260 | if (SpecDecl->getSpecializationKind() == TSK_ExplicitSpecialization) { |
| 1261 | ExplicitSpecializationsInSpecifier.push_back(SpecDecl); |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1262 | continue; |
Douglas Gregor | 3ebd753 | 2009-11-23 12:11:45 +0000 | [diff] [blame] | 1263 | } |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1264 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1265 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1266 | TemplateIdsInSpecifier.push_back(SpecType); |
| 1267 | } |
| 1268 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1269 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1270 | // Reverse the list of template-ids in the scope specifier, so that we can |
| 1271 | // more easily match up the template-ids and the template parameter lists. |
| 1272 | std::reverse(TemplateIdsInSpecifier.begin(), TemplateIdsInSpecifier.end()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1273 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1274 | SourceLocation FirstTemplateLoc = DeclStartLoc; |
| 1275 | if (NumParamLists) |
| 1276 | FirstTemplateLoc = ParamLists[0]->getTemplateLoc(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1277 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1278 | // Match the template-ids found in the specifier to the template parameter |
| 1279 | // lists. |
| 1280 | unsigned Idx = 0; |
| 1281 | for (unsigned NumTemplateIds = TemplateIdsInSpecifier.size(); |
| 1282 | Idx != NumTemplateIds; ++Idx) { |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 1283 | QualType TemplateId = QualType(TemplateIdsInSpecifier[Idx], 0); |
| 1284 | bool DependentTemplateId = TemplateId->isDependentType(); |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1285 | if (Idx >= NumParamLists) { |
| 1286 | // We have a template-id without a corresponding template parameter |
| 1287 | // list. |
| 1288 | if (DependentTemplateId) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1289 | // FIXME: the location information here isn't great. |
| 1290 | Diag(SS.getRange().getBegin(), |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1291 | diag::err_template_spec_needs_template_parameters) |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 1292 | << TemplateId |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1293 | << SS.getRange(); |
| 1294 | } else { |
| 1295 | Diag(SS.getRange().getBegin(), diag::err_template_spec_needs_header) |
| 1296 | << SS.getRange() |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 1297 | << FixItHint::CreateInsertion(FirstTemplateLoc, "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, |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 1313 | ClassTemplate->getInjectedClassNameSpecialization(Context))) |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 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); |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 1438 | assert(isa<RecordType>(CanonType) && |
| 1439 | "type of non-dependent specialization is not a RecordType"); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1440 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1441 | |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1442 | // Build the fully-sugared type for this class template |
| 1443 | // specialization, which refers back to the class template |
| 1444 | // specialization we created or found. |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1445 | return Context.getTemplateSpecializationType(Name, TemplateArgs, CanonType); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1446 | } |
| 1447 | |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 1448 | Action::TypeResult |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1449 | Sema::ActOnTemplateIdType(TemplateTy TemplateD, SourceLocation TemplateLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1450 | SourceLocation LAngleLoc, |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1451 | ASTTemplateArgsPtr TemplateArgsIn, |
John McCall | 6b2becf | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 1452 | SourceLocation RAngleLoc) { |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1453 | TemplateName Template = TemplateD.getAsVal<TemplateName>(); |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 1454 | |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1455 | // Translate the parser's template argument list in our AST format. |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1456 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
Douglas Gregor | 314b97f | 2009-11-10 19:49:08 +0000 | [diff] [blame] | 1457 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 1458 | |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1459 | QualType Result = CheckTemplateIdType(Template, TemplateLoc, TemplateArgs); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1460 | TemplateArgsIn.release(); |
Douglas Gregor | 31a19b6 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 1461 | |
| 1462 | if (Result.isNull()) |
| 1463 | return true; |
| 1464 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1465 | TypeSourceInfo *DI = Context.CreateTypeSourceInfo(Result); |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1466 | TemplateSpecializationTypeLoc TL |
| 1467 | = cast<TemplateSpecializationTypeLoc>(DI->getTypeLoc()); |
| 1468 | TL.setTemplateNameLoc(TemplateLoc); |
| 1469 | TL.setLAngleLoc(LAngleLoc); |
| 1470 | TL.setRAngleLoc(RAngleLoc); |
| 1471 | for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) |
| 1472 | TL.setArgLocInfo(i, TemplateArgs[i].getLocInfo()); |
| 1473 | |
| 1474 | return CreateLocInfoType(Result, DI).getAsOpaquePtr(); |
John McCall | 6b2becf | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 1475 | } |
John McCall | f1bbbb4 | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 1476 | |
John McCall | 6b2becf | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 1477 | Sema::TypeResult Sema::ActOnTagTemplateIdType(TypeResult TypeResult, |
| 1478 | TagUseKind TUK, |
| 1479 | DeclSpec::TST TagSpec, |
| 1480 | SourceLocation TagLoc) { |
| 1481 | if (TypeResult.isInvalid()) |
| 1482 | return Sema::TypeResult(); |
John McCall | f1bbbb4 | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 1483 | |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1484 | // FIXME: preserve source info, ideally without copying the DI. |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1485 | TypeSourceInfo *DI; |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1486 | QualType Type = GetTypeFromParser(TypeResult.get(), &DI); |
John McCall | f1bbbb4 | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 1487 | |
John McCall | 6b2becf | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 1488 | // Verify the tag specifier. |
| 1489 | TagDecl::TagKind TagKind = TagDecl::getTagKindForTypeSpec(TagSpec); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1490 | |
John McCall | 6b2becf | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 1491 | if (const RecordType *RT = Type->getAs<RecordType>()) { |
| 1492 | RecordDecl *D = RT->getDecl(); |
| 1493 | |
| 1494 | IdentifierInfo *Id = D->getIdentifier(); |
| 1495 | assert(Id && "templated class must have an identifier"); |
| 1496 | |
| 1497 | if (!isAcceptableTagRedeclaration(D, TagKind, TagLoc, *Id)) { |
| 1498 | Diag(TagLoc, diag::err_use_with_wrong_tag) |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 1499 | << Type |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 1500 | << FixItHint::CreateReplacement(SourceRange(TagLoc), D->getKindName()); |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 1501 | Diag(D->getLocation(), diag::note_previous_use); |
John McCall | f1bbbb4 | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 1502 | } |
| 1503 | } |
| 1504 | |
John McCall | 6b2becf | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 1505 | QualType ElabType = Context.getElaboratedType(Type, TagKind); |
| 1506 | |
| 1507 | return ElabType.getAsOpaquePtr(); |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 1508 | } |
| 1509 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1510 | Sema::OwningExprResult Sema::BuildTemplateIdExpr(const CXXScopeSpec &SS, |
| 1511 | LookupResult &R, |
| 1512 | bool RequiresADL, |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1513 | const TemplateArgumentListInfo &TemplateArgs) { |
Douglas Gregor | edce4dd | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 1514 | // FIXME: Can we do any checking at this point? I guess we could check the |
| 1515 | // template arguments that we have against the template name, if the template |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1516 | // 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] | 1517 | // though. |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1518 | |
| 1519 | // These should be filtered out by our callers. |
| 1520 | assert(!R.empty() && "empty lookup results when building templateid"); |
| 1521 | assert(!R.isAmbiguous() && "ambiguous lookup when building templateid"); |
| 1522 | |
| 1523 | NestedNameSpecifier *Qualifier = 0; |
| 1524 | SourceRange QualifierRange; |
| 1525 | if (SS.isSet()) { |
| 1526 | Qualifier = static_cast<NestedNameSpecifier*>(SS.getScopeRep()); |
| 1527 | QualifierRange = SS.getRange(); |
Douglas Gregor | a9e29aa | 2009-10-22 07:19:14 +0000 | [diff] [blame] | 1528 | } |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 1529 | |
| 1530 | // We don't want lookup warnings at this point. |
| 1531 | R.suppressDiagnostics(); |
Douglas Gregor | a9e29aa | 2009-10-22 07:19:14 +0000 | [diff] [blame] | 1532 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1533 | bool Dependent |
| 1534 | = UnresolvedLookupExpr::ComputeDependence(R.begin(), R.end(), |
| 1535 | &TemplateArgs); |
| 1536 | UnresolvedLookupExpr *ULE |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 1537 | = UnresolvedLookupExpr::Create(Context, Dependent, R.getNamingClass(), |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1538 | Qualifier, QualifierRange, |
| 1539 | R.getLookupName(), R.getNameLoc(), |
| 1540 | RequiresADL, TemplateArgs); |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 1541 | ULE->addDecls(R.begin(), R.end()); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1542 | |
| 1543 | return Owned(ULE); |
Douglas Gregor | edce4dd | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 1544 | } |
| 1545 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1546 | // We actually only call this from template instantiation. |
| 1547 | Sema::OwningExprResult |
| 1548 | Sema::BuildQualifiedTemplateIdExpr(const CXXScopeSpec &SS, |
| 1549 | DeclarationName Name, |
| 1550 | SourceLocation NameLoc, |
| 1551 | const TemplateArgumentListInfo &TemplateArgs) { |
| 1552 | DeclContext *DC; |
| 1553 | if (!(DC = computeDeclContext(SS, false)) || |
| 1554 | DC->isDependentContext() || |
| 1555 | RequireCompleteDeclContext(SS)) |
| 1556 | return BuildDependentDeclRefExpr(SS, Name, NameLoc, &TemplateArgs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1557 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1558 | LookupResult R(*this, Name, NameLoc, LookupOrdinaryName); |
| 1559 | LookupTemplateName(R, (Scope*) 0, SS, QualType(), /*Entering*/ false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1560 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1561 | if (R.isAmbiguous()) |
| 1562 | return ExprError(); |
| 1563 | |
| 1564 | if (R.empty()) { |
| 1565 | Diag(NameLoc, diag::err_template_kw_refers_to_non_template) |
| 1566 | << Name << SS.getRange(); |
| 1567 | return ExprError(); |
| 1568 | } |
| 1569 | |
| 1570 | if (ClassTemplateDecl *Temp = R.getAsSingle<ClassTemplateDecl>()) { |
| 1571 | Diag(NameLoc, diag::err_template_kw_refers_to_class_template) |
| 1572 | << (NestedNameSpecifier*) SS.getScopeRep() << Name << SS.getRange(); |
| 1573 | Diag(Temp->getLocation(), diag::note_referenced_class_template); |
| 1574 | return ExprError(); |
| 1575 | } |
| 1576 | |
| 1577 | return BuildTemplateIdExpr(SS, R, /* ADL */ false, TemplateArgs); |
Douglas Gregor | edce4dd | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 1578 | } |
| 1579 | |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 1580 | /// \brief Form a dependent template name. |
| 1581 | /// |
| 1582 | /// This action forms a dependent template name given the template |
| 1583 | /// name and its (presumably dependent) scope specifier. For |
| 1584 | /// example, given "MetaFun::template apply", the scope specifier \p |
| 1585 | /// SS will be "MetaFun::", \p TemplateKWLoc contains the location |
| 1586 | /// of the "template" keyword, and "apply" is the \p Name. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1587 | Sema::TemplateTy |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 1588 | Sema::ActOnDependentTemplateName(SourceLocation TemplateKWLoc, |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 1589 | const CXXScopeSpec &SS, |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 1590 | UnqualifiedId &Name, |
Douglas Gregor | a481edb | 2009-11-20 23:39:24 +0000 | [diff] [blame] | 1591 | TypeTy *ObjectType, |
| 1592 | bool EnteringContext) { |
Douglas Gregor | 0707bc5 | 2010-01-19 16:01:07 +0000 | [diff] [blame] | 1593 | DeclContext *LookupCtx = 0; |
| 1594 | if (SS.isSet()) |
| 1595 | LookupCtx = computeDeclContext(SS, EnteringContext); |
| 1596 | if (!LookupCtx && ObjectType) |
| 1597 | LookupCtx = computeDeclContext(QualType::getFromOpaquePtr(ObjectType)); |
| 1598 | if (LookupCtx) { |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 1599 | // C++0x [temp.names]p5: |
| 1600 | // If a name prefixed by the keyword template is not the name of |
| 1601 | // a template, the program is ill-formed. [Note: the keyword |
| 1602 | // template may not be applied to non-template members of class |
| 1603 | // templates. -end note ] [ Note: as is the case with the |
| 1604 | // typename prefix, the template prefix is allowed in cases |
| 1605 | // where it is not strictly necessary; i.e., when the |
| 1606 | // nested-name-specifier or the expression on the left of the -> |
| 1607 | // or . is not dependent on a template-parameter, or the use |
| 1608 | // does not appear in the scope of a template. -end note] |
| 1609 | // |
| 1610 | // Note: C++03 was more strict here, because it banned the use of |
| 1611 | // the "template" keyword prior to a template-name that was not a |
| 1612 | // dependent name. C++ DR468 relaxed this requirement (the |
| 1613 | // "template" keyword is now permitted). We follow the C++0x |
| 1614 | // rules, even in C++03 mode, retroactively applying the DR. |
| 1615 | TemplateTy Template; |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 1616 | TemplateNameKind TNK = isTemplateName(0, SS, Name, ObjectType, |
Douglas Gregor | a481edb | 2009-11-20 23:39:24 +0000 | [diff] [blame] | 1617 | EnteringContext, Template); |
Douglas Gregor | 0707bc5 | 2010-01-19 16:01:07 +0000 | [diff] [blame] | 1618 | if (TNK == TNK_Non_template && LookupCtx->isDependentContext() && |
| 1619 | isa<CXXRecordDecl>(LookupCtx) && |
| 1620 | cast<CXXRecordDecl>(LookupCtx)->hasAnyDependentBases()) { |
Douglas Gregor | 9edad9b | 2010-01-14 17:47:39 +0000 | [diff] [blame] | 1621 | // This is a dependent template. |
| 1622 | } else if (TNK == TNK_Non_template) { |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 1623 | Diag(Name.getSourceRange().getBegin(), |
| 1624 | diag::err_template_kw_refers_to_non_template) |
| 1625 | << GetNameFromUnqualifiedId(Name) |
| 1626 | << Name.getSourceRange(); |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 1627 | return TemplateTy(); |
Douglas Gregor | 9edad9b | 2010-01-14 17:47:39 +0000 | [diff] [blame] | 1628 | } else { |
| 1629 | // We found something; return it. |
| 1630 | return Template; |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 1631 | } |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 1632 | } |
| 1633 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1634 | NestedNameSpecifier *Qualifier |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 1635 | = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 1636 | |
| 1637 | switch (Name.getKind()) { |
| 1638 | case UnqualifiedId::IK_Identifier: |
| 1639 | return TemplateTy::make(Context.getDependentTemplateName(Qualifier, |
| 1640 | Name.Identifier)); |
| 1641 | |
Douglas Gregor | ca1bdd7 | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 1642 | case UnqualifiedId::IK_OperatorFunctionId: |
| 1643 | return TemplateTy::make(Context.getDependentTemplateName(Qualifier, |
| 1644 | Name.OperatorFunctionId.Operator)); |
Sean Hunt | e6252d1 | 2009-11-28 08:58:14 +0000 | [diff] [blame] | 1645 | |
| 1646 | case UnqualifiedId::IK_LiteralOperatorId: |
| 1647 | assert(false && "We don't support these; Parse shouldn't have allowed propagation"); |
| 1648 | |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 1649 | default: |
| 1650 | break; |
| 1651 | } |
| 1652 | |
| 1653 | Diag(Name.getSourceRange().getBegin(), |
| 1654 | diag::err_template_kw_refers_to_non_template) |
| 1655 | << GetNameFromUnqualifiedId(Name) |
| 1656 | << Name.getSourceRange(); |
| 1657 | return TemplateTy(); |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 1658 | } |
| 1659 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1660 | bool Sema::CheckTemplateTypeArgument(TemplateTypeParmDecl *Param, |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1661 | const TemplateArgumentLoc &AL, |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 1662 | TemplateArgumentListBuilder &Converted) { |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1663 | const TemplateArgument &Arg = AL.getArgument(); |
| 1664 | |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 1665 | // Check template type parameter. |
| 1666 | if (Arg.getKind() != TemplateArgument::Type) { |
| 1667 | // C++ [temp.arg.type]p1: |
| 1668 | // A template-argument for a template-parameter which is a |
| 1669 | // type shall be a type-id. |
| 1670 | |
| 1671 | // We have a template type parameter but the template argument |
| 1672 | // is not a type. |
John McCall | 828bff2 | 2009-10-29 18:45:58 +0000 | [diff] [blame] | 1673 | SourceRange SR = AL.getSourceRange(); |
| 1674 | Diag(SR.getBegin(), diag::err_template_arg_must_be_type) << SR; |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 1675 | Diag(Param->getLocation(), diag::note_template_param_here); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1676 | |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 1677 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1678 | } |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 1679 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1680 | if (CheckTemplateArgument(Param, AL.getTypeSourceInfo())) |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 1681 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1682 | |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 1683 | // Add the converted template type argument. |
Anders Carlsson | fb25052 | 2009-06-23 01:26:57 +0000 | [diff] [blame] | 1684 | Converted.Append( |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1685 | TemplateArgument(Context.getCanonicalType(Arg.getAsType()))); |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 1686 | return false; |
| 1687 | } |
| 1688 | |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 1689 | /// \brief Substitute template arguments into the default template argument for |
| 1690 | /// the given template type parameter. |
| 1691 | /// |
| 1692 | /// \param SemaRef the semantic analysis object for which we are performing |
| 1693 | /// the substitution. |
| 1694 | /// |
| 1695 | /// \param Template the template that we are synthesizing template arguments |
| 1696 | /// for. |
| 1697 | /// |
| 1698 | /// \param TemplateLoc the location of the template name that started the |
| 1699 | /// template-id we are checking. |
| 1700 | /// |
| 1701 | /// \param RAngleLoc the location of the right angle bracket ('>') that |
| 1702 | /// terminates the template-id. |
| 1703 | /// |
| 1704 | /// \param Param the template template parameter whose default we are |
| 1705 | /// substituting into. |
| 1706 | /// |
| 1707 | /// \param Converted the list of template arguments provided for template |
| 1708 | /// parameters that precede \p Param in the template parameter list. |
| 1709 | /// |
| 1710 | /// \returns the substituted template argument, or NULL if an error occurred. |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1711 | static TypeSourceInfo * |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 1712 | SubstDefaultTemplateArgument(Sema &SemaRef, |
| 1713 | TemplateDecl *Template, |
| 1714 | SourceLocation TemplateLoc, |
| 1715 | SourceLocation RAngleLoc, |
| 1716 | TemplateTypeParmDecl *Param, |
| 1717 | TemplateArgumentListBuilder &Converted) { |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1718 | TypeSourceInfo *ArgType = Param->getDefaultArgumentInfo(); |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 1719 | |
| 1720 | // If the argument type is dependent, instantiate it now based |
| 1721 | // on the previously-computed template arguments. |
| 1722 | if (ArgType->getType()->isDependentType()) { |
| 1723 | TemplateArgumentList TemplateArgs(SemaRef.Context, Converted, |
| 1724 | /*TakeArgs=*/false); |
| 1725 | |
| 1726 | MultiLevelTemplateArgumentList AllTemplateArgs |
| 1727 | = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs); |
| 1728 | |
| 1729 | Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc, |
| 1730 | Template, Converted.getFlatArguments(), |
| 1731 | Converted.flatSize(), |
| 1732 | SourceRange(TemplateLoc, RAngleLoc)); |
| 1733 | |
| 1734 | ArgType = SemaRef.SubstType(ArgType, AllTemplateArgs, |
| 1735 | Param->getDefaultArgumentLoc(), |
| 1736 | Param->getDeclName()); |
| 1737 | } |
| 1738 | |
| 1739 | return ArgType; |
| 1740 | } |
| 1741 | |
| 1742 | /// \brief Substitute template arguments into the default template argument for |
| 1743 | /// the given non-type template parameter. |
| 1744 | /// |
| 1745 | /// \param SemaRef the semantic analysis object for which we are performing |
| 1746 | /// the substitution. |
| 1747 | /// |
| 1748 | /// \param Template the template that we are synthesizing template arguments |
| 1749 | /// for. |
| 1750 | /// |
| 1751 | /// \param TemplateLoc the location of the template name that started the |
| 1752 | /// template-id we are checking. |
| 1753 | /// |
| 1754 | /// \param RAngleLoc the location of the right angle bracket ('>') that |
| 1755 | /// terminates the template-id. |
| 1756 | /// |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1757 | /// \param Param the non-type template parameter whose default we are |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 1758 | /// substituting into. |
| 1759 | /// |
| 1760 | /// \param Converted the list of template arguments provided for template |
| 1761 | /// parameters that precede \p Param in the template parameter list. |
| 1762 | /// |
| 1763 | /// \returns the substituted template argument, or NULL if an error occurred. |
| 1764 | static Sema::OwningExprResult |
| 1765 | SubstDefaultTemplateArgument(Sema &SemaRef, |
| 1766 | TemplateDecl *Template, |
| 1767 | SourceLocation TemplateLoc, |
| 1768 | SourceLocation RAngleLoc, |
| 1769 | NonTypeTemplateParmDecl *Param, |
| 1770 | TemplateArgumentListBuilder &Converted) { |
| 1771 | TemplateArgumentList TemplateArgs(SemaRef.Context, Converted, |
| 1772 | /*TakeArgs=*/false); |
| 1773 | |
| 1774 | MultiLevelTemplateArgumentList AllTemplateArgs |
| 1775 | = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs); |
| 1776 | |
| 1777 | Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc, |
| 1778 | Template, Converted.getFlatArguments(), |
| 1779 | Converted.flatSize(), |
| 1780 | SourceRange(TemplateLoc, RAngleLoc)); |
| 1781 | |
| 1782 | return SemaRef.SubstExpr(Param->getDefaultArgument(), AllTemplateArgs); |
| 1783 | } |
| 1784 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1785 | /// \brief Substitute template arguments into the default template argument for |
| 1786 | /// the given template template parameter. |
| 1787 | /// |
| 1788 | /// \param SemaRef the semantic analysis object for which we are performing |
| 1789 | /// the substitution. |
| 1790 | /// |
| 1791 | /// \param Template the template that we are synthesizing template arguments |
| 1792 | /// for. |
| 1793 | /// |
| 1794 | /// \param TemplateLoc the location of the template name that started the |
| 1795 | /// template-id we are checking. |
| 1796 | /// |
| 1797 | /// \param RAngleLoc the location of the right angle bracket ('>') that |
| 1798 | /// terminates the template-id. |
| 1799 | /// |
| 1800 | /// \param Param the template template parameter whose default we are |
| 1801 | /// substituting into. |
| 1802 | /// |
| 1803 | /// \param Converted the list of template arguments provided for template |
| 1804 | /// parameters that precede \p Param in the template parameter list. |
| 1805 | /// |
| 1806 | /// \returns the substituted template argument, or NULL if an error occurred. |
| 1807 | static TemplateName |
| 1808 | SubstDefaultTemplateArgument(Sema &SemaRef, |
| 1809 | TemplateDecl *Template, |
| 1810 | SourceLocation TemplateLoc, |
| 1811 | SourceLocation RAngleLoc, |
| 1812 | TemplateTemplateParmDecl *Param, |
| 1813 | TemplateArgumentListBuilder &Converted) { |
| 1814 | TemplateArgumentList TemplateArgs(SemaRef.Context, Converted, |
| 1815 | /*TakeArgs=*/false); |
| 1816 | |
| 1817 | MultiLevelTemplateArgumentList AllTemplateArgs |
| 1818 | = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs); |
| 1819 | |
| 1820 | Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc, |
| 1821 | Template, Converted.getFlatArguments(), |
| 1822 | Converted.flatSize(), |
| 1823 | SourceRange(TemplateLoc, RAngleLoc)); |
| 1824 | |
| 1825 | return SemaRef.SubstTemplateName( |
| 1826 | Param->getDefaultArgument().getArgument().getAsTemplate(), |
| 1827 | Param->getDefaultArgument().getTemplateNameLoc(), |
| 1828 | AllTemplateArgs); |
| 1829 | } |
| 1830 | |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 1831 | /// \brief If the given template parameter has a default template |
| 1832 | /// argument, substitute into that default template argument and |
| 1833 | /// return the corresponding template argument. |
| 1834 | TemplateArgumentLoc |
| 1835 | Sema::SubstDefaultTemplateArgumentIfAvailable(TemplateDecl *Template, |
| 1836 | SourceLocation TemplateLoc, |
| 1837 | SourceLocation RAngleLoc, |
| 1838 | Decl *Param, |
| 1839 | TemplateArgumentListBuilder &Converted) { |
| 1840 | if (TemplateTypeParmDecl *TypeParm = dyn_cast<TemplateTypeParmDecl>(Param)) { |
| 1841 | if (!TypeParm->hasDefaultArgument()) |
| 1842 | return TemplateArgumentLoc(); |
| 1843 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1844 | TypeSourceInfo *DI = SubstDefaultTemplateArgument(*this, Template, |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 1845 | TemplateLoc, |
| 1846 | RAngleLoc, |
| 1847 | TypeParm, |
| 1848 | Converted); |
| 1849 | if (DI) |
| 1850 | return TemplateArgumentLoc(TemplateArgument(DI->getType()), DI); |
| 1851 | |
| 1852 | return TemplateArgumentLoc(); |
| 1853 | } |
| 1854 | |
| 1855 | if (NonTypeTemplateParmDecl *NonTypeParm |
| 1856 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
| 1857 | if (!NonTypeParm->hasDefaultArgument()) |
| 1858 | return TemplateArgumentLoc(); |
| 1859 | |
| 1860 | OwningExprResult Arg = SubstDefaultTemplateArgument(*this, Template, |
| 1861 | TemplateLoc, |
| 1862 | RAngleLoc, |
| 1863 | NonTypeParm, |
| 1864 | Converted); |
| 1865 | if (Arg.isInvalid()) |
| 1866 | return TemplateArgumentLoc(); |
| 1867 | |
| 1868 | Expr *ArgE = Arg.takeAs<Expr>(); |
| 1869 | return TemplateArgumentLoc(TemplateArgument(ArgE), ArgE); |
| 1870 | } |
| 1871 | |
| 1872 | TemplateTemplateParmDecl *TempTempParm |
| 1873 | = cast<TemplateTemplateParmDecl>(Param); |
| 1874 | if (!TempTempParm->hasDefaultArgument()) |
| 1875 | return TemplateArgumentLoc(); |
| 1876 | |
| 1877 | TemplateName TName = SubstDefaultTemplateArgument(*this, Template, |
| 1878 | TemplateLoc, |
| 1879 | RAngleLoc, |
| 1880 | TempTempParm, |
| 1881 | Converted); |
| 1882 | if (TName.isNull()) |
| 1883 | return TemplateArgumentLoc(); |
| 1884 | |
| 1885 | return TemplateArgumentLoc(TemplateArgument(TName), |
| 1886 | TempTempParm->getDefaultArgument().getTemplateQualifierRange(), |
| 1887 | TempTempParm->getDefaultArgument().getTemplateNameLoc()); |
| 1888 | } |
| 1889 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 1890 | /// \brief Check that the given template argument corresponds to the given |
| 1891 | /// template parameter. |
| 1892 | bool Sema::CheckTemplateArgument(NamedDecl *Param, |
| 1893 | const TemplateArgumentLoc &Arg, |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 1894 | TemplateDecl *Template, |
| 1895 | SourceLocation TemplateLoc, |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 1896 | SourceLocation RAngleLoc, |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 1897 | TemplateArgumentListBuilder &Converted, |
| 1898 | CheckTemplateArgumentKind CTAK) { |
Douglas Gregor | d9e1530 | 2009-11-11 19:41:09 +0000 | [diff] [blame] | 1899 | // Check template type parameters. |
| 1900 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 1901 | return CheckTemplateTypeArgument(TTP, Arg, Converted); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 1902 | |
Douglas Gregor | d9e1530 | 2009-11-11 19:41:09 +0000 | [diff] [blame] | 1903 | // Check non-type template parameters. |
| 1904 | if (NonTypeTemplateParmDecl *NTTP =dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 1905 | // Do substitution on the type of the non-type template parameter |
| 1906 | // with the template arguments we've seen thus far. |
| 1907 | QualType NTTPType = NTTP->getType(); |
| 1908 | if (NTTPType->isDependentType()) { |
| 1909 | // Do substitution on the type of the non-type template parameter. |
| 1910 | InstantiatingTemplate Inst(*this, TemplateLoc, Template, |
| 1911 | NTTP, Converted.getFlatArguments(), |
| 1912 | Converted.flatSize(), |
| 1913 | SourceRange(TemplateLoc, RAngleLoc)); |
| 1914 | |
| 1915 | TemplateArgumentList TemplateArgs(Context, Converted, |
| 1916 | /*TakeArgs=*/false); |
| 1917 | NTTPType = SubstType(NTTPType, |
| 1918 | MultiLevelTemplateArgumentList(TemplateArgs), |
| 1919 | NTTP->getLocation(), |
| 1920 | NTTP->getDeclName()); |
| 1921 | // If that worked, check the non-type template parameter type |
| 1922 | // for validity. |
| 1923 | if (!NTTPType.isNull()) |
| 1924 | NTTPType = CheckNonTypeTemplateParameterType(NTTPType, |
| 1925 | NTTP->getLocation()); |
| 1926 | if (NTTPType.isNull()) |
| 1927 | return true; |
| 1928 | } |
| 1929 | |
| 1930 | switch (Arg.getArgument().getKind()) { |
| 1931 | case TemplateArgument::Null: |
| 1932 | assert(false && "Should never see a NULL template argument here"); |
| 1933 | return true; |
| 1934 | |
| 1935 | case TemplateArgument::Expression: { |
| 1936 | Expr *E = Arg.getArgument().getAsExpr(); |
| 1937 | TemplateArgument Result; |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 1938 | if (CheckTemplateArgument(NTTP, NTTPType, E, Result, CTAK)) |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 1939 | return true; |
| 1940 | |
| 1941 | Converted.Append(Result); |
| 1942 | break; |
| 1943 | } |
| 1944 | |
| 1945 | case TemplateArgument::Declaration: |
| 1946 | case TemplateArgument::Integral: |
| 1947 | // We've already checked this template argument, so just copy |
| 1948 | // it to the list of converted arguments. |
| 1949 | Converted.Append(Arg.getArgument()); |
| 1950 | break; |
| 1951 | |
| 1952 | case TemplateArgument::Template: |
| 1953 | // We were given a template template argument. It may not be ill-formed; |
| 1954 | // see below. |
| 1955 | if (DependentTemplateName *DTN |
| 1956 | = Arg.getArgument().getAsTemplate().getAsDependentTemplateName()) { |
| 1957 | // We have a template argument such as \c T::template X, which we |
| 1958 | // parsed as a template template argument. However, since we now |
| 1959 | // know that we need a non-type template argument, convert this |
| 1960 | // template name into an expression. |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1961 | Expr *E = DependentScopeDeclRefExpr::Create(Context, |
| 1962 | DTN->getQualifier(), |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 1963 | Arg.getTemplateQualifierRange(), |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1964 | DTN->getIdentifier(), |
| 1965 | Arg.getTemplateNameLoc()); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 1966 | |
| 1967 | TemplateArgument Result; |
| 1968 | if (CheckTemplateArgument(NTTP, NTTPType, E, Result)) |
| 1969 | return true; |
| 1970 | |
| 1971 | Converted.Append(Result); |
| 1972 | break; |
| 1973 | } |
| 1974 | |
| 1975 | // We have a template argument that actually does refer to a class |
| 1976 | // template, template alias, or template template parameter, and |
| 1977 | // therefore cannot be a non-type template argument. |
| 1978 | Diag(Arg.getLocation(), diag::err_template_arg_must_be_expr) |
| 1979 | << Arg.getSourceRange(); |
| 1980 | |
| 1981 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 1982 | return true; |
| 1983 | |
| 1984 | case TemplateArgument::Type: { |
| 1985 | // We have a non-type template parameter but the template |
| 1986 | // argument is a type. |
| 1987 | |
| 1988 | // C++ [temp.arg]p2: |
| 1989 | // In a template-argument, an ambiguity between a type-id and |
| 1990 | // an expression is resolved to a type-id, regardless of the |
| 1991 | // form of the corresponding template-parameter. |
| 1992 | // |
| 1993 | // We warn specifically about this case, since it can be rather |
| 1994 | // confusing for users. |
| 1995 | QualType T = Arg.getArgument().getAsType(); |
| 1996 | SourceRange SR = Arg.getSourceRange(); |
| 1997 | if (T->isFunctionType()) |
| 1998 | Diag(SR.getBegin(), diag::err_template_arg_nontype_ambig) << SR << T; |
| 1999 | else |
| 2000 | Diag(SR.getBegin(), diag::err_template_arg_must_be_expr) << SR; |
| 2001 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 2002 | return true; |
| 2003 | } |
| 2004 | |
| 2005 | case TemplateArgument::Pack: |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 2006 | llvm_unreachable("Caller must expand template argument packs"); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2007 | break; |
| 2008 | } |
| 2009 | |
| 2010 | return false; |
| 2011 | } |
| 2012 | |
| 2013 | |
| 2014 | // Check template template parameters. |
| 2015 | TemplateTemplateParmDecl *TempParm = cast<TemplateTemplateParmDecl>(Param); |
| 2016 | |
| 2017 | // Substitute into the template parameter list of the template |
| 2018 | // template parameter, since previously-supplied template arguments |
| 2019 | // may appear within the template template parameter. |
| 2020 | { |
| 2021 | // Set up a template instantiation context. |
| 2022 | LocalInstantiationScope Scope(*this); |
| 2023 | InstantiatingTemplate Inst(*this, TemplateLoc, Template, |
| 2024 | TempParm, Converted.getFlatArguments(), |
| 2025 | Converted.flatSize(), |
| 2026 | SourceRange(TemplateLoc, RAngleLoc)); |
| 2027 | |
| 2028 | TemplateArgumentList TemplateArgs(Context, Converted, |
| 2029 | /*TakeArgs=*/false); |
| 2030 | TempParm = cast_or_null<TemplateTemplateParmDecl>( |
| 2031 | SubstDecl(TempParm, CurContext, |
| 2032 | MultiLevelTemplateArgumentList(TemplateArgs))); |
| 2033 | if (!TempParm) |
| 2034 | return true; |
| 2035 | |
| 2036 | // FIXME: TempParam is leaked. |
| 2037 | } |
| 2038 | |
| 2039 | switch (Arg.getArgument().getKind()) { |
| 2040 | case TemplateArgument::Null: |
| 2041 | assert(false && "Should never see a NULL template argument here"); |
| 2042 | return true; |
| 2043 | |
| 2044 | case TemplateArgument::Template: |
| 2045 | if (CheckTemplateArgument(TempParm, Arg)) |
| 2046 | return true; |
| 2047 | |
| 2048 | Converted.Append(Arg.getArgument()); |
| 2049 | break; |
| 2050 | |
| 2051 | case TemplateArgument::Expression: |
| 2052 | case TemplateArgument::Type: |
| 2053 | // We have a template template parameter but the template |
| 2054 | // argument does not refer to a template. |
| 2055 | Diag(Arg.getLocation(), diag::err_template_arg_must_be_template); |
| 2056 | return true; |
| 2057 | |
| 2058 | case TemplateArgument::Declaration: |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 2059 | llvm_unreachable( |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2060 | "Declaration argument with template template parameter"); |
| 2061 | break; |
| 2062 | case TemplateArgument::Integral: |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 2063 | llvm_unreachable( |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2064 | "Integral argument with template template parameter"); |
| 2065 | break; |
| 2066 | |
| 2067 | case TemplateArgument::Pack: |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 2068 | llvm_unreachable("Caller must expand template argument packs"); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2069 | break; |
| 2070 | } |
| 2071 | |
| 2072 | return false; |
| 2073 | } |
| 2074 | |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2075 | /// \brief Check that the given template argument list is well-formed |
| 2076 | /// for specializing the given template. |
| 2077 | bool Sema::CheckTemplateArgumentList(TemplateDecl *Template, |
| 2078 | SourceLocation TemplateLoc, |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2079 | const TemplateArgumentListInfo &TemplateArgs, |
Douglas Gregor | 16134c6 | 2009-07-01 00:28:38 +0000 | [diff] [blame] | 2080 | bool PartialTemplateArgs, |
Anders Carlsson | 1c5976e | 2009-06-05 03:43:12 +0000 | [diff] [blame] | 2081 | TemplateArgumentListBuilder &Converted) { |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2082 | TemplateParameterList *Params = Template->getTemplateParameters(); |
| 2083 | unsigned NumParams = Params->size(); |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2084 | unsigned NumArgs = TemplateArgs.size(); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2085 | bool Invalid = false; |
| 2086 | |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2087 | SourceLocation RAngleLoc = TemplateArgs.getRAngleLoc(); |
| 2088 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2089 | bool HasParameterPack = |
Anders Carlsson | 0ceffb5 | 2009-06-13 02:08:00 +0000 | [diff] [blame] | 2090 | NumParams > 0 && Params->getParam(NumParams - 1)->isTemplateParameterPack(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2091 | |
Anders Carlsson | 0ceffb5 | 2009-06-13 02:08:00 +0000 | [diff] [blame] | 2092 | if ((NumArgs > NumParams && !HasParameterPack) || |
Douglas Gregor | 16134c6 | 2009-07-01 00:28:38 +0000 | [diff] [blame] | 2093 | (NumArgs < Params->getMinRequiredArguments() && |
| 2094 | !PartialTemplateArgs)) { |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2095 | // FIXME: point at either the first arg beyond what we can handle, |
| 2096 | // or the '>', depending on whether we have too many or too few |
| 2097 | // arguments. |
| 2098 | SourceRange Range; |
| 2099 | if (NumArgs > NumParams) |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2100 | Range = SourceRange(TemplateArgs[NumParams].getLocation(), RAngleLoc); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2101 | Diag(TemplateLoc, diag::err_template_arg_list_different_arity) |
| 2102 | << (NumArgs > NumParams) |
| 2103 | << (isa<ClassTemplateDecl>(Template)? 0 : |
| 2104 | isa<FunctionTemplateDecl>(Template)? 1 : |
| 2105 | isa<TemplateTemplateParmDecl>(Template)? 2 : 3) |
| 2106 | << Template << Range; |
Douglas Gregor | 62cb18d | 2009-02-11 18:16:40 +0000 | [diff] [blame] | 2107 | Diag(Template->getLocation(), diag::note_template_decl_here) |
| 2108 | << Params->getSourceRange(); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2109 | Invalid = true; |
| 2110 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2111 | |
| 2112 | // C++ [temp.arg]p1: |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2113 | // [...] The type and form of each template-argument specified in |
| 2114 | // a template-id shall match the type and form specified for the |
| 2115 | // corresponding parameter declared by the template in its |
| 2116 | // template-parameter-list. |
| 2117 | unsigned ArgIdx = 0; |
| 2118 | for (TemplateParameterList::iterator Param = Params->begin(), |
| 2119 | ParamEnd = Params->end(); |
| 2120 | Param != ParamEnd; ++Param, ++ArgIdx) { |
Douglas Gregor | 16134c6 | 2009-07-01 00:28:38 +0000 | [diff] [blame] | 2121 | if (ArgIdx > NumArgs && PartialTemplateArgs) |
| 2122 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2123 | |
Douglas Gregor | d9e1530 | 2009-11-11 19:41:09 +0000 | [diff] [blame] | 2124 | // If we have a template parameter pack, check every remaining template |
| 2125 | // argument against that template parameter pack. |
| 2126 | if ((*Param)->isTemplateParameterPack()) { |
| 2127 | Converted.BeginPack(); |
| 2128 | for (; ArgIdx < NumArgs; ++ArgIdx) { |
| 2129 | if (CheckTemplateArgument(*Param, TemplateArgs[ArgIdx], Template, |
| 2130 | TemplateLoc, RAngleLoc, Converted)) { |
| 2131 | Invalid = true; |
| 2132 | break; |
| 2133 | } |
| 2134 | } |
| 2135 | Converted.EndPack(); |
| 2136 | continue; |
| 2137 | } |
| 2138 | |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2139 | if (ArgIdx < NumArgs) { |
| 2140 | // Check the template argument we were given. |
| 2141 | if (CheckTemplateArgument(*Param, TemplateArgs[ArgIdx], Template, |
| 2142 | TemplateLoc, RAngleLoc, Converted)) |
| 2143 | return true; |
| 2144 | |
| 2145 | continue; |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 2146 | } |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2147 | |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2148 | // We have a default template argument that we will use. |
| 2149 | TemplateArgumentLoc Arg; |
| 2150 | |
| 2151 | // Retrieve the default template argument from the template |
| 2152 | // parameter. For each kind of template parameter, we substitute the |
| 2153 | // template arguments provided thus far and any "outer" template arguments |
| 2154 | // (when the template parameter was part of a nested template) into |
| 2155 | // the default argument. |
| 2156 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*Param)) { |
| 2157 | if (!TTP->hasDefaultArgument()) { |
| 2158 | assert((Invalid || PartialTemplateArgs) && "Missing default argument"); |
| 2159 | break; |
| 2160 | } |
| 2161 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2162 | TypeSourceInfo *ArgType = SubstDefaultTemplateArgument(*this, |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2163 | Template, |
| 2164 | TemplateLoc, |
| 2165 | RAngleLoc, |
| 2166 | TTP, |
| 2167 | Converted); |
| 2168 | if (!ArgType) |
| 2169 | return true; |
| 2170 | |
| 2171 | Arg = TemplateArgumentLoc(TemplateArgument(ArgType->getType()), |
| 2172 | ArgType); |
| 2173 | } else if (NonTypeTemplateParmDecl *NTTP |
| 2174 | = dyn_cast<NonTypeTemplateParmDecl>(*Param)) { |
| 2175 | if (!NTTP->hasDefaultArgument()) { |
| 2176 | assert((Invalid || PartialTemplateArgs) && "Missing default argument"); |
| 2177 | break; |
| 2178 | } |
| 2179 | |
| 2180 | Sema::OwningExprResult E = SubstDefaultTemplateArgument(*this, Template, |
| 2181 | TemplateLoc, |
| 2182 | RAngleLoc, |
| 2183 | NTTP, |
| 2184 | Converted); |
| 2185 | if (E.isInvalid()) |
| 2186 | return true; |
| 2187 | |
| 2188 | Expr *Ex = E.takeAs<Expr>(); |
| 2189 | Arg = TemplateArgumentLoc(TemplateArgument(Ex), Ex); |
| 2190 | } else { |
| 2191 | TemplateTemplateParmDecl *TempParm |
| 2192 | = cast<TemplateTemplateParmDecl>(*Param); |
| 2193 | |
| 2194 | if (!TempParm->hasDefaultArgument()) { |
| 2195 | assert((Invalid || PartialTemplateArgs) && "Missing default argument"); |
| 2196 | break; |
| 2197 | } |
| 2198 | |
| 2199 | TemplateName Name = SubstDefaultTemplateArgument(*this, Template, |
| 2200 | TemplateLoc, |
| 2201 | RAngleLoc, |
| 2202 | TempParm, |
| 2203 | Converted); |
| 2204 | if (Name.isNull()) |
| 2205 | return true; |
| 2206 | |
| 2207 | Arg = TemplateArgumentLoc(TemplateArgument(Name), |
| 2208 | TempParm->getDefaultArgument().getTemplateQualifierRange(), |
| 2209 | TempParm->getDefaultArgument().getTemplateNameLoc()); |
| 2210 | } |
| 2211 | |
| 2212 | // Introduce an instantiation record that describes where we are using |
| 2213 | // the default template argument. |
| 2214 | InstantiatingTemplate Instantiating(*this, RAngleLoc, Template, *Param, |
| 2215 | Converted.getFlatArguments(), |
| 2216 | Converted.flatSize(), |
| 2217 | SourceRange(TemplateLoc, RAngleLoc)); |
| 2218 | |
| 2219 | // Check the default template argument. |
Douglas Gregor | d9e1530 | 2009-11-11 19:41:09 +0000 | [diff] [blame] | 2220 | if (CheckTemplateArgument(*Param, Arg, Template, TemplateLoc, |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2221 | RAngleLoc, Converted)) |
| 2222 | return true; |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2223 | } |
| 2224 | |
| 2225 | return Invalid; |
| 2226 | } |
| 2227 | |
| 2228 | /// \brief Check a template argument against its corresponding |
| 2229 | /// template type parameter. |
| 2230 | /// |
| 2231 | /// This routine implements the semantics of C++ [temp.arg.type]. It |
| 2232 | /// returns true if an error occurred, and false otherwise. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2233 | bool Sema::CheckTemplateArgument(TemplateTypeParmDecl *Param, |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2234 | TypeSourceInfo *ArgInfo) { |
| 2235 | assert(ArgInfo && "invalid TypeSourceInfo"); |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2236 | QualType Arg = ArgInfo->getType(); |
| 2237 | |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2238 | // C++ [temp.arg.type]p2: |
| 2239 | // A local type, a type with no linkage, an unnamed type or a type |
| 2240 | // compounded from any of these types shall not be used as a |
| 2241 | // template-argument for a template type-parameter. |
| 2242 | // |
| 2243 | // FIXME: Perform the recursive and no-linkage type checks. |
| 2244 | const TagType *Tag = 0; |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2245 | if (const EnumType *EnumT = Arg->getAs<EnumType>()) |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2246 | Tag = EnumT; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2247 | else if (const RecordType *RecordT = Arg->getAs<RecordType>()) |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2248 | Tag = RecordT; |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2249 | if (Tag && Tag->getDecl()->getDeclContext()->isFunctionOrMethod()) { |
| 2250 | SourceRange SR = ArgInfo->getTypeLoc().getFullSourceRange(); |
| 2251 | return Diag(SR.getBegin(), diag::err_template_arg_local_type) |
| 2252 | << QualType(Tag, 0) << SR; |
| 2253 | } else if (Tag && !Tag->getDecl()->getDeclName() && |
Douglas Gregor | 9813753 | 2009-03-10 18:33:27 +0000 | [diff] [blame] | 2254 | !Tag->getDecl()->getTypedefForAnonDecl()) { |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2255 | SourceRange SR = ArgInfo->getTypeLoc().getFullSourceRange(); |
| 2256 | Diag(SR.getBegin(), diag::err_template_arg_unnamed_type) << SR; |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2257 | Diag(Tag->getDecl()->getLocation(), diag::note_template_unnamed_type_here); |
| 2258 | return true; |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 2259 | } else if (Context.hasSameUnqualifiedType(Arg, Context.OverloadTy)) { |
| 2260 | SourceRange SR = ArgInfo->getTypeLoc().getFullSourceRange(); |
| 2261 | return Diag(SR.getBegin(), diag::err_template_arg_overload_type) << SR; |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2262 | } |
| 2263 | |
| 2264 | return false; |
| 2265 | } |
| 2266 | |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2267 | /// \brief Checks whether the given template argument is the address |
| 2268 | /// of an object or function according to C++ [temp.arg.nontype]p1. |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 2269 | bool Sema::CheckTemplateArgumentAddressOfObjectOrFunction(Expr *Arg, |
| 2270 | NamedDecl *&Entity) { |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2271 | bool Invalid = false; |
| 2272 | |
| 2273 | // See through any implicit casts we added to fix the type. |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 2274 | while (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(Arg)) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2275 | Arg = Cast->getSubExpr(); |
| 2276 | |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 2277 | // C++0x allows nullptr, and there's no further checking to be done for that. |
| 2278 | if (Arg->getType()->isNullPtrType()) |
| 2279 | return false; |
| 2280 | |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2281 | // C++ [temp.arg.nontype]p1: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2282 | // |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2283 | // A template-argument for a non-type, non-template |
| 2284 | // template-parameter shall be one of: [...] |
| 2285 | // |
| 2286 | // -- the address of an object or function with external |
| 2287 | // linkage, including function templates and function |
| 2288 | // template-ids but excluding non-static class members, |
| 2289 | // expressed as & id-expression where the & is optional if |
| 2290 | // the name refers to a function or array, or if the |
| 2291 | // corresponding template-parameter is a reference; or |
| 2292 | DeclRefExpr *DRE = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2293 | |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2294 | // Ignore (and complain about) any excess parentheses. |
| 2295 | while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) { |
| 2296 | if (!Invalid) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2297 | Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2298 | diag::err_template_arg_extra_parens) |
| 2299 | << Arg->getSourceRange(); |
| 2300 | Invalid = true; |
| 2301 | } |
| 2302 | |
| 2303 | Arg = Parens->getSubExpr(); |
| 2304 | } |
| 2305 | |
| 2306 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) { |
| 2307 | if (UnOp->getOpcode() == UnaryOperator::AddrOf) |
| 2308 | DRE = dyn_cast<DeclRefExpr>(UnOp->getSubExpr()); |
| 2309 | } else |
| 2310 | DRE = dyn_cast<DeclRefExpr>(Arg); |
| 2311 | |
Chandler Carruth | 038cc39 | 2010-01-31 10:01:20 +0000 | [diff] [blame] | 2312 | if (!DRE) |
| 2313 | return Diag(Arg->getSourceRange().getBegin(), |
| 2314 | diag::err_template_arg_not_decl_ref) |
| 2315 | << Arg->getSourceRange(); |
| 2316 | |
| 2317 | // Stop checking the precise nature of the argument if it is value dependent, |
| 2318 | // it should be checked when instantiated. |
| 2319 | if (Arg->isValueDependent()) |
| 2320 | return false; |
| 2321 | |
| 2322 | if (!isa<ValueDecl>(DRE->getDecl())) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2323 | return Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2324 | diag::err_template_arg_not_object_or_func_form) |
| 2325 | << Arg->getSourceRange(); |
| 2326 | |
| 2327 | // Cannot refer to non-static data members |
| 2328 | if (FieldDecl *Field = dyn_cast<FieldDecl>(DRE->getDecl())) |
| 2329 | return Diag(Arg->getSourceRange().getBegin(), diag::err_template_arg_field) |
| 2330 | << Field << Arg->getSourceRange(); |
| 2331 | |
| 2332 | // Cannot refer to non-static member functions |
| 2333 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(DRE->getDecl())) |
| 2334 | if (!Method->isStatic()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2335 | return Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2336 | diag::err_template_arg_method) |
| 2337 | << Method << Arg->getSourceRange(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2338 | |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2339 | // Functions must have external linkage. |
| 2340 | if (FunctionDecl *Func = dyn_cast<FunctionDecl>(DRE->getDecl())) { |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 2341 | if (!isExternalLinkage(Func->getLinkage())) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2342 | Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2343 | diag::err_template_arg_function_not_extern) |
| 2344 | << Func << Arg->getSourceRange(); |
| 2345 | Diag(Func->getLocation(), diag::note_template_arg_internal_object) |
| 2346 | << true; |
| 2347 | return true; |
| 2348 | } |
| 2349 | |
| 2350 | // Okay: we've named a function with external linkage. |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 2351 | Entity = Func; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2352 | return Invalid; |
| 2353 | } |
| 2354 | |
| 2355 | if (VarDecl *Var = dyn_cast<VarDecl>(DRE->getDecl())) { |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 2356 | if (!isExternalLinkage(Var->getLinkage())) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2357 | Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2358 | diag::err_template_arg_object_not_extern) |
| 2359 | << Var << Arg->getSourceRange(); |
| 2360 | Diag(Var->getLocation(), diag::note_template_arg_internal_object) |
| 2361 | << true; |
| 2362 | return true; |
| 2363 | } |
| 2364 | |
| 2365 | // Okay: we've named an object with external linkage |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 2366 | Entity = Var; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2367 | return Invalid; |
| 2368 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2369 | |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2370 | // 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] | 2371 | Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2372 | diag::err_template_arg_not_object_or_func) |
| 2373 | << Arg->getSourceRange(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2374 | Diag(DRE->getDecl()->getLocation(), |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2375 | diag::note_template_arg_refers_here); |
| 2376 | return true; |
| 2377 | } |
| 2378 | |
| 2379 | /// \brief Checks whether the given template argument is a pointer to |
| 2380 | /// member constant according to C++ [temp.arg.nontype]p1. |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 2381 | bool Sema::CheckTemplateArgumentPointerToMember(Expr *Arg, |
| 2382 | TemplateArgument &Converted) { |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2383 | bool Invalid = false; |
| 2384 | |
| 2385 | // See through any implicit casts we added to fix the type. |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 2386 | while (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(Arg)) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2387 | Arg = Cast->getSubExpr(); |
| 2388 | |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 2389 | // C++0x allows nullptr, and there's no further checking to be done for that. |
| 2390 | if (Arg->getType()->isNullPtrType()) |
| 2391 | return false; |
| 2392 | |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2393 | // C++ [temp.arg.nontype]p1: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2394 | // |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2395 | // A template-argument for a non-type, non-template |
| 2396 | // template-parameter shall be one of: [...] |
| 2397 | // |
| 2398 | // -- a pointer to member expressed as described in 5.3.1. |
Douglas Gregor | a2813ce | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 2399 | DeclRefExpr *DRE = 0; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2400 | |
| 2401 | // Ignore (and complain about) any excess parentheses. |
| 2402 | while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) { |
| 2403 | if (!Invalid) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2404 | Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2405 | diag::err_template_arg_extra_parens) |
| 2406 | << Arg->getSourceRange(); |
| 2407 | Invalid = true; |
| 2408 | } |
| 2409 | |
| 2410 | Arg = Parens->getSubExpr(); |
| 2411 | } |
| 2412 | |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 2413 | // A pointer-to-member constant written &Class::member. |
| 2414 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) { |
Douglas Gregor | a2813ce | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 2415 | if (UnOp->getOpcode() == UnaryOperator::AddrOf) { |
| 2416 | DRE = dyn_cast<DeclRefExpr>(UnOp->getSubExpr()); |
| 2417 | if (DRE && !DRE->getQualifier()) |
| 2418 | DRE = 0; |
| 2419 | } |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 2420 | } |
| 2421 | // A constant of pointer-to-member type. |
| 2422 | else if ((DRE = dyn_cast<DeclRefExpr>(Arg))) { |
| 2423 | if (ValueDecl *VD = dyn_cast<ValueDecl>(DRE->getDecl())) { |
| 2424 | if (VD->getType()->isMemberPointerType()) { |
| 2425 | if (isa<NonTypeTemplateParmDecl>(VD) || |
| 2426 | (isa<VarDecl>(VD) && |
| 2427 | Context.getCanonicalType(VD->getType()).isConstQualified())) { |
| 2428 | if (Arg->isTypeDependent() || Arg->isValueDependent()) |
| 2429 | Converted = TemplateArgument(Arg->Retain()); |
| 2430 | else |
| 2431 | Converted = TemplateArgument(VD->getCanonicalDecl()); |
| 2432 | return Invalid; |
| 2433 | } |
| 2434 | } |
| 2435 | } |
| 2436 | |
| 2437 | DRE = 0; |
| 2438 | } |
| 2439 | |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2440 | if (!DRE) |
| 2441 | return Diag(Arg->getSourceRange().getBegin(), |
| 2442 | diag::err_template_arg_not_pointer_to_member_form) |
| 2443 | << Arg->getSourceRange(); |
| 2444 | |
| 2445 | if (isa<FieldDecl>(DRE->getDecl()) || isa<CXXMethodDecl>(DRE->getDecl())) { |
| 2446 | assert((isa<FieldDecl>(DRE->getDecl()) || |
| 2447 | !cast<CXXMethodDecl>(DRE->getDecl())->isStatic()) && |
| 2448 | "Only non-static member pointers can make it here"); |
| 2449 | |
| 2450 | // Okay: this is the address of a non-static member, and therefore |
| 2451 | // a member pointer constant. |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 2452 | if (Arg->isTypeDependent() || Arg->isValueDependent()) |
| 2453 | Converted = TemplateArgument(Arg->Retain()); |
| 2454 | else |
| 2455 | Converted = TemplateArgument(DRE->getDecl()->getCanonicalDecl()); |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2456 | return Invalid; |
| 2457 | } |
| 2458 | |
| 2459 | // 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] | 2460 | Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2461 | diag::err_template_arg_not_pointer_to_member_form) |
| 2462 | << Arg->getSourceRange(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2463 | Diag(DRE->getDecl()->getLocation(), |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2464 | diag::note_template_arg_refers_here); |
| 2465 | return true; |
| 2466 | } |
| 2467 | |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2468 | /// \brief Check a template argument against its corresponding |
| 2469 | /// non-type template parameter. |
| 2470 | /// |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 2471 | /// This routine implements the semantics of C++ [temp.arg.nontype]. |
| 2472 | /// It returns true if an error occurred, and false otherwise. \p |
| 2473 | /// InstantiatedParamType is the type of the non-type template |
| 2474 | /// parameter after it has been instantiated. |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 2475 | /// |
Douglas Gregor | 02cbbd2 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 2476 | /// If no error was detected, Converted receives the converted template argument. |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2477 | bool Sema::CheckTemplateArgument(NonTypeTemplateParmDecl *Param, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2478 | QualType InstantiatedParamType, Expr *&Arg, |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 2479 | TemplateArgument &Converted, |
| 2480 | CheckTemplateArgumentKind CTAK) { |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2481 | SourceLocation StartLoc = Arg->getSourceRange().getBegin(); |
| 2482 | |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 2483 | // If either the parameter has a dependent type or the argument is |
| 2484 | // type-dependent, there's nothing we can check now. |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 2485 | // FIXME: Add template argument to Converted! |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2486 | if (InstantiatedParamType->isDependentType() || Arg->isTypeDependent()) { |
| 2487 | // FIXME: Produce a cloned, canonical expression? |
Douglas Gregor | 02cbbd2 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 2488 | Converted = TemplateArgument(Arg); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 2489 | return false; |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2490 | } |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 2491 | |
| 2492 | // C++ [temp.arg.nontype]p5: |
| 2493 | // The following conversions are performed on each expression used |
| 2494 | // as a non-type template-argument. If a non-type |
| 2495 | // template-argument cannot be converted to the type of the |
| 2496 | // corresponding template-parameter then the program is |
| 2497 | // ill-formed. |
| 2498 | // |
| 2499 | // -- for a non-type template-parameter of integral or |
| 2500 | // enumeration type, integral promotions (4.5) and integral |
| 2501 | // conversions (4.7) are applied. |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 2502 | QualType ParamType = InstantiatedParamType; |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 2503 | QualType ArgType = Arg->getType(); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 2504 | if (ParamType->isIntegralType() || ParamType->isEnumeralType()) { |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 2505 | // C++ [temp.arg.nontype]p1: |
| 2506 | // A template-argument for a non-type, non-template |
| 2507 | // template-parameter shall be one of: |
| 2508 | // |
| 2509 | // -- an integral constant-expression of integral or enumeration |
| 2510 | // type; or |
| 2511 | // -- the name of a non-type template-parameter; or |
| 2512 | SourceLocation NonConstantLoc; |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 2513 | llvm::APSInt Value; |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 2514 | if (!ArgType->isIntegralType() && !ArgType->isEnumeralType()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2515 | Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 2516 | diag::err_template_arg_not_integral_or_enumeral) |
| 2517 | << ArgType << Arg->getSourceRange(); |
| 2518 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 2519 | return true; |
| 2520 | } else if (!Arg->isValueDependent() && |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 2521 | !Arg->isIntegerConstantExpr(Value, Context, &NonConstantLoc)) { |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 2522 | Diag(NonConstantLoc, diag::err_template_arg_not_ice) |
| 2523 | << ArgType << Arg->getSourceRange(); |
| 2524 | return true; |
| 2525 | } |
| 2526 | |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 2527 | // From here on out, all we care about are the unqualified forms |
| 2528 | // of the parameter and argument types. |
| 2529 | ParamType = ParamType.getUnqualifiedType(); |
| 2530 | ArgType = ArgType.getUnqualifiedType(); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 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 |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 2535 | } else if (CTAK == CTAK_Deduced) { |
| 2536 | // C++ [temp.deduct.type]p17: |
| 2537 | // If, in the declaration of a function template with a non-type |
| 2538 | // template-parameter, the non-type template- parameter is used |
| 2539 | // in an expression in the function parameter-list and, if the |
| 2540 | // corresponding template-argument is deduced, the |
| 2541 | // template-argument type shall match the type of the |
| 2542 | // template-parameter exactly, except that a template-argument |
| 2543 | // deduced from an array bound may be of any integral type. |
| 2544 | Diag(StartLoc, diag::err_deduced_non_type_template_arg_type_mismatch) |
| 2545 | << ArgType << ParamType; |
| 2546 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 2547 | return true; |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 2548 | } else if (IsIntegralPromotion(Arg, ArgType, ParamType) || |
| 2549 | !ParamType->isEnumeralType()) { |
| 2550 | // This is an integral promotion or conversion. |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 2551 | ImpCastExprToType(Arg, ParamType, CastExpr::CK_IntegralCast); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 2552 | } else { |
| 2553 | // We can't perform this conversion. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2554 | Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 2555 | diag::err_template_arg_not_convertible) |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 2556 | << Arg->getType() << InstantiatedParamType << Arg->getSourceRange(); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 2557 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 2558 | return true; |
| 2559 | } |
| 2560 | |
Douglas Gregor | f80a9d5 | 2009-03-14 00:20:21 +0000 | [diff] [blame] | 2561 | QualType IntegerType = Context.getCanonicalType(ParamType); |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2562 | if (const EnumType *Enum = IntegerType->getAs<EnumType>()) |
Douglas Gregor | 02cbbd2 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 2563 | IntegerType = Context.getCanonicalType(Enum->getDecl()->getIntegerType()); |
Douglas Gregor | f80a9d5 | 2009-03-14 00:20:21 +0000 | [diff] [blame] | 2564 | |
| 2565 | if (!Arg->isValueDependent()) { |
Douglas Gregor | 1a6e034 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 2566 | llvm::APSInt OldValue = Value; |
| 2567 | |
| 2568 | // Coerce the template argument's value to the value it will have |
| 2569 | // based on the template parameter's type. |
Douglas Gregor | 0d4fd8e | 2010-03-26 00:39:40 +0000 | [diff] [blame] | 2570 | unsigned AllowedBits = Context.getTypeSize(IntegerType); |
Douglas Gregor | 0d4fd8e | 2010-03-26 00:39:40 +0000 | [diff] [blame] | 2571 | if (Value.getBitWidth() != AllowedBits) |
| 2572 | Value.extOrTrunc(AllowedBits); |
| 2573 | Value.setIsSigned(IntegerType->isSignedIntegerType()); |
Douglas Gregor | 1a6e034 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 2574 | |
| 2575 | // Complain if an unsigned parameter received a negative value. |
| 2576 | if (IntegerType->isUnsignedIntegerType() |
| 2577 | && (OldValue.isSigned() && OldValue.isNegative())) { |
| 2578 | Diag(Arg->getSourceRange().getBegin(), diag::warn_template_arg_negative) |
| 2579 | << OldValue.toString(10) << Value.toString(10) << Param->getType() |
| 2580 | << Arg->getSourceRange(); |
| 2581 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 2582 | } |
| 2583 | |
| 2584 | // Complain if we overflowed the template parameter's type. |
| 2585 | unsigned RequiredBits; |
| 2586 | if (IntegerType->isUnsignedIntegerType()) |
| 2587 | RequiredBits = OldValue.getActiveBits(); |
| 2588 | else if (OldValue.isUnsigned()) |
| 2589 | RequiredBits = OldValue.getActiveBits() + 1; |
| 2590 | else |
| 2591 | RequiredBits = OldValue.getMinSignedBits(); |
| 2592 | if (RequiredBits > AllowedBits) { |
| 2593 | Diag(Arg->getSourceRange().getBegin(), |
| 2594 | diag::warn_template_arg_too_large) |
| 2595 | << OldValue.toString(10) << Value.toString(10) << Param->getType() |
| 2596 | << Arg->getSourceRange(); |
| 2597 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 2598 | } |
Douglas Gregor | f80a9d5 | 2009-03-14 00:20:21 +0000 | [diff] [blame] | 2599 | } |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 2600 | |
Douglas Gregor | 02cbbd2 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 2601 | // Add the value of this argument to the list of converted |
| 2602 | // arguments. We use the bitwidth and signedness of the template |
| 2603 | // parameter. |
| 2604 | if (Arg->isValueDependent()) { |
| 2605 | // The argument is value-dependent. Create a new |
| 2606 | // TemplateArgument with the converted expression. |
| 2607 | Converted = TemplateArgument(Arg); |
| 2608 | return false; |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 2609 | } |
| 2610 | |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2611 | Converted = TemplateArgument(Value, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2612 | ParamType->isEnumeralType() ? ParamType |
Douglas Gregor | 02cbbd2 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 2613 | : IntegerType); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 2614 | return false; |
| 2615 | } |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 2616 | |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2617 | DeclAccessPair FoundResult; // temporary for ResolveOverloadedFunction |
| 2618 | |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 2619 | // Handle pointer-to-function, reference-to-function, and |
| 2620 | // pointer-to-member-function all in (roughly) the same way. |
| 2621 | if (// -- For a non-type template-parameter of type pointer to |
| 2622 | // function, only the function-to-pointer conversion (4.3) is |
| 2623 | // applied. If the template-argument represents a set of |
| 2624 | // overloaded functions (or a pointer to such), the matching |
| 2625 | // function is selected from the set (13.4). |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 2626 | // In C++0x, any std::nullptr_t value can be converted. |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 2627 | (ParamType->isPointerType() && |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2628 | ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType()) || |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 2629 | // -- For a non-type template-parameter of type reference to |
| 2630 | // function, no conversions apply. If the template-argument |
| 2631 | // represents a set of overloaded functions, the matching |
| 2632 | // function is selected from the set (13.4). |
| 2633 | (ParamType->isReferenceType() && |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2634 | ParamType->getAs<ReferenceType>()->getPointeeType()->isFunctionType()) || |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 2635 | // -- For a non-type template-parameter of type pointer to |
| 2636 | // member function, no conversions apply. If the |
| 2637 | // template-argument represents a set of overloaded member |
| 2638 | // functions, the matching member function is selected from |
| 2639 | // the set (13.4). |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 2640 | // Again, C++0x allows a std::nullptr_t value. |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 2641 | (ParamType->isMemberPointerType() && |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2642 | ParamType->getAs<MemberPointerType>()->getPointeeType() |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 2643 | ->isFunctionType())) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2644 | if (Context.hasSameUnqualifiedType(ArgType, |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2645 | ParamType.getNonReferenceType())) { |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 2646 | // We don't have to do anything: the types already match. |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 2647 | } else if (ArgType->isNullPtrType() && (ParamType->isPointerType() || |
| 2648 | ParamType->isMemberPointerType())) { |
| 2649 | ArgType = ParamType; |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 2650 | if (ParamType->isMemberPointerType()) |
| 2651 | ImpCastExprToType(Arg, ParamType, CastExpr::CK_NullToMemberPointer); |
| 2652 | else |
| 2653 | ImpCastExprToType(Arg, ParamType, CastExpr::CK_BitCast); |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 2654 | } else if (ArgType->isFunctionType() && ParamType->isPointerType()) { |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 2655 | ArgType = Context.getPointerType(ArgType); |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 2656 | ImpCastExprToType(Arg, ArgType, CastExpr::CK_FunctionToPointerDecay); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2657 | } else if (FunctionDecl *Fn |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2658 | = ResolveAddressOfOverloadedFunction(Arg, ParamType, true, |
| 2659 | FoundResult)) { |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 2660 | if (DiagnoseUseOfDecl(Fn, Arg->getSourceRange().getBegin())) |
| 2661 | return true; |
| 2662 | |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2663 | Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn); |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 2664 | ArgType = Arg->getType(); |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 2665 | if (ArgType->isFunctionType() && ParamType->isPointerType()) { |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 2666 | ArgType = Context.getPointerType(Arg->getType()); |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 2667 | ImpCastExprToType(Arg, ArgType, CastExpr::CK_FunctionToPointerDecay); |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 2668 | } |
| 2669 | } |
| 2670 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2671 | if (!Context.hasSameUnqualifiedType(ArgType, |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2672 | ParamType.getNonReferenceType())) { |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 2673 | // We can't perform this conversion. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2674 | Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 2675 | diag::err_template_arg_not_convertible) |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 2676 | << Arg->getType() << InstantiatedParamType << Arg->getSourceRange(); |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 2677 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 2678 | return true; |
| 2679 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2680 | |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 2681 | if (ParamType->isMemberPointerType()) |
| 2682 | return CheckTemplateArgumentPointerToMember(Arg, Converted); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2683 | |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 2684 | NamedDecl *Entity = 0; |
| 2685 | if (CheckTemplateArgumentAddressOfObjectOrFunction(Arg, Entity)) |
| 2686 | return true; |
| 2687 | |
Chandler Carruth | 038cc39 | 2010-01-31 10:01:20 +0000 | [diff] [blame] | 2688 | if (Arg->isValueDependent()) { |
| 2689 | Converted = TemplateArgument(Arg); |
| 2690 | } else { |
| 2691 | if (Entity) |
| 2692 | Entity = cast<NamedDecl>(Entity->getCanonicalDecl()); |
| 2693 | Converted = TemplateArgument(Entity); |
| 2694 | } |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 2695 | return false; |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 2696 | } |
| 2697 | |
Chris Lattner | fe90de7 | 2009-02-20 21:37:53 +0000 | [diff] [blame] | 2698 | if (ParamType->isPointerType()) { |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 2699 | // -- for a non-type template-parameter of type pointer to |
| 2700 | // object, qualification conversions (4.4) and the |
| 2701 | // array-to-pointer conversion (4.2) are applied. |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 2702 | // C++0x also allows a value of std::nullptr_t. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2703 | assert(ParamType->getAs<PointerType>()->getPointeeType()->isObjectType() && |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 2704 | "Only object pointers allowed here"); |
Douglas Gregor | f684e6e | 2009-02-11 00:44:29 +0000 | [diff] [blame] | 2705 | |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 2706 | if (ArgType->isNullPtrType()) { |
| 2707 | ArgType = ParamType; |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 2708 | ImpCastExprToType(Arg, ParamType, CastExpr::CK_BitCast); |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 2709 | } else if (ArgType->isArrayType()) { |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 2710 | ArgType = Context.getArrayDecayedType(ArgType); |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 2711 | ImpCastExprToType(Arg, ArgType, CastExpr::CK_ArrayToPointerDecay); |
Douglas Gregor | f684e6e | 2009-02-11 00:44:29 +0000 | [diff] [blame] | 2712 | } |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 2713 | |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 2714 | if (IsQualificationConversion(ArgType, ParamType)) { |
| 2715 | ArgType = ParamType; |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 2716 | ImpCastExprToType(Arg, ParamType, CastExpr::CK_NoOp); |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 2717 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2718 | |
Douglas Gregor | 8e6563b | 2009-02-11 18:22:40 +0000 | [diff] [blame] | 2719 | if (!Context.hasSameUnqualifiedType(ArgType, ParamType)) { |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 2720 | // We can't perform this conversion. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2721 | Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 2722 | diag::err_template_arg_not_convertible) |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 2723 | << Arg->getType() << InstantiatedParamType << Arg->getSourceRange(); |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 2724 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 2725 | return true; |
| 2726 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2727 | |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 2728 | NamedDecl *Entity = 0; |
| 2729 | if (CheckTemplateArgumentAddressOfObjectOrFunction(Arg, Entity)) |
| 2730 | return true; |
| 2731 | |
Chandler Carruth | 038cc39 | 2010-01-31 10:01:20 +0000 | [diff] [blame] | 2732 | if (Arg->isValueDependent()) { |
| 2733 | Converted = TemplateArgument(Arg); |
| 2734 | } else { |
| 2735 | if (Entity) |
| 2736 | Entity = cast<NamedDecl>(Entity->getCanonicalDecl()); |
| 2737 | Converted = TemplateArgument(Entity); |
| 2738 | } |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 2739 | return false; |
Douglas Gregor | f684e6e | 2009-02-11 00:44:29 +0000 | [diff] [blame] | 2740 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2741 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2742 | if (const ReferenceType *ParamRefType = ParamType->getAs<ReferenceType>()) { |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 2743 | // -- For a non-type template-parameter of type reference to |
| 2744 | // object, no conversions apply. The type referred to by the |
| 2745 | // reference may be more cv-qualified than the (otherwise |
| 2746 | // identical) type of the template-argument. The |
| 2747 | // template-parameter is bound directly to the |
| 2748 | // template-argument, which must be an lvalue. |
Douglas Gregor | bad0e65 | 2009-03-24 20:32:41 +0000 | [diff] [blame] | 2749 | assert(ParamRefType->getPointeeType()->isObjectType() && |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 2750 | "Only object references allowed here"); |
Douglas Gregor | f684e6e | 2009-02-11 00:44:29 +0000 | [diff] [blame] | 2751 | |
Chandler Carruth | 5147fa6 | 2010-02-03 09:37:33 +0000 | [diff] [blame] | 2752 | QualType ReferredType = ParamRefType->getPointeeType(); |
| 2753 | if (!Context.hasSameUnqualifiedType(ReferredType, ArgType)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2754 | Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 2755 | diag::err_template_arg_no_ref_bind) |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 2756 | << InstantiatedParamType << Arg->getType() |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 2757 | << Arg->getSourceRange(); |
| 2758 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 2759 | return true; |
| 2760 | } |
| 2761 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2762 | unsigned ParamQuals |
Chandler Carruth | 5147fa6 | 2010-02-03 09:37:33 +0000 | [diff] [blame] | 2763 | = Context.getCanonicalType(ReferredType).getCVRQualifiers(); |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 2764 | unsigned ArgQuals = Context.getCanonicalType(ArgType).getCVRQualifiers(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2765 | |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 2766 | if ((ParamQuals | ArgQuals) != ParamQuals) { |
| 2767 | Diag(Arg->getSourceRange().getBegin(), |
| 2768 | diag::err_template_arg_ref_bind_ignores_quals) |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 2769 | << InstantiatedParamType << Arg->getType() |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 2770 | << Arg->getSourceRange(); |
| 2771 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 2772 | return true; |
| 2773 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2774 | |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 2775 | NamedDecl *Entity = 0; |
| 2776 | if (CheckTemplateArgumentAddressOfObjectOrFunction(Arg, Entity)) |
| 2777 | return true; |
| 2778 | |
Chandler Carruth | 038cc39 | 2010-01-31 10:01:20 +0000 | [diff] [blame] | 2779 | if (Arg->isValueDependent()) { |
| 2780 | Converted = TemplateArgument(Arg); |
| 2781 | } else { |
| 2782 | Entity = cast<NamedDecl>(Entity->getCanonicalDecl()); |
| 2783 | Converted = TemplateArgument(Entity); |
| 2784 | } |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 2785 | return false; |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 2786 | } |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 2787 | |
| 2788 | // -- For a non-type template-parameter of type pointer to data |
| 2789 | // member, qualification conversions (4.4) are applied. |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 2790 | // C++0x allows std::nullptr_t values. |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 2791 | assert(ParamType->isMemberPointerType() && "Only pointers to members remain"); |
| 2792 | |
Douglas Gregor | 8e6563b | 2009-02-11 18:22:40 +0000 | [diff] [blame] | 2793 | if (Context.hasSameUnqualifiedType(ParamType, ArgType)) { |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 2794 | // Types match exactly: nothing more to do here. |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 2795 | } else if (ArgType->isNullPtrType()) { |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 2796 | ImpCastExprToType(Arg, ParamType, CastExpr::CK_NullToMemberPointer); |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 2797 | } else if (IsQualificationConversion(ArgType, ParamType)) { |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 2798 | ImpCastExprToType(Arg, ParamType, CastExpr::CK_NoOp); |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 2799 | } else { |
| 2800 | // We can't perform this conversion. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2801 | Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 2802 | diag::err_template_arg_not_convertible) |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 2803 | << Arg->getType() << InstantiatedParamType << Arg->getSourceRange(); |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 2804 | Diag(Param->getLocation(), diag::note_template_param_here); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2805 | return true; |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 2806 | } |
| 2807 | |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 2808 | return CheckTemplateArgumentPointerToMember(Arg, Converted); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2809 | } |
| 2810 | |
| 2811 | /// \brief Check a template argument against its corresponding |
| 2812 | /// template template parameter. |
| 2813 | /// |
| 2814 | /// This routine implements the semantics of C++ [temp.arg.template]. |
| 2815 | /// It returns true if an error occurred, and false otherwise. |
| 2816 | bool Sema::CheckTemplateArgument(TemplateTemplateParmDecl *Param, |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2817 | const TemplateArgumentLoc &Arg) { |
| 2818 | TemplateName Name = Arg.getArgument().getAsTemplate(); |
| 2819 | TemplateDecl *Template = Name.getAsTemplateDecl(); |
| 2820 | if (!Template) { |
| 2821 | // Any dependent template name is fine. |
| 2822 | assert(Name.isDependent() && "Non-dependent template isn't a declaration?"); |
| 2823 | return false; |
| 2824 | } |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 2825 | |
| 2826 | // C++ [temp.arg.template]p1: |
| 2827 | // A template-argument for a template template-parameter shall be |
| 2828 | // the name of a class template, expressed as id-expression. Only |
| 2829 | // primary class templates are considered when matching the |
| 2830 | // template template argument with the corresponding parameter; |
| 2831 | // partial specializations are not considered even if their |
| 2832 | // parameter lists match that of the template template parameter. |
Douglas Gregor | ba1ecb5 | 2009-06-12 19:43:02 +0000 | [diff] [blame] | 2833 | // |
| 2834 | // Note that we also allow template template parameters here, which |
| 2835 | // will happen when we are dealing with, e.g., class template |
| 2836 | // partial specializations. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2837 | if (!isa<ClassTemplateDecl>(Template) && |
Douglas Gregor | ba1ecb5 | 2009-06-12 19:43:02 +0000 | [diff] [blame] | 2838 | !isa<TemplateTemplateParmDecl>(Template)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2839 | assert(isa<FunctionTemplateDecl>(Template) && |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 2840 | "Only function templates are possible here"); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2841 | Diag(Arg.getLocation(), diag::err_template_arg_not_class_template); |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 2842 | Diag(Template->getLocation(), diag::note_template_arg_refers_here_func) |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 2843 | << Template; |
| 2844 | } |
| 2845 | |
| 2846 | return !TemplateParameterListsAreEqual(Template->getTemplateParameters(), |
| 2847 | Param->getTemplateParameters(), |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 2848 | true, |
| 2849 | TPL_TemplateTemplateArgumentMatch, |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2850 | Arg.getLocation()); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2851 | } |
| 2852 | |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 2853 | /// \brief Given a non-type template argument that refers to a |
| 2854 | /// declaration and the type of its corresponding non-type template |
| 2855 | /// parameter, produce an expression that properly refers to that |
| 2856 | /// declaration. |
| 2857 | Sema::OwningExprResult |
| 2858 | Sema::BuildExpressionFromDeclTemplateArgument(const TemplateArgument &Arg, |
| 2859 | QualType ParamType, |
| 2860 | SourceLocation Loc) { |
| 2861 | assert(Arg.getKind() == TemplateArgument::Declaration && |
| 2862 | "Only declaration template arguments permitted here"); |
| 2863 | ValueDecl *VD = cast<ValueDecl>(Arg.getAsDecl()); |
| 2864 | |
| 2865 | if (VD->getDeclContext()->isRecord() && |
| 2866 | (isa<CXXMethodDecl>(VD) || isa<FieldDecl>(VD))) { |
| 2867 | // If the value is a class member, we might have a pointer-to-member. |
| 2868 | // Determine whether the non-type template template parameter is of |
| 2869 | // pointer-to-member type. If so, we need to build an appropriate |
| 2870 | // expression for a pointer-to-member, since a "normal" DeclRefExpr |
| 2871 | // would refer to the member itself. |
| 2872 | if (ParamType->isMemberPointerType()) { |
| 2873 | QualType ClassType |
| 2874 | = Context.getTypeDeclType(cast<RecordDecl>(VD->getDeclContext())); |
| 2875 | NestedNameSpecifier *Qualifier |
| 2876 | = NestedNameSpecifier::Create(Context, 0, false, ClassType.getTypePtr()); |
| 2877 | CXXScopeSpec SS; |
| 2878 | SS.setScopeRep(Qualifier); |
| 2879 | OwningExprResult RefExpr = BuildDeclRefExpr(VD, |
| 2880 | VD->getType().getNonReferenceType(), |
| 2881 | Loc, |
| 2882 | &SS); |
| 2883 | if (RefExpr.isInvalid()) |
| 2884 | return ExprError(); |
| 2885 | |
| 2886 | RefExpr = CreateBuiltinUnaryOp(Loc, UnaryOperator::AddrOf, move(RefExpr)); |
| 2887 | assert(!RefExpr.isInvalid() && |
| 2888 | Context.hasSameType(((Expr*) RefExpr.get())->getType(), |
| 2889 | ParamType)); |
| 2890 | return move(RefExpr); |
| 2891 | } |
| 2892 | } |
| 2893 | |
| 2894 | QualType T = VD->getType().getNonReferenceType(); |
| 2895 | if (ParamType->isPointerType()) { |
| 2896 | // C++03 [temp.arg.nontype]p5: |
| 2897 | // - For a non-type template-parameter of type pointer to |
| 2898 | // object, qualification conversions and the array-to-pointer |
| 2899 | // conversion are applied. |
| 2900 | // - For a non-type template-parameter of type pointer to |
| 2901 | // function, only the function-to-pointer conversion is |
| 2902 | // applied. |
| 2903 | OwningExprResult RefExpr = BuildDeclRefExpr(VD, T, Loc); |
| 2904 | if (RefExpr.isInvalid()) |
| 2905 | return ExprError(); |
| 2906 | |
| 2907 | // Decay functions and arrays. |
| 2908 | Expr *RefE = (Expr *)RefExpr.get(); |
| 2909 | DefaultFunctionArrayConversion(RefE); |
| 2910 | if (RefE != RefExpr.get()) { |
| 2911 | RefExpr.release(); |
| 2912 | RefExpr = Owned(RefE); |
| 2913 | } |
| 2914 | |
| 2915 | // Qualification conversions. |
| 2916 | RefExpr.release(); |
| 2917 | ImpCastExprToType(RefE, ParamType.getUnqualifiedType(), CastExpr::CK_NoOp); |
| 2918 | return Owned(RefE); |
| 2919 | } |
| 2920 | |
| 2921 | // If the non-type template parameter has reference type, qualify the |
| 2922 | // resulting declaration reference with the extra qualifiers on the |
| 2923 | // type that the reference refers to. |
| 2924 | if (const ReferenceType *TargetRef = ParamType->getAs<ReferenceType>()) |
| 2925 | T = Context.getQualifiedType(T, TargetRef->getPointeeType().getQualifiers()); |
| 2926 | |
| 2927 | return BuildDeclRefExpr(VD, T, Loc); |
| 2928 | } |
| 2929 | |
| 2930 | /// \brief Construct a new expression that refers to the given |
| 2931 | /// integral template argument with the given source-location |
| 2932 | /// information. |
| 2933 | /// |
| 2934 | /// This routine takes care of the mapping from an integral template |
| 2935 | /// argument (which may have any integral type) to the appropriate |
| 2936 | /// literal value. |
| 2937 | Sema::OwningExprResult |
| 2938 | Sema::BuildExpressionFromIntegralTemplateArgument(const TemplateArgument &Arg, |
| 2939 | SourceLocation Loc) { |
| 2940 | assert(Arg.getKind() == TemplateArgument::Integral && |
| 2941 | "Operation is only value for integral template arguments"); |
| 2942 | QualType T = Arg.getIntegralType(); |
| 2943 | if (T->isCharType() || T->isWideCharType()) |
| 2944 | return Owned(new (Context) CharacterLiteral( |
| 2945 | Arg.getAsIntegral()->getZExtValue(), |
| 2946 | T->isWideCharType(), |
| 2947 | T, |
| 2948 | Loc)); |
| 2949 | if (T->isBooleanType()) |
| 2950 | return Owned(new (Context) CXXBoolLiteralExpr( |
| 2951 | Arg.getAsIntegral()->getBoolValue(), |
| 2952 | T, |
| 2953 | Loc)); |
| 2954 | |
| 2955 | return Owned(new (Context) IntegerLiteral(*Arg.getAsIntegral(), T, Loc)); |
| 2956 | } |
| 2957 | |
| 2958 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 2959 | /// \brief Determine whether the given template parameter lists are |
| 2960 | /// equivalent. |
| 2961 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2962 | /// \param New The new template parameter list, typically written in the |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 2963 | /// source code as part of a new template declaration. |
| 2964 | /// |
| 2965 | /// \param Old The old template parameter list, typically found via |
| 2966 | /// name lookup of the template declared with this template parameter |
| 2967 | /// list. |
| 2968 | /// |
| 2969 | /// \param Complain If true, this routine will produce a diagnostic if |
| 2970 | /// the template parameter lists are not equivalent. |
| 2971 | /// |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 2972 | /// \param Kind describes how we are to match the template parameter lists. |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 2973 | /// |
| 2974 | /// \param TemplateArgLoc If this source location is valid, then we |
| 2975 | /// are actually checking the template parameter list of a template |
| 2976 | /// argument (New) against the template parameter list of its |
| 2977 | /// corresponding template template parameter (Old). We produce |
| 2978 | /// slightly different diagnostics in this scenario. |
| 2979 | /// |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 2980 | /// \returns True if the template parameter lists are equal, false |
| 2981 | /// otherwise. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2982 | bool |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 2983 | Sema::TemplateParameterListsAreEqual(TemplateParameterList *New, |
| 2984 | TemplateParameterList *Old, |
| 2985 | bool Complain, |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 2986 | TemplateParameterListEqualKind Kind, |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 2987 | SourceLocation TemplateArgLoc) { |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 2988 | if (Old->size() != New->size()) { |
| 2989 | if (Complain) { |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 2990 | unsigned NextDiag = diag::err_template_param_list_different_arity; |
| 2991 | if (TemplateArgLoc.isValid()) { |
| 2992 | Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch); |
| 2993 | NextDiag = diag::note_template_param_list_different_arity; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2994 | } |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 2995 | Diag(New->getTemplateLoc(), NextDiag) |
| 2996 | << (New->size() > Old->size()) |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 2997 | << (Kind != TPL_TemplateMatch) |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 2998 | << SourceRange(New->getTemplateLoc(), New->getRAngleLoc()); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 2999 | Diag(Old->getTemplateLoc(), diag::note_template_prev_declaration) |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 3000 | << (Kind != TPL_TemplateMatch) |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3001 | << SourceRange(Old->getTemplateLoc(), Old->getRAngleLoc()); |
| 3002 | } |
| 3003 | |
| 3004 | return false; |
| 3005 | } |
| 3006 | |
| 3007 | for (TemplateParameterList::iterator OldParm = Old->begin(), |
| 3008 | OldParmEnd = Old->end(), NewParm = New->begin(); |
| 3009 | OldParm != OldParmEnd; ++OldParm, ++NewParm) { |
| 3010 | if ((*OldParm)->getKind() != (*NewParm)->getKind()) { |
Douglas Gregor | 34d1dc9 | 2009-06-24 16:50:40 +0000 | [diff] [blame] | 3011 | if (Complain) { |
| 3012 | unsigned NextDiag = diag::err_template_param_different_kind; |
| 3013 | if (TemplateArgLoc.isValid()) { |
| 3014 | Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch); |
| 3015 | NextDiag = diag::note_template_param_different_kind; |
| 3016 | } |
| 3017 | Diag((*NewParm)->getLocation(), NextDiag) |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 3018 | << (Kind != TPL_TemplateMatch); |
Douglas Gregor | 34d1dc9 | 2009-06-24 16:50:40 +0000 | [diff] [blame] | 3019 | Diag((*OldParm)->getLocation(), diag::note_template_prev_declaration) |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 3020 | << (Kind != TPL_TemplateMatch); |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 3021 | } |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3022 | return false; |
| 3023 | } |
| 3024 | |
| 3025 | if (isa<TemplateTypeParmDecl>(*OldParm)) { |
| 3026 | // Okay; all template type parameters are equivalent (since we |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 3027 | // know we're at the same index). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3028 | } else if (NonTypeTemplateParmDecl *OldNTTP |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3029 | = dyn_cast<NonTypeTemplateParmDecl>(*OldParm)) { |
| 3030 | // The types of non-type template parameters must agree. |
| 3031 | NonTypeTemplateParmDecl *NewNTTP |
| 3032 | = cast<NonTypeTemplateParmDecl>(*NewParm); |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 3033 | |
| 3034 | // If we are matching a template template argument to a template |
| 3035 | // template parameter and one of the non-type template parameter types |
| 3036 | // is dependent, then we must wait until template instantiation time |
| 3037 | // to actually compare the arguments. |
| 3038 | if (Kind == TPL_TemplateTemplateArgumentMatch && |
| 3039 | (OldNTTP->getType()->isDependentType() || |
| 3040 | NewNTTP->getType()->isDependentType())) |
| 3041 | continue; |
| 3042 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3043 | if (Context.getCanonicalType(OldNTTP->getType()) != |
| 3044 | Context.getCanonicalType(NewNTTP->getType())) { |
| 3045 | if (Complain) { |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 3046 | unsigned NextDiag = diag::err_template_nontype_parm_different_type; |
| 3047 | if (TemplateArgLoc.isValid()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3048 | Diag(TemplateArgLoc, |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 3049 | diag::err_template_arg_template_params_mismatch); |
| 3050 | NextDiag = diag::note_template_nontype_parm_different_type; |
| 3051 | } |
| 3052 | Diag(NewNTTP->getLocation(), NextDiag) |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3053 | << NewNTTP->getType() |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 3054 | << (Kind != TPL_TemplateMatch); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3055 | Diag(OldNTTP->getLocation(), |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3056 | diag::note_template_nontype_parm_prev_declaration) |
| 3057 | << OldNTTP->getType(); |
| 3058 | } |
| 3059 | return false; |
| 3060 | } |
| 3061 | } else { |
| 3062 | // The template parameter lists of template template |
| 3063 | // parameters must agree. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3064 | assert(isa<TemplateTemplateParmDecl>(*OldParm) && |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3065 | "Only template template parameters handled here"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3066 | TemplateTemplateParmDecl *OldTTP |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3067 | = cast<TemplateTemplateParmDecl>(*OldParm); |
| 3068 | TemplateTemplateParmDecl *NewTTP |
| 3069 | = cast<TemplateTemplateParmDecl>(*NewParm); |
| 3070 | if (!TemplateParameterListsAreEqual(NewTTP->getTemplateParameters(), |
| 3071 | OldTTP->getTemplateParameters(), |
| 3072 | Complain, |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 3073 | (Kind == TPL_TemplateMatch? TPL_TemplateTemplateParmMatch : Kind), |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 3074 | TemplateArgLoc)) |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3075 | return false; |
| 3076 | } |
| 3077 | } |
| 3078 | |
| 3079 | return true; |
| 3080 | } |
| 3081 | |
| 3082 | /// \brief Check whether a template can be declared within this scope. |
| 3083 | /// |
| 3084 | /// If the template declaration is valid in this scope, returns |
| 3085 | /// false. Otherwise, issues a diagnostic and returns true. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3086 | bool |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 3087 | Sema::CheckTemplateDeclScope(Scope *S, TemplateParameterList *TemplateParams) { |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3088 | // Find the nearest enclosing declaration scope. |
| 3089 | while ((S->getFlags() & Scope::DeclScope) == 0 || |
| 3090 | (S->getFlags() & Scope::TemplateParamScope) != 0) |
| 3091 | S = S->getParent(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3092 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3093 | // C++ [temp]p2: |
| 3094 | // A template-declaration can appear only as a namespace scope or |
| 3095 | // class scope declaration. |
| 3096 | DeclContext *Ctx = static_cast<DeclContext *>(S->getEntity()); |
Eli Friedman | 1503f77 | 2009-07-31 01:43:05 +0000 | [diff] [blame] | 3097 | if (Ctx && isa<LinkageSpecDecl>(Ctx) && |
| 3098 | cast<LinkageSpecDecl>(Ctx)->getLanguage() != LinkageSpecDecl::lang_cxx) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3099 | return Diag(TemplateParams->getTemplateLoc(), diag::err_template_linkage) |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 3100 | << TemplateParams->getSourceRange(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3101 | |
Eli Friedman | 1503f77 | 2009-07-31 01:43:05 +0000 | [diff] [blame] | 3102 | while (Ctx && isa<LinkageSpecDecl>(Ctx)) |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3103 | Ctx = Ctx->getParent(); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3104 | |
| 3105 | if (Ctx && (Ctx->isFileContext() || Ctx->isRecord())) |
| 3106 | return false; |
| 3107 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3108 | return Diag(TemplateParams->getTemplateLoc(), |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 3109 | diag::err_template_outside_namespace_or_class_scope) |
| 3110 | << TemplateParams->getSourceRange(); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3111 | } |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3112 | |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3113 | /// \brief Determine what kind of template specialization the given declaration |
| 3114 | /// is. |
| 3115 | static TemplateSpecializationKind getTemplateSpecializationKind(NamedDecl *D) { |
| 3116 | if (!D) |
| 3117 | return TSK_Undeclared; |
| 3118 | |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 3119 | if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) |
| 3120 | return Record->getTemplateSpecializationKind(); |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3121 | if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) |
| 3122 | return Function->getTemplateSpecializationKind(); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 3123 | if (VarDecl *Var = dyn_cast<VarDecl>(D)) |
| 3124 | return Var->getTemplateSpecializationKind(); |
| 3125 | |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3126 | return TSK_Undeclared; |
| 3127 | } |
| 3128 | |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 3129 | /// \brief Check whether a specialization is well-formed in the current |
| 3130 | /// context. |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 3131 | /// |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 3132 | /// This routine determines whether a template specialization can be declared |
| 3133 | /// in the current context (C++ [temp.expl.spec]p2). |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3134 | /// |
| 3135 | /// \param S the semantic analysis object for which this check is being |
| 3136 | /// performed. |
| 3137 | /// |
| 3138 | /// \param Specialized the entity being specialized or instantiated, which |
| 3139 | /// may be a kind of template (class template, function template, etc.) or |
| 3140 | /// a member of a class template (member function, static data member, |
| 3141 | /// member class). |
| 3142 | /// |
| 3143 | /// \param PrevDecl the previous declaration of this entity, if any. |
| 3144 | /// |
| 3145 | /// \param Loc the location of the explicit specialization or instantiation of |
| 3146 | /// this entity. |
| 3147 | /// |
| 3148 | /// \param IsPartialSpecialization whether this is a partial specialization of |
| 3149 | /// a class template. |
| 3150 | /// |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3151 | /// \returns true if there was an error that we cannot recover from, false |
| 3152 | /// otherwise. |
| 3153 | static bool CheckTemplateSpecializationScope(Sema &S, |
| 3154 | NamedDecl *Specialized, |
| 3155 | NamedDecl *PrevDecl, |
| 3156 | SourceLocation Loc, |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 3157 | bool IsPartialSpecialization) { |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3158 | // Keep these "kind" numbers in sync with the %select statements in the |
| 3159 | // various diagnostics emitted by this routine. |
| 3160 | int EntityKind = 0; |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 3161 | bool isTemplateSpecialization = false; |
| 3162 | if (isa<ClassTemplateDecl>(Specialized)) { |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3163 | EntityKind = IsPartialSpecialization? 1 : 0; |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 3164 | isTemplateSpecialization = true; |
| 3165 | } else if (isa<FunctionTemplateDecl>(Specialized)) { |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3166 | EntityKind = 2; |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 3167 | isTemplateSpecialization = true; |
| 3168 | } else if (isa<CXXMethodDecl>(Specialized)) |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3169 | EntityKind = 3; |
| 3170 | else if (isa<VarDecl>(Specialized)) |
| 3171 | EntityKind = 4; |
| 3172 | else if (isa<RecordDecl>(Specialized)) |
| 3173 | EntityKind = 5; |
| 3174 | else { |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 3175 | S.Diag(Loc, diag::err_template_spec_unknown_kind); |
| 3176 | S.Diag(Specialized->getLocation(), diag::note_specialized_entity); |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3177 | return true; |
| 3178 | } |
| 3179 | |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 3180 | // C++ [temp.expl.spec]p2: |
| 3181 | // An explicit specialization shall be declared in the namespace |
| 3182 | // of which the template is a member, or, for member templates, in |
| 3183 | // the namespace of which the enclosing class or enclosing class |
| 3184 | // template is a member. An explicit specialization of a member |
| 3185 | // function, member class or static data member of a class |
| 3186 | // template shall be declared in the namespace of which the class |
| 3187 | // template is a member. Such a declaration may also be a |
| 3188 | // definition. If the declaration is not a definition, the |
| 3189 | // specialization may be defined later in the name- space in which |
| 3190 | // the explicit specialization was declared, or in a namespace |
| 3191 | // that encloses the one in which the explicit specialization was |
| 3192 | // declared. |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3193 | if (S.CurContext->getLookupContext()->isFunctionOrMethod()) { |
| 3194 | S.Diag(Loc, diag::err_template_spec_decl_function_scope) |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 3195 | << Specialized; |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 3196 | return true; |
| 3197 | } |
Douglas Gregor | 7974c3b | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 3198 | |
Douglas Gregor | 0a40747 | 2009-10-07 17:30:37 +0000 | [diff] [blame] | 3199 | if (S.CurContext->isRecord() && !IsPartialSpecialization) { |
| 3200 | S.Diag(Loc, diag::err_template_spec_decl_class_scope) |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 3201 | << Specialized; |
Douglas Gregor | 0a40747 | 2009-10-07 17:30:37 +0000 | [diff] [blame] | 3202 | return true; |
| 3203 | } |
| 3204 | |
Douglas Gregor | 7974c3b | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 3205 | // C++ [temp.class.spec]p6: |
| 3206 | // A class template partial specialization may be declared or redeclared |
| 3207 | // in any namespace scope in which its definition may be defined (14.5.1 |
| 3208 | // and 14.5.2). |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3209 | bool ComplainedAboutScope = false; |
Douglas Gregor | 7974c3b | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 3210 | DeclContext *SpecializedContext |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3211 | = Specialized->getDeclContext()->getEnclosingNamespaceContext(); |
Douglas Gregor | 7974c3b | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 3212 | DeclContext *DC = S.CurContext->getEnclosingNamespaceContext(); |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 3213 | if ((!PrevDecl || |
| 3214 | getTemplateSpecializationKind(PrevDecl) == TSK_Undeclared || |
| 3215 | getTemplateSpecializationKind(PrevDecl) == TSK_ImplicitInstantiation)){ |
| 3216 | // There is no prior declaration of this entity, so this |
| 3217 | // specialization must be in the same context as the template |
| 3218 | // itself. |
| 3219 | if (!DC->Equals(SpecializedContext)) { |
| 3220 | if (isa<TranslationUnitDecl>(SpecializedContext)) |
| 3221 | S.Diag(Loc, diag::err_template_spec_decl_out_of_scope_global) |
| 3222 | << EntityKind << Specialized; |
| 3223 | else if (isa<NamespaceDecl>(SpecializedContext)) |
| 3224 | S.Diag(Loc, diag::err_template_spec_decl_out_of_scope) |
| 3225 | << EntityKind << Specialized |
| 3226 | << cast<NamedDecl>(SpecializedContext); |
| 3227 | |
| 3228 | S.Diag(Specialized->getLocation(), diag::note_specialized_entity); |
| 3229 | ComplainedAboutScope = true; |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 3230 | } |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 3231 | } |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3232 | |
| 3233 | // Make sure that this redeclaration (or definition) occurs in an enclosing |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 3234 | // namespace. |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3235 | // Note that HandleDeclarator() performs this check for explicit |
| 3236 | // specializations of function templates, static data members, and member |
| 3237 | // functions, so we skip the check here for those kinds of entities. |
| 3238 | // FIXME: HandleDeclarator's diagnostics aren't quite as good, though. |
Douglas Gregor | 7974c3b | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 3239 | // Should we refactor that check, so that it occurs later? |
| 3240 | if (!ComplainedAboutScope && !DC->Encloses(SpecializedContext) && |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 3241 | !(isa<FunctionTemplateDecl>(Specialized) || isa<VarDecl>(Specialized) || |
| 3242 | isa<FunctionDecl>(Specialized))) { |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3243 | if (isa<TranslationUnitDecl>(SpecializedContext)) |
| 3244 | S.Diag(Loc, diag::err_template_spec_redecl_global_scope) |
| 3245 | << EntityKind << Specialized; |
| 3246 | else if (isa<NamespaceDecl>(SpecializedContext)) |
| 3247 | S.Diag(Loc, diag::err_template_spec_redecl_out_of_scope) |
| 3248 | << EntityKind << Specialized |
| 3249 | << cast<NamedDecl>(SpecializedContext); |
| 3250 | |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 3251 | S.Diag(Specialized->getLocation(), diag::note_specialized_entity); |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 3252 | } |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3253 | |
| 3254 | // FIXME: check for specialization-after-instantiation errors and such. |
| 3255 | |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 3256 | return false; |
| 3257 | } |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3258 | |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 3259 | /// \brief Check the non-type template arguments of a class template |
| 3260 | /// partial specialization according to C++ [temp.class.spec]p9. |
| 3261 | /// |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3262 | /// \param TemplateParams the template parameters of the primary class |
| 3263 | /// template. |
| 3264 | /// |
| 3265 | /// \param TemplateArg the template arguments of the class template |
| 3266 | /// partial specialization. |
| 3267 | /// |
| 3268 | /// \param MirrorsPrimaryTemplate will be set true if the class |
| 3269 | /// template partial specialization arguments are identical to the |
| 3270 | /// implicit template arguments of the primary template. This is not |
| 3271 | /// necessarily an error (C++0x), and it is left to the caller to diagnose |
| 3272 | /// this condition when it is an error. |
| 3273 | /// |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 3274 | /// \returns true if there was an error, false otherwise. |
| 3275 | bool Sema::CheckClassTemplatePartialSpecializationArgs( |
| 3276 | TemplateParameterList *TemplateParams, |
Anders Carlsson | 6360be7 | 2009-06-13 18:20:51 +0000 | [diff] [blame] | 3277 | const TemplateArgumentListBuilder &TemplateArgs, |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3278 | bool &MirrorsPrimaryTemplate) { |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 3279 | // FIXME: the interface to this function will have to change to |
| 3280 | // accommodate variadic templates. |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3281 | MirrorsPrimaryTemplate = true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3282 | |
Anders Carlsson | fb25052 | 2009-06-23 01:26:57 +0000 | [diff] [blame] | 3283 | const TemplateArgument *ArgList = TemplateArgs.getFlatArguments(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3284 | |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 3285 | for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) { |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3286 | // Determine whether the template argument list of the partial |
| 3287 | // specialization is identical to the implicit argument list of |
| 3288 | // the primary template. The caller may need to diagnostic this as |
| 3289 | // an error per C++ [temp.class.spec]p9b3. |
| 3290 | if (MirrorsPrimaryTemplate) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3291 | if (TemplateTypeParmDecl *TTP |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3292 | = dyn_cast<TemplateTypeParmDecl>(TemplateParams->getParam(I))) { |
| 3293 | if (Context.getCanonicalType(Context.getTypeDeclType(TTP)) != |
Anders Carlsson | 6360be7 | 2009-06-13 18:20:51 +0000 | [diff] [blame] | 3294 | Context.getCanonicalType(ArgList[I].getAsType())) |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3295 | MirrorsPrimaryTemplate = false; |
| 3296 | } else if (TemplateTemplateParmDecl *TTP |
| 3297 | = dyn_cast<TemplateTemplateParmDecl>( |
| 3298 | TemplateParams->getParam(I))) { |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 3299 | TemplateName Name = ArgList[I].getAsTemplate(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3300 | TemplateTemplateParmDecl *ArgDecl |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 3301 | = dyn_cast_or_null<TemplateTemplateParmDecl>(Name.getAsTemplateDecl()); |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3302 | if (!ArgDecl || |
| 3303 | ArgDecl->getIndex() != TTP->getIndex() || |
| 3304 | ArgDecl->getDepth() != TTP->getDepth()) |
| 3305 | MirrorsPrimaryTemplate = false; |
| 3306 | } |
| 3307 | } |
| 3308 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3309 | NonTypeTemplateParmDecl *Param |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 3310 | = dyn_cast<NonTypeTemplateParmDecl>(TemplateParams->getParam(I)); |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3311 | if (!Param) { |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 3312 | continue; |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3313 | } |
| 3314 | |
Anders Carlsson | 6360be7 | 2009-06-13 18:20:51 +0000 | [diff] [blame] | 3315 | Expr *ArgExpr = ArgList[I].getAsExpr(); |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3316 | if (!ArgExpr) { |
| 3317 | MirrorsPrimaryTemplate = false; |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 3318 | continue; |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3319 | } |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 3320 | |
| 3321 | // C++ [temp.class.spec]p8: |
| 3322 | // A non-type argument is non-specialized if it is the name of a |
| 3323 | // non-type parameter. All other non-type arguments are |
| 3324 | // specialized. |
| 3325 | // |
| 3326 | // Below, we check the two conditions that only apply to |
| 3327 | // specialized non-type arguments, so skip any non-specialized |
| 3328 | // arguments. |
| 3329 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ArgExpr)) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3330 | if (NonTypeTemplateParmDecl *NTTP |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3331 | = dyn_cast<NonTypeTemplateParmDecl>(DRE->getDecl())) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3332 | if (MirrorsPrimaryTemplate && |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3333 | (Param->getIndex() != NTTP->getIndex() || |
| 3334 | Param->getDepth() != NTTP->getDepth())) |
| 3335 | MirrorsPrimaryTemplate = false; |
| 3336 | |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 3337 | continue; |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3338 | } |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 3339 | |
| 3340 | // C++ [temp.class.spec]p9: |
| 3341 | // Within the argument list of a class template partial |
| 3342 | // specialization, the following restrictions apply: |
| 3343 | // -- A partially specialized non-type argument expression |
| 3344 | // shall not involve a template parameter of the partial |
| 3345 | // specialization except when the argument expression is a |
| 3346 | // simple identifier. |
| 3347 | if (ArgExpr->isTypeDependent() || ArgExpr->isValueDependent()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3348 | Diag(ArgExpr->getLocStart(), |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 3349 | diag::err_dependent_non_type_arg_in_partial_spec) |
| 3350 | << ArgExpr->getSourceRange(); |
| 3351 | return true; |
| 3352 | } |
| 3353 | |
| 3354 | // -- The type of a template parameter corresponding to a |
| 3355 | // specialized non-type argument shall not be dependent on a |
| 3356 | // parameter of the specialization. |
| 3357 | if (Param->getType()->isDependentType()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3358 | Diag(ArgExpr->getLocStart(), |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 3359 | diag::err_dependent_typed_non_type_arg_in_partial_spec) |
| 3360 | << Param->getType() |
| 3361 | << ArgExpr->getSourceRange(); |
| 3362 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 3363 | return true; |
| 3364 | } |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3365 | |
| 3366 | MirrorsPrimaryTemplate = false; |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 3367 | } |
| 3368 | |
| 3369 | return false; |
| 3370 | } |
| 3371 | |
Douglas Gregor | dc0a11c | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 3372 | /// \brief Retrieve the previous declaration of the given declaration. |
| 3373 | static NamedDecl *getPreviousDecl(NamedDecl *ND) { |
| 3374 | if (VarDecl *VD = dyn_cast<VarDecl>(ND)) |
| 3375 | return VD->getPreviousDeclaration(); |
| 3376 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) |
| 3377 | return FD->getPreviousDeclaration(); |
| 3378 | if (TagDecl *TD = dyn_cast<TagDecl>(ND)) |
| 3379 | return TD->getPreviousDeclaration(); |
| 3380 | if (TypedefDecl *TD = dyn_cast<TypedefDecl>(ND)) |
| 3381 | return TD->getPreviousDeclaration(); |
| 3382 | if (FunctionTemplateDecl *FTD = dyn_cast<FunctionTemplateDecl>(ND)) |
| 3383 | return FTD->getPreviousDeclaration(); |
| 3384 | if (ClassTemplateDecl *CTD = dyn_cast<ClassTemplateDecl>(ND)) |
| 3385 | return CTD->getPreviousDeclaration(); |
| 3386 | return 0; |
| 3387 | } |
| 3388 | |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 3389 | Sema::DeclResult |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 3390 | Sema::ActOnClassTemplateSpecialization(Scope *S, unsigned TagSpec, |
| 3391 | TagUseKind TUK, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3392 | SourceLocation KWLoc, |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3393 | const CXXScopeSpec &SS, |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 3394 | TemplateTy TemplateD, |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3395 | SourceLocation TemplateNameLoc, |
| 3396 | SourceLocation LAngleLoc, |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 3397 | ASTTemplateArgsPtr TemplateArgsIn, |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3398 | SourceLocation RAngleLoc, |
| 3399 | AttributeList *Attr, |
| 3400 | MultiTemplateParamsArg TemplateParameterLists) { |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 3401 | assert(TUK != TUK_Reference && "References are not specializations"); |
John McCall | f1bbbb4 | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 3402 | |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3403 | // Find the class template we're specializing |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 3404 | TemplateName Name = TemplateD.getAsVal<TemplateName>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3405 | ClassTemplateDecl *ClassTemplate |
Douglas Gregor | 8b13c08 | 2009-11-12 00:46:20 +0000 | [diff] [blame] | 3406 | = dyn_cast_or_null<ClassTemplateDecl>(Name.getAsTemplateDecl()); |
| 3407 | |
| 3408 | if (!ClassTemplate) { |
| 3409 | Diag(TemplateNameLoc, diag::err_not_class_template_specialization) |
| 3410 | << (Name.getAsTemplateDecl() && |
| 3411 | isa<TemplateTemplateParmDecl>(Name.getAsTemplateDecl())); |
| 3412 | return true; |
| 3413 | } |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3414 | |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 3415 | bool isExplicitSpecialization = false; |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 3416 | bool isPartialSpecialization = false; |
| 3417 | |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 3418 | // Check the validity of the template headers that introduce this |
| 3419 | // template. |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 3420 | // FIXME: We probably shouldn't complain about these headers for |
| 3421 | // friend declarations. |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 3422 | TemplateParameterList *TemplateParams |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3423 | = MatchTemplateParametersToScopeSpecifier(TemplateNameLoc, SS, |
| 3424 | (TemplateParameterList**)TemplateParameterLists.get(), |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 3425 | TemplateParameterLists.size(), |
| 3426 | isExplicitSpecialization); |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 3427 | if (TemplateParams && TemplateParams->size() > 0) { |
| 3428 | isPartialSpecialization = true; |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 3429 | |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 3430 | // C++ [temp.class.spec]p10: |
| 3431 | // The template parameter list of a specialization shall not |
| 3432 | // contain default template argument values. |
| 3433 | for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) { |
| 3434 | Decl *Param = TemplateParams->getParam(I); |
| 3435 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) { |
| 3436 | if (TTP->hasDefaultArgument()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3437 | Diag(TTP->getDefaultArgumentLoc(), |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 3438 | diag::err_default_arg_in_partial_spec); |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 3439 | TTP->removeDefaultArgument(); |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 3440 | } |
| 3441 | } else if (NonTypeTemplateParmDecl *NTTP |
| 3442 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
| 3443 | if (Expr *DefArg = NTTP->getDefaultArgument()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3444 | Diag(NTTP->getDefaultArgumentLoc(), |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 3445 | diag::err_default_arg_in_partial_spec) |
| 3446 | << DefArg->getSourceRange(); |
| 3447 | NTTP->setDefaultArgument(0); |
| 3448 | DefArg->Destroy(Context); |
| 3449 | } |
| 3450 | } else { |
| 3451 | TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(Param); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 3452 | if (TTP->hasDefaultArgument()) { |
| 3453 | Diag(TTP->getDefaultArgument().getLocation(), |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 3454 | diag::err_default_arg_in_partial_spec) |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 3455 | << TTP->getDefaultArgument().getSourceRange(); |
| 3456 | TTP->setDefaultArgument(TemplateArgumentLoc()); |
Douglas Gregor | ba1ecb5 | 2009-06-12 19:43:02 +0000 | [diff] [blame] | 3457 | } |
| 3458 | } |
| 3459 | } |
Douglas Gregor | a735b20 | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 3460 | } else if (TemplateParams) { |
| 3461 | if (TUK == TUK_Friend) |
| 3462 | Diag(KWLoc, diag::err_template_spec_friend) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 3463 | << FixItHint::CreateRemoval( |
Douglas Gregor | a735b20 | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 3464 | SourceRange(TemplateParams->getTemplateLoc(), |
| 3465 | TemplateParams->getRAngleLoc())) |
| 3466 | << SourceRange(LAngleLoc, RAngleLoc); |
| 3467 | else |
| 3468 | isExplicitSpecialization = true; |
| 3469 | } else if (TUK != TUK_Friend) { |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 3470 | Diag(KWLoc, diag::err_template_spec_needs_header) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 3471 | << FixItHint::CreateInsertion(KWLoc, "template<> "); |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 3472 | isExplicitSpecialization = true; |
| 3473 | } |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 3474 | |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3475 | // Check that the specialization uses the same tag kind as the |
| 3476 | // original template. |
| 3477 | TagDecl::TagKind Kind; |
| 3478 | switch (TagSpec) { |
| 3479 | default: assert(0 && "Unknown tag type!"); |
| 3480 | case DeclSpec::TST_struct: Kind = TagDecl::TK_struct; break; |
| 3481 | case DeclSpec::TST_union: Kind = TagDecl::TK_union; break; |
| 3482 | case DeclSpec::TST_class: Kind = TagDecl::TK_class; break; |
| 3483 | } |
Douglas Gregor | 501c5ce | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 3484 | if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3485 | Kind, KWLoc, |
Douglas Gregor | 501c5ce | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 3486 | *ClassTemplate->getIdentifier())) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3487 | Diag(KWLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 3488 | << ClassTemplate |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 3489 | << FixItHint::CreateReplacement(KWLoc, |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 3490 | ClassTemplate->getTemplatedDecl()->getKindName()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3491 | Diag(ClassTemplate->getTemplatedDecl()->getLocation(), |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3492 | diag::note_previous_use); |
| 3493 | Kind = ClassTemplate->getTemplatedDecl()->getTagKind(); |
| 3494 | } |
| 3495 | |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 3496 | // Translate the parser's template argument list in our AST format. |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 3497 | TemplateArgumentListInfo TemplateArgs; |
| 3498 | TemplateArgs.setLAngleLoc(LAngleLoc); |
| 3499 | TemplateArgs.setRAngleLoc(RAngleLoc); |
Douglas Gregor | 314b97f | 2009-11-10 19:49:08 +0000 | [diff] [blame] | 3500 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 3501 | |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3502 | // Check that the template argument list is well-formed for this |
| 3503 | // template. |
Anders Carlsson | fb25052 | 2009-06-23 01:26:57 +0000 | [diff] [blame] | 3504 | TemplateArgumentListBuilder Converted(ClassTemplate->getTemplateParameters(), |
| 3505 | TemplateArgs.size()); |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 3506 | if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc, |
| 3507 | TemplateArgs, false, Converted)) |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 3508 | return true; |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3509 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3510 | assert((Converted.structuredSize() == |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3511 | ClassTemplate->getTemplateParameters()->size()) && |
| 3512 | "Converted template argument list is too short!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3513 | |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 3514 | // Find the class template (partial) specialization declaration that |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3515 | // corresponds to these arguments. |
| 3516 | llvm::FoldingSetNodeID ID; |
Douglas Gregor | ba1ecb5 | 2009-06-12 19:43:02 +0000 | [diff] [blame] | 3517 | if (isPartialSpecialization) { |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3518 | bool MirrorsPrimaryTemplate; |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 3519 | if (CheckClassTemplatePartialSpecializationArgs( |
| 3520 | ClassTemplate->getTemplateParameters(), |
Anders Carlsson | fb25052 | 2009-06-23 01:26:57 +0000 | [diff] [blame] | 3521 | Converted, MirrorsPrimaryTemplate)) |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 3522 | return true; |
| 3523 | |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3524 | if (MirrorsPrimaryTemplate) { |
| 3525 | // C++ [temp.class.spec]p9b3: |
| 3526 | // |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3527 | // -- The argument list of the specialization shall not be identical |
| 3528 | // to the implicit argument list of the primary template. |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3529 | Diag(TemplateNameLoc, diag::err_partial_spec_args_match_primary_template) |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 3530 | << (TUK == TUK_Definition) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 3531 | << FixItHint::CreateRemoval(SourceRange(LAngleLoc, RAngleLoc)); |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 3532 | return CheckClassTemplate(S, TagSpec, TUK, KWLoc, SS, |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3533 | ClassTemplate->getIdentifier(), |
| 3534 | TemplateNameLoc, |
| 3535 | Attr, |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 3536 | TemplateParams, |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3537 | AS_none); |
| 3538 | } |
| 3539 | |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 3540 | // FIXME: Diagnose friend partial specializations |
| 3541 | |
Douglas Gregor | de09096 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 3542 | if (!Name.isDependent() && |
| 3543 | !TemplateSpecializationType::anyDependentTemplateArguments( |
| 3544 | TemplateArgs.getArgumentArray(), |
| 3545 | TemplateArgs.size())) { |
| 3546 | Diag(TemplateNameLoc, diag::err_partial_spec_fully_specialized) |
| 3547 | << ClassTemplate->getDeclName(); |
| 3548 | isPartialSpecialization = false; |
| 3549 | } else { |
| 3550 | // FIXME: Template parameter list matters, too |
| 3551 | ClassTemplatePartialSpecializationDecl::Profile(ID, |
| 3552 | Converted.getFlatArguments(), |
| 3553 | Converted.flatSize(), |
| 3554 | Context); |
| 3555 | } |
| 3556 | } |
| 3557 | |
| 3558 | if (!isPartialSpecialization) |
Anders Carlsson | 1c5976e | 2009-06-05 03:43:12 +0000 | [diff] [blame] | 3559 | ClassTemplateSpecializationDecl::Profile(ID, |
Anders Carlsson | fb25052 | 2009-06-23 01:26:57 +0000 | [diff] [blame] | 3560 | Converted.getFlatArguments(), |
Douglas Gregor | 828e226 | 2009-07-29 16:09:57 +0000 | [diff] [blame] | 3561 | Converted.flatSize(), |
| 3562 | Context); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3563 | void *InsertPos = 0; |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 3564 | ClassTemplateSpecializationDecl *PrevDecl = 0; |
| 3565 | |
| 3566 | if (isPartialSpecialization) |
| 3567 | PrevDecl |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3568 | = ClassTemplate->getPartialSpecializations().FindNodeOrInsertPos(ID, |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 3569 | InsertPos); |
| 3570 | else |
| 3571 | PrevDecl |
| 3572 | = ClassTemplate->getSpecializations().FindNodeOrInsertPos(ID, InsertPos); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3573 | |
| 3574 | ClassTemplateSpecializationDecl *Specialization = 0; |
| 3575 | |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 3576 | // Check whether we can declare a class template specialization in |
| 3577 | // the current scope. |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 3578 | if (TUK != TUK_Friend && |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3579 | CheckTemplateSpecializationScope(*this, ClassTemplate, PrevDecl, |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 3580 | TemplateNameLoc, |
| 3581 | isPartialSpecialization)) |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 3582 | return true; |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 3583 | |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 3584 | // The canonical type |
| 3585 | QualType CanonType; |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 3586 | if (PrevDecl && |
| 3587 | (PrevDecl->getSpecializationKind() == TSK_Undeclared || |
Douglas Gregor | de09096 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 3588 | TUK == TUK_Friend)) { |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3589 | // Since the only prior class template specialization with these |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 3590 | // arguments was referenced but not declared, or we're only |
| 3591 | // referencing this specialization as a friend, reuse that |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3592 | // declaration node as our own, updating its source location to |
| 3593 | // reflect our new declaration. |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3594 | Specialization = PrevDecl; |
Douglas Gregor | 6bc9f7e | 2009-02-25 22:18:32 +0000 | [diff] [blame] | 3595 | Specialization->setLocation(TemplateNameLoc); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3596 | PrevDecl = 0; |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 3597 | CanonType = Context.getTypeDeclType(Specialization); |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 3598 | } else if (isPartialSpecialization) { |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 3599 | // Build the canonical type that describes the converted template |
| 3600 | // arguments of the class template partial specialization. |
Douglas Gregor | de09096 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 3601 | TemplateName CanonTemplate = Context.getCanonicalTemplateName(Name); |
| 3602 | CanonType = Context.getTemplateSpecializationType(CanonTemplate, |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 3603 | Converted.getFlatArguments(), |
| 3604 | Converted.flatSize()); |
| 3605 | |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 3606 | // Create a new class template partial specialization declaration node. |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 3607 | ClassTemplatePartialSpecializationDecl *PrevPartial |
| 3608 | = cast_or_null<ClassTemplatePartialSpecializationDecl>(PrevDecl); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3609 | ClassTemplatePartialSpecializationDecl *Partial |
| 3610 | = ClassTemplatePartialSpecializationDecl::Create(Context, |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 3611 | ClassTemplate->getDeclContext(), |
Anders Carlsson | 91fdf6f | 2009-06-05 04:06:48 +0000 | [diff] [blame] | 3612 | TemplateNameLoc, |
| 3613 | TemplateParams, |
| 3614 | ClassTemplate, |
Anders Carlsson | fb25052 | 2009-06-23 01:26:57 +0000 | [diff] [blame] | 3615 | Converted, |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 3616 | TemplateArgs, |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 3617 | CanonType, |
Anders Carlsson | 91fdf6f | 2009-06-05 04:06:48 +0000 | [diff] [blame] | 3618 | PrevPartial); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 3619 | SetNestedNameSpecifier(Partial, SS); |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 3620 | |
| 3621 | if (PrevPartial) { |
| 3622 | ClassTemplate->getPartialSpecializations().RemoveNode(PrevPartial); |
| 3623 | ClassTemplate->getPartialSpecializations().GetOrInsertNode(Partial); |
| 3624 | } else { |
| 3625 | ClassTemplate->getPartialSpecializations().InsertNode(Partial, InsertPos); |
| 3626 | } |
| 3627 | Specialization = Partial; |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 3628 | |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 3629 | // If we are providing an explicit specialization of a member class |
| 3630 | // template specialization, make a note of that. |
| 3631 | if (PrevPartial && PrevPartial->getInstantiatedFromMember()) |
| 3632 | PrevPartial->setMemberSpecialization(); |
| 3633 | |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 3634 | // Check that all of the template parameters of the class template |
| 3635 | // partial specialization are deducible from the template |
| 3636 | // arguments. If not, this class template partial specialization |
| 3637 | // will never be used. |
| 3638 | llvm::SmallVector<bool, 8> DeducibleParams; |
| 3639 | DeducibleParams.resize(TemplateParams->size()); |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 3640 | MarkUsedTemplateParameters(Partial->getTemplateArgs(), true, |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 3641 | TemplateParams->getDepth(), |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 3642 | DeducibleParams); |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 3643 | unsigned NumNonDeducible = 0; |
| 3644 | for (unsigned I = 0, N = DeducibleParams.size(); I != N; ++I) |
| 3645 | if (!DeducibleParams[I]) |
| 3646 | ++NumNonDeducible; |
| 3647 | |
| 3648 | if (NumNonDeducible) { |
| 3649 | Diag(TemplateNameLoc, diag::warn_partial_specs_not_deducible) |
| 3650 | << (NumNonDeducible > 1) |
| 3651 | << SourceRange(TemplateNameLoc, RAngleLoc); |
| 3652 | for (unsigned I = 0, N = DeducibleParams.size(); I != N; ++I) { |
| 3653 | if (!DeducibleParams[I]) { |
| 3654 | NamedDecl *Param = cast<NamedDecl>(TemplateParams->getParam(I)); |
| 3655 | if (Param->getDeclName()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3656 | Diag(Param->getLocation(), |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 3657 | diag::note_partial_spec_unused_parameter) |
| 3658 | << Param->getDeclName(); |
| 3659 | else |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3660 | Diag(Param->getLocation(), |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 3661 | diag::note_partial_spec_unused_parameter) |
| 3662 | << std::string("<anonymous>"); |
| 3663 | } |
| 3664 | } |
| 3665 | } |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3666 | } else { |
| 3667 | // Create a new class template specialization declaration node for |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 3668 | // this explicit specialization or friend declaration. |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3669 | Specialization |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3670 | = ClassTemplateSpecializationDecl::Create(Context, |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3671 | ClassTemplate->getDeclContext(), |
| 3672 | TemplateNameLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3673 | ClassTemplate, |
Anders Carlsson | fb25052 | 2009-06-23 01:26:57 +0000 | [diff] [blame] | 3674 | Converted, |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3675 | PrevDecl); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 3676 | SetNestedNameSpecifier(Specialization, SS); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3677 | |
| 3678 | if (PrevDecl) { |
| 3679 | ClassTemplate->getSpecializations().RemoveNode(PrevDecl); |
| 3680 | ClassTemplate->getSpecializations().GetOrInsertNode(Specialization); |
| 3681 | } else { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3682 | ClassTemplate->getSpecializations().InsertNode(Specialization, |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3683 | InsertPos); |
| 3684 | } |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 3685 | |
| 3686 | CanonType = Context.getTypeDeclType(Specialization); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3687 | } |
| 3688 | |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 3689 | // C++ [temp.expl.spec]p6: |
| 3690 | // If a template, a member template or the member of a class template is |
| 3691 | // explicitly specialized then that specialization shall be declared |
| 3692 | // before the first use of that specialization that would cause an implicit |
| 3693 | // instantiation to take place, in every translation unit in which such a |
| 3694 | // use occurs; no diagnostic is required. |
| 3695 | if (PrevDecl && PrevDecl->getPointOfInstantiation().isValid()) { |
Douglas Gregor | dc0a11c | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 3696 | bool Okay = false; |
| 3697 | for (NamedDecl *Prev = PrevDecl; Prev; Prev = getPreviousDecl(Prev)) { |
| 3698 | // Is there any previous explicit specialization declaration? |
| 3699 | if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) { |
| 3700 | Okay = true; |
| 3701 | break; |
| 3702 | } |
| 3703 | } |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 3704 | |
Douglas Gregor | dc0a11c | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 3705 | if (!Okay) { |
| 3706 | SourceRange Range(TemplateNameLoc, RAngleLoc); |
| 3707 | Diag(TemplateNameLoc, diag::err_specialization_after_instantiation) |
| 3708 | << Context.getTypeDeclType(Specialization) << Range; |
| 3709 | |
| 3710 | Diag(PrevDecl->getPointOfInstantiation(), |
| 3711 | diag::note_instantiation_required_here) |
| 3712 | << (PrevDecl->getTemplateSpecializationKind() |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 3713 | != TSK_ImplicitInstantiation); |
Douglas Gregor | dc0a11c | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 3714 | return true; |
| 3715 | } |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 3716 | } |
| 3717 | |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 3718 | // If this is not a friend, note that this is an explicit specialization. |
| 3719 | if (TUK != TUK_Friend) |
| 3720 | Specialization->setSpecializationKind(TSK_ExplicitSpecialization); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3721 | |
| 3722 | // Check that this isn't a redefinition of this specialization. |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 3723 | if (TUK == TUK_Definition) { |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 3724 | if (RecordDecl *Def = Specialization->getDefinition()) { |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3725 | SourceRange Range(TemplateNameLoc, RAngleLoc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3726 | Diag(TemplateNameLoc, diag::err_redefinition) |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 3727 | << Context.getTypeDeclType(Specialization) << Range; |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3728 | Diag(Def->getLocation(), diag::note_previous_definition); |
| 3729 | Specialization->setInvalidDecl(); |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 3730 | return true; |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3731 | } |
| 3732 | } |
| 3733 | |
Douglas Gregor | fc705b8 | 2009-02-26 22:19:44 +0000 | [diff] [blame] | 3734 | // Build the fully-sugared type for this class template |
| 3735 | // specialization as the user wrote in the specialization |
| 3736 | // itself. This means that we'll pretty-print the type retrieved |
| 3737 | // from the specialization's declaration the way that the user |
| 3738 | // actually wrote the specialization, rather than formatting the |
| 3739 | // name based on the "canonical" representation used to store the |
| 3740 | // template arguments in the specialization. |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 3741 | TypeSourceInfo *WrittenTy |
| 3742 | = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc, |
| 3743 | TemplateArgs, CanonType); |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 3744 | if (TUK != TUK_Friend) |
| 3745 | Specialization->setTypeAsWritten(WrittenTy); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 3746 | TemplateArgsIn.release(); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3747 | |
Douglas Gregor | 6bc9f7e | 2009-02-25 22:18:32 +0000 | [diff] [blame] | 3748 | // C++ [temp.expl.spec]p9: |
| 3749 | // A template explicit specialization is in the scope of the |
| 3750 | // namespace in which the template was defined. |
| 3751 | // |
| 3752 | // We actually implement this paragraph where we set the semantic |
| 3753 | // context (in the creation of the ClassTemplateSpecializationDecl), |
| 3754 | // but we also maintain the lexical context where the actual |
| 3755 | // definition occurs. |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3756 | Specialization->setLexicalDeclContext(CurContext); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3757 | |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3758 | // We may be starting the definition of this specialization. |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 3759 | if (TUK == TUK_Definition) |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3760 | Specialization->startDefinition(); |
| 3761 | |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 3762 | if (TUK == TUK_Friend) { |
| 3763 | FriendDecl *Friend = FriendDecl::Create(Context, CurContext, |
| 3764 | TemplateNameLoc, |
John McCall | 32f2fb5 | 2010-03-25 18:04:51 +0000 | [diff] [blame] | 3765 | WrittenTy, |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 3766 | /*FIXME:*/KWLoc); |
| 3767 | Friend->setAccess(AS_public); |
| 3768 | CurContext->addDecl(Friend); |
| 3769 | } else { |
| 3770 | // Add the specialization into its lexical context, so that it can |
| 3771 | // be seen when iterating through the list of declarations in that |
| 3772 | // context. However, specializations are not found by name lookup. |
| 3773 | CurContext->addDecl(Specialization); |
| 3774 | } |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 3775 | return DeclPtrTy::make(Specialization); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3776 | } |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3777 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3778 | Sema::DeclPtrTy |
| 3779 | Sema::ActOnTemplateDeclarator(Scope *S, |
Douglas Gregor | e542c86 | 2009-06-23 23:11:28 +0000 | [diff] [blame] | 3780 | MultiTemplateParamsArg TemplateParameterLists, |
| 3781 | Declarator &D) { |
| 3782 | return HandleDeclarator(S, D, move(TemplateParameterLists), false); |
| 3783 | } |
| 3784 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3785 | Sema::DeclPtrTy |
| 3786 | Sema::ActOnStartOfFunctionTemplateDef(Scope *FnBodyScope, |
Douglas Gregor | 52591bf | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 3787 | MultiTemplateParamsArg TemplateParameterLists, |
| 3788 | Declarator &D) { |
| 3789 | assert(getCurFunctionDecl() == 0 && "Function parsing confused"); |
| 3790 | assert(D.getTypeObject(0).Kind == DeclaratorChunk::Function && |
| 3791 | "Not a function declarator!"); |
| 3792 | DeclaratorChunk::FunctionTypeInfo &FTI = D.getTypeObject(0).Fun; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3793 | |
Douglas Gregor | 52591bf | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 3794 | if (FTI.hasPrototype) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3795 | // FIXME: Diagnose arguments without names in C. |
Douglas Gregor | 52591bf | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 3796 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3797 | |
Douglas Gregor | 52591bf | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 3798 | Scope *ParentScope = FnBodyScope->getParent(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3799 | |
| 3800 | DeclPtrTy DP = HandleDeclarator(ParentScope, D, |
Douglas Gregor | 52591bf | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 3801 | move(TemplateParameterLists), |
| 3802 | /*IsFunctionDefinition=*/true); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3803 | if (FunctionTemplateDecl *FunctionTemplate |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 3804 | = dyn_cast_or_null<FunctionTemplateDecl>(DP.getAs<Decl>())) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3805 | return ActOnStartOfFunctionDef(FnBodyScope, |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3806 | DeclPtrTy::make(FunctionTemplate->getTemplatedDecl())); |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 3807 | if (FunctionDecl *Function = dyn_cast_or_null<FunctionDecl>(DP.getAs<Decl>())) |
| 3808 | return ActOnStartOfFunctionDef(FnBodyScope, DeclPtrTy::make(Function)); |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3809 | return DeclPtrTy(); |
Douglas Gregor | 52591bf | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 3810 | } |
| 3811 | |
John McCall | 7504239 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 3812 | /// \brief Strips various properties off an implicit instantiation |
| 3813 | /// that has just been explicitly specialized. |
| 3814 | static void StripImplicitInstantiation(NamedDecl *D) { |
| 3815 | D->invalidateAttrs(); |
| 3816 | |
| 3817 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 3818 | FD->setInlineSpecified(false); |
| 3819 | } |
| 3820 | } |
| 3821 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 3822 | /// \brief Diagnose cases where we have an explicit template specialization |
| 3823 | /// before/after an explicit template instantiation, producing diagnostics |
| 3824 | /// for those cases where they are required and determining whether the |
| 3825 | /// new specialization/instantiation will have any effect. |
| 3826 | /// |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 3827 | /// \param NewLoc the location of the new explicit specialization or |
| 3828 | /// instantiation. |
| 3829 | /// |
| 3830 | /// \param NewTSK the kind of the new explicit specialization or instantiation. |
| 3831 | /// |
| 3832 | /// \param PrevDecl the previous declaration of the entity. |
| 3833 | /// |
| 3834 | /// \param PrevTSK the kind of the old explicit specialization or instantiatin. |
| 3835 | /// |
| 3836 | /// \param PrevPointOfInstantiation if valid, indicates where the previus |
| 3837 | /// declaration was instantiated (either implicitly or explicitly). |
| 3838 | /// |
| 3839 | /// \param SuppressNew will be set to true to indicate that the new |
| 3840 | /// specialization or instantiation has no effect and should be ignored. |
| 3841 | /// |
| 3842 | /// \returns true if there was an error that should prevent the introduction of |
| 3843 | /// the new declaration into the AST, false otherwise. |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 3844 | bool |
| 3845 | Sema::CheckSpecializationInstantiationRedecl(SourceLocation NewLoc, |
| 3846 | TemplateSpecializationKind NewTSK, |
| 3847 | NamedDecl *PrevDecl, |
| 3848 | TemplateSpecializationKind PrevTSK, |
| 3849 | SourceLocation PrevPointOfInstantiation, |
| 3850 | bool &SuppressNew) { |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 3851 | SuppressNew = false; |
| 3852 | |
| 3853 | switch (NewTSK) { |
| 3854 | case TSK_Undeclared: |
| 3855 | case TSK_ImplicitInstantiation: |
| 3856 | assert(false && "Don't check implicit instantiations here"); |
| 3857 | return false; |
| 3858 | |
| 3859 | case TSK_ExplicitSpecialization: |
| 3860 | switch (PrevTSK) { |
| 3861 | case TSK_Undeclared: |
| 3862 | case TSK_ExplicitSpecialization: |
| 3863 | // Okay, we're just specializing something that is either already |
| 3864 | // explicitly specialized or has merely been mentioned without any |
| 3865 | // instantiation. |
| 3866 | return false; |
| 3867 | |
| 3868 | case TSK_ImplicitInstantiation: |
| 3869 | if (PrevPointOfInstantiation.isInvalid()) { |
| 3870 | // The declaration itself has not actually been instantiated, so it is |
| 3871 | // still okay to specialize it. |
John McCall | 7504239 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 3872 | StripImplicitInstantiation(PrevDecl); |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 3873 | return false; |
| 3874 | } |
| 3875 | // Fall through |
| 3876 | |
| 3877 | case TSK_ExplicitInstantiationDeclaration: |
| 3878 | case TSK_ExplicitInstantiationDefinition: |
| 3879 | assert((PrevTSK == TSK_ImplicitInstantiation || |
| 3880 | PrevPointOfInstantiation.isValid()) && |
| 3881 | "Explicit instantiation without point of instantiation?"); |
| 3882 | |
| 3883 | // C++ [temp.expl.spec]p6: |
| 3884 | // If a template, a member template or the member of a class template |
| 3885 | // is explicitly specialized then that specialization shall be declared |
| 3886 | // before the first use of that specialization that would cause an |
| 3887 | // implicit instantiation to take place, in every translation unit in |
| 3888 | // which such a use occurs; no diagnostic is required. |
Douglas Gregor | dc0a11c | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 3889 | for (NamedDecl *Prev = PrevDecl; Prev; Prev = getPreviousDecl(Prev)) { |
| 3890 | // Is there any previous explicit specialization declaration? |
| 3891 | if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) |
| 3892 | return false; |
| 3893 | } |
| 3894 | |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 3895 | Diag(NewLoc, diag::err_specialization_after_instantiation) |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 3896 | << PrevDecl; |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 3897 | Diag(PrevPointOfInstantiation, diag::note_instantiation_required_here) |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 3898 | << (PrevTSK != TSK_ImplicitInstantiation); |
| 3899 | |
| 3900 | return true; |
| 3901 | } |
| 3902 | break; |
| 3903 | |
| 3904 | case TSK_ExplicitInstantiationDeclaration: |
| 3905 | switch (PrevTSK) { |
| 3906 | case TSK_ExplicitInstantiationDeclaration: |
| 3907 | // This explicit instantiation declaration is redundant (that's okay). |
| 3908 | SuppressNew = true; |
| 3909 | return false; |
| 3910 | |
| 3911 | case TSK_Undeclared: |
| 3912 | case TSK_ImplicitInstantiation: |
| 3913 | // We're explicitly instantiating something that may have already been |
| 3914 | // implicitly instantiated; that's fine. |
| 3915 | return false; |
| 3916 | |
| 3917 | case TSK_ExplicitSpecialization: |
| 3918 | // C++0x [temp.explicit]p4: |
| 3919 | // For a given set of template parameters, if an explicit instantiation |
| 3920 | // of a template appears after a declaration of an explicit |
| 3921 | // specialization for that template, the explicit instantiation has no |
| 3922 | // effect. |
John McCall | e97c32f | 2010-03-02 23:09:38 +0000 | [diff] [blame] | 3923 | SuppressNew = true; |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 3924 | return false; |
| 3925 | |
| 3926 | case TSK_ExplicitInstantiationDefinition: |
| 3927 | // C++0x [temp.explicit]p10: |
| 3928 | // If an entity is the subject of both an explicit instantiation |
| 3929 | // declaration and an explicit instantiation definition in the same |
| 3930 | // translation unit, the definition shall follow the declaration. |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 3931 | Diag(NewLoc, |
| 3932 | diag::err_explicit_instantiation_declaration_after_definition); |
| 3933 | Diag(PrevPointOfInstantiation, |
| 3934 | diag::note_explicit_instantiation_definition_here); |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 3935 | assert(PrevPointOfInstantiation.isValid() && |
| 3936 | "Explicit instantiation without point of instantiation?"); |
| 3937 | SuppressNew = true; |
| 3938 | return false; |
| 3939 | } |
| 3940 | break; |
| 3941 | |
| 3942 | case TSK_ExplicitInstantiationDefinition: |
| 3943 | switch (PrevTSK) { |
| 3944 | case TSK_Undeclared: |
| 3945 | case TSK_ImplicitInstantiation: |
| 3946 | // We're explicitly instantiating something that may have already been |
| 3947 | // implicitly instantiated; that's fine. |
| 3948 | return false; |
| 3949 | |
| 3950 | case TSK_ExplicitSpecialization: |
| 3951 | // C++ DR 259, C++0x [temp.explicit]p4: |
| 3952 | // For a given set of template parameters, if an explicit |
| 3953 | // instantiation of a template appears after a declaration of |
| 3954 | // an explicit specialization for that template, the explicit |
| 3955 | // instantiation has no effect. |
| 3956 | // |
| 3957 | // In C++98/03 mode, we only give an extension warning here, because it |
| 3958 | // is not not harmful to try to explicitly instantiate something that |
| 3959 | // has been explicitly specialized. |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 3960 | if (!getLangOptions().CPlusPlus0x) { |
| 3961 | Diag(NewLoc, diag::ext_explicit_instantiation_after_specialization) |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 3962 | << PrevDecl; |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 3963 | Diag(PrevDecl->getLocation(), |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 3964 | diag::note_previous_template_specialization); |
| 3965 | } |
| 3966 | SuppressNew = true; |
| 3967 | return false; |
| 3968 | |
| 3969 | case TSK_ExplicitInstantiationDeclaration: |
| 3970 | // We're explicity instantiating a definition for something for which we |
| 3971 | // were previously asked to suppress instantiations. That's fine. |
| 3972 | return false; |
| 3973 | |
| 3974 | case TSK_ExplicitInstantiationDefinition: |
| 3975 | // C++0x [temp.spec]p5: |
| 3976 | // For a given template and a given set of template-arguments, |
| 3977 | // - an explicit instantiation definition shall appear at most once |
| 3978 | // in a program, |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 3979 | Diag(NewLoc, diag::err_explicit_instantiation_duplicate) |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 3980 | << PrevDecl; |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 3981 | Diag(PrevPointOfInstantiation, |
| 3982 | diag::note_previous_explicit_instantiation); |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 3983 | SuppressNew = true; |
| 3984 | return false; |
| 3985 | } |
| 3986 | break; |
| 3987 | } |
| 3988 | |
| 3989 | assert(false && "Missing specialization/instantiation case?"); |
| 3990 | |
| 3991 | return false; |
| 3992 | } |
| 3993 | |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 3994 | /// \brief Perform semantic analysis for the given function template |
| 3995 | /// specialization. |
| 3996 | /// |
| 3997 | /// This routine performs all of the semantic analysis required for an |
| 3998 | /// explicit function template specialization. On successful completion, |
| 3999 | /// the function declaration \p FD will become a function template |
| 4000 | /// specialization. |
| 4001 | /// |
| 4002 | /// \param FD the function declaration, which will be updated to become a |
| 4003 | /// function template specialization. |
| 4004 | /// |
| 4005 | /// \param HasExplicitTemplateArgs whether any template arguments were |
| 4006 | /// explicitly provided. |
| 4007 | /// |
| 4008 | /// \param LAngleLoc the location of the left angle bracket ('<'), if |
| 4009 | /// template arguments were explicitly provided. |
| 4010 | /// |
| 4011 | /// \param ExplicitTemplateArgs the explicitly-provided template arguments, |
| 4012 | /// if any. |
| 4013 | /// |
| 4014 | /// \param NumExplicitTemplateArgs the number of explicitly-provided template |
| 4015 | /// arguments. This number may be zero even when HasExplicitTemplateArgs is |
| 4016 | /// true as in, e.g., \c void sort<>(char*, char*); |
| 4017 | /// |
| 4018 | /// \param RAngleLoc the location of the right angle bracket ('>'), if |
| 4019 | /// template arguments were explicitly provided. |
| 4020 | /// |
| 4021 | /// \param PrevDecl the set of declarations that |
| 4022 | bool |
| 4023 | Sema::CheckFunctionTemplateSpecialization(FunctionDecl *FD, |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 4024 | const TemplateArgumentListInfo *ExplicitTemplateArgs, |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 4025 | LookupResult &Previous) { |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4026 | // The set of function template specializations that could match this |
| 4027 | // explicit function template specialization. |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4028 | UnresolvedSet<8> Candidates; |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4029 | |
| 4030 | DeclContext *FDLookupContext = FD->getDeclContext()->getLookupContext(); |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 4031 | for (LookupResult::iterator I = Previous.begin(), E = Previous.end(); |
| 4032 | I != E; ++I) { |
| 4033 | NamedDecl *Ovl = (*I)->getUnderlyingDecl(); |
| 4034 | if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Ovl)) { |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4035 | // Only consider templates found within the same semantic lookup scope as |
| 4036 | // FD. |
| 4037 | if (!FDLookupContext->Equals(Ovl->getDeclContext()->getLookupContext())) |
| 4038 | continue; |
| 4039 | |
| 4040 | // C++ [temp.expl.spec]p11: |
| 4041 | // A trailing template-argument can be left unspecified in the |
| 4042 | // template-id naming an explicit function template specialization |
| 4043 | // provided it can be deduced from the function argument type. |
| 4044 | // Perform template argument deduction to determine whether we may be |
| 4045 | // specializing this template. |
| 4046 | // FIXME: It is somewhat wasteful to build |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 4047 | TemplateDeductionInfo Info(Context, FD->getLocation()); |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4048 | FunctionDecl *Specialization = 0; |
| 4049 | if (TemplateDeductionResult TDK |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 4050 | = DeduceTemplateArguments(FunTmpl, ExplicitTemplateArgs, |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4051 | FD->getType(), |
| 4052 | Specialization, |
| 4053 | Info)) { |
| 4054 | // FIXME: Template argument deduction failed; record why it failed, so |
| 4055 | // that we can provide nifty diagnostics. |
| 4056 | (void)TDK; |
| 4057 | continue; |
| 4058 | } |
| 4059 | |
| 4060 | // Record this candidate. |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4061 | Candidates.addDecl(Specialization, I.getAccess()); |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4062 | } |
| 4063 | } |
| 4064 | |
Douglas Gregor | c5df30f | 2009-09-26 03:41:46 +0000 | [diff] [blame] | 4065 | // Find the most specialized function template. |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4066 | UnresolvedSetIterator Result |
| 4067 | = getMostSpecialized(Candidates.begin(), Candidates.end(), |
| 4068 | TPOC_Other, FD->getLocation(), |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 4069 | PDiag(diag::err_function_template_spec_no_match) |
Douglas Gregor | c5df30f | 2009-09-26 03:41:46 +0000 | [diff] [blame] | 4070 | << FD->getDeclName(), |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 4071 | PDiag(diag::err_function_template_spec_ambiguous) |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 4072 | << FD->getDeclName() << (ExplicitTemplateArgs != 0), |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 4073 | PDiag(diag::note_function_template_spec_matched)); |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4074 | if (Result == Candidates.end()) |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4075 | return true; |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4076 | |
| 4077 | // Ignore access information; it doesn't figure into redeclaration checking. |
| 4078 | FunctionDecl *Specialization = cast<FunctionDecl>(*Result); |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4079 | |
| 4080 | // FIXME: Check if the prior specialization has a point of instantiation. |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 4081 | // If so, we have run afoul of . |
John McCall | 7ad650f | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 4082 | |
| 4083 | // If this is a friend declaration, then we're not really declaring |
| 4084 | // an explicit specialization. |
| 4085 | bool isFriend = (FD->getFriendObjectKind() != Decl::FOK_None); |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4086 | |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4087 | // Check the scope of this explicit specialization. |
John McCall | 7ad650f | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 4088 | if (!isFriend && |
| 4089 | CheckTemplateSpecializationScope(*this, |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4090 | Specialization->getPrimaryTemplate(), |
| 4091 | Specialization, FD->getLocation(), |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4092 | false)) |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4093 | return true; |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 4094 | |
| 4095 | // C++ [temp.expl.spec]p6: |
| 4096 | // 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] | 4097 | // explicitly specialized then that specialization shall be declared |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 4098 | // before the first use of that specialization that would cause an implicit |
| 4099 | // instantiation to take place, in every translation unit in which such a |
| 4100 | // use occurs; no diagnostic is required. |
| 4101 | FunctionTemplateSpecializationInfo *SpecInfo |
| 4102 | = Specialization->getTemplateSpecializationInfo(); |
| 4103 | assert(SpecInfo && "Function template specialization info missing?"); |
John McCall | 7504239 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 4104 | |
| 4105 | bool SuppressNew = false; |
John McCall | 7ad650f | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 4106 | if (!isFriend && |
| 4107 | CheckSpecializationInstantiationRedecl(FD->getLocation(), |
John McCall | 7504239 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 4108 | TSK_ExplicitSpecialization, |
| 4109 | Specialization, |
| 4110 | SpecInfo->getTemplateSpecializationKind(), |
| 4111 | SpecInfo->getPointOfInstantiation(), |
| 4112 | SuppressNew)) |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 4113 | return true; |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4114 | |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4115 | // Mark the prior declaration as an explicit specialization, so that later |
| 4116 | // clients know that this is an explicit specialization. |
John McCall | 7ad650f | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 4117 | if (!isFriend) |
| 4118 | SpecInfo->setTemplateSpecializationKind(TSK_ExplicitSpecialization); |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4119 | |
| 4120 | // Turn the given function declaration into a function template |
| 4121 | // specialization, with the template arguments from the previous |
| 4122 | // specialization. |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 4123 | FD->setFunctionTemplateSpecialization(Specialization->getPrimaryTemplate(), |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4124 | new (Context) TemplateArgumentList( |
| 4125 | *Specialization->getTemplateSpecializationArgs()), |
| 4126 | /*InsertPos=*/0, |
John McCall | 7ad650f | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 4127 | SpecInfo->getTemplateSpecializationKind()); |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4128 | |
| 4129 | // The "previous declaration" for this function template specialization is |
| 4130 | // the prior function template specialization. |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 4131 | Previous.clear(); |
| 4132 | Previous.addDecl(Specialization); |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4133 | return false; |
| 4134 | } |
| 4135 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4136 | /// \brief Perform semantic analysis for the given non-template member |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 4137 | /// specialization. |
| 4138 | /// |
| 4139 | /// This routine performs all of the semantic analysis required for an |
| 4140 | /// explicit member function specialization. On successful completion, |
| 4141 | /// the function declaration \p FD will become a member function |
| 4142 | /// specialization. |
| 4143 | /// |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4144 | /// \param Member the member declaration, which will be updated to become a |
| 4145 | /// specialization. |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 4146 | /// |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 4147 | /// \param Previous the set of declarations, one of which may be specialized |
| 4148 | /// by this function specialization; the set will be modified to contain the |
| 4149 | /// redeclared member. |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 4150 | bool |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 4151 | Sema::CheckMemberSpecialization(NamedDecl *Member, LookupResult &Previous) { |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4152 | assert(!isa<TemplateDecl>(Member) && "Only for non-template members"); |
| 4153 | |
| 4154 | // Try to find the member we are instantiating. |
| 4155 | NamedDecl *Instantiation = 0; |
| 4156 | NamedDecl *InstantiatedFrom = 0; |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 4157 | MemberSpecializationInfo *MSInfo = 0; |
| 4158 | |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 4159 | if (Previous.empty()) { |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4160 | // Nowhere to look anyway. |
| 4161 | } else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Member)) { |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 4162 | for (LookupResult::iterator I = Previous.begin(), E = Previous.end(); |
| 4163 | I != E; ++I) { |
| 4164 | NamedDecl *D = (*I)->getUnderlyingDecl(); |
| 4165 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) { |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4166 | if (Context.hasSameType(Function->getType(), Method->getType())) { |
| 4167 | Instantiation = Method; |
| 4168 | InstantiatedFrom = Method->getInstantiatedFromMemberFunction(); |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 4169 | MSInfo = Method->getMemberSpecializationInfo(); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4170 | break; |
| 4171 | } |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 4172 | } |
| 4173 | } |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4174 | } else if (isa<VarDecl>(Member)) { |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 4175 | VarDecl *PrevVar; |
| 4176 | if (Previous.isSingleResult() && |
| 4177 | (PrevVar = dyn_cast<VarDecl>(Previous.getFoundDecl()))) |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4178 | if (PrevVar->isStaticDataMember()) { |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 4179 | Instantiation = PrevVar; |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4180 | InstantiatedFrom = PrevVar->getInstantiatedFromStaticDataMember(); |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 4181 | MSInfo = PrevVar->getMemberSpecializationInfo(); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4182 | } |
| 4183 | } else if (isa<RecordDecl>(Member)) { |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 4184 | CXXRecordDecl *PrevRecord; |
| 4185 | if (Previous.isSingleResult() && |
| 4186 | (PrevRecord = dyn_cast<CXXRecordDecl>(Previous.getFoundDecl()))) { |
| 4187 | Instantiation = PrevRecord; |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4188 | InstantiatedFrom = PrevRecord->getInstantiatedFromMemberClass(); |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 4189 | MSInfo = PrevRecord->getMemberSpecializationInfo(); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4190 | } |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 4191 | } |
| 4192 | |
| 4193 | if (!Instantiation) { |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4194 | // There is no previous declaration that matches. Since member |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 4195 | // specializations are always out-of-line, the caller will complain about |
| 4196 | // this mismatch later. |
| 4197 | return false; |
| 4198 | } |
| 4199 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4200 | // Make sure that this is a specialization of a member. |
| 4201 | if (!InstantiatedFrom) { |
| 4202 | Diag(Member->getLocation(), diag::err_spec_member_not_instantiated) |
| 4203 | << Member; |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 4204 | Diag(Instantiation->getLocation(), diag::note_specialized_decl); |
| 4205 | return true; |
| 4206 | } |
| 4207 | |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 4208 | // C++ [temp.expl.spec]p6: |
| 4209 | // If a template, a member template or the member of a class template is |
| 4210 | // explicitly specialized then that spe- cialization shall be declared |
| 4211 | // before the first use of that specialization that would cause an implicit |
| 4212 | // instantiation to take place, in every translation unit in which such a |
| 4213 | // use occurs; no diagnostic is required. |
| 4214 | assert(MSInfo && "Member specialization info missing?"); |
John McCall | 7504239 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 4215 | |
| 4216 | bool SuppressNew = false; |
| 4217 | if (CheckSpecializationInstantiationRedecl(Member->getLocation(), |
| 4218 | TSK_ExplicitSpecialization, |
| 4219 | Instantiation, |
| 4220 | MSInfo->getTemplateSpecializationKind(), |
| 4221 | MSInfo->getPointOfInstantiation(), |
| 4222 | SuppressNew)) |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 4223 | return true; |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 4224 | |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 4225 | // Check the scope of this explicit specialization. |
| 4226 | if (CheckTemplateSpecializationScope(*this, |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4227 | InstantiatedFrom, |
| 4228 | Instantiation, Member->getLocation(), |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4229 | false)) |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 4230 | return true; |
Douglas Gregor | 2db3232 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 4231 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4232 | // Note that this is an explicit instantiation of a member. |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 4233 | // the original declaration to note that it is an explicit specialization |
| 4234 | // (if it was previously an implicit instantiation). This latter step |
| 4235 | // makes bookkeeping easier. |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4236 | if (isa<FunctionDecl>(Member)) { |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 4237 | FunctionDecl *InstantiationFunction = cast<FunctionDecl>(Instantiation); |
| 4238 | if (InstantiationFunction->getTemplateSpecializationKind() == |
| 4239 | TSK_ImplicitInstantiation) { |
| 4240 | InstantiationFunction->setTemplateSpecializationKind( |
| 4241 | TSK_ExplicitSpecialization); |
| 4242 | InstantiationFunction->setLocation(Member->getLocation()); |
| 4243 | } |
| 4244 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4245 | cast<FunctionDecl>(Member)->setInstantiationOfMemberFunction( |
| 4246 | cast<CXXMethodDecl>(InstantiatedFrom), |
| 4247 | TSK_ExplicitSpecialization); |
| 4248 | } else if (isa<VarDecl>(Member)) { |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 4249 | VarDecl *InstantiationVar = cast<VarDecl>(Instantiation); |
| 4250 | if (InstantiationVar->getTemplateSpecializationKind() == |
| 4251 | TSK_ImplicitInstantiation) { |
| 4252 | InstantiationVar->setTemplateSpecializationKind( |
| 4253 | TSK_ExplicitSpecialization); |
| 4254 | InstantiationVar->setLocation(Member->getLocation()); |
| 4255 | } |
| 4256 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4257 | Context.setInstantiatedFromStaticDataMember(cast<VarDecl>(Member), |
| 4258 | cast<VarDecl>(InstantiatedFrom), |
| 4259 | TSK_ExplicitSpecialization); |
| 4260 | } else { |
| 4261 | assert(isa<CXXRecordDecl>(Member) && "Only member classes remain"); |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 4262 | CXXRecordDecl *InstantiationClass = cast<CXXRecordDecl>(Instantiation); |
| 4263 | if (InstantiationClass->getTemplateSpecializationKind() == |
| 4264 | TSK_ImplicitInstantiation) { |
| 4265 | InstantiationClass->setTemplateSpecializationKind( |
| 4266 | TSK_ExplicitSpecialization); |
| 4267 | InstantiationClass->setLocation(Member->getLocation()); |
| 4268 | } |
| 4269 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4270 | cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass( |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 4271 | cast<CXXRecordDecl>(InstantiatedFrom), |
| 4272 | TSK_ExplicitSpecialization); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4273 | } |
| 4274 | |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 4275 | // Save the caller the trouble of having to figure out which declaration |
| 4276 | // this specialization matches. |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 4277 | Previous.clear(); |
| 4278 | Previous.addDecl(Instantiation); |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 4279 | return false; |
| 4280 | } |
| 4281 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 4282 | /// \brief Check the scope of an explicit instantiation. |
| 4283 | static void CheckExplicitInstantiationScope(Sema &S, NamedDecl *D, |
| 4284 | SourceLocation InstLoc, |
| 4285 | bool WasQualifiedName) { |
| 4286 | DeclContext *ExpectedContext |
| 4287 | = D->getDeclContext()->getEnclosingNamespaceContext()->getLookupContext(); |
| 4288 | DeclContext *CurContext = S.CurContext->getLookupContext(); |
| 4289 | |
| 4290 | // C++0x [temp.explicit]p2: |
| 4291 | // An explicit instantiation shall appear in an enclosing namespace of its |
| 4292 | // template. |
| 4293 | // |
| 4294 | // This is DR275, which we do not retroactively apply to C++98/03. |
| 4295 | if (S.getLangOptions().CPlusPlus0x && |
| 4296 | !CurContext->Encloses(ExpectedContext)) { |
| 4297 | if (NamespaceDecl *NS = dyn_cast<NamespaceDecl>(ExpectedContext)) |
| 4298 | S.Diag(InstLoc, diag::err_explicit_instantiation_out_of_scope) |
| 4299 | << D << NS; |
| 4300 | else |
| 4301 | S.Diag(InstLoc, diag::err_explicit_instantiation_must_be_global) |
| 4302 | << D; |
| 4303 | S.Diag(D->getLocation(), diag::note_explicit_instantiation_here); |
| 4304 | return; |
| 4305 | } |
| 4306 | |
| 4307 | // C++0x [temp.explicit]p2: |
| 4308 | // If the name declared in the explicit instantiation is an unqualified |
| 4309 | // name, the explicit instantiation shall appear in the namespace where |
| 4310 | // its template is declared or, if that namespace is inline (7.3.1), any |
| 4311 | // namespace from its enclosing namespace set. |
| 4312 | if (WasQualifiedName) |
| 4313 | return; |
| 4314 | |
| 4315 | if (CurContext->Equals(ExpectedContext)) |
| 4316 | return; |
| 4317 | |
| 4318 | S.Diag(InstLoc, diag::err_explicit_instantiation_unqualified_wrong_namespace) |
| 4319 | << D << ExpectedContext; |
| 4320 | S.Diag(D->getLocation(), diag::note_explicit_instantiation_here); |
| 4321 | } |
| 4322 | |
| 4323 | /// \brief Determine whether the given scope specifier has a template-id in it. |
| 4324 | static bool ScopeSpecifierHasTemplateId(const CXXScopeSpec &SS) { |
| 4325 | if (!SS.isSet()) |
| 4326 | return false; |
| 4327 | |
| 4328 | // C++0x [temp.explicit]p2: |
| 4329 | // If the explicit instantiation is for a member function, a member class |
| 4330 | // or a static data member of a class template specialization, the name of |
| 4331 | // the class template specialization in the qualified-id for the member |
| 4332 | // name shall be a simple-template-id. |
| 4333 | // |
| 4334 | // C++98 has the same restriction, just worded differently. |
| 4335 | for (NestedNameSpecifier *NNS = (NestedNameSpecifier *)SS.getScopeRep(); |
| 4336 | NNS; NNS = NNS->getPrefix()) |
| 4337 | if (Type *T = NNS->getAsType()) |
| 4338 | if (isa<TemplateSpecializationType>(T)) |
| 4339 | return true; |
| 4340 | |
| 4341 | return false; |
| 4342 | } |
| 4343 | |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 4344 | // Explicit instantiation of a class template specialization |
Douglas Gregor | 45f9655 | 2009-09-04 06:33:52 +0000 | [diff] [blame] | 4345 | // FIXME: Implement extern template semantics |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4346 | Sema::DeclResult |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4347 | Sema::ActOnExplicitInstantiation(Scope *S, |
Douglas Gregor | 45f9655 | 2009-09-04 06:33:52 +0000 | [diff] [blame] | 4348 | SourceLocation ExternLoc, |
| 4349 | SourceLocation TemplateLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4350 | unsigned TagSpec, |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4351 | SourceLocation KWLoc, |
| 4352 | const CXXScopeSpec &SS, |
| 4353 | TemplateTy TemplateD, |
| 4354 | SourceLocation TemplateNameLoc, |
| 4355 | SourceLocation LAngleLoc, |
| 4356 | ASTTemplateArgsPtr TemplateArgsIn, |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4357 | SourceLocation RAngleLoc, |
| 4358 | AttributeList *Attr) { |
| 4359 | // Find the class template we're specializing |
| 4360 | TemplateName Name = TemplateD.getAsVal<TemplateName>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4361 | ClassTemplateDecl *ClassTemplate |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4362 | = cast<ClassTemplateDecl>(Name.getAsTemplateDecl()); |
| 4363 | |
| 4364 | // Check that the specialization uses the same tag kind as the |
| 4365 | // original template. |
| 4366 | TagDecl::TagKind Kind; |
| 4367 | switch (TagSpec) { |
| 4368 | default: assert(0 && "Unknown tag type!"); |
| 4369 | case DeclSpec::TST_struct: Kind = TagDecl::TK_struct; break; |
| 4370 | case DeclSpec::TST_union: Kind = TagDecl::TK_union; break; |
| 4371 | case DeclSpec::TST_class: Kind = TagDecl::TK_class; break; |
| 4372 | } |
Douglas Gregor | 501c5ce | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 4373 | if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4374 | Kind, KWLoc, |
Douglas Gregor | 501c5ce | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 4375 | *ClassTemplate->getIdentifier())) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4376 | Diag(KWLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4377 | << ClassTemplate |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 4378 | << FixItHint::CreateReplacement(KWLoc, |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4379 | ClassTemplate->getTemplatedDecl()->getKindName()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4380 | Diag(ClassTemplate->getTemplatedDecl()->getLocation(), |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4381 | diag::note_previous_use); |
| 4382 | Kind = ClassTemplate->getTemplatedDecl()->getTagKind(); |
| 4383 | } |
| 4384 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 4385 | // C++0x [temp.explicit]p2: |
| 4386 | // There are two forms of explicit instantiation: an explicit instantiation |
| 4387 | // definition and an explicit instantiation declaration. An explicit |
| 4388 | // instantiation declaration begins with the extern keyword. [...] |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4389 | TemplateSpecializationKind TSK |
| 4390 | = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition |
| 4391 | : TSK_ExplicitInstantiationDeclaration; |
| 4392 | |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4393 | // Translate the parser's template argument list in our AST format. |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 4394 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
Douglas Gregor | 314b97f | 2009-11-10 19:49:08 +0000 | [diff] [blame] | 4395 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4396 | |
| 4397 | // Check that the template argument list is well-formed for this |
| 4398 | // template. |
Anders Carlsson | fb25052 | 2009-06-23 01:26:57 +0000 | [diff] [blame] | 4399 | TemplateArgumentListBuilder Converted(ClassTemplate->getTemplateParameters(), |
| 4400 | TemplateArgs.size()); |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 4401 | if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc, |
| 4402 | TemplateArgs, false, Converted)) |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4403 | return true; |
| 4404 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4405 | assert((Converted.structuredSize() == |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4406 | ClassTemplate->getTemplateParameters()->size()) && |
| 4407 | "Converted template argument list is too short!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4408 | |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4409 | // Find the class template specialization declaration that |
| 4410 | // corresponds to these arguments. |
| 4411 | llvm::FoldingSetNodeID ID; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4412 | ClassTemplateSpecializationDecl::Profile(ID, |
Anders Carlsson | fb25052 | 2009-06-23 01:26:57 +0000 | [diff] [blame] | 4413 | Converted.getFlatArguments(), |
Douglas Gregor | 828e226 | 2009-07-29 16:09:57 +0000 | [diff] [blame] | 4414 | Converted.flatSize(), |
| 4415 | Context); |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4416 | void *InsertPos = 0; |
| 4417 | ClassTemplateSpecializationDecl *PrevDecl |
| 4418 | = ClassTemplate->getSpecializations().FindNodeOrInsertPos(ID, InsertPos); |
| 4419 | |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4420 | // C++0x [temp.explicit]p2: |
| 4421 | // [...] An explicit instantiation shall appear in an enclosing |
| 4422 | // namespace of its template. [...] |
| 4423 | // |
| 4424 | // This is C++ DR 275. |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 4425 | CheckExplicitInstantiationScope(*this, ClassTemplate, TemplateNameLoc, |
| 4426 | SS.isSet()); |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4427 | |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4428 | ClassTemplateSpecializationDecl *Specialization = 0; |
| 4429 | |
Douglas Gregor | d78f598 | 2009-11-25 06:01:46 +0000 | [diff] [blame] | 4430 | bool ReusedDecl = false; |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4431 | if (PrevDecl) { |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 4432 | bool SuppressNew = false; |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 4433 | if (CheckSpecializationInstantiationRedecl(TemplateNameLoc, TSK, |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 4434 | PrevDecl, |
| 4435 | PrevDecl->getSpecializationKind(), |
| 4436 | PrevDecl->getPointOfInstantiation(), |
| 4437 | SuppressNew)) |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4438 | return DeclPtrTy::make(PrevDecl); |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4439 | |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 4440 | if (SuppressNew) |
Douglas Gregor | 52604ab | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 4441 | return DeclPtrTy::make(PrevDecl); |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 4442 | |
Douglas Gregor | 52604ab | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 4443 | if (PrevDecl->getSpecializationKind() == TSK_ImplicitInstantiation || |
| 4444 | PrevDecl->getSpecializationKind() == TSK_Undeclared) { |
| 4445 | // Since the only prior class template specialization with these |
| 4446 | // arguments was referenced but not declared, reuse that |
| 4447 | // declaration node as our own, updating its source location to |
| 4448 | // reflect our new declaration. |
| 4449 | Specialization = PrevDecl; |
| 4450 | Specialization->setLocation(TemplateNameLoc); |
| 4451 | PrevDecl = 0; |
Douglas Gregor | d78f598 | 2009-11-25 06:01:46 +0000 | [diff] [blame] | 4452 | ReusedDecl = true; |
Douglas Gregor | 52604ab | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 4453 | } |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 4454 | } |
Douglas Gregor | 52604ab | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 4455 | |
| 4456 | if (!Specialization) { |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4457 | // Create a new class template specialization declaration node for |
| 4458 | // this explicit specialization. |
| 4459 | Specialization |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4460 | = ClassTemplateSpecializationDecl::Create(Context, |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4461 | ClassTemplate->getDeclContext(), |
| 4462 | TemplateNameLoc, |
| 4463 | ClassTemplate, |
Douglas Gregor | 52604ab | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 4464 | Converted, PrevDecl); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 4465 | SetNestedNameSpecifier(Specialization, SS); |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4466 | |
Douglas Gregor | 52604ab | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 4467 | if (PrevDecl) { |
| 4468 | // Remove the previous declaration from the folding set, since we want |
| 4469 | // to introduce a new declaration. |
| 4470 | ClassTemplate->getSpecializations().RemoveNode(PrevDecl); |
| 4471 | ClassTemplate->getSpecializations().FindNodeOrInsertPos(ID, InsertPos); |
| 4472 | } |
| 4473 | |
| 4474 | // Insert the new specialization. |
| 4475 | ClassTemplate->getSpecializations().InsertNode(Specialization, InsertPos); |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4476 | } |
| 4477 | |
| 4478 | // Build the fully-sugared type for this explicit instantiation as |
| 4479 | // the user wrote in the explicit instantiation itself. This means |
| 4480 | // that we'll pretty-print the type retrieved from the |
| 4481 | // specialization's declaration the way that the user actually wrote |
| 4482 | // the explicit instantiation, rather than formatting the name based |
| 4483 | // on the "canonical" representation used to store the template |
| 4484 | // arguments in the specialization. |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 4485 | TypeSourceInfo *WrittenTy |
| 4486 | = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc, |
| 4487 | TemplateArgs, |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4488 | Context.getTypeDeclType(Specialization)); |
| 4489 | Specialization->setTypeAsWritten(WrittenTy); |
| 4490 | TemplateArgsIn.release(); |
| 4491 | |
Douglas Gregor | d78f598 | 2009-11-25 06:01:46 +0000 | [diff] [blame] | 4492 | if (!ReusedDecl) { |
| 4493 | // Add the explicit instantiation into its lexical context. However, |
| 4494 | // since explicit instantiations are never found by name lookup, we |
| 4495 | // just put it into the declaration context directly. |
| 4496 | Specialization->setLexicalDeclContext(CurContext); |
| 4497 | CurContext->addDecl(Specialization); |
| 4498 | } |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4499 | |
| 4500 | // C++ [temp.explicit]p3: |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4501 | // A definition of a class template or class member template |
| 4502 | // shall be in scope at the point of the explicit instantiation of |
| 4503 | // the class template or class member template. |
| 4504 | // |
| 4505 | // This check comes when we actually try to perform the |
| 4506 | // instantiation. |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 4507 | ClassTemplateSpecializationDecl *Def |
| 4508 | = cast_or_null<ClassTemplateSpecializationDecl>( |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 4509 | Specialization->getDefinition()); |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 4510 | if (!Def) |
Douglas Gregor | 972e6ce | 2009-10-27 06:26:26 +0000 | [diff] [blame] | 4511 | InstantiateClassTemplateSpecialization(TemplateNameLoc, Specialization, TSK); |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 4512 | |
| 4513 | // Instantiate the members of this class template specialization. |
| 4514 | Def = cast_or_null<ClassTemplateSpecializationDecl>( |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 4515 | Specialization->getDefinition()); |
Rafael Espindola | b0f65ca | 2010-03-22 23:12:48 +0000 | [diff] [blame] | 4516 | if (Def) { |
Rafael Espindola | f075b22 | 2010-03-23 19:55:22 +0000 | [diff] [blame] | 4517 | TemplateSpecializationKind Old_TSK = Def->getTemplateSpecializationKind(); |
| 4518 | |
| 4519 | // Fix a TSK_ExplicitInstantiationDeclaration followed by a |
| 4520 | // TSK_ExplicitInstantiationDefinition |
| 4521 | if (Old_TSK == TSK_ExplicitInstantiationDeclaration && |
| 4522 | TSK == TSK_ExplicitInstantiationDefinition) |
| 4523 | Def->setTemplateSpecializationKind(TSK); |
Rafael Espindola | b0f65ca | 2010-03-22 23:12:48 +0000 | [diff] [blame] | 4524 | |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 4525 | InstantiateClassTemplateSpecializationMembers(TemplateNameLoc, Def, TSK); |
Rafael Espindola | b0f65ca | 2010-03-22 23:12:48 +0000 | [diff] [blame] | 4526 | } |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4527 | |
| 4528 | return DeclPtrTy::make(Specialization); |
| 4529 | } |
| 4530 | |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 4531 | // Explicit instantiation of a member class of a class template. |
| 4532 | Sema::DeclResult |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4533 | Sema::ActOnExplicitInstantiation(Scope *S, |
Douglas Gregor | 45f9655 | 2009-09-04 06:33:52 +0000 | [diff] [blame] | 4534 | SourceLocation ExternLoc, |
| 4535 | SourceLocation TemplateLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4536 | unsigned TagSpec, |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 4537 | SourceLocation KWLoc, |
| 4538 | const CXXScopeSpec &SS, |
| 4539 | IdentifierInfo *Name, |
| 4540 | SourceLocation NameLoc, |
| 4541 | AttributeList *Attr) { |
| 4542 | |
Douglas Gregor | 402abb5 | 2009-05-28 23:31:59 +0000 | [diff] [blame] | 4543 | bool Owned = false; |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 4544 | bool IsDependent = false; |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 4545 | DeclPtrTy TagD = ActOnTag(S, TagSpec, Action::TUK_Reference, |
Douglas Gregor | 7cdbc58 | 2009-07-22 23:48:44 +0000 | [diff] [blame] | 4546 | KWLoc, SS, Name, NameLoc, Attr, AS_none, |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 4547 | MultiTemplateParamsArg(*this, 0, 0), |
| 4548 | Owned, IsDependent); |
| 4549 | assert(!IsDependent && "explicit instantiation of dependent name not yet handled"); |
| 4550 | |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 4551 | if (!TagD) |
| 4552 | return true; |
| 4553 | |
| 4554 | TagDecl *Tag = cast<TagDecl>(TagD.getAs<Decl>()); |
| 4555 | if (Tag->isEnum()) { |
| 4556 | Diag(TemplateLoc, diag::err_explicit_instantiation_enum) |
| 4557 | << Context.getTypeDeclType(Tag); |
| 4558 | return true; |
| 4559 | } |
| 4560 | |
Douglas Gregor | d0c8737 | 2009-05-27 17:30:49 +0000 | [diff] [blame] | 4561 | if (Tag->isInvalidDecl()) |
| 4562 | return true; |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 4563 | |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 4564 | CXXRecordDecl *Record = cast<CXXRecordDecl>(Tag); |
| 4565 | CXXRecordDecl *Pattern = Record->getInstantiatedFromMemberClass(); |
| 4566 | if (!Pattern) { |
| 4567 | Diag(TemplateLoc, diag::err_explicit_instantiation_nontemplate_type) |
| 4568 | << Context.getTypeDeclType(Record); |
| 4569 | Diag(Record->getLocation(), diag::note_nontemplate_decl_here); |
| 4570 | return true; |
| 4571 | } |
| 4572 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 4573 | // C++0x [temp.explicit]p2: |
| 4574 | // If the explicit instantiation is for a class or member class, the |
| 4575 | // elaborated-type-specifier in the declaration shall include a |
| 4576 | // simple-template-id. |
| 4577 | // |
| 4578 | // C++98 has the same restriction, just worded differently. |
| 4579 | if (!ScopeSpecifierHasTemplateId(SS)) |
| 4580 | Diag(TemplateLoc, diag::err_explicit_instantiation_without_qualified_id) |
| 4581 | << Record << SS.getRange(); |
| 4582 | |
| 4583 | // C++0x [temp.explicit]p2: |
| 4584 | // There are two forms of explicit instantiation: an explicit instantiation |
| 4585 | // definition and an explicit instantiation declaration. An explicit |
| 4586 | // instantiation declaration begins with the extern keyword. [...] |
Douglas Gregor | a74bbe2 | 2009-10-14 21:46:58 +0000 | [diff] [blame] | 4587 | TemplateSpecializationKind TSK |
| 4588 | = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition |
| 4589 | : TSK_ExplicitInstantiationDeclaration; |
| 4590 | |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 4591 | // C++0x [temp.explicit]p2: |
| 4592 | // [...] An explicit instantiation shall appear in an enclosing |
| 4593 | // namespace of its template. [...] |
| 4594 | // |
| 4595 | // This is C++ DR 275. |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 4596 | CheckExplicitInstantiationScope(*this, Record, NameLoc, true); |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4597 | |
| 4598 | // Verify that it is okay to explicitly instantiate here. |
Douglas Gregor | 583f33b | 2009-10-15 18:07:02 +0000 | [diff] [blame] | 4599 | CXXRecordDecl *PrevDecl |
| 4600 | = cast_or_null<CXXRecordDecl>(Record->getPreviousDeclaration()); |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 4601 | if (!PrevDecl && Record->getDefinition()) |
Douglas Gregor | 583f33b | 2009-10-15 18:07:02 +0000 | [diff] [blame] | 4602 | PrevDecl = Record; |
| 4603 | if (PrevDecl) { |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4604 | MemberSpecializationInfo *MSInfo = PrevDecl->getMemberSpecializationInfo(); |
| 4605 | bool SuppressNew = false; |
| 4606 | assert(MSInfo && "No member specialization information?"); |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 4607 | if (CheckSpecializationInstantiationRedecl(TemplateLoc, TSK, |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4608 | PrevDecl, |
| 4609 | MSInfo->getTemplateSpecializationKind(), |
| 4610 | MSInfo->getPointOfInstantiation(), |
| 4611 | SuppressNew)) |
| 4612 | return true; |
| 4613 | if (SuppressNew) |
| 4614 | return TagD; |
| 4615 | } |
| 4616 | |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 4617 | CXXRecordDecl *RecordDef |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 4618 | = cast_or_null<CXXRecordDecl>(Record->getDefinition()); |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 4619 | if (!RecordDef) { |
Douglas Gregor | bf7643e | 2009-10-15 12:53:22 +0000 | [diff] [blame] | 4620 | // C++ [temp.explicit]p3: |
| 4621 | // A definition of a member class of a class template shall be in scope |
| 4622 | // at the point of an explicit instantiation of the member class. |
| 4623 | CXXRecordDecl *Def |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 4624 | = cast_or_null<CXXRecordDecl>(Pattern->getDefinition()); |
Douglas Gregor | bf7643e | 2009-10-15 12:53:22 +0000 | [diff] [blame] | 4625 | if (!Def) { |
Douglas Gregor | e2d3a3d | 2009-10-15 14:05:49 +0000 | [diff] [blame] | 4626 | Diag(TemplateLoc, diag::err_explicit_instantiation_undefined_member) |
| 4627 | << 0 << Record->getDeclName() << Record->getDeclContext(); |
Douglas Gregor | bf7643e | 2009-10-15 12:53:22 +0000 | [diff] [blame] | 4628 | Diag(Pattern->getLocation(), diag::note_forward_declaration) |
| 4629 | << Pattern; |
| 4630 | return true; |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 4631 | } else { |
| 4632 | if (InstantiateClass(NameLoc, Record, Def, |
| 4633 | getTemplateInstantiationArgs(Record), |
| 4634 | TSK)) |
| 4635 | return true; |
| 4636 | |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 4637 | RecordDef = cast_or_null<CXXRecordDecl>(Record->getDefinition()); |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 4638 | if (!RecordDef) |
| 4639 | return true; |
| 4640 | } |
| 4641 | } |
| 4642 | |
| 4643 | // Instantiate all of the members of the class. |
| 4644 | InstantiateClassMembers(NameLoc, RecordDef, |
| 4645 | getTemplateInstantiationArgs(Record), TSK); |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 4646 | |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 4647 | // FIXME: We don't have any representation for explicit instantiations of |
| 4648 | // member classes. Such a representation is not needed for compilation, but it |
| 4649 | // should be available for clients that want to see all of the declarations in |
| 4650 | // the source code. |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 4651 | return TagD; |
| 4652 | } |
| 4653 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4654 | Sema::DeclResult Sema::ActOnExplicitInstantiation(Scope *S, |
| 4655 | SourceLocation ExternLoc, |
| 4656 | SourceLocation TemplateLoc, |
| 4657 | Declarator &D) { |
| 4658 | // Explicit instantiations always require a name. |
| 4659 | DeclarationName Name = GetNameForDeclarator(D); |
| 4660 | if (!Name) { |
| 4661 | if (!D.isInvalidType()) |
| 4662 | Diag(D.getDeclSpec().getSourceRange().getBegin(), |
| 4663 | diag::err_explicit_instantiation_requires_name) |
| 4664 | << D.getDeclSpec().getSourceRange() |
| 4665 | << D.getSourceRange(); |
| 4666 | |
| 4667 | return true; |
| 4668 | } |
| 4669 | |
| 4670 | // The scope passed in may not be a decl scope. Zip up the scope tree until |
| 4671 | // we find one that is. |
| 4672 | while ((S->getFlags() & Scope::DeclScope) == 0 || |
| 4673 | (S->getFlags() & Scope::TemplateParamScope) != 0) |
| 4674 | S = S->getParent(); |
| 4675 | |
| 4676 | // Determine the type of the declaration. |
| 4677 | QualType R = GetTypeForDeclarator(D, S, 0); |
| 4678 | if (R.isNull()) |
| 4679 | return true; |
| 4680 | |
| 4681 | if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef) { |
| 4682 | // Cannot explicitly instantiate a typedef. |
| 4683 | Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_of_typedef) |
| 4684 | << Name; |
| 4685 | return true; |
| 4686 | } |
| 4687 | |
Douglas Gregor | 663b5a0 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 4688 | // C++0x [temp.explicit]p1: |
| 4689 | // [...] An explicit instantiation of a function template shall not use the |
| 4690 | // inline or constexpr specifiers. |
| 4691 | // Presumably, this also applies to member functions of class templates as |
| 4692 | // well. |
| 4693 | if (D.getDeclSpec().isInlineSpecified() && getLangOptions().CPlusPlus0x) |
| 4694 | Diag(D.getDeclSpec().getInlineSpecLoc(), |
| 4695 | diag::err_explicit_instantiation_inline) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 4696 | <<FixItHint::CreateRemoval(D.getDeclSpec().getInlineSpecLoc()); |
Douglas Gregor | 663b5a0 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 4697 | |
| 4698 | // FIXME: check for constexpr specifier. |
| 4699 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 4700 | // C++0x [temp.explicit]p2: |
| 4701 | // There are two forms of explicit instantiation: an explicit instantiation |
| 4702 | // definition and an explicit instantiation declaration. An explicit |
| 4703 | // instantiation declaration begins with the extern keyword. [...] |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4704 | TemplateSpecializationKind TSK |
| 4705 | = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition |
| 4706 | : TSK_ExplicitInstantiationDeclaration; |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 4707 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 4708 | LookupResult Previous(*this, Name, D.getIdentifierLoc(), LookupOrdinaryName); |
| 4709 | LookupParsedName(Previous, S, &D.getCXXScopeSpec()); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4710 | |
| 4711 | if (!R->isFunctionType()) { |
| 4712 | // C++ [temp.explicit]p1: |
| 4713 | // A [...] static data member of a class template can be explicitly |
| 4714 | // instantiated from the member definition associated with its class |
| 4715 | // template. |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 4716 | if (Previous.isAmbiguous()) |
| 4717 | return true; |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4718 | |
John McCall | 1bcee0a | 2009-12-02 08:25:40 +0000 | [diff] [blame] | 4719 | VarDecl *Prev = Previous.getAsSingle<VarDecl>(); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4720 | if (!Prev || !Prev->isStaticDataMember()) { |
| 4721 | // We expect to see a data data member here. |
| 4722 | Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_not_known) |
| 4723 | << Name; |
| 4724 | for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end(); |
| 4725 | P != PEnd; ++P) |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 4726 | Diag((*P)->getLocation(), diag::note_explicit_instantiation_here); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4727 | return true; |
| 4728 | } |
| 4729 | |
| 4730 | if (!Prev->getInstantiatedFromStaticDataMember()) { |
| 4731 | // FIXME: Check for explicit specialization? |
| 4732 | Diag(D.getIdentifierLoc(), |
| 4733 | diag::err_explicit_instantiation_data_member_not_instantiated) |
| 4734 | << Prev; |
| 4735 | Diag(Prev->getLocation(), diag::note_explicit_instantiation_here); |
| 4736 | // FIXME: Can we provide a note showing where this was declared? |
| 4737 | return true; |
| 4738 | } |
| 4739 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 4740 | // C++0x [temp.explicit]p2: |
| 4741 | // If the explicit instantiation is for a member function, a member class |
| 4742 | // or a static data member of a class template specialization, the name of |
| 4743 | // the class template specialization in the qualified-id for the member |
| 4744 | // name shall be a simple-template-id. |
| 4745 | // |
| 4746 | // C++98 has the same restriction, just worded differently. |
| 4747 | if (!ScopeSpecifierHasTemplateId(D.getCXXScopeSpec())) |
| 4748 | Diag(D.getIdentifierLoc(), |
| 4749 | diag::err_explicit_instantiation_without_qualified_id) |
| 4750 | << Prev << D.getCXXScopeSpec().getRange(); |
| 4751 | |
| 4752 | // Check the scope of this explicit instantiation. |
| 4753 | CheckExplicitInstantiationScope(*this, Prev, D.getIdentifierLoc(), true); |
| 4754 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4755 | // Verify that it is okay to explicitly instantiate here. |
| 4756 | MemberSpecializationInfo *MSInfo = Prev->getMemberSpecializationInfo(); |
| 4757 | assert(MSInfo && "Missing static data member specialization info?"); |
| 4758 | bool SuppressNew = false; |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 4759 | if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK, Prev, |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4760 | MSInfo->getTemplateSpecializationKind(), |
| 4761 | MSInfo->getPointOfInstantiation(), |
| 4762 | SuppressNew)) |
| 4763 | return true; |
| 4764 | if (SuppressNew) |
| 4765 | return DeclPtrTy(); |
| 4766 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4767 | // Instantiate static data member. |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 4768 | Prev->setTemplateSpecializationKind(TSK, D.getIdentifierLoc()); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4769 | if (TSK == TSK_ExplicitInstantiationDefinition) |
Douglas Gregor | e2d3a3d | 2009-10-15 14:05:49 +0000 | [diff] [blame] | 4770 | InstantiateStaticDataMemberDefinition(D.getIdentifierLoc(), Prev, false, |
| 4771 | /*DefinitionRequired=*/true); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4772 | |
| 4773 | // FIXME: Create an ExplicitInstantiation node? |
| 4774 | return DeclPtrTy(); |
| 4775 | } |
| 4776 | |
Douglas Gregor | 0b60d9e | 2009-09-25 23:53:26 +0000 | [diff] [blame] | 4777 | // If the declarator is a template-id, translate the parser's template |
| 4778 | // argument list into our AST format. |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 4779 | bool HasExplicitTemplateArgs = false; |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 4780 | TemplateArgumentListInfo TemplateArgs; |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 4781 | if (D.getName().getKind() == UnqualifiedId::IK_TemplateId) { |
| 4782 | TemplateIdAnnotation *TemplateId = D.getName().TemplateId; |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 4783 | TemplateArgs.setLAngleLoc(TemplateId->LAngleLoc); |
| 4784 | TemplateArgs.setRAngleLoc(TemplateId->RAngleLoc); |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 4785 | ASTTemplateArgsPtr TemplateArgsPtr(*this, |
| 4786 | TemplateId->getTemplateArgs(), |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 4787 | TemplateId->NumArgs); |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 4788 | translateTemplateArguments(TemplateArgsPtr, TemplateArgs); |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 4789 | HasExplicitTemplateArgs = true; |
Douglas Gregor | b2f81cf | 2009-10-01 23:51:25 +0000 | [diff] [blame] | 4790 | TemplateArgsPtr.release(); |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 4791 | } |
Douglas Gregor | 0b60d9e | 2009-09-25 23:53:26 +0000 | [diff] [blame] | 4792 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4793 | // C++ [temp.explicit]p1: |
| 4794 | // A [...] function [...] can be explicitly instantiated from its template. |
| 4795 | // A member function [...] of a class template can be explicitly |
| 4796 | // instantiated from the member definition associated with its class |
| 4797 | // template. |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4798 | UnresolvedSet<8> Matches; |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4799 | for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end(); |
| 4800 | P != PEnd; ++P) { |
| 4801 | NamedDecl *Prev = *P; |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 4802 | if (!HasExplicitTemplateArgs) { |
| 4803 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Prev)) { |
| 4804 | if (Context.hasSameUnqualifiedType(Method->getType(), R)) { |
| 4805 | Matches.clear(); |
Douglas Gregor | 48026d2 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 4806 | |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4807 | Matches.addDecl(Method, P.getAccess()); |
Douglas Gregor | 48026d2 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 4808 | if (Method->getTemplateSpecializationKind() == TSK_Undeclared) |
| 4809 | break; |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 4810 | } |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4811 | } |
| 4812 | } |
| 4813 | |
| 4814 | FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Prev); |
| 4815 | if (!FunTmpl) |
| 4816 | continue; |
| 4817 | |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 4818 | TemplateDeductionInfo Info(Context, D.getIdentifierLoc()); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4819 | FunctionDecl *Specialization = 0; |
| 4820 | if (TemplateDeductionResult TDK |
Douglas Gregor | 48026d2 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 4821 | = DeduceTemplateArguments(FunTmpl, |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 4822 | (HasExplicitTemplateArgs ? &TemplateArgs : 0), |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4823 | R, Specialization, Info)) { |
| 4824 | // FIXME: Keep track of almost-matches? |
| 4825 | (void)TDK; |
| 4826 | continue; |
| 4827 | } |
| 4828 | |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4829 | Matches.addDecl(Specialization, P.getAccess()); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4830 | } |
| 4831 | |
| 4832 | // Find the most specialized function template specialization. |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4833 | UnresolvedSetIterator Result |
| 4834 | = getMostSpecialized(Matches.begin(), Matches.end(), TPOC_Other, |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4835 | D.getIdentifierLoc(), |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 4836 | PDiag(diag::err_explicit_instantiation_not_known) << Name, |
| 4837 | PDiag(diag::err_explicit_instantiation_ambiguous) << Name, |
| 4838 | PDiag(diag::note_explicit_instantiation_candidate)); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4839 | |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4840 | if (Result == Matches.end()) |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4841 | return true; |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4842 | |
| 4843 | // Ignore access control bits, we don't need them for redeclaration checking. |
| 4844 | FunctionDecl *Specialization = cast<FunctionDecl>(*Result); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4845 | |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 4846 | if (Specialization->getTemplateSpecializationKind() == TSK_Undeclared) { |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4847 | Diag(D.getIdentifierLoc(), |
| 4848 | diag::err_explicit_instantiation_member_function_not_instantiated) |
| 4849 | << Specialization |
| 4850 | << (Specialization->getTemplateSpecializationKind() == |
| 4851 | TSK_ExplicitSpecialization); |
| 4852 | Diag(Specialization->getLocation(), diag::note_explicit_instantiation_here); |
| 4853 | return true; |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 4854 | } |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 4855 | |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 4856 | FunctionDecl *PrevDecl = Specialization->getPreviousDeclaration(); |
Douglas Gregor | 583f33b | 2009-10-15 18:07:02 +0000 | [diff] [blame] | 4857 | if (!PrevDecl && Specialization->isThisDeclarationADefinition()) |
| 4858 | PrevDecl = Specialization; |
| 4859 | |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 4860 | if (PrevDecl) { |
| 4861 | bool SuppressNew = false; |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 4862 | if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK, |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 4863 | PrevDecl, |
| 4864 | PrevDecl->getTemplateSpecializationKind(), |
| 4865 | PrevDecl->getPointOfInstantiation(), |
| 4866 | SuppressNew)) |
| 4867 | return true; |
| 4868 | |
| 4869 | // FIXME: We may still want to build some representation of this |
| 4870 | // explicit specialization. |
| 4871 | if (SuppressNew) |
| 4872 | return DeclPtrTy(); |
| 4873 | } |
Anders Carlsson | 26d6e9d | 2009-11-24 05:34:41 +0000 | [diff] [blame] | 4874 | |
| 4875 | Specialization->setTemplateSpecializationKind(TSK, D.getIdentifierLoc()); |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 4876 | |
| 4877 | if (TSK == TSK_ExplicitInstantiationDefinition) |
| 4878 | InstantiateFunctionDefinition(D.getIdentifierLoc(), Specialization, |
| 4879 | false, /*DefinitionRequired=*/true); |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 4880 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 4881 | // C++0x [temp.explicit]p2: |
| 4882 | // If the explicit instantiation is for a member function, a member class |
| 4883 | // or a static data member of a class template specialization, the name of |
| 4884 | // the class template specialization in the qualified-id for the member |
| 4885 | // name shall be a simple-template-id. |
| 4886 | // |
| 4887 | // C++98 has the same restriction, just worded differently. |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 4888 | FunctionTemplateDecl *FunTmpl = Specialization->getPrimaryTemplate(); |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 4889 | if (D.getName().getKind() != UnqualifiedId::IK_TemplateId && !FunTmpl && |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 4890 | D.getCXXScopeSpec().isSet() && |
| 4891 | !ScopeSpecifierHasTemplateId(D.getCXXScopeSpec())) |
| 4892 | Diag(D.getIdentifierLoc(), |
| 4893 | diag::err_explicit_instantiation_without_qualified_id) |
| 4894 | << Specialization << D.getCXXScopeSpec().getRange(); |
| 4895 | |
| 4896 | CheckExplicitInstantiationScope(*this, |
| 4897 | FunTmpl? (NamedDecl *)FunTmpl |
| 4898 | : Specialization->getInstantiatedFromMemberFunction(), |
| 4899 | D.getIdentifierLoc(), |
| 4900 | D.getCXXScopeSpec().isSet()); |
| 4901 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4902 | // FIXME: Create some kind of ExplicitInstantiationDecl here. |
| 4903 | return DeclPtrTy(); |
| 4904 | } |
| 4905 | |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 4906 | Sema::TypeResult |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 4907 | Sema::ActOnDependentTag(Scope *S, unsigned TagSpec, TagUseKind TUK, |
| 4908 | const CXXScopeSpec &SS, IdentifierInfo *Name, |
| 4909 | SourceLocation TagLoc, SourceLocation NameLoc) { |
| 4910 | // This has to hold, because SS is expected to be defined. |
| 4911 | assert(Name && "Expected a name in a dependent tag"); |
| 4912 | |
| 4913 | NestedNameSpecifier *NNS |
| 4914 | = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); |
| 4915 | if (!NNS) |
| 4916 | return true; |
| 4917 | |
| 4918 | QualType T = CheckTypenameType(NNS, *Name, SourceRange(TagLoc, NameLoc)); |
| 4919 | if (T.isNull()) |
| 4920 | return true; |
| 4921 | |
| 4922 | TagDecl::TagKind TagKind = TagDecl::getTagKindForTypeSpec(TagSpec); |
| 4923 | QualType ElabType = Context.getElaboratedType(T, TagKind); |
| 4924 | |
| 4925 | return ElabType.getAsOpaquePtr(); |
| 4926 | } |
| 4927 | |
| 4928 | Sema::TypeResult |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 4929 | Sema::ActOnTypenameType(SourceLocation TypenameLoc, const CXXScopeSpec &SS, |
| 4930 | const IdentifierInfo &II, SourceLocation IdLoc) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4931 | NestedNameSpecifier *NNS |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 4932 | = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); |
| 4933 | if (!NNS) |
| 4934 | return true; |
| 4935 | |
| 4936 | QualType T = CheckTypenameType(NNS, II, SourceRange(TypenameLoc, IdLoc)); |
Douglas Gregor | 31a19b6 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 4937 | if (T.isNull()) |
| 4938 | return true; |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 4939 | return T.getAsOpaquePtr(); |
| 4940 | } |
| 4941 | |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 4942 | Sema::TypeResult |
| 4943 | Sema::ActOnTypenameType(SourceLocation TypenameLoc, const CXXScopeSpec &SS, |
| 4944 | SourceLocation TemplateLoc, TypeTy *Ty) { |
Argyrios Kyrtzidis | e866190 | 2009-08-19 01:28:28 +0000 | [diff] [blame] | 4945 | QualType T = GetTypeFromParser(Ty); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4946 | NestedNameSpecifier *NNS |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 4947 | = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4948 | const TemplateSpecializationType *TemplateId |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4949 | = T->getAs<TemplateSpecializationType>(); |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 4950 | assert(TemplateId && "Expected a template specialization type"); |
| 4951 | |
Douglas Gregor | 6946baf | 2009-09-02 13:05:45 +0000 | [diff] [blame] | 4952 | if (computeDeclContext(SS, false)) { |
| 4953 | // If we can compute a declaration context, then the "typename" |
| 4954 | // keyword was superfluous. Just build a QualifiedNameType to keep |
| 4955 | // track of the nested-name-specifier. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4956 | |
Douglas Gregor | 6946baf | 2009-09-02 13:05:45 +0000 | [diff] [blame] | 4957 | // FIXME: Note that the QualifiedNameType had the "typename" keyword! |
| 4958 | return Context.getQualifiedNameType(NNS, T).getAsOpaquePtr(); |
| 4959 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4960 | |
Douglas Gregor | 4a2023f | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 4961 | return Context.getDependentNameType(ETK_Typename, NNS, TemplateId) |
| 4962 | .getAsOpaquePtr(); |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 4963 | } |
| 4964 | |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 4965 | /// \brief Build the type that describes a C++ typename specifier, |
| 4966 | /// e.g., "typename T::type". |
| 4967 | QualType |
| 4968 | Sema::CheckTypenameType(NestedNameSpecifier *NNS, const IdentifierInfo &II, |
| 4969 | SourceRange Range) { |
Douglas Gregor | 42af25f | 2009-05-11 19:58:34 +0000 | [diff] [blame] | 4970 | CXXRecordDecl *CurrentInstantiation = 0; |
| 4971 | if (NNS->isDependent()) { |
| 4972 | CurrentInstantiation = getCurrentInstantiationOf(NNS); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 4973 | |
Douglas Gregor | 42af25f | 2009-05-11 19:58:34 +0000 | [diff] [blame] | 4974 | // If the nested-name-specifier does not refer to the current |
| 4975 | // instantiation, then build a typename type. |
| 4976 | if (!CurrentInstantiation) |
Douglas Gregor | 4a2023f | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 4977 | return Context.getDependentNameType(ETK_Typename, NNS, &II); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4978 | |
Douglas Gregor | de18d12 | 2009-09-02 13:12:51 +0000 | [diff] [blame] | 4979 | // The nested-name-specifier refers to the current instantiation, so the |
| 4980 | // "typename" keyword itself is superfluous. In C++03, the program is |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4981 | // actually ill-formed. However, DR 382 (in C++0x CD1) allows such |
Douglas Gregor | de18d12 | 2009-09-02 13:12:51 +0000 | [diff] [blame] | 4982 | // extraneous "typename" keywords, and we retroactively apply this DR to |
| 4983 | // C++03 code. |
Douglas Gregor | 42af25f | 2009-05-11 19:58:34 +0000 | [diff] [blame] | 4984 | } |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 4985 | |
Douglas Gregor | 42af25f | 2009-05-11 19:58:34 +0000 | [diff] [blame] | 4986 | DeclContext *Ctx = 0; |
| 4987 | |
| 4988 | if (CurrentInstantiation) |
| 4989 | Ctx = CurrentInstantiation; |
| 4990 | else { |
| 4991 | CXXScopeSpec SS; |
| 4992 | SS.setScopeRep(NNS); |
| 4993 | SS.setRange(Range); |
| 4994 | if (RequireCompleteDeclContext(SS)) |
| 4995 | return QualType(); |
| 4996 | |
| 4997 | Ctx = computeDeclContext(SS); |
| 4998 | } |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 4999 | assert(Ctx && "No declaration context?"); |
| 5000 | |
| 5001 | DeclarationName Name(&II); |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 5002 | LookupResult Result(*this, Name, Range.getEnd(), LookupOrdinaryName); |
| 5003 | LookupQualifiedName(Result, Ctx); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5004 | unsigned DiagID = 0; |
| 5005 | Decl *Referenced = 0; |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 5006 | switch (Result.getResultKind()) { |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5007 | case LookupResult::NotFound: |
Douglas Gregor | 3f09327 | 2009-10-13 21:16:44 +0000 | [diff] [blame] | 5008 | DiagID = diag::err_typename_nested_not_found; |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5009 | break; |
Douglas Gregor | 7d3f576 | 2010-01-15 01:44:47 +0000 | [diff] [blame] | 5010 | |
| 5011 | case LookupResult::NotFoundInCurrentInstantiation: |
| 5012 | // Okay, it's a member of an unknown instantiation. |
Douglas Gregor | 4a2023f | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 5013 | return Context.getDependentNameType(ETK_Typename, NNS, &II); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5014 | |
| 5015 | case LookupResult::Found: |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 5016 | if (TypeDecl *Type = dyn_cast<TypeDecl>(Result.getFoundDecl())) { |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5017 | // We found a type. Build a QualifiedNameType, since the |
| 5018 | // typename-specifier was just sugar. FIXME: Tell |
| 5019 | // QualifiedNameType that it has a "typename" prefix. |
| 5020 | return Context.getQualifiedNameType(NNS, Context.getTypeDeclType(Type)); |
| 5021 | } |
| 5022 | |
| 5023 | DiagID = diag::err_typename_nested_not_type; |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 5024 | Referenced = Result.getFoundDecl(); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5025 | break; |
| 5026 | |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 5027 | case LookupResult::FoundUnresolvedValue: |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 5028 | llvm_unreachable("unresolved using decl in non-dependent context"); |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 5029 | return QualType(); |
| 5030 | |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5031 | case LookupResult::FoundOverloaded: |
| 5032 | DiagID = diag::err_typename_nested_not_type; |
| 5033 | Referenced = *Result.begin(); |
| 5034 | break; |
| 5035 | |
John McCall | 6e24726 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 5036 | case LookupResult::Ambiguous: |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5037 | return QualType(); |
| 5038 | } |
| 5039 | |
| 5040 | // If we get here, it's because name lookup did not find a |
| 5041 | // type. Emit an appropriate diagnostic and return an error. |
Douglas Gregor | 3f09327 | 2009-10-13 21:16:44 +0000 | [diff] [blame] | 5042 | Diag(Range.getEnd(), DiagID) << Range << Name << Ctx; |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5043 | if (Referenced) |
| 5044 | Diag(Referenced->getLocation(), diag::note_typename_refers_here) |
| 5045 | << Name; |
| 5046 | return QualType(); |
| 5047 | } |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 5048 | |
| 5049 | namespace { |
| 5050 | // See Sema::RebuildTypeInCurrentInstantiation |
Benjamin Kramer | 85b4521 | 2009-11-28 19:45:26 +0000 | [diff] [blame] | 5051 | class CurrentInstantiationRebuilder |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5052 | : public TreeTransform<CurrentInstantiationRebuilder> { |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 5053 | SourceLocation Loc; |
| 5054 | DeclarationName Entity; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5055 | |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 5056 | public: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5057 | CurrentInstantiationRebuilder(Sema &SemaRef, |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 5058 | SourceLocation Loc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5059 | DeclarationName Entity) |
| 5060 | : TreeTransform<CurrentInstantiationRebuilder>(SemaRef), |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 5061 | Loc(Loc), Entity(Entity) { } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5062 | |
| 5063 | /// \brief Determine whether the given type \p T has already been |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 5064 | /// transformed. |
| 5065 | /// |
| 5066 | /// For the purposes of type reconstruction, a type has already been |
| 5067 | /// transformed if it is NULL or if it is not dependent. |
| 5068 | bool AlreadyTransformed(QualType T) { |
| 5069 | return T.isNull() || !T->isDependentType(); |
| 5070 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5071 | |
| 5072 | /// \brief Returns the location of the entity whose type is being |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 5073 | /// rebuilt. |
| 5074 | SourceLocation getBaseLocation() { return Loc; } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5075 | |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 5076 | /// \brief Returns the name of the entity whose type is being rebuilt. |
| 5077 | DeclarationName getBaseEntity() { return Entity; } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5078 | |
Douglas Gregor | 972e6ce | 2009-10-27 06:26:26 +0000 | [diff] [blame] | 5079 | /// \brief Sets the "base" location and entity when that |
| 5080 | /// information is known based on another transformation. |
| 5081 | void setBase(SourceLocation Loc, DeclarationName Entity) { |
| 5082 | this->Loc = Loc; |
| 5083 | this->Entity = Entity; |
| 5084 | } |
| 5085 | |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 5086 | /// \brief Transforms an expression by returning the expression itself |
| 5087 | /// (an identity function). |
| 5088 | /// |
| 5089 | /// FIXME: This is completely unsafe; we will need to actually clone the |
| 5090 | /// expressions. |
| 5091 | Sema::OwningExprResult TransformExpr(Expr *E) { |
| 5092 | return getSema().Owned(E); |
| 5093 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5094 | |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 5095 | /// \brief Transforms a typename type by determining whether the type now |
| 5096 | /// refers to a member of the current instantiation, and then |
| 5097 | /// type-checking and building a QualifiedNameType (when possible). |
Douglas Gregor | 4714c12 | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 5098 | QualType TransformDependentNameType(TypeLocBuilder &TLB, DependentNameTypeLoc TL, |
Douglas Gregor | 124b878 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 5099 | QualType ObjectType); |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 5100 | }; |
| 5101 | } |
| 5102 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5103 | QualType |
Douglas Gregor | 4714c12 | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 5104 | CurrentInstantiationRebuilder::TransformDependentNameType(TypeLocBuilder &TLB, |
| 5105 | DependentNameTypeLoc TL, |
Douglas Gregor | 124b878 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 5106 | QualType ObjectType) { |
Douglas Gregor | 4714c12 | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 5107 | DependentNameType *T = TL.getTypePtr(); |
John McCall | a2becad | 2009-10-21 00:40:46 +0000 | [diff] [blame] | 5108 | |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 5109 | NestedNameSpecifier *NNS |
| 5110 | = TransformNestedNameSpecifier(T->getQualifier(), |
Douglas Gregor | 124b878 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 5111 | /*FIXME:*/SourceRange(getBaseLocation()), |
| 5112 | ObjectType); |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 5113 | if (!NNS) |
| 5114 | return QualType(); |
| 5115 | |
| 5116 | // If the nested-name-specifier did not change, and we cannot compute the |
| 5117 | // context corresponding to the nested-name-specifier, then this |
| 5118 | // typename type will not change; exit early. |
| 5119 | CXXScopeSpec SS; |
| 5120 | SS.setRange(SourceRange(getBaseLocation())); |
| 5121 | SS.setScopeRep(NNS); |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 5122 | |
| 5123 | QualType Result; |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 5124 | if (NNS == T->getQualifier() && getSema().computeDeclContext(SS) == 0) |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 5125 | Result = QualType(T, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5126 | |
| 5127 | // Rebuild the typename type, which will probably turn into a |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 5128 | // QualifiedNameType. |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 5129 | else if (const TemplateSpecializationType *TemplateId = T->getTemplateId()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5130 | QualType NewTemplateId |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 5131 | = TransformType(QualType(TemplateId, 0)); |
| 5132 | if (NewTemplateId.isNull()) |
| 5133 | return QualType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5134 | |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 5135 | if (NNS == T->getQualifier() && |
| 5136 | NewTemplateId == QualType(TemplateId, 0)) |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 5137 | Result = QualType(T, 0); |
| 5138 | else |
Douglas Gregor | 4a2023f | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 5139 | Result = getDerived().RebuildDependentNameType(T->getKeyword(), |
| 5140 | NNS, NewTemplateId); |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 5141 | } else |
Douglas Gregor | 4a2023f | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 5142 | Result = getDerived().RebuildDependentNameType(T->getKeyword(), |
| 5143 | NNS, T->getIdentifier(), |
| 5144 | SourceRange(TL.getNameLoc())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5145 | |
Douglas Gregor | a50ce32 | 2010-03-07 23:26:22 +0000 | [diff] [blame] | 5146 | if (Result.isNull()) |
| 5147 | return QualType(); |
| 5148 | |
Douglas Gregor | 4714c12 | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 5149 | DependentNameTypeLoc NewTL = TLB.push<DependentNameTypeLoc>(Result); |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 5150 | NewTL.setNameLoc(TL.getNameLoc()); |
| 5151 | return Result; |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 5152 | } |
| 5153 | |
| 5154 | /// \brief Rebuilds a type within the context of the current instantiation. |
| 5155 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5156 | /// 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] | 5157 | /// a class template (or class template partial specialization) that was parsed |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5158 | /// and constructed before we entered the scope of the class template (or |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 5159 | /// partial specialization thereof). This routine will rebuild that type now |
| 5160 | /// that we have entered the declarator's scope, which may produce different |
| 5161 | /// canonical types, e.g., |
| 5162 | /// |
| 5163 | /// \code |
| 5164 | /// template<typename T> |
| 5165 | /// struct X { |
| 5166 | /// typedef T* pointer; |
| 5167 | /// pointer data(); |
| 5168 | /// }; |
| 5169 | /// |
| 5170 | /// template<typename T> |
| 5171 | /// typename X<T>::pointer X<T>::data() { ... } |
| 5172 | /// \endcode |
| 5173 | /// |
Douglas Gregor | 4714c12 | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 5174 | /// Here, the type "typename X<T>::pointer" will be created as a DependentNameType, |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 5175 | /// since we do not know that we can look into X<T> when we parsed the type. |
| 5176 | /// This function will rebuild the type, performing the lookup of "pointer" |
| 5177 | /// in X<T> and returning a QualifiedNameType whose canonical type is the same |
| 5178 | /// as the canonical type of T*, allowing the return types of the out-of-line |
| 5179 | /// definition and the declaration to match. |
| 5180 | QualType Sema::RebuildTypeInCurrentInstantiation(QualType T, SourceLocation Loc, |
| 5181 | DeclarationName Name) { |
| 5182 | if (T.isNull() || !T->isDependentType()) |
| 5183 | return T; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5184 | |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 5185 | CurrentInstantiationRebuilder Rebuilder(*this, Loc, Name); |
| 5186 | return Rebuilder.TransformType(T); |
Benjamin Kramer | 27ba2f0 | 2009-08-11 22:33:06 +0000 | [diff] [blame] | 5187 | } |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 5188 | |
| 5189 | /// \brief Produces a formatted string that describes the binding of |
| 5190 | /// template parameters to template arguments. |
| 5191 | std::string |
| 5192 | Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params, |
| 5193 | const TemplateArgumentList &Args) { |
Douglas Gregor | 9148c3f | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 5194 | // FIXME: For variadic templates, we'll need to get the structured list. |
| 5195 | return getTemplateArgumentBindingsText(Params, Args.getFlatArgumentList(), |
| 5196 | Args.flat_size()); |
| 5197 | } |
| 5198 | |
| 5199 | std::string |
| 5200 | Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params, |
| 5201 | const TemplateArgument *Args, |
| 5202 | unsigned NumArgs) { |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 5203 | std::string Result; |
| 5204 | |
Douglas Gregor | 9148c3f | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 5205 | if (!Params || Params->size() == 0 || NumArgs == 0) |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 5206 | return Result; |
| 5207 | |
| 5208 | for (unsigned I = 0, N = Params->size(); I != N; ++I) { |
Douglas Gregor | 9148c3f | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 5209 | if (I >= NumArgs) |
| 5210 | break; |
| 5211 | |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 5212 | if (I == 0) |
| 5213 | Result += "[with "; |
| 5214 | else |
| 5215 | Result += ", "; |
| 5216 | |
| 5217 | if (const IdentifierInfo *Id = Params->getParam(I)->getIdentifier()) { |
| 5218 | Result += Id->getName(); |
| 5219 | } else { |
| 5220 | Result += '$'; |
| 5221 | Result += llvm::utostr(I); |
| 5222 | } |
| 5223 | |
| 5224 | Result += " = "; |
| 5225 | |
| 5226 | switch (Args[I].getKind()) { |
| 5227 | case TemplateArgument::Null: |
| 5228 | Result += "<no value>"; |
| 5229 | break; |
| 5230 | |
| 5231 | case TemplateArgument::Type: { |
| 5232 | std::string TypeStr; |
| 5233 | Args[I].getAsType().getAsStringInternal(TypeStr, |
| 5234 | Context.PrintingPolicy); |
| 5235 | Result += TypeStr; |
| 5236 | break; |
| 5237 | } |
| 5238 | |
| 5239 | case TemplateArgument::Declaration: { |
| 5240 | bool Unnamed = true; |
| 5241 | if (NamedDecl *ND = dyn_cast_or_null<NamedDecl>(Args[I].getAsDecl())) { |
| 5242 | if (ND->getDeclName()) { |
| 5243 | Unnamed = false; |
| 5244 | Result += ND->getNameAsString(); |
| 5245 | } |
| 5246 | } |
| 5247 | |
| 5248 | if (Unnamed) { |
| 5249 | Result += "<anonymous>"; |
| 5250 | } |
| 5251 | break; |
| 5252 | } |
| 5253 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 5254 | case TemplateArgument::Template: { |
| 5255 | std::string Str; |
| 5256 | llvm::raw_string_ostream OS(Str); |
| 5257 | Args[I].getAsTemplate().print(OS, Context.PrintingPolicy); |
| 5258 | Result += OS.str(); |
| 5259 | break; |
| 5260 | } |
| 5261 | |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 5262 | case TemplateArgument::Integral: { |
| 5263 | Result += Args[I].getAsIntegral()->toString(10); |
| 5264 | break; |
| 5265 | } |
| 5266 | |
| 5267 | case TemplateArgument::Expression: { |
| 5268 | assert(false && "No expressions in deduced template arguments!"); |
| 5269 | Result += "<expression>"; |
| 5270 | break; |
| 5271 | } |
| 5272 | |
| 5273 | case TemplateArgument::Pack: |
| 5274 | // FIXME: Format template argument packs |
| 5275 | Result += "<template argument pack>"; |
| 5276 | break; |
| 5277 | } |
| 5278 | } |
| 5279 | |
| 5280 | Result += ']'; |
| 5281 | return Result; |
| 5282 | } |