Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 1 | //===------- SemaTemplate.cpp - Semantic Analysis for C++ Templates -------===/ |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 7 | //===----------------------------------------------------------------------===/ |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 8 | // |
| 9 | // This file implements semantic analysis for C++ templates. |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 10 | //===----------------------------------------------------------------------===/ |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 11 | |
| 12 | #include "Sema.h" |
John McCall | 7d384dd | 2009-11-18 07:57:50 +0000 | [diff] [blame] | 13 | #include "Lookup.h" |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 14 | #include "TreeTransform.h" |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 15 | #include "clang/AST/ASTContext.h" |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 16 | #include "clang/AST/Expr.h" |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 17 | #include "clang/AST/ExprCXX.h" |
John McCall | 92b7f70 | 2010-03-11 07:50:04 +0000 | [diff] [blame] | 18 | #include "clang/AST/DeclFriend.h" |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 19 | #include "clang/AST/DeclTemplate.h" |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 20 | #include "clang/Parse/DeclSpec.h" |
Douglas Gregor | 314b97f | 2009-11-10 19:49:08 +0000 | [diff] [blame] | 21 | #include "clang/Parse/Template.h" |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 22 | #include "clang/Basic/LangOptions.h" |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 23 | #include "clang/Basic/PartialDiagnostic.h" |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/StringExtras.h" |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 25 | using namespace clang; |
| 26 | |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 27 | /// \brief Determine whether the declaration found is acceptable as the name |
| 28 | /// of a template and, if so, return that template declaration. Otherwise, |
| 29 | /// returns NULL. |
| 30 | static NamedDecl *isAcceptableTemplateName(ASTContext &Context, NamedDecl *D) { |
| 31 | if (!D) |
| 32 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 33 | |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 34 | if (isa<TemplateDecl>(D)) |
| 35 | return D; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 36 | |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 37 | if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) { |
| 38 | // C++ [temp.local]p1: |
| 39 | // Like normal (non-template) classes, class templates have an |
| 40 | // injected-class-name (Clause 9). The injected-class-name |
| 41 | // can be used with or without a template-argument-list. When |
| 42 | // it is used without a template-argument-list, it is |
| 43 | // equivalent to the injected-class-name followed by the |
| 44 | // template-parameters of the class template enclosed in |
| 45 | // <>. When it is used with a template-argument-list, it |
| 46 | // refers to the specified class template specialization, |
| 47 | // which could be the current specialization or another |
| 48 | // specialization. |
| 49 | if (Record->isInjectedClassName()) { |
Douglas Gregor | 542b548 | 2009-10-14 17:30:58 +0000 | [diff] [blame] | 50 | Record = cast<CXXRecordDecl>(Record->getDeclContext()); |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 51 | if (Record->getDescribedClassTemplate()) |
| 52 | return Record->getDescribedClassTemplate(); |
| 53 | |
| 54 | if (ClassTemplateSpecializationDecl *Spec |
| 55 | = dyn_cast<ClassTemplateSpecializationDecl>(Record)) |
| 56 | return Spec->getSpecializedTemplate(); |
| 57 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 58 | |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 59 | return 0; |
| 60 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 61 | |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 62 | return 0; |
| 63 | } |
| 64 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 65 | static void FilterAcceptableTemplateNames(ASTContext &C, LookupResult &R) { |
Douglas Gregor | 01e56ae | 2010-04-12 20:54:26 +0000 | [diff] [blame] | 66 | // The set of class templates we've already seen. |
| 67 | llvm::SmallPtrSet<ClassTemplateDecl *, 8> ClassTemplates; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 68 | LookupResult::Filter filter = R.makeFilter(); |
| 69 | while (filter.hasNext()) { |
| 70 | NamedDecl *Orig = filter.next(); |
| 71 | NamedDecl *Repl = isAcceptableTemplateName(C, Orig->getUnderlyingDecl()); |
| 72 | if (!Repl) |
| 73 | filter.erase(); |
Douglas Gregor | 01e56ae | 2010-04-12 20:54:26 +0000 | [diff] [blame] | 74 | else if (Repl != Orig) { |
| 75 | |
| 76 | // C++ [temp.local]p3: |
| 77 | // A lookup that finds an injected-class-name (10.2) can result in an |
| 78 | // ambiguity in certain cases (for example, if it is found in more than |
| 79 | // one base class). If all of the injected-class-names that are found |
| 80 | // refer to specializations of the same class template, and if the name |
| 81 | // is followed by a template-argument-list, the reference refers to the |
| 82 | // class template itself and not a specialization thereof, and is not |
| 83 | // ambiguous. |
| 84 | // |
| 85 | // FIXME: Will we eventually have to do the same for alias templates? |
| 86 | if (ClassTemplateDecl *ClassTmpl = dyn_cast<ClassTemplateDecl>(Repl)) |
| 87 | if (!ClassTemplates.insert(ClassTmpl)) { |
| 88 | filter.erase(); |
| 89 | continue; |
| 90 | } |
| 91 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 92 | filter.replace(Repl); |
Douglas Gregor | 01e56ae | 2010-04-12 20:54:26 +0000 | [diff] [blame] | 93 | } |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 94 | } |
| 95 | filter.done(); |
| 96 | } |
| 97 | |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 98 | TemplateNameKind Sema::isTemplateName(Scope *S, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 99 | CXXScopeSpec &SS, |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 100 | UnqualifiedId &Name, |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 101 | TypeTy *ObjectTypePtr, |
Douglas Gregor | 495c35d | 2009-08-25 22:51:20 +0000 | [diff] [blame] | 102 | bool EnteringContext, |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 103 | TemplateTy &TemplateResult) { |
Douglas Gregor | b862b8f | 2010-01-11 23:29:10 +0000 | [diff] [blame] | 104 | assert(getLangOptions().CPlusPlus && "No template names in C!"); |
| 105 | |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 106 | DeclarationName TName; |
| 107 | |
| 108 | switch (Name.getKind()) { |
| 109 | case UnqualifiedId::IK_Identifier: |
| 110 | TName = DeclarationName(Name.Identifier); |
| 111 | break; |
| 112 | |
| 113 | case UnqualifiedId::IK_OperatorFunctionId: |
| 114 | TName = Context.DeclarationNames.getCXXOperatorName( |
| 115 | Name.OperatorFunctionId.Operator); |
| 116 | break; |
| 117 | |
Sean Hunt | e6252d1 | 2009-11-28 08:58:14 +0000 | [diff] [blame] | 118 | case UnqualifiedId::IK_LiteralOperatorId: |
Sean Hunt | 3e518bd | 2009-11-29 07:34:05 +0000 | [diff] [blame] | 119 | TName = Context.DeclarationNames.getCXXLiteralOperatorName(Name.Identifier); |
| 120 | break; |
Sean Hunt | e6252d1 | 2009-11-28 08:58:14 +0000 | [diff] [blame] | 121 | |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 122 | default: |
| 123 | return TNK_Non_template; |
| 124 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 125 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 126 | QualType ObjectType = QualType::getFromOpaquePtr(ObjectTypePtr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 127 | |
Douglas Gregor | bfea239 | 2009-12-31 08:11:17 +0000 | [diff] [blame] | 128 | LookupResult R(*this, TName, Name.getSourceRange().getBegin(), |
| 129 | LookupOrdinaryName); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 130 | R.suppressDiagnostics(); |
| 131 | LookupTemplateName(R, S, SS, ObjectType, EnteringContext); |
Douglas Gregor | 01e56ae | 2010-04-12 20:54:26 +0000 | [diff] [blame] | 132 | if (R.empty() || R.isAmbiguous()) |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 133 | return TNK_Non_template; |
| 134 | |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 135 | TemplateName Template; |
| 136 | TemplateNameKind TemplateKind; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 137 | |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 138 | unsigned ResultCount = R.end() - R.begin(); |
| 139 | if (ResultCount > 1) { |
| 140 | // We assume that we'll preserve the qualifier from a function |
| 141 | // template name in other ways. |
| 142 | Template = Context.getOverloadedTemplateName(R.begin(), R.end()); |
| 143 | TemplateKind = TNK_Function_template; |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 144 | } else { |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 145 | TemplateDecl *TD = cast<TemplateDecl>((*R.begin())->getUnderlyingDecl()); |
| 146 | |
| 147 | if (SS.isSet() && !SS.isInvalid()) { |
| 148 | NestedNameSpecifier *Qualifier |
| 149 | = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); |
| 150 | Template = Context.getQualifiedTemplateName(Qualifier, false, TD); |
| 151 | } else { |
| 152 | Template = TemplateName(TD); |
| 153 | } |
| 154 | |
| 155 | if (isa<FunctionTemplateDecl>(TD)) |
| 156 | TemplateKind = TNK_Function_template; |
| 157 | else { |
| 158 | assert(isa<ClassTemplateDecl>(TD) || isa<TemplateTemplateParmDecl>(TD)); |
| 159 | TemplateKind = TNK_Type_template; |
| 160 | } |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 161 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 162 | |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 163 | TemplateResult = TemplateTy::make(Template); |
| 164 | return TemplateKind; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 165 | } |
| 166 | |
Douglas Gregor | 84d0a19 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 167 | bool Sema::DiagnoseUnknownTemplateName(const IdentifierInfo &II, |
| 168 | SourceLocation IILoc, |
| 169 | Scope *S, |
| 170 | const CXXScopeSpec *SS, |
| 171 | TemplateTy &SuggestedTemplate, |
| 172 | TemplateNameKind &SuggestedKind) { |
| 173 | // We can't recover unless there's a dependent scope specifier preceding the |
| 174 | // template name. |
| 175 | if (!SS || !SS->isSet() || !isDependentScopeSpecifier(*SS) || |
| 176 | computeDeclContext(*SS)) |
| 177 | return false; |
| 178 | |
| 179 | // The code is missing a 'template' keyword prior to the dependent template |
| 180 | // name. |
| 181 | NestedNameSpecifier *Qualifier = (NestedNameSpecifier*)SS->getScopeRep(); |
| 182 | Diag(IILoc, diag::err_template_kw_missing) |
| 183 | << Qualifier << II.getName() |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 184 | << FixItHint::CreateInsertion(IILoc, "template "); |
Douglas Gregor | 84d0a19 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 185 | SuggestedTemplate |
| 186 | = TemplateTy::make(Context.getDependentTemplateName(Qualifier, &II)); |
| 187 | SuggestedKind = TNK_Dependent_template_name; |
| 188 | return true; |
| 189 | } |
| 190 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 191 | void Sema::LookupTemplateName(LookupResult &Found, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 192 | Scope *S, CXXScopeSpec &SS, |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 193 | QualType ObjectType, |
| 194 | bool EnteringContext) { |
| 195 | // Determine where to perform name lookup |
| 196 | DeclContext *LookupCtx = 0; |
| 197 | bool isDependent = false; |
| 198 | if (!ObjectType.isNull()) { |
| 199 | // This nested-name-specifier occurs in a member access expression, e.g., |
| 200 | // x->B::f, and we are looking into the type of the object. |
| 201 | assert(!SS.isSet() && "ObjectType and scope specifier cannot coexist"); |
| 202 | LookupCtx = computeDeclContext(ObjectType); |
| 203 | isDependent = ObjectType->isDependentType(); |
| 204 | assert((isDependent || !ObjectType->isIncompleteType()) && |
| 205 | "Caller should have completed object type"); |
| 206 | } else if (SS.isSet()) { |
| 207 | // This nested-name-specifier occurs after another nested-name-specifier, |
| 208 | // so long into the context associated with the prior nested-name-specifier. |
| 209 | LookupCtx = computeDeclContext(SS, EnteringContext); |
| 210 | isDependent = isDependentScopeSpecifier(SS); |
| 211 | |
| 212 | // The declaration context must be complete. |
| 213 | if (LookupCtx && RequireCompleteDeclContext(SS)) |
| 214 | return; |
| 215 | } |
| 216 | |
| 217 | bool ObjectTypeSearchedInScope = false; |
| 218 | if (LookupCtx) { |
| 219 | // Perform "qualified" name lookup into the declaration context we |
| 220 | // computed, which is either the type of the base of a member access |
| 221 | // expression or the declaration context associated with a prior |
| 222 | // nested-name-specifier. |
| 223 | LookupQualifiedName(Found, LookupCtx); |
| 224 | |
| 225 | if (!ObjectType.isNull() && Found.empty()) { |
| 226 | // C++ [basic.lookup.classref]p1: |
| 227 | // In a class member access expression (5.2.5), if the . or -> token is |
| 228 | // immediately followed by an identifier followed by a <, the |
| 229 | // identifier must be looked up to determine whether the < is the |
| 230 | // beginning of a template argument list (14.2) or a less-than operator. |
| 231 | // The identifier is first looked up in the class of the object |
| 232 | // expression. If the identifier is not found, it is then looked up in |
| 233 | // the context of the entire postfix-expression and shall name a class |
| 234 | // or function template. |
| 235 | // |
| 236 | // FIXME: When we're instantiating a template, do we actually have to |
| 237 | // look in the scope of the template? Seems fishy... |
| 238 | if (S) LookupName(Found, S); |
| 239 | ObjectTypeSearchedInScope = true; |
| 240 | } |
| 241 | } else if (isDependent) { |
Douglas Gregor | 2e93388 | 2010-01-12 17:06:20 +0000 | [diff] [blame] | 242 | // We cannot look into a dependent object type or nested nme |
| 243 | // specifier. |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 244 | return; |
| 245 | } else { |
| 246 | // Perform unqualified name lookup in the current scope. |
| 247 | LookupName(Found, S); |
| 248 | } |
| 249 | |
Douglas Gregor | 2e93388 | 2010-01-12 17:06:20 +0000 | [diff] [blame] | 250 | if (Found.empty() && !isDependent) { |
Douglas Gregor | bfea239 | 2009-12-31 08:11:17 +0000 | [diff] [blame] | 251 | // If we did not find any names, attempt to correct any typos. |
| 252 | DeclarationName Name = Found.getLookupName(); |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 253 | if (DeclarationName Corrected = CorrectTypo(Found, S, &SS, LookupCtx, |
| 254 | false, CTC_CXXCasts)) { |
Douglas Gregor | bfea239 | 2009-12-31 08:11:17 +0000 | [diff] [blame] | 255 | FilterAcceptableTemplateNames(Context, Found); |
| 256 | if (!Found.empty() && isa<TemplateDecl>(*Found.begin())) { |
| 257 | if (LookupCtx) |
| 258 | Diag(Found.getNameLoc(), diag::err_no_member_template_suggest) |
| 259 | << Name << LookupCtx << Found.getLookupName() << SS.getRange() |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 260 | << FixItHint::CreateReplacement(Found.getNameLoc(), |
Douglas Gregor | bfea239 | 2009-12-31 08:11:17 +0000 | [diff] [blame] | 261 | Found.getLookupName().getAsString()); |
| 262 | else |
| 263 | Diag(Found.getNameLoc(), diag::err_no_template_suggest) |
| 264 | << Name << Found.getLookupName() |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 265 | << FixItHint::CreateReplacement(Found.getNameLoc(), |
Douglas Gregor | bfea239 | 2009-12-31 08:11:17 +0000 | [diff] [blame] | 266 | Found.getLookupName().getAsString()); |
Douglas Gregor | 67dd1d4 | 2010-01-07 00:17:44 +0000 | [diff] [blame] | 267 | if (TemplateDecl *Template = Found.getAsSingle<TemplateDecl>()) |
| 268 | Diag(Template->getLocation(), diag::note_previous_decl) |
| 269 | << Template->getDeclName(); |
Douglas Gregor | bfea239 | 2009-12-31 08:11:17 +0000 | [diff] [blame] | 270 | } else |
| 271 | Found.clear(); |
| 272 | } else { |
| 273 | Found.clear(); |
| 274 | } |
| 275 | } |
| 276 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 277 | FilterAcceptableTemplateNames(Context, Found); |
| 278 | if (Found.empty()) |
| 279 | return; |
| 280 | |
| 281 | if (S && !ObjectType.isNull() && !ObjectTypeSearchedInScope) { |
| 282 | // C++ [basic.lookup.classref]p1: |
| 283 | // [...] If the lookup in the class of the object expression finds a |
| 284 | // template, the name is also looked up in the context of the entire |
| 285 | // postfix-expression and [...] |
| 286 | // |
| 287 | LookupResult FoundOuter(*this, Found.getLookupName(), Found.getNameLoc(), |
| 288 | LookupOrdinaryName); |
| 289 | LookupName(FoundOuter, S); |
| 290 | FilterAcceptableTemplateNames(Context, FoundOuter); |
Douglas Gregor | 01e56ae | 2010-04-12 20:54:26 +0000 | [diff] [blame] | 291 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 292 | if (FoundOuter.empty()) { |
| 293 | // - if the name is not found, the name found in the class of the |
| 294 | // object expression is used, otherwise |
| 295 | } else if (!FoundOuter.getAsSingle<ClassTemplateDecl>()) { |
| 296 | // - if the name is found in the context of the entire |
| 297 | // postfix-expression and does not name a class template, the name |
| 298 | // found in the class of the object expression is used, otherwise |
| 299 | } else { |
| 300 | // - if the name found is a class template, it must refer to the same |
| 301 | // entity as the one found in the class of the object expression, |
| 302 | // otherwise the program is ill-formed. |
| 303 | if (!Found.isSingleResult() || |
| 304 | Found.getFoundDecl()->getCanonicalDecl() |
| 305 | != FoundOuter.getFoundDecl()->getCanonicalDecl()) { |
| 306 | Diag(Found.getNameLoc(), |
| 307 | diag::err_nested_name_member_ref_lookup_ambiguous) |
| 308 | << Found.getLookupName(); |
| 309 | Diag(Found.getRepresentativeDecl()->getLocation(), |
| 310 | diag::note_ambig_member_ref_object_type) |
| 311 | << ObjectType; |
| 312 | Diag(FoundOuter.getFoundDecl()->getLocation(), |
| 313 | diag::note_ambig_member_ref_scope); |
| 314 | |
| 315 | // Recover by taking the template that we found in the object |
| 316 | // expression's type. |
| 317 | } |
| 318 | } |
| 319 | } |
| 320 | } |
| 321 | |
John McCall | 2f841ba | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 322 | /// ActOnDependentIdExpression - Handle a dependent id-expression that |
| 323 | /// was just parsed. This is only possible with an explicit scope |
| 324 | /// specifier naming a dependent type. |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 325 | Sema::OwningExprResult |
| 326 | Sema::ActOnDependentIdExpression(const CXXScopeSpec &SS, |
| 327 | DeclarationName Name, |
| 328 | SourceLocation NameLoc, |
John McCall | 2f841ba | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 329 | bool isAddressOfOperand, |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 330 | const TemplateArgumentListInfo *TemplateArgs) { |
| 331 | NestedNameSpecifier *Qualifier |
| 332 | = static_cast<NestedNameSpecifier*>(SS.getScopeRep()); |
| 333 | |
John McCall | 2f841ba | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 334 | if (!isAddressOfOperand && |
| 335 | isa<CXXMethodDecl>(CurContext) && |
| 336 | cast<CXXMethodDecl>(CurContext)->isInstance()) { |
| 337 | QualType ThisType = cast<CXXMethodDecl>(CurContext)->getThisType(Context); |
| 338 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 339 | // Since the 'this' expression is synthesized, we don't need to |
| 340 | // perform the double-lookup check. |
| 341 | NamedDecl *FirstQualifierInScope = 0; |
| 342 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 343 | return Owned(CXXDependentScopeMemberExpr::Create(Context, |
| 344 | /*This*/ 0, ThisType, |
| 345 | /*IsArrow*/ true, |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 346 | /*Op*/ SourceLocation(), |
| 347 | Qualifier, SS.getRange(), |
| 348 | FirstQualifierInScope, |
| 349 | Name, NameLoc, |
| 350 | TemplateArgs)); |
| 351 | } |
| 352 | |
| 353 | return BuildDependentDeclRefExpr(SS, Name, NameLoc, TemplateArgs); |
| 354 | } |
| 355 | |
| 356 | Sema::OwningExprResult |
| 357 | Sema::BuildDependentDeclRefExpr(const CXXScopeSpec &SS, |
| 358 | DeclarationName Name, |
| 359 | SourceLocation NameLoc, |
| 360 | const TemplateArgumentListInfo *TemplateArgs) { |
| 361 | return Owned(DependentScopeDeclRefExpr::Create(Context, |
| 362 | static_cast<NestedNameSpecifier*>(SS.getScopeRep()), |
| 363 | SS.getRange(), |
| 364 | Name, NameLoc, |
| 365 | TemplateArgs)); |
Douglas Gregor | d6fb7ef | 2008-12-18 19:37:40 +0000 | [diff] [blame] | 366 | } |
| 367 | |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 368 | /// DiagnoseTemplateParameterShadow - Produce a diagnostic complaining |
| 369 | /// that the template parameter 'PrevDecl' is being shadowed by a new |
| 370 | /// declaration at location Loc. Returns true to indicate that this is |
| 371 | /// an error, and false otherwise. |
| 372 | bool Sema::DiagnoseTemplateParameterShadow(SourceLocation Loc, Decl *PrevDecl) { |
Douglas Gregor | f57172b | 2008-12-08 18:40:42 +0000 | [diff] [blame] | 373 | assert(PrevDecl->isTemplateParameter() && "Not a template parameter"); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 374 | |
| 375 | // Microsoft Visual C++ permits template parameters to be shadowed. |
| 376 | if (getLangOptions().Microsoft) |
| 377 | return false; |
| 378 | |
| 379 | // C++ [temp.local]p4: |
| 380 | // A template-parameter shall not be redeclared within its |
| 381 | // scope (including nested scopes). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 382 | Diag(Loc, diag::err_template_param_shadow) |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 383 | << cast<NamedDecl>(PrevDecl)->getDeclName(); |
| 384 | Diag(PrevDecl->getLocation(), diag::note_template_param_here); |
| 385 | return true; |
| 386 | } |
| 387 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 388 | /// AdjustDeclIfTemplate - If the given decl happens to be a template, reset |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 389 | /// the parameter D to reference the templated declaration and return a pointer |
| 390 | /// to the template declaration. Otherwise, do nothing to D and return null. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 391 | TemplateDecl *Sema::AdjustDeclIfTemplate(DeclPtrTy &D) { |
Douglas Gregor | 13d2d6c | 2009-10-06 21:27:51 +0000 | [diff] [blame] | 392 | if (TemplateDecl *Temp = dyn_cast_or_null<TemplateDecl>(D.getAs<Decl>())) { |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 393 | D = DeclPtrTy::make(Temp->getTemplatedDecl()); |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 394 | return Temp; |
| 395 | } |
| 396 | return 0; |
| 397 | } |
| 398 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 399 | static TemplateArgumentLoc translateTemplateArgument(Sema &SemaRef, |
| 400 | const ParsedTemplateArgument &Arg) { |
| 401 | |
| 402 | switch (Arg.getKind()) { |
| 403 | case ParsedTemplateArgument::Type: { |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 404 | TypeSourceInfo *DI; |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 405 | QualType T = SemaRef.GetTypeFromParser(Arg.getAsType(), &DI); |
| 406 | if (!DI) |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 407 | DI = SemaRef.Context.getTrivialTypeSourceInfo(T, Arg.getLocation()); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 408 | return TemplateArgumentLoc(TemplateArgument(T), DI); |
| 409 | } |
| 410 | |
| 411 | case ParsedTemplateArgument::NonType: { |
| 412 | Expr *E = static_cast<Expr *>(Arg.getAsExpr()); |
| 413 | return TemplateArgumentLoc(TemplateArgument(E), E); |
| 414 | } |
| 415 | |
| 416 | case ParsedTemplateArgument::Template: { |
| 417 | TemplateName Template |
| 418 | = TemplateName::getFromVoidPointer(Arg.getAsTemplate().get()); |
| 419 | return TemplateArgumentLoc(TemplateArgument(Template), |
| 420 | Arg.getScopeSpec().getRange(), |
| 421 | Arg.getLocation()); |
| 422 | } |
| 423 | } |
| 424 | |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 425 | llvm_unreachable("Unhandled parsed template argument"); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 426 | return TemplateArgumentLoc(); |
| 427 | } |
| 428 | |
| 429 | /// \brief Translates template arguments as provided by the parser |
| 430 | /// into template arguments used by semantic analysis. |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 431 | void Sema::translateTemplateArguments(const ASTTemplateArgsPtr &TemplateArgsIn, |
| 432 | TemplateArgumentListInfo &TemplateArgs) { |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 433 | for (unsigned I = 0, Last = TemplateArgsIn.size(); I != Last; ++I) |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 434 | TemplateArgs.addArgument(translateTemplateArgument(*this, |
| 435 | TemplateArgsIn[I])); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 436 | } |
| 437 | |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 438 | /// ActOnTypeParameter - Called when a C++ template type parameter |
| 439 | /// (e.g., "typename T") has been parsed. Typename specifies whether |
| 440 | /// the keyword "typename" was used to declare the type parameter |
| 441 | /// (otherwise, "class" was used), and KeyLoc is the location of the |
| 442 | /// "class" or "typename" keyword. ParamName is the name of the |
| 443 | /// parameter (NULL indicates an unnamed template parameter) and |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 444 | /// ParamName is the location of the parameter name (if any). |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 445 | /// If the type parameter has a default argument, it will be added |
| 446 | /// later via ActOnTypeParameterDefault. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 447 | Sema::DeclPtrTy Sema::ActOnTypeParameter(Scope *S, bool Typename, bool Ellipsis, |
Anders Carlsson | 941df7d | 2009-06-12 19:58:00 +0000 | [diff] [blame] | 448 | SourceLocation EllipsisLoc, |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 449 | SourceLocation KeyLoc, |
| 450 | IdentifierInfo *ParamName, |
| 451 | SourceLocation ParamNameLoc, |
| 452 | unsigned Depth, unsigned Position) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 453 | assert(S->isTemplateParamScope() && |
| 454 | "Template type parameter not in template parameter scope!"); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 455 | bool Invalid = false; |
| 456 | |
| 457 | if (ParamName) { |
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 458 | NamedDecl *PrevDecl = LookupSingleName(S, ParamName, ParamNameLoc, |
Douglas Gregor | c0b3964 | 2010-04-15 23:40:53 +0000 | [diff] [blame] | 459 | LookupOrdinaryName, |
| 460 | ForRedeclaration); |
Douglas Gregor | f57172b | 2008-12-08 18:40:42 +0000 | [diff] [blame] | 461 | if (PrevDecl && PrevDecl->isTemplateParameter()) |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 462 | Invalid = Invalid || DiagnoseTemplateParameterShadow(ParamNameLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 463 | PrevDecl); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 464 | } |
| 465 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 466 | SourceLocation Loc = ParamNameLoc; |
| 467 | if (!ParamName) |
| 468 | Loc = KeyLoc; |
| 469 | |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 470 | TemplateTypeParmDecl *Param |
John McCall | 7a9813c | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 471 | = TemplateTypeParmDecl::Create(Context, Context.getTranslationUnitDecl(), |
| 472 | Loc, Depth, Position, ParamName, Typename, |
Anders Carlsson | 6d845ae | 2009-06-12 22:23:22 +0000 | [diff] [blame] | 473 | Ellipsis); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 474 | if (Invalid) |
| 475 | Param->setInvalidDecl(); |
| 476 | |
| 477 | if (ParamName) { |
| 478 | // Add the template parameter into the current scope. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 479 | S->AddDecl(DeclPtrTy::make(Param)); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 480 | IdResolver.AddDecl(Param); |
| 481 | } |
| 482 | |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 483 | return DeclPtrTy::make(Param); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 484 | } |
| 485 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 486 | /// ActOnTypeParameterDefault - Adds a default argument (the type |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 487 | /// Default) to the given template type parameter (TypeParam). |
| 488 | void Sema::ActOnTypeParameterDefault(DeclPtrTy TypeParam, |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 489 | SourceLocation EqualLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 490 | SourceLocation DefaultLoc, |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 491 | TypeTy *DefaultT) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 492 | TemplateTypeParmDecl *Parm |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 493 | = cast<TemplateTypeParmDecl>(TypeParam.getAs<Decl>()); |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 494 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 495 | TypeSourceInfo *DefaultTInfo; |
| 496 | GetTypeFromParser(DefaultT, &DefaultTInfo); |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 497 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 498 | assert(DefaultTInfo && "expected source information for type"); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 499 | |
Anders Carlsson | 9c4c5c8 | 2009-06-12 22:30:13 +0000 | [diff] [blame] | 500 | // C++0x [temp.param]p9: |
| 501 | // A default template-argument may be specified for any kind of |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 502 | // template-parameter that is not a template parameter pack. |
Anders Carlsson | 9c4c5c8 | 2009-06-12 22:30:13 +0000 | [diff] [blame] | 503 | if (Parm->isParameterPack()) { |
| 504 | Diag(DefaultLoc, diag::err_template_param_pack_default_arg); |
Anders Carlsson | 9c4c5c8 | 2009-06-12 22:30:13 +0000 | [diff] [blame] | 505 | return; |
| 506 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 507 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 508 | // C++ [temp.param]p14: |
| 509 | // A template-parameter shall not be used in its own default argument. |
| 510 | // FIXME: Implement this check! Needs a recursive walk over the types. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 511 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 512 | // Check the template argument itself. |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 513 | if (CheckTemplateArgument(Parm, DefaultTInfo)) { |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 514 | Parm->setInvalidDecl(); |
| 515 | return; |
| 516 | } |
| 517 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 518 | Parm->setDefaultArgument(DefaultTInfo, false); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 519 | } |
| 520 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 521 | /// \brief Check that the type of a non-type template parameter is |
| 522 | /// well-formed. |
| 523 | /// |
| 524 | /// \returns the (possibly-promoted) parameter type if valid; |
| 525 | /// otherwise, produces a diagnostic and returns a NULL type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 526 | QualType |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 527 | Sema::CheckNonTypeTemplateParameterType(QualType T, SourceLocation Loc) { |
| 528 | // C++ [temp.param]p4: |
| 529 | // |
| 530 | // A non-type template-parameter shall have one of the following |
| 531 | // (optionally cv-qualified) types: |
| 532 | // |
| 533 | // -- integral or enumeration type, |
| 534 | if (T->isIntegralType() || T->isEnumeralType() || |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 535 | // -- pointer to object or pointer to function, |
| 536 | (T->isPointerType() && |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 537 | (T->getAs<PointerType>()->getPointeeType()->isObjectType() || |
| 538 | T->getAs<PointerType>()->getPointeeType()->isFunctionType())) || |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 539 | // -- reference to object or reference to function, |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 540 | T->isReferenceType() || |
| 541 | // -- pointer to member. |
| 542 | T->isMemberPointerType() || |
| 543 | // If T is a dependent type, we can't do the check now, so we |
| 544 | // assume that it is well-formed. |
| 545 | T->isDependentType()) |
| 546 | return T; |
| 547 | // C++ [temp.param]p8: |
| 548 | // |
| 549 | // A non-type template-parameter of type "array of T" or |
| 550 | // "function returning T" is adjusted to be of type "pointer to |
| 551 | // T" or "pointer to function returning T", respectively. |
| 552 | else if (T->isArrayType()) |
| 553 | // FIXME: Keep the type prior to promotion? |
| 554 | return Context.getArrayDecayedType(T); |
| 555 | else if (T->isFunctionType()) |
| 556 | // FIXME: Keep the type prior to promotion? |
| 557 | return Context.getPointerType(T); |
| 558 | |
| 559 | Diag(Loc, diag::err_template_nontype_parm_bad_type) |
| 560 | << T; |
| 561 | |
| 562 | return QualType(); |
| 563 | } |
| 564 | |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 565 | /// ActOnNonTypeTemplateParameter - Called when a C++ non-type |
| 566 | /// template parameter (e.g., "int Size" in "template<int Size> |
| 567 | /// class Array") has been parsed. S is the current scope and D is |
| 568 | /// the parsed declarator. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 569 | Sema::DeclPtrTy Sema::ActOnNonTypeTemplateParameter(Scope *S, Declarator &D, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 570 | unsigned Depth, |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 571 | unsigned Position) { |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 572 | TypeSourceInfo *TInfo = 0; |
| 573 | QualType T = GetTypeForDeclarator(D, S, &TInfo); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 574 | |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 575 | assert(S->isTemplateParamScope() && |
| 576 | "Non-type template parameter not in template parameter scope!"); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 577 | bool Invalid = false; |
| 578 | |
| 579 | IdentifierInfo *ParamName = D.getIdentifier(); |
| 580 | if (ParamName) { |
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 581 | NamedDecl *PrevDecl = LookupSingleName(S, ParamName, D.getIdentifierLoc(), |
Douglas Gregor | c0b3964 | 2010-04-15 23:40:53 +0000 | [diff] [blame] | 582 | LookupOrdinaryName, |
| 583 | ForRedeclaration); |
Douglas Gregor | f57172b | 2008-12-08 18:40:42 +0000 | [diff] [blame] | 584 | if (PrevDecl && PrevDecl->isTemplateParameter()) |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 585 | Invalid = Invalid || DiagnoseTemplateParameterShadow(D.getIdentifierLoc(), |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 586 | PrevDecl); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 587 | } |
| 588 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 589 | T = CheckNonTypeTemplateParameterType(T, D.getIdentifierLoc()); |
Douglas Gregor | ceef30c | 2009-03-09 16:46:39 +0000 | [diff] [blame] | 590 | if (T.isNull()) { |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 591 | T = Context.IntTy; // Recover with an 'int' type. |
Douglas Gregor | ceef30c | 2009-03-09 16:46:39 +0000 | [diff] [blame] | 592 | Invalid = true; |
| 593 | } |
Douglas Gregor | 5d290d5 | 2009-02-10 17:43:50 +0000 | [diff] [blame] | 594 | |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 595 | NonTypeTemplateParmDecl *Param |
John McCall | 7a9813c | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 596 | = NonTypeTemplateParmDecl::Create(Context, Context.getTranslationUnitDecl(), |
| 597 | D.getIdentifierLoc(), |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 598 | Depth, Position, ParamName, T, TInfo); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 599 | if (Invalid) |
| 600 | Param->setInvalidDecl(); |
| 601 | |
| 602 | if (D.getIdentifier()) { |
| 603 | // Add the template parameter into the current scope. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 604 | S->AddDecl(DeclPtrTy::make(Param)); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 605 | IdResolver.AddDecl(Param); |
| 606 | } |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 607 | return DeclPtrTy::make(Param); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 608 | } |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 609 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 610 | /// \brief Adds a default argument to the given non-type template |
| 611 | /// parameter. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 612 | void Sema::ActOnNonTypeTemplateParameterDefault(DeclPtrTy TemplateParamD, |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 613 | SourceLocation EqualLoc, |
| 614 | ExprArg DefaultE) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 615 | NonTypeTemplateParmDecl *TemplateParm |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 616 | = cast<NonTypeTemplateParmDecl>(TemplateParamD.getAs<Decl>()); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 617 | Expr *Default = static_cast<Expr *>(DefaultE.get()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 618 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 619 | // C++ [temp.param]p14: |
| 620 | // A template-parameter shall not be used in its own default argument. |
| 621 | // FIXME: Implement this check! Needs a recursive walk over the types. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 622 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 623 | // Check the well-formedness of the default template argument. |
Douglas Gregor | 02cbbd2 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 624 | TemplateArgument Converted; |
| 625 | if (CheckTemplateArgument(TemplateParm, TemplateParm->getType(), Default, |
| 626 | Converted)) { |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 627 | TemplateParm->setInvalidDecl(); |
| 628 | return; |
| 629 | } |
| 630 | |
Anders Carlsson | e9146f2 | 2009-05-01 19:49:17 +0000 | [diff] [blame] | 631 | TemplateParm->setDefaultArgument(DefaultE.takeAs<Expr>()); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 632 | } |
| 633 | |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 634 | |
| 635 | /// ActOnTemplateTemplateParameter - Called when a C++ template template |
| 636 | /// parameter (e.g. T in template <template <typename> class T> class array) |
| 637 | /// has been parsed. S is the current scope. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 638 | Sema::DeclPtrTy Sema::ActOnTemplateTemplateParameter(Scope* S, |
| 639 | SourceLocation TmpLoc, |
| 640 | TemplateParamsTy *Params, |
| 641 | IdentifierInfo *Name, |
| 642 | SourceLocation NameLoc, |
| 643 | unsigned Depth, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 644 | unsigned Position) { |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 645 | assert(S->isTemplateParamScope() && |
| 646 | "Template template parameter not in template parameter scope!"); |
| 647 | |
| 648 | // Construct the parameter object. |
| 649 | TemplateTemplateParmDecl *Param = |
John McCall | 7a9813c | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 650 | TemplateTemplateParmDecl::Create(Context, Context.getTranslationUnitDecl(), |
| 651 | TmpLoc, Depth, Position, Name, |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 652 | (TemplateParameterList*)Params); |
| 653 | |
| 654 | // Make sure the parameter is valid. |
| 655 | // FIXME: Decl object is not currently invalidated anywhere so this doesn't |
| 656 | // do anything yet. However, if the template parameter list or (eventual) |
| 657 | // default value is ever invalidated, that will propagate here. |
| 658 | bool Invalid = false; |
| 659 | if (Invalid) { |
| 660 | Param->setInvalidDecl(); |
| 661 | } |
| 662 | |
| 663 | // If the tt-param has a name, then link the identifier into the scope |
| 664 | // and lookup mechanisms. |
| 665 | if (Name) { |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 666 | S->AddDecl(DeclPtrTy::make(Param)); |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 667 | IdResolver.AddDecl(Param); |
| 668 | } |
| 669 | |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 670 | return DeclPtrTy::make(Param); |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 671 | } |
| 672 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 673 | /// \brief Adds a default argument to the given template template |
| 674 | /// parameter. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 675 | void Sema::ActOnTemplateTemplateParameterDefault(DeclPtrTy TemplateParamD, |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 676 | SourceLocation EqualLoc, |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 677 | const ParsedTemplateArgument &Default) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 678 | TemplateTemplateParmDecl *TemplateParm |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 679 | = cast<TemplateTemplateParmDecl>(TemplateParamD.getAs<Decl>()); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 680 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 681 | // C++ [temp.param]p14: |
| 682 | // A template-parameter shall not be used in its own default argument. |
| 683 | // FIXME: Implement this check! Needs a recursive walk over the types. |
| 684 | |
Douglas Gregor | 9148c3f | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 685 | // Check only that we have a template template argument. We don't want to |
| 686 | // try to check well-formedness now, because our template template parameter |
| 687 | // might have dependent types in its template parameters, which we wouldn't |
| 688 | // be able to match now. |
| 689 | // |
| 690 | // If none of the template template parameter's template arguments mention |
| 691 | // other template parameters, we could actually perform more checking here. |
| 692 | // However, it isn't worth doing. |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 693 | TemplateArgumentLoc DefaultArg = translateTemplateArgument(*this, Default); |
Douglas Gregor | 9148c3f | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 694 | if (DefaultArg.getArgument().getAsTemplate().isNull()) { |
| 695 | Diag(DefaultArg.getLocation(), diag::err_template_arg_not_class_template) |
| 696 | << DefaultArg.getSourceRange(); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 697 | return; |
| 698 | } |
Douglas Gregor | 9148c3f | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 699 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 700 | TemplateParm->setDefaultArgument(DefaultArg); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 701 | } |
| 702 | |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 703 | /// ActOnTemplateParameterList - Builds a TemplateParameterList that |
| 704 | /// contains the template parameters in Params/NumParams. |
| 705 | Sema::TemplateParamsTy * |
| 706 | Sema::ActOnTemplateParameterList(unsigned Depth, |
| 707 | SourceLocation ExportLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 708 | SourceLocation TemplateLoc, |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 709 | SourceLocation LAngleLoc, |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 710 | DeclPtrTy *Params, unsigned NumParams, |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 711 | SourceLocation RAngleLoc) { |
| 712 | if (ExportLoc.isValid()) |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 713 | Diag(ExportLoc, diag::warn_template_export_unsupported); |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 714 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 715 | return TemplateParameterList::Create(Context, TemplateLoc, LAngleLoc, |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 716 | (NamedDecl**)Params, NumParams, |
| 717 | RAngleLoc); |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 718 | } |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 719 | |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 720 | static void SetNestedNameSpecifier(TagDecl *T, const CXXScopeSpec &SS) { |
| 721 | if (SS.isSet()) |
| 722 | T->setQualifierInfo(static_cast<NestedNameSpecifier*>(SS.getScopeRep()), |
| 723 | SS.getRange()); |
| 724 | } |
| 725 | |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 726 | Sema::DeclResult |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 727 | Sema::CheckClassTemplate(Scope *S, unsigned TagSpec, TagUseKind TUK, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 728 | SourceLocation KWLoc, CXXScopeSpec &SS, |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 729 | IdentifierInfo *Name, SourceLocation NameLoc, |
| 730 | AttributeList *Attr, |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 731 | TemplateParameterList *TemplateParams, |
Anders Carlsson | 5aeccdb | 2009-03-26 00:52:18 +0000 | [diff] [blame] | 732 | AccessSpecifier AS) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 733 | assert(TemplateParams && TemplateParams->size() > 0 && |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 734 | "No template parameters"); |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 735 | assert(TUK != TUK_Reference && "Can only declare or define class templates"); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 736 | bool Invalid = false; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 737 | |
| 738 | // Check that we can declare a template here. |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 739 | if (CheckTemplateDeclScope(S, TemplateParams)) |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 740 | return true; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 741 | |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 742 | TagDecl::TagKind Kind = TagDecl::getTagKindForTypeSpec(TagSpec); |
| 743 | assert(Kind != TagDecl::TK_enum && "can't build template of enumerated type"); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 744 | |
| 745 | // There is no such thing as an unnamed class template. |
| 746 | if (!Name) { |
| 747 | Diag(KWLoc, diag::err_template_unnamed_class); |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 748 | return true; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 749 | } |
| 750 | |
| 751 | // Find any previous declaration with this name. |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 752 | DeclContext *SemanticContext; |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 753 | LookupResult Previous(*this, Name, NameLoc, LookupOrdinaryName, |
John McCall | 7d384dd | 2009-11-18 07:57:50 +0000 | [diff] [blame] | 754 | ForRedeclaration); |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 755 | if (SS.isNotEmpty() && !SS.isInvalid()) { |
Douglas Gregor | f0510d4 | 2009-10-12 23:11:44 +0000 | [diff] [blame] | 756 | if (RequireCompleteDeclContext(SS)) |
| 757 | return true; |
| 758 | |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 759 | SemanticContext = computeDeclContext(SS, true); |
| 760 | if (!SemanticContext) { |
| 761 | // FIXME: Produce a reasonable diagnostic here |
| 762 | return true; |
| 763 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 764 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 765 | LookupQualifiedName(Previous, SemanticContext); |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 766 | } else { |
| 767 | SemanticContext = CurContext; |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 768 | LookupName(Previous, S); |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 769 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 770 | |
Douglas Gregor | 57265e3 | 2010-04-12 16:00:01 +0000 | [diff] [blame] | 771 | if (Previous.isAmbiguous()) |
| 772 | return true; |
| 773 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 774 | NamedDecl *PrevDecl = 0; |
| 775 | if (Previous.begin() != Previous.end()) |
Douglas Gregor | 57265e3 | 2010-04-12 16:00:01 +0000 | [diff] [blame] | 776 | PrevDecl = (*Previous.begin())->getUnderlyingDecl(); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 777 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 778 | // If there is a previous declaration with the same name, check |
| 779 | // whether this is a valid redeclaration. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 780 | ClassTemplateDecl *PrevClassTemplate |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 781 | = dyn_cast_or_null<ClassTemplateDecl>(PrevDecl); |
Douglas Gregor | d7e5bdb | 2009-10-09 21:11:42 +0000 | [diff] [blame] | 782 | |
| 783 | // We may have found the injected-class-name of a class template, |
| 784 | // class template partial specialization, or class template specialization. |
| 785 | // In these cases, grab the template that is being defined or specialized. |
| 786 | if (!PrevClassTemplate && PrevDecl && isa<CXXRecordDecl>(PrevDecl) && |
| 787 | cast<CXXRecordDecl>(PrevDecl)->isInjectedClassName()) { |
| 788 | PrevDecl = cast<CXXRecordDecl>(PrevDecl->getDeclContext()); |
| 789 | PrevClassTemplate |
| 790 | = cast<CXXRecordDecl>(PrevDecl)->getDescribedClassTemplate(); |
| 791 | if (!PrevClassTemplate && isa<ClassTemplateSpecializationDecl>(PrevDecl)) { |
| 792 | PrevClassTemplate |
| 793 | = cast<ClassTemplateSpecializationDecl>(PrevDecl) |
| 794 | ->getSpecializedTemplate(); |
| 795 | } |
| 796 | } |
| 797 | |
John McCall | 65c4946 | 2009-12-18 11:25:59 +0000 | [diff] [blame] | 798 | if (TUK == TUK_Friend) { |
John McCall | e129d44 | 2009-12-17 23:21:11 +0000 | [diff] [blame] | 799 | // C++ [namespace.memdef]p3: |
| 800 | // [...] When looking for a prior declaration of a class or a function |
| 801 | // declared as a friend, and when the name of the friend class or |
| 802 | // function is neither a qualified name nor a template-id, scopes outside |
| 803 | // the innermost enclosing namespace scope are not considered. |
Douglas Gregor | c1c9df7 | 2010-04-18 17:37:40 +0000 | [diff] [blame] | 804 | if (!SS.isSet()) { |
| 805 | DeclContext *OutermostContext = CurContext; |
| 806 | while (!OutermostContext->isFileContext()) |
| 807 | OutermostContext = OutermostContext->getLookupParent(); |
John McCall | 65c4946 | 2009-12-18 11:25:59 +0000 | [diff] [blame] | 808 | |
Douglas Gregor | c1c9df7 | 2010-04-18 17:37:40 +0000 | [diff] [blame] | 809 | if (PrevDecl && |
| 810 | (OutermostContext->Equals(PrevDecl->getDeclContext()) || |
| 811 | OutermostContext->Encloses(PrevDecl->getDeclContext()))) { |
| 812 | SemanticContext = PrevDecl->getDeclContext(); |
| 813 | } else { |
| 814 | // Declarations in outer scopes don't matter. However, the outermost |
| 815 | // context we computed is the semantic context for our new |
| 816 | // declaration. |
| 817 | PrevDecl = PrevClassTemplate = 0; |
| 818 | SemanticContext = OutermostContext; |
| 819 | } |
John McCall | e129d44 | 2009-12-17 23:21:11 +0000 | [diff] [blame] | 820 | } |
Douglas Gregor | c1c9df7 | 2010-04-18 17:37:40 +0000 | [diff] [blame] | 821 | |
John McCall | e129d44 | 2009-12-17 23:21:11 +0000 | [diff] [blame] | 822 | if (CurContext->isDependentContext()) { |
| 823 | // If this is a dependent context, we don't want to link the friend |
| 824 | // class template to the template in scope, because that would perform |
| 825 | // checking of the template parameter lists that can't be performed |
| 826 | // until the outer context is instantiated. |
| 827 | PrevDecl = PrevClassTemplate = 0; |
| 828 | } |
| 829 | } else if (PrevDecl && !isDeclInScope(PrevDecl, SemanticContext, S)) |
| 830 | PrevDecl = PrevClassTemplate = 0; |
Douglas Gregor | 57265e3 | 2010-04-12 16:00:01 +0000 | [diff] [blame] | 831 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 832 | if (PrevClassTemplate) { |
| 833 | // Ensure that the template parameter lists are compatible. |
| 834 | if (!TemplateParameterListsAreEqual(TemplateParams, |
| 835 | PrevClassTemplate->getTemplateParameters(), |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 836 | /*Complain=*/true, |
| 837 | TPL_TemplateMatch)) |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 838 | return true; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 839 | |
| 840 | // C++ [temp.class]p4: |
| 841 | // In a redeclaration, partial specialization, explicit |
| 842 | // specialization or explicit instantiation of a class template, |
| 843 | // the class-key shall agree in kind with the original class |
| 844 | // template declaration (7.1.5.3). |
| 845 | RecordDecl *PrevRecordDecl = PrevClassTemplate->getTemplatedDecl(); |
Douglas Gregor | 501c5ce | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 846 | if (!isAcceptableTagRedeclaration(PrevRecordDecl, Kind, KWLoc, *Name)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 847 | Diag(KWLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 848 | << Name |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 849 | << FixItHint::CreateReplacement(KWLoc, PrevRecordDecl->getKindName()); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 850 | Diag(PrevRecordDecl->getLocation(), diag::note_previous_use); |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 851 | Kind = PrevRecordDecl->getTagKind(); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 852 | } |
| 853 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 854 | // Check for redefinition of this class template. |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 855 | if (TUK == TUK_Definition) { |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 856 | if (TagDecl *Def = PrevRecordDecl->getDefinition()) { |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 857 | Diag(NameLoc, diag::err_redefinition) << Name; |
| 858 | Diag(Def->getLocation(), diag::note_previous_definition); |
| 859 | // FIXME: Would it make sense to try to "forget" the previous |
| 860 | // definition, as part of error recovery? |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 861 | return true; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 862 | } |
| 863 | } |
| 864 | } else if (PrevDecl && PrevDecl->isTemplateParameter()) { |
| 865 | // Maybe we will complain about the shadowed template parameter. |
| 866 | DiagnoseTemplateParameterShadow(NameLoc, PrevDecl); |
| 867 | // Just pretend that we didn't see the previous declaration. |
| 868 | PrevDecl = 0; |
| 869 | } else if (PrevDecl) { |
| 870 | // C++ [temp]p5: |
| 871 | // A class template shall not have the same name as any other |
| 872 | // template, class, function, object, enumeration, enumerator, |
| 873 | // namespace, or type in the same scope (3.3), except as specified |
| 874 | // in (14.5.4). |
| 875 | Diag(NameLoc, diag::err_redefinition_different_kind) << Name; |
| 876 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 877 | return true; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 878 | } |
| 879 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 880 | // Check the template parameter list of this declaration, possibly |
| 881 | // merging in the template parameter list from the previous class |
| 882 | // template declaration. |
| 883 | if (CheckTemplateParameterList(TemplateParams, |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 884 | PrevClassTemplate? PrevClassTemplate->getTemplateParameters() : 0, |
| 885 | TPC_ClassTemplate)) |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 886 | Invalid = true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 887 | |
Douglas Gregor | 57265e3 | 2010-04-12 16:00:01 +0000 | [diff] [blame] | 888 | if (SS.isSet()) { |
| 889 | // If the name of the template was qualified, we must be defining the |
| 890 | // template out-of-line. |
| 891 | if (!SS.isInvalid() && !Invalid && !PrevClassTemplate && |
| 892 | !(TUK == TUK_Friend && CurContext->isDependentContext())) |
| 893 | Diag(NameLoc, diag::err_member_def_does_not_match) |
| 894 | << Name << SemanticContext << SS.getRange(); |
| 895 | } |
| 896 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 897 | CXXRecordDecl *NewClass = |
Douglas Gregor | 741dd9a | 2009-07-21 14:46:17 +0000 | [diff] [blame] | 898 | CXXRecordDecl::Create(Context, Kind, SemanticContext, NameLoc, Name, KWLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 899 | PrevClassTemplate? |
Douglas Gregor | aafc0cc | 2009-05-15 19:11:46 +0000 | [diff] [blame] | 900 | PrevClassTemplate->getTemplatedDecl() : 0, |
| 901 | /*DelayTypeCreation=*/true); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 902 | SetNestedNameSpecifier(NewClass, SS); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 903 | |
| 904 | ClassTemplateDecl *NewTemplate |
| 905 | = ClassTemplateDecl::Create(Context, SemanticContext, NameLoc, |
| 906 | DeclarationName(Name), TemplateParams, |
Douglas Gregor | 5953d8b | 2009-03-19 17:26:29 +0000 | [diff] [blame] | 907 | NewClass, PrevClassTemplate); |
Douglas Gregor | befc20e | 2009-03-26 00:10:35 +0000 | [diff] [blame] | 908 | NewClass->setDescribedClassTemplate(NewTemplate); |
| 909 | |
Douglas Gregor | aafc0cc | 2009-05-15 19:11:46 +0000 | [diff] [blame] | 910 | // Build the type for the class template declaration now. |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 911 | QualType T = NewTemplate->getInjectedClassNameSpecialization(Context); |
| 912 | T = Context.getInjectedClassNameType(NewClass, T); |
Douglas Gregor | aafc0cc | 2009-05-15 19:11:46 +0000 | [diff] [blame] | 913 | assert(T->isDependentType() && "Class template type is not dependent?"); |
| 914 | (void)T; |
| 915 | |
Douglas Gregor | fd056bc | 2009-10-13 16:30:37 +0000 | [diff] [blame] | 916 | // If we are providing an explicit specialization of a member that is a |
| 917 | // class template, make a note of that. |
| 918 | if (PrevClassTemplate && |
| 919 | PrevClassTemplate->getInstantiatedFromMemberTemplate()) |
| 920 | PrevClassTemplate->setMemberSpecialization(); |
| 921 | |
Anders Carlsson | 4cbe82c | 2009-03-26 01:24:28 +0000 | [diff] [blame] | 922 | // Set the access specifier. |
Douglas Gregor | d85bea2 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 923 | if (!Invalid && TUK != TUK_Friend) |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 924 | SetMemberAccessSpecifier(NewTemplate, PrevClassTemplate, AS); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 925 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 926 | // Set the lexical context of these templates |
| 927 | NewClass->setLexicalDeclContext(CurContext); |
| 928 | NewTemplate->setLexicalDeclContext(CurContext); |
| 929 | |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 930 | if (TUK == TUK_Definition) |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 931 | NewClass->startDefinition(); |
| 932 | |
| 933 | if (Attr) |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 934 | ProcessDeclAttributeList(S, NewClass, Attr); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 935 | |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 936 | if (TUK != TUK_Friend) |
| 937 | PushOnScopeChains(NewTemplate, S); |
| 938 | else { |
Douglas Gregor | d85bea2 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 939 | if (PrevClassTemplate && PrevClassTemplate->getAccess() != AS_none) { |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 940 | NewTemplate->setAccess(PrevClassTemplate->getAccess()); |
Douglas Gregor | d85bea2 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 941 | NewClass->setAccess(PrevClassTemplate->getAccess()); |
| 942 | } |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 943 | |
Douglas Gregor | d85bea2 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 944 | NewTemplate->setObjectOfFriendDecl(/* PreviouslyDeclared = */ |
| 945 | PrevClassTemplate != NULL); |
| 946 | |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 947 | // Friend templates are visible in fairly strange ways. |
| 948 | if (!CurContext->isDependentContext()) { |
| 949 | DeclContext *DC = SemanticContext->getLookupContext(); |
| 950 | DC->makeDeclVisibleInContext(NewTemplate, /* Recoverable = */ false); |
| 951 | if (Scope *EnclosingScope = getScopeForDeclContext(S, DC)) |
| 952 | PushOnScopeChains(NewTemplate, EnclosingScope, |
| 953 | /* AddToContext = */ false); |
| 954 | } |
Douglas Gregor | d85bea2 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 955 | |
| 956 | FriendDecl *Friend = FriendDecl::Create(Context, CurContext, |
| 957 | NewClass->getLocation(), |
| 958 | NewTemplate, |
| 959 | /*FIXME:*/NewClass->getLocation()); |
| 960 | Friend->setAccess(AS_public); |
| 961 | CurContext->addDecl(Friend); |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 962 | } |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 963 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 964 | if (Invalid) { |
| 965 | NewTemplate->setInvalidDecl(); |
| 966 | NewClass->setInvalidDecl(); |
| 967 | } |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 968 | return DeclPtrTy::make(NewTemplate); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 969 | } |
| 970 | |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 971 | /// \brief Diagnose the presence of a default template argument on a |
| 972 | /// template parameter, which is ill-formed in certain contexts. |
| 973 | /// |
| 974 | /// \returns true if the default template argument should be dropped. |
| 975 | static bool DiagnoseDefaultTemplateArgument(Sema &S, |
| 976 | Sema::TemplateParamListContext TPC, |
| 977 | SourceLocation ParamLoc, |
| 978 | SourceRange DefArgRange) { |
| 979 | switch (TPC) { |
| 980 | case Sema::TPC_ClassTemplate: |
| 981 | return false; |
| 982 | |
| 983 | case Sema::TPC_FunctionTemplate: |
| 984 | // C++ [temp.param]p9: |
| 985 | // A default template-argument shall not be specified in a |
| 986 | // function template declaration or a function template |
| 987 | // definition [...] |
| 988 | // (This sentence is not in C++0x, per DR226). |
| 989 | if (!S.getLangOptions().CPlusPlus0x) |
| 990 | S.Diag(ParamLoc, |
| 991 | diag::err_template_parameter_default_in_function_template) |
| 992 | << DefArgRange; |
| 993 | return false; |
| 994 | |
| 995 | case Sema::TPC_ClassTemplateMember: |
| 996 | // C++0x [temp.param]p9: |
| 997 | // A default template-argument shall not be specified in the |
| 998 | // template-parameter-lists of the definition of a member of a |
| 999 | // class template that appears outside of the member's class. |
| 1000 | S.Diag(ParamLoc, diag::err_template_parameter_default_template_member) |
| 1001 | << DefArgRange; |
| 1002 | return true; |
| 1003 | |
| 1004 | case Sema::TPC_FriendFunctionTemplate: |
| 1005 | // C++ [temp.param]p9: |
| 1006 | // A default template-argument shall not be specified in a |
| 1007 | // friend template declaration. |
| 1008 | S.Diag(ParamLoc, diag::err_template_parameter_default_friend_template) |
| 1009 | << DefArgRange; |
| 1010 | return true; |
| 1011 | |
| 1012 | // FIXME: C++0x [temp.param]p9 allows default template-arguments |
| 1013 | // for friend function templates if there is only a single |
| 1014 | // declaration (and it is a definition). Strange! |
| 1015 | } |
| 1016 | |
| 1017 | return false; |
| 1018 | } |
| 1019 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1020 | /// \brief Checks the validity of a template parameter list, possibly |
| 1021 | /// considering the template parameter list from a previous |
| 1022 | /// declaration. |
| 1023 | /// |
| 1024 | /// If an "old" template parameter list is provided, it must be |
| 1025 | /// equivalent (per TemplateParameterListsAreEqual) to the "new" |
| 1026 | /// template parameter list. |
| 1027 | /// |
| 1028 | /// \param NewParams Template parameter list for a new template |
| 1029 | /// declaration. This template parameter list will be updated with any |
| 1030 | /// default arguments that are carried through from the previous |
| 1031 | /// template parameter list. |
| 1032 | /// |
| 1033 | /// \param OldParams If provided, template parameter list from a |
| 1034 | /// previous declaration of the same template. Default template |
| 1035 | /// arguments will be merged from the old template parameter list to |
| 1036 | /// the new template parameter list. |
| 1037 | /// |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1038 | /// \param TPC Describes the context in which we are checking the given |
| 1039 | /// template parameter list. |
| 1040 | /// |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1041 | /// \returns true if an error occurred, false otherwise. |
| 1042 | bool Sema::CheckTemplateParameterList(TemplateParameterList *NewParams, |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1043 | TemplateParameterList *OldParams, |
| 1044 | TemplateParamListContext TPC) { |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1045 | bool Invalid = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1046 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1047 | // C++ [temp.param]p10: |
| 1048 | // The set of default template-arguments available for use with a |
| 1049 | // template declaration or definition is obtained by merging the |
| 1050 | // default arguments from the definition (if in scope) and all |
| 1051 | // declarations in scope in the same way default function |
| 1052 | // arguments are (8.3.6). |
| 1053 | bool SawDefaultArgument = false; |
| 1054 | SourceLocation PreviousDefaultArgLoc; |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 1055 | |
Anders Carlsson | 49d2557 | 2009-06-12 23:20:15 +0000 | [diff] [blame] | 1056 | bool SawParameterPack = false; |
| 1057 | SourceLocation ParameterPackLoc; |
| 1058 | |
Mike Stump | 1a35fde | 2009-02-11 23:03:27 +0000 | [diff] [blame] | 1059 | // Dummy initialization to avoid warnings. |
Douglas Gregor | 1bc6913 | 2009-02-11 20:46:19 +0000 | [diff] [blame] | 1060 | TemplateParameterList::iterator OldParam = NewParams->end(); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1061 | if (OldParams) |
| 1062 | OldParam = OldParams->begin(); |
| 1063 | |
| 1064 | for (TemplateParameterList::iterator NewParam = NewParams->begin(), |
| 1065 | NewParamEnd = NewParams->end(); |
| 1066 | NewParam != NewParamEnd; ++NewParam) { |
| 1067 | // Variables used to diagnose redundant default arguments |
| 1068 | bool RedundantDefaultArg = false; |
| 1069 | SourceLocation OldDefaultLoc; |
| 1070 | SourceLocation NewDefaultLoc; |
| 1071 | |
| 1072 | // Variables used to diagnose missing default arguments |
| 1073 | bool MissingDefaultArg = false; |
| 1074 | |
Anders Carlsson | 49d2557 | 2009-06-12 23:20:15 +0000 | [diff] [blame] | 1075 | // C++0x [temp.param]p11: |
| 1076 | // If a template parameter of a class template is a template parameter pack, |
| 1077 | // it must be the last template parameter. |
| 1078 | if (SawParameterPack) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1079 | Diag(ParameterPackLoc, |
Anders Carlsson | 49d2557 | 2009-06-12 23:20:15 +0000 | [diff] [blame] | 1080 | diag::err_template_param_pack_must_be_last_template_parameter); |
| 1081 | Invalid = true; |
| 1082 | } |
| 1083 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1084 | if (TemplateTypeParmDecl *NewTypeParm |
| 1085 | = dyn_cast<TemplateTypeParmDecl>(*NewParam)) { |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1086 | // Check the presence of a default argument here. |
| 1087 | if (NewTypeParm->hasDefaultArgument() && |
| 1088 | DiagnoseDefaultTemplateArgument(*this, TPC, |
| 1089 | NewTypeParm->getLocation(), |
| 1090 | NewTypeParm->getDefaultArgumentInfo()->getTypeLoc() |
| 1091 | .getFullSourceRange())) |
| 1092 | NewTypeParm->removeDefaultArgument(); |
| 1093 | |
| 1094 | // Merge default arguments for template type parameters. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1095 | TemplateTypeParmDecl *OldTypeParm |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1096 | = OldParams? cast<TemplateTypeParmDecl>(*OldParam) : 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1097 | |
Anders Carlsson | 49d2557 | 2009-06-12 23:20:15 +0000 | [diff] [blame] | 1098 | if (NewTypeParm->isParameterPack()) { |
| 1099 | assert(!NewTypeParm->hasDefaultArgument() && |
| 1100 | "Parameter packs can't have a default argument!"); |
| 1101 | SawParameterPack = true; |
| 1102 | ParameterPackLoc = NewTypeParm->getLocation(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1103 | } else if (OldTypeParm && OldTypeParm->hasDefaultArgument() && |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1104 | NewTypeParm->hasDefaultArgument()) { |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1105 | OldDefaultLoc = OldTypeParm->getDefaultArgumentLoc(); |
| 1106 | NewDefaultLoc = NewTypeParm->getDefaultArgumentLoc(); |
| 1107 | SawDefaultArgument = true; |
| 1108 | RedundantDefaultArg = true; |
| 1109 | PreviousDefaultArgLoc = NewDefaultLoc; |
| 1110 | } else if (OldTypeParm && OldTypeParm->hasDefaultArgument()) { |
| 1111 | // Merge the default argument from the old declaration to the |
| 1112 | // new declaration. |
| 1113 | SawDefaultArgument = true; |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1114 | NewTypeParm->setDefaultArgument(OldTypeParm->getDefaultArgumentInfo(), |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1115 | true); |
| 1116 | PreviousDefaultArgLoc = OldTypeParm->getDefaultArgumentLoc(); |
| 1117 | } else if (NewTypeParm->hasDefaultArgument()) { |
| 1118 | SawDefaultArgument = true; |
| 1119 | PreviousDefaultArgLoc = NewTypeParm->getDefaultArgumentLoc(); |
| 1120 | } else if (SawDefaultArgument) |
| 1121 | MissingDefaultArg = true; |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1122 | } else if (NonTypeTemplateParmDecl *NewNonTypeParm |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1123 | = dyn_cast<NonTypeTemplateParmDecl>(*NewParam)) { |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1124 | // Check the presence of a default argument here. |
| 1125 | if (NewNonTypeParm->hasDefaultArgument() && |
| 1126 | DiagnoseDefaultTemplateArgument(*this, TPC, |
| 1127 | NewNonTypeParm->getLocation(), |
| 1128 | NewNonTypeParm->getDefaultArgument()->getSourceRange())) { |
| 1129 | NewNonTypeParm->getDefaultArgument()->Destroy(Context); |
| 1130 | NewNonTypeParm->setDefaultArgument(0); |
| 1131 | } |
| 1132 | |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1133 | // Merge default arguments for non-type template parameters |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1134 | NonTypeTemplateParmDecl *OldNonTypeParm |
| 1135 | = OldParams? cast<NonTypeTemplateParmDecl>(*OldParam) : 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1136 | if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument() && |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1137 | NewNonTypeParm->hasDefaultArgument()) { |
| 1138 | OldDefaultLoc = OldNonTypeParm->getDefaultArgumentLoc(); |
| 1139 | NewDefaultLoc = NewNonTypeParm->getDefaultArgumentLoc(); |
| 1140 | SawDefaultArgument = true; |
| 1141 | RedundantDefaultArg = true; |
| 1142 | PreviousDefaultArgLoc = NewDefaultLoc; |
| 1143 | } else if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument()) { |
| 1144 | // Merge the default argument from the old declaration to the |
| 1145 | // new declaration. |
| 1146 | SawDefaultArgument = true; |
| 1147 | // FIXME: We need to create a new kind of "default argument" |
| 1148 | // expression that points to a previous template template |
| 1149 | // parameter. |
| 1150 | NewNonTypeParm->setDefaultArgument( |
| 1151 | OldNonTypeParm->getDefaultArgument()); |
| 1152 | PreviousDefaultArgLoc = OldNonTypeParm->getDefaultArgumentLoc(); |
| 1153 | } else if (NewNonTypeParm->hasDefaultArgument()) { |
| 1154 | SawDefaultArgument = true; |
| 1155 | PreviousDefaultArgLoc = NewNonTypeParm->getDefaultArgumentLoc(); |
| 1156 | } else if (SawDefaultArgument) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1157 | MissingDefaultArg = true; |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1158 | } else { |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1159 | // Check the presence of a default argument here. |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1160 | TemplateTemplateParmDecl *NewTemplateParm |
| 1161 | = cast<TemplateTemplateParmDecl>(*NewParam); |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1162 | if (NewTemplateParm->hasDefaultArgument() && |
| 1163 | DiagnoseDefaultTemplateArgument(*this, TPC, |
| 1164 | NewTemplateParm->getLocation(), |
| 1165 | NewTemplateParm->getDefaultArgument().getSourceRange())) |
| 1166 | NewTemplateParm->setDefaultArgument(TemplateArgumentLoc()); |
| 1167 | |
| 1168 | // Merge default arguments for template template parameters |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1169 | TemplateTemplateParmDecl *OldTemplateParm |
| 1170 | = OldParams? cast<TemplateTemplateParmDecl>(*OldParam) : 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1171 | if (OldTemplateParm && OldTemplateParm->hasDefaultArgument() && |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1172 | NewTemplateParm->hasDefaultArgument()) { |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1173 | OldDefaultLoc = OldTemplateParm->getDefaultArgument().getLocation(); |
| 1174 | NewDefaultLoc = NewTemplateParm->getDefaultArgument().getLocation(); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1175 | SawDefaultArgument = true; |
| 1176 | RedundantDefaultArg = true; |
| 1177 | PreviousDefaultArgLoc = NewDefaultLoc; |
| 1178 | } else if (OldTemplateParm && OldTemplateParm->hasDefaultArgument()) { |
| 1179 | // Merge the default argument from the old declaration to the |
| 1180 | // new declaration. |
| 1181 | SawDefaultArgument = true; |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 1182 | // FIXME: We need to create a new kind of "default argument" expression |
| 1183 | // that points to a previous template template parameter. |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1184 | NewTemplateParm->setDefaultArgument( |
| 1185 | OldTemplateParm->getDefaultArgument()); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1186 | PreviousDefaultArgLoc |
| 1187 | = OldTemplateParm->getDefaultArgument().getLocation(); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1188 | } else if (NewTemplateParm->hasDefaultArgument()) { |
| 1189 | SawDefaultArgument = true; |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1190 | PreviousDefaultArgLoc |
| 1191 | = NewTemplateParm->getDefaultArgument().getLocation(); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1192 | } else if (SawDefaultArgument) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1193 | MissingDefaultArg = true; |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1194 | } |
| 1195 | |
| 1196 | if (RedundantDefaultArg) { |
| 1197 | // C++ [temp.param]p12: |
| 1198 | // A template-parameter shall not be given default arguments |
| 1199 | // by two different declarations in the same scope. |
| 1200 | Diag(NewDefaultLoc, diag::err_template_param_default_arg_redefinition); |
| 1201 | Diag(OldDefaultLoc, diag::note_template_param_prev_default_arg); |
| 1202 | Invalid = true; |
| 1203 | } else if (MissingDefaultArg) { |
| 1204 | // C++ [temp.param]p11: |
| 1205 | // If a template-parameter has a default template-argument, |
| 1206 | // all subsequent template-parameters shall have a default |
| 1207 | // template-argument supplied. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1208 | Diag((*NewParam)->getLocation(), |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1209 | diag::err_template_param_default_arg_missing); |
| 1210 | Diag(PreviousDefaultArgLoc, diag::note_template_param_prev_default_arg); |
| 1211 | Invalid = true; |
| 1212 | } |
| 1213 | |
| 1214 | // If we have an old template parameter list that we're merging |
| 1215 | // in, move on to the next parameter. |
| 1216 | if (OldParams) |
| 1217 | ++OldParam; |
| 1218 | } |
| 1219 | |
| 1220 | return Invalid; |
| 1221 | } |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 1222 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1223 | /// \brief Match the given template parameter lists to the given scope |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1224 | /// specifier, returning the template parameter list that applies to the |
| 1225 | /// name. |
| 1226 | /// |
| 1227 | /// \param DeclStartLoc the start of the declaration that has a scope |
| 1228 | /// specifier or a template parameter list. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1229 | /// |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1230 | /// \param SS the scope specifier that will be matched to the given template |
| 1231 | /// parameter lists. This scope specifier precedes a qualified name that is |
| 1232 | /// being declared. |
| 1233 | /// |
| 1234 | /// \param ParamLists the template parameter lists, from the outermost to the |
| 1235 | /// innermost template parameter lists. |
| 1236 | /// |
| 1237 | /// \param NumParamLists the number of template parameter lists in ParamLists. |
| 1238 | /// |
John McCall | 77e8b11 | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 1239 | /// \param IsFriend Whether to apply the slightly different rules for |
| 1240 | /// matching template parameters to scope specifiers in friend |
| 1241 | /// declarations. |
| 1242 | /// |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 1243 | /// \param IsExplicitSpecialization will be set true if the entity being |
| 1244 | /// declared is an explicit specialization, false otherwise. |
| 1245 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1246 | /// \returns the template parameter list, if any, that corresponds to the |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1247 | /// name that is preceded by the scope specifier @p SS. This template |
| 1248 | /// parameter list may be have template parameters (if we're declaring a |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1249 | /// template) or may have no template parameters (if we're declaring a |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1250 | /// template specialization), or may be NULL (if we were's declaring isn't |
| 1251 | /// itself a template). |
| 1252 | TemplateParameterList * |
| 1253 | Sema::MatchTemplateParametersToScopeSpecifier(SourceLocation DeclStartLoc, |
| 1254 | const CXXScopeSpec &SS, |
| 1255 | TemplateParameterList **ParamLists, |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 1256 | unsigned NumParamLists, |
John McCall | 77e8b11 | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 1257 | bool IsFriend, |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 1258 | bool &IsExplicitSpecialization) { |
| 1259 | IsExplicitSpecialization = false; |
| 1260 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1261 | // Find the template-ids that occur within the nested-name-specifier. These |
| 1262 | // template-ids will match up with the template parameter lists. |
| 1263 | llvm::SmallVector<const TemplateSpecializationType *, 4> |
| 1264 | TemplateIdsInSpecifier; |
Douglas Gregor | 3ebd753 | 2009-11-23 12:11:45 +0000 | [diff] [blame] | 1265 | llvm::SmallVector<ClassTemplateSpecializationDecl *, 4> |
| 1266 | ExplicitSpecializationsInSpecifier; |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1267 | for (NestedNameSpecifier *NNS = (NestedNameSpecifier *)SS.getScopeRep(); |
| 1268 | NNS; NNS = NNS->getPrefix()) { |
John McCall | 4b2b02b | 2009-12-15 02:19:47 +0000 | [diff] [blame] | 1269 | const Type *T = NNS->getAsType(); |
| 1270 | if (!T) break; |
| 1271 | |
| 1272 | // C++0x [temp.expl.spec]p17: |
| 1273 | // A member or a member template may be nested within many |
| 1274 | // enclosing class templates. In an explicit specialization for |
| 1275 | // such a member, the member declaration shall be preceded by a |
| 1276 | // template<> for each enclosing class template that is |
| 1277 | // explicitly specialized. |
Douglas Gregor | fe33106 | 2010-02-13 05:23:25 +0000 | [diff] [blame] | 1278 | // |
| 1279 | // Following the existing practice of GNU and EDG, we allow a typedef of a |
| 1280 | // template specialization type. |
| 1281 | if (const TypedefType *TT = dyn_cast<TypedefType>(T)) |
| 1282 | T = TT->LookThroughTypedefs().getTypePtr(); |
John McCall | 4b2b02b | 2009-12-15 02:19:47 +0000 | [diff] [blame] | 1283 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1284 | if (const TemplateSpecializationType *SpecType |
Douglas Gregor | fe33106 | 2010-02-13 05:23:25 +0000 | [diff] [blame] | 1285 | = dyn_cast<TemplateSpecializationType>(T)) { |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1286 | TemplateDecl *Template = SpecType->getTemplateName().getAsTemplateDecl(); |
| 1287 | if (!Template) |
| 1288 | continue; // FIXME: should this be an error? probably... |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1289 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1290 | if (const RecordType *Record = SpecType->getAs<RecordType>()) { |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1291 | ClassTemplateSpecializationDecl *SpecDecl |
| 1292 | = cast<ClassTemplateSpecializationDecl>(Record->getDecl()); |
| 1293 | // If the nested name specifier refers to an explicit specialization, |
| 1294 | // we don't need a template<> header. |
Douglas Gregor | 3ebd753 | 2009-11-23 12:11:45 +0000 | [diff] [blame] | 1295 | if (SpecDecl->getSpecializationKind() == TSK_ExplicitSpecialization) { |
| 1296 | ExplicitSpecializationsInSpecifier.push_back(SpecDecl); |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1297 | continue; |
Douglas Gregor | 3ebd753 | 2009-11-23 12:11:45 +0000 | [diff] [blame] | 1298 | } |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1299 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1300 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1301 | TemplateIdsInSpecifier.push_back(SpecType); |
| 1302 | } |
| 1303 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1304 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1305 | // Reverse the list of template-ids in the scope specifier, so that we can |
| 1306 | // more easily match up the template-ids and the template parameter lists. |
| 1307 | std::reverse(TemplateIdsInSpecifier.begin(), TemplateIdsInSpecifier.end()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1308 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1309 | SourceLocation FirstTemplateLoc = DeclStartLoc; |
| 1310 | if (NumParamLists) |
| 1311 | FirstTemplateLoc = ParamLists[0]->getTemplateLoc(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1312 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1313 | // Match the template-ids found in the specifier to the template parameter |
| 1314 | // lists. |
| 1315 | unsigned Idx = 0; |
| 1316 | for (unsigned NumTemplateIds = TemplateIdsInSpecifier.size(); |
| 1317 | Idx != NumTemplateIds; ++Idx) { |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 1318 | QualType TemplateId = QualType(TemplateIdsInSpecifier[Idx], 0); |
| 1319 | bool DependentTemplateId = TemplateId->isDependentType(); |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1320 | if (Idx >= NumParamLists) { |
| 1321 | // We have a template-id without a corresponding template parameter |
| 1322 | // list. |
John McCall | 77e8b11 | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 1323 | |
| 1324 | // ...which is fine if this is a friend declaration. |
| 1325 | if (IsFriend) { |
| 1326 | IsExplicitSpecialization = true; |
| 1327 | break; |
| 1328 | } |
| 1329 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1330 | if (DependentTemplateId) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1331 | // FIXME: the location information here isn't great. |
| 1332 | Diag(SS.getRange().getBegin(), |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1333 | diag::err_template_spec_needs_template_parameters) |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 1334 | << TemplateId |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1335 | << SS.getRange(); |
| 1336 | } else { |
| 1337 | Diag(SS.getRange().getBegin(), diag::err_template_spec_needs_header) |
| 1338 | << SS.getRange() |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 1339 | << FixItHint::CreateInsertion(FirstTemplateLoc, "template<> "); |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 1340 | IsExplicitSpecialization = true; |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1341 | } |
| 1342 | return 0; |
| 1343 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1344 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1345 | // Check the template parameter list against its corresponding template-id. |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 1346 | if (DependentTemplateId) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1347 | TemplateDecl *Template |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 1348 | = TemplateIdsInSpecifier[Idx]->getTemplateName().getAsTemplateDecl(); |
| 1349 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1350 | if (ClassTemplateDecl *ClassTemplate |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 1351 | = dyn_cast<ClassTemplateDecl>(Template)) { |
| 1352 | TemplateParameterList *ExpectedTemplateParams = 0; |
| 1353 | // Is this template-id naming the primary template? |
| 1354 | if (Context.hasSameType(TemplateId, |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 1355 | ClassTemplate->getInjectedClassNameSpecialization(Context))) |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 1356 | ExpectedTemplateParams = ClassTemplate->getTemplateParameters(); |
| 1357 | // ... or a partial specialization? |
| 1358 | else if (ClassTemplatePartialSpecializationDecl *PartialSpec |
| 1359 | = ClassTemplate->findPartialSpecialization(TemplateId)) |
| 1360 | ExpectedTemplateParams = PartialSpec->getTemplateParameters(); |
| 1361 | |
| 1362 | if (ExpectedTemplateParams) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1363 | TemplateParameterListsAreEqual(ParamLists[Idx], |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 1364 | ExpectedTemplateParams, |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 1365 | true, TPL_TemplateMatch); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1366 | } |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1367 | |
| 1368 | CheckTemplateParameterList(ParamLists[Idx], 0, TPC_ClassTemplateMember); |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 1369 | } else if (ParamLists[Idx]->size() > 0) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1370 | Diag(ParamLists[Idx]->getTemplateLoc(), |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 1371 | diag::err_template_param_list_matches_nontemplate) |
| 1372 | << TemplateId |
| 1373 | << ParamLists[Idx]->getSourceRange(); |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 1374 | else |
| 1375 | IsExplicitSpecialization = true; |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1376 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1377 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1378 | // If there were at least as many template-ids as there were template |
| 1379 | // parameter lists, then there are no template parameter lists remaining for |
| 1380 | // the declaration itself. |
| 1381 | if (Idx >= NumParamLists) |
| 1382 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1383 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1384 | // If there were too many template parameter lists, complain about that now. |
| 1385 | if (Idx != NumParamLists - 1) { |
| 1386 | while (Idx < NumParamLists - 1) { |
Douglas Gregor | 3ebd753 | 2009-11-23 12:11:45 +0000 | [diff] [blame] | 1387 | bool isExplicitSpecHeader = ParamLists[Idx]->size() == 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1388 | Diag(ParamLists[Idx]->getTemplateLoc(), |
Douglas Gregor | 3ebd753 | 2009-11-23 12:11:45 +0000 | [diff] [blame] | 1389 | isExplicitSpecHeader? diag::warn_template_spec_extra_headers |
| 1390 | : diag::err_template_spec_extra_headers) |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1391 | << SourceRange(ParamLists[Idx]->getTemplateLoc(), |
| 1392 | ParamLists[Idx]->getRAngleLoc()); |
Douglas Gregor | 3ebd753 | 2009-11-23 12:11:45 +0000 | [diff] [blame] | 1393 | |
| 1394 | if (isExplicitSpecHeader && !ExplicitSpecializationsInSpecifier.empty()) { |
| 1395 | Diag(ExplicitSpecializationsInSpecifier.back()->getLocation(), |
| 1396 | diag::note_explicit_template_spec_does_not_need_header) |
| 1397 | << ExplicitSpecializationsInSpecifier.back(); |
| 1398 | ExplicitSpecializationsInSpecifier.pop_back(); |
| 1399 | } |
| 1400 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1401 | ++Idx; |
| 1402 | } |
| 1403 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1404 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1405 | // Return the last template parameter list, which corresponds to the |
| 1406 | // entity being declared. |
| 1407 | return ParamLists[NumParamLists - 1]; |
| 1408 | } |
| 1409 | |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1410 | QualType Sema::CheckTemplateIdType(TemplateName Name, |
| 1411 | SourceLocation TemplateLoc, |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1412 | const TemplateArgumentListInfo &TemplateArgs) { |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1413 | TemplateDecl *Template = Name.getAsTemplateDecl(); |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 1414 | if (!Template) { |
| 1415 | // The template name does not resolve to a template, so we just |
| 1416 | // build a dependent template-id type. |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1417 | return Context.getTemplateSpecializationType(Name, TemplateArgs); |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 1418 | } |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1419 | |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1420 | // Check that the template argument list is well-formed for this |
| 1421 | // template. |
Anders Carlsson | fb25052 | 2009-06-23 01:26:57 +0000 | [diff] [blame] | 1422 | TemplateArgumentListBuilder Converted(Template->getTemplateParameters(), |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1423 | TemplateArgs.size()); |
| 1424 | if (CheckTemplateArgumentList(Template, TemplateLoc, TemplateArgs, |
Douglas Gregor | 16134c6 | 2009-07-01 00:28:38 +0000 | [diff] [blame] | 1425 | false, Converted)) |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1426 | return QualType(); |
| 1427 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1428 | assert((Converted.structuredSize() == |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1429 | Template->getTemplateParameters()->size()) && |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1430 | "Converted template argument list is too short!"); |
| 1431 | |
| 1432 | QualType CanonType; |
| 1433 | |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 1434 | if (Name.isDependent() || |
| 1435 | TemplateSpecializationType::anyDependentTemplateArguments( |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1436 | TemplateArgs)) { |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1437 | // This class template specialization is a dependent |
| 1438 | // type. Therefore, its canonical type is another class template |
| 1439 | // specialization type that contains all of the converted |
| 1440 | // arguments in canonical form. This ensures that, e.g., A<T> and |
| 1441 | // A<T, T> have identical types when A is declared as: |
| 1442 | // |
| 1443 | // template<typename T, typename U = T> struct A; |
Douglas Gregor | 25a3ef7 | 2009-05-07 06:41:52 +0000 | [diff] [blame] | 1444 | TemplateName CanonName = Context.getCanonicalTemplateName(Name); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1445 | CanonType = Context.getTemplateSpecializationType(CanonName, |
Anders Carlsson | fb25052 | 2009-06-23 01:26:57 +0000 | [diff] [blame] | 1446 | Converted.getFlatArguments(), |
| 1447 | Converted.flatSize()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1448 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 1449 | // FIXME: CanonType is not actually the canonical type, and unfortunately |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1450 | // it is a TemplateSpecializationType that we will never use again. |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 1451 | // In the future, we need to teach getTemplateSpecializationType to only |
| 1452 | // build the canonical type and return that to us. |
| 1453 | CanonType = Context.getCanonicalType(CanonType); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1454 | } else if (ClassTemplateDecl *ClassTemplate |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1455 | = dyn_cast<ClassTemplateDecl>(Template)) { |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1456 | // Find the class template specialization declaration that |
| 1457 | // corresponds to these arguments. |
| 1458 | llvm::FoldingSetNodeID ID; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1459 | ClassTemplateSpecializationDecl::Profile(ID, |
Anders Carlsson | fb25052 | 2009-06-23 01:26:57 +0000 | [diff] [blame] | 1460 | Converted.getFlatArguments(), |
Douglas Gregor | 828e226 | 2009-07-29 16:09:57 +0000 | [diff] [blame] | 1461 | Converted.flatSize(), |
| 1462 | Context); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1463 | void *InsertPos = 0; |
| 1464 | ClassTemplateSpecializationDecl *Decl |
| 1465 | = ClassTemplate->getSpecializations().FindNodeOrInsertPos(ID, InsertPos); |
| 1466 | if (!Decl) { |
| 1467 | // This is the first time we have referenced this class template |
| 1468 | // specialization. Create the canonical declaration and add it to |
| 1469 | // the set of specializations. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1470 | Decl = ClassTemplateSpecializationDecl::Create(Context, |
Anders Carlsson | 1c5976e | 2009-06-05 03:43:12 +0000 | [diff] [blame] | 1471 | ClassTemplate->getDeclContext(), |
John McCall | 9cc7807 | 2009-09-11 07:25:08 +0000 | [diff] [blame] | 1472 | ClassTemplate->getLocation(), |
Anders Carlsson | 1c5976e | 2009-06-05 03:43:12 +0000 | [diff] [blame] | 1473 | ClassTemplate, |
Anders Carlsson | fb25052 | 2009-06-23 01:26:57 +0000 | [diff] [blame] | 1474 | Converted, 0); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1475 | ClassTemplate->getSpecializations().InsertNode(Decl, InsertPos); |
| 1476 | Decl->setLexicalDeclContext(CurContext); |
| 1477 | } |
| 1478 | |
| 1479 | CanonType = Context.getTypeDeclType(Decl); |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 1480 | assert(isa<RecordType>(CanonType) && |
| 1481 | "type of non-dependent specialization is not a RecordType"); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1482 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1483 | |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1484 | // Build the fully-sugared type for this class template |
| 1485 | // specialization, which refers back to the class template |
| 1486 | // specialization we created or found. |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1487 | return Context.getTemplateSpecializationType(Name, TemplateArgs, CanonType); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1488 | } |
| 1489 | |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 1490 | Action::TypeResult |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1491 | Sema::ActOnTemplateIdType(TemplateTy TemplateD, SourceLocation TemplateLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1492 | SourceLocation LAngleLoc, |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1493 | ASTTemplateArgsPtr TemplateArgsIn, |
John McCall | 6b2becf | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 1494 | SourceLocation RAngleLoc) { |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1495 | TemplateName Template = TemplateD.getAsVal<TemplateName>(); |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 1496 | |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1497 | // Translate the parser's template argument list in our AST format. |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1498 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
Douglas Gregor | 314b97f | 2009-11-10 19:49:08 +0000 | [diff] [blame] | 1499 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 1500 | |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1501 | QualType Result = CheckTemplateIdType(Template, TemplateLoc, TemplateArgs); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1502 | TemplateArgsIn.release(); |
Douglas Gregor | 31a19b6 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 1503 | |
| 1504 | if (Result.isNull()) |
| 1505 | return true; |
| 1506 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1507 | TypeSourceInfo *DI = Context.CreateTypeSourceInfo(Result); |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1508 | TemplateSpecializationTypeLoc TL |
| 1509 | = cast<TemplateSpecializationTypeLoc>(DI->getTypeLoc()); |
| 1510 | TL.setTemplateNameLoc(TemplateLoc); |
| 1511 | TL.setLAngleLoc(LAngleLoc); |
| 1512 | TL.setRAngleLoc(RAngleLoc); |
| 1513 | for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) |
| 1514 | TL.setArgLocInfo(i, TemplateArgs[i].getLocInfo()); |
| 1515 | |
| 1516 | return CreateLocInfoType(Result, DI).getAsOpaquePtr(); |
John McCall | 6b2becf | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 1517 | } |
John McCall | f1bbbb4 | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 1518 | |
John McCall | 6b2becf | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 1519 | Sema::TypeResult Sema::ActOnTagTemplateIdType(TypeResult TypeResult, |
| 1520 | TagUseKind TUK, |
| 1521 | DeclSpec::TST TagSpec, |
| 1522 | SourceLocation TagLoc) { |
| 1523 | if (TypeResult.isInvalid()) |
| 1524 | return Sema::TypeResult(); |
John McCall | f1bbbb4 | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 1525 | |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1526 | // FIXME: preserve source info, ideally without copying the DI. |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1527 | TypeSourceInfo *DI; |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1528 | QualType Type = GetTypeFromParser(TypeResult.get(), &DI); |
John McCall | f1bbbb4 | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 1529 | |
John McCall | 6b2becf | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 1530 | // Verify the tag specifier. |
| 1531 | TagDecl::TagKind TagKind = TagDecl::getTagKindForTypeSpec(TagSpec); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1532 | |
John McCall | 6b2becf | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 1533 | if (const RecordType *RT = Type->getAs<RecordType>()) { |
| 1534 | RecordDecl *D = RT->getDecl(); |
| 1535 | |
| 1536 | IdentifierInfo *Id = D->getIdentifier(); |
| 1537 | assert(Id && "templated class must have an identifier"); |
| 1538 | |
| 1539 | if (!isAcceptableTagRedeclaration(D, TagKind, TagLoc, *Id)) { |
| 1540 | Diag(TagLoc, diag::err_use_with_wrong_tag) |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 1541 | << Type |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 1542 | << FixItHint::CreateReplacement(SourceRange(TagLoc), D->getKindName()); |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 1543 | Diag(D->getLocation(), diag::note_previous_use); |
John McCall | f1bbbb4 | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 1544 | } |
| 1545 | } |
| 1546 | |
John McCall | 6b2becf | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 1547 | QualType ElabType = Context.getElaboratedType(Type, TagKind); |
| 1548 | |
| 1549 | return ElabType.getAsOpaquePtr(); |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 1550 | } |
| 1551 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1552 | Sema::OwningExprResult Sema::BuildTemplateIdExpr(const CXXScopeSpec &SS, |
| 1553 | LookupResult &R, |
| 1554 | bool RequiresADL, |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1555 | const TemplateArgumentListInfo &TemplateArgs) { |
Douglas Gregor | edce4dd | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 1556 | // FIXME: Can we do any checking at this point? I guess we could check the |
| 1557 | // template arguments that we have against the template name, if the template |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1558 | // 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] | 1559 | // though. |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1560 | |
| 1561 | // These should be filtered out by our callers. |
| 1562 | assert(!R.empty() && "empty lookup results when building templateid"); |
| 1563 | assert(!R.isAmbiguous() && "ambiguous lookup when building templateid"); |
| 1564 | |
| 1565 | NestedNameSpecifier *Qualifier = 0; |
| 1566 | SourceRange QualifierRange; |
| 1567 | if (SS.isSet()) { |
| 1568 | Qualifier = static_cast<NestedNameSpecifier*>(SS.getScopeRep()); |
| 1569 | QualifierRange = SS.getRange(); |
Douglas Gregor | a9e29aa | 2009-10-22 07:19:14 +0000 | [diff] [blame] | 1570 | } |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 1571 | |
| 1572 | // We don't want lookup warnings at this point. |
| 1573 | R.suppressDiagnostics(); |
Douglas Gregor | a9e29aa | 2009-10-22 07:19:14 +0000 | [diff] [blame] | 1574 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1575 | bool Dependent |
| 1576 | = UnresolvedLookupExpr::ComputeDependence(R.begin(), R.end(), |
| 1577 | &TemplateArgs); |
| 1578 | UnresolvedLookupExpr *ULE |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 1579 | = UnresolvedLookupExpr::Create(Context, Dependent, R.getNamingClass(), |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1580 | Qualifier, QualifierRange, |
| 1581 | R.getLookupName(), R.getNameLoc(), |
| 1582 | RequiresADL, TemplateArgs); |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 1583 | ULE->addDecls(R.begin(), R.end()); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1584 | |
| 1585 | return Owned(ULE); |
Douglas Gregor | edce4dd | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 1586 | } |
| 1587 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1588 | // We actually only call this from template instantiation. |
| 1589 | Sema::OwningExprResult |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 1590 | Sema::BuildQualifiedTemplateIdExpr(CXXScopeSpec &SS, |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1591 | DeclarationName Name, |
| 1592 | SourceLocation NameLoc, |
| 1593 | const TemplateArgumentListInfo &TemplateArgs) { |
| 1594 | DeclContext *DC; |
| 1595 | if (!(DC = computeDeclContext(SS, false)) || |
| 1596 | DC->isDependentContext() || |
| 1597 | RequireCompleteDeclContext(SS)) |
| 1598 | return BuildDependentDeclRefExpr(SS, Name, NameLoc, &TemplateArgs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1599 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1600 | LookupResult R(*this, Name, NameLoc, LookupOrdinaryName); |
| 1601 | LookupTemplateName(R, (Scope*) 0, SS, QualType(), /*Entering*/ false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1602 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1603 | if (R.isAmbiguous()) |
| 1604 | return ExprError(); |
| 1605 | |
| 1606 | if (R.empty()) { |
| 1607 | Diag(NameLoc, diag::err_template_kw_refers_to_non_template) |
| 1608 | << Name << SS.getRange(); |
| 1609 | return ExprError(); |
| 1610 | } |
| 1611 | |
| 1612 | if (ClassTemplateDecl *Temp = R.getAsSingle<ClassTemplateDecl>()) { |
| 1613 | Diag(NameLoc, diag::err_template_kw_refers_to_class_template) |
| 1614 | << (NestedNameSpecifier*) SS.getScopeRep() << Name << SS.getRange(); |
| 1615 | Diag(Temp->getLocation(), diag::note_referenced_class_template); |
| 1616 | return ExprError(); |
| 1617 | } |
| 1618 | |
| 1619 | return BuildTemplateIdExpr(SS, R, /* ADL */ false, TemplateArgs); |
Douglas Gregor | edce4dd | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 1620 | } |
| 1621 | |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 1622 | /// \brief Form a dependent template name. |
| 1623 | /// |
| 1624 | /// This action forms a dependent template name given the template |
| 1625 | /// name and its (presumably dependent) scope specifier. For |
| 1626 | /// example, given "MetaFun::template apply", the scope specifier \p |
| 1627 | /// SS will be "MetaFun::", \p TemplateKWLoc contains the location |
| 1628 | /// of the "template" keyword, and "apply" is the \p Name. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1629 | Sema::TemplateTy |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 1630 | Sema::ActOnDependentTemplateName(SourceLocation TemplateKWLoc, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 1631 | CXXScopeSpec &SS, |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 1632 | UnqualifiedId &Name, |
Douglas Gregor | a481edb | 2009-11-20 23:39:24 +0000 | [diff] [blame] | 1633 | TypeTy *ObjectType, |
| 1634 | bool EnteringContext) { |
Douglas Gregor | 0707bc5 | 2010-01-19 16:01:07 +0000 | [diff] [blame] | 1635 | DeclContext *LookupCtx = 0; |
| 1636 | if (SS.isSet()) |
| 1637 | LookupCtx = computeDeclContext(SS, EnteringContext); |
| 1638 | if (!LookupCtx && ObjectType) |
| 1639 | LookupCtx = computeDeclContext(QualType::getFromOpaquePtr(ObjectType)); |
| 1640 | if (LookupCtx) { |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 1641 | // C++0x [temp.names]p5: |
| 1642 | // If a name prefixed by the keyword template is not the name of |
| 1643 | // a template, the program is ill-formed. [Note: the keyword |
| 1644 | // template may not be applied to non-template members of class |
| 1645 | // templates. -end note ] [ Note: as is the case with the |
| 1646 | // typename prefix, the template prefix is allowed in cases |
| 1647 | // where it is not strictly necessary; i.e., when the |
| 1648 | // nested-name-specifier or the expression on the left of the -> |
| 1649 | // or . is not dependent on a template-parameter, or the use |
| 1650 | // does not appear in the scope of a template. -end note] |
| 1651 | // |
| 1652 | // Note: C++03 was more strict here, because it banned the use of |
| 1653 | // the "template" keyword prior to a template-name that was not a |
| 1654 | // dependent name. C++ DR468 relaxed this requirement (the |
| 1655 | // "template" keyword is now permitted). We follow the C++0x |
| 1656 | // rules, even in C++03 mode, retroactively applying the DR. |
| 1657 | TemplateTy Template; |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 1658 | TemplateNameKind TNK = isTemplateName(0, SS, Name, ObjectType, |
Douglas Gregor | a481edb | 2009-11-20 23:39:24 +0000 | [diff] [blame] | 1659 | EnteringContext, Template); |
Douglas Gregor | 0707bc5 | 2010-01-19 16:01:07 +0000 | [diff] [blame] | 1660 | if (TNK == TNK_Non_template && LookupCtx->isDependentContext() && |
| 1661 | isa<CXXRecordDecl>(LookupCtx) && |
| 1662 | cast<CXXRecordDecl>(LookupCtx)->hasAnyDependentBases()) { |
Douglas Gregor | 9edad9b | 2010-01-14 17:47:39 +0000 | [diff] [blame] | 1663 | // This is a dependent template. |
| 1664 | } else if (TNK == TNK_Non_template) { |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 1665 | Diag(Name.getSourceRange().getBegin(), |
| 1666 | diag::err_template_kw_refers_to_non_template) |
| 1667 | << GetNameFromUnqualifiedId(Name) |
| 1668 | << Name.getSourceRange(); |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 1669 | return TemplateTy(); |
Douglas Gregor | 9edad9b | 2010-01-14 17:47:39 +0000 | [diff] [blame] | 1670 | } else { |
| 1671 | // We found something; return it. |
| 1672 | return Template; |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 1673 | } |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 1674 | } |
| 1675 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1676 | NestedNameSpecifier *Qualifier |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 1677 | = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 1678 | |
| 1679 | switch (Name.getKind()) { |
| 1680 | case UnqualifiedId::IK_Identifier: |
| 1681 | return TemplateTy::make(Context.getDependentTemplateName(Qualifier, |
| 1682 | Name.Identifier)); |
| 1683 | |
Douglas Gregor | ca1bdd7 | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 1684 | case UnqualifiedId::IK_OperatorFunctionId: |
| 1685 | return TemplateTy::make(Context.getDependentTemplateName(Qualifier, |
| 1686 | Name.OperatorFunctionId.Operator)); |
Sean Hunt | e6252d1 | 2009-11-28 08:58:14 +0000 | [diff] [blame] | 1687 | |
| 1688 | case UnqualifiedId::IK_LiteralOperatorId: |
| 1689 | assert(false && "We don't support these; Parse shouldn't have allowed propagation"); |
| 1690 | |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 1691 | default: |
| 1692 | break; |
| 1693 | } |
| 1694 | |
| 1695 | Diag(Name.getSourceRange().getBegin(), |
| 1696 | diag::err_template_kw_refers_to_non_template) |
| 1697 | << GetNameFromUnqualifiedId(Name) |
| 1698 | << Name.getSourceRange(); |
| 1699 | return TemplateTy(); |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 1700 | } |
| 1701 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1702 | bool Sema::CheckTemplateTypeArgument(TemplateTypeParmDecl *Param, |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1703 | const TemplateArgumentLoc &AL, |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 1704 | TemplateArgumentListBuilder &Converted) { |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1705 | const TemplateArgument &Arg = AL.getArgument(); |
| 1706 | |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 1707 | // Check template type parameter. |
Jeffrey Yasskin | db88d8a | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 1708 | switch(Arg.getKind()) { |
| 1709 | case TemplateArgument::Type: |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 1710 | // C++ [temp.arg.type]p1: |
| 1711 | // A template-argument for a template-parameter which is a |
| 1712 | // type shall be a type-id. |
Jeffrey Yasskin | db88d8a | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 1713 | break; |
| 1714 | case TemplateArgument::Template: { |
| 1715 | // We have a template type parameter but the template argument |
| 1716 | // is a template without any arguments. |
| 1717 | SourceRange SR = AL.getSourceRange(); |
| 1718 | TemplateName Name = Arg.getAsTemplate(); |
| 1719 | Diag(SR.getBegin(), diag::err_template_missing_args) |
| 1720 | << Name << SR; |
| 1721 | if (TemplateDecl *Decl = Name.getAsTemplateDecl()) |
| 1722 | Diag(Decl->getLocation(), diag::note_template_decl_here); |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 1723 | |
Jeffrey Yasskin | db88d8a | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 1724 | return true; |
| 1725 | } |
| 1726 | default: { |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 1727 | // We have a template type parameter but the template argument |
| 1728 | // is not a type. |
John McCall | 828bff2 | 2009-10-29 18:45:58 +0000 | [diff] [blame] | 1729 | SourceRange SR = AL.getSourceRange(); |
| 1730 | Diag(SR.getBegin(), diag::err_template_arg_must_be_type) << SR; |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 1731 | Diag(Param->getLocation(), diag::note_template_param_here); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1732 | |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 1733 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1734 | } |
Jeffrey Yasskin | db88d8a | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 1735 | } |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 1736 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1737 | if (CheckTemplateArgument(Param, AL.getTypeSourceInfo())) |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 1738 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1739 | |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 1740 | // Add the converted template type argument. |
Anders Carlsson | fb25052 | 2009-06-23 01:26:57 +0000 | [diff] [blame] | 1741 | Converted.Append( |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1742 | TemplateArgument(Context.getCanonicalType(Arg.getAsType()))); |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 1743 | return false; |
| 1744 | } |
| 1745 | |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 1746 | /// \brief Substitute template arguments into the default template argument for |
| 1747 | /// the given template type parameter. |
| 1748 | /// |
| 1749 | /// \param SemaRef the semantic analysis object for which we are performing |
| 1750 | /// the substitution. |
| 1751 | /// |
| 1752 | /// \param Template the template that we are synthesizing template arguments |
| 1753 | /// for. |
| 1754 | /// |
| 1755 | /// \param TemplateLoc the location of the template name that started the |
| 1756 | /// template-id we are checking. |
| 1757 | /// |
| 1758 | /// \param RAngleLoc the location of the right angle bracket ('>') that |
| 1759 | /// terminates the template-id. |
| 1760 | /// |
| 1761 | /// \param Param the template template parameter whose default we are |
| 1762 | /// substituting into. |
| 1763 | /// |
| 1764 | /// \param Converted the list of template arguments provided for template |
| 1765 | /// parameters that precede \p Param in the template parameter list. |
| 1766 | /// |
| 1767 | /// \returns the substituted template argument, or NULL if an error occurred. |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1768 | static TypeSourceInfo * |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 1769 | SubstDefaultTemplateArgument(Sema &SemaRef, |
| 1770 | TemplateDecl *Template, |
| 1771 | SourceLocation TemplateLoc, |
| 1772 | SourceLocation RAngleLoc, |
| 1773 | TemplateTypeParmDecl *Param, |
| 1774 | TemplateArgumentListBuilder &Converted) { |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1775 | TypeSourceInfo *ArgType = Param->getDefaultArgumentInfo(); |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 1776 | |
| 1777 | // If the argument type is dependent, instantiate it now based |
| 1778 | // on the previously-computed template arguments. |
| 1779 | if (ArgType->getType()->isDependentType()) { |
| 1780 | TemplateArgumentList TemplateArgs(SemaRef.Context, Converted, |
| 1781 | /*TakeArgs=*/false); |
| 1782 | |
| 1783 | MultiLevelTemplateArgumentList AllTemplateArgs |
| 1784 | = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs); |
| 1785 | |
| 1786 | Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc, |
| 1787 | Template, Converted.getFlatArguments(), |
| 1788 | Converted.flatSize(), |
| 1789 | SourceRange(TemplateLoc, RAngleLoc)); |
| 1790 | |
| 1791 | ArgType = SemaRef.SubstType(ArgType, AllTemplateArgs, |
| 1792 | Param->getDefaultArgumentLoc(), |
| 1793 | Param->getDeclName()); |
| 1794 | } |
| 1795 | |
| 1796 | return ArgType; |
| 1797 | } |
| 1798 | |
| 1799 | /// \brief Substitute template arguments into the default template argument for |
| 1800 | /// the given non-type template parameter. |
| 1801 | /// |
| 1802 | /// \param SemaRef the semantic analysis object for which we are performing |
| 1803 | /// the substitution. |
| 1804 | /// |
| 1805 | /// \param Template the template that we are synthesizing template arguments |
| 1806 | /// for. |
| 1807 | /// |
| 1808 | /// \param TemplateLoc the location of the template name that started the |
| 1809 | /// template-id we are checking. |
| 1810 | /// |
| 1811 | /// \param RAngleLoc the location of the right angle bracket ('>') that |
| 1812 | /// terminates the template-id. |
| 1813 | /// |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1814 | /// \param Param the non-type template parameter whose default we are |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 1815 | /// substituting into. |
| 1816 | /// |
| 1817 | /// \param Converted the list of template arguments provided for template |
| 1818 | /// parameters that precede \p Param in the template parameter list. |
| 1819 | /// |
| 1820 | /// \returns the substituted template argument, or NULL if an error occurred. |
| 1821 | static Sema::OwningExprResult |
| 1822 | SubstDefaultTemplateArgument(Sema &SemaRef, |
| 1823 | TemplateDecl *Template, |
| 1824 | SourceLocation TemplateLoc, |
| 1825 | SourceLocation RAngleLoc, |
| 1826 | NonTypeTemplateParmDecl *Param, |
| 1827 | TemplateArgumentListBuilder &Converted) { |
| 1828 | TemplateArgumentList TemplateArgs(SemaRef.Context, Converted, |
| 1829 | /*TakeArgs=*/false); |
| 1830 | |
| 1831 | MultiLevelTemplateArgumentList AllTemplateArgs |
| 1832 | = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs); |
| 1833 | |
| 1834 | Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc, |
| 1835 | Template, Converted.getFlatArguments(), |
| 1836 | Converted.flatSize(), |
| 1837 | SourceRange(TemplateLoc, RAngleLoc)); |
| 1838 | |
| 1839 | return SemaRef.SubstExpr(Param->getDefaultArgument(), AllTemplateArgs); |
| 1840 | } |
| 1841 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1842 | /// \brief Substitute template arguments into the default template argument for |
| 1843 | /// the given template template parameter. |
| 1844 | /// |
| 1845 | /// \param SemaRef the semantic analysis object for which we are performing |
| 1846 | /// the substitution. |
| 1847 | /// |
| 1848 | /// \param Template the template that we are synthesizing template arguments |
| 1849 | /// for. |
| 1850 | /// |
| 1851 | /// \param TemplateLoc the location of the template name that started the |
| 1852 | /// template-id we are checking. |
| 1853 | /// |
| 1854 | /// \param RAngleLoc the location of the right angle bracket ('>') that |
| 1855 | /// terminates the template-id. |
| 1856 | /// |
| 1857 | /// \param Param the template template parameter whose default we are |
| 1858 | /// substituting into. |
| 1859 | /// |
| 1860 | /// \param Converted the list of template arguments provided for template |
| 1861 | /// parameters that precede \p Param in the template parameter list. |
| 1862 | /// |
| 1863 | /// \returns the substituted template argument, or NULL if an error occurred. |
| 1864 | static TemplateName |
| 1865 | SubstDefaultTemplateArgument(Sema &SemaRef, |
| 1866 | TemplateDecl *Template, |
| 1867 | SourceLocation TemplateLoc, |
| 1868 | SourceLocation RAngleLoc, |
| 1869 | TemplateTemplateParmDecl *Param, |
| 1870 | TemplateArgumentListBuilder &Converted) { |
| 1871 | TemplateArgumentList TemplateArgs(SemaRef.Context, Converted, |
| 1872 | /*TakeArgs=*/false); |
| 1873 | |
| 1874 | MultiLevelTemplateArgumentList AllTemplateArgs |
| 1875 | = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs); |
| 1876 | |
| 1877 | Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc, |
| 1878 | Template, Converted.getFlatArguments(), |
| 1879 | Converted.flatSize(), |
| 1880 | SourceRange(TemplateLoc, RAngleLoc)); |
| 1881 | |
| 1882 | return SemaRef.SubstTemplateName( |
| 1883 | Param->getDefaultArgument().getArgument().getAsTemplate(), |
| 1884 | Param->getDefaultArgument().getTemplateNameLoc(), |
| 1885 | AllTemplateArgs); |
| 1886 | } |
| 1887 | |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 1888 | /// \brief If the given template parameter has a default template |
| 1889 | /// argument, substitute into that default template argument and |
| 1890 | /// return the corresponding template argument. |
| 1891 | TemplateArgumentLoc |
| 1892 | Sema::SubstDefaultTemplateArgumentIfAvailable(TemplateDecl *Template, |
| 1893 | SourceLocation TemplateLoc, |
| 1894 | SourceLocation RAngleLoc, |
| 1895 | Decl *Param, |
| 1896 | TemplateArgumentListBuilder &Converted) { |
| 1897 | if (TemplateTypeParmDecl *TypeParm = dyn_cast<TemplateTypeParmDecl>(Param)) { |
| 1898 | if (!TypeParm->hasDefaultArgument()) |
| 1899 | return TemplateArgumentLoc(); |
| 1900 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1901 | TypeSourceInfo *DI = SubstDefaultTemplateArgument(*this, Template, |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 1902 | TemplateLoc, |
| 1903 | RAngleLoc, |
| 1904 | TypeParm, |
| 1905 | Converted); |
| 1906 | if (DI) |
| 1907 | return TemplateArgumentLoc(TemplateArgument(DI->getType()), DI); |
| 1908 | |
| 1909 | return TemplateArgumentLoc(); |
| 1910 | } |
| 1911 | |
| 1912 | if (NonTypeTemplateParmDecl *NonTypeParm |
| 1913 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
| 1914 | if (!NonTypeParm->hasDefaultArgument()) |
| 1915 | return TemplateArgumentLoc(); |
| 1916 | |
| 1917 | OwningExprResult Arg = SubstDefaultTemplateArgument(*this, Template, |
| 1918 | TemplateLoc, |
| 1919 | RAngleLoc, |
| 1920 | NonTypeParm, |
| 1921 | Converted); |
| 1922 | if (Arg.isInvalid()) |
| 1923 | return TemplateArgumentLoc(); |
| 1924 | |
| 1925 | Expr *ArgE = Arg.takeAs<Expr>(); |
| 1926 | return TemplateArgumentLoc(TemplateArgument(ArgE), ArgE); |
| 1927 | } |
| 1928 | |
| 1929 | TemplateTemplateParmDecl *TempTempParm |
| 1930 | = cast<TemplateTemplateParmDecl>(Param); |
| 1931 | if (!TempTempParm->hasDefaultArgument()) |
| 1932 | return TemplateArgumentLoc(); |
| 1933 | |
| 1934 | TemplateName TName = SubstDefaultTemplateArgument(*this, Template, |
| 1935 | TemplateLoc, |
| 1936 | RAngleLoc, |
| 1937 | TempTempParm, |
| 1938 | Converted); |
| 1939 | if (TName.isNull()) |
| 1940 | return TemplateArgumentLoc(); |
| 1941 | |
| 1942 | return TemplateArgumentLoc(TemplateArgument(TName), |
| 1943 | TempTempParm->getDefaultArgument().getTemplateQualifierRange(), |
| 1944 | TempTempParm->getDefaultArgument().getTemplateNameLoc()); |
| 1945 | } |
| 1946 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 1947 | /// \brief Check that the given template argument corresponds to the given |
| 1948 | /// template parameter. |
| 1949 | bool Sema::CheckTemplateArgument(NamedDecl *Param, |
| 1950 | const TemplateArgumentLoc &Arg, |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 1951 | TemplateDecl *Template, |
| 1952 | SourceLocation TemplateLoc, |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 1953 | SourceLocation RAngleLoc, |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 1954 | TemplateArgumentListBuilder &Converted, |
| 1955 | CheckTemplateArgumentKind CTAK) { |
Douglas Gregor | d9e1530 | 2009-11-11 19:41:09 +0000 | [diff] [blame] | 1956 | // Check template type parameters. |
| 1957 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 1958 | return CheckTemplateTypeArgument(TTP, Arg, Converted); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 1959 | |
Douglas Gregor | d9e1530 | 2009-11-11 19:41:09 +0000 | [diff] [blame] | 1960 | // Check non-type template parameters. |
| 1961 | if (NonTypeTemplateParmDecl *NTTP =dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 1962 | // Do substitution on the type of the non-type template parameter |
| 1963 | // with the template arguments we've seen thus far. |
| 1964 | QualType NTTPType = NTTP->getType(); |
| 1965 | if (NTTPType->isDependentType()) { |
| 1966 | // Do substitution on the type of the non-type template parameter. |
| 1967 | InstantiatingTemplate Inst(*this, TemplateLoc, Template, |
| 1968 | NTTP, Converted.getFlatArguments(), |
| 1969 | Converted.flatSize(), |
| 1970 | SourceRange(TemplateLoc, RAngleLoc)); |
| 1971 | |
| 1972 | TemplateArgumentList TemplateArgs(Context, Converted, |
| 1973 | /*TakeArgs=*/false); |
| 1974 | NTTPType = SubstType(NTTPType, |
| 1975 | MultiLevelTemplateArgumentList(TemplateArgs), |
| 1976 | NTTP->getLocation(), |
| 1977 | NTTP->getDeclName()); |
| 1978 | // If that worked, check the non-type template parameter type |
| 1979 | // for validity. |
| 1980 | if (!NTTPType.isNull()) |
| 1981 | NTTPType = CheckNonTypeTemplateParameterType(NTTPType, |
| 1982 | NTTP->getLocation()); |
| 1983 | if (NTTPType.isNull()) |
| 1984 | return true; |
| 1985 | } |
| 1986 | |
| 1987 | switch (Arg.getArgument().getKind()) { |
| 1988 | case TemplateArgument::Null: |
| 1989 | assert(false && "Should never see a NULL template argument here"); |
| 1990 | return true; |
| 1991 | |
| 1992 | case TemplateArgument::Expression: { |
| 1993 | Expr *E = Arg.getArgument().getAsExpr(); |
| 1994 | TemplateArgument Result; |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 1995 | if (CheckTemplateArgument(NTTP, NTTPType, E, Result, CTAK)) |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 1996 | return true; |
| 1997 | |
| 1998 | Converted.Append(Result); |
| 1999 | break; |
| 2000 | } |
| 2001 | |
| 2002 | case TemplateArgument::Declaration: |
| 2003 | case TemplateArgument::Integral: |
| 2004 | // We've already checked this template argument, so just copy |
| 2005 | // it to the list of converted arguments. |
| 2006 | Converted.Append(Arg.getArgument()); |
| 2007 | break; |
| 2008 | |
| 2009 | case TemplateArgument::Template: |
| 2010 | // We were given a template template argument. It may not be ill-formed; |
| 2011 | // see below. |
| 2012 | if (DependentTemplateName *DTN |
| 2013 | = Arg.getArgument().getAsTemplate().getAsDependentTemplateName()) { |
| 2014 | // We have a template argument such as \c T::template X, which we |
| 2015 | // parsed as a template template argument. However, since we now |
| 2016 | // know that we need a non-type template argument, convert this |
| 2017 | // template name into an expression. |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2018 | Expr *E = DependentScopeDeclRefExpr::Create(Context, |
| 2019 | DTN->getQualifier(), |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2020 | Arg.getTemplateQualifierRange(), |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2021 | DTN->getIdentifier(), |
| 2022 | Arg.getTemplateNameLoc()); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2023 | |
| 2024 | TemplateArgument Result; |
| 2025 | if (CheckTemplateArgument(NTTP, NTTPType, E, Result)) |
| 2026 | return true; |
| 2027 | |
| 2028 | Converted.Append(Result); |
| 2029 | break; |
| 2030 | } |
| 2031 | |
| 2032 | // We have a template argument that actually does refer to a class |
| 2033 | // template, template alias, or template template parameter, and |
| 2034 | // therefore cannot be a non-type template argument. |
| 2035 | Diag(Arg.getLocation(), diag::err_template_arg_must_be_expr) |
| 2036 | << Arg.getSourceRange(); |
| 2037 | |
| 2038 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 2039 | return true; |
| 2040 | |
| 2041 | case TemplateArgument::Type: { |
| 2042 | // We have a non-type template parameter but the template |
| 2043 | // argument is a type. |
| 2044 | |
| 2045 | // C++ [temp.arg]p2: |
| 2046 | // In a template-argument, an ambiguity between a type-id and |
| 2047 | // an expression is resolved to a type-id, regardless of the |
| 2048 | // form of the corresponding template-parameter. |
| 2049 | // |
| 2050 | // We warn specifically about this case, since it can be rather |
| 2051 | // confusing for users. |
| 2052 | QualType T = Arg.getArgument().getAsType(); |
| 2053 | SourceRange SR = Arg.getSourceRange(); |
| 2054 | if (T->isFunctionType()) |
| 2055 | Diag(SR.getBegin(), diag::err_template_arg_nontype_ambig) << SR << T; |
| 2056 | else |
| 2057 | Diag(SR.getBegin(), diag::err_template_arg_must_be_expr) << SR; |
| 2058 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 2059 | return true; |
| 2060 | } |
| 2061 | |
| 2062 | case TemplateArgument::Pack: |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 2063 | llvm_unreachable("Caller must expand template argument packs"); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2064 | break; |
| 2065 | } |
| 2066 | |
| 2067 | return false; |
| 2068 | } |
| 2069 | |
| 2070 | |
| 2071 | // Check template template parameters. |
| 2072 | TemplateTemplateParmDecl *TempParm = cast<TemplateTemplateParmDecl>(Param); |
| 2073 | |
| 2074 | // Substitute into the template parameter list of the template |
| 2075 | // template parameter, since previously-supplied template arguments |
| 2076 | // may appear within the template template parameter. |
| 2077 | { |
| 2078 | // Set up a template instantiation context. |
| 2079 | LocalInstantiationScope Scope(*this); |
| 2080 | InstantiatingTemplate Inst(*this, TemplateLoc, Template, |
| 2081 | TempParm, Converted.getFlatArguments(), |
| 2082 | Converted.flatSize(), |
| 2083 | SourceRange(TemplateLoc, RAngleLoc)); |
| 2084 | |
| 2085 | TemplateArgumentList TemplateArgs(Context, Converted, |
| 2086 | /*TakeArgs=*/false); |
| 2087 | TempParm = cast_or_null<TemplateTemplateParmDecl>( |
| 2088 | SubstDecl(TempParm, CurContext, |
| 2089 | MultiLevelTemplateArgumentList(TemplateArgs))); |
| 2090 | if (!TempParm) |
| 2091 | return true; |
| 2092 | |
| 2093 | // FIXME: TempParam is leaked. |
| 2094 | } |
| 2095 | |
| 2096 | switch (Arg.getArgument().getKind()) { |
| 2097 | case TemplateArgument::Null: |
| 2098 | assert(false && "Should never see a NULL template argument here"); |
| 2099 | return true; |
| 2100 | |
| 2101 | case TemplateArgument::Template: |
| 2102 | if (CheckTemplateArgument(TempParm, Arg)) |
| 2103 | return true; |
| 2104 | |
| 2105 | Converted.Append(Arg.getArgument()); |
| 2106 | break; |
| 2107 | |
| 2108 | case TemplateArgument::Expression: |
| 2109 | case TemplateArgument::Type: |
| 2110 | // We have a template template parameter but the template |
| 2111 | // argument does not refer to a template. |
| 2112 | Diag(Arg.getLocation(), diag::err_template_arg_must_be_template); |
| 2113 | return true; |
| 2114 | |
| 2115 | case TemplateArgument::Declaration: |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 2116 | llvm_unreachable( |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2117 | "Declaration argument with template template parameter"); |
| 2118 | break; |
| 2119 | case TemplateArgument::Integral: |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 2120 | llvm_unreachable( |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2121 | "Integral argument with template template parameter"); |
| 2122 | break; |
| 2123 | |
| 2124 | case TemplateArgument::Pack: |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 2125 | llvm_unreachable("Caller must expand template argument packs"); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2126 | break; |
| 2127 | } |
| 2128 | |
| 2129 | return false; |
| 2130 | } |
| 2131 | |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2132 | /// \brief Check that the given template argument list is well-formed |
| 2133 | /// for specializing the given template. |
| 2134 | bool Sema::CheckTemplateArgumentList(TemplateDecl *Template, |
| 2135 | SourceLocation TemplateLoc, |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2136 | const TemplateArgumentListInfo &TemplateArgs, |
Douglas Gregor | 16134c6 | 2009-07-01 00:28:38 +0000 | [diff] [blame] | 2137 | bool PartialTemplateArgs, |
Anders Carlsson | 1c5976e | 2009-06-05 03:43:12 +0000 | [diff] [blame] | 2138 | TemplateArgumentListBuilder &Converted) { |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2139 | TemplateParameterList *Params = Template->getTemplateParameters(); |
| 2140 | unsigned NumParams = Params->size(); |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2141 | unsigned NumArgs = TemplateArgs.size(); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2142 | bool Invalid = false; |
| 2143 | |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2144 | SourceLocation RAngleLoc = TemplateArgs.getRAngleLoc(); |
| 2145 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2146 | bool HasParameterPack = |
Anders Carlsson | 0ceffb5 | 2009-06-13 02:08:00 +0000 | [diff] [blame] | 2147 | NumParams > 0 && Params->getParam(NumParams - 1)->isTemplateParameterPack(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2148 | |
Anders Carlsson | 0ceffb5 | 2009-06-13 02:08:00 +0000 | [diff] [blame] | 2149 | if ((NumArgs > NumParams && !HasParameterPack) || |
Douglas Gregor | 16134c6 | 2009-07-01 00:28:38 +0000 | [diff] [blame] | 2150 | (NumArgs < Params->getMinRequiredArguments() && |
| 2151 | !PartialTemplateArgs)) { |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2152 | // FIXME: point at either the first arg beyond what we can handle, |
| 2153 | // or the '>', depending on whether we have too many or too few |
| 2154 | // arguments. |
| 2155 | SourceRange Range; |
| 2156 | if (NumArgs > NumParams) |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2157 | Range = SourceRange(TemplateArgs[NumParams].getLocation(), RAngleLoc); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2158 | Diag(TemplateLoc, diag::err_template_arg_list_different_arity) |
| 2159 | << (NumArgs > NumParams) |
| 2160 | << (isa<ClassTemplateDecl>(Template)? 0 : |
| 2161 | isa<FunctionTemplateDecl>(Template)? 1 : |
| 2162 | isa<TemplateTemplateParmDecl>(Template)? 2 : 3) |
| 2163 | << Template << Range; |
Douglas Gregor | 62cb18d | 2009-02-11 18:16:40 +0000 | [diff] [blame] | 2164 | Diag(Template->getLocation(), diag::note_template_decl_here) |
| 2165 | << Params->getSourceRange(); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2166 | Invalid = true; |
| 2167 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2168 | |
| 2169 | // C++ [temp.arg]p1: |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2170 | // [...] The type and form of each template-argument specified in |
| 2171 | // a template-id shall match the type and form specified for the |
| 2172 | // corresponding parameter declared by the template in its |
| 2173 | // template-parameter-list. |
| 2174 | unsigned ArgIdx = 0; |
| 2175 | for (TemplateParameterList::iterator Param = Params->begin(), |
| 2176 | ParamEnd = Params->end(); |
| 2177 | Param != ParamEnd; ++Param, ++ArgIdx) { |
Douglas Gregor | 16134c6 | 2009-07-01 00:28:38 +0000 | [diff] [blame] | 2178 | if (ArgIdx > NumArgs && PartialTemplateArgs) |
| 2179 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2180 | |
Douglas Gregor | d9e1530 | 2009-11-11 19:41:09 +0000 | [diff] [blame] | 2181 | // If we have a template parameter pack, check every remaining template |
| 2182 | // argument against that template parameter pack. |
| 2183 | if ((*Param)->isTemplateParameterPack()) { |
| 2184 | Converted.BeginPack(); |
| 2185 | for (; ArgIdx < NumArgs; ++ArgIdx) { |
| 2186 | if (CheckTemplateArgument(*Param, TemplateArgs[ArgIdx], Template, |
| 2187 | TemplateLoc, RAngleLoc, Converted)) { |
| 2188 | Invalid = true; |
| 2189 | break; |
| 2190 | } |
| 2191 | } |
| 2192 | Converted.EndPack(); |
| 2193 | continue; |
| 2194 | } |
| 2195 | |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2196 | if (ArgIdx < NumArgs) { |
| 2197 | // Check the template argument we were given. |
| 2198 | if (CheckTemplateArgument(*Param, TemplateArgs[ArgIdx], Template, |
| 2199 | TemplateLoc, RAngleLoc, Converted)) |
| 2200 | return true; |
| 2201 | |
| 2202 | continue; |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 2203 | } |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2204 | |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2205 | // We have a default template argument that we will use. |
| 2206 | TemplateArgumentLoc Arg; |
| 2207 | |
| 2208 | // Retrieve the default template argument from the template |
| 2209 | // parameter. For each kind of template parameter, we substitute the |
| 2210 | // template arguments provided thus far and any "outer" template arguments |
| 2211 | // (when the template parameter was part of a nested template) into |
| 2212 | // the default argument. |
| 2213 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*Param)) { |
| 2214 | if (!TTP->hasDefaultArgument()) { |
| 2215 | assert((Invalid || PartialTemplateArgs) && "Missing default argument"); |
| 2216 | break; |
| 2217 | } |
| 2218 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2219 | TypeSourceInfo *ArgType = SubstDefaultTemplateArgument(*this, |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2220 | Template, |
| 2221 | TemplateLoc, |
| 2222 | RAngleLoc, |
| 2223 | TTP, |
| 2224 | Converted); |
| 2225 | if (!ArgType) |
| 2226 | return true; |
| 2227 | |
| 2228 | Arg = TemplateArgumentLoc(TemplateArgument(ArgType->getType()), |
| 2229 | ArgType); |
| 2230 | } else if (NonTypeTemplateParmDecl *NTTP |
| 2231 | = dyn_cast<NonTypeTemplateParmDecl>(*Param)) { |
| 2232 | if (!NTTP->hasDefaultArgument()) { |
| 2233 | assert((Invalid || PartialTemplateArgs) && "Missing default argument"); |
| 2234 | break; |
| 2235 | } |
| 2236 | |
| 2237 | Sema::OwningExprResult E = SubstDefaultTemplateArgument(*this, Template, |
| 2238 | TemplateLoc, |
| 2239 | RAngleLoc, |
| 2240 | NTTP, |
| 2241 | Converted); |
| 2242 | if (E.isInvalid()) |
| 2243 | return true; |
| 2244 | |
| 2245 | Expr *Ex = E.takeAs<Expr>(); |
| 2246 | Arg = TemplateArgumentLoc(TemplateArgument(Ex), Ex); |
| 2247 | } else { |
| 2248 | TemplateTemplateParmDecl *TempParm |
| 2249 | = cast<TemplateTemplateParmDecl>(*Param); |
| 2250 | |
| 2251 | if (!TempParm->hasDefaultArgument()) { |
| 2252 | assert((Invalid || PartialTemplateArgs) && "Missing default argument"); |
| 2253 | break; |
| 2254 | } |
| 2255 | |
| 2256 | TemplateName Name = SubstDefaultTemplateArgument(*this, Template, |
| 2257 | TemplateLoc, |
| 2258 | RAngleLoc, |
| 2259 | TempParm, |
| 2260 | Converted); |
| 2261 | if (Name.isNull()) |
| 2262 | return true; |
| 2263 | |
| 2264 | Arg = TemplateArgumentLoc(TemplateArgument(Name), |
| 2265 | TempParm->getDefaultArgument().getTemplateQualifierRange(), |
| 2266 | TempParm->getDefaultArgument().getTemplateNameLoc()); |
| 2267 | } |
| 2268 | |
| 2269 | // Introduce an instantiation record that describes where we are using |
| 2270 | // the default template argument. |
| 2271 | InstantiatingTemplate Instantiating(*this, RAngleLoc, Template, *Param, |
| 2272 | Converted.getFlatArguments(), |
| 2273 | Converted.flatSize(), |
| 2274 | SourceRange(TemplateLoc, RAngleLoc)); |
| 2275 | |
| 2276 | // Check the default template argument. |
Douglas Gregor | d9e1530 | 2009-11-11 19:41:09 +0000 | [diff] [blame] | 2277 | if (CheckTemplateArgument(*Param, Arg, Template, TemplateLoc, |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2278 | RAngleLoc, Converted)) |
| 2279 | return true; |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2280 | } |
| 2281 | |
| 2282 | return Invalid; |
| 2283 | } |
| 2284 | |
| 2285 | /// \brief Check a template argument against its corresponding |
| 2286 | /// template type parameter. |
| 2287 | /// |
| 2288 | /// This routine implements the semantics of C++ [temp.arg.type]. It |
| 2289 | /// returns true if an error occurred, and false otherwise. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2290 | bool Sema::CheckTemplateArgument(TemplateTypeParmDecl *Param, |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2291 | TypeSourceInfo *ArgInfo) { |
| 2292 | assert(ArgInfo && "invalid TypeSourceInfo"); |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2293 | QualType Arg = ArgInfo->getType(); |
| 2294 | |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2295 | // C++ [temp.arg.type]p2: |
| 2296 | // A local type, a type with no linkage, an unnamed type or a type |
| 2297 | // compounded from any of these types shall not be used as a |
| 2298 | // template-argument for a template type-parameter. |
| 2299 | // |
| 2300 | // FIXME: Perform the recursive and no-linkage type checks. |
| 2301 | const TagType *Tag = 0; |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2302 | if (const EnumType *EnumT = Arg->getAs<EnumType>()) |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2303 | Tag = EnumT; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2304 | else if (const RecordType *RecordT = Arg->getAs<RecordType>()) |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2305 | Tag = RecordT; |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2306 | if (Tag && Tag->getDecl()->getDeclContext()->isFunctionOrMethod()) { |
| 2307 | SourceRange SR = ArgInfo->getTypeLoc().getFullSourceRange(); |
| 2308 | return Diag(SR.getBegin(), diag::err_template_arg_local_type) |
| 2309 | << QualType(Tag, 0) << SR; |
| 2310 | } else if (Tag && !Tag->getDecl()->getDeclName() && |
Douglas Gregor | 9813753 | 2009-03-10 18:33:27 +0000 | [diff] [blame] | 2311 | !Tag->getDecl()->getTypedefForAnonDecl()) { |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2312 | SourceRange SR = ArgInfo->getTypeLoc().getFullSourceRange(); |
| 2313 | Diag(SR.getBegin(), diag::err_template_arg_unnamed_type) << SR; |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2314 | Diag(Tag->getDecl()->getLocation(), diag::note_template_unnamed_type_here); |
| 2315 | return true; |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 2316 | } else if (Context.hasSameUnqualifiedType(Arg, Context.OverloadTy)) { |
| 2317 | SourceRange SR = ArgInfo->getTypeLoc().getFullSourceRange(); |
| 2318 | return Diag(SR.getBegin(), diag::err_template_arg_overload_type) << SR; |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2319 | } |
| 2320 | |
| 2321 | return false; |
| 2322 | } |
| 2323 | |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2324 | /// \brief Checks whether the given template argument is the address |
| 2325 | /// of an object or function according to C++ [temp.arg.nontype]p1. |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2326 | static bool |
| 2327 | CheckTemplateArgumentAddressOfObjectOrFunction(Sema &S, |
| 2328 | NonTypeTemplateParmDecl *Param, |
| 2329 | QualType ParamType, |
| 2330 | Expr *ArgIn, |
| 2331 | TemplateArgument &Converted) { |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2332 | bool Invalid = false; |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2333 | Expr *Arg = ArgIn; |
| 2334 | QualType ArgType = Arg->getType(); |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2335 | |
| 2336 | // See through any implicit casts we added to fix the type. |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 2337 | while (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(Arg)) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2338 | Arg = Cast->getSubExpr(); |
| 2339 | |
| 2340 | // C++ [temp.arg.nontype]p1: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2341 | // |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2342 | // A template-argument for a non-type, non-template |
| 2343 | // template-parameter shall be one of: [...] |
| 2344 | // |
| 2345 | // -- the address of an object or function with external |
| 2346 | // linkage, including function templates and function |
| 2347 | // template-ids but excluding non-static class members, |
| 2348 | // expressed as & id-expression where the & is optional if |
| 2349 | // the name refers to a function or array, or if the |
| 2350 | // corresponding template-parameter is a reference; or |
| 2351 | DeclRefExpr *DRE = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2352 | |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2353 | // Ignore (and complain about) any excess parentheses. |
| 2354 | while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) { |
| 2355 | if (!Invalid) { |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2356 | S.Diag(Arg->getSourceRange().getBegin(), |
| 2357 | diag::err_template_arg_extra_parens) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2358 | << Arg->getSourceRange(); |
| 2359 | Invalid = true; |
| 2360 | } |
| 2361 | |
| 2362 | Arg = Parens->getSubExpr(); |
| 2363 | } |
| 2364 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2365 | bool AddressTaken = false; |
| 2366 | SourceLocation AddrOpLoc; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2367 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) { |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2368 | if (UnOp->getOpcode() == UnaryOperator::AddrOf) { |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2369 | DRE = dyn_cast<DeclRefExpr>(UnOp->getSubExpr()); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2370 | AddressTaken = true; |
| 2371 | AddrOpLoc = UnOp->getOperatorLoc(); |
| 2372 | } |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2373 | } else |
| 2374 | DRE = dyn_cast<DeclRefExpr>(Arg); |
| 2375 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2376 | if (!DRE) { |
Douglas Gregor | 1a8cf73 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 2377 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_decl_ref) |
| 2378 | << Arg->getSourceRange(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2379 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 2380 | return true; |
| 2381 | } |
Chandler Carruth | 038cc39 | 2010-01-31 10:01:20 +0000 | [diff] [blame] | 2382 | |
| 2383 | // Stop checking the precise nature of the argument if it is value dependent, |
| 2384 | // it should be checked when instantiated. |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2385 | if (Arg->isValueDependent()) { |
| 2386 | Converted = TemplateArgument(ArgIn->Retain()); |
Chandler Carruth | 038cc39 | 2010-01-31 10:01:20 +0000 | [diff] [blame] | 2387 | return false; |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2388 | } |
Chandler Carruth | 038cc39 | 2010-01-31 10:01:20 +0000 | [diff] [blame] | 2389 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2390 | if (!isa<ValueDecl>(DRE->getDecl())) { |
| 2391 | S.Diag(Arg->getSourceRange().getBegin(), |
| 2392 | diag::err_template_arg_not_object_or_func_form) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2393 | << Arg->getSourceRange(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2394 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 2395 | return true; |
| 2396 | } |
| 2397 | |
| 2398 | NamedDecl *Entity = 0; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2399 | |
| 2400 | // Cannot refer to non-static data members |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2401 | if (FieldDecl *Field = dyn_cast<FieldDecl>(DRE->getDecl())) { |
| 2402 | S.Diag(Arg->getSourceRange().getBegin(), diag::err_template_arg_field) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2403 | << Field << Arg->getSourceRange(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2404 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 2405 | return true; |
| 2406 | } |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2407 | |
| 2408 | // Cannot refer to non-static member functions |
| 2409 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(DRE->getDecl())) |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2410 | if (!Method->isStatic()) { |
| 2411 | S.Diag(Arg->getSourceRange().getBegin(), diag::err_template_arg_method) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2412 | << Method << Arg->getSourceRange(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2413 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 2414 | return true; |
| 2415 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2416 | |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2417 | // Functions must have external linkage. |
| 2418 | if (FunctionDecl *Func = dyn_cast<FunctionDecl>(DRE->getDecl())) { |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 2419 | if (!isExternalLinkage(Func->getLinkage())) { |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2420 | S.Diag(Arg->getSourceRange().getBegin(), |
| 2421 | diag::err_template_arg_function_not_extern) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2422 | << Func << Arg->getSourceRange(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2423 | S.Diag(Func->getLocation(), diag::note_template_arg_internal_object) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2424 | << true; |
| 2425 | return true; |
| 2426 | } |
| 2427 | |
| 2428 | // Okay: we've named a function with external linkage. |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 2429 | Entity = Func; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2430 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2431 | // If the template parameter has pointer type, the function decays. |
| 2432 | if (ParamType->isPointerType() && !AddressTaken) |
| 2433 | ArgType = S.Context.getPointerType(Func->getType()); |
| 2434 | else if (AddressTaken && ParamType->isReferenceType()) { |
| 2435 | // If we originally had an address-of operator, but the |
| 2436 | // parameter has reference type, complain and (if things look |
| 2437 | // like they will work) drop the address-of operator. |
| 2438 | if (!S.Context.hasSameUnqualifiedType(Func->getType(), |
| 2439 | ParamType.getNonReferenceType())) { |
| 2440 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 2441 | << ParamType; |
| 2442 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 2443 | return true; |
| 2444 | } |
| 2445 | |
| 2446 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 2447 | << ParamType |
| 2448 | << FixItHint::CreateRemoval(AddrOpLoc); |
| 2449 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 2450 | |
| 2451 | ArgType = Func->getType(); |
| 2452 | } |
| 2453 | } else if (VarDecl *Var = dyn_cast<VarDecl>(DRE->getDecl())) { |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 2454 | if (!isExternalLinkage(Var->getLinkage())) { |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2455 | S.Diag(Arg->getSourceRange().getBegin(), |
| 2456 | diag::err_template_arg_object_not_extern) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2457 | << Var << Arg->getSourceRange(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2458 | S.Diag(Var->getLocation(), diag::note_template_arg_internal_object) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2459 | << true; |
| 2460 | return true; |
| 2461 | } |
| 2462 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2463 | // A value of reference type is not an object. |
| 2464 | if (Var->getType()->isReferenceType()) { |
| 2465 | S.Diag(Arg->getSourceRange().getBegin(), |
| 2466 | diag::err_template_arg_reference_var) |
| 2467 | << Var->getType() << Arg->getSourceRange(); |
| 2468 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 2469 | return true; |
| 2470 | } |
| 2471 | |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2472 | // Okay: we've named an object with external linkage |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 2473 | Entity = Var; |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2474 | |
| 2475 | // If the template parameter has pointer type, we must have taken |
| 2476 | // the address of this object. |
| 2477 | if (ParamType->isReferenceType()) { |
| 2478 | if (AddressTaken) { |
| 2479 | // If we originally had an address-of operator, but the |
| 2480 | // parameter has reference type, complain and (if things look |
| 2481 | // like they will work) drop the address-of operator. |
| 2482 | if (!S.Context.hasSameUnqualifiedType(Var->getType(), |
| 2483 | ParamType.getNonReferenceType())) { |
| 2484 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 2485 | << ParamType; |
| 2486 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 2487 | return true; |
| 2488 | } |
| 2489 | |
| 2490 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 2491 | << ParamType |
| 2492 | << FixItHint::CreateRemoval(AddrOpLoc); |
| 2493 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 2494 | |
| 2495 | ArgType = Var->getType(); |
| 2496 | } |
| 2497 | } else if (!AddressTaken && ParamType->isPointerType()) { |
| 2498 | if (Var->getType()->isArrayType()) { |
| 2499 | // Array-to-pointer decay. |
| 2500 | ArgType = S.Context.getArrayDecayedType(Var->getType()); |
| 2501 | } else { |
| 2502 | // If the template parameter has pointer type but the address of |
| 2503 | // this object was not taken, complain and (possibly) recover by |
| 2504 | // taking the address of the entity. |
| 2505 | ArgType = S.Context.getPointerType(Var->getType()); |
| 2506 | if (!S.Context.hasSameUnqualifiedType(ArgType, ParamType)) { |
| 2507 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_address_of) |
| 2508 | << ParamType; |
| 2509 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 2510 | return true; |
| 2511 | } |
| 2512 | |
| 2513 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_address_of) |
| 2514 | << ParamType |
| 2515 | << FixItHint::CreateInsertion(Arg->getLocStart(), "&"); |
| 2516 | |
| 2517 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 2518 | } |
| 2519 | } |
| 2520 | } else { |
| 2521 | // We found something else, but we don't know specifically what it is. |
| 2522 | S.Diag(Arg->getSourceRange().getBegin(), |
| 2523 | diag::err_template_arg_not_object_or_func) |
| 2524 | << Arg->getSourceRange(); |
| 2525 | S.Diag(DRE->getDecl()->getLocation(), diag::note_template_arg_refers_here); |
| 2526 | return true; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2527 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2528 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2529 | if (ParamType->isPointerType() && |
| 2530 | !ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType() && |
| 2531 | S.IsQualificationConversion(ArgType, ParamType)) { |
| 2532 | // For pointer-to-object types, qualification conversions are |
| 2533 | // permitted. |
| 2534 | } else { |
| 2535 | if (const ReferenceType *ParamRef = ParamType->getAs<ReferenceType>()) { |
| 2536 | if (!ParamRef->getPointeeType()->isFunctionType()) { |
| 2537 | // C++ [temp.arg.nontype]p5b3: |
| 2538 | // For a non-type template-parameter of type reference to |
| 2539 | // object, no conversions apply. The type referred to by the |
| 2540 | // reference may be more cv-qualified than the (otherwise |
| 2541 | // identical) type of the template- argument. The |
| 2542 | // template-parameter is bound directly to the |
| 2543 | // template-argument, which shall be an lvalue. |
| 2544 | |
| 2545 | // FIXME: Other qualifiers? |
| 2546 | unsigned ParamQuals = ParamRef->getPointeeType().getCVRQualifiers(); |
| 2547 | unsigned ArgQuals = ArgType.getCVRQualifiers(); |
| 2548 | |
| 2549 | if ((ParamQuals | ArgQuals) != ParamQuals) { |
| 2550 | S.Diag(Arg->getSourceRange().getBegin(), |
| 2551 | diag::err_template_arg_ref_bind_ignores_quals) |
| 2552 | << ParamType << Arg->getType() |
| 2553 | << Arg->getSourceRange(); |
| 2554 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 2555 | return true; |
| 2556 | } |
| 2557 | } |
| 2558 | } |
| 2559 | |
| 2560 | // At this point, the template argument refers to an object or |
| 2561 | // function with external linkage. We now need to check whether the |
| 2562 | // argument and parameter types are compatible. |
| 2563 | if (!S.Context.hasSameUnqualifiedType(ArgType, |
| 2564 | ParamType.getNonReferenceType())) { |
| 2565 | // We can't perform this conversion or binding. |
| 2566 | if (ParamType->isReferenceType()) |
| 2567 | S.Diag(Arg->getLocStart(), diag::err_template_arg_no_ref_bind) |
| 2568 | << ParamType << Arg->getType() << Arg->getSourceRange(); |
| 2569 | else |
| 2570 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_convertible) |
| 2571 | << Arg->getType() << ParamType << Arg->getSourceRange(); |
| 2572 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 2573 | return true; |
| 2574 | } |
| 2575 | } |
| 2576 | |
| 2577 | // Create the template argument. |
| 2578 | Converted = TemplateArgument(Entity->getCanonicalDecl()); |
| 2579 | return false; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2580 | } |
| 2581 | |
| 2582 | /// \brief Checks whether the given template argument is a pointer to |
| 2583 | /// member constant according to C++ [temp.arg.nontype]p1. |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 2584 | bool Sema::CheckTemplateArgumentPointerToMember(Expr *Arg, |
| 2585 | TemplateArgument &Converted) { |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2586 | bool Invalid = false; |
| 2587 | |
| 2588 | // See through any implicit casts we added to fix the type. |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 2589 | while (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(Arg)) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2590 | Arg = Cast->getSubExpr(); |
| 2591 | |
| 2592 | // C++ [temp.arg.nontype]p1: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2593 | // |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2594 | // A template-argument for a non-type, non-template |
| 2595 | // template-parameter shall be one of: [...] |
| 2596 | // |
| 2597 | // -- a pointer to member expressed as described in 5.3.1. |
Douglas Gregor | a2813ce | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 2598 | DeclRefExpr *DRE = 0; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2599 | |
| 2600 | // Ignore (and complain about) any excess parentheses. |
| 2601 | while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) { |
| 2602 | if (!Invalid) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2603 | Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2604 | diag::err_template_arg_extra_parens) |
| 2605 | << Arg->getSourceRange(); |
| 2606 | Invalid = true; |
| 2607 | } |
| 2608 | |
| 2609 | Arg = Parens->getSubExpr(); |
| 2610 | } |
| 2611 | |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 2612 | // A pointer-to-member constant written &Class::member. |
| 2613 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) { |
Douglas Gregor | a2813ce | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 2614 | if (UnOp->getOpcode() == UnaryOperator::AddrOf) { |
| 2615 | DRE = dyn_cast<DeclRefExpr>(UnOp->getSubExpr()); |
| 2616 | if (DRE && !DRE->getQualifier()) |
| 2617 | DRE = 0; |
| 2618 | } |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 2619 | } |
| 2620 | // A constant of pointer-to-member type. |
| 2621 | else if ((DRE = dyn_cast<DeclRefExpr>(Arg))) { |
| 2622 | if (ValueDecl *VD = dyn_cast<ValueDecl>(DRE->getDecl())) { |
| 2623 | if (VD->getType()->isMemberPointerType()) { |
| 2624 | if (isa<NonTypeTemplateParmDecl>(VD) || |
| 2625 | (isa<VarDecl>(VD) && |
| 2626 | Context.getCanonicalType(VD->getType()).isConstQualified())) { |
| 2627 | if (Arg->isTypeDependent() || Arg->isValueDependent()) |
| 2628 | Converted = TemplateArgument(Arg->Retain()); |
| 2629 | else |
| 2630 | Converted = TemplateArgument(VD->getCanonicalDecl()); |
| 2631 | return Invalid; |
| 2632 | } |
| 2633 | } |
| 2634 | } |
| 2635 | |
| 2636 | DRE = 0; |
| 2637 | } |
| 2638 | |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2639 | if (!DRE) |
| 2640 | return Diag(Arg->getSourceRange().getBegin(), |
| 2641 | diag::err_template_arg_not_pointer_to_member_form) |
| 2642 | << Arg->getSourceRange(); |
| 2643 | |
| 2644 | if (isa<FieldDecl>(DRE->getDecl()) || isa<CXXMethodDecl>(DRE->getDecl())) { |
| 2645 | assert((isa<FieldDecl>(DRE->getDecl()) || |
| 2646 | !cast<CXXMethodDecl>(DRE->getDecl())->isStatic()) && |
| 2647 | "Only non-static member pointers can make it here"); |
| 2648 | |
| 2649 | // Okay: this is the address of a non-static member, and therefore |
| 2650 | // a member pointer constant. |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 2651 | if (Arg->isTypeDependent() || Arg->isValueDependent()) |
| 2652 | Converted = TemplateArgument(Arg->Retain()); |
| 2653 | else |
| 2654 | Converted = TemplateArgument(DRE->getDecl()->getCanonicalDecl()); |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2655 | return Invalid; |
| 2656 | } |
| 2657 | |
| 2658 | // 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] | 2659 | Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2660 | diag::err_template_arg_not_pointer_to_member_form) |
| 2661 | << Arg->getSourceRange(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2662 | Diag(DRE->getDecl()->getLocation(), |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2663 | diag::note_template_arg_refers_here); |
| 2664 | return true; |
| 2665 | } |
| 2666 | |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2667 | /// \brief Check a template argument against its corresponding |
| 2668 | /// non-type template parameter. |
| 2669 | /// |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 2670 | /// This routine implements the semantics of C++ [temp.arg.nontype]. |
| 2671 | /// It returns true if an error occurred, and false otherwise. \p |
| 2672 | /// InstantiatedParamType is the type of the non-type template |
| 2673 | /// parameter after it has been instantiated. |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 2674 | /// |
Douglas Gregor | 02cbbd2 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 2675 | /// If no error was detected, Converted receives the converted template argument. |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2676 | bool Sema::CheckTemplateArgument(NonTypeTemplateParmDecl *Param, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2677 | QualType InstantiatedParamType, Expr *&Arg, |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 2678 | TemplateArgument &Converted, |
| 2679 | CheckTemplateArgumentKind CTAK) { |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2680 | SourceLocation StartLoc = Arg->getSourceRange().getBegin(); |
| 2681 | |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 2682 | // If either the parameter has a dependent type or the argument is |
| 2683 | // type-dependent, there's nothing we can check now. |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2684 | if (InstantiatedParamType->isDependentType() || Arg->isTypeDependent()) { |
| 2685 | // FIXME: Produce a cloned, canonical expression? |
Douglas Gregor | 02cbbd2 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 2686 | Converted = TemplateArgument(Arg); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 2687 | return false; |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2688 | } |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 2689 | |
| 2690 | // C++ [temp.arg.nontype]p5: |
| 2691 | // The following conversions are performed on each expression used |
| 2692 | // as a non-type template-argument. If a non-type |
| 2693 | // template-argument cannot be converted to the type of the |
| 2694 | // corresponding template-parameter then the program is |
| 2695 | // ill-formed. |
| 2696 | // |
| 2697 | // -- for a non-type template-parameter of integral or |
| 2698 | // enumeration type, integral promotions (4.5) and integral |
| 2699 | // conversions (4.7) are applied. |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 2700 | QualType ParamType = InstantiatedParamType; |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 2701 | QualType ArgType = Arg->getType(); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 2702 | if (ParamType->isIntegralType() || ParamType->isEnumeralType()) { |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 2703 | // C++ [temp.arg.nontype]p1: |
| 2704 | // A template-argument for a non-type, non-template |
| 2705 | // template-parameter shall be one of: |
| 2706 | // |
| 2707 | // -- an integral constant-expression of integral or enumeration |
| 2708 | // type; or |
| 2709 | // -- the name of a non-type template-parameter; or |
| 2710 | SourceLocation NonConstantLoc; |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 2711 | llvm::APSInt Value; |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 2712 | if (!ArgType->isIntegralType() && !ArgType->isEnumeralType()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2713 | Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 2714 | diag::err_template_arg_not_integral_or_enumeral) |
| 2715 | << ArgType << Arg->getSourceRange(); |
| 2716 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 2717 | return true; |
| 2718 | } else if (!Arg->isValueDependent() && |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 2719 | !Arg->isIntegerConstantExpr(Value, Context, &NonConstantLoc)) { |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 2720 | Diag(NonConstantLoc, diag::err_template_arg_not_ice) |
| 2721 | << ArgType << Arg->getSourceRange(); |
| 2722 | return true; |
| 2723 | } |
| 2724 | |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 2725 | // From here on out, all we care about are the unqualified forms |
| 2726 | // of the parameter and argument types. |
| 2727 | ParamType = ParamType.getUnqualifiedType(); |
| 2728 | ArgType = ArgType.getUnqualifiedType(); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 2729 | |
| 2730 | // Try to convert the argument to the parameter's type. |
Douglas Gregor | ff52439 | 2009-11-04 21:50:46 +0000 | [diff] [blame] | 2731 | if (Context.hasSameType(ParamType, ArgType)) { |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 2732 | // Okay: no conversion necessary |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 2733 | } else if (CTAK == CTAK_Deduced) { |
| 2734 | // C++ [temp.deduct.type]p17: |
| 2735 | // If, in the declaration of a function template with a non-type |
| 2736 | // template-parameter, the non-type template- parameter is used |
| 2737 | // in an expression in the function parameter-list and, if the |
| 2738 | // corresponding template-argument is deduced, the |
| 2739 | // template-argument type shall match the type of the |
| 2740 | // template-parameter exactly, except that a template-argument |
| 2741 | // deduced from an array bound may be of any integral type. |
| 2742 | Diag(StartLoc, diag::err_deduced_non_type_template_arg_type_mismatch) |
| 2743 | << ArgType << ParamType; |
| 2744 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 2745 | return true; |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 2746 | } else if (IsIntegralPromotion(Arg, ArgType, ParamType) || |
| 2747 | !ParamType->isEnumeralType()) { |
| 2748 | // This is an integral promotion or conversion. |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 2749 | ImpCastExprToType(Arg, ParamType, CastExpr::CK_IntegralCast); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 2750 | } else { |
| 2751 | // We can't perform this conversion. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2752 | Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 2753 | diag::err_template_arg_not_convertible) |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 2754 | << Arg->getType() << InstantiatedParamType << Arg->getSourceRange(); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 2755 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 2756 | return true; |
| 2757 | } |
| 2758 | |
Douglas Gregor | f80a9d5 | 2009-03-14 00:20:21 +0000 | [diff] [blame] | 2759 | QualType IntegerType = Context.getCanonicalType(ParamType); |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2760 | if (const EnumType *Enum = IntegerType->getAs<EnumType>()) |
Douglas Gregor | 02cbbd2 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 2761 | IntegerType = Context.getCanonicalType(Enum->getDecl()->getIntegerType()); |
Douglas Gregor | f80a9d5 | 2009-03-14 00:20:21 +0000 | [diff] [blame] | 2762 | |
| 2763 | if (!Arg->isValueDependent()) { |
Douglas Gregor | 1a6e034 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 2764 | llvm::APSInt OldValue = Value; |
| 2765 | |
| 2766 | // Coerce the template argument's value to the value it will have |
| 2767 | // based on the template parameter's type. |
Douglas Gregor | 0d4fd8e | 2010-03-26 00:39:40 +0000 | [diff] [blame] | 2768 | unsigned AllowedBits = Context.getTypeSize(IntegerType); |
Douglas Gregor | 0d4fd8e | 2010-03-26 00:39:40 +0000 | [diff] [blame] | 2769 | if (Value.getBitWidth() != AllowedBits) |
| 2770 | Value.extOrTrunc(AllowedBits); |
| 2771 | Value.setIsSigned(IntegerType->isSignedIntegerType()); |
Douglas Gregor | 1a6e034 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 2772 | |
| 2773 | // Complain if an unsigned parameter received a negative value. |
| 2774 | if (IntegerType->isUnsignedIntegerType() |
| 2775 | && (OldValue.isSigned() && OldValue.isNegative())) { |
| 2776 | Diag(Arg->getSourceRange().getBegin(), diag::warn_template_arg_negative) |
| 2777 | << OldValue.toString(10) << Value.toString(10) << Param->getType() |
| 2778 | << Arg->getSourceRange(); |
| 2779 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 2780 | } |
| 2781 | |
| 2782 | // Complain if we overflowed the template parameter's type. |
| 2783 | unsigned RequiredBits; |
| 2784 | if (IntegerType->isUnsignedIntegerType()) |
| 2785 | RequiredBits = OldValue.getActiveBits(); |
| 2786 | else if (OldValue.isUnsigned()) |
| 2787 | RequiredBits = OldValue.getActiveBits() + 1; |
| 2788 | else |
| 2789 | RequiredBits = OldValue.getMinSignedBits(); |
| 2790 | if (RequiredBits > AllowedBits) { |
| 2791 | Diag(Arg->getSourceRange().getBegin(), |
| 2792 | diag::warn_template_arg_too_large) |
| 2793 | << OldValue.toString(10) << Value.toString(10) << Param->getType() |
| 2794 | << Arg->getSourceRange(); |
| 2795 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 2796 | } |
Douglas Gregor | f80a9d5 | 2009-03-14 00:20:21 +0000 | [diff] [blame] | 2797 | } |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 2798 | |
Douglas Gregor | 02cbbd2 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 2799 | // Add the value of this argument to the list of converted |
| 2800 | // arguments. We use the bitwidth and signedness of the template |
| 2801 | // parameter. |
| 2802 | if (Arg->isValueDependent()) { |
| 2803 | // The argument is value-dependent. Create a new |
| 2804 | // TemplateArgument with the converted expression. |
| 2805 | Converted = TemplateArgument(Arg); |
| 2806 | return false; |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 2807 | } |
| 2808 | |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2809 | Converted = TemplateArgument(Value, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2810 | ParamType->isEnumeralType() ? ParamType |
Douglas Gregor | 02cbbd2 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 2811 | : IntegerType); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 2812 | return false; |
| 2813 | } |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 2814 | |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2815 | DeclAccessPair FoundResult; // temporary for ResolveOverloadedFunction |
| 2816 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2817 | // C++0x [temp.arg.nontype]p5 bullets 2, 4 and 6 permit conversion |
| 2818 | // from a template argument of type std::nullptr_t to a non-type |
| 2819 | // template parameter of type pointer to object, pointer to |
| 2820 | // function, or pointer-to-member, respectively. |
| 2821 | if (ArgType->isNullPtrType() && |
| 2822 | (ParamType->isPointerType() || ParamType->isMemberPointerType())) { |
| 2823 | Converted = TemplateArgument((NamedDecl *)0); |
| 2824 | return false; |
| 2825 | } |
| 2826 | |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 2827 | // Handle pointer-to-function, reference-to-function, and |
| 2828 | // pointer-to-member-function all in (roughly) the same way. |
| 2829 | if (// -- For a non-type template-parameter of type pointer to |
| 2830 | // function, only the function-to-pointer conversion (4.3) is |
| 2831 | // applied. If the template-argument represents a set of |
| 2832 | // overloaded functions (or a pointer to such), the matching |
| 2833 | // function is selected from the set (13.4). |
| 2834 | (ParamType->isPointerType() && |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2835 | ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType()) || |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 2836 | // -- For a non-type template-parameter of type reference to |
| 2837 | // function, no conversions apply. If the template-argument |
| 2838 | // represents a set of overloaded functions, the matching |
| 2839 | // function is selected from the set (13.4). |
| 2840 | (ParamType->isReferenceType() && |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2841 | ParamType->getAs<ReferenceType>()->getPointeeType()->isFunctionType()) || |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 2842 | // -- For a non-type template-parameter of type pointer to |
| 2843 | // member function, no conversions apply. If the |
| 2844 | // template-argument represents a set of overloaded member |
| 2845 | // functions, the matching member function is selected from |
| 2846 | // the set (13.4). |
| 2847 | (ParamType->isMemberPointerType() && |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2848 | ParamType->getAs<MemberPointerType>()->getPointeeType() |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 2849 | ->isFunctionType())) { |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2850 | |
Douglas Gregor | 1a8cf73 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 2851 | if (Arg->getType() == Context.OverloadTy) { |
| 2852 | if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg, ParamType, |
| 2853 | true, |
| 2854 | FoundResult)) { |
| 2855 | if (DiagnoseUseOfDecl(Fn, Arg->getSourceRange().getBegin())) |
| 2856 | return true; |
| 2857 | |
| 2858 | Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn); |
| 2859 | ArgType = Arg->getType(); |
| 2860 | } else |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 2861 | return true; |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 2862 | } |
Douglas Gregor | 1a8cf73 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 2863 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2864 | if (!ParamType->isMemberPointerType()) |
| 2865 | return CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param, |
| 2866 | ParamType, |
| 2867 | Arg, Converted); |
| 2868 | |
| 2869 | if (IsQualificationConversion(ArgType, ParamType.getNonReferenceType())) { |
| 2870 | ImpCastExprToType(Arg, ParamType, CastExpr::CK_NoOp, |
| 2871 | Arg->isLvalue(Context) == Expr::LV_Valid); |
| 2872 | } else if (!Context.hasSameUnqualifiedType(ArgType, |
| 2873 | ParamType.getNonReferenceType())) { |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 2874 | // We can't perform this conversion. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2875 | Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 2876 | diag::err_template_arg_not_convertible) |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 2877 | << Arg->getType() << InstantiatedParamType << Arg->getSourceRange(); |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 2878 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 2879 | return true; |
| 2880 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2881 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2882 | return CheckTemplateArgumentPointerToMember(Arg, Converted); |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 2883 | } |
| 2884 | |
Chris Lattner | fe90de7 | 2009-02-20 21:37:53 +0000 | [diff] [blame] | 2885 | if (ParamType->isPointerType()) { |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 2886 | // -- for a non-type template-parameter of type pointer to |
| 2887 | // object, qualification conversions (4.4) and the |
| 2888 | // array-to-pointer conversion (4.2) are applied. |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 2889 | // C++0x also allows a value of std::nullptr_t. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2890 | assert(ParamType->getAs<PointerType>()->getPointeeType()->isObjectType() && |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 2891 | "Only object pointers allowed here"); |
Douglas Gregor | f684e6e | 2009-02-11 00:44:29 +0000 | [diff] [blame] | 2892 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2893 | return CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param, |
| 2894 | ParamType, |
| 2895 | Arg, Converted); |
Douglas Gregor | f684e6e | 2009-02-11 00:44:29 +0000 | [diff] [blame] | 2896 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2897 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2898 | if (const ReferenceType *ParamRefType = ParamType->getAs<ReferenceType>()) { |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 2899 | // -- For a non-type template-parameter of type reference to |
| 2900 | // object, no conversions apply. The type referred to by the |
| 2901 | // reference may be more cv-qualified than the (otherwise |
| 2902 | // identical) type of the template-argument. The |
| 2903 | // template-parameter is bound directly to the |
| 2904 | // template-argument, which must be an lvalue. |
Douglas Gregor | bad0e65 | 2009-03-24 20:32:41 +0000 | [diff] [blame] | 2905 | assert(ParamRefType->getPointeeType()->isObjectType() && |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 2906 | "Only object references allowed here"); |
Douglas Gregor | f684e6e | 2009-02-11 00:44:29 +0000 | [diff] [blame] | 2907 | |
Douglas Gregor | 1a8cf73 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 2908 | if (Arg->getType() == Context.OverloadTy) { |
| 2909 | if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg, |
| 2910 | ParamRefType->getPointeeType(), |
| 2911 | true, |
| 2912 | FoundResult)) { |
| 2913 | if (DiagnoseUseOfDecl(Fn, Arg->getSourceRange().getBegin())) |
| 2914 | return true; |
| 2915 | |
| 2916 | Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn); |
| 2917 | ArgType = Arg->getType(); |
| 2918 | } else |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2919 | return true; |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 2920 | } |
Douglas Gregor | 1a8cf73 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 2921 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2922 | return CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param, |
| 2923 | ParamType, |
| 2924 | Arg, Converted); |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 2925 | } |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 2926 | |
| 2927 | // -- For a non-type template-parameter of type pointer to data |
| 2928 | // member, qualification conversions (4.4) are applied. |
| 2929 | assert(ParamType->isMemberPointerType() && "Only pointers to members remain"); |
| 2930 | |
Douglas Gregor | 8e6563b | 2009-02-11 18:22:40 +0000 | [diff] [blame] | 2931 | if (Context.hasSameUnqualifiedType(ParamType, ArgType)) { |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 2932 | // Types match exactly: nothing more to do here. |
| 2933 | } else if (IsQualificationConversion(ArgType, ParamType)) { |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2934 | ImpCastExprToType(Arg, ParamType, CastExpr::CK_NoOp, |
| 2935 | Arg->isLvalue(Context) == Expr::LV_Valid); |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 2936 | } else { |
| 2937 | // We can't perform this conversion. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2938 | Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 2939 | diag::err_template_arg_not_convertible) |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 2940 | << Arg->getType() << InstantiatedParamType << Arg->getSourceRange(); |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 2941 | Diag(Param->getLocation(), diag::note_template_param_here); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2942 | return true; |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 2943 | } |
| 2944 | |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 2945 | return CheckTemplateArgumentPointerToMember(Arg, Converted); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2946 | } |
| 2947 | |
| 2948 | /// \brief Check a template argument against its corresponding |
| 2949 | /// template template parameter. |
| 2950 | /// |
| 2951 | /// This routine implements the semantics of C++ [temp.arg.template]. |
| 2952 | /// It returns true if an error occurred, and false otherwise. |
| 2953 | bool Sema::CheckTemplateArgument(TemplateTemplateParmDecl *Param, |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2954 | const TemplateArgumentLoc &Arg) { |
| 2955 | TemplateName Name = Arg.getArgument().getAsTemplate(); |
| 2956 | TemplateDecl *Template = Name.getAsTemplateDecl(); |
| 2957 | if (!Template) { |
| 2958 | // Any dependent template name is fine. |
| 2959 | assert(Name.isDependent() && "Non-dependent template isn't a declaration?"); |
| 2960 | return false; |
| 2961 | } |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 2962 | |
| 2963 | // C++ [temp.arg.template]p1: |
| 2964 | // A template-argument for a template template-parameter shall be |
| 2965 | // the name of a class template, expressed as id-expression. Only |
| 2966 | // primary class templates are considered when matching the |
| 2967 | // template template argument with the corresponding parameter; |
| 2968 | // partial specializations are not considered even if their |
| 2969 | // parameter lists match that of the template template parameter. |
Douglas Gregor | ba1ecb5 | 2009-06-12 19:43:02 +0000 | [diff] [blame] | 2970 | // |
| 2971 | // Note that we also allow template template parameters here, which |
| 2972 | // will happen when we are dealing with, e.g., class template |
| 2973 | // partial specializations. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2974 | if (!isa<ClassTemplateDecl>(Template) && |
Douglas Gregor | ba1ecb5 | 2009-06-12 19:43:02 +0000 | [diff] [blame] | 2975 | !isa<TemplateTemplateParmDecl>(Template)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2976 | assert(isa<FunctionTemplateDecl>(Template) && |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 2977 | "Only function templates are possible here"); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2978 | Diag(Arg.getLocation(), diag::err_template_arg_not_class_template); |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 2979 | Diag(Template->getLocation(), diag::note_template_arg_refers_here_func) |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 2980 | << Template; |
| 2981 | } |
| 2982 | |
| 2983 | return !TemplateParameterListsAreEqual(Template->getTemplateParameters(), |
| 2984 | Param->getTemplateParameters(), |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 2985 | true, |
| 2986 | TPL_TemplateTemplateArgumentMatch, |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2987 | Arg.getLocation()); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2988 | } |
| 2989 | |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 2990 | /// \brief Given a non-type template argument that refers to a |
| 2991 | /// declaration and the type of its corresponding non-type template |
| 2992 | /// parameter, produce an expression that properly refers to that |
| 2993 | /// declaration. |
| 2994 | Sema::OwningExprResult |
| 2995 | Sema::BuildExpressionFromDeclTemplateArgument(const TemplateArgument &Arg, |
| 2996 | QualType ParamType, |
| 2997 | SourceLocation Loc) { |
| 2998 | assert(Arg.getKind() == TemplateArgument::Declaration && |
| 2999 | "Only declaration template arguments permitted here"); |
| 3000 | ValueDecl *VD = cast<ValueDecl>(Arg.getAsDecl()); |
| 3001 | |
| 3002 | if (VD->getDeclContext()->isRecord() && |
| 3003 | (isa<CXXMethodDecl>(VD) || isa<FieldDecl>(VD))) { |
| 3004 | // If the value is a class member, we might have a pointer-to-member. |
| 3005 | // Determine whether the non-type template template parameter is of |
| 3006 | // pointer-to-member type. If so, we need to build an appropriate |
| 3007 | // expression for a pointer-to-member, since a "normal" DeclRefExpr |
| 3008 | // would refer to the member itself. |
| 3009 | if (ParamType->isMemberPointerType()) { |
| 3010 | QualType ClassType |
| 3011 | = Context.getTypeDeclType(cast<RecordDecl>(VD->getDeclContext())); |
| 3012 | NestedNameSpecifier *Qualifier |
| 3013 | = NestedNameSpecifier::Create(Context, 0, false, ClassType.getTypePtr()); |
| 3014 | CXXScopeSpec SS; |
| 3015 | SS.setScopeRep(Qualifier); |
| 3016 | OwningExprResult RefExpr = BuildDeclRefExpr(VD, |
| 3017 | VD->getType().getNonReferenceType(), |
| 3018 | Loc, |
| 3019 | &SS); |
| 3020 | if (RefExpr.isInvalid()) |
| 3021 | return ExprError(); |
| 3022 | |
| 3023 | RefExpr = CreateBuiltinUnaryOp(Loc, UnaryOperator::AddrOf, move(RefExpr)); |
| 3024 | assert(!RefExpr.isInvalid() && |
| 3025 | Context.hasSameType(((Expr*) RefExpr.get())->getType(), |
| 3026 | ParamType)); |
| 3027 | return move(RefExpr); |
| 3028 | } |
| 3029 | } |
| 3030 | |
| 3031 | QualType T = VD->getType().getNonReferenceType(); |
| 3032 | if (ParamType->isPointerType()) { |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3033 | // When the non-type template parameter is a pointer, take the |
| 3034 | // address of the declaration. |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3035 | OwningExprResult RefExpr = BuildDeclRefExpr(VD, T, Loc); |
| 3036 | if (RefExpr.isInvalid()) |
| 3037 | return ExprError(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3038 | |
| 3039 | if (T->isFunctionType() || T->isArrayType()) { |
| 3040 | // Decay functions and arrays. |
| 3041 | Expr *RefE = (Expr *)RefExpr.get(); |
| 3042 | DefaultFunctionArrayConversion(RefE); |
| 3043 | if (RefE != RefExpr.get()) { |
| 3044 | RefExpr.release(); |
| 3045 | RefExpr = Owned(RefE); |
| 3046 | } |
| 3047 | |
| 3048 | return move(RefExpr); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3049 | } |
| 3050 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3051 | // Take the address of everything else |
| 3052 | return CreateBuiltinUnaryOp(Loc, UnaryOperator::AddrOf, move(RefExpr)); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3053 | } |
| 3054 | |
| 3055 | // If the non-type template parameter has reference type, qualify the |
| 3056 | // resulting declaration reference with the extra qualifiers on the |
| 3057 | // type that the reference refers to. |
| 3058 | if (const ReferenceType *TargetRef = ParamType->getAs<ReferenceType>()) |
| 3059 | T = Context.getQualifiedType(T, TargetRef->getPointeeType().getQualifiers()); |
| 3060 | |
| 3061 | return BuildDeclRefExpr(VD, T, Loc); |
| 3062 | } |
| 3063 | |
| 3064 | /// \brief Construct a new expression that refers to the given |
| 3065 | /// integral template argument with the given source-location |
| 3066 | /// information. |
| 3067 | /// |
| 3068 | /// This routine takes care of the mapping from an integral template |
| 3069 | /// argument (which may have any integral type) to the appropriate |
| 3070 | /// literal value. |
| 3071 | Sema::OwningExprResult |
| 3072 | Sema::BuildExpressionFromIntegralTemplateArgument(const TemplateArgument &Arg, |
| 3073 | SourceLocation Loc) { |
| 3074 | assert(Arg.getKind() == TemplateArgument::Integral && |
| 3075 | "Operation is only value for integral template arguments"); |
| 3076 | QualType T = Arg.getIntegralType(); |
| 3077 | if (T->isCharType() || T->isWideCharType()) |
| 3078 | return Owned(new (Context) CharacterLiteral( |
| 3079 | Arg.getAsIntegral()->getZExtValue(), |
| 3080 | T->isWideCharType(), |
| 3081 | T, |
| 3082 | Loc)); |
| 3083 | if (T->isBooleanType()) |
| 3084 | return Owned(new (Context) CXXBoolLiteralExpr( |
| 3085 | Arg.getAsIntegral()->getBoolValue(), |
| 3086 | T, |
| 3087 | Loc)); |
| 3088 | |
| 3089 | return Owned(new (Context) IntegerLiteral(*Arg.getAsIntegral(), T, Loc)); |
| 3090 | } |
| 3091 | |
| 3092 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3093 | /// \brief Determine whether the given template parameter lists are |
| 3094 | /// equivalent. |
| 3095 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3096 | /// \param New The new template parameter list, typically written in the |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3097 | /// source code as part of a new template declaration. |
| 3098 | /// |
| 3099 | /// \param Old The old template parameter list, typically found via |
| 3100 | /// name lookup of the template declared with this template parameter |
| 3101 | /// list. |
| 3102 | /// |
| 3103 | /// \param Complain If true, this routine will produce a diagnostic if |
| 3104 | /// the template parameter lists are not equivalent. |
| 3105 | /// |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 3106 | /// \param Kind describes how we are to match the template parameter lists. |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 3107 | /// |
| 3108 | /// \param TemplateArgLoc If this source location is valid, then we |
| 3109 | /// are actually checking the template parameter list of a template |
| 3110 | /// argument (New) against the template parameter list of its |
| 3111 | /// corresponding template template parameter (Old). We produce |
| 3112 | /// slightly different diagnostics in this scenario. |
| 3113 | /// |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3114 | /// \returns True if the template parameter lists are equal, false |
| 3115 | /// otherwise. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3116 | bool |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3117 | Sema::TemplateParameterListsAreEqual(TemplateParameterList *New, |
| 3118 | TemplateParameterList *Old, |
| 3119 | bool Complain, |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 3120 | TemplateParameterListEqualKind Kind, |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 3121 | SourceLocation TemplateArgLoc) { |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3122 | if (Old->size() != New->size()) { |
| 3123 | if (Complain) { |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 3124 | unsigned NextDiag = diag::err_template_param_list_different_arity; |
| 3125 | if (TemplateArgLoc.isValid()) { |
| 3126 | Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch); |
| 3127 | NextDiag = diag::note_template_param_list_different_arity; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3128 | } |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 3129 | Diag(New->getTemplateLoc(), NextDiag) |
| 3130 | << (New->size() > Old->size()) |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 3131 | << (Kind != TPL_TemplateMatch) |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 3132 | << SourceRange(New->getTemplateLoc(), New->getRAngleLoc()); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3133 | Diag(Old->getTemplateLoc(), diag::note_template_prev_declaration) |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 3134 | << (Kind != TPL_TemplateMatch) |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3135 | << SourceRange(Old->getTemplateLoc(), Old->getRAngleLoc()); |
| 3136 | } |
| 3137 | |
| 3138 | return false; |
| 3139 | } |
| 3140 | |
| 3141 | for (TemplateParameterList::iterator OldParm = Old->begin(), |
| 3142 | OldParmEnd = Old->end(), NewParm = New->begin(); |
| 3143 | OldParm != OldParmEnd; ++OldParm, ++NewParm) { |
| 3144 | if ((*OldParm)->getKind() != (*NewParm)->getKind()) { |
Douglas Gregor | 34d1dc9 | 2009-06-24 16:50:40 +0000 | [diff] [blame] | 3145 | if (Complain) { |
| 3146 | unsigned NextDiag = diag::err_template_param_different_kind; |
| 3147 | if (TemplateArgLoc.isValid()) { |
| 3148 | Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch); |
| 3149 | NextDiag = diag::note_template_param_different_kind; |
| 3150 | } |
| 3151 | Diag((*NewParm)->getLocation(), NextDiag) |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 3152 | << (Kind != TPL_TemplateMatch); |
Douglas Gregor | 34d1dc9 | 2009-06-24 16:50:40 +0000 | [diff] [blame] | 3153 | Diag((*OldParm)->getLocation(), diag::note_template_prev_declaration) |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 3154 | << (Kind != TPL_TemplateMatch); |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 3155 | } |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3156 | return false; |
| 3157 | } |
| 3158 | |
| 3159 | if (isa<TemplateTypeParmDecl>(*OldParm)) { |
| 3160 | // Okay; all template type parameters are equivalent (since we |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 3161 | // know we're at the same index). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3162 | } else if (NonTypeTemplateParmDecl *OldNTTP |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3163 | = dyn_cast<NonTypeTemplateParmDecl>(*OldParm)) { |
| 3164 | // The types of non-type template parameters must agree. |
| 3165 | NonTypeTemplateParmDecl *NewNTTP |
| 3166 | = cast<NonTypeTemplateParmDecl>(*NewParm); |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 3167 | |
| 3168 | // If we are matching a template template argument to a template |
| 3169 | // template parameter and one of the non-type template parameter types |
| 3170 | // is dependent, then we must wait until template instantiation time |
| 3171 | // to actually compare the arguments. |
| 3172 | if (Kind == TPL_TemplateTemplateArgumentMatch && |
| 3173 | (OldNTTP->getType()->isDependentType() || |
| 3174 | NewNTTP->getType()->isDependentType())) |
| 3175 | continue; |
| 3176 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3177 | if (Context.getCanonicalType(OldNTTP->getType()) != |
| 3178 | Context.getCanonicalType(NewNTTP->getType())) { |
| 3179 | if (Complain) { |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 3180 | unsigned NextDiag = diag::err_template_nontype_parm_different_type; |
| 3181 | if (TemplateArgLoc.isValid()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3182 | Diag(TemplateArgLoc, |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 3183 | diag::err_template_arg_template_params_mismatch); |
| 3184 | NextDiag = diag::note_template_nontype_parm_different_type; |
| 3185 | } |
| 3186 | Diag(NewNTTP->getLocation(), NextDiag) |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3187 | << NewNTTP->getType() |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 3188 | << (Kind != TPL_TemplateMatch); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3189 | Diag(OldNTTP->getLocation(), |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3190 | diag::note_template_nontype_parm_prev_declaration) |
| 3191 | << OldNTTP->getType(); |
| 3192 | } |
| 3193 | return false; |
| 3194 | } |
| 3195 | } else { |
| 3196 | // The template parameter lists of template template |
| 3197 | // parameters must agree. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3198 | assert(isa<TemplateTemplateParmDecl>(*OldParm) && |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3199 | "Only template template parameters handled here"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3200 | TemplateTemplateParmDecl *OldTTP |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3201 | = cast<TemplateTemplateParmDecl>(*OldParm); |
| 3202 | TemplateTemplateParmDecl *NewTTP |
| 3203 | = cast<TemplateTemplateParmDecl>(*NewParm); |
| 3204 | if (!TemplateParameterListsAreEqual(NewTTP->getTemplateParameters(), |
| 3205 | OldTTP->getTemplateParameters(), |
| 3206 | Complain, |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 3207 | (Kind == TPL_TemplateMatch? TPL_TemplateTemplateParmMatch : Kind), |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 3208 | TemplateArgLoc)) |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3209 | return false; |
| 3210 | } |
| 3211 | } |
| 3212 | |
| 3213 | return true; |
| 3214 | } |
| 3215 | |
| 3216 | /// \brief Check whether a template can be declared within this scope. |
| 3217 | /// |
| 3218 | /// If the template declaration is valid in this scope, returns |
| 3219 | /// false. Otherwise, issues a diagnostic and returns true. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3220 | bool |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 3221 | Sema::CheckTemplateDeclScope(Scope *S, TemplateParameterList *TemplateParams) { |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3222 | // Find the nearest enclosing declaration scope. |
| 3223 | while ((S->getFlags() & Scope::DeclScope) == 0 || |
| 3224 | (S->getFlags() & Scope::TemplateParamScope) != 0) |
| 3225 | S = S->getParent(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3226 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3227 | // C++ [temp]p2: |
| 3228 | // A template-declaration can appear only as a namespace scope or |
| 3229 | // class scope declaration. |
| 3230 | DeclContext *Ctx = static_cast<DeclContext *>(S->getEntity()); |
Eli Friedman | 1503f77 | 2009-07-31 01:43:05 +0000 | [diff] [blame] | 3231 | if (Ctx && isa<LinkageSpecDecl>(Ctx) && |
| 3232 | cast<LinkageSpecDecl>(Ctx)->getLanguage() != LinkageSpecDecl::lang_cxx) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3233 | return Diag(TemplateParams->getTemplateLoc(), diag::err_template_linkage) |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 3234 | << TemplateParams->getSourceRange(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3235 | |
Eli Friedman | 1503f77 | 2009-07-31 01:43:05 +0000 | [diff] [blame] | 3236 | while (Ctx && isa<LinkageSpecDecl>(Ctx)) |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3237 | Ctx = Ctx->getParent(); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3238 | |
| 3239 | if (Ctx && (Ctx->isFileContext() || Ctx->isRecord())) |
| 3240 | return false; |
| 3241 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3242 | return Diag(TemplateParams->getTemplateLoc(), |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 3243 | diag::err_template_outside_namespace_or_class_scope) |
| 3244 | << TemplateParams->getSourceRange(); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3245 | } |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3246 | |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3247 | /// \brief Determine what kind of template specialization the given declaration |
| 3248 | /// is. |
| 3249 | static TemplateSpecializationKind getTemplateSpecializationKind(NamedDecl *D) { |
| 3250 | if (!D) |
| 3251 | return TSK_Undeclared; |
| 3252 | |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 3253 | if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) |
| 3254 | return Record->getTemplateSpecializationKind(); |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3255 | if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) |
| 3256 | return Function->getTemplateSpecializationKind(); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 3257 | if (VarDecl *Var = dyn_cast<VarDecl>(D)) |
| 3258 | return Var->getTemplateSpecializationKind(); |
| 3259 | |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3260 | return TSK_Undeclared; |
| 3261 | } |
| 3262 | |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 3263 | /// \brief Check whether a specialization is well-formed in the current |
| 3264 | /// context. |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 3265 | /// |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 3266 | /// This routine determines whether a template specialization can be declared |
| 3267 | /// in the current context (C++ [temp.expl.spec]p2). |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3268 | /// |
| 3269 | /// \param S the semantic analysis object for which this check is being |
| 3270 | /// performed. |
| 3271 | /// |
| 3272 | /// \param Specialized the entity being specialized or instantiated, which |
| 3273 | /// may be a kind of template (class template, function template, etc.) or |
| 3274 | /// a member of a class template (member function, static data member, |
| 3275 | /// member class). |
| 3276 | /// |
| 3277 | /// \param PrevDecl the previous declaration of this entity, if any. |
| 3278 | /// |
| 3279 | /// \param Loc the location of the explicit specialization or instantiation of |
| 3280 | /// this entity. |
| 3281 | /// |
| 3282 | /// \param IsPartialSpecialization whether this is a partial specialization of |
| 3283 | /// a class template. |
| 3284 | /// |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3285 | /// \returns true if there was an error that we cannot recover from, false |
| 3286 | /// otherwise. |
| 3287 | static bool CheckTemplateSpecializationScope(Sema &S, |
| 3288 | NamedDecl *Specialized, |
| 3289 | NamedDecl *PrevDecl, |
| 3290 | SourceLocation Loc, |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 3291 | bool IsPartialSpecialization) { |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3292 | // Keep these "kind" numbers in sync with the %select statements in the |
| 3293 | // various diagnostics emitted by this routine. |
| 3294 | int EntityKind = 0; |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 3295 | bool isTemplateSpecialization = false; |
| 3296 | if (isa<ClassTemplateDecl>(Specialized)) { |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3297 | EntityKind = IsPartialSpecialization? 1 : 0; |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 3298 | isTemplateSpecialization = true; |
| 3299 | } else if (isa<FunctionTemplateDecl>(Specialized)) { |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3300 | EntityKind = 2; |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 3301 | isTemplateSpecialization = true; |
| 3302 | } else if (isa<CXXMethodDecl>(Specialized)) |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3303 | EntityKind = 3; |
| 3304 | else if (isa<VarDecl>(Specialized)) |
| 3305 | EntityKind = 4; |
| 3306 | else if (isa<RecordDecl>(Specialized)) |
| 3307 | EntityKind = 5; |
| 3308 | else { |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 3309 | S.Diag(Loc, diag::err_template_spec_unknown_kind); |
| 3310 | S.Diag(Specialized->getLocation(), diag::note_specialized_entity); |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3311 | return true; |
| 3312 | } |
| 3313 | |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 3314 | // C++ [temp.expl.spec]p2: |
| 3315 | // An explicit specialization shall be declared in the namespace |
| 3316 | // of which the template is a member, or, for member templates, in |
| 3317 | // the namespace of which the enclosing class or enclosing class |
| 3318 | // template is a member. An explicit specialization of a member |
| 3319 | // function, member class or static data member of a class |
| 3320 | // template shall be declared in the namespace of which the class |
| 3321 | // template is a member. Such a declaration may also be a |
| 3322 | // definition. If the declaration is not a definition, the |
| 3323 | // specialization may be defined later in the name- space in which |
| 3324 | // the explicit specialization was declared, or in a namespace |
| 3325 | // that encloses the one in which the explicit specialization was |
| 3326 | // declared. |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3327 | if (S.CurContext->getLookupContext()->isFunctionOrMethod()) { |
| 3328 | S.Diag(Loc, diag::err_template_spec_decl_function_scope) |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 3329 | << Specialized; |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 3330 | return true; |
| 3331 | } |
Douglas Gregor | 7974c3b | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 3332 | |
Douglas Gregor | 0a40747 | 2009-10-07 17:30:37 +0000 | [diff] [blame] | 3333 | if (S.CurContext->isRecord() && !IsPartialSpecialization) { |
| 3334 | S.Diag(Loc, diag::err_template_spec_decl_class_scope) |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 3335 | << Specialized; |
Douglas Gregor | 0a40747 | 2009-10-07 17:30:37 +0000 | [diff] [blame] | 3336 | return true; |
| 3337 | } |
| 3338 | |
Douglas Gregor | 7974c3b | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 3339 | // C++ [temp.class.spec]p6: |
| 3340 | // A class template partial specialization may be declared or redeclared |
| 3341 | // in any namespace scope in which its definition may be defined (14.5.1 |
| 3342 | // and 14.5.2). |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3343 | bool ComplainedAboutScope = false; |
Douglas Gregor | 7974c3b | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 3344 | DeclContext *SpecializedContext |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3345 | = Specialized->getDeclContext()->getEnclosingNamespaceContext(); |
Douglas Gregor | 7974c3b | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 3346 | DeclContext *DC = S.CurContext->getEnclosingNamespaceContext(); |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 3347 | if ((!PrevDecl || |
| 3348 | getTemplateSpecializationKind(PrevDecl) == TSK_Undeclared || |
| 3349 | getTemplateSpecializationKind(PrevDecl) == TSK_ImplicitInstantiation)){ |
| 3350 | // There is no prior declaration of this entity, so this |
| 3351 | // specialization must be in the same context as the template |
| 3352 | // itself. |
| 3353 | if (!DC->Equals(SpecializedContext)) { |
| 3354 | if (isa<TranslationUnitDecl>(SpecializedContext)) |
| 3355 | S.Diag(Loc, diag::err_template_spec_decl_out_of_scope_global) |
| 3356 | << EntityKind << Specialized; |
| 3357 | else if (isa<NamespaceDecl>(SpecializedContext)) |
| 3358 | S.Diag(Loc, diag::err_template_spec_decl_out_of_scope) |
| 3359 | << EntityKind << Specialized |
| 3360 | << cast<NamedDecl>(SpecializedContext); |
| 3361 | |
| 3362 | S.Diag(Specialized->getLocation(), diag::note_specialized_entity); |
| 3363 | ComplainedAboutScope = true; |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 3364 | } |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 3365 | } |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3366 | |
| 3367 | // Make sure that this redeclaration (or definition) occurs in an enclosing |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 3368 | // namespace. |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3369 | // Note that HandleDeclarator() performs this check for explicit |
| 3370 | // specializations of function templates, static data members, and member |
| 3371 | // functions, so we skip the check here for those kinds of entities. |
| 3372 | // FIXME: HandleDeclarator's diagnostics aren't quite as good, though. |
Douglas Gregor | 7974c3b | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 3373 | // Should we refactor that check, so that it occurs later? |
| 3374 | if (!ComplainedAboutScope && !DC->Encloses(SpecializedContext) && |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 3375 | !(isa<FunctionTemplateDecl>(Specialized) || isa<VarDecl>(Specialized) || |
| 3376 | isa<FunctionDecl>(Specialized))) { |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3377 | if (isa<TranslationUnitDecl>(SpecializedContext)) |
| 3378 | S.Diag(Loc, diag::err_template_spec_redecl_global_scope) |
| 3379 | << EntityKind << Specialized; |
| 3380 | else if (isa<NamespaceDecl>(SpecializedContext)) |
| 3381 | S.Diag(Loc, diag::err_template_spec_redecl_out_of_scope) |
| 3382 | << EntityKind << Specialized |
| 3383 | << cast<NamedDecl>(SpecializedContext); |
| 3384 | |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 3385 | S.Diag(Specialized->getLocation(), diag::note_specialized_entity); |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 3386 | } |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3387 | |
| 3388 | // FIXME: check for specialization-after-instantiation errors and such. |
| 3389 | |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 3390 | return false; |
| 3391 | } |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3392 | |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 3393 | /// \brief Check the non-type template arguments of a class template |
| 3394 | /// partial specialization according to C++ [temp.class.spec]p9. |
| 3395 | /// |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3396 | /// \param TemplateParams the template parameters of the primary class |
| 3397 | /// template. |
| 3398 | /// |
| 3399 | /// \param TemplateArg the template arguments of the class template |
| 3400 | /// partial specialization. |
| 3401 | /// |
| 3402 | /// \param MirrorsPrimaryTemplate will be set true if the class |
| 3403 | /// template partial specialization arguments are identical to the |
| 3404 | /// implicit template arguments of the primary template. This is not |
| 3405 | /// necessarily an error (C++0x), and it is left to the caller to diagnose |
| 3406 | /// this condition when it is an error. |
| 3407 | /// |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 3408 | /// \returns true if there was an error, false otherwise. |
| 3409 | bool Sema::CheckClassTemplatePartialSpecializationArgs( |
| 3410 | TemplateParameterList *TemplateParams, |
Anders Carlsson | 6360be7 | 2009-06-13 18:20:51 +0000 | [diff] [blame] | 3411 | const TemplateArgumentListBuilder &TemplateArgs, |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3412 | bool &MirrorsPrimaryTemplate) { |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 3413 | // FIXME: the interface to this function will have to change to |
| 3414 | // accommodate variadic templates. |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3415 | MirrorsPrimaryTemplate = true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3416 | |
Anders Carlsson | fb25052 | 2009-06-23 01:26:57 +0000 | [diff] [blame] | 3417 | const TemplateArgument *ArgList = TemplateArgs.getFlatArguments(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3418 | |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 3419 | for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) { |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3420 | // Determine whether the template argument list of the partial |
| 3421 | // specialization is identical to the implicit argument list of |
| 3422 | // the primary template. The caller may need to diagnostic this as |
| 3423 | // an error per C++ [temp.class.spec]p9b3. |
| 3424 | if (MirrorsPrimaryTemplate) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3425 | if (TemplateTypeParmDecl *TTP |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3426 | = dyn_cast<TemplateTypeParmDecl>(TemplateParams->getParam(I))) { |
| 3427 | if (Context.getCanonicalType(Context.getTypeDeclType(TTP)) != |
Anders Carlsson | 6360be7 | 2009-06-13 18:20:51 +0000 | [diff] [blame] | 3428 | Context.getCanonicalType(ArgList[I].getAsType())) |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3429 | MirrorsPrimaryTemplate = false; |
| 3430 | } else if (TemplateTemplateParmDecl *TTP |
| 3431 | = dyn_cast<TemplateTemplateParmDecl>( |
| 3432 | TemplateParams->getParam(I))) { |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 3433 | TemplateName Name = ArgList[I].getAsTemplate(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3434 | TemplateTemplateParmDecl *ArgDecl |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 3435 | = dyn_cast_or_null<TemplateTemplateParmDecl>(Name.getAsTemplateDecl()); |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3436 | if (!ArgDecl || |
| 3437 | ArgDecl->getIndex() != TTP->getIndex() || |
| 3438 | ArgDecl->getDepth() != TTP->getDepth()) |
| 3439 | MirrorsPrimaryTemplate = false; |
| 3440 | } |
| 3441 | } |
| 3442 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3443 | NonTypeTemplateParmDecl *Param |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 3444 | = dyn_cast<NonTypeTemplateParmDecl>(TemplateParams->getParam(I)); |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3445 | if (!Param) { |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 3446 | continue; |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3447 | } |
| 3448 | |
Anders Carlsson | 6360be7 | 2009-06-13 18:20:51 +0000 | [diff] [blame] | 3449 | Expr *ArgExpr = ArgList[I].getAsExpr(); |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3450 | if (!ArgExpr) { |
| 3451 | MirrorsPrimaryTemplate = false; |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 3452 | continue; |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3453 | } |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 3454 | |
| 3455 | // C++ [temp.class.spec]p8: |
| 3456 | // A non-type argument is non-specialized if it is the name of a |
| 3457 | // non-type parameter. All other non-type arguments are |
| 3458 | // specialized. |
| 3459 | // |
| 3460 | // Below, we check the two conditions that only apply to |
| 3461 | // specialized non-type arguments, so skip any non-specialized |
| 3462 | // arguments. |
| 3463 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ArgExpr)) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3464 | if (NonTypeTemplateParmDecl *NTTP |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3465 | = dyn_cast<NonTypeTemplateParmDecl>(DRE->getDecl())) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3466 | if (MirrorsPrimaryTemplate && |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3467 | (Param->getIndex() != NTTP->getIndex() || |
| 3468 | Param->getDepth() != NTTP->getDepth())) |
| 3469 | MirrorsPrimaryTemplate = false; |
| 3470 | |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 3471 | continue; |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3472 | } |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 3473 | |
| 3474 | // C++ [temp.class.spec]p9: |
| 3475 | // Within the argument list of a class template partial |
| 3476 | // specialization, the following restrictions apply: |
| 3477 | // -- A partially specialized non-type argument expression |
| 3478 | // shall not involve a template parameter of the partial |
| 3479 | // specialization except when the argument expression is a |
| 3480 | // simple identifier. |
| 3481 | if (ArgExpr->isTypeDependent() || ArgExpr->isValueDependent()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3482 | Diag(ArgExpr->getLocStart(), |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 3483 | diag::err_dependent_non_type_arg_in_partial_spec) |
| 3484 | << ArgExpr->getSourceRange(); |
| 3485 | return true; |
| 3486 | } |
| 3487 | |
| 3488 | // -- The type of a template parameter corresponding to a |
| 3489 | // specialized non-type argument shall not be dependent on a |
| 3490 | // parameter of the specialization. |
| 3491 | if (Param->getType()->isDependentType()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3492 | Diag(ArgExpr->getLocStart(), |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 3493 | diag::err_dependent_typed_non_type_arg_in_partial_spec) |
| 3494 | << Param->getType() |
| 3495 | << ArgExpr->getSourceRange(); |
| 3496 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 3497 | return true; |
| 3498 | } |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3499 | |
| 3500 | MirrorsPrimaryTemplate = false; |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 3501 | } |
| 3502 | |
| 3503 | return false; |
| 3504 | } |
| 3505 | |
Douglas Gregor | dc0a11c | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 3506 | /// \brief Retrieve the previous declaration of the given declaration. |
| 3507 | static NamedDecl *getPreviousDecl(NamedDecl *ND) { |
| 3508 | if (VarDecl *VD = dyn_cast<VarDecl>(ND)) |
| 3509 | return VD->getPreviousDeclaration(); |
| 3510 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) |
| 3511 | return FD->getPreviousDeclaration(); |
| 3512 | if (TagDecl *TD = dyn_cast<TagDecl>(ND)) |
| 3513 | return TD->getPreviousDeclaration(); |
| 3514 | if (TypedefDecl *TD = dyn_cast<TypedefDecl>(ND)) |
| 3515 | return TD->getPreviousDeclaration(); |
| 3516 | if (FunctionTemplateDecl *FTD = dyn_cast<FunctionTemplateDecl>(ND)) |
| 3517 | return FTD->getPreviousDeclaration(); |
| 3518 | if (ClassTemplateDecl *CTD = dyn_cast<ClassTemplateDecl>(ND)) |
| 3519 | return CTD->getPreviousDeclaration(); |
| 3520 | return 0; |
| 3521 | } |
| 3522 | |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 3523 | Sema::DeclResult |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 3524 | Sema::ActOnClassTemplateSpecialization(Scope *S, unsigned TagSpec, |
| 3525 | TagUseKind TUK, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3526 | SourceLocation KWLoc, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 3527 | CXXScopeSpec &SS, |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 3528 | TemplateTy TemplateD, |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3529 | SourceLocation TemplateNameLoc, |
| 3530 | SourceLocation LAngleLoc, |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 3531 | ASTTemplateArgsPtr TemplateArgsIn, |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3532 | SourceLocation RAngleLoc, |
| 3533 | AttributeList *Attr, |
| 3534 | MultiTemplateParamsArg TemplateParameterLists) { |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 3535 | assert(TUK != TUK_Reference && "References are not specializations"); |
John McCall | f1bbbb4 | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 3536 | |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3537 | // Find the class template we're specializing |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 3538 | TemplateName Name = TemplateD.getAsVal<TemplateName>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3539 | ClassTemplateDecl *ClassTemplate |
Douglas Gregor | 8b13c08 | 2009-11-12 00:46:20 +0000 | [diff] [blame] | 3540 | = dyn_cast_or_null<ClassTemplateDecl>(Name.getAsTemplateDecl()); |
| 3541 | |
| 3542 | if (!ClassTemplate) { |
| 3543 | Diag(TemplateNameLoc, diag::err_not_class_template_specialization) |
| 3544 | << (Name.getAsTemplateDecl() && |
| 3545 | isa<TemplateTemplateParmDecl>(Name.getAsTemplateDecl())); |
| 3546 | return true; |
| 3547 | } |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3548 | |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 3549 | bool isExplicitSpecialization = false; |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 3550 | bool isPartialSpecialization = false; |
| 3551 | |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 3552 | // Check the validity of the template headers that introduce this |
| 3553 | // template. |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 3554 | // FIXME: We probably shouldn't complain about these headers for |
| 3555 | // friend declarations. |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 3556 | TemplateParameterList *TemplateParams |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3557 | = MatchTemplateParametersToScopeSpecifier(TemplateNameLoc, SS, |
| 3558 | (TemplateParameterList**)TemplateParameterLists.get(), |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 3559 | TemplateParameterLists.size(), |
John McCall | 77e8b11 | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 3560 | TUK == TUK_Friend, |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 3561 | isExplicitSpecialization); |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 3562 | if (TemplateParams && TemplateParams->size() > 0) { |
| 3563 | isPartialSpecialization = true; |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 3564 | |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 3565 | // C++ [temp.class.spec]p10: |
| 3566 | // The template parameter list of a specialization shall not |
| 3567 | // contain default template argument values. |
| 3568 | for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) { |
| 3569 | Decl *Param = TemplateParams->getParam(I); |
| 3570 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) { |
| 3571 | if (TTP->hasDefaultArgument()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3572 | Diag(TTP->getDefaultArgumentLoc(), |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 3573 | diag::err_default_arg_in_partial_spec); |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 3574 | TTP->removeDefaultArgument(); |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 3575 | } |
| 3576 | } else if (NonTypeTemplateParmDecl *NTTP |
| 3577 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
| 3578 | if (Expr *DefArg = NTTP->getDefaultArgument()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3579 | Diag(NTTP->getDefaultArgumentLoc(), |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 3580 | diag::err_default_arg_in_partial_spec) |
| 3581 | << DefArg->getSourceRange(); |
| 3582 | NTTP->setDefaultArgument(0); |
| 3583 | DefArg->Destroy(Context); |
| 3584 | } |
| 3585 | } else { |
| 3586 | TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(Param); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 3587 | if (TTP->hasDefaultArgument()) { |
| 3588 | Diag(TTP->getDefaultArgument().getLocation(), |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 3589 | diag::err_default_arg_in_partial_spec) |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 3590 | << TTP->getDefaultArgument().getSourceRange(); |
| 3591 | TTP->setDefaultArgument(TemplateArgumentLoc()); |
Douglas Gregor | ba1ecb5 | 2009-06-12 19:43:02 +0000 | [diff] [blame] | 3592 | } |
| 3593 | } |
| 3594 | } |
Douglas Gregor | a735b20 | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 3595 | } else if (TemplateParams) { |
| 3596 | if (TUK == TUK_Friend) |
| 3597 | Diag(KWLoc, diag::err_template_spec_friend) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 3598 | << FixItHint::CreateRemoval( |
Douglas Gregor | a735b20 | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 3599 | SourceRange(TemplateParams->getTemplateLoc(), |
| 3600 | TemplateParams->getRAngleLoc())) |
| 3601 | << SourceRange(LAngleLoc, RAngleLoc); |
| 3602 | else |
| 3603 | isExplicitSpecialization = true; |
| 3604 | } else if (TUK != TUK_Friend) { |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 3605 | Diag(KWLoc, diag::err_template_spec_needs_header) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 3606 | << FixItHint::CreateInsertion(KWLoc, "template<> "); |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 3607 | isExplicitSpecialization = true; |
| 3608 | } |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 3609 | |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3610 | // Check that the specialization uses the same tag kind as the |
| 3611 | // original template. |
| 3612 | TagDecl::TagKind Kind; |
| 3613 | switch (TagSpec) { |
| 3614 | default: assert(0 && "Unknown tag type!"); |
| 3615 | case DeclSpec::TST_struct: Kind = TagDecl::TK_struct; break; |
| 3616 | case DeclSpec::TST_union: Kind = TagDecl::TK_union; break; |
| 3617 | case DeclSpec::TST_class: Kind = TagDecl::TK_class; break; |
| 3618 | } |
Douglas Gregor | 501c5ce | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 3619 | if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3620 | Kind, KWLoc, |
Douglas Gregor | 501c5ce | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 3621 | *ClassTemplate->getIdentifier())) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3622 | Diag(KWLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 3623 | << ClassTemplate |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 3624 | << FixItHint::CreateReplacement(KWLoc, |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 3625 | ClassTemplate->getTemplatedDecl()->getKindName()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3626 | Diag(ClassTemplate->getTemplatedDecl()->getLocation(), |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3627 | diag::note_previous_use); |
| 3628 | Kind = ClassTemplate->getTemplatedDecl()->getTagKind(); |
| 3629 | } |
| 3630 | |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 3631 | // Translate the parser's template argument list in our AST format. |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 3632 | TemplateArgumentListInfo TemplateArgs; |
| 3633 | TemplateArgs.setLAngleLoc(LAngleLoc); |
| 3634 | TemplateArgs.setRAngleLoc(RAngleLoc); |
Douglas Gregor | 314b97f | 2009-11-10 19:49:08 +0000 | [diff] [blame] | 3635 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 3636 | |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3637 | // Check that the template argument list is well-formed for this |
| 3638 | // template. |
Anders Carlsson | fb25052 | 2009-06-23 01:26:57 +0000 | [diff] [blame] | 3639 | TemplateArgumentListBuilder Converted(ClassTemplate->getTemplateParameters(), |
| 3640 | TemplateArgs.size()); |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 3641 | if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc, |
| 3642 | TemplateArgs, false, Converted)) |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 3643 | return true; |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3644 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3645 | assert((Converted.structuredSize() == |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3646 | ClassTemplate->getTemplateParameters()->size()) && |
| 3647 | "Converted template argument list is too short!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3648 | |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 3649 | // Find the class template (partial) specialization declaration that |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3650 | // corresponds to these arguments. |
| 3651 | llvm::FoldingSetNodeID ID; |
Douglas Gregor | ba1ecb5 | 2009-06-12 19:43:02 +0000 | [diff] [blame] | 3652 | if (isPartialSpecialization) { |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3653 | bool MirrorsPrimaryTemplate; |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 3654 | if (CheckClassTemplatePartialSpecializationArgs( |
| 3655 | ClassTemplate->getTemplateParameters(), |
Anders Carlsson | fb25052 | 2009-06-23 01:26:57 +0000 | [diff] [blame] | 3656 | Converted, MirrorsPrimaryTemplate)) |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 3657 | return true; |
| 3658 | |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3659 | if (MirrorsPrimaryTemplate) { |
| 3660 | // C++ [temp.class.spec]p9b3: |
| 3661 | // |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3662 | // -- The argument list of the specialization shall not be identical |
| 3663 | // to the implicit argument list of the primary template. |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3664 | Diag(TemplateNameLoc, diag::err_partial_spec_args_match_primary_template) |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 3665 | << (TUK == TUK_Definition) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 3666 | << FixItHint::CreateRemoval(SourceRange(LAngleLoc, RAngleLoc)); |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 3667 | return CheckClassTemplate(S, TagSpec, TUK, KWLoc, SS, |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3668 | ClassTemplate->getIdentifier(), |
| 3669 | TemplateNameLoc, |
| 3670 | Attr, |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 3671 | TemplateParams, |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3672 | AS_none); |
| 3673 | } |
| 3674 | |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 3675 | // FIXME: Diagnose friend partial specializations |
| 3676 | |
Douglas Gregor | de09096 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 3677 | if (!Name.isDependent() && |
| 3678 | !TemplateSpecializationType::anyDependentTemplateArguments( |
| 3679 | TemplateArgs.getArgumentArray(), |
| 3680 | TemplateArgs.size())) { |
| 3681 | Diag(TemplateNameLoc, diag::err_partial_spec_fully_specialized) |
| 3682 | << ClassTemplate->getDeclName(); |
| 3683 | isPartialSpecialization = false; |
| 3684 | } else { |
| 3685 | // FIXME: Template parameter list matters, too |
| 3686 | ClassTemplatePartialSpecializationDecl::Profile(ID, |
| 3687 | Converted.getFlatArguments(), |
| 3688 | Converted.flatSize(), |
| 3689 | Context); |
| 3690 | } |
| 3691 | } |
| 3692 | |
| 3693 | if (!isPartialSpecialization) |
Anders Carlsson | 1c5976e | 2009-06-05 03:43:12 +0000 | [diff] [blame] | 3694 | ClassTemplateSpecializationDecl::Profile(ID, |
Anders Carlsson | fb25052 | 2009-06-23 01:26:57 +0000 | [diff] [blame] | 3695 | Converted.getFlatArguments(), |
Douglas Gregor | 828e226 | 2009-07-29 16:09:57 +0000 | [diff] [blame] | 3696 | Converted.flatSize(), |
| 3697 | Context); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3698 | void *InsertPos = 0; |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 3699 | ClassTemplateSpecializationDecl *PrevDecl = 0; |
| 3700 | |
| 3701 | if (isPartialSpecialization) |
| 3702 | PrevDecl |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3703 | = ClassTemplate->getPartialSpecializations().FindNodeOrInsertPos(ID, |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 3704 | InsertPos); |
| 3705 | else |
| 3706 | PrevDecl |
| 3707 | = ClassTemplate->getSpecializations().FindNodeOrInsertPos(ID, InsertPos); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3708 | |
| 3709 | ClassTemplateSpecializationDecl *Specialization = 0; |
| 3710 | |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 3711 | // Check whether we can declare a class template specialization in |
| 3712 | // the current scope. |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 3713 | if (TUK != TUK_Friend && |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3714 | CheckTemplateSpecializationScope(*this, ClassTemplate, PrevDecl, |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 3715 | TemplateNameLoc, |
| 3716 | isPartialSpecialization)) |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 3717 | return true; |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 3718 | |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 3719 | // The canonical type |
| 3720 | QualType CanonType; |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 3721 | if (PrevDecl && |
| 3722 | (PrevDecl->getSpecializationKind() == TSK_Undeclared || |
Douglas Gregor | de09096 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 3723 | TUK == TUK_Friend)) { |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3724 | // Since the only prior class template specialization with these |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 3725 | // arguments was referenced but not declared, or we're only |
| 3726 | // referencing this specialization as a friend, reuse that |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3727 | // declaration node as our own, updating its source location to |
| 3728 | // reflect our new declaration. |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3729 | Specialization = PrevDecl; |
Douglas Gregor | 6bc9f7e | 2009-02-25 22:18:32 +0000 | [diff] [blame] | 3730 | Specialization->setLocation(TemplateNameLoc); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3731 | PrevDecl = 0; |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 3732 | CanonType = Context.getTypeDeclType(Specialization); |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 3733 | } else if (isPartialSpecialization) { |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 3734 | // Build the canonical type that describes the converted template |
| 3735 | // arguments of the class template partial specialization. |
Douglas Gregor | de09096 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 3736 | TemplateName CanonTemplate = Context.getCanonicalTemplateName(Name); |
| 3737 | CanonType = Context.getTemplateSpecializationType(CanonTemplate, |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 3738 | Converted.getFlatArguments(), |
| 3739 | Converted.flatSize()); |
| 3740 | |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 3741 | // Create a new class template partial specialization declaration node. |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 3742 | ClassTemplatePartialSpecializationDecl *PrevPartial |
| 3743 | = cast_or_null<ClassTemplatePartialSpecializationDecl>(PrevDecl); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3744 | ClassTemplatePartialSpecializationDecl *Partial |
| 3745 | = ClassTemplatePartialSpecializationDecl::Create(Context, |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 3746 | ClassTemplate->getDeclContext(), |
Anders Carlsson | 91fdf6f | 2009-06-05 04:06:48 +0000 | [diff] [blame] | 3747 | TemplateNameLoc, |
| 3748 | TemplateParams, |
| 3749 | ClassTemplate, |
Anders Carlsson | fb25052 | 2009-06-23 01:26:57 +0000 | [diff] [blame] | 3750 | Converted, |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 3751 | TemplateArgs, |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 3752 | CanonType, |
Anders Carlsson | 91fdf6f | 2009-06-05 04:06:48 +0000 | [diff] [blame] | 3753 | PrevPartial); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 3754 | SetNestedNameSpecifier(Partial, SS); |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 3755 | |
| 3756 | if (PrevPartial) { |
| 3757 | ClassTemplate->getPartialSpecializations().RemoveNode(PrevPartial); |
| 3758 | ClassTemplate->getPartialSpecializations().GetOrInsertNode(Partial); |
| 3759 | } else { |
| 3760 | ClassTemplate->getPartialSpecializations().InsertNode(Partial, InsertPos); |
| 3761 | } |
| 3762 | Specialization = Partial; |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 3763 | |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 3764 | // If we are providing an explicit specialization of a member class |
| 3765 | // template specialization, make a note of that. |
| 3766 | if (PrevPartial && PrevPartial->getInstantiatedFromMember()) |
| 3767 | PrevPartial->setMemberSpecialization(); |
| 3768 | |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 3769 | // Check that all of the template parameters of the class template |
| 3770 | // partial specialization are deducible from the template |
| 3771 | // arguments. If not, this class template partial specialization |
| 3772 | // will never be used. |
| 3773 | llvm::SmallVector<bool, 8> DeducibleParams; |
| 3774 | DeducibleParams.resize(TemplateParams->size()); |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 3775 | MarkUsedTemplateParameters(Partial->getTemplateArgs(), true, |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 3776 | TemplateParams->getDepth(), |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 3777 | DeducibleParams); |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 3778 | unsigned NumNonDeducible = 0; |
| 3779 | for (unsigned I = 0, N = DeducibleParams.size(); I != N; ++I) |
| 3780 | if (!DeducibleParams[I]) |
| 3781 | ++NumNonDeducible; |
| 3782 | |
| 3783 | if (NumNonDeducible) { |
| 3784 | Diag(TemplateNameLoc, diag::warn_partial_specs_not_deducible) |
| 3785 | << (NumNonDeducible > 1) |
| 3786 | << SourceRange(TemplateNameLoc, RAngleLoc); |
| 3787 | for (unsigned I = 0, N = DeducibleParams.size(); I != N; ++I) { |
| 3788 | if (!DeducibleParams[I]) { |
| 3789 | NamedDecl *Param = cast<NamedDecl>(TemplateParams->getParam(I)); |
| 3790 | if (Param->getDeclName()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3791 | Diag(Param->getLocation(), |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 3792 | diag::note_partial_spec_unused_parameter) |
| 3793 | << Param->getDeclName(); |
| 3794 | else |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3795 | Diag(Param->getLocation(), |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 3796 | diag::note_partial_spec_unused_parameter) |
| 3797 | << std::string("<anonymous>"); |
| 3798 | } |
| 3799 | } |
| 3800 | } |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3801 | } else { |
| 3802 | // Create a new class template specialization declaration node for |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 3803 | // this explicit specialization or friend declaration. |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3804 | Specialization |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3805 | = ClassTemplateSpecializationDecl::Create(Context, |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3806 | ClassTemplate->getDeclContext(), |
| 3807 | TemplateNameLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3808 | ClassTemplate, |
Anders Carlsson | fb25052 | 2009-06-23 01:26:57 +0000 | [diff] [blame] | 3809 | Converted, |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3810 | PrevDecl); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 3811 | SetNestedNameSpecifier(Specialization, SS); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3812 | |
| 3813 | if (PrevDecl) { |
| 3814 | ClassTemplate->getSpecializations().RemoveNode(PrevDecl); |
| 3815 | ClassTemplate->getSpecializations().GetOrInsertNode(Specialization); |
| 3816 | } else { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3817 | ClassTemplate->getSpecializations().InsertNode(Specialization, |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3818 | InsertPos); |
| 3819 | } |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 3820 | |
| 3821 | CanonType = Context.getTypeDeclType(Specialization); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3822 | } |
| 3823 | |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 3824 | // C++ [temp.expl.spec]p6: |
| 3825 | // If a template, a member template or the member of a class template is |
| 3826 | // explicitly specialized then that specialization shall be declared |
| 3827 | // before the first use of that specialization that would cause an implicit |
| 3828 | // instantiation to take place, in every translation unit in which such a |
| 3829 | // use occurs; no diagnostic is required. |
| 3830 | if (PrevDecl && PrevDecl->getPointOfInstantiation().isValid()) { |
Douglas Gregor | dc0a11c | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 3831 | bool Okay = false; |
| 3832 | for (NamedDecl *Prev = PrevDecl; Prev; Prev = getPreviousDecl(Prev)) { |
| 3833 | // Is there any previous explicit specialization declaration? |
| 3834 | if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) { |
| 3835 | Okay = true; |
| 3836 | break; |
| 3837 | } |
| 3838 | } |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 3839 | |
Douglas Gregor | dc0a11c | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 3840 | if (!Okay) { |
| 3841 | SourceRange Range(TemplateNameLoc, RAngleLoc); |
| 3842 | Diag(TemplateNameLoc, diag::err_specialization_after_instantiation) |
| 3843 | << Context.getTypeDeclType(Specialization) << Range; |
| 3844 | |
| 3845 | Diag(PrevDecl->getPointOfInstantiation(), |
| 3846 | diag::note_instantiation_required_here) |
| 3847 | << (PrevDecl->getTemplateSpecializationKind() |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 3848 | != TSK_ImplicitInstantiation); |
Douglas Gregor | dc0a11c | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 3849 | return true; |
| 3850 | } |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 3851 | } |
| 3852 | |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 3853 | // If this is not a friend, note that this is an explicit specialization. |
| 3854 | if (TUK != TUK_Friend) |
| 3855 | Specialization->setSpecializationKind(TSK_ExplicitSpecialization); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3856 | |
| 3857 | // Check that this isn't a redefinition of this specialization. |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 3858 | if (TUK == TUK_Definition) { |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 3859 | if (RecordDecl *Def = Specialization->getDefinition()) { |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3860 | SourceRange Range(TemplateNameLoc, RAngleLoc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3861 | Diag(TemplateNameLoc, diag::err_redefinition) |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 3862 | << Context.getTypeDeclType(Specialization) << Range; |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3863 | Diag(Def->getLocation(), diag::note_previous_definition); |
| 3864 | Specialization->setInvalidDecl(); |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 3865 | return true; |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3866 | } |
| 3867 | } |
| 3868 | |
Douglas Gregor | fc705b8 | 2009-02-26 22:19:44 +0000 | [diff] [blame] | 3869 | // Build the fully-sugared type for this class template |
| 3870 | // specialization as the user wrote in the specialization |
| 3871 | // itself. This means that we'll pretty-print the type retrieved |
| 3872 | // from the specialization's declaration the way that the user |
| 3873 | // actually wrote the specialization, rather than formatting the |
| 3874 | // name based on the "canonical" representation used to store the |
| 3875 | // template arguments in the specialization. |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 3876 | TypeSourceInfo *WrittenTy |
| 3877 | = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc, |
| 3878 | TemplateArgs, CanonType); |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 3879 | if (TUK != TUK_Friend) |
| 3880 | Specialization->setTypeAsWritten(WrittenTy); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 3881 | TemplateArgsIn.release(); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3882 | |
Douglas Gregor | 6bc9f7e | 2009-02-25 22:18:32 +0000 | [diff] [blame] | 3883 | // C++ [temp.expl.spec]p9: |
| 3884 | // A template explicit specialization is in the scope of the |
| 3885 | // namespace in which the template was defined. |
| 3886 | // |
| 3887 | // We actually implement this paragraph where we set the semantic |
| 3888 | // context (in the creation of the ClassTemplateSpecializationDecl), |
| 3889 | // but we also maintain the lexical context where the actual |
| 3890 | // definition occurs. |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3891 | Specialization->setLexicalDeclContext(CurContext); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3892 | |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3893 | // We may be starting the definition of this specialization. |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 3894 | if (TUK == TUK_Definition) |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3895 | Specialization->startDefinition(); |
| 3896 | |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 3897 | if (TUK == TUK_Friend) { |
| 3898 | FriendDecl *Friend = FriendDecl::Create(Context, CurContext, |
| 3899 | TemplateNameLoc, |
John McCall | 32f2fb5 | 2010-03-25 18:04:51 +0000 | [diff] [blame] | 3900 | WrittenTy, |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 3901 | /*FIXME:*/KWLoc); |
| 3902 | Friend->setAccess(AS_public); |
| 3903 | CurContext->addDecl(Friend); |
| 3904 | } else { |
| 3905 | // Add the specialization into its lexical context, so that it can |
| 3906 | // be seen when iterating through the list of declarations in that |
| 3907 | // context. However, specializations are not found by name lookup. |
| 3908 | CurContext->addDecl(Specialization); |
| 3909 | } |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 3910 | return DeclPtrTy::make(Specialization); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3911 | } |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3912 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3913 | Sema::DeclPtrTy |
| 3914 | Sema::ActOnTemplateDeclarator(Scope *S, |
Douglas Gregor | e542c86 | 2009-06-23 23:11:28 +0000 | [diff] [blame] | 3915 | MultiTemplateParamsArg TemplateParameterLists, |
| 3916 | Declarator &D) { |
| 3917 | return HandleDeclarator(S, D, move(TemplateParameterLists), false); |
| 3918 | } |
| 3919 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3920 | Sema::DeclPtrTy |
| 3921 | Sema::ActOnStartOfFunctionTemplateDef(Scope *FnBodyScope, |
Douglas Gregor | 52591bf | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 3922 | MultiTemplateParamsArg TemplateParameterLists, |
| 3923 | Declarator &D) { |
| 3924 | assert(getCurFunctionDecl() == 0 && "Function parsing confused"); |
| 3925 | assert(D.getTypeObject(0).Kind == DeclaratorChunk::Function && |
| 3926 | "Not a function declarator!"); |
| 3927 | DeclaratorChunk::FunctionTypeInfo &FTI = D.getTypeObject(0).Fun; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3928 | |
Douglas Gregor | 52591bf | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 3929 | if (FTI.hasPrototype) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3930 | // FIXME: Diagnose arguments without names in C. |
Douglas Gregor | 52591bf | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 3931 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3932 | |
Douglas Gregor | 52591bf | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 3933 | Scope *ParentScope = FnBodyScope->getParent(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3934 | |
| 3935 | DeclPtrTy DP = HandleDeclarator(ParentScope, D, |
Douglas Gregor | 52591bf | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 3936 | move(TemplateParameterLists), |
| 3937 | /*IsFunctionDefinition=*/true); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3938 | if (FunctionTemplateDecl *FunctionTemplate |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 3939 | = dyn_cast_or_null<FunctionTemplateDecl>(DP.getAs<Decl>())) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3940 | return ActOnStartOfFunctionDef(FnBodyScope, |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3941 | DeclPtrTy::make(FunctionTemplate->getTemplatedDecl())); |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 3942 | if (FunctionDecl *Function = dyn_cast_or_null<FunctionDecl>(DP.getAs<Decl>())) |
| 3943 | return ActOnStartOfFunctionDef(FnBodyScope, DeclPtrTy::make(Function)); |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3944 | return DeclPtrTy(); |
Douglas Gregor | 52591bf | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 3945 | } |
| 3946 | |
John McCall | 7504239 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 3947 | /// \brief Strips various properties off an implicit instantiation |
| 3948 | /// that has just been explicitly specialized. |
| 3949 | static void StripImplicitInstantiation(NamedDecl *D) { |
| 3950 | D->invalidateAttrs(); |
| 3951 | |
| 3952 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 3953 | FD->setInlineSpecified(false); |
| 3954 | } |
| 3955 | } |
| 3956 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 3957 | /// \brief Diagnose cases where we have an explicit template specialization |
| 3958 | /// before/after an explicit template instantiation, producing diagnostics |
| 3959 | /// for those cases where they are required and determining whether the |
| 3960 | /// new specialization/instantiation will have any effect. |
| 3961 | /// |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 3962 | /// \param NewLoc the location of the new explicit specialization or |
| 3963 | /// instantiation. |
| 3964 | /// |
| 3965 | /// \param NewTSK the kind of the new explicit specialization or instantiation. |
| 3966 | /// |
| 3967 | /// \param PrevDecl the previous declaration of the entity. |
| 3968 | /// |
| 3969 | /// \param PrevTSK the kind of the old explicit specialization or instantiatin. |
| 3970 | /// |
| 3971 | /// \param PrevPointOfInstantiation if valid, indicates where the previus |
| 3972 | /// declaration was instantiated (either implicitly or explicitly). |
| 3973 | /// |
| 3974 | /// \param SuppressNew will be set to true to indicate that the new |
| 3975 | /// specialization or instantiation has no effect and should be ignored. |
| 3976 | /// |
| 3977 | /// \returns true if there was an error that should prevent the introduction of |
| 3978 | /// the new declaration into the AST, false otherwise. |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 3979 | bool |
| 3980 | Sema::CheckSpecializationInstantiationRedecl(SourceLocation NewLoc, |
| 3981 | TemplateSpecializationKind NewTSK, |
| 3982 | NamedDecl *PrevDecl, |
| 3983 | TemplateSpecializationKind PrevTSK, |
| 3984 | SourceLocation PrevPointOfInstantiation, |
| 3985 | bool &SuppressNew) { |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 3986 | SuppressNew = false; |
| 3987 | |
| 3988 | switch (NewTSK) { |
| 3989 | case TSK_Undeclared: |
| 3990 | case TSK_ImplicitInstantiation: |
| 3991 | assert(false && "Don't check implicit instantiations here"); |
| 3992 | return false; |
| 3993 | |
| 3994 | case TSK_ExplicitSpecialization: |
| 3995 | switch (PrevTSK) { |
| 3996 | case TSK_Undeclared: |
| 3997 | case TSK_ExplicitSpecialization: |
| 3998 | // Okay, we're just specializing something that is either already |
| 3999 | // explicitly specialized or has merely been mentioned without any |
| 4000 | // instantiation. |
| 4001 | return false; |
| 4002 | |
| 4003 | case TSK_ImplicitInstantiation: |
| 4004 | if (PrevPointOfInstantiation.isInvalid()) { |
| 4005 | // The declaration itself has not actually been instantiated, so it is |
| 4006 | // still okay to specialize it. |
John McCall | 7504239 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 4007 | StripImplicitInstantiation(PrevDecl); |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4008 | return false; |
| 4009 | } |
| 4010 | // Fall through |
| 4011 | |
| 4012 | case TSK_ExplicitInstantiationDeclaration: |
| 4013 | case TSK_ExplicitInstantiationDefinition: |
| 4014 | assert((PrevTSK == TSK_ImplicitInstantiation || |
| 4015 | PrevPointOfInstantiation.isValid()) && |
| 4016 | "Explicit instantiation without point of instantiation?"); |
| 4017 | |
| 4018 | // C++ [temp.expl.spec]p6: |
| 4019 | // If a template, a member template or the member of a class template |
| 4020 | // is explicitly specialized then that specialization shall be declared |
| 4021 | // before the first use of that specialization that would cause an |
| 4022 | // implicit instantiation to take place, in every translation unit in |
| 4023 | // which such a use occurs; no diagnostic is required. |
Douglas Gregor | dc0a11c | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 4024 | for (NamedDecl *Prev = PrevDecl; Prev; Prev = getPreviousDecl(Prev)) { |
| 4025 | // Is there any previous explicit specialization declaration? |
| 4026 | if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) |
| 4027 | return false; |
| 4028 | } |
| 4029 | |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 4030 | Diag(NewLoc, diag::err_specialization_after_instantiation) |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4031 | << PrevDecl; |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 4032 | Diag(PrevPointOfInstantiation, diag::note_instantiation_required_here) |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4033 | << (PrevTSK != TSK_ImplicitInstantiation); |
| 4034 | |
| 4035 | return true; |
| 4036 | } |
| 4037 | break; |
| 4038 | |
| 4039 | case TSK_ExplicitInstantiationDeclaration: |
| 4040 | switch (PrevTSK) { |
| 4041 | case TSK_ExplicitInstantiationDeclaration: |
| 4042 | // This explicit instantiation declaration is redundant (that's okay). |
| 4043 | SuppressNew = true; |
| 4044 | return false; |
| 4045 | |
| 4046 | case TSK_Undeclared: |
| 4047 | case TSK_ImplicitInstantiation: |
| 4048 | // We're explicitly instantiating something that may have already been |
| 4049 | // implicitly instantiated; that's fine. |
| 4050 | return false; |
| 4051 | |
| 4052 | case TSK_ExplicitSpecialization: |
| 4053 | // C++0x [temp.explicit]p4: |
| 4054 | // For a given set of template parameters, if an explicit instantiation |
| 4055 | // of a template appears after a declaration of an explicit |
| 4056 | // specialization for that template, the explicit instantiation has no |
| 4057 | // effect. |
John McCall | e97c32f | 2010-03-02 23:09:38 +0000 | [diff] [blame] | 4058 | SuppressNew = true; |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4059 | return false; |
| 4060 | |
| 4061 | case TSK_ExplicitInstantiationDefinition: |
| 4062 | // C++0x [temp.explicit]p10: |
| 4063 | // If an entity is the subject of both an explicit instantiation |
| 4064 | // declaration and an explicit instantiation definition in the same |
| 4065 | // translation unit, the definition shall follow the declaration. |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 4066 | Diag(NewLoc, |
| 4067 | diag::err_explicit_instantiation_declaration_after_definition); |
| 4068 | Diag(PrevPointOfInstantiation, |
| 4069 | diag::note_explicit_instantiation_definition_here); |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4070 | assert(PrevPointOfInstantiation.isValid() && |
| 4071 | "Explicit instantiation without point of instantiation?"); |
| 4072 | SuppressNew = true; |
| 4073 | return false; |
| 4074 | } |
| 4075 | break; |
| 4076 | |
| 4077 | case TSK_ExplicitInstantiationDefinition: |
| 4078 | switch (PrevTSK) { |
| 4079 | case TSK_Undeclared: |
| 4080 | case TSK_ImplicitInstantiation: |
| 4081 | // We're explicitly instantiating something that may have already been |
| 4082 | // implicitly instantiated; that's fine. |
| 4083 | return false; |
| 4084 | |
| 4085 | case TSK_ExplicitSpecialization: |
| 4086 | // C++ DR 259, C++0x [temp.explicit]p4: |
| 4087 | // For a given set of template parameters, if an explicit |
| 4088 | // instantiation of a template appears after a declaration of |
| 4089 | // an explicit specialization for that template, the explicit |
| 4090 | // instantiation has no effect. |
| 4091 | // |
| 4092 | // In C++98/03 mode, we only give an extension warning here, because it |
Douglas Gregor | c42b652 | 2010-04-09 21:02:29 +0000 | [diff] [blame] | 4093 | // is not harmful to try to explicitly instantiate something that |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4094 | // has been explicitly specialized. |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 4095 | if (!getLangOptions().CPlusPlus0x) { |
| 4096 | Diag(NewLoc, diag::ext_explicit_instantiation_after_specialization) |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4097 | << PrevDecl; |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 4098 | Diag(PrevDecl->getLocation(), |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4099 | diag::note_previous_template_specialization); |
| 4100 | } |
| 4101 | SuppressNew = true; |
| 4102 | return false; |
| 4103 | |
| 4104 | case TSK_ExplicitInstantiationDeclaration: |
| 4105 | // We're explicity instantiating a definition for something for which we |
| 4106 | // were previously asked to suppress instantiations. That's fine. |
| 4107 | return false; |
| 4108 | |
| 4109 | case TSK_ExplicitInstantiationDefinition: |
| 4110 | // C++0x [temp.spec]p5: |
| 4111 | // For a given template and a given set of template-arguments, |
| 4112 | // - an explicit instantiation definition shall appear at most once |
| 4113 | // in a program, |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 4114 | Diag(NewLoc, diag::err_explicit_instantiation_duplicate) |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4115 | << PrevDecl; |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 4116 | Diag(PrevPointOfInstantiation, |
| 4117 | diag::note_previous_explicit_instantiation); |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4118 | SuppressNew = true; |
| 4119 | return false; |
| 4120 | } |
| 4121 | break; |
| 4122 | } |
| 4123 | |
| 4124 | assert(false && "Missing specialization/instantiation case?"); |
| 4125 | |
| 4126 | return false; |
| 4127 | } |
| 4128 | |
John McCall | af2094e | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 4129 | /// \brief Perform semantic analysis for the given dependent function |
| 4130 | /// template specialization. The only possible way to get a dependent |
| 4131 | /// function template specialization is with a friend declaration, |
| 4132 | /// like so: |
| 4133 | /// |
| 4134 | /// template <class T> void foo(T); |
| 4135 | /// template <class T> class A { |
| 4136 | /// friend void foo<>(T); |
| 4137 | /// }; |
| 4138 | /// |
| 4139 | /// There really isn't any useful analysis we can do here, so we |
| 4140 | /// just store the information. |
| 4141 | bool |
| 4142 | Sema::CheckDependentFunctionTemplateSpecialization(FunctionDecl *FD, |
| 4143 | const TemplateArgumentListInfo &ExplicitTemplateArgs, |
| 4144 | LookupResult &Previous) { |
| 4145 | // Remove anything from Previous that isn't a function template in |
| 4146 | // the correct context. |
| 4147 | DeclContext *FDLookupContext = FD->getDeclContext()->getLookupContext(); |
| 4148 | LookupResult::Filter F = Previous.makeFilter(); |
| 4149 | while (F.hasNext()) { |
| 4150 | NamedDecl *D = F.next()->getUnderlyingDecl(); |
| 4151 | if (!isa<FunctionTemplateDecl>(D) || |
| 4152 | !FDLookupContext->Equals(D->getDeclContext()->getLookupContext())) |
| 4153 | F.erase(); |
| 4154 | } |
| 4155 | F.done(); |
| 4156 | |
| 4157 | // Should this be diagnosed here? |
| 4158 | if (Previous.empty()) return true; |
| 4159 | |
| 4160 | FD->setDependentTemplateSpecialization(Context, Previous.asUnresolvedSet(), |
| 4161 | ExplicitTemplateArgs); |
| 4162 | return false; |
| 4163 | } |
| 4164 | |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4165 | /// \brief Perform semantic analysis for the given function template |
| 4166 | /// specialization. |
| 4167 | /// |
| 4168 | /// This routine performs all of the semantic analysis required for an |
| 4169 | /// explicit function template specialization. On successful completion, |
| 4170 | /// the function declaration \p FD will become a function template |
| 4171 | /// specialization. |
| 4172 | /// |
| 4173 | /// \param FD the function declaration, which will be updated to become a |
| 4174 | /// function template specialization. |
| 4175 | /// |
| 4176 | /// \param HasExplicitTemplateArgs whether any template arguments were |
| 4177 | /// explicitly provided. |
| 4178 | /// |
| 4179 | /// \param LAngleLoc the location of the left angle bracket ('<'), if |
| 4180 | /// template arguments were explicitly provided. |
| 4181 | /// |
| 4182 | /// \param ExplicitTemplateArgs the explicitly-provided template arguments, |
| 4183 | /// if any. |
| 4184 | /// |
| 4185 | /// \param NumExplicitTemplateArgs the number of explicitly-provided template |
| 4186 | /// arguments. This number may be zero even when HasExplicitTemplateArgs is |
| 4187 | /// true as in, e.g., \c void sort<>(char*, char*); |
| 4188 | /// |
| 4189 | /// \param RAngleLoc the location of the right angle bracket ('>'), if |
| 4190 | /// template arguments were explicitly provided. |
| 4191 | /// |
| 4192 | /// \param PrevDecl the set of declarations that |
| 4193 | bool |
| 4194 | Sema::CheckFunctionTemplateSpecialization(FunctionDecl *FD, |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 4195 | const TemplateArgumentListInfo *ExplicitTemplateArgs, |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 4196 | LookupResult &Previous) { |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4197 | // The set of function template specializations that could match this |
| 4198 | // explicit function template specialization. |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4199 | UnresolvedSet<8> Candidates; |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4200 | |
| 4201 | DeclContext *FDLookupContext = FD->getDeclContext()->getLookupContext(); |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 4202 | for (LookupResult::iterator I = Previous.begin(), E = Previous.end(); |
| 4203 | I != E; ++I) { |
| 4204 | NamedDecl *Ovl = (*I)->getUnderlyingDecl(); |
| 4205 | if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Ovl)) { |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4206 | // Only consider templates found within the same semantic lookup scope as |
| 4207 | // FD. |
| 4208 | if (!FDLookupContext->Equals(Ovl->getDeclContext()->getLookupContext())) |
| 4209 | continue; |
| 4210 | |
| 4211 | // C++ [temp.expl.spec]p11: |
| 4212 | // A trailing template-argument can be left unspecified in the |
| 4213 | // template-id naming an explicit function template specialization |
| 4214 | // provided it can be deduced from the function argument type. |
| 4215 | // Perform template argument deduction to determine whether we may be |
| 4216 | // specializing this template. |
| 4217 | // FIXME: It is somewhat wasteful to build |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 4218 | TemplateDeductionInfo Info(Context, FD->getLocation()); |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4219 | FunctionDecl *Specialization = 0; |
| 4220 | if (TemplateDeductionResult TDK |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 4221 | = DeduceTemplateArguments(FunTmpl, ExplicitTemplateArgs, |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4222 | FD->getType(), |
| 4223 | Specialization, |
| 4224 | Info)) { |
| 4225 | // FIXME: Template argument deduction failed; record why it failed, so |
| 4226 | // that we can provide nifty diagnostics. |
| 4227 | (void)TDK; |
| 4228 | continue; |
| 4229 | } |
| 4230 | |
| 4231 | // Record this candidate. |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4232 | Candidates.addDecl(Specialization, I.getAccess()); |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4233 | } |
| 4234 | } |
| 4235 | |
Douglas Gregor | c5df30f | 2009-09-26 03:41:46 +0000 | [diff] [blame] | 4236 | // Find the most specialized function template. |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4237 | UnresolvedSetIterator Result |
| 4238 | = getMostSpecialized(Candidates.begin(), Candidates.end(), |
| 4239 | TPOC_Other, FD->getLocation(), |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 4240 | PDiag(diag::err_function_template_spec_no_match) |
Douglas Gregor | c5df30f | 2009-09-26 03:41:46 +0000 | [diff] [blame] | 4241 | << FD->getDeclName(), |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 4242 | PDiag(diag::err_function_template_spec_ambiguous) |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 4243 | << FD->getDeclName() << (ExplicitTemplateArgs != 0), |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 4244 | PDiag(diag::note_function_template_spec_matched)); |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4245 | if (Result == Candidates.end()) |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4246 | return true; |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4247 | |
| 4248 | // Ignore access information; it doesn't figure into redeclaration checking. |
| 4249 | FunctionDecl *Specialization = cast<FunctionDecl>(*Result); |
Douglas Gregor | c42b652 | 2010-04-09 21:02:29 +0000 | [diff] [blame] | 4250 | Specialization->setLocation(FD->getLocation()); |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4251 | |
| 4252 | // FIXME: Check if the prior specialization has a point of instantiation. |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 4253 | // If so, we have run afoul of . |
John McCall | 7ad650f | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 4254 | |
| 4255 | // If this is a friend declaration, then we're not really declaring |
| 4256 | // an explicit specialization. |
| 4257 | bool isFriend = (FD->getFriendObjectKind() != Decl::FOK_None); |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4258 | |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4259 | // Check the scope of this explicit specialization. |
John McCall | 7ad650f | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 4260 | if (!isFriend && |
| 4261 | CheckTemplateSpecializationScope(*this, |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4262 | Specialization->getPrimaryTemplate(), |
| 4263 | Specialization, FD->getLocation(), |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4264 | false)) |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4265 | return true; |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 4266 | |
| 4267 | // C++ [temp.expl.spec]p6: |
| 4268 | // 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] | 4269 | // explicitly specialized then that specialization shall be declared |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 4270 | // before the first use of that specialization that would cause an implicit |
| 4271 | // instantiation to take place, in every translation unit in which such a |
| 4272 | // use occurs; no diagnostic is required. |
| 4273 | FunctionTemplateSpecializationInfo *SpecInfo |
| 4274 | = Specialization->getTemplateSpecializationInfo(); |
| 4275 | assert(SpecInfo && "Function template specialization info missing?"); |
John McCall | 7504239 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 4276 | |
| 4277 | bool SuppressNew = false; |
John McCall | 7ad650f | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 4278 | if (!isFriend && |
| 4279 | CheckSpecializationInstantiationRedecl(FD->getLocation(), |
John McCall | 7504239 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 4280 | TSK_ExplicitSpecialization, |
| 4281 | Specialization, |
| 4282 | SpecInfo->getTemplateSpecializationKind(), |
| 4283 | SpecInfo->getPointOfInstantiation(), |
| 4284 | SuppressNew)) |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 4285 | return true; |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4286 | |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4287 | // Mark the prior declaration as an explicit specialization, so that later |
| 4288 | // clients know that this is an explicit specialization. |
John McCall | 7ad650f | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 4289 | if (!isFriend) |
| 4290 | SpecInfo->setTemplateSpecializationKind(TSK_ExplicitSpecialization); |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4291 | |
| 4292 | // Turn the given function declaration into a function template |
| 4293 | // specialization, with the template arguments from the previous |
| 4294 | // specialization. |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 4295 | FD->setFunctionTemplateSpecialization(Specialization->getPrimaryTemplate(), |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4296 | new (Context) TemplateArgumentList( |
| 4297 | *Specialization->getTemplateSpecializationArgs()), |
| 4298 | /*InsertPos=*/0, |
John McCall | 7ad650f | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 4299 | SpecInfo->getTemplateSpecializationKind()); |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4300 | |
| 4301 | // The "previous declaration" for this function template specialization is |
| 4302 | // the prior function template specialization. |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 4303 | Previous.clear(); |
| 4304 | Previous.addDecl(Specialization); |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4305 | return false; |
| 4306 | } |
| 4307 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4308 | /// \brief Perform semantic analysis for the given non-template member |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 4309 | /// specialization. |
| 4310 | /// |
| 4311 | /// This routine performs all of the semantic analysis required for an |
| 4312 | /// explicit member function specialization. On successful completion, |
| 4313 | /// the function declaration \p FD will become a member function |
| 4314 | /// specialization. |
| 4315 | /// |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4316 | /// \param Member the member declaration, which will be updated to become a |
| 4317 | /// specialization. |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 4318 | /// |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 4319 | /// \param Previous the set of declarations, one of which may be specialized |
| 4320 | /// by this function specialization; the set will be modified to contain the |
| 4321 | /// redeclared member. |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 4322 | bool |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 4323 | Sema::CheckMemberSpecialization(NamedDecl *Member, LookupResult &Previous) { |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4324 | assert(!isa<TemplateDecl>(Member) && "Only for non-template members"); |
John McCall | 77e8b11 | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 4325 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4326 | // Try to find the member we are instantiating. |
| 4327 | NamedDecl *Instantiation = 0; |
| 4328 | NamedDecl *InstantiatedFrom = 0; |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 4329 | MemberSpecializationInfo *MSInfo = 0; |
| 4330 | |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 4331 | if (Previous.empty()) { |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4332 | // Nowhere to look anyway. |
| 4333 | } else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Member)) { |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 4334 | for (LookupResult::iterator I = Previous.begin(), E = Previous.end(); |
| 4335 | I != E; ++I) { |
| 4336 | NamedDecl *D = (*I)->getUnderlyingDecl(); |
| 4337 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) { |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4338 | if (Context.hasSameType(Function->getType(), Method->getType())) { |
| 4339 | Instantiation = Method; |
| 4340 | InstantiatedFrom = Method->getInstantiatedFromMemberFunction(); |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 4341 | MSInfo = Method->getMemberSpecializationInfo(); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4342 | break; |
| 4343 | } |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 4344 | } |
| 4345 | } |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4346 | } else if (isa<VarDecl>(Member)) { |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 4347 | VarDecl *PrevVar; |
| 4348 | if (Previous.isSingleResult() && |
| 4349 | (PrevVar = dyn_cast<VarDecl>(Previous.getFoundDecl()))) |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4350 | if (PrevVar->isStaticDataMember()) { |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 4351 | Instantiation = PrevVar; |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4352 | InstantiatedFrom = PrevVar->getInstantiatedFromStaticDataMember(); |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 4353 | MSInfo = PrevVar->getMemberSpecializationInfo(); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4354 | } |
| 4355 | } else if (isa<RecordDecl>(Member)) { |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 4356 | CXXRecordDecl *PrevRecord; |
| 4357 | if (Previous.isSingleResult() && |
| 4358 | (PrevRecord = dyn_cast<CXXRecordDecl>(Previous.getFoundDecl()))) { |
| 4359 | Instantiation = PrevRecord; |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4360 | InstantiatedFrom = PrevRecord->getInstantiatedFromMemberClass(); |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 4361 | MSInfo = PrevRecord->getMemberSpecializationInfo(); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4362 | } |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 4363 | } |
| 4364 | |
| 4365 | if (!Instantiation) { |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4366 | // There is no previous declaration that matches. Since member |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 4367 | // specializations are always out-of-line, the caller will complain about |
| 4368 | // this mismatch later. |
| 4369 | return false; |
| 4370 | } |
John McCall | 77e8b11 | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 4371 | |
| 4372 | // If this is a friend, just bail out here before we start turning |
| 4373 | // things into explicit specializations. |
| 4374 | if (Member->getFriendObjectKind() != Decl::FOK_None) { |
| 4375 | // Preserve instantiation information. |
| 4376 | if (InstantiatedFrom && isa<CXXMethodDecl>(Member)) { |
| 4377 | cast<CXXMethodDecl>(Member)->setInstantiationOfMemberFunction( |
| 4378 | cast<CXXMethodDecl>(InstantiatedFrom), |
| 4379 | cast<CXXMethodDecl>(Instantiation)->getTemplateSpecializationKind()); |
| 4380 | } else if (InstantiatedFrom && isa<CXXRecordDecl>(Member)) { |
| 4381 | cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass( |
| 4382 | cast<CXXRecordDecl>(InstantiatedFrom), |
| 4383 | cast<CXXRecordDecl>(Instantiation)->getTemplateSpecializationKind()); |
| 4384 | } |
| 4385 | |
| 4386 | Previous.clear(); |
| 4387 | Previous.addDecl(Instantiation); |
| 4388 | return false; |
| 4389 | } |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 4390 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4391 | // Make sure that this is a specialization of a member. |
| 4392 | if (!InstantiatedFrom) { |
| 4393 | Diag(Member->getLocation(), diag::err_spec_member_not_instantiated) |
| 4394 | << Member; |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 4395 | Diag(Instantiation->getLocation(), diag::note_specialized_decl); |
| 4396 | return true; |
| 4397 | } |
| 4398 | |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 4399 | // C++ [temp.expl.spec]p6: |
| 4400 | // If a template, a member template or the member of a class template is |
| 4401 | // explicitly specialized then that spe- cialization shall be declared |
| 4402 | // before the first use of that specialization that would cause an implicit |
| 4403 | // instantiation to take place, in every translation unit in which such a |
| 4404 | // use occurs; no diagnostic is required. |
| 4405 | assert(MSInfo && "Member specialization info missing?"); |
John McCall | 7504239 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 4406 | |
| 4407 | bool SuppressNew = false; |
| 4408 | if (CheckSpecializationInstantiationRedecl(Member->getLocation(), |
| 4409 | TSK_ExplicitSpecialization, |
| 4410 | Instantiation, |
| 4411 | MSInfo->getTemplateSpecializationKind(), |
| 4412 | MSInfo->getPointOfInstantiation(), |
| 4413 | SuppressNew)) |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 4414 | return true; |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 4415 | |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 4416 | // Check the scope of this explicit specialization. |
| 4417 | if (CheckTemplateSpecializationScope(*this, |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4418 | InstantiatedFrom, |
| 4419 | Instantiation, Member->getLocation(), |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4420 | false)) |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 4421 | return true; |
Douglas Gregor | 2db3232 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 4422 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4423 | // Note that this is an explicit instantiation of a member. |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 4424 | // the original declaration to note that it is an explicit specialization |
| 4425 | // (if it was previously an implicit instantiation). This latter step |
| 4426 | // makes bookkeeping easier. |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4427 | if (isa<FunctionDecl>(Member)) { |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 4428 | FunctionDecl *InstantiationFunction = cast<FunctionDecl>(Instantiation); |
| 4429 | if (InstantiationFunction->getTemplateSpecializationKind() == |
| 4430 | TSK_ImplicitInstantiation) { |
| 4431 | InstantiationFunction->setTemplateSpecializationKind( |
| 4432 | TSK_ExplicitSpecialization); |
| 4433 | InstantiationFunction->setLocation(Member->getLocation()); |
| 4434 | } |
| 4435 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4436 | cast<FunctionDecl>(Member)->setInstantiationOfMemberFunction( |
| 4437 | cast<CXXMethodDecl>(InstantiatedFrom), |
| 4438 | TSK_ExplicitSpecialization); |
| 4439 | } else if (isa<VarDecl>(Member)) { |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 4440 | VarDecl *InstantiationVar = cast<VarDecl>(Instantiation); |
| 4441 | if (InstantiationVar->getTemplateSpecializationKind() == |
| 4442 | TSK_ImplicitInstantiation) { |
| 4443 | InstantiationVar->setTemplateSpecializationKind( |
| 4444 | TSK_ExplicitSpecialization); |
| 4445 | InstantiationVar->setLocation(Member->getLocation()); |
| 4446 | } |
| 4447 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4448 | Context.setInstantiatedFromStaticDataMember(cast<VarDecl>(Member), |
| 4449 | cast<VarDecl>(InstantiatedFrom), |
| 4450 | TSK_ExplicitSpecialization); |
| 4451 | } else { |
| 4452 | assert(isa<CXXRecordDecl>(Member) && "Only member classes remain"); |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 4453 | CXXRecordDecl *InstantiationClass = cast<CXXRecordDecl>(Instantiation); |
| 4454 | if (InstantiationClass->getTemplateSpecializationKind() == |
| 4455 | TSK_ImplicitInstantiation) { |
| 4456 | InstantiationClass->setTemplateSpecializationKind( |
| 4457 | TSK_ExplicitSpecialization); |
| 4458 | InstantiationClass->setLocation(Member->getLocation()); |
| 4459 | } |
| 4460 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4461 | cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass( |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 4462 | cast<CXXRecordDecl>(InstantiatedFrom), |
| 4463 | TSK_ExplicitSpecialization); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4464 | } |
| 4465 | |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 4466 | // Save the caller the trouble of having to figure out which declaration |
| 4467 | // this specialization matches. |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 4468 | Previous.clear(); |
| 4469 | Previous.addDecl(Instantiation); |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 4470 | return false; |
| 4471 | } |
| 4472 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 4473 | /// \brief Check the scope of an explicit instantiation. |
| 4474 | static void CheckExplicitInstantiationScope(Sema &S, NamedDecl *D, |
| 4475 | SourceLocation InstLoc, |
| 4476 | bool WasQualifiedName) { |
| 4477 | DeclContext *ExpectedContext |
| 4478 | = D->getDeclContext()->getEnclosingNamespaceContext()->getLookupContext(); |
| 4479 | DeclContext *CurContext = S.CurContext->getLookupContext(); |
| 4480 | |
| 4481 | // C++0x [temp.explicit]p2: |
| 4482 | // An explicit instantiation shall appear in an enclosing namespace of its |
| 4483 | // template. |
| 4484 | // |
| 4485 | // This is DR275, which we do not retroactively apply to C++98/03. |
| 4486 | if (S.getLangOptions().CPlusPlus0x && |
| 4487 | !CurContext->Encloses(ExpectedContext)) { |
| 4488 | if (NamespaceDecl *NS = dyn_cast<NamespaceDecl>(ExpectedContext)) |
| 4489 | S.Diag(InstLoc, diag::err_explicit_instantiation_out_of_scope) |
| 4490 | << D << NS; |
| 4491 | else |
| 4492 | S.Diag(InstLoc, diag::err_explicit_instantiation_must_be_global) |
| 4493 | << D; |
| 4494 | S.Diag(D->getLocation(), diag::note_explicit_instantiation_here); |
| 4495 | return; |
| 4496 | } |
| 4497 | |
| 4498 | // C++0x [temp.explicit]p2: |
| 4499 | // If the name declared in the explicit instantiation is an unqualified |
| 4500 | // name, the explicit instantiation shall appear in the namespace where |
| 4501 | // its template is declared or, if that namespace is inline (7.3.1), any |
| 4502 | // namespace from its enclosing namespace set. |
| 4503 | if (WasQualifiedName) |
| 4504 | return; |
| 4505 | |
| 4506 | if (CurContext->Equals(ExpectedContext)) |
| 4507 | return; |
| 4508 | |
| 4509 | S.Diag(InstLoc, diag::err_explicit_instantiation_unqualified_wrong_namespace) |
| 4510 | << D << ExpectedContext; |
| 4511 | S.Diag(D->getLocation(), diag::note_explicit_instantiation_here); |
| 4512 | } |
| 4513 | |
| 4514 | /// \brief Determine whether the given scope specifier has a template-id in it. |
| 4515 | static bool ScopeSpecifierHasTemplateId(const CXXScopeSpec &SS) { |
| 4516 | if (!SS.isSet()) |
| 4517 | return false; |
| 4518 | |
| 4519 | // C++0x [temp.explicit]p2: |
| 4520 | // If the explicit instantiation is for a member function, a member class |
| 4521 | // or a static data member of a class template specialization, the name of |
| 4522 | // the class template specialization in the qualified-id for the member |
| 4523 | // name shall be a simple-template-id. |
| 4524 | // |
| 4525 | // C++98 has the same restriction, just worded differently. |
| 4526 | for (NestedNameSpecifier *NNS = (NestedNameSpecifier *)SS.getScopeRep(); |
| 4527 | NNS; NNS = NNS->getPrefix()) |
| 4528 | if (Type *T = NNS->getAsType()) |
| 4529 | if (isa<TemplateSpecializationType>(T)) |
| 4530 | return true; |
| 4531 | |
| 4532 | return false; |
| 4533 | } |
| 4534 | |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 4535 | // Explicit instantiation of a class template specialization |
Douglas Gregor | 45f9655 | 2009-09-04 06:33:52 +0000 | [diff] [blame] | 4536 | // FIXME: Implement extern template semantics |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4537 | Sema::DeclResult |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4538 | Sema::ActOnExplicitInstantiation(Scope *S, |
Douglas Gregor | 45f9655 | 2009-09-04 06:33:52 +0000 | [diff] [blame] | 4539 | SourceLocation ExternLoc, |
| 4540 | SourceLocation TemplateLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4541 | unsigned TagSpec, |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4542 | SourceLocation KWLoc, |
| 4543 | const CXXScopeSpec &SS, |
| 4544 | TemplateTy TemplateD, |
| 4545 | SourceLocation TemplateNameLoc, |
| 4546 | SourceLocation LAngleLoc, |
| 4547 | ASTTemplateArgsPtr TemplateArgsIn, |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4548 | SourceLocation RAngleLoc, |
| 4549 | AttributeList *Attr) { |
| 4550 | // Find the class template we're specializing |
| 4551 | TemplateName Name = TemplateD.getAsVal<TemplateName>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4552 | ClassTemplateDecl *ClassTemplate |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4553 | = cast<ClassTemplateDecl>(Name.getAsTemplateDecl()); |
| 4554 | |
| 4555 | // Check that the specialization uses the same tag kind as the |
| 4556 | // original template. |
| 4557 | TagDecl::TagKind Kind; |
| 4558 | switch (TagSpec) { |
| 4559 | default: assert(0 && "Unknown tag type!"); |
| 4560 | case DeclSpec::TST_struct: Kind = TagDecl::TK_struct; break; |
| 4561 | case DeclSpec::TST_union: Kind = TagDecl::TK_union; break; |
| 4562 | case DeclSpec::TST_class: Kind = TagDecl::TK_class; break; |
| 4563 | } |
Douglas Gregor | 501c5ce | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 4564 | if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4565 | Kind, KWLoc, |
Douglas Gregor | 501c5ce | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 4566 | *ClassTemplate->getIdentifier())) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4567 | Diag(KWLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4568 | << ClassTemplate |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 4569 | << FixItHint::CreateReplacement(KWLoc, |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4570 | ClassTemplate->getTemplatedDecl()->getKindName()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4571 | Diag(ClassTemplate->getTemplatedDecl()->getLocation(), |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4572 | diag::note_previous_use); |
| 4573 | Kind = ClassTemplate->getTemplatedDecl()->getTagKind(); |
| 4574 | } |
| 4575 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 4576 | // C++0x [temp.explicit]p2: |
| 4577 | // There are two forms of explicit instantiation: an explicit instantiation |
| 4578 | // definition and an explicit instantiation declaration. An explicit |
| 4579 | // instantiation declaration begins with the extern keyword. [...] |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4580 | TemplateSpecializationKind TSK |
| 4581 | = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition |
| 4582 | : TSK_ExplicitInstantiationDeclaration; |
| 4583 | |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4584 | // Translate the parser's template argument list in our AST format. |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 4585 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
Douglas Gregor | 314b97f | 2009-11-10 19:49:08 +0000 | [diff] [blame] | 4586 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4587 | |
| 4588 | // Check that the template argument list is well-formed for this |
| 4589 | // template. |
Anders Carlsson | fb25052 | 2009-06-23 01:26:57 +0000 | [diff] [blame] | 4590 | TemplateArgumentListBuilder Converted(ClassTemplate->getTemplateParameters(), |
| 4591 | TemplateArgs.size()); |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 4592 | if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc, |
| 4593 | TemplateArgs, false, Converted)) |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4594 | return true; |
| 4595 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4596 | assert((Converted.structuredSize() == |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4597 | ClassTemplate->getTemplateParameters()->size()) && |
| 4598 | "Converted template argument list is too short!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4599 | |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4600 | // Find the class template specialization declaration that |
| 4601 | // corresponds to these arguments. |
| 4602 | llvm::FoldingSetNodeID ID; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4603 | ClassTemplateSpecializationDecl::Profile(ID, |
Anders Carlsson | fb25052 | 2009-06-23 01:26:57 +0000 | [diff] [blame] | 4604 | Converted.getFlatArguments(), |
Douglas Gregor | 828e226 | 2009-07-29 16:09:57 +0000 | [diff] [blame] | 4605 | Converted.flatSize(), |
| 4606 | Context); |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4607 | void *InsertPos = 0; |
| 4608 | ClassTemplateSpecializationDecl *PrevDecl |
| 4609 | = ClassTemplate->getSpecializations().FindNodeOrInsertPos(ID, InsertPos); |
| 4610 | |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4611 | // C++0x [temp.explicit]p2: |
| 4612 | // [...] An explicit instantiation shall appear in an enclosing |
| 4613 | // namespace of its template. [...] |
| 4614 | // |
| 4615 | // This is C++ DR 275. |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 4616 | CheckExplicitInstantiationScope(*this, ClassTemplate, TemplateNameLoc, |
| 4617 | SS.isSet()); |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4618 | |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4619 | ClassTemplateSpecializationDecl *Specialization = 0; |
| 4620 | |
Douglas Gregor | d78f598 | 2009-11-25 06:01:46 +0000 | [diff] [blame] | 4621 | bool ReusedDecl = false; |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4622 | if (PrevDecl) { |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 4623 | bool SuppressNew = false; |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 4624 | if (CheckSpecializationInstantiationRedecl(TemplateNameLoc, TSK, |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 4625 | PrevDecl, |
| 4626 | PrevDecl->getSpecializationKind(), |
| 4627 | PrevDecl->getPointOfInstantiation(), |
| 4628 | SuppressNew)) |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4629 | return DeclPtrTy::make(PrevDecl); |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4630 | |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 4631 | if (SuppressNew) |
Douglas Gregor | 52604ab | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 4632 | return DeclPtrTy::make(PrevDecl); |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 4633 | |
Douglas Gregor | 52604ab | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 4634 | if (PrevDecl->getSpecializationKind() == TSK_ImplicitInstantiation || |
| 4635 | PrevDecl->getSpecializationKind() == TSK_Undeclared) { |
| 4636 | // Since the only prior class template specialization with these |
| 4637 | // arguments was referenced but not declared, reuse that |
| 4638 | // declaration node as our own, updating its source location to |
| 4639 | // reflect our new declaration. |
| 4640 | Specialization = PrevDecl; |
| 4641 | Specialization->setLocation(TemplateNameLoc); |
| 4642 | PrevDecl = 0; |
Douglas Gregor | d78f598 | 2009-11-25 06:01:46 +0000 | [diff] [blame] | 4643 | ReusedDecl = true; |
Douglas Gregor | 52604ab | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 4644 | } |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 4645 | } |
Douglas Gregor | 52604ab | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 4646 | |
| 4647 | if (!Specialization) { |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4648 | // Create a new class template specialization declaration node for |
| 4649 | // this explicit specialization. |
| 4650 | Specialization |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4651 | = ClassTemplateSpecializationDecl::Create(Context, |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4652 | ClassTemplate->getDeclContext(), |
| 4653 | TemplateNameLoc, |
| 4654 | ClassTemplate, |
Douglas Gregor | 52604ab | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 4655 | Converted, PrevDecl); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 4656 | SetNestedNameSpecifier(Specialization, SS); |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4657 | |
Douglas Gregor | 52604ab | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 4658 | if (PrevDecl) { |
| 4659 | // Remove the previous declaration from the folding set, since we want |
| 4660 | // to introduce a new declaration. |
| 4661 | ClassTemplate->getSpecializations().RemoveNode(PrevDecl); |
| 4662 | ClassTemplate->getSpecializations().FindNodeOrInsertPos(ID, InsertPos); |
| 4663 | } |
| 4664 | |
| 4665 | // Insert the new specialization. |
| 4666 | ClassTemplate->getSpecializations().InsertNode(Specialization, InsertPos); |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4667 | } |
| 4668 | |
| 4669 | // Build the fully-sugared type for this explicit instantiation as |
| 4670 | // the user wrote in the explicit instantiation itself. This means |
| 4671 | // that we'll pretty-print the type retrieved from the |
| 4672 | // specialization's declaration the way that the user actually wrote |
| 4673 | // the explicit instantiation, rather than formatting the name based |
| 4674 | // on the "canonical" representation used to store the template |
| 4675 | // arguments in the specialization. |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 4676 | TypeSourceInfo *WrittenTy |
| 4677 | = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc, |
| 4678 | TemplateArgs, |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4679 | Context.getTypeDeclType(Specialization)); |
| 4680 | Specialization->setTypeAsWritten(WrittenTy); |
| 4681 | TemplateArgsIn.release(); |
| 4682 | |
Douglas Gregor | d78f598 | 2009-11-25 06:01:46 +0000 | [diff] [blame] | 4683 | if (!ReusedDecl) { |
| 4684 | // Add the explicit instantiation into its lexical context. However, |
| 4685 | // since explicit instantiations are never found by name lookup, we |
| 4686 | // just put it into the declaration context directly. |
| 4687 | Specialization->setLexicalDeclContext(CurContext); |
| 4688 | CurContext->addDecl(Specialization); |
| 4689 | } |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4690 | |
| 4691 | // C++ [temp.explicit]p3: |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4692 | // A definition of a class template or class member template |
| 4693 | // shall be in scope at the point of the explicit instantiation of |
| 4694 | // the class template or class member template. |
| 4695 | // |
| 4696 | // This check comes when we actually try to perform the |
| 4697 | // instantiation. |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 4698 | ClassTemplateSpecializationDecl *Def |
| 4699 | = cast_or_null<ClassTemplateSpecializationDecl>( |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 4700 | Specialization->getDefinition()); |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 4701 | if (!Def) |
Douglas Gregor | 972e6ce | 2009-10-27 06:26:26 +0000 | [diff] [blame] | 4702 | InstantiateClassTemplateSpecialization(TemplateNameLoc, Specialization, TSK); |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 4703 | |
| 4704 | // Instantiate the members of this class template specialization. |
| 4705 | Def = cast_or_null<ClassTemplateSpecializationDecl>( |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 4706 | Specialization->getDefinition()); |
Rafael Espindola | b0f65ca | 2010-03-22 23:12:48 +0000 | [diff] [blame] | 4707 | if (Def) { |
Rafael Espindola | f075b22 | 2010-03-23 19:55:22 +0000 | [diff] [blame] | 4708 | TemplateSpecializationKind Old_TSK = Def->getTemplateSpecializationKind(); |
| 4709 | |
| 4710 | // Fix a TSK_ExplicitInstantiationDeclaration followed by a |
| 4711 | // TSK_ExplicitInstantiationDefinition |
| 4712 | if (Old_TSK == TSK_ExplicitInstantiationDeclaration && |
| 4713 | TSK == TSK_ExplicitInstantiationDefinition) |
| 4714 | Def->setTemplateSpecializationKind(TSK); |
Rafael Espindola | b0f65ca | 2010-03-22 23:12:48 +0000 | [diff] [blame] | 4715 | |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 4716 | InstantiateClassTemplateSpecializationMembers(TemplateNameLoc, Def, TSK); |
Rafael Espindola | b0f65ca | 2010-03-22 23:12:48 +0000 | [diff] [blame] | 4717 | } |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4718 | |
| 4719 | return DeclPtrTy::make(Specialization); |
| 4720 | } |
| 4721 | |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 4722 | // Explicit instantiation of a member class of a class template. |
| 4723 | Sema::DeclResult |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4724 | Sema::ActOnExplicitInstantiation(Scope *S, |
Douglas Gregor | 45f9655 | 2009-09-04 06:33:52 +0000 | [diff] [blame] | 4725 | SourceLocation ExternLoc, |
| 4726 | SourceLocation TemplateLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4727 | unsigned TagSpec, |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 4728 | SourceLocation KWLoc, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 4729 | CXXScopeSpec &SS, |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 4730 | IdentifierInfo *Name, |
| 4731 | SourceLocation NameLoc, |
| 4732 | AttributeList *Attr) { |
| 4733 | |
Douglas Gregor | 402abb5 | 2009-05-28 23:31:59 +0000 | [diff] [blame] | 4734 | bool Owned = false; |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 4735 | bool IsDependent = false; |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 4736 | DeclPtrTy TagD = ActOnTag(S, TagSpec, Action::TUK_Reference, |
Douglas Gregor | 7cdbc58 | 2009-07-22 23:48:44 +0000 | [diff] [blame] | 4737 | KWLoc, SS, Name, NameLoc, Attr, AS_none, |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 4738 | MultiTemplateParamsArg(*this, 0, 0), |
| 4739 | Owned, IsDependent); |
| 4740 | assert(!IsDependent && "explicit instantiation of dependent name not yet handled"); |
| 4741 | |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 4742 | if (!TagD) |
| 4743 | return true; |
| 4744 | |
| 4745 | TagDecl *Tag = cast<TagDecl>(TagD.getAs<Decl>()); |
| 4746 | if (Tag->isEnum()) { |
| 4747 | Diag(TemplateLoc, diag::err_explicit_instantiation_enum) |
| 4748 | << Context.getTypeDeclType(Tag); |
| 4749 | return true; |
| 4750 | } |
| 4751 | |
Douglas Gregor | d0c8737 | 2009-05-27 17:30:49 +0000 | [diff] [blame] | 4752 | if (Tag->isInvalidDecl()) |
| 4753 | return true; |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 4754 | |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 4755 | CXXRecordDecl *Record = cast<CXXRecordDecl>(Tag); |
| 4756 | CXXRecordDecl *Pattern = Record->getInstantiatedFromMemberClass(); |
| 4757 | if (!Pattern) { |
| 4758 | Diag(TemplateLoc, diag::err_explicit_instantiation_nontemplate_type) |
| 4759 | << Context.getTypeDeclType(Record); |
| 4760 | Diag(Record->getLocation(), diag::note_nontemplate_decl_here); |
| 4761 | return true; |
| 4762 | } |
| 4763 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 4764 | // C++0x [temp.explicit]p2: |
| 4765 | // If the explicit instantiation is for a class or member class, the |
| 4766 | // elaborated-type-specifier in the declaration shall include a |
| 4767 | // simple-template-id. |
| 4768 | // |
| 4769 | // C++98 has the same restriction, just worded differently. |
| 4770 | if (!ScopeSpecifierHasTemplateId(SS)) |
| 4771 | Diag(TemplateLoc, diag::err_explicit_instantiation_without_qualified_id) |
| 4772 | << Record << SS.getRange(); |
| 4773 | |
| 4774 | // C++0x [temp.explicit]p2: |
| 4775 | // There are two forms of explicit instantiation: an explicit instantiation |
| 4776 | // definition and an explicit instantiation declaration. An explicit |
| 4777 | // instantiation declaration begins with the extern keyword. [...] |
Douglas Gregor | a74bbe2 | 2009-10-14 21:46:58 +0000 | [diff] [blame] | 4778 | TemplateSpecializationKind TSK |
| 4779 | = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition |
| 4780 | : TSK_ExplicitInstantiationDeclaration; |
| 4781 | |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 4782 | // C++0x [temp.explicit]p2: |
| 4783 | // [...] An explicit instantiation shall appear in an enclosing |
| 4784 | // namespace of its template. [...] |
| 4785 | // |
| 4786 | // This is C++ DR 275. |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 4787 | CheckExplicitInstantiationScope(*this, Record, NameLoc, true); |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4788 | |
| 4789 | // Verify that it is okay to explicitly instantiate here. |
Douglas Gregor | 583f33b | 2009-10-15 18:07:02 +0000 | [diff] [blame] | 4790 | CXXRecordDecl *PrevDecl |
| 4791 | = cast_or_null<CXXRecordDecl>(Record->getPreviousDeclaration()); |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 4792 | if (!PrevDecl && Record->getDefinition()) |
Douglas Gregor | 583f33b | 2009-10-15 18:07:02 +0000 | [diff] [blame] | 4793 | PrevDecl = Record; |
| 4794 | if (PrevDecl) { |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4795 | MemberSpecializationInfo *MSInfo = PrevDecl->getMemberSpecializationInfo(); |
| 4796 | bool SuppressNew = false; |
| 4797 | assert(MSInfo && "No member specialization information?"); |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 4798 | if (CheckSpecializationInstantiationRedecl(TemplateLoc, TSK, |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4799 | PrevDecl, |
| 4800 | MSInfo->getTemplateSpecializationKind(), |
| 4801 | MSInfo->getPointOfInstantiation(), |
| 4802 | SuppressNew)) |
| 4803 | return true; |
| 4804 | if (SuppressNew) |
| 4805 | return TagD; |
| 4806 | } |
| 4807 | |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 4808 | CXXRecordDecl *RecordDef |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 4809 | = cast_or_null<CXXRecordDecl>(Record->getDefinition()); |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 4810 | if (!RecordDef) { |
Douglas Gregor | bf7643e | 2009-10-15 12:53:22 +0000 | [diff] [blame] | 4811 | // C++ [temp.explicit]p3: |
| 4812 | // A definition of a member class of a class template shall be in scope |
| 4813 | // at the point of an explicit instantiation of the member class. |
| 4814 | CXXRecordDecl *Def |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 4815 | = cast_or_null<CXXRecordDecl>(Pattern->getDefinition()); |
Douglas Gregor | bf7643e | 2009-10-15 12:53:22 +0000 | [diff] [blame] | 4816 | if (!Def) { |
Douglas Gregor | e2d3a3d | 2009-10-15 14:05:49 +0000 | [diff] [blame] | 4817 | Diag(TemplateLoc, diag::err_explicit_instantiation_undefined_member) |
| 4818 | << 0 << Record->getDeclName() << Record->getDeclContext(); |
Douglas Gregor | bf7643e | 2009-10-15 12:53:22 +0000 | [diff] [blame] | 4819 | Diag(Pattern->getLocation(), diag::note_forward_declaration) |
| 4820 | << Pattern; |
| 4821 | return true; |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 4822 | } else { |
| 4823 | if (InstantiateClass(NameLoc, Record, Def, |
| 4824 | getTemplateInstantiationArgs(Record), |
| 4825 | TSK)) |
| 4826 | return true; |
| 4827 | |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 4828 | RecordDef = cast_or_null<CXXRecordDecl>(Record->getDefinition()); |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 4829 | if (!RecordDef) |
| 4830 | return true; |
| 4831 | } |
| 4832 | } |
| 4833 | |
| 4834 | // Instantiate all of the members of the class. |
| 4835 | InstantiateClassMembers(NameLoc, RecordDef, |
| 4836 | getTemplateInstantiationArgs(Record), TSK); |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 4837 | |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 4838 | // FIXME: We don't have any representation for explicit instantiations of |
| 4839 | // member classes. Such a representation is not needed for compilation, but it |
| 4840 | // should be available for clients that want to see all of the declarations in |
| 4841 | // the source code. |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 4842 | return TagD; |
| 4843 | } |
| 4844 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4845 | Sema::DeclResult Sema::ActOnExplicitInstantiation(Scope *S, |
| 4846 | SourceLocation ExternLoc, |
| 4847 | SourceLocation TemplateLoc, |
| 4848 | Declarator &D) { |
| 4849 | // Explicit instantiations always require a name. |
| 4850 | DeclarationName Name = GetNameForDeclarator(D); |
| 4851 | if (!Name) { |
| 4852 | if (!D.isInvalidType()) |
| 4853 | Diag(D.getDeclSpec().getSourceRange().getBegin(), |
| 4854 | diag::err_explicit_instantiation_requires_name) |
| 4855 | << D.getDeclSpec().getSourceRange() |
| 4856 | << D.getSourceRange(); |
| 4857 | |
| 4858 | return true; |
| 4859 | } |
| 4860 | |
| 4861 | // The scope passed in may not be a decl scope. Zip up the scope tree until |
| 4862 | // we find one that is. |
| 4863 | while ((S->getFlags() & Scope::DeclScope) == 0 || |
| 4864 | (S->getFlags() & Scope::TemplateParamScope) != 0) |
| 4865 | S = S->getParent(); |
| 4866 | |
| 4867 | // Determine the type of the declaration. |
| 4868 | QualType R = GetTypeForDeclarator(D, S, 0); |
| 4869 | if (R.isNull()) |
| 4870 | return true; |
| 4871 | |
| 4872 | if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef) { |
| 4873 | // Cannot explicitly instantiate a typedef. |
| 4874 | Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_of_typedef) |
| 4875 | << Name; |
| 4876 | return true; |
| 4877 | } |
| 4878 | |
Douglas Gregor | 663b5a0 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 4879 | // C++0x [temp.explicit]p1: |
| 4880 | // [...] An explicit instantiation of a function template shall not use the |
| 4881 | // inline or constexpr specifiers. |
| 4882 | // Presumably, this also applies to member functions of class templates as |
| 4883 | // well. |
| 4884 | if (D.getDeclSpec().isInlineSpecified() && getLangOptions().CPlusPlus0x) |
| 4885 | Diag(D.getDeclSpec().getInlineSpecLoc(), |
| 4886 | diag::err_explicit_instantiation_inline) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 4887 | <<FixItHint::CreateRemoval(D.getDeclSpec().getInlineSpecLoc()); |
Douglas Gregor | 663b5a0 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 4888 | |
| 4889 | // FIXME: check for constexpr specifier. |
| 4890 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 4891 | // C++0x [temp.explicit]p2: |
| 4892 | // There are two forms of explicit instantiation: an explicit instantiation |
| 4893 | // definition and an explicit instantiation declaration. An explicit |
| 4894 | // instantiation declaration begins with the extern keyword. [...] |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4895 | TemplateSpecializationKind TSK |
| 4896 | = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition |
| 4897 | : TSK_ExplicitInstantiationDeclaration; |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 4898 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 4899 | LookupResult Previous(*this, Name, D.getIdentifierLoc(), LookupOrdinaryName); |
| 4900 | LookupParsedName(Previous, S, &D.getCXXScopeSpec()); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4901 | |
| 4902 | if (!R->isFunctionType()) { |
| 4903 | // C++ [temp.explicit]p1: |
| 4904 | // A [...] static data member of a class template can be explicitly |
| 4905 | // instantiated from the member definition associated with its class |
| 4906 | // template. |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 4907 | if (Previous.isAmbiguous()) |
| 4908 | return true; |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4909 | |
John McCall | 1bcee0a | 2009-12-02 08:25:40 +0000 | [diff] [blame] | 4910 | VarDecl *Prev = Previous.getAsSingle<VarDecl>(); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4911 | if (!Prev || !Prev->isStaticDataMember()) { |
| 4912 | // We expect to see a data data member here. |
| 4913 | Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_not_known) |
| 4914 | << Name; |
| 4915 | for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end(); |
| 4916 | P != PEnd; ++P) |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 4917 | Diag((*P)->getLocation(), diag::note_explicit_instantiation_here); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4918 | return true; |
| 4919 | } |
| 4920 | |
| 4921 | if (!Prev->getInstantiatedFromStaticDataMember()) { |
| 4922 | // FIXME: Check for explicit specialization? |
| 4923 | Diag(D.getIdentifierLoc(), |
| 4924 | diag::err_explicit_instantiation_data_member_not_instantiated) |
| 4925 | << Prev; |
| 4926 | Diag(Prev->getLocation(), diag::note_explicit_instantiation_here); |
| 4927 | // FIXME: Can we provide a note showing where this was declared? |
| 4928 | return true; |
| 4929 | } |
| 4930 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 4931 | // C++0x [temp.explicit]p2: |
| 4932 | // If the explicit instantiation is for a member function, a member class |
| 4933 | // or a static data member of a class template specialization, the name of |
| 4934 | // the class template specialization in the qualified-id for the member |
| 4935 | // name shall be a simple-template-id. |
| 4936 | // |
| 4937 | // C++98 has the same restriction, just worded differently. |
| 4938 | if (!ScopeSpecifierHasTemplateId(D.getCXXScopeSpec())) |
| 4939 | Diag(D.getIdentifierLoc(), |
| 4940 | diag::err_explicit_instantiation_without_qualified_id) |
| 4941 | << Prev << D.getCXXScopeSpec().getRange(); |
| 4942 | |
| 4943 | // Check the scope of this explicit instantiation. |
| 4944 | CheckExplicitInstantiationScope(*this, Prev, D.getIdentifierLoc(), true); |
| 4945 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4946 | // Verify that it is okay to explicitly instantiate here. |
| 4947 | MemberSpecializationInfo *MSInfo = Prev->getMemberSpecializationInfo(); |
| 4948 | assert(MSInfo && "Missing static data member specialization info?"); |
| 4949 | bool SuppressNew = false; |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 4950 | if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK, Prev, |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4951 | MSInfo->getTemplateSpecializationKind(), |
| 4952 | MSInfo->getPointOfInstantiation(), |
| 4953 | SuppressNew)) |
| 4954 | return true; |
| 4955 | if (SuppressNew) |
| 4956 | return DeclPtrTy(); |
| 4957 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4958 | // Instantiate static data member. |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 4959 | Prev->setTemplateSpecializationKind(TSK, D.getIdentifierLoc()); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4960 | if (TSK == TSK_ExplicitInstantiationDefinition) |
Douglas Gregor | e2d3a3d | 2009-10-15 14:05:49 +0000 | [diff] [blame] | 4961 | InstantiateStaticDataMemberDefinition(D.getIdentifierLoc(), Prev, false, |
| 4962 | /*DefinitionRequired=*/true); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4963 | |
| 4964 | // FIXME: Create an ExplicitInstantiation node? |
| 4965 | return DeclPtrTy(); |
| 4966 | } |
| 4967 | |
Douglas Gregor | 0b60d9e | 2009-09-25 23:53:26 +0000 | [diff] [blame] | 4968 | // If the declarator is a template-id, translate the parser's template |
| 4969 | // argument list into our AST format. |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 4970 | bool HasExplicitTemplateArgs = false; |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 4971 | TemplateArgumentListInfo TemplateArgs; |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 4972 | if (D.getName().getKind() == UnqualifiedId::IK_TemplateId) { |
| 4973 | TemplateIdAnnotation *TemplateId = D.getName().TemplateId; |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 4974 | TemplateArgs.setLAngleLoc(TemplateId->LAngleLoc); |
| 4975 | TemplateArgs.setRAngleLoc(TemplateId->RAngleLoc); |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 4976 | ASTTemplateArgsPtr TemplateArgsPtr(*this, |
| 4977 | TemplateId->getTemplateArgs(), |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 4978 | TemplateId->NumArgs); |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 4979 | translateTemplateArguments(TemplateArgsPtr, TemplateArgs); |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 4980 | HasExplicitTemplateArgs = true; |
Douglas Gregor | b2f81cf | 2009-10-01 23:51:25 +0000 | [diff] [blame] | 4981 | TemplateArgsPtr.release(); |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 4982 | } |
Douglas Gregor | 0b60d9e | 2009-09-25 23:53:26 +0000 | [diff] [blame] | 4983 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4984 | // C++ [temp.explicit]p1: |
| 4985 | // A [...] function [...] can be explicitly instantiated from its template. |
| 4986 | // A member function [...] of a class template can be explicitly |
| 4987 | // instantiated from the member definition associated with its class |
| 4988 | // template. |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4989 | UnresolvedSet<8> Matches; |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4990 | for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end(); |
| 4991 | P != PEnd; ++P) { |
| 4992 | NamedDecl *Prev = *P; |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 4993 | if (!HasExplicitTemplateArgs) { |
| 4994 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Prev)) { |
| 4995 | if (Context.hasSameUnqualifiedType(Method->getType(), R)) { |
| 4996 | Matches.clear(); |
Douglas Gregor | 48026d2 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 4997 | |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4998 | Matches.addDecl(Method, P.getAccess()); |
Douglas Gregor | 48026d2 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 4999 | if (Method->getTemplateSpecializationKind() == TSK_Undeclared) |
| 5000 | break; |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 5001 | } |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5002 | } |
| 5003 | } |
| 5004 | |
| 5005 | FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Prev); |
| 5006 | if (!FunTmpl) |
| 5007 | continue; |
| 5008 | |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 5009 | TemplateDeductionInfo Info(Context, D.getIdentifierLoc()); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5010 | FunctionDecl *Specialization = 0; |
| 5011 | if (TemplateDeductionResult TDK |
Douglas Gregor | 48026d2 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 5012 | = DeduceTemplateArguments(FunTmpl, |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 5013 | (HasExplicitTemplateArgs ? &TemplateArgs : 0), |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5014 | R, Specialization, Info)) { |
| 5015 | // FIXME: Keep track of almost-matches? |
| 5016 | (void)TDK; |
| 5017 | continue; |
| 5018 | } |
| 5019 | |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5020 | Matches.addDecl(Specialization, P.getAccess()); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5021 | } |
| 5022 | |
| 5023 | // Find the most specialized function template specialization. |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5024 | UnresolvedSetIterator Result |
| 5025 | = getMostSpecialized(Matches.begin(), Matches.end(), TPOC_Other, |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5026 | D.getIdentifierLoc(), |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 5027 | PDiag(diag::err_explicit_instantiation_not_known) << Name, |
| 5028 | PDiag(diag::err_explicit_instantiation_ambiguous) << Name, |
| 5029 | PDiag(diag::note_explicit_instantiation_candidate)); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5030 | |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5031 | if (Result == Matches.end()) |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5032 | return true; |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5033 | |
| 5034 | // Ignore access control bits, we don't need them for redeclaration checking. |
| 5035 | FunctionDecl *Specialization = cast<FunctionDecl>(*Result); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5036 | |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 5037 | if (Specialization->getTemplateSpecializationKind() == TSK_Undeclared) { |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5038 | Diag(D.getIdentifierLoc(), |
| 5039 | diag::err_explicit_instantiation_member_function_not_instantiated) |
| 5040 | << Specialization |
| 5041 | << (Specialization->getTemplateSpecializationKind() == |
| 5042 | TSK_ExplicitSpecialization); |
| 5043 | Diag(Specialization->getLocation(), diag::note_explicit_instantiation_here); |
| 5044 | return true; |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 5045 | } |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5046 | |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 5047 | FunctionDecl *PrevDecl = Specialization->getPreviousDeclaration(); |
Douglas Gregor | 583f33b | 2009-10-15 18:07:02 +0000 | [diff] [blame] | 5048 | if (!PrevDecl && Specialization->isThisDeclarationADefinition()) |
| 5049 | PrevDecl = Specialization; |
| 5050 | |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 5051 | if (PrevDecl) { |
| 5052 | bool SuppressNew = false; |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 5053 | if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK, |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 5054 | PrevDecl, |
| 5055 | PrevDecl->getTemplateSpecializationKind(), |
| 5056 | PrevDecl->getPointOfInstantiation(), |
| 5057 | SuppressNew)) |
| 5058 | return true; |
| 5059 | |
| 5060 | // FIXME: We may still want to build some representation of this |
| 5061 | // explicit specialization. |
| 5062 | if (SuppressNew) |
| 5063 | return DeclPtrTy(); |
| 5064 | } |
Anders Carlsson | 26d6e9d | 2009-11-24 05:34:41 +0000 | [diff] [blame] | 5065 | |
| 5066 | Specialization->setTemplateSpecializationKind(TSK, D.getIdentifierLoc()); |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 5067 | |
| 5068 | if (TSK == TSK_ExplicitInstantiationDefinition) |
| 5069 | InstantiateFunctionDefinition(D.getIdentifierLoc(), Specialization, |
| 5070 | false, /*DefinitionRequired=*/true); |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 5071 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5072 | // C++0x [temp.explicit]p2: |
| 5073 | // If the explicit instantiation is for a member function, a member class |
| 5074 | // or a static data member of a class template specialization, the name of |
| 5075 | // the class template specialization in the qualified-id for the member |
| 5076 | // name shall be a simple-template-id. |
| 5077 | // |
| 5078 | // C++98 has the same restriction, just worded differently. |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 5079 | FunctionTemplateDecl *FunTmpl = Specialization->getPrimaryTemplate(); |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 5080 | if (D.getName().getKind() != UnqualifiedId::IK_TemplateId && !FunTmpl && |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5081 | D.getCXXScopeSpec().isSet() && |
| 5082 | !ScopeSpecifierHasTemplateId(D.getCXXScopeSpec())) |
| 5083 | Diag(D.getIdentifierLoc(), |
| 5084 | diag::err_explicit_instantiation_without_qualified_id) |
| 5085 | << Specialization << D.getCXXScopeSpec().getRange(); |
| 5086 | |
| 5087 | CheckExplicitInstantiationScope(*this, |
| 5088 | FunTmpl? (NamedDecl *)FunTmpl |
| 5089 | : Specialization->getInstantiatedFromMemberFunction(), |
| 5090 | D.getIdentifierLoc(), |
| 5091 | D.getCXXScopeSpec().isSet()); |
| 5092 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5093 | // FIXME: Create some kind of ExplicitInstantiationDecl here. |
| 5094 | return DeclPtrTy(); |
| 5095 | } |
| 5096 | |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5097 | Sema::TypeResult |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 5098 | Sema::ActOnDependentTag(Scope *S, unsigned TagSpec, TagUseKind TUK, |
| 5099 | const CXXScopeSpec &SS, IdentifierInfo *Name, |
| 5100 | SourceLocation TagLoc, SourceLocation NameLoc) { |
| 5101 | // This has to hold, because SS is expected to be defined. |
| 5102 | assert(Name && "Expected a name in a dependent tag"); |
| 5103 | |
| 5104 | NestedNameSpecifier *NNS |
| 5105 | = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); |
| 5106 | if (!NNS) |
| 5107 | return true; |
| 5108 | |
Daniel Dunbar | 12c0ade | 2010-04-01 16:50:48 +0000 | [diff] [blame] | 5109 | ElaboratedTypeKeyword Keyword = ETK_None; |
Douglas Gregor | 4033642 | 2010-03-31 22:19:08 +0000 | [diff] [blame] | 5110 | switch (TagDecl::getTagKindForTypeSpec(TagSpec)) { |
| 5111 | case TagDecl::TK_struct: Keyword = ETK_Struct; break; |
| 5112 | case TagDecl::TK_class: Keyword = ETK_Class; break; |
| 5113 | case TagDecl::TK_union: Keyword = ETK_Union; break; |
| 5114 | case TagDecl::TK_enum: Keyword = ETK_Enum; break; |
| 5115 | } |
Daniel Dunbar | 12c0ade | 2010-04-01 16:50:48 +0000 | [diff] [blame] | 5116 | assert(Keyword != ETK_None && "Invalid tag kind!"); |
| 5117 | |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 5118 | if (TUK == TUK_Declaration || TUK == TUK_Definition) { |
| 5119 | Diag(NameLoc, diag::err_dependent_tag_decl) |
| 5120 | << (TUK == TUK_Definition) << TagDecl::getTagKindForTypeSpec(TagSpec) |
| 5121 | << SS.getRange(); |
| 5122 | return true; |
| 5123 | } |
| 5124 | |
Douglas Gregor | 4033642 | 2010-03-31 22:19:08 +0000 | [diff] [blame] | 5125 | return Context.getDependentNameType(Keyword, NNS, Name).getAsOpaquePtr(); |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 5126 | } |
| 5127 | |
| 5128 | Sema::TypeResult |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5129 | Sema::ActOnTypenameType(SourceLocation TypenameLoc, const CXXScopeSpec &SS, |
| 5130 | const IdentifierInfo &II, SourceLocation IdLoc) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5131 | NestedNameSpecifier *NNS |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5132 | = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); |
| 5133 | if (!NNS) |
| 5134 | return true; |
| 5135 | |
Douglas Gregor | 107de90 | 2010-04-24 15:35:55 +0000 | [diff] [blame] | 5136 | QualType T = CheckTypenameType(ETK_Typename, NNS, II, |
| 5137 | SourceRange(TypenameLoc, IdLoc)); |
Douglas Gregor | 31a19b6 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 5138 | if (T.isNull()) |
| 5139 | return true; |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5140 | return T.getAsOpaquePtr(); |
| 5141 | } |
| 5142 | |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 5143 | Sema::TypeResult |
| 5144 | Sema::ActOnTypenameType(SourceLocation TypenameLoc, const CXXScopeSpec &SS, |
| 5145 | SourceLocation TemplateLoc, TypeTy *Ty) { |
Argyrios Kyrtzidis | e866190 | 2009-08-19 01:28:28 +0000 | [diff] [blame] | 5146 | QualType T = GetTypeFromParser(Ty); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5147 | NestedNameSpecifier *NNS |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 5148 | = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5149 | const TemplateSpecializationType *TemplateId |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5150 | = T->getAs<TemplateSpecializationType>(); |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 5151 | assert(TemplateId && "Expected a template specialization type"); |
| 5152 | |
Douglas Gregor | 6946baf | 2009-09-02 13:05:45 +0000 | [diff] [blame] | 5153 | if (computeDeclContext(SS, false)) { |
| 5154 | // If we can compute a declaration context, then the "typename" |
| 5155 | // keyword was superfluous. Just build a QualifiedNameType to keep |
| 5156 | // track of the nested-name-specifier. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5157 | |
Douglas Gregor | 6946baf | 2009-09-02 13:05:45 +0000 | [diff] [blame] | 5158 | // FIXME: Note that the QualifiedNameType had the "typename" keyword! |
| 5159 | return Context.getQualifiedNameType(NNS, T).getAsOpaquePtr(); |
| 5160 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5161 | |
Douglas Gregor | 4a2023f | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 5162 | return Context.getDependentNameType(ETK_Typename, NNS, TemplateId) |
| 5163 | .getAsOpaquePtr(); |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 5164 | } |
| 5165 | |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5166 | /// \brief Build the type that describes a C++ typename specifier, |
| 5167 | /// e.g., "typename T::type". |
| 5168 | QualType |
Douglas Gregor | 107de90 | 2010-04-24 15:35:55 +0000 | [diff] [blame] | 5169 | Sema::CheckTypenameType(ElaboratedTypeKeyword Keyword, |
| 5170 | NestedNameSpecifier *NNS, const IdentifierInfo &II, |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5171 | SourceRange Range) { |
Douglas Gregor | 42af25f | 2009-05-11 19:58:34 +0000 | [diff] [blame] | 5172 | CXXRecordDecl *CurrentInstantiation = 0; |
| 5173 | if (NNS->isDependent()) { |
| 5174 | CurrentInstantiation = getCurrentInstantiationOf(NNS); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5175 | |
Douglas Gregor | 42af25f | 2009-05-11 19:58:34 +0000 | [diff] [blame] | 5176 | // If the nested-name-specifier does not refer to the current |
| 5177 | // instantiation, then build a typename type. |
| 5178 | if (!CurrentInstantiation) |
Douglas Gregor | 107de90 | 2010-04-24 15:35:55 +0000 | [diff] [blame] | 5179 | return Context.getDependentNameType(Keyword, NNS, &II); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5180 | |
Douglas Gregor | de18d12 | 2009-09-02 13:12:51 +0000 | [diff] [blame] | 5181 | // The nested-name-specifier refers to the current instantiation, so the |
| 5182 | // "typename" keyword itself is superfluous. In C++03, the program is |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5183 | // actually ill-formed. However, DR 382 (in C++0x CD1) allows such |
Douglas Gregor | de18d12 | 2009-09-02 13:12:51 +0000 | [diff] [blame] | 5184 | // extraneous "typename" keywords, and we retroactively apply this DR to |
| 5185 | // C++03 code. |
Douglas Gregor | 42af25f | 2009-05-11 19:58:34 +0000 | [diff] [blame] | 5186 | } |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5187 | |
Douglas Gregor | 42af25f | 2009-05-11 19:58:34 +0000 | [diff] [blame] | 5188 | DeclContext *Ctx = 0; |
| 5189 | |
| 5190 | if (CurrentInstantiation) |
| 5191 | Ctx = CurrentInstantiation; |
| 5192 | else { |
| 5193 | CXXScopeSpec SS; |
| 5194 | SS.setScopeRep(NNS); |
| 5195 | SS.setRange(Range); |
| 5196 | if (RequireCompleteDeclContext(SS)) |
| 5197 | return QualType(); |
| 5198 | |
| 5199 | Ctx = computeDeclContext(SS); |
| 5200 | } |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5201 | assert(Ctx && "No declaration context?"); |
| 5202 | |
| 5203 | DeclarationName Name(&II); |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 5204 | LookupResult Result(*this, Name, Range.getEnd(), LookupOrdinaryName); |
| 5205 | LookupQualifiedName(Result, Ctx); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5206 | unsigned DiagID = 0; |
| 5207 | Decl *Referenced = 0; |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 5208 | switch (Result.getResultKind()) { |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5209 | case LookupResult::NotFound: |
Douglas Gregor | 3f09327 | 2009-10-13 21:16:44 +0000 | [diff] [blame] | 5210 | DiagID = diag::err_typename_nested_not_found; |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5211 | break; |
Douglas Gregor | 7d3f576 | 2010-01-15 01:44:47 +0000 | [diff] [blame] | 5212 | |
| 5213 | case LookupResult::NotFoundInCurrentInstantiation: |
| 5214 | // Okay, it's a member of an unknown instantiation. |
Douglas Gregor | 107de90 | 2010-04-24 15:35:55 +0000 | [diff] [blame] | 5215 | return Context.getDependentNameType(Keyword, NNS, &II); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5216 | |
| 5217 | case LookupResult::Found: |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 5218 | if (TypeDecl *Type = dyn_cast<TypeDecl>(Result.getFoundDecl())) { |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5219 | // We found a type. Build a QualifiedNameType, since the |
| 5220 | // typename-specifier was just sugar. FIXME: Tell |
| 5221 | // QualifiedNameType that it has a "typename" prefix. |
| 5222 | return Context.getQualifiedNameType(NNS, Context.getTypeDeclType(Type)); |
| 5223 | } |
| 5224 | |
| 5225 | DiagID = diag::err_typename_nested_not_type; |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 5226 | Referenced = Result.getFoundDecl(); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5227 | break; |
| 5228 | |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 5229 | case LookupResult::FoundUnresolvedValue: |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 5230 | llvm_unreachable("unresolved using decl in non-dependent context"); |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 5231 | return QualType(); |
| 5232 | |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5233 | case LookupResult::FoundOverloaded: |
| 5234 | DiagID = diag::err_typename_nested_not_type; |
| 5235 | Referenced = *Result.begin(); |
| 5236 | break; |
| 5237 | |
John McCall | 6e24726 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 5238 | case LookupResult::Ambiguous: |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5239 | return QualType(); |
| 5240 | } |
| 5241 | |
| 5242 | // If we get here, it's because name lookup did not find a |
| 5243 | // type. Emit an appropriate diagnostic and return an error. |
Douglas Gregor | 3f09327 | 2009-10-13 21:16:44 +0000 | [diff] [blame] | 5244 | Diag(Range.getEnd(), DiagID) << Range << Name << Ctx; |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5245 | if (Referenced) |
| 5246 | Diag(Referenced->getLocation(), diag::note_typename_refers_here) |
| 5247 | << Name; |
| 5248 | return QualType(); |
| 5249 | } |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 5250 | |
| 5251 | namespace { |
| 5252 | // See Sema::RebuildTypeInCurrentInstantiation |
Benjamin Kramer | 85b4521 | 2009-11-28 19:45:26 +0000 | [diff] [blame] | 5253 | class CurrentInstantiationRebuilder |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5254 | : public TreeTransform<CurrentInstantiationRebuilder> { |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 5255 | SourceLocation Loc; |
| 5256 | DeclarationName Entity; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5257 | |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 5258 | public: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5259 | CurrentInstantiationRebuilder(Sema &SemaRef, |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 5260 | SourceLocation Loc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5261 | DeclarationName Entity) |
| 5262 | : TreeTransform<CurrentInstantiationRebuilder>(SemaRef), |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 5263 | Loc(Loc), Entity(Entity) { } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5264 | |
| 5265 | /// \brief Determine whether the given type \p T has already been |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 5266 | /// transformed. |
| 5267 | /// |
| 5268 | /// For the purposes of type reconstruction, a type has already been |
| 5269 | /// transformed if it is NULL or if it is not dependent. |
| 5270 | bool AlreadyTransformed(QualType T) { |
| 5271 | return T.isNull() || !T->isDependentType(); |
| 5272 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5273 | |
| 5274 | /// \brief Returns the location of the entity whose type is being |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 5275 | /// rebuilt. |
| 5276 | SourceLocation getBaseLocation() { return Loc; } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5277 | |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 5278 | /// \brief Returns the name of the entity whose type is being rebuilt. |
| 5279 | DeclarationName getBaseEntity() { return Entity; } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5280 | |
Douglas Gregor | 972e6ce | 2009-10-27 06:26:26 +0000 | [diff] [blame] | 5281 | /// \brief Sets the "base" location and entity when that |
| 5282 | /// information is known based on another transformation. |
| 5283 | void setBase(SourceLocation Loc, DeclarationName Entity) { |
| 5284 | this->Loc = Loc; |
| 5285 | this->Entity = Entity; |
| 5286 | } |
| 5287 | |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 5288 | /// \brief Transforms an expression by returning the expression itself |
| 5289 | /// (an identity function). |
| 5290 | /// |
| 5291 | /// FIXME: This is completely unsafe; we will need to actually clone the |
| 5292 | /// expressions. |
| 5293 | Sema::OwningExprResult TransformExpr(Expr *E) { |
| 5294 | return getSema().Owned(E); |
| 5295 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5296 | |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 5297 | /// \brief Transforms a typename type by determining whether the type now |
| 5298 | /// refers to a member of the current instantiation, and then |
| 5299 | /// type-checking and building a QualifiedNameType (when possible). |
Douglas Gregor | 4714c12 | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 5300 | QualType TransformDependentNameType(TypeLocBuilder &TLB, DependentNameTypeLoc TL, |
Douglas Gregor | 124b878 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 5301 | QualType ObjectType); |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 5302 | }; |
| 5303 | } |
| 5304 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5305 | QualType |
Douglas Gregor | 4714c12 | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 5306 | CurrentInstantiationRebuilder::TransformDependentNameType(TypeLocBuilder &TLB, |
| 5307 | DependentNameTypeLoc TL, |
Douglas Gregor | 124b878 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 5308 | QualType ObjectType) { |
Douglas Gregor | 4714c12 | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 5309 | DependentNameType *T = TL.getTypePtr(); |
John McCall | a2becad | 2009-10-21 00:40:46 +0000 | [diff] [blame] | 5310 | |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 5311 | NestedNameSpecifier *NNS |
| 5312 | = TransformNestedNameSpecifier(T->getQualifier(), |
Douglas Gregor | 124b878 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 5313 | /*FIXME:*/SourceRange(getBaseLocation()), |
| 5314 | ObjectType); |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 5315 | if (!NNS) |
| 5316 | return QualType(); |
| 5317 | |
| 5318 | // If the nested-name-specifier did not change, and we cannot compute the |
| 5319 | // context corresponding to the nested-name-specifier, then this |
| 5320 | // typename type will not change; exit early. |
| 5321 | CXXScopeSpec SS; |
| 5322 | SS.setRange(SourceRange(getBaseLocation())); |
| 5323 | SS.setScopeRep(NNS); |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 5324 | |
| 5325 | QualType Result; |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 5326 | if (NNS == T->getQualifier() && getSema().computeDeclContext(SS) == 0) |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 5327 | Result = QualType(T, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5328 | |
| 5329 | // Rebuild the typename type, which will probably turn into a |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 5330 | // QualifiedNameType. |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 5331 | else if (const TemplateSpecializationType *TemplateId = T->getTemplateId()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5332 | QualType NewTemplateId |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 5333 | = TransformType(QualType(TemplateId, 0)); |
| 5334 | if (NewTemplateId.isNull()) |
| 5335 | return QualType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5336 | |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 5337 | if (NNS == T->getQualifier() && |
| 5338 | NewTemplateId == QualType(TemplateId, 0)) |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 5339 | Result = QualType(T, 0); |
| 5340 | else |
Douglas Gregor | 4a2023f | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 5341 | Result = getDerived().RebuildDependentNameType(T->getKeyword(), |
| 5342 | NNS, NewTemplateId); |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 5343 | } else |
Douglas Gregor | 4a2023f | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 5344 | Result = getDerived().RebuildDependentNameType(T->getKeyword(), |
| 5345 | NNS, T->getIdentifier(), |
| 5346 | SourceRange(TL.getNameLoc())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5347 | |
Douglas Gregor | a50ce32 | 2010-03-07 23:26:22 +0000 | [diff] [blame] | 5348 | if (Result.isNull()) |
| 5349 | return QualType(); |
| 5350 | |
Douglas Gregor | 4714c12 | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 5351 | DependentNameTypeLoc NewTL = TLB.push<DependentNameTypeLoc>(Result); |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 5352 | NewTL.setNameLoc(TL.getNameLoc()); |
| 5353 | return Result; |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 5354 | } |
| 5355 | |
| 5356 | /// \brief Rebuilds a type within the context of the current instantiation. |
| 5357 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5358 | /// 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] | 5359 | /// a class template (or class template partial specialization) that was parsed |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5360 | /// and constructed before we entered the scope of the class template (or |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 5361 | /// partial specialization thereof). This routine will rebuild that type now |
| 5362 | /// that we have entered the declarator's scope, which may produce different |
| 5363 | /// canonical types, e.g., |
| 5364 | /// |
| 5365 | /// \code |
| 5366 | /// template<typename T> |
| 5367 | /// struct X { |
| 5368 | /// typedef T* pointer; |
| 5369 | /// pointer data(); |
| 5370 | /// }; |
| 5371 | /// |
| 5372 | /// template<typename T> |
| 5373 | /// typename X<T>::pointer X<T>::data() { ... } |
| 5374 | /// \endcode |
| 5375 | /// |
Douglas Gregor | 4714c12 | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 5376 | /// 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] | 5377 | /// since we do not know that we can look into X<T> when we parsed the type. |
| 5378 | /// This function will rebuild the type, performing the lookup of "pointer" |
| 5379 | /// in X<T> and returning a QualifiedNameType whose canonical type is the same |
| 5380 | /// as the canonical type of T*, allowing the return types of the out-of-line |
| 5381 | /// definition and the declaration to match. |
| 5382 | QualType Sema::RebuildTypeInCurrentInstantiation(QualType T, SourceLocation Loc, |
| 5383 | DeclarationName Name) { |
| 5384 | if (T.isNull() || !T->isDependentType()) |
| 5385 | return T; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5386 | |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 5387 | CurrentInstantiationRebuilder Rebuilder(*this, Loc, Name); |
| 5388 | return Rebuilder.TransformType(T); |
Benjamin Kramer | 27ba2f0 | 2009-08-11 22:33:06 +0000 | [diff] [blame] | 5389 | } |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 5390 | |
| 5391 | /// \brief Produces a formatted string that describes the binding of |
| 5392 | /// template parameters to template arguments. |
| 5393 | std::string |
| 5394 | Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params, |
| 5395 | const TemplateArgumentList &Args) { |
Douglas Gregor | 9148c3f | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 5396 | // FIXME: For variadic templates, we'll need to get the structured list. |
| 5397 | return getTemplateArgumentBindingsText(Params, Args.getFlatArgumentList(), |
| 5398 | Args.flat_size()); |
| 5399 | } |
| 5400 | |
| 5401 | std::string |
| 5402 | Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params, |
| 5403 | const TemplateArgument *Args, |
| 5404 | unsigned NumArgs) { |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 5405 | std::string Result; |
| 5406 | |
Douglas Gregor | 9148c3f | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 5407 | if (!Params || Params->size() == 0 || NumArgs == 0) |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 5408 | return Result; |
| 5409 | |
| 5410 | for (unsigned I = 0, N = Params->size(); I != N; ++I) { |
Douglas Gregor | 9148c3f | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 5411 | if (I >= NumArgs) |
| 5412 | break; |
| 5413 | |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 5414 | if (I == 0) |
| 5415 | Result += "[with "; |
| 5416 | else |
| 5417 | Result += ", "; |
| 5418 | |
| 5419 | if (const IdentifierInfo *Id = Params->getParam(I)->getIdentifier()) { |
| 5420 | Result += Id->getName(); |
| 5421 | } else { |
| 5422 | Result += '$'; |
| 5423 | Result += llvm::utostr(I); |
| 5424 | } |
| 5425 | |
| 5426 | Result += " = "; |
| 5427 | |
| 5428 | switch (Args[I].getKind()) { |
| 5429 | case TemplateArgument::Null: |
| 5430 | Result += "<no value>"; |
| 5431 | break; |
| 5432 | |
| 5433 | case TemplateArgument::Type: { |
| 5434 | std::string TypeStr; |
| 5435 | Args[I].getAsType().getAsStringInternal(TypeStr, |
| 5436 | Context.PrintingPolicy); |
| 5437 | Result += TypeStr; |
| 5438 | break; |
| 5439 | } |
| 5440 | |
| 5441 | case TemplateArgument::Declaration: { |
| 5442 | bool Unnamed = true; |
| 5443 | if (NamedDecl *ND = dyn_cast_or_null<NamedDecl>(Args[I].getAsDecl())) { |
| 5444 | if (ND->getDeclName()) { |
| 5445 | Unnamed = false; |
| 5446 | Result += ND->getNameAsString(); |
| 5447 | } |
| 5448 | } |
| 5449 | |
| 5450 | if (Unnamed) { |
| 5451 | Result += "<anonymous>"; |
| 5452 | } |
| 5453 | break; |
| 5454 | } |
| 5455 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 5456 | case TemplateArgument::Template: { |
| 5457 | std::string Str; |
| 5458 | llvm::raw_string_ostream OS(Str); |
| 5459 | Args[I].getAsTemplate().print(OS, Context.PrintingPolicy); |
| 5460 | Result += OS.str(); |
| 5461 | break; |
| 5462 | } |
| 5463 | |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 5464 | case TemplateArgument::Integral: { |
| 5465 | Result += Args[I].getAsIntegral()->toString(10); |
| 5466 | break; |
| 5467 | } |
| 5468 | |
| 5469 | case TemplateArgument::Expression: { |
| 5470 | assert(false && "No expressions in deduced template arguments!"); |
| 5471 | Result += "<expression>"; |
| 5472 | break; |
| 5473 | } |
| 5474 | |
| 5475 | case TemplateArgument::Pack: |
| 5476 | // FIXME: Format template argument packs |
| 5477 | Result += "<template argument pack>"; |
| 5478 | break; |
| 5479 | } |
| 5480 | } |
| 5481 | |
| 5482 | Result += ']'; |
| 5483 | return Result; |
| 5484 | } |