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) { |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 1347 | TemplateParameterList *ExpectedTemplateParams = 0; |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 1348 | |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 1349 | // Are there cases in (e.g.) friends where this won't match? |
| 1350 | if (const InjectedClassNameType *Injected |
| 1351 | = TemplateId->getAs<InjectedClassNameType>()) { |
| 1352 | CXXRecordDecl *Record = Injected->getDecl(); |
| 1353 | if (ClassTemplatePartialSpecializationDecl *Partial = |
| 1354 | dyn_cast<ClassTemplatePartialSpecializationDecl>(Record)) |
| 1355 | ExpectedTemplateParams = Partial->getTemplateParameters(); |
| 1356 | else |
| 1357 | ExpectedTemplateParams = Record->getDescribedClassTemplate() |
| 1358 | ->getTemplateParameters(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1359 | } |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1360 | |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 1361 | if (ExpectedTemplateParams) |
| 1362 | TemplateParameterListsAreEqual(ParamLists[Idx], |
| 1363 | ExpectedTemplateParams, |
| 1364 | true, TPL_TemplateMatch); |
| 1365 | |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1366 | CheckTemplateParameterList(ParamLists[Idx], 0, TPC_ClassTemplateMember); |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 1367 | } else if (ParamLists[Idx]->size() > 0) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1368 | Diag(ParamLists[Idx]->getTemplateLoc(), |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 1369 | diag::err_template_param_list_matches_nontemplate) |
| 1370 | << TemplateId |
| 1371 | << ParamLists[Idx]->getSourceRange(); |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 1372 | else |
| 1373 | IsExplicitSpecialization = true; |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1374 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1375 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1376 | // If there were at least as many template-ids as there were template |
| 1377 | // parameter lists, then there are no template parameter lists remaining for |
| 1378 | // the declaration itself. |
| 1379 | if (Idx >= NumParamLists) |
| 1380 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1381 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1382 | // If there were too many template parameter lists, complain about that now. |
| 1383 | if (Idx != NumParamLists - 1) { |
| 1384 | while (Idx < NumParamLists - 1) { |
Douglas Gregor | 3ebd753 | 2009-11-23 12:11:45 +0000 | [diff] [blame] | 1385 | bool isExplicitSpecHeader = ParamLists[Idx]->size() == 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1386 | Diag(ParamLists[Idx]->getTemplateLoc(), |
Douglas Gregor | 3ebd753 | 2009-11-23 12:11:45 +0000 | [diff] [blame] | 1387 | isExplicitSpecHeader? diag::warn_template_spec_extra_headers |
| 1388 | : diag::err_template_spec_extra_headers) |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1389 | << SourceRange(ParamLists[Idx]->getTemplateLoc(), |
| 1390 | ParamLists[Idx]->getRAngleLoc()); |
Douglas Gregor | 3ebd753 | 2009-11-23 12:11:45 +0000 | [diff] [blame] | 1391 | |
| 1392 | if (isExplicitSpecHeader && !ExplicitSpecializationsInSpecifier.empty()) { |
| 1393 | Diag(ExplicitSpecializationsInSpecifier.back()->getLocation(), |
| 1394 | diag::note_explicit_template_spec_does_not_need_header) |
| 1395 | << ExplicitSpecializationsInSpecifier.back(); |
| 1396 | ExplicitSpecializationsInSpecifier.pop_back(); |
| 1397 | } |
| 1398 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1399 | ++Idx; |
| 1400 | } |
| 1401 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1402 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1403 | // Return the last template parameter list, which corresponds to the |
| 1404 | // entity being declared. |
| 1405 | return ParamLists[NumParamLists - 1]; |
| 1406 | } |
| 1407 | |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1408 | QualType Sema::CheckTemplateIdType(TemplateName Name, |
| 1409 | SourceLocation TemplateLoc, |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1410 | const TemplateArgumentListInfo &TemplateArgs) { |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1411 | TemplateDecl *Template = Name.getAsTemplateDecl(); |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 1412 | if (!Template) { |
| 1413 | // The template name does not resolve to a template, so we just |
| 1414 | // build a dependent template-id type. |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1415 | return Context.getTemplateSpecializationType(Name, TemplateArgs); |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 1416 | } |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1417 | |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1418 | // Check that the template argument list is well-formed for this |
| 1419 | // template. |
Anders Carlsson | fb25052 | 2009-06-23 01:26:57 +0000 | [diff] [blame] | 1420 | TemplateArgumentListBuilder Converted(Template->getTemplateParameters(), |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1421 | TemplateArgs.size()); |
| 1422 | if (CheckTemplateArgumentList(Template, TemplateLoc, TemplateArgs, |
Douglas Gregor | 16134c6 | 2009-07-01 00:28:38 +0000 | [diff] [blame] | 1423 | false, Converted)) |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1424 | return QualType(); |
| 1425 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1426 | assert((Converted.structuredSize() == |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1427 | Template->getTemplateParameters()->size()) && |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1428 | "Converted template argument list is too short!"); |
| 1429 | |
| 1430 | QualType CanonType; |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 1431 | bool IsCurrentInstantiation = false; |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1432 | |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 1433 | if (Name.isDependent() || |
| 1434 | TemplateSpecializationType::anyDependentTemplateArguments( |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1435 | TemplateArgs)) { |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1436 | // This class template specialization is a dependent |
| 1437 | // type. Therefore, its canonical type is another class template |
| 1438 | // specialization type that contains all of the converted |
| 1439 | // arguments in canonical form. This ensures that, e.g., A<T> and |
| 1440 | // A<T, T> have identical types when A is declared as: |
| 1441 | // |
| 1442 | // template<typename T, typename U = T> struct A; |
Douglas Gregor | 25a3ef7 | 2009-05-07 06:41:52 +0000 | [diff] [blame] | 1443 | TemplateName CanonName = Context.getCanonicalTemplateName(Name); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1444 | CanonType = Context.getTemplateSpecializationType(CanonName, |
Anders Carlsson | fb25052 | 2009-06-23 01:26:57 +0000 | [diff] [blame] | 1445 | Converted.getFlatArguments(), |
| 1446 | Converted.flatSize()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1447 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 1448 | // FIXME: CanonType is not actually the canonical type, and unfortunately |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1449 | // it is a TemplateSpecializationType that we will never use again. |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 1450 | // In the future, we need to teach getTemplateSpecializationType to only |
| 1451 | // build the canonical type and return that to us. |
| 1452 | CanonType = Context.getCanonicalType(CanonType); |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 1453 | |
| 1454 | // This might work out to be a current instantiation, in which |
| 1455 | // case the canonical type needs to be the InjectedClassNameType. |
| 1456 | // |
| 1457 | // TODO: in theory this could be a simple hashtable lookup; most |
| 1458 | // changes to CurContext don't change the set of current |
| 1459 | // instantiations. |
| 1460 | if (isa<ClassTemplateDecl>(Template)) { |
| 1461 | for (DeclContext *Ctx = CurContext; Ctx; Ctx = Ctx->getLookupParent()) { |
| 1462 | // If we get out to a namespace, we're done. |
| 1463 | if (Ctx->isFileContext()) break; |
| 1464 | |
| 1465 | // If this isn't a record, keep looking. |
| 1466 | CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Ctx); |
| 1467 | if (!Record) continue; |
| 1468 | |
| 1469 | // Look for one of the two cases with InjectedClassNameTypes |
| 1470 | // and check whether it's the same template. |
| 1471 | if (!isa<ClassTemplatePartialSpecializationDecl>(Record) && |
| 1472 | !Record->getDescribedClassTemplate()) |
| 1473 | continue; |
| 1474 | |
| 1475 | // Fetch the injected class name type and check whether its |
| 1476 | // injected type is equal to the type we just built. |
| 1477 | QualType ICNT = Context.getTypeDeclType(Record); |
| 1478 | QualType Injected = cast<InjectedClassNameType>(ICNT) |
| 1479 | ->getInjectedSpecializationType(); |
| 1480 | |
| 1481 | if (CanonType != Injected->getCanonicalTypeInternal()) |
| 1482 | continue; |
| 1483 | |
| 1484 | // If so, the canonical type of this TST is the injected |
| 1485 | // class name type of the record we just found. |
| 1486 | assert(ICNT.isCanonical()); |
| 1487 | CanonType = ICNT; |
| 1488 | IsCurrentInstantiation = true; |
| 1489 | break; |
| 1490 | } |
| 1491 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1492 | } else if (ClassTemplateDecl *ClassTemplate |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1493 | = dyn_cast<ClassTemplateDecl>(Template)) { |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1494 | // Find the class template specialization declaration that |
| 1495 | // corresponds to these arguments. |
| 1496 | llvm::FoldingSetNodeID ID; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1497 | ClassTemplateSpecializationDecl::Profile(ID, |
Anders Carlsson | fb25052 | 2009-06-23 01:26:57 +0000 | [diff] [blame] | 1498 | Converted.getFlatArguments(), |
Douglas Gregor | 828e226 | 2009-07-29 16:09:57 +0000 | [diff] [blame] | 1499 | Converted.flatSize(), |
| 1500 | Context); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1501 | void *InsertPos = 0; |
| 1502 | ClassTemplateSpecializationDecl *Decl |
| 1503 | = ClassTemplate->getSpecializations().FindNodeOrInsertPos(ID, InsertPos); |
| 1504 | if (!Decl) { |
| 1505 | // This is the first time we have referenced this class template |
| 1506 | // specialization. Create the canonical declaration and add it to |
| 1507 | // the set of specializations. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1508 | Decl = ClassTemplateSpecializationDecl::Create(Context, |
Anders Carlsson | 1c5976e | 2009-06-05 03:43:12 +0000 | [diff] [blame] | 1509 | ClassTemplate->getDeclContext(), |
John McCall | 9cc7807 | 2009-09-11 07:25:08 +0000 | [diff] [blame] | 1510 | ClassTemplate->getLocation(), |
Anders Carlsson | 1c5976e | 2009-06-05 03:43:12 +0000 | [diff] [blame] | 1511 | ClassTemplate, |
Anders Carlsson | fb25052 | 2009-06-23 01:26:57 +0000 | [diff] [blame] | 1512 | Converted, 0); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1513 | ClassTemplate->getSpecializations().InsertNode(Decl, InsertPos); |
| 1514 | Decl->setLexicalDeclContext(CurContext); |
| 1515 | } |
| 1516 | |
| 1517 | CanonType = Context.getTypeDeclType(Decl); |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 1518 | assert(isa<RecordType>(CanonType) && |
| 1519 | "type of non-dependent specialization is not a RecordType"); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1520 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1521 | |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1522 | // Build the fully-sugared type for this class template |
| 1523 | // specialization, which refers back to the class template |
| 1524 | // specialization we created or found. |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 1525 | return Context.getTemplateSpecializationType(Name, TemplateArgs, CanonType, |
| 1526 | IsCurrentInstantiation); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1527 | } |
| 1528 | |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 1529 | Action::TypeResult |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1530 | Sema::ActOnTemplateIdType(TemplateTy TemplateD, SourceLocation TemplateLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1531 | SourceLocation LAngleLoc, |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1532 | ASTTemplateArgsPtr TemplateArgsIn, |
John McCall | 6b2becf | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 1533 | SourceLocation RAngleLoc) { |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1534 | TemplateName Template = TemplateD.getAsVal<TemplateName>(); |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 1535 | |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1536 | // Translate the parser's template argument list in our AST format. |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1537 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
Douglas Gregor | 314b97f | 2009-11-10 19:49:08 +0000 | [diff] [blame] | 1538 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 1539 | |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1540 | QualType Result = CheckTemplateIdType(Template, TemplateLoc, TemplateArgs); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1541 | TemplateArgsIn.release(); |
Douglas Gregor | 31a19b6 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 1542 | |
| 1543 | if (Result.isNull()) |
| 1544 | return true; |
| 1545 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1546 | TypeSourceInfo *DI = Context.CreateTypeSourceInfo(Result); |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1547 | TemplateSpecializationTypeLoc TL |
| 1548 | = cast<TemplateSpecializationTypeLoc>(DI->getTypeLoc()); |
| 1549 | TL.setTemplateNameLoc(TemplateLoc); |
| 1550 | TL.setLAngleLoc(LAngleLoc); |
| 1551 | TL.setRAngleLoc(RAngleLoc); |
| 1552 | for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) |
| 1553 | TL.setArgLocInfo(i, TemplateArgs[i].getLocInfo()); |
| 1554 | |
| 1555 | return CreateLocInfoType(Result, DI).getAsOpaquePtr(); |
John McCall | 6b2becf | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 1556 | } |
John McCall | f1bbbb4 | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 1557 | |
John McCall | 6b2becf | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 1558 | Sema::TypeResult Sema::ActOnTagTemplateIdType(TypeResult TypeResult, |
| 1559 | TagUseKind TUK, |
| 1560 | DeclSpec::TST TagSpec, |
| 1561 | SourceLocation TagLoc) { |
| 1562 | if (TypeResult.isInvalid()) |
| 1563 | return Sema::TypeResult(); |
John McCall | f1bbbb4 | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 1564 | |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1565 | // FIXME: preserve source info, ideally without copying the DI. |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1566 | TypeSourceInfo *DI; |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1567 | QualType Type = GetTypeFromParser(TypeResult.get(), &DI); |
John McCall | f1bbbb4 | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 1568 | |
John McCall | 6b2becf | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 1569 | // Verify the tag specifier. |
| 1570 | TagDecl::TagKind TagKind = TagDecl::getTagKindForTypeSpec(TagSpec); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1571 | |
John McCall | 6b2becf | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 1572 | if (const RecordType *RT = Type->getAs<RecordType>()) { |
| 1573 | RecordDecl *D = RT->getDecl(); |
| 1574 | |
| 1575 | IdentifierInfo *Id = D->getIdentifier(); |
| 1576 | assert(Id && "templated class must have an identifier"); |
| 1577 | |
| 1578 | if (!isAcceptableTagRedeclaration(D, TagKind, TagLoc, *Id)) { |
| 1579 | Diag(TagLoc, diag::err_use_with_wrong_tag) |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 1580 | << Type |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 1581 | << FixItHint::CreateReplacement(SourceRange(TagLoc), D->getKindName()); |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 1582 | Diag(D->getLocation(), diag::note_previous_use); |
John McCall | f1bbbb4 | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 1583 | } |
| 1584 | } |
| 1585 | |
John McCall | 6b2becf | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 1586 | QualType ElabType = Context.getElaboratedType(Type, TagKind); |
| 1587 | |
| 1588 | return ElabType.getAsOpaquePtr(); |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 1589 | } |
| 1590 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1591 | Sema::OwningExprResult Sema::BuildTemplateIdExpr(const CXXScopeSpec &SS, |
| 1592 | LookupResult &R, |
| 1593 | bool RequiresADL, |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1594 | const TemplateArgumentListInfo &TemplateArgs) { |
Douglas Gregor | edce4dd | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 1595 | // FIXME: Can we do any checking at this point? I guess we could check the |
| 1596 | // template arguments that we have against the template name, if the template |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1597 | // 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] | 1598 | // though. |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1599 | |
| 1600 | // These should be filtered out by our callers. |
| 1601 | assert(!R.empty() && "empty lookup results when building templateid"); |
| 1602 | assert(!R.isAmbiguous() && "ambiguous lookup when building templateid"); |
| 1603 | |
| 1604 | NestedNameSpecifier *Qualifier = 0; |
| 1605 | SourceRange QualifierRange; |
| 1606 | if (SS.isSet()) { |
| 1607 | Qualifier = static_cast<NestedNameSpecifier*>(SS.getScopeRep()); |
| 1608 | QualifierRange = SS.getRange(); |
Douglas Gregor | a9e29aa | 2009-10-22 07:19:14 +0000 | [diff] [blame] | 1609 | } |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 1610 | |
| 1611 | // We don't want lookup warnings at this point. |
| 1612 | R.suppressDiagnostics(); |
Douglas Gregor | a9e29aa | 2009-10-22 07:19:14 +0000 | [diff] [blame] | 1613 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1614 | bool Dependent |
| 1615 | = UnresolvedLookupExpr::ComputeDependence(R.begin(), R.end(), |
| 1616 | &TemplateArgs); |
| 1617 | UnresolvedLookupExpr *ULE |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 1618 | = UnresolvedLookupExpr::Create(Context, Dependent, R.getNamingClass(), |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1619 | Qualifier, QualifierRange, |
| 1620 | R.getLookupName(), R.getNameLoc(), |
| 1621 | RequiresADL, TemplateArgs); |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 1622 | ULE->addDecls(R.begin(), R.end()); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1623 | |
| 1624 | return Owned(ULE); |
Douglas Gregor | edce4dd | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 1625 | } |
| 1626 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1627 | // We actually only call this from template instantiation. |
| 1628 | Sema::OwningExprResult |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 1629 | Sema::BuildQualifiedTemplateIdExpr(CXXScopeSpec &SS, |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1630 | DeclarationName Name, |
| 1631 | SourceLocation NameLoc, |
| 1632 | const TemplateArgumentListInfo &TemplateArgs) { |
| 1633 | DeclContext *DC; |
| 1634 | if (!(DC = computeDeclContext(SS, false)) || |
| 1635 | DC->isDependentContext() || |
| 1636 | RequireCompleteDeclContext(SS)) |
| 1637 | return BuildDependentDeclRefExpr(SS, Name, NameLoc, &TemplateArgs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1638 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1639 | LookupResult R(*this, Name, NameLoc, LookupOrdinaryName); |
| 1640 | LookupTemplateName(R, (Scope*) 0, SS, QualType(), /*Entering*/ false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1641 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1642 | if (R.isAmbiguous()) |
| 1643 | return ExprError(); |
| 1644 | |
| 1645 | if (R.empty()) { |
| 1646 | Diag(NameLoc, diag::err_template_kw_refers_to_non_template) |
| 1647 | << Name << SS.getRange(); |
| 1648 | return ExprError(); |
| 1649 | } |
| 1650 | |
| 1651 | if (ClassTemplateDecl *Temp = R.getAsSingle<ClassTemplateDecl>()) { |
| 1652 | Diag(NameLoc, diag::err_template_kw_refers_to_class_template) |
| 1653 | << (NestedNameSpecifier*) SS.getScopeRep() << Name << SS.getRange(); |
| 1654 | Diag(Temp->getLocation(), diag::note_referenced_class_template); |
| 1655 | return ExprError(); |
| 1656 | } |
| 1657 | |
| 1658 | return BuildTemplateIdExpr(SS, R, /* ADL */ false, TemplateArgs); |
Douglas Gregor | edce4dd | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 1659 | } |
| 1660 | |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 1661 | /// \brief Form a dependent template name. |
| 1662 | /// |
| 1663 | /// This action forms a dependent template name given the template |
| 1664 | /// name and its (presumably dependent) scope specifier. For |
| 1665 | /// example, given "MetaFun::template apply", the scope specifier \p |
| 1666 | /// SS will be "MetaFun::", \p TemplateKWLoc contains the location |
| 1667 | /// of the "template" keyword, and "apply" is the \p Name. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1668 | Sema::TemplateTy |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 1669 | Sema::ActOnDependentTemplateName(SourceLocation TemplateKWLoc, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 1670 | CXXScopeSpec &SS, |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 1671 | UnqualifiedId &Name, |
Douglas Gregor | a481edb | 2009-11-20 23:39:24 +0000 | [diff] [blame] | 1672 | TypeTy *ObjectType, |
| 1673 | bool EnteringContext) { |
Douglas Gregor | 0707bc5 | 2010-01-19 16:01:07 +0000 | [diff] [blame] | 1674 | DeclContext *LookupCtx = 0; |
| 1675 | if (SS.isSet()) |
| 1676 | LookupCtx = computeDeclContext(SS, EnteringContext); |
| 1677 | if (!LookupCtx && ObjectType) |
| 1678 | LookupCtx = computeDeclContext(QualType::getFromOpaquePtr(ObjectType)); |
| 1679 | if (LookupCtx) { |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 1680 | // C++0x [temp.names]p5: |
| 1681 | // If a name prefixed by the keyword template is not the name of |
| 1682 | // a template, the program is ill-formed. [Note: the keyword |
| 1683 | // template may not be applied to non-template members of class |
| 1684 | // templates. -end note ] [ Note: as is the case with the |
| 1685 | // typename prefix, the template prefix is allowed in cases |
| 1686 | // where it is not strictly necessary; i.e., when the |
| 1687 | // nested-name-specifier or the expression on the left of the -> |
| 1688 | // or . is not dependent on a template-parameter, or the use |
| 1689 | // does not appear in the scope of a template. -end note] |
| 1690 | // |
| 1691 | // Note: C++03 was more strict here, because it banned the use of |
| 1692 | // the "template" keyword prior to a template-name that was not a |
| 1693 | // dependent name. C++ DR468 relaxed this requirement (the |
| 1694 | // "template" keyword is now permitted). We follow the C++0x |
| 1695 | // rules, even in C++03 mode, retroactively applying the DR. |
| 1696 | TemplateTy Template; |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 1697 | TemplateNameKind TNK = isTemplateName(0, SS, Name, ObjectType, |
Douglas Gregor | a481edb | 2009-11-20 23:39:24 +0000 | [diff] [blame] | 1698 | EnteringContext, Template); |
Douglas Gregor | 0707bc5 | 2010-01-19 16:01:07 +0000 | [diff] [blame] | 1699 | if (TNK == TNK_Non_template && LookupCtx->isDependentContext() && |
| 1700 | isa<CXXRecordDecl>(LookupCtx) && |
| 1701 | cast<CXXRecordDecl>(LookupCtx)->hasAnyDependentBases()) { |
Douglas Gregor | 9edad9b | 2010-01-14 17:47:39 +0000 | [diff] [blame] | 1702 | // This is a dependent template. |
| 1703 | } else if (TNK == TNK_Non_template) { |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 1704 | Diag(Name.getSourceRange().getBegin(), |
| 1705 | diag::err_template_kw_refers_to_non_template) |
| 1706 | << GetNameFromUnqualifiedId(Name) |
| 1707 | << Name.getSourceRange(); |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 1708 | return TemplateTy(); |
Douglas Gregor | 9edad9b | 2010-01-14 17:47:39 +0000 | [diff] [blame] | 1709 | } else { |
| 1710 | // We found something; return it. |
| 1711 | return Template; |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 1712 | } |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 1713 | } |
| 1714 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1715 | NestedNameSpecifier *Qualifier |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 1716 | = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 1717 | |
| 1718 | switch (Name.getKind()) { |
| 1719 | case UnqualifiedId::IK_Identifier: |
| 1720 | return TemplateTy::make(Context.getDependentTemplateName(Qualifier, |
| 1721 | Name.Identifier)); |
| 1722 | |
Douglas Gregor | ca1bdd7 | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 1723 | case UnqualifiedId::IK_OperatorFunctionId: |
| 1724 | return TemplateTy::make(Context.getDependentTemplateName(Qualifier, |
| 1725 | Name.OperatorFunctionId.Operator)); |
Sean Hunt | e6252d1 | 2009-11-28 08:58:14 +0000 | [diff] [blame] | 1726 | |
| 1727 | case UnqualifiedId::IK_LiteralOperatorId: |
| 1728 | assert(false && "We don't support these; Parse shouldn't have allowed propagation"); |
| 1729 | |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 1730 | default: |
| 1731 | break; |
| 1732 | } |
| 1733 | |
| 1734 | Diag(Name.getSourceRange().getBegin(), |
| 1735 | diag::err_template_kw_refers_to_non_template) |
| 1736 | << GetNameFromUnqualifiedId(Name) |
| 1737 | << Name.getSourceRange(); |
| 1738 | return TemplateTy(); |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 1739 | } |
| 1740 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1741 | bool Sema::CheckTemplateTypeArgument(TemplateTypeParmDecl *Param, |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1742 | const TemplateArgumentLoc &AL, |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 1743 | TemplateArgumentListBuilder &Converted) { |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1744 | const TemplateArgument &Arg = AL.getArgument(); |
| 1745 | |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 1746 | // Check template type parameter. |
Jeffrey Yasskin | db88d8a | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 1747 | switch(Arg.getKind()) { |
| 1748 | case TemplateArgument::Type: |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 1749 | // C++ [temp.arg.type]p1: |
| 1750 | // A template-argument for a template-parameter which is a |
| 1751 | // type shall be a type-id. |
Jeffrey Yasskin | db88d8a | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 1752 | break; |
| 1753 | case TemplateArgument::Template: { |
| 1754 | // We have a template type parameter but the template argument |
| 1755 | // is a template without any arguments. |
| 1756 | SourceRange SR = AL.getSourceRange(); |
| 1757 | TemplateName Name = Arg.getAsTemplate(); |
| 1758 | Diag(SR.getBegin(), diag::err_template_missing_args) |
| 1759 | << Name << SR; |
| 1760 | if (TemplateDecl *Decl = Name.getAsTemplateDecl()) |
| 1761 | Diag(Decl->getLocation(), diag::note_template_decl_here); |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 1762 | |
Jeffrey Yasskin | db88d8a | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 1763 | return true; |
| 1764 | } |
| 1765 | default: { |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 1766 | // We have a template type parameter but the template argument |
| 1767 | // is not a type. |
John McCall | 828bff2 | 2009-10-29 18:45:58 +0000 | [diff] [blame] | 1768 | SourceRange SR = AL.getSourceRange(); |
| 1769 | Diag(SR.getBegin(), diag::err_template_arg_must_be_type) << SR; |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 1770 | Diag(Param->getLocation(), diag::note_template_param_here); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1771 | |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 1772 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1773 | } |
Jeffrey Yasskin | db88d8a | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 1774 | } |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 1775 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1776 | if (CheckTemplateArgument(Param, AL.getTypeSourceInfo())) |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 1777 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1778 | |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 1779 | // Add the converted template type argument. |
Anders Carlsson | fb25052 | 2009-06-23 01:26:57 +0000 | [diff] [blame] | 1780 | Converted.Append( |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1781 | TemplateArgument(Context.getCanonicalType(Arg.getAsType()))); |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 1782 | return false; |
| 1783 | } |
| 1784 | |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 1785 | /// \brief Substitute template arguments into the default template argument for |
| 1786 | /// the given template type parameter. |
| 1787 | /// |
| 1788 | /// \param SemaRef the semantic analysis object for which we are performing |
| 1789 | /// the substitution. |
| 1790 | /// |
| 1791 | /// \param Template the template that we are synthesizing template arguments |
| 1792 | /// for. |
| 1793 | /// |
| 1794 | /// \param TemplateLoc the location of the template name that started the |
| 1795 | /// template-id we are checking. |
| 1796 | /// |
| 1797 | /// \param RAngleLoc the location of the right angle bracket ('>') that |
| 1798 | /// terminates the template-id. |
| 1799 | /// |
| 1800 | /// \param Param the template template parameter whose default we are |
| 1801 | /// substituting into. |
| 1802 | /// |
| 1803 | /// \param Converted the list of template arguments provided for template |
| 1804 | /// parameters that precede \p Param in the template parameter list. |
| 1805 | /// |
| 1806 | /// \returns the substituted template argument, or NULL if an error occurred. |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1807 | static TypeSourceInfo * |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 1808 | SubstDefaultTemplateArgument(Sema &SemaRef, |
| 1809 | TemplateDecl *Template, |
| 1810 | SourceLocation TemplateLoc, |
| 1811 | SourceLocation RAngleLoc, |
| 1812 | TemplateTypeParmDecl *Param, |
| 1813 | TemplateArgumentListBuilder &Converted) { |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1814 | TypeSourceInfo *ArgType = Param->getDefaultArgumentInfo(); |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 1815 | |
| 1816 | // If the argument type is dependent, instantiate it now based |
| 1817 | // on the previously-computed template arguments. |
| 1818 | if (ArgType->getType()->isDependentType()) { |
| 1819 | TemplateArgumentList TemplateArgs(SemaRef.Context, Converted, |
| 1820 | /*TakeArgs=*/false); |
| 1821 | |
| 1822 | MultiLevelTemplateArgumentList AllTemplateArgs |
| 1823 | = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs); |
| 1824 | |
| 1825 | Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc, |
| 1826 | Template, Converted.getFlatArguments(), |
| 1827 | Converted.flatSize(), |
| 1828 | SourceRange(TemplateLoc, RAngleLoc)); |
| 1829 | |
| 1830 | ArgType = SemaRef.SubstType(ArgType, AllTemplateArgs, |
| 1831 | Param->getDefaultArgumentLoc(), |
| 1832 | Param->getDeclName()); |
| 1833 | } |
| 1834 | |
| 1835 | return ArgType; |
| 1836 | } |
| 1837 | |
| 1838 | /// \brief Substitute template arguments into the default template argument for |
| 1839 | /// the given non-type template parameter. |
| 1840 | /// |
| 1841 | /// \param SemaRef the semantic analysis object for which we are performing |
| 1842 | /// the substitution. |
| 1843 | /// |
| 1844 | /// \param Template the template that we are synthesizing template arguments |
| 1845 | /// for. |
| 1846 | /// |
| 1847 | /// \param TemplateLoc the location of the template name that started the |
| 1848 | /// template-id we are checking. |
| 1849 | /// |
| 1850 | /// \param RAngleLoc the location of the right angle bracket ('>') that |
| 1851 | /// terminates the template-id. |
| 1852 | /// |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1853 | /// \param Param the non-type template parameter whose default we are |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 1854 | /// substituting into. |
| 1855 | /// |
| 1856 | /// \param Converted the list of template arguments provided for template |
| 1857 | /// parameters that precede \p Param in the template parameter list. |
| 1858 | /// |
| 1859 | /// \returns the substituted template argument, or NULL if an error occurred. |
| 1860 | static Sema::OwningExprResult |
| 1861 | SubstDefaultTemplateArgument(Sema &SemaRef, |
| 1862 | TemplateDecl *Template, |
| 1863 | SourceLocation TemplateLoc, |
| 1864 | SourceLocation RAngleLoc, |
| 1865 | NonTypeTemplateParmDecl *Param, |
| 1866 | TemplateArgumentListBuilder &Converted) { |
| 1867 | TemplateArgumentList TemplateArgs(SemaRef.Context, Converted, |
| 1868 | /*TakeArgs=*/false); |
| 1869 | |
| 1870 | MultiLevelTemplateArgumentList AllTemplateArgs |
| 1871 | = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs); |
| 1872 | |
| 1873 | Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc, |
| 1874 | Template, Converted.getFlatArguments(), |
| 1875 | Converted.flatSize(), |
| 1876 | SourceRange(TemplateLoc, RAngleLoc)); |
| 1877 | |
| 1878 | return SemaRef.SubstExpr(Param->getDefaultArgument(), AllTemplateArgs); |
| 1879 | } |
| 1880 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1881 | /// \brief Substitute template arguments into the default template argument for |
| 1882 | /// the given template template parameter. |
| 1883 | /// |
| 1884 | /// \param SemaRef the semantic analysis object for which we are performing |
| 1885 | /// the substitution. |
| 1886 | /// |
| 1887 | /// \param Template the template that we are synthesizing template arguments |
| 1888 | /// for. |
| 1889 | /// |
| 1890 | /// \param TemplateLoc the location of the template name that started the |
| 1891 | /// template-id we are checking. |
| 1892 | /// |
| 1893 | /// \param RAngleLoc the location of the right angle bracket ('>') that |
| 1894 | /// terminates the template-id. |
| 1895 | /// |
| 1896 | /// \param Param the template template parameter whose default we are |
| 1897 | /// substituting into. |
| 1898 | /// |
| 1899 | /// \param Converted the list of template arguments provided for template |
| 1900 | /// parameters that precede \p Param in the template parameter list. |
| 1901 | /// |
| 1902 | /// \returns the substituted template argument, or NULL if an error occurred. |
| 1903 | static TemplateName |
| 1904 | SubstDefaultTemplateArgument(Sema &SemaRef, |
| 1905 | TemplateDecl *Template, |
| 1906 | SourceLocation TemplateLoc, |
| 1907 | SourceLocation RAngleLoc, |
| 1908 | TemplateTemplateParmDecl *Param, |
| 1909 | TemplateArgumentListBuilder &Converted) { |
| 1910 | TemplateArgumentList TemplateArgs(SemaRef.Context, Converted, |
| 1911 | /*TakeArgs=*/false); |
| 1912 | |
| 1913 | MultiLevelTemplateArgumentList AllTemplateArgs |
| 1914 | = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs); |
| 1915 | |
| 1916 | Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc, |
| 1917 | Template, Converted.getFlatArguments(), |
| 1918 | Converted.flatSize(), |
| 1919 | SourceRange(TemplateLoc, RAngleLoc)); |
| 1920 | |
| 1921 | return SemaRef.SubstTemplateName( |
| 1922 | Param->getDefaultArgument().getArgument().getAsTemplate(), |
| 1923 | Param->getDefaultArgument().getTemplateNameLoc(), |
| 1924 | AllTemplateArgs); |
| 1925 | } |
| 1926 | |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 1927 | /// \brief If the given template parameter has a default template |
| 1928 | /// argument, substitute into that default template argument and |
| 1929 | /// return the corresponding template argument. |
| 1930 | TemplateArgumentLoc |
| 1931 | Sema::SubstDefaultTemplateArgumentIfAvailable(TemplateDecl *Template, |
| 1932 | SourceLocation TemplateLoc, |
| 1933 | SourceLocation RAngleLoc, |
| 1934 | Decl *Param, |
| 1935 | TemplateArgumentListBuilder &Converted) { |
| 1936 | if (TemplateTypeParmDecl *TypeParm = dyn_cast<TemplateTypeParmDecl>(Param)) { |
| 1937 | if (!TypeParm->hasDefaultArgument()) |
| 1938 | return TemplateArgumentLoc(); |
| 1939 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1940 | TypeSourceInfo *DI = SubstDefaultTemplateArgument(*this, Template, |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 1941 | TemplateLoc, |
| 1942 | RAngleLoc, |
| 1943 | TypeParm, |
| 1944 | Converted); |
| 1945 | if (DI) |
| 1946 | return TemplateArgumentLoc(TemplateArgument(DI->getType()), DI); |
| 1947 | |
| 1948 | return TemplateArgumentLoc(); |
| 1949 | } |
| 1950 | |
| 1951 | if (NonTypeTemplateParmDecl *NonTypeParm |
| 1952 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
| 1953 | if (!NonTypeParm->hasDefaultArgument()) |
| 1954 | return TemplateArgumentLoc(); |
| 1955 | |
| 1956 | OwningExprResult Arg = SubstDefaultTemplateArgument(*this, Template, |
| 1957 | TemplateLoc, |
| 1958 | RAngleLoc, |
| 1959 | NonTypeParm, |
| 1960 | Converted); |
| 1961 | if (Arg.isInvalid()) |
| 1962 | return TemplateArgumentLoc(); |
| 1963 | |
| 1964 | Expr *ArgE = Arg.takeAs<Expr>(); |
| 1965 | return TemplateArgumentLoc(TemplateArgument(ArgE), ArgE); |
| 1966 | } |
| 1967 | |
| 1968 | TemplateTemplateParmDecl *TempTempParm |
| 1969 | = cast<TemplateTemplateParmDecl>(Param); |
| 1970 | if (!TempTempParm->hasDefaultArgument()) |
| 1971 | return TemplateArgumentLoc(); |
| 1972 | |
| 1973 | TemplateName TName = SubstDefaultTemplateArgument(*this, Template, |
| 1974 | TemplateLoc, |
| 1975 | RAngleLoc, |
| 1976 | TempTempParm, |
| 1977 | Converted); |
| 1978 | if (TName.isNull()) |
| 1979 | return TemplateArgumentLoc(); |
| 1980 | |
| 1981 | return TemplateArgumentLoc(TemplateArgument(TName), |
| 1982 | TempTempParm->getDefaultArgument().getTemplateQualifierRange(), |
| 1983 | TempTempParm->getDefaultArgument().getTemplateNameLoc()); |
| 1984 | } |
| 1985 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 1986 | /// \brief Check that the given template argument corresponds to the given |
| 1987 | /// template parameter. |
| 1988 | bool Sema::CheckTemplateArgument(NamedDecl *Param, |
| 1989 | const TemplateArgumentLoc &Arg, |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 1990 | TemplateDecl *Template, |
| 1991 | SourceLocation TemplateLoc, |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 1992 | SourceLocation RAngleLoc, |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 1993 | TemplateArgumentListBuilder &Converted, |
| 1994 | CheckTemplateArgumentKind CTAK) { |
Douglas Gregor | d9e1530 | 2009-11-11 19:41:09 +0000 | [diff] [blame] | 1995 | // Check template type parameters. |
| 1996 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 1997 | return CheckTemplateTypeArgument(TTP, Arg, Converted); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 1998 | |
Douglas Gregor | d9e1530 | 2009-11-11 19:41:09 +0000 | [diff] [blame] | 1999 | // Check non-type template parameters. |
| 2000 | if (NonTypeTemplateParmDecl *NTTP =dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2001 | // Do substitution on the type of the non-type template parameter |
| 2002 | // with the template arguments we've seen thus far. |
| 2003 | QualType NTTPType = NTTP->getType(); |
| 2004 | if (NTTPType->isDependentType()) { |
| 2005 | // Do substitution on the type of the non-type template parameter. |
| 2006 | InstantiatingTemplate Inst(*this, TemplateLoc, Template, |
| 2007 | NTTP, Converted.getFlatArguments(), |
| 2008 | Converted.flatSize(), |
| 2009 | SourceRange(TemplateLoc, RAngleLoc)); |
| 2010 | |
| 2011 | TemplateArgumentList TemplateArgs(Context, Converted, |
| 2012 | /*TakeArgs=*/false); |
| 2013 | NTTPType = SubstType(NTTPType, |
| 2014 | MultiLevelTemplateArgumentList(TemplateArgs), |
| 2015 | NTTP->getLocation(), |
| 2016 | NTTP->getDeclName()); |
| 2017 | // If that worked, check the non-type template parameter type |
| 2018 | // for validity. |
| 2019 | if (!NTTPType.isNull()) |
| 2020 | NTTPType = CheckNonTypeTemplateParameterType(NTTPType, |
| 2021 | NTTP->getLocation()); |
| 2022 | if (NTTPType.isNull()) |
| 2023 | return true; |
| 2024 | } |
| 2025 | |
| 2026 | switch (Arg.getArgument().getKind()) { |
| 2027 | case TemplateArgument::Null: |
| 2028 | assert(false && "Should never see a NULL template argument here"); |
| 2029 | return true; |
| 2030 | |
| 2031 | case TemplateArgument::Expression: { |
| 2032 | Expr *E = Arg.getArgument().getAsExpr(); |
| 2033 | TemplateArgument Result; |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 2034 | if (CheckTemplateArgument(NTTP, NTTPType, E, Result, CTAK)) |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2035 | return true; |
| 2036 | |
| 2037 | Converted.Append(Result); |
| 2038 | break; |
| 2039 | } |
| 2040 | |
| 2041 | case TemplateArgument::Declaration: |
| 2042 | case TemplateArgument::Integral: |
| 2043 | // We've already checked this template argument, so just copy |
| 2044 | // it to the list of converted arguments. |
| 2045 | Converted.Append(Arg.getArgument()); |
| 2046 | break; |
| 2047 | |
| 2048 | case TemplateArgument::Template: |
| 2049 | // We were given a template template argument. It may not be ill-formed; |
| 2050 | // see below. |
| 2051 | if (DependentTemplateName *DTN |
| 2052 | = Arg.getArgument().getAsTemplate().getAsDependentTemplateName()) { |
| 2053 | // We have a template argument such as \c T::template X, which we |
| 2054 | // parsed as a template template argument. However, since we now |
| 2055 | // know that we need a non-type template argument, convert this |
| 2056 | // template name into an expression. |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2057 | Expr *E = DependentScopeDeclRefExpr::Create(Context, |
| 2058 | DTN->getQualifier(), |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2059 | Arg.getTemplateQualifierRange(), |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2060 | DTN->getIdentifier(), |
| 2061 | Arg.getTemplateNameLoc()); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2062 | |
| 2063 | TemplateArgument Result; |
| 2064 | if (CheckTemplateArgument(NTTP, NTTPType, E, Result)) |
| 2065 | return true; |
| 2066 | |
| 2067 | Converted.Append(Result); |
| 2068 | break; |
| 2069 | } |
| 2070 | |
| 2071 | // We have a template argument that actually does refer to a class |
| 2072 | // template, template alias, or template template parameter, and |
| 2073 | // therefore cannot be a non-type template argument. |
| 2074 | Diag(Arg.getLocation(), diag::err_template_arg_must_be_expr) |
| 2075 | << Arg.getSourceRange(); |
| 2076 | |
| 2077 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 2078 | return true; |
| 2079 | |
| 2080 | case TemplateArgument::Type: { |
| 2081 | // We have a non-type template parameter but the template |
| 2082 | // argument is a type. |
| 2083 | |
| 2084 | // C++ [temp.arg]p2: |
| 2085 | // In a template-argument, an ambiguity between a type-id and |
| 2086 | // an expression is resolved to a type-id, regardless of the |
| 2087 | // form of the corresponding template-parameter. |
| 2088 | // |
| 2089 | // We warn specifically about this case, since it can be rather |
| 2090 | // confusing for users. |
| 2091 | QualType T = Arg.getArgument().getAsType(); |
| 2092 | SourceRange SR = Arg.getSourceRange(); |
| 2093 | if (T->isFunctionType()) |
| 2094 | Diag(SR.getBegin(), diag::err_template_arg_nontype_ambig) << SR << T; |
| 2095 | else |
| 2096 | Diag(SR.getBegin(), diag::err_template_arg_must_be_expr) << SR; |
| 2097 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 2098 | return true; |
| 2099 | } |
| 2100 | |
| 2101 | case TemplateArgument::Pack: |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 2102 | llvm_unreachable("Caller must expand template argument packs"); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2103 | break; |
| 2104 | } |
| 2105 | |
| 2106 | return false; |
| 2107 | } |
| 2108 | |
| 2109 | |
| 2110 | // Check template template parameters. |
| 2111 | TemplateTemplateParmDecl *TempParm = cast<TemplateTemplateParmDecl>(Param); |
| 2112 | |
| 2113 | // Substitute into the template parameter list of the template |
| 2114 | // template parameter, since previously-supplied template arguments |
| 2115 | // may appear within the template template parameter. |
| 2116 | { |
| 2117 | // Set up a template instantiation context. |
| 2118 | LocalInstantiationScope Scope(*this); |
| 2119 | InstantiatingTemplate Inst(*this, TemplateLoc, Template, |
| 2120 | TempParm, Converted.getFlatArguments(), |
| 2121 | Converted.flatSize(), |
| 2122 | SourceRange(TemplateLoc, RAngleLoc)); |
| 2123 | |
| 2124 | TemplateArgumentList TemplateArgs(Context, Converted, |
| 2125 | /*TakeArgs=*/false); |
| 2126 | TempParm = cast_or_null<TemplateTemplateParmDecl>( |
| 2127 | SubstDecl(TempParm, CurContext, |
| 2128 | MultiLevelTemplateArgumentList(TemplateArgs))); |
| 2129 | if (!TempParm) |
| 2130 | return true; |
| 2131 | |
| 2132 | // FIXME: TempParam is leaked. |
| 2133 | } |
| 2134 | |
| 2135 | switch (Arg.getArgument().getKind()) { |
| 2136 | case TemplateArgument::Null: |
| 2137 | assert(false && "Should never see a NULL template argument here"); |
| 2138 | return true; |
| 2139 | |
| 2140 | case TemplateArgument::Template: |
| 2141 | if (CheckTemplateArgument(TempParm, Arg)) |
| 2142 | return true; |
| 2143 | |
| 2144 | Converted.Append(Arg.getArgument()); |
| 2145 | break; |
| 2146 | |
| 2147 | case TemplateArgument::Expression: |
| 2148 | case TemplateArgument::Type: |
| 2149 | // We have a template template parameter but the template |
| 2150 | // argument does not refer to a template. |
| 2151 | Diag(Arg.getLocation(), diag::err_template_arg_must_be_template); |
| 2152 | return true; |
| 2153 | |
| 2154 | case TemplateArgument::Declaration: |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 2155 | llvm_unreachable( |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2156 | "Declaration argument with template template parameter"); |
| 2157 | break; |
| 2158 | case TemplateArgument::Integral: |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 2159 | llvm_unreachable( |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2160 | "Integral argument with template template parameter"); |
| 2161 | break; |
| 2162 | |
| 2163 | case TemplateArgument::Pack: |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 2164 | llvm_unreachable("Caller must expand template argument packs"); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2165 | break; |
| 2166 | } |
| 2167 | |
| 2168 | return false; |
| 2169 | } |
| 2170 | |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2171 | /// \brief Check that the given template argument list is well-formed |
| 2172 | /// for specializing the given template. |
| 2173 | bool Sema::CheckTemplateArgumentList(TemplateDecl *Template, |
| 2174 | SourceLocation TemplateLoc, |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2175 | const TemplateArgumentListInfo &TemplateArgs, |
Douglas Gregor | 16134c6 | 2009-07-01 00:28:38 +0000 | [diff] [blame] | 2176 | bool PartialTemplateArgs, |
Anders Carlsson | 1c5976e | 2009-06-05 03:43:12 +0000 | [diff] [blame] | 2177 | TemplateArgumentListBuilder &Converted) { |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2178 | TemplateParameterList *Params = Template->getTemplateParameters(); |
| 2179 | unsigned NumParams = Params->size(); |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2180 | unsigned NumArgs = TemplateArgs.size(); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2181 | bool Invalid = false; |
| 2182 | |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2183 | SourceLocation RAngleLoc = TemplateArgs.getRAngleLoc(); |
| 2184 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2185 | bool HasParameterPack = |
Anders Carlsson | 0ceffb5 | 2009-06-13 02:08:00 +0000 | [diff] [blame] | 2186 | NumParams > 0 && Params->getParam(NumParams - 1)->isTemplateParameterPack(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2187 | |
Anders Carlsson | 0ceffb5 | 2009-06-13 02:08:00 +0000 | [diff] [blame] | 2188 | if ((NumArgs > NumParams && !HasParameterPack) || |
Douglas Gregor | 16134c6 | 2009-07-01 00:28:38 +0000 | [diff] [blame] | 2189 | (NumArgs < Params->getMinRequiredArguments() && |
| 2190 | !PartialTemplateArgs)) { |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2191 | // FIXME: point at either the first arg beyond what we can handle, |
| 2192 | // or the '>', depending on whether we have too many or too few |
| 2193 | // arguments. |
| 2194 | SourceRange Range; |
| 2195 | if (NumArgs > NumParams) |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2196 | Range = SourceRange(TemplateArgs[NumParams].getLocation(), RAngleLoc); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2197 | Diag(TemplateLoc, diag::err_template_arg_list_different_arity) |
| 2198 | << (NumArgs > NumParams) |
| 2199 | << (isa<ClassTemplateDecl>(Template)? 0 : |
| 2200 | isa<FunctionTemplateDecl>(Template)? 1 : |
| 2201 | isa<TemplateTemplateParmDecl>(Template)? 2 : 3) |
| 2202 | << Template << Range; |
Douglas Gregor | 62cb18d | 2009-02-11 18:16:40 +0000 | [diff] [blame] | 2203 | Diag(Template->getLocation(), diag::note_template_decl_here) |
| 2204 | << Params->getSourceRange(); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2205 | Invalid = true; |
| 2206 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2207 | |
| 2208 | // C++ [temp.arg]p1: |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2209 | // [...] The type and form of each template-argument specified in |
| 2210 | // a template-id shall match the type and form specified for the |
| 2211 | // corresponding parameter declared by the template in its |
| 2212 | // template-parameter-list. |
| 2213 | unsigned ArgIdx = 0; |
| 2214 | for (TemplateParameterList::iterator Param = Params->begin(), |
| 2215 | ParamEnd = Params->end(); |
| 2216 | Param != ParamEnd; ++Param, ++ArgIdx) { |
Douglas Gregor | 16134c6 | 2009-07-01 00:28:38 +0000 | [diff] [blame] | 2217 | if (ArgIdx > NumArgs && PartialTemplateArgs) |
| 2218 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2219 | |
Douglas Gregor | d9e1530 | 2009-11-11 19:41:09 +0000 | [diff] [blame] | 2220 | // If we have a template parameter pack, check every remaining template |
| 2221 | // argument against that template parameter pack. |
| 2222 | if ((*Param)->isTemplateParameterPack()) { |
| 2223 | Converted.BeginPack(); |
| 2224 | for (; ArgIdx < NumArgs; ++ArgIdx) { |
| 2225 | if (CheckTemplateArgument(*Param, TemplateArgs[ArgIdx], Template, |
| 2226 | TemplateLoc, RAngleLoc, Converted)) { |
| 2227 | Invalid = true; |
| 2228 | break; |
| 2229 | } |
| 2230 | } |
| 2231 | Converted.EndPack(); |
| 2232 | continue; |
| 2233 | } |
| 2234 | |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2235 | if (ArgIdx < NumArgs) { |
| 2236 | // Check the template argument we were given. |
| 2237 | if (CheckTemplateArgument(*Param, TemplateArgs[ArgIdx], Template, |
| 2238 | TemplateLoc, RAngleLoc, Converted)) |
| 2239 | return true; |
| 2240 | |
| 2241 | continue; |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 2242 | } |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2243 | |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2244 | // We have a default template argument that we will use. |
| 2245 | TemplateArgumentLoc Arg; |
| 2246 | |
| 2247 | // Retrieve the default template argument from the template |
| 2248 | // parameter. For each kind of template parameter, we substitute the |
| 2249 | // template arguments provided thus far and any "outer" template arguments |
| 2250 | // (when the template parameter was part of a nested template) into |
| 2251 | // the default argument. |
| 2252 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*Param)) { |
| 2253 | if (!TTP->hasDefaultArgument()) { |
| 2254 | assert((Invalid || PartialTemplateArgs) && "Missing default argument"); |
| 2255 | break; |
| 2256 | } |
| 2257 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2258 | TypeSourceInfo *ArgType = SubstDefaultTemplateArgument(*this, |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2259 | Template, |
| 2260 | TemplateLoc, |
| 2261 | RAngleLoc, |
| 2262 | TTP, |
| 2263 | Converted); |
| 2264 | if (!ArgType) |
| 2265 | return true; |
| 2266 | |
| 2267 | Arg = TemplateArgumentLoc(TemplateArgument(ArgType->getType()), |
| 2268 | ArgType); |
| 2269 | } else if (NonTypeTemplateParmDecl *NTTP |
| 2270 | = dyn_cast<NonTypeTemplateParmDecl>(*Param)) { |
| 2271 | if (!NTTP->hasDefaultArgument()) { |
| 2272 | assert((Invalid || PartialTemplateArgs) && "Missing default argument"); |
| 2273 | break; |
| 2274 | } |
| 2275 | |
| 2276 | Sema::OwningExprResult E = SubstDefaultTemplateArgument(*this, Template, |
| 2277 | TemplateLoc, |
| 2278 | RAngleLoc, |
| 2279 | NTTP, |
| 2280 | Converted); |
| 2281 | if (E.isInvalid()) |
| 2282 | return true; |
| 2283 | |
| 2284 | Expr *Ex = E.takeAs<Expr>(); |
| 2285 | Arg = TemplateArgumentLoc(TemplateArgument(Ex), Ex); |
| 2286 | } else { |
| 2287 | TemplateTemplateParmDecl *TempParm |
| 2288 | = cast<TemplateTemplateParmDecl>(*Param); |
| 2289 | |
| 2290 | if (!TempParm->hasDefaultArgument()) { |
| 2291 | assert((Invalid || PartialTemplateArgs) && "Missing default argument"); |
| 2292 | break; |
| 2293 | } |
| 2294 | |
| 2295 | TemplateName Name = SubstDefaultTemplateArgument(*this, Template, |
| 2296 | TemplateLoc, |
| 2297 | RAngleLoc, |
| 2298 | TempParm, |
| 2299 | Converted); |
| 2300 | if (Name.isNull()) |
| 2301 | return true; |
| 2302 | |
| 2303 | Arg = TemplateArgumentLoc(TemplateArgument(Name), |
| 2304 | TempParm->getDefaultArgument().getTemplateQualifierRange(), |
| 2305 | TempParm->getDefaultArgument().getTemplateNameLoc()); |
| 2306 | } |
| 2307 | |
| 2308 | // Introduce an instantiation record that describes where we are using |
| 2309 | // the default template argument. |
| 2310 | InstantiatingTemplate Instantiating(*this, RAngleLoc, Template, *Param, |
| 2311 | Converted.getFlatArguments(), |
| 2312 | Converted.flatSize(), |
| 2313 | SourceRange(TemplateLoc, RAngleLoc)); |
| 2314 | |
| 2315 | // Check the default template argument. |
Douglas Gregor | d9e1530 | 2009-11-11 19:41:09 +0000 | [diff] [blame] | 2316 | if (CheckTemplateArgument(*Param, Arg, Template, TemplateLoc, |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2317 | RAngleLoc, Converted)) |
| 2318 | return true; |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2319 | } |
| 2320 | |
| 2321 | return Invalid; |
| 2322 | } |
| 2323 | |
| 2324 | /// \brief Check a template argument against its corresponding |
| 2325 | /// template type parameter. |
| 2326 | /// |
| 2327 | /// This routine implements the semantics of C++ [temp.arg.type]. It |
| 2328 | /// returns true if an error occurred, and false otherwise. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2329 | bool Sema::CheckTemplateArgument(TemplateTypeParmDecl *Param, |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2330 | TypeSourceInfo *ArgInfo) { |
| 2331 | assert(ArgInfo && "invalid TypeSourceInfo"); |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2332 | QualType Arg = ArgInfo->getType(); |
| 2333 | |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2334 | // C++ [temp.arg.type]p2: |
| 2335 | // A local type, a type with no linkage, an unnamed type or a type |
| 2336 | // compounded from any of these types shall not be used as a |
| 2337 | // template-argument for a template type-parameter. |
| 2338 | // |
| 2339 | // FIXME: Perform the recursive and no-linkage type checks. |
| 2340 | const TagType *Tag = 0; |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2341 | if (const EnumType *EnumT = Arg->getAs<EnumType>()) |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2342 | Tag = EnumT; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2343 | else if (const RecordType *RecordT = Arg->getAs<RecordType>()) |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2344 | Tag = RecordT; |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2345 | if (Tag && Tag->getDecl()->getDeclContext()->isFunctionOrMethod()) { |
| 2346 | SourceRange SR = ArgInfo->getTypeLoc().getFullSourceRange(); |
| 2347 | return Diag(SR.getBegin(), diag::err_template_arg_local_type) |
| 2348 | << QualType(Tag, 0) << SR; |
| 2349 | } else if (Tag && !Tag->getDecl()->getDeclName() && |
Douglas Gregor | 9813753 | 2009-03-10 18:33:27 +0000 | [diff] [blame] | 2350 | !Tag->getDecl()->getTypedefForAnonDecl()) { |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2351 | SourceRange SR = ArgInfo->getTypeLoc().getFullSourceRange(); |
| 2352 | Diag(SR.getBegin(), diag::err_template_arg_unnamed_type) << SR; |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2353 | Diag(Tag->getDecl()->getLocation(), diag::note_template_unnamed_type_here); |
| 2354 | return true; |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 2355 | } else if (Context.hasSameUnqualifiedType(Arg, Context.OverloadTy)) { |
| 2356 | SourceRange SR = ArgInfo->getTypeLoc().getFullSourceRange(); |
| 2357 | return Diag(SR.getBegin(), diag::err_template_arg_overload_type) << SR; |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2358 | } |
| 2359 | |
| 2360 | return false; |
| 2361 | } |
| 2362 | |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2363 | /// \brief Checks whether the given template argument is the address |
| 2364 | /// of an object or function according to C++ [temp.arg.nontype]p1. |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2365 | static bool |
| 2366 | CheckTemplateArgumentAddressOfObjectOrFunction(Sema &S, |
| 2367 | NonTypeTemplateParmDecl *Param, |
| 2368 | QualType ParamType, |
| 2369 | Expr *ArgIn, |
| 2370 | TemplateArgument &Converted) { |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2371 | bool Invalid = false; |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2372 | Expr *Arg = ArgIn; |
| 2373 | QualType ArgType = Arg->getType(); |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2374 | |
| 2375 | // See through any implicit casts we added to fix the type. |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 2376 | while (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(Arg)) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2377 | Arg = Cast->getSubExpr(); |
| 2378 | |
| 2379 | // C++ [temp.arg.nontype]p1: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2380 | // |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2381 | // A template-argument for a non-type, non-template |
| 2382 | // template-parameter shall be one of: [...] |
| 2383 | // |
| 2384 | // -- the address of an object or function with external |
| 2385 | // linkage, including function templates and function |
| 2386 | // template-ids but excluding non-static class members, |
| 2387 | // expressed as & id-expression where the & is optional if |
| 2388 | // the name refers to a function or array, or if the |
| 2389 | // corresponding template-parameter is a reference; or |
| 2390 | DeclRefExpr *DRE = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2391 | |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2392 | // Ignore (and complain about) any excess parentheses. |
| 2393 | while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) { |
| 2394 | if (!Invalid) { |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2395 | S.Diag(Arg->getSourceRange().getBegin(), |
| 2396 | diag::err_template_arg_extra_parens) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2397 | << Arg->getSourceRange(); |
| 2398 | Invalid = true; |
| 2399 | } |
| 2400 | |
| 2401 | Arg = Parens->getSubExpr(); |
| 2402 | } |
| 2403 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2404 | bool AddressTaken = false; |
| 2405 | SourceLocation AddrOpLoc; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2406 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) { |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2407 | if (UnOp->getOpcode() == UnaryOperator::AddrOf) { |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2408 | DRE = dyn_cast<DeclRefExpr>(UnOp->getSubExpr()); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2409 | AddressTaken = true; |
| 2410 | AddrOpLoc = UnOp->getOperatorLoc(); |
| 2411 | } |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2412 | } else |
| 2413 | DRE = dyn_cast<DeclRefExpr>(Arg); |
| 2414 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2415 | if (!DRE) { |
Douglas Gregor | 1a8cf73 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 2416 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_decl_ref) |
| 2417 | << Arg->getSourceRange(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2418 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 2419 | return true; |
| 2420 | } |
Chandler Carruth | 038cc39 | 2010-01-31 10:01:20 +0000 | [diff] [blame] | 2421 | |
| 2422 | // Stop checking the precise nature of the argument if it is value dependent, |
| 2423 | // it should be checked when instantiated. |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2424 | if (Arg->isValueDependent()) { |
| 2425 | Converted = TemplateArgument(ArgIn->Retain()); |
Chandler Carruth | 038cc39 | 2010-01-31 10:01:20 +0000 | [diff] [blame] | 2426 | return false; |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2427 | } |
Chandler Carruth | 038cc39 | 2010-01-31 10:01:20 +0000 | [diff] [blame] | 2428 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2429 | if (!isa<ValueDecl>(DRE->getDecl())) { |
| 2430 | S.Diag(Arg->getSourceRange().getBegin(), |
| 2431 | diag::err_template_arg_not_object_or_func_form) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2432 | << Arg->getSourceRange(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2433 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 2434 | return true; |
| 2435 | } |
| 2436 | |
| 2437 | NamedDecl *Entity = 0; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2438 | |
| 2439 | // Cannot refer to non-static data members |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2440 | if (FieldDecl *Field = dyn_cast<FieldDecl>(DRE->getDecl())) { |
| 2441 | S.Diag(Arg->getSourceRange().getBegin(), diag::err_template_arg_field) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2442 | << Field << Arg->getSourceRange(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2443 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 2444 | return true; |
| 2445 | } |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2446 | |
| 2447 | // Cannot refer to non-static member functions |
| 2448 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(DRE->getDecl())) |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2449 | if (!Method->isStatic()) { |
| 2450 | S.Diag(Arg->getSourceRange().getBegin(), diag::err_template_arg_method) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2451 | << Method << Arg->getSourceRange(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2452 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 2453 | return true; |
| 2454 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2455 | |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2456 | // Functions must have external linkage. |
| 2457 | if (FunctionDecl *Func = dyn_cast<FunctionDecl>(DRE->getDecl())) { |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 2458 | if (!isExternalLinkage(Func->getLinkage())) { |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2459 | S.Diag(Arg->getSourceRange().getBegin(), |
| 2460 | diag::err_template_arg_function_not_extern) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2461 | << Func << Arg->getSourceRange(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2462 | S.Diag(Func->getLocation(), diag::note_template_arg_internal_object) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2463 | << true; |
| 2464 | return true; |
| 2465 | } |
| 2466 | |
| 2467 | // Okay: we've named a function with external linkage. |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 2468 | Entity = Func; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2469 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2470 | // If the template parameter has pointer type, the function decays. |
| 2471 | if (ParamType->isPointerType() && !AddressTaken) |
| 2472 | ArgType = S.Context.getPointerType(Func->getType()); |
| 2473 | else if (AddressTaken && ParamType->isReferenceType()) { |
| 2474 | // If we originally had an address-of operator, but the |
| 2475 | // parameter has reference type, complain and (if things look |
| 2476 | // like they will work) drop the address-of operator. |
| 2477 | if (!S.Context.hasSameUnqualifiedType(Func->getType(), |
| 2478 | ParamType.getNonReferenceType())) { |
| 2479 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 2480 | << ParamType; |
| 2481 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 2482 | return true; |
| 2483 | } |
| 2484 | |
| 2485 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 2486 | << ParamType |
| 2487 | << FixItHint::CreateRemoval(AddrOpLoc); |
| 2488 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 2489 | |
| 2490 | ArgType = Func->getType(); |
| 2491 | } |
| 2492 | } else if (VarDecl *Var = dyn_cast<VarDecl>(DRE->getDecl())) { |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 2493 | if (!isExternalLinkage(Var->getLinkage())) { |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2494 | S.Diag(Arg->getSourceRange().getBegin(), |
| 2495 | diag::err_template_arg_object_not_extern) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2496 | << Var << Arg->getSourceRange(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2497 | S.Diag(Var->getLocation(), diag::note_template_arg_internal_object) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2498 | << true; |
| 2499 | return true; |
| 2500 | } |
| 2501 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2502 | // A value of reference type is not an object. |
| 2503 | if (Var->getType()->isReferenceType()) { |
| 2504 | S.Diag(Arg->getSourceRange().getBegin(), |
| 2505 | diag::err_template_arg_reference_var) |
| 2506 | << Var->getType() << Arg->getSourceRange(); |
| 2507 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 2508 | return true; |
| 2509 | } |
| 2510 | |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2511 | // Okay: we've named an object with external linkage |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 2512 | Entity = Var; |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2513 | |
| 2514 | // If the template parameter has pointer type, we must have taken |
| 2515 | // the address of this object. |
| 2516 | if (ParamType->isReferenceType()) { |
| 2517 | if (AddressTaken) { |
| 2518 | // If we originally had an address-of operator, but the |
| 2519 | // parameter has reference type, complain and (if things look |
| 2520 | // like they will work) drop the address-of operator. |
| 2521 | if (!S.Context.hasSameUnqualifiedType(Var->getType(), |
| 2522 | ParamType.getNonReferenceType())) { |
| 2523 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 2524 | << ParamType; |
| 2525 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 2526 | return true; |
| 2527 | } |
| 2528 | |
| 2529 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 2530 | << ParamType |
| 2531 | << FixItHint::CreateRemoval(AddrOpLoc); |
| 2532 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 2533 | |
| 2534 | ArgType = Var->getType(); |
| 2535 | } |
| 2536 | } else if (!AddressTaken && ParamType->isPointerType()) { |
| 2537 | if (Var->getType()->isArrayType()) { |
| 2538 | // Array-to-pointer decay. |
| 2539 | ArgType = S.Context.getArrayDecayedType(Var->getType()); |
| 2540 | } else { |
| 2541 | // If the template parameter has pointer type but the address of |
| 2542 | // this object was not taken, complain and (possibly) recover by |
| 2543 | // taking the address of the entity. |
| 2544 | ArgType = S.Context.getPointerType(Var->getType()); |
| 2545 | if (!S.Context.hasSameUnqualifiedType(ArgType, ParamType)) { |
| 2546 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_address_of) |
| 2547 | << ParamType; |
| 2548 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 2549 | return true; |
| 2550 | } |
| 2551 | |
| 2552 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_address_of) |
| 2553 | << ParamType |
| 2554 | << FixItHint::CreateInsertion(Arg->getLocStart(), "&"); |
| 2555 | |
| 2556 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 2557 | } |
| 2558 | } |
| 2559 | } else { |
| 2560 | // We found something else, but we don't know specifically what it is. |
| 2561 | S.Diag(Arg->getSourceRange().getBegin(), |
| 2562 | diag::err_template_arg_not_object_or_func) |
| 2563 | << Arg->getSourceRange(); |
| 2564 | S.Diag(DRE->getDecl()->getLocation(), diag::note_template_arg_refers_here); |
| 2565 | return true; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2566 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2567 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2568 | if (ParamType->isPointerType() && |
| 2569 | !ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType() && |
| 2570 | S.IsQualificationConversion(ArgType, ParamType)) { |
| 2571 | // For pointer-to-object types, qualification conversions are |
| 2572 | // permitted. |
| 2573 | } else { |
| 2574 | if (const ReferenceType *ParamRef = ParamType->getAs<ReferenceType>()) { |
| 2575 | if (!ParamRef->getPointeeType()->isFunctionType()) { |
| 2576 | // C++ [temp.arg.nontype]p5b3: |
| 2577 | // For a non-type template-parameter of type reference to |
| 2578 | // object, no conversions apply. The type referred to by the |
| 2579 | // reference may be more cv-qualified than the (otherwise |
| 2580 | // identical) type of the template- argument. The |
| 2581 | // template-parameter is bound directly to the |
| 2582 | // template-argument, which shall be an lvalue. |
| 2583 | |
| 2584 | // FIXME: Other qualifiers? |
| 2585 | unsigned ParamQuals = ParamRef->getPointeeType().getCVRQualifiers(); |
| 2586 | unsigned ArgQuals = ArgType.getCVRQualifiers(); |
| 2587 | |
| 2588 | if ((ParamQuals | ArgQuals) != ParamQuals) { |
| 2589 | S.Diag(Arg->getSourceRange().getBegin(), |
| 2590 | diag::err_template_arg_ref_bind_ignores_quals) |
| 2591 | << ParamType << Arg->getType() |
| 2592 | << Arg->getSourceRange(); |
| 2593 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 2594 | return true; |
| 2595 | } |
| 2596 | } |
| 2597 | } |
| 2598 | |
| 2599 | // At this point, the template argument refers to an object or |
| 2600 | // function with external linkage. We now need to check whether the |
| 2601 | // argument and parameter types are compatible. |
| 2602 | if (!S.Context.hasSameUnqualifiedType(ArgType, |
| 2603 | ParamType.getNonReferenceType())) { |
| 2604 | // We can't perform this conversion or binding. |
| 2605 | if (ParamType->isReferenceType()) |
| 2606 | S.Diag(Arg->getLocStart(), diag::err_template_arg_no_ref_bind) |
| 2607 | << ParamType << Arg->getType() << Arg->getSourceRange(); |
| 2608 | else |
| 2609 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_convertible) |
| 2610 | << Arg->getType() << ParamType << Arg->getSourceRange(); |
| 2611 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 2612 | return true; |
| 2613 | } |
| 2614 | } |
| 2615 | |
| 2616 | // Create the template argument. |
| 2617 | Converted = TemplateArgument(Entity->getCanonicalDecl()); |
Douglas Gregor | 77c13e0 | 2010-04-24 18:20:53 +0000 | [diff] [blame] | 2618 | S.MarkDeclarationReferenced(Arg->getLocStart(), Entity); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2619 | return false; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2620 | } |
| 2621 | |
| 2622 | /// \brief Checks whether the given template argument is a pointer to |
| 2623 | /// member constant according to C++ [temp.arg.nontype]p1. |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 2624 | bool Sema::CheckTemplateArgumentPointerToMember(Expr *Arg, |
| 2625 | TemplateArgument &Converted) { |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2626 | bool Invalid = false; |
| 2627 | |
| 2628 | // See through any implicit casts we added to fix the type. |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 2629 | while (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(Arg)) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2630 | Arg = Cast->getSubExpr(); |
| 2631 | |
| 2632 | // C++ [temp.arg.nontype]p1: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2633 | // |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2634 | // A template-argument for a non-type, non-template |
| 2635 | // template-parameter shall be one of: [...] |
| 2636 | // |
| 2637 | // -- a pointer to member expressed as described in 5.3.1. |
Douglas Gregor | a2813ce | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 2638 | DeclRefExpr *DRE = 0; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2639 | |
| 2640 | // Ignore (and complain about) any excess parentheses. |
| 2641 | while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) { |
| 2642 | if (!Invalid) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2643 | Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2644 | diag::err_template_arg_extra_parens) |
| 2645 | << Arg->getSourceRange(); |
| 2646 | Invalid = true; |
| 2647 | } |
| 2648 | |
| 2649 | Arg = Parens->getSubExpr(); |
| 2650 | } |
| 2651 | |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 2652 | // A pointer-to-member constant written &Class::member. |
| 2653 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) { |
Douglas Gregor | a2813ce | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 2654 | if (UnOp->getOpcode() == UnaryOperator::AddrOf) { |
| 2655 | DRE = dyn_cast<DeclRefExpr>(UnOp->getSubExpr()); |
| 2656 | if (DRE && !DRE->getQualifier()) |
| 2657 | DRE = 0; |
| 2658 | } |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 2659 | } |
| 2660 | // A constant of pointer-to-member type. |
| 2661 | else if ((DRE = dyn_cast<DeclRefExpr>(Arg))) { |
| 2662 | if (ValueDecl *VD = dyn_cast<ValueDecl>(DRE->getDecl())) { |
| 2663 | if (VD->getType()->isMemberPointerType()) { |
| 2664 | if (isa<NonTypeTemplateParmDecl>(VD) || |
| 2665 | (isa<VarDecl>(VD) && |
| 2666 | Context.getCanonicalType(VD->getType()).isConstQualified())) { |
| 2667 | if (Arg->isTypeDependent() || Arg->isValueDependent()) |
| 2668 | Converted = TemplateArgument(Arg->Retain()); |
| 2669 | else |
| 2670 | Converted = TemplateArgument(VD->getCanonicalDecl()); |
| 2671 | return Invalid; |
| 2672 | } |
| 2673 | } |
| 2674 | } |
| 2675 | |
| 2676 | DRE = 0; |
| 2677 | } |
| 2678 | |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2679 | if (!DRE) |
| 2680 | return Diag(Arg->getSourceRange().getBegin(), |
| 2681 | diag::err_template_arg_not_pointer_to_member_form) |
| 2682 | << Arg->getSourceRange(); |
| 2683 | |
| 2684 | if (isa<FieldDecl>(DRE->getDecl()) || isa<CXXMethodDecl>(DRE->getDecl())) { |
| 2685 | assert((isa<FieldDecl>(DRE->getDecl()) || |
| 2686 | !cast<CXXMethodDecl>(DRE->getDecl())->isStatic()) && |
| 2687 | "Only non-static member pointers can make it here"); |
| 2688 | |
| 2689 | // Okay: this is the address of a non-static member, and therefore |
| 2690 | // a member pointer constant. |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 2691 | if (Arg->isTypeDependent() || Arg->isValueDependent()) |
| 2692 | Converted = TemplateArgument(Arg->Retain()); |
| 2693 | else |
| 2694 | Converted = TemplateArgument(DRE->getDecl()->getCanonicalDecl()); |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2695 | return Invalid; |
| 2696 | } |
| 2697 | |
| 2698 | // 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] | 2699 | Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2700 | diag::err_template_arg_not_pointer_to_member_form) |
| 2701 | << Arg->getSourceRange(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2702 | Diag(DRE->getDecl()->getLocation(), |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2703 | diag::note_template_arg_refers_here); |
| 2704 | return true; |
| 2705 | } |
| 2706 | |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2707 | /// \brief Check a template argument against its corresponding |
| 2708 | /// non-type template parameter. |
| 2709 | /// |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 2710 | /// This routine implements the semantics of C++ [temp.arg.nontype]. |
| 2711 | /// It returns true if an error occurred, and false otherwise. \p |
| 2712 | /// InstantiatedParamType is the type of the non-type template |
| 2713 | /// parameter after it has been instantiated. |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 2714 | /// |
Douglas Gregor | 02cbbd2 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 2715 | /// If no error was detected, Converted receives the converted template argument. |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2716 | bool Sema::CheckTemplateArgument(NonTypeTemplateParmDecl *Param, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2717 | QualType InstantiatedParamType, Expr *&Arg, |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 2718 | TemplateArgument &Converted, |
| 2719 | CheckTemplateArgumentKind CTAK) { |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2720 | SourceLocation StartLoc = Arg->getSourceRange().getBegin(); |
| 2721 | |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 2722 | // If either the parameter has a dependent type or the argument is |
| 2723 | // type-dependent, there's nothing we can check now. |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2724 | if (InstantiatedParamType->isDependentType() || Arg->isTypeDependent()) { |
| 2725 | // FIXME: Produce a cloned, canonical expression? |
Douglas Gregor | 02cbbd2 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 2726 | Converted = TemplateArgument(Arg); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 2727 | return false; |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2728 | } |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 2729 | |
| 2730 | // C++ [temp.arg.nontype]p5: |
| 2731 | // The following conversions are performed on each expression used |
| 2732 | // as a non-type template-argument. If a non-type |
| 2733 | // template-argument cannot be converted to the type of the |
| 2734 | // corresponding template-parameter then the program is |
| 2735 | // ill-formed. |
| 2736 | // |
| 2737 | // -- for a non-type template-parameter of integral or |
| 2738 | // enumeration type, integral promotions (4.5) and integral |
| 2739 | // conversions (4.7) are applied. |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 2740 | QualType ParamType = InstantiatedParamType; |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 2741 | QualType ArgType = Arg->getType(); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 2742 | if (ParamType->isIntegralType() || ParamType->isEnumeralType()) { |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 2743 | // C++ [temp.arg.nontype]p1: |
| 2744 | // A template-argument for a non-type, non-template |
| 2745 | // template-parameter shall be one of: |
| 2746 | // |
| 2747 | // -- an integral constant-expression of integral or enumeration |
| 2748 | // type; or |
| 2749 | // -- the name of a non-type template-parameter; or |
| 2750 | SourceLocation NonConstantLoc; |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 2751 | llvm::APSInt Value; |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 2752 | if (!ArgType->isIntegralType() && !ArgType->isEnumeralType()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2753 | Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 2754 | diag::err_template_arg_not_integral_or_enumeral) |
| 2755 | << ArgType << Arg->getSourceRange(); |
| 2756 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 2757 | return true; |
| 2758 | } else if (!Arg->isValueDependent() && |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 2759 | !Arg->isIntegerConstantExpr(Value, Context, &NonConstantLoc)) { |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 2760 | Diag(NonConstantLoc, diag::err_template_arg_not_ice) |
| 2761 | << ArgType << Arg->getSourceRange(); |
| 2762 | return true; |
| 2763 | } |
| 2764 | |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 2765 | // From here on out, all we care about are the unqualified forms |
| 2766 | // of the parameter and argument types. |
| 2767 | ParamType = ParamType.getUnqualifiedType(); |
| 2768 | ArgType = ArgType.getUnqualifiedType(); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 2769 | |
| 2770 | // Try to convert the argument to the parameter's type. |
Douglas Gregor | ff52439 | 2009-11-04 21:50:46 +0000 | [diff] [blame] | 2771 | if (Context.hasSameType(ParamType, ArgType)) { |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 2772 | // Okay: no conversion necessary |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 2773 | } else if (CTAK == CTAK_Deduced) { |
| 2774 | // C++ [temp.deduct.type]p17: |
| 2775 | // If, in the declaration of a function template with a non-type |
| 2776 | // template-parameter, the non-type template- parameter is used |
| 2777 | // in an expression in the function parameter-list and, if the |
| 2778 | // corresponding template-argument is deduced, the |
| 2779 | // template-argument type shall match the type of the |
| 2780 | // template-parameter exactly, except that a template-argument |
| 2781 | // deduced from an array bound may be of any integral type. |
| 2782 | Diag(StartLoc, diag::err_deduced_non_type_template_arg_type_mismatch) |
| 2783 | << ArgType << ParamType; |
| 2784 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 2785 | return true; |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 2786 | } else if (IsIntegralPromotion(Arg, ArgType, ParamType) || |
| 2787 | !ParamType->isEnumeralType()) { |
| 2788 | // This is an integral promotion or conversion. |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 2789 | ImpCastExprToType(Arg, ParamType, CastExpr::CK_IntegralCast); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 2790 | } else { |
| 2791 | // We can't perform this conversion. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2792 | Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 2793 | diag::err_template_arg_not_convertible) |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 2794 | << Arg->getType() << InstantiatedParamType << Arg->getSourceRange(); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 2795 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 2796 | return true; |
| 2797 | } |
| 2798 | |
Douglas Gregor | f80a9d5 | 2009-03-14 00:20:21 +0000 | [diff] [blame] | 2799 | QualType IntegerType = Context.getCanonicalType(ParamType); |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2800 | if (const EnumType *Enum = IntegerType->getAs<EnumType>()) |
Douglas Gregor | 02cbbd2 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 2801 | IntegerType = Context.getCanonicalType(Enum->getDecl()->getIntegerType()); |
Douglas Gregor | f80a9d5 | 2009-03-14 00:20:21 +0000 | [diff] [blame] | 2802 | |
| 2803 | if (!Arg->isValueDependent()) { |
Douglas Gregor | 1a6e034 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 2804 | llvm::APSInt OldValue = Value; |
| 2805 | |
| 2806 | // Coerce the template argument's value to the value it will have |
| 2807 | // based on the template parameter's type. |
Douglas Gregor | 0d4fd8e | 2010-03-26 00:39:40 +0000 | [diff] [blame] | 2808 | unsigned AllowedBits = Context.getTypeSize(IntegerType); |
Douglas Gregor | 0d4fd8e | 2010-03-26 00:39:40 +0000 | [diff] [blame] | 2809 | if (Value.getBitWidth() != AllowedBits) |
| 2810 | Value.extOrTrunc(AllowedBits); |
| 2811 | Value.setIsSigned(IntegerType->isSignedIntegerType()); |
Douglas Gregor | 1a6e034 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 2812 | |
| 2813 | // Complain if an unsigned parameter received a negative value. |
| 2814 | if (IntegerType->isUnsignedIntegerType() |
| 2815 | && (OldValue.isSigned() && OldValue.isNegative())) { |
| 2816 | Diag(Arg->getSourceRange().getBegin(), diag::warn_template_arg_negative) |
| 2817 | << OldValue.toString(10) << Value.toString(10) << Param->getType() |
| 2818 | << Arg->getSourceRange(); |
| 2819 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 2820 | } |
| 2821 | |
| 2822 | // Complain if we overflowed the template parameter's type. |
| 2823 | unsigned RequiredBits; |
| 2824 | if (IntegerType->isUnsignedIntegerType()) |
| 2825 | RequiredBits = OldValue.getActiveBits(); |
| 2826 | else if (OldValue.isUnsigned()) |
| 2827 | RequiredBits = OldValue.getActiveBits() + 1; |
| 2828 | else |
| 2829 | RequiredBits = OldValue.getMinSignedBits(); |
| 2830 | if (RequiredBits > AllowedBits) { |
| 2831 | Diag(Arg->getSourceRange().getBegin(), |
| 2832 | diag::warn_template_arg_too_large) |
| 2833 | << OldValue.toString(10) << Value.toString(10) << Param->getType() |
| 2834 | << Arg->getSourceRange(); |
| 2835 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 2836 | } |
Douglas Gregor | f80a9d5 | 2009-03-14 00:20:21 +0000 | [diff] [blame] | 2837 | } |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 2838 | |
Douglas Gregor | 02cbbd2 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 2839 | // Add the value of this argument to the list of converted |
| 2840 | // arguments. We use the bitwidth and signedness of the template |
| 2841 | // parameter. |
| 2842 | if (Arg->isValueDependent()) { |
| 2843 | // The argument is value-dependent. Create a new |
| 2844 | // TemplateArgument with the converted expression. |
| 2845 | Converted = TemplateArgument(Arg); |
| 2846 | return false; |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 2847 | } |
| 2848 | |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2849 | Converted = TemplateArgument(Value, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2850 | ParamType->isEnumeralType() ? ParamType |
Douglas Gregor | 02cbbd2 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 2851 | : IntegerType); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 2852 | return false; |
| 2853 | } |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 2854 | |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2855 | DeclAccessPair FoundResult; // temporary for ResolveOverloadedFunction |
| 2856 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2857 | // C++0x [temp.arg.nontype]p5 bullets 2, 4 and 6 permit conversion |
| 2858 | // from a template argument of type std::nullptr_t to a non-type |
| 2859 | // template parameter of type pointer to object, pointer to |
| 2860 | // function, or pointer-to-member, respectively. |
| 2861 | if (ArgType->isNullPtrType() && |
| 2862 | (ParamType->isPointerType() || ParamType->isMemberPointerType())) { |
| 2863 | Converted = TemplateArgument((NamedDecl *)0); |
| 2864 | return false; |
| 2865 | } |
| 2866 | |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 2867 | // Handle pointer-to-function, reference-to-function, and |
| 2868 | // pointer-to-member-function all in (roughly) the same way. |
| 2869 | if (// -- For a non-type template-parameter of type pointer to |
| 2870 | // function, only the function-to-pointer conversion (4.3) is |
| 2871 | // applied. If the template-argument represents a set of |
| 2872 | // overloaded functions (or a pointer to such), the matching |
| 2873 | // function is selected from the set (13.4). |
| 2874 | (ParamType->isPointerType() && |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2875 | ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType()) || |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 2876 | // -- For a non-type template-parameter of type reference to |
| 2877 | // function, no conversions apply. If the template-argument |
| 2878 | // represents a set of overloaded functions, the matching |
| 2879 | // function is selected from the set (13.4). |
| 2880 | (ParamType->isReferenceType() && |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2881 | ParamType->getAs<ReferenceType>()->getPointeeType()->isFunctionType()) || |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 2882 | // -- For a non-type template-parameter of type pointer to |
| 2883 | // member function, no conversions apply. If the |
| 2884 | // template-argument represents a set of overloaded member |
| 2885 | // functions, the matching member function is selected from |
| 2886 | // the set (13.4). |
| 2887 | (ParamType->isMemberPointerType() && |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2888 | ParamType->getAs<MemberPointerType>()->getPointeeType() |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 2889 | ->isFunctionType())) { |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2890 | |
Douglas Gregor | 1a8cf73 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 2891 | if (Arg->getType() == Context.OverloadTy) { |
| 2892 | if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg, ParamType, |
| 2893 | true, |
| 2894 | FoundResult)) { |
| 2895 | if (DiagnoseUseOfDecl(Fn, Arg->getSourceRange().getBegin())) |
| 2896 | return true; |
| 2897 | |
| 2898 | Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn); |
| 2899 | ArgType = Arg->getType(); |
| 2900 | } else |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 2901 | return true; |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 2902 | } |
Douglas Gregor | 1a8cf73 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 2903 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2904 | if (!ParamType->isMemberPointerType()) |
| 2905 | return CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param, |
| 2906 | ParamType, |
| 2907 | Arg, Converted); |
| 2908 | |
| 2909 | if (IsQualificationConversion(ArgType, ParamType.getNonReferenceType())) { |
| 2910 | ImpCastExprToType(Arg, ParamType, CastExpr::CK_NoOp, |
| 2911 | Arg->isLvalue(Context) == Expr::LV_Valid); |
| 2912 | } else if (!Context.hasSameUnqualifiedType(ArgType, |
| 2913 | ParamType.getNonReferenceType())) { |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 2914 | // We can't perform this conversion. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2915 | Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 2916 | diag::err_template_arg_not_convertible) |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 2917 | << Arg->getType() << InstantiatedParamType << Arg->getSourceRange(); |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 2918 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 2919 | return true; |
| 2920 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2921 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2922 | return CheckTemplateArgumentPointerToMember(Arg, Converted); |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 2923 | } |
| 2924 | |
Chris Lattner | fe90de7 | 2009-02-20 21:37:53 +0000 | [diff] [blame] | 2925 | if (ParamType->isPointerType()) { |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 2926 | // -- for a non-type template-parameter of type pointer to |
| 2927 | // object, qualification conversions (4.4) and the |
| 2928 | // array-to-pointer conversion (4.2) are applied. |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 2929 | // C++0x also allows a value of std::nullptr_t. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2930 | assert(ParamType->getAs<PointerType>()->getPointeeType()->isObjectType() && |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 2931 | "Only object pointers allowed here"); |
Douglas Gregor | f684e6e | 2009-02-11 00:44:29 +0000 | [diff] [blame] | 2932 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2933 | return CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param, |
| 2934 | ParamType, |
| 2935 | Arg, Converted); |
Douglas Gregor | f684e6e | 2009-02-11 00:44:29 +0000 | [diff] [blame] | 2936 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2937 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2938 | if (const ReferenceType *ParamRefType = ParamType->getAs<ReferenceType>()) { |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 2939 | // -- For a non-type template-parameter of type reference to |
| 2940 | // object, no conversions apply. The type referred to by the |
| 2941 | // reference may be more cv-qualified than the (otherwise |
| 2942 | // identical) type of the template-argument. The |
| 2943 | // template-parameter is bound directly to the |
| 2944 | // template-argument, which must be an lvalue. |
Douglas Gregor | bad0e65 | 2009-03-24 20:32:41 +0000 | [diff] [blame] | 2945 | assert(ParamRefType->getPointeeType()->isObjectType() && |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 2946 | "Only object references allowed here"); |
Douglas Gregor | f684e6e | 2009-02-11 00:44:29 +0000 | [diff] [blame] | 2947 | |
Douglas Gregor | 1a8cf73 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 2948 | if (Arg->getType() == Context.OverloadTy) { |
| 2949 | if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg, |
| 2950 | ParamRefType->getPointeeType(), |
| 2951 | true, |
| 2952 | FoundResult)) { |
| 2953 | if (DiagnoseUseOfDecl(Fn, Arg->getSourceRange().getBegin())) |
| 2954 | return true; |
| 2955 | |
| 2956 | Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn); |
| 2957 | ArgType = Arg->getType(); |
| 2958 | } else |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2959 | return true; |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 2960 | } |
Douglas Gregor | 1a8cf73 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 2961 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2962 | return CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param, |
| 2963 | ParamType, |
| 2964 | Arg, Converted); |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 2965 | } |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 2966 | |
| 2967 | // -- For a non-type template-parameter of type pointer to data |
| 2968 | // member, qualification conversions (4.4) are applied. |
| 2969 | assert(ParamType->isMemberPointerType() && "Only pointers to members remain"); |
| 2970 | |
Douglas Gregor | 8e6563b | 2009-02-11 18:22:40 +0000 | [diff] [blame] | 2971 | if (Context.hasSameUnqualifiedType(ParamType, ArgType)) { |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 2972 | // Types match exactly: nothing more to do here. |
| 2973 | } else if (IsQualificationConversion(ArgType, ParamType)) { |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2974 | ImpCastExprToType(Arg, ParamType, CastExpr::CK_NoOp, |
| 2975 | Arg->isLvalue(Context) == Expr::LV_Valid); |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 2976 | } else { |
| 2977 | // We can't perform this conversion. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2978 | Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 2979 | diag::err_template_arg_not_convertible) |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 2980 | << Arg->getType() << InstantiatedParamType << Arg->getSourceRange(); |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 2981 | Diag(Param->getLocation(), diag::note_template_param_here); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2982 | return true; |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 2983 | } |
| 2984 | |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 2985 | return CheckTemplateArgumentPointerToMember(Arg, Converted); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2986 | } |
| 2987 | |
| 2988 | /// \brief Check a template argument against its corresponding |
| 2989 | /// template template parameter. |
| 2990 | /// |
| 2991 | /// This routine implements the semantics of C++ [temp.arg.template]. |
| 2992 | /// It returns true if an error occurred, and false otherwise. |
| 2993 | bool Sema::CheckTemplateArgument(TemplateTemplateParmDecl *Param, |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2994 | const TemplateArgumentLoc &Arg) { |
| 2995 | TemplateName Name = Arg.getArgument().getAsTemplate(); |
| 2996 | TemplateDecl *Template = Name.getAsTemplateDecl(); |
| 2997 | if (!Template) { |
| 2998 | // Any dependent template name is fine. |
| 2999 | assert(Name.isDependent() && "Non-dependent template isn't a declaration?"); |
| 3000 | return false; |
| 3001 | } |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 3002 | |
| 3003 | // C++ [temp.arg.template]p1: |
| 3004 | // A template-argument for a template template-parameter shall be |
| 3005 | // the name of a class template, expressed as id-expression. Only |
| 3006 | // primary class templates are considered when matching the |
| 3007 | // template template argument with the corresponding parameter; |
| 3008 | // partial specializations are not considered even if their |
| 3009 | // parameter lists match that of the template template parameter. |
Douglas Gregor | ba1ecb5 | 2009-06-12 19:43:02 +0000 | [diff] [blame] | 3010 | // |
| 3011 | // Note that we also allow template template parameters here, which |
| 3012 | // will happen when we are dealing with, e.g., class template |
| 3013 | // partial specializations. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3014 | if (!isa<ClassTemplateDecl>(Template) && |
Douglas Gregor | ba1ecb5 | 2009-06-12 19:43:02 +0000 | [diff] [blame] | 3015 | !isa<TemplateTemplateParmDecl>(Template)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3016 | assert(isa<FunctionTemplateDecl>(Template) && |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 3017 | "Only function templates are possible here"); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 3018 | Diag(Arg.getLocation(), diag::err_template_arg_not_class_template); |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3019 | Diag(Template->getLocation(), diag::note_template_arg_refers_here_func) |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 3020 | << Template; |
| 3021 | } |
| 3022 | |
| 3023 | return !TemplateParameterListsAreEqual(Template->getTemplateParameters(), |
| 3024 | Param->getTemplateParameters(), |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 3025 | true, |
| 3026 | TPL_TemplateTemplateArgumentMatch, |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 3027 | Arg.getLocation()); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3028 | } |
| 3029 | |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3030 | /// \brief Given a non-type template argument that refers to a |
| 3031 | /// declaration and the type of its corresponding non-type template |
| 3032 | /// parameter, produce an expression that properly refers to that |
| 3033 | /// declaration. |
| 3034 | Sema::OwningExprResult |
| 3035 | Sema::BuildExpressionFromDeclTemplateArgument(const TemplateArgument &Arg, |
| 3036 | QualType ParamType, |
| 3037 | SourceLocation Loc) { |
| 3038 | assert(Arg.getKind() == TemplateArgument::Declaration && |
| 3039 | "Only declaration template arguments permitted here"); |
| 3040 | ValueDecl *VD = cast<ValueDecl>(Arg.getAsDecl()); |
| 3041 | |
| 3042 | if (VD->getDeclContext()->isRecord() && |
| 3043 | (isa<CXXMethodDecl>(VD) || isa<FieldDecl>(VD))) { |
| 3044 | // If the value is a class member, we might have a pointer-to-member. |
| 3045 | // Determine whether the non-type template template parameter is of |
| 3046 | // pointer-to-member type. If so, we need to build an appropriate |
| 3047 | // expression for a pointer-to-member, since a "normal" DeclRefExpr |
| 3048 | // would refer to the member itself. |
| 3049 | if (ParamType->isMemberPointerType()) { |
| 3050 | QualType ClassType |
| 3051 | = Context.getTypeDeclType(cast<RecordDecl>(VD->getDeclContext())); |
| 3052 | NestedNameSpecifier *Qualifier |
| 3053 | = NestedNameSpecifier::Create(Context, 0, false, ClassType.getTypePtr()); |
| 3054 | CXXScopeSpec SS; |
| 3055 | SS.setScopeRep(Qualifier); |
| 3056 | OwningExprResult RefExpr = BuildDeclRefExpr(VD, |
| 3057 | VD->getType().getNonReferenceType(), |
| 3058 | Loc, |
| 3059 | &SS); |
| 3060 | if (RefExpr.isInvalid()) |
| 3061 | return ExprError(); |
| 3062 | |
| 3063 | RefExpr = CreateBuiltinUnaryOp(Loc, UnaryOperator::AddrOf, move(RefExpr)); |
Douglas Gregor | c0c8300 | 2010-04-30 21:46:38 +0000 | [diff] [blame] | 3064 | |
| 3065 | // We might need to perform a trailing qualification conversion, since |
| 3066 | // the element type on the parameter could be more qualified than the |
| 3067 | // element type in the expression we constructed. |
| 3068 | if (IsQualificationConversion(((Expr*) RefExpr.get())->getType(), |
| 3069 | ParamType.getUnqualifiedType())) { |
| 3070 | Expr *RefE = RefExpr.takeAs<Expr>(); |
| 3071 | ImpCastExprToType(RefE, ParamType.getUnqualifiedType(), |
| 3072 | CastExpr::CK_NoOp); |
| 3073 | RefExpr = Owned(RefE); |
| 3074 | } |
| 3075 | |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3076 | assert(!RefExpr.isInvalid() && |
| 3077 | Context.hasSameType(((Expr*) RefExpr.get())->getType(), |
Douglas Gregor | c0c8300 | 2010-04-30 21:46:38 +0000 | [diff] [blame] | 3078 | ParamType.getUnqualifiedType())); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3079 | return move(RefExpr); |
| 3080 | } |
| 3081 | } |
| 3082 | |
| 3083 | QualType T = VD->getType().getNonReferenceType(); |
| 3084 | if (ParamType->isPointerType()) { |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3085 | // When the non-type template parameter is a pointer, take the |
| 3086 | // address of the declaration. |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3087 | OwningExprResult RefExpr = BuildDeclRefExpr(VD, T, Loc); |
| 3088 | if (RefExpr.isInvalid()) |
| 3089 | return ExprError(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3090 | |
| 3091 | if (T->isFunctionType() || T->isArrayType()) { |
| 3092 | // Decay functions and arrays. |
| 3093 | Expr *RefE = (Expr *)RefExpr.get(); |
| 3094 | DefaultFunctionArrayConversion(RefE); |
| 3095 | if (RefE != RefExpr.get()) { |
| 3096 | RefExpr.release(); |
| 3097 | RefExpr = Owned(RefE); |
| 3098 | } |
| 3099 | |
| 3100 | return move(RefExpr); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3101 | } |
| 3102 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3103 | // Take the address of everything else |
| 3104 | return CreateBuiltinUnaryOp(Loc, UnaryOperator::AddrOf, move(RefExpr)); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3105 | } |
| 3106 | |
| 3107 | // If the non-type template parameter has reference type, qualify the |
| 3108 | // resulting declaration reference with the extra qualifiers on the |
| 3109 | // type that the reference refers to. |
| 3110 | if (const ReferenceType *TargetRef = ParamType->getAs<ReferenceType>()) |
| 3111 | T = Context.getQualifiedType(T, TargetRef->getPointeeType().getQualifiers()); |
| 3112 | |
| 3113 | return BuildDeclRefExpr(VD, T, Loc); |
| 3114 | } |
| 3115 | |
| 3116 | /// \brief Construct a new expression that refers to the given |
| 3117 | /// integral template argument with the given source-location |
| 3118 | /// information. |
| 3119 | /// |
| 3120 | /// This routine takes care of the mapping from an integral template |
| 3121 | /// argument (which may have any integral type) to the appropriate |
| 3122 | /// literal value. |
| 3123 | Sema::OwningExprResult |
| 3124 | Sema::BuildExpressionFromIntegralTemplateArgument(const TemplateArgument &Arg, |
| 3125 | SourceLocation Loc) { |
| 3126 | assert(Arg.getKind() == TemplateArgument::Integral && |
| 3127 | "Operation is only value for integral template arguments"); |
| 3128 | QualType T = Arg.getIntegralType(); |
| 3129 | if (T->isCharType() || T->isWideCharType()) |
| 3130 | return Owned(new (Context) CharacterLiteral( |
| 3131 | Arg.getAsIntegral()->getZExtValue(), |
| 3132 | T->isWideCharType(), |
| 3133 | T, |
| 3134 | Loc)); |
| 3135 | if (T->isBooleanType()) |
| 3136 | return Owned(new (Context) CXXBoolLiteralExpr( |
| 3137 | Arg.getAsIntegral()->getBoolValue(), |
| 3138 | T, |
| 3139 | Loc)); |
| 3140 | |
| 3141 | return Owned(new (Context) IntegerLiteral(*Arg.getAsIntegral(), T, Loc)); |
| 3142 | } |
| 3143 | |
| 3144 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3145 | /// \brief Determine whether the given template parameter lists are |
| 3146 | /// equivalent. |
| 3147 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3148 | /// \param New The new template parameter list, typically written in the |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3149 | /// source code as part of a new template declaration. |
| 3150 | /// |
| 3151 | /// \param Old The old template parameter list, typically found via |
| 3152 | /// name lookup of the template declared with this template parameter |
| 3153 | /// list. |
| 3154 | /// |
| 3155 | /// \param Complain If true, this routine will produce a diagnostic if |
| 3156 | /// the template parameter lists are not equivalent. |
| 3157 | /// |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 3158 | /// \param Kind describes how we are to match the template parameter lists. |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 3159 | /// |
| 3160 | /// \param TemplateArgLoc If this source location is valid, then we |
| 3161 | /// are actually checking the template parameter list of a template |
| 3162 | /// argument (New) against the template parameter list of its |
| 3163 | /// corresponding template template parameter (Old). We produce |
| 3164 | /// slightly different diagnostics in this scenario. |
| 3165 | /// |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3166 | /// \returns True if the template parameter lists are equal, false |
| 3167 | /// otherwise. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3168 | bool |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3169 | Sema::TemplateParameterListsAreEqual(TemplateParameterList *New, |
| 3170 | TemplateParameterList *Old, |
| 3171 | bool Complain, |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 3172 | TemplateParameterListEqualKind Kind, |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 3173 | SourceLocation TemplateArgLoc) { |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3174 | if (Old->size() != New->size()) { |
| 3175 | if (Complain) { |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 3176 | unsigned NextDiag = diag::err_template_param_list_different_arity; |
| 3177 | if (TemplateArgLoc.isValid()) { |
| 3178 | Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch); |
| 3179 | NextDiag = diag::note_template_param_list_different_arity; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3180 | } |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 3181 | Diag(New->getTemplateLoc(), NextDiag) |
| 3182 | << (New->size() > Old->size()) |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 3183 | << (Kind != TPL_TemplateMatch) |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 3184 | << SourceRange(New->getTemplateLoc(), New->getRAngleLoc()); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3185 | Diag(Old->getTemplateLoc(), diag::note_template_prev_declaration) |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 3186 | << (Kind != TPL_TemplateMatch) |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3187 | << SourceRange(Old->getTemplateLoc(), Old->getRAngleLoc()); |
| 3188 | } |
| 3189 | |
| 3190 | return false; |
| 3191 | } |
| 3192 | |
| 3193 | for (TemplateParameterList::iterator OldParm = Old->begin(), |
| 3194 | OldParmEnd = Old->end(), NewParm = New->begin(); |
| 3195 | OldParm != OldParmEnd; ++OldParm, ++NewParm) { |
| 3196 | if ((*OldParm)->getKind() != (*NewParm)->getKind()) { |
Douglas Gregor | 34d1dc9 | 2009-06-24 16:50:40 +0000 | [diff] [blame] | 3197 | if (Complain) { |
| 3198 | unsigned NextDiag = diag::err_template_param_different_kind; |
| 3199 | if (TemplateArgLoc.isValid()) { |
| 3200 | Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch); |
| 3201 | NextDiag = diag::note_template_param_different_kind; |
| 3202 | } |
| 3203 | Diag((*NewParm)->getLocation(), NextDiag) |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 3204 | << (Kind != TPL_TemplateMatch); |
Douglas Gregor | 34d1dc9 | 2009-06-24 16:50:40 +0000 | [diff] [blame] | 3205 | Diag((*OldParm)->getLocation(), diag::note_template_prev_declaration) |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 3206 | << (Kind != TPL_TemplateMatch); |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 3207 | } |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3208 | return false; |
| 3209 | } |
| 3210 | |
| 3211 | if (isa<TemplateTypeParmDecl>(*OldParm)) { |
| 3212 | // Okay; all template type parameters are equivalent (since we |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 3213 | // know we're at the same index). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3214 | } else if (NonTypeTemplateParmDecl *OldNTTP |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3215 | = dyn_cast<NonTypeTemplateParmDecl>(*OldParm)) { |
| 3216 | // The types of non-type template parameters must agree. |
| 3217 | NonTypeTemplateParmDecl *NewNTTP |
| 3218 | = cast<NonTypeTemplateParmDecl>(*NewParm); |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 3219 | |
| 3220 | // If we are matching a template template argument to a template |
| 3221 | // template parameter and one of the non-type template parameter types |
| 3222 | // is dependent, then we must wait until template instantiation time |
| 3223 | // to actually compare the arguments. |
| 3224 | if (Kind == TPL_TemplateTemplateArgumentMatch && |
| 3225 | (OldNTTP->getType()->isDependentType() || |
| 3226 | NewNTTP->getType()->isDependentType())) |
| 3227 | continue; |
| 3228 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3229 | if (Context.getCanonicalType(OldNTTP->getType()) != |
| 3230 | Context.getCanonicalType(NewNTTP->getType())) { |
| 3231 | if (Complain) { |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 3232 | unsigned NextDiag = diag::err_template_nontype_parm_different_type; |
| 3233 | if (TemplateArgLoc.isValid()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3234 | Diag(TemplateArgLoc, |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 3235 | diag::err_template_arg_template_params_mismatch); |
| 3236 | NextDiag = diag::note_template_nontype_parm_different_type; |
| 3237 | } |
| 3238 | Diag(NewNTTP->getLocation(), NextDiag) |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3239 | << NewNTTP->getType() |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 3240 | << (Kind != TPL_TemplateMatch); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3241 | Diag(OldNTTP->getLocation(), |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3242 | diag::note_template_nontype_parm_prev_declaration) |
| 3243 | << OldNTTP->getType(); |
| 3244 | } |
| 3245 | return false; |
| 3246 | } |
| 3247 | } else { |
| 3248 | // The template parameter lists of template template |
| 3249 | // parameters must agree. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3250 | assert(isa<TemplateTemplateParmDecl>(*OldParm) && |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3251 | "Only template template parameters handled here"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3252 | TemplateTemplateParmDecl *OldTTP |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3253 | = cast<TemplateTemplateParmDecl>(*OldParm); |
| 3254 | TemplateTemplateParmDecl *NewTTP |
| 3255 | = cast<TemplateTemplateParmDecl>(*NewParm); |
| 3256 | if (!TemplateParameterListsAreEqual(NewTTP->getTemplateParameters(), |
| 3257 | OldTTP->getTemplateParameters(), |
| 3258 | Complain, |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 3259 | (Kind == TPL_TemplateMatch? TPL_TemplateTemplateParmMatch : Kind), |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 3260 | TemplateArgLoc)) |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3261 | return false; |
| 3262 | } |
| 3263 | } |
| 3264 | |
| 3265 | return true; |
| 3266 | } |
| 3267 | |
| 3268 | /// \brief Check whether a template can be declared within this scope. |
| 3269 | /// |
| 3270 | /// If the template declaration is valid in this scope, returns |
| 3271 | /// false. Otherwise, issues a diagnostic and returns true. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3272 | bool |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 3273 | Sema::CheckTemplateDeclScope(Scope *S, TemplateParameterList *TemplateParams) { |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3274 | // Find the nearest enclosing declaration scope. |
| 3275 | while ((S->getFlags() & Scope::DeclScope) == 0 || |
| 3276 | (S->getFlags() & Scope::TemplateParamScope) != 0) |
| 3277 | S = S->getParent(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3278 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3279 | // C++ [temp]p2: |
| 3280 | // A template-declaration can appear only as a namespace scope or |
| 3281 | // class scope declaration. |
| 3282 | DeclContext *Ctx = static_cast<DeclContext *>(S->getEntity()); |
Eli Friedman | 1503f77 | 2009-07-31 01:43:05 +0000 | [diff] [blame] | 3283 | if (Ctx && isa<LinkageSpecDecl>(Ctx) && |
| 3284 | cast<LinkageSpecDecl>(Ctx)->getLanguage() != LinkageSpecDecl::lang_cxx) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3285 | return Diag(TemplateParams->getTemplateLoc(), diag::err_template_linkage) |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 3286 | << TemplateParams->getSourceRange(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3287 | |
Eli Friedman | 1503f77 | 2009-07-31 01:43:05 +0000 | [diff] [blame] | 3288 | while (Ctx && isa<LinkageSpecDecl>(Ctx)) |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3289 | Ctx = Ctx->getParent(); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3290 | |
| 3291 | if (Ctx && (Ctx->isFileContext() || Ctx->isRecord())) |
| 3292 | return false; |
| 3293 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3294 | return Diag(TemplateParams->getTemplateLoc(), |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 3295 | diag::err_template_outside_namespace_or_class_scope) |
| 3296 | << TemplateParams->getSourceRange(); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3297 | } |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3298 | |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3299 | /// \brief Determine what kind of template specialization the given declaration |
| 3300 | /// is. |
| 3301 | static TemplateSpecializationKind getTemplateSpecializationKind(NamedDecl *D) { |
| 3302 | if (!D) |
| 3303 | return TSK_Undeclared; |
| 3304 | |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 3305 | if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) |
| 3306 | return Record->getTemplateSpecializationKind(); |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3307 | if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) |
| 3308 | return Function->getTemplateSpecializationKind(); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 3309 | if (VarDecl *Var = dyn_cast<VarDecl>(D)) |
| 3310 | return Var->getTemplateSpecializationKind(); |
| 3311 | |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3312 | return TSK_Undeclared; |
| 3313 | } |
| 3314 | |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 3315 | /// \brief Check whether a specialization is well-formed in the current |
| 3316 | /// context. |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 3317 | /// |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 3318 | /// This routine determines whether a template specialization can be declared |
| 3319 | /// in the current context (C++ [temp.expl.spec]p2). |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3320 | /// |
| 3321 | /// \param S the semantic analysis object for which this check is being |
| 3322 | /// performed. |
| 3323 | /// |
| 3324 | /// \param Specialized the entity being specialized or instantiated, which |
| 3325 | /// may be a kind of template (class template, function template, etc.) or |
| 3326 | /// a member of a class template (member function, static data member, |
| 3327 | /// member class). |
| 3328 | /// |
| 3329 | /// \param PrevDecl the previous declaration of this entity, if any. |
| 3330 | /// |
| 3331 | /// \param Loc the location of the explicit specialization or instantiation of |
| 3332 | /// this entity. |
| 3333 | /// |
| 3334 | /// \param IsPartialSpecialization whether this is a partial specialization of |
| 3335 | /// a class template. |
| 3336 | /// |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3337 | /// \returns true if there was an error that we cannot recover from, false |
| 3338 | /// otherwise. |
| 3339 | static bool CheckTemplateSpecializationScope(Sema &S, |
| 3340 | NamedDecl *Specialized, |
| 3341 | NamedDecl *PrevDecl, |
| 3342 | SourceLocation Loc, |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 3343 | bool IsPartialSpecialization) { |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3344 | // Keep these "kind" numbers in sync with the %select statements in the |
| 3345 | // various diagnostics emitted by this routine. |
| 3346 | int EntityKind = 0; |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 3347 | bool isTemplateSpecialization = false; |
| 3348 | if (isa<ClassTemplateDecl>(Specialized)) { |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3349 | EntityKind = IsPartialSpecialization? 1 : 0; |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 3350 | isTemplateSpecialization = true; |
| 3351 | } else if (isa<FunctionTemplateDecl>(Specialized)) { |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3352 | EntityKind = 2; |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 3353 | isTemplateSpecialization = true; |
| 3354 | } else if (isa<CXXMethodDecl>(Specialized)) |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3355 | EntityKind = 3; |
| 3356 | else if (isa<VarDecl>(Specialized)) |
| 3357 | EntityKind = 4; |
| 3358 | else if (isa<RecordDecl>(Specialized)) |
| 3359 | EntityKind = 5; |
| 3360 | else { |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 3361 | S.Diag(Loc, diag::err_template_spec_unknown_kind); |
| 3362 | S.Diag(Specialized->getLocation(), diag::note_specialized_entity); |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3363 | return true; |
| 3364 | } |
| 3365 | |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 3366 | // C++ [temp.expl.spec]p2: |
| 3367 | // An explicit specialization shall be declared in the namespace |
| 3368 | // of which the template is a member, or, for member templates, in |
| 3369 | // the namespace of which the enclosing class or enclosing class |
| 3370 | // template is a member. An explicit specialization of a member |
| 3371 | // function, member class or static data member of a class |
| 3372 | // template shall be declared in the namespace of which the class |
| 3373 | // template is a member. Such a declaration may also be a |
| 3374 | // definition. If the declaration is not a definition, the |
| 3375 | // specialization may be defined later in the name- space in which |
| 3376 | // the explicit specialization was declared, or in a namespace |
| 3377 | // that encloses the one in which the explicit specialization was |
| 3378 | // declared. |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3379 | if (S.CurContext->getLookupContext()->isFunctionOrMethod()) { |
| 3380 | S.Diag(Loc, diag::err_template_spec_decl_function_scope) |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 3381 | << Specialized; |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 3382 | return true; |
| 3383 | } |
Douglas Gregor | 7974c3b | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 3384 | |
Douglas Gregor | 0a40747 | 2009-10-07 17:30:37 +0000 | [diff] [blame] | 3385 | if (S.CurContext->isRecord() && !IsPartialSpecialization) { |
| 3386 | S.Diag(Loc, diag::err_template_spec_decl_class_scope) |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 3387 | << Specialized; |
Douglas Gregor | 0a40747 | 2009-10-07 17:30:37 +0000 | [diff] [blame] | 3388 | return true; |
| 3389 | } |
| 3390 | |
Douglas Gregor | 7974c3b | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 3391 | // C++ [temp.class.spec]p6: |
| 3392 | // A class template partial specialization may be declared or redeclared |
| 3393 | // in any namespace scope in which its definition may be defined (14.5.1 |
| 3394 | // and 14.5.2). |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3395 | bool ComplainedAboutScope = false; |
Douglas Gregor | 7974c3b | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 3396 | DeclContext *SpecializedContext |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3397 | = Specialized->getDeclContext()->getEnclosingNamespaceContext(); |
Douglas Gregor | 7974c3b | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 3398 | DeclContext *DC = S.CurContext->getEnclosingNamespaceContext(); |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 3399 | if ((!PrevDecl || |
| 3400 | getTemplateSpecializationKind(PrevDecl) == TSK_Undeclared || |
| 3401 | getTemplateSpecializationKind(PrevDecl) == TSK_ImplicitInstantiation)){ |
| 3402 | // There is no prior declaration of this entity, so this |
| 3403 | // specialization must be in the same context as the template |
| 3404 | // itself. |
| 3405 | if (!DC->Equals(SpecializedContext)) { |
| 3406 | if (isa<TranslationUnitDecl>(SpecializedContext)) |
| 3407 | S.Diag(Loc, diag::err_template_spec_decl_out_of_scope_global) |
| 3408 | << EntityKind << Specialized; |
| 3409 | else if (isa<NamespaceDecl>(SpecializedContext)) |
| 3410 | S.Diag(Loc, diag::err_template_spec_decl_out_of_scope) |
| 3411 | << EntityKind << Specialized |
| 3412 | << cast<NamedDecl>(SpecializedContext); |
| 3413 | |
| 3414 | S.Diag(Specialized->getLocation(), diag::note_specialized_entity); |
| 3415 | ComplainedAboutScope = true; |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 3416 | } |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 3417 | } |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3418 | |
| 3419 | // Make sure that this redeclaration (or definition) occurs in an enclosing |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 3420 | // namespace. |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3421 | // Note that HandleDeclarator() performs this check for explicit |
| 3422 | // specializations of function templates, static data members, and member |
| 3423 | // functions, so we skip the check here for those kinds of entities. |
| 3424 | // FIXME: HandleDeclarator's diagnostics aren't quite as good, though. |
Douglas Gregor | 7974c3b | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 3425 | // Should we refactor that check, so that it occurs later? |
| 3426 | if (!ComplainedAboutScope && !DC->Encloses(SpecializedContext) && |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 3427 | !(isa<FunctionTemplateDecl>(Specialized) || isa<VarDecl>(Specialized) || |
| 3428 | isa<FunctionDecl>(Specialized))) { |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3429 | if (isa<TranslationUnitDecl>(SpecializedContext)) |
| 3430 | S.Diag(Loc, diag::err_template_spec_redecl_global_scope) |
| 3431 | << EntityKind << Specialized; |
| 3432 | else if (isa<NamespaceDecl>(SpecializedContext)) |
| 3433 | S.Diag(Loc, diag::err_template_spec_redecl_out_of_scope) |
| 3434 | << EntityKind << Specialized |
| 3435 | << cast<NamedDecl>(SpecializedContext); |
| 3436 | |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 3437 | S.Diag(Specialized->getLocation(), diag::note_specialized_entity); |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 3438 | } |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3439 | |
| 3440 | // FIXME: check for specialization-after-instantiation errors and such. |
| 3441 | |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 3442 | return false; |
| 3443 | } |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3444 | |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 3445 | /// \brief Check the non-type template arguments of a class template |
| 3446 | /// partial specialization according to C++ [temp.class.spec]p9. |
| 3447 | /// |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3448 | /// \param TemplateParams the template parameters of the primary class |
| 3449 | /// template. |
| 3450 | /// |
| 3451 | /// \param TemplateArg the template arguments of the class template |
| 3452 | /// partial specialization. |
| 3453 | /// |
| 3454 | /// \param MirrorsPrimaryTemplate will be set true if the class |
| 3455 | /// template partial specialization arguments are identical to the |
| 3456 | /// implicit template arguments of the primary template. This is not |
| 3457 | /// necessarily an error (C++0x), and it is left to the caller to diagnose |
| 3458 | /// this condition when it is an error. |
| 3459 | /// |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 3460 | /// \returns true if there was an error, false otherwise. |
| 3461 | bool Sema::CheckClassTemplatePartialSpecializationArgs( |
| 3462 | TemplateParameterList *TemplateParams, |
Anders Carlsson | 6360be7 | 2009-06-13 18:20:51 +0000 | [diff] [blame] | 3463 | const TemplateArgumentListBuilder &TemplateArgs, |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3464 | bool &MirrorsPrimaryTemplate) { |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 3465 | // FIXME: the interface to this function will have to change to |
| 3466 | // accommodate variadic templates. |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3467 | MirrorsPrimaryTemplate = true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3468 | |
Anders Carlsson | fb25052 | 2009-06-23 01:26:57 +0000 | [diff] [blame] | 3469 | const TemplateArgument *ArgList = TemplateArgs.getFlatArguments(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3470 | |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 3471 | for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) { |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3472 | // Determine whether the template argument list of the partial |
| 3473 | // specialization is identical to the implicit argument list of |
| 3474 | // the primary template. The caller may need to diagnostic this as |
| 3475 | // an error per C++ [temp.class.spec]p9b3. |
| 3476 | if (MirrorsPrimaryTemplate) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3477 | if (TemplateTypeParmDecl *TTP |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3478 | = dyn_cast<TemplateTypeParmDecl>(TemplateParams->getParam(I))) { |
| 3479 | if (Context.getCanonicalType(Context.getTypeDeclType(TTP)) != |
Anders Carlsson | 6360be7 | 2009-06-13 18:20:51 +0000 | [diff] [blame] | 3480 | Context.getCanonicalType(ArgList[I].getAsType())) |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3481 | MirrorsPrimaryTemplate = false; |
| 3482 | } else if (TemplateTemplateParmDecl *TTP |
| 3483 | = dyn_cast<TemplateTemplateParmDecl>( |
| 3484 | TemplateParams->getParam(I))) { |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 3485 | TemplateName Name = ArgList[I].getAsTemplate(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3486 | TemplateTemplateParmDecl *ArgDecl |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 3487 | = dyn_cast_or_null<TemplateTemplateParmDecl>(Name.getAsTemplateDecl()); |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3488 | if (!ArgDecl || |
| 3489 | ArgDecl->getIndex() != TTP->getIndex() || |
| 3490 | ArgDecl->getDepth() != TTP->getDepth()) |
| 3491 | MirrorsPrimaryTemplate = false; |
| 3492 | } |
| 3493 | } |
| 3494 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3495 | NonTypeTemplateParmDecl *Param |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 3496 | = dyn_cast<NonTypeTemplateParmDecl>(TemplateParams->getParam(I)); |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3497 | if (!Param) { |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 3498 | continue; |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3499 | } |
| 3500 | |
Anders Carlsson | 6360be7 | 2009-06-13 18:20:51 +0000 | [diff] [blame] | 3501 | Expr *ArgExpr = ArgList[I].getAsExpr(); |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3502 | if (!ArgExpr) { |
| 3503 | MirrorsPrimaryTemplate = false; |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 3504 | continue; |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3505 | } |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 3506 | |
| 3507 | // C++ [temp.class.spec]p8: |
| 3508 | // A non-type argument is non-specialized if it is the name of a |
| 3509 | // non-type parameter. All other non-type arguments are |
| 3510 | // specialized. |
| 3511 | // |
| 3512 | // Below, we check the two conditions that only apply to |
| 3513 | // specialized non-type arguments, so skip any non-specialized |
| 3514 | // arguments. |
| 3515 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ArgExpr)) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3516 | if (NonTypeTemplateParmDecl *NTTP |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3517 | = dyn_cast<NonTypeTemplateParmDecl>(DRE->getDecl())) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3518 | if (MirrorsPrimaryTemplate && |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3519 | (Param->getIndex() != NTTP->getIndex() || |
| 3520 | Param->getDepth() != NTTP->getDepth())) |
| 3521 | MirrorsPrimaryTemplate = false; |
| 3522 | |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 3523 | continue; |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3524 | } |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 3525 | |
| 3526 | // C++ [temp.class.spec]p9: |
| 3527 | // Within the argument list of a class template partial |
| 3528 | // specialization, the following restrictions apply: |
| 3529 | // -- A partially specialized non-type argument expression |
| 3530 | // shall not involve a template parameter of the partial |
| 3531 | // specialization except when the argument expression is a |
| 3532 | // simple identifier. |
| 3533 | if (ArgExpr->isTypeDependent() || ArgExpr->isValueDependent()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3534 | Diag(ArgExpr->getLocStart(), |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 3535 | diag::err_dependent_non_type_arg_in_partial_spec) |
| 3536 | << ArgExpr->getSourceRange(); |
| 3537 | return true; |
| 3538 | } |
| 3539 | |
| 3540 | // -- The type of a template parameter corresponding to a |
| 3541 | // specialized non-type argument shall not be dependent on a |
| 3542 | // parameter of the specialization. |
| 3543 | if (Param->getType()->isDependentType()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3544 | Diag(ArgExpr->getLocStart(), |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 3545 | diag::err_dependent_typed_non_type_arg_in_partial_spec) |
| 3546 | << Param->getType() |
| 3547 | << ArgExpr->getSourceRange(); |
| 3548 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 3549 | return true; |
| 3550 | } |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3551 | |
| 3552 | MirrorsPrimaryTemplate = false; |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 3553 | } |
| 3554 | |
| 3555 | return false; |
| 3556 | } |
| 3557 | |
Douglas Gregor | dc0a11c | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 3558 | /// \brief Retrieve the previous declaration of the given declaration. |
| 3559 | static NamedDecl *getPreviousDecl(NamedDecl *ND) { |
| 3560 | if (VarDecl *VD = dyn_cast<VarDecl>(ND)) |
| 3561 | return VD->getPreviousDeclaration(); |
| 3562 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) |
| 3563 | return FD->getPreviousDeclaration(); |
| 3564 | if (TagDecl *TD = dyn_cast<TagDecl>(ND)) |
| 3565 | return TD->getPreviousDeclaration(); |
| 3566 | if (TypedefDecl *TD = dyn_cast<TypedefDecl>(ND)) |
| 3567 | return TD->getPreviousDeclaration(); |
| 3568 | if (FunctionTemplateDecl *FTD = dyn_cast<FunctionTemplateDecl>(ND)) |
| 3569 | return FTD->getPreviousDeclaration(); |
| 3570 | if (ClassTemplateDecl *CTD = dyn_cast<ClassTemplateDecl>(ND)) |
| 3571 | return CTD->getPreviousDeclaration(); |
| 3572 | return 0; |
| 3573 | } |
| 3574 | |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 3575 | Sema::DeclResult |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 3576 | Sema::ActOnClassTemplateSpecialization(Scope *S, unsigned TagSpec, |
| 3577 | TagUseKind TUK, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3578 | SourceLocation KWLoc, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 3579 | CXXScopeSpec &SS, |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 3580 | TemplateTy TemplateD, |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3581 | SourceLocation TemplateNameLoc, |
| 3582 | SourceLocation LAngleLoc, |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 3583 | ASTTemplateArgsPtr TemplateArgsIn, |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3584 | SourceLocation RAngleLoc, |
| 3585 | AttributeList *Attr, |
| 3586 | MultiTemplateParamsArg TemplateParameterLists) { |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 3587 | assert(TUK != TUK_Reference && "References are not specializations"); |
John McCall | f1bbbb4 | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 3588 | |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3589 | // Find the class template we're specializing |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 3590 | TemplateName Name = TemplateD.getAsVal<TemplateName>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3591 | ClassTemplateDecl *ClassTemplate |
Douglas Gregor | 8b13c08 | 2009-11-12 00:46:20 +0000 | [diff] [blame] | 3592 | = dyn_cast_or_null<ClassTemplateDecl>(Name.getAsTemplateDecl()); |
| 3593 | |
| 3594 | if (!ClassTemplate) { |
| 3595 | Diag(TemplateNameLoc, diag::err_not_class_template_specialization) |
| 3596 | << (Name.getAsTemplateDecl() && |
| 3597 | isa<TemplateTemplateParmDecl>(Name.getAsTemplateDecl())); |
| 3598 | return true; |
| 3599 | } |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3600 | |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 3601 | bool isExplicitSpecialization = false; |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 3602 | bool isPartialSpecialization = false; |
| 3603 | |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 3604 | // Check the validity of the template headers that introduce this |
| 3605 | // template. |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 3606 | // FIXME: We probably shouldn't complain about these headers for |
| 3607 | // friend declarations. |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 3608 | TemplateParameterList *TemplateParams |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3609 | = MatchTemplateParametersToScopeSpecifier(TemplateNameLoc, SS, |
| 3610 | (TemplateParameterList**)TemplateParameterLists.get(), |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 3611 | TemplateParameterLists.size(), |
John McCall | 77e8b11 | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 3612 | TUK == TUK_Friend, |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 3613 | isExplicitSpecialization); |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 3614 | if (TemplateParams && TemplateParams->size() > 0) { |
| 3615 | isPartialSpecialization = true; |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 3616 | |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 3617 | // C++ [temp.class.spec]p10: |
| 3618 | // The template parameter list of a specialization shall not |
| 3619 | // contain default template argument values. |
| 3620 | for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) { |
| 3621 | Decl *Param = TemplateParams->getParam(I); |
| 3622 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) { |
| 3623 | if (TTP->hasDefaultArgument()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3624 | Diag(TTP->getDefaultArgumentLoc(), |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 3625 | diag::err_default_arg_in_partial_spec); |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 3626 | TTP->removeDefaultArgument(); |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 3627 | } |
| 3628 | } else if (NonTypeTemplateParmDecl *NTTP |
| 3629 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
| 3630 | if (Expr *DefArg = NTTP->getDefaultArgument()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3631 | Diag(NTTP->getDefaultArgumentLoc(), |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 3632 | diag::err_default_arg_in_partial_spec) |
| 3633 | << DefArg->getSourceRange(); |
| 3634 | NTTP->setDefaultArgument(0); |
| 3635 | DefArg->Destroy(Context); |
| 3636 | } |
| 3637 | } else { |
| 3638 | TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(Param); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 3639 | if (TTP->hasDefaultArgument()) { |
| 3640 | Diag(TTP->getDefaultArgument().getLocation(), |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 3641 | diag::err_default_arg_in_partial_spec) |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 3642 | << TTP->getDefaultArgument().getSourceRange(); |
| 3643 | TTP->setDefaultArgument(TemplateArgumentLoc()); |
Douglas Gregor | ba1ecb5 | 2009-06-12 19:43:02 +0000 | [diff] [blame] | 3644 | } |
| 3645 | } |
| 3646 | } |
Douglas Gregor | a735b20 | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 3647 | } else if (TemplateParams) { |
| 3648 | if (TUK == TUK_Friend) |
| 3649 | Diag(KWLoc, diag::err_template_spec_friend) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 3650 | << FixItHint::CreateRemoval( |
Douglas Gregor | a735b20 | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 3651 | SourceRange(TemplateParams->getTemplateLoc(), |
| 3652 | TemplateParams->getRAngleLoc())) |
| 3653 | << SourceRange(LAngleLoc, RAngleLoc); |
| 3654 | else |
| 3655 | isExplicitSpecialization = true; |
| 3656 | } else if (TUK != TUK_Friend) { |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 3657 | Diag(KWLoc, diag::err_template_spec_needs_header) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 3658 | << FixItHint::CreateInsertion(KWLoc, "template<> "); |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 3659 | isExplicitSpecialization = true; |
| 3660 | } |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 3661 | |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3662 | // Check that the specialization uses the same tag kind as the |
| 3663 | // original template. |
| 3664 | TagDecl::TagKind Kind; |
| 3665 | switch (TagSpec) { |
| 3666 | default: assert(0 && "Unknown tag type!"); |
| 3667 | case DeclSpec::TST_struct: Kind = TagDecl::TK_struct; break; |
| 3668 | case DeclSpec::TST_union: Kind = TagDecl::TK_union; break; |
| 3669 | case DeclSpec::TST_class: Kind = TagDecl::TK_class; break; |
| 3670 | } |
Douglas Gregor | 501c5ce | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 3671 | if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3672 | Kind, KWLoc, |
Douglas Gregor | 501c5ce | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 3673 | *ClassTemplate->getIdentifier())) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3674 | Diag(KWLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 3675 | << ClassTemplate |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 3676 | << FixItHint::CreateReplacement(KWLoc, |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 3677 | ClassTemplate->getTemplatedDecl()->getKindName()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3678 | Diag(ClassTemplate->getTemplatedDecl()->getLocation(), |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3679 | diag::note_previous_use); |
| 3680 | Kind = ClassTemplate->getTemplatedDecl()->getTagKind(); |
| 3681 | } |
| 3682 | |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 3683 | // Translate the parser's template argument list in our AST format. |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 3684 | TemplateArgumentListInfo TemplateArgs; |
| 3685 | TemplateArgs.setLAngleLoc(LAngleLoc); |
| 3686 | TemplateArgs.setRAngleLoc(RAngleLoc); |
Douglas Gregor | 314b97f | 2009-11-10 19:49:08 +0000 | [diff] [blame] | 3687 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 3688 | |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3689 | // Check that the template argument list is well-formed for this |
| 3690 | // template. |
Anders Carlsson | fb25052 | 2009-06-23 01:26:57 +0000 | [diff] [blame] | 3691 | TemplateArgumentListBuilder Converted(ClassTemplate->getTemplateParameters(), |
| 3692 | TemplateArgs.size()); |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 3693 | if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc, |
| 3694 | TemplateArgs, false, Converted)) |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 3695 | return true; |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3696 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3697 | assert((Converted.structuredSize() == |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3698 | ClassTemplate->getTemplateParameters()->size()) && |
| 3699 | "Converted template argument list is too short!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3700 | |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 3701 | // Find the class template (partial) specialization declaration that |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3702 | // corresponds to these arguments. |
| 3703 | llvm::FoldingSetNodeID ID; |
Douglas Gregor | ba1ecb5 | 2009-06-12 19:43:02 +0000 | [diff] [blame] | 3704 | if (isPartialSpecialization) { |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3705 | bool MirrorsPrimaryTemplate; |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 3706 | if (CheckClassTemplatePartialSpecializationArgs( |
| 3707 | ClassTemplate->getTemplateParameters(), |
Anders Carlsson | fb25052 | 2009-06-23 01:26:57 +0000 | [diff] [blame] | 3708 | Converted, MirrorsPrimaryTemplate)) |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 3709 | return true; |
| 3710 | |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3711 | if (MirrorsPrimaryTemplate) { |
| 3712 | // C++ [temp.class.spec]p9b3: |
| 3713 | // |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3714 | // -- The argument list of the specialization shall not be identical |
| 3715 | // to the implicit argument list of the primary template. |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3716 | Diag(TemplateNameLoc, diag::err_partial_spec_args_match_primary_template) |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 3717 | << (TUK == TUK_Definition) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 3718 | << FixItHint::CreateRemoval(SourceRange(LAngleLoc, RAngleLoc)); |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 3719 | return CheckClassTemplate(S, TagSpec, TUK, KWLoc, SS, |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3720 | ClassTemplate->getIdentifier(), |
| 3721 | TemplateNameLoc, |
| 3722 | Attr, |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 3723 | TemplateParams, |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3724 | AS_none); |
| 3725 | } |
| 3726 | |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 3727 | // FIXME: Diagnose friend partial specializations |
| 3728 | |
Douglas Gregor | de09096 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 3729 | if (!Name.isDependent() && |
| 3730 | !TemplateSpecializationType::anyDependentTemplateArguments( |
| 3731 | TemplateArgs.getArgumentArray(), |
| 3732 | TemplateArgs.size())) { |
| 3733 | Diag(TemplateNameLoc, diag::err_partial_spec_fully_specialized) |
| 3734 | << ClassTemplate->getDeclName(); |
| 3735 | isPartialSpecialization = false; |
| 3736 | } else { |
| 3737 | // FIXME: Template parameter list matters, too |
| 3738 | ClassTemplatePartialSpecializationDecl::Profile(ID, |
| 3739 | Converted.getFlatArguments(), |
| 3740 | Converted.flatSize(), |
| 3741 | Context); |
| 3742 | } |
| 3743 | } |
| 3744 | |
| 3745 | if (!isPartialSpecialization) |
Anders Carlsson | 1c5976e | 2009-06-05 03:43:12 +0000 | [diff] [blame] | 3746 | ClassTemplateSpecializationDecl::Profile(ID, |
Anders Carlsson | fb25052 | 2009-06-23 01:26:57 +0000 | [diff] [blame] | 3747 | Converted.getFlatArguments(), |
Douglas Gregor | 828e226 | 2009-07-29 16:09:57 +0000 | [diff] [blame] | 3748 | Converted.flatSize(), |
| 3749 | Context); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3750 | void *InsertPos = 0; |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 3751 | ClassTemplateSpecializationDecl *PrevDecl = 0; |
| 3752 | |
| 3753 | if (isPartialSpecialization) |
| 3754 | PrevDecl |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3755 | = ClassTemplate->getPartialSpecializations().FindNodeOrInsertPos(ID, |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 3756 | InsertPos); |
| 3757 | else |
| 3758 | PrevDecl |
| 3759 | = ClassTemplate->getSpecializations().FindNodeOrInsertPos(ID, InsertPos); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3760 | |
| 3761 | ClassTemplateSpecializationDecl *Specialization = 0; |
| 3762 | |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 3763 | // Check whether we can declare a class template specialization in |
| 3764 | // the current scope. |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 3765 | if (TUK != TUK_Friend && |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3766 | CheckTemplateSpecializationScope(*this, ClassTemplate, PrevDecl, |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 3767 | TemplateNameLoc, |
| 3768 | isPartialSpecialization)) |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 3769 | return true; |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 3770 | |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 3771 | // The canonical type |
| 3772 | QualType CanonType; |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 3773 | if (PrevDecl && |
| 3774 | (PrevDecl->getSpecializationKind() == TSK_Undeclared || |
Douglas Gregor | de09096 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 3775 | TUK == TUK_Friend)) { |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3776 | // Since the only prior class template specialization with these |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 3777 | // arguments was referenced but not declared, or we're only |
| 3778 | // referencing this specialization as a friend, reuse that |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3779 | // declaration node as our own, updating its source location to |
| 3780 | // reflect our new declaration. |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3781 | Specialization = PrevDecl; |
Douglas Gregor | 6bc9f7e | 2009-02-25 22:18:32 +0000 | [diff] [blame] | 3782 | Specialization->setLocation(TemplateNameLoc); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3783 | PrevDecl = 0; |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 3784 | CanonType = Context.getTypeDeclType(Specialization); |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 3785 | } else if (isPartialSpecialization) { |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 3786 | // Build the canonical type that describes the converted template |
| 3787 | // arguments of the class template partial specialization. |
Douglas Gregor | de09096 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 3788 | TemplateName CanonTemplate = Context.getCanonicalTemplateName(Name); |
| 3789 | CanonType = Context.getTemplateSpecializationType(CanonTemplate, |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 3790 | Converted.getFlatArguments(), |
| 3791 | Converted.flatSize()); |
| 3792 | |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 3793 | // Create a new class template partial specialization declaration node. |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 3794 | ClassTemplatePartialSpecializationDecl *PrevPartial |
| 3795 | = cast_or_null<ClassTemplatePartialSpecializationDecl>(PrevDecl); |
Douglas Gregor | dc60c1e | 2010-04-30 05:56:50 +0000 | [diff] [blame] | 3796 | unsigned SequenceNumber = PrevPartial? PrevPartial->getSequenceNumber() |
| 3797 | : ClassTemplate->getPartialSpecializations().size(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3798 | ClassTemplatePartialSpecializationDecl *Partial |
| 3799 | = ClassTemplatePartialSpecializationDecl::Create(Context, |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 3800 | ClassTemplate->getDeclContext(), |
Anders Carlsson | 91fdf6f | 2009-06-05 04:06:48 +0000 | [diff] [blame] | 3801 | TemplateNameLoc, |
| 3802 | TemplateParams, |
| 3803 | ClassTemplate, |
Anders Carlsson | fb25052 | 2009-06-23 01:26:57 +0000 | [diff] [blame] | 3804 | Converted, |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 3805 | TemplateArgs, |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 3806 | CanonType, |
Douglas Gregor | dc60c1e | 2010-04-30 05:56:50 +0000 | [diff] [blame] | 3807 | PrevPartial, |
| 3808 | SequenceNumber); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 3809 | SetNestedNameSpecifier(Partial, SS); |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 3810 | |
| 3811 | if (PrevPartial) { |
| 3812 | ClassTemplate->getPartialSpecializations().RemoveNode(PrevPartial); |
| 3813 | ClassTemplate->getPartialSpecializations().GetOrInsertNode(Partial); |
| 3814 | } else { |
| 3815 | ClassTemplate->getPartialSpecializations().InsertNode(Partial, InsertPos); |
| 3816 | } |
| 3817 | Specialization = Partial; |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 3818 | |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 3819 | // If we are providing an explicit specialization of a member class |
| 3820 | // template specialization, make a note of that. |
| 3821 | if (PrevPartial && PrevPartial->getInstantiatedFromMember()) |
| 3822 | PrevPartial->setMemberSpecialization(); |
| 3823 | |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 3824 | // Check that all of the template parameters of the class template |
| 3825 | // partial specialization are deducible from the template |
| 3826 | // arguments. If not, this class template partial specialization |
| 3827 | // will never be used. |
| 3828 | llvm::SmallVector<bool, 8> DeducibleParams; |
| 3829 | DeducibleParams.resize(TemplateParams->size()); |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 3830 | MarkUsedTemplateParameters(Partial->getTemplateArgs(), true, |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 3831 | TemplateParams->getDepth(), |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 3832 | DeducibleParams); |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 3833 | unsigned NumNonDeducible = 0; |
| 3834 | for (unsigned I = 0, N = DeducibleParams.size(); I != N; ++I) |
| 3835 | if (!DeducibleParams[I]) |
| 3836 | ++NumNonDeducible; |
| 3837 | |
| 3838 | if (NumNonDeducible) { |
| 3839 | Diag(TemplateNameLoc, diag::warn_partial_specs_not_deducible) |
| 3840 | << (NumNonDeducible > 1) |
| 3841 | << SourceRange(TemplateNameLoc, RAngleLoc); |
| 3842 | for (unsigned I = 0, N = DeducibleParams.size(); I != N; ++I) { |
| 3843 | if (!DeducibleParams[I]) { |
| 3844 | NamedDecl *Param = cast<NamedDecl>(TemplateParams->getParam(I)); |
| 3845 | if (Param->getDeclName()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3846 | Diag(Param->getLocation(), |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 3847 | diag::note_partial_spec_unused_parameter) |
| 3848 | << Param->getDeclName(); |
| 3849 | else |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3850 | Diag(Param->getLocation(), |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 3851 | diag::note_partial_spec_unused_parameter) |
| 3852 | << std::string("<anonymous>"); |
| 3853 | } |
| 3854 | } |
| 3855 | } |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3856 | } else { |
| 3857 | // Create a new class template specialization declaration node for |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 3858 | // this explicit specialization or friend declaration. |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3859 | Specialization |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3860 | = ClassTemplateSpecializationDecl::Create(Context, |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3861 | ClassTemplate->getDeclContext(), |
| 3862 | TemplateNameLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3863 | ClassTemplate, |
Anders Carlsson | fb25052 | 2009-06-23 01:26:57 +0000 | [diff] [blame] | 3864 | Converted, |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3865 | PrevDecl); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 3866 | SetNestedNameSpecifier(Specialization, SS); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3867 | |
| 3868 | if (PrevDecl) { |
| 3869 | ClassTemplate->getSpecializations().RemoveNode(PrevDecl); |
| 3870 | ClassTemplate->getSpecializations().GetOrInsertNode(Specialization); |
| 3871 | } else { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3872 | ClassTemplate->getSpecializations().InsertNode(Specialization, |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3873 | InsertPos); |
| 3874 | } |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 3875 | |
| 3876 | CanonType = Context.getTypeDeclType(Specialization); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3877 | } |
| 3878 | |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 3879 | // C++ [temp.expl.spec]p6: |
| 3880 | // If a template, a member template or the member of a class template is |
| 3881 | // explicitly specialized then that specialization shall be declared |
| 3882 | // before the first use of that specialization that would cause an implicit |
| 3883 | // instantiation to take place, in every translation unit in which such a |
| 3884 | // use occurs; no diagnostic is required. |
| 3885 | if (PrevDecl && PrevDecl->getPointOfInstantiation().isValid()) { |
Douglas Gregor | dc0a11c | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 3886 | bool Okay = false; |
| 3887 | for (NamedDecl *Prev = PrevDecl; Prev; Prev = getPreviousDecl(Prev)) { |
| 3888 | // Is there any previous explicit specialization declaration? |
| 3889 | if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) { |
| 3890 | Okay = true; |
| 3891 | break; |
| 3892 | } |
| 3893 | } |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 3894 | |
Douglas Gregor | dc0a11c | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 3895 | if (!Okay) { |
| 3896 | SourceRange Range(TemplateNameLoc, RAngleLoc); |
| 3897 | Diag(TemplateNameLoc, diag::err_specialization_after_instantiation) |
| 3898 | << Context.getTypeDeclType(Specialization) << Range; |
| 3899 | |
| 3900 | Diag(PrevDecl->getPointOfInstantiation(), |
| 3901 | diag::note_instantiation_required_here) |
| 3902 | << (PrevDecl->getTemplateSpecializationKind() |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 3903 | != TSK_ImplicitInstantiation); |
Douglas Gregor | dc0a11c | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 3904 | return true; |
| 3905 | } |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 3906 | } |
| 3907 | |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 3908 | // If this is not a friend, note that this is an explicit specialization. |
| 3909 | if (TUK != TUK_Friend) |
| 3910 | Specialization->setSpecializationKind(TSK_ExplicitSpecialization); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3911 | |
| 3912 | // Check that this isn't a redefinition of this specialization. |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 3913 | if (TUK == TUK_Definition) { |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 3914 | if (RecordDecl *Def = Specialization->getDefinition()) { |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3915 | SourceRange Range(TemplateNameLoc, RAngleLoc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3916 | Diag(TemplateNameLoc, diag::err_redefinition) |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 3917 | << Context.getTypeDeclType(Specialization) << Range; |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3918 | Diag(Def->getLocation(), diag::note_previous_definition); |
| 3919 | Specialization->setInvalidDecl(); |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 3920 | return true; |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3921 | } |
| 3922 | } |
| 3923 | |
Douglas Gregor | fc705b8 | 2009-02-26 22:19:44 +0000 | [diff] [blame] | 3924 | // Build the fully-sugared type for this class template |
| 3925 | // specialization as the user wrote in the specialization |
| 3926 | // itself. This means that we'll pretty-print the type retrieved |
| 3927 | // from the specialization's declaration the way that the user |
| 3928 | // actually wrote the specialization, rather than formatting the |
| 3929 | // name based on the "canonical" representation used to store the |
| 3930 | // template arguments in the specialization. |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 3931 | TypeSourceInfo *WrittenTy |
| 3932 | = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc, |
| 3933 | TemplateArgs, CanonType); |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 3934 | if (TUK != TUK_Friend) |
| 3935 | Specialization->setTypeAsWritten(WrittenTy); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 3936 | TemplateArgsIn.release(); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3937 | |
Douglas Gregor | 6bc9f7e | 2009-02-25 22:18:32 +0000 | [diff] [blame] | 3938 | // C++ [temp.expl.spec]p9: |
| 3939 | // A template explicit specialization is in the scope of the |
| 3940 | // namespace in which the template was defined. |
| 3941 | // |
| 3942 | // We actually implement this paragraph where we set the semantic |
| 3943 | // context (in the creation of the ClassTemplateSpecializationDecl), |
| 3944 | // but we also maintain the lexical context where the actual |
| 3945 | // definition occurs. |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3946 | Specialization->setLexicalDeclContext(CurContext); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3947 | |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3948 | // We may be starting the definition of this specialization. |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 3949 | if (TUK == TUK_Definition) |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3950 | Specialization->startDefinition(); |
| 3951 | |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 3952 | if (TUK == TUK_Friend) { |
| 3953 | FriendDecl *Friend = FriendDecl::Create(Context, CurContext, |
| 3954 | TemplateNameLoc, |
John McCall | 32f2fb5 | 2010-03-25 18:04:51 +0000 | [diff] [blame] | 3955 | WrittenTy, |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 3956 | /*FIXME:*/KWLoc); |
| 3957 | Friend->setAccess(AS_public); |
| 3958 | CurContext->addDecl(Friend); |
| 3959 | } else { |
| 3960 | // Add the specialization into its lexical context, so that it can |
| 3961 | // be seen when iterating through the list of declarations in that |
| 3962 | // context. However, specializations are not found by name lookup. |
| 3963 | CurContext->addDecl(Specialization); |
| 3964 | } |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 3965 | return DeclPtrTy::make(Specialization); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3966 | } |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3967 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3968 | Sema::DeclPtrTy |
| 3969 | Sema::ActOnTemplateDeclarator(Scope *S, |
Douglas Gregor | e542c86 | 2009-06-23 23:11:28 +0000 | [diff] [blame] | 3970 | MultiTemplateParamsArg TemplateParameterLists, |
| 3971 | Declarator &D) { |
| 3972 | return HandleDeclarator(S, D, move(TemplateParameterLists), false); |
| 3973 | } |
| 3974 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3975 | Sema::DeclPtrTy |
| 3976 | Sema::ActOnStartOfFunctionTemplateDef(Scope *FnBodyScope, |
Douglas Gregor | 52591bf | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 3977 | MultiTemplateParamsArg TemplateParameterLists, |
| 3978 | Declarator &D) { |
| 3979 | assert(getCurFunctionDecl() == 0 && "Function parsing confused"); |
| 3980 | assert(D.getTypeObject(0).Kind == DeclaratorChunk::Function && |
| 3981 | "Not a function declarator!"); |
| 3982 | DeclaratorChunk::FunctionTypeInfo &FTI = D.getTypeObject(0).Fun; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3983 | |
Douglas Gregor | 52591bf | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 3984 | if (FTI.hasPrototype) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3985 | // FIXME: Diagnose arguments without names in C. |
Douglas Gregor | 52591bf | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 3986 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3987 | |
Douglas Gregor | 52591bf | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 3988 | Scope *ParentScope = FnBodyScope->getParent(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3989 | |
| 3990 | DeclPtrTy DP = HandleDeclarator(ParentScope, D, |
Douglas Gregor | 52591bf | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 3991 | move(TemplateParameterLists), |
| 3992 | /*IsFunctionDefinition=*/true); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3993 | if (FunctionTemplateDecl *FunctionTemplate |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 3994 | = dyn_cast_or_null<FunctionTemplateDecl>(DP.getAs<Decl>())) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3995 | return ActOnStartOfFunctionDef(FnBodyScope, |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3996 | DeclPtrTy::make(FunctionTemplate->getTemplatedDecl())); |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 3997 | if (FunctionDecl *Function = dyn_cast_or_null<FunctionDecl>(DP.getAs<Decl>())) |
| 3998 | return ActOnStartOfFunctionDef(FnBodyScope, DeclPtrTy::make(Function)); |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3999 | return DeclPtrTy(); |
Douglas Gregor | 52591bf | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 4000 | } |
| 4001 | |
John McCall | 7504239 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 4002 | /// \brief Strips various properties off an implicit instantiation |
| 4003 | /// that has just been explicitly specialized. |
| 4004 | static void StripImplicitInstantiation(NamedDecl *D) { |
| 4005 | D->invalidateAttrs(); |
| 4006 | |
| 4007 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 4008 | FD->setInlineSpecified(false); |
| 4009 | } |
| 4010 | } |
| 4011 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4012 | /// \brief Diagnose cases where we have an explicit template specialization |
| 4013 | /// before/after an explicit template instantiation, producing diagnostics |
| 4014 | /// for those cases where they are required and determining whether the |
| 4015 | /// new specialization/instantiation will have any effect. |
| 4016 | /// |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4017 | /// \param NewLoc the location of the new explicit specialization or |
| 4018 | /// instantiation. |
| 4019 | /// |
| 4020 | /// \param NewTSK the kind of the new explicit specialization or instantiation. |
| 4021 | /// |
| 4022 | /// \param PrevDecl the previous declaration of the entity. |
| 4023 | /// |
| 4024 | /// \param PrevTSK the kind of the old explicit specialization or instantiatin. |
| 4025 | /// |
| 4026 | /// \param PrevPointOfInstantiation if valid, indicates where the previus |
| 4027 | /// declaration was instantiated (either implicitly or explicitly). |
| 4028 | /// |
| 4029 | /// \param SuppressNew will be set to true to indicate that the new |
| 4030 | /// specialization or instantiation has no effect and should be ignored. |
| 4031 | /// |
| 4032 | /// \returns true if there was an error that should prevent the introduction of |
| 4033 | /// the new declaration into the AST, false otherwise. |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 4034 | bool |
| 4035 | Sema::CheckSpecializationInstantiationRedecl(SourceLocation NewLoc, |
| 4036 | TemplateSpecializationKind NewTSK, |
| 4037 | NamedDecl *PrevDecl, |
| 4038 | TemplateSpecializationKind PrevTSK, |
| 4039 | SourceLocation PrevPointOfInstantiation, |
| 4040 | bool &SuppressNew) { |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4041 | SuppressNew = false; |
| 4042 | |
| 4043 | switch (NewTSK) { |
| 4044 | case TSK_Undeclared: |
| 4045 | case TSK_ImplicitInstantiation: |
| 4046 | assert(false && "Don't check implicit instantiations here"); |
| 4047 | return false; |
| 4048 | |
| 4049 | case TSK_ExplicitSpecialization: |
| 4050 | switch (PrevTSK) { |
| 4051 | case TSK_Undeclared: |
| 4052 | case TSK_ExplicitSpecialization: |
| 4053 | // Okay, we're just specializing something that is either already |
| 4054 | // explicitly specialized or has merely been mentioned without any |
| 4055 | // instantiation. |
| 4056 | return false; |
| 4057 | |
| 4058 | case TSK_ImplicitInstantiation: |
| 4059 | if (PrevPointOfInstantiation.isInvalid()) { |
| 4060 | // The declaration itself has not actually been instantiated, so it is |
| 4061 | // still okay to specialize it. |
John McCall | 7504239 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 4062 | StripImplicitInstantiation(PrevDecl); |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4063 | return false; |
| 4064 | } |
| 4065 | // Fall through |
| 4066 | |
| 4067 | case TSK_ExplicitInstantiationDeclaration: |
| 4068 | case TSK_ExplicitInstantiationDefinition: |
| 4069 | assert((PrevTSK == TSK_ImplicitInstantiation || |
| 4070 | PrevPointOfInstantiation.isValid()) && |
| 4071 | "Explicit instantiation without point of instantiation?"); |
| 4072 | |
| 4073 | // C++ [temp.expl.spec]p6: |
| 4074 | // If a template, a member template or the member of a class template |
| 4075 | // is explicitly specialized then that specialization shall be declared |
| 4076 | // before the first use of that specialization that would cause an |
| 4077 | // implicit instantiation to take place, in every translation unit in |
| 4078 | // which such a use occurs; no diagnostic is required. |
Douglas Gregor | dc0a11c | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 4079 | for (NamedDecl *Prev = PrevDecl; Prev; Prev = getPreviousDecl(Prev)) { |
| 4080 | // Is there any previous explicit specialization declaration? |
| 4081 | if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) |
| 4082 | return false; |
| 4083 | } |
| 4084 | |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 4085 | Diag(NewLoc, diag::err_specialization_after_instantiation) |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4086 | << PrevDecl; |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 4087 | Diag(PrevPointOfInstantiation, diag::note_instantiation_required_here) |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4088 | << (PrevTSK != TSK_ImplicitInstantiation); |
| 4089 | |
| 4090 | return true; |
| 4091 | } |
| 4092 | break; |
| 4093 | |
| 4094 | case TSK_ExplicitInstantiationDeclaration: |
| 4095 | switch (PrevTSK) { |
| 4096 | case TSK_ExplicitInstantiationDeclaration: |
| 4097 | // This explicit instantiation declaration is redundant (that's okay). |
| 4098 | SuppressNew = true; |
| 4099 | return false; |
| 4100 | |
| 4101 | case TSK_Undeclared: |
| 4102 | case TSK_ImplicitInstantiation: |
| 4103 | // We're explicitly instantiating something that may have already been |
| 4104 | // implicitly instantiated; that's fine. |
| 4105 | return false; |
| 4106 | |
| 4107 | case TSK_ExplicitSpecialization: |
| 4108 | // C++0x [temp.explicit]p4: |
| 4109 | // For a given set of template parameters, if an explicit instantiation |
| 4110 | // of a template appears after a declaration of an explicit |
| 4111 | // specialization for that template, the explicit instantiation has no |
| 4112 | // effect. |
John McCall | e97c32f | 2010-03-02 23:09:38 +0000 | [diff] [blame] | 4113 | SuppressNew = true; |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4114 | return false; |
| 4115 | |
| 4116 | case TSK_ExplicitInstantiationDefinition: |
| 4117 | // C++0x [temp.explicit]p10: |
| 4118 | // If an entity is the subject of both an explicit instantiation |
| 4119 | // declaration and an explicit instantiation definition in the same |
| 4120 | // translation unit, the definition shall follow the declaration. |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 4121 | Diag(NewLoc, |
| 4122 | diag::err_explicit_instantiation_declaration_after_definition); |
| 4123 | Diag(PrevPointOfInstantiation, |
| 4124 | diag::note_explicit_instantiation_definition_here); |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4125 | assert(PrevPointOfInstantiation.isValid() && |
| 4126 | "Explicit instantiation without point of instantiation?"); |
| 4127 | SuppressNew = true; |
| 4128 | return false; |
| 4129 | } |
| 4130 | break; |
| 4131 | |
| 4132 | case TSK_ExplicitInstantiationDefinition: |
| 4133 | switch (PrevTSK) { |
| 4134 | case TSK_Undeclared: |
| 4135 | case TSK_ImplicitInstantiation: |
| 4136 | // We're explicitly instantiating something that may have already been |
| 4137 | // implicitly instantiated; that's fine. |
| 4138 | return false; |
| 4139 | |
| 4140 | case TSK_ExplicitSpecialization: |
| 4141 | // C++ DR 259, C++0x [temp.explicit]p4: |
| 4142 | // For a given set of template parameters, if an explicit |
| 4143 | // instantiation of a template appears after a declaration of |
| 4144 | // an explicit specialization for that template, the explicit |
| 4145 | // instantiation has no effect. |
| 4146 | // |
| 4147 | // 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] | 4148 | // is not harmful to try to explicitly instantiate something that |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4149 | // has been explicitly specialized. |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 4150 | if (!getLangOptions().CPlusPlus0x) { |
| 4151 | Diag(NewLoc, diag::ext_explicit_instantiation_after_specialization) |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4152 | << PrevDecl; |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 4153 | Diag(PrevDecl->getLocation(), |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4154 | diag::note_previous_template_specialization); |
| 4155 | } |
| 4156 | SuppressNew = true; |
| 4157 | return false; |
| 4158 | |
| 4159 | case TSK_ExplicitInstantiationDeclaration: |
| 4160 | // We're explicity instantiating a definition for something for which we |
| 4161 | // were previously asked to suppress instantiations. That's fine. |
| 4162 | return false; |
| 4163 | |
| 4164 | case TSK_ExplicitInstantiationDefinition: |
| 4165 | // C++0x [temp.spec]p5: |
| 4166 | // For a given template and a given set of template-arguments, |
| 4167 | // - an explicit instantiation definition shall appear at most once |
| 4168 | // in a program, |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 4169 | Diag(NewLoc, diag::err_explicit_instantiation_duplicate) |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4170 | << PrevDecl; |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 4171 | Diag(PrevPointOfInstantiation, |
| 4172 | diag::note_previous_explicit_instantiation); |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4173 | SuppressNew = true; |
| 4174 | return false; |
| 4175 | } |
| 4176 | break; |
| 4177 | } |
| 4178 | |
| 4179 | assert(false && "Missing specialization/instantiation case?"); |
| 4180 | |
| 4181 | return false; |
| 4182 | } |
| 4183 | |
John McCall | af2094e | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 4184 | /// \brief Perform semantic analysis for the given dependent function |
| 4185 | /// template specialization. The only possible way to get a dependent |
| 4186 | /// function template specialization is with a friend declaration, |
| 4187 | /// like so: |
| 4188 | /// |
| 4189 | /// template <class T> void foo(T); |
| 4190 | /// template <class T> class A { |
| 4191 | /// friend void foo<>(T); |
| 4192 | /// }; |
| 4193 | /// |
| 4194 | /// There really isn't any useful analysis we can do here, so we |
| 4195 | /// just store the information. |
| 4196 | bool |
| 4197 | Sema::CheckDependentFunctionTemplateSpecialization(FunctionDecl *FD, |
| 4198 | const TemplateArgumentListInfo &ExplicitTemplateArgs, |
| 4199 | LookupResult &Previous) { |
| 4200 | // Remove anything from Previous that isn't a function template in |
| 4201 | // the correct context. |
| 4202 | DeclContext *FDLookupContext = FD->getDeclContext()->getLookupContext(); |
| 4203 | LookupResult::Filter F = Previous.makeFilter(); |
| 4204 | while (F.hasNext()) { |
| 4205 | NamedDecl *D = F.next()->getUnderlyingDecl(); |
| 4206 | if (!isa<FunctionTemplateDecl>(D) || |
| 4207 | !FDLookupContext->Equals(D->getDeclContext()->getLookupContext())) |
| 4208 | F.erase(); |
| 4209 | } |
| 4210 | F.done(); |
| 4211 | |
| 4212 | // Should this be diagnosed here? |
| 4213 | if (Previous.empty()) return true; |
| 4214 | |
| 4215 | FD->setDependentTemplateSpecialization(Context, Previous.asUnresolvedSet(), |
| 4216 | ExplicitTemplateArgs); |
| 4217 | return false; |
| 4218 | } |
| 4219 | |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4220 | /// \brief Perform semantic analysis for the given function template |
| 4221 | /// specialization. |
| 4222 | /// |
| 4223 | /// This routine performs all of the semantic analysis required for an |
| 4224 | /// explicit function template specialization. On successful completion, |
| 4225 | /// the function declaration \p FD will become a function template |
| 4226 | /// specialization. |
| 4227 | /// |
| 4228 | /// \param FD the function declaration, which will be updated to become a |
| 4229 | /// function template specialization. |
| 4230 | /// |
| 4231 | /// \param HasExplicitTemplateArgs whether any template arguments were |
| 4232 | /// explicitly provided. |
| 4233 | /// |
| 4234 | /// \param LAngleLoc the location of the left angle bracket ('<'), if |
| 4235 | /// template arguments were explicitly provided. |
| 4236 | /// |
| 4237 | /// \param ExplicitTemplateArgs the explicitly-provided template arguments, |
| 4238 | /// if any. |
| 4239 | /// |
| 4240 | /// \param NumExplicitTemplateArgs the number of explicitly-provided template |
| 4241 | /// arguments. This number may be zero even when HasExplicitTemplateArgs is |
| 4242 | /// true as in, e.g., \c void sort<>(char*, char*); |
| 4243 | /// |
| 4244 | /// \param RAngleLoc the location of the right angle bracket ('>'), if |
| 4245 | /// template arguments were explicitly provided. |
| 4246 | /// |
| 4247 | /// \param PrevDecl the set of declarations that |
| 4248 | bool |
| 4249 | Sema::CheckFunctionTemplateSpecialization(FunctionDecl *FD, |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 4250 | const TemplateArgumentListInfo *ExplicitTemplateArgs, |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 4251 | LookupResult &Previous) { |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4252 | // The set of function template specializations that could match this |
| 4253 | // explicit function template specialization. |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4254 | UnresolvedSet<8> Candidates; |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4255 | |
| 4256 | DeclContext *FDLookupContext = FD->getDeclContext()->getLookupContext(); |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 4257 | for (LookupResult::iterator I = Previous.begin(), E = Previous.end(); |
| 4258 | I != E; ++I) { |
| 4259 | NamedDecl *Ovl = (*I)->getUnderlyingDecl(); |
| 4260 | if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Ovl)) { |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4261 | // Only consider templates found within the same semantic lookup scope as |
| 4262 | // FD. |
| 4263 | if (!FDLookupContext->Equals(Ovl->getDeclContext()->getLookupContext())) |
| 4264 | continue; |
| 4265 | |
| 4266 | // C++ [temp.expl.spec]p11: |
| 4267 | // A trailing template-argument can be left unspecified in the |
| 4268 | // template-id naming an explicit function template specialization |
| 4269 | // provided it can be deduced from the function argument type. |
| 4270 | // Perform template argument deduction to determine whether we may be |
| 4271 | // specializing this template. |
| 4272 | // FIXME: It is somewhat wasteful to build |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 4273 | TemplateDeductionInfo Info(Context, FD->getLocation()); |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4274 | FunctionDecl *Specialization = 0; |
| 4275 | if (TemplateDeductionResult TDK |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 4276 | = DeduceTemplateArguments(FunTmpl, ExplicitTemplateArgs, |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4277 | FD->getType(), |
| 4278 | Specialization, |
| 4279 | Info)) { |
| 4280 | // FIXME: Template argument deduction failed; record why it failed, so |
| 4281 | // that we can provide nifty diagnostics. |
| 4282 | (void)TDK; |
| 4283 | continue; |
| 4284 | } |
| 4285 | |
| 4286 | // Record this candidate. |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4287 | Candidates.addDecl(Specialization, I.getAccess()); |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4288 | } |
| 4289 | } |
| 4290 | |
Douglas Gregor | c5df30f | 2009-09-26 03:41:46 +0000 | [diff] [blame] | 4291 | // Find the most specialized function template. |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4292 | UnresolvedSetIterator Result |
| 4293 | = getMostSpecialized(Candidates.begin(), Candidates.end(), |
| 4294 | TPOC_Other, FD->getLocation(), |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 4295 | PDiag(diag::err_function_template_spec_no_match) |
Douglas Gregor | c5df30f | 2009-09-26 03:41:46 +0000 | [diff] [blame] | 4296 | << FD->getDeclName(), |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 4297 | PDiag(diag::err_function_template_spec_ambiguous) |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 4298 | << FD->getDeclName() << (ExplicitTemplateArgs != 0), |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 4299 | PDiag(diag::note_function_template_spec_matched)); |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4300 | if (Result == Candidates.end()) |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4301 | return true; |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4302 | |
| 4303 | // Ignore access information; it doesn't figure into redeclaration checking. |
| 4304 | FunctionDecl *Specialization = cast<FunctionDecl>(*Result); |
Douglas Gregor | c42b652 | 2010-04-09 21:02:29 +0000 | [diff] [blame] | 4305 | Specialization->setLocation(FD->getLocation()); |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4306 | |
| 4307 | // FIXME: Check if the prior specialization has a point of instantiation. |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 4308 | // If so, we have run afoul of . |
John McCall | 7ad650f | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 4309 | |
| 4310 | // If this is a friend declaration, then we're not really declaring |
| 4311 | // an explicit specialization. |
| 4312 | bool isFriend = (FD->getFriendObjectKind() != Decl::FOK_None); |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4313 | |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4314 | // Check the scope of this explicit specialization. |
John McCall | 7ad650f | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 4315 | if (!isFriend && |
| 4316 | CheckTemplateSpecializationScope(*this, |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4317 | Specialization->getPrimaryTemplate(), |
| 4318 | Specialization, FD->getLocation(), |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4319 | false)) |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4320 | return true; |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 4321 | |
| 4322 | // C++ [temp.expl.spec]p6: |
| 4323 | // 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] | 4324 | // explicitly specialized then that specialization shall be declared |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 4325 | // before the first use of that specialization that would cause an implicit |
| 4326 | // instantiation to take place, in every translation unit in which such a |
| 4327 | // use occurs; no diagnostic is required. |
| 4328 | FunctionTemplateSpecializationInfo *SpecInfo |
| 4329 | = Specialization->getTemplateSpecializationInfo(); |
| 4330 | assert(SpecInfo && "Function template specialization info missing?"); |
John McCall | 7504239 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 4331 | |
| 4332 | bool SuppressNew = false; |
John McCall | 7ad650f | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 4333 | if (!isFriend && |
| 4334 | CheckSpecializationInstantiationRedecl(FD->getLocation(), |
John McCall | 7504239 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 4335 | TSK_ExplicitSpecialization, |
| 4336 | Specialization, |
| 4337 | SpecInfo->getTemplateSpecializationKind(), |
| 4338 | SpecInfo->getPointOfInstantiation(), |
| 4339 | SuppressNew)) |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 4340 | return true; |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4341 | |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4342 | // Mark the prior declaration as an explicit specialization, so that later |
| 4343 | // clients know that this is an explicit specialization. |
John McCall | 7ad650f | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 4344 | if (!isFriend) |
| 4345 | SpecInfo->setTemplateSpecializationKind(TSK_ExplicitSpecialization); |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4346 | |
| 4347 | // Turn the given function declaration into a function template |
| 4348 | // specialization, with the template arguments from the previous |
| 4349 | // specialization. |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 4350 | FD->setFunctionTemplateSpecialization(Specialization->getPrimaryTemplate(), |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4351 | new (Context) TemplateArgumentList( |
| 4352 | *Specialization->getTemplateSpecializationArgs()), |
| 4353 | /*InsertPos=*/0, |
John McCall | 7ad650f | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 4354 | SpecInfo->getTemplateSpecializationKind()); |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4355 | |
| 4356 | // The "previous declaration" for this function template specialization is |
| 4357 | // the prior function template specialization. |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 4358 | Previous.clear(); |
| 4359 | Previous.addDecl(Specialization); |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4360 | return false; |
| 4361 | } |
| 4362 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4363 | /// \brief Perform semantic analysis for the given non-template member |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 4364 | /// specialization. |
| 4365 | /// |
| 4366 | /// This routine performs all of the semantic analysis required for an |
| 4367 | /// explicit member function specialization. On successful completion, |
| 4368 | /// the function declaration \p FD will become a member function |
| 4369 | /// specialization. |
| 4370 | /// |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4371 | /// \param Member the member declaration, which will be updated to become a |
| 4372 | /// specialization. |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 4373 | /// |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 4374 | /// \param Previous the set of declarations, one of which may be specialized |
| 4375 | /// by this function specialization; the set will be modified to contain the |
| 4376 | /// redeclared member. |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 4377 | bool |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 4378 | Sema::CheckMemberSpecialization(NamedDecl *Member, LookupResult &Previous) { |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4379 | assert(!isa<TemplateDecl>(Member) && "Only for non-template members"); |
John McCall | 77e8b11 | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 4380 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4381 | // Try to find the member we are instantiating. |
| 4382 | NamedDecl *Instantiation = 0; |
| 4383 | NamedDecl *InstantiatedFrom = 0; |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 4384 | MemberSpecializationInfo *MSInfo = 0; |
| 4385 | |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 4386 | if (Previous.empty()) { |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4387 | // Nowhere to look anyway. |
| 4388 | } else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Member)) { |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 4389 | for (LookupResult::iterator I = Previous.begin(), E = Previous.end(); |
| 4390 | I != E; ++I) { |
| 4391 | NamedDecl *D = (*I)->getUnderlyingDecl(); |
| 4392 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) { |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4393 | if (Context.hasSameType(Function->getType(), Method->getType())) { |
| 4394 | Instantiation = Method; |
| 4395 | InstantiatedFrom = Method->getInstantiatedFromMemberFunction(); |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 4396 | MSInfo = Method->getMemberSpecializationInfo(); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4397 | break; |
| 4398 | } |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 4399 | } |
| 4400 | } |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4401 | } else if (isa<VarDecl>(Member)) { |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 4402 | VarDecl *PrevVar; |
| 4403 | if (Previous.isSingleResult() && |
| 4404 | (PrevVar = dyn_cast<VarDecl>(Previous.getFoundDecl()))) |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4405 | if (PrevVar->isStaticDataMember()) { |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 4406 | Instantiation = PrevVar; |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4407 | InstantiatedFrom = PrevVar->getInstantiatedFromStaticDataMember(); |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 4408 | MSInfo = PrevVar->getMemberSpecializationInfo(); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4409 | } |
| 4410 | } else if (isa<RecordDecl>(Member)) { |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 4411 | CXXRecordDecl *PrevRecord; |
| 4412 | if (Previous.isSingleResult() && |
| 4413 | (PrevRecord = dyn_cast<CXXRecordDecl>(Previous.getFoundDecl()))) { |
| 4414 | Instantiation = PrevRecord; |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4415 | InstantiatedFrom = PrevRecord->getInstantiatedFromMemberClass(); |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 4416 | MSInfo = PrevRecord->getMemberSpecializationInfo(); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4417 | } |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 4418 | } |
| 4419 | |
| 4420 | if (!Instantiation) { |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4421 | // There is no previous declaration that matches. Since member |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 4422 | // specializations are always out-of-line, the caller will complain about |
| 4423 | // this mismatch later. |
| 4424 | return false; |
| 4425 | } |
John McCall | 77e8b11 | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 4426 | |
| 4427 | // If this is a friend, just bail out here before we start turning |
| 4428 | // things into explicit specializations. |
| 4429 | if (Member->getFriendObjectKind() != Decl::FOK_None) { |
| 4430 | // Preserve instantiation information. |
| 4431 | if (InstantiatedFrom && isa<CXXMethodDecl>(Member)) { |
| 4432 | cast<CXXMethodDecl>(Member)->setInstantiationOfMemberFunction( |
| 4433 | cast<CXXMethodDecl>(InstantiatedFrom), |
| 4434 | cast<CXXMethodDecl>(Instantiation)->getTemplateSpecializationKind()); |
| 4435 | } else if (InstantiatedFrom && isa<CXXRecordDecl>(Member)) { |
| 4436 | cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass( |
| 4437 | cast<CXXRecordDecl>(InstantiatedFrom), |
| 4438 | cast<CXXRecordDecl>(Instantiation)->getTemplateSpecializationKind()); |
| 4439 | } |
| 4440 | |
| 4441 | Previous.clear(); |
| 4442 | Previous.addDecl(Instantiation); |
| 4443 | return false; |
| 4444 | } |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 4445 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4446 | // Make sure that this is a specialization of a member. |
| 4447 | if (!InstantiatedFrom) { |
| 4448 | Diag(Member->getLocation(), diag::err_spec_member_not_instantiated) |
| 4449 | << Member; |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 4450 | Diag(Instantiation->getLocation(), diag::note_specialized_decl); |
| 4451 | return true; |
| 4452 | } |
| 4453 | |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 4454 | // C++ [temp.expl.spec]p6: |
| 4455 | // If a template, a member template or the member of a class template is |
| 4456 | // explicitly specialized then that spe- cialization shall be declared |
| 4457 | // before the first use of that specialization that would cause an implicit |
| 4458 | // instantiation to take place, in every translation unit in which such a |
| 4459 | // use occurs; no diagnostic is required. |
| 4460 | assert(MSInfo && "Member specialization info missing?"); |
John McCall | 7504239 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 4461 | |
| 4462 | bool SuppressNew = false; |
| 4463 | if (CheckSpecializationInstantiationRedecl(Member->getLocation(), |
| 4464 | TSK_ExplicitSpecialization, |
| 4465 | Instantiation, |
| 4466 | MSInfo->getTemplateSpecializationKind(), |
| 4467 | MSInfo->getPointOfInstantiation(), |
| 4468 | SuppressNew)) |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 4469 | return true; |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 4470 | |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 4471 | // Check the scope of this explicit specialization. |
| 4472 | if (CheckTemplateSpecializationScope(*this, |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4473 | InstantiatedFrom, |
| 4474 | Instantiation, Member->getLocation(), |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4475 | false)) |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 4476 | return true; |
Douglas Gregor | 2db3232 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 4477 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4478 | // Note that this is an explicit instantiation of a member. |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 4479 | // the original declaration to note that it is an explicit specialization |
| 4480 | // (if it was previously an implicit instantiation). This latter step |
| 4481 | // makes bookkeeping easier. |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4482 | if (isa<FunctionDecl>(Member)) { |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 4483 | FunctionDecl *InstantiationFunction = cast<FunctionDecl>(Instantiation); |
| 4484 | if (InstantiationFunction->getTemplateSpecializationKind() == |
| 4485 | TSK_ImplicitInstantiation) { |
| 4486 | InstantiationFunction->setTemplateSpecializationKind( |
| 4487 | TSK_ExplicitSpecialization); |
| 4488 | InstantiationFunction->setLocation(Member->getLocation()); |
| 4489 | } |
| 4490 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4491 | cast<FunctionDecl>(Member)->setInstantiationOfMemberFunction( |
| 4492 | cast<CXXMethodDecl>(InstantiatedFrom), |
| 4493 | TSK_ExplicitSpecialization); |
| 4494 | } else if (isa<VarDecl>(Member)) { |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 4495 | VarDecl *InstantiationVar = cast<VarDecl>(Instantiation); |
| 4496 | if (InstantiationVar->getTemplateSpecializationKind() == |
| 4497 | TSK_ImplicitInstantiation) { |
| 4498 | InstantiationVar->setTemplateSpecializationKind( |
| 4499 | TSK_ExplicitSpecialization); |
| 4500 | InstantiationVar->setLocation(Member->getLocation()); |
| 4501 | } |
| 4502 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4503 | Context.setInstantiatedFromStaticDataMember(cast<VarDecl>(Member), |
| 4504 | cast<VarDecl>(InstantiatedFrom), |
| 4505 | TSK_ExplicitSpecialization); |
| 4506 | } else { |
| 4507 | assert(isa<CXXRecordDecl>(Member) && "Only member classes remain"); |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 4508 | CXXRecordDecl *InstantiationClass = cast<CXXRecordDecl>(Instantiation); |
| 4509 | if (InstantiationClass->getTemplateSpecializationKind() == |
| 4510 | TSK_ImplicitInstantiation) { |
| 4511 | InstantiationClass->setTemplateSpecializationKind( |
| 4512 | TSK_ExplicitSpecialization); |
| 4513 | InstantiationClass->setLocation(Member->getLocation()); |
| 4514 | } |
| 4515 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4516 | cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass( |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 4517 | cast<CXXRecordDecl>(InstantiatedFrom), |
| 4518 | TSK_ExplicitSpecialization); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4519 | } |
| 4520 | |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 4521 | // Save the caller the trouble of having to figure out which declaration |
| 4522 | // this specialization matches. |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 4523 | Previous.clear(); |
| 4524 | Previous.addDecl(Instantiation); |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 4525 | return false; |
| 4526 | } |
| 4527 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 4528 | /// \brief Check the scope of an explicit instantiation. |
| 4529 | static void CheckExplicitInstantiationScope(Sema &S, NamedDecl *D, |
| 4530 | SourceLocation InstLoc, |
| 4531 | bool WasQualifiedName) { |
| 4532 | DeclContext *ExpectedContext |
| 4533 | = D->getDeclContext()->getEnclosingNamespaceContext()->getLookupContext(); |
| 4534 | DeclContext *CurContext = S.CurContext->getLookupContext(); |
| 4535 | |
| 4536 | // C++0x [temp.explicit]p2: |
| 4537 | // An explicit instantiation shall appear in an enclosing namespace of its |
| 4538 | // template. |
| 4539 | // |
| 4540 | // This is DR275, which we do not retroactively apply to C++98/03. |
| 4541 | if (S.getLangOptions().CPlusPlus0x && |
| 4542 | !CurContext->Encloses(ExpectedContext)) { |
| 4543 | if (NamespaceDecl *NS = dyn_cast<NamespaceDecl>(ExpectedContext)) |
| 4544 | S.Diag(InstLoc, diag::err_explicit_instantiation_out_of_scope) |
| 4545 | << D << NS; |
| 4546 | else |
| 4547 | S.Diag(InstLoc, diag::err_explicit_instantiation_must_be_global) |
| 4548 | << D; |
| 4549 | S.Diag(D->getLocation(), diag::note_explicit_instantiation_here); |
| 4550 | return; |
| 4551 | } |
| 4552 | |
| 4553 | // C++0x [temp.explicit]p2: |
| 4554 | // If the name declared in the explicit instantiation is an unqualified |
| 4555 | // name, the explicit instantiation shall appear in the namespace where |
| 4556 | // its template is declared or, if that namespace is inline (7.3.1), any |
| 4557 | // namespace from its enclosing namespace set. |
| 4558 | if (WasQualifiedName) |
| 4559 | return; |
| 4560 | |
| 4561 | if (CurContext->Equals(ExpectedContext)) |
| 4562 | return; |
| 4563 | |
| 4564 | S.Diag(InstLoc, diag::err_explicit_instantiation_unqualified_wrong_namespace) |
| 4565 | << D << ExpectedContext; |
| 4566 | S.Diag(D->getLocation(), diag::note_explicit_instantiation_here); |
| 4567 | } |
| 4568 | |
| 4569 | /// \brief Determine whether the given scope specifier has a template-id in it. |
| 4570 | static bool ScopeSpecifierHasTemplateId(const CXXScopeSpec &SS) { |
| 4571 | if (!SS.isSet()) |
| 4572 | return false; |
| 4573 | |
| 4574 | // C++0x [temp.explicit]p2: |
| 4575 | // If the explicit instantiation is for a member function, a member class |
| 4576 | // or a static data member of a class template specialization, the name of |
| 4577 | // the class template specialization in the qualified-id for the member |
| 4578 | // name shall be a simple-template-id. |
| 4579 | // |
| 4580 | // C++98 has the same restriction, just worded differently. |
| 4581 | for (NestedNameSpecifier *NNS = (NestedNameSpecifier *)SS.getScopeRep(); |
| 4582 | NNS; NNS = NNS->getPrefix()) |
| 4583 | if (Type *T = NNS->getAsType()) |
| 4584 | if (isa<TemplateSpecializationType>(T)) |
| 4585 | return true; |
| 4586 | |
| 4587 | return false; |
| 4588 | } |
| 4589 | |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 4590 | // Explicit instantiation of a class template specialization |
Douglas Gregor | 45f9655 | 2009-09-04 06:33:52 +0000 | [diff] [blame] | 4591 | // FIXME: Implement extern template semantics |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4592 | Sema::DeclResult |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4593 | Sema::ActOnExplicitInstantiation(Scope *S, |
Douglas Gregor | 45f9655 | 2009-09-04 06:33:52 +0000 | [diff] [blame] | 4594 | SourceLocation ExternLoc, |
| 4595 | SourceLocation TemplateLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4596 | unsigned TagSpec, |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4597 | SourceLocation KWLoc, |
| 4598 | const CXXScopeSpec &SS, |
| 4599 | TemplateTy TemplateD, |
| 4600 | SourceLocation TemplateNameLoc, |
| 4601 | SourceLocation LAngleLoc, |
| 4602 | ASTTemplateArgsPtr TemplateArgsIn, |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4603 | SourceLocation RAngleLoc, |
| 4604 | AttributeList *Attr) { |
| 4605 | // Find the class template we're specializing |
| 4606 | TemplateName Name = TemplateD.getAsVal<TemplateName>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4607 | ClassTemplateDecl *ClassTemplate |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4608 | = cast<ClassTemplateDecl>(Name.getAsTemplateDecl()); |
| 4609 | |
| 4610 | // Check that the specialization uses the same tag kind as the |
| 4611 | // original template. |
| 4612 | TagDecl::TagKind Kind; |
| 4613 | switch (TagSpec) { |
| 4614 | default: assert(0 && "Unknown tag type!"); |
| 4615 | case DeclSpec::TST_struct: Kind = TagDecl::TK_struct; break; |
| 4616 | case DeclSpec::TST_union: Kind = TagDecl::TK_union; break; |
| 4617 | case DeclSpec::TST_class: Kind = TagDecl::TK_class; break; |
| 4618 | } |
Douglas Gregor | 501c5ce | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 4619 | if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4620 | Kind, KWLoc, |
Douglas Gregor | 501c5ce | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 4621 | *ClassTemplate->getIdentifier())) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4622 | Diag(KWLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4623 | << ClassTemplate |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 4624 | << FixItHint::CreateReplacement(KWLoc, |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4625 | ClassTemplate->getTemplatedDecl()->getKindName()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4626 | Diag(ClassTemplate->getTemplatedDecl()->getLocation(), |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4627 | diag::note_previous_use); |
| 4628 | Kind = ClassTemplate->getTemplatedDecl()->getTagKind(); |
| 4629 | } |
| 4630 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 4631 | // C++0x [temp.explicit]p2: |
| 4632 | // There are two forms of explicit instantiation: an explicit instantiation |
| 4633 | // definition and an explicit instantiation declaration. An explicit |
| 4634 | // instantiation declaration begins with the extern keyword. [...] |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4635 | TemplateSpecializationKind TSK |
| 4636 | = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition |
| 4637 | : TSK_ExplicitInstantiationDeclaration; |
| 4638 | |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4639 | // Translate the parser's template argument list in our AST format. |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 4640 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
Douglas Gregor | 314b97f | 2009-11-10 19:49:08 +0000 | [diff] [blame] | 4641 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4642 | |
| 4643 | // Check that the template argument list is well-formed for this |
| 4644 | // template. |
Anders Carlsson | fb25052 | 2009-06-23 01:26:57 +0000 | [diff] [blame] | 4645 | TemplateArgumentListBuilder Converted(ClassTemplate->getTemplateParameters(), |
| 4646 | TemplateArgs.size()); |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 4647 | if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc, |
| 4648 | TemplateArgs, false, Converted)) |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4649 | return true; |
| 4650 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4651 | assert((Converted.structuredSize() == |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4652 | ClassTemplate->getTemplateParameters()->size()) && |
| 4653 | "Converted template argument list is too short!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4654 | |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4655 | // Find the class template specialization declaration that |
| 4656 | // corresponds to these arguments. |
| 4657 | llvm::FoldingSetNodeID ID; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4658 | ClassTemplateSpecializationDecl::Profile(ID, |
Anders Carlsson | fb25052 | 2009-06-23 01:26:57 +0000 | [diff] [blame] | 4659 | Converted.getFlatArguments(), |
Douglas Gregor | 828e226 | 2009-07-29 16:09:57 +0000 | [diff] [blame] | 4660 | Converted.flatSize(), |
| 4661 | Context); |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4662 | void *InsertPos = 0; |
| 4663 | ClassTemplateSpecializationDecl *PrevDecl |
| 4664 | = ClassTemplate->getSpecializations().FindNodeOrInsertPos(ID, InsertPos); |
| 4665 | |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4666 | // C++0x [temp.explicit]p2: |
| 4667 | // [...] An explicit instantiation shall appear in an enclosing |
| 4668 | // namespace of its template. [...] |
| 4669 | // |
| 4670 | // This is C++ DR 275. |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 4671 | CheckExplicitInstantiationScope(*this, ClassTemplate, TemplateNameLoc, |
| 4672 | SS.isSet()); |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4673 | |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4674 | ClassTemplateSpecializationDecl *Specialization = 0; |
| 4675 | |
Douglas Gregor | d78f598 | 2009-11-25 06:01:46 +0000 | [diff] [blame] | 4676 | bool ReusedDecl = false; |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4677 | if (PrevDecl) { |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 4678 | bool SuppressNew = false; |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 4679 | if (CheckSpecializationInstantiationRedecl(TemplateNameLoc, TSK, |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 4680 | PrevDecl, |
| 4681 | PrevDecl->getSpecializationKind(), |
| 4682 | PrevDecl->getPointOfInstantiation(), |
| 4683 | SuppressNew)) |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4684 | return DeclPtrTy::make(PrevDecl); |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4685 | |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 4686 | if (SuppressNew) |
Douglas Gregor | 52604ab | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 4687 | return DeclPtrTy::make(PrevDecl); |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 4688 | |
Douglas Gregor | 52604ab | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 4689 | if (PrevDecl->getSpecializationKind() == TSK_ImplicitInstantiation || |
| 4690 | PrevDecl->getSpecializationKind() == TSK_Undeclared) { |
| 4691 | // Since the only prior class template specialization with these |
| 4692 | // arguments was referenced but not declared, reuse that |
| 4693 | // declaration node as our own, updating its source location to |
| 4694 | // reflect our new declaration. |
| 4695 | Specialization = PrevDecl; |
| 4696 | Specialization->setLocation(TemplateNameLoc); |
| 4697 | PrevDecl = 0; |
Douglas Gregor | d78f598 | 2009-11-25 06:01:46 +0000 | [diff] [blame] | 4698 | ReusedDecl = true; |
Douglas Gregor | 52604ab | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 4699 | } |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 4700 | } |
Douglas Gregor | 52604ab | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 4701 | |
| 4702 | if (!Specialization) { |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4703 | // Create a new class template specialization declaration node for |
| 4704 | // this explicit specialization. |
| 4705 | Specialization |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4706 | = ClassTemplateSpecializationDecl::Create(Context, |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4707 | ClassTemplate->getDeclContext(), |
| 4708 | TemplateNameLoc, |
| 4709 | ClassTemplate, |
Douglas Gregor | 52604ab | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 4710 | Converted, PrevDecl); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 4711 | SetNestedNameSpecifier(Specialization, SS); |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4712 | |
Douglas Gregor | 52604ab | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 4713 | if (PrevDecl) { |
| 4714 | // Remove the previous declaration from the folding set, since we want |
| 4715 | // to introduce a new declaration. |
| 4716 | ClassTemplate->getSpecializations().RemoveNode(PrevDecl); |
| 4717 | ClassTemplate->getSpecializations().FindNodeOrInsertPos(ID, InsertPos); |
| 4718 | } |
| 4719 | |
| 4720 | // Insert the new specialization. |
| 4721 | ClassTemplate->getSpecializations().InsertNode(Specialization, InsertPos); |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4722 | } |
| 4723 | |
| 4724 | // Build the fully-sugared type for this explicit instantiation as |
| 4725 | // the user wrote in the explicit instantiation itself. This means |
| 4726 | // that we'll pretty-print the type retrieved from the |
| 4727 | // specialization's declaration the way that the user actually wrote |
| 4728 | // the explicit instantiation, rather than formatting the name based |
| 4729 | // on the "canonical" representation used to store the template |
| 4730 | // arguments in the specialization. |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 4731 | TypeSourceInfo *WrittenTy |
| 4732 | = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc, |
| 4733 | TemplateArgs, |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4734 | Context.getTypeDeclType(Specialization)); |
| 4735 | Specialization->setTypeAsWritten(WrittenTy); |
| 4736 | TemplateArgsIn.release(); |
| 4737 | |
Douglas Gregor | d78f598 | 2009-11-25 06:01:46 +0000 | [diff] [blame] | 4738 | if (!ReusedDecl) { |
| 4739 | // Add the explicit instantiation into its lexical context. However, |
| 4740 | // since explicit instantiations are never found by name lookup, we |
| 4741 | // just put it into the declaration context directly. |
| 4742 | Specialization->setLexicalDeclContext(CurContext); |
| 4743 | CurContext->addDecl(Specialization); |
| 4744 | } |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4745 | |
| 4746 | // C++ [temp.explicit]p3: |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4747 | // A definition of a class template or class member template |
| 4748 | // shall be in scope at the point of the explicit instantiation of |
| 4749 | // the class template or class member template. |
| 4750 | // |
| 4751 | // This check comes when we actually try to perform the |
| 4752 | // instantiation. |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 4753 | ClassTemplateSpecializationDecl *Def |
| 4754 | = cast_or_null<ClassTemplateSpecializationDecl>( |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 4755 | Specialization->getDefinition()); |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 4756 | if (!Def) |
Douglas Gregor | 972e6ce | 2009-10-27 06:26:26 +0000 | [diff] [blame] | 4757 | InstantiateClassTemplateSpecialization(TemplateNameLoc, Specialization, TSK); |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 4758 | |
| 4759 | // Instantiate the members of this class template specialization. |
| 4760 | Def = cast_or_null<ClassTemplateSpecializationDecl>( |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 4761 | Specialization->getDefinition()); |
Rafael Espindola | b0f65ca | 2010-03-22 23:12:48 +0000 | [diff] [blame] | 4762 | if (Def) { |
Rafael Espindola | f075b22 | 2010-03-23 19:55:22 +0000 | [diff] [blame] | 4763 | TemplateSpecializationKind Old_TSK = Def->getTemplateSpecializationKind(); |
| 4764 | |
| 4765 | // Fix a TSK_ExplicitInstantiationDeclaration followed by a |
| 4766 | // TSK_ExplicitInstantiationDefinition |
| 4767 | if (Old_TSK == TSK_ExplicitInstantiationDeclaration && |
| 4768 | TSK == TSK_ExplicitInstantiationDefinition) |
| 4769 | Def->setTemplateSpecializationKind(TSK); |
Rafael Espindola | b0f65ca | 2010-03-22 23:12:48 +0000 | [diff] [blame] | 4770 | |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 4771 | InstantiateClassTemplateSpecializationMembers(TemplateNameLoc, Def, TSK); |
Rafael Espindola | b0f65ca | 2010-03-22 23:12:48 +0000 | [diff] [blame] | 4772 | } |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4773 | |
| 4774 | return DeclPtrTy::make(Specialization); |
| 4775 | } |
| 4776 | |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 4777 | // Explicit instantiation of a member class of a class template. |
| 4778 | Sema::DeclResult |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4779 | Sema::ActOnExplicitInstantiation(Scope *S, |
Douglas Gregor | 45f9655 | 2009-09-04 06:33:52 +0000 | [diff] [blame] | 4780 | SourceLocation ExternLoc, |
| 4781 | SourceLocation TemplateLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4782 | unsigned TagSpec, |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 4783 | SourceLocation KWLoc, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 4784 | CXXScopeSpec &SS, |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 4785 | IdentifierInfo *Name, |
| 4786 | SourceLocation NameLoc, |
| 4787 | AttributeList *Attr) { |
| 4788 | |
Douglas Gregor | 402abb5 | 2009-05-28 23:31:59 +0000 | [diff] [blame] | 4789 | bool Owned = false; |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 4790 | bool IsDependent = false; |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 4791 | DeclPtrTy TagD = ActOnTag(S, TagSpec, Action::TUK_Reference, |
Douglas Gregor | 7cdbc58 | 2009-07-22 23:48:44 +0000 | [diff] [blame] | 4792 | KWLoc, SS, Name, NameLoc, Attr, AS_none, |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 4793 | MultiTemplateParamsArg(*this, 0, 0), |
| 4794 | Owned, IsDependent); |
| 4795 | assert(!IsDependent && "explicit instantiation of dependent name not yet handled"); |
| 4796 | |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 4797 | if (!TagD) |
| 4798 | return true; |
| 4799 | |
| 4800 | TagDecl *Tag = cast<TagDecl>(TagD.getAs<Decl>()); |
| 4801 | if (Tag->isEnum()) { |
| 4802 | Diag(TemplateLoc, diag::err_explicit_instantiation_enum) |
| 4803 | << Context.getTypeDeclType(Tag); |
| 4804 | return true; |
| 4805 | } |
| 4806 | |
Douglas Gregor | d0c8737 | 2009-05-27 17:30:49 +0000 | [diff] [blame] | 4807 | if (Tag->isInvalidDecl()) |
| 4808 | return true; |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 4809 | |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 4810 | CXXRecordDecl *Record = cast<CXXRecordDecl>(Tag); |
| 4811 | CXXRecordDecl *Pattern = Record->getInstantiatedFromMemberClass(); |
| 4812 | if (!Pattern) { |
| 4813 | Diag(TemplateLoc, diag::err_explicit_instantiation_nontemplate_type) |
| 4814 | << Context.getTypeDeclType(Record); |
| 4815 | Diag(Record->getLocation(), diag::note_nontemplate_decl_here); |
| 4816 | return true; |
| 4817 | } |
| 4818 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 4819 | // C++0x [temp.explicit]p2: |
| 4820 | // If the explicit instantiation is for a class or member class, the |
| 4821 | // elaborated-type-specifier in the declaration shall include a |
| 4822 | // simple-template-id. |
| 4823 | // |
| 4824 | // C++98 has the same restriction, just worded differently. |
| 4825 | if (!ScopeSpecifierHasTemplateId(SS)) |
| 4826 | Diag(TemplateLoc, diag::err_explicit_instantiation_without_qualified_id) |
| 4827 | << Record << SS.getRange(); |
| 4828 | |
| 4829 | // C++0x [temp.explicit]p2: |
| 4830 | // There are two forms of explicit instantiation: an explicit instantiation |
| 4831 | // definition and an explicit instantiation declaration. An explicit |
| 4832 | // instantiation declaration begins with the extern keyword. [...] |
Douglas Gregor | a74bbe2 | 2009-10-14 21:46:58 +0000 | [diff] [blame] | 4833 | TemplateSpecializationKind TSK |
| 4834 | = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition |
| 4835 | : TSK_ExplicitInstantiationDeclaration; |
| 4836 | |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 4837 | // C++0x [temp.explicit]p2: |
| 4838 | // [...] An explicit instantiation shall appear in an enclosing |
| 4839 | // namespace of its template. [...] |
| 4840 | // |
| 4841 | // This is C++ DR 275. |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 4842 | CheckExplicitInstantiationScope(*this, Record, NameLoc, true); |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4843 | |
| 4844 | // Verify that it is okay to explicitly instantiate here. |
Douglas Gregor | 583f33b | 2009-10-15 18:07:02 +0000 | [diff] [blame] | 4845 | CXXRecordDecl *PrevDecl |
| 4846 | = cast_or_null<CXXRecordDecl>(Record->getPreviousDeclaration()); |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 4847 | if (!PrevDecl && Record->getDefinition()) |
Douglas Gregor | 583f33b | 2009-10-15 18:07:02 +0000 | [diff] [blame] | 4848 | PrevDecl = Record; |
| 4849 | if (PrevDecl) { |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4850 | MemberSpecializationInfo *MSInfo = PrevDecl->getMemberSpecializationInfo(); |
| 4851 | bool SuppressNew = false; |
| 4852 | assert(MSInfo && "No member specialization information?"); |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 4853 | if (CheckSpecializationInstantiationRedecl(TemplateLoc, TSK, |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4854 | PrevDecl, |
| 4855 | MSInfo->getTemplateSpecializationKind(), |
| 4856 | MSInfo->getPointOfInstantiation(), |
| 4857 | SuppressNew)) |
| 4858 | return true; |
| 4859 | if (SuppressNew) |
| 4860 | return TagD; |
| 4861 | } |
| 4862 | |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 4863 | CXXRecordDecl *RecordDef |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 4864 | = cast_or_null<CXXRecordDecl>(Record->getDefinition()); |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 4865 | if (!RecordDef) { |
Douglas Gregor | bf7643e | 2009-10-15 12:53:22 +0000 | [diff] [blame] | 4866 | // C++ [temp.explicit]p3: |
| 4867 | // A definition of a member class of a class template shall be in scope |
| 4868 | // at the point of an explicit instantiation of the member class. |
| 4869 | CXXRecordDecl *Def |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 4870 | = cast_or_null<CXXRecordDecl>(Pattern->getDefinition()); |
Douglas Gregor | bf7643e | 2009-10-15 12:53:22 +0000 | [diff] [blame] | 4871 | if (!Def) { |
Douglas Gregor | e2d3a3d | 2009-10-15 14:05:49 +0000 | [diff] [blame] | 4872 | Diag(TemplateLoc, diag::err_explicit_instantiation_undefined_member) |
| 4873 | << 0 << Record->getDeclName() << Record->getDeclContext(); |
Douglas Gregor | bf7643e | 2009-10-15 12:53:22 +0000 | [diff] [blame] | 4874 | Diag(Pattern->getLocation(), diag::note_forward_declaration) |
| 4875 | << Pattern; |
| 4876 | return true; |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 4877 | } else { |
| 4878 | if (InstantiateClass(NameLoc, Record, Def, |
| 4879 | getTemplateInstantiationArgs(Record), |
| 4880 | TSK)) |
| 4881 | return true; |
| 4882 | |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 4883 | RecordDef = cast_or_null<CXXRecordDecl>(Record->getDefinition()); |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 4884 | if (!RecordDef) |
| 4885 | return true; |
| 4886 | } |
| 4887 | } |
| 4888 | |
| 4889 | // Instantiate all of the members of the class. |
| 4890 | InstantiateClassMembers(NameLoc, RecordDef, |
| 4891 | getTemplateInstantiationArgs(Record), TSK); |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 4892 | |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 4893 | // FIXME: We don't have any representation for explicit instantiations of |
| 4894 | // member classes. Such a representation is not needed for compilation, but it |
| 4895 | // should be available for clients that want to see all of the declarations in |
| 4896 | // the source code. |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 4897 | return TagD; |
| 4898 | } |
| 4899 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4900 | Sema::DeclResult Sema::ActOnExplicitInstantiation(Scope *S, |
| 4901 | SourceLocation ExternLoc, |
| 4902 | SourceLocation TemplateLoc, |
| 4903 | Declarator &D) { |
| 4904 | // Explicit instantiations always require a name. |
| 4905 | DeclarationName Name = GetNameForDeclarator(D); |
| 4906 | if (!Name) { |
| 4907 | if (!D.isInvalidType()) |
| 4908 | Diag(D.getDeclSpec().getSourceRange().getBegin(), |
| 4909 | diag::err_explicit_instantiation_requires_name) |
| 4910 | << D.getDeclSpec().getSourceRange() |
| 4911 | << D.getSourceRange(); |
| 4912 | |
| 4913 | return true; |
| 4914 | } |
| 4915 | |
| 4916 | // The scope passed in may not be a decl scope. Zip up the scope tree until |
| 4917 | // we find one that is. |
| 4918 | while ((S->getFlags() & Scope::DeclScope) == 0 || |
| 4919 | (S->getFlags() & Scope::TemplateParamScope) != 0) |
| 4920 | S = S->getParent(); |
| 4921 | |
| 4922 | // Determine the type of the declaration. |
| 4923 | QualType R = GetTypeForDeclarator(D, S, 0); |
| 4924 | if (R.isNull()) |
| 4925 | return true; |
| 4926 | |
| 4927 | if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef) { |
| 4928 | // Cannot explicitly instantiate a typedef. |
| 4929 | Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_of_typedef) |
| 4930 | << Name; |
| 4931 | return true; |
| 4932 | } |
| 4933 | |
Douglas Gregor | 663b5a0 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 4934 | // C++0x [temp.explicit]p1: |
| 4935 | // [...] An explicit instantiation of a function template shall not use the |
| 4936 | // inline or constexpr specifiers. |
| 4937 | // Presumably, this also applies to member functions of class templates as |
| 4938 | // well. |
| 4939 | if (D.getDeclSpec().isInlineSpecified() && getLangOptions().CPlusPlus0x) |
| 4940 | Diag(D.getDeclSpec().getInlineSpecLoc(), |
| 4941 | diag::err_explicit_instantiation_inline) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 4942 | <<FixItHint::CreateRemoval(D.getDeclSpec().getInlineSpecLoc()); |
Douglas Gregor | 663b5a0 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 4943 | |
| 4944 | // FIXME: check for constexpr specifier. |
| 4945 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 4946 | // C++0x [temp.explicit]p2: |
| 4947 | // There are two forms of explicit instantiation: an explicit instantiation |
| 4948 | // definition and an explicit instantiation declaration. An explicit |
| 4949 | // instantiation declaration begins with the extern keyword. [...] |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4950 | TemplateSpecializationKind TSK |
| 4951 | = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition |
| 4952 | : TSK_ExplicitInstantiationDeclaration; |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 4953 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 4954 | LookupResult Previous(*this, Name, D.getIdentifierLoc(), LookupOrdinaryName); |
| 4955 | LookupParsedName(Previous, S, &D.getCXXScopeSpec()); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4956 | |
| 4957 | if (!R->isFunctionType()) { |
| 4958 | // C++ [temp.explicit]p1: |
| 4959 | // A [...] static data member of a class template can be explicitly |
| 4960 | // instantiated from the member definition associated with its class |
| 4961 | // template. |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 4962 | if (Previous.isAmbiguous()) |
| 4963 | return true; |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4964 | |
John McCall | 1bcee0a | 2009-12-02 08:25:40 +0000 | [diff] [blame] | 4965 | VarDecl *Prev = Previous.getAsSingle<VarDecl>(); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4966 | if (!Prev || !Prev->isStaticDataMember()) { |
| 4967 | // We expect to see a data data member here. |
| 4968 | Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_not_known) |
| 4969 | << Name; |
| 4970 | for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end(); |
| 4971 | P != PEnd; ++P) |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 4972 | Diag((*P)->getLocation(), diag::note_explicit_instantiation_here); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4973 | return true; |
| 4974 | } |
| 4975 | |
| 4976 | if (!Prev->getInstantiatedFromStaticDataMember()) { |
| 4977 | // FIXME: Check for explicit specialization? |
| 4978 | Diag(D.getIdentifierLoc(), |
| 4979 | diag::err_explicit_instantiation_data_member_not_instantiated) |
| 4980 | << Prev; |
| 4981 | Diag(Prev->getLocation(), diag::note_explicit_instantiation_here); |
| 4982 | // FIXME: Can we provide a note showing where this was declared? |
| 4983 | return true; |
| 4984 | } |
| 4985 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 4986 | // C++0x [temp.explicit]p2: |
| 4987 | // If the explicit instantiation is for a member function, a member class |
| 4988 | // or a static data member of a class template specialization, the name of |
| 4989 | // the class template specialization in the qualified-id for the member |
| 4990 | // name shall be a simple-template-id. |
| 4991 | // |
| 4992 | // C++98 has the same restriction, just worded differently. |
| 4993 | if (!ScopeSpecifierHasTemplateId(D.getCXXScopeSpec())) |
| 4994 | Diag(D.getIdentifierLoc(), |
| 4995 | diag::err_explicit_instantiation_without_qualified_id) |
| 4996 | << Prev << D.getCXXScopeSpec().getRange(); |
| 4997 | |
| 4998 | // Check the scope of this explicit instantiation. |
| 4999 | CheckExplicitInstantiationScope(*this, Prev, D.getIdentifierLoc(), true); |
| 5000 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5001 | // Verify that it is okay to explicitly instantiate here. |
| 5002 | MemberSpecializationInfo *MSInfo = Prev->getMemberSpecializationInfo(); |
| 5003 | assert(MSInfo && "Missing static data member specialization info?"); |
| 5004 | bool SuppressNew = false; |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 5005 | if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK, Prev, |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5006 | MSInfo->getTemplateSpecializationKind(), |
| 5007 | MSInfo->getPointOfInstantiation(), |
| 5008 | SuppressNew)) |
| 5009 | return true; |
| 5010 | if (SuppressNew) |
| 5011 | return DeclPtrTy(); |
| 5012 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5013 | // Instantiate static data member. |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 5014 | Prev->setTemplateSpecializationKind(TSK, D.getIdentifierLoc()); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5015 | if (TSK == TSK_ExplicitInstantiationDefinition) |
Douglas Gregor | e2d3a3d | 2009-10-15 14:05:49 +0000 | [diff] [blame] | 5016 | InstantiateStaticDataMemberDefinition(D.getIdentifierLoc(), Prev, false, |
| 5017 | /*DefinitionRequired=*/true); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5018 | |
| 5019 | // FIXME: Create an ExplicitInstantiation node? |
| 5020 | return DeclPtrTy(); |
| 5021 | } |
| 5022 | |
Douglas Gregor | 0b60d9e | 2009-09-25 23:53:26 +0000 | [diff] [blame] | 5023 | // If the declarator is a template-id, translate the parser's template |
| 5024 | // argument list into our AST format. |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 5025 | bool HasExplicitTemplateArgs = false; |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 5026 | TemplateArgumentListInfo TemplateArgs; |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 5027 | if (D.getName().getKind() == UnqualifiedId::IK_TemplateId) { |
| 5028 | TemplateIdAnnotation *TemplateId = D.getName().TemplateId; |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 5029 | TemplateArgs.setLAngleLoc(TemplateId->LAngleLoc); |
| 5030 | TemplateArgs.setRAngleLoc(TemplateId->RAngleLoc); |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 5031 | ASTTemplateArgsPtr TemplateArgsPtr(*this, |
| 5032 | TemplateId->getTemplateArgs(), |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 5033 | TemplateId->NumArgs); |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 5034 | translateTemplateArguments(TemplateArgsPtr, TemplateArgs); |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 5035 | HasExplicitTemplateArgs = true; |
Douglas Gregor | b2f81cf | 2009-10-01 23:51:25 +0000 | [diff] [blame] | 5036 | TemplateArgsPtr.release(); |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 5037 | } |
Douglas Gregor | 0b60d9e | 2009-09-25 23:53:26 +0000 | [diff] [blame] | 5038 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5039 | // C++ [temp.explicit]p1: |
| 5040 | // A [...] function [...] can be explicitly instantiated from its template. |
| 5041 | // A member function [...] of a class template can be explicitly |
| 5042 | // instantiated from the member definition associated with its class |
| 5043 | // template. |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5044 | UnresolvedSet<8> Matches; |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5045 | for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end(); |
| 5046 | P != PEnd; ++P) { |
| 5047 | NamedDecl *Prev = *P; |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 5048 | if (!HasExplicitTemplateArgs) { |
| 5049 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Prev)) { |
| 5050 | if (Context.hasSameUnqualifiedType(Method->getType(), R)) { |
| 5051 | Matches.clear(); |
Douglas Gregor | 48026d2 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 5052 | |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5053 | Matches.addDecl(Method, P.getAccess()); |
Douglas Gregor | 48026d2 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 5054 | if (Method->getTemplateSpecializationKind() == TSK_Undeclared) |
| 5055 | break; |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 5056 | } |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5057 | } |
| 5058 | } |
| 5059 | |
| 5060 | FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Prev); |
| 5061 | if (!FunTmpl) |
| 5062 | continue; |
| 5063 | |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 5064 | TemplateDeductionInfo Info(Context, D.getIdentifierLoc()); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5065 | FunctionDecl *Specialization = 0; |
| 5066 | if (TemplateDeductionResult TDK |
Douglas Gregor | 48026d2 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 5067 | = DeduceTemplateArguments(FunTmpl, |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 5068 | (HasExplicitTemplateArgs ? &TemplateArgs : 0), |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5069 | R, Specialization, Info)) { |
| 5070 | // FIXME: Keep track of almost-matches? |
| 5071 | (void)TDK; |
| 5072 | continue; |
| 5073 | } |
| 5074 | |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5075 | Matches.addDecl(Specialization, P.getAccess()); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5076 | } |
| 5077 | |
| 5078 | // Find the most specialized function template specialization. |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5079 | UnresolvedSetIterator Result |
| 5080 | = getMostSpecialized(Matches.begin(), Matches.end(), TPOC_Other, |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5081 | D.getIdentifierLoc(), |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 5082 | PDiag(diag::err_explicit_instantiation_not_known) << Name, |
| 5083 | PDiag(diag::err_explicit_instantiation_ambiguous) << Name, |
| 5084 | PDiag(diag::note_explicit_instantiation_candidate)); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5085 | |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5086 | if (Result == Matches.end()) |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5087 | return true; |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5088 | |
| 5089 | // Ignore access control bits, we don't need them for redeclaration checking. |
| 5090 | FunctionDecl *Specialization = cast<FunctionDecl>(*Result); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5091 | |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 5092 | if (Specialization->getTemplateSpecializationKind() == TSK_Undeclared) { |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5093 | Diag(D.getIdentifierLoc(), |
| 5094 | diag::err_explicit_instantiation_member_function_not_instantiated) |
| 5095 | << Specialization |
| 5096 | << (Specialization->getTemplateSpecializationKind() == |
| 5097 | TSK_ExplicitSpecialization); |
| 5098 | Diag(Specialization->getLocation(), diag::note_explicit_instantiation_here); |
| 5099 | return true; |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 5100 | } |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5101 | |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 5102 | FunctionDecl *PrevDecl = Specialization->getPreviousDeclaration(); |
Douglas Gregor | 583f33b | 2009-10-15 18:07:02 +0000 | [diff] [blame] | 5103 | if (!PrevDecl && Specialization->isThisDeclarationADefinition()) |
| 5104 | PrevDecl = Specialization; |
| 5105 | |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 5106 | if (PrevDecl) { |
| 5107 | bool SuppressNew = false; |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 5108 | if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK, |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 5109 | PrevDecl, |
| 5110 | PrevDecl->getTemplateSpecializationKind(), |
| 5111 | PrevDecl->getPointOfInstantiation(), |
| 5112 | SuppressNew)) |
| 5113 | return true; |
| 5114 | |
| 5115 | // FIXME: We may still want to build some representation of this |
| 5116 | // explicit specialization. |
| 5117 | if (SuppressNew) |
| 5118 | return DeclPtrTy(); |
| 5119 | } |
Anders Carlsson | 26d6e9d | 2009-11-24 05:34:41 +0000 | [diff] [blame] | 5120 | |
| 5121 | Specialization->setTemplateSpecializationKind(TSK, D.getIdentifierLoc()); |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 5122 | |
| 5123 | if (TSK == TSK_ExplicitInstantiationDefinition) |
| 5124 | InstantiateFunctionDefinition(D.getIdentifierLoc(), Specialization, |
| 5125 | false, /*DefinitionRequired=*/true); |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 5126 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5127 | // C++0x [temp.explicit]p2: |
| 5128 | // If the explicit instantiation is for a member function, a member class |
| 5129 | // or a static data member of a class template specialization, the name of |
| 5130 | // the class template specialization in the qualified-id for the member |
| 5131 | // name shall be a simple-template-id. |
| 5132 | // |
| 5133 | // C++98 has the same restriction, just worded differently. |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 5134 | FunctionTemplateDecl *FunTmpl = Specialization->getPrimaryTemplate(); |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 5135 | if (D.getName().getKind() != UnqualifiedId::IK_TemplateId && !FunTmpl && |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5136 | D.getCXXScopeSpec().isSet() && |
| 5137 | !ScopeSpecifierHasTemplateId(D.getCXXScopeSpec())) |
| 5138 | Diag(D.getIdentifierLoc(), |
| 5139 | diag::err_explicit_instantiation_without_qualified_id) |
| 5140 | << Specialization << D.getCXXScopeSpec().getRange(); |
| 5141 | |
| 5142 | CheckExplicitInstantiationScope(*this, |
| 5143 | FunTmpl? (NamedDecl *)FunTmpl |
| 5144 | : Specialization->getInstantiatedFromMemberFunction(), |
| 5145 | D.getIdentifierLoc(), |
| 5146 | D.getCXXScopeSpec().isSet()); |
| 5147 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5148 | // FIXME: Create some kind of ExplicitInstantiationDecl here. |
| 5149 | return DeclPtrTy(); |
| 5150 | } |
| 5151 | |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5152 | Sema::TypeResult |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 5153 | Sema::ActOnDependentTag(Scope *S, unsigned TagSpec, TagUseKind TUK, |
| 5154 | const CXXScopeSpec &SS, IdentifierInfo *Name, |
| 5155 | SourceLocation TagLoc, SourceLocation NameLoc) { |
| 5156 | // This has to hold, because SS is expected to be defined. |
| 5157 | assert(Name && "Expected a name in a dependent tag"); |
| 5158 | |
| 5159 | NestedNameSpecifier *NNS |
| 5160 | = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); |
| 5161 | if (!NNS) |
| 5162 | return true; |
| 5163 | |
Daniel Dunbar | 12c0ade | 2010-04-01 16:50:48 +0000 | [diff] [blame] | 5164 | ElaboratedTypeKeyword Keyword = ETK_None; |
Douglas Gregor | 4033642 | 2010-03-31 22:19:08 +0000 | [diff] [blame] | 5165 | switch (TagDecl::getTagKindForTypeSpec(TagSpec)) { |
| 5166 | case TagDecl::TK_struct: Keyword = ETK_Struct; break; |
| 5167 | case TagDecl::TK_class: Keyword = ETK_Class; break; |
| 5168 | case TagDecl::TK_union: Keyword = ETK_Union; break; |
| 5169 | case TagDecl::TK_enum: Keyword = ETK_Enum; break; |
| 5170 | } |
Daniel Dunbar | 12c0ade | 2010-04-01 16:50:48 +0000 | [diff] [blame] | 5171 | assert(Keyword != ETK_None && "Invalid tag kind!"); |
| 5172 | |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 5173 | if (TUK == TUK_Declaration || TUK == TUK_Definition) { |
| 5174 | Diag(NameLoc, diag::err_dependent_tag_decl) |
| 5175 | << (TUK == TUK_Definition) << TagDecl::getTagKindForTypeSpec(TagSpec) |
| 5176 | << SS.getRange(); |
| 5177 | return true; |
| 5178 | } |
| 5179 | |
Douglas Gregor | 4033642 | 2010-03-31 22:19:08 +0000 | [diff] [blame] | 5180 | return Context.getDependentNameType(Keyword, NNS, Name).getAsOpaquePtr(); |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 5181 | } |
| 5182 | |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 5183 | static void FillTypeLoc(DependentNameTypeLoc TL, |
| 5184 | SourceLocation TypenameLoc, |
| 5185 | SourceRange QualifierRange) { |
| 5186 | // FIXME: typename, qualifier range |
| 5187 | TL.setNameLoc(TypenameLoc); |
| 5188 | } |
| 5189 | |
| 5190 | static void FillTypeLoc(QualifiedNameTypeLoc TL, |
| 5191 | SourceLocation TypenameLoc, |
| 5192 | SourceRange QualifierRange) { |
| 5193 | // FIXME: typename, qualifier range |
| 5194 | TL.setNameLoc(TypenameLoc); |
| 5195 | } |
| 5196 | |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 5197 | Sema::TypeResult |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5198 | Sema::ActOnTypenameType(SourceLocation TypenameLoc, const CXXScopeSpec &SS, |
| 5199 | const IdentifierInfo &II, SourceLocation IdLoc) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5200 | NestedNameSpecifier *NNS |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5201 | = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); |
| 5202 | if (!NNS) |
| 5203 | return true; |
| 5204 | |
Douglas Gregor | 107de90 | 2010-04-24 15:35:55 +0000 | [diff] [blame] | 5205 | QualType T = CheckTypenameType(ETK_Typename, NNS, II, |
| 5206 | SourceRange(TypenameLoc, IdLoc)); |
Douglas Gregor | 31a19b6 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 5207 | if (T.isNull()) |
| 5208 | return true; |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 5209 | |
| 5210 | TypeSourceInfo *TSI = Context.CreateTypeSourceInfo(T); |
| 5211 | if (isa<DependentNameType>(T)) { |
| 5212 | DependentNameTypeLoc TL = cast<DependentNameTypeLoc>(TSI->getTypeLoc()); |
| 5213 | // FIXME: fill inner type loc |
| 5214 | FillTypeLoc(TL, TypenameLoc, SS.getRange()); |
| 5215 | } else { |
| 5216 | QualifiedNameTypeLoc TL = cast<QualifiedNameTypeLoc>(TSI->getTypeLoc()); |
| 5217 | // FIXME: fill inner type loc |
| 5218 | FillTypeLoc(TL, TypenameLoc, SS.getRange()); |
| 5219 | } |
| 5220 | |
| 5221 | return CreateLocInfoType(T, TSI).getAsOpaquePtr(); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5222 | } |
| 5223 | |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 5224 | Sema::TypeResult |
| 5225 | Sema::ActOnTypenameType(SourceLocation TypenameLoc, const CXXScopeSpec &SS, |
| 5226 | SourceLocation TemplateLoc, TypeTy *Ty) { |
Argyrios Kyrtzidis | e866190 | 2009-08-19 01:28:28 +0000 | [diff] [blame] | 5227 | QualType T = GetTypeFromParser(Ty); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5228 | NestedNameSpecifier *NNS |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 5229 | = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5230 | const TemplateSpecializationType *TemplateId |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5231 | = T->getAs<TemplateSpecializationType>(); |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 5232 | assert(TemplateId && "Expected a template specialization type"); |
| 5233 | |
Douglas Gregor | 6946baf | 2009-09-02 13:05:45 +0000 | [diff] [blame] | 5234 | if (computeDeclContext(SS, false)) { |
| 5235 | // If we can compute a declaration context, then the "typename" |
| 5236 | // keyword was superfluous. Just build a QualifiedNameType to keep |
| 5237 | // track of the nested-name-specifier. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5238 | |
Douglas Gregor | 6946baf | 2009-09-02 13:05:45 +0000 | [diff] [blame] | 5239 | // FIXME: Note that the QualifiedNameType had the "typename" keyword! |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 5240 | |
| 5241 | T = Context.getQualifiedNameType(NNS, T); |
| 5242 | TypeSourceInfo *TSI = Context.CreateTypeSourceInfo(T); |
| 5243 | QualifiedNameTypeLoc TL = cast<QualifiedNameTypeLoc>(TSI->getTypeLoc()); |
| 5244 | // FIXME: fill inner type loc |
| 5245 | FillTypeLoc(TL, TypenameLoc, SS.getRange()); |
| 5246 | return CreateLocInfoType(T, TSI).getAsOpaquePtr(); |
Douglas Gregor | 6946baf | 2009-09-02 13:05:45 +0000 | [diff] [blame] | 5247 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5248 | |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 5249 | T = Context.getDependentNameType(ETK_Typename, NNS, TemplateId); |
| 5250 | TypeSourceInfo *TSI = Context.CreateTypeSourceInfo(T); |
| 5251 | DependentNameTypeLoc TL = cast<DependentNameTypeLoc>(TSI->getTypeLoc()); |
| 5252 | // FIXME: fill inner type loc |
| 5253 | FillTypeLoc(TL, TypenameLoc, SS.getRange()); |
| 5254 | return CreateLocInfoType(T, TSI).getAsOpaquePtr(); |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 5255 | } |
| 5256 | |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5257 | /// \brief Build the type that describes a C++ typename specifier, |
| 5258 | /// e.g., "typename T::type". |
| 5259 | QualType |
Douglas Gregor | 107de90 | 2010-04-24 15:35:55 +0000 | [diff] [blame] | 5260 | Sema::CheckTypenameType(ElaboratedTypeKeyword Keyword, |
| 5261 | NestedNameSpecifier *NNS, const IdentifierInfo &II, |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5262 | SourceRange Range) { |
Douglas Gregor | 42af25f | 2009-05-11 19:58:34 +0000 | [diff] [blame] | 5263 | CXXRecordDecl *CurrentInstantiation = 0; |
| 5264 | if (NNS->isDependent()) { |
| 5265 | CurrentInstantiation = getCurrentInstantiationOf(NNS); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5266 | |
Douglas Gregor | 42af25f | 2009-05-11 19:58:34 +0000 | [diff] [blame] | 5267 | // If the nested-name-specifier does not refer to the current |
| 5268 | // instantiation, then build a typename type. |
| 5269 | if (!CurrentInstantiation) |
Douglas Gregor | 107de90 | 2010-04-24 15:35:55 +0000 | [diff] [blame] | 5270 | return Context.getDependentNameType(Keyword, NNS, &II); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5271 | |
Douglas Gregor | de18d12 | 2009-09-02 13:12:51 +0000 | [diff] [blame] | 5272 | // The nested-name-specifier refers to the current instantiation, so the |
| 5273 | // "typename" keyword itself is superfluous. In C++03, the program is |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5274 | // actually ill-formed. However, DR 382 (in C++0x CD1) allows such |
Douglas Gregor | de18d12 | 2009-09-02 13:12:51 +0000 | [diff] [blame] | 5275 | // extraneous "typename" keywords, and we retroactively apply this DR to |
| 5276 | // C++03 code. |
Douglas Gregor | 42af25f | 2009-05-11 19:58:34 +0000 | [diff] [blame] | 5277 | } |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5278 | |
Douglas Gregor | 42af25f | 2009-05-11 19:58:34 +0000 | [diff] [blame] | 5279 | DeclContext *Ctx = 0; |
| 5280 | |
| 5281 | if (CurrentInstantiation) |
| 5282 | Ctx = CurrentInstantiation; |
| 5283 | else { |
| 5284 | CXXScopeSpec SS; |
| 5285 | SS.setScopeRep(NNS); |
| 5286 | SS.setRange(Range); |
| 5287 | if (RequireCompleteDeclContext(SS)) |
| 5288 | return QualType(); |
| 5289 | |
| 5290 | Ctx = computeDeclContext(SS); |
| 5291 | } |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5292 | assert(Ctx && "No declaration context?"); |
| 5293 | |
| 5294 | DeclarationName Name(&II); |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 5295 | LookupResult Result(*this, Name, Range.getEnd(), LookupOrdinaryName); |
| 5296 | LookupQualifiedName(Result, Ctx); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5297 | unsigned DiagID = 0; |
| 5298 | Decl *Referenced = 0; |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 5299 | switch (Result.getResultKind()) { |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5300 | case LookupResult::NotFound: |
Douglas Gregor | 3f09327 | 2009-10-13 21:16:44 +0000 | [diff] [blame] | 5301 | DiagID = diag::err_typename_nested_not_found; |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5302 | break; |
Douglas Gregor | 7d3f576 | 2010-01-15 01:44:47 +0000 | [diff] [blame] | 5303 | |
| 5304 | case LookupResult::NotFoundInCurrentInstantiation: |
| 5305 | // Okay, it's a member of an unknown instantiation. |
Douglas Gregor | 107de90 | 2010-04-24 15:35:55 +0000 | [diff] [blame] | 5306 | return Context.getDependentNameType(Keyword, NNS, &II); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5307 | |
| 5308 | case LookupResult::Found: |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 5309 | if (TypeDecl *Type = dyn_cast<TypeDecl>(Result.getFoundDecl())) { |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5310 | // We found a type. Build a QualifiedNameType, since the |
| 5311 | // typename-specifier was just sugar. FIXME: Tell |
| 5312 | // QualifiedNameType that it has a "typename" prefix. |
| 5313 | return Context.getQualifiedNameType(NNS, Context.getTypeDeclType(Type)); |
| 5314 | } |
| 5315 | |
| 5316 | DiagID = diag::err_typename_nested_not_type; |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 5317 | Referenced = Result.getFoundDecl(); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5318 | break; |
| 5319 | |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 5320 | case LookupResult::FoundUnresolvedValue: |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 5321 | llvm_unreachable("unresolved using decl in non-dependent context"); |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 5322 | return QualType(); |
| 5323 | |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5324 | case LookupResult::FoundOverloaded: |
| 5325 | DiagID = diag::err_typename_nested_not_type; |
| 5326 | Referenced = *Result.begin(); |
| 5327 | break; |
| 5328 | |
John McCall | 6e24726 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 5329 | case LookupResult::Ambiguous: |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5330 | return QualType(); |
| 5331 | } |
| 5332 | |
| 5333 | // If we get here, it's because name lookup did not find a |
| 5334 | // type. Emit an appropriate diagnostic and return an error. |
Douglas Gregor | 3f09327 | 2009-10-13 21:16:44 +0000 | [diff] [blame] | 5335 | Diag(Range.getEnd(), DiagID) << Range << Name << Ctx; |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5336 | if (Referenced) |
| 5337 | Diag(Referenced->getLocation(), diag::note_typename_refers_here) |
| 5338 | << Name; |
| 5339 | return QualType(); |
| 5340 | } |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 5341 | |
| 5342 | namespace { |
| 5343 | // See Sema::RebuildTypeInCurrentInstantiation |
Benjamin Kramer | 85b4521 | 2009-11-28 19:45:26 +0000 | [diff] [blame] | 5344 | class CurrentInstantiationRebuilder |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5345 | : public TreeTransform<CurrentInstantiationRebuilder> { |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 5346 | SourceLocation Loc; |
| 5347 | DeclarationName Entity; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5348 | |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 5349 | public: |
Douglas Gregor | 895162d | 2010-04-30 18:55:50 +0000 | [diff] [blame] | 5350 | typedef TreeTransform<CurrentInstantiationRebuilder> inherited; |
| 5351 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5352 | CurrentInstantiationRebuilder(Sema &SemaRef, |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 5353 | SourceLocation Loc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5354 | DeclarationName Entity) |
| 5355 | : TreeTransform<CurrentInstantiationRebuilder>(SemaRef), |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 5356 | Loc(Loc), Entity(Entity) { } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5357 | |
| 5358 | /// \brief Determine whether the given type \p T has already been |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 5359 | /// transformed. |
| 5360 | /// |
| 5361 | /// For the purposes of type reconstruction, a type has already been |
| 5362 | /// transformed if it is NULL or if it is not dependent. |
| 5363 | bool AlreadyTransformed(QualType T) { |
| 5364 | return T.isNull() || !T->isDependentType(); |
| 5365 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5366 | |
| 5367 | /// \brief Returns the location of the entity whose type is being |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 5368 | /// rebuilt. |
| 5369 | SourceLocation getBaseLocation() { return Loc; } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5370 | |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 5371 | /// \brief Returns the name of the entity whose type is being rebuilt. |
| 5372 | DeclarationName getBaseEntity() { return Entity; } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5373 | |
Douglas Gregor | 972e6ce | 2009-10-27 06:26:26 +0000 | [diff] [blame] | 5374 | /// \brief Sets the "base" location and entity when that |
| 5375 | /// information is known based on another transformation. |
| 5376 | void setBase(SourceLocation Loc, DeclarationName Entity) { |
| 5377 | this->Loc = Loc; |
| 5378 | this->Entity = Entity; |
| 5379 | } |
| 5380 | |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 5381 | /// \brief Transforms an expression by returning the expression itself |
| 5382 | /// (an identity function). |
| 5383 | /// |
| 5384 | /// FIXME: This is completely unsafe; we will need to actually clone the |
| 5385 | /// expressions. |
| 5386 | Sema::OwningExprResult TransformExpr(Expr *E) { |
Douglas Gregor | 895162d | 2010-04-30 18:55:50 +0000 | [diff] [blame] | 5387 | return getSema().Owned(E->Retain()); |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 5388 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5389 | |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 5390 | /// \brief Transforms a typename type by determining whether the type now |
| 5391 | /// refers to a member of the current instantiation, and then |
| 5392 | /// type-checking and building a QualifiedNameType (when possible). |
Douglas Gregor | 4714c12 | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 5393 | QualType TransformDependentNameType(TypeLocBuilder &TLB, DependentNameTypeLoc TL, |
Douglas Gregor | 124b878 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 5394 | QualType ObjectType); |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 5395 | }; |
| 5396 | } |
| 5397 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5398 | QualType |
Douglas Gregor | 4714c12 | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 5399 | CurrentInstantiationRebuilder::TransformDependentNameType(TypeLocBuilder &TLB, |
| 5400 | DependentNameTypeLoc TL, |
Douglas Gregor | 124b878 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 5401 | QualType ObjectType) { |
Douglas Gregor | 4714c12 | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 5402 | DependentNameType *T = TL.getTypePtr(); |
John McCall | a2becad | 2009-10-21 00:40:46 +0000 | [diff] [blame] | 5403 | |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 5404 | NestedNameSpecifier *NNS |
| 5405 | = TransformNestedNameSpecifier(T->getQualifier(), |
Douglas Gregor | 124b878 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 5406 | /*FIXME:*/SourceRange(getBaseLocation()), |
| 5407 | ObjectType); |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 5408 | if (!NNS) |
| 5409 | return QualType(); |
| 5410 | |
| 5411 | // If the nested-name-specifier did not change, and we cannot compute the |
| 5412 | // context corresponding to the nested-name-specifier, then this |
| 5413 | // typename type will not change; exit early. |
| 5414 | CXXScopeSpec SS; |
| 5415 | SS.setRange(SourceRange(getBaseLocation())); |
| 5416 | SS.setScopeRep(NNS); |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 5417 | |
| 5418 | QualType Result; |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 5419 | if (NNS == T->getQualifier() && getSema().computeDeclContext(SS) == 0) |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 5420 | Result = QualType(T, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5421 | |
| 5422 | // Rebuild the typename type, which will probably turn into a |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 5423 | // QualifiedNameType. |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 5424 | else if (const TemplateSpecializationType *TemplateId = T->getTemplateId()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5425 | QualType NewTemplateId |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 5426 | = TransformType(QualType(TemplateId, 0)); |
| 5427 | if (NewTemplateId.isNull()) |
| 5428 | return QualType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5429 | |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 5430 | if (NNS == T->getQualifier() && |
| 5431 | NewTemplateId == QualType(TemplateId, 0)) |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 5432 | Result = QualType(T, 0); |
| 5433 | else |
Douglas Gregor | 4a2023f | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 5434 | Result = getDerived().RebuildDependentNameType(T->getKeyword(), |
| 5435 | NNS, NewTemplateId); |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 5436 | } else |
Douglas Gregor | 4a2023f | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 5437 | Result = getDerived().RebuildDependentNameType(T->getKeyword(), |
| 5438 | NNS, T->getIdentifier(), |
| 5439 | SourceRange(TL.getNameLoc())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5440 | |
Douglas Gregor | a50ce32 | 2010-03-07 23:26:22 +0000 | [diff] [blame] | 5441 | if (Result.isNull()) |
| 5442 | return QualType(); |
| 5443 | |
Douglas Gregor | 4714c12 | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 5444 | DependentNameTypeLoc NewTL = TLB.push<DependentNameTypeLoc>(Result); |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 5445 | NewTL.setNameLoc(TL.getNameLoc()); |
| 5446 | return Result; |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 5447 | } |
| 5448 | |
| 5449 | /// \brief Rebuilds a type within the context of the current instantiation. |
| 5450 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5451 | /// 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] | 5452 | /// a class template (or class template partial specialization) that was parsed |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5453 | /// and constructed before we entered the scope of the class template (or |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 5454 | /// partial specialization thereof). This routine will rebuild that type now |
| 5455 | /// that we have entered the declarator's scope, which may produce different |
| 5456 | /// canonical types, e.g., |
| 5457 | /// |
| 5458 | /// \code |
| 5459 | /// template<typename T> |
| 5460 | /// struct X { |
| 5461 | /// typedef T* pointer; |
| 5462 | /// pointer data(); |
| 5463 | /// }; |
| 5464 | /// |
| 5465 | /// template<typename T> |
| 5466 | /// typename X<T>::pointer X<T>::data() { ... } |
| 5467 | /// \endcode |
| 5468 | /// |
Douglas Gregor | 4714c12 | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 5469 | /// 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] | 5470 | /// since we do not know that we can look into X<T> when we parsed the type. |
| 5471 | /// This function will rebuild the type, performing the lookup of "pointer" |
| 5472 | /// in X<T> and returning a QualifiedNameType whose canonical type is the same |
| 5473 | /// as the canonical type of T*, allowing the return types of the out-of-line |
| 5474 | /// definition and the declaration to match. |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 5475 | TypeSourceInfo *Sema::RebuildTypeInCurrentInstantiation(TypeSourceInfo *T, |
| 5476 | SourceLocation Loc, |
| 5477 | DeclarationName Name) { |
| 5478 | if (!T || !T->getType()->isDependentType()) |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 5479 | return T; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5480 | |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 5481 | CurrentInstantiationRebuilder Rebuilder(*this, Loc, Name); |
| 5482 | return Rebuilder.TransformType(T); |
Benjamin Kramer | 27ba2f0 | 2009-08-11 22:33:06 +0000 | [diff] [blame] | 5483 | } |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 5484 | |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 5485 | bool Sema::RebuildNestedNameSpecifierInCurrentInstantiation(CXXScopeSpec &SS) { |
| 5486 | if (SS.isInvalid()) return true; |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 5487 | |
| 5488 | NestedNameSpecifier *NNS = static_cast<NestedNameSpecifier*>(SS.getScopeRep()); |
| 5489 | CurrentInstantiationRebuilder Rebuilder(*this, SS.getRange().getBegin(), |
| 5490 | DeclarationName()); |
| 5491 | NestedNameSpecifier *Rebuilt = |
| 5492 | Rebuilder.TransformNestedNameSpecifier(NNS, SS.getRange()); |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 5493 | if (!Rebuilt) return true; |
| 5494 | |
| 5495 | SS.setScopeRep(Rebuilt); |
| 5496 | return false; |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 5497 | } |
| 5498 | |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 5499 | /// \brief Produces a formatted string that describes the binding of |
| 5500 | /// template parameters to template arguments. |
| 5501 | std::string |
| 5502 | Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params, |
| 5503 | const TemplateArgumentList &Args) { |
Douglas Gregor | 9148c3f | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 5504 | // FIXME: For variadic templates, we'll need to get the structured list. |
| 5505 | return getTemplateArgumentBindingsText(Params, Args.getFlatArgumentList(), |
| 5506 | Args.flat_size()); |
| 5507 | } |
| 5508 | |
| 5509 | std::string |
| 5510 | Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params, |
| 5511 | const TemplateArgument *Args, |
| 5512 | unsigned NumArgs) { |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 5513 | std::string Result; |
| 5514 | |
Douglas Gregor | 9148c3f | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 5515 | if (!Params || Params->size() == 0 || NumArgs == 0) |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 5516 | return Result; |
| 5517 | |
| 5518 | for (unsigned I = 0, N = Params->size(); I != N; ++I) { |
Douglas Gregor | 9148c3f | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 5519 | if (I >= NumArgs) |
| 5520 | break; |
| 5521 | |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 5522 | if (I == 0) |
| 5523 | Result += "[with "; |
| 5524 | else |
| 5525 | Result += ", "; |
| 5526 | |
| 5527 | if (const IdentifierInfo *Id = Params->getParam(I)->getIdentifier()) { |
| 5528 | Result += Id->getName(); |
| 5529 | } else { |
| 5530 | Result += '$'; |
| 5531 | Result += llvm::utostr(I); |
| 5532 | } |
| 5533 | |
| 5534 | Result += " = "; |
| 5535 | |
| 5536 | switch (Args[I].getKind()) { |
| 5537 | case TemplateArgument::Null: |
| 5538 | Result += "<no value>"; |
| 5539 | break; |
| 5540 | |
| 5541 | case TemplateArgument::Type: { |
| 5542 | std::string TypeStr; |
| 5543 | Args[I].getAsType().getAsStringInternal(TypeStr, |
| 5544 | Context.PrintingPolicy); |
| 5545 | Result += TypeStr; |
| 5546 | break; |
| 5547 | } |
| 5548 | |
| 5549 | case TemplateArgument::Declaration: { |
| 5550 | bool Unnamed = true; |
| 5551 | if (NamedDecl *ND = dyn_cast_or_null<NamedDecl>(Args[I].getAsDecl())) { |
| 5552 | if (ND->getDeclName()) { |
| 5553 | Unnamed = false; |
| 5554 | Result += ND->getNameAsString(); |
| 5555 | } |
| 5556 | } |
| 5557 | |
| 5558 | if (Unnamed) { |
| 5559 | Result += "<anonymous>"; |
| 5560 | } |
| 5561 | break; |
| 5562 | } |
| 5563 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 5564 | case TemplateArgument::Template: { |
| 5565 | std::string Str; |
| 5566 | llvm::raw_string_ostream OS(Str); |
| 5567 | Args[I].getAsTemplate().print(OS, Context.PrintingPolicy); |
| 5568 | Result += OS.str(); |
| 5569 | break; |
| 5570 | } |
| 5571 | |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 5572 | case TemplateArgument::Integral: { |
| 5573 | Result += Args[I].getAsIntegral()->toString(10); |
| 5574 | break; |
| 5575 | } |
| 5576 | |
| 5577 | case TemplateArgument::Expression: { |
Douglas Gregor | 77e2c67 | 2010-04-29 04:55:13 +0000 | [diff] [blame] | 5578 | // FIXME: This is non-optimal, since we're regurgitating the |
| 5579 | // expression we were given. |
| 5580 | std::string Str; |
| 5581 | { |
| 5582 | llvm::raw_string_ostream OS(Str); |
| 5583 | Args[I].getAsExpr()->printPretty(OS, Context, 0, |
| 5584 | Context.PrintingPolicy); |
| 5585 | } |
| 5586 | Result += Str; |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 5587 | break; |
| 5588 | } |
| 5589 | |
| 5590 | case TemplateArgument::Pack: |
| 5591 | // FIXME: Format template argument packs |
| 5592 | Result += "<template argument pack>"; |
| 5593 | break; |
| 5594 | } |
| 5595 | } |
| 5596 | |
| 5597 | Result += ']'; |
| 5598 | return Result; |
| 5599 | } |