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