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. |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 30 | static NamedDecl *isAcceptableTemplateName(ASTContext &Context, |
| 31 | NamedDecl *Orig) { |
| 32 | NamedDecl *D = Orig->getUnderlyingDecl(); |
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)) |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 35 | return Orig; |
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(); |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 71 | NamedDecl *Repl = isAcceptableTemplateName(C, Orig); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 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 | 1fd6d44 | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 103 | TemplateTy &TemplateResult, |
| 104 | bool &MemberOfUnknownSpecialization) { |
Douglas Gregor | b862b8f | 2010-01-11 23:29:10 +0000 | [diff] [blame] | 105 | assert(getLangOptions().CPlusPlus && "No template names in C!"); |
| 106 | |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 107 | DeclarationName TName; |
Douglas Gregor | 1fd6d44 | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 108 | MemberOfUnknownSpecialization = false; |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 109 | |
| 110 | switch (Name.getKind()) { |
| 111 | case UnqualifiedId::IK_Identifier: |
| 112 | TName = DeclarationName(Name.Identifier); |
| 113 | break; |
| 114 | |
| 115 | case UnqualifiedId::IK_OperatorFunctionId: |
| 116 | TName = Context.DeclarationNames.getCXXOperatorName( |
| 117 | Name.OperatorFunctionId.Operator); |
| 118 | break; |
| 119 | |
Sean Hunt | e6252d1 | 2009-11-28 08:58:14 +0000 | [diff] [blame] | 120 | case UnqualifiedId::IK_LiteralOperatorId: |
Sean Hunt | 3e518bd | 2009-11-29 07:34:05 +0000 | [diff] [blame] | 121 | TName = Context.DeclarationNames.getCXXLiteralOperatorName(Name.Identifier); |
| 122 | break; |
Sean Hunt | e6252d1 | 2009-11-28 08:58:14 +0000 | [diff] [blame] | 123 | |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 124 | default: |
| 125 | return TNK_Non_template; |
| 126 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 127 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 128 | QualType ObjectType = QualType::getFromOpaquePtr(ObjectTypePtr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 129 | |
Douglas Gregor | bfea239 | 2009-12-31 08:11:17 +0000 | [diff] [blame] | 130 | LookupResult R(*this, TName, Name.getSourceRange().getBegin(), |
| 131 | LookupOrdinaryName); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 132 | R.suppressDiagnostics(); |
Douglas Gregor | 1fd6d44 | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 133 | LookupTemplateName(R, S, SS, ObjectType, EnteringContext, |
| 134 | MemberOfUnknownSpecialization); |
Douglas Gregor | 01e56ae | 2010-04-12 20:54:26 +0000 | [diff] [blame] | 135 | if (R.empty() || R.isAmbiguous()) |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 136 | return TNK_Non_template; |
| 137 | |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 138 | TemplateName Template; |
| 139 | TemplateNameKind TemplateKind; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 140 | |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 141 | unsigned ResultCount = R.end() - R.begin(); |
| 142 | if (ResultCount > 1) { |
| 143 | // We assume that we'll preserve the qualifier from a function |
| 144 | // template name in other ways. |
| 145 | Template = Context.getOverloadedTemplateName(R.begin(), R.end()); |
| 146 | TemplateKind = TNK_Function_template; |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 147 | } else { |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 148 | TemplateDecl *TD = cast<TemplateDecl>((*R.begin())->getUnderlyingDecl()); |
| 149 | |
| 150 | if (SS.isSet() && !SS.isInvalid()) { |
| 151 | NestedNameSpecifier *Qualifier |
| 152 | = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); |
| 153 | Template = Context.getQualifiedTemplateName(Qualifier, false, TD); |
| 154 | } else { |
| 155 | Template = TemplateName(TD); |
| 156 | } |
| 157 | |
| 158 | if (isa<FunctionTemplateDecl>(TD)) |
| 159 | TemplateKind = TNK_Function_template; |
| 160 | else { |
| 161 | assert(isa<ClassTemplateDecl>(TD) || isa<TemplateTemplateParmDecl>(TD)); |
| 162 | TemplateKind = TNK_Type_template; |
| 163 | } |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 164 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 165 | |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 166 | TemplateResult = TemplateTy::make(Template); |
| 167 | return TemplateKind; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 168 | } |
| 169 | |
Douglas Gregor | 84d0a19 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 170 | bool Sema::DiagnoseUnknownTemplateName(const IdentifierInfo &II, |
| 171 | SourceLocation IILoc, |
| 172 | Scope *S, |
| 173 | const CXXScopeSpec *SS, |
| 174 | TemplateTy &SuggestedTemplate, |
| 175 | TemplateNameKind &SuggestedKind) { |
| 176 | // We can't recover unless there's a dependent scope specifier preceding the |
| 177 | // template name. |
Douglas Gregor | d5ab9b0 | 2010-05-21 23:43:39 +0000 | [diff] [blame] | 178 | // FIXME: Typo correction? |
Douglas Gregor | 84d0a19 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 179 | if (!SS || !SS->isSet() || !isDependentScopeSpecifier(*SS) || |
| 180 | computeDeclContext(*SS)) |
| 181 | return false; |
| 182 | |
| 183 | // The code is missing a 'template' keyword prior to the dependent template |
| 184 | // name. |
| 185 | NestedNameSpecifier *Qualifier = (NestedNameSpecifier*)SS->getScopeRep(); |
| 186 | Diag(IILoc, diag::err_template_kw_missing) |
| 187 | << Qualifier << II.getName() |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 188 | << FixItHint::CreateInsertion(IILoc, "template "); |
Douglas Gregor | 84d0a19 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 189 | SuggestedTemplate |
| 190 | = TemplateTy::make(Context.getDependentTemplateName(Qualifier, &II)); |
| 191 | SuggestedKind = TNK_Dependent_template_name; |
| 192 | return true; |
| 193 | } |
| 194 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 195 | void Sema::LookupTemplateName(LookupResult &Found, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 196 | Scope *S, CXXScopeSpec &SS, |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 197 | QualType ObjectType, |
Douglas Gregor | 1fd6d44 | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 198 | bool EnteringContext, |
| 199 | bool &MemberOfUnknownSpecialization) { |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 200 | // Determine where to perform name lookup |
Douglas Gregor | 1fd6d44 | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 201 | MemberOfUnknownSpecialization = false; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 202 | DeclContext *LookupCtx = 0; |
| 203 | bool isDependent = false; |
| 204 | if (!ObjectType.isNull()) { |
| 205 | // This nested-name-specifier occurs in a member access expression, e.g., |
| 206 | // x->B::f, and we are looking into the type of the object. |
| 207 | assert(!SS.isSet() && "ObjectType and scope specifier cannot coexist"); |
| 208 | LookupCtx = computeDeclContext(ObjectType); |
| 209 | isDependent = ObjectType->isDependentType(); |
| 210 | assert((isDependent || !ObjectType->isIncompleteType()) && |
| 211 | "Caller should have completed object type"); |
| 212 | } else if (SS.isSet()) { |
| 213 | // This nested-name-specifier occurs after another nested-name-specifier, |
| 214 | // so long into the context associated with the prior nested-name-specifier. |
| 215 | LookupCtx = computeDeclContext(SS, EnteringContext); |
| 216 | isDependent = isDependentScopeSpecifier(SS); |
| 217 | |
| 218 | // The declaration context must be complete. |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 219 | if (LookupCtx && RequireCompleteDeclContext(SS, LookupCtx)) |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 220 | return; |
| 221 | } |
| 222 | |
| 223 | bool ObjectTypeSearchedInScope = false; |
| 224 | if (LookupCtx) { |
| 225 | // Perform "qualified" name lookup into the declaration context we |
| 226 | // computed, which is either the type of the base of a member access |
| 227 | // expression or the declaration context associated with a prior |
| 228 | // nested-name-specifier. |
| 229 | LookupQualifiedName(Found, LookupCtx); |
| 230 | |
| 231 | if (!ObjectType.isNull() && Found.empty()) { |
| 232 | // C++ [basic.lookup.classref]p1: |
| 233 | // In a class member access expression (5.2.5), if the . or -> token is |
| 234 | // immediately followed by an identifier followed by a <, the |
| 235 | // identifier must be looked up to determine whether the < is the |
| 236 | // beginning of a template argument list (14.2) or a less-than operator. |
| 237 | // The identifier is first looked up in the class of the object |
| 238 | // expression. If the identifier is not found, it is then looked up in |
| 239 | // the context of the entire postfix-expression and shall name a class |
| 240 | // or function template. |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 241 | if (S) LookupName(Found, S); |
| 242 | ObjectTypeSearchedInScope = true; |
| 243 | } |
Douglas Gregor | f9f97a0 | 2010-07-16 16:54:17 +0000 | [diff] [blame] | 244 | } else if (isDependent && (!S || ObjectType.isNull())) { |
Douglas Gregor | 2e93388 | 2010-01-12 17:06:20 +0000 | [diff] [blame] | 245 | // We cannot look into a dependent object type or nested nme |
| 246 | // specifier. |
Douglas Gregor | 1fd6d44 | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 247 | MemberOfUnknownSpecialization = true; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 248 | return; |
| 249 | } else { |
| 250 | // Perform unqualified name lookup in the current scope. |
| 251 | LookupName(Found, S); |
| 252 | } |
| 253 | |
Douglas Gregor | 2e93388 | 2010-01-12 17:06:20 +0000 | [diff] [blame] | 254 | if (Found.empty() && !isDependent) { |
Douglas Gregor | bfea239 | 2009-12-31 08:11:17 +0000 | [diff] [blame] | 255 | // If we did not find any names, attempt to correct any typos. |
| 256 | DeclarationName Name = Found.getLookupName(); |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 257 | if (DeclarationName Corrected = CorrectTypo(Found, S, &SS, LookupCtx, |
Douglas Gregor | 12eb5d6 | 2010-06-29 19:27:42 +0000 | [diff] [blame] | 258 | false, CTC_CXXCasts)) { |
Douglas Gregor | bfea239 | 2009-12-31 08:11:17 +0000 | [diff] [blame] | 259 | FilterAcceptableTemplateNames(Context, Found); |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 260 | if (!Found.empty()) { |
Douglas Gregor | bfea239 | 2009-12-31 08:11:17 +0000 | [diff] [blame] | 261 | if (LookupCtx) |
| 262 | Diag(Found.getNameLoc(), diag::err_no_member_template_suggest) |
| 263 | << Name << LookupCtx << Found.getLookupName() << SS.getRange() |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 264 | << FixItHint::CreateReplacement(Found.getNameLoc(), |
Douglas Gregor | bfea239 | 2009-12-31 08:11:17 +0000 | [diff] [blame] | 265 | Found.getLookupName().getAsString()); |
| 266 | else |
| 267 | Diag(Found.getNameLoc(), diag::err_no_template_suggest) |
| 268 | << Name << Found.getLookupName() |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 269 | << FixItHint::CreateReplacement(Found.getNameLoc(), |
Douglas Gregor | bfea239 | 2009-12-31 08:11:17 +0000 | [diff] [blame] | 270 | Found.getLookupName().getAsString()); |
Douglas Gregor | 67dd1d4 | 2010-01-07 00:17:44 +0000 | [diff] [blame] | 271 | if (TemplateDecl *Template = Found.getAsSingle<TemplateDecl>()) |
| 272 | Diag(Template->getLocation(), diag::note_previous_decl) |
| 273 | << Template->getDeclName(); |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 274 | } |
Douglas Gregor | bfea239 | 2009-12-31 08:11:17 +0000 | [diff] [blame] | 275 | } else { |
| 276 | Found.clear(); |
Douglas Gregor | 12eb5d6 | 2010-06-29 19:27:42 +0000 | [diff] [blame] | 277 | Found.setLookupName(Name); |
Douglas Gregor | bfea239 | 2009-12-31 08:11:17 +0000 | [diff] [blame] | 278 | } |
| 279 | } |
| 280 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 281 | FilterAcceptableTemplateNames(Context, Found); |
Douglas Gregor | f9f97a0 | 2010-07-16 16:54:17 +0000 | [diff] [blame] | 282 | if (Found.empty()) { |
| 283 | if (isDependent) |
| 284 | MemberOfUnknownSpecialization = true; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 285 | return; |
Douglas Gregor | f9f97a0 | 2010-07-16 16:54:17 +0000 | [diff] [blame] | 286 | } |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 287 | |
| 288 | if (S && !ObjectType.isNull() && !ObjectTypeSearchedInScope) { |
| 289 | // C++ [basic.lookup.classref]p1: |
| 290 | // [...] If the lookup in the class of the object expression finds a |
| 291 | // template, the name is also looked up in the context of the entire |
| 292 | // postfix-expression and [...] |
| 293 | // |
| 294 | LookupResult FoundOuter(*this, Found.getLookupName(), Found.getNameLoc(), |
| 295 | LookupOrdinaryName); |
| 296 | LookupName(FoundOuter, S); |
| 297 | FilterAcceptableTemplateNames(Context, FoundOuter); |
Douglas Gregor | 01e56ae | 2010-04-12 20:54:26 +0000 | [diff] [blame] | 298 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 299 | if (FoundOuter.empty()) { |
| 300 | // - if the name is not found, the name found in the class of the |
| 301 | // object expression is used, otherwise |
| 302 | } else if (!FoundOuter.getAsSingle<ClassTemplateDecl>()) { |
| 303 | // - if the name is found in the context of the entire |
| 304 | // postfix-expression and does not name a class template, the name |
| 305 | // found in the class of the object expression is used, otherwise |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 306 | } else if (!Found.isSuppressingDiagnostics()) { |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 307 | // - if the name found is a class template, it must refer to the same |
| 308 | // entity as the one found in the class of the object expression, |
| 309 | // otherwise the program is ill-formed. |
| 310 | if (!Found.isSingleResult() || |
| 311 | Found.getFoundDecl()->getCanonicalDecl() |
| 312 | != FoundOuter.getFoundDecl()->getCanonicalDecl()) { |
| 313 | Diag(Found.getNameLoc(), |
Jeffrey Yasskin | 21d07e4 | 2010-06-05 01:39:57 +0000 | [diff] [blame] | 314 | diag::ext_nested_name_member_ref_lookup_ambiguous) |
| 315 | << Found.getLookupName() |
| 316 | << ObjectType; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 317 | Diag(Found.getRepresentativeDecl()->getLocation(), |
| 318 | diag::note_ambig_member_ref_object_type) |
| 319 | << ObjectType; |
| 320 | Diag(FoundOuter.getFoundDecl()->getLocation(), |
| 321 | diag::note_ambig_member_ref_scope); |
| 322 | |
| 323 | // Recover by taking the template that we found in the object |
| 324 | // expression's type. |
| 325 | } |
| 326 | } |
| 327 | } |
| 328 | } |
| 329 | |
John McCall | 2f841ba | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 330 | /// ActOnDependentIdExpression - Handle a dependent id-expression that |
| 331 | /// was just parsed. This is only possible with an explicit scope |
| 332 | /// specifier naming a dependent type. |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 333 | Sema::OwningExprResult |
| 334 | Sema::ActOnDependentIdExpression(const CXXScopeSpec &SS, |
| 335 | DeclarationName Name, |
| 336 | SourceLocation NameLoc, |
John McCall | 2f841ba | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 337 | bool isAddressOfOperand, |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 338 | const TemplateArgumentListInfo *TemplateArgs) { |
| 339 | NestedNameSpecifier *Qualifier |
| 340 | = static_cast<NestedNameSpecifier*>(SS.getScopeRep()); |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 341 | |
| 342 | DeclContext *DC = getFunctionLevelDeclContext(); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 343 | |
John McCall | 2f841ba | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 344 | if (!isAddressOfOperand && |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 345 | isa<CXXMethodDecl>(DC) && |
| 346 | cast<CXXMethodDecl>(DC)->isInstance()) { |
| 347 | QualType ThisType = cast<CXXMethodDecl>(DC)->getThisType(Context); |
John McCall | 2f841ba | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 348 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 349 | // Since the 'this' expression is synthesized, we don't need to |
| 350 | // perform the double-lookup check. |
| 351 | NamedDecl *FirstQualifierInScope = 0; |
| 352 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 353 | return Owned(CXXDependentScopeMemberExpr::Create(Context, |
| 354 | /*This*/ 0, ThisType, |
| 355 | /*IsArrow*/ true, |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 356 | /*Op*/ SourceLocation(), |
| 357 | Qualifier, SS.getRange(), |
| 358 | FirstQualifierInScope, |
| 359 | Name, NameLoc, |
| 360 | TemplateArgs)); |
| 361 | } |
| 362 | |
| 363 | return BuildDependentDeclRefExpr(SS, Name, NameLoc, TemplateArgs); |
| 364 | } |
| 365 | |
| 366 | Sema::OwningExprResult |
| 367 | Sema::BuildDependentDeclRefExpr(const CXXScopeSpec &SS, |
| 368 | DeclarationName Name, |
| 369 | SourceLocation NameLoc, |
| 370 | const TemplateArgumentListInfo *TemplateArgs) { |
| 371 | return Owned(DependentScopeDeclRefExpr::Create(Context, |
| 372 | static_cast<NestedNameSpecifier*>(SS.getScopeRep()), |
| 373 | SS.getRange(), |
| 374 | Name, NameLoc, |
| 375 | TemplateArgs)); |
Douglas Gregor | d6fb7ef | 2008-12-18 19:37:40 +0000 | [diff] [blame] | 376 | } |
| 377 | |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 378 | /// DiagnoseTemplateParameterShadow - Produce a diagnostic complaining |
| 379 | /// that the template parameter 'PrevDecl' is being shadowed by a new |
| 380 | /// declaration at location Loc. Returns true to indicate that this is |
| 381 | /// an error, and false otherwise. |
| 382 | bool Sema::DiagnoseTemplateParameterShadow(SourceLocation Loc, Decl *PrevDecl) { |
Douglas Gregor | f57172b | 2008-12-08 18:40:42 +0000 | [diff] [blame] | 383 | assert(PrevDecl->isTemplateParameter() && "Not a template parameter"); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 384 | |
| 385 | // Microsoft Visual C++ permits template parameters to be shadowed. |
| 386 | if (getLangOptions().Microsoft) |
| 387 | return false; |
| 388 | |
| 389 | // C++ [temp.local]p4: |
| 390 | // A template-parameter shall not be redeclared within its |
| 391 | // scope (including nested scopes). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 392 | Diag(Loc, diag::err_template_param_shadow) |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 393 | << cast<NamedDecl>(PrevDecl)->getDeclName(); |
| 394 | Diag(PrevDecl->getLocation(), diag::note_template_param_here); |
| 395 | return true; |
| 396 | } |
| 397 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 398 | /// AdjustDeclIfTemplate - If the given decl happens to be a template, reset |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 399 | /// the parameter D to reference the templated declaration and return a pointer |
| 400 | /// to the template declaration. Otherwise, do nothing to D and return null. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 401 | TemplateDecl *Sema::AdjustDeclIfTemplate(DeclPtrTy &D) { |
Douglas Gregor | 13d2d6c | 2009-10-06 21:27:51 +0000 | [diff] [blame] | 402 | if (TemplateDecl *Temp = dyn_cast_or_null<TemplateDecl>(D.getAs<Decl>())) { |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 403 | D = DeclPtrTy::make(Temp->getTemplatedDecl()); |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 404 | return Temp; |
| 405 | } |
| 406 | return 0; |
| 407 | } |
| 408 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 409 | static TemplateArgumentLoc translateTemplateArgument(Sema &SemaRef, |
| 410 | const ParsedTemplateArgument &Arg) { |
| 411 | |
| 412 | switch (Arg.getKind()) { |
| 413 | case ParsedTemplateArgument::Type: { |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 414 | TypeSourceInfo *DI; |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 415 | QualType T = SemaRef.GetTypeFromParser(Arg.getAsType(), &DI); |
| 416 | if (!DI) |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 417 | DI = SemaRef.Context.getTrivialTypeSourceInfo(T, Arg.getLocation()); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 418 | return TemplateArgumentLoc(TemplateArgument(T), DI); |
| 419 | } |
| 420 | |
| 421 | case ParsedTemplateArgument::NonType: { |
| 422 | Expr *E = static_cast<Expr *>(Arg.getAsExpr()); |
| 423 | return TemplateArgumentLoc(TemplateArgument(E), E); |
| 424 | } |
| 425 | |
| 426 | case ParsedTemplateArgument::Template: { |
| 427 | TemplateName Template |
| 428 | = TemplateName::getFromVoidPointer(Arg.getAsTemplate().get()); |
| 429 | return TemplateArgumentLoc(TemplateArgument(Template), |
| 430 | Arg.getScopeSpec().getRange(), |
| 431 | Arg.getLocation()); |
| 432 | } |
| 433 | } |
| 434 | |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 435 | llvm_unreachable("Unhandled parsed template argument"); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 436 | return TemplateArgumentLoc(); |
| 437 | } |
| 438 | |
| 439 | /// \brief Translates template arguments as provided by the parser |
| 440 | /// into template arguments used by semantic analysis. |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 441 | void Sema::translateTemplateArguments(const ASTTemplateArgsPtr &TemplateArgsIn, |
| 442 | TemplateArgumentListInfo &TemplateArgs) { |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 443 | for (unsigned I = 0, Last = TemplateArgsIn.size(); I != Last; ++I) |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 444 | TemplateArgs.addArgument(translateTemplateArgument(*this, |
| 445 | TemplateArgsIn[I])); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 446 | } |
| 447 | |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 448 | /// ActOnTypeParameter - Called when a C++ template type parameter |
| 449 | /// (e.g., "typename T") has been parsed. Typename specifies whether |
| 450 | /// the keyword "typename" was used to declare the type parameter |
| 451 | /// (otherwise, "class" was used), and KeyLoc is the location of the |
| 452 | /// "class" or "typename" keyword. ParamName is the name of the |
| 453 | /// parameter (NULL indicates an unnamed template parameter) and |
Douglas Gregor | efed5c8 | 2010-06-16 15:23:05 +0000 | [diff] [blame] | 454 | /// ParamName is the location of the parameter name (if any). |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 455 | /// If the type parameter has a default argument, it will be added |
| 456 | /// later via ActOnTypeParameterDefault. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 457 | Sema::DeclPtrTy Sema::ActOnTypeParameter(Scope *S, bool Typename, bool Ellipsis, |
Anders Carlsson | 941df7d | 2009-06-12 19:58:00 +0000 | [diff] [blame] | 458 | SourceLocation EllipsisLoc, |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 459 | SourceLocation KeyLoc, |
| 460 | IdentifierInfo *ParamName, |
| 461 | SourceLocation ParamNameLoc, |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 462 | unsigned Depth, unsigned Position, |
| 463 | SourceLocation EqualLoc, |
| 464 | TypeTy *DefaultArg) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 465 | assert(S->isTemplateParamScope() && |
| 466 | "Template type parameter not in template parameter scope!"); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 467 | bool Invalid = false; |
| 468 | |
| 469 | if (ParamName) { |
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 470 | NamedDecl *PrevDecl = LookupSingleName(S, ParamName, ParamNameLoc, |
Douglas Gregor | c0b3964 | 2010-04-15 23:40:53 +0000 | [diff] [blame] | 471 | LookupOrdinaryName, |
| 472 | ForRedeclaration); |
Douglas Gregor | f57172b | 2008-12-08 18:40:42 +0000 | [diff] [blame] | 473 | if (PrevDecl && PrevDecl->isTemplateParameter()) |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 474 | Invalid = Invalid || DiagnoseTemplateParameterShadow(ParamNameLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 475 | PrevDecl); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 476 | } |
| 477 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 478 | SourceLocation Loc = ParamNameLoc; |
| 479 | if (!ParamName) |
| 480 | Loc = KeyLoc; |
| 481 | |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 482 | TemplateTypeParmDecl *Param |
John McCall | 7a9813c | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 483 | = TemplateTypeParmDecl::Create(Context, Context.getTranslationUnitDecl(), |
| 484 | Loc, Depth, Position, ParamName, Typename, |
Anders Carlsson | 6d845ae | 2009-06-12 22:23:22 +0000 | [diff] [blame] | 485 | Ellipsis); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 486 | if (Invalid) |
| 487 | Param->setInvalidDecl(); |
| 488 | |
| 489 | if (ParamName) { |
| 490 | // Add the template parameter into the current scope. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 491 | S->AddDecl(DeclPtrTy::make(Param)); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 492 | IdResolver.AddDecl(Param); |
| 493 | } |
| 494 | |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 495 | // Handle the default argument, if provided. |
| 496 | if (DefaultArg) { |
| 497 | TypeSourceInfo *DefaultTInfo; |
| 498 | GetTypeFromParser(DefaultArg, &DefaultTInfo); |
| 499 | |
| 500 | assert(DefaultTInfo && "expected source information for type"); |
| 501 | |
| 502 | // C++0x [temp.param]p9: |
| 503 | // A default template-argument may be specified for any kind of |
| 504 | // template-parameter that is not a template parameter pack. |
| 505 | if (Ellipsis) { |
| 506 | Diag(EqualLoc, diag::err_template_param_pack_default_arg); |
| 507 | return DeclPtrTy::make(Param); |
| 508 | } |
| 509 | |
| 510 | // Check the template argument itself. |
| 511 | if (CheckTemplateArgument(Param, DefaultTInfo)) { |
| 512 | Param->setInvalidDecl(); |
| 513 | return DeclPtrTy::make(Param);; |
| 514 | } |
| 515 | |
| 516 | Param->setDefaultArgument(DefaultTInfo, false); |
| 517 | } |
| 518 | |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 519 | return DeclPtrTy::make(Param); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 520 | } |
| 521 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 522 | /// \brief Check that the type of a non-type template parameter is |
| 523 | /// well-formed. |
| 524 | /// |
| 525 | /// \returns the (possibly-promoted) parameter type if valid; |
| 526 | /// otherwise, produces a diagnostic and returns a NULL type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 527 | QualType |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 528 | Sema::CheckNonTypeTemplateParameterType(QualType T, SourceLocation Loc) { |
Douglas Gregor | a481ec4 | 2010-05-23 19:57:01 +0000 | [diff] [blame] | 529 | // We don't allow variably-modified types as the type of non-type template |
| 530 | // parameters. |
| 531 | if (T->isVariablyModifiedType()) { |
| 532 | Diag(Loc, diag::err_variably_modified_nontype_template_param) |
| 533 | << T; |
| 534 | return QualType(); |
| 535 | } |
| 536 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 537 | // C++ [temp.param]p4: |
| 538 | // |
| 539 | // A non-type template-parameter shall have one of the following |
| 540 | // (optionally cv-qualified) types: |
| 541 | // |
| 542 | // -- integral or enumeration type, |
Douglas Gregor | 2ade35e | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 543 | if (T->isIntegralOrEnumerationType() || |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 544 | // -- pointer to object or pointer to function, |
| 545 | (T->isPointerType() && |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 546 | (T->getAs<PointerType>()->getPointeeType()->isObjectType() || |
| 547 | T->getAs<PointerType>()->getPointeeType()->isFunctionType())) || |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 548 | // -- reference to object or reference to function, |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 549 | T->isReferenceType() || |
| 550 | // -- pointer to member. |
| 551 | T->isMemberPointerType() || |
| 552 | // If T is a dependent type, we can't do the check now, so we |
| 553 | // assume that it is well-formed. |
| 554 | T->isDependentType()) |
| 555 | return T; |
| 556 | // C++ [temp.param]p8: |
| 557 | // |
| 558 | // A non-type template-parameter of type "array of T" or |
| 559 | // "function returning T" is adjusted to be of type "pointer to |
| 560 | // T" or "pointer to function returning T", respectively. |
| 561 | else if (T->isArrayType()) |
| 562 | // FIXME: Keep the type prior to promotion? |
| 563 | return Context.getArrayDecayedType(T); |
| 564 | else if (T->isFunctionType()) |
| 565 | // FIXME: Keep the type prior to promotion? |
| 566 | return Context.getPointerType(T); |
Douglas Gregor | 0fddb97 | 2010-05-22 16:17:30 +0000 | [diff] [blame] | 567 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 568 | Diag(Loc, diag::err_template_nontype_parm_bad_type) |
| 569 | << T; |
| 570 | |
| 571 | return QualType(); |
| 572 | } |
| 573 | |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 574 | Sema::DeclPtrTy Sema::ActOnNonTypeTemplateParameter(Scope *S, Declarator &D, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 575 | unsigned Depth, |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 576 | unsigned Position, |
| 577 | SourceLocation EqualLoc, |
| 578 | ExprArg DefaultArg) { |
John McCall | bf1a028 | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 579 | TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S); |
| 580 | QualType T = TInfo->getType(); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 581 | |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 582 | assert(S->isTemplateParamScope() && |
| 583 | "Non-type template parameter not in template parameter scope!"); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 584 | bool Invalid = false; |
| 585 | |
| 586 | IdentifierInfo *ParamName = D.getIdentifier(); |
| 587 | if (ParamName) { |
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 588 | NamedDecl *PrevDecl = LookupSingleName(S, ParamName, D.getIdentifierLoc(), |
Douglas Gregor | c0b3964 | 2010-04-15 23:40:53 +0000 | [diff] [blame] | 589 | LookupOrdinaryName, |
| 590 | ForRedeclaration); |
Douglas Gregor | f57172b | 2008-12-08 18:40:42 +0000 | [diff] [blame] | 591 | if (PrevDecl && PrevDecl->isTemplateParameter()) |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 592 | Invalid = Invalid || DiagnoseTemplateParameterShadow(D.getIdentifierLoc(), |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 593 | PrevDecl); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 594 | } |
| 595 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 596 | T = CheckNonTypeTemplateParameterType(T, D.getIdentifierLoc()); |
Douglas Gregor | ceef30c | 2009-03-09 16:46:39 +0000 | [diff] [blame] | 597 | if (T.isNull()) { |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 598 | T = Context.IntTy; // Recover with an 'int' type. |
Douglas Gregor | ceef30c | 2009-03-09 16:46:39 +0000 | [diff] [blame] | 599 | Invalid = true; |
| 600 | } |
Douglas Gregor | 5d290d5 | 2009-02-10 17:43:50 +0000 | [diff] [blame] | 601 | |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 602 | NonTypeTemplateParmDecl *Param |
John McCall | 7a9813c | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 603 | = NonTypeTemplateParmDecl::Create(Context, Context.getTranslationUnitDecl(), |
| 604 | D.getIdentifierLoc(), |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 605 | Depth, Position, ParamName, T, TInfo); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 606 | if (Invalid) |
| 607 | Param->setInvalidDecl(); |
| 608 | |
| 609 | if (D.getIdentifier()) { |
| 610 | // Add the template parameter into the current scope. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 611 | S->AddDecl(DeclPtrTy::make(Param)); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 612 | IdResolver.AddDecl(Param); |
| 613 | } |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 614 | |
| 615 | // Check the well-formedness of the default template argument, if provided. |
| 616 | if (Expr *Default = static_cast<Expr *>(DefaultArg.get())) { |
| 617 | TemplateArgument Converted; |
| 618 | if (CheckTemplateArgument(Param, Param->getType(), Default, Converted)) { |
| 619 | Param->setInvalidDecl(); |
| 620 | return DeclPtrTy::make(Param);; |
| 621 | } |
| 622 | |
| 623 | Param->setDefaultArgument(DefaultArg.takeAs<Expr>(), false); |
| 624 | } |
| 625 | |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 626 | return DeclPtrTy::make(Param); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 627 | } |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 628 | |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 629 | /// ActOnTemplateTemplateParameter - Called when a C++ template template |
| 630 | /// parameter (e.g. T in template <template <typename> class T> class array) |
| 631 | /// has been parsed. S is the current scope. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 632 | Sema::DeclPtrTy Sema::ActOnTemplateTemplateParameter(Scope* S, |
| 633 | SourceLocation TmpLoc, |
| 634 | TemplateParamsTy *Params, |
| 635 | IdentifierInfo *Name, |
| 636 | SourceLocation NameLoc, |
| 637 | unsigned Depth, |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 638 | unsigned Position, |
| 639 | SourceLocation EqualLoc, |
| 640 | const ParsedTemplateArgument &Default) { |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 641 | assert(S->isTemplateParamScope() && |
| 642 | "Template template parameter not in template parameter scope!"); |
| 643 | |
| 644 | // Construct the parameter object. |
| 645 | TemplateTemplateParmDecl *Param = |
John McCall | 7a9813c | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 646 | TemplateTemplateParmDecl::Create(Context, Context.getTranslationUnitDecl(), |
| 647 | TmpLoc, Depth, Position, Name, |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 648 | (TemplateParameterList*)Params); |
| 649 | |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 650 | // If the template template parameter has a name, then link the identifier |
| 651 | // into the scope and lookup mechanisms. |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 652 | if (Name) { |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 653 | S->AddDecl(DeclPtrTy::make(Param)); |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 654 | IdResolver.AddDecl(Param); |
| 655 | } |
| 656 | |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 657 | if (!Default.isInvalid()) { |
| 658 | // Check only that we have a template template argument. We don't want to |
| 659 | // try to check well-formedness now, because our template template parameter |
| 660 | // might have dependent types in its template parameters, which we wouldn't |
| 661 | // be able to match now. |
| 662 | // |
| 663 | // If none of the template template parameter's template arguments mention |
| 664 | // other template parameters, we could actually perform more checking here. |
| 665 | // However, it isn't worth doing. |
| 666 | TemplateArgumentLoc DefaultArg = translateTemplateArgument(*this, Default); |
| 667 | if (DefaultArg.getArgument().getAsTemplate().isNull()) { |
| 668 | Diag(DefaultArg.getLocation(), diag::err_template_arg_not_class_template) |
| 669 | << DefaultArg.getSourceRange(); |
| 670 | return DeclPtrTy::make(Param); |
| 671 | } |
| 672 | |
| 673 | Param->setDefaultArgument(DefaultArg, false); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 674 | } |
Douglas Gregor | 9148c3f | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 675 | |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 676 | return DeclPtrTy::make(Param); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 677 | } |
| 678 | |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 679 | /// ActOnTemplateParameterList - Builds a TemplateParameterList that |
| 680 | /// contains the template parameters in Params/NumParams. |
| 681 | Sema::TemplateParamsTy * |
| 682 | Sema::ActOnTemplateParameterList(unsigned Depth, |
| 683 | SourceLocation ExportLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 684 | SourceLocation TemplateLoc, |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 685 | SourceLocation LAngleLoc, |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 686 | DeclPtrTy *Params, unsigned NumParams, |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 687 | SourceLocation RAngleLoc) { |
| 688 | if (ExportLoc.isValid()) |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 689 | Diag(ExportLoc, diag::warn_template_export_unsupported); |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 690 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 691 | return TemplateParameterList::Create(Context, TemplateLoc, LAngleLoc, |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 692 | (NamedDecl**)Params, NumParams, |
| 693 | RAngleLoc); |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 694 | } |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 695 | |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 696 | static void SetNestedNameSpecifier(TagDecl *T, const CXXScopeSpec &SS) { |
| 697 | if (SS.isSet()) |
| 698 | T->setQualifierInfo(static_cast<NestedNameSpecifier*>(SS.getScopeRep()), |
| 699 | SS.getRange()); |
| 700 | } |
| 701 | |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 702 | Sema::DeclResult |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 703 | Sema::CheckClassTemplate(Scope *S, unsigned TagSpec, TagUseKind TUK, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 704 | SourceLocation KWLoc, CXXScopeSpec &SS, |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 705 | IdentifierInfo *Name, SourceLocation NameLoc, |
| 706 | AttributeList *Attr, |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 707 | TemplateParameterList *TemplateParams, |
Anders Carlsson | 5aeccdb | 2009-03-26 00:52:18 +0000 | [diff] [blame] | 708 | AccessSpecifier AS) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 709 | assert(TemplateParams && TemplateParams->size() > 0 && |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 710 | "No template parameters"); |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 711 | assert(TUK != TUK_Reference && "Can only declare or define class templates"); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 712 | bool Invalid = false; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 713 | |
| 714 | // Check that we can declare a template here. |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 715 | if (CheckTemplateDeclScope(S, TemplateParams)) |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 716 | return true; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 717 | |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 718 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
| 719 | assert(Kind != TTK_Enum && "can't build template of enumerated type"); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 720 | |
| 721 | // There is no such thing as an unnamed class template. |
| 722 | if (!Name) { |
| 723 | Diag(KWLoc, diag::err_template_unnamed_class); |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 724 | return true; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 725 | } |
| 726 | |
| 727 | // Find any previous declaration with this name. |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 728 | DeclContext *SemanticContext; |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 729 | LookupResult Previous(*this, Name, NameLoc, LookupOrdinaryName, |
John McCall | 7d384dd | 2009-11-18 07:57:50 +0000 | [diff] [blame] | 730 | ForRedeclaration); |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 731 | if (SS.isNotEmpty() && !SS.isInvalid()) { |
| 732 | SemanticContext = computeDeclContext(SS, true); |
| 733 | if (!SemanticContext) { |
| 734 | // FIXME: Produce a reasonable diagnostic here |
| 735 | return true; |
| 736 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 737 | |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 738 | if (RequireCompleteDeclContext(SS, SemanticContext)) |
| 739 | return true; |
| 740 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 741 | LookupQualifiedName(Previous, SemanticContext); |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 742 | } else { |
| 743 | SemanticContext = CurContext; |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 744 | LookupName(Previous, S); |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 745 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 746 | |
Douglas Gregor | 57265e3 | 2010-04-12 16:00:01 +0000 | [diff] [blame] | 747 | if (Previous.isAmbiguous()) |
| 748 | return true; |
| 749 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 750 | NamedDecl *PrevDecl = 0; |
| 751 | if (Previous.begin() != Previous.end()) |
Douglas Gregor | 57265e3 | 2010-04-12 16:00:01 +0000 | [diff] [blame] | 752 | PrevDecl = (*Previous.begin())->getUnderlyingDecl(); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 753 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 754 | // If there is a previous declaration with the same name, check |
| 755 | // whether this is a valid redeclaration. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 756 | ClassTemplateDecl *PrevClassTemplate |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 757 | = dyn_cast_or_null<ClassTemplateDecl>(PrevDecl); |
Douglas Gregor | d7e5bdb | 2009-10-09 21:11:42 +0000 | [diff] [blame] | 758 | |
| 759 | // We may have found the injected-class-name of a class template, |
| 760 | // class template partial specialization, or class template specialization. |
| 761 | // In these cases, grab the template that is being defined or specialized. |
| 762 | if (!PrevClassTemplate && PrevDecl && isa<CXXRecordDecl>(PrevDecl) && |
| 763 | cast<CXXRecordDecl>(PrevDecl)->isInjectedClassName()) { |
| 764 | PrevDecl = cast<CXXRecordDecl>(PrevDecl->getDeclContext()); |
| 765 | PrevClassTemplate |
| 766 | = cast<CXXRecordDecl>(PrevDecl)->getDescribedClassTemplate(); |
| 767 | if (!PrevClassTemplate && isa<ClassTemplateSpecializationDecl>(PrevDecl)) { |
| 768 | PrevClassTemplate |
| 769 | = cast<ClassTemplateSpecializationDecl>(PrevDecl) |
| 770 | ->getSpecializedTemplate(); |
| 771 | } |
| 772 | } |
| 773 | |
John McCall | 65c4946 | 2009-12-18 11:25:59 +0000 | [diff] [blame] | 774 | if (TUK == TUK_Friend) { |
John McCall | e129d44 | 2009-12-17 23:21:11 +0000 | [diff] [blame] | 775 | // C++ [namespace.memdef]p3: |
| 776 | // [...] When looking for a prior declaration of a class or a function |
| 777 | // declared as a friend, and when the name of the friend class or |
| 778 | // function is neither a qualified name nor a template-id, scopes outside |
| 779 | // the innermost enclosing namespace scope are not considered. |
Douglas Gregor | c1c9df7 | 2010-04-18 17:37:40 +0000 | [diff] [blame] | 780 | if (!SS.isSet()) { |
| 781 | DeclContext *OutermostContext = CurContext; |
| 782 | while (!OutermostContext->isFileContext()) |
| 783 | OutermostContext = OutermostContext->getLookupParent(); |
John McCall | 65c4946 | 2009-12-18 11:25:59 +0000 | [diff] [blame] | 784 | |
Douglas Gregor | c1c9df7 | 2010-04-18 17:37:40 +0000 | [diff] [blame] | 785 | if (PrevDecl && |
| 786 | (OutermostContext->Equals(PrevDecl->getDeclContext()) || |
| 787 | OutermostContext->Encloses(PrevDecl->getDeclContext()))) { |
| 788 | SemanticContext = PrevDecl->getDeclContext(); |
| 789 | } else { |
| 790 | // Declarations in outer scopes don't matter. However, the outermost |
| 791 | // context we computed is the semantic context for our new |
| 792 | // declaration. |
| 793 | PrevDecl = PrevClassTemplate = 0; |
| 794 | SemanticContext = OutermostContext; |
| 795 | } |
John McCall | e129d44 | 2009-12-17 23:21:11 +0000 | [diff] [blame] | 796 | } |
Douglas Gregor | c1c9df7 | 2010-04-18 17:37:40 +0000 | [diff] [blame] | 797 | |
John McCall | e129d44 | 2009-12-17 23:21:11 +0000 | [diff] [blame] | 798 | if (CurContext->isDependentContext()) { |
| 799 | // If this is a dependent context, we don't want to link the friend |
| 800 | // class template to the template in scope, because that would perform |
| 801 | // checking of the template parameter lists that can't be performed |
| 802 | // until the outer context is instantiated. |
| 803 | PrevDecl = PrevClassTemplate = 0; |
| 804 | } |
| 805 | } else if (PrevDecl && !isDeclInScope(PrevDecl, SemanticContext, S)) |
| 806 | PrevDecl = PrevClassTemplate = 0; |
Douglas Gregor | 57265e3 | 2010-04-12 16:00:01 +0000 | [diff] [blame] | 807 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 808 | if (PrevClassTemplate) { |
| 809 | // Ensure that the template parameter lists are compatible. |
| 810 | if (!TemplateParameterListsAreEqual(TemplateParams, |
| 811 | PrevClassTemplate->getTemplateParameters(), |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 812 | /*Complain=*/true, |
| 813 | TPL_TemplateMatch)) |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 814 | return true; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 815 | |
| 816 | // C++ [temp.class]p4: |
| 817 | // In a redeclaration, partial specialization, explicit |
| 818 | // specialization or explicit instantiation of a class template, |
| 819 | // the class-key shall agree in kind with the original class |
| 820 | // template declaration (7.1.5.3). |
| 821 | RecordDecl *PrevRecordDecl = PrevClassTemplate->getTemplatedDecl(); |
Douglas Gregor | 501c5ce | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 822 | if (!isAcceptableTagRedeclaration(PrevRecordDecl, Kind, KWLoc, *Name)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 823 | Diag(KWLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 824 | << Name |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 825 | << FixItHint::CreateReplacement(KWLoc, PrevRecordDecl->getKindName()); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 826 | Diag(PrevRecordDecl->getLocation(), diag::note_previous_use); |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 827 | Kind = PrevRecordDecl->getTagKind(); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 828 | } |
| 829 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 830 | // Check for redefinition of this class template. |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 831 | if (TUK == TUK_Definition) { |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 832 | if (TagDecl *Def = PrevRecordDecl->getDefinition()) { |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 833 | Diag(NameLoc, diag::err_redefinition) << Name; |
| 834 | Diag(Def->getLocation(), diag::note_previous_definition); |
| 835 | // FIXME: Would it make sense to try to "forget" the previous |
| 836 | // definition, as part of error recovery? |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 837 | return true; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 838 | } |
| 839 | } |
| 840 | } else if (PrevDecl && PrevDecl->isTemplateParameter()) { |
| 841 | // Maybe we will complain about the shadowed template parameter. |
| 842 | DiagnoseTemplateParameterShadow(NameLoc, PrevDecl); |
| 843 | // Just pretend that we didn't see the previous declaration. |
| 844 | PrevDecl = 0; |
| 845 | } else if (PrevDecl) { |
| 846 | // C++ [temp]p5: |
| 847 | // A class template shall not have the same name as any other |
| 848 | // template, class, function, object, enumeration, enumerator, |
| 849 | // namespace, or type in the same scope (3.3), except as specified |
| 850 | // in (14.5.4). |
| 851 | Diag(NameLoc, diag::err_redefinition_different_kind) << Name; |
| 852 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 853 | return true; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 854 | } |
| 855 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 856 | // Check the template parameter list of this declaration, possibly |
| 857 | // merging in the template parameter list from the previous class |
| 858 | // template declaration. |
| 859 | if (CheckTemplateParameterList(TemplateParams, |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 860 | PrevClassTemplate? PrevClassTemplate->getTemplateParameters() : 0, |
| 861 | TPC_ClassTemplate)) |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 862 | Invalid = true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 863 | |
Douglas Gregor | 57265e3 | 2010-04-12 16:00:01 +0000 | [diff] [blame] | 864 | if (SS.isSet()) { |
| 865 | // If the name of the template was qualified, we must be defining the |
| 866 | // template out-of-line. |
| 867 | if (!SS.isInvalid() && !Invalid && !PrevClassTemplate && |
| 868 | !(TUK == TUK_Friend && CurContext->isDependentContext())) |
| 869 | Diag(NameLoc, diag::err_member_def_does_not_match) |
| 870 | << Name << SemanticContext << SS.getRange(); |
| 871 | } |
| 872 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 873 | CXXRecordDecl *NewClass = |
Douglas Gregor | 741dd9a | 2009-07-21 14:46:17 +0000 | [diff] [blame] | 874 | CXXRecordDecl::Create(Context, Kind, SemanticContext, NameLoc, Name, KWLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 875 | PrevClassTemplate? |
Douglas Gregor | aafc0cc | 2009-05-15 19:11:46 +0000 | [diff] [blame] | 876 | PrevClassTemplate->getTemplatedDecl() : 0, |
| 877 | /*DelayTypeCreation=*/true); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 878 | SetNestedNameSpecifier(NewClass, SS); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 879 | |
| 880 | ClassTemplateDecl *NewTemplate |
| 881 | = ClassTemplateDecl::Create(Context, SemanticContext, NameLoc, |
| 882 | DeclarationName(Name), TemplateParams, |
Douglas Gregor | 5953d8b | 2009-03-19 17:26:29 +0000 | [diff] [blame] | 883 | NewClass, PrevClassTemplate); |
Douglas Gregor | befc20e | 2009-03-26 00:10:35 +0000 | [diff] [blame] | 884 | NewClass->setDescribedClassTemplate(NewTemplate); |
| 885 | |
Douglas Gregor | aafc0cc | 2009-05-15 19:11:46 +0000 | [diff] [blame] | 886 | // Build the type for the class template declaration now. |
Douglas Gregor | 24bae92 | 2010-07-08 18:37:38 +0000 | [diff] [blame] | 887 | QualType T = NewTemplate->getInjectedClassNameSpecialization(); |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 888 | T = Context.getInjectedClassNameType(NewClass, T); |
Douglas Gregor | aafc0cc | 2009-05-15 19:11:46 +0000 | [diff] [blame] | 889 | assert(T->isDependentType() && "Class template type is not dependent?"); |
| 890 | (void)T; |
| 891 | |
Douglas Gregor | fd056bc | 2009-10-13 16:30:37 +0000 | [diff] [blame] | 892 | // If we are providing an explicit specialization of a member that is a |
| 893 | // class template, make a note of that. |
| 894 | if (PrevClassTemplate && |
| 895 | PrevClassTemplate->getInstantiatedFromMemberTemplate()) |
| 896 | PrevClassTemplate->setMemberSpecialization(); |
| 897 | |
Anders Carlsson | 4cbe82c | 2009-03-26 01:24:28 +0000 | [diff] [blame] | 898 | // Set the access specifier. |
Douglas Gregor | d85bea2 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 899 | if (!Invalid && TUK != TUK_Friend) |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 900 | SetMemberAccessSpecifier(NewTemplate, PrevClassTemplate, AS); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 901 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 902 | // Set the lexical context of these templates |
| 903 | NewClass->setLexicalDeclContext(CurContext); |
| 904 | NewTemplate->setLexicalDeclContext(CurContext); |
| 905 | |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 906 | if (TUK == TUK_Definition) |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 907 | NewClass->startDefinition(); |
| 908 | |
| 909 | if (Attr) |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 910 | ProcessDeclAttributeList(S, NewClass, Attr); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 911 | |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 912 | if (TUK != TUK_Friend) |
| 913 | PushOnScopeChains(NewTemplate, S); |
| 914 | else { |
Douglas Gregor | d85bea2 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 915 | if (PrevClassTemplate && PrevClassTemplate->getAccess() != AS_none) { |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 916 | NewTemplate->setAccess(PrevClassTemplate->getAccess()); |
Douglas Gregor | d85bea2 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 917 | NewClass->setAccess(PrevClassTemplate->getAccess()); |
| 918 | } |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 919 | |
Douglas Gregor | d85bea2 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 920 | NewTemplate->setObjectOfFriendDecl(/* PreviouslyDeclared = */ |
| 921 | PrevClassTemplate != NULL); |
| 922 | |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 923 | // Friend templates are visible in fairly strange ways. |
| 924 | if (!CurContext->isDependentContext()) { |
| 925 | DeclContext *DC = SemanticContext->getLookupContext(); |
| 926 | DC->makeDeclVisibleInContext(NewTemplate, /* Recoverable = */ false); |
| 927 | if (Scope *EnclosingScope = getScopeForDeclContext(S, DC)) |
| 928 | PushOnScopeChains(NewTemplate, EnclosingScope, |
| 929 | /* AddToContext = */ false); |
| 930 | } |
Douglas Gregor | d85bea2 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 931 | |
| 932 | FriendDecl *Friend = FriendDecl::Create(Context, CurContext, |
| 933 | NewClass->getLocation(), |
| 934 | NewTemplate, |
| 935 | /*FIXME:*/NewClass->getLocation()); |
| 936 | Friend->setAccess(AS_public); |
| 937 | CurContext->addDecl(Friend); |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 938 | } |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 939 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 940 | if (Invalid) { |
| 941 | NewTemplate->setInvalidDecl(); |
| 942 | NewClass->setInvalidDecl(); |
| 943 | } |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 944 | return DeclPtrTy::make(NewTemplate); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 945 | } |
| 946 | |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 947 | /// \brief Diagnose the presence of a default template argument on a |
| 948 | /// template parameter, which is ill-formed in certain contexts. |
| 949 | /// |
| 950 | /// \returns true if the default template argument should be dropped. |
| 951 | static bool DiagnoseDefaultTemplateArgument(Sema &S, |
| 952 | Sema::TemplateParamListContext TPC, |
| 953 | SourceLocation ParamLoc, |
| 954 | SourceRange DefArgRange) { |
| 955 | switch (TPC) { |
| 956 | case Sema::TPC_ClassTemplate: |
| 957 | return false; |
| 958 | |
| 959 | case Sema::TPC_FunctionTemplate: |
| 960 | // C++ [temp.param]p9: |
| 961 | // A default template-argument shall not be specified in a |
| 962 | // function template declaration or a function template |
| 963 | // definition [...] |
| 964 | // (This sentence is not in C++0x, per DR226). |
| 965 | if (!S.getLangOptions().CPlusPlus0x) |
| 966 | S.Diag(ParamLoc, |
| 967 | diag::err_template_parameter_default_in_function_template) |
| 968 | << DefArgRange; |
| 969 | return false; |
| 970 | |
| 971 | case Sema::TPC_ClassTemplateMember: |
| 972 | // C++0x [temp.param]p9: |
| 973 | // A default template-argument shall not be specified in the |
| 974 | // template-parameter-lists of the definition of a member of a |
| 975 | // class template that appears outside of the member's class. |
| 976 | S.Diag(ParamLoc, diag::err_template_parameter_default_template_member) |
| 977 | << DefArgRange; |
| 978 | return true; |
| 979 | |
| 980 | case Sema::TPC_FriendFunctionTemplate: |
| 981 | // C++ [temp.param]p9: |
| 982 | // A default template-argument shall not be specified in a |
| 983 | // friend template declaration. |
| 984 | S.Diag(ParamLoc, diag::err_template_parameter_default_friend_template) |
| 985 | << DefArgRange; |
| 986 | return true; |
| 987 | |
| 988 | // FIXME: C++0x [temp.param]p9 allows default template-arguments |
| 989 | // for friend function templates if there is only a single |
| 990 | // declaration (and it is a definition). Strange! |
| 991 | } |
| 992 | |
| 993 | return false; |
| 994 | } |
| 995 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 996 | /// \brief Checks the validity of a template parameter list, possibly |
| 997 | /// considering the template parameter list from a previous |
| 998 | /// declaration. |
| 999 | /// |
| 1000 | /// If an "old" template parameter list is provided, it must be |
| 1001 | /// equivalent (per TemplateParameterListsAreEqual) to the "new" |
| 1002 | /// template parameter list. |
| 1003 | /// |
| 1004 | /// \param NewParams Template parameter list for a new template |
| 1005 | /// declaration. This template parameter list will be updated with any |
| 1006 | /// default arguments that are carried through from the previous |
| 1007 | /// template parameter list. |
| 1008 | /// |
| 1009 | /// \param OldParams If provided, template parameter list from a |
| 1010 | /// previous declaration of the same template. Default template |
| 1011 | /// arguments will be merged from the old template parameter list to |
| 1012 | /// the new template parameter list. |
| 1013 | /// |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1014 | /// \param TPC Describes the context in which we are checking the given |
| 1015 | /// template parameter list. |
| 1016 | /// |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1017 | /// \returns true if an error occurred, false otherwise. |
| 1018 | bool Sema::CheckTemplateParameterList(TemplateParameterList *NewParams, |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1019 | TemplateParameterList *OldParams, |
| 1020 | TemplateParamListContext TPC) { |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1021 | bool Invalid = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1022 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1023 | // C++ [temp.param]p10: |
| 1024 | // The set of default template-arguments available for use with a |
| 1025 | // template declaration or definition is obtained by merging the |
| 1026 | // default arguments from the definition (if in scope) and all |
| 1027 | // declarations in scope in the same way default function |
| 1028 | // arguments are (8.3.6). |
| 1029 | bool SawDefaultArgument = false; |
| 1030 | SourceLocation PreviousDefaultArgLoc; |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 1031 | |
Anders Carlsson | 49d2557 | 2009-06-12 23:20:15 +0000 | [diff] [blame] | 1032 | bool SawParameterPack = false; |
| 1033 | SourceLocation ParameterPackLoc; |
| 1034 | |
Mike Stump | 1a35fde | 2009-02-11 23:03:27 +0000 | [diff] [blame] | 1035 | // Dummy initialization to avoid warnings. |
Douglas Gregor | 1bc6913 | 2009-02-11 20:46:19 +0000 | [diff] [blame] | 1036 | TemplateParameterList::iterator OldParam = NewParams->end(); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1037 | if (OldParams) |
| 1038 | OldParam = OldParams->begin(); |
| 1039 | |
| 1040 | for (TemplateParameterList::iterator NewParam = NewParams->begin(), |
| 1041 | NewParamEnd = NewParams->end(); |
| 1042 | NewParam != NewParamEnd; ++NewParam) { |
| 1043 | // Variables used to diagnose redundant default arguments |
| 1044 | bool RedundantDefaultArg = false; |
| 1045 | SourceLocation OldDefaultLoc; |
| 1046 | SourceLocation NewDefaultLoc; |
| 1047 | |
| 1048 | // Variables used to diagnose missing default arguments |
| 1049 | bool MissingDefaultArg = false; |
| 1050 | |
Anders Carlsson | 49d2557 | 2009-06-12 23:20:15 +0000 | [diff] [blame] | 1051 | // C++0x [temp.param]p11: |
| 1052 | // If a template parameter of a class template is a template parameter pack, |
| 1053 | // it must be the last template parameter. |
| 1054 | if (SawParameterPack) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1055 | Diag(ParameterPackLoc, |
Anders Carlsson | 49d2557 | 2009-06-12 23:20:15 +0000 | [diff] [blame] | 1056 | diag::err_template_param_pack_must_be_last_template_parameter); |
| 1057 | Invalid = true; |
| 1058 | } |
| 1059 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1060 | if (TemplateTypeParmDecl *NewTypeParm |
| 1061 | = dyn_cast<TemplateTypeParmDecl>(*NewParam)) { |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1062 | // Check the presence of a default argument here. |
| 1063 | if (NewTypeParm->hasDefaultArgument() && |
| 1064 | DiagnoseDefaultTemplateArgument(*this, TPC, |
| 1065 | NewTypeParm->getLocation(), |
| 1066 | NewTypeParm->getDefaultArgumentInfo()->getTypeLoc() |
Abramo Bagnara | bd054db | 2010-05-20 10:00:11 +0000 | [diff] [blame] | 1067 | .getSourceRange())) |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1068 | NewTypeParm->removeDefaultArgument(); |
| 1069 | |
| 1070 | // Merge default arguments for template type parameters. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1071 | TemplateTypeParmDecl *OldTypeParm |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1072 | = OldParams? cast<TemplateTypeParmDecl>(*OldParam) : 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1073 | |
Anders Carlsson | 49d2557 | 2009-06-12 23:20:15 +0000 | [diff] [blame] | 1074 | if (NewTypeParm->isParameterPack()) { |
| 1075 | assert(!NewTypeParm->hasDefaultArgument() && |
| 1076 | "Parameter packs can't have a default argument!"); |
| 1077 | SawParameterPack = true; |
| 1078 | ParameterPackLoc = NewTypeParm->getLocation(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1079 | } else if (OldTypeParm && OldTypeParm->hasDefaultArgument() && |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1080 | NewTypeParm->hasDefaultArgument()) { |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1081 | OldDefaultLoc = OldTypeParm->getDefaultArgumentLoc(); |
| 1082 | NewDefaultLoc = NewTypeParm->getDefaultArgumentLoc(); |
| 1083 | SawDefaultArgument = true; |
| 1084 | RedundantDefaultArg = true; |
| 1085 | PreviousDefaultArgLoc = NewDefaultLoc; |
| 1086 | } else if (OldTypeParm && OldTypeParm->hasDefaultArgument()) { |
| 1087 | // Merge the default argument from the old declaration to the |
| 1088 | // new declaration. |
| 1089 | SawDefaultArgument = true; |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1090 | NewTypeParm->setDefaultArgument(OldTypeParm->getDefaultArgumentInfo(), |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1091 | true); |
| 1092 | PreviousDefaultArgLoc = OldTypeParm->getDefaultArgumentLoc(); |
| 1093 | } else if (NewTypeParm->hasDefaultArgument()) { |
| 1094 | SawDefaultArgument = true; |
| 1095 | PreviousDefaultArgLoc = NewTypeParm->getDefaultArgumentLoc(); |
| 1096 | } else if (SawDefaultArgument) |
| 1097 | MissingDefaultArg = true; |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1098 | } else if (NonTypeTemplateParmDecl *NewNonTypeParm |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1099 | = dyn_cast<NonTypeTemplateParmDecl>(*NewParam)) { |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1100 | // Check the presence of a default argument here. |
| 1101 | if (NewNonTypeParm->hasDefaultArgument() && |
| 1102 | DiagnoseDefaultTemplateArgument(*this, TPC, |
| 1103 | NewNonTypeParm->getLocation(), |
| 1104 | NewNonTypeParm->getDefaultArgument()->getSourceRange())) { |
| 1105 | NewNonTypeParm->getDefaultArgument()->Destroy(Context); |
Abramo Bagnara | d92f7a2 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 1106 | NewNonTypeParm->removeDefaultArgument(); |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1107 | } |
| 1108 | |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1109 | // Merge default arguments for non-type template parameters |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1110 | NonTypeTemplateParmDecl *OldNonTypeParm |
| 1111 | = OldParams? cast<NonTypeTemplateParmDecl>(*OldParam) : 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1112 | if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument() && |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1113 | NewNonTypeParm->hasDefaultArgument()) { |
| 1114 | OldDefaultLoc = OldNonTypeParm->getDefaultArgumentLoc(); |
| 1115 | NewDefaultLoc = NewNonTypeParm->getDefaultArgumentLoc(); |
| 1116 | SawDefaultArgument = true; |
| 1117 | RedundantDefaultArg = true; |
| 1118 | PreviousDefaultArgLoc = NewDefaultLoc; |
| 1119 | } else if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument()) { |
| 1120 | // Merge the default argument from the old declaration to the |
| 1121 | // new declaration. |
| 1122 | SawDefaultArgument = true; |
| 1123 | // FIXME: We need to create a new kind of "default argument" |
| 1124 | // expression that points to a previous template template |
| 1125 | // parameter. |
| 1126 | NewNonTypeParm->setDefaultArgument( |
Abramo Bagnara | d92f7a2 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 1127 | OldNonTypeParm->getDefaultArgument(), |
| 1128 | /*Inherited=*/ true); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1129 | PreviousDefaultArgLoc = OldNonTypeParm->getDefaultArgumentLoc(); |
| 1130 | } else if (NewNonTypeParm->hasDefaultArgument()) { |
| 1131 | SawDefaultArgument = true; |
| 1132 | PreviousDefaultArgLoc = NewNonTypeParm->getDefaultArgumentLoc(); |
| 1133 | } else if (SawDefaultArgument) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1134 | MissingDefaultArg = true; |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1135 | } else { |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1136 | // Check the presence of a default argument here. |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1137 | TemplateTemplateParmDecl *NewTemplateParm |
| 1138 | = cast<TemplateTemplateParmDecl>(*NewParam); |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1139 | if (NewTemplateParm->hasDefaultArgument() && |
| 1140 | DiagnoseDefaultTemplateArgument(*this, TPC, |
| 1141 | NewTemplateParm->getLocation(), |
| 1142 | NewTemplateParm->getDefaultArgument().getSourceRange())) |
Abramo Bagnara | d92f7a2 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 1143 | NewTemplateParm->removeDefaultArgument(); |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1144 | |
| 1145 | // Merge default arguments for template template parameters |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1146 | TemplateTemplateParmDecl *OldTemplateParm |
| 1147 | = OldParams? cast<TemplateTemplateParmDecl>(*OldParam) : 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1148 | if (OldTemplateParm && OldTemplateParm->hasDefaultArgument() && |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1149 | NewTemplateParm->hasDefaultArgument()) { |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1150 | OldDefaultLoc = OldTemplateParm->getDefaultArgument().getLocation(); |
| 1151 | NewDefaultLoc = NewTemplateParm->getDefaultArgument().getLocation(); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1152 | SawDefaultArgument = true; |
| 1153 | RedundantDefaultArg = true; |
| 1154 | PreviousDefaultArgLoc = NewDefaultLoc; |
| 1155 | } else if (OldTemplateParm && OldTemplateParm->hasDefaultArgument()) { |
| 1156 | // Merge the default argument from the old declaration to the |
| 1157 | // new declaration. |
| 1158 | SawDefaultArgument = true; |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 1159 | // FIXME: We need to create a new kind of "default argument" expression |
| 1160 | // that points to a previous template template parameter. |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1161 | NewTemplateParm->setDefaultArgument( |
Abramo Bagnara | d92f7a2 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 1162 | OldTemplateParm->getDefaultArgument(), |
| 1163 | /*Inherited=*/ true); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1164 | PreviousDefaultArgLoc |
| 1165 | = OldTemplateParm->getDefaultArgument().getLocation(); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1166 | } else if (NewTemplateParm->hasDefaultArgument()) { |
| 1167 | SawDefaultArgument = true; |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1168 | PreviousDefaultArgLoc |
| 1169 | = NewTemplateParm->getDefaultArgument().getLocation(); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1170 | } else if (SawDefaultArgument) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1171 | MissingDefaultArg = true; |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1172 | } |
| 1173 | |
| 1174 | if (RedundantDefaultArg) { |
| 1175 | // C++ [temp.param]p12: |
| 1176 | // A template-parameter shall not be given default arguments |
| 1177 | // by two different declarations in the same scope. |
| 1178 | Diag(NewDefaultLoc, diag::err_template_param_default_arg_redefinition); |
| 1179 | Diag(OldDefaultLoc, diag::note_template_param_prev_default_arg); |
| 1180 | Invalid = true; |
| 1181 | } else if (MissingDefaultArg) { |
| 1182 | // C++ [temp.param]p11: |
| 1183 | // If a template-parameter has a default template-argument, |
| 1184 | // all subsequent template-parameters shall have a default |
| 1185 | // template-argument supplied. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1186 | Diag((*NewParam)->getLocation(), |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1187 | diag::err_template_param_default_arg_missing); |
| 1188 | Diag(PreviousDefaultArgLoc, diag::note_template_param_prev_default_arg); |
| 1189 | Invalid = true; |
| 1190 | } |
| 1191 | |
| 1192 | // If we have an old template parameter list that we're merging |
| 1193 | // in, move on to the next parameter. |
| 1194 | if (OldParams) |
| 1195 | ++OldParam; |
| 1196 | } |
| 1197 | |
| 1198 | return Invalid; |
| 1199 | } |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 1200 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1201 | /// \brief Match the given template parameter lists to the given scope |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1202 | /// specifier, returning the template parameter list that applies to the |
| 1203 | /// name. |
| 1204 | /// |
| 1205 | /// \param DeclStartLoc the start of the declaration that has a scope |
| 1206 | /// specifier or a template parameter list. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1207 | /// |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1208 | /// \param SS the scope specifier that will be matched to the given template |
| 1209 | /// parameter lists. This scope specifier precedes a qualified name that is |
| 1210 | /// being declared. |
| 1211 | /// |
| 1212 | /// \param ParamLists the template parameter lists, from the outermost to the |
| 1213 | /// innermost template parameter lists. |
| 1214 | /// |
| 1215 | /// \param NumParamLists the number of template parameter lists in ParamLists. |
| 1216 | /// |
John McCall | 77e8b11 | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 1217 | /// \param IsFriend Whether to apply the slightly different rules for |
| 1218 | /// matching template parameters to scope specifiers in friend |
| 1219 | /// declarations. |
| 1220 | /// |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 1221 | /// \param IsExplicitSpecialization will be set true if the entity being |
| 1222 | /// declared is an explicit specialization, false otherwise. |
| 1223 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1224 | /// \returns the template parameter list, if any, that corresponds to the |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1225 | /// name that is preceded by the scope specifier @p SS. This template |
| 1226 | /// parameter list may be have template parameters (if we're declaring a |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1227 | /// template) or may have no template parameters (if we're declaring a |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1228 | /// template specialization), or may be NULL (if we were's declaring isn't |
| 1229 | /// itself a template). |
| 1230 | TemplateParameterList * |
| 1231 | Sema::MatchTemplateParametersToScopeSpecifier(SourceLocation DeclStartLoc, |
| 1232 | const CXXScopeSpec &SS, |
| 1233 | TemplateParameterList **ParamLists, |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 1234 | unsigned NumParamLists, |
John McCall | 77e8b11 | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 1235 | bool IsFriend, |
Douglas Gregor | 0167f3c | 2010-07-14 23:14:12 +0000 | [diff] [blame] | 1236 | bool &IsExplicitSpecialization, |
| 1237 | bool &Invalid) { |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 1238 | IsExplicitSpecialization = false; |
| 1239 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1240 | // Find the template-ids that occur within the nested-name-specifier. These |
| 1241 | // template-ids will match up with the template parameter lists. |
| 1242 | llvm::SmallVector<const TemplateSpecializationType *, 4> |
| 1243 | TemplateIdsInSpecifier; |
Douglas Gregor | 3ebd753 | 2009-11-23 12:11:45 +0000 | [diff] [blame] | 1244 | llvm::SmallVector<ClassTemplateSpecializationDecl *, 4> |
| 1245 | ExplicitSpecializationsInSpecifier; |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1246 | for (NestedNameSpecifier *NNS = (NestedNameSpecifier *)SS.getScopeRep(); |
| 1247 | NNS; NNS = NNS->getPrefix()) { |
John McCall | 4b2b02b | 2009-12-15 02:19:47 +0000 | [diff] [blame] | 1248 | const Type *T = NNS->getAsType(); |
| 1249 | if (!T) break; |
| 1250 | |
| 1251 | // C++0x [temp.expl.spec]p17: |
| 1252 | // A member or a member template may be nested within many |
| 1253 | // enclosing class templates. In an explicit specialization for |
| 1254 | // such a member, the member declaration shall be preceded by a |
| 1255 | // template<> for each enclosing class template that is |
| 1256 | // explicitly specialized. |
Douglas Gregor | fe33106 | 2010-02-13 05:23:25 +0000 | [diff] [blame] | 1257 | // |
| 1258 | // Following the existing practice of GNU and EDG, we allow a typedef of a |
| 1259 | // template specialization type. |
| 1260 | if (const TypedefType *TT = dyn_cast<TypedefType>(T)) |
| 1261 | T = TT->LookThroughTypedefs().getTypePtr(); |
John McCall | 4b2b02b | 2009-12-15 02:19:47 +0000 | [diff] [blame] | 1262 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1263 | if (const TemplateSpecializationType *SpecType |
Douglas Gregor | fe33106 | 2010-02-13 05:23:25 +0000 | [diff] [blame] | 1264 | = dyn_cast<TemplateSpecializationType>(T)) { |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1265 | TemplateDecl *Template = SpecType->getTemplateName().getAsTemplateDecl(); |
| 1266 | if (!Template) |
| 1267 | continue; // FIXME: should this be an error? probably... |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1268 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1269 | if (const RecordType *Record = SpecType->getAs<RecordType>()) { |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1270 | ClassTemplateSpecializationDecl *SpecDecl |
| 1271 | = cast<ClassTemplateSpecializationDecl>(Record->getDecl()); |
| 1272 | // If the nested name specifier refers to an explicit specialization, |
| 1273 | // we don't need a template<> header. |
Douglas Gregor | 3ebd753 | 2009-11-23 12:11:45 +0000 | [diff] [blame] | 1274 | if (SpecDecl->getSpecializationKind() == TSK_ExplicitSpecialization) { |
| 1275 | ExplicitSpecializationsInSpecifier.push_back(SpecDecl); |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1276 | continue; |
Douglas Gregor | 3ebd753 | 2009-11-23 12:11:45 +0000 | [diff] [blame] | 1277 | } |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1278 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1279 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1280 | TemplateIdsInSpecifier.push_back(SpecType); |
| 1281 | } |
| 1282 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1283 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1284 | // Reverse the list of template-ids in the scope specifier, so that we can |
| 1285 | // more easily match up the template-ids and the template parameter lists. |
| 1286 | std::reverse(TemplateIdsInSpecifier.begin(), TemplateIdsInSpecifier.end()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1287 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1288 | SourceLocation FirstTemplateLoc = DeclStartLoc; |
| 1289 | if (NumParamLists) |
| 1290 | FirstTemplateLoc = ParamLists[0]->getTemplateLoc(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1291 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1292 | // Match the template-ids found in the specifier to the template parameter |
| 1293 | // lists. |
| 1294 | unsigned Idx = 0; |
| 1295 | for (unsigned NumTemplateIds = TemplateIdsInSpecifier.size(); |
| 1296 | Idx != NumTemplateIds; ++Idx) { |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 1297 | QualType TemplateId = QualType(TemplateIdsInSpecifier[Idx], 0); |
| 1298 | bool DependentTemplateId = TemplateId->isDependentType(); |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1299 | if (Idx >= NumParamLists) { |
| 1300 | // We have a template-id without a corresponding template parameter |
| 1301 | // list. |
John McCall | 77e8b11 | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 1302 | |
| 1303 | // ...which is fine if this is a friend declaration. |
| 1304 | if (IsFriend) { |
| 1305 | IsExplicitSpecialization = true; |
| 1306 | break; |
| 1307 | } |
| 1308 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1309 | if (DependentTemplateId) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1310 | // FIXME: the location information here isn't great. |
| 1311 | Diag(SS.getRange().getBegin(), |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1312 | diag::err_template_spec_needs_template_parameters) |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 1313 | << TemplateId |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1314 | << SS.getRange(); |
Douglas Gregor | 0167f3c | 2010-07-14 23:14:12 +0000 | [diff] [blame] | 1315 | Invalid = true; |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1316 | } else { |
| 1317 | Diag(SS.getRange().getBegin(), diag::err_template_spec_needs_header) |
| 1318 | << SS.getRange() |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 1319 | << FixItHint::CreateInsertion(FirstTemplateLoc, "template<> "); |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 1320 | IsExplicitSpecialization = true; |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1321 | } |
| 1322 | return 0; |
| 1323 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1324 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1325 | // Check the template parameter list against its corresponding template-id. |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 1326 | if (DependentTemplateId) { |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 1327 | TemplateParameterList *ExpectedTemplateParams = 0; |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 1328 | |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 1329 | // Are there cases in (e.g.) friends where this won't match? |
| 1330 | if (const InjectedClassNameType *Injected |
| 1331 | = TemplateId->getAs<InjectedClassNameType>()) { |
| 1332 | CXXRecordDecl *Record = Injected->getDecl(); |
| 1333 | if (ClassTemplatePartialSpecializationDecl *Partial = |
| 1334 | dyn_cast<ClassTemplatePartialSpecializationDecl>(Record)) |
| 1335 | ExpectedTemplateParams = Partial->getTemplateParameters(); |
| 1336 | else |
| 1337 | ExpectedTemplateParams = Record->getDescribedClassTemplate() |
| 1338 | ->getTemplateParameters(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1339 | } |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1340 | |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 1341 | if (ExpectedTemplateParams) |
| 1342 | TemplateParameterListsAreEqual(ParamLists[Idx], |
| 1343 | ExpectedTemplateParams, |
| 1344 | true, TPL_TemplateMatch); |
| 1345 | |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1346 | CheckTemplateParameterList(ParamLists[Idx], 0, TPC_ClassTemplateMember); |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 1347 | } else if (ParamLists[Idx]->size() > 0) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1348 | Diag(ParamLists[Idx]->getTemplateLoc(), |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 1349 | diag::err_template_param_list_matches_nontemplate) |
| 1350 | << TemplateId |
| 1351 | << ParamLists[Idx]->getSourceRange(); |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 1352 | else |
| 1353 | IsExplicitSpecialization = true; |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1354 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1355 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1356 | // If there were at least as many template-ids as there were template |
| 1357 | // parameter lists, then there are no template parameter lists remaining for |
| 1358 | // the declaration itself. |
| 1359 | if (Idx >= NumParamLists) |
| 1360 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1361 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1362 | // If there were too many template parameter lists, complain about that now. |
| 1363 | if (Idx != NumParamLists - 1) { |
| 1364 | while (Idx < NumParamLists - 1) { |
Douglas Gregor | 3ebd753 | 2009-11-23 12:11:45 +0000 | [diff] [blame] | 1365 | bool isExplicitSpecHeader = ParamLists[Idx]->size() == 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1366 | Diag(ParamLists[Idx]->getTemplateLoc(), |
Douglas Gregor | 3ebd753 | 2009-11-23 12:11:45 +0000 | [diff] [blame] | 1367 | isExplicitSpecHeader? diag::warn_template_spec_extra_headers |
| 1368 | : diag::err_template_spec_extra_headers) |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1369 | << SourceRange(ParamLists[Idx]->getTemplateLoc(), |
| 1370 | ParamLists[Idx]->getRAngleLoc()); |
Douglas Gregor | 3ebd753 | 2009-11-23 12:11:45 +0000 | [diff] [blame] | 1371 | |
| 1372 | if (isExplicitSpecHeader && !ExplicitSpecializationsInSpecifier.empty()) { |
| 1373 | Diag(ExplicitSpecializationsInSpecifier.back()->getLocation(), |
| 1374 | diag::note_explicit_template_spec_does_not_need_header) |
| 1375 | << ExplicitSpecializationsInSpecifier.back(); |
| 1376 | ExplicitSpecializationsInSpecifier.pop_back(); |
| 1377 | } |
Douglas Gregor | 0167f3c | 2010-07-14 23:14:12 +0000 | [diff] [blame] | 1378 | |
| 1379 | // We have a template parameter list with no corresponding scope, which |
| 1380 | // means that the resulting template declaration can't be instantiated |
| 1381 | // properly (we'll end up with dependent nodes when we shouldn't). |
| 1382 | if (!isExplicitSpecHeader) |
| 1383 | Invalid = true; |
| 1384 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1385 | ++Idx; |
| 1386 | } |
| 1387 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1388 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1389 | // Return the last template parameter list, which corresponds to the |
| 1390 | // entity being declared. |
| 1391 | return ParamLists[NumParamLists - 1]; |
| 1392 | } |
| 1393 | |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1394 | QualType Sema::CheckTemplateIdType(TemplateName Name, |
| 1395 | SourceLocation TemplateLoc, |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1396 | const TemplateArgumentListInfo &TemplateArgs) { |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1397 | TemplateDecl *Template = Name.getAsTemplateDecl(); |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 1398 | if (!Template) { |
| 1399 | // The template name does not resolve to a template, so we just |
| 1400 | // build a dependent template-id type. |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1401 | return Context.getTemplateSpecializationType(Name, TemplateArgs); |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 1402 | } |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1403 | |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1404 | // Check that the template argument list is well-formed for this |
| 1405 | // template. |
Anders Carlsson | fb25052 | 2009-06-23 01:26:57 +0000 | [diff] [blame] | 1406 | TemplateArgumentListBuilder Converted(Template->getTemplateParameters(), |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1407 | TemplateArgs.size()); |
| 1408 | if (CheckTemplateArgumentList(Template, TemplateLoc, TemplateArgs, |
Douglas Gregor | 16134c6 | 2009-07-01 00:28:38 +0000 | [diff] [blame] | 1409 | false, Converted)) |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1410 | return QualType(); |
| 1411 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1412 | assert((Converted.structuredSize() == |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1413 | Template->getTemplateParameters()->size()) && |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1414 | "Converted template argument list is too short!"); |
| 1415 | |
| 1416 | QualType CanonType; |
| 1417 | |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 1418 | if (Name.isDependent() || |
| 1419 | TemplateSpecializationType::anyDependentTemplateArguments( |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1420 | TemplateArgs)) { |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1421 | // This class template specialization is a dependent |
| 1422 | // type. Therefore, its canonical type is another class template |
| 1423 | // specialization type that contains all of the converted |
| 1424 | // arguments in canonical form. This ensures that, e.g., A<T> and |
| 1425 | // A<T, T> have identical types when A is declared as: |
| 1426 | // |
| 1427 | // template<typename T, typename U = T> struct A; |
Douglas Gregor | 25a3ef7 | 2009-05-07 06:41:52 +0000 | [diff] [blame] | 1428 | TemplateName CanonName = Context.getCanonicalTemplateName(Name); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1429 | CanonType = Context.getTemplateSpecializationType(CanonName, |
Anders Carlsson | fb25052 | 2009-06-23 01:26:57 +0000 | [diff] [blame] | 1430 | Converted.getFlatArguments(), |
| 1431 | Converted.flatSize()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1432 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 1433 | // FIXME: CanonType is not actually the canonical type, and unfortunately |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1434 | // it is a TemplateSpecializationType that we will never use again. |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 1435 | // In the future, we need to teach getTemplateSpecializationType to only |
| 1436 | // build the canonical type and return that to us. |
| 1437 | CanonType = Context.getCanonicalType(CanonType); |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 1438 | |
| 1439 | // This might work out to be a current instantiation, in which |
| 1440 | // case the canonical type needs to be the InjectedClassNameType. |
| 1441 | // |
| 1442 | // TODO: in theory this could be a simple hashtable lookup; most |
| 1443 | // changes to CurContext don't change the set of current |
| 1444 | // instantiations. |
| 1445 | if (isa<ClassTemplateDecl>(Template)) { |
| 1446 | for (DeclContext *Ctx = CurContext; Ctx; Ctx = Ctx->getLookupParent()) { |
| 1447 | // If we get out to a namespace, we're done. |
| 1448 | if (Ctx->isFileContext()) break; |
| 1449 | |
| 1450 | // If this isn't a record, keep looking. |
| 1451 | CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Ctx); |
| 1452 | if (!Record) continue; |
| 1453 | |
| 1454 | // Look for one of the two cases with InjectedClassNameTypes |
| 1455 | // and check whether it's the same template. |
| 1456 | if (!isa<ClassTemplatePartialSpecializationDecl>(Record) && |
| 1457 | !Record->getDescribedClassTemplate()) |
| 1458 | continue; |
| 1459 | |
| 1460 | // Fetch the injected class name type and check whether its |
| 1461 | // injected type is equal to the type we just built. |
| 1462 | QualType ICNT = Context.getTypeDeclType(Record); |
| 1463 | QualType Injected = cast<InjectedClassNameType>(ICNT) |
| 1464 | ->getInjectedSpecializationType(); |
| 1465 | |
| 1466 | if (CanonType != Injected->getCanonicalTypeInternal()) |
| 1467 | continue; |
| 1468 | |
| 1469 | // If so, the canonical type of this TST is the injected |
| 1470 | // class name type of the record we just found. |
| 1471 | assert(ICNT.isCanonical()); |
| 1472 | CanonType = ICNT; |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 1473 | break; |
| 1474 | } |
| 1475 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1476 | } else if (ClassTemplateDecl *ClassTemplate |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1477 | = dyn_cast<ClassTemplateDecl>(Template)) { |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1478 | // Find the class template specialization declaration that |
| 1479 | // corresponds to these arguments. |
| 1480 | llvm::FoldingSetNodeID ID; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1481 | ClassTemplateSpecializationDecl::Profile(ID, |
Anders Carlsson | fb25052 | 2009-06-23 01:26:57 +0000 | [diff] [blame] | 1482 | Converted.getFlatArguments(), |
Douglas Gregor | 828e226 | 2009-07-29 16:09:57 +0000 | [diff] [blame] | 1483 | Converted.flatSize(), |
| 1484 | Context); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1485 | void *InsertPos = 0; |
| 1486 | ClassTemplateSpecializationDecl *Decl |
| 1487 | = ClassTemplate->getSpecializations().FindNodeOrInsertPos(ID, InsertPos); |
| 1488 | if (!Decl) { |
| 1489 | // This is the first time we have referenced this class template |
| 1490 | // specialization. Create the canonical declaration and add it to |
| 1491 | // the set of specializations. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1492 | Decl = ClassTemplateSpecializationDecl::Create(Context, |
Douglas Gregor | 13c8577 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 1493 | ClassTemplate->getTemplatedDecl()->getTagKind(), |
| 1494 | ClassTemplate->getDeclContext(), |
| 1495 | ClassTemplate->getLocation(), |
| 1496 | ClassTemplate, |
| 1497 | Converted, 0); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1498 | ClassTemplate->getSpecializations().InsertNode(Decl, InsertPos); |
| 1499 | Decl->setLexicalDeclContext(CurContext); |
| 1500 | } |
| 1501 | |
| 1502 | CanonType = Context.getTypeDeclType(Decl); |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 1503 | assert(isa<RecordType>(CanonType) && |
| 1504 | "type of non-dependent specialization is not a RecordType"); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1505 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1506 | |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1507 | // Build the fully-sugared type for this class template |
| 1508 | // specialization, which refers back to the class template |
| 1509 | // specialization we created or found. |
John McCall | 71d74bc | 2010-06-13 09:25:03 +0000 | [diff] [blame] | 1510 | return Context.getTemplateSpecializationType(Name, TemplateArgs, CanonType); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1511 | } |
| 1512 | |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 1513 | Action::TypeResult |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1514 | Sema::ActOnTemplateIdType(TemplateTy TemplateD, SourceLocation TemplateLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1515 | SourceLocation LAngleLoc, |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1516 | ASTTemplateArgsPtr TemplateArgsIn, |
John McCall | 6b2becf | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 1517 | SourceLocation RAngleLoc) { |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1518 | TemplateName Template = TemplateD.getAsVal<TemplateName>(); |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 1519 | |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1520 | // Translate the parser's template argument list in our AST format. |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1521 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
Douglas Gregor | 314b97f | 2009-11-10 19:49:08 +0000 | [diff] [blame] | 1522 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 1523 | |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1524 | QualType Result = CheckTemplateIdType(Template, TemplateLoc, TemplateArgs); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1525 | TemplateArgsIn.release(); |
Douglas Gregor | 31a19b6 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 1526 | |
| 1527 | if (Result.isNull()) |
| 1528 | return true; |
| 1529 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1530 | TypeSourceInfo *DI = Context.CreateTypeSourceInfo(Result); |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1531 | TemplateSpecializationTypeLoc TL |
| 1532 | = cast<TemplateSpecializationTypeLoc>(DI->getTypeLoc()); |
| 1533 | TL.setTemplateNameLoc(TemplateLoc); |
| 1534 | TL.setLAngleLoc(LAngleLoc); |
| 1535 | TL.setRAngleLoc(RAngleLoc); |
| 1536 | for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) |
| 1537 | TL.setArgLocInfo(i, TemplateArgs[i].getLocInfo()); |
| 1538 | |
| 1539 | return CreateLocInfoType(Result, DI).getAsOpaquePtr(); |
John McCall | 6b2becf | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 1540 | } |
John McCall | f1bbbb4 | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 1541 | |
John McCall | 6b2becf | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 1542 | Sema::TypeResult Sema::ActOnTagTemplateIdType(TypeResult TypeResult, |
| 1543 | TagUseKind TUK, |
| 1544 | DeclSpec::TST TagSpec, |
| 1545 | SourceLocation TagLoc) { |
| 1546 | if (TypeResult.isInvalid()) |
| 1547 | return Sema::TypeResult(); |
John McCall | f1bbbb4 | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 1548 | |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1549 | // FIXME: preserve source info, ideally without copying the DI. |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1550 | TypeSourceInfo *DI; |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1551 | QualType Type = GetTypeFromParser(TypeResult.get(), &DI); |
John McCall | f1bbbb4 | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 1552 | |
John McCall | 6b2becf | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 1553 | // Verify the tag specifier. |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 1554 | TagTypeKind TagKind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1555 | |
John McCall | 6b2becf | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 1556 | if (const RecordType *RT = Type->getAs<RecordType>()) { |
| 1557 | RecordDecl *D = RT->getDecl(); |
| 1558 | |
| 1559 | IdentifierInfo *Id = D->getIdentifier(); |
| 1560 | assert(Id && "templated class must have an identifier"); |
| 1561 | |
| 1562 | if (!isAcceptableTagRedeclaration(D, TagKind, TagLoc, *Id)) { |
| 1563 | Diag(TagLoc, diag::err_use_with_wrong_tag) |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 1564 | << Type |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 1565 | << FixItHint::CreateReplacement(SourceRange(TagLoc), D->getKindName()); |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 1566 | Diag(D->getLocation(), diag::note_previous_use); |
John McCall | f1bbbb4 | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 1567 | } |
| 1568 | } |
| 1569 | |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 1570 | ElaboratedTypeKeyword Keyword |
| 1571 | = TypeWithKeyword::getKeywordForTagTypeKind(TagKind); |
| 1572 | QualType ElabType = Context.getElaboratedType(Keyword, /*NNS=*/0, Type); |
John McCall | 6b2becf | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 1573 | |
| 1574 | return ElabType.getAsOpaquePtr(); |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 1575 | } |
| 1576 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1577 | Sema::OwningExprResult Sema::BuildTemplateIdExpr(const CXXScopeSpec &SS, |
| 1578 | LookupResult &R, |
| 1579 | bool RequiresADL, |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1580 | const TemplateArgumentListInfo &TemplateArgs) { |
Douglas Gregor | edce4dd | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 1581 | // FIXME: Can we do any checking at this point? I guess we could check the |
| 1582 | // template arguments that we have against the template name, if the template |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1583 | // 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] | 1584 | // though. |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1585 | |
| 1586 | // These should be filtered out by our callers. |
| 1587 | assert(!R.empty() && "empty lookup results when building templateid"); |
| 1588 | assert(!R.isAmbiguous() && "ambiguous lookup when building templateid"); |
| 1589 | |
| 1590 | NestedNameSpecifier *Qualifier = 0; |
| 1591 | SourceRange QualifierRange; |
| 1592 | if (SS.isSet()) { |
| 1593 | Qualifier = static_cast<NestedNameSpecifier*>(SS.getScopeRep()); |
| 1594 | QualifierRange = SS.getRange(); |
Douglas Gregor | a9e29aa | 2009-10-22 07:19:14 +0000 | [diff] [blame] | 1595 | } |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 1596 | |
| 1597 | // We don't want lookup warnings at this point. |
| 1598 | R.suppressDiagnostics(); |
Douglas Gregor | a9e29aa | 2009-10-22 07:19:14 +0000 | [diff] [blame] | 1599 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1600 | bool Dependent |
| 1601 | = UnresolvedLookupExpr::ComputeDependence(R.begin(), R.end(), |
| 1602 | &TemplateArgs); |
| 1603 | UnresolvedLookupExpr *ULE |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 1604 | = UnresolvedLookupExpr::Create(Context, Dependent, R.getNamingClass(), |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1605 | Qualifier, QualifierRange, |
| 1606 | R.getLookupName(), R.getNameLoc(), |
Douglas Gregor | 5a84dec | 2010-05-23 18:57:34 +0000 | [diff] [blame] | 1607 | RequiresADL, TemplateArgs, |
| 1608 | R.begin(), R.end()); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1609 | |
| 1610 | return Owned(ULE); |
Douglas Gregor | edce4dd | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 1611 | } |
| 1612 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1613 | // We actually only call this from template instantiation. |
| 1614 | Sema::OwningExprResult |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 1615 | Sema::BuildQualifiedTemplateIdExpr(CXXScopeSpec &SS, |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1616 | DeclarationName Name, |
| 1617 | SourceLocation NameLoc, |
| 1618 | const TemplateArgumentListInfo &TemplateArgs) { |
| 1619 | DeclContext *DC; |
| 1620 | if (!(DC = computeDeclContext(SS, false)) || |
| 1621 | DC->isDependentContext() || |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 1622 | RequireCompleteDeclContext(SS, DC)) |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1623 | return BuildDependentDeclRefExpr(SS, Name, NameLoc, &TemplateArgs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1624 | |
Douglas Gregor | 1fd6d44 | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 1625 | bool MemberOfUnknownSpecialization; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1626 | LookupResult R(*this, Name, NameLoc, LookupOrdinaryName); |
Douglas Gregor | 1fd6d44 | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 1627 | LookupTemplateName(R, (Scope*) 0, SS, QualType(), /*Entering*/ false, |
| 1628 | MemberOfUnknownSpecialization); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1629 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1630 | if (R.isAmbiguous()) |
| 1631 | return ExprError(); |
| 1632 | |
| 1633 | if (R.empty()) { |
| 1634 | Diag(NameLoc, diag::err_template_kw_refers_to_non_template) |
| 1635 | << Name << SS.getRange(); |
| 1636 | return ExprError(); |
| 1637 | } |
| 1638 | |
| 1639 | if (ClassTemplateDecl *Temp = R.getAsSingle<ClassTemplateDecl>()) { |
| 1640 | Diag(NameLoc, diag::err_template_kw_refers_to_class_template) |
| 1641 | << (NestedNameSpecifier*) SS.getScopeRep() << Name << SS.getRange(); |
| 1642 | Diag(Temp->getLocation(), diag::note_referenced_class_template); |
| 1643 | return ExprError(); |
| 1644 | } |
| 1645 | |
| 1646 | return BuildTemplateIdExpr(SS, R, /* ADL */ false, TemplateArgs); |
Douglas Gregor | edce4dd | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 1647 | } |
| 1648 | |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 1649 | /// \brief Form a dependent template name. |
| 1650 | /// |
| 1651 | /// This action forms a dependent template name given the template |
| 1652 | /// name and its (presumably dependent) scope specifier. For |
| 1653 | /// example, given "MetaFun::template apply", the scope specifier \p |
| 1654 | /// SS will be "MetaFun::", \p TemplateKWLoc contains the location |
| 1655 | /// of the "template" keyword, and "apply" is the \p Name. |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 1656 | TemplateNameKind Sema::ActOnDependentTemplateName(Scope *S, |
| 1657 | SourceLocation TemplateKWLoc, |
| 1658 | CXXScopeSpec &SS, |
| 1659 | UnqualifiedId &Name, |
| 1660 | TypeTy *ObjectType, |
| 1661 | bool EnteringContext, |
| 1662 | TemplateTy &Result) { |
Douglas Gregor | 1a15dae | 2010-06-16 22:31:08 +0000 | [diff] [blame] | 1663 | if (TemplateKWLoc.isValid() && S && !S->getTemplateParamParent() && |
| 1664 | !getLangOptions().CPlusPlus0x) |
| 1665 | Diag(TemplateKWLoc, diag::ext_template_outside_of_template) |
| 1666 | << FixItHint::CreateRemoval(TemplateKWLoc); |
| 1667 | |
Douglas Gregor | 0707bc5 | 2010-01-19 16:01:07 +0000 | [diff] [blame] | 1668 | DeclContext *LookupCtx = 0; |
| 1669 | if (SS.isSet()) |
| 1670 | LookupCtx = computeDeclContext(SS, EnteringContext); |
| 1671 | if (!LookupCtx && ObjectType) |
| 1672 | LookupCtx = computeDeclContext(QualType::getFromOpaquePtr(ObjectType)); |
| 1673 | if (LookupCtx) { |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 1674 | // C++0x [temp.names]p5: |
| 1675 | // If a name prefixed by the keyword template is not the name of |
| 1676 | // a template, the program is ill-formed. [Note: the keyword |
| 1677 | // template may not be applied to non-template members of class |
| 1678 | // templates. -end note ] [ Note: as is the case with the |
| 1679 | // typename prefix, the template prefix is allowed in cases |
| 1680 | // where it is not strictly necessary; i.e., when the |
| 1681 | // nested-name-specifier or the expression on the left of the -> |
| 1682 | // or . is not dependent on a template-parameter, or the use |
| 1683 | // does not appear in the scope of a template. -end note] |
| 1684 | // |
| 1685 | // Note: C++03 was more strict here, because it banned the use of |
| 1686 | // the "template" keyword prior to a template-name that was not a |
| 1687 | // dependent name. C++ DR468 relaxed this requirement (the |
| 1688 | // "template" keyword is now permitted). We follow the C++0x |
Douglas Gregor | 732281d | 2010-06-14 22:07:54 +0000 | [diff] [blame] | 1689 | // rules, even in C++03 mode with a warning, retroactively applying the DR. |
Douglas Gregor | 1fd6d44 | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 1690 | bool MemberOfUnknownSpecialization; |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 1691 | TemplateNameKind TNK = isTemplateName(0, SS, Name, ObjectType, |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 1692 | EnteringContext, Result, |
Douglas Gregor | 1fd6d44 | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 1693 | MemberOfUnknownSpecialization); |
Douglas Gregor | 0707bc5 | 2010-01-19 16:01:07 +0000 | [diff] [blame] | 1694 | if (TNK == TNK_Non_template && LookupCtx->isDependentContext() && |
| 1695 | isa<CXXRecordDecl>(LookupCtx) && |
| 1696 | cast<CXXRecordDecl>(LookupCtx)->hasAnyDependentBases()) { |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 1697 | // This is a dependent template. Handle it below. |
Douglas Gregor | 9edad9b | 2010-01-14 17:47:39 +0000 | [diff] [blame] | 1698 | } else if (TNK == TNK_Non_template) { |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 1699 | Diag(Name.getSourceRange().getBegin(), |
| 1700 | diag::err_template_kw_refers_to_non_template) |
| 1701 | << GetNameFromUnqualifiedId(Name) |
Douglas Gregor | 0278e12 | 2010-05-05 05:58:24 +0000 | [diff] [blame] | 1702 | << Name.getSourceRange() |
| 1703 | << TemplateKWLoc; |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 1704 | return TNK_Non_template; |
Douglas Gregor | 9edad9b | 2010-01-14 17:47:39 +0000 | [diff] [blame] | 1705 | } else { |
| 1706 | // We found something; return it. |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 1707 | return TNK; |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 1708 | } |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 1709 | } |
| 1710 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1711 | NestedNameSpecifier *Qualifier |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 1712 | = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 1713 | |
| 1714 | switch (Name.getKind()) { |
| 1715 | case UnqualifiedId::IK_Identifier: |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 1716 | Result = TemplateTy::make(Context.getDependentTemplateName(Qualifier, |
| 1717 | Name.Identifier)); |
| 1718 | return TNK_Dependent_template_name; |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 1719 | |
Douglas Gregor | ca1bdd7 | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 1720 | case UnqualifiedId::IK_OperatorFunctionId: |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 1721 | Result = TemplateTy::make(Context.getDependentTemplateName(Qualifier, |
Douglas Gregor | ca1bdd7 | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 1722 | Name.OperatorFunctionId.Operator)); |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 1723 | return TNK_Dependent_template_name; |
Sean Hunt | e6252d1 | 2009-11-28 08:58:14 +0000 | [diff] [blame] | 1724 | |
| 1725 | case UnqualifiedId::IK_LiteralOperatorId: |
| 1726 | assert(false && "We don't support these; Parse shouldn't have allowed propagation"); |
| 1727 | |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 1728 | default: |
| 1729 | break; |
| 1730 | } |
| 1731 | |
| 1732 | Diag(Name.getSourceRange().getBegin(), |
| 1733 | diag::err_template_kw_refers_to_non_template) |
| 1734 | << GetNameFromUnqualifiedId(Name) |
Douglas Gregor | 0278e12 | 2010-05-05 05:58:24 +0000 | [diff] [blame] | 1735 | << Name.getSourceRange() |
| 1736 | << TemplateKWLoc; |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 1737 | return TNK_Non_template; |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 1738 | } |
| 1739 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1740 | bool Sema::CheckTemplateTypeArgument(TemplateTypeParmDecl *Param, |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1741 | const TemplateArgumentLoc &AL, |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 1742 | TemplateArgumentListBuilder &Converted) { |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1743 | const TemplateArgument &Arg = AL.getArgument(); |
| 1744 | |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 1745 | // Check template type parameter. |
Jeffrey Yasskin | db88d8a | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 1746 | switch(Arg.getKind()) { |
| 1747 | case TemplateArgument::Type: |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 1748 | // C++ [temp.arg.type]p1: |
| 1749 | // A template-argument for a template-parameter which is a |
| 1750 | // type shall be a type-id. |
Jeffrey Yasskin | db88d8a | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 1751 | break; |
| 1752 | case TemplateArgument::Template: { |
| 1753 | // We have a template type parameter but the template argument |
| 1754 | // is a template without any arguments. |
| 1755 | SourceRange SR = AL.getSourceRange(); |
| 1756 | TemplateName Name = Arg.getAsTemplate(); |
| 1757 | Diag(SR.getBegin(), diag::err_template_missing_args) |
| 1758 | << Name << SR; |
| 1759 | if (TemplateDecl *Decl = Name.getAsTemplateDecl()) |
| 1760 | Diag(Decl->getLocation(), diag::note_template_decl_here); |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 1761 | |
Jeffrey Yasskin | db88d8a | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 1762 | return true; |
| 1763 | } |
| 1764 | default: { |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 1765 | // We have a template type parameter but the template argument |
| 1766 | // is not a type. |
John McCall | 828bff2 | 2009-10-29 18:45:58 +0000 | [diff] [blame] | 1767 | SourceRange SR = AL.getSourceRange(); |
| 1768 | Diag(SR.getBegin(), diag::err_template_arg_must_be_type) << SR; |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 1769 | Diag(Param->getLocation(), diag::note_template_param_here); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1770 | |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 1771 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1772 | } |
Jeffrey Yasskin | db88d8a | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 1773 | } |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 1774 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1775 | if (CheckTemplateArgument(Param, AL.getTypeSourceInfo())) |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 1776 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1777 | |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 1778 | // Add the converted template type argument. |
Anders Carlsson | fb25052 | 2009-06-23 01:26:57 +0000 | [diff] [blame] | 1779 | Converted.Append( |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1780 | TemplateArgument(Context.getCanonicalType(Arg.getAsType()))); |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 1781 | return false; |
| 1782 | } |
| 1783 | |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 1784 | /// \brief Substitute template arguments into the default template argument for |
| 1785 | /// the given template type parameter. |
| 1786 | /// |
| 1787 | /// \param SemaRef the semantic analysis object for which we are performing |
| 1788 | /// the substitution. |
| 1789 | /// |
| 1790 | /// \param Template the template that we are synthesizing template arguments |
| 1791 | /// for. |
| 1792 | /// |
| 1793 | /// \param TemplateLoc the location of the template name that started the |
| 1794 | /// template-id we are checking. |
| 1795 | /// |
| 1796 | /// \param RAngleLoc the location of the right angle bracket ('>') that |
| 1797 | /// terminates the template-id. |
| 1798 | /// |
| 1799 | /// \param Param the template template parameter whose default we are |
| 1800 | /// substituting into. |
| 1801 | /// |
| 1802 | /// \param Converted the list of template arguments provided for template |
| 1803 | /// parameters that precede \p Param in the template parameter list. |
| 1804 | /// |
| 1805 | /// \returns the substituted template argument, or NULL if an error occurred. |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1806 | static TypeSourceInfo * |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 1807 | SubstDefaultTemplateArgument(Sema &SemaRef, |
| 1808 | TemplateDecl *Template, |
| 1809 | SourceLocation TemplateLoc, |
| 1810 | SourceLocation RAngleLoc, |
| 1811 | TemplateTypeParmDecl *Param, |
| 1812 | TemplateArgumentListBuilder &Converted) { |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1813 | TypeSourceInfo *ArgType = Param->getDefaultArgumentInfo(); |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 1814 | |
| 1815 | // If the argument type is dependent, instantiate it now based |
| 1816 | // on the previously-computed template arguments. |
| 1817 | if (ArgType->getType()->isDependentType()) { |
| 1818 | TemplateArgumentList TemplateArgs(SemaRef.Context, Converted, |
| 1819 | /*TakeArgs=*/false); |
| 1820 | |
| 1821 | MultiLevelTemplateArgumentList AllTemplateArgs |
| 1822 | = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs); |
| 1823 | |
| 1824 | Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc, |
| 1825 | Template, Converted.getFlatArguments(), |
| 1826 | Converted.flatSize(), |
| 1827 | SourceRange(TemplateLoc, RAngleLoc)); |
| 1828 | |
| 1829 | ArgType = SemaRef.SubstType(ArgType, AllTemplateArgs, |
| 1830 | Param->getDefaultArgumentLoc(), |
| 1831 | Param->getDeclName()); |
| 1832 | } |
| 1833 | |
| 1834 | return ArgType; |
| 1835 | } |
| 1836 | |
| 1837 | /// \brief Substitute template arguments into the default template argument for |
| 1838 | /// the given non-type template parameter. |
| 1839 | /// |
| 1840 | /// \param SemaRef the semantic analysis object for which we are performing |
| 1841 | /// the substitution. |
| 1842 | /// |
| 1843 | /// \param Template the template that we are synthesizing template arguments |
| 1844 | /// for. |
| 1845 | /// |
| 1846 | /// \param TemplateLoc the location of the template name that started the |
| 1847 | /// template-id we are checking. |
| 1848 | /// |
| 1849 | /// \param RAngleLoc the location of the right angle bracket ('>') that |
| 1850 | /// terminates the template-id. |
| 1851 | /// |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1852 | /// \param Param the non-type template parameter whose default we are |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 1853 | /// substituting into. |
| 1854 | /// |
| 1855 | /// \param Converted the list of template arguments provided for template |
| 1856 | /// parameters that precede \p Param in the template parameter list. |
| 1857 | /// |
| 1858 | /// \returns the substituted template argument, or NULL if an error occurred. |
| 1859 | static Sema::OwningExprResult |
| 1860 | SubstDefaultTemplateArgument(Sema &SemaRef, |
| 1861 | TemplateDecl *Template, |
| 1862 | SourceLocation TemplateLoc, |
| 1863 | SourceLocation RAngleLoc, |
| 1864 | NonTypeTemplateParmDecl *Param, |
| 1865 | TemplateArgumentListBuilder &Converted) { |
| 1866 | TemplateArgumentList TemplateArgs(SemaRef.Context, Converted, |
| 1867 | /*TakeArgs=*/false); |
| 1868 | |
| 1869 | MultiLevelTemplateArgumentList AllTemplateArgs |
| 1870 | = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs); |
| 1871 | |
| 1872 | Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc, |
| 1873 | Template, Converted.getFlatArguments(), |
| 1874 | Converted.flatSize(), |
| 1875 | SourceRange(TemplateLoc, RAngleLoc)); |
| 1876 | |
| 1877 | return SemaRef.SubstExpr(Param->getDefaultArgument(), AllTemplateArgs); |
| 1878 | } |
| 1879 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1880 | /// \brief Substitute template arguments into the default template argument for |
| 1881 | /// the given template template parameter. |
| 1882 | /// |
| 1883 | /// \param SemaRef the semantic analysis object for which we are performing |
| 1884 | /// the substitution. |
| 1885 | /// |
| 1886 | /// \param Template the template that we are synthesizing template arguments |
| 1887 | /// for. |
| 1888 | /// |
| 1889 | /// \param TemplateLoc the location of the template name that started the |
| 1890 | /// template-id we are checking. |
| 1891 | /// |
| 1892 | /// \param RAngleLoc the location of the right angle bracket ('>') that |
| 1893 | /// terminates the template-id. |
| 1894 | /// |
| 1895 | /// \param Param the template template parameter whose default we are |
| 1896 | /// substituting into. |
| 1897 | /// |
| 1898 | /// \param Converted the list of template arguments provided for template |
| 1899 | /// parameters that precede \p Param in the template parameter list. |
| 1900 | /// |
| 1901 | /// \returns the substituted template argument, or NULL if an error occurred. |
| 1902 | static TemplateName |
| 1903 | SubstDefaultTemplateArgument(Sema &SemaRef, |
| 1904 | TemplateDecl *Template, |
| 1905 | SourceLocation TemplateLoc, |
| 1906 | SourceLocation RAngleLoc, |
| 1907 | TemplateTemplateParmDecl *Param, |
| 1908 | TemplateArgumentListBuilder &Converted) { |
| 1909 | TemplateArgumentList TemplateArgs(SemaRef.Context, Converted, |
| 1910 | /*TakeArgs=*/false); |
| 1911 | |
| 1912 | MultiLevelTemplateArgumentList AllTemplateArgs |
| 1913 | = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs); |
| 1914 | |
| 1915 | Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc, |
| 1916 | Template, Converted.getFlatArguments(), |
| 1917 | Converted.flatSize(), |
| 1918 | SourceRange(TemplateLoc, RAngleLoc)); |
| 1919 | |
| 1920 | return SemaRef.SubstTemplateName( |
| 1921 | Param->getDefaultArgument().getArgument().getAsTemplate(), |
| 1922 | Param->getDefaultArgument().getTemplateNameLoc(), |
| 1923 | AllTemplateArgs); |
| 1924 | } |
| 1925 | |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 1926 | /// \brief If the given template parameter has a default template |
| 1927 | /// argument, substitute into that default template argument and |
| 1928 | /// return the corresponding template argument. |
| 1929 | TemplateArgumentLoc |
| 1930 | Sema::SubstDefaultTemplateArgumentIfAvailable(TemplateDecl *Template, |
| 1931 | SourceLocation TemplateLoc, |
| 1932 | SourceLocation RAngleLoc, |
| 1933 | Decl *Param, |
| 1934 | TemplateArgumentListBuilder &Converted) { |
| 1935 | if (TemplateTypeParmDecl *TypeParm = dyn_cast<TemplateTypeParmDecl>(Param)) { |
| 1936 | if (!TypeParm->hasDefaultArgument()) |
| 1937 | return TemplateArgumentLoc(); |
| 1938 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1939 | TypeSourceInfo *DI = SubstDefaultTemplateArgument(*this, Template, |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 1940 | TemplateLoc, |
| 1941 | RAngleLoc, |
| 1942 | TypeParm, |
| 1943 | Converted); |
| 1944 | if (DI) |
| 1945 | return TemplateArgumentLoc(TemplateArgument(DI->getType()), DI); |
| 1946 | |
| 1947 | return TemplateArgumentLoc(); |
| 1948 | } |
| 1949 | |
| 1950 | if (NonTypeTemplateParmDecl *NonTypeParm |
| 1951 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
| 1952 | if (!NonTypeParm->hasDefaultArgument()) |
| 1953 | return TemplateArgumentLoc(); |
| 1954 | |
| 1955 | OwningExprResult Arg = SubstDefaultTemplateArgument(*this, Template, |
| 1956 | TemplateLoc, |
| 1957 | RAngleLoc, |
| 1958 | NonTypeParm, |
| 1959 | Converted); |
| 1960 | if (Arg.isInvalid()) |
| 1961 | return TemplateArgumentLoc(); |
| 1962 | |
| 1963 | Expr *ArgE = Arg.takeAs<Expr>(); |
| 1964 | return TemplateArgumentLoc(TemplateArgument(ArgE), ArgE); |
| 1965 | } |
| 1966 | |
| 1967 | TemplateTemplateParmDecl *TempTempParm |
| 1968 | = cast<TemplateTemplateParmDecl>(Param); |
| 1969 | if (!TempTempParm->hasDefaultArgument()) |
| 1970 | return TemplateArgumentLoc(); |
| 1971 | |
| 1972 | TemplateName TName = SubstDefaultTemplateArgument(*this, Template, |
| 1973 | TemplateLoc, |
| 1974 | RAngleLoc, |
| 1975 | TempTempParm, |
| 1976 | Converted); |
| 1977 | if (TName.isNull()) |
| 1978 | return TemplateArgumentLoc(); |
| 1979 | |
| 1980 | return TemplateArgumentLoc(TemplateArgument(TName), |
| 1981 | TempTempParm->getDefaultArgument().getTemplateQualifierRange(), |
| 1982 | TempTempParm->getDefaultArgument().getTemplateNameLoc()); |
| 1983 | } |
| 1984 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 1985 | /// \brief Check that the given template argument corresponds to the given |
| 1986 | /// template parameter. |
| 1987 | bool Sema::CheckTemplateArgument(NamedDecl *Param, |
| 1988 | const TemplateArgumentLoc &Arg, |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 1989 | TemplateDecl *Template, |
| 1990 | SourceLocation TemplateLoc, |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 1991 | SourceLocation RAngleLoc, |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 1992 | TemplateArgumentListBuilder &Converted, |
| 1993 | CheckTemplateArgumentKind CTAK) { |
Douglas Gregor | d9e1530 | 2009-11-11 19:41:09 +0000 | [diff] [blame] | 1994 | // Check template type parameters. |
| 1995 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 1996 | return CheckTemplateTypeArgument(TTP, Arg, Converted); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 1997 | |
Douglas Gregor | d9e1530 | 2009-11-11 19:41:09 +0000 | [diff] [blame] | 1998 | // Check non-type template parameters. |
| 1999 | if (NonTypeTemplateParmDecl *NTTP =dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2000 | // Do substitution on the type of the non-type template parameter |
| 2001 | // with the template arguments we've seen thus far. |
| 2002 | QualType NTTPType = NTTP->getType(); |
| 2003 | if (NTTPType->isDependentType()) { |
| 2004 | // Do substitution on the type of the non-type template parameter. |
| 2005 | InstantiatingTemplate Inst(*this, TemplateLoc, Template, |
| 2006 | NTTP, Converted.getFlatArguments(), |
| 2007 | Converted.flatSize(), |
| 2008 | SourceRange(TemplateLoc, RAngleLoc)); |
| 2009 | |
| 2010 | TemplateArgumentList TemplateArgs(Context, Converted, |
| 2011 | /*TakeArgs=*/false); |
| 2012 | NTTPType = SubstType(NTTPType, |
| 2013 | MultiLevelTemplateArgumentList(TemplateArgs), |
| 2014 | NTTP->getLocation(), |
| 2015 | NTTP->getDeclName()); |
| 2016 | // If that worked, check the non-type template parameter type |
| 2017 | // for validity. |
| 2018 | if (!NTTPType.isNull()) |
| 2019 | NTTPType = CheckNonTypeTemplateParameterType(NTTPType, |
| 2020 | NTTP->getLocation()); |
| 2021 | if (NTTPType.isNull()) |
| 2022 | return true; |
| 2023 | } |
| 2024 | |
| 2025 | switch (Arg.getArgument().getKind()) { |
| 2026 | case TemplateArgument::Null: |
| 2027 | assert(false && "Should never see a NULL template argument here"); |
| 2028 | return true; |
| 2029 | |
| 2030 | case TemplateArgument::Expression: { |
| 2031 | Expr *E = Arg.getArgument().getAsExpr(); |
| 2032 | TemplateArgument Result; |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 2033 | if (CheckTemplateArgument(NTTP, NTTPType, E, Result, CTAK)) |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2034 | return true; |
| 2035 | |
| 2036 | Converted.Append(Result); |
| 2037 | break; |
| 2038 | } |
| 2039 | |
| 2040 | case TemplateArgument::Declaration: |
| 2041 | case TemplateArgument::Integral: |
| 2042 | // We've already checked this template argument, so just copy |
| 2043 | // it to the list of converted arguments. |
| 2044 | Converted.Append(Arg.getArgument()); |
| 2045 | break; |
| 2046 | |
| 2047 | case TemplateArgument::Template: |
| 2048 | // We were given a template template argument. It may not be ill-formed; |
| 2049 | // see below. |
| 2050 | if (DependentTemplateName *DTN |
| 2051 | = Arg.getArgument().getAsTemplate().getAsDependentTemplateName()) { |
| 2052 | // We have a template argument such as \c T::template X, which we |
| 2053 | // parsed as a template template argument. However, since we now |
| 2054 | // know that we need a non-type template argument, convert this |
| 2055 | // template name into an expression. |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2056 | Expr *E = DependentScopeDeclRefExpr::Create(Context, |
| 2057 | DTN->getQualifier(), |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2058 | Arg.getTemplateQualifierRange(), |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2059 | DTN->getIdentifier(), |
| 2060 | Arg.getTemplateNameLoc()); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2061 | |
| 2062 | TemplateArgument Result; |
| 2063 | if (CheckTemplateArgument(NTTP, NTTPType, E, Result)) |
| 2064 | return true; |
| 2065 | |
| 2066 | Converted.Append(Result); |
| 2067 | break; |
| 2068 | } |
| 2069 | |
| 2070 | // We have a template argument that actually does refer to a class |
| 2071 | // template, template alias, or template template parameter, and |
| 2072 | // therefore cannot be a non-type template argument. |
| 2073 | Diag(Arg.getLocation(), diag::err_template_arg_must_be_expr) |
| 2074 | << Arg.getSourceRange(); |
| 2075 | |
| 2076 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 2077 | return true; |
| 2078 | |
| 2079 | case TemplateArgument::Type: { |
| 2080 | // We have a non-type template parameter but the template |
| 2081 | // argument is a type. |
| 2082 | |
| 2083 | // C++ [temp.arg]p2: |
| 2084 | // In a template-argument, an ambiguity between a type-id and |
| 2085 | // an expression is resolved to a type-id, regardless of the |
| 2086 | // form of the corresponding template-parameter. |
| 2087 | // |
| 2088 | // We warn specifically about this case, since it can be rather |
| 2089 | // confusing for users. |
| 2090 | QualType T = Arg.getArgument().getAsType(); |
| 2091 | SourceRange SR = Arg.getSourceRange(); |
| 2092 | if (T->isFunctionType()) |
| 2093 | Diag(SR.getBegin(), diag::err_template_arg_nontype_ambig) << SR << T; |
| 2094 | else |
| 2095 | Diag(SR.getBegin(), diag::err_template_arg_must_be_expr) << SR; |
| 2096 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 2097 | return true; |
| 2098 | } |
| 2099 | |
| 2100 | case TemplateArgument::Pack: |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 2101 | llvm_unreachable("Caller must expand template argument packs"); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2102 | break; |
| 2103 | } |
| 2104 | |
| 2105 | return false; |
| 2106 | } |
| 2107 | |
| 2108 | |
| 2109 | // Check template template parameters. |
| 2110 | TemplateTemplateParmDecl *TempParm = cast<TemplateTemplateParmDecl>(Param); |
| 2111 | |
| 2112 | // Substitute into the template parameter list of the template |
| 2113 | // template parameter, since previously-supplied template arguments |
| 2114 | // may appear within the template template parameter. |
| 2115 | { |
| 2116 | // Set up a template instantiation context. |
| 2117 | LocalInstantiationScope Scope(*this); |
| 2118 | InstantiatingTemplate Inst(*this, TemplateLoc, Template, |
| 2119 | TempParm, Converted.getFlatArguments(), |
| 2120 | Converted.flatSize(), |
| 2121 | SourceRange(TemplateLoc, RAngleLoc)); |
| 2122 | |
| 2123 | TemplateArgumentList TemplateArgs(Context, Converted, |
| 2124 | /*TakeArgs=*/false); |
| 2125 | TempParm = cast_or_null<TemplateTemplateParmDecl>( |
| 2126 | SubstDecl(TempParm, CurContext, |
| 2127 | MultiLevelTemplateArgumentList(TemplateArgs))); |
| 2128 | if (!TempParm) |
| 2129 | return true; |
| 2130 | |
| 2131 | // FIXME: TempParam is leaked. |
| 2132 | } |
| 2133 | |
| 2134 | switch (Arg.getArgument().getKind()) { |
| 2135 | case TemplateArgument::Null: |
| 2136 | assert(false && "Should never see a NULL template argument here"); |
| 2137 | return true; |
| 2138 | |
| 2139 | case TemplateArgument::Template: |
| 2140 | if (CheckTemplateArgument(TempParm, Arg)) |
| 2141 | return true; |
| 2142 | |
| 2143 | Converted.Append(Arg.getArgument()); |
| 2144 | break; |
| 2145 | |
| 2146 | case TemplateArgument::Expression: |
| 2147 | case TemplateArgument::Type: |
| 2148 | // We have a template template parameter but the template |
| 2149 | // argument does not refer to a template. |
| 2150 | Diag(Arg.getLocation(), diag::err_template_arg_must_be_template); |
| 2151 | return true; |
| 2152 | |
| 2153 | case TemplateArgument::Declaration: |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 2154 | llvm_unreachable( |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2155 | "Declaration argument with template template parameter"); |
| 2156 | break; |
| 2157 | case TemplateArgument::Integral: |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 2158 | llvm_unreachable( |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2159 | "Integral argument with template template parameter"); |
| 2160 | break; |
| 2161 | |
| 2162 | case TemplateArgument::Pack: |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 2163 | llvm_unreachable("Caller must expand template argument packs"); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2164 | break; |
| 2165 | } |
| 2166 | |
| 2167 | return false; |
| 2168 | } |
| 2169 | |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2170 | /// \brief Check that the given template argument list is well-formed |
| 2171 | /// for specializing the given template. |
| 2172 | bool Sema::CheckTemplateArgumentList(TemplateDecl *Template, |
| 2173 | SourceLocation TemplateLoc, |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2174 | const TemplateArgumentListInfo &TemplateArgs, |
Douglas Gregor | 16134c6 | 2009-07-01 00:28:38 +0000 | [diff] [blame] | 2175 | bool PartialTemplateArgs, |
Anders Carlsson | 1c5976e | 2009-06-05 03:43:12 +0000 | [diff] [blame] | 2176 | TemplateArgumentListBuilder &Converted) { |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2177 | TemplateParameterList *Params = Template->getTemplateParameters(); |
| 2178 | unsigned NumParams = Params->size(); |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2179 | unsigned NumArgs = TemplateArgs.size(); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2180 | bool Invalid = false; |
| 2181 | |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2182 | SourceLocation RAngleLoc = TemplateArgs.getRAngleLoc(); |
| 2183 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2184 | bool HasParameterPack = |
Anders Carlsson | 0ceffb5 | 2009-06-13 02:08:00 +0000 | [diff] [blame] | 2185 | NumParams > 0 && Params->getParam(NumParams - 1)->isTemplateParameterPack(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2186 | |
Anders Carlsson | 0ceffb5 | 2009-06-13 02:08:00 +0000 | [diff] [blame] | 2187 | if ((NumArgs > NumParams && !HasParameterPack) || |
Douglas Gregor | 16134c6 | 2009-07-01 00:28:38 +0000 | [diff] [blame] | 2188 | (NumArgs < Params->getMinRequiredArguments() && |
| 2189 | !PartialTemplateArgs)) { |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2190 | // FIXME: point at either the first arg beyond what we can handle, |
| 2191 | // or the '>', depending on whether we have too many or too few |
| 2192 | // arguments. |
| 2193 | SourceRange Range; |
| 2194 | if (NumArgs > NumParams) |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2195 | Range = SourceRange(TemplateArgs[NumParams].getLocation(), RAngleLoc); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2196 | Diag(TemplateLoc, diag::err_template_arg_list_different_arity) |
| 2197 | << (NumArgs > NumParams) |
| 2198 | << (isa<ClassTemplateDecl>(Template)? 0 : |
| 2199 | isa<FunctionTemplateDecl>(Template)? 1 : |
| 2200 | isa<TemplateTemplateParmDecl>(Template)? 2 : 3) |
| 2201 | << Template << Range; |
Douglas Gregor | 62cb18d | 2009-02-11 18:16:40 +0000 | [diff] [blame] | 2202 | Diag(Template->getLocation(), diag::note_template_decl_here) |
| 2203 | << Params->getSourceRange(); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2204 | Invalid = true; |
| 2205 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2206 | |
| 2207 | // C++ [temp.arg]p1: |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2208 | // [...] The type and form of each template-argument specified in |
| 2209 | // a template-id shall match the type and form specified for the |
| 2210 | // corresponding parameter declared by the template in its |
| 2211 | // template-parameter-list. |
| 2212 | unsigned ArgIdx = 0; |
| 2213 | for (TemplateParameterList::iterator Param = Params->begin(), |
| 2214 | ParamEnd = Params->end(); |
| 2215 | Param != ParamEnd; ++Param, ++ArgIdx) { |
Douglas Gregor | 16134c6 | 2009-07-01 00:28:38 +0000 | [diff] [blame] | 2216 | if (ArgIdx > NumArgs && PartialTemplateArgs) |
| 2217 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2218 | |
Douglas Gregor | d9e1530 | 2009-11-11 19:41:09 +0000 | [diff] [blame] | 2219 | // If we have a template parameter pack, check every remaining template |
| 2220 | // argument against that template parameter pack. |
| 2221 | if ((*Param)->isTemplateParameterPack()) { |
| 2222 | Converted.BeginPack(); |
| 2223 | for (; ArgIdx < NumArgs; ++ArgIdx) { |
| 2224 | if (CheckTemplateArgument(*Param, TemplateArgs[ArgIdx], Template, |
| 2225 | TemplateLoc, RAngleLoc, Converted)) { |
| 2226 | Invalid = true; |
| 2227 | break; |
| 2228 | } |
| 2229 | } |
| 2230 | Converted.EndPack(); |
| 2231 | continue; |
| 2232 | } |
| 2233 | |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2234 | if (ArgIdx < NumArgs) { |
| 2235 | // Check the template argument we were given. |
| 2236 | if (CheckTemplateArgument(*Param, TemplateArgs[ArgIdx], Template, |
| 2237 | TemplateLoc, RAngleLoc, Converted)) |
| 2238 | return true; |
| 2239 | |
| 2240 | continue; |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 2241 | } |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2242 | |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2243 | // We have a default template argument that we will use. |
| 2244 | TemplateArgumentLoc Arg; |
| 2245 | |
| 2246 | // Retrieve the default template argument from the template |
| 2247 | // parameter. For each kind of template parameter, we substitute the |
| 2248 | // template arguments provided thus far and any "outer" template arguments |
| 2249 | // (when the template parameter was part of a nested template) into |
| 2250 | // the default argument. |
| 2251 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*Param)) { |
| 2252 | if (!TTP->hasDefaultArgument()) { |
| 2253 | assert((Invalid || PartialTemplateArgs) && "Missing default argument"); |
| 2254 | break; |
| 2255 | } |
| 2256 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2257 | TypeSourceInfo *ArgType = SubstDefaultTemplateArgument(*this, |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2258 | Template, |
| 2259 | TemplateLoc, |
| 2260 | RAngleLoc, |
| 2261 | TTP, |
| 2262 | Converted); |
| 2263 | if (!ArgType) |
| 2264 | return true; |
| 2265 | |
| 2266 | Arg = TemplateArgumentLoc(TemplateArgument(ArgType->getType()), |
| 2267 | ArgType); |
| 2268 | } else if (NonTypeTemplateParmDecl *NTTP |
| 2269 | = dyn_cast<NonTypeTemplateParmDecl>(*Param)) { |
| 2270 | if (!NTTP->hasDefaultArgument()) { |
| 2271 | assert((Invalid || PartialTemplateArgs) && "Missing default argument"); |
| 2272 | break; |
| 2273 | } |
| 2274 | |
| 2275 | Sema::OwningExprResult E = SubstDefaultTemplateArgument(*this, Template, |
| 2276 | TemplateLoc, |
| 2277 | RAngleLoc, |
| 2278 | NTTP, |
| 2279 | Converted); |
| 2280 | if (E.isInvalid()) |
| 2281 | return true; |
| 2282 | |
| 2283 | Expr *Ex = E.takeAs<Expr>(); |
| 2284 | Arg = TemplateArgumentLoc(TemplateArgument(Ex), Ex); |
| 2285 | } else { |
| 2286 | TemplateTemplateParmDecl *TempParm |
| 2287 | = cast<TemplateTemplateParmDecl>(*Param); |
| 2288 | |
| 2289 | if (!TempParm->hasDefaultArgument()) { |
| 2290 | assert((Invalid || PartialTemplateArgs) && "Missing default argument"); |
| 2291 | break; |
| 2292 | } |
| 2293 | |
| 2294 | TemplateName Name = SubstDefaultTemplateArgument(*this, Template, |
| 2295 | TemplateLoc, |
| 2296 | RAngleLoc, |
| 2297 | TempParm, |
| 2298 | Converted); |
| 2299 | if (Name.isNull()) |
| 2300 | return true; |
| 2301 | |
| 2302 | Arg = TemplateArgumentLoc(TemplateArgument(Name), |
| 2303 | TempParm->getDefaultArgument().getTemplateQualifierRange(), |
| 2304 | TempParm->getDefaultArgument().getTemplateNameLoc()); |
| 2305 | } |
| 2306 | |
| 2307 | // Introduce an instantiation record that describes where we are using |
| 2308 | // the default template argument. |
| 2309 | InstantiatingTemplate Instantiating(*this, RAngleLoc, Template, *Param, |
| 2310 | Converted.getFlatArguments(), |
| 2311 | Converted.flatSize(), |
| 2312 | SourceRange(TemplateLoc, RAngleLoc)); |
| 2313 | |
| 2314 | // Check the default template argument. |
Douglas Gregor | d9e1530 | 2009-11-11 19:41:09 +0000 | [diff] [blame] | 2315 | if (CheckTemplateArgument(*Param, Arg, Template, TemplateLoc, |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2316 | RAngleLoc, Converted)) |
| 2317 | return true; |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2318 | } |
| 2319 | |
| 2320 | return Invalid; |
| 2321 | } |
| 2322 | |
| 2323 | /// \brief Check a template argument against its corresponding |
| 2324 | /// template type parameter. |
| 2325 | /// |
| 2326 | /// This routine implements the semantics of C++ [temp.arg.type]. It |
| 2327 | /// returns true if an error occurred, and false otherwise. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2328 | bool Sema::CheckTemplateArgument(TemplateTypeParmDecl *Param, |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2329 | TypeSourceInfo *ArgInfo) { |
| 2330 | assert(ArgInfo && "invalid TypeSourceInfo"); |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2331 | QualType Arg = ArgInfo->getType(); |
| 2332 | |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2333 | // C++ [temp.arg.type]p2: |
| 2334 | // A local type, a type with no linkage, an unnamed type or a type |
| 2335 | // compounded from any of these types shall not be used as a |
| 2336 | // template-argument for a template type-parameter. |
| 2337 | // |
Douglas Gregor | 0fddb97 | 2010-05-22 16:17:30 +0000 | [diff] [blame] | 2338 | // FIXME: Perform the unnamed type check. |
| 2339 | SourceRange SR = ArgInfo->getTypeLoc().getSourceRange(); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 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()) { |
Abramo Bagnara | bd054db | 2010-05-20 10:00:11 +0000 | [diff] [blame] | 2346 | SourceRange SR = ArgInfo->getTypeLoc().getSourceRange(); |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 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 | Diag(SR.getBegin(), diag::err_template_arg_unnamed_type) << SR; |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2352 | Diag(Tag->getDecl()->getLocation(), diag::note_template_unnamed_type_here); |
| 2353 | return true; |
Douglas Gregor | 0fddb97 | 2010-05-22 16:17:30 +0000 | [diff] [blame] | 2354 | } else if (Arg->isVariablyModifiedType()) { |
| 2355 | Diag(SR.getBegin(), diag::err_variably_modified_template_arg) |
| 2356 | << Arg; |
| 2357 | return true; |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 2358 | } else if (Context.hasSameUnqualifiedType(Arg, Context.OverloadTy)) { |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 2359 | return Diag(SR.getBegin(), diag::err_template_arg_overload_type) << SR; |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2360 | } |
| 2361 | |
| 2362 | return false; |
| 2363 | } |
| 2364 | |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2365 | /// \brief Checks whether the given template argument is the address |
| 2366 | /// of an object or function according to C++ [temp.arg.nontype]p1. |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2367 | static bool |
| 2368 | CheckTemplateArgumentAddressOfObjectOrFunction(Sema &S, |
| 2369 | NonTypeTemplateParmDecl *Param, |
| 2370 | QualType ParamType, |
| 2371 | Expr *ArgIn, |
| 2372 | TemplateArgument &Converted) { |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2373 | bool Invalid = false; |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2374 | Expr *Arg = ArgIn; |
| 2375 | QualType ArgType = Arg->getType(); |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2376 | |
| 2377 | // See through any implicit casts we added to fix the type. |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 2378 | while (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(Arg)) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2379 | Arg = Cast->getSubExpr(); |
| 2380 | |
| 2381 | // C++ [temp.arg.nontype]p1: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2382 | // |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2383 | // A template-argument for a non-type, non-template |
| 2384 | // template-parameter shall be one of: [...] |
| 2385 | // |
| 2386 | // -- the address of an object or function with external |
| 2387 | // linkage, including function templates and function |
| 2388 | // template-ids but excluding non-static class members, |
| 2389 | // expressed as & id-expression where the & is optional if |
| 2390 | // the name refers to a function or array, or if the |
| 2391 | // corresponding template-parameter is a reference; or |
| 2392 | DeclRefExpr *DRE = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2393 | |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2394 | // Ignore (and complain about) any excess parentheses. |
| 2395 | while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) { |
| 2396 | if (!Invalid) { |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2397 | S.Diag(Arg->getSourceRange().getBegin(), |
| 2398 | diag::err_template_arg_extra_parens) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2399 | << Arg->getSourceRange(); |
| 2400 | Invalid = true; |
| 2401 | } |
| 2402 | |
| 2403 | Arg = Parens->getSubExpr(); |
| 2404 | } |
| 2405 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2406 | bool AddressTaken = false; |
| 2407 | SourceLocation AddrOpLoc; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2408 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) { |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2409 | if (UnOp->getOpcode() == UnaryOperator::AddrOf) { |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2410 | DRE = dyn_cast<DeclRefExpr>(UnOp->getSubExpr()); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2411 | AddressTaken = true; |
| 2412 | AddrOpLoc = UnOp->getOperatorLoc(); |
| 2413 | } |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2414 | } else |
| 2415 | DRE = dyn_cast<DeclRefExpr>(Arg); |
| 2416 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2417 | if (!DRE) { |
Douglas Gregor | 1a8cf73 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 2418 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_decl_ref) |
| 2419 | << Arg->getSourceRange(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2420 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 2421 | return true; |
| 2422 | } |
Chandler Carruth | 038cc39 | 2010-01-31 10:01:20 +0000 | [diff] [blame] | 2423 | |
| 2424 | // Stop checking the precise nature of the argument if it is value dependent, |
| 2425 | // it should be checked when instantiated. |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2426 | if (Arg->isValueDependent()) { |
| 2427 | Converted = TemplateArgument(ArgIn->Retain()); |
Chandler Carruth | 038cc39 | 2010-01-31 10:01:20 +0000 | [diff] [blame] | 2428 | return false; |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2429 | } |
Chandler Carruth | 038cc39 | 2010-01-31 10:01:20 +0000 | [diff] [blame] | 2430 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2431 | if (!isa<ValueDecl>(DRE->getDecl())) { |
| 2432 | S.Diag(Arg->getSourceRange().getBegin(), |
| 2433 | diag::err_template_arg_not_object_or_func_form) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2434 | << Arg->getSourceRange(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2435 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 2436 | return true; |
| 2437 | } |
| 2438 | |
| 2439 | NamedDecl *Entity = 0; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2440 | |
| 2441 | // Cannot refer to non-static data members |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2442 | if (FieldDecl *Field = dyn_cast<FieldDecl>(DRE->getDecl())) { |
| 2443 | S.Diag(Arg->getSourceRange().getBegin(), diag::err_template_arg_field) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2444 | << Field << Arg->getSourceRange(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2445 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 2446 | return true; |
| 2447 | } |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2448 | |
| 2449 | // Cannot refer to non-static member functions |
| 2450 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(DRE->getDecl())) |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2451 | if (!Method->isStatic()) { |
| 2452 | S.Diag(Arg->getSourceRange().getBegin(), diag::err_template_arg_method) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2453 | << Method << Arg->getSourceRange(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2454 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 2455 | return true; |
| 2456 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2457 | |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2458 | // Functions must have external linkage. |
| 2459 | if (FunctionDecl *Func = dyn_cast<FunctionDecl>(DRE->getDecl())) { |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 2460 | if (!isExternalLinkage(Func->getLinkage())) { |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2461 | S.Diag(Arg->getSourceRange().getBegin(), |
| 2462 | diag::err_template_arg_function_not_extern) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2463 | << Func << Arg->getSourceRange(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2464 | S.Diag(Func->getLocation(), diag::note_template_arg_internal_object) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2465 | << true; |
| 2466 | return true; |
| 2467 | } |
| 2468 | |
| 2469 | // Okay: we've named a function with external linkage. |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 2470 | Entity = Func; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2471 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2472 | // If the template parameter has pointer type, the function decays. |
| 2473 | if (ParamType->isPointerType() && !AddressTaken) |
| 2474 | ArgType = S.Context.getPointerType(Func->getType()); |
| 2475 | else if (AddressTaken && ParamType->isReferenceType()) { |
| 2476 | // If we originally had an address-of operator, but the |
| 2477 | // parameter has reference type, complain and (if things look |
| 2478 | // like they will work) drop the address-of operator. |
| 2479 | if (!S.Context.hasSameUnqualifiedType(Func->getType(), |
| 2480 | ParamType.getNonReferenceType())) { |
| 2481 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 2482 | << ParamType; |
| 2483 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 2484 | return true; |
| 2485 | } |
| 2486 | |
| 2487 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 2488 | << ParamType |
| 2489 | << FixItHint::CreateRemoval(AddrOpLoc); |
| 2490 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 2491 | |
| 2492 | ArgType = Func->getType(); |
| 2493 | } |
| 2494 | } else if (VarDecl *Var = dyn_cast<VarDecl>(DRE->getDecl())) { |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 2495 | if (!isExternalLinkage(Var->getLinkage())) { |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2496 | S.Diag(Arg->getSourceRange().getBegin(), |
| 2497 | diag::err_template_arg_object_not_extern) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2498 | << Var << Arg->getSourceRange(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2499 | S.Diag(Var->getLocation(), diag::note_template_arg_internal_object) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2500 | << true; |
| 2501 | return true; |
| 2502 | } |
| 2503 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2504 | // A value of reference type is not an object. |
| 2505 | if (Var->getType()->isReferenceType()) { |
| 2506 | S.Diag(Arg->getSourceRange().getBegin(), |
| 2507 | diag::err_template_arg_reference_var) |
| 2508 | << Var->getType() << Arg->getSourceRange(); |
| 2509 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 2510 | return true; |
| 2511 | } |
| 2512 | |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2513 | // Okay: we've named an object with external linkage |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 2514 | Entity = Var; |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2515 | |
| 2516 | // If the template parameter has pointer type, we must have taken |
| 2517 | // the address of this object. |
| 2518 | if (ParamType->isReferenceType()) { |
| 2519 | if (AddressTaken) { |
| 2520 | // If we originally had an address-of operator, but the |
| 2521 | // parameter has reference type, complain and (if things look |
| 2522 | // like they will work) drop the address-of operator. |
| 2523 | if (!S.Context.hasSameUnqualifiedType(Var->getType(), |
| 2524 | ParamType.getNonReferenceType())) { |
| 2525 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 2526 | << ParamType; |
| 2527 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 2528 | return true; |
| 2529 | } |
| 2530 | |
| 2531 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 2532 | << ParamType |
| 2533 | << FixItHint::CreateRemoval(AddrOpLoc); |
| 2534 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 2535 | |
| 2536 | ArgType = Var->getType(); |
| 2537 | } |
| 2538 | } else if (!AddressTaken && ParamType->isPointerType()) { |
| 2539 | if (Var->getType()->isArrayType()) { |
| 2540 | // Array-to-pointer decay. |
| 2541 | ArgType = S.Context.getArrayDecayedType(Var->getType()); |
| 2542 | } else { |
| 2543 | // If the template parameter has pointer type but the address of |
| 2544 | // this object was not taken, complain and (possibly) recover by |
| 2545 | // taking the address of the entity. |
| 2546 | ArgType = S.Context.getPointerType(Var->getType()); |
| 2547 | if (!S.Context.hasSameUnqualifiedType(ArgType, ParamType)) { |
| 2548 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_address_of) |
| 2549 | << ParamType; |
| 2550 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 2551 | return true; |
| 2552 | } |
| 2553 | |
| 2554 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_address_of) |
| 2555 | << ParamType |
| 2556 | << FixItHint::CreateInsertion(Arg->getLocStart(), "&"); |
| 2557 | |
| 2558 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 2559 | } |
| 2560 | } |
| 2561 | } else { |
| 2562 | // We found something else, but we don't know specifically what it is. |
| 2563 | S.Diag(Arg->getSourceRange().getBegin(), |
| 2564 | diag::err_template_arg_not_object_or_func) |
| 2565 | << Arg->getSourceRange(); |
| 2566 | S.Diag(DRE->getDecl()->getLocation(), diag::note_template_arg_refers_here); |
| 2567 | return true; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2568 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2569 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2570 | if (ParamType->isPointerType() && |
| 2571 | !ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType() && |
| 2572 | S.IsQualificationConversion(ArgType, ParamType)) { |
| 2573 | // For pointer-to-object types, qualification conversions are |
| 2574 | // permitted. |
| 2575 | } else { |
| 2576 | if (const ReferenceType *ParamRef = ParamType->getAs<ReferenceType>()) { |
| 2577 | if (!ParamRef->getPointeeType()->isFunctionType()) { |
| 2578 | // C++ [temp.arg.nontype]p5b3: |
| 2579 | // For a non-type template-parameter of type reference to |
| 2580 | // object, no conversions apply. The type referred to by the |
| 2581 | // reference may be more cv-qualified than the (otherwise |
| 2582 | // identical) type of the template- argument. The |
| 2583 | // template-parameter is bound directly to the |
| 2584 | // template-argument, which shall be an lvalue. |
| 2585 | |
| 2586 | // FIXME: Other qualifiers? |
| 2587 | unsigned ParamQuals = ParamRef->getPointeeType().getCVRQualifiers(); |
| 2588 | unsigned ArgQuals = ArgType.getCVRQualifiers(); |
| 2589 | |
| 2590 | if ((ParamQuals | ArgQuals) != ParamQuals) { |
| 2591 | S.Diag(Arg->getSourceRange().getBegin(), |
| 2592 | diag::err_template_arg_ref_bind_ignores_quals) |
| 2593 | << ParamType << Arg->getType() |
| 2594 | << Arg->getSourceRange(); |
| 2595 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 2596 | return true; |
| 2597 | } |
| 2598 | } |
| 2599 | } |
| 2600 | |
| 2601 | // At this point, the template argument refers to an object or |
| 2602 | // function with external linkage. We now need to check whether the |
| 2603 | // argument and parameter types are compatible. |
| 2604 | if (!S.Context.hasSameUnqualifiedType(ArgType, |
| 2605 | ParamType.getNonReferenceType())) { |
| 2606 | // We can't perform this conversion or binding. |
| 2607 | if (ParamType->isReferenceType()) |
| 2608 | S.Diag(Arg->getLocStart(), diag::err_template_arg_no_ref_bind) |
| 2609 | << ParamType << Arg->getType() << Arg->getSourceRange(); |
| 2610 | else |
| 2611 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_convertible) |
| 2612 | << Arg->getType() << ParamType << Arg->getSourceRange(); |
| 2613 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 2614 | return true; |
| 2615 | } |
| 2616 | } |
| 2617 | |
| 2618 | // Create the template argument. |
| 2619 | Converted = TemplateArgument(Entity->getCanonicalDecl()); |
Douglas Gregor | 77c13e0 | 2010-04-24 18:20:53 +0000 | [diff] [blame] | 2620 | S.MarkDeclarationReferenced(Arg->getLocStart(), Entity); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2621 | return false; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2622 | } |
| 2623 | |
| 2624 | /// \brief Checks whether the given template argument is a pointer to |
| 2625 | /// member constant according to C++ [temp.arg.nontype]p1. |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 2626 | bool Sema::CheckTemplateArgumentPointerToMember(Expr *Arg, |
| 2627 | TemplateArgument &Converted) { |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2628 | bool Invalid = false; |
| 2629 | |
| 2630 | // See through any implicit casts we added to fix the type. |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 2631 | while (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(Arg)) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2632 | Arg = Cast->getSubExpr(); |
| 2633 | |
| 2634 | // C++ [temp.arg.nontype]p1: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2635 | // |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2636 | // A template-argument for a non-type, non-template |
| 2637 | // template-parameter shall be one of: [...] |
| 2638 | // |
| 2639 | // -- a pointer to member expressed as described in 5.3.1. |
Douglas Gregor | a2813ce | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 2640 | DeclRefExpr *DRE = 0; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2641 | |
| 2642 | // Ignore (and complain about) any excess parentheses. |
| 2643 | while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) { |
| 2644 | if (!Invalid) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2645 | Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2646 | diag::err_template_arg_extra_parens) |
| 2647 | << Arg->getSourceRange(); |
| 2648 | Invalid = true; |
| 2649 | } |
| 2650 | |
| 2651 | Arg = Parens->getSubExpr(); |
| 2652 | } |
| 2653 | |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 2654 | // A pointer-to-member constant written &Class::member. |
| 2655 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) { |
Douglas Gregor | a2813ce | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 2656 | if (UnOp->getOpcode() == UnaryOperator::AddrOf) { |
| 2657 | DRE = dyn_cast<DeclRefExpr>(UnOp->getSubExpr()); |
| 2658 | if (DRE && !DRE->getQualifier()) |
| 2659 | DRE = 0; |
| 2660 | } |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 2661 | } |
| 2662 | // A constant of pointer-to-member type. |
| 2663 | else if ((DRE = dyn_cast<DeclRefExpr>(Arg))) { |
| 2664 | if (ValueDecl *VD = dyn_cast<ValueDecl>(DRE->getDecl())) { |
| 2665 | if (VD->getType()->isMemberPointerType()) { |
| 2666 | if (isa<NonTypeTemplateParmDecl>(VD) || |
| 2667 | (isa<VarDecl>(VD) && |
| 2668 | Context.getCanonicalType(VD->getType()).isConstQualified())) { |
| 2669 | if (Arg->isTypeDependent() || Arg->isValueDependent()) |
| 2670 | Converted = TemplateArgument(Arg->Retain()); |
| 2671 | else |
| 2672 | Converted = TemplateArgument(VD->getCanonicalDecl()); |
| 2673 | return Invalid; |
| 2674 | } |
| 2675 | } |
| 2676 | } |
| 2677 | |
| 2678 | DRE = 0; |
| 2679 | } |
| 2680 | |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2681 | if (!DRE) |
| 2682 | return Diag(Arg->getSourceRange().getBegin(), |
| 2683 | diag::err_template_arg_not_pointer_to_member_form) |
| 2684 | << Arg->getSourceRange(); |
| 2685 | |
| 2686 | if (isa<FieldDecl>(DRE->getDecl()) || isa<CXXMethodDecl>(DRE->getDecl())) { |
| 2687 | assert((isa<FieldDecl>(DRE->getDecl()) || |
| 2688 | !cast<CXXMethodDecl>(DRE->getDecl())->isStatic()) && |
| 2689 | "Only non-static member pointers can make it here"); |
| 2690 | |
| 2691 | // Okay: this is the address of a non-static member, and therefore |
| 2692 | // a member pointer constant. |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 2693 | if (Arg->isTypeDependent() || Arg->isValueDependent()) |
| 2694 | Converted = TemplateArgument(Arg->Retain()); |
| 2695 | else |
| 2696 | Converted = TemplateArgument(DRE->getDecl()->getCanonicalDecl()); |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2697 | return Invalid; |
| 2698 | } |
| 2699 | |
| 2700 | // 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] | 2701 | Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2702 | diag::err_template_arg_not_pointer_to_member_form) |
| 2703 | << Arg->getSourceRange(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2704 | Diag(DRE->getDecl()->getLocation(), |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2705 | diag::note_template_arg_refers_here); |
| 2706 | return true; |
| 2707 | } |
| 2708 | |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2709 | /// \brief Check a template argument against its corresponding |
| 2710 | /// non-type template parameter. |
| 2711 | /// |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 2712 | /// This routine implements the semantics of C++ [temp.arg.nontype]. |
| 2713 | /// It returns true if an error occurred, and false otherwise. \p |
| 2714 | /// InstantiatedParamType is the type of the non-type template |
| 2715 | /// parameter after it has been instantiated. |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 2716 | /// |
Douglas Gregor | 02cbbd2 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 2717 | /// If no error was detected, Converted receives the converted template argument. |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2718 | bool Sema::CheckTemplateArgument(NonTypeTemplateParmDecl *Param, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2719 | QualType InstantiatedParamType, Expr *&Arg, |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 2720 | TemplateArgument &Converted, |
| 2721 | CheckTemplateArgumentKind CTAK) { |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2722 | SourceLocation StartLoc = Arg->getSourceRange().getBegin(); |
| 2723 | |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 2724 | // If either the parameter has a dependent type or the argument is |
| 2725 | // type-dependent, there's nothing we can check now. |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2726 | if (InstantiatedParamType->isDependentType() || Arg->isTypeDependent()) { |
| 2727 | // FIXME: Produce a cloned, canonical expression? |
Douglas Gregor | 02cbbd2 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 2728 | Converted = TemplateArgument(Arg); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 2729 | return false; |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2730 | } |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 2731 | |
| 2732 | // C++ [temp.arg.nontype]p5: |
| 2733 | // The following conversions are performed on each expression used |
| 2734 | // as a non-type template-argument. If a non-type |
| 2735 | // template-argument cannot be converted to the type of the |
| 2736 | // corresponding template-parameter then the program is |
| 2737 | // ill-formed. |
| 2738 | // |
| 2739 | // -- for a non-type template-parameter of integral or |
| 2740 | // enumeration type, integral promotions (4.5) and integral |
| 2741 | // conversions (4.7) are applied. |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 2742 | QualType ParamType = InstantiatedParamType; |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 2743 | QualType ArgType = Arg->getType(); |
Douglas Gregor | 2ade35e | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 2744 | if (ParamType->isIntegralOrEnumerationType()) { |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 2745 | // C++ [temp.arg.nontype]p1: |
| 2746 | // A template-argument for a non-type, non-template |
| 2747 | // template-parameter shall be one of: |
| 2748 | // |
| 2749 | // -- an integral constant-expression of integral or enumeration |
| 2750 | // type; or |
| 2751 | // -- the name of a non-type template-parameter; or |
| 2752 | SourceLocation NonConstantLoc; |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 2753 | llvm::APSInt Value; |
Douglas Gregor | 2ade35e | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 2754 | if (!ArgType->isIntegralOrEnumerationType()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2755 | Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 2756 | diag::err_template_arg_not_integral_or_enumeral) |
| 2757 | << ArgType << Arg->getSourceRange(); |
| 2758 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 2759 | return true; |
| 2760 | } else if (!Arg->isValueDependent() && |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 2761 | !Arg->isIntegerConstantExpr(Value, Context, &NonConstantLoc)) { |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 2762 | Diag(NonConstantLoc, diag::err_template_arg_not_ice) |
| 2763 | << ArgType << Arg->getSourceRange(); |
| 2764 | return true; |
| 2765 | } |
| 2766 | |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 2767 | // From here on out, all we care about are the unqualified forms |
| 2768 | // of the parameter and argument types. |
| 2769 | ParamType = ParamType.getUnqualifiedType(); |
| 2770 | ArgType = ArgType.getUnqualifiedType(); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 2771 | |
| 2772 | // Try to convert the argument to the parameter's type. |
Douglas Gregor | ff52439 | 2009-11-04 21:50:46 +0000 | [diff] [blame] | 2773 | if (Context.hasSameType(ParamType, ArgType)) { |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 2774 | // Okay: no conversion necessary |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 2775 | } else if (CTAK == CTAK_Deduced) { |
| 2776 | // C++ [temp.deduct.type]p17: |
| 2777 | // If, in the declaration of a function template with a non-type |
| 2778 | // template-parameter, the non-type template- parameter is used |
| 2779 | // in an expression in the function parameter-list and, if the |
| 2780 | // corresponding template-argument is deduced, the |
| 2781 | // template-argument type shall match the type of the |
| 2782 | // template-parameter exactly, except that a template-argument |
| 2783 | // deduced from an array bound may be of any integral type. |
| 2784 | Diag(StartLoc, diag::err_deduced_non_type_template_arg_type_mismatch) |
| 2785 | << ArgType << ParamType; |
| 2786 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 2787 | return true; |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 2788 | } else if (IsIntegralPromotion(Arg, ArgType, ParamType) || |
| 2789 | !ParamType->isEnumeralType()) { |
| 2790 | // This is an integral promotion or conversion. |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 2791 | ImpCastExprToType(Arg, ParamType, CastExpr::CK_IntegralCast); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 2792 | } else { |
| 2793 | // We can't perform this conversion. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2794 | Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 2795 | diag::err_template_arg_not_convertible) |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 2796 | << Arg->getType() << InstantiatedParamType << Arg->getSourceRange(); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 2797 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 2798 | return true; |
| 2799 | } |
| 2800 | |
Douglas Gregor | f80a9d5 | 2009-03-14 00:20:21 +0000 | [diff] [blame] | 2801 | QualType IntegerType = Context.getCanonicalType(ParamType); |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2802 | if (const EnumType *Enum = IntegerType->getAs<EnumType>()) |
Douglas Gregor | 02cbbd2 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 2803 | IntegerType = Context.getCanonicalType(Enum->getDecl()->getIntegerType()); |
Douglas Gregor | f80a9d5 | 2009-03-14 00:20:21 +0000 | [diff] [blame] | 2804 | |
| 2805 | if (!Arg->isValueDependent()) { |
Douglas Gregor | 1a6e034 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 2806 | llvm::APSInt OldValue = Value; |
| 2807 | |
| 2808 | // Coerce the template argument's value to the value it will have |
| 2809 | // based on the template parameter's type. |
Douglas Gregor | 0d4fd8e | 2010-03-26 00:39:40 +0000 | [diff] [blame] | 2810 | unsigned AllowedBits = Context.getTypeSize(IntegerType); |
Douglas Gregor | 0d4fd8e | 2010-03-26 00:39:40 +0000 | [diff] [blame] | 2811 | if (Value.getBitWidth() != AllowedBits) |
| 2812 | Value.extOrTrunc(AllowedBits); |
| 2813 | Value.setIsSigned(IntegerType->isSignedIntegerType()); |
Douglas Gregor | 1a6e034 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 2814 | |
| 2815 | // Complain if an unsigned parameter received a negative value. |
| 2816 | if (IntegerType->isUnsignedIntegerType() |
| 2817 | && (OldValue.isSigned() && OldValue.isNegative())) { |
| 2818 | Diag(Arg->getSourceRange().getBegin(), diag::warn_template_arg_negative) |
| 2819 | << OldValue.toString(10) << Value.toString(10) << Param->getType() |
| 2820 | << Arg->getSourceRange(); |
| 2821 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 2822 | } |
| 2823 | |
| 2824 | // Complain if we overflowed the template parameter's type. |
| 2825 | unsigned RequiredBits; |
| 2826 | if (IntegerType->isUnsignedIntegerType()) |
| 2827 | RequiredBits = OldValue.getActiveBits(); |
| 2828 | else if (OldValue.isUnsigned()) |
| 2829 | RequiredBits = OldValue.getActiveBits() + 1; |
| 2830 | else |
| 2831 | RequiredBits = OldValue.getMinSignedBits(); |
| 2832 | if (RequiredBits > AllowedBits) { |
| 2833 | Diag(Arg->getSourceRange().getBegin(), |
| 2834 | diag::warn_template_arg_too_large) |
| 2835 | << OldValue.toString(10) << Value.toString(10) << Param->getType() |
| 2836 | << Arg->getSourceRange(); |
| 2837 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 2838 | } |
Douglas Gregor | f80a9d5 | 2009-03-14 00:20:21 +0000 | [diff] [blame] | 2839 | } |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 2840 | |
Douglas Gregor | 02cbbd2 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 2841 | // Add the value of this argument to the list of converted |
| 2842 | // arguments. We use the bitwidth and signedness of the template |
| 2843 | // parameter. |
| 2844 | if (Arg->isValueDependent()) { |
| 2845 | // The argument is value-dependent. Create a new |
| 2846 | // TemplateArgument with the converted expression. |
| 2847 | Converted = TemplateArgument(Arg); |
| 2848 | return false; |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 2849 | } |
| 2850 | |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2851 | Converted = TemplateArgument(Value, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2852 | ParamType->isEnumeralType() ? ParamType |
Douglas Gregor | 02cbbd2 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 2853 | : IntegerType); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 2854 | return false; |
| 2855 | } |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 2856 | |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2857 | DeclAccessPair FoundResult; // temporary for ResolveOverloadedFunction |
| 2858 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2859 | // C++0x [temp.arg.nontype]p5 bullets 2, 4 and 6 permit conversion |
| 2860 | // from a template argument of type std::nullptr_t to a non-type |
| 2861 | // template parameter of type pointer to object, pointer to |
| 2862 | // function, or pointer-to-member, respectively. |
| 2863 | if (ArgType->isNullPtrType() && |
| 2864 | (ParamType->isPointerType() || ParamType->isMemberPointerType())) { |
| 2865 | Converted = TemplateArgument((NamedDecl *)0); |
| 2866 | return false; |
| 2867 | } |
| 2868 | |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 2869 | // Handle pointer-to-function, reference-to-function, and |
| 2870 | // pointer-to-member-function all in (roughly) the same way. |
| 2871 | if (// -- For a non-type template-parameter of type pointer to |
| 2872 | // function, only the function-to-pointer conversion (4.3) is |
| 2873 | // applied. If the template-argument represents a set of |
| 2874 | // overloaded functions (or a pointer to such), the matching |
| 2875 | // function is selected from the set (13.4). |
| 2876 | (ParamType->isPointerType() && |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2877 | ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType()) || |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 2878 | // -- For a non-type template-parameter of type reference to |
| 2879 | // function, no conversions apply. If the template-argument |
| 2880 | // represents a set of overloaded functions, the matching |
| 2881 | // function is selected from the set (13.4). |
| 2882 | (ParamType->isReferenceType() && |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2883 | ParamType->getAs<ReferenceType>()->getPointeeType()->isFunctionType()) || |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 2884 | // -- For a non-type template-parameter of type pointer to |
| 2885 | // member function, no conversions apply. If the |
| 2886 | // template-argument represents a set of overloaded member |
| 2887 | // functions, the matching member function is selected from |
| 2888 | // the set (13.4). |
| 2889 | (ParamType->isMemberPointerType() && |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2890 | ParamType->getAs<MemberPointerType>()->getPointeeType() |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 2891 | ->isFunctionType())) { |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2892 | |
Douglas Gregor | 1a8cf73 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 2893 | if (Arg->getType() == Context.OverloadTy) { |
| 2894 | if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg, ParamType, |
| 2895 | true, |
| 2896 | FoundResult)) { |
| 2897 | if (DiagnoseUseOfDecl(Fn, Arg->getSourceRange().getBegin())) |
| 2898 | return true; |
| 2899 | |
| 2900 | Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn); |
| 2901 | ArgType = Arg->getType(); |
| 2902 | } else |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 2903 | return true; |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 2904 | } |
Douglas Gregor | 1a8cf73 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 2905 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2906 | if (!ParamType->isMemberPointerType()) |
| 2907 | return CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param, |
| 2908 | ParamType, |
| 2909 | Arg, Converted); |
| 2910 | |
| 2911 | if (IsQualificationConversion(ArgType, ParamType.getNonReferenceType())) { |
| 2912 | ImpCastExprToType(Arg, ParamType, CastExpr::CK_NoOp, |
| 2913 | Arg->isLvalue(Context) == Expr::LV_Valid); |
| 2914 | } else if (!Context.hasSameUnqualifiedType(ArgType, |
| 2915 | ParamType.getNonReferenceType())) { |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 2916 | // We can't perform this conversion. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2917 | Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 2918 | diag::err_template_arg_not_convertible) |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 2919 | << Arg->getType() << InstantiatedParamType << Arg->getSourceRange(); |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 2920 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 2921 | return true; |
| 2922 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2923 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2924 | return CheckTemplateArgumentPointerToMember(Arg, Converted); |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 2925 | } |
| 2926 | |
Chris Lattner | fe90de7 | 2009-02-20 21:37:53 +0000 | [diff] [blame] | 2927 | if (ParamType->isPointerType()) { |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 2928 | // -- for a non-type template-parameter of type pointer to |
| 2929 | // object, qualification conversions (4.4) and the |
| 2930 | // array-to-pointer conversion (4.2) are applied. |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 2931 | // C++0x also allows a value of std::nullptr_t. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2932 | assert(ParamType->getAs<PointerType>()->getPointeeType()->isObjectType() && |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 2933 | "Only object pointers allowed here"); |
Douglas Gregor | f684e6e | 2009-02-11 00:44:29 +0000 | [diff] [blame] | 2934 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2935 | return CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param, |
| 2936 | ParamType, |
| 2937 | Arg, Converted); |
Douglas Gregor | f684e6e | 2009-02-11 00:44:29 +0000 | [diff] [blame] | 2938 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2939 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2940 | if (const ReferenceType *ParamRefType = ParamType->getAs<ReferenceType>()) { |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 2941 | // -- For a non-type template-parameter of type reference to |
| 2942 | // object, no conversions apply. The type referred to by the |
| 2943 | // reference may be more cv-qualified than the (otherwise |
| 2944 | // identical) type of the template-argument. The |
| 2945 | // template-parameter is bound directly to the |
| 2946 | // template-argument, which must be an lvalue. |
Douglas Gregor | bad0e65 | 2009-03-24 20:32:41 +0000 | [diff] [blame] | 2947 | assert(ParamRefType->getPointeeType()->isObjectType() && |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 2948 | "Only object references allowed here"); |
Douglas Gregor | f684e6e | 2009-02-11 00:44:29 +0000 | [diff] [blame] | 2949 | |
Douglas Gregor | 1a8cf73 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 2950 | if (Arg->getType() == Context.OverloadTy) { |
| 2951 | if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg, |
| 2952 | ParamRefType->getPointeeType(), |
| 2953 | true, |
| 2954 | FoundResult)) { |
| 2955 | if (DiagnoseUseOfDecl(Fn, Arg->getSourceRange().getBegin())) |
| 2956 | return true; |
| 2957 | |
| 2958 | Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn); |
| 2959 | ArgType = Arg->getType(); |
| 2960 | } else |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2961 | return true; |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 2962 | } |
Douglas Gregor | 1a8cf73 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 2963 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2964 | return CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param, |
| 2965 | ParamType, |
| 2966 | Arg, Converted); |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 2967 | } |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 2968 | |
| 2969 | // -- For a non-type template-parameter of type pointer to data |
| 2970 | // member, qualification conversions (4.4) are applied. |
| 2971 | assert(ParamType->isMemberPointerType() && "Only pointers to members remain"); |
| 2972 | |
Douglas Gregor | 8e6563b | 2009-02-11 18:22:40 +0000 | [diff] [blame] | 2973 | if (Context.hasSameUnqualifiedType(ParamType, ArgType)) { |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 2974 | // Types match exactly: nothing more to do here. |
| 2975 | } else if (IsQualificationConversion(ArgType, ParamType)) { |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2976 | ImpCastExprToType(Arg, ParamType, CastExpr::CK_NoOp, |
| 2977 | Arg->isLvalue(Context) == Expr::LV_Valid); |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 2978 | } else { |
| 2979 | // We can't perform this conversion. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2980 | Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 2981 | diag::err_template_arg_not_convertible) |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 2982 | << Arg->getType() << InstantiatedParamType << Arg->getSourceRange(); |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 2983 | Diag(Param->getLocation(), diag::note_template_param_here); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2984 | return true; |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 2985 | } |
| 2986 | |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 2987 | return CheckTemplateArgumentPointerToMember(Arg, Converted); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2988 | } |
| 2989 | |
| 2990 | /// \brief Check a template argument against its corresponding |
| 2991 | /// template template parameter. |
| 2992 | /// |
| 2993 | /// This routine implements the semantics of C++ [temp.arg.template]. |
| 2994 | /// It returns true if an error occurred, and false otherwise. |
| 2995 | bool Sema::CheckTemplateArgument(TemplateTemplateParmDecl *Param, |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2996 | const TemplateArgumentLoc &Arg) { |
| 2997 | TemplateName Name = Arg.getArgument().getAsTemplate(); |
| 2998 | TemplateDecl *Template = Name.getAsTemplateDecl(); |
| 2999 | if (!Template) { |
| 3000 | // Any dependent template name is fine. |
| 3001 | assert(Name.isDependent() && "Non-dependent template isn't a declaration?"); |
| 3002 | return false; |
| 3003 | } |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 3004 | |
| 3005 | // C++ [temp.arg.template]p1: |
| 3006 | // A template-argument for a template template-parameter shall be |
| 3007 | // the name of a class template, expressed as id-expression. Only |
| 3008 | // primary class templates are considered when matching the |
| 3009 | // template template argument with the corresponding parameter; |
| 3010 | // partial specializations are not considered even if their |
| 3011 | // parameter lists match that of the template template parameter. |
Douglas Gregor | ba1ecb5 | 2009-06-12 19:43:02 +0000 | [diff] [blame] | 3012 | // |
| 3013 | // Note that we also allow template template parameters here, which |
| 3014 | // will happen when we are dealing with, e.g., class template |
| 3015 | // partial specializations. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3016 | if (!isa<ClassTemplateDecl>(Template) && |
Douglas Gregor | ba1ecb5 | 2009-06-12 19:43:02 +0000 | [diff] [blame] | 3017 | !isa<TemplateTemplateParmDecl>(Template)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3018 | assert(isa<FunctionTemplateDecl>(Template) && |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 3019 | "Only function templates are possible here"); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 3020 | Diag(Arg.getLocation(), diag::err_template_arg_not_class_template); |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3021 | Diag(Template->getLocation(), diag::note_template_arg_refers_here_func) |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 3022 | << Template; |
| 3023 | } |
| 3024 | |
| 3025 | return !TemplateParameterListsAreEqual(Template->getTemplateParameters(), |
| 3026 | Param->getTemplateParameters(), |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 3027 | true, |
| 3028 | TPL_TemplateTemplateArgumentMatch, |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 3029 | Arg.getLocation()); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3030 | } |
| 3031 | |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3032 | /// \brief Given a non-type template argument that refers to a |
| 3033 | /// declaration and the type of its corresponding non-type template |
| 3034 | /// parameter, produce an expression that properly refers to that |
| 3035 | /// declaration. |
| 3036 | Sema::OwningExprResult |
| 3037 | Sema::BuildExpressionFromDeclTemplateArgument(const TemplateArgument &Arg, |
| 3038 | QualType ParamType, |
| 3039 | SourceLocation Loc) { |
| 3040 | assert(Arg.getKind() == TemplateArgument::Declaration && |
| 3041 | "Only declaration template arguments permitted here"); |
| 3042 | ValueDecl *VD = cast<ValueDecl>(Arg.getAsDecl()); |
| 3043 | |
| 3044 | if (VD->getDeclContext()->isRecord() && |
| 3045 | (isa<CXXMethodDecl>(VD) || isa<FieldDecl>(VD))) { |
| 3046 | // If the value is a class member, we might have a pointer-to-member. |
| 3047 | // Determine whether the non-type template template parameter is of |
| 3048 | // pointer-to-member type. If so, we need to build an appropriate |
| 3049 | // expression for a pointer-to-member, since a "normal" DeclRefExpr |
| 3050 | // would refer to the member itself. |
| 3051 | if (ParamType->isMemberPointerType()) { |
| 3052 | QualType ClassType |
| 3053 | = Context.getTypeDeclType(cast<RecordDecl>(VD->getDeclContext())); |
| 3054 | NestedNameSpecifier *Qualifier |
| 3055 | = NestedNameSpecifier::Create(Context, 0, false, ClassType.getTypePtr()); |
| 3056 | CXXScopeSpec SS; |
| 3057 | SS.setScopeRep(Qualifier); |
| 3058 | OwningExprResult RefExpr = BuildDeclRefExpr(VD, |
| 3059 | VD->getType().getNonReferenceType(), |
| 3060 | Loc, |
| 3061 | &SS); |
| 3062 | if (RefExpr.isInvalid()) |
| 3063 | return ExprError(); |
| 3064 | |
| 3065 | RefExpr = CreateBuiltinUnaryOp(Loc, UnaryOperator::AddrOf, move(RefExpr)); |
Douglas Gregor | c0c8300 | 2010-04-30 21:46:38 +0000 | [diff] [blame] | 3066 | |
| 3067 | // We might need to perform a trailing qualification conversion, since |
| 3068 | // the element type on the parameter could be more qualified than the |
| 3069 | // element type in the expression we constructed. |
| 3070 | if (IsQualificationConversion(((Expr*) RefExpr.get())->getType(), |
| 3071 | ParamType.getUnqualifiedType())) { |
| 3072 | Expr *RefE = RefExpr.takeAs<Expr>(); |
| 3073 | ImpCastExprToType(RefE, ParamType.getUnqualifiedType(), |
| 3074 | CastExpr::CK_NoOp); |
| 3075 | RefExpr = Owned(RefE); |
| 3076 | } |
| 3077 | |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3078 | assert(!RefExpr.isInvalid() && |
| 3079 | Context.hasSameType(((Expr*) RefExpr.get())->getType(), |
Douglas Gregor | c0c8300 | 2010-04-30 21:46:38 +0000 | [diff] [blame] | 3080 | ParamType.getUnqualifiedType())); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3081 | return move(RefExpr); |
| 3082 | } |
| 3083 | } |
| 3084 | |
| 3085 | QualType T = VD->getType().getNonReferenceType(); |
| 3086 | if (ParamType->isPointerType()) { |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3087 | // When the non-type template parameter is a pointer, take the |
| 3088 | // address of the declaration. |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3089 | OwningExprResult RefExpr = BuildDeclRefExpr(VD, T, Loc); |
| 3090 | if (RefExpr.isInvalid()) |
| 3091 | return ExprError(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3092 | |
| 3093 | if (T->isFunctionType() || T->isArrayType()) { |
| 3094 | // Decay functions and arrays. |
| 3095 | Expr *RefE = (Expr *)RefExpr.get(); |
| 3096 | DefaultFunctionArrayConversion(RefE); |
| 3097 | if (RefE != RefExpr.get()) { |
| 3098 | RefExpr.release(); |
| 3099 | RefExpr = Owned(RefE); |
| 3100 | } |
| 3101 | |
| 3102 | return move(RefExpr); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3103 | } |
| 3104 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3105 | // Take the address of everything else |
| 3106 | return CreateBuiltinUnaryOp(Loc, UnaryOperator::AddrOf, move(RefExpr)); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3107 | } |
| 3108 | |
| 3109 | // If the non-type template parameter has reference type, qualify the |
| 3110 | // resulting declaration reference with the extra qualifiers on the |
| 3111 | // type that the reference refers to. |
| 3112 | if (const ReferenceType *TargetRef = ParamType->getAs<ReferenceType>()) |
| 3113 | T = Context.getQualifiedType(T, TargetRef->getPointeeType().getQualifiers()); |
| 3114 | |
| 3115 | return BuildDeclRefExpr(VD, T, Loc); |
| 3116 | } |
| 3117 | |
| 3118 | /// \brief Construct a new expression that refers to the given |
| 3119 | /// integral template argument with the given source-location |
| 3120 | /// information. |
| 3121 | /// |
| 3122 | /// This routine takes care of the mapping from an integral template |
| 3123 | /// argument (which may have any integral type) to the appropriate |
| 3124 | /// literal value. |
| 3125 | Sema::OwningExprResult |
| 3126 | Sema::BuildExpressionFromIntegralTemplateArgument(const TemplateArgument &Arg, |
| 3127 | SourceLocation Loc) { |
| 3128 | assert(Arg.getKind() == TemplateArgument::Integral && |
| 3129 | "Operation is only value for integral template arguments"); |
| 3130 | QualType T = Arg.getIntegralType(); |
| 3131 | if (T->isCharType() || T->isWideCharType()) |
| 3132 | return Owned(new (Context) CharacterLiteral( |
| 3133 | Arg.getAsIntegral()->getZExtValue(), |
| 3134 | T->isWideCharType(), |
| 3135 | T, |
| 3136 | Loc)); |
| 3137 | if (T->isBooleanType()) |
| 3138 | return Owned(new (Context) CXXBoolLiteralExpr( |
| 3139 | Arg.getAsIntegral()->getBoolValue(), |
| 3140 | T, |
| 3141 | Loc)); |
| 3142 | |
| 3143 | return Owned(new (Context) IntegerLiteral(*Arg.getAsIntegral(), T, Loc)); |
| 3144 | } |
| 3145 | |
| 3146 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3147 | /// \brief Determine whether the given template parameter lists are |
| 3148 | /// equivalent. |
| 3149 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3150 | /// \param New The new template parameter list, typically written in the |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3151 | /// source code as part of a new template declaration. |
| 3152 | /// |
| 3153 | /// \param Old The old template parameter list, typically found via |
| 3154 | /// name lookup of the template declared with this template parameter |
| 3155 | /// list. |
| 3156 | /// |
| 3157 | /// \param Complain If true, this routine will produce a diagnostic if |
| 3158 | /// the template parameter lists are not equivalent. |
| 3159 | /// |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 3160 | /// \param Kind describes how we are to match the template parameter lists. |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 3161 | /// |
| 3162 | /// \param TemplateArgLoc If this source location is valid, then we |
| 3163 | /// are actually checking the template parameter list of a template |
| 3164 | /// argument (New) against the template parameter list of its |
| 3165 | /// corresponding template template parameter (Old). We produce |
| 3166 | /// slightly different diagnostics in this scenario. |
| 3167 | /// |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3168 | /// \returns True if the template parameter lists are equal, false |
| 3169 | /// otherwise. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3170 | bool |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3171 | Sema::TemplateParameterListsAreEqual(TemplateParameterList *New, |
| 3172 | TemplateParameterList *Old, |
| 3173 | bool Complain, |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 3174 | TemplateParameterListEqualKind Kind, |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 3175 | SourceLocation TemplateArgLoc) { |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3176 | if (Old->size() != New->size()) { |
| 3177 | if (Complain) { |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 3178 | unsigned NextDiag = diag::err_template_param_list_different_arity; |
| 3179 | if (TemplateArgLoc.isValid()) { |
| 3180 | Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch); |
| 3181 | NextDiag = diag::note_template_param_list_different_arity; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3182 | } |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 3183 | Diag(New->getTemplateLoc(), NextDiag) |
| 3184 | << (New->size() > Old->size()) |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 3185 | << (Kind != TPL_TemplateMatch) |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 3186 | << SourceRange(New->getTemplateLoc(), New->getRAngleLoc()); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3187 | Diag(Old->getTemplateLoc(), diag::note_template_prev_declaration) |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 3188 | << (Kind != TPL_TemplateMatch) |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3189 | << SourceRange(Old->getTemplateLoc(), Old->getRAngleLoc()); |
| 3190 | } |
| 3191 | |
| 3192 | return false; |
| 3193 | } |
| 3194 | |
| 3195 | for (TemplateParameterList::iterator OldParm = Old->begin(), |
| 3196 | OldParmEnd = Old->end(), NewParm = New->begin(); |
| 3197 | OldParm != OldParmEnd; ++OldParm, ++NewParm) { |
| 3198 | if ((*OldParm)->getKind() != (*NewParm)->getKind()) { |
Douglas Gregor | 34d1dc9 | 2009-06-24 16:50:40 +0000 | [diff] [blame] | 3199 | if (Complain) { |
| 3200 | unsigned NextDiag = diag::err_template_param_different_kind; |
| 3201 | if (TemplateArgLoc.isValid()) { |
| 3202 | Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch); |
| 3203 | NextDiag = diag::note_template_param_different_kind; |
| 3204 | } |
| 3205 | Diag((*NewParm)->getLocation(), NextDiag) |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 3206 | << (Kind != TPL_TemplateMatch); |
Douglas Gregor | 34d1dc9 | 2009-06-24 16:50:40 +0000 | [diff] [blame] | 3207 | Diag((*OldParm)->getLocation(), diag::note_template_prev_declaration) |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 3208 | << (Kind != TPL_TemplateMatch); |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 3209 | } |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3210 | return false; |
| 3211 | } |
| 3212 | |
Douglas Gregor | a417b87 | 2010-06-04 08:34:32 +0000 | [diff] [blame] | 3213 | if (TemplateTypeParmDecl *OldTTP |
| 3214 | = dyn_cast<TemplateTypeParmDecl>(*OldParm)) { |
| 3215 | // Template type parameters are equivalent if either both are template |
| 3216 | // type parameter packs or neither are (since we know we're at the same |
| 3217 | // index). |
| 3218 | TemplateTypeParmDecl *NewTTP = cast<TemplateTypeParmDecl>(*NewParm); |
| 3219 | if (OldTTP->isParameterPack() != NewTTP->isParameterPack()) { |
| 3220 | // FIXME: Implement the rules in C++0x [temp.arg.template]p5 that |
| 3221 | // allow one to match a template parameter pack in the template |
| 3222 | // parameter list of a template template parameter to one or more |
| 3223 | // template parameters in the template parameter list of the |
| 3224 | // corresponding template template argument. |
| 3225 | if (Complain) { |
| 3226 | unsigned NextDiag = diag::err_template_parameter_pack_non_pack; |
| 3227 | if (TemplateArgLoc.isValid()) { |
| 3228 | Diag(TemplateArgLoc, |
| 3229 | diag::err_template_arg_template_params_mismatch); |
| 3230 | NextDiag = diag::note_template_parameter_pack_non_pack; |
| 3231 | } |
| 3232 | Diag(NewTTP->getLocation(), NextDiag) |
| 3233 | << 0 << NewTTP->isParameterPack(); |
| 3234 | Diag(OldTTP->getLocation(), diag::note_template_parameter_pack_here) |
| 3235 | << 0 << OldTTP->isParameterPack(); |
| 3236 | } |
| 3237 | return false; |
| 3238 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3239 | } else if (NonTypeTemplateParmDecl *OldNTTP |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3240 | = dyn_cast<NonTypeTemplateParmDecl>(*OldParm)) { |
| 3241 | // The types of non-type template parameters must agree. |
| 3242 | NonTypeTemplateParmDecl *NewNTTP |
| 3243 | = cast<NonTypeTemplateParmDecl>(*NewParm); |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 3244 | |
| 3245 | // If we are matching a template template argument to a template |
| 3246 | // template parameter and one of the non-type template parameter types |
| 3247 | // is dependent, then we must wait until template instantiation time |
| 3248 | // to actually compare the arguments. |
| 3249 | if (Kind == TPL_TemplateTemplateArgumentMatch && |
| 3250 | (OldNTTP->getType()->isDependentType() || |
| 3251 | NewNTTP->getType()->isDependentType())) |
| 3252 | continue; |
| 3253 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3254 | if (Context.getCanonicalType(OldNTTP->getType()) != |
| 3255 | Context.getCanonicalType(NewNTTP->getType())) { |
| 3256 | if (Complain) { |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 3257 | unsigned NextDiag = diag::err_template_nontype_parm_different_type; |
| 3258 | if (TemplateArgLoc.isValid()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3259 | Diag(TemplateArgLoc, |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 3260 | diag::err_template_arg_template_params_mismatch); |
| 3261 | NextDiag = diag::note_template_nontype_parm_different_type; |
| 3262 | } |
| 3263 | Diag(NewNTTP->getLocation(), NextDiag) |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3264 | << NewNTTP->getType() |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 3265 | << (Kind != TPL_TemplateMatch); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3266 | Diag(OldNTTP->getLocation(), |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3267 | diag::note_template_nontype_parm_prev_declaration) |
| 3268 | << OldNTTP->getType(); |
| 3269 | } |
| 3270 | return false; |
| 3271 | } |
| 3272 | } else { |
| 3273 | // The template parameter lists of template template |
| 3274 | // parameters must agree. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3275 | assert(isa<TemplateTemplateParmDecl>(*OldParm) && |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3276 | "Only template template parameters handled here"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3277 | TemplateTemplateParmDecl *OldTTP |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3278 | = cast<TemplateTemplateParmDecl>(*OldParm); |
| 3279 | TemplateTemplateParmDecl *NewTTP |
| 3280 | = cast<TemplateTemplateParmDecl>(*NewParm); |
| 3281 | if (!TemplateParameterListsAreEqual(NewTTP->getTemplateParameters(), |
| 3282 | OldTTP->getTemplateParameters(), |
| 3283 | Complain, |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 3284 | (Kind == TPL_TemplateMatch? TPL_TemplateTemplateParmMatch : Kind), |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 3285 | TemplateArgLoc)) |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3286 | return false; |
| 3287 | } |
| 3288 | } |
| 3289 | |
| 3290 | return true; |
| 3291 | } |
| 3292 | |
| 3293 | /// \brief Check whether a template can be declared within this scope. |
| 3294 | /// |
| 3295 | /// If the template declaration is valid in this scope, returns |
| 3296 | /// false. Otherwise, issues a diagnostic and returns true. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3297 | bool |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 3298 | Sema::CheckTemplateDeclScope(Scope *S, TemplateParameterList *TemplateParams) { |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3299 | // Find the nearest enclosing declaration scope. |
| 3300 | while ((S->getFlags() & Scope::DeclScope) == 0 || |
| 3301 | (S->getFlags() & Scope::TemplateParamScope) != 0) |
| 3302 | S = S->getParent(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3303 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3304 | // C++ [temp]p2: |
| 3305 | // A template-declaration can appear only as a namespace scope or |
| 3306 | // class scope declaration. |
| 3307 | DeclContext *Ctx = static_cast<DeclContext *>(S->getEntity()); |
Eli Friedman | 1503f77 | 2009-07-31 01:43:05 +0000 | [diff] [blame] | 3308 | if (Ctx && isa<LinkageSpecDecl>(Ctx) && |
| 3309 | cast<LinkageSpecDecl>(Ctx)->getLanguage() != LinkageSpecDecl::lang_cxx) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3310 | return Diag(TemplateParams->getTemplateLoc(), diag::err_template_linkage) |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 3311 | << TemplateParams->getSourceRange(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3312 | |
Eli Friedman | 1503f77 | 2009-07-31 01:43:05 +0000 | [diff] [blame] | 3313 | while (Ctx && isa<LinkageSpecDecl>(Ctx)) |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3314 | Ctx = Ctx->getParent(); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3315 | |
| 3316 | if (Ctx && (Ctx->isFileContext() || Ctx->isRecord())) |
| 3317 | return false; |
| 3318 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3319 | return Diag(TemplateParams->getTemplateLoc(), |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 3320 | diag::err_template_outside_namespace_or_class_scope) |
| 3321 | << TemplateParams->getSourceRange(); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3322 | } |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3323 | |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3324 | /// \brief Determine what kind of template specialization the given declaration |
| 3325 | /// is. |
| 3326 | static TemplateSpecializationKind getTemplateSpecializationKind(NamedDecl *D) { |
| 3327 | if (!D) |
| 3328 | return TSK_Undeclared; |
| 3329 | |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 3330 | if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) |
| 3331 | return Record->getTemplateSpecializationKind(); |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3332 | if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) |
| 3333 | return Function->getTemplateSpecializationKind(); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 3334 | if (VarDecl *Var = dyn_cast<VarDecl>(D)) |
| 3335 | return Var->getTemplateSpecializationKind(); |
| 3336 | |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3337 | return TSK_Undeclared; |
| 3338 | } |
| 3339 | |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 3340 | /// \brief Check whether a specialization is well-formed in the current |
| 3341 | /// context. |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 3342 | /// |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 3343 | /// This routine determines whether a template specialization can be declared |
| 3344 | /// in the current context (C++ [temp.expl.spec]p2). |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3345 | /// |
| 3346 | /// \param S the semantic analysis object for which this check is being |
| 3347 | /// performed. |
| 3348 | /// |
| 3349 | /// \param Specialized the entity being specialized or instantiated, which |
| 3350 | /// may be a kind of template (class template, function template, etc.) or |
| 3351 | /// a member of a class template (member function, static data member, |
| 3352 | /// member class). |
| 3353 | /// |
| 3354 | /// \param PrevDecl the previous declaration of this entity, if any. |
| 3355 | /// |
| 3356 | /// \param Loc the location of the explicit specialization or instantiation of |
| 3357 | /// this entity. |
| 3358 | /// |
| 3359 | /// \param IsPartialSpecialization whether this is a partial specialization of |
| 3360 | /// a class template. |
| 3361 | /// |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3362 | /// \returns true if there was an error that we cannot recover from, false |
| 3363 | /// otherwise. |
| 3364 | static bool CheckTemplateSpecializationScope(Sema &S, |
| 3365 | NamedDecl *Specialized, |
| 3366 | NamedDecl *PrevDecl, |
| 3367 | SourceLocation Loc, |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 3368 | bool IsPartialSpecialization) { |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3369 | // Keep these "kind" numbers in sync with the %select statements in the |
| 3370 | // various diagnostics emitted by this routine. |
| 3371 | int EntityKind = 0; |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 3372 | bool isTemplateSpecialization = false; |
| 3373 | if (isa<ClassTemplateDecl>(Specialized)) { |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3374 | EntityKind = IsPartialSpecialization? 1 : 0; |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 3375 | isTemplateSpecialization = true; |
| 3376 | } else if (isa<FunctionTemplateDecl>(Specialized)) { |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3377 | EntityKind = 2; |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 3378 | isTemplateSpecialization = true; |
| 3379 | } else if (isa<CXXMethodDecl>(Specialized)) |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3380 | EntityKind = 3; |
| 3381 | else if (isa<VarDecl>(Specialized)) |
| 3382 | EntityKind = 4; |
| 3383 | else if (isa<RecordDecl>(Specialized)) |
| 3384 | EntityKind = 5; |
| 3385 | else { |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 3386 | S.Diag(Loc, diag::err_template_spec_unknown_kind); |
| 3387 | S.Diag(Specialized->getLocation(), diag::note_specialized_entity); |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3388 | return true; |
| 3389 | } |
| 3390 | |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 3391 | // C++ [temp.expl.spec]p2: |
| 3392 | // An explicit specialization shall be declared in the namespace |
| 3393 | // of which the template is a member, or, for member templates, in |
| 3394 | // the namespace of which the enclosing class or enclosing class |
| 3395 | // template is a member. An explicit specialization of a member |
| 3396 | // function, member class or static data member of a class |
| 3397 | // template shall be declared in the namespace of which the class |
| 3398 | // template is a member. Such a declaration may also be a |
| 3399 | // definition. If the declaration is not a definition, the |
| 3400 | // specialization may be defined later in the name- space in which |
| 3401 | // the explicit specialization was declared, or in a namespace |
| 3402 | // that encloses the one in which the explicit specialization was |
| 3403 | // declared. |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3404 | if (S.CurContext->getLookupContext()->isFunctionOrMethod()) { |
| 3405 | S.Diag(Loc, diag::err_template_spec_decl_function_scope) |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 3406 | << Specialized; |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 3407 | return true; |
| 3408 | } |
Douglas Gregor | 7974c3b | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 3409 | |
Douglas Gregor | 0a40747 | 2009-10-07 17:30:37 +0000 | [diff] [blame] | 3410 | if (S.CurContext->isRecord() && !IsPartialSpecialization) { |
| 3411 | S.Diag(Loc, diag::err_template_spec_decl_class_scope) |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 3412 | << Specialized; |
Douglas Gregor | 0a40747 | 2009-10-07 17:30:37 +0000 | [diff] [blame] | 3413 | return true; |
| 3414 | } |
| 3415 | |
Douglas Gregor | 7974c3b | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 3416 | // C++ [temp.class.spec]p6: |
| 3417 | // A class template partial specialization may be declared or redeclared |
| 3418 | // in any namespace scope in which its definition may be defined (14.5.1 |
| 3419 | // and 14.5.2). |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3420 | bool ComplainedAboutScope = false; |
Douglas Gregor | 7974c3b | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 3421 | DeclContext *SpecializedContext |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3422 | = Specialized->getDeclContext()->getEnclosingNamespaceContext(); |
Douglas Gregor | 7974c3b | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 3423 | DeclContext *DC = S.CurContext->getEnclosingNamespaceContext(); |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 3424 | if ((!PrevDecl || |
| 3425 | getTemplateSpecializationKind(PrevDecl) == TSK_Undeclared || |
| 3426 | getTemplateSpecializationKind(PrevDecl) == TSK_ImplicitInstantiation)){ |
| 3427 | // There is no prior declaration of this entity, so this |
| 3428 | // specialization must be in the same context as the template |
| 3429 | // itself. |
| 3430 | if (!DC->Equals(SpecializedContext)) { |
| 3431 | if (isa<TranslationUnitDecl>(SpecializedContext)) |
| 3432 | S.Diag(Loc, diag::err_template_spec_decl_out_of_scope_global) |
| 3433 | << EntityKind << Specialized; |
| 3434 | else if (isa<NamespaceDecl>(SpecializedContext)) |
| 3435 | S.Diag(Loc, diag::err_template_spec_decl_out_of_scope) |
| 3436 | << EntityKind << Specialized |
| 3437 | << cast<NamedDecl>(SpecializedContext); |
| 3438 | |
| 3439 | S.Diag(Specialized->getLocation(), diag::note_specialized_entity); |
| 3440 | ComplainedAboutScope = true; |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 3441 | } |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 3442 | } |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3443 | |
| 3444 | // Make sure that this redeclaration (or definition) occurs in an enclosing |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 3445 | // namespace. |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3446 | // Note that HandleDeclarator() performs this check for explicit |
| 3447 | // specializations of function templates, static data members, and member |
| 3448 | // functions, so we skip the check here for those kinds of entities. |
| 3449 | // FIXME: HandleDeclarator's diagnostics aren't quite as good, though. |
Douglas Gregor | 7974c3b | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 3450 | // Should we refactor that check, so that it occurs later? |
| 3451 | if (!ComplainedAboutScope && !DC->Encloses(SpecializedContext) && |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 3452 | !(isa<FunctionTemplateDecl>(Specialized) || isa<VarDecl>(Specialized) || |
| 3453 | isa<FunctionDecl>(Specialized))) { |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3454 | if (isa<TranslationUnitDecl>(SpecializedContext)) |
| 3455 | S.Diag(Loc, diag::err_template_spec_redecl_global_scope) |
| 3456 | << EntityKind << Specialized; |
| 3457 | else if (isa<NamespaceDecl>(SpecializedContext)) |
| 3458 | S.Diag(Loc, diag::err_template_spec_redecl_out_of_scope) |
| 3459 | << EntityKind << Specialized |
| 3460 | << cast<NamedDecl>(SpecializedContext); |
| 3461 | |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 3462 | S.Diag(Specialized->getLocation(), diag::note_specialized_entity); |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 3463 | } |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3464 | |
| 3465 | // FIXME: check for specialization-after-instantiation errors and such. |
| 3466 | |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 3467 | return false; |
| 3468 | } |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3469 | |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 3470 | /// \brief Check the non-type template arguments of a class template |
| 3471 | /// partial specialization according to C++ [temp.class.spec]p9. |
| 3472 | /// |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3473 | /// \param TemplateParams the template parameters of the primary class |
| 3474 | /// template. |
| 3475 | /// |
| 3476 | /// \param TemplateArg the template arguments of the class template |
| 3477 | /// partial specialization. |
| 3478 | /// |
| 3479 | /// \param MirrorsPrimaryTemplate will be set true if the class |
| 3480 | /// template partial specialization arguments are identical to the |
| 3481 | /// implicit template arguments of the primary template. This is not |
| 3482 | /// necessarily an error (C++0x), and it is left to the caller to diagnose |
| 3483 | /// this condition when it is an error. |
| 3484 | /// |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 3485 | /// \returns true if there was an error, false otherwise. |
| 3486 | bool Sema::CheckClassTemplatePartialSpecializationArgs( |
| 3487 | TemplateParameterList *TemplateParams, |
Anders Carlsson | 6360be7 | 2009-06-13 18:20:51 +0000 | [diff] [blame] | 3488 | const TemplateArgumentListBuilder &TemplateArgs, |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3489 | bool &MirrorsPrimaryTemplate) { |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 3490 | // FIXME: the interface to this function will have to change to |
| 3491 | // accommodate variadic templates. |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3492 | MirrorsPrimaryTemplate = true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3493 | |
Anders Carlsson | fb25052 | 2009-06-23 01:26:57 +0000 | [diff] [blame] | 3494 | const TemplateArgument *ArgList = TemplateArgs.getFlatArguments(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3495 | |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 3496 | for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) { |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3497 | // Determine whether the template argument list of the partial |
| 3498 | // specialization is identical to the implicit argument list of |
| 3499 | // the primary template. The caller may need to diagnostic this as |
| 3500 | // an error per C++ [temp.class.spec]p9b3. |
| 3501 | if (MirrorsPrimaryTemplate) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3502 | if (TemplateTypeParmDecl *TTP |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3503 | = dyn_cast<TemplateTypeParmDecl>(TemplateParams->getParam(I))) { |
| 3504 | if (Context.getCanonicalType(Context.getTypeDeclType(TTP)) != |
Anders Carlsson | 6360be7 | 2009-06-13 18:20:51 +0000 | [diff] [blame] | 3505 | Context.getCanonicalType(ArgList[I].getAsType())) |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3506 | MirrorsPrimaryTemplate = false; |
| 3507 | } else if (TemplateTemplateParmDecl *TTP |
| 3508 | = dyn_cast<TemplateTemplateParmDecl>( |
| 3509 | TemplateParams->getParam(I))) { |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 3510 | TemplateName Name = ArgList[I].getAsTemplate(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3511 | TemplateTemplateParmDecl *ArgDecl |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 3512 | = dyn_cast_or_null<TemplateTemplateParmDecl>(Name.getAsTemplateDecl()); |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3513 | if (!ArgDecl || |
| 3514 | ArgDecl->getIndex() != TTP->getIndex() || |
| 3515 | ArgDecl->getDepth() != TTP->getDepth()) |
| 3516 | MirrorsPrimaryTemplate = false; |
| 3517 | } |
| 3518 | } |
| 3519 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3520 | NonTypeTemplateParmDecl *Param |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 3521 | = dyn_cast<NonTypeTemplateParmDecl>(TemplateParams->getParam(I)); |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3522 | if (!Param) { |
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 | } |
| 3525 | |
Anders Carlsson | 6360be7 | 2009-06-13 18:20:51 +0000 | [diff] [blame] | 3526 | Expr *ArgExpr = ArgList[I].getAsExpr(); |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3527 | if (!ArgExpr) { |
| 3528 | MirrorsPrimaryTemplate = false; |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 3529 | continue; |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3530 | } |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 3531 | |
| 3532 | // C++ [temp.class.spec]p8: |
| 3533 | // A non-type argument is non-specialized if it is the name of a |
| 3534 | // non-type parameter. All other non-type arguments are |
| 3535 | // specialized. |
| 3536 | // |
| 3537 | // Below, we check the two conditions that only apply to |
| 3538 | // specialized non-type arguments, so skip any non-specialized |
| 3539 | // arguments. |
| 3540 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ArgExpr)) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3541 | if (NonTypeTemplateParmDecl *NTTP |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3542 | = dyn_cast<NonTypeTemplateParmDecl>(DRE->getDecl())) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3543 | if (MirrorsPrimaryTemplate && |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3544 | (Param->getIndex() != NTTP->getIndex() || |
| 3545 | Param->getDepth() != NTTP->getDepth())) |
| 3546 | MirrorsPrimaryTemplate = false; |
| 3547 | |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 3548 | continue; |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3549 | } |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 3550 | |
| 3551 | // C++ [temp.class.spec]p9: |
| 3552 | // Within the argument list of a class template partial |
| 3553 | // specialization, the following restrictions apply: |
| 3554 | // -- A partially specialized non-type argument expression |
| 3555 | // shall not involve a template parameter of the partial |
| 3556 | // specialization except when the argument expression is a |
| 3557 | // simple identifier. |
| 3558 | if (ArgExpr->isTypeDependent() || ArgExpr->isValueDependent()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3559 | Diag(ArgExpr->getLocStart(), |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 3560 | diag::err_dependent_non_type_arg_in_partial_spec) |
| 3561 | << ArgExpr->getSourceRange(); |
| 3562 | return true; |
| 3563 | } |
| 3564 | |
| 3565 | // -- The type of a template parameter corresponding to a |
| 3566 | // specialized non-type argument shall not be dependent on a |
| 3567 | // parameter of the specialization. |
| 3568 | if (Param->getType()->isDependentType()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3569 | Diag(ArgExpr->getLocStart(), |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 3570 | diag::err_dependent_typed_non_type_arg_in_partial_spec) |
| 3571 | << Param->getType() |
| 3572 | << ArgExpr->getSourceRange(); |
| 3573 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 3574 | return true; |
| 3575 | } |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3576 | |
| 3577 | MirrorsPrimaryTemplate = false; |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 3578 | } |
| 3579 | |
| 3580 | return false; |
| 3581 | } |
| 3582 | |
Douglas Gregor | dc0a11c | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 3583 | /// \brief Retrieve the previous declaration of the given declaration. |
| 3584 | static NamedDecl *getPreviousDecl(NamedDecl *ND) { |
| 3585 | if (VarDecl *VD = dyn_cast<VarDecl>(ND)) |
| 3586 | return VD->getPreviousDeclaration(); |
| 3587 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) |
| 3588 | return FD->getPreviousDeclaration(); |
| 3589 | if (TagDecl *TD = dyn_cast<TagDecl>(ND)) |
| 3590 | return TD->getPreviousDeclaration(); |
| 3591 | if (TypedefDecl *TD = dyn_cast<TypedefDecl>(ND)) |
| 3592 | return TD->getPreviousDeclaration(); |
| 3593 | if (FunctionTemplateDecl *FTD = dyn_cast<FunctionTemplateDecl>(ND)) |
| 3594 | return FTD->getPreviousDeclaration(); |
| 3595 | if (ClassTemplateDecl *CTD = dyn_cast<ClassTemplateDecl>(ND)) |
| 3596 | return CTD->getPreviousDeclaration(); |
| 3597 | return 0; |
| 3598 | } |
| 3599 | |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 3600 | Sema::DeclResult |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 3601 | Sema::ActOnClassTemplateSpecialization(Scope *S, unsigned TagSpec, |
| 3602 | TagUseKind TUK, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3603 | SourceLocation KWLoc, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 3604 | CXXScopeSpec &SS, |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 3605 | TemplateTy TemplateD, |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3606 | SourceLocation TemplateNameLoc, |
| 3607 | SourceLocation LAngleLoc, |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 3608 | ASTTemplateArgsPtr TemplateArgsIn, |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3609 | SourceLocation RAngleLoc, |
| 3610 | AttributeList *Attr, |
| 3611 | MultiTemplateParamsArg TemplateParameterLists) { |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 3612 | assert(TUK != TUK_Reference && "References are not specializations"); |
John McCall | f1bbbb4 | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 3613 | |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3614 | // Find the class template we're specializing |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 3615 | TemplateName Name = TemplateD.getAsVal<TemplateName>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3616 | ClassTemplateDecl *ClassTemplate |
Douglas Gregor | 8b13c08 | 2009-11-12 00:46:20 +0000 | [diff] [blame] | 3617 | = dyn_cast_or_null<ClassTemplateDecl>(Name.getAsTemplateDecl()); |
| 3618 | |
| 3619 | if (!ClassTemplate) { |
| 3620 | Diag(TemplateNameLoc, diag::err_not_class_template_specialization) |
| 3621 | << (Name.getAsTemplateDecl() && |
| 3622 | isa<TemplateTemplateParmDecl>(Name.getAsTemplateDecl())); |
| 3623 | return true; |
| 3624 | } |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3625 | |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 3626 | bool isExplicitSpecialization = false; |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 3627 | bool isPartialSpecialization = false; |
| 3628 | |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 3629 | // Check the validity of the template headers that introduce this |
| 3630 | // template. |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 3631 | // FIXME: We probably shouldn't complain about these headers for |
| 3632 | // friend declarations. |
Douglas Gregor | 0167f3c | 2010-07-14 23:14:12 +0000 | [diff] [blame] | 3633 | bool Invalid = false; |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 3634 | TemplateParameterList *TemplateParams |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3635 | = MatchTemplateParametersToScopeSpecifier(TemplateNameLoc, SS, |
| 3636 | (TemplateParameterList**)TemplateParameterLists.get(), |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 3637 | TemplateParameterLists.size(), |
John McCall | 77e8b11 | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 3638 | TUK == TUK_Friend, |
Douglas Gregor | 0167f3c | 2010-07-14 23:14:12 +0000 | [diff] [blame] | 3639 | isExplicitSpecialization, |
| 3640 | Invalid); |
| 3641 | if (Invalid) |
| 3642 | return true; |
| 3643 | |
Abramo Bagnara | 9b93488 | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 3644 | unsigned NumMatchedTemplateParamLists = TemplateParameterLists.size(); |
| 3645 | if (TemplateParams) |
| 3646 | --NumMatchedTemplateParamLists; |
| 3647 | |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 3648 | if (TemplateParams && TemplateParams->size() > 0) { |
| 3649 | isPartialSpecialization = true; |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 3650 | |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 3651 | // C++ [temp.class.spec]p10: |
| 3652 | // The template parameter list of a specialization shall not |
| 3653 | // contain default template argument values. |
| 3654 | for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) { |
| 3655 | Decl *Param = TemplateParams->getParam(I); |
| 3656 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) { |
| 3657 | if (TTP->hasDefaultArgument()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3658 | Diag(TTP->getDefaultArgumentLoc(), |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 3659 | diag::err_default_arg_in_partial_spec); |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 3660 | TTP->removeDefaultArgument(); |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 3661 | } |
| 3662 | } else if (NonTypeTemplateParmDecl *NTTP |
| 3663 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
| 3664 | if (Expr *DefArg = NTTP->getDefaultArgument()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3665 | Diag(NTTP->getDefaultArgumentLoc(), |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 3666 | diag::err_default_arg_in_partial_spec) |
| 3667 | << DefArg->getSourceRange(); |
Abramo Bagnara | d92f7a2 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 3668 | NTTP->removeDefaultArgument(); |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 3669 | DefArg->Destroy(Context); |
| 3670 | } |
| 3671 | } else { |
| 3672 | TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(Param); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 3673 | if (TTP->hasDefaultArgument()) { |
| 3674 | Diag(TTP->getDefaultArgument().getLocation(), |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 3675 | diag::err_default_arg_in_partial_spec) |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 3676 | << TTP->getDefaultArgument().getSourceRange(); |
Abramo Bagnara | d92f7a2 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 3677 | TTP->removeDefaultArgument(); |
Douglas Gregor | ba1ecb5 | 2009-06-12 19:43:02 +0000 | [diff] [blame] | 3678 | } |
| 3679 | } |
| 3680 | } |
Douglas Gregor | a735b20 | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 3681 | } else if (TemplateParams) { |
| 3682 | if (TUK == TUK_Friend) |
| 3683 | Diag(KWLoc, diag::err_template_spec_friend) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 3684 | << FixItHint::CreateRemoval( |
Douglas Gregor | a735b20 | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 3685 | SourceRange(TemplateParams->getTemplateLoc(), |
| 3686 | TemplateParams->getRAngleLoc())) |
| 3687 | << SourceRange(LAngleLoc, RAngleLoc); |
| 3688 | else |
| 3689 | isExplicitSpecialization = true; |
| 3690 | } else if (TUK != TUK_Friend) { |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 3691 | Diag(KWLoc, diag::err_template_spec_needs_header) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 3692 | << FixItHint::CreateInsertion(KWLoc, "template<> "); |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 3693 | isExplicitSpecialization = true; |
| 3694 | } |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 3695 | |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3696 | // Check that the specialization uses the same tag kind as the |
| 3697 | // original template. |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 3698 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
| 3699 | assert(Kind != TTK_Enum && "Invalid enum tag in class template spec!"); |
Douglas Gregor | 501c5ce | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 3700 | if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3701 | Kind, KWLoc, |
Douglas Gregor | 501c5ce | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 3702 | *ClassTemplate->getIdentifier())) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3703 | Diag(KWLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 3704 | << ClassTemplate |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 3705 | << FixItHint::CreateReplacement(KWLoc, |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 3706 | ClassTemplate->getTemplatedDecl()->getKindName()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3707 | Diag(ClassTemplate->getTemplatedDecl()->getLocation(), |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3708 | diag::note_previous_use); |
| 3709 | Kind = ClassTemplate->getTemplatedDecl()->getTagKind(); |
| 3710 | } |
| 3711 | |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 3712 | // Translate the parser's template argument list in our AST format. |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 3713 | TemplateArgumentListInfo TemplateArgs; |
| 3714 | TemplateArgs.setLAngleLoc(LAngleLoc); |
| 3715 | TemplateArgs.setRAngleLoc(RAngleLoc); |
Douglas Gregor | 314b97f | 2009-11-10 19:49:08 +0000 | [diff] [blame] | 3716 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 3717 | |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3718 | // Check that the template argument list is well-formed for this |
| 3719 | // template. |
Anders Carlsson | fb25052 | 2009-06-23 01:26:57 +0000 | [diff] [blame] | 3720 | TemplateArgumentListBuilder Converted(ClassTemplate->getTemplateParameters(), |
| 3721 | TemplateArgs.size()); |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 3722 | if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc, |
| 3723 | TemplateArgs, false, Converted)) |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 3724 | return true; |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3725 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3726 | assert((Converted.structuredSize() == |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3727 | ClassTemplate->getTemplateParameters()->size()) && |
| 3728 | "Converted template argument list is too short!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3729 | |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 3730 | // Find the class template (partial) specialization declaration that |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3731 | // corresponds to these arguments. |
| 3732 | llvm::FoldingSetNodeID ID; |
Douglas Gregor | ba1ecb5 | 2009-06-12 19:43:02 +0000 | [diff] [blame] | 3733 | if (isPartialSpecialization) { |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3734 | bool MirrorsPrimaryTemplate; |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 3735 | if (CheckClassTemplatePartialSpecializationArgs( |
| 3736 | ClassTemplate->getTemplateParameters(), |
Anders Carlsson | fb25052 | 2009-06-23 01:26:57 +0000 | [diff] [blame] | 3737 | Converted, MirrorsPrimaryTemplate)) |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 3738 | return true; |
| 3739 | |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3740 | if (MirrorsPrimaryTemplate) { |
| 3741 | // C++ [temp.class.spec]p9b3: |
| 3742 | // |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3743 | // -- The argument list of the specialization shall not be identical |
| 3744 | // to the implicit argument list of the primary template. |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3745 | Diag(TemplateNameLoc, diag::err_partial_spec_args_match_primary_template) |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 3746 | << (TUK == TUK_Definition) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 3747 | << FixItHint::CreateRemoval(SourceRange(LAngleLoc, RAngleLoc)); |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 3748 | return CheckClassTemplate(S, TagSpec, TUK, KWLoc, SS, |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3749 | ClassTemplate->getIdentifier(), |
| 3750 | TemplateNameLoc, |
| 3751 | Attr, |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 3752 | TemplateParams, |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3753 | AS_none); |
| 3754 | } |
| 3755 | |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 3756 | // FIXME: Diagnose friend partial specializations |
| 3757 | |
Douglas Gregor | de09096 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 3758 | if (!Name.isDependent() && |
| 3759 | !TemplateSpecializationType::anyDependentTemplateArguments( |
| 3760 | TemplateArgs.getArgumentArray(), |
| 3761 | TemplateArgs.size())) { |
| 3762 | Diag(TemplateNameLoc, diag::err_partial_spec_fully_specialized) |
| 3763 | << ClassTemplate->getDeclName(); |
| 3764 | isPartialSpecialization = false; |
| 3765 | } else { |
| 3766 | // FIXME: Template parameter list matters, too |
| 3767 | ClassTemplatePartialSpecializationDecl::Profile(ID, |
| 3768 | Converted.getFlatArguments(), |
| 3769 | Converted.flatSize(), |
| 3770 | Context); |
| 3771 | } |
| 3772 | } |
| 3773 | |
| 3774 | if (!isPartialSpecialization) |
Anders Carlsson | 1c5976e | 2009-06-05 03:43:12 +0000 | [diff] [blame] | 3775 | ClassTemplateSpecializationDecl::Profile(ID, |
Anders Carlsson | fb25052 | 2009-06-23 01:26:57 +0000 | [diff] [blame] | 3776 | Converted.getFlatArguments(), |
Douglas Gregor | 828e226 | 2009-07-29 16:09:57 +0000 | [diff] [blame] | 3777 | Converted.flatSize(), |
| 3778 | Context); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3779 | void *InsertPos = 0; |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 3780 | ClassTemplateSpecializationDecl *PrevDecl = 0; |
| 3781 | |
| 3782 | if (isPartialSpecialization) |
| 3783 | PrevDecl |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3784 | = ClassTemplate->getPartialSpecializations().FindNodeOrInsertPos(ID, |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 3785 | InsertPos); |
| 3786 | else |
| 3787 | PrevDecl |
| 3788 | = ClassTemplate->getSpecializations().FindNodeOrInsertPos(ID, InsertPos); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3789 | |
| 3790 | ClassTemplateSpecializationDecl *Specialization = 0; |
| 3791 | |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 3792 | // Check whether we can declare a class template specialization in |
| 3793 | // the current scope. |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 3794 | if (TUK != TUK_Friend && |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3795 | CheckTemplateSpecializationScope(*this, ClassTemplate, PrevDecl, |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 3796 | TemplateNameLoc, |
| 3797 | isPartialSpecialization)) |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 3798 | return true; |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 3799 | |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 3800 | // The canonical type |
| 3801 | QualType CanonType; |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 3802 | if (PrevDecl && |
| 3803 | (PrevDecl->getSpecializationKind() == TSK_Undeclared || |
Douglas Gregor | de09096 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 3804 | TUK == TUK_Friend)) { |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3805 | // Since the only prior class template specialization with these |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 3806 | // arguments was referenced but not declared, or we're only |
| 3807 | // referencing this specialization as a friend, reuse that |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3808 | // declaration node as our own, updating its source location to |
| 3809 | // reflect our new declaration. |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3810 | Specialization = PrevDecl; |
Douglas Gregor | 6bc9f7e | 2009-02-25 22:18:32 +0000 | [diff] [blame] | 3811 | Specialization->setLocation(TemplateNameLoc); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3812 | PrevDecl = 0; |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 3813 | CanonType = Context.getTypeDeclType(Specialization); |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 3814 | } else if (isPartialSpecialization) { |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 3815 | // Build the canonical type that describes the converted template |
| 3816 | // arguments of the class template partial specialization. |
Douglas Gregor | de09096 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 3817 | TemplateName CanonTemplate = Context.getCanonicalTemplateName(Name); |
| 3818 | CanonType = Context.getTemplateSpecializationType(CanonTemplate, |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 3819 | Converted.getFlatArguments(), |
| 3820 | Converted.flatSize()); |
| 3821 | |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 3822 | // Create a new class template partial specialization declaration node. |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 3823 | ClassTemplatePartialSpecializationDecl *PrevPartial |
| 3824 | = cast_or_null<ClassTemplatePartialSpecializationDecl>(PrevDecl); |
Douglas Gregor | dc60c1e | 2010-04-30 05:56:50 +0000 | [diff] [blame] | 3825 | unsigned SequenceNumber = PrevPartial? PrevPartial->getSequenceNumber() |
| 3826 | : ClassTemplate->getPartialSpecializations().size(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3827 | ClassTemplatePartialSpecializationDecl *Partial |
Douglas Gregor | 13c8577 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 3828 | = ClassTemplatePartialSpecializationDecl::Create(Context, Kind, |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 3829 | ClassTemplate->getDeclContext(), |
Anders Carlsson | 91fdf6f | 2009-06-05 04:06:48 +0000 | [diff] [blame] | 3830 | TemplateNameLoc, |
| 3831 | TemplateParams, |
| 3832 | ClassTemplate, |
Anders Carlsson | fb25052 | 2009-06-23 01:26:57 +0000 | [diff] [blame] | 3833 | Converted, |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 3834 | TemplateArgs, |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 3835 | CanonType, |
Douglas Gregor | dc60c1e | 2010-04-30 05:56:50 +0000 | [diff] [blame] | 3836 | PrevPartial, |
| 3837 | SequenceNumber); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 3838 | SetNestedNameSpecifier(Partial, SS); |
Abramo Bagnara | 9b93488 | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 3839 | if (NumMatchedTemplateParamLists > 0) { |
Douglas Gregor | c722ea4 | 2010-06-15 17:44:38 +0000 | [diff] [blame] | 3840 | Partial->setTemplateParameterListsInfo(Context, |
| 3841 | NumMatchedTemplateParamLists, |
Abramo Bagnara | 9b93488 | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 3842 | (TemplateParameterList**) TemplateParameterLists.release()); |
| 3843 | } |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 3844 | |
| 3845 | if (PrevPartial) { |
| 3846 | ClassTemplate->getPartialSpecializations().RemoveNode(PrevPartial); |
| 3847 | ClassTemplate->getPartialSpecializations().GetOrInsertNode(Partial); |
| 3848 | } else { |
| 3849 | ClassTemplate->getPartialSpecializations().InsertNode(Partial, InsertPos); |
| 3850 | } |
| 3851 | Specialization = Partial; |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 3852 | |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 3853 | // If we are providing an explicit specialization of a member class |
| 3854 | // template specialization, make a note of that. |
| 3855 | if (PrevPartial && PrevPartial->getInstantiatedFromMember()) |
| 3856 | PrevPartial->setMemberSpecialization(); |
| 3857 | |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 3858 | // Check that all of the template parameters of the class template |
| 3859 | // partial specialization are deducible from the template |
| 3860 | // arguments. If not, this class template partial specialization |
| 3861 | // will never be used. |
| 3862 | llvm::SmallVector<bool, 8> DeducibleParams; |
| 3863 | DeducibleParams.resize(TemplateParams->size()); |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 3864 | MarkUsedTemplateParameters(Partial->getTemplateArgs(), true, |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 3865 | TemplateParams->getDepth(), |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 3866 | DeducibleParams); |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 3867 | unsigned NumNonDeducible = 0; |
| 3868 | for (unsigned I = 0, N = DeducibleParams.size(); I != N; ++I) |
| 3869 | if (!DeducibleParams[I]) |
| 3870 | ++NumNonDeducible; |
| 3871 | |
| 3872 | if (NumNonDeducible) { |
| 3873 | Diag(TemplateNameLoc, diag::warn_partial_specs_not_deducible) |
| 3874 | << (NumNonDeducible > 1) |
| 3875 | << SourceRange(TemplateNameLoc, RAngleLoc); |
| 3876 | for (unsigned I = 0, N = DeducibleParams.size(); I != N; ++I) { |
| 3877 | if (!DeducibleParams[I]) { |
| 3878 | NamedDecl *Param = cast<NamedDecl>(TemplateParams->getParam(I)); |
| 3879 | if (Param->getDeclName()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3880 | Diag(Param->getLocation(), |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 3881 | diag::note_partial_spec_unused_parameter) |
| 3882 | << Param->getDeclName(); |
| 3883 | else |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3884 | Diag(Param->getLocation(), |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 3885 | diag::note_partial_spec_unused_parameter) |
| 3886 | << std::string("<anonymous>"); |
| 3887 | } |
| 3888 | } |
| 3889 | } |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3890 | } else { |
| 3891 | // Create a new class template specialization declaration node for |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 3892 | // this explicit specialization or friend declaration. |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3893 | Specialization |
Douglas Gregor | 13c8577 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 3894 | = ClassTemplateSpecializationDecl::Create(Context, Kind, |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3895 | ClassTemplate->getDeclContext(), |
| 3896 | TemplateNameLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3897 | ClassTemplate, |
Anders Carlsson | fb25052 | 2009-06-23 01:26:57 +0000 | [diff] [blame] | 3898 | Converted, |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3899 | PrevDecl); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 3900 | SetNestedNameSpecifier(Specialization, SS); |
Abramo Bagnara | 9b93488 | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 3901 | if (NumMatchedTemplateParamLists > 0) { |
Douglas Gregor | c722ea4 | 2010-06-15 17:44:38 +0000 | [diff] [blame] | 3902 | Specialization->setTemplateParameterListsInfo(Context, |
| 3903 | NumMatchedTemplateParamLists, |
Abramo Bagnara | 9b93488 | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 3904 | (TemplateParameterList**) TemplateParameterLists.release()); |
| 3905 | } |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3906 | |
| 3907 | if (PrevDecl) { |
| 3908 | ClassTemplate->getSpecializations().RemoveNode(PrevDecl); |
| 3909 | ClassTemplate->getSpecializations().GetOrInsertNode(Specialization); |
| 3910 | } else { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3911 | ClassTemplate->getSpecializations().InsertNode(Specialization, |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3912 | InsertPos); |
| 3913 | } |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 3914 | |
| 3915 | CanonType = Context.getTypeDeclType(Specialization); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3916 | } |
| 3917 | |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 3918 | // C++ [temp.expl.spec]p6: |
| 3919 | // If a template, a member template or the member of a class template is |
| 3920 | // explicitly specialized then that specialization shall be declared |
| 3921 | // before the first use of that specialization that would cause an implicit |
| 3922 | // instantiation to take place, in every translation unit in which such a |
| 3923 | // use occurs; no diagnostic is required. |
| 3924 | if (PrevDecl && PrevDecl->getPointOfInstantiation().isValid()) { |
Douglas Gregor | dc0a11c | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 3925 | bool Okay = false; |
| 3926 | for (NamedDecl *Prev = PrevDecl; Prev; Prev = getPreviousDecl(Prev)) { |
| 3927 | // Is there any previous explicit specialization declaration? |
| 3928 | if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) { |
| 3929 | Okay = true; |
| 3930 | break; |
| 3931 | } |
| 3932 | } |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 3933 | |
Douglas Gregor | dc0a11c | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 3934 | if (!Okay) { |
| 3935 | SourceRange Range(TemplateNameLoc, RAngleLoc); |
| 3936 | Diag(TemplateNameLoc, diag::err_specialization_after_instantiation) |
| 3937 | << Context.getTypeDeclType(Specialization) << Range; |
| 3938 | |
| 3939 | Diag(PrevDecl->getPointOfInstantiation(), |
| 3940 | diag::note_instantiation_required_here) |
| 3941 | << (PrevDecl->getTemplateSpecializationKind() |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 3942 | != TSK_ImplicitInstantiation); |
Douglas Gregor | dc0a11c | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 3943 | return true; |
| 3944 | } |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 3945 | } |
| 3946 | |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 3947 | // If this is not a friend, note that this is an explicit specialization. |
| 3948 | if (TUK != TUK_Friend) |
| 3949 | Specialization->setSpecializationKind(TSK_ExplicitSpecialization); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3950 | |
| 3951 | // Check that this isn't a redefinition of this specialization. |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 3952 | if (TUK == TUK_Definition) { |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 3953 | if (RecordDecl *Def = Specialization->getDefinition()) { |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3954 | SourceRange Range(TemplateNameLoc, RAngleLoc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3955 | Diag(TemplateNameLoc, diag::err_redefinition) |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 3956 | << Context.getTypeDeclType(Specialization) << Range; |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3957 | Diag(Def->getLocation(), diag::note_previous_definition); |
| 3958 | Specialization->setInvalidDecl(); |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 3959 | return true; |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3960 | } |
| 3961 | } |
| 3962 | |
Douglas Gregor | fc705b8 | 2009-02-26 22:19:44 +0000 | [diff] [blame] | 3963 | // Build the fully-sugared type for this class template |
| 3964 | // specialization as the user wrote in the specialization |
| 3965 | // itself. This means that we'll pretty-print the type retrieved |
| 3966 | // from the specialization's declaration the way that the user |
| 3967 | // actually wrote the specialization, rather than formatting the |
| 3968 | // name based on the "canonical" representation used to store the |
| 3969 | // template arguments in the specialization. |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 3970 | TypeSourceInfo *WrittenTy |
| 3971 | = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc, |
| 3972 | TemplateArgs, CanonType); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 3973 | if (TUK != TUK_Friend) { |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 3974 | Specialization->setTypeAsWritten(WrittenTy); |
Douglas Gregor | 7e9b57b | 2010-07-06 18:33:12 +0000 | [diff] [blame] | 3975 | if (TemplateParams) |
| 3976 | Specialization->setTemplateKeywordLoc(TemplateParams->getTemplateLoc()); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 3977 | } |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 3978 | TemplateArgsIn.release(); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3979 | |
Douglas Gregor | 6bc9f7e | 2009-02-25 22:18:32 +0000 | [diff] [blame] | 3980 | // C++ [temp.expl.spec]p9: |
| 3981 | // A template explicit specialization is in the scope of the |
| 3982 | // namespace in which the template was defined. |
| 3983 | // |
| 3984 | // We actually implement this paragraph where we set the semantic |
| 3985 | // context (in the creation of the ClassTemplateSpecializationDecl), |
| 3986 | // but we also maintain the lexical context where the actual |
| 3987 | // definition occurs. |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3988 | Specialization->setLexicalDeclContext(CurContext); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3989 | |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3990 | // We may be starting the definition of this specialization. |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 3991 | if (TUK == TUK_Definition) |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3992 | Specialization->startDefinition(); |
| 3993 | |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 3994 | if (TUK == TUK_Friend) { |
| 3995 | FriendDecl *Friend = FriendDecl::Create(Context, CurContext, |
| 3996 | TemplateNameLoc, |
John McCall | 32f2fb5 | 2010-03-25 18:04:51 +0000 | [diff] [blame] | 3997 | WrittenTy, |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 3998 | /*FIXME:*/KWLoc); |
| 3999 | Friend->setAccess(AS_public); |
| 4000 | CurContext->addDecl(Friend); |
| 4001 | } else { |
| 4002 | // Add the specialization into its lexical context, so that it can |
| 4003 | // be seen when iterating through the list of declarations in that |
| 4004 | // context. However, specializations are not found by name lookup. |
| 4005 | CurContext->addDecl(Specialization); |
| 4006 | } |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 4007 | return DeclPtrTy::make(Specialization); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4008 | } |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 4009 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4010 | Sema::DeclPtrTy |
| 4011 | Sema::ActOnTemplateDeclarator(Scope *S, |
Douglas Gregor | e542c86 | 2009-06-23 23:11:28 +0000 | [diff] [blame] | 4012 | MultiTemplateParamsArg TemplateParameterLists, |
| 4013 | Declarator &D) { |
| 4014 | return HandleDeclarator(S, D, move(TemplateParameterLists), false); |
| 4015 | } |
| 4016 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4017 | Sema::DeclPtrTy |
| 4018 | Sema::ActOnStartOfFunctionTemplateDef(Scope *FnBodyScope, |
Douglas Gregor | 52591bf | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 4019 | MultiTemplateParamsArg TemplateParameterLists, |
| 4020 | Declarator &D) { |
| 4021 | assert(getCurFunctionDecl() == 0 && "Function parsing confused"); |
| 4022 | assert(D.getTypeObject(0).Kind == DeclaratorChunk::Function && |
| 4023 | "Not a function declarator!"); |
| 4024 | DeclaratorChunk::FunctionTypeInfo &FTI = D.getTypeObject(0).Fun; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4025 | |
Douglas Gregor | 52591bf | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 4026 | if (FTI.hasPrototype) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4027 | // FIXME: Diagnose arguments without names in C. |
Douglas Gregor | 52591bf | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 4028 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4029 | |
Douglas Gregor | 52591bf | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 4030 | Scope *ParentScope = FnBodyScope->getParent(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4031 | |
| 4032 | DeclPtrTy DP = HandleDeclarator(ParentScope, D, |
Douglas Gregor | 52591bf | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 4033 | move(TemplateParameterLists), |
| 4034 | /*IsFunctionDefinition=*/true); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4035 | if (FunctionTemplateDecl *FunctionTemplate |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 4036 | = dyn_cast_or_null<FunctionTemplateDecl>(DP.getAs<Decl>())) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4037 | return ActOnStartOfFunctionDef(FnBodyScope, |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 4038 | DeclPtrTy::make(FunctionTemplate->getTemplatedDecl())); |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 4039 | if (FunctionDecl *Function = dyn_cast_or_null<FunctionDecl>(DP.getAs<Decl>())) |
| 4040 | return ActOnStartOfFunctionDef(FnBodyScope, DeclPtrTy::make(Function)); |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 4041 | return DeclPtrTy(); |
Douglas Gregor | 52591bf | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 4042 | } |
| 4043 | |
John McCall | 7504239 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 4044 | /// \brief Strips various properties off an implicit instantiation |
| 4045 | /// that has just been explicitly specialized. |
| 4046 | static void StripImplicitInstantiation(NamedDecl *D) { |
| 4047 | D->invalidateAttrs(); |
| 4048 | |
| 4049 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 4050 | FD->setInlineSpecified(false); |
| 4051 | } |
| 4052 | } |
| 4053 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4054 | /// \brief Diagnose cases where we have an explicit template specialization |
| 4055 | /// before/after an explicit template instantiation, producing diagnostics |
| 4056 | /// for those cases where they are required and determining whether the |
| 4057 | /// new specialization/instantiation will have any effect. |
| 4058 | /// |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4059 | /// \param NewLoc the location of the new explicit specialization or |
| 4060 | /// instantiation. |
| 4061 | /// |
| 4062 | /// \param NewTSK the kind of the new explicit specialization or instantiation. |
| 4063 | /// |
| 4064 | /// \param PrevDecl the previous declaration of the entity. |
| 4065 | /// |
| 4066 | /// \param PrevTSK the kind of the old explicit specialization or instantiatin. |
| 4067 | /// |
| 4068 | /// \param PrevPointOfInstantiation if valid, indicates where the previus |
| 4069 | /// declaration was instantiated (either implicitly or explicitly). |
| 4070 | /// |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 4071 | /// \param HasNoEffect will be set to true to indicate that the new |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4072 | /// specialization or instantiation has no effect and should be ignored. |
| 4073 | /// |
| 4074 | /// \returns true if there was an error that should prevent the introduction of |
| 4075 | /// the new declaration into the AST, false otherwise. |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 4076 | bool |
| 4077 | Sema::CheckSpecializationInstantiationRedecl(SourceLocation NewLoc, |
| 4078 | TemplateSpecializationKind NewTSK, |
| 4079 | NamedDecl *PrevDecl, |
| 4080 | TemplateSpecializationKind PrevTSK, |
| 4081 | SourceLocation PrevPointOfInstantiation, |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 4082 | bool &HasNoEffect) { |
| 4083 | HasNoEffect = false; |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4084 | |
| 4085 | switch (NewTSK) { |
| 4086 | case TSK_Undeclared: |
| 4087 | case TSK_ImplicitInstantiation: |
| 4088 | assert(false && "Don't check implicit instantiations here"); |
| 4089 | return false; |
| 4090 | |
| 4091 | case TSK_ExplicitSpecialization: |
| 4092 | switch (PrevTSK) { |
| 4093 | case TSK_Undeclared: |
| 4094 | case TSK_ExplicitSpecialization: |
| 4095 | // Okay, we're just specializing something that is either already |
| 4096 | // explicitly specialized or has merely been mentioned without any |
| 4097 | // instantiation. |
| 4098 | return false; |
| 4099 | |
| 4100 | case TSK_ImplicitInstantiation: |
| 4101 | if (PrevPointOfInstantiation.isInvalid()) { |
| 4102 | // The declaration itself has not actually been instantiated, so it is |
| 4103 | // still okay to specialize it. |
John McCall | 7504239 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 4104 | StripImplicitInstantiation(PrevDecl); |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4105 | return false; |
| 4106 | } |
| 4107 | // Fall through |
| 4108 | |
| 4109 | case TSK_ExplicitInstantiationDeclaration: |
| 4110 | case TSK_ExplicitInstantiationDefinition: |
| 4111 | assert((PrevTSK == TSK_ImplicitInstantiation || |
| 4112 | PrevPointOfInstantiation.isValid()) && |
| 4113 | "Explicit instantiation without point of instantiation?"); |
| 4114 | |
| 4115 | // C++ [temp.expl.spec]p6: |
| 4116 | // If a template, a member template or the member of a class template |
| 4117 | // is explicitly specialized then that specialization shall be declared |
| 4118 | // before the first use of that specialization that would cause an |
| 4119 | // implicit instantiation to take place, in every translation unit in |
| 4120 | // which such a use occurs; no diagnostic is required. |
Douglas Gregor | dc0a11c | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 4121 | for (NamedDecl *Prev = PrevDecl; Prev; Prev = getPreviousDecl(Prev)) { |
| 4122 | // Is there any previous explicit specialization declaration? |
| 4123 | if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) |
| 4124 | return false; |
| 4125 | } |
| 4126 | |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 4127 | Diag(NewLoc, diag::err_specialization_after_instantiation) |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4128 | << PrevDecl; |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 4129 | Diag(PrevPointOfInstantiation, diag::note_instantiation_required_here) |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4130 | << (PrevTSK != TSK_ImplicitInstantiation); |
| 4131 | |
| 4132 | return true; |
| 4133 | } |
| 4134 | break; |
| 4135 | |
| 4136 | case TSK_ExplicitInstantiationDeclaration: |
| 4137 | switch (PrevTSK) { |
| 4138 | case TSK_ExplicitInstantiationDeclaration: |
| 4139 | // This explicit instantiation declaration is redundant (that's okay). |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 4140 | HasNoEffect = true; |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4141 | return false; |
| 4142 | |
| 4143 | case TSK_Undeclared: |
| 4144 | case TSK_ImplicitInstantiation: |
| 4145 | // We're explicitly instantiating something that may have already been |
| 4146 | // implicitly instantiated; that's fine. |
| 4147 | return false; |
| 4148 | |
| 4149 | case TSK_ExplicitSpecialization: |
| 4150 | // C++0x [temp.explicit]p4: |
| 4151 | // For a given set of template parameters, if an explicit instantiation |
| 4152 | // of a template appears after a declaration of an explicit |
| 4153 | // specialization for that template, the explicit instantiation has no |
| 4154 | // effect. |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 4155 | HasNoEffect = true; |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4156 | return false; |
| 4157 | |
| 4158 | case TSK_ExplicitInstantiationDefinition: |
| 4159 | // C++0x [temp.explicit]p10: |
| 4160 | // If an entity is the subject of both an explicit instantiation |
| 4161 | // declaration and an explicit instantiation definition in the same |
| 4162 | // translation unit, the definition shall follow the declaration. |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 4163 | Diag(NewLoc, |
| 4164 | diag::err_explicit_instantiation_declaration_after_definition); |
| 4165 | Diag(PrevPointOfInstantiation, |
| 4166 | diag::note_explicit_instantiation_definition_here); |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4167 | assert(PrevPointOfInstantiation.isValid() && |
| 4168 | "Explicit instantiation without point of instantiation?"); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 4169 | HasNoEffect = true; |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4170 | return false; |
| 4171 | } |
| 4172 | break; |
| 4173 | |
| 4174 | case TSK_ExplicitInstantiationDefinition: |
| 4175 | switch (PrevTSK) { |
| 4176 | case TSK_Undeclared: |
| 4177 | case TSK_ImplicitInstantiation: |
| 4178 | // We're explicitly instantiating something that may have already been |
| 4179 | // implicitly instantiated; that's fine. |
| 4180 | return false; |
| 4181 | |
| 4182 | case TSK_ExplicitSpecialization: |
| 4183 | // C++ DR 259, C++0x [temp.explicit]p4: |
| 4184 | // For a given set of template parameters, if an explicit |
| 4185 | // instantiation of a template appears after a declaration of |
| 4186 | // an explicit specialization for that template, the explicit |
| 4187 | // instantiation has no effect. |
| 4188 | // |
| 4189 | // 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] | 4190 | // is not harmful to try to explicitly instantiate something that |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4191 | // has been explicitly specialized. |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 4192 | if (!getLangOptions().CPlusPlus0x) { |
| 4193 | Diag(NewLoc, diag::ext_explicit_instantiation_after_specialization) |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4194 | << PrevDecl; |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 4195 | Diag(PrevDecl->getLocation(), |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4196 | diag::note_previous_template_specialization); |
| 4197 | } |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 4198 | HasNoEffect = true; |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4199 | return false; |
| 4200 | |
| 4201 | case TSK_ExplicitInstantiationDeclaration: |
| 4202 | // We're explicity instantiating a definition for something for which we |
| 4203 | // were previously asked to suppress instantiations. That's fine. |
| 4204 | return false; |
| 4205 | |
| 4206 | case TSK_ExplicitInstantiationDefinition: |
| 4207 | // C++0x [temp.spec]p5: |
| 4208 | // For a given template and a given set of template-arguments, |
| 4209 | // - an explicit instantiation definition shall appear at most once |
| 4210 | // in a program, |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 4211 | Diag(NewLoc, diag::err_explicit_instantiation_duplicate) |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4212 | << PrevDecl; |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 4213 | Diag(PrevPointOfInstantiation, |
| 4214 | diag::note_previous_explicit_instantiation); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 4215 | HasNoEffect = true; |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4216 | return false; |
| 4217 | } |
| 4218 | break; |
| 4219 | } |
| 4220 | |
| 4221 | assert(false && "Missing specialization/instantiation case?"); |
| 4222 | |
| 4223 | return false; |
| 4224 | } |
| 4225 | |
John McCall | af2094e | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 4226 | /// \brief Perform semantic analysis for the given dependent function |
| 4227 | /// template specialization. The only possible way to get a dependent |
| 4228 | /// function template specialization is with a friend declaration, |
| 4229 | /// like so: |
| 4230 | /// |
| 4231 | /// template <class T> void foo(T); |
| 4232 | /// template <class T> class A { |
| 4233 | /// friend void foo<>(T); |
| 4234 | /// }; |
| 4235 | /// |
| 4236 | /// There really isn't any useful analysis we can do here, so we |
| 4237 | /// just store the information. |
| 4238 | bool |
| 4239 | Sema::CheckDependentFunctionTemplateSpecialization(FunctionDecl *FD, |
| 4240 | const TemplateArgumentListInfo &ExplicitTemplateArgs, |
| 4241 | LookupResult &Previous) { |
| 4242 | // Remove anything from Previous that isn't a function template in |
| 4243 | // the correct context. |
| 4244 | DeclContext *FDLookupContext = FD->getDeclContext()->getLookupContext(); |
| 4245 | LookupResult::Filter F = Previous.makeFilter(); |
| 4246 | while (F.hasNext()) { |
| 4247 | NamedDecl *D = F.next()->getUnderlyingDecl(); |
| 4248 | if (!isa<FunctionTemplateDecl>(D) || |
| 4249 | !FDLookupContext->Equals(D->getDeclContext()->getLookupContext())) |
| 4250 | F.erase(); |
| 4251 | } |
| 4252 | F.done(); |
| 4253 | |
| 4254 | // Should this be diagnosed here? |
| 4255 | if (Previous.empty()) return true; |
| 4256 | |
| 4257 | FD->setDependentTemplateSpecialization(Context, Previous.asUnresolvedSet(), |
| 4258 | ExplicitTemplateArgs); |
| 4259 | return false; |
| 4260 | } |
| 4261 | |
Abramo Bagnara | e03db98 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 4262 | /// \brief Perform semantic analysis for the given function template |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4263 | /// specialization. |
| 4264 | /// |
Abramo Bagnara | e03db98 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 4265 | /// This routine performs all of the semantic analysis required for an |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4266 | /// explicit function template specialization. On successful completion, |
| 4267 | /// the function declaration \p FD will become a function template |
| 4268 | /// specialization. |
| 4269 | /// |
| 4270 | /// \param FD the function declaration, which will be updated to become a |
| 4271 | /// function template specialization. |
| 4272 | /// |
Abramo Bagnara | e03db98 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 4273 | /// \param ExplicitTemplateArgs the explicitly-provided template arguments, |
| 4274 | /// if any. Note that this may be valid info even when 0 arguments are |
| 4275 | /// explicitly provided as in, e.g., \c void sort<>(char*, char*); |
| 4276 | /// as it anyway contains info on the angle brackets locations. |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4277 | /// |
Abramo Bagnara | e03db98 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 4278 | /// \param PrevDecl the set of declarations that may be specialized by |
| 4279 | /// this function specialization. |
| 4280 | bool |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4281 | Sema::CheckFunctionTemplateSpecialization(FunctionDecl *FD, |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 4282 | const TemplateArgumentListInfo *ExplicitTemplateArgs, |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 4283 | LookupResult &Previous) { |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4284 | // The set of function template specializations that could match this |
| 4285 | // explicit function template specialization. |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4286 | UnresolvedSet<8> Candidates; |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4287 | |
| 4288 | DeclContext *FDLookupContext = FD->getDeclContext()->getLookupContext(); |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 4289 | for (LookupResult::iterator I = Previous.begin(), E = Previous.end(); |
| 4290 | I != E; ++I) { |
| 4291 | NamedDecl *Ovl = (*I)->getUnderlyingDecl(); |
| 4292 | if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Ovl)) { |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4293 | // Only consider templates found within the same semantic lookup scope as |
| 4294 | // FD. |
| 4295 | if (!FDLookupContext->Equals(Ovl->getDeclContext()->getLookupContext())) |
| 4296 | continue; |
| 4297 | |
| 4298 | // C++ [temp.expl.spec]p11: |
| 4299 | // A trailing template-argument can be left unspecified in the |
| 4300 | // template-id naming an explicit function template specialization |
| 4301 | // provided it can be deduced from the function argument type. |
| 4302 | // Perform template argument deduction to determine whether we may be |
| 4303 | // specializing this template. |
| 4304 | // FIXME: It is somewhat wasteful to build |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 4305 | TemplateDeductionInfo Info(Context, FD->getLocation()); |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4306 | FunctionDecl *Specialization = 0; |
| 4307 | if (TemplateDeductionResult TDK |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 4308 | = DeduceTemplateArguments(FunTmpl, ExplicitTemplateArgs, |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4309 | FD->getType(), |
| 4310 | Specialization, |
| 4311 | Info)) { |
| 4312 | // FIXME: Template argument deduction failed; record why it failed, so |
| 4313 | // that we can provide nifty diagnostics. |
| 4314 | (void)TDK; |
| 4315 | continue; |
| 4316 | } |
| 4317 | |
| 4318 | // Record this candidate. |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4319 | Candidates.addDecl(Specialization, I.getAccess()); |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4320 | } |
| 4321 | } |
| 4322 | |
Douglas Gregor | c5df30f | 2009-09-26 03:41:46 +0000 | [diff] [blame] | 4323 | // Find the most specialized function template. |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4324 | UnresolvedSetIterator Result |
| 4325 | = getMostSpecialized(Candidates.begin(), Candidates.end(), |
| 4326 | TPOC_Other, FD->getLocation(), |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 4327 | PDiag(diag::err_function_template_spec_no_match) |
Douglas Gregor | c5df30f | 2009-09-26 03:41:46 +0000 | [diff] [blame] | 4328 | << FD->getDeclName(), |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 4329 | PDiag(diag::err_function_template_spec_ambiguous) |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 4330 | << FD->getDeclName() << (ExplicitTemplateArgs != 0), |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 4331 | PDiag(diag::note_function_template_spec_matched)); |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4332 | if (Result == Candidates.end()) |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4333 | return true; |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4334 | |
| 4335 | // Ignore access information; it doesn't figure into redeclaration checking. |
| 4336 | FunctionDecl *Specialization = cast<FunctionDecl>(*Result); |
Douglas Gregor | c42b652 | 2010-04-09 21:02:29 +0000 | [diff] [blame] | 4337 | Specialization->setLocation(FD->getLocation()); |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4338 | |
| 4339 | // FIXME: Check if the prior specialization has a point of instantiation. |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 4340 | // If so, we have run afoul of . |
John McCall | 7ad650f | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 4341 | |
| 4342 | // If this is a friend declaration, then we're not really declaring |
| 4343 | // an explicit specialization. |
| 4344 | bool isFriend = (FD->getFriendObjectKind() != Decl::FOK_None); |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4345 | |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4346 | // Check the scope of this explicit specialization. |
John McCall | 7ad650f | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 4347 | if (!isFriend && |
| 4348 | CheckTemplateSpecializationScope(*this, |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4349 | Specialization->getPrimaryTemplate(), |
| 4350 | Specialization, FD->getLocation(), |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4351 | false)) |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4352 | return true; |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 4353 | |
| 4354 | // C++ [temp.expl.spec]p6: |
| 4355 | // 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] | 4356 | // explicitly specialized then that specialization shall be declared |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 4357 | // before the first use of that specialization that would cause an implicit |
| 4358 | // instantiation to take place, in every translation unit in which such a |
| 4359 | // use occurs; no diagnostic is required. |
| 4360 | FunctionTemplateSpecializationInfo *SpecInfo |
| 4361 | = Specialization->getTemplateSpecializationInfo(); |
| 4362 | assert(SpecInfo && "Function template specialization info missing?"); |
John McCall | 7504239 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 4363 | |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 4364 | bool HasNoEffect = false; |
John McCall | 7ad650f | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 4365 | if (!isFriend && |
| 4366 | CheckSpecializationInstantiationRedecl(FD->getLocation(), |
John McCall | 7504239 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 4367 | TSK_ExplicitSpecialization, |
| 4368 | Specialization, |
| 4369 | SpecInfo->getTemplateSpecializationKind(), |
| 4370 | SpecInfo->getPointOfInstantiation(), |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 4371 | HasNoEffect)) |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 4372 | return true; |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4373 | |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4374 | // Mark the prior declaration as an explicit specialization, so that later |
| 4375 | // clients know that this is an explicit specialization. |
John McCall | 7ad650f | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 4376 | if (!isFriend) |
| 4377 | SpecInfo->setTemplateSpecializationKind(TSK_ExplicitSpecialization); |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4378 | |
| 4379 | // Turn the given function declaration into a function template |
| 4380 | // specialization, with the template arguments from the previous |
| 4381 | // specialization. |
Abramo Bagnara | e03db98 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 4382 | // Take copies of (semantic and syntactic) template argument lists. |
| 4383 | const TemplateArgumentList* TemplArgs = new (Context) |
| 4384 | TemplateArgumentList(Specialization->getTemplateSpecializationArgs()); |
| 4385 | const TemplateArgumentListInfo* TemplArgsAsWritten = ExplicitTemplateArgs |
| 4386 | ? new (Context) TemplateArgumentListInfo(*ExplicitTemplateArgs) : 0; |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 4387 | FD->setFunctionTemplateSpecialization(Specialization->getPrimaryTemplate(), |
Abramo Bagnara | e03db98 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 4388 | TemplArgs, /*InsertPos=*/0, |
| 4389 | SpecInfo->getTemplateSpecializationKind(), |
| 4390 | TemplArgsAsWritten); |
| 4391 | |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4392 | // The "previous declaration" for this function template specialization is |
| 4393 | // the prior function template specialization. |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 4394 | Previous.clear(); |
| 4395 | Previous.addDecl(Specialization); |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4396 | return false; |
| 4397 | } |
| 4398 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4399 | /// \brief Perform semantic analysis for the given non-template member |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 4400 | /// specialization. |
| 4401 | /// |
| 4402 | /// This routine performs all of the semantic analysis required for an |
| 4403 | /// explicit member function specialization. On successful completion, |
| 4404 | /// the function declaration \p FD will become a member function |
| 4405 | /// specialization. |
| 4406 | /// |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4407 | /// \param Member the member declaration, which will be updated to become a |
| 4408 | /// specialization. |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 4409 | /// |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 4410 | /// \param Previous the set of declarations, one of which may be specialized |
| 4411 | /// by this function specialization; the set will be modified to contain the |
| 4412 | /// redeclared member. |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 4413 | bool |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 4414 | Sema::CheckMemberSpecialization(NamedDecl *Member, LookupResult &Previous) { |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4415 | assert(!isa<TemplateDecl>(Member) && "Only for non-template members"); |
John McCall | 77e8b11 | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 4416 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4417 | // Try to find the member we are instantiating. |
| 4418 | NamedDecl *Instantiation = 0; |
| 4419 | NamedDecl *InstantiatedFrom = 0; |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 4420 | MemberSpecializationInfo *MSInfo = 0; |
| 4421 | |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 4422 | if (Previous.empty()) { |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4423 | // Nowhere to look anyway. |
| 4424 | } else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Member)) { |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 4425 | for (LookupResult::iterator I = Previous.begin(), E = Previous.end(); |
| 4426 | I != E; ++I) { |
| 4427 | NamedDecl *D = (*I)->getUnderlyingDecl(); |
| 4428 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) { |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4429 | if (Context.hasSameType(Function->getType(), Method->getType())) { |
| 4430 | Instantiation = Method; |
| 4431 | InstantiatedFrom = Method->getInstantiatedFromMemberFunction(); |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 4432 | MSInfo = Method->getMemberSpecializationInfo(); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4433 | break; |
| 4434 | } |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 4435 | } |
| 4436 | } |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4437 | } else if (isa<VarDecl>(Member)) { |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 4438 | VarDecl *PrevVar; |
| 4439 | if (Previous.isSingleResult() && |
| 4440 | (PrevVar = dyn_cast<VarDecl>(Previous.getFoundDecl()))) |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4441 | if (PrevVar->isStaticDataMember()) { |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 4442 | Instantiation = PrevVar; |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4443 | InstantiatedFrom = PrevVar->getInstantiatedFromStaticDataMember(); |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 4444 | MSInfo = PrevVar->getMemberSpecializationInfo(); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4445 | } |
| 4446 | } else if (isa<RecordDecl>(Member)) { |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 4447 | CXXRecordDecl *PrevRecord; |
| 4448 | if (Previous.isSingleResult() && |
| 4449 | (PrevRecord = dyn_cast<CXXRecordDecl>(Previous.getFoundDecl()))) { |
| 4450 | Instantiation = PrevRecord; |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4451 | InstantiatedFrom = PrevRecord->getInstantiatedFromMemberClass(); |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 4452 | MSInfo = PrevRecord->getMemberSpecializationInfo(); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4453 | } |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 4454 | } |
| 4455 | |
| 4456 | if (!Instantiation) { |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4457 | // There is no previous declaration that matches. Since member |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 4458 | // specializations are always out-of-line, the caller will complain about |
| 4459 | // this mismatch later. |
| 4460 | return false; |
| 4461 | } |
John McCall | 77e8b11 | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 4462 | |
| 4463 | // If this is a friend, just bail out here before we start turning |
| 4464 | // things into explicit specializations. |
| 4465 | if (Member->getFriendObjectKind() != Decl::FOK_None) { |
| 4466 | // Preserve instantiation information. |
| 4467 | if (InstantiatedFrom && isa<CXXMethodDecl>(Member)) { |
| 4468 | cast<CXXMethodDecl>(Member)->setInstantiationOfMemberFunction( |
| 4469 | cast<CXXMethodDecl>(InstantiatedFrom), |
| 4470 | cast<CXXMethodDecl>(Instantiation)->getTemplateSpecializationKind()); |
| 4471 | } else if (InstantiatedFrom && isa<CXXRecordDecl>(Member)) { |
| 4472 | cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass( |
| 4473 | cast<CXXRecordDecl>(InstantiatedFrom), |
| 4474 | cast<CXXRecordDecl>(Instantiation)->getTemplateSpecializationKind()); |
| 4475 | } |
| 4476 | |
| 4477 | Previous.clear(); |
| 4478 | Previous.addDecl(Instantiation); |
| 4479 | return false; |
| 4480 | } |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 4481 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4482 | // Make sure that this is a specialization of a member. |
| 4483 | if (!InstantiatedFrom) { |
| 4484 | Diag(Member->getLocation(), diag::err_spec_member_not_instantiated) |
| 4485 | << Member; |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 4486 | Diag(Instantiation->getLocation(), diag::note_specialized_decl); |
| 4487 | return true; |
| 4488 | } |
| 4489 | |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 4490 | // C++ [temp.expl.spec]p6: |
| 4491 | // If a template, a member template or the member of a class template is |
| 4492 | // explicitly specialized then that spe- cialization shall be declared |
| 4493 | // before the first use of that specialization that would cause an implicit |
| 4494 | // instantiation to take place, in every translation unit in which such a |
| 4495 | // use occurs; no diagnostic is required. |
| 4496 | assert(MSInfo && "Member specialization info missing?"); |
John McCall | 7504239 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 4497 | |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 4498 | bool HasNoEffect = false; |
John McCall | 7504239 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 4499 | if (CheckSpecializationInstantiationRedecl(Member->getLocation(), |
| 4500 | TSK_ExplicitSpecialization, |
| 4501 | Instantiation, |
| 4502 | MSInfo->getTemplateSpecializationKind(), |
| 4503 | MSInfo->getPointOfInstantiation(), |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 4504 | HasNoEffect)) |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 4505 | return true; |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 4506 | |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 4507 | // Check the scope of this explicit specialization. |
| 4508 | if (CheckTemplateSpecializationScope(*this, |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4509 | InstantiatedFrom, |
| 4510 | Instantiation, Member->getLocation(), |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4511 | false)) |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 4512 | return true; |
Douglas Gregor | 2db3232 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 4513 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4514 | // Note that this is an explicit instantiation of a member. |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 4515 | // the original declaration to note that it is an explicit specialization |
| 4516 | // (if it was previously an implicit instantiation). This latter step |
| 4517 | // makes bookkeeping easier. |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4518 | if (isa<FunctionDecl>(Member)) { |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 4519 | FunctionDecl *InstantiationFunction = cast<FunctionDecl>(Instantiation); |
| 4520 | if (InstantiationFunction->getTemplateSpecializationKind() == |
| 4521 | TSK_ImplicitInstantiation) { |
| 4522 | InstantiationFunction->setTemplateSpecializationKind( |
| 4523 | TSK_ExplicitSpecialization); |
| 4524 | InstantiationFunction->setLocation(Member->getLocation()); |
| 4525 | } |
| 4526 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4527 | cast<FunctionDecl>(Member)->setInstantiationOfMemberFunction( |
| 4528 | cast<CXXMethodDecl>(InstantiatedFrom), |
| 4529 | TSK_ExplicitSpecialization); |
| 4530 | } else if (isa<VarDecl>(Member)) { |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 4531 | VarDecl *InstantiationVar = cast<VarDecl>(Instantiation); |
| 4532 | if (InstantiationVar->getTemplateSpecializationKind() == |
| 4533 | TSK_ImplicitInstantiation) { |
| 4534 | InstantiationVar->setTemplateSpecializationKind( |
| 4535 | TSK_ExplicitSpecialization); |
| 4536 | InstantiationVar->setLocation(Member->getLocation()); |
| 4537 | } |
| 4538 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4539 | Context.setInstantiatedFromStaticDataMember(cast<VarDecl>(Member), |
| 4540 | cast<VarDecl>(InstantiatedFrom), |
| 4541 | TSK_ExplicitSpecialization); |
| 4542 | } else { |
| 4543 | assert(isa<CXXRecordDecl>(Member) && "Only member classes remain"); |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 4544 | CXXRecordDecl *InstantiationClass = cast<CXXRecordDecl>(Instantiation); |
| 4545 | if (InstantiationClass->getTemplateSpecializationKind() == |
| 4546 | TSK_ImplicitInstantiation) { |
| 4547 | InstantiationClass->setTemplateSpecializationKind( |
| 4548 | TSK_ExplicitSpecialization); |
| 4549 | InstantiationClass->setLocation(Member->getLocation()); |
| 4550 | } |
| 4551 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4552 | cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass( |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 4553 | cast<CXXRecordDecl>(InstantiatedFrom), |
| 4554 | TSK_ExplicitSpecialization); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4555 | } |
| 4556 | |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 4557 | // Save the caller the trouble of having to figure out which declaration |
| 4558 | // this specialization matches. |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 4559 | Previous.clear(); |
| 4560 | Previous.addDecl(Instantiation); |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 4561 | return false; |
| 4562 | } |
| 4563 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 4564 | /// \brief Check the scope of an explicit instantiation. |
Douglas Gregor | 669eed8 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 4565 | /// |
| 4566 | /// \returns true if a serious error occurs, false otherwise. |
| 4567 | static bool CheckExplicitInstantiationScope(Sema &S, NamedDecl *D, |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 4568 | SourceLocation InstLoc, |
| 4569 | bool WasQualifiedName) { |
| 4570 | DeclContext *ExpectedContext |
| 4571 | = D->getDeclContext()->getEnclosingNamespaceContext()->getLookupContext(); |
| 4572 | DeclContext *CurContext = S.CurContext->getLookupContext(); |
| 4573 | |
Douglas Gregor | 669eed8 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 4574 | if (CurContext->isRecord()) { |
| 4575 | S.Diag(InstLoc, diag::err_explicit_instantiation_in_class) |
| 4576 | << D; |
| 4577 | return true; |
| 4578 | } |
| 4579 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 4580 | // C++0x [temp.explicit]p2: |
| 4581 | // An explicit instantiation shall appear in an enclosing namespace of its |
| 4582 | // template. |
| 4583 | // |
| 4584 | // This is DR275, which we do not retroactively apply to C++98/03. |
| 4585 | if (S.getLangOptions().CPlusPlus0x && |
| 4586 | !CurContext->Encloses(ExpectedContext)) { |
| 4587 | if (NamespaceDecl *NS = dyn_cast<NamespaceDecl>(ExpectedContext)) |
Douglas Gregor | 2166beb | 2010-05-11 17:39:34 +0000 | [diff] [blame] | 4588 | S.Diag(InstLoc, |
| 4589 | S.getLangOptions().CPlusPlus0x? |
| 4590 | diag::err_explicit_instantiation_out_of_scope |
| 4591 | : diag::warn_explicit_instantiation_out_of_scope_0x) |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 4592 | << D << NS; |
| 4593 | else |
Douglas Gregor | 2166beb | 2010-05-11 17:39:34 +0000 | [diff] [blame] | 4594 | S.Diag(InstLoc, |
| 4595 | S.getLangOptions().CPlusPlus0x? |
| 4596 | diag::err_explicit_instantiation_must_be_global |
| 4597 | : diag::warn_explicit_instantiation_out_of_scope_0x) |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 4598 | << D; |
| 4599 | S.Diag(D->getLocation(), diag::note_explicit_instantiation_here); |
Douglas Gregor | 669eed8 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 4600 | return false; |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 4601 | } |
| 4602 | |
| 4603 | // C++0x [temp.explicit]p2: |
| 4604 | // If the name declared in the explicit instantiation is an unqualified |
| 4605 | // name, the explicit instantiation shall appear in the namespace where |
| 4606 | // its template is declared or, if that namespace is inline (7.3.1), any |
| 4607 | // namespace from its enclosing namespace set. |
| 4608 | if (WasQualifiedName) |
Douglas Gregor | 669eed8 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 4609 | return false; |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 4610 | |
| 4611 | if (CurContext->Equals(ExpectedContext)) |
Douglas Gregor | 669eed8 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 4612 | return false; |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 4613 | |
Douglas Gregor | 2166beb | 2010-05-11 17:39:34 +0000 | [diff] [blame] | 4614 | S.Diag(InstLoc, |
| 4615 | S.getLangOptions().CPlusPlus0x? |
| 4616 | diag::err_explicit_instantiation_unqualified_wrong_namespace |
| 4617 | : diag::warn_explicit_instantiation_unqualified_wrong_namespace_0x) |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 4618 | << D << ExpectedContext; |
| 4619 | S.Diag(D->getLocation(), diag::note_explicit_instantiation_here); |
Douglas Gregor | 669eed8 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 4620 | return false; |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 4621 | } |
| 4622 | |
| 4623 | /// \brief Determine whether the given scope specifier has a template-id in it. |
| 4624 | static bool ScopeSpecifierHasTemplateId(const CXXScopeSpec &SS) { |
| 4625 | if (!SS.isSet()) |
| 4626 | return false; |
| 4627 | |
| 4628 | // C++0x [temp.explicit]p2: |
| 4629 | // If the explicit instantiation is for a member function, a member class |
| 4630 | // or a static data member of a class template specialization, the name of |
| 4631 | // the class template specialization in the qualified-id for the member |
| 4632 | // name shall be a simple-template-id. |
| 4633 | // |
| 4634 | // C++98 has the same restriction, just worded differently. |
| 4635 | for (NestedNameSpecifier *NNS = (NestedNameSpecifier *)SS.getScopeRep(); |
| 4636 | NNS; NNS = NNS->getPrefix()) |
| 4637 | if (Type *T = NNS->getAsType()) |
| 4638 | if (isa<TemplateSpecializationType>(T)) |
| 4639 | return true; |
| 4640 | |
| 4641 | return false; |
| 4642 | } |
| 4643 | |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 4644 | // Explicit instantiation of a class template specialization |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4645 | Sema::DeclResult |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4646 | Sema::ActOnExplicitInstantiation(Scope *S, |
Douglas Gregor | 45f9655 | 2009-09-04 06:33:52 +0000 | [diff] [blame] | 4647 | SourceLocation ExternLoc, |
| 4648 | SourceLocation TemplateLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4649 | unsigned TagSpec, |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4650 | SourceLocation KWLoc, |
| 4651 | const CXXScopeSpec &SS, |
| 4652 | TemplateTy TemplateD, |
| 4653 | SourceLocation TemplateNameLoc, |
| 4654 | SourceLocation LAngleLoc, |
| 4655 | ASTTemplateArgsPtr TemplateArgsIn, |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4656 | SourceLocation RAngleLoc, |
| 4657 | AttributeList *Attr) { |
| 4658 | // Find the class template we're specializing |
| 4659 | TemplateName Name = TemplateD.getAsVal<TemplateName>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4660 | ClassTemplateDecl *ClassTemplate |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4661 | = cast<ClassTemplateDecl>(Name.getAsTemplateDecl()); |
| 4662 | |
| 4663 | // Check that the specialization uses the same tag kind as the |
| 4664 | // original template. |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 4665 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
| 4666 | assert(Kind != TTK_Enum && |
| 4667 | "Invalid enum tag in class template explicit instantiation!"); |
Douglas Gregor | 501c5ce | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 4668 | if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4669 | Kind, KWLoc, |
Douglas Gregor | 501c5ce | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 4670 | *ClassTemplate->getIdentifier())) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4671 | Diag(KWLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4672 | << ClassTemplate |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 4673 | << FixItHint::CreateReplacement(KWLoc, |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4674 | ClassTemplate->getTemplatedDecl()->getKindName()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4675 | Diag(ClassTemplate->getTemplatedDecl()->getLocation(), |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4676 | diag::note_previous_use); |
| 4677 | Kind = ClassTemplate->getTemplatedDecl()->getTagKind(); |
| 4678 | } |
| 4679 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 4680 | // C++0x [temp.explicit]p2: |
| 4681 | // There are two forms of explicit instantiation: an explicit instantiation |
| 4682 | // definition and an explicit instantiation declaration. An explicit |
| 4683 | // instantiation declaration begins with the extern keyword. [...] |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4684 | TemplateSpecializationKind TSK |
| 4685 | = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition |
| 4686 | : TSK_ExplicitInstantiationDeclaration; |
| 4687 | |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4688 | // Translate the parser's template argument list in our AST format. |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 4689 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
Douglas Gregor | 314b97f | 2009-11-10 19:49:08 +0000 | [diff] [blame] | 4690 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4691 | |
| 4692 | // Check that the template argument list is well-formed for this |
| 4693 | // template. |
Anders Carlsson | fb25052 | 2009-06-23 01:26:57 +0000 | [diff] [blame] | 4694 | TemplateArgumentListBuilder Converted(ClassTemplate->getTemplateParameters(), |
| 4695 | TemplateArgs.size()); |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 4696 | if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc, |
| 4697 | TemplateArgs, false, Converted)) |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4698 | return true; |
| 4699 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4700 | assert((Converted.structuredSize() == |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4701 | ClassTemplate->getTemplateParameters()->size()) && |
| 4702 | "Converted template argument list is too short!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4703 | |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4704 | // Find the class template specialization declaration that |
| 4705 | // corresponds to these arguments. |
| 4706 | llvm::FoldingSetNodeID ID; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4707 | ClassTemplateSpecializationDecl::Profile(ID, |
Anders Carlsson | fb25052 | 2009-06-23 01:26:57 +0000 | [diff] [blame] | 4708 | Converted.getFlatArguments(), |
Douglas Gregor | 828e226 | 2009-07-29 16:09:57 +0000 | [diff] [blame] | 4709 | Converted.flatSize(), |
| 4710 | Context); |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4711 | void *InsertPos = 0; |
| 4712 | ClassTemplateSpecializationDecl *PrevDecl |
| 4713 | = ClassTemplate->getSpecializations().FindNodeOrInsertPos(ID, InsertPos); |
| 4714 | |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 4715 | TemplateSpecializationKind PrevDecl_TSK |
| 4716 | = PrevDecl ? PrevDecl->getTemplateSpecializationKind() : TSK_Undeclared; |
| 4717 | |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4718 | // C++0x [temp.explicit]p2: |
| 4719 | // [...] An explicit instantiation shall appear in an enclosing |
| 4720 | // namespace of its template. [...] |
| 4721 | // |
| 4722 | // This is C++ DR 275. |
Douglas Gregor | 669eed8 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 4723 | if (CheckExplicitInstantiationScope(*this, ClassTemplate, TemplateNameLoc, |
| 4724 | SS.isSet())) |
| 4725 | return true; |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4726 | |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4727 | ClassTemplateSpecializationDecl *Specialization = 0; |
| 4728 | |
Douglas Gregor | d78f598 | 2009-11-25 06:01:46 +0000 | [diff] [blame] | 4729 | bool ReusedDecl = false; |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 4730 | bool HasNoEffect = false; |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4731 | if (PrevDecl) { |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 4732 | if (CheckSpecializationInstantiationRedecl(TemplateNameLoc, TSK, |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 4733 | PrevDecl, PrevDecl_TSK, |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 4734 | PrevDecl->getPointOfInstantiation(), |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 4735 | HasNoEffect)) |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4736 | return DeclPtrTy::make(PrevDecl); |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4737 | |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 4738 | // Even though HasNoEffect == true means that this explicit instantiation |
| 4739 | // has no effect on semantics, we go on to put its syntax in the AST. |
| 4740 | |
| 4741 | if (PrevDecl_TSK == TSK_ImplicitInstantiation || |
| 4742 | PrevDecl_TSK == TSK_Undeclared) { |
Douglas Gregor | 52604ab | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 4743 | // Since the only prior class template specialization with these |
| 4744 | // arguments was referenced but not declared, reuse that |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 4745 | // declaration node as our own, updating the source location |
| 4746 | // for the template name to reflect our new declaration. |
| 4747 | // (Other source locations will be updated later.) |
Douglas Gregor | 52604ab | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 4748 | Specialization = PrevDecl; |
| 4749 | Specialization->setLocation(TemplateNameLoc); |
| 4750 | PrevDecl = 0; |
Douglas Gregor | d78f598 | 2009-11-25 06:01:46 +0000 | [diff] [blame] | 4751 | ReusedDecl = true; |
Douglas Gregor | 52604ab | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 4752 | } |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 4753 | } |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 4754 | |
Douglas Gregor | 52604ab | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 4755 | if (!Specialization) { |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4756 | // Create a new class template specialization declaration node for |
| 4757 | // this explicit specialization. |
| 4758 | Specialization |
Douglas Gregor | 13c8577 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 4759 | = ClassTemplateSpecializationDecl::Create(Context, Kind, |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4760 | ClassTemplate->getDeclContext(), |
| 4761 | TemplateNameLoc, |
| 4762 | ClassTemplate, |
Douglas Gregor | 52604ab | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 4763 | Converted, PrevDecl); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 4764 | SetNestedNameSpecifier(Specialization, SS); |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4765 | |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 4766 | if (!HasNoEffect) { |
| 4767 | if (PrevDecl) { |
| 4768 | // Remove the previous declaration from the folding set, since we want |
| 4769 | // to introduce a new declaration. |
| 4770 | ClassTemplate->getSpecializations().RemoveNode(PrevDecl); |
| 4771 | ClassTemplate->getSpecializations().FindNodeOrInsertPos(ID, InsertPos); |
| 4772 | } |
| 4773 | // Insert the new specialization. |
| 4774 | ClassTemplate->getSpecializations().InsertNode(Specialization, InsertPos); |
| 4775 | } |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4776 | } |
| 4777 | |
| 4778 | // Build the fully-sugared type for this explicit instantiation as |
| 4779 | // the user wrote in the explicit instantiation itself. This means |
| 4780 | // that we'll pretty-print the type retrieved from the |
| 4781 | // specialization's declaration the way that the user actually wrote |
| 4782 | // the explicit instantiation, rather than formatting the name based |
| 4783 | // on the "canonical" representation used to store the template |
| 4784 | // arguments in the specialization. |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 4785 | TypeSourceInfo *WrittenTy |
| 4786 | = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc, |
| 4787 | TemplateArgs, |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4788 | Context.getTypeDeclType(Specialization)); |
| 4789 | Specialization->setTypeAsWritten(WrittenTy); |
| 4790 | TemplateArgsIn.release(); |
| 4791 | |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 4792 | // Set source locations for keywords. |
| 4793 | Specialization->setExternLoc(ExternLoc); |
| 4794 | Specialization->setTemplateKeywordLoc(TemplateLoc); |
| 4795 | |
| 4796 | // Add the explicit instantiation into its lexical context. However, |
| 4797 | // since explicit instantiations are never found by name lookup, we |
| 4798 | // just put it into the declaration context directly. |
| 4799 | Specialization->setLexicalDeclContext(CurContext); |
| 4800 | CurContext->addDecl(Specialization); |
| 4801 | |
| 4802 | // Syntax is now OK, so return if it has no other effect on semantics. |
| 4803 | if (HasNoEffect) { |
| 4804 | // Set the template specialization kind. |
| 4805 | Specialization->setTemplateSpecializationKind(TSK); |
| 4806 | return DeclPtrTy::make(Specialization); |
Douglas Gregor | d78f598 | 2009-11-25 06:01:46 +0000 | [diff] [blame] | 4807 | } |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4808 | |
| 4809 | // C++ [temp.explicit]p3: |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4810 | // A definition of a class template or class member template |
| 4811 | // shall be in scope at the point of the explicit instantiation of |
| 4812 | // the class template or class member template. |
| 4813 | // |
| 4814 | // This check comes when we actually try to perform the |
| 4815 | // instantiation. |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 4816 | ClassTemplateSpecializationDecl *Def |
| 4817 | = cast_or_null<ClassTemplateSpecializationDecl>( |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 4818 | Specialization->getDefinition()); |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 4819 | if (!Def) |
Douglas Gregor | 972e6ce | 2009-10-27 06:26:26 +0000 | [diff] [blame] | 4820 | InstantiateClassTemplateSpecialization(TemplateNameLoc, Specialization, TSK); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 4821 | else if (TSK == TSK_ExplicitInstantiationDefinition) { |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 4822 | MarkVTableUsed(TemplateNameLoc, Specialization, true); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 4823 | Specialization->setPointOfInstantiation(Def->getPointOfInstantiation()); |
| 4824 | } |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 4825 | |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 4826 | // Instantiate the members of this class template specialization. |
| 4827 | Def = cast_or_null<ClassTemplateSpecializationDecl>( |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 4828 | Specialization->getDefinition()); |
Rafael Espindola | b0f65ca | 2010-03-22 23:12:48 +0000 | [diff] [blame] | 4829 | if (Def) { |
Rafael Espindola | f075b22 | 2010-03-23 19:55:22 +0000 | [diff] [blame] | 4830 | TemplateSpecializationKind Old_TSK = Def->getTemplateSpecializationKind(); |
| 4831 | |
| 4832 | // Fix a TSK_ExplicitInstantiationDeclaration followed by a |
| 4833 | // TSK_ExplicitInstantiationDefinition |
| 4834 | if (Old_TSK == TSK_ExplicitInstantiationDeclaration && |
| 4835 | TSK == TSK_ExplicitInstantiationDefinition) |
| 4836 | Def->setTemplateSpecializationKind(TSK); |
Rafael Espindola | b0f65ca | 2010-03-22 23:12:48 +0000 | [diff] [blame] | 4837 | |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 4838 | InstantiateClassTemplateSpecializationMembers(TemplateNameLoc, Def, TSK); |
Rafael Espindola | b0f65ca | 2010-03-22 23:12:48 +0000 | [diff] [blame] | 4839 | } |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4840 | |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 4841 | // Set the template specialization kind. |
| 4842 | Specialization->setTemplateSpecializationKind(TSK); |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4843 | return DeclPtrTy::make(Specialization); |
| 4844 | } |
| 4845 | |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 4846 | // Explicit instantiation of a member class of a class template. |
| 4847 | Sema::DeclResult |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4848 | Sema::ActOnExplicitInstantiation(Scope *S, |
Douglas Gregor | 45f9655 | 2009-09-04 06:33:52 +0000 | [diff] [blame] | 4849 | SourceLocation ExternLoc, |
| 4850 | SourceLocation TemplateLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4851 | unsigned TagSpec, |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 4852 | SourceLocation KWLoc, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 4853 | CXXScopeSpec &SS, |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 4854 | IdentifierInfo *Name, |
| 4855 | SourceLocation NameLoc, |
| 4856 | AttributeList *Attr) { |
| 4857 | |
Douglas Gregor | 402abb5 | 2009-05-28 23:31:59 +0000 | [diff] [blame] | 4858 | bool Owned = false; |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 4859 | bool IsDependent = false; |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 4860 | DeclPtrTy TagD = ActOnTag(S, TagSpec, Action::TUK_Reference, |
Douglas Gregor | 7cdbc58 | 2009-07-22 23:48:44 +0000 | [diff] [blame] | 4861 | KWLoc, SS, Name, NameLoc, Attr, AS_none, |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 4862 | MultiTemplateParamsArg(*this, 0, 0), |
| 4863 | Owned, IsDependent); |
| 4864 | assert(!IsDependent && "explicit instantiation of dependent name not yet handled"); |
| 4865 | |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 4866 | if (!TagD) |
| 4867 | return true; |
| 4868 | |
| 4869 | TagDecl *Tag = cast<TagDecl>(TagD.getAs<Decl>()); |
| 4870 | if (Tag->isEnum()) { |
| 4871 | Diag(TemplateLoc, diag::err_explicit_instantiation_enum) |
| 4872 | << Context.getTypeDeclType(Tag); |
| 4873 | return true; |
| 4874 | } |
| 4875 | |
Douglas Gregor | d0c8737 | 2009-05-27 17:30:49 +0000 | [diff] [blame] | 4876 | if (Tag->isInvalidDecl()) |
| 4877 | return true; |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 4878 | |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 4879 | CXXRecordDecl *Record = cast<CXXRecordDecl>(Tag); |
| 4880 | CXXRecordDecl *Pattern = Record->getInstantiatedFromMemberClass(); |
| 4881 | if (!Pattern) { |
| 4882 | Diag(TemplateLoc, diag::err_explicit_instantiation_nontemplate_type) |
| 4883 | << Context.getTypeDeclType(Record); |
| 4884 | Diag(Record->getLocation(), diag::note_nontemplate_decl_here); |
| 4885 | return true; |
| 4886 | } |
| 4887 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 4888 | // C++0x [temp.explicit]p2: |
| 4889 | // If the explicit instantiation is for a class or member class, the |
| 4890 | // elaborated-type-specifier in the declaration shall include a |
| 4891 | // simple-template-id. |
| 4892 | // |
| 4893 | // C++98 has the same restriction, just worded differently. |
| 4894 | if (!ScopeSpecifierHasTemplateId(SS)) |
Douglas Gregor | a2dd828 | 2010-06-16 16:26:47 +0000 | [diff] [blame] | 4895 | Diag(TemplateLoc, diag::ext_explicit_instantiation_without_qualified_id) |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 4896 | << Record << SS.getRange(); |
| 4897 | |
| 4898 | // C++0x [temp.explicit]p2: |
| 4899 | // There are two forms of explicit instantiation: an explicit instantiation |
| 4900 | // definition and an explicit instantiation declaration. An explicit |
| 4901 | // instantiation declaration begins with the extern keyword. [...] |
Douglas Gregor | a74bbe2 | 2009-10-14 21:46:58 +0000 | [diff] [blame] | 4902 | TemplateSpecializationKind TSK |
| 4903 | = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition |
| 4904 | : TSK_ExplicitInstantiationDeclaration; |
| 4905 | |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 4906 | // C++0x [temp.explicit]p2: |
| 4907 | // [...] An explicit instantiation shall appear in an enclosing |
| 4908 | // namespace of its template. [...] |
| 4909 | // |
| 4910 | // This is C++ DR 275. |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 4911 | CheckExplicitInstantiationScope(*this, Record, NameLoc, true); |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4912 | |
| 4913 | // Verify that it is okay to explicitly instantiate here. |
Douglas Gregor | 583f33b | 2009-10-15 18:07:02 +0000 | [diff] [blame] | 4914 | CXXRecordDecl *PrevDecl |
| 4915 | = cast_or_null<CXXRecordDecl>(Record->getPreviousDeclaration()); |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 4916 | if (!PrevDecl && Record->getDefinition()) |
Douglas Gregor | 583f33b | 2009-10-15 18:07:02 +0000 | [diff] [blame] | 4917 | PrevDecl = Record; |
| 4918 | if (PrevDecl) { |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4919 | MemberSpecializationInfo *MSInfo = PrevDecl->getMemberSpecializationInfo(); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 4920 | bool HasNoEffect = false; |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4921 | assert(MSInfo && "No member specialization information?"); |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 4922 | if (CheckSpecializationInstantiationRedecl(TemplateLoc, TSK, |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4923 | PrevDecl, |
| 4924 | MSInfo->getTemplateSpecializationKind(), |
| 4925 | MSInfo->getPointOfInstantiation(), |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 4926 | HasNoEffect)) |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4927 | return true; |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 4928 | if (HasNoEffect) |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4929 | return TagD; |
| 4930 | } |
| 4931 | |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 4932 | CXXRecordDecl *RecordDef |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 4933 | = cast_or_null<CXXRecordDecl>(Record->getDefinition()); |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 4934 | if (!RecordDef) { |
Douglas Gregor | bf7643e | 2009-10-15 12:53:22 +0000 | [diff] [blame] | 4935 | // C++ [temp.explicit]p3: |
| 4936 | // A definition of a member class of a class template shall be in scope |
| 4937 | // at the point of an explicit instantiation of the member class. |
| 4938 | CXXRecordDecl *Def |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 4939 | = cast_or_null<CXXRecordDecl>(Pattern->getDefinition()); |
Douglas Gregor | bf7643e | 2009-10-15 12:53:22 +0000 | [diff] [blame] | 4940 | if (!Def) { |
Douglas Gregor | e2d3a3d | 2009-10-15 14:05:49 +0000 | [diff] [blame] | 4941 | Diag(TemplateLoc, diag::err_explicit_instantiation_undefined_member) |
| 4942 | << 0 << Record->getDeclName() << Record->getDeclContext(); |
Douglas Gregor | bf7643e | 2009-10-15 12:53:22 +0000 | [diff] [blame] | 4943 | Diag(Pattern->getLocation(), diag::note_forward_declaration) |
| 4944 | << Pattern; |
| 4945 | return true; |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 4946 | } else { |
| 4947 | if (InstantiateClass(NameLoc, Record, Def, |
| 4948 | getTemplateInstantiationArgs(Record), |
| 4949 | TSK)) |
| 4950 | return true; |
| 4951 | |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 4952 | RecordDef = cast_or_null<CXXRecordDecl>(Record->getDefinition()); |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 4953 | if (!RecordDef) |
| 4954 | return true; |
| 4955 | } |
| 4956 | } |
| 4957 | |
| 4958 | // Instantiate all of the members of the class. |
| 4959 | InstantiateClassMembers(NameLoc, RecordDef, |
| 4960 | getTemplateInstantiationArgs(Record), TSK); |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 4961 | |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 4962 | if (TSK == TSK_ExplicitInstantiationDefinition) |
| 4963 | MarkVTableUsed(NameLoc, RecordDef, true); |
| 4964 | |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 4965 | // FIXME: We don't have any representation for explicit instantiations of |
| 4966 | // member classes. Such a representation is not needed for compilation, but it |
| 4967 | // should be available for clients that want to see all of the declarations in |
| 4968 | // the source code. |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 4969 | return TagD; |
| 4970 | } |
| 4971 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4972 | Sema::DeclResult Sema::ActOnExplicitInstantiation(Scope *S, |
| 4973 | SourceLocation ExternLoc, |
| 4974 | SourceLocation TemplateLoc, |
| 4975 | Declarator &D) { |
| 4976 | // Explicit instantiations always require a name. |
| 4977 | DeclarationName Name = GetNameForDeclarator(D); |
| 4978 | if (!Name) { |
| 4979 | if (!D.isInvalidType()) |
| 4980 | Diag(D.getDeclSpec().getSourceRange().getBegin(), |
| 4981 | diag::err_explicit_instantiation_requires_name) |
| 4982 | << D.getDeclSpec().getSourceRange() |
| 4983 | << D.getSourceRange(); |
| 4984 | |
| 4985 | return true; |
| 4986 | } |
| 4987 | |
| 4988 | // The scope passed in may not be a decl scope. Zip up the scope tree until |
| 4989 | // we find one that is. |
| 4990 | while ((S->getFlags() & Scope::DeclScope) == 0 || |
| 4991 | (S->getFlags() & Scope::TemplateParamScope) != 0) |
| 4992 | S = S->getParent(); |
| 4993 | |
| 4994 | // Determine the type of the declaration. |
John McCall | bf1a028 | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 4995 | TypeSourceInfo *T = GetTypeForDeclarator(D, S); |
| 4996 | QualType R = T->getType(); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4997 | if (R.isNull()) |
| 4998 | return true; |
| 4999 | |
| 5000 | if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef) { |
| 5001 | // Cannot explicitly instantiate a typedef. |
| 5002 | Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_of_typedef) |
| 5003 | << Name; |
| 5004 | return true; |
| 5005 | } |
| 5006 | |
Douglas Gregor | 663b5a0 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 5007 | // C++0x [temp.explicit]p1: |
| 5008 | // [...] An explicit instantiation of a function template shall not use the |
| 5009 | // inline or constexpr specifiers. |
| 5010 | // Presumably, this also applies to member functions of class templates as |
| 5011 | // well. |
| 5012 | if (D.getDeclSpec().isInlineSpecified() && getLangOptions().CPlusPlus0x) |
| 5013 | Diag(D.getDeclSpec().getInlineSpecLoc(), |
| 5014 | diag::err_explicit_instantiation_inline) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 5015 | <<FixItHint::CreateRemoval(D.getDeclSpec().getInlineSpecLoc()); |
Douglas Gregor | 663b5a0 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 5016 | |
| 5017 | // FIXME: check for constexpr specifier. |
| 5018 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5019 | // C++0x [temp.explicit]p2: |
| 5020 | // There are two forms of explicit instantiation: an explicit instantiation |
| 5021 | // definition and an explicit instantiation declaration. An explicit |
| 5022 | // instantiation declaration begins with the extern keyword. [...] |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5023 | TemplateSpecializationKind TSK |
| 5024 | = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition |
| 5025 | : TSK_ExplicitInstantiationDeclaration; |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5026 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 5027 | LookupResult Previous(*this, Name, D.getIdentifierLoc(), LookupOrdinaryName); |
| 5028 | LookupParsedName(Previous, S, &D.getCXXScopeSpec()); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5029 | |
| 5030 | if (!R->isFunctionType()) { |
| 5031 | // C++ [temp.explicit]p1: |
| 5032 | // A [...] static data member of a class template can be explicitly |
| 5033 | // instantiated from the member definition associated with its class |
| 5034 | // template. |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 5035 | if (Previous.isAmbiguous()) |
| 5036 | return true; |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5037 | |
John McCall | 1bcee0a | 2009-12-02 08:25:40 +0000 | [diff] [blame] | 5038 | VarDecl *Prev = Previous.getAsSingle<VarDecl>(); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5039 | if (!Prev || !Prev->isStaticDataMember()) { |
| 5040 | // We expect to see a data data member here. |
| 5041 | Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_not_known) |
| 5042 | << Name; |
| 5043 | for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end(); |
| 5044 | P != PEnd; ++P) |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 5045 | Diag((*P)->getLocation(), diag::note_explicit_instantiation_here); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5046 | return true; |
| 5047 | } |
| 5048 | |
| 5049 | if (!Prev->getInstantiatedFromStaticDataMember()) { |
| 5050 | // FIXME: Check for explicit specialization? |
| 5051 | Diag(D.getIdentifierLoc(), |
| 5052 | diag::err_explicit_instantiation_data_member_not_instantiated) |
| 5053 | << Prev; |
| 5054 | Diag(Prev->getLocation(), diag::note_explicit_instantiation_here); |
| 5055 | // FIXME: Can we provide a note showing where this was declared? |
| 5056 | return true; |
| 5057 | } |
| 5058 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5059 | // C++0x [temp.explicit]p2: |
| 5060 | // If the explicit instantiation is for a member function, a member class |
| 5061 | // or a static data member of a class template specialization, the name of |
| 5062 | // the class template specialization in the qualified-id for the member |
| 5063 | // name shall be a simple-template-id. |
| 5064 | // |
| 5065 | // C++98 has the same restriction, just worded differently. |
| 5066 | if (!ScopeSpecifierHasTemplateId(D.getCXXScopeSpec())) |
| 5067 | Diag(D.getIdentifierLoc(), |
Douglas Gregor | a2dd828 | 2010-06-16 16:26:47 +0000 | [diff] [blame] | 5068 | diag::ext_explicit_instantiation_without_qualified_id) |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5069 | << Prev << D.getCXXScopeSpec().getRange(); |
| 5070 | |
| 5071 | // Check the scope of this explicit instantiation. |
| 5072 | CheckExplicitInstantiationScope(*this, Prev, D.getIdentifierLoc(), true); |
| 5073 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5074 | // Verify that it is okay to explicitly instantiate here. |
| 5075 | MemberSpecializationInfo *MSInfo = Prev->getMemberSpecializationInfo(); |
| 5076 | assert(MSInfo && "Missing static data member specialization info?"); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5077 | bool HasNoEffect = false; |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 5078 | if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK, Prev, |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5079 | MSInfo->getTemplateSpecializationKind(), |
| 5080 | MSInfo->getPointOfInstantiation(), |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5081 | HasNoEffect)) |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5082 | return true; |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5083 | if (HasNoEffect) |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5084 | return DeclPtrTy(); |
| 5085 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5086 | // Instantiate static data member. |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 5087 | Prev->setTemplateSpecializationKind(TSK, D.getIdentifierLoc()); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5088 | if (TSK == TSK_ExplicitInstantiationDefinition) |
Douglas Gregor | e2d3a3d | 2009-10-15 14:05:49 +0000 | [diff] [blame] | 5089 | InstantiateStaticDataMemberDefinition(D.getIdentifierLoc(), Prev, false, |
| 5090 | /*DefinitionRequired=*/true); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5091 | |
| 5092 | // FIXME: Create an ExplicitInstantiation node? |
| 5093 | return DeclPtrTy(); |
| 5094 | } |
| 5095 | |
Douglas Gregor | 0b60d9e | 2009-09-25 23:53:26 +0000 | [diff] [blame] | 5096 | // If the declarator is a template-id, translate the parser's template |
| 5097 | // argument list into our AST format. |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 5098 | bool HasExplicitTemplateArgs = false; |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 5099 | TemplateArgumentListInfo TemplateArgs; |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 5100 | if (D.getName().getKind() == UnqualifiedId::IK_TemplateId) { |
| 5101 | TemplateIdAnnotation *TemplateId = D.getName().TemplateId; |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 5102 | TemplateArgs.setLAngleLoc(TemplateId->LAngleLoc); |
| 5103 | TemplateArgs.setRAngleLoc(TemplateId->RAngleLoc); |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 5104 | ASTTemplateArgsPtr TemplateArgsPtr(*this, |
| 5105 | TemplateId->getTemplateArgs(), |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 5106 | TemplateId->NumArgs); |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 5107 | translateTemplateArguments(TemplateArgsPtr, TemplateArgs); |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 5108 | HasExplicitTemplateArgs = true; |
Douglas Gregor | b2f81cf | 2009-10-01 23:51:25 +0000 | [diff] [blame] | 5109 | TemplateArgsPtr.release(); |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 5110 | } |
Douglas Gregor | 0b60d9e | 2009-09-25 23:53:26 +0000 | [diff] [blame] | 5111 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5112 | // C++ [temp.explicit]p1: |
| 5113 | // A [...] function [...] can be explicitly instantiated from its template. |
| 5114 | // A member function [...] of a class template can be explicitly |
| 5115 | // instantiated from the member definition associated with its class |
| 5116 | // template. |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5117 | UnresolvedSet<8> Matches; |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5118 | for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end(); |
| 5119 | P != PEnd; ++P) { |
| 5120 | NamedDecl *Prev = *P; |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 5121 | if (!HasExplicitTemplateArgs) { |
| 5122 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Prev)) { |
| 5123 | if (Context.hasSameUnqualifiedType(Method->getType(), R)) { |
| 5124 | Matches.clear(); |
Douglas Gregor | 48026d2 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 5125 | |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5126 | Matches.addDecl(Method, P.getAccess()); |
Douglas Gregor | 48026d2 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 5127 | if (Method->getTemplateSpecializationKind() == TSK_Undeclared) |
| 5128 | break; |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 5129 | } |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5130 | } |
| 5131 | } |
| 5132 | |
| 5133 | FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Prev); |
| 5134 | if (!FunTmpl) |
| 5135 | continue; |
| 5136 | |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 5137 | TemplateDeductionInfo Info(Context, D.getIdentifierLoc()); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5138 | FunctionDecl *Specialization = 0; |
| 5139 | if (TemplateDeductionResult TDK |
Douglas Gregor | 48026d2 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 5140 | = DeduceTemplateArguments(FunTmpl, |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 5141 | (HasExplicitTemplateArgs ? &TemplateArgs : 0), |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5142 | R, Specialization, Info)) { |
| 5143 | // FIXME: Keep track of almost-matches? |
| 5144 | (void)TDK; |
| 5145 | continue; |
| 5146 | } |
| 5147 | |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5148 | Matches.addDecl(Specialization, P.getAccess()); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5149 | } |
| 5150 | |
| 5151 | // Find the most specialized function template specialization. |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5152 | UnresolvedSetIterator Result |
| 5153 | = getMostSpecialized(Matches.begin(), Matches.end(), TPOC_Other, |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5154 | D.getIdentifierLoc(), |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 5155 | PDiag(diag::err_explicit_instantiation_not_known) << Name, |
| 5156 | PDiag(diag::err_explicit_instantiation_ambiguous) << Name, |
| 5157 | PDiag(diag::note_explicit_instantiation_candidate)); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5158 | |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5159 | if (Result == Matches.end()) |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5160 | return true; |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5161 | |
| 5162 | // Ignore access control bits, we don't need them for redeclaration checking. |
| 5163 | FunctionDecl *Specialization = cast<FunctionDecl>(*Result); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5164 | |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 5165 | if (Specialization->getTemplateSpecializationKind() == TSK_Undeclared) { |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5166 | Diag(D.getIdentifierLoc(), |
| 5167 | diag::err_explicit_instantiation_member_function_not_instantiated) |
| 5168 | << Specialization |
| 5169 | << (Specialization->getTemplateSpecializationKind() == |
| 5170 | TSK_ExplicitSpecialization); |
| 5171 | Diag(Specialization->getLocation(), diag::note_explicit_instantiation_here); |
| 5172 | return true; |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 5173 | } |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5174 | |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 5175 | FunctionDecl *PrevDecl = Specialization->getPreviousDeclaration(); |
Douglas Gregor | 583f33b | 2009-10-15 18:07:02 +0000 | [diff] [blame] | 5176 | if (!PrevDecl && Specialization->isThisDeclarationADefinition()) |
| 5177 | PrevDecl = Specialization; |
| 5178 | |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 5179 | if (PrevDecl) { |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5180 | bool HasNoEffect = false; |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 5181 | if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK, |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 5182 | PrevDecl, |
| 5183 | PrevDecl->getTemplateSpecializationKind(), |
| 5184 | PrevDecl->getPointOfInstantiation(), |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5185 | HasNoEffect)) |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 5186 | return true; |
| 5187 | |
| 5188 | // FIXME: We may still want to build some representation of this |
| 5189 | // explicit specialization. |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5190 | if (HasNoEffect) |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 5191 | return DeclPtrTy(); |
| 5192 | } |
Anders Carlsson | 26d6e9d | 2009-11-24 05:34:41 +0000 | [diff] [blame] | 5193 | |
| 5194 | Specialization->setTemplateSpecializationKind(TSK, D.getIdentifierLoc()); |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 5195 | |
| 5196 | if (TSK == TSK_ExplicitInstantiationDefinition) |
| 5197 | InstantiateFunctionDefinition(D.getIdentifierLoc(), Specialization, |
| 5198 | false, /*DefinitionRequired=*/true); |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 5199 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5200 | // C++0x [temp.explicit]p2: |
| 5201 | // If the explicit instantiation is for a member function, a member class |
| 5202 | // or a static data member of a class template specialization, the name of |
| 5203 | // the class template specialization in the qualified-id for the member |
| 5204 | // name shall be a simple-template-id. |
| 5205 | // |
| 5206 | // C++98 has the same restriction, just worded differently. |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 5207 | FunctionTemplateDecl *FunTmpl = Specialization->getPrimaryTemplate(); |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 5208 | if (D.getName().getKind() != UnqualifiedId::IK_TemplateId && !FunTmpl && |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5209 | D.getCXXScopeSpec().isSet() && |
| 5210 | !ScopeSpecifierHasTemplateId(D.getCXXScopeSpec())) |
| 5211 | Diag(D.getIdentifierLoc(), |
Douglas Gregor | a2dd828 | 2010-06-16 16:26:47 +0000 | [diff] [blame] | 5212 | diag::ext_explicit_instantiation_without_qualified_id) |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5213 | << Specialization << D.getCXXScopeSpec().getRange(); |
| 5214 | |
| 5215 | CheckExplicitInstantiationScope(*this, |
| 5216 | FunTmpl? (NamedDecl *)FunTmpl |
| 5217 | : Specialization->getInstantiatedFromMemberFunction(), |
| 5218 | D.getIdentifierLoc(), |
| 5219 | D.getCXXScopeSpec().isSet()); |
| 5220 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5221 | // FIXME: Create some kind of ExplicitInstantiationDecl here. |
| 5222 | return DeclPtrTy(); |
| 5223 | } |
| 5224 | |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5225 | Sema::TypeResult |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 5226 | Sema::ActOnDependentTag(Scope *S, unsigned TagSpec, TagUseKind TUK, |
| 5227 | const CXXScopeSpec &SS, IdentifierInfo *Name, |
| 5228 | SourceLocation TagLoc, SourceLocation NameLoc) { |
| 5229 | // This has to hold, because SS is expected to be defined. |
| 5230 | assert(Name && "Expected a name in a dependent tag"); |
| 5231 | |
| 5232 | NestedNameSpecifier *NNS |
| 5233 | = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); |
| 5234 | if (!NNS) |
| 5235 | return true; |
| 5236 | |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 5237 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
Daniel Dunbar | 12c0ade | 2010-04-01 16:50:48 +0000 | [diff] [blame] | 5238 | |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 5239 | if (TUK == TUK_Declaration || TUK == TUK_Definition) { |
| 5240 | Diag(NameLoc, diag::err_dependent_tag_decl) |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 5241 | << (TUK == TUK_Definition) << Kind << SS.getRange(); |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 5242 | return true; |
| 5243 | } |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 5244 | |
| 5245 | ElaboratedTypeKeyword Kwd = TypeWithKeyword::getKeywordForTagTypeKind(Kind); |
| 5246 | return Context.getDependentNameType(Kwd, NNS, Name).getAsOpaquePtr(); |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 5247 | } |
| 5248 | |
| 5249 | Sema::TypeResult |
Douglas Gregor | 1a15dae | 2010-06-16 22:31:08 +0000 | [diff] [blame] | 5250 | Sema::ActOnTypenameType(Scope *S, SourceLocation TypenameLoc, |
| 5251 | const CXXScopeSpec &SS, const IdentifierInfo &II, |
| 5252 | SourceLocation IdLoc) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5253 | NestedNameSpecifier *NNS |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5254 | = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); |
| 5255 | if (!NNS) |
| 5256 | return true; |
| 5257 | |
Douglas Gregor | 1a15dae | 2010-06-16 22:31:08 +0000 | [diff] [blame] | 5258 | if (TypenameLoc.isValid() && S && !S->getTemplateParamParent() && |
| 5259 | !getLangOptions().CPlusPlus0x) |
| 5260 | Diag(TypenameLoc, diag::ext_typename_outside_of_template) |
| 5261 | << FixItHint::CreateRemoval(TypenameLoc); |
| 5262 | |
Douglas Gregor | 107de90 | 2010-04-24 15:35:55 +0000 | [diff] [blame] | 5263 | QualType T = CheckTypenameType(ETK_Typename, NNS, II, |
Abramo Bagnara | e4da7a0 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 5264 | TypenameLoc, SS.getRange(), IdLoc); |
Douglas Gregor | 31a19b6 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 5265 | if (T.isNull()) |
| 5266 | return true; |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 5267 | |
| 5268 | TypeSourceInfo *TSI = Context.CreateTypeSourceInfo(T); |
| 5269 | if (isa<DependentNameType>(T)) { |
| 5270 | DependentNameTypeLoc TL = cast<DependentNameTypeLoc>(TSI->getTypeLoc()); |
John McCall | 4e44983 | 2010-05-28 23:32:21 +0000 | [diff] [blame] | 5271 | TL.setKeywordLoc(TypenameLoc); |
| 5272 | TL.setQualifierRange(SS.getRange()); |
| 5273 | TL.setNameLoc(IdLoc); |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 5274 | } else { |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 5275 | ElaboratedTypeLoc TL = cast<ElaboratedTypeLoc>(TSI->getTypeLoc()); |
John McCall | 4e44983 | 2010-05-28 23:32:21 +0000 | [diff] [blame] | 5276 | TL.setKeywordLoc(TypenameLoc); |
| 5277 | TL.setQualifierRange(SS.getRange()); |
| 5278 | cast<TypeSpecTypeLoc>(TL.getNamedTypeLoc()).setNameLoc(IdLoc); |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 5279 | } |
| 5280 | |
| 5281 | return CreateLocInfoType(T, TSI).getAsOpaquePtr(); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5282 | } |
| 5283 | |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 5284 | Sema::TypeResult |
Douglas Gregor | 1a15dae | 2010-06-16 22:31:08 +0000 | [diff] [blame] | 5285 | Sema::ActOnTypenameType(Scope *S, SourceLocation TypenameLoc, |
| 5286 | const CXXScopeSpec &SS, SourceLocation TemplateLoc, |
| 5287 | TypeTy *Ty) { |
| 5288 | if (TypenameLoc.isValid() && S && !S->getTemplateParamParent() && |
| 5289 | !getLangOptions().CPlusPlus0x) |
| 5290 | Diag(TypenameLoc, diag::ext_typename_outside_of_template) |
| 5291 | << FixItHint::CreateRemoval(TypenameLoc); |
| 5292 | |
John McCall | 4e44983 | 2010-05-28 23:32:21 +0000 | [diff] [blame] | 5293 | TypeSourceInfo *InnerTSI = 0; |
| 5294 | QualType T = GetTypeFromParser(Ty, &InnerTSI); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5295 | NestedNameSpecifier *NNS |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 5296 | = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); |
John McCall | 4e44983 | 2010-05-28 23:32:21 +0000 | [diff] [blame] | 5297 | |
| 5298 | assert(isa<TemplateSpecializationType>(T) && |
| 5299 | "Expected a template specialization type"); |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 5300 | |
Douglas Gregor | 6946baf | 2009-09-02 13:05:45 +0000 | [diff] [blame] | 5301 | if (computeDeclContext(SS, false)) { |
| 5302 | // If we can compute a declaration context, then the "typename" |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 5303 | // keyword was superfluous. Just build an ElaboratedType to keep |
Douglas Gregor | 6946baf | 2009-09-02 13:05:45 +0000 | [diff] [blame] | 5304 | // track of the nested-name-specifier. |
John McCall | 4e44983 | 2010-05-28 23:32:21 +0000 | [diff] [blame] | 5305 | |
| 5306 | // Push the inner type, preserving its source locations if possible. |
| 5307 | TypeLocBuilder Builder; |
| 5308 | if (InnerTSI) |
| 5309 | Builder.pushFullCopy(InnerTSI->getTypeLoc()); |
| 5310 | else |
| 5311 | Builder.push<TemplateSpecializationTypeLoc>(T).initialize(TemplateLoc); |
| 5312 | |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 5313 | T = Context.getElaboratedType(ETK_Typename, NNS, T); |
John McCall | 4e44983 | 2010-05-28 23:32:21 +0000 | [diff] [blame] | 5314 | ElaboratedTypeLoc TL = Builder.push<ElaboratedTypeLoc>(T); |
| 5315 | TL.setKeywordLoc(TypenameLoc); |
| 5316 | TL.setQualifierRange(SS.getRange()); |
| 5317 | |
| 5318 | TypeSourceInfo *TSI = Builder.getTypeSourceInfo(Context, T); |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 5319 | return CreateLocInfoType(T, TSI).getAsOpaquePtr(); |
Douglas Gregor | 6946baf | 2009-09-02 13:05:45 +0000 | [diff] [blame] | 5320 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5321 | |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 5322 | // TODO: it's really silly that we make a template specialization |
| 5323 | // type earlier only to drop it again here. |
| 5324 | TemplateSpecializationType *TST = cast<TemplateSpecializationType>(T); |
| 5325 | DependentTemplateName *DTN = |
| 5326 | TST->getTemplateName().getAsDependentTemplateName(); |
| 5327 | assert(DTN && "dependent template has non-dependent name?"); |
| 5328 | T = Context.getDependentTemplateSpecializationType(ETK_Typename, NNS, |
| 5329 | DTN->getIdentifier(), |
| 5330 | TST->getNumArgs(), |
| 5331 | TST->getArgs()); |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 5332 | TypeSourceInfo *TSI = Context.CreateTypeSourceInfo(T); |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 5333 | DependentTemplateSpecializationTypeLoc TL = |
| 5334 | cast<DependentTemplateSpecializationTypeLoc>(TSI->getTypeLoc()); |
| 5335 | if (InnerTSI) { |
| 5336 | TemplateSpecializationTypeLoc TSTL = |
| 5337 | cast<TemplateSpecializationTypeLoc>(InnerTSI->getTypeLoc()); |
| 5338 | TL.setLAngleLoc(TSTL.getLAngleLoc()); |
| 5339 | TL.setRAngleLoc(TSTL.getRAngleLoc()); |
| 5340 | for (unsigned I = 0, E = TST->getNumArgs(); I != E; ++I) |
| 5341 | TL.setArgLocInfo(I, TSTL.getArgLocInfo(I)); |
| 5342 | } else { |
| 5343 | TL.initializeLocal(SourceLocation()); |
| 5344 | } |
John McCall | 4e44983 | 2010-05-28 23:32:21 +0000 | [diff] [blame] | 5345 | TL.setKeywordLoc(TypenameLoc); |
| 5346 | TL.setQualifierRange(SS.getRange()); |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 5347 | return CreateLocInfoType(T, TSI).getAsOpaquePtr(); |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 5348 | } |
| 5349 | |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5350 | /// \brief Build the type that describes a C++ typename specifier, |
| 5351 | /// e.g., "typename T::type". |
| 5352 | QualType |
Douglas Gregor | 107de90 | 2010-04-24 15:35:55 +0000 | [diff] [blame] | 5353 | Sema::CheckTypenameType(ElaboratedTypeKeyword Keyword, |
| 5354 | NestedNameSpecifier *NNS, const IdentifierInfo &II, |
Abramo Bagnara | e4da7a0 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 5355 | SourceLocation KeywordLoc, SourceRange NNSRange, |
| 5356 | SourceLocation IILoc) { |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 5357 | CXXScopeSpec SS; |
| 5358 | SS.setScopeRep(NNS); |
Abramo Bagnara | e4da7a0 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 5359 | SS.setRange(NNSRange); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5360 | |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 5361 | DeclContext *Ctx = computeDeclContext(SS); |
| 5362 | if (!Ctx) { |
| 5363 | // If the nested-name-specifier is dependent and couldn't be |
| 5364 | // resolved to a type, build a typename type. |
| 5365 | assert(NNS->isDependent()); |
| 5366 | return Context.getDependentNameType(Keyword, NNS, &II); |
Douglas Gregor | 42af25f | 2009-05-11 19:58:34 +0000 | [diff] [blame] | 5367 | } |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5368 | |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 5369 | // If the nested-name-specifier refers to the current instantiation, |
| 5370 | // the "typename" keyword itself is superfluous. In C++03, the |
| 5371 | // program is actually ill-formed. However, DR 382 (in C++0x CD1) |
| 5372 | // allows such extraneous "typename" keywords, and we retroactively |
Douglas Gregor | 732281d | 2010-06-14 22:07:54 +0000 | [diff] [blame] | 5373 | // apply this DR to C++03 code with only a warning. In any case we continue. |
Douglas Gregor | 42af25f | 2009-05-11 19:58:34 +0000 | [diff] [blame] | 5374 | |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 5375 | if (RequireCompleteDeclContext(SS, Ctx)) |
| 5376 | return QualType(); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5377 | |
| 5378 | DeclarationName Name(&II); |
Abramo Bagnara | e4da7a0 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 5379 | LookupResult Result(*this, Name, IILoc, LookupOrdinaryName); |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 5380 | LookupQualifiedName(Result, Ctx); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5381 | unsigned DiagID = 0; |
| 5382 | Decl *Referenced = 0; |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 5383 | switch (Result.getResultKind()) { |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5384 | case LookupResult::NotFound: |
Douglas Gregor | 3f09327 | 2009-10-13 21:16:44 +0000 | [diff] [blame] | 5385 | DiagID = diag::err_typename_nested_not_found; |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5386 | break; |
Douglas Gregor | 7d3f576 | 2010-01-15 01:44:47 +0000 | [diff] [blame] | 5387 | |
| 5388 | case LookupResult::NotFoundInCurrentInstantiation: |
| 5389 | // Okay, it's a member of an unknown instantiation. |
Douglas Gregor | 107de90 | 2010-04-24 15:35:55 +0000 | [diff] [blame] | 5390 | return Context.getDependentNameType(Keyword, NNS, &II); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5391 | |
| 5392 | case LookupResult::Found: |
Douglas Gregor | 1a15dae | 2010-06-16 22:31:08 +0000 | [diff] [blame] | 5393 | if (TypeDecl *Type = dyn_cast<TypeDecl>(Result.getFoundDecl())) { |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 5394 | // We found a type. Build an ElaboratedType, since the |
| 5395 | // typename-specifier was just sugar. |
| 5396 | return Context.getElaboratedType(ETK_Typename, NNS, |
| 5397 | Context.getTypeDeclType(Type)); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5398 | } |
| 5399 | |
| 5400 | DiagID = diag::err_typename_nested_not_type; |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 5401 | Referenced = Result.getFoundDecl(); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5402 | break; |
| 5403 | |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 5404 | case LookupResult::FoundUnresolvedValue: |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 5405 | llvm_unreachable("unresolved using decl in non-dependent context"); |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 5406 | return QualType(); |
| 5407 | |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5408 | case LookupResult::FoundOverloaded: |
| 5409 | DiagID = diag::err_typename_nested_not_type; |
| 5410 | Referenced = *Result.begin(); |
| 5411 | break; |
| 5412 | |
John McCall | 6e24726 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 5413 | case LookupResult::Ambiguous: |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5414 | return QualType(); |
| 5415 | } |
| 5416 | |
| 5417 | // If we get here, it's because name lookup did not find a |
| 5418 | // type. Emit an appropriate diagnostic and return an error. |
Abramo Bagnara | e4da7a0 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 5419 | SourceRange FullRange(KeywordLoc.isValid() ? KeywordLoc : NNSRange.getBegin(), |
| 5420 | IILoc); |
| 5421 | Diag(IILoc, DiagID) << FullRange << Name << Ctx; |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5422 | if (Referenced) |
| 5423 | Diag(Referenced->getLocation(), diag::note_typename_refers_here) |
| 5424 | << Name; |
| 5425 | return QualType(); |
| 5426 | } |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 5427 | |
| 5428 | namespace { |
| 5429 | // See Sema::RebuildTypeInCurrentInstantiation |
Benjamin Kramer | 85b4521 | 2009-11-28 19:45:26 +0000 | [diff] [blame] | 5430 | class CurrentInstantiationRebuilder |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5431 | : public TreeTransform<CurrentInstantiationRebuilder> { |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 5432 | SourceLocation Loc; |
| 5433 | DeclarationName Entity; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5434 | |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 5435 | public: |
Douglas Gregor | 895162d | 2010-04-30 18:55:50 +0000 | [diff] [blame] | 5436 | typedef TreeTransform<CurrentInstantiationRebuilder> inherited; |
| 5437 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5438 | CurrentInstantiationRebuilder(Sema &SemaRef, |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 5439 | SourceLocation Loc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5440 | DeclarationName Entity) |
| 5441 | : TreeTransform<CurrentInstantiationRebuilder>(SemaRef), |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 5442 | Loc(Loc), Entity(Entity) { } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5443 | |
| 5444 | /// \brief Determine whether the given type \p T has already been |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 5445 | /// transformed. |
| 5446 | /// |
| 5447 | /// For the purposes of type reconstruction, a type has already been |
| 5448 | /// transformed if it is NULL or if it is not dependent. |
| 5449 | bool AlreadyTransformed(QualType T) { |
| 5450 | return T.isNull() || !T->isDependentType(); |
| 5451 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5452 | |
| 5453 | /// \brief Returns the location of the entity whose type is being |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 5454 | /// rebuilt. |
| 5455 | SourceLocation getBaseLocation() { return Loc; } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5456 | |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 5457 | /// \brief Returns the name of the entity whose type is being rebuilt. |
| 5458 | DeclarationName getBaseEntity() { return Entity; } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5459 | |
Douglas Gregor | 972e6ce | 2009-10-27 06:26:26 +0000 | [diff] [blame] | 5460 | /// \brief Sets the "base" location and entity when that |
| 5461 | /// information is known based on another transformation. |
| 5462 | void setBase(SourceLocation Loc, DeclarationName Entity) { |
| 5463 | this->Loc = Loc; |
| 5464 | this->Entity = Entity; |
| 5465 | } |
| 5466 | |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 5467 | /// \brief Transforms an expression by returning the expression itself |
| 5468 | /// (an identity function). |
| 5469 | /// |
| 5470 | /// FIXME: This is completely unsafe; we will need to actually clone the |
| 5471 | /// expressions. |
| 5472 | Sema::OwningExprResult TransformExpr(Expr *E) { |
Douglas Gregor | 895162d | 2010-04-30 18:55:50 +0000 | [diff] [blame] | 5473 | return getSema().Owned(E->Retain()); |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 5474 | } |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 5475 | }; |
| 5476 | } |
| 5477 | |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 5478 | /// \brief Rebuilds a type within the context of the current instantiation. |
| 5479 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5480 | /// 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] | 5481 | /// a class template (or class template partial specialization) that was parsed |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5482 | /// and constructed before we entered the scope of the class template (or |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 5483 | /// partial specialization thereof). This routine will rebuild that type now |
| 5484 | /// that we have entered the declarator's scope, which may produce different |
| 5485 | /// canonical types, e.g., |
| 5486 | /// |
| 5487 | /// \code |
| 5488 | /// template<typename T> |
| 5489 | /// struct X { |
| 5490 | /// typedef T* pointer; |
| 5491 | /// pointer data(); |
| 5492 | /// }; |
| 5493 | /// |
| 5494 | /// template<typename T> |
| 5495 | /// typename X<T>::pointer X<T>::data() { ... } |
| 5496 | /// \endcode |
| 5497 | /// |
Douglas Gregor | 4714c12 | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 5498 | /// 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] | 5499 | /// since we do not know that we can look into X<T> when we parsed the type. |
| 5500 | /// This function will rebuild the type, performing the lookup of "pointer" |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 5501 | /// in X<T> and returning an ElaboratedType whose canonical type is the same |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 5502 | /// as the canonical type of T*, allowing the return types of the out-of-line |
| 5503 | /// definition and the declaration to match. |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 5504 | TypeSourceInfo *Sema::RebuildTypeInCurrentInstantiation(TypeSourceInfo *T, |
| 5505 | SourceLocation Loc, |
| 5506 | DeclarationName Name) { |
| 5507 | if (!T || !T->getType()->isDependentType()) |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 5508 | return T; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5509 | |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 5510 | CurrentInstantiationRebuilder Rebuilder(*this, Loc, Name); |
| 5511 | return Rebuilder.TransformType(T); |
Benjamin Kramer | 27ba2f0 | 2009-08-11 22:33:06 +0000 | [diff] [blame] | 5512 | } |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 5513 | |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 5514 | bool Sema::RebuildNestedNameSpecifierInCurrentInstantiation(CXXScopeSpec &SS) { |
| 5515 | if (SS.isInvalid()) return true; |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 5516 | |
| 5517 | NestedNameSpecifier *NNS = static_cast<NestedNameSpecifier*>(SS.getScopeRep()); |
| 5518 | CurrentInstantiationRebuilder Rebuilder(*this, SS.getRange().getBegin(), |
| 5519 | DeclarationName()); |
| 5520 | NestedNameSpecifier *Rebuilt = |
| 5521 | Rebuilder.TransformNestedNameSpecifier(NNS, SS.getRange()); |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 5522 | if (!Rebuilt) return true; |
| 5523 | |
| 5524 | SS.setScopeRep(Rebuilt); |
| 5525 | return false; |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 5526 | } |
| 5527 | |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 5528 | /// \brief Produces a formatted string that describes the binding of |
| 5529 | /// template parameters to template arguments. |
| 5530 | std::string |
| 5531 | Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params, |
| 5532 | const TemplateArgumentList &Args) { |
Douglas Gregor | 9148c3f | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 5533 | // FIXME: For variadic templates, we'll need to get the structured list. |
| 5534 | return getTemplateArgumentBindingsText(Params, Args.getFlatArgumentList(), |
| 5535 | Args.flat_size()); |
| 5536 | } |
| 5537 | |
| 5538 | std::string |
| 5539 | Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params, |
| 5540 | const TemplateArgument *Args, |
| 5541 | unsigned NumArgs) { |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 5542 | std::string Result; |
| 5543 | |
Douglas Gregor | 9148c3f | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 5544 | if (!Params || Params->size() == 0 || NumArgs == 0) |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 5545 | return Result; |
| 5546 | |
| 5547 | for (unsigned I = 0, N = Params->size(); I != N; ++I) { |
Douglas Gregor | 9148c3f | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 5548 | if (I >= NumArgs) |
| 5549 | break; |
| 5550 | |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 5551 | if (I == 0) |
| 5552 | Result += "[with "; |
| 5553 | else |
| 5554 | Result += ", "; |
| 5555 | |
| 5556 | if (const IdentifierInfo *Id = Params->getParam(I)->getIdentifier()) { |
| 5557 | Result += Id->getName(); |
| 5558 | } else { |
| 5559 | Result += '$'; |
| 5560 | Result += llvm::utostr(I); |
| 5561 | } |
| 5562 | |
| 5563 | Result += " = "; |
| 5564 | |
| 5565 | switch (Args[I].getKind()) { |
| 5566 | case TemplateArgument::Null: |
| 5567 | Result += "<no value>"; |
| 5568 | break; |
| 5569 | |
| 5570 | case TemplateArgument::Type: { |
| 5571 | std::string TypeStr; |
| 5572 | Args[I].getAsType().getAsStringInternal(TypeStr, |
| 5573 | Context.PrintingPolicy); |
| 5574 | Result += TypeStr; |
| 5575 | break; |
| 5576 | } |
| 5577 | |
| 5578 | case TemplateArgument::Declaration: { |
| 5579 | bool Unnamed = true; |
| 5580 | if (NamedDecl *ND = dyn_cast_or_null<NamedDecl>(Args[I].getAsDecl())) { |
| 5581 | if (ND->getDeclName()) { |
| 5582 | Unnamed = false; |
| 5583 | Result += ND->getNameAsString(); |
| 5584 | } |
| 5585 | } |
| 5586 | |
| 5587 | if (Unnamed) { |
| 5588 | Result += "<anonymous>"; |
| 5589 | } |
| 5590 | break; |
| 5591 | } |
| 5592 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 5593 | case TemplateArgument::Template: { |
| 5594 | std::string Str; |
| 5595 | llvm::raw_string_ostream OS(Str); |
| 5596 | Args[I].getAsTemplate().print(OS, Context.PrintingPolicy); |
| 5597 | Result += OS.str(); |
| 5598 | break; |
| 5599 | } |
| 5600 | |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 5601 | case TemplateArgument::Integral: { |
| 5602 | Result += Args[I].getAsIntegral()->toString(10); |
| 5603 | break; |
| 5604 | } |
| 5605 | |
| 5606 | case TemplateArgument::Expression: { |
Douglas Gregor | 77e2c67 | 2010-04-29 04:55:13 +0000 | [diff] [blame] | 5607 | // FIXME: This is non-optimal, since we're regurgitating the |
| 5608 | // expression we were given. |
| 5609 | std::string Str; |
| 5610 | { |
| 5611 | llvm::raw_string_ostream OS(Str); |
| 5612 | Args[I].getAsExpr()->printPretty(OS, Context, 0, |
| 5613 | Context.PrintingPolicy); |
| 5614 | } |
| 5615 | Result += Str; |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 5616 | break; |
| 5617 | } |
| 5618 | |
| 5619 | case TemplateArgument::Pack: |
| 5620 | // FIXME: Format template argument packs |
| 5621 | Result += "<template argument pack>"; |
| 5622 | break; |
| 5623 | } |
| 5624 | } |
| 5625 | |
| 5626 | Result += ']'; |
| 5627 | return Result; |
| 5628 | } |