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 | |
John McCall | 2d88708 | 2010-08-25 22:03:47 +0000 | [diff] [blame] | 12 | #include "clang/Sema/SemaInternal.h" |
Douglas Gregor | e737f50 | 2010-08-12 20:07:10 +0000 | [diff] [blame] | 13 | #include "clang/Sema/Lookup.h" |
John McCall | 5f1e094 | 2010-08-24 08:50:51 +0000 | [diff] [blame] | 14 | #include "clang/Sema/Scope.h" |
John McCall | 7cd088e | 2010-08-24 07:21:54 +0000 | [diff] [blame] | 15 | #include "clang/Sema/Template.h" |
John McCall | 2a7fb27 | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 16 | #include "clang/Sema/TemplateDeduction.h" |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 17 | #include "TreeTransform.h" |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 18 | #include "clang/AST/ASTContext.h" |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 19 | #include "clang/AST/Expr.h" |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 20 | #include "clang/AST/ExprCXX.h" |
John McCall | 92b7f70 | 2010-03-11 07:50:04 +0000 | [diff] [blame] | 21 | #include "clang/AST/DeclFriend.h" |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 22 | #include "clang/AST/DeclTemplate.h" |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 23 | #include "clang/AST/RecursiveASTVisitor.h" |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 24 | #include "clang/AST/TypeVisitor.h" |
John McCall | 1951085 | 2010-08-20 18:27:03 +0000 | [diff] [blame] | 25 | #include "clang/Sema/DeclSpec.h" |
| 26 | #include "clang/Sema/ParsedTemplate.h" |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 27 | #include "clang/Basic/LangOptions.h" |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 28 | #include "clang/Basic/PartialDiagnostic.h" |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 29 | #include "llvm/ADT/StringExtras.h" |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 30 | using namespace clang; |
John McCall | 2a7fb27 | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 31 | using namespace sema; |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 32 | |
John McCall | 78b8105 | 2010-11-10 02:40:36 +0000 | [diff] [blame] | 33 | // Exported for use by Parser. |
| 34 | SourceRange |
| 35 | clang::getTemplateParamsRange(TemplateParameterList const * const *Ps, |
| 36 | unsigned N) { |
| 37 | if (!N) return SourceRange(); |
| 38 | return SourceRange(Ps[0]->getTemplateLoc(), Ps[N-1]->getRAngleLoc()); |
| 39 | } |
| 40 | |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 41 | /// \brief Determine whether the declaration found is acceptable as the name |
| 42 | /// of a template and, if so, return that template declaration. Otherwise, |
| 43 | /// returns NULL. |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 44 | static NamedDecl *isAcceptableTemplateName(ASTContext &Context, |
| 45 | NamedDecl *Orig) { |
| 46 | NamedDecl *D = Orig->getUnderlyingDecl(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 47 | |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 48 | if (isa<TemplateDecl>(D)) |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 49 | return Orig; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 50 | |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 51 | if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) { |
| 52 | // C++ [temp.local]p1: |
| 53 | // Like normal (non-template) classes, class templates have an |
| 54 | // injected-class-name (Clause 9). The injected-class-name |
| 55 | // can be used with or without a template-argument-list. When |
| 56 | // it is used without a template-argument-list, it is |
| 57 | // equivalent to the injected-class-name followed by the |
| 58 | // template-parameters of the class template enclosed in |
| 59 | // <>. When it is used with a template-argument-list, it |
| 60 | // refers to the specified class template specialization, |
| 61 | // which could be the current specialization or another |
| 62 | // specialization. |
| 63 | if (Record->isInjectedClassName()) { |
Douglas Gregor | 542b548 | 2009-10-14 17:30:58 +0000 | [diff] [blame] | 64 | Record = cast<CXXRecordDecl>(Record->getDeclContext()); |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 65 | if (Record->getDescribedClassTemplate()) |
| 66 | return Record->getDescribedClassTemplate(); |
| 67 | |
| 68 | if (ClassTemplateSpecializationDecl *Spec |
| 69 | = dyn_cast<ClassTemplateSpecializationDecl>(Record)) |
| 70 | return Spec->getSpecializedTemplate(); |
| 71 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 72 | |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 73 | return 0; |
| 74 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 75 | |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 76 | return 0; |
| 77 | } |
| 78 | |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 79 | void Sema::FilterAcceptableTemplateNames(LookupResult &R) { |
Douglas Gregor | 01e56ae | 2010-04-12 20:54:26 +0000 | [diff] [blame] | 80 | // The set of class templates we've already seen. |
| 81 | llvm::SmallPtrSet<ClassTemplateDecl *, 8> ClassTemplates; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 82 | LookupResult::Filter filter = R.makeFilter(); |
| 83 | while (filter.hasNext()) { |
| 84 | NamedDecl *Orig = filter.next(); |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 85 | NamedDecl *Repl = isAcceptableTemplateName(Context, Orig); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 86 | if (!Repl) |
| 87 | filter.erase(); |
Douglas Gregor | 01e56ae | 2010-04-12 20:54:26 +0000 | [diff] [blame] | 88 | else if (Repl != Orig) { |
| 89 | |
| 90 | // C++ [temp.local]p3: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 91 | // A lookup that finds an injected-class-name (10.2) can result in an |
Douglas Gregor | 01e56ae | 2010-04-12 20:54:26 +0000 | [diff] [blame] | 92 | // ambiguity in certain cases (for example, if it is found in more than |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 93 | // one base class). If all of the injected-class-names that are found |
| 94 | // refer to specializations of the same class template, and if the name |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 95 | // is used as a template-name, the reference refers to the class |
| 96 | // template itself and not a specialization thereof, and is not |
Douglas Gregor | 01e56ae | 2010-04-12 20:54:26 +0000 | [diff] [blame] | 97 | // ambiguous. |
Douglas Gregor | 01e56ae | 2010-04-12 20:54:26 +0000 | [diff] [blame] | 98 | if (ClassTemplateDecl *ClassTmpl = dyn_cast<ClassTemplateDecl>(Repl)) |
| 99 | if (!ClassTemplates.insert(ClassTmpl)) { |
| 100 | filter.erase(); |
| 101 | continue; |
| 102 | } |
John McCall | 8ba6691 | 2010-08-13 07:02:08 +0000 | [diff] [blame] | 103 | |
| 104 | // FIXME: we promote access to public here as a workaround to |
| 105 | // the fact that LookupResult doesn't let us remember that we |
| 106 | // found this template through a particular injected class name, |
| 107 | // which means we end up doing nasty things to the invariants. |
| 108 | // Pretending that access is public is *much* safer. |
| 109 | filter.replace(Repl, AS_public); |
Douglas Gregor | 01e56ae | 2010-04-12 20:54:26 +0000 | [diff] [blame] | 110 | } |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 111 | } |
| 112 | filter.done(); |
| 113 | } |
| 114 | |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 115 | bool Sema::hasAnyAcceptableTemplateNames(LookupResult &R) { |
| 116 | for (LookupResult::iterator I = R.begin(), IEnd = R.end(); I != IEnd; ++I) |
| 117 | if (isAcceptableTemplateName(Context, *I)) |
| 118 | return true; |
| 119 | |
Douglas Gregor | 3b88735 | 2011-04-27 04:48:22 +0000 | [diff] [blame] | 120 | return false; |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 121 | } |
| 122 | |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 123 | TemplateNameKind Sema::isTemplateName(Scope *S, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 124 | CXXScopeSpec &SS, |
Abramo Bagnara | 7c15353 | 2010-08-06 12:11:11 +0000 | [diff] [blame] | 125 | bool hasTemplateKeyword, |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 126 | UnqualifiedId &Name, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 127 | ParsedType ObjectTypePtr, |
Douglas Gregor | 495c35d | 2009-08-25 22:51:20 +0000 | [diff] [blame] | 128 | bool EnteringContext, |
Douglas Gregor | 1fd6d44 | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 129 | TemplateTy &TemplateResult, |
| 130 | bool &MemberOfUnknownSpecialization) { |
Douglas Gregor | b862b8f | 2010-01-11 23:29:10 +0000 | [diff] [blame] | 131 | assert(getLangOptions().CPlusPlus && "No template names in C!"); |
| 132 | |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 133 | DeclarationName TName; |
Douglas Gregor | 1fd6d44 | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 134 | MemberOfUnknownSpecialization = false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 135 | |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 136 | switch (Name.getKind()) { |
| 137 | case UnqualifiedId::IK_Identifier: |
| 138 | TName = DeclarationName(Name.Identifier); |
| 139 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 140 | |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 141 | case UnqualifiedId::IK_OperatorFunctionId: |
| 142 | TName = Context.DeclarationNames.getCXXOperatorName( |
| 143 | Name.OperatorFunctionId.Operator); |
| 144 | break; |
| 145 | |
Sean Hunt | e6252d1 | 2009-11-28 08:58:14 +0000 | [diff] [blame] | 146 | case UnqualifiedId::IK_LiteralOperatorId: |
Sean Hunt | 3e518bd | 2009-11-29 07:34:05 +0000 | [diff] [blame] | 147 | TName = Context.DeclarationNames.getCXXLiteralOperatorName(Name.Identifier); |
| 148 | break; |
Sean Hunt | e6252d1 | 2009-11-28 08:58:14 +0000 | [diff] [blame] | 149 | |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 150 | default: |
| 151 | return TNK_Non_template; |
| 152 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 153 | |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 154 | QualType ObjectType = ObjectTypePtr.get(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 155 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 156 | LookupResult R(*this, TName, Name.getSourceRange().getBegin(), |
Douglas Gregor | bfea239 | 2009-12-31 08:11:17 +0000 | [diff] [blame] | 157 | LookupOrdinaryName); |
Douglas Gregor | 1fd6d44 | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 158 | LookupTemplateName(R, S, SS, ObjectType, EnteringContext, |
| 159 | MemberOfUnknownSpecialization); |
John McCall | 67d22fb | 2010-08-28 20:17:00 +0000 | [diff] [blame] | 160 | if (R.empty()) return TNK_Non_template; |
| 161 | if (R.isAmbiguous()) { |
| 162 | // Suppress diagnostics; we'll redo this lookup later. |
John McCall | b859206 | 2010-08-13 02:23:42 +0000 | [diff] [blame] | 163 | R.suppressDiagnostics(); |
John McCall | 67d22fb | 2010-08-28 20:17:00 +0000 | [diff] [blame] | 164 | |
| 165 | // FIXME: we might have ambiguous templates, in which case we |
| 166 | // should at least parse them properly! |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 167 | return TNK_Non_template; |
John McCall | b859206 | 2010-08-13 02:23:42 +0000 | [diff] [blame] | 168 | } |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 169 | |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 170 | TemplateName Template; |
| 171 | TemplateNameKind TemplateKind; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 172 | |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 173 | unsigned ResultCount = R.end() - R.begin(); |
| 174 | if (ResultCount > 1) { |
| 175 | // We assume that we'll preserve the qualifier from a function |
| 176 | // template name in other ways. |
| 177 | Template = Context.getOverloadedTemplateName(R.begin(), R.end()); |
| 178 | TemplateKind = TNK_Function_template; |
John McCall | b859206 | 2010-08-13 02:23:42 +0000 | [diff] [blame] | 179 | |
| 180 | // We'll do this lookup again later. |
| 181 | R.suppressDiagnostics(); |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 182 | } else { |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 183 | TemplateDecl *TD = cast<TemplateDecl>((*R.begin())->getUnderlyingDecl()); |
| 184 | |
| 185 | if (SS.isSet() && !SS.isInvalid()) { |
| 186 | NestedNameSpecifier *Qualifier |
| 187 | = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); |
Abramo Bagnara | 7c15353 | 2010-08-06 12:11:11 +0000 | [diff] [blame] | 188 | Template = Context.getQualifiedTemplateName(Qualifier, |
| 189 | hasTemplateKeyword, TD); |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 190 | } else { |
| 191 | Template = TemplateName(TD); |
| 192 | } |
| 193 | |
John McCall | b859206 | 2010-08-13 02:23:42 +0000 | [diff] [blame] | 194 | if (isa<FunctionTemplateDecl>(TD)) { |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 195 | TemplateKind = TNK_Function_template; |
John McCall | b859206 | 2010-08-13 02:23:42 +0000 | [diff] [blame] | 196 | |
| 197 | // We'll do this lookup again later. |
| 198 | R.suppressDiagnostics(); |
| 199 | } else { |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 200 | assert(isa<ClassTemplateDecl>(TD) || isa<TemplateTemplateParmDecl>(TD) || |
| 201 | isa<TypeAliasTemplateDecl>(TD)); |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 202 | TemplateKind = TNK_Type_template; |
| 203 | } |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 204 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 205 | |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 206 | TemplateResult = TemplateTy::make(Template); |
| 207 | return TemplateKind; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 208 | } |
| 209 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 210 | bool Sema::DiagnoseUnknownTemplateName(const IdentifierInfo &II, |
Douglas Gregor | 84d0a19 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 211 | SourceLocation IILoc, |
| 212 | Scope *S, |
| 213 | const CXXScopeSpec *SS, |
| 214 | TemplateTy &SuggestedTemplate, |
| 215 | TemplateNameKind &SuggestedKind) { |
| 216 | // We can't recover unless there's a dependent scope specifier preceding the |
| 217 | // template name. |
Douglas Gregor | d5ab9b0 | 2010-05-21 23:43:39 +0000 | [diff] [blame] | 218 | // FIXME: Typo correction? |
Douglas Gregor | 84d0a19 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 219 | if (!SS || !SS->isSet() || !isDependentScopeSpecifier(*SS) || |
| 220 | computeDeclContext(*SS)) |
| 221 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 222 | |
Douglas Gregor | 84d0a19 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 223 | // The code is missing a 'template' keyword prior to the dependent template |
| 224 | // name. |
| 225 | NestedNameSpecifier *Qualifier = (NestedNameSpecifier*)SS->getScopeRep(); |
| 226 | Diag(IILoc, diag::err_template_kw_missing) |
| 227 | << Qualifier << II.getName() |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 228 | << FixItHint::CreateInsertion(IILoc, "template "); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 229 | SuggestedTemplate |
Douglas Gregor | 84d0a19 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 230 | = TemplateTy::make(Context.getDependentTemplateName(Qualifier, &II)); |
| 231 | SuggestedKind = TNK_Dependent_template_name; |
| 232 | return true; |
| 233 | } |
| 234 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 235 | void Sema::LookupTemplateName(LookupResult &Found, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 236 | Scope *S, CXXScopeSpec &SS, |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 237 | QualType ObjectType, |
Douglas Gregor | 1fd6d44 | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 238 | bool EnteringContext, |
| 239 | bool &MemberOfUnknownSpecialization) { |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 240 | // Determine where to perform name lookup |
Douglas Gregor | 1fd6d44 | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 241 | MemberOfUnknownSpecialization = false; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 242 | DeclContext *LookupCtx = 0; |
| 243 | bool isDependent = false; |
| 244 | if (!ObjectType.isNull()) { |
| 245 | // This nested-name-specifier occurs in a member access expression, e.g., |
| 246 | // x->B::f, and we are looking into the type of the object. |
| 247 | assert(!SS.isSet() && "ObjectType and scope specifier cannot coexist"); |
| 248 | LookupCtx = computeDeclContext(ObjectType); |
| 249 | isDependent = ObjectType->isDependentType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 250 | assert((isDependent || !ObjectType->isIncompleteType()) && |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 251 | "Caller should have completed object type"); |
| 252 | } else if (SS.isSet()) { |
| 253 | // This nested-name-specifier occurs after another nested-name-specifier, |
| 254 | // so long into the context associated with the prior nested-name-specifier. |
| 255 | LookupCtx = computeDeclContext(SS, EnteringContext); |
| 256 | isDependent = isDependentScopeSpecifier(SS); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 257 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 258 | // The declaration context must be complete. |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 259 | if (LookupCtx && RequireCompleteDeclContext(SS, LookupCtx)) |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 260 | return; |
| 261 | } |
| 262 | |
| 263 | bool ObjectTypeSearchedInScope = false; |
| 264 | if (LookupCtx) { |
| 265 | // Perform "qualified" name lookup into the declaration context we |
| 266 | // computed, which is either the type of the base of a member access |
| 267 | // expression or the declaration context associated with a prior |
| 268 | // nested-name-specifier. |
| 269 | LookupQualifiedName(Found, LookupCtx); |
| 270 | |
| 271 | if (!ObjectType.isNull() && Found.empty()) { |
| 272 | // C++ [basic.lookup.classref]p1: |
| 273 | // In a class member access expression (5.2.5), if the . or -> token is |
| 274 | // immediately followed by an identifier followed by a <, the |
| 275 | // identifier must be looked up to determine whether the < is the |
| 276 | // beginning of a template argument list (14.2) or a less-than operator. |
| 277 | // The identifier is first looked up in the class of the object |
| 278 | // expression. If the identifier is not found, it is then looked up in |
| 279 | // the context of the entire postfix-expression and shall name a class |
| 280 | // or function template. |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 281 | if (S) LookupName(Found, S); |
| 282 | ObjectTypeSearchedInScope = true; |
| 283 | } |
Douglas Gregor | f9f97a0 | 2010-07-16 16:54:17 +0000 | [diff] [blame] | 284 | } else if (isDependent && (!S || ObjectType.isNull())) { |
Douglas Gregor | 2e93388 | 2010-01-12 17:06:20 +0000 | [diff] [blame] | 285 | // We cannot look into a dependent object type or nested nme |
| 286 | // specifier. |
Douglas Gregor | 1fd6d44 | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 287 | MemberOfUnknownSpecialization = true; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 288 | return; |
| 289 | } else { |
| 290 | // Perform unqualified name lookup in the current scope. |
| 291 | LookupName(Found, S); |
| 292 | } |
| 293 | |
Douglas Gregor | 2e93388 | 2010-01-12 17:06:20 +0000 | [diff] [blame] | 294 | if (Found.empty() && !isDependent) { |
Douglas Gregor | bfea239 | 2009-12-31 08:11:17 +0000 | [diff] [blame] | 295 | // If we did not find any names, attempt to correct any typos. |
| 296 | DeclarationName Name = Found.getLookupName(); |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 297 | Found.clear(); |
| 298 | if (TypoCorrection Corrected = CorrectTypo(Found.getLookupNameInfo(), |
| 299 | Found.getLookupKind(), S, &SS, |
| 300 | LookupCtx, false, |
| 301 | CTC_CXXCasts)) { |
| 302 | Found.setLookupName(Corrected.getCorrection()); |
| 303 | if (Corrected.getCorrectionDecl()) |
| 304 | Found.addDecl(Corrected.getCorrectionDecl()); |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 305 | FilterAcceptableTemplateNames(Found); |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 306 | if (!Found.empty()) { |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 307 | std::string CorrectedStr(Corrected.getAsString(getLangOptions())); |
| 308 | std::string CorrectedQuotedStr(Corrected.getQuoted(getLangOptions())); |
Douglas Gregor | bfea239 | 2009-12-31 08:11:17 +0000 | [diff] [blame] | 309 | if (LookupCtx) |
| 310 | Diag(Found.getNameLoc(), diag::err_no_member_template_suggest) |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 311 | << Name << LookupCtx << CorrectedQuotedStr << SS.getRange() |
| 312 | << FixItHint::CreateReplacement(Found.getNameLoc(), CorrectedStr); |
Douglas Gregor | bfea239 | 2009-12-31 08:11:17 +0000 | [diff] [blame] | 313 | else |
| 314 | Diag(Found.getNameLoc(), diag::err_no_template_suggest) |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 315 | << Name << CorrectedQuotedStr |
| 316 | << FixItHint::CreateReplacement(Found.getNameLoc(), CorrectedStr); |
Douglas Gregor | 67dd1d4 | 2010-01-07 00:17:44 +0000 | [diff] [blame] | 317 | if (TemplateDecl *Template = Found.getAsSingle<TemplateDecl>()) |
| 318 | Diag(Template->getLocation(), diag::note_previous_decl) |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 319 | << CorrectedQuotedStr; |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 320 | } |
Douglas Gregor | bfea239 | 2009-12-31 08:11:17 +0000 | [diff] [blame] | 321 | } else { |
Douglas Gregor | 12eb5d6 | 2010-06-29 19:27:42 +0000 | [diff] [blame] | 322 | Found.setLookupName(Name); |
Douglas Gregor | bfea239 | 2009-12-31 08:11:17 +0000 | [diff] [blame] | 323 | } |
| 324 | } |
| 325 | |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 326 | FilterAcceptableTemplateNames(Found); |
Douglas Gregor | f9f97a0 | 2010-07-16 16:54:17 +0000 | [diff] [blame] | 327 | if (Found.empty()) { |
| 328 | if (isDependent) |
| 329 | MemberOfUnknownSpecialization = true; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 330 | return; |
Douglas Gregor | f9f97a0 | 2010-07-16 16:54:17 +0000 | [diff] [blame] | 331 | } |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 332 | |
| 333 | if (S && !ObjectType.isNull() && !ObjectTypeSearchedInScope) { |
| 334 | // C++ [basic.lookup.classref]p1: |
| 335 | // [...] If the lookup in the class of the object expression finds a |
| 336 | // template, the name is also looked up in the context of the entire |
| 337 | // postfix-expression and [...] |
| 338 | // |
| 339 | LookupResult FoundOuter(*this, Found.getLookupName(), Found.getNameLoc(), |
| 340 | LookupOrdinaryName); |
| 341 | LookupName(FoundOuter, S); |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 342 | FilterAcceptableTemplateNames(FoundOuter); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 343 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 344 | if (FoundOuter.empty()) { |
| 345 | // - if the name is not found, the name found in the class of the |
| 346 | // object expression is used, otherwise |
| 347 | } else if (!FoundOuter.getAsSingle<ClassTemplateDecl>()) { |
| 348 | // - if the name is found in the context of the entire |
| 349 | // postfix-expression and does not name a class template, the name |
| 350 | // found in the class of the object expression is used, otherwise |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 351 | } else if (!Found.isSuppressingDiagnostics()) { |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 352 | // - if the name found is a class template, it must refer to the same |
| 353 | // entity as the one found in the class of the object expression, |
| 354 | // otherwise the program is ill-formed. |
| 355 | if (!Found.isSingleResult() || |
| 356 | Found.getFoundDecl()->getCanonicalDecl() |
| 357 | != FoundOuter.getFoundDecl()->getCanonicalDecl()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 358 | Diag(Found.getNameLoc(), |
Jeffrey Yasskin | 21d07e4 | 2010-06-05 01:39:57 +0000 | [diff] [blame] | 359 | diag::ext_nested_name_member_ref_lookup_ambiguous) |
| 360 | << Found.getLookupName() |
| 361 | << ObjectType; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 362 | Diag(Found.getRepresentativeDecl()->getLocation(), |
| 363 | diag::note_ambig_member_ref_object_type) |
| 364 | << ObjectType; |
| 365 | Diag(FoundOuter.getFoundDecl()->getLocation(), |
| 366 | diag::note_ambig_member_ref_scope); |
| 367 | |
| 368 | // Recover by taking the template that we found in the object |
| 369 | // expression's type. |
| 370 | } |
| 371 | } |
| 372 | } |
| 373 | } |
| 374 | |
John McCall | 2f841ba | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 375 | /// ActOnDependentIdExpression - Handle a dependent id-expression that |
| 376 | /// was just parsed. This is only possible with an explicit scope |
| 377 | /// specifier naming a dependent type. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 378 | ExprResult |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 379 | Sema::ActOnDependentIdExpression(const CXXScopeSpec &SS, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 380 | const DeclarationNameInfo &NameInfo, |
John McCall | 2f841ba | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 381 | bool isAddressOfOperand, |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 382 | const TemplateArgumentListInfo *TemplateArgs) { |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 383 | DeclContext *DC = getFunctionLevelDeclContext(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 384 | |
John McCall | 2f841ba | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 385 | if (!isAddressOfOperand && |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 386 | isa<CXXMethodDecl>(DC) && |
| 387 | cast<CXXMethodDecl>(DC)->isInstance()) { |
| 388 | QualType ThisType = cast<CXXMethodDecl>(DC)->getThisType(Context); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 389 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 390 | // Since the 'this' expression is synthesized, we don't need to |
| 391 | // perform the double-lookup check. |
| 392 | NamedDecl *FirstQualifierInScope = 0; |
| 393 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 394 | return Owned(CXXDependentScopeMemberExpr::Create(Context, |
| 395 | /*This*/ 0, ThisType, |
| 396 | /*IsArrow*/ true, |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 397 | /*Op*/ SourceLocation(), |
Douglas Gregor | 7c3179c | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 398 | SS.getWithLocInContext(Context), |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 399 | FirstQualifierInScope, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 400 | NameInfo, |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 401 | TemplateArgs)); |
| 402 | } |
| 403 | |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 404 | return BuildDependentDeclRefExpr(SS, NameInfo, TemplateArgs); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 405 | } |
| 406 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 407 | ExprResult |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 408 | Sema::BuildDependentDeclRefExpr(const CXXScopeSpec &SS, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 409 | const DeclarationNameInfo &NameInfo, |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 410 | const TemplateArgumentListInfo *TemplateArgs) { |
| 411 | return Owned(DependentScopeDeclRefExpr::Create(Context, |
Douglas Gregor | 00cf3cc | 2011-02-25 20:49:16 +0000 | [diff] [blame] | 412 | SS.getWithLocInContext(Context), |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 413 | NameInfo, |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 414 | TemplateArgs)); |
Douglas Gregor | d6fb7ef | 2008-12-18 19:37:40 +0000 | [diff] [blame] | 415 | } |
| 416 | |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 417 | /// DiagnoseTemplateParameterShadow - Produce a diagnostic complaining |
| 418 | /// that the template parameter 'PrevDecl' is being shadowed by a new |
| 419 | /// declaration at location Loc. Returns true to indicate that this is |
| 420 | /// an error, and false otherwise. |
| 421 | bool Sema::DiagnoseTemplateParameterShadow(SourceLocation Loc, Decl *PrevDecl) { |
Douglas Gregor | f57172b | 2008-12-08 18:40:42 +0000 | [diff] [blame] | 422 | assert(PrevDecl->isTemplateParameter() && "Not a template parameter"); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 423 | |
| 424 | // Microsoft Visual C++ permits template parameters to be shadowed. |
| 425 | if (getLangOptions().Microsoft) |
| 426 | return false; |
| 427 | |
| 428 | // C++ [temp.local]p4: |
| 429 | // A template-parameter shall not be redeclared within its |
| 430 | // scope (including nested scopes). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 431 | Diag(Loc, diag::err_template_param_shadow) |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 432 | << cast<NamedDecl>(PrevDecl)->getDeclName(); |
| 433 | Diag(PrevDecl->getLocation(), diag::note_template_param_here); |
| 434 | return true; |
| 435 | } |
| 436 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 437 | /// AdjustDeclIfTemplate - If the given decl happens to be a template, reset |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 438 | /// the parameter D to reference the templated declaration and return a pointer |
| 439 | /// to the template declaration. Otherwise, do nothing to D and return null. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 440 | TemplateDecl *Sema::AdjustDeclIfTemplate(Decl *&D) { |
| 441 | if (TemplateDecl *Temp = dyn_cast_or_null<TemplateDecl>(D)) { |
| 442 | D = Temp->getTemplatedDecl(); |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 443 | return Temp; |
| 444 | } |
| 445 | return 0; |
| 446 | } |
| 447 | |
Douglas Gregor | ba68eca | 2011-01-05 17:40:24 +0000 | [diff] [blame] | 448 | ParsedTemplateArgument ParsedTemplateArgument::getTemplatePackExpansion( |
| 449 | SourceLocation EllipsisLoc) const { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 450 | assert(Kind == Template && |
Douglas Gregor | ba68eca | 2011-01-05 17:40:24 +0000 | [diff] [blame] | 451 | "Only template template arguments can be pack expansions here"); |
| 452 | assert(getAsTemplate().get().containsUnexpandedParameterPack() && |
| 453 | "Template template argument pack expansion without packs"); |
| 454 | ParsedTemplateArgument Result(*this); |
| 455 | Result.EllipsisLoc = EllipsisLoc; |
| 456 | return Result; |
| 457 | } |
| 458 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 459 | static TemplateArgumentLoc translateTemplateArgument(Sema &SemaRef, |
| 460 | const ParsedTemplateArgument &Arg) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 461 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 462 | switch (Arg.getKind()) { |
| 463 | case ParsedTemplateArgument::Type: { |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 464 | TypeSourceInfo *DI; |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 465 | QualType T = SemaRef.GetTypeFromParser(Arg.getAsType(), &DI); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 466 | if (!DI) |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 467 | DI = SemaRef.Context.getTrivialTypeSourceInfo(T, Arg.getLocation()); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 468 | return TemplateArgumentLoc(TemplateArgument(T), DI); |
| 469 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 470 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 471 | case ParsedTemplateArgument::NonType: { |
| 472 | Expr *E = static_cast<Expr *>(Arg.getAsExpr()); |
| 473 | return TemplateArgumentLoc(TemplateArgument(E), E); |
| 474 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 475 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 476 | case ParsedTemplateArgument::Template: { |
John McCall | 2b5289b | 2010-08-23 07:28:44 +0000 | [diff] [blame] | 477 | TemplateName Template = Arg.getAsTemplate().get(); |
Douglas Gregor | 2be29f4 | 2011-01-14 23:41:42 +0000 | [diff] [blame] | 478 | TemplateArgument TArg; |
| 479 | if (Arg.getEllipsisLoc().isValid()) |
| 480 | TArg = TemplateArgument(Template, llvm::Optional<unsigned int>()); |
| 481 | else |
| 482 | TArg = Template; |
| 483 | return TemplateArgumentLoc(TArg, |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 484 | Arg.getScopeSpec().getWithLocInContext( |
| 485 | SemaRef.Context), |
Douglas Gregor | ba68eca | 2011-01-05 17:40:24 +0000 | [diff] [blame] | 486 | Arg.getLocation(), |
| 487 | Arg.getEllipsisLoc()); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 488 | } |
| 489 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 490 | |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 491 | llvm_unreachable("Unhandled parsed template argument"); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 492 | return TemplateArgumentLoc(); |
| 493 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 494 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 495 | /// \brief Translates template arguments as provided by the parser |
| 496 | /// into template arguments used by semantic analysis. |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 497 | void Sema::translateTemplateArguments(const ASTTemplateArgsPtr &TemplateArgsIn, |
| 498 | TemplateArgumentListInfo &TemplateArgs) { |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 499 | for (unsigned I = 0, Last = TemplateArgsIn.size(); I != Last; ++I) |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 500 | TemplateArgs.addArgument(translateTemplateArgument(*this, |
| 501 | TemplateArgsIn[I])); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 502 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 503 | |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 504 | /// ActOnTypeParameter - Called when a C++ template type parameter |
| 505 | /// (e.g., "typename T") has been parsed. Typename specifies whether |
| 506 | /// the keyword "typename" was used to declare the type parameter |
| 507 | /// (otherwise, "class" was used), and KeyLoc is the location of the |
| 508 | /// "class" or "typename" keyword. ParamName is the name of the |
| 509 | /// parameter (NULL indicates an unnamed template parameter) and |
Chandler Carruth | 4fb86f8 | 2011-05-01 00:51:33 +0000 | [diff] [blame] | 510 | /// ParamNameLoc is the location of the parameter name (if any). |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 511 | /// If the type parameter has a default argument, it will be added |
| 512 | /// later via ActOnTypeParameterDefault. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 513 | Decl *Sema::ActOnTypeParameter(Scope *S, bool Typename, bool Ellipsis, |
| 514 | SourceLocation EllipsisLoc, |
| 515 | SourceLocation KeyLoc, |
| 516 | IdentifierInfo *ParamName, |
| 517 | SourceLocation ParamNameLoc, |
| 518 | unsigned Depth, unsigned Position, |
| 519 | SourceLocation EqualLoc, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 520 | ParsedType DefaultArg) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 521 | assert(S->isTemplateParamScope() && |
| 522 | "Template type parameter not in template parameter scope!"); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 523 | bool Invalid = false; |
| 524 | |
| 525 | if (ParamName) { |
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 526 | NamedDecl *PrevDecl = LookupSingleName(S, ParamName, ParamNameLoc, |
Douglas Gregor | c0b3964 | 2010-04-15 23:40:53 +0000 | [diff] [blame] | 527 | LookupOrdinaryName, |
| 528 | ForRedeclaration); |
Douglas Gregor | f57172b | 2008-12-08 18:40:42 +0000 | [diff] [blame] | 529 | if (PrevDecl && PrevDecl->isTemplateParameter()) |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 530 | Invalid = Invalid || DiagnoseTemplateParameterShadow(ParamNameLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 531 | PrevDecl); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 532 | } |
| 533 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 534 | SourceLocation Loc = ParamNameLoc; |
| 535 | if (!ParamName) |
| 536 | Loc = KeyLoc; |
| 537 | |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 538 | TemplateTypeParmDecl *Param |
John McCall | 7a9813c | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 539 | = TemplateTypeParmDecl::Create(Context, Context.getTranslationUnitDecl(), |
Abramo Bagnara | 344577e | 2011-03-06 15:48:19 +0000 | [diff] [blame] | 540 | KeyLoc, Loc, Depth, Position, ParamName, |
| 541 | Typename, Ellipsis); |
Douglas Gregor | 9a299e0 | 2011-03-04 17:52:15 +0000 | [diff] [blame] | 542 | Param->setAccess(AS_public); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 543 | if (Invalid) |
| 544 | Param->setInvalidDecl(); |
| 545 | |
| 546 | if (ParamName) { |
| 547 | // Add the template parameter into the current scope. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 548 | S->AddDecl(Param); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 549 | IdResolver.AddDecl(Param); |
| 550 | } |
| 551 | |
Douglas Gregor | 61c4d28 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 552 | // C++0x [temp.param]p9: |
| 553 | // A default template-argument may be specified for any kind of |
| 554 | // template-parameter that is not a template parameter pack. |
| 555 | if (DefaultArg && Ellipsis) { |
| 556 | Diag(EqualLoc, diag::err_template_param_pack_default_arg); |
| 557 | DefaultArg = ParsedType(); |
| 558 | } |
| 559 | |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 560 | // Handle the default argument, if provided. |
| 561 | if (DefaultArg) { |
| 562 | TypeSourceInfo *DefaultTInfo; |
| 563 | GetTypeFromParser(DefaultArg, &DefaultTInfo); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 564 | |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 565 | assert(DefaultTInfo && "expected source information for type"); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 566 | |
Douglas Gregor | 6f52675 | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 567 | // Check for unexpanded parameter packs. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 568 | if (DiagnoseUnexpandedParameterPack(Loc, DefaultTInfo, |
Douglas Gregor | 6f52675 | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 569 | UPPC_DefaultArgument)) |
| 570 | return Param; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 571 | |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 572 | // Check the template argument itself. |
| 573 | if (CheckTemplateArgument(Param, DefaultTInfo)) { |
| 574 | Param->setInvalidDecl(); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 575 | return Param; |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 576 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 577 | |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 578 | Param->setDefaultArgument(DefaultTInfo, false); |
| 579 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 580 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 581 | return Param; |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 582 | } |
| 583 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 584 | /// \brief Check that the type of a non-type template parameter is |
| 585 | /// well-formed. |
| 586 | /// |
| 587 | /// \returns the (possibly-promoted) parameter type if valid; |
| 588 | /// otherwise, produces a diagnostic and returns a NULL type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 589 | QualType |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 590 | Sema::CheckNonTypeTemplateParameterType(QualType T, SourceLocation Loc) { |
Douglas Gregor | a481ec4 | 2010-05-23 19:57:01 +0000 | [diff] [blame] | 591 | // We don't allow variably-modified types as the type of non-type template |
| 592 | // parameters. |
| 593 | if (T->isVariablyModifiedType()) { |
| 594 | Diag(Loc, diag::err_variably_modified_nontype_template_param) |
| 595 | << T; |
| 596 | return QualType(); |
| 597 | } |
| 598 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 599 | // C++ [temp.param]p4: |
| 600 | // |
| 601 | // A non-type template-parameter shall have one of the following |
| 602 | // (optionally cv-qualified) types: |
| 603 | // |
| 604 | // -- integral or enumeration type, |
Douglas Gregor | 2ade35e | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 605 | if (T->isIntegralOrEnumerationType() || |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 606 | // -- pointer to object or pointer to function, |
Eli Friedman | 1357869 | 2010-08-05 02:49:48 +0000 | [diff] [blame] | 607 | T->isPointerType() || |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 608 | // -- reference to object or reference to function, |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 609 | T->isReferenceType() || |
Douglas Gregor | 84ee2ee | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 610 | // -- pointer to member, |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 611 | T->isMemberPointerType() || |
Douglas Gregor | 84ee2ee | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 612 | // -- std::nullptr_t. |
| 613 | T->isNullPtrType() || |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 614 | // If T is a dependent type, we can't do the check now, so we |
| 615 | // assume that it is well-formed. |
| 616 | T->isDependentType()) |
| 617 | return T; |
| 618 | // C++ [temp.param]p8: |
| 619 | // |
| 620 | // A non-type template-parameter of type "array of T" or |
| 621 | // "function returning T" is adjusted to be of type "pointer to |
| 622 | // T" or "pointer to function returning T", respectively. |
| 623 | else if (T->isArrayType()) |
| 624 | // FIXME: Keep the type prior to promotion? |
| 625 | return Context.getArrayDecayedType(T); |
| 626 | else if (T->isFunctionType()) |
| 627 | // FIXME: Keep the type prior to promotion? |
| 628 | return Context.getPointerType(T); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 629 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 630 | Diag(Loc, diag::err_template_nontype_parm_bad_type) |
| 631 | << T; |
| 632 | |
| 633 | return QualType(); |
| 634 | } |
| 635 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 636 | Decl *Sema::ActOnNonTypeTemplateParameter(Scope *S, Declarator &D, |
| 637 | unsigned Depth, |
| 638 | unsigned Position, |
| 639 | SourceLocation EqualLoc, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 640 | Expr *Default) { |
John McCall | bf1a028 | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 641 | TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S); |
| 642 | QualType T = TInfo->getType(); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 643 | |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 644 | assert(S->isTemplateParamScope() && |
| 645 | "Non-type template parameter not in template parameter scope!"); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 646 | bool Invalid = false; |
| 647 | |
| 648 | IdentifierInfo *ParamName = D.getIdentifier(); |
| 649 | if (ParamName) { |
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 650 | NamedDecl *PrevDecl = LookupSingleName(S, ParamName, D.getIdentifierLoc(), |
Douglas Gregor | c0b3964 | 2010-04-15 23:40:53 +0000 | [diff] [blame] | 651 | LookupOrdinaryName, |
| 652 | ForRedeclaration); |
Douglas Gregor | f57172b | 2008-12-08 18:40:42 +0000 | [diff] [blame] | 653 | if (PrevDecl && PrevDecl->isTemplateParameter()) |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 654 | Invalid = Invalid || DiagnoseTemplateParameterShadow(D.getIdentifierLoc(), |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 655 | PrevDecl); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 656 | } |
| 657 | |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 658 | T = CheckNonTypeTemplateParameterType(T, D.getIdentifierLoc()); |
| 659 | if (T.isNull()) { |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 660 | T = Context.IntTy; // Recover with an 'int' type. |
Douglas Gregor | ceef30c | 2009-03-09 16:46:39 +0000 | [diff] [blame] | 661 | Invalid = true; |
| 662 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 663 | |
Douglas Gregor | 10738d3 | 2010-12-23 23:51:58 +0000 | [diff] [blame] | 664 | bool IsParameterPack = D.hasEllipsis(); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 665 | NonTypeTemplateParmDecl *Param |
John McCall | 7a9813c | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 666 | = NonTypeTemplateParmDecl::Create(Context, Context.getTranslationUnitDecl(), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 667 | D.getSourceRange().getBegin(), |
John McCall | 7a9813c | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 668 | D.getIdentifierLoc(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 669 | Depth, Position, ParamName, T, |
Douglas Gregor | 10738d3 | 2010-12-23 23:51:58 +0000 | [diff] [blame] | 670 | IsParameterPack, TInfo); |
Douglas Gregor | 9a299e0 | 2011-03-04 17:52:15 +0000 | [diff] [blame] | 671 | Param->setAccess(AS_public); |
| 672 | |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 673 | if (Invalid) |
| 674 | Param->setInvalidDecl(); |
| 675 | |
| 676 | if (D.getIdentifier()) { |
| 677 | // Add the template parameter into the current scope. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 678 | S->AddDecl(Param); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 679 | IdResolver.AddDecl(Param); |
| 680 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 681 | |
Douglas Gregor | 61c4d28 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 682 | // C++0x [temp.param]p9: |
| 683 | // A default template-argument may be specified for any kind of |
| 684 | // template-parameter that is not a template parameter pack. |
| 685 | if (Default && IsParameterPack) { |
| 686 | Diag(EqualLoc, diag::err_template_param_pack_default_arg); |
| 687 | Default = 0; |
| 688 | } |
| 689 | |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 690 | // Check the well-formedness of the default template argument, if provided. |
Douglas Gregor | 10738d3 | 2010-12-23 23:51:58 +0000 | [diff] [blame] | 691 | if (Default) { |
Douglas Gregor | 6f52675 | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 692 | // Check for unexpanded parameter packs. |
| 693 | if (DiagnoseUnexpandedParameterPack(Default, UPPC_DefaultArgument)) |
| 694 | return Param; |
| 695 | |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 696 | TemplateArgument Converted; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 697 | ExprResult DefaultRes = CheckTemplateArgument(Param, Param->getType(), Default, Converted); |
| 698 | if (DefaultRes.isInvalid()) { |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 699 | Param->setInvalidDecl(); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 700 | return Param; |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 701 | } |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 702 | Default = DefaultRes.take(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 703 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 704 | Param->setDefaultArgument(Default, false); |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 705 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 706 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 707 | return Param; |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 708 | } |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 709 | |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 710 | /// ActOnTemplateTemplateParameter - Called when a C++ template template |
| 711 | /// parameter (e.g. T in template <template <typename> class T> class array) |
| 712 | /// has been parsed. S is the current scope. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 713 | Decl *Sema::ActOnTemplateTemplateParameter(Scope* S, |
| 714 | SourceLocation TmpLoc, |
| 715 | TemplateParamsTy *Params, |
Douglas Gregor | 61c4d28 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 716 | SourceLocation EllipsisLoc, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 717 | IdentifierInfo *Name, |
| 718 | SourceLocation NameLoc, |
| 719 | unsigned Depth, |
| 720 | unsigned Position, |
| 721 | SourceLocation EqualLoc, |
Douglas Gregor | 61c4d28 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 722 | ParsedTemplateArgument Default) { |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 723 | assert(S->isTemplateParamScope() && |
| 724 | "Template template parameter not in template parameter scope!"); |
| 725 | |
| 726 | // Construct the parameter object. |
Douglas Gregor | 61c4d28 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 727 | bool IsParameterPack = EllipsisLoc.isValid(); |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 728 | TemplateTemplateParmDecl *Param = |
John McCall | 7a9813c | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 729 | TemplateTemplateParmDecl::Create(Context, Context.getTranslationUnitDecl(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 730 | NameLoc.isInvalid()? TmpLoc : NameLoc, |
| 731 | Depth, Position, IsParameterPack, |
Douglas Gregor | 61c4d28 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 732 | Name, Params); |
Douglas Gregor | 9a299e0 | 2011-03-04 17:52:15 +0000 | [diff] [blame] | 733 | Param->setAccess(AS_public); |
| 734 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 735 | // If the template template parameter has a name, then link the identifier |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 736 | // into the scope and lookup mechanisms. |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 737 | if (Name) { |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 738 | S->AddDecl(Param); |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 739 | IdResolver.AddDecl(Param); |
| 740 | } |
| 741 | |
Douglas Gregor | 6f52675 | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 742 | if (Params->size() == 0) { |
| 743 | Diag(Param->getLocation(), diag::err_template_template_parm_no_parms) |
| 744 | << SourceRange(Params->getLAngleLoc(), Params->getRAngleLoc()); |
| 745 | Param->setInvalidDecl(); |
| 746 | } |
| 747 | |
Douglas Gregor | 61c4d28 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 748 | // C++0x [temp.param]p9: |
| 749 | // A default template-argument may be specified for any kind of |
| 750 | // template-parameter that is not a template parameter pack. |
| 751 | if (IsParameterPack && !Default.isInvalid()) { |
| 752 | Diag(EqualLoc, diag::err_template_param_pack_default_arg); |
| 753 | Default = ParsedTemplateArgument(); |
| 754 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 755 | |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 756 | if (!Default.isInvalid()) { |
| 757 | // Check only that we have a template template argument. We don't want to |
| 758 | // try to check well-formedness now, because our template template parameter |
| 759 | // might have dependent types in its template parameters, which we wouldn't |
| 760 | // be able to match now. |
| 761 | // |
| 762 | // If none of the template template parameter's template arguments mention |
| 763 | // other template parameters, we could actually perform more checking here. |
| 764 | // However, it isn't worth doing. |
| 765 | TemplateArgumentLoc DefaultArg = translateTemplateArgument(*this, Default); |
| 766 | if (DefaultArg.getArgument().getAsTemplate().isNull()) { |
| 767 | Diag(DefaultArg.getLocation(), diag::err_template_arg_not_class_template) |
| 768 | << DefaultArg.getSourceRange(); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 769 | return Param; |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 770 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 771 | |
Douglas Gregor | 6f52675 | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 772 | // Check for unexpanded parameter packs. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 773 | if (DiagnoseUnexpandedParameterPack(DefaultArg.getLocation(), |
Douglas Gregor | 6f52675 | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 774 | DefaultArg.getArgument().getAsTemplate(), |
| 775 | UPPC_DefaultArgument)) |
| 776 | return Param; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 777 | |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 778 | Param->setDefaultArgument(DefaultArg, false); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 779 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 780 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 781 | return Param; |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 782 | } |
| 783 | |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 784 | /// ActOnTemplateParameterList - Builds a TemplateParameterList that |
| 785 | /// contains the template parameters in Params/NumParams. |
| 786 | Sema::TemplateParamsTy * |
| 787 | Sema::ActOnTemplateParameterList(unsigned Depth, |
| 788 | SourceLocation ExportLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 789 | SourceLocation TemplateLoc, |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 790 | SourceLocation LAngleLoc, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 791 | Decl **Params, unsigned NumParams, |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 792 | SourceLocation RAngleLoc) { |
| 793 | if (ExportLoc.isValid()) |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 794 | Diag(ExportLoc, diag::warn_template_export_unsupported); |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 795 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 796 | return TemplateParameterList::Create(Context, TemplateLoc, LAngleLoc, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 797 | (NamedDecl**)Params, NumParams, |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 798 | RAngleLoc); |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 799 | } |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 800 | |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 801 | static void SetNestedNameSpecifier(TagDecl *T, const CXXScopeSpec &SS) { |
| 802 | if (SS.isSet()) |
Douglas Gregor | c22b5ff | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 803 | T->setQualifierInfo(SS.getWithLocInContext(T->getASTContext())); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 804 | } |
| 805 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 806 | DeclResult |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 807 | Sema::CheckClassTemplate(Scope *S, unsigned TagSpec, TagUseKind TUK, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 808 | SourceLocation KWLoc, CXXScopeSpec &SS, |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 809 | IdentifierInfo *Name, SourceLocation NameLoc, |
| 810 | AttributeList *Attr, |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 811 | TemplateParameterList *TemplateParams, |
Abramo Bagnara | c57c17d | 2011-03-10 13:28:31 +0000 | [diff] [blame] | 812 | AccessSpecifier AS, |
| 813 | unsigned NumOuterTemplateParamLists, |
| 814 | TemplateParameterList** OuterTemplateParamLists) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 815 | assert(TemplateParams && TemplateParams->size() > 0 && |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 816 | "No template parameters"); |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 817 | assert(TUK != TUK_Reference && "Can only declare or define class templates"); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 818 | bool Invalid = false; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 819 | |
| 820 | // Check that we can declare a template here. |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 821 | if (CheckTemplateDeclScope(S, TemplateParams)) |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 822 | return true; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 823 | |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 824 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
| 825 | assert(Kind != TTK_Enum && "can't build template of enumerated type"); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 826 | |
| 827 | // There is no such thing as an unnamed class template. |
| 828 | if (!Name) { |
| 829 | Diag(KWLoc, diag::err_template_unnamed_class); |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 830 | return true; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 831 | } |
| 832 | |
| 833 | // Find any previous declaration with this name. |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 834 | DeclContext *SemanticContext; |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 835 | LookupResult Previous(*this, Name, NameLoc, LookupOrdinaryName, |
John McCall | 7d384dd | 2009-11-18 07:57:50 +0000 | [diff] [blame] | 836 | ForRedeclaration); |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 837 | if (SS.isNotEmpty() && !SS.isInvalid()) { |
| 838 | SemanticContext = computeDeclContext(SS, true); |
| 839 | if (!SemanticContext) { |
| 840 | // FIXME: Produce a reasonable diagnostic here |
| 841 | return true; |
| 842 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 843 | |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 844 | if (RequireCompleteDeclContext(SS, SemanticContext)) |
| 845 | return true; |
| 846 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 847 | LookupQualifiedName(Previous, SemanticContext); |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 848 | } else { |
| 849 | SemanticContext = CurContext; |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 850 | LookupName(Previous, S); |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 851 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 852 | |
Douglas Gregor | 57265e3 | 2010-04-12 16:00:01 +0000 | [diff] [blame] | 853 | if (Previous.isAmbiguous()) |
| 854 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 855 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 856 | NamedDecl *PrevDecl = 0; |
| 857 | if (Previous.begin() != Previous.end()) |
Douglas Gregor | 57265e3 | 2010-04-12 16:00:01 +0000 | [diff] [blame] | 858 | PrevDecl = (*Previous.begin())->getUnderlyingDecl(); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 859 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 860 | // If there is a previous declaration with the same name, check |
| 861 | // whether this is a valid redeclaration. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 862 | ClassTemplateDecl *PrevClassTemplate |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 863 | = dyn_cast_or_null<ClassTemplateDecl>(PrevDecl); |
Douglas Gregor | d7e5bdb | 2009-10-09 21:11:42 +0000 | [diff] [blame] | 864 | |
| 865 | // We may have found the injected-class-name of a class template, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 866 | // class template partial specialization, or class template specialization. |
Douglas Gregor | d7e5bdb | 2009-10-09 21:11:42 +0000 | [diff] [blame] | 867 | // In these cases, grab the template that is being defined or specialized. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 868 | if (!PrevClassTemplate && PrevDecl && isa<CXXRecordDecl>(PrevDecl) && |
Douglas Gregor | d7e5bdb | 2009-10-09 21:11:42 +0000 | [diff] [blame] | 869 | cast<CXXRecordDecl>(PrevDecl)->isInjectedClassName()) { |
| 870 | PrevDecl = cast<CXXRecordDecl>(PrevDecl->getDeclContext()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 871 | PrevClassTemplate |
Douglas Gregor | d7e5bdb | 2009-10-09 21:11:42 +0000 | [diff] [blame] | 872 | = cast<CXXRecordDecl>(PrevDecl)->getDescribedClassTemplate(); |
| 873 | if (!PrevClassTemplate && isa<ClassTemplateSpecializationDecl>(PrevDecl)) { |
| 874 | PrevClassTemplate |
| 875 | = cast<ClassTemplateSpecializationDecl>(PrevDecl) |
| 876 | ->getSpecializedTemplate(); |
| 877 | } |
| 878 | } |
| 879 | |
John McCall | 65c4946 | 2009-12-18 11:25:59 +0000 | [diff] [blame] | 880 | if (TUK == TUK_Friend) { |
John McCall | e129d44 | 2009-12-17 23:21:11 +0000 | [diff] [blame] | 881 | // C++ [namespace.memdef]p3: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 882 | // [...] When looking for a prior declaration of a class or a function |
| 883 | // declared as a friend, and when the name of the friend class or |
John McCall | e129d44 | 2009-12-17 23:21:11 +0000 | [diff] [blame] | 884 | // function is neither a qualified name nor a template-id, scopes outside |
| 885 | // the innermost enclosing namespace scope are not considered. |
Douglas Gregor | c1c9df7 | 2010-04-18 17:37:40 +0000 | [diff] [blame] | 886 | if (!SS.isSet()) { |
| 887 | DeclContext *OutermostContext = CurContext; |
| 888 | while (!OutermostContext->isFileContext()) |
| 889 | OutermostContext = OutermostContext->getLookupParent(); |
John McCall | 65c4946 | 2009-12-18 11:25:59 +0000 | [diff] [blame] | 890 | |
Douglas Gregor | c1c9df7 | 2010-04-18 17:37:40 +0000 | [diff] [blame] | 891 | if (PrevDecl && |
| 892 | (OutermostContext->Equals(PrevDecl->getDeclContext()) || |
| 893 | OutermostContext->Encloses(PrevDecl->getDeclContext()))) { |
| 894 | SemanticContext = PrevDecl->getDeclContext(); |
| 895 | } else { |
| 896 | // Declarations in outer scopes don't matter. However, the outermost |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 897 | // context we computed is the semantic context for our new |
Douglas Gregor | c1c9df7 | 2010-04-18 17:37:40 +0000 | [diff] [blame] | 898 | // declaration. |
| 899 | PrevDecl = PrevClassTemplate = 0; |
| 900 | SemanticContext = OutermostContext; |
| 901 | } |
John McCall | e129d44 | 2009-12-17 23:21:11 +0000 | [diff] [blame] | 902 | } |
Douglas Gregor | c1c9df7 | 2010-04-18 17:37:40 +0000 | [diff] [blame] | 903 | |
John McCall | e129d44 | 2009-12-17 23:21:11 +0000 | [diff] [blame] | 904 | if (CurContext->isDependentContext()) { |
| 905 | // If this is a dependent context, we don't want to link the friend |
| 906 | // class template to the template in scope, because that would perform |
| 907 | // checking of the template parameter lists that can't be performed |
| 908 | // until the outer context is instantiated. |
| 909 | PrevDecl = PrevClassTemplate = 0; |
| 910 | } |
| 911 | } else if (PrevDecl && !isDeclInScope(PrevDecl, SemanticContext, S)) |
| 912 | PrevDecl = PrevClassTemplate = 0; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 913 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 914 | if (PrevClassTemplate) { |
| 915 | // Ensure that the template parameter lists are compatible. |
| 916 | if (!TemplateParameterListsAreEqual(TemplateParams, |
| 917 | PrevClassTemplate->getTemplateParameters(), |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 918 | /*Complain=*/true, |
| 919 | TPL_TemplateMatch)) |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 920 | return true; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 921 | |
| 922 | // C++ [temp.class]p4: |
| 923 | // In a redeclaration, partial specialization, explicit |
| 924 | // specialization or explicit instantiation of a class template, |
| 925 | // the class-key shall agree in kind with the original class |
| 926 | // template declaration (7.1.5.3). |
| 927 | RecordDecl *PrevRecordDecl = PrevClassTemplate->getTemplatedDecl(); |
Richard Trieu | bbf34c0 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 928 | if (!isAcceptableTagRedeclaration(PrevRecordDecl, Kind, |
| 929 | TUK == TUK_Definition, KWLoc, *Name)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 930 | Diag(KWLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 931 | << Name |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 932 | << FixItHint::CreateReplacement(KWLoc, PrevRecordDecl->getKindName()); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 933 | Diag(PrevRecordDecl->getLocation(), diag::note_previous_use); |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 934 | Kind = PrevRecordDecl->getTagKind(); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 935 | } |
| 936 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 937 | // Check for redefinition of this class template. |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 938 | if (TUK == TUK_Definition) { |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 939 | if (TagDecl *Def = PrevRecordDecl->getDefinition()) { |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 940 | Diag(NameLoc, diag::err_redefinition) << Name; |
| 941 | Diag(Def->getLocation(), diag::note_previous_definition); |
| 942 | // FIXME: Would it make sense to try to "forget" the previous |
| 943 | // definition, as part of error recovery? |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 944 | return true; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 945 | } |
| 946 | } |
| 947 | } else if (PrevDecl && PrevDecl->isTemplateParameter()) { |
| 948 | // Maybe we will complain about the shadowed template parameter. |
| 949 | DiagnoseTemplateParameterShadow(NameLoc, PrevDecl); |
| 950 | // Just pretend that we didn't see the previous declaration. |
| 951 | PrevDecl = 0; |
| 952 | } else if (PrevDecl) { |
| 953 | // C++ [temp]p5: |
| 954 | // A class template shall not have the same name as any other |
| 955 | // template, class, function, object, enumeration, enumerator, |
| 956 | // namespace, or type in the same scope (3.3), except as specified |
| 957 | // in (14.5.4). |
| 958 | Diag(NameLoc, diag::err_redefinition_different_kind) << Name; |
| 959 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 960 | return true; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 961 | } |
| 962 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 963 | // Check the template parameter list of this declaration, possibly |
| 964 | // merging in the template parameter list from the previous class |
| 965 | // template declaration. |
| 966 | if (CheckTemplateParameterList(TemplateParams, |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 967 | PrevClassTemplate? PrevClassTemplate->getTemplateParameters() : 0, |
Douglas Gregor | d89d86f | 2011-02-04 04:20:44 +0000 | [diff] [blame] | 968 | (SS.isSet() && SemanticContext && |
Douglas Gregor | 461bf2e | 2011-02-04 12:22:53 +0000 | [diff] [blame] | 969 | SemanticContext->isRecord() && |
| 970 | SemanticContext->isDependentContext()) |
Douglas Gregor | d89d86f | 2011-02-04 04:20:44 +0000 | [diff] [blame] | 971 | ? TPC_ClassTemplateMember |
| 972 | : TPC_ClassTemplate)) |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 973 | Invalid = true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 974 | |
Douglas Gregor | 57265e3 | 2010-04-12 16:00:01 +0000 | [diff] [blame] | 975 | if (SS.isSet()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 976 | // If the name of the template was qualified, we must be defining the |
Douglas Gregor | 57265e3 | 2010-04-12 16:00:01 +0000 | [diff] [blame] | 977 | // template out-of-line. |
| 978 | if (!SS.isInvalid() && !Invalid && !PrevClassTemplate && |
| 979 | !(TUK == TUK_Friend && CurContext->isDependentContext())) |
| 980 | Diag(NameLoc, diag::err_member_def_does_not_match) |
| 981 | << Name << SemanticContext << SS.getRange(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 982 | } |
| 983 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 984 | CXXRecordDecl *NewClass = |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 985 | CXXRecordDecl::Create(Context, Kind, SemanticContext, KWLoc, NameLoc, Name, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 986 | PrevClassTemplate? |
Douglas Gregor | aafc0cc | 2009-05-15 19:11:46 +0000 | [diff] [blame] | 987 | PrevClassTemplate->getTemplatedDecl() : 0, |
| 988 | /*DelayTypeCreation=*/true); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 989 | SetNestedNameSpecifier(NewClass, SS); |
Abramo Bagnara | c57c17d | 2011-03-10 13:28:31 +0000 | [diff] [blame] | 990 | if (NumOuterTemplateParamLists > 0) |
| 991 | NewClass->setTemplateParameterListsInfo(Context, |
| 992 | NumOuterTemplateParamLists, |
| 993 | OuterTemplateParamLists); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 994 | |
| 995 | ClassTemplateDecl *NewTemplate |
| 996 | = ClassTemplateDecl::Create(Context, SemanticContext, NameLoc, |
| 997 | DeclarationName(Name), TemplateParams, |
Douglas Gregor | 5953d8b | 2009-03-19 17:26:29 +0000 | [diff] [blame] | 998 | NewClass, PrevClassTemplate); |
Douglas Gregor | befc20e | 2009-03-26 00:10:35 +0000 | [diff] [blame] | 999 | NewClass->setDescribedClassTemplate(NewTemplate); |
| 1000 | |
Douglas Gregor | aafc0cc | 2009-05-15 19:11:46 +0000 | [diff] [blame] | 1001 | // Build the type for the class template declaration now. |
Douglas Gregor | 24bae92 | 2010-07-08 18:37:38 +0000 | [diff] [blame] | 1002 | QualType T = NewTemplate->getInjectedClassNameSpecialization(); |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 1003 | T = Context.getInjectedClassNameType(NewClass, T); |
Douglas Gregor | aafc0cc | 2009-05-15 19:11:46 +0000 | [diff] [blame] | 1004 | assert(T->isDependentType() && "Class template type is not dependent?"); |
| 1005 | (void)T; |
| 1006 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1007 | // If we are providing an explicit specialization of a member that is a |
Douglas Gregor | fd056bc | 2009-10-13 16:30:37 +0000 | [diff] [blame] | 1008 | // class template, make a note of that. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1009 | if (PrevClassTemplate && |
Douglas Gregor | fd056bc | 2009-10-13 16:30:37 +0000 | [diff] [blame] | 1010 | PrevClassTemplate->getInstantiatedFromMemberTemplate()) |
| 1011 | PrevClassTemplate->setMemberSpecialization(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1012 | |
Anders Carlsson | 4cbe82c | 2009-03-26 01:24:28 +0000 | [diff] [blame] | 1013 | // Set the access specifier. |
Douglas Gregor | d85bea2 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 1014 | if (!Invalid && TUK != TUK_Friend) |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1015 | SetMemberAccessSpecifier(NewTemplate, PrevClassTemplate, AS); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1016 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1017 | // Set the lexical context of these templates |
| 1018 | NewClass->setLexicalDeclContext(CurContext); |
| 1019 | NewTemplate->setLexicalDeclContext(CurContext); |
| 1020 | |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 1021 | if (TUK == TUK_Definition) |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1022 | NewClass->startDefinition(); |
| 1023 | |
| 1024 | if (Attr) |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 1025 | ProcessDeclAttributeList(S, NewClass, Attr); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1026 | |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1027 | if (TUK != TUK_Friend) |
| 1028 | PushOnScopeChains(NewTemplate, S); |
| 1029 | else { |
Douglas Gregor | d85bea2 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 1030 | if (PrevClassTemplate && PrevClassTemplate->getAccess() != AS_none) { |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1031 | NewTemplate->setAccess(PrevClassTemplate->getAccess()); |
Douglas Gregor | d85bea2 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 1032 | NewClass->setAccess(PrevClassTemplate->getAccess()); |
| 1033 | } |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1034 | |
Douglas Gregor | d85bea2 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 1035 | NewTemplate->setObjectOfFriendDecl(/* PreviouslyDeclared = */ |
| 1036 | PrevClassTemplate != NULL); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1037 | |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1038 | // Friend templates are visible in fairly strange ways. |
| 1039 | if (!CurContext->isDependentContext()) { |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 1040 | DeclContext *DC = SemanticContext->getRedeclContext(); |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1041 | DC->makeDeclVisibleInContext(NewTemplate, /* Recoverable = */ false); |
| 1042 | if (Scope *EnclosingScope = getScopeForDeclContext(S, DC)) |
| 1043 | PushOnScopeChains(NewTemplate, EnclosingScope, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1044 | /* AddToContext = */ false); |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1045 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1046 | |
Douglas Gregor | d85bea2 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 1047 | FriendDecl *Friend = FriendDecl::Create(Context, CurContext, |
| 1048 | NewClass->getLocation(), |
| 1049 | NewTemplate, |
| 1050 | /*FIXME:*/NewClass->getLocation()); |
| 1051 | Friend->setAccess(AS_public); |
| 1052 | CurContext->addDecl(Friend); |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1053 | } |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1054 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1055 | if (Invalid) { |
| 1056 | NewTemplate->setInvalidDecl(); |
| 1057 | NewClass->setInvalidDecl(); |
| 1058 | } |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1059 | return NewTemplate; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1060 | } |
| 1061 | |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1062 | /// \brief Diagnose the presence of a default template argument on a |
| 1063 | /// template parameter, which is ill-formed in certain contexts. |
| 1064 | /// |
| 1065 | /// \returns true if the default template argument should be dropped. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1066 | static bool DiagnoseDefaultTemplateArgument(Sema &S, |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1067 | Sema::TemplateParamListContext TPC, |
| 1068 | SourceLocation ParamLoc, |
| 1069 | SourceRange DefArgRange) { |
| 1070 | switch (TPC) { |
| 1071 | case Sema::TPC_ClassTemplate: |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 1072 | case Sema::TPC_TypeAliasTemplate: |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1073 | return false; |
| 1074 | |
| 1075 | case Sema::TPC_FunctionTemplate: |
Douglas Gregor | d89d86f | 2011-02-04 04:20:44 +0000 | [diff] [blame] | 1076 | case Sema::TPC_FriendFunctionTemplateDefinition: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1077 | // C++ [temp.param]p9: |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1078 | // A default template-argument shall not be specified in a |
| 1079 | // function template declaration or a function template |
| 1080 | // definition [...] |
Douglas Gregor | d89d86f | 2011-02-04 04:20:44 +0000 | [diff] [blame] | 1081 | // If a friend function template declaration specifies a default |
| 1082 | // template-argument, that declaration shall be a definition and shall be |
| 1083 | // the only declaration of the function template in the translation unit. |
| 1084 | // (C++98/03 doesn't have this wording; see DR226). |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1085 | if (!S.getLangOptions().CPlusPlus0x) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1086 | S.Diag(ParamLoc, |
Douglas Gregor | ee5d21f | 2011-02-04 03:57:22 +0000 | [diff] [blame] | 1087 | diag::ext_template_parameter_default_in_function_template) |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1088 | << DefArgRange; |
| 1089 | return false; |
| 1090 | |
| 1091 | case Sema::TPC_ClassTemplateMember: |
| 1092 | // C++0x [temp.param]p9: |
| 1093 | // A default template-argument shall not be specified in the |
| 1094 | // template-parameter-lists of the definition of a member of a |
| 1095 | // class template that appears outside of the member's class. |
| 1096 | S.Diag(ParamLoc, diag::err_template_parameter_default_template_member) |
| 1097 | << DefArgRange; |
| 1098 | return true; |
| 1099 | |
| 1100 | case Sema::TPC_FriendFunctionTemplate: |
| 1101 | // C++ [temp.param]p9: |
| 1102 | // A default template-argument shall not be specified in a |
| 1103 | // friend template declaration. |
| 1104 | S.Diag(ParamLoc, diag::err_template_parameter_default_friend_template) |
| 1105 | << DefArgRange; |
| 1106 | return true; |
| 1107 | |
| 1108 | // FIXME: C++0x [temp.param]p9 allows default template-arguments |
| 1109 | // for friend function templates if there is only a single |
| 1110 | // declaration (and it is a definition). Strange! |
| 1111 | } |
| 1112 | |
| 1113 | return false; |
| 1114 | } |
| 1115 | |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1116 | /// \brief Check for unexpanded parameter packs within the template parameters |
| 1117 | /// of a template template parameter, recursively. |
Benjamin Kramer | da57f3e | 2011-03-26 12:38:21 +0000 | [diff] [blame] | 1118 | static bool DiagnoseUnexpandedParameterPacks(Sema &S, |
| 1119 | TemplateTemplateParmDecl *TTP) { |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1120 | TemplateParameterList *Params = TTP->getTemplateParameters(); |
| 1121 | for (unsigned I = 0, N = Params->size(); I != N; ++I) { |
| 1122 | NamedDecl *P = Params->getParam(I); |
| 1123 | if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(P)) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1124 | if (S.DiagnoseUnexpandedParameterPack(NTTP->getLocation(), |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1125 | NTTP->getTypeSourceInfo(), |
| 1126 | Sema::UPPC_NonTypeTemplateParameterType)) |
| 1127 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1128 | |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1129 | continue; |
| 1130 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1131 | |
| 1132 | if (TemplateTemplateParmDecl *InnerTTP |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1133 | = dyn_cast<TemplateTemplateParmDecl>(P)) |
| 1134 | if (DiagnoseUnexpandedParameterPacks(S, InnerTTP)) |
| 1135 | return true; |
| 1136 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1137 | |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1138 | return false; |
| 1139 | } |
| 1140 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1141 | /// \brief Checks the validity of a template parameter list, possibly |
| 1142 | /// considering the template parameter list from a previous |
| 1143 | /// declaration. |
| 1144 | /// |
| 1145 | /// If an "old" template parameter list is provided, it must be |
| 1146 | /// equivalent (per TemplateParameterListsAreEqual) to the "new" |
| 1147 | /// template parameter list. |
| 1148 | /// |
| 1149 | /// \param NewParams Template parameter list for a new template |
| 1150 | /// declaration. This template parameter list will be updated with any |
| 1151 | /// default arguments that are carried through from the previous |
| 1152 | /// template parameter list. |
| 1153 | /// |
| 1154 | /// \param OldParams If provided, template parameter list from a |
| 1155 | /// previous declaration of the same template. Default template |
| 1156 | /// arguments will be merged from the old template parameter list to |
| 1157 | /// the new template parameter list. |
| 1158 | /// |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1159 | /// \param TPC Describes the context in which we are checking the given |
| 1160 | /// template parameter list. |
| 1161 | /// |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1162 | /// \returns true if an error occurred, false otherwise. |
| 1163 | bool Sema::CheckTemplateParameterList(TemplateParameterList *NewParams, |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1164 | TemplateParameterList *OldParams, |
| 1165 | TemplateParamListContext TPC) { |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1166 | bool Invalid = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1167 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1168 | // C++ [temp.param]p10: |
| 1169 | // The set of default template-arguments available for use with a |
| 1170 | // template declaration or definition is obtained by merging the |
| 1171 | // default arguments from the definition (if in scope) and all |
| 1172 | // declarations in scope in the same way default function |
| 1173 | // arguments are (8.3.6). |
| 1174 | bool SawDefaultArgument = false; |
| 1175 | SourceLocation PreviousDefaultArgLoc; |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 1176 | |
Anders Carlsson | 49d2557 | 2009-06-12 23:20:15 +0000 | [diff] [blame] | 1177 | bool SawParameterPack = false; |
| 1178 | SourceLocation ParameterPackLoc; |
| 1179 | |
Mike Stump | 1a35fde | 2009-02-11 23:03:27 +0000 | [diff] [blame] | 1180 | // Dummy initialization to avoid warnings. |
Douglas Gregor | 1bc6913 | 2009-02-11 20:46:19 +0000 | [diff] [blame] | 1181 | TemplateParameterList::iterator OldParam = NewParams->end(); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1182 | if (OldParams) |
| 1183 | OldParam = OldParams->begin(); |
| 1184 | |
Douglas Gregor | fd1a8fd | 2011-01-27 01:40:17 +0000 | [diff] [blame] | 1185 | bool RemoveDefaultArguments = false; |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1186 | for (TemplateParameterList::iterator NewParam = NewParams->begin(), |
| 1187 | NewParamEnd = NewParams->end(); |
| 1188 | NewParam != NewParamEnd; ++NewParam) { |
| 1189 | // Variables used to diagnose redundant default arguments |
| 1190 | bool RedundantDefaultArg = false; |
| 1191 | SourceLocation OldDefaultLoc; |
| 1192 | SourceLocation NewDefaultLoc; |
| 1193 | |
| 1194 | // Variables used to diagnose missing default arguments |
| 1195 | bool MissingDefaultArg = false; |
| 1196 | |
Anders Carlsson | 49d2557 | 2009-06-12 23:20:15 +0000 | [diff] [blame] | 1197 | // C++0x [temp.param]p11: |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 1198 | // If a template parameter of a primary class template or alias template |
| 1199 | // is a template parameter pack, it shall be the last template parameter. |
| 1200 | if (SawParameterPack && |
| 1201 | (TPC == TPC_ClassTemplate || TPC == TPC_TypeAliasTemplate)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1202 | Diag(ParameterPackLoc, |
Anders Carlsson | 49d2557 | 2009-06-12 23:20:15 +0000 | [diff] [blame] | 1203 | diag::err_template_param_pack_must_be_last_template_parameter); |
| 1204 | Invalid = true; |
| 1205 | } |
| 1206 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1207 | if (TemplateTypeParmDecl *NewTypeParm |
| 1208 | = dyn_cast<TemplateTypeParmDecl>(*NewParam)) { |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1209 | // Check the presence of a default argument here. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1210 | if (NewTypeParm->hasDefaultArgument() && |
| 1211 | DiagnoseDefaultTemplateArgument(*this, TPC, |
| 1212 | NewTypeParm->getLocation(), |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1213 | NewTypeParm->getDefaultArgumentInfo()->getTypeLoc() |
Abramo Bagnara | bd054db | 2010-05-20 10:00:11 +0000 | [diff] [blame] | 1214 | .getSourceRange())) |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1215 | NewTypeParm->removeDefaultArgument(); |
| 1216 | |
| 1217 | // Merge default arguments for template type parameters. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1218 | TemplateTypeParmDecl *OldTypeParm |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1219 | = OldParams? cast<TemplateTypeParmDecl>(*OldParam) : 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1220 | |
Anders Carlsson | 49d2557 | 2009-06-12 23:20:15 +0000 | [diff] [blame] | 1221 | if (NewTypeParm->isParameterPack()) { |
| 1222 | assert(!NewTypeParm->hasDefaultArgument() && |
| 1223 | "Parameter packs can't have a default argument!"); |
| 1224 | SawParameterPack = true; |
| 1225 | ParameterPackLoc = NewTypeParm->getLocation(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1226 | } else if (OldTypeParm && OldTypeParm->hasDefaultArgument() && |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1227 | NewTypeParm->hasDefaultArgument()) { |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1228 | OldDefaultLoc = OldTypeParm->getDefaultArgumentLoc(); |
| 1229 | NewDefaultLoc = NewTypeParm->getDefaultArgumentLoc(); |
| 1230 | SawDefaultArgument = true; |
| 1231 | RedundantDefaultArg = true; |
| 1232 | PreviousDefaultArgLoc = NewDefaultLoc; |
| 1233 | } else if (OldTypeParm && OldTypeParm->hasDefaultArgument()) { |
| 1234 | // Merge the default argument from the old declaration to the |
| 1235 | // new declaration. |
| 1236 | SawDefaultArgument = true; |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1237 | NewTypeParm->setDefaultArgument(OldTypeParm->getDefaultArgumentInfo(), |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1238 | true); |
| 1239 | PreviousDefaultArgLoc = OldTypeParm->getDefaultArgumentLoc(); |
| 1240 | } else if (NewTypeParm->hasDefaultArgument()) { |
| 1241 | SawDefaultArgument = true; |
| 1242 | PreviousDefaultArgLoc = NewTypeParm->getDefaultArgumentLoc(); |
| 1243 | } else if (SawDefaultArgument) |
| 1244 | MissingDefaultArg = true; |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1245 | } else if (NonTypeTemplateParmDecl *NewNonTypeParm |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1246 | = dyn_cast<NonTypeTemplateParmDecl>(*NewParam)) { |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1247 | // Check for unexpanded parameter packs. |
| 1248 | if (DiagnoseUnexpandedParameterPack(NewNonTypeParm->getLocation(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1249 | NewNonTypeParm->getTypeSourceInfo(), |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1250 | UPPC_NonTypeTemplateParameterType)) { |
| 1251 | Invalid = true; |
| 1252 | continue; |
| 1253 | } |
| 1254 | |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1255 | // Check the presence of a default argument here. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1256 | if (NewNonTypeParm->hasDefaultArgument() && |
| 1257 | DiagnoseDefaultTemplateArgument(*this, TPC, |
| 1258 | NewNonTypeParm->getLocation(), |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1259 | NewNonTypeParm->getDefaultArgument()->getSourceRange())) { |
Abramo Bagnara | d92f7a2 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 1260 | NewNonTypeParm->removeDefaultArgument(); |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1261 | } |
| 1262 | |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1263 | // Merge default arguments for non-type template parameters |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1264 | NonTypeTemplateParmDecl *OldNonTypeParm |
| 1265 | = OldParams? cast<NonTypeTemplateParmDecl>(*OldParam) : 0; |
Douglas Gregor | 1ed6476 | 2011-01-05 16:19:19 +0000 | [diff] [blame] | 1266 | if (NewNonTypeParm->isParameterPack()) { |
| 1267 | assert(!NewNonTypeParm->hasDefaultArgument() && |
| 1268 | "Parameter packs can't have a default argument!"); |
| 1269 | SawParameterPack = true; |
| 1270 | ParameterPackLoc = NewNonTypeParm->getLocation(); |
| 1271 | } else if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument() && |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1272 | NewNonTypeParm->hasDefaultArgument()) { |
| 1273 | OldDefaultLoc = OldNonTypeParm->getDefaultArgumentLoc(); |
| 1274 | NewDefaultLoc = NewNonTypeParm->getDefaultArgumentLoc(); |
| 1275 | SawDefaultArgument = true; |
| 1276 | RedundantDefaultArg = true; |
| 1277 | PreviousDefaultArgLoc = NewDefaultLoc; |
| 1278 | } else if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument()) { |
| 1279 | // Merge the default argument from the old declaration to the |
| 1280 | // new declaration. |
| 1281 | SawDefaultArgument = true; |
| 1282 | // FIXME: We need to create a new kind of "default argument" |
Douglas Gregor | 61c4d28 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 1283 | // expression that points to a previous non-type template |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1284 | // parameter. |
| 1285 | NewNonTypeParm->setDefaultArgument( |
Abramo Bagnara | d92f7a2 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 1286 | OldNonTypeParm->getDefaultArgument(), |
| 1287 | /*Inherited=*/ true); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1288 | PreviousDefaultArgLoc = OldNonTypeParm->getDefaultArgumentLoc(); |
| 1289 | } else if (NewNonTypeParm->hasDefaultArgument()) { |
| 1290 | SawDefaultArgument = true; |
| 1291 | PreviousDefaultArgLoc = NewNonTypeParm->getDefaultArgumentLoc(); |
| 1292 | } else if (SawDefaultArgument) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1293 | MissingDefaultArg = true; |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1294 | } else { |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1295 | // Check the presence of a default argument here. |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1296 | TemplateTemplateParmDecl *NewTemplateParm |
| 1297 | = cast<TemplateTemplateParmDecl>(*NewParam); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1298 | |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1299 | // Check for unexpanded parameter packs, recursively. |
| 1300 | if (DiagnoseUnexpandedParameterPacks(*this, NewTemplateParm)) { |
| 1301 | Invalid = true; |
| 1302 | continue; |
| 1303 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1304 | |
| 1305 | if (NewTemplateParm->hasDefaultArgument() && |
| 1306 | DiagnoseDefaultTemplateArgument(*this, TPC, |
| 1307 | NewTemplateParm->getLocation(), |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1308 | NewTemplateParm->getDefaultArgument().getSourceRange())) |
Abramo Bagnara | d92f7a2 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 1309 | NewTemplateParm->removeDefaultArgument(); |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1310 | |
| 1311 | // Merge default arguments for template template parameters |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1312 | TemplateTemplateParmDecl *OldTemplateParm |
| 1313 | = OldParams? cast<TemplateTemplateParmDecl>(*OldParam) : 0; |
Douglas Gregor | 1ed6476 | 2011-01-05 16:19:19 +0000 | [diff] [blame] | 1314 | if (NewTemplateParm->isParameterPack()) { |
| 1315 | assert(!NewTemplateParm->hasDefaultArgument() && |
| 1316 | "Parameter packs can't have a default argument!"); |
| 1317 | SawParameterPack = true; |
| 1318 | ParameterPackLoc = NewTemplateParm->getLocation(); |
| 1319 | } else if (OldTemplateParm && OldTemplateParm->hasDefaultArgument() && |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1320 | NewTemplateParm->hasDefaultArgument()) { |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1321 | OldDefaultLoc = OldTemplateParm->getDefaultArgument().getLocation(); |
| 1322 | NewDefaultLoc = NewTemplateParm->getDefaultArgument().getLocation(); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1323 | SawDefaultArgument = true; |
| 1324 | RedundantDefaultArg = true; |
| 1325 | PreviousDefaultArgLoc = NewDefaultLoc; |
| 1326 | } else if (OldTemplateParm && OldTemplateParm->hasDefaultArgument()) { |
| 1327 | // Merge the default argument from the old declaration to the |
| 1328 | // new declaration. |
| 1329 | SawDefaultArgument = true; |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 1330 | // FIXME: We need to create a new kind of "default argument" expression |
| 1331 | // that points to a previous template template parameter. |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1332 | NewTemplateParm->setDefaultArgument( |
Abramo Bagnara | d92f7a2 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 1333 | OldTemplateParm->getDefaultArgument(), |
| 1334 | /*Inherited=*/ true); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1335 | PreviousDefaultArgLoc |
| 1336 | = OldTemplateParm->getDefaultArgument().getLocation(); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1337 | } else if (NewTemplateParm->hasDefaultArgument()) { |
| 1338 | SawDefaultArgument = true; |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1339 | PreviousDefaultArgLoc |
| 1340 | = NewTemplateParm->getDefaultArgument().getLocation(); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1341 | } else if (SawDefaultArgument) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1342 | MissingDefaultArg = true; |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1343 | } |
| 1344 | |
| 1345 | if (RedundantDefaultArg) { |
| 1346 | // C++ [temp.param]p12: |
| 1347 | // A template-parameter shall not be given default arguments |
| 1348 | // by two different declarations in the same scope. |
| 1349 | Diag(NewDefaultLoc, diag::err_template_param_default_arg_redefinition); |
| 1350 | Diag(OldDefaultLoc, diag::note_template_param_prev_default_arg); |
| 1351 | Invalid = true; |
Douglas Gregor | ee5d21f | 2011-02-04 03:57:22 +0000 | [diff] [blame] | 1352 | } else if (MissingDefaultArg && TPC != TPC_FunctionTemplate) { |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1353 | // C++ [temp.param]p11: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1354 | // If a template-parameter of a class template has a default |
| 1355 | // template-argument, each subsequent template-parameter shall either |
Douglas Gregor | b49e415 | 2011-01-05 16:21:17 +0000 | [diff] [blame] | 1356 | // have a default template-argument supplied or be a template parameter |
| 1357 | // pack. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1358 | Diag((*NewParam)->getLocation(), |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1359 | diag::err_template_param_default_arg_missing); |
| 1360 | Diag(PreviousDefaultArgLoc, diag::note_template_param_prev_default_arg); |
| 1361 | Invalid = true; |
Douglas Gregor | fd1a8fd | 2011-01-27 01:40:17 +0000 | [diff] [blame] | 1362 | RemoveDefaultArguments = true; |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1363 | } |
| 1364 | |
| 1365 | // If we have an old template parameter list that we're merging |
| 1366 | // in, move on to the next parameter. |
| 1367 | if (OldParams) |
| 1368 | ++OldParam; |
| 1369 | } |
| 1370 | |
Douglas Gregor | fd1a8fd | 2011-01-27 01:40:17 +0000 | [diff] [blame] | 1371 | // We were missing some default arguments at the end of the list, so remove |
| 1372 | // all of the default arguments. |
| 1373 | if (RemoveDefaultArguments) { |
| 1374 | for (TemplateParameterList::iterator NewParam = NewParams->begin(), |
| 1375 | NewParamEnd = NewParams->end(); |
| 1376 | NewParam != NewParamEnd; ++NewParam) { |
| 1377 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*NewParam)) |
| 1378 | TTP->removeDefaultArgument(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1379 | else if (NonTypeTemplateParmDecl *NTTP |
Douglas Gregor | fd1a8fd | 2011-01-27 01:40:17 +0000 | [diff] [blame] | 1380 | = dyn_cast<NonTypeTemplateParmDecl>(*NewParam)) |
| 1381 | NTTP->removeDefaultArgument(); |
| 1382 | else |
| 1383 | cast<TemplateTemplateParmDecl>(*NewParam)->removeDefaultArgument(); |
| 1384 | } |
| 1385 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1386 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1387 | return Invalid; |
| 1388 | } |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 1389 | |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1390 | namespace { |
| 1391 | |
| 1392 | /// A class which looks for a use of a certain level of template |
| 1393 | /// parameter. |
| 1394 | struct DependencyChecker : RecursiveASTVisitor<DependencyChecker> { |
| 1395 | typedef RecursiveASTVisitor<DependencyChecker> super; |
| 1396 | |
| 1397 | unsigned Depth; |
| 1398 | bool Match; |
| 1399 | |
| 1400 | DependencyChecker(TemplateParameterList *Params) : Match(false) { |
| 1401 | NamedDecl *ND = Params->getParam(0); |
| 1402 | if (TemplateTypeParmDecl *PD = dyn_cast<TemplateTypeParmDecl>(ND)) { |
| 1403 | Depth = PD->getDepth(); |
| 1404 | } else if (NonTypeTemplateParmDecl *PD = |
| 1405 | dyn_cast<NonTypeTemplateParmDecl>(ND)) { |
| 1406 | Depth = PD->getDepth(); |
| 1407 | } else { |
| 1408 | Depth = cast<TemplateTemplateParmDecl>(ND)->getDepth(); |
| 1409 | } |
| 1410 | } |
| 1411 | |
| 1412 | bool Matches(unsigned ParmDepth) { |
| 1413 | if (ParmDepth >= Depth) { |
| 1414 | Match = true; |
| 1415 | return true; |
| 1416 | } |
| 1417 | return false; |
| 1418 | } |
| 1419 | |
| 1420 | bool VisitTemplateTypeParmType(const TemplateTypeParmType *T) { |
| 1421 | return !Matches(T->getDepth()); |
| 1422 | } |
| 1423 | |
| 1424 | bool TraverseTemplateName(TemplateName N) { |
| 1425 | if (TemplateTemplateParmDecl *PD = |
| 1426 | dyn_cast_or_null<TemplateTemplateParmDecl>(N.getAsTemplateDecl())) |
| 1427 | if (Matches(PD->getDepth())) return false; |
| 1428 | return super::TraverseTemplateName(N); |
| 1429 | } |
| 1430 | |
| 1431 | bool VisitDeclRefExpr(DeclRefExpr *E) { |
| 1432 | if (NonTypeTemplateParmDecl *PD = |
| 1433 | dyn_cast<NonTypeTemplateParmDecl>(E->getDecl())) { |
| 1434 | if (PD->getDepth() == Depth) { |
| 1435 | Match = true; |
| 1436 | return false; |
| 1437 | } |
| 1438 | } |
| 1439 | return super::VisitDeclRefExpr(E); |
| 1440 | } |
Douglas Gregor | 18c8339 | 2011-05-13 00:34:01 +0000 | [diff] [blame] | 1441 | |
| 1442 | bool TraverseInjectedClassNameType(const InjectedClassNameType *T) { |
| 1443 | return TraverseType(T->getInjectedSpecializationType()); |
| 1444 | } |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1445 | }; |
| 1446 | } |
| 1447 | |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1448 | /// Determines whether a given type depends on the given parameter |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1449 | /// list. |
| 1450 | static bool |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1451 | DependsOnTemplateParameters(QualType T, TemplateParameterList *Params) { |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1452 | DependencyChecker Checker(Params); |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1453 | Checker.TraverseType(T); |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1454 | return Checker.Match; |
| 1455 | } |
| 1456 | |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1457 | // Find the source range corresponding to the named type in the given |
| 1458 | // nested-name-specifier, if any. |
| 1459 | static SourceRange getRangeOfTypeInNestedNameSpecifier(ASTContext &Context, |
| 1460 | QualType T, |
| 1461 | const CXXScopeSpec &SS) { |
| 1462 | NestedNameSpecifierLoc NNSLoc(SS.getScopeRep(), SS.location_data()); |
| 1463 | while (NestedNameSpecifier *NNS = NNSLoc.getNestedNameSpecifier()) { |
| 1464 | if (const Type *CurType = NNS->getAsType()) { |
| 1465 | if (Context.hasSameUnqualifiedType(T, QualType(CurType, 0))) |
| 1466 | return NNSLoc.getTypeLoc().getSourceRange(); |
| 1467 | } else |
| 1468 | break; |
| 1469 | |
| 1470 | NNSLoc = NNSLoc.getPrefix(); |
| 1471 | } |
| 1472 | |
| 1473 | return SourceRange(); |
| 1474 | } |
| 1475 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1476 | /// \brief Match the given template parameter lists to the given scope |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1477 | /// specifier, returning the template parameter list that applies to the |
| 1478 | /// name. |
| 1479 | /// |
| 1480 | /// \param DeclStartLoc the start of the declaration that has a scope |
| 1481 | /// specifier or a template parameter list. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1482 | /// |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1483 | /// \param DeclLoc The location of the declaration itself. |
| 1484 | /// |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1485 | /// \param SS the scope specifier that will be matched to the given template |
| 1486 | /// parameter lists. This scope specifier precedes a qualified name that is |
| 1487 | /// being declared. |
| 1488 | /// |
| 1489 | /// \param ParamLists the template parameter lists, from the outermost to the |
| 1490 | /// innermost template parameter lists. |
| 1491 | /// |
| 1492 | /// \param NumParamLists the number of template parameter lists in ParamLists. |
| 1493 | /// |
John McCall | 77e8b11 | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 1494 | /// \param IsFriend Whether to apply the slightly different rules for |
| 1495 | /// matching template parameters to scope specifiers in friend |
| 1496 | /// declarations. |
| 1497 | /// |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 1498 | /// \param IsExplicitSpecialization will be set true if the entity being |
| 1499 | /// declared is an explicit specialization, false otherwise. |
| 1500 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1501 | /// \returns the template parameter list, if any, that corresponds to the |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1502 | /// name that is preceded by the scope specifier @p SS. This template |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 1503 | /// parameter list may have template parameters (if we're declaring a |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1504 | /// template) or may have no template parameters (if we're declaring a |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 1505 | /// template specialization), or may be NULL (if what we're declaring isn't |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1506 | /// itself a template). |
| 1507 | TemplateParameterList * |
| 1508 | Sema::MatchTemplateParametersToScopeSpecifier(SourceLocation DeclStartLoc, |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1509 | SourceLocation DeclLoc, |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1510 | const CXXScopeSpec &SS, |
| 1511 | TemplateParameterList **ParamLists, |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 1512 | unsigned NumParamLists, |
John McCall | 77e8b11 | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 1513 | bool IsFriend, |
Douglas Gregor | 0167f3c | 2010-07-14 23:14:12 +0000 | [diff] [blame] | 1514 | bool &IsExplicitSpecialization, |
| 1515 | bool &Invalid) { |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 1516 | IsExplicitSpecialization = false; |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1517 | Invalid = false; |
| 1518 | |
| 1519 | // The sequence of nested types to which we will match up the template |
| 1520 | // parameter lists. We first build this list by starting with the type named |
| 1521 | // by the nested-name-specifier and walking out until we run out of types. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1522 | SmallVector<QualType, 4> NestedTypes; |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1523 | QualType T; |
Douglas Gregor | 714c992 | 2011-05-15 17:27:27 +0000 | [diff] [blame] | 1524 | if (SS.getScopeRep()) { |
| 1525 | if (CXXRecordDecl *Record |
| 1526 | = dyn_cast_or_null<CXXRecordDecl>(computeDeclContext(SS, true))) |
| 1527 | T = Context.getTypeDeclType(Record); |
| 1528 | else |
| 1529 | T = QualType(SS.getScopeRep()->getAsType(), 0); |
| 1530 | } |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1531 | |
| 1532 | // If we found an explicit specialization that prevents us from needing |
| 1533 | // 'template<>' headers, this will be set to the location of that |
| 1534 | // explicit specialization. |
| 1535 | SourceLocation ExplicitSpecLoc; |
| 1536 | |
| 1537 | while (!T.isNull()) { |
| 1538 | NestedTypes.push_back(T); |
| 1539 | |
| 1540 | // Retrieve the parent of a record type. |
| 1541 | if (CXXRecordDecl *Record = T->getAsCXXRecordDecl()) { |
| 1542 | // If this type is an explicit specialization, we're done. |
| 1543 | if (ClassTemplateSpecializationDecl *Spec |
| 1544 | = dyn_cast<ClassTemplateSpecializationDecl>(Record)) { |
| 1545 | if (!isa<ClassTemplatePartialSpecializationDecl>(Spec) && |
| 1546 | Spec->getSpecializationKind() == TSK_ExplicitSpecialization) { |
| 1547 | ExplicitSpecLoc = Spec->getLocation(); |
| 1548 | break; |
Douglas Gregor | 3ebd753 | 2009-11-23 12:11:45 +0000 | [diff] [blame] | 1549 | } |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1550 | } else if (Record->getTemplateSpecializationKind() |
| 1551 | == TSK_ExplicitSpecialization) { |
| 1552 | ExplicitSpecLoc = Record->getLocation(); |
John McCall | 77e8b11 | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 1553 | break; |
| 1554 | } |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1555 | |
| 1556 | if (TypeDecl *Parent = dyn_cast<TypeDecl>(Record->getParent())) |
| 1557 | T = Context.getTypeDeclType(Parent); |
| 1558 | else |
| 1559 | T = QualType(); |
| 1560 | continue; |
| 1561 | } |
| 1562 | |
| 1563 | if (const TemplateSpecializationType *TST |
| 1564 | = T->getAs<TemplateSpecializationType>()) { |
| 1565 | if (TemplateDecl *Template = TST->getTemplateName().getAsTemplateDecl()) { |
| 1566 | if (TypeDecl *Parent = dyn_cast<TypeDecl>(Template->getDeclContext())) |
| 1567 | T = Context.getTypeDeclType(Parent); |
| 1568 | else |
| 1569 | T = QualType(); |
| 1570 | continue; |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1571 | } |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1572 | } |
| 1573 | |
| 1574 | // Look one step prior in a dependent template specialization type. |
| 1575 | if (const DependentTemplateSpecializationType *DependentTST |
| 1576 | = T->getAs<DependentTemplateSpecializationType>()) { |
| 1577 | if (NestedNameSpecifier *NNS = DependentTST->getQualifier()) |
| 1578 | T = QualType(NNS->getAsType(), 0); |
| 1579 | else |
| 1580 | T = QualType(); |
| 1581 | continue; |
| 1582 | } |
| 1583 | |
| 1584 | // Look one step prior in a dependent name type. |
| 1585 | if (const DependentNameType *DependentName = T->getAs<DependentNameType>()){ |
| 1586 | if (NestedNameSpecifier *NNS = DependentName->getQualifier()) |
| 1587 | T = QualType(NNS->getAsType(), 0); |
| 1588 | else |
| 1589 | T = QualType(); |
| 1590 | continue; |
| 1591 | } |
| 1592 | |
| 1593 | // Retrieve the parent of an enumeration type. |
| 1594 | if (const EnumType *EnumT = T->getAs<EnumType>()) { |
| 1595 | // FIXME: Forward-declared enums require a TSK_ExplicitSpecialization |
| 1596 | // check here. |
| 1597 | EnumDecl *Enum = EnumT->getDecl(); |
| 1598 | |
| 1599 | // Get to the parent type. |
| 1600 | if (TypeDecl *Parent = dyn_cast<TypeDecl>(Enum->getParent())) |
| 1601 | T = Context.getTypeDeclType(Parent); |
| 1602 | else |
| 1603 | T = QualType(); |
| 1604 | continue; |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1605 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1606 | |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1607 | T = QualType(); |
| 1608 | } |
| 1609 | // Reverse the nested types list, since we want to traverse from the outermost |
| 1610 | // to the innermost while checking template-parameter-lists. |
| 1611 | std::reverse(NestedTypes.begin(), NestedTypes.end()); |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 1612 | |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1613 | // C++0x [temp.expl.spec]p17: |
| 1614 | // A member or a member template may be nested within many |
| 1615 | // enclosing class templates. In an explicit specialization for |
| 1616 | // such a member, the member declaration shall be preceded by a |
| 1617 | // template<> for each enclosing class template that is |
| 1618 | // explicitly specialized. |
Douglas Gregor | 89b9f10 | 2011-06-06 15:22:55 +0000 | [diff] [blame] | 1619 | bool SawNonEmptyTemplateParameterList = false; |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1620 | unsigned ParamIdx = 0; |
| 1621 | for (unsigned TypeIdx = 0, NumTypes = NestedTypes.size(); TypeIdx != NumTypes; |
| 1622 | ++TypeIdx) { |
| 1623 | T = NestedTypes[TypeIdx]; |
| 1624 | |
| 1625 | // Whether we expect a 'template<>' header. |
| 1626 | bool NeedEmptyTemplateHeader = false; |
| 1627 | |
| 1628 | // Whether we expect a template header with parameters. |
| 1629 | bool NeedNonemptyTemplateHeader = false; |
| 1630 | |
| 1631 | // For a dependent type, the set of template parameters that we |
| 1632 | // expect to see. |
| 1633 | TemplateParameterList *ExpectedTemplateParams = 0; |
| 1634 | |
Douglas Gregor | 175c5bb | 2011-05-11 23:26:17 +0000 | [diff] [blame] | 1635 | // C++0x [temp.expl.spec]p15: |
| 1636 | // A member or a member template may be nested within many enclosing |
| 1637 | // class templates. In an explicit specialization for such a member, the |
| 1638 | // member declaration shall be preceded by a template<> for each |
| 1639 | // enclosing class template that is explicitly specialized. |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1640 | if (CXXRecordDecl *Record = T->getAsCXXRecordDecl()) { |
| 1641 | if (ClassTemplatePartialSpecializationDecl *Partial |
| 1642 | = dyn_cast<ClassTemplatePartialSpecializationDecl>(Record)) { |
| 1643 | ExpectedTemplateParams = Partial->getTemplateParameters(); |
| 1644 | NeedNonemptyTemplateHeader = true; |
| 1645 | } else if (Record->isDependentType()) { |
| 1646 | if (Record->getDescribedClassTemplate()) { |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 1647 | ExpectedTemplateParams = Record->getDescribedClassTemplate() |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1648 | ->getTemplateParameters(); |
| 1649 | NeedNonemptyTemplateHeader = true; |
| 1650 | } |
| 1651 | } else if (ClassTemplateSpecializationDecl *Spec |
| 1652 | = dyn_cast<ClassTemplateSpecializationDecl>(Record)) { |
| 1653 | // C++0x [temp.expl.spec]p4: |
| 1654 | // Members of an explicitly specialized class template are defined |
| 1655 | // in the same manner as members of normal classes, and not using |
| 1656 | // the template<> syntax. |
| 1657 | if (Spec->getSpecializationKind() != TSK_ExplicitSpecialization) |
| 1658 | NeedEmptyTemplateHeader = true; |
| 1659 | else |
Douglas Gregor | 95ea450 | 2011-06-01 22:37:07 +0000 | [diff] [blame] | 1660 | continue; |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1661 | } else if (Record->getTemplateSpecializationKind()) { |
| 1662 | if (Record->getTemplateSpecializationKind() |
Douglas Gregor | 175c5bb | 2011-05-11 23:26:17 +0000 | [diff] [blame] | 1663 | != TSK_ExplicitSpecialization && |
| 1664 | TypeIdx == NumTypes - 1) |
| 1665 | IsExplicitSpecialization = true; |
| 1666 | |
| 1667 | continue; |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1668 | } |
| 1669 | } else if (const TemplateSpecializationType *TST |
| 1670 | = T->getAs<TemplateSpecializationType>()) { |
| 1671 | if (TemplateDecl *Template = TST->getTemplateName().getAsTemplateDecl()) { |
| 1672 | ExpectedTemplateParams = Template->getTemplateParameters(); |
| 1673 | NeedNonemptyTemplateHeader = true; |
| 1674 | } |
| 1675 | } else if (T->getAs<DependentTemplateSpecializationType>()) { |
| 1676 | // FIXME: We actually could/should check the template arguments here |
| 1677 | // against the corresponding template parameter list. |
| 1678 | NeedNonemptyTemplateHeader = false; |
| 1679 | } |
| 1680 | |
Douglas Gregor | 89b9f10 | 2011-06-06 15:22:55 +0000 | [diff] [blame] | 1681 | // C++ [temp.expl.spec]p16: |
| 1682 | // In an explicit specialization declaration for a member of a class |
| 1683 | // template or a member template that ap- pears in namespace scope, the |
| 1684 | // member template and some of its enclosing class templates may remain |
| 1685 | // unspecialized, except that the declaration shall not explicitly |
| 1686 | // specialize a class member template if its en- closing class templates |
| 1687 | // are not explicitly specialized as well. |
| 1688 | if (ParamIdx < NumParamLists) { |
| 1689 | if (ParamLists[ParamIdx]->size() == 0) { |
| 1690 | if (SawNonEmptyTemplateParameterList) { |
| 1691 | Diag(DeclLoc, diag::err_specialize_member_of_template) |
| 1692 | << ParamLists[ParamIdx]->getSourceRange(); |
| 1693 | Invalid = true; |
| 1694 | IsExplicitSpecialization = false; |
| 1695 | return 0; |
| 1696 | } |
| 1697 | } else |
| 1698 | SawNonEmptyTemplateParameterList = true; |
| 1699 | } |
| 1700 | |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1701 | if (NeedEmptyTemplateHeader) { |
| 1702 | // If we're on the last of the types, and we need a 'template<>' header |
| 1703 | // here, then it's an explicit specialization. |
| 1704 | if (TypeIdx == NumTypes - 1) |
| 1705 | IsExplicitSpecialization = true; |
| 1706 | |
| 1707 | if (ParamIdx < NumParamLists) { |
| 1708 | if (ParamLists[ParamIdx]->size() > 0) { |
| 1709 | // The header has template parameters when it shouldn't. Complain. |
| 1710 | Diag(ParamLists[ParamIdx]->getTemplateLoc(), |
| 1711 | diag::err_template_param_list_matches_nontemplate) |
| 1712 | << T |
| 1713 | << SourceRange(ParamLists[ParamIdx]->getLAngleLoc(), |
| 1714 | ParamLists[ParamIdx]->getRAngleLoc()) |
| 1715 | << getRangeOfTypeInNestedNameSpecifier(Context, T, SS); |
| 1716 | Invalid = true; |
| 1717 | return 0; |
| 1718 | } |
| 1719 | |
| 1720 | // Consume this template header. |
| 1721 | ++ParamIdx; |
| 1722 | continue; |
| 1723 | } |
| 1724 | |
| 1725 | if (!IsFriend) { |
| 1726 | // We don't have a template header, but we should. |
| 1727 | SourceLocation ExpectedTemplateLoc; |
| 1728 | if (NumParamLists > 0) |
| 1729 | ExpectedTemplateLoc = ParamLists[0]->getTemplateLoc(); |
| 1730 | else |
| 1731 | ExpectedTemplateLoc = DeclStartLoc; |
| 1732 | |
| 1733 | Diag(DeclLoc, diag::err_template_spec_needs_header) |
| 1734 | << getRangeOfTypeInNestedNameSpecifier(Context, T, SS) |
| 1735 | << FixItHint::CreateInsertion(ExpectedTemplateLoc, "template<> "); |
| 1736 | } |
| 1737 | |
| 1738 | continue; |
| 1739 | } |
| 1740 | |
| 1741 | if (NeedNonemptyTemplateHeader) { |
| 1742 | // In friend declarations we can have template-ids which don't |
| 1743 | // depend on the corresponding template parameter lists. But |
| 1744 | // assume that empty parameter lists are supposed to match this |
| 1745 | // template-id. |
| 1746 | if (IsFriend && T->isDependentType()) { |
| 1747 | if (ParamIdx < NumParamLists && |
| 1748 | DependsOnTemplateParameters(T, ParamLists[ParamIdx])) |
| 1749 | ExpectedTemplateParams = 0; |
| 1750 | else |
| 1751 | continue; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1752 | } |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1753 | |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1754 | if (ParamIdx < NumParamLists) { |
| 1755 | // Check the template parameter list, if we can. |
| 1756 | if (ExpectedTemplateParams && |
| 1757 | !TemplateParameterListsAreEqual(ParamLists[ParamIdx], |
| 1758 | ExpectedTemplateParams, |
| 1759 | true, TPL_TemplateMatch)) |
| 1760 | Invalid = true; |
| 1761 | |
| 1762 | if (!Invalid && |
| 1763 | CheckTemplateParameterList(ParamLists[ParamIdx], 0, |
| 1764 | TPC_ClassTemplateMember)) |
| 1765 | Invalid = true; |
| 1766 | |
| 1767 | ++ParamIdx; |
| 1768 | continue; |
| 1769 | } |
| 1770 | |
| 1771 | Diag(DeclLoc, diag::err_template_spec_needs_template_parameters) |
| 1772 | << T |
| 1773 | << getRangeOfTypeInNestedNameSpecifier(Context, T, SS); |
| 1774 | Invalid = true; |
| 1775 | continue; |
| 1776 | } |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1777 | } |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1778 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1779 | // If there were at least as many template-ids as there were template |
| 1780 | // parameter lists, then there are no template parameter lists remaining for |
| 1781 | // the declaration itself. |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1782 | if (ParamIdx >= NumParamLists) |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1783 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1784 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1785 | // If there were too many template parameter lists, complain about that now. |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1786 | if (ParamIdx < NumParamLists - 1) { |
| 1787 | bool HasAnyExplicitSpecHeader = false; |
| 1788 | bool AllExplicitSpecHeaders = true; |
| 1789 | for (unsigned I = ParamIdx; I != NumParamLists - 1; ++I) { |
| 1790 | if (ParamLists[I]->size() == 0) |
| 1791 | HasAnyExplicitSpecHeader = true; |
| 1792 | else |
| 1793 | AllExplicitSpecHeaders = false; |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1794 | } |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1795 | |
| 1796 | Diag(ParamLists[ParamIdx]->getTemplateLoc(), |
| 1797 | AllExplicitSpecHeaders? diag::warn_template_spec_extra_headers |
| 1798 | : diag::err_template_spec_extra_headers) |
| 1799 | << SourceRange(ParamLists[ParamIdx]->getTemplateLoc(), |
| 1800 | ParamLists[NumParamLists - 2]->getRAngleLoc()); |
| 1801 | |
| 1802 | // If there was a specialization somewhere, such that 'template<>' is |
| 1803 | // not required, and there were any 'template<>' headers, note where the |
| 1804 | // specialization occurred. |
| 1805 | if (ExplicitSpecLoc.isValid() && HasAnyExplicitSpecHeader) |
| 1806 | Diag(ExplicitSpecLoc, |
| 1807 | diag::note_explicit_template_spec_does_not_need_header) |
| 1808 | << NestedTypes.back(); |
| 1809 | |
| 1810 | // We have a template parameter list with no corresponding scope, which |
| 1811 | // means that the resulting template declaration can't be instantiated |
| 1812 | // properly (we'll end up with dependent nodes when we shouldn't). |
| 1813 | if (!AllExplicitSpecHeaders) |
| 1814 | Invalid = true; |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1815 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1816 | |
Douglas Gregor | 89b9f10 | 2011-06-06 15:22:55 +0000 | [diff] [blame] | 1817 | // C++ [temp.expl.spec]p16: |
| 1818 | // In an explicit specialization declaration for a member of a class |
| 1819 | // template or a member template that ap- pears in namespace scope, the |
| 1820 | // member template and some of its enclosing class templates may remain |
| 1821 | // unspecialized, except that the declaration shall not explicitly |
| 1822 | // specialize a class member template if its en- closing class templates |
| 1823 | // are not explicitly specialized as well. |
| 1824 | if (ParamLists[NumParamLists - 1]->size() == 0 && |
| 1825 | SawNonEmptyTemplateParameterList) { |
| 1826 | Diag(DeclLoc, diag::err_specialize_member_of_template) |
| 1827 | << ParamLists[ParamIdx]->getSourceRange(); |
| 1828 | Invalid = true; |
| 1829 | IsExplicitSpecialization = false; |
| 1830 | return 0; |
| 1831 | } |
| 1832 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1833 | // Return the last template parameter list, which corresponds to the |
| 1834 | // entity being declared. |
| 1835 | return ParamLists[NumParamLists - 1]; |
| 1836 | } |
| 1837 | |
Douglas Gregor | 6cd9d4a | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 1838 | void Sema::NoteAllFoundTemplates(TemplateName Name) { |
| 1839 | if (TemplateDecl *Template = Name.getAsTemplateDecl()) { |
| 1840 | Diag(Template->getLocation(), diag::note_template_declared_here) |
| 1841 | << (isa<FunctionTemplateDecl>(Template)? 0 |
| 1842 | : isa<ClassTemplateDecl>(Template)? 1 |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 1843 | : isa<TypeAliasTemplateDecl>(Template)? 2 |
| 1844 | : 3) |
Douglas Gregor | 6cd9d4a | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 1845 | << Template->getDeclName(); |
| 1846 | return; |
| 1847 | } |
| 1848 | |
| 1849 | if (OverloadedTemplateStorage *OST = Name.getAsOverloadedTemplate()) { |
| 1850 | for (OverloadedTemplateStorage::iterator I = OST->begin(), |
| 1851 | IEnd = OST->end(); |
| 1852 | I != IEnd; ++I) |
| 1853 | Diag((*I)->getLocation(), diag::note_template_declared_here) |
| 1854 | << 0 << (*I)->getDeclName(); |
| 1855 | |
| 1856 | return; |
| 1857 | } |
| 1858 | } |
| 1859 | |
| 1860 | |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1861 | QualType Sema::CheckTemplateIdType(TemplateName Name, |
| 1862 | SourceLocation TemplateLoc, |
Douglas Gregor | 6771423 | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 1863 | TemplateArgumentListInfo &TemplateArgs) { |
John McCall | 1460604 | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 1864 | DependentTemplateName *DTN |
| 1865 | = Name.getUnderlying().getAsDependentTemplateName(); |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 1866 | if (DTN && DTN->isIdentifier()) |
| 1867 | // When building a template-id where the template-name is dependent, |
| 1868 | // assume the template is a type template. Either our assumption is |
| 1869 | // correct, or the code is ill-formed and will be diagnosed when the |
| 1870 | // dependent name is substituted. |
| 1871 | return Context.getDependentTemplateSpecializationType(ETK_None, |
| 1872 | DTN->getQualifier(), |
| 1873 | DTN->getIdentifier(), |
| 1874 | TemplateArgs); |
| 1875 | |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1876 | TemplateDecl *Template = Name.getAsTemplateDecl(); |
Douglas Gregor | 6cd9d4a | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 1877 | if (!Template || isa<FunctionTemplateDecl>(Template)) { |
| 1878 | // We might have a substituted template template parameter pack. If so, |
| 1879 | // build a template specialization type for it. |
| 1880 | if (Name.getAsSubstTemplateTemplateParmPack()) |
| 1881 | return Context.getTemplateSpecializationType(Name, TemplateArgs); |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 1882 | |
Douglas Gregor | 6cd9d4a | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 1883 | Diag(TemplateLoc, diag::err_template_id_not_a_type) |
| 1884 | << Name; |
| 1885 | NoteAllFoundTemplates(Name); |
| 1886 | return QualType(); |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 1887 | } |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1888 | |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1889 | // Check that the template argument list is well-formed for this |
| 1890 | // template. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1891 | SmallVector<TemplateArgument, 4> Converted; |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1892 | if (CheckTemplateArgumentList(Template, TemplateLoc, TemplateArgs, |
Douglas Gregor | 16134c6 | 2009-07-01 00:28:38 +0000 | [diff] [blame] | 1893 | false, Converted)) |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1894 | return QualType(); |
| 1895 | |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 1896 | assert((Converted.size() == Template->getTemplateParameters()->size()) && |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1897 | "Converted template argument list is too short!"); |
| 1898 | |
| 1899 | QualType CanonType; |
| 1900 | |
Douglas Gregor | 561f812 | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 1901 | bool InstantiationDependent = false; |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 1902 | if (TypeAliasTemplateDecl *AliasTemplate |
| 1903 | = dyn_cast<TypeAliasTemplateDecl>(Template)) { |
| 1904 | // Find the canonical type for this type alias template specialization. |
| 1905 | TypeAliasDecl *Pattern = AliasTemplate->getTemplatedDecl(); |
| 1906 | if (Pattern->isInvalidDecl()) |
| 1907 | return QualType(); |
| 1908 | |
| 1909 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
| 1910 | Converted.data(), Converted.size()); |
| 1911 | |
| 1912 | // Only substitute for the innermost template argument list. |
| 1913 | MultiLevelTemplateArgumentList TemplateArgLists; |
Richard Smith | 1804174 | 2011-05-14 15:04:18 +0000 | [diff] [blame] | 1914 | TemplateArgLists.addOuterTemplateArguments(&TemplateArgs); |
Richard Smith | aff37b4 | 2011-05-12 00:06:17 +0000 | [diff] [blame] | 1915 | unsigned Depth = AliasTemplate->getTemplateParameters()->getDepth(); |
| 1916 | for (unsigned I = 0; I < Depth; ++I) |
| 1917 | TemplateArgLists.addOuterTemplateArguments(0, 0); |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 1918 | |
| 1919 | InstantiatingTemplate Inst(*this, TemplateLoc, Template); |
| 1920 | CanonType = SubstType(Pattern->getUnderlyingType(), |
| 1921 | TemplateArgLists, AliasTemplate->getLocation(), |
| 1922 | AliasTemplate->getDeclName()); |
| 1923 | if (CanonType.isNull()) |
| 1924 | return QualType(); |
| 1925 | } else if (Name.isDependent() || |
| 1926 | TemplateSpecializationType::anyDependentTemplateArguments( |
Douglas Gregor | 561f812 | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 1927 | TemplateArgs, InstantiationDependent)) { |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1928 | // This class template specialization is a dependent |
| 1929 | // type. Therefore, its canonical type is another class template |
| 1930 | // specialization type that contains all of the converted |
| 1931 | // arguments in canonical form. This ensures that, e.g., A<T> and |
| 1932 | // A<T, T> have identical types when A is declared as: |
| 1933 | // |
| 1934 | // template<typename T, typename U = T> struct A; |
Douglas Gregor | 25a3ef7 | 2009-05-07 06:41:52 +0000 | [diff] [blame] | 1935 | TemplateName CanonName = Context.getCanonicalTemplateName(Name); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1936 | CanonType = Context.getTemplateSpecializationType(CanonName, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 1937 | Converted.data(), |
| 1938 | Converted.size()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1939 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 1940 | // FIXME: CanonType is not actually the canonical type, and unfortunately |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1941 | // it is a TemplateSpecializationType that we will never use again. |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 1942 | // In the future, we need to teach getTemplateSpecializationType to only |
| 1943 | // build the canonical type and return that to us. |
| 1944 | CanonType = Context.getCanonicalType(CanonType); |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 1945 | |
| 1946 | // This might work out to be a current instantiation, in which |
| 1947 | // case the canonical type needs to be the InjectedClassNameType. |
| 1948 | // |
| 1949 | // TODO: in theory this could be a simple hashtable lookup; most |
| 1950 | // changes to CurContext don't change the set of current |
| 1951 | // instantiations. |
| 1952 | if (isa<ClassTemplateDecl>(Template)) { |
| 1953 | for (DeclContext *Ctx = CurContext; Ctx; Ctx = Ctx->getLookupParent()) { |
| 1954 | // If we get out to a namespace, we're done. |
| 1955 | if (Ctx->isFileContext()) break; |
| 1956 | |
| 1957 | // If this isn't a record, keep looking. |
| 1958 | CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Ctx); |
| 1959 | if (!Record) continue; |
| 1960 | |
| 1961 | // Look for one of the two cases with InjectedClassNameTypes |
| 1962 | // and check whether it's the same template. |
| 1963 | if (!isa<ClassTemplatePartialSpecializationDecl>(Record) && |
| 1964 | !Record->getDescribedClassTemplate()) |
| 1965 | continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1966 | |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 1967 | // Fetch the injected class name type and check whether its |
| 1968 | // injected type is equal to the type we just built. |
| 1969 | QualType ICNT = Context.getTypeDeclType(Record); |
| 1970 | QualType Injected = cast<InjectedClassNameType>(ICNT) |
| 1971 | ->getInjectedSpecializationType(); |
| 1972 | |
| 1973 | if (CanonType != Injected->getCanonicalTypeInternal()) |
| 1974 | continue; |
| 1975 | |
| 1976 | // If so, the canonical type of this TST is the injected |
| 1977 | // class name type of the record we just found. |
| 1978 | assert(ICNT.isCanonical()); |
| 1979 | CanonType = ICNT; |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 1980 | break; |
| 1981 | } |
| 1982 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1983 | } else if (ClassTemplateDecl *ClassTemplate |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1984 | = dyn_cast<ClassTemplateDecl>(Template)) { |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1985 | // Find the class template specialization declaration that |
| 1986 | // corresponds to these arguments. |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1987 | void *InsertPos = 0; |
| 1988 | ClassTemplateSpecializationDecl *Decl |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1989 | = ClassTemplate->findSpecialization(Converted.data(), Converted.size(), |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 1990 | InsertPos); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1991 | if (!Decl) { |
| 1992 | // This is the first time we have referenced this class template |
| 1993 | // specialization. Create the canonical declaration and add it to |
| 1994 | // the set of specializations. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1995 | Decl = ClassTemplateSpecializationDecl::Create(Context, |
Douglas Gregor | 13c8577 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 1996 | ClassTemplate->getTemplatedDecl()->getTagKind(), |
| 1997 | ClassTemplate->getDeclContext(), |
| 1998 | ClassTemplate->getLocation(), |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 1999 | ClassTemplate->getLocation(), |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2000 | ClassTemplate, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2001 | Converted.data(), |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2002 | Converted.size(), 0); |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 2003 | ClassTemplate->AddSpecialization(Decl, InsertPos); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2004 | Decl->setLexicalDeclContext(CurContext); |
| 2005 | } |
| 2006 | |
| 2007 | CanonType = Context.getTypeDeclType(Decl); |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 2008 | assert(isa<RecordType>(CanonType) && |
| 2009 | "type of non-dependent specialization is not a RecordType"); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2010 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2011 | |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2012 | // Build the fully-sugared type for this class template |
| 2013 | // specialization, which refers back to the class template |
| 2014 | // specialization we created or found. |
John McCall | 71d74bc | 2010-06-13 09:25:03 +0000 | [diff] [blame] | 2015 | return Context.getTemplateSpecializationType(Name, TemplateArgs, CanonType); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2016 | } |
| 2017 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2018 | TypeResult |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2019 | Sema::ActOnTemplateIdType(CXXScopeSpec &SS, |
| 2020 | TemplateTy TemplateD, SourceLocation TemplateLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2021 | SourceLocation LAngleLoc, |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 2022 | ASTTemplateArgsPtr TemplateArgsIn, |
John McCall | 6b2becf | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 2023 | SourceLocation RAngleLoc) { |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2024 | if (SS.isInvalid()) |
| 2025 | return true; |
| 2026 | |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 2027 | TemplateName Template = TemplateD.getAsVal<TemplateName>(); |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 2028 | |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2029 | // Translate the parser's template argument list in our AST format. |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2030 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
Douglas Gregor | 314b97f | 2009-11-10 19:49:08 +0000 | [diff] [blame] | 2031 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2032 | |
Douglas Gregor | a88f09f | 2011-02-28 17:23:35 +0000 | [diff] [blame] | 2033 | if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) { |
| 2034 | QualType T = Context.getDependentTemplateSpecializationType(ETK_None, |
| 2035 | DTN->getQualifier(), |
| 2036 | DTN->getIdentifier(), |
| 2037 | TemplateArgs); |
| 2038 | |
| 2039 | // Build type-source information. |
| 2040 | TypeLocBuilder TLB; |
| 2041 | DependentTemplateSpecializationTypeLoc SpecTL |
| 2042 | = TLB.push<DependentTemplateSpecializationTypeLoc>(T); |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2043 | SpecTL.setKeywordLoc(SourceLocation()); |
Douglas Gregor | a88f09f | 2011-02-28 17:23:35 +0000 | [diff] [blame] | 2044 | SpecTL.setNameLoc(TemplateLoc); |
| 2045 | SpecTL.setLAngleLoc(LAngleLoc); |
| 2046 | SpecTL.setRAngleLoc(RAngleLoc); |
Douglas Gregor | 94fdffa | 2011-03-01 20:11:18 +0000 | [diff] [blame] | 2047 | SpecTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
Douglas Gregor | a88f09f | 2011-02-28 17:23:35 +0000 | [diff] [blame] | 2048 | for (unsigned I = 0, N = SpecTL.getNumArgs(); I != N; ++I) |
| 2049 | SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo()); |
| 2050 | return CreateParsedType(T, TLB.getTypeSourceInfo(Context, T)); |
| 2051 | } |
| 2052 | |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2053 | QualType Result = CheckTemplateIdType(Template, TemplateLoc, TemplateArgs); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2054 | TemplateArgsIn.release(); |
Douglas Gregor | 31a19b6 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 2055 | |
| 2056 | if (Result.isNull()) |
| 2057 | return true; |
| 2058 | |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2059 | // Build type-source information. |
| 2060 | TypeLocBuilder TLB; |
| 2061 | TemplateSpecializationTypeLoc SpecTL |
| 2062 | = TLB.push<TemplateSpecializationTypeLoc>(Result); |
| 2063 | SpecTL.setTemplateNameLoc(TemplateLoc); |
| 2064 | SpecTL.setLAngleLoc(LAngleLoc); |
| 2065 | SpecTL.setRAngleLoc(RAngleLoc); |
| 2066 | for (unsigned i = 0, e = SpecTL.getNumArgs(); i != e; ++i) |
| 2067 | SpecTL.setArgLocInfo(i, TemplateArgs[i].getLocInfo()); |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2068 | |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2069 | if (SS.isNotEmpty()) { |
| 2070 | // Create an elaborated-type-specifier containing the nested-name-specifier. |
| 2071 | Result = Context.getElaboratedType(ETK_None, SS.getScopeRep(), Result); |
| 2072 | ElaboratedTypeLoc ElabTL = TLB.push<ElaboratedTypeLoc>(Result); |
| 2073 | ElabTL.setKeywordLoc(SourceLocation()); |
| 2074 | ElabTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 2075 | } |
| 2076 | |
| 2077 | return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result)); |
John McCall | 6b2becf | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 2078 | } |
John McCall | f1bbbb4 | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 2079 | |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2080 | TypeResult Sema::ActOnTagTemplateIdType(TagUseKind TUK, |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2081 | TypeSpecifierType TagSpec, |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2082 | SourceLocation TagLoc, |
| 2083 | CXXScopeSpec &SS, |
| 2084 | TemplateTy TemplateD, |
| 2085 | SourceLocation TemplateLoc, |
| 2086 | SourceLocation LAngleLoc, |
| 2087 | ASTTemplateArgsPtr TemplateArgsIn, |
| 2088 | SourceLocation RAngleLoc) { |
| 2089 | TemplateName Template = TemplateD.getAsVal<TemplateName>(); |
| 2090 | |
| 2091 | // Translate the parser's template argument list in our AST format. |
| 2092 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
| 2093 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
| 2094 | |
| 2095 | // Determine the tag kind |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2096 | TagTypeKind TagKind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2097 | ElaboratedTypeKeyword Keyword |
| 2098 | = TypeWithKeyword::getKeywordForTagTypeKind(TagKind); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2099 | |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2100 | if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) { |
| 2101 | QualType T = Context.getDependentTemplateSpecializationType(Keyword, |
| 2102 | DTN->getQualifier(), |
| 2103 | DTN->getIdentifier(), |
| 2104 | TemplateArgs); |
| 2105 | |
| 2106 | // Build type-source information. |
| 2107 | TypeLocBuilder TLB; |
| 2108 | DependentTemplateSpecializationTypeLoc SpecTL |
| 2109 | = TLB.push<DependentTemplateSpecializationTypeLoc>(T); |
| 2110 | SpecTL.setKeywordLoc(TagLoc); |
| 2111 | SpecTL.setNameLoc(TemplateLoc); |
| 2112 | SpecTL.setLAngleLoc(LAngleLoc); |
| 2113 | SpecTL.setRAngleLoc(RAngleLoc); |
| 2114 | SpecTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 2115 | for (unsigned I = 0, N = SpecTL.getNumArgs(); I != N; ++I) |
| 2116 | SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo()); |
| 2117 | return CreateParsedType(T, TLB.getTypeSourceInfo(Context, T)); |
| 2118 | } |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2119 | |
| 2120 | if (TypeAliasTemplateDecl *TAT = |
| 2121 | dyn_cast_or_null<TypeAliasTemplateDecl>(Template.getAsTemplateDecl())) { |
| 2122 | // C++0x [dcl.type.elab]p2: |
| 2123 | // If the identifier resolves to a typedef-name or the simple-template-id |
| 2124 | // resolves to an alias template specialization, the |
| 2125 | // elaborated-type-specifier is ill-formed. |
| 2126 | Diag(TemplateLoc, diag::err_tag_reference_non_tag) << 4; |
| 2127 | Diag(TAT->getLocation(), diag::note_declared_at); |
| 2128 | } |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2129 | |
| 2130 | QualType Result = CheckTemplateIdType(Template, TemplateLoc, TemplateArgs); |
| 2131 | if (Result.isNull()) |
| 2132 | return TypeResult(); |
| 2133 | |
| 2134 | // Check the tag kind |
| 2135 | if (const RecordType *RT = Result->getAs<RecordType>()) { |
John McCall | 6b2becf | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 2136 | RecordDecl *D = RT->getDecl(); |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2137 | |
John McCall | 6b2becf | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 2138 | IdentifierInfo *Id = D->getIdentifier(); |
| 2139 | assert(Id && "templated class must have an identifier"); |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2140 | |
Richard Trieu | bbf34c0 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 2141 | if (!isAcceptableTagRedeclaration(D, TagKind, TUK == TUK_Definition, |
| 2142 | TagLoc, *Id)) { |
John McCall | 6b2becf | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 2143 | Diag(TagLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2144 | << Result |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 2145 | << FixItHint::CreateReplacement(SourceRange(TagLoc), D->getKindName()); |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 2146 | Diag(D->getLocation(), diag::note_previous_use); |
John McCall | f1bbbb4 | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 2147 | } |
| 2148 | } |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2149 | |
| 2150 | // Provide source-location information for the template specialization. |
| 2151 | TypeLocBuilder TLB; |
| 2152 | TemplateSpecializationTypeLoc SpecTL |
| 2153 | = TLB.push<TemplateSpecializationTypeLoc>(Result); |
| 2154 | SpecTL.setTemplateNameLoc(TemplateLoc); |
| 2155 | SpecTL.setLAngleLoc(LAngleLoc); |
| 2156 | SpecTL.setRAngleLoc(RAngleLoc); |
| 2157 | for (unsigned i = 0, e = SpecTL.getNumArgs(); i != e; ++i) |
| 2158 | SpecTL.setArgLocInfo(i, TemplateArgs[i].getLocInfo()); |
John McCall | f1bbbb4 | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 2159 | |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2160 | // Construct an elaborated type containing the nested-name-specifier (if any) |
| 2161 | // and keyword. |
| 2162 | Result = Context.getElaboratedType(Keyword, SS.getScopeRep(), Result); |
| 2163 | ElaboratedTypeLoc ElabTL = TLB.push<ElaboratedTypeLoc>(Result); |
| 2164 | ElabTL.setKeywordLoc(TagLoc); |
| 2165 | ElabTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 2166 | return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result)); |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 2167 | } |
| 2168 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2169 | ExprResult Sema::BuildTemplateIdExpr(const CXXScopeSpec &SS, |
Douglas Gregor | 4c9be89 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 2170 | LookupResult &R, |
| 2171 | bool RequiresADL, |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2172 | const TemplateArgumentListInfo &TemplateArgs) { |
Douglas Gregor | edce4dd | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 2173 | // FIXME: Can we do any checking at this point? I guess we could check the |
| 2174 | // template arguments that we have against the template name, if the template |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2175 | // 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] | 2176 | // though. |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 2177 | // foo<int> could identify a single function unambiguously |
| 2178 | // This approach does NOT work, since f<int>(1); |
| 2179 | // gets resolved prior to resorting to overload resolution |
| 2180 | // i.e., template<class T> void f(double); |
| 2181 | // vs template<class T, class U> void f(U); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2182 | |
| 2183 | // These should be filtered out by our callers. |
| 2184 | assert(!R.empty() && "empty lookup results when building templateid"); |
| 2185 | assert(!R.isAmbiguous() && "ambiguous lookup when building templateid"); |
| 2186 | |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 2187 | // We don't want lookup warnings at this point. |
| 2188 | R.suppressDiagnostics(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2189 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2190 | UnresolvedLookupExpr *ULE |
Douglas Gregor | bebbe0d | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 2191 | = UnresolvedLookupExpr::Create(Context, R.getNamingClass(), |
Douglas Gregor | 4c9be89 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 2192 | SS.getWithLocInContext(Context), |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2193 | R.getLookupNameInfo(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2194 | RequiresADL, TemplateArgs, |
Douglas Gregor | 5a84dec | 2010-05-23 18:57:34 +0000 | [diff] [blame] | 2195 | R.begin(), R.end()); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2196 | |
| 2197 | return Owned(ULE); |
Douglas Gregor | edce4dd | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 2198 | } |
| 2199 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2200 | // We actually only call this from template instantiation. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2201 | ExprResult |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 2202 | Sema::BuildQualifiedTemplateIdExpr(CXXScopeSpec &SS, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2203 | const DeclarationNameInfo &NameInfo, |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2204 | const TemplateArgumentListInfo &TemplateArgs) { |
| 2205 | DeclContext *DC; |
| 2206 | if (!(DC = computeDeclContext(SS, false)) || |
| 2207 | DC->isDependentContext() || |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 2208 | RequireCompleteDeclContext(SS, DC)) |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2209 | return BuildDependentDeclRefExpr(SS, NameInfo, &TemplateArgs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2210 | |
Douglas Gregor | 1fd6d44 | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 2211 | bool MemberOfUnknownSpecialization; |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2212 | LookupResult R(*this, NameInfo, LookupOrdinaryName); |
Douglas Gregor | 1fd6d44 | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 2213 | LookupTemplateName(R, (Scope*) 0, SS, QualType(), /*Entering*/ false, |
| 2214 | MemberOfUnknownSpecialization); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2215 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2216 | if (R.isAmbiguous()) |
| 2217 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2218 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2219 | if (R.empty()) { |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2220 | Diag(NameInfo.getLoc(), diag::err_template_kw_refers_to_non_template) |
| 2221 | << NameInfo.getName() << SS.getRange(); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2222 | return ExprError(); |
| 2223 | } |
| 2224 | |
| 2225 | if (ClassTemplateDecl *Temp = R.getAsSingle<ClassTemplateDecl>()) { |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2226 | Diag(NameInfo.getLoc(), diag::err_template_kw_refers_to_class_template) |
| 2227 | << (NestedNameSpecifier*) SS.getScopeRep() |
| 2228 | << NameInfo.getName() << SS.getRange(); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2229 | Diag(Temp->getLocation(), diag::note_referenced_class_template); |
| 2230 | return ExprError(); |
| 2231 | } |
| 2232 | |
| 2233 | return BuildTemplateIdExpr(SS, R, /* ADL */ false, TemplateArgs); |
Douglas Gregor | edce4dd | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 2234 | } |
| 2235 | |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 2236 | /// \brief Form a dependent template name. |
| 2237 | /// |
| 2238 | /// This action forms a dependent template name given the template |
| 2239 | /// name and its (presumably dependent) scope specifier. For |
| 2240 | /// example, given "MetaFun::template apply", the scope specifier \p |
| 2241 | /// SS will be "MetaFun::", \p TemplateKWLoc contains the location |
| 2242 | /// of the "template" keyword, and "apply" is the \p Name. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2243 | TemplateNameKind Sema::ActOnDependentTemplateName(Scope *S, |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2244 | SourceLocation TemplateKWLoc, |
| 2245 | CXXScopeSpec &SS, |
| 2246 | UnqualifiedId &Name, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2247 | ParsedType ObjectType, |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2248 | bool EnteringContext, |
| 2249 | TemplateTy &Result) { |
Douglas Gregor | 1a15dae | 2010-06-16 22:31:08 +0000 | [diff] [blame] | 2250 | if (TemplateKWLoc.isValid() && S && !S->getTemplateParamParent() && |
| 2251 | !getLangOptions().CPlusPlus0x) |
| 2252 | Diag(TemplateKWLoc, diag::ext_template_outside_of_template) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2253 | << FixItHint::CreateRemoval(TemplateKWLoc); |
| 2254 | |
Douglas Gregor | 0707bc5 | 2010-01-19 16:01:07 +0000 | [diff] [blame] | 2255 | DeclContext *LookupCtx = 0; |
| 2256 | if (SS.isSet()) |
| 2257 | LookupCtx = computeDeclContext(SS, EnteringContext); |
| 2258 | if (!LookupCtx && ObjectType) |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2259 | LookupCtx = computeDeclContext(ObjectType.get()); |
Douglas Gregor | 0707bc5 | 2010-01-19 16:01:07 +0000 | [diff] [blame] | 2260 | if (LookupCtx) { |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 2261 | // C++0x [temp.names]p5: |
| 2262 | // If a name prefixed by the keyword template is not the name of |
| 2263 | // a template, the program is ill-formed. [Note: the keyword |
| 2264 | // template may not be applied to non-template members of class |
| 2265 | // templates. -end note ] [ Note: as is the case with the |
| 2266 | // typename prefix, the template prefix is allowed in cases |
| 2267 | // where it is not strictly necessary; i.e., when the |
| 2268 | // nested-name-specifier or the expression on the left of the -> |
| 2269 | // or . is not dependent on a template-parameter, or the use |
| 2270 | // does not appear in the scope of a template. -end note] |
| 2271 | // |
| 2272 | // Note: C++03 was more strict here, because it banned the use of |
| 2273 | // the "template" keyword prior to a template-name that was not a |
| 2274 | // dependent name. C++ DR468 relaxed this requirement (the |
| 2275 | // "template" keyword is now permitted). We follow the C++0x |
Douglas Gregor | 732281d | 2010-06-14 22:07:54 +0000 | [diff] [blame] | 2276 | // 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] | 2277 | bool MemberOfUnknownSpecialization; |
Abramo Bagnara | 7c15353 | 2010-08-06 12:11:11 +0000 | [diff] [blame] | 2278 | TemplateNameKind TNK = isTemplateName(0, SS, TemplateKWLoc.isValid(), Name, |
| 2279 | ObjectType, EnteringContext, Result, |
Douglas Gregor | 1fd6d44 | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 2280 | MemberOfUnknownSpecialization); |
Douglas Gregor | 0707bc5 | 2010-01-19 16:01:07 +0000 | [diff] [blame] | 2281 | if (TNK == TNK_Non_template && LookupCtx->isDependentContext() && |
| 2282 | isa<CXXRecordDecl>(LookupCtx) && |
Douglas Gregor | d078bd2 | 2011-03-11 23:27:41 +0000 | [diff] [blame] | 2283 | (!cast<CXXRecordDecl>(LookupCtx)->hasDefinition() || |
| 2284 | cast<CXXRecordDecl>(LookupCtx)->hasAnyDependentBases())) { |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2285 | // This is a dependent template. Handle it below. |
Douglas Gregor | 9edad9b | 2010-01-14 17:47:39 +0000 | [diff] [blame] | 2286 | } else if (TNK == TNK_Non_template) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2287 | Diag(Name.getSourceRange().getBegin(), |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 2288 | diag::err_template_kw_refers_to_non_template) |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2289 | << GetNameFromUnqualifiedId(Name).getName() |
Douglas Gregor | 0278e12 | 2010-05-05 05:58:24 +0000 | [diff] [blame] | 2290 | << Name.getSourceRange() |
| 2291 | << TemplateKWLoc; |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2292 | return TNK_Non_template; |
Douglas Gregor | 9edad9b | 2010-01-14 17:47:39 +0000 | [diff] [blame] | 2293 | } else { |
| 2294 | // We found something; return it. |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2295 | return TNK; |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 2296 | } |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 2297 | } |
| 2298 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2299 | NestedNameSpecifier *Qualifier |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 2300 | = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2301 | |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 2302 | switch (Name.getKind()) { |
| 2303 | case UnqualifiedId::IK_Identifier: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2304 | Result = TemplateTy::make(Context.getDependentTemplateName(Qualifier, |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2305 | Name.Identifier)); |
| 2306 | return TNK_Dependent_template_name; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2307 | |
Douglas Gregor | ca1bdd7 | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 2308 | case UnqualifiedId::IK_OperatorFunctionId: |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2309 | Result = TemplateTy::make(Context.getDependentTemplateName(Qualifier, |
Douglas Gregor | ca1bdd7 | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 2310 | Name.OperatorFunctionId.Operator)); |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2311 | return TNK_Dependent_template_name; |
Sean Hunt | e6252d1 | 2009-11-28 08:58:14 +0000 | [diff] [blame] | 2312 | |
| 2313 | case UnqualifiedId::IK_LiteralOperatorId: |
| 2314 | assert(false && "We don't support these; Parse shouldn't have allowed propagation"); |
| 2315 | |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 2316 | default: |
| 2317 | break; |
| 2318 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2319 | |
| 2320 | Diag(Name.getSourceRange().getBegin(), |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 2321 | diag::err_template_kw_refers_to_non_template) |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2322 | << GetNameFromUnqualifiedId(Name).getName() |
Douglas Gregor | 0278e12 | 2010-05-05 05:58:24 +0000 | [diff] [blame] | 2323 | << Name.getSourceRange() |
| 2324 | << TemplateKWLoc; |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2325 | return TNK_Non_template; |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 2326 | } |
| 2327 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2328 | bool Sema::CheckTemplateTypeArgument(TemplateTypeParmDecl *Param, |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2329 | const TemplateArgumentLoc &AL, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2330 | SmallVectorImpl<TemplateArgument> &Converted) { |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2331 | const TemplateArgument &Arg = AL.getArgument(); |
| 2332 | |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 2333 | // Check template type parameter. |
Jeffrey Yasskin | db88d8a | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 2334 | switch(Arg.getKind()) { |
| 2335 | case TemplateArgument::Type: |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 2336 | // C++ [temp.arg.type]p1: |
| 2337 | // A template-argument for a template-parameter which is a |
| 2338 | // type shall be a type-id. |
Jeffrey Yasskin | db88d8a | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 2339 | break; |
| 2340 | case TemplateArgument::Template: { |
| 2341 | // We have a template type parameter but the template argument |
| 2342 | // is a template without any arguments. |
| 2343 | SourceRange SR = AL.getSourceRange(); |
| 2344 | TemplateName Name = Arg.getAsTemplate(); |
| 2345 | Diag(SR.getBegin(), diag::err_template_missing_args) |
| 2346 | << Name << SR; |
| 2347 | if (TemplateDecl *Decl = Name.getAsTemplateDecl()) |
| 2348 | Diag(Decl->getLocation(), diag::note_template_decl_here); |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 2349 | |
Jeffrey Yasskin | db88d8a | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 2350 | return true; |
| 2351 | } |
| 2352 | default: { |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 2353 | // We have a template type parameter but the template argument |
| 2354 | // is not a type. |
John McCall | 828bff2 | 2009-10-29 18:45:58 +0000 | [diff] [blame] | 2355 | SourceRange SR = AL.getSourceRange(); |
| 2356 | Diag(SR.getBegin(), diag::err_template_arg_must_be_type) << SR; |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 2357 | Diag(Param->getLocation(), diag::note_template_param_here); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2358 | |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 2359 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2360 | } |
Jeffrey Yasskin | db88d8a | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 2361 | } |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 2362 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2363 | if (CheckTemplateArgument(Param, AL.getTypeSourceInfo())) |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 2364 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2365 | |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 2366 | // Add the converted template type argument. |
Douglas Gregor | e559ca1 | 2011-06-17 22:11:49 +0000 | [diff] [blame] | 2367 | QualType ArgType = Context.getCanonicalType(Arg.getAsType()); |
| 2368 | |
| 2369 | // Objective-C ARC: |
| 2370 | // If an explicitly-specified template argument type is a lifetime type |
| 2371 | // with no lifetime qualifier, the __strong lifetime qualifier is inferred. |
| 2372 | if (getLangOptions().ObjCAutoRefCount && |
| 2373 | ArgType->isObjCLifetimeType() && |
| 2374 | !ArgType.getObjCLifetime()) { |
| 2375 | Qualifiers Qs; |
| 2376 | Qs.setObjCLifetime(Qualifiers::OCL_Strong); |
| 2377 | ArgType = Context.getQualifiedType(ArgType, Qs); |
| 2378 | } |
| 2379 | |
| 2380 | Converted.push_back(TemplateArgument(ArgType)); |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 2381 | return false; |
| 2382 | } |
| 2383 | |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2384 | /// \brief Substitute template arguments into the default template argument for |
| 2385 | /// the given template type parameter. |
| 2386 | /// |
| 2387 | /// \param SemaRef the semantic analysis object for which we are performing |
| 2388 | /// the substitution. |
| 2389 | /// |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2390 | /// \param Template the template that we are synthesizing template arguments |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2391 | /// for. |
| 2392 | /// |
| 2393 | /// \param TemplateLoc the location of the template name that started the |
| 2394 | /// template-id we are checking. |
| 2395 | /// |
| 2396 | /// \param RAngleLoc the location of the right angle bracket ('>') that |
| 2397 | /// terminates the template-id. |
| 2398 | /// |
| 2399 | /// \param Param the template template parameter whose default we are |
| 2400 | /// substituting into. |
| 2401 | /// |
| 2402 | /// \param Converted the list of template arguments provided for template |
| 2403 | /// parameters that precede \p Param in the template parameter list. |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2404 | /// \returns the substituted template argument, or NULL if an error occurred. |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2405 | static TypeSourceInfo * |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2406 | SubstDefaultTemplateArgument(Sema &SemaRef, |
| 2407 | TemplateDecl *Template, |
| 2408 | SourceLocation TemplateLoc, |
| 2409 | SourceLocation RAngleLoc, |
| 2410 | TemplateTypeParmDecl *Param, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2411 | SmallVectorImpl<TemplateArgument> &Converted) { |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2412 | TypeSourceInfo *ArgType = Param->getDefaultArgumentInfo(); |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2413 | |
| 2414 | // If the argument type is dependent, instantiate it now based |
| 2415 | // on the previously-computed template arguments. |
| 2416 | if (ArgType->getType()->isDependentType()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2417 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2418 | Converted.data(), Converted.size()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2419 | |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2420 | MultiLevelTemplateArgumentList AllTemplateArgs |
| 2421 | = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs); |
| 2422 | |
| 2423 | Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2424 | Template, Converted.data(), |
| 2425 | Converted.size(), |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2426 | SourceRange(TemplateLoc, RAngleLoc)); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2427 | |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2428 | ArgType = SemaRef.SubstType(ArgType, AllTemplateArgs, |
| 2429 | Param->getDefaultArgumentLoc(), |
| 2430 | Param->getDeclName()); |
| 2431 | } |
| 2432 | |
| 2433 | return ArgType; |
| 2434 | } |
| 2435 | |
| 2436 | /// \brief Substitute template arguments into the default template argument for |
| 2437 | /// the given non-type template parameter. |
| 2438 | /// |
| 2439 | /// \param SemaRef the semantic analysis object for which we are performing |
| 2440 | /// the substitution. |
| 2441 | /// |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2442 | /// \param Template the template that we are synthesizing template arguments |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2443 | /// for. |
| 2444 | /// |
| 2445 | /// \param TemplateLoc the location of the template name that started the |
| 2446 | /// template-id we are checking. |
| 2447 | /// |
| 2448 | /// \param RAngleLoc the location of the right angle bracket ('>') that |
| 2449 | /// terminates the template-id. |
| 2450 | /// |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2451 | /// \param Param the non-type template parameter whose default we are |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2452 | /// substituting into. |
| 2453 | /// |
| 2454 | /// \param Converted the list of template arguments provided for template |
| 2455 | /// parameters that precede \p Param in the template parameter list. |
| 2456 | /// |
| 2457 | /// \returns the substituted template argument, or NULL if an error occurred. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2458 | static ExprResult |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2459 | SubstDefaultTemplateArgument(Sema &SemaRef, |
| 2460 | TemplateDecl *Template, |
| 2461 | SourceLocation TemplateLoc, |
| 2462 | SourceLocation RAngleLoc, |
| 2463 | NonTypeTemplateParmDecl *Param, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2464 | SmallVectorImpl<TemplateArgument> &Converted) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2465 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2466 | Converted.data(), Converted.size()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2467 | |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2468 | MultiLevelTemplateArgumentList AllTemplateArgs |
| 2469 | = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2470 | |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2471 | Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2472 | Template, Converted.data(), |
| 2473 | Converted.size(), |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2474 | SourceRange(TemplateLoc, RAngleLoc)); |
| 2475 | |
| 2476 | return SemaRef.SubstExpr(Param->getDefaultArgument(), AllTemplateArgs); |
| 2477 | } |
| 2478 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2479 | /// \brief Substitute template arguments into the default template argument for |
| 2480 | /// the given template template parameter. |
| 2481 | /// |
| 2482 | /// \param SemaRef the semantic analysis object for which we are performing |
| 2483 | /// the substitution. |
| 2484 | /// |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2485 | /// \param Template the template that we are synthesizing template arguments |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2486 | /// for. |
| 2487 | /// |
| 2488 | /// \param TemplateLoc the location of the template name that started the |
| 2489 | /// template-id we are checking. |
| 2490 | /// |
| 2491 | /// \param RAngleLoc the location of the right angle bracket ('>') that |
| 2492 | /// terminates the template-id. |
| 2493 | /// |
| 2494 | /// \param Param the template template parameter whose default we are |
| 2495 | /// substituting into. |
| 2496 | /// |
| 2497 | /// \param Converted the list of template arguments provided for template |
| 2498 | /// parameters that precede \p Param in the template parameter list. |
| 2499 | /// |
Douglas Gregor | 1d752d7 | 2011-03-02 18:46:51 +0000 | [diff] [blame] | 2500 | /// \param QualifierLoc Will be set to the nested-name-specifier (with |
| 2501 | /// source-location information) that precedes the template name. |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2502 | /// |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2503 | /// \returns the substituted template argument, or NULL if an error occurred. |
| 2504 | static TemplateName |
| 2505 | SubstDefaultTemplateArgument(Sema &SemaRef, |
| 2506 | TemplateDecl *Template, |
| 2507 | SourceLocation TemplateLoc, |
| 2508 | SourceLocation RAngleLoc, |
| 2509 | TemplateTemplateParmDecl *Param, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2510 | SmallVectorImpl<TemplateArgument> &Converted, |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2511 | NestedNameSpecifierLoc &QualifierLoc) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2512 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2513 | Converted.data(), Converted.size()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2514 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2515 | MultiLevelTemplateArgumentList AllTemplateArgs |
| 2516 | = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2517 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2518 | Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2519 | Template, Converted.data(), |
| 2520 | Converted.size(), |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2521 | SourceRange(TemplateLoc, RAngleLoc)); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2522 | |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2523 | // Substitute into the nested-name-specifier first, |
Douglas Gregor | 1d752d7 | 2011-03-02 18:46:51 +0000 | [diff] [blame] | 2524 | QualifierLoc = Param->getDefaultArgument().getTemplateQualifierLoc(); |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2525 | if (QualifierLoc) { |
| 2526 | QualifierLoc = SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc, |
| 2527 | AllTemplateArgs); |
| 2528 | if (!QualifierLoc) |
| 2529 | return TemplateName(); |
| 2530 | } |
| 2531 | |
Douglas Gregor | 1d752d7 | 2011-03-02 18:46:51 +0000 | [diff] [blame] | 2532 | return SemaRef.SubstTemplateName(QualifierLoc, |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2533 | Param->getDefaultArgument().getArgument().getAsTemplate(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2534 | Param->getDefaultArgument().getTemplateNameLoc(), |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2535 | AllTemplateArgs); |
| 2536 | } |
| 2537 | |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2538 | /// \brief If the given template parameter has a default template |
| 2539 | /// argument, substitute into that default template argument and |
| 2540 | /// return the corresponding template argument. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2541 | TemplateArgumentLoc |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2542 | Sema::SubstDefaultTemplateArgumentIfAvailable(TemplateDecl *Template, |
| 2543 | SourceLocation TemplateLoc, |
| 2544 | SourceLocation RAngleLoc, |
| 2545 | Decl *Param, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2546 | SmallVectorImpl<TemplateArgument> &Converted) { |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2547 | if (TemplateTypeParmDecl *TypeParm = dyn_cast<TemplateTypeParmDecl>(Param)) { |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2548 | if (!TypeParm->hasDefaultArgument()) |
| 2549 | return TemplateArgumentLoc(); |
| 2550 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2551 | TypeSourceInfo *DI = SubstDefaultTemplateArgument(*this, Template, |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2552 | TemplateLoc, |
| 2553 | RAngleLoc, |
| 2554 | TypeParm, |
| 2555 | Converted); |
| 2556 | if (DI) |
| 2557 | return TemplateArgumentLoc(TemplateArgument(DI->getType()), DI); |
| 2558 | |
| 2559 | return TemplateArgumentLoc(); |
| 2560 | } |
| 2561 | |
| 2562 | if (NonTypeTemplateParmDecl *NonTypeParm |
| 2563 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
| 2564 | if (!NonTypeParm->hasDefaultArgument()) |
| 2565 | return TemplateArgumentLoc(); |
| 2566 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2567 | ExprResult Arg = SubstDefaultTemplateArgument(*this, Template, |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2568 | TemplateLoc, |
| 2569 | RAngleLoc, |
| 2570 | NonTypeParm, |
| 2571 | Converted); |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2572 | if (Arg.isInvalid()) |
| 2573 | return TemplateArgumentLoc(); |
| 2574 | |
| 2575 | Expr *ArgE = Arg.takeAs<Expr>(); |
| 2576 | return TemplateArgumentLoc(TemplateArgument(ArgE), ArgE); |
| 2577 | } |
| 2578 | |
| 2579 | TemplateTemplateParmDecl *TempTempParm |
| 2580 | = cast<TemplateTemplateParmDecl>(Param); |
| 2581 | if (!TempTempParm->hasDefaultArgument()) |
| 2582 | return TemplateArgumentLoc(); |
| 2583 | |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2584 | |
Douglas Gregor | 1d752d7 | 2011-03-02 18:46:51 +0000 | [diff] [blame] | 2585 | NestedNameSpecifierLoc QualifierLoc; |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2586 | TemplateName TName = SubstDefaultTemplateArgument(*this, Template, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2587 | TemplateLoc, |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2588 | RAngleLoc, |
| 2589 | TempTempParm, |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2590 | Converted, |
| 2591 | QualifierLoc); |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2592 | if (TName.isNull()) |
| 2593 | return TemplateArgumentLoc(); |
| 2594 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2595 | return TemplateArgumentLoc(TemplateArgument(TName), |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2596 | TempTempParm->getDefaultArgument().getTemplateQualifierLoc(), |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2597 | TempTempParm->getDefaultArgument().getTemplateNameLoc()); |
| 2598 | } |
| 2599 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2600 | /// \brief Check that the given template argument corresponds to the given |
| 2601 | /// template parameter. |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2602 | /// |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2603 | /// \param Param The template parameter against which the argument will be |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2604 | /// checked. |
| 2605 | /// |
| 2606 | /// \param Arg The template argument. |
| 2607 | /// |
| 2608 | /// \param Template The template in which the template argument resides. |
| 2609 | /// |
| 2610 | /// \param TemplateLoc The location of the template name for the template |
| 2611 | /// whose argument list we're matching. |
| 2612 | /// |
| 2613 | /// \param RAngleLoc The location of the right angle bracket ('>') that closes |
| 2614 | /// the template argument list. |
| 2615 | /// |
| 2616 | /// \param ArgumentPackIndex The index into the argument pack where this |
| 2617 | /// argument will be placed. Only valid if the parameter is a parameter pack. |
| 2618 | /// |
| 2619 | /// \param Converted The checked, converted argument will be added to the |
| 2620 | /// end of this small vector. |
| 2621 | /// |
| 2622 | /// \param CTAK Describes how we arrived at this particular template argument: |
| 2623 | /// explicitly written, deduced, etc. |
| 2624 | /// |
| 2625 | /// \returns true on error, false otherwise. |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2626 | bool Sema::CheckTemplateArgument(NamedDecl *Param, |
| 2627 | const TemplateArgumentLoc &Arg, |
Douglas Gregor | 54c53cc | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2628 | NamedDecl *Template, |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2629 | SourceLocation TemplateLoc, |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2630 | SourceLocation RAngleLoc, |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2631 | unsigned ArgumentPackIndex, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2632 | SmallVectorImpl<TemplateArgument> &Converted, |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 2633 | CheckTemplateArgumentKind CTAK) { |
Douglas Gregor | d9e1530 | 2009-11-11 19:41:09 +0000 | [diff] [blame] | 2634 | // Check template type parameters. |
| 2635 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2636 | return CheckTemplateTypeArgument(TTP, Arg, Converted); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2637 | |
Douglas Gregor | d9e1530 | 2009-11-11 19:41:09 +0000 | [diff] [blame] | 2638 | // Check non-type template parameters. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2639 | if (NonTypeTemplateParmDecl *NTTP =dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2640 | // Do substitution on the type of the non-type template parameter |
Peter Collingbourne | 9f6f6a1 | 2010-12-10 17:08:53 +0000 | [diff] [blame] | 2641 | // with the template arguments we've seen thus far. But if the |
| 2642 | // template has a dependent context then we cannot substitute yet. |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2643 | QualType NTTPType = NTTP->getType(); |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2644 | if (NTTP->isParameterPack() && NTTP->isExpandedParameterPack()) |
| 2645 | NTTPType = NTTP->getExpansionType(ArgumentPackIndex); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2646 | |
Peter Collingbourne | 9f6f6a1 | 2010-12-10 17:08:53 +0000 | [diff] [blame] | 2647 | if (NTTPType->isDependentType() && |
| 2648 | !isa<TemplateTemplateParmDecl>(Template) && |
| 2649 | !Template->getDeclContext()->isDependentContext()) { |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2650 | // Do substitution on the type of the non-type template parameter. |
| 2651 | InstantiatingTemplate Inst(*this, TemplateLoc, Template, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2652 | NTTP, Converted.data(), Converted.size(), |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2653 | SourceRange(TemplateLoc, RAngleLoc)); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2654 | |
| 2655 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2656 | Converted.data(), Converted.size()); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2657 | NTTPType = SubstType(NTTPType, |
| 2658 | MultiLevelTemplateArgumentList(TemplateArgs), |
| 2659 | NTTP->getLocation(), |
| 2660 | NTTP->getDeclName()); |
| 2661 | // If that worked, check the non-type template parameter type |
| 2662 | // for validity. |
| 2663 | if (!NTTPType.isNull()) |
| 2664 | NTTPType = CheckNonTypeTemplateParameterType(NTTPType, |
| 2665 | NTTP->getLocation()); |
| 2666 | if (NTTPType.isNull()) |
| 2667 | return true; |
| 2668 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2669 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2670 | switch (Arg.getArgument().getKind()) { |
| 2671 | case TemplateArgument::Null: |
| 2672 | assert(false && "Should never see a NULL template argument here"); |
| 2673 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2674 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2675 | case TemplateArgument::Expression: { |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2676 | TemplateArgument Result; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2677 | ExprResult Res = |
| 2678 | CheckTemplateArgument(NTTP, NTTPType, Arg.getArgument().getAsExpr(), |
| 2679 | Result, CTAK); |
| 2680 | if (Res.isInvalid()) |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2681 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2682 | |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2683 | Converted.push_back(Result); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2684 | break; |
| 2685 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2686 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2687 | case TemplateArgument::Declaration: |
| 2688 | case TemplateArgument::Integral: |
| 2689 | // We've already checked this template argument, so just copy |
| 2690 | // it to the list of converted arguments. |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2691 | Converted.push_back(Arg.getArgument()); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2692 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2693 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2694 | case TemplateArgument::Template: |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 2695 | case TemplateArgument::TemplateExpansion: |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2696 | // We were given a template template argument. It may not be ill-formed; |
| 2697 | // see below. |
| 2698 | if (DependentTemplateName *DTN |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 2699 | = Arg.getArgument().getAsTemplateOrTemplatePattern() |
| 2700 | .getAsDependentTemplateName()) { |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2701 | // We have a template argument such as \c T::template X, which we |
| 2702 | // parsed as a template template argument. However, since we now |
| 2703 | // know that we need a non-type template argument, convert this |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2704 | // template name into an expression. |
| 2705 | |
| 2706 | DeclarationNameInfo NameInfo(DTN->getIdentifier(), |
| 2707 | Arg.getTemplateNameLoc()); |
| 2708 | |
Douglas Gregor | 00cf3cc | 2011-02-25 20:49:16 +0000 | [diff] [blame] | 2709 | CXXScopeSpec SS; |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2710 | SS.Adopt(Arg.getTemplateQualifierLoc()); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2711 | ExprResult E = Owned(DependentScopeDeclRefExpr::Create(Context, |
Douglas Gregor | 00cf3cc | 2011-02-25 20:49:16 +0000 | [diff] [blame] | 2712 | SS.getWithLocInContext(Context), |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2713 | NameInfo)); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2714 | |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 2715 | // If we parsed the template argument as a pack expansion, create a |
| 2716 | // pack expansion expression. |
| 2717 | if (Arg.getArgument().getKind() == TemplateArgument::TemplateExpansion){ |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2718 | E = ActOnPackExpansion(E.take(), Arg.getTemplateEllipsisLoc()); |
| 2719 | if (E.isInvalid()) |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 2720 | return true; |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 2721 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2722 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2723 | TemplateArgument Result; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2724 | E = CheckTemplateArgument(NTTP, NTTPType, E.take(), Result); |
| 2725 | if (E.isInvalid()) |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2726 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2727 | |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2728 | Converted.push_back(Result); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2729 | break; |
| 2730 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2731 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2732 | // We have a template argument that actually does refer to a class |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2733 | // template, alias template, or template template parameter, and |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2734 | // therefore cannot be a non-type template argument. |
| 2735 | Diag(Arg.getLocation(), diag::err_template_arg_must_be_expr) |
| 2736 | << Arg.getSourceRange(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2737 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2738 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 2739 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2740 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2741 | case TemplateArgument::Type: { |
| 2742 | // We have a non-type template parameter but the template |
| 2743 | // argument is a type. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2744 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2745 | // C++ [temp.arg]p2: |
| 2746 | // In a template-argument, an ambiguity between a type-id and |
| 2747 | // an expression is resolved to a type-id, regardless of the |
| 2748 | // form of the corresponding template-parameter. |
| 2749 | // |
| 2750 | // We warn specifically about this case, since it can be rather |
| 2751 | // confusing for users. |
| 2752 | QualType T = Arg.getArgument().getAsType(); |
| 2753 | SourceRange SR = Arg.getSourceRange(); |
| 2754 | if (T->isFunctionType()) |
| 2755 | Diag(SR.getBegin(), diag::err_template_arg_nontype_ambig) << SR << T; |
| 2756 | else |
| 2757 | Diag(SR.getBegin(), diag::err_template_arg_must_be_expr) << SR; |
| 2758 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 2759 | return true; |
| 2760 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2761 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2762 | case TemplateArgument::Pack: |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 2763 | llvm_unreachable("Caller must expand template argument packs"); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2764 | break; |
| 2765 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2766 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2767 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2768 | } |
| 2769 | |
| 2770 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2771 | // Check template template parameters. |
| 2772 | TemplateTemplateParmDecl *TempParm = cast<TemplateTemplateParmDecl>(Param); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2773 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2774 | // Substitute into the template parameter list of the template |
| 2775 | // template parameter, since previously-supplied template arguments |
| 2776 | // may appear within the template template parameter. |
| 2777 | { |
| 2778 | // Set up a template instantiation context. |
| 2779 | LocalInstantiationScope Scope(*this); |
| 2780 | InstantiatingTemplate Inst(*this, TemplateLoc, Template, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2781 | TempParm, Converted.data(), Converted.size(), |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2782 | SourceRange(TemplateLoc, RAngleLoc)); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2783 | |
| 2784 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2785 | Converted.data(), Converted.size()); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2786 | TempParm = cast_or_null<TemplateTemplateParmDecl>( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2787 | SubstDecl(TempParm, CurContext, |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2788 | MultiLevelTemplateArgumentList(TemplateArgs))); |
| 2789 | if (!TempParm) |
| 2790 | return true; |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2791 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2792 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2793 | switch (Arg.getArgument().getKind()) { |
| 2794 | case TemplateArgument::Null: |
| 2795 | assert(false && "Should never see a NULL template argument here"); |
| 2796 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2797 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2798 | case TemplateArgument::Template: |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 2799 | case TemplateArgument::TemplateExpansion: |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2800 | if (CheckTemplateArgument(TempParm, Arg)) |
| 2801 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2802 | |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2803 | Converted.push_back(Arg.getArgument()); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2804 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2805 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2806 | case TemplateArgument::Expression: |
| 2807 | case TemplateArgument::Type: |
| 2808 | // We have a template template parameter but the template |
| 2809 | // argument does not refer to a template. |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2810 | Diag(Arg.getLocation(), diag::err_template_arg_must_be_template) |
| 2811 | << getLangOptions().CPlusPlus0x; |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2812 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2813 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2814 | case TemplateArgument::Declaration: |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 2815 | llvm_unreachable( |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2816 | "Declaration argument with template template parameter"); |
| 2817 | break; |
| 2818 | case TemplateArgument::Integral: |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 2819 | llvm_unreachable( |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2820 | "Integral argument with template template parameter"); |
| 2821 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2822 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2823 | case TemplateArgument::Pack: |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 2824 | llvm_unreachable("Caller must expand template argument packs"); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2825 | break; |
| 2826 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2827 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2828 | return false; |
| 2829 | } |
| 2830 | |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2831 | /// \brief Check that the given template argument list is well-formed |
| 2832 | /// for specializing the given template. |
| 2833 | bool Sema::CheckTemplateArgumentList(TemplateDecl *Template, |
| 2834 | SourceLocation TemplateLoc, |
Douglas Gregor | 6771423 | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 2835 | TemplateArgumentListInfo &TemplateArgs, |
Douglas Gregor | 16134c6 | 2009-07-01 00:28:38 +0000 | [diff] [blame] | 2836 | bool PartialTemplateArgs, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2837 | SmallVectorImpl<TemplateArgument> &Converted) { |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2838 | TemplateParameterList *Params = Template->getTemplateParameters(); |
| 2839 | unsigned NumParams = Params->size(); |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2840 | unsigned NumArgs = TemplateArgs.size(); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2841 | bool Invalid = false; |
| 2842 | |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2843 | SourceLocation RAngleLoc = TemplateArgs.getRAngleLoc(); |
| 2844 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2845 | bool HasParameterPack = |
Anders Carlsson | 0ceffb5 | 2009-06-13 02:08:00 +0000 | [diff] [blame] | 2846 | NumParams > 0 && Params->getParam(NumParams - 1)->isTemplateParameterPack(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2847 | |
Anders Carlsson | 0ceffb5 | 2009-06-13 02:08:00 +0000 | [diff] [blame] | 2848 | if ((NumArgs > NumParams && !HasParameterPack) || |
Douglas Gregor | 16134c6 | 2009-07-01 00:28:38 +0000 | [diff] [blame] | 2849 | (NumArgs < Params->getMinRequiredArguments() && |
| 2850 | !PartialTemplateArgs)) { |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2851 | // FIXME: point at either the first arg beyond what we can handle, |
| 2852 | // or the '>', depending on whether we have too many or too few |
| 2853 | // arguments. |
| 2854 | SourceRange Range; |
| 2855 | if (NumArgs > NumParams) |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2856 | Range = SourceRange(TemplateArgs[NumParams].getLocation(), RAngleLoc); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2857 | Diag(TemplateLoc, diag::err_template_arg_list_different_arity) |
| 2858 | << (NumArgs > NumParams) |
| 2859 | << (isa<ClassTemplateDecl>(Template)? 0 : |
| 2860 | isa<FunctionTemplateDecl>(Template)? 1 : |
| 2861 | isa<TemplateTemplateParmDecl>(Template)? 2 : 3) |
| 2862 | << Template << Range; |
Douglas Gregor | 62cb18d | 2009-02-11 18:16:40 +0000 | [diff] [blame] | 2863 | Diag(Template->getLocation(), diag::note_template_decl_here) |
| 2864 | << Params->getSourceRange(); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2865 | Invalid = true; |
| 2866 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2867 | |
| 2868 | // C++ [temp.arg]p1: |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2869 | // [...] The type and form of each template-argument specified in |
| 2870 | // a template-id shall match the type and form specified for the |
| 2871 | // corresponding parameter declared by the template in its |
| 2872 | // template-parameter-list. |
Douglas Gregor | 6771423 | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 2873 | bool isTemplateTemplateParameter = isa<TemplateTemplateParmDecl>(Template); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2874 | SmallVector<TemplateArgument, 2> ArgumentPack; |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 2875 | TemplateParameterList::iterator Param = Params->begin(), |
| 2876 | ParamEnd = Params->end(); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2877 | unsigned ArgIdx = 0; |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 2878 | LocalInstantiationScope InstScope(*this, true); |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 2879 | while (Param != ParamEnd) { |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2880 | if (ArgIdx < NumArgs) { |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2881 | // If we have an expanded parameter pack, make sure we don't have too |
| 2882 | // many arguments. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2883 | if (NonTypeTemplateParmDecl *NTTP |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2884 | = dyn_cast<NonTypeTemplateParmDecl>(*Param)) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2885 | if (NTTP->isExpandedParameterPack() && |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2886 | ArgumentPack.size() >= NTTP->getNumExpansionTypes()) { |
| 2887 | Diag(TemplateLoc, diag::err_template_arg_list_different_arity) |
| 2888 | << true |
| 2889 | << (isa<ClassTemplateDecl>(Template)? 0 : |
| 2890 | isa<FunctionTemplateDecl>(Template)? 1 : |
| 2891 | isa<TemplateTemplateParmDecl>(Template)? 2 : 3) |
| 2892 | << Template; |
| 2893 | Diag(Template->getLocation(), diag::note_template_decl_here) |
| 2894 | << Params->getSourceRange(); |
| 2895 | return true; |
| 2896 | } |
| 2897 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2898 | |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2899 | // Check the template argument we were given. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2900 | if (CheckTemplateArgument(*Param, TemplateArgs[ArgIdx], Template, |
| 2901 | TemplateLoc, RAngleLoc, |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2902 | ArgumentPack.size(), Converted)) |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2903 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2904 | |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 2905 | if ((*Param)->isTemplateParameterPack()) { |
| 2906 | // The template parameter was a template parameter pack, so take the |
| 2907 | // deduced argument and place it on the argument pack. Note that we |
| 2908 | // stay on the same template parameter so that we can deduce more |
| 2909 | // arguments. |
| 2910 | ArgumentPack.push_back(Converted.back()); |
| 2911 | Converted.pop_back(); |
| 2912 | } else { |
| 2913 | // Move to the next template parameter. |
| 2914 | ++Param; |
| 2915 | } |
| 2916 | ++ArgIdx; |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2917 | continue; |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 2918 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2919 | |
Douglas Gregor | 8735b29 | 2011-06-03 02:59:40 +0000 | [diff] [blame] | 2920 | // If we're checking a partial template argument list, we're done. |
| 2921 | if (PartialTemplateArgs) { |
| 2922 | if ((*Param)->isTemplateParameterPack() && !ArgumentPack.empty()) |
| 2923 | Converted.push_back(TemplateArgument::CreatePackCopy(Context, |
| 2924 | ArgumentPack.data(), |
| 2925 | ArgumentPack.size())); |
| 2926 | |
| 2927 | return Invalid; |
| 2928 | } |
| 2929 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2930 | // If we have a template parameter pack with no more corresponding |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 2931 | // arguments, just break out now and we'll fill in the argument pack below. |
| 2932 | if ((*Param)->isTemplateParameterPack()) |
| 2933 | break; |
Douglas Gregor | f968d83 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 2934 | |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2935 | // We have a default template argument that we will use. |
| 2936 | TemplateArgumentLoc Arg; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2937 | |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2938 | // Retrieve the default template argument from the template |
| 2939 | // parameter. For each kind of template parameter, we substitute the |
| 2940 | // template arguments provided thus far and any "outer" template arguments |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2941 | // (when the template parameter was part of a nested template) into |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2942 | // the default argument. |
| 2943 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*Param)) { |
| 2944 | if (!TTP->hasDefaultArgument()) { |
Douglas Gregor | 8735b29 | 2011-06-03 02:59:40 +0000 | [diff] [blame] | 2945 | assert(Invalid && "Missing default argument"); |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2946 | break; |
| 2947 | } |
| 2948 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2949 | TypeSourceInfo *ArgType = SubstDefaultTemplateArgument(*this, |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2950 | Template, |
| 2951 | TemplateLoc, |
| 2952 | RAngleLoc, |
| 2953 | TTP, |
| 2954 | Converted); |
| 2955 | if (!ArgType) |
| 2956 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2957 | |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2958 | Arg = TemplateArgumentLoc(TemplateArgument(ArgType->getType()), |
| 2959 | ArgType); |
| 2960 | } else if (NonTypeTemplateParmDecl *NTTP |
| 2961 | = dyn_cast<NonTypeTemplateParmDecl>(*Param)) { |
| 2962 | if (!NTTP->hasDefaultArgument()) { |
Douglas Gregor | 8735b29 | 2011-06-03 02:59:40 +0000 | [diff] [blame] | 2963 | assert(Invalid && "Missing default argument"); |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2964 | break; |
| 2965 | } |
| 2966 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2967 | ExprResult E = SubstDefaultTemplateArgument(*this, Template, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2968 | TemplateLoc, |
| 2969 | RAngleLoc, |
| 2970 | NTTP, |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2971 | Converted); |
| 2972 | if (E.isInvalid()) |
| 2973 | return true; |
| 2974 | |
| 2975 | Expr *Ex = E.takeAs<Expr>(); |
| 2976 | Arg = TemplateArgumentLoc(TemplateArgument(Ex), Ex); |
| 2977 | } else { |
| 2978 | TemplateTemplateParmDecl *TempParm |
| 2979 | = cast<TemplateTemplateParmDecl>(*Param); |
| 2980 | |
| 2981 | if (!TempParm->hasDefaultArgument()) { |
Douglas Gregor | 8735b29 | 2011-06-03 02:59:40 +0000 | [diff] [blame] | 2982 | assert(Invalid && "Missing default argument"); |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2983 | break; |
| 2984 | } |
| 2985 | |
Douglas Gregor | 1d752d7 | 2011-03-02 18:46:51 +0000 | [diff] [blame] | 2986 | NestedNameSpecifierLoc QualifierLoc; |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2987 | TemplateName Name = SubstDefaultTemplateArgument(*this, Template, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2988 | TemplateLoc, |
| 2989 | RAngleLoc, |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2990 | TempParm, |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2991 | Converted, |
| 2992 | QualifierLoc); |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2993 | if (Name.isNull()) |
| 2994 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2995 | |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2996 | Arg = TemplateArgumentLoc(TemplateArgument(Name), QualifierLoc, |
| 2997 | TempParm->getDefaultArgument().getTemplateNameLoc()); |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2998 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2999 | |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3000 | // Introduce an instantiation record that describes where we are using |
| 3001 | // the default template argument. |
| 3002 | InstantiatingTemplate Instantiating(*this, RAngleLoc, Template, *Param, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 3003 | Converted.data(), Converted.size(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3004 | SourceRange(TemplateLoc, RAngleLoc)); |
| 3005 | |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3006 | // Check the default template argument. |
Douglas Gregor | d9e1530 | 2009-11-11 19:41:09 +0000 | [diff] [blame] | 3007 | if (CheckTemplateArgument(*Param, Arg, Template, TemplateLoc, |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 3008 | RAngleLoc, 0, Converted)) |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3009 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3010 | |
Douglas Gregor | 6771423 | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 3011 | // Core issue 150 (assumed resolution): if this is a template template |
| 3012 | // parameter, keep track of the default template arguments from the |
| 3013 | // template definition. |
| 3014 | if (isTemplateTemplateParameter) |
| 3015 | TemplateArgs.addArgument(Arg); |
| 3016 | |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 3017 | // Move to the next template parameter and argument. |
| 3018 | ++Param; |
| 3019 | ++ArgIdx; |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3020 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3021 | |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 3022 | // Form argument packs for each of the parameter packs remaining. |
| 3023 | while (Param != ParamEnd) { |
Douglas Gregor | d373119 | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 3024 | // If we're checking a partial list of template arguments, don't fill |
| 3025 | // in arguments for non-template parameter packs. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3026 | |
| 3027 | if ((*Param)->isTemplateParameterPack()) { |
Douglas Gregor | 8735b29 | 2011-06-03 02:59:40 +0000 | [diff] [blame] | 3028 | if (ArgumentPack.empty()) |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 3029 | Converted.push_back(TemplateArgument(0, 0)); |
Douglas Gregor | 203e6a3 | 2011-01-11 23:09:57 +0000 | [diff] [blame] | 3030 | else { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3031 | Converted.push_back(TemplateArgument::CreatePackCopy(Context, |
| 3032 | ArgumentPack.data(), |
Douglas Gregor | 203e6a3 | 2011-01-11 23:09:57 +0000 | [diff] [blame] | 3033 | ArgumentPack.size())); |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 3034 | ArgumentPack.clear(); |
| 3035 | } |
| 3036 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3037 | |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 3038 | ++Param; |
| 3039 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3040 | |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3041 | return Invalid; |
| 3042 | } |
| 3043 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3044 | namespace { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3045 | class UnnamedLocalNoLinkageFinder |
| 3046 | : public TypeVisitor<UnnamedLocalNoLinkageFinder, bool> |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3047 | { |
| 3048 | Sema &S; |
| 3049 | SourceRange SR; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3050 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3051 | typedef TypeVisitor<UnnamedLocalNoLinkageFinder, bool> inherited; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3052 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3053 | public: |
| 3054 | UnnamedLocalNoLinkageFinder(Sema &S, SourceRange SR) : S(S), SR(SR) { } |
| 3055 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3056 | bool Visit(QualType T) { |
| 3057 | return inherited::Visit(T.getTypePtr()); |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3058 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3059 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3060 | #define TYPE(Class, Parent) \ |
| 3061 | bool Visit##Class##Type(const Class##Type *); |
| 3062 | #define ABSTRACT_TYPE(Class, Parent) \ |
| 3063 | bool Visit##Class##Type(const Class##Type *) { return false; } |
| 3064 | #define NON_CANONICAL_TYPE(Class, Parent) \ |
| 3065 | bool Visit##Class##Type(const Class##Type *) { return false; } |
| 3066 | #include "clang/AST/TypeNodes.def" |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3067 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3068 | bool VisitTagDecl(const TagDecl *Tag); |
| 3069 | bool VisitNestedNameSpecifier(NestedNameSpecifier *NNS); |
| 3070 | }; |
| 3071 | } |
| 3072 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3073 | bool UnnamedLocalNoLinkageFinder::VisitBuiltinType(const BuiltinType*) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3074 | return false; |
| 3075 | } |
| 3076 | |
| 3077 | bool UnnamedLocalNoLinkageFinder::VisitComplexType(const ComplexType* T) { |
| 3078 | return Visit(T->getElementType()); |
| 3079 | } |
| 3080 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3081 | bool UnnamedLocalNoLinkageFinder::VisitPointerType(const PointerType* T) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3082 | return Visit(T->getPointeeType()); |
| 3083 | } |
| 3084 | |
| 3085 | bool UnnamedLocalNoLinkageFinder::VisitBlockPointerType( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3086 | const BlockPointerType* T) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3087 | return Visit(T->getPointeeType()); |
| 3088 | } |
| 3089 | |
| 3090 | bool UnnamedLocalNoLinkageFinder::VisitLValueReferenceType( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3091 | const LValueReferenceType* T) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3092 | return Visit(T->getPointeeType()); |
| 3093 | } |
| 3094 | |
| 3095 | bool UnnamedLocalNoLinkageFinder::VisitRValueReferenceType( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3096 | const RValueReferenceType* T) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3097 | return Visit(T->getPointeeType()); |
| 3098 | } |
| 3099 | |
| 3100 | bool UnnamedLocalNoLinkageFinder::VisitMemberPointerType( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3101 | const MemberPointerType* T) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3102 | return Visit(T->getPointeeType()) || Visit(QualType(T->getClass(), 0)); |
| 3103 | } |
| 3104 | |
| 3105 | bool UnnamedLocalNoLinkageFinder::VisitConstantArrayType( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3106 | const ConstantArrayType* T) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3107 | return Visit(T->getElementType()); |
| 3108 | } |
| 3109 | |
| 3110 | bool UnnamedLocalNoLinkageFinder::VisitIncompleteArrayType( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3111 | const IncompleteArrayType* T) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3112 | return Visit(T->getElementType()); |
| 3113 | } |
| 3114 | |
| 3115 | bool UnnamedLocalNoLinkageFinder::VisitVariableArrayType( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3116 | const VariableArrayType* T) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3117 | return Visit(T->getElementType()); |
| 3118 | } |
| 3119 | |
| 3120 | bool UnnamedLocalNoLinkageFinder::VisitDependentSizedArrayType( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3121 | const DependentSizedArrayType* T) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3122 | return Visit(T->getElementType()); |
| 3123 | } |
| 3124 | |
| 3125 | bool UnnamedLocalNoLinkageFinder::VisitDependentSizedExtVectorType( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3126 | const DependentSizedExtVectorType* T) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3127 | return Visit(T->getElementType()); |
| 3128 | } |
| 3129 | |
| 3130 | bool UnnamedLocalNoLinkageFinder::VisitVectorType(const VectorType* T) { |
| 3131 | return Visit(T->getElementType()); |
| 3132 | } |
| 3133 | |
| 3134 | bool UnnamedLocalNoLinkageFinder::VisitExtVectorType(const ExtVectorType* T) { |
| 3135 | return Visit(T->getElementType()); |
| 3136 | } |
| 3137 | |
| 3138 | bool UnnamedLocalNoLinkageFinder::VisitFunctionProtoType( |
| 3139 | const FunctionProtoType* T) { |
| 3140 | for (FunctionProtoType::arg_type_iterator A = T->arg_type_begin(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3141 | AEnd = T->arg_type_end(); |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3142 | A != AEnd; ++A) { |
| 3143 | if (Visit(*A)) |
| 3144 | return true; |
| 3145 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3146 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3147 | return Visit(T->getResultType()); |
| 3148 | } |
| 3149 | |
| 3150 | bool UnnamedLocalNoLinkageFinder::VisitFunctionNoProtoType( |
| 3151 | const FunctionNoProtoType* T) { |
| 3152 | return Visit(T->getResultType()); |
| 3153 | } |
| 3154 | |
| 3155 | bool UnnamedLocalNoLinkageFinder::VisitUnresolvedUsingType( |
| 3156 | const UnresolvedUsingType*) { |
| 3157 | return false; |
| 3158 | } |
| 3159 | |
| 3160 | bool UnnamedLocalNoLinkageFinder::VisitTypeOfExprType(const TypeOfExprType*) { |
| 3161 | return false; |
| 3162 | } |
| 3163 | |
| 3164 | bool UnnamedLocalNoLinkageFinder::VisitTypeOfType(const TypeOfType* T) { |
| 3165 | return Visit(T->getUnderlyingType()); |
| 3166 | } |
| 3167 | |
| 3168 | bool UnnamedLocalNoLinkageFinder::VisitDecltypeType(const DecltypeType*) { |
| 3169 | return false; |
| 3170 | } |
| 3171 | |
Sean Hunt | ca63c20 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 3172 | bool UnnamedLocalNoLinkageFinder::VisitUnaryTransformType( |
| 3173 | const UnaryTransformType*) { |
| 3174 | return false; |
| 3175 | } |
| 3176 | |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 3177 | bool UnnamedLocalNoLinkageFinder::VisitAutoType(const AutoType *T) { |
| 3178 | return Visit(T->getDeducedType()); |
| 3179 | } |
| 3180 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3181 | bool UnnamedLocalNoLinkageFinder::VisitRecordType(const RecordType* T) { |
| 3182 | return VisitTagDecl(T->getDecl()); |
| 3183 | } |
| 3184 | |
| 3185 | bool UnnamedLocalNoLinkageFinder::VisitEnumType(const EnumType* T) { |
| 3186 | return VisitTagDecl(T->getDecl()); |
| 3187 | } |
| 3188 | |
| 3189 | bool UnnamedLocalNoLinkageFinder::VisitTemplateTypeParmType( |
| 3190 | const TemplateTypeParmType*) { |
| 3191 | return false; |
| 3192 | } |
| 3193 | |
Douglas Gregor | c3069d6 | 2011-01-14 02:55:32 +0000 | [diff] [blame] | 3194 | bool UnnamedLocalNoLinkageFinder::VisitSubstTemplateTypeParmPackType( |
| 3195 | const SubstTemplateTypeParmPackType *) { |
| 3196 | return false; |
| 3197 | } |
| 3198 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3199 | bool UnnamedLocalNoLinkageFinder::VisitTemplateSpecializationType( |
| 3200 | const TemplateSpecializationType*) { |
| 3201 | return false; |
| 3202 | } |
| 3203 | |
| 3204 | bool UnnamedLocalNoLinkageFinder::VisitInjectedClassNameType( |
| 3205 | const InjectedClassNameType* T) { |
| 3206 | return VisitTagDecl(T->getDecl()); |
| 3207 | } |
| 3208 | |
| 3209 | bool UnnamedLocalNoLinkageFinder::VisitDependentNameType( |
| 3210 | const DependentNameType* T) { |
| 3211 | return VisitNestedNameSpecifier(T->getQualifier()); |
| 3212 | } |
| 3213 | |
| 3214 | bool UnnamedLocalNoLinkageFinder::VisitDependentTemplateSpecializationType( |
| 3215 | const DependentTemplateSpecializationType* T) { |
| 3216 | return VisitNestedNameSpecifier(T->getQualifier()); |
| 3217 | } |
| 3218 | |
Douglas Gregor | 7536dd5 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 3219 | bool UnnamedLocalNoLinkageFinder::VisitPackExpansionType( |
| 3220 | const PackExpansionType* T) { |
| 3221 | return Visit(T->getPattern()); |
| 3222 | } |
| 3223 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3224 | bool UnnamedLocalNoLinkageFinder::VisitObjCObjectType(const ObjCObjectType *) { |
| 3225 | return false; |
| 3226 | } |
| 3227 | |
| 3228 | bool UnnamedLocalNoLinkageFinder::VisitObjCInterfaceType( |
| 3229 | const ObjCInterfaceType *) { |
| 3230 | return false; |
| 3231 | } |
| 3232 | |
| 3233 | bool UnnamedLocalNoLinkageFinder::VisitObjCObjectPointerType( |
| 3234 | const ObjCObjectPointerType *) { |
| 3235 | return false; |
| 3236 | } |
| 3237 | |
| 3238 | bool UnnamedLocalNoLinkageFinder::VisitTagDecl(const TagDecl *Tag) { |
| 3239 | if (Tag->getDeclContext()->isFunctionOrMethod()) { |
| 3240 | S.Diag(SR.getBegin(), diag::ext_template_arg_local_type) |
| 3241 | << S.Context.getTypeDeclType(Tag) << SR; |
| 3242 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3243 | } |
| 3244 | |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 3245 | if (!Tag->getDeclName() && !Tag->getTypedefNameForAnonDecl()) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3246 | S.Diag(SR.getBegin(), diag::ext_template_arg_unnamed_type) << SR; |
| 3247 | S.Diag(Tag->getLocation(), diag::note_template_unnamed_type_here); |
| 3248 | return true; |
| 3249 | } |
| 3250 | |
| 3251 | return false; |
| 3252 | } |
| 3253 | |
| 3254 | bool UnnamedLocalNoLinkageFinder::VisitNestedNameSpecifier( |
| 3255 | NestedNameSpecifier *NNS) { |
| 3256 | if (NNS->getPrefix() && VisitNestedNameSpecifier(NNS->getPrefix())) |
| 3257 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3258 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3259 | switch (NNS->getKind()) { |
| 3260 | case NestedNameSpecifier::Identifier: |
| 3261 | case NestedNameSpecifier::Namespace: |
Douglas Gregor | 14aba76 | 2011-02-24 02:36:08 +0000 | [diff] [blame] | 3262 | case NestedNameSpecifier::NamespaceAlias: |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3263 | case NestedNameSpecifier::Global: |
| 3264 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3265 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3266 | case NestedNameSpecifier::TypeSpec: |
| 3267 | case NestedNameSpecifier::TypeSpecWithTemplate: |
| 3268 | return Visit(QualType(NNS->getAsType(), 0)); |
| 3269 | } |
Fariborz Jahanian | 7b1ec6c | 2010-10-13 16:19:16 +0000 | [diff] [blame] | 3270 | return false; |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3271 | } |
| 3272 | |
| 3273 | |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3274 | /// \brief Check a template argument against its corresponding |
| 3275 | /// template type parameter. |
| 3276 | /// |
| 3277 | /// This routine implements the semantics of C++ [temp.arg.type]. It |
| 3278 | /// returns true if an error occurred, and false otherwise. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3279 | bool Sema::CheckTemplateArgument(TemplateTypeParmDecl *Param, |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3280 | TypeSourceInfo *ArgInfo) { |
| 3281 | assert(ArgInfo && "invalid TypeSourceInfo"); |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 3282 | QualType Arg = ArgInfo->getType(); |
Douglas Gregor | 0fddb97 | 2010-05-22 16:17:30 +0000 | [diff] [blame] | 3283 | SourceRange SR = ArgInfo->getTypeLoc().getSourceRange(); |
Chandler Carruth | 17fb855 | 2010-09-03 21:12:34 +0000 | [diff] [blame] | 3284 | |
| 3285 | if (Arg->isVariablyModifiedType()) { |
| 3286 | return Diag(SR.getBegin(), diag::err_variably_modified_template_arg) << Arg; |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 3287 | } else if (Context.hasSameUnqualifiedType(Arg, Context.OverloadTy)) { |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 3288 | return Diag(SR.getBegin(), diag::err_template_arg_overload_type) << SR; |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3289 | } |
| 3290 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3291 | // C++03 [temp.arg.type]p2: |
| 3292 | // A local type, a type with no linkage, an unnamed type or a type |
| 3293 | // compounded from any of these types shall not be used as a |
| 3294 | // template-argument for a template type-parameter. |
| 3295 | // |
| 3296 | // C++0x allows these, and even in C++03 we allow them as an extension with |
| 3297 | // a warning. |
Douglas Gregor | db4d4bb | 2010-10-13 18:05:20 +0000 | [diff] [blame] | 3298 | if (!LangOpts.CPlusPlus0x && Arg->hasUnnamedOrLocalType()) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3299 | UnnamedLocalNoLinkageFinder Finder(*this, SR); |
| 3300 | (void)Finder.Visit(Context.getCanonicalType(Arg)); |
| 3301 | } |
| 3302 | |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3303 | return false; |
| 3304 | } |
| 3305 | |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3306 | /// \brief Checks whether the given template argument is the address |
| 3307 | /// of an object or function according to C++ [temp.arg.nontype]p1. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3308 | static bool |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3309 | CheckTemplateArgumentAddressOfObjectOrFunction(Sema &S, |
| 3310 | NonTypeTemplateParmDecl *Param, |
| 3311 | QualType ParamType, |
| 3312 | Expr *ArgIn, |
| 3313 | TemplateArgument &Converted) { |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3314 | bool Invalid = false; |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3315 | Expr *Arg = ArgIn; |
| 3316 | QualType ArgType = Arg->getType(); |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3317 | |
| 3318 | // See through any implicit casts we added to fix the type. |
John McCall | 91a5755 | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 3319 | Arg = Arg->IgnoreImpCasts(); |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3320 | |
| 3321 | // C++ [temp.arg.nontype]p1: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3322 | // |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3323 | // A template-argument for a non-type, non-template |
| 3324 | // template-parameter shall be one of: [...] |
| 3325 | // |
| 3326 | // -- the address of an object or function with external |
| 3327 | // linkage, including function templates and function |
| 3328 | // template-ids but excluding non-static class members, |
| 3329 | // expressed as & id-expression where the & is optional if |
| 3330 | // the name refers to a function or array, or if the |
| 3331 | // corresponding template-parameter is a reference; or |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3332 | |
Abramo Bagnara | 2c5399f | 2010-09-13 06:06:58 +0000 | [diff] [blame] | 3333 | // In C++98/03 mode, give an extension warning on any extra parentheses. |
| 3334 | // See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#773 |
| 3335 | bool ExtraParens = false; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3336 | while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) { |
Abramo Bagnara | 2c5399f | 2010-09-13 06:06:58 +0000 | [diff] [blame] | 3337 | if (!Invalid && !ExtraParens && !S.getLangOptions().CPlusPlus0x) { |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3338 | S.Diag(Arg->getSourceRange().getBegin(), |
Abramo Bagnara | 2c5399f | 2010-09-13 06:06:58 +0000 | [diff] [blame] | 3339 | diag::ext_template_arg_extra_parens) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3340 | << Arg->getSourceRange(); |
Abramo Bagnara | 2c5399f | 2010-09-13 06:06:58 +0000 | [diff] [blame] | 3341 | ExtraParens = true; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3342 | } |
| 3343 | |
| 3344 | Arg = Parens->getSubExpr(); |
| 3345 | } |
| 3346 | |
John McCall | 91a5755 | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 3347 | while (SubstNonTypeTemplateParmExpr *subst = |
| 3348 | dyn_cast<SubstNonTypeTemplateParmExpr>(Arg)) |
| 3349 | Arg = subst->getReplacement()->IgnoreImpCasts(); |
| 3350 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3351 | bool AddressTaken = false; |
| 3352 | SourceLocation AddrOpLoc; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3353 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3354 | if (UnOp->getOpcode() == UO_AddrOf) { |
John McCall | 91a5755 | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 3355 | Arg = UnOp->getSubExpr(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3356 | AddressTaken = true; |
| 3357 | AddrOpLoc = UnOp->getOperatorLoc(); |
| 3358 | } |
Francois Pichet | a343a41 | 2011-04-29 09:08:14 +0000 | [diff] [blame] | 3359 | } |
John McCall | 91a5755 | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 3360 | |
| 3361 | if (S.getLangOptions().Microsoft && isa<CXXUuidofExpr>(Arg)) { |
| 3362 | Converted = TemplateArgument(ArgIn); |
| 3363 | return false; |
| 3364 | } |
| 3365 | |
| 3366 | while (SubstNonTypeTemplateParmExpr *subst = |
| 3367 | dyn_cast<SubstNonTypeTemplateParmExpr>(Arg)) |
| 3368 | Arg = subst->getReplacement()->IgnoreImpCasts(); |
| 3369 | |
| 3370 | DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Arg); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3371 | if (!DRE) { |
Douglas Gregor | 1a8cf73 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 3372 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_decl_ref) |
| 3373 | << Arg->getSourceRange(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3374 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3375 | return true; |
| 3376 | } |
Chandler Carruth | 038cc39 | 2010-01-31 10:01:20 +0000 | [diff] [blame] | 3377 | |
| 3378 | // Stop checking the precise nature of the argument if it is value dependent, |
| 3379 | // it should be checked when instantiated. |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3380 | if (Arg->isValueDependent()) { |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 3381 | Converted = TemplateArgument(ArgIn); |
Chandler Carruth | 038cc39 | 2010-01-31 10:01:20 +0000 | [diff] [blame] | 3382 | return false; |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3383 | } |
Chandler Carruth | 038cc39 | 2010-01-31 10:01:20 +0000 | [diff] [blame] | 3384 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3385 | if (!isa<ValueDecl>(DRE->getDecl())) { |
| 3386 | S.Diag(Arg->getSourceRange().getBegin(), |
| 3387 | diag::err_template_arg_not_object_or_func_form) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3388 | << Arg->getSourceRange(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3389 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3390 | return true; |
| 3391 | } |
| 3392 | |
| 3393 | NamedDecl *Entity = 0; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3394 | |
| 3395 | // Cannot refer to non-static data members |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3396 | if (FieldDecl *Field = dyn_cast<FieldDecl>(DRE->getDecl())) { |
| 3397 | S.Diag(Arg->getSourceRange().getBegin(), diag::err_template_arg_field) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3398 | << Field << Arg->getSourceRange(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3399 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3400 | return true; |
| 3401 | } |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3402 | |
| 3403 | // Cannot refer to non-static member functions |
| 3404 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(DRE->getDecl())) |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3405 | if (!Method->isStatic()) { |
| 3406 | S.Diag(Arg->getSourceRange().getBegin(), diag::err_template_arg_method) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3407 | << Method << Arg->getSourceRange(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3408 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3409 | return true; |
| 3410 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3411 | |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3412 | // Functions must have external linkage. |
| 3413 | if (FunctionDecl *Func = dyn_cast<FunctionDecl>(DRE->getDecl())) { |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 3414 | if (!isExternalLinkage(Func->getLinkage())) { |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3415 | S.Diag(Arg->getSourceRange().getBegin(), |
| 3416 | diag::err_template_arg_function_not_extern) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3417 | << Func << Arg->getSourceRange(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3418 | S.Diag(Func->getLocation(), diag::note_template_arg_internal_object) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3419 | << true; |
| 3420 | return true; |
| 3421 | } |
| 3422 | |
| 3423 | // Okay: we've named a function with external linkage. |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 3424 | Entity = Func; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3425 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3426 | // If the template parameter has pointer type, the function decays. |
| 3427 | if (ParamType->isPointerType() && !AddressTaken) |
| 3428 | ArgType = S.Context.getPointerType(Func->getType()); |
| 3429 | else if (AddressTaken && ParamType->isReferenceType()) { |
| 3430 | // If we originally had an address-of operator, but the |
| 3431 | // parameter has reference type, complain and (if things look |
| 3432 | // like they will work) drop the address-of operator. |
| 3433 | if (!S.Context.hasSameUnqualifiedType(Func->getType(), |
| 3434 | ParamType.getNonReferenceType())) { |
| 3435 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 3436 | << ParamType; |
| 3437 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3438 | return true; |
| 3439 | } |
| 3440 | |
| 3441 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 3442 | << ParamType |
| 3443 | << FixItHint::CreateRemoval(AddrOpLoc); |
| 3444 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3445 | |
| 3446 | ArgType = Func->getType(); |
| 3447 | } |
| 3448 | } else if (VarDecl *Var = dyn_cast<VarDecl>(DRE->getDecl())) { |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 3449 | if (!isExternalLinkage(Var->getLinkage())) { |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3450 | S.Diag(Arg->getSourceRange().getBegin(), |
| 3451 | diag::err_template_arg_object_not_extern) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3452 | << Var << Arg->getSourceRange(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3453 | S.Diag(Var->getLocation(), diag::note_template_arg_internal_object) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3454 | << true; |
| 3455 | return true; |
| 3456 | } |
| 3457 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3458 | // A value of reference type is not an object. |
| 3459 | if (Var->getType()->isReferenceType()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3460 | S.Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3461 | diag::err_template_arg_reference_var) |
| 3462 | << Var->getType() << Arg->getSourceRange(); |
| 3463 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3464 | return true; |
| 3465 | } |
| 3466 | |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3467 | // Okay: we've named an object with external linkage |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 3468 | Entity = Var; |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3469 | |
| 3470 | // If the template parameter has pointer type, we must have taken |
| 3471 | // the address of this object. |
| 3472 | if (ParamType->isReferenceType()) { |
| 3473 | if (AddressTaken) { |
| 3474 | // If we originally had an address-of operator, but the |
| 3475 | // parameter has reference type, complain and (if things look |
| 3476 | // like they will work) drop the address-of operator. |
| 3477 | if (!S.Context.hasSameUnqualifiedType(Var->getType(), |
| 3478 | ParamType.getNonReferenceType())) { |
| 3479 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 3480 | << ParamType; |
| 3481 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3482 | return true; |
| 3483 | } |
| 3484 | |
| 3485 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 3486 | << ParamType |
| 3487 | << FixItHint::CreateRemoval(AddrOpLoc); |
| 3488 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3489 | |
| 3490 | ArgType = Var->getType(); |
| 3491 | } |
| 3492 | } else if (!AddressTaken && ParamType->isPointerType()) { |
| 3493 | if (Var->getType()->isArrayType()) { |
| 3494 | // Array-to-pointer decay. |
| 3495 | ArgType = S.Context.getArrayDecayedType(Var->getType()); |
| 3496 | } else { |
| 3497 | // If the template parameter has pointer type but the address of |
| 3498 | // this object was not taken, complain and (possibly) recover by |
| 3499 | // taking the address of the entity. |
| 3500 | ArgType = S.Context.getPointerType(Var->getType()); |
| 3501 | if (!S.Context.hasSameUnqualifiedType(ArgType, ParamType)) { |
| 3502 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_address_of) |
| 3503 | << ParamType; |
| 3504 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3505 | return true; |
| 3506 | } |
| 3507 | |
| 3508 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_address_of) |
| 3509 | << ParamType |
| 3510 | << FixItHint::CreateInsertion(Arg->getLocStart(), "&"); |
| 3511 | |
| 3512 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3513 | } |
| 3514 | } |
| 3515 | } else { |
| 3516 | // We found something else, but we don't know specifically what it is. |
| 3517 | S.Diag(Arg->getSourceRange().getBegin(), |
| 3518 | diag::err_template_arg_not_object_or_func) |
| 3519 | << Arg->getSourceRange(); |
| 3520 | S.Diag(DRE->getDecl()->getLocation(), diag::note_template_arg_refers_here); |
| 3521 | return true; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3522 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3523 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3524 | bool ObjCLifetimeConversion; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3525 | if (ParamType->isPointerType() && |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3526 | !ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType() && |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3527 | S.IsQualificationConversion(ArgType, ParamType, false, |
| 3528 | ObjCLifetimeConversion)) { |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3529 | // For pointer-to-object types, qualification conversions are |
| 3530 | // permitted. |
| 3531 | } else { |
| 3532 | if (const ReferenceType *ParamRef = ParamType->getAs<ReferenceType>()) { |
| 3533 | if (!ParamRef->getPointeeType()->isFunctionType()) { |
| 3534 | // C++ [temp.arg.nontype]p5b3: |
| 3535 | // For a non-type template-parameter of type reference to |
| 3536 | // object, no conversions apply. The type referred to by the |
| 3537 | // reference may be more cv-qualified than the (otherwise |
| 3538 | // identical) type of the template- argument. The |
| 3539 | // template-parameter is bound directly to the |
| 3540 | // template-argument, which shall be an lvalue. |
| 3541 | |
| 3542 | // FIXME: Other qualifiers? |
| 3543 | unsigned ParamQuals = ParamRef->getPointeeType().getCVRQualifiers(); |
| 3544 | unsigned ArgQuals = ArgType.getCVRQualifiers(); |
| 3545 | |
| 3546 | if ((ParamQuals | ArgQuals) != ParamQuals) { |
| 3547 | S.Diag(Arg->getSourceRange().getBegin(), |
| 3548 | diag::err_template_arg_ref_bind_ignores_quals) |
| 3549 | << ParamType << Arg->getType() |
| 3550 | << Arg->getSourceRange(); |
| 3551 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3552 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3553 | } |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3554 | } |
| 3555 | } |
| 3556 | |
| 3557 | // At this point, the template argument refers to an object or |
| 3558 | // function with external linkage. We now need to check whether the |
| 3559 | // argument and parameter types are compatible. |
| 3560 | if (!S.Context.hasSameUnqualifiedType(ArgType, |
| 3561 | ParamType.getNonReferenceType())) { |
| 3562 | // We can't perform this conversion or binding. |
| 3563 | if (ParamType->isReferenceType()) |
| 3564 | S.Diag(Arg->getLocStart(), diag::err_template_arg_no_ref_bind) |
John McCall | 91a5755 | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 3565 | << ParamType << ArgIn->getType() << Arg->getSourceRange(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3566 | else |
| 3567 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_convertible) |
John McCall | 91a5755 | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 3568 | << ArgIn->getType() << ParamType << Arg->getSourceRange(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3569 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3570 | return true; |
| 3571 | } |
| 3572 | } |
| 3573 | |
| 3574 | // Create the template argument. |
| 3575 | Converted = TemplateArgument(Entity->getCanonicalDecl()); |
Douglas Gregor | 77c13e0 | 2010-04-24 18:20:53 +0000 | [diff] [blame] | 3576 | S.MarkDeclarationReferenced(Arg->getLocStart(), Entity); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3577 | return false; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3578 | } |
| 3579 | |
| 3580 | /// \brief Checks whether the given template argument is a pointer to |
| 3581 | /// member constant according to C++ [temp.arg.nontype]p1. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3582 | bool Sema::CheckTemplateArgumentPointerToMember(Expr *Arg, |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 3583 | TemplateArgument &Converted) { |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3584 | bool Invalid = false; |
| 3585 | |
| 3586 | // See through any implicit casts we added to fix the type. |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 3587 | while (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(Arg)) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3588 | Arg = Cast->getSubExpr(); |
| 3589 | |
| 3590 | // C++ [temp.arg.nontype]p1: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3591 | // |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3592 | // A template-argument for a non-type, non-template |
| 3593 | // template-parameter shall be one of: [...] |
| 3594 | // |
| 3595 | // -- a pointer to member expressed as described in 5.3.1. |
Douglas Gregor | a2813ce | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 3596 | DeclRefExpr *DRE = 0; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3597 | |
Abramo Bagnara | 2c5399f | 2010-09-13 06:06:58 +0000 | [diff] [blame] | 3598 | // In C++98/03 mode, give an extension warning on any extra parentheses. |
| 3599 | // See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#773 |
| 3600 | bool ExtraParens = false; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3601 | while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) { |
Abramo Bagnara | 2c5399f | 2010-09-13 06:06:58 +0000 | [diff] [blame] | 3602 | if (!Invalid && !ExtraParens && !getLangOptions().CPlusPlus0x) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3603 | Diag(Arg->getSourceRange().getBegin(), |
Abramo Bagnara | 2c5399f | 2010-09-13 06:06:58 +0000 | [diff] [blame] | 3604 | diag::ext_template_arg_extra_parens) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3605 | << Arg->getSourceRange(); |
Abramo Bagnara | 2c5399f | 2010-09-13 06:06:58 +0000 | [diff] [blame] | 3606 | ExtraParens = true; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3607 | } |
| 3608 | |
| 3609 | Arg = Parens->getSubExpr(); |
| 3610 | } |
| 3611 | |
John McCall | 91a5755 | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 3612 | while (SubstNonTypeTemplateParmExpr *subst = |
| 3613 | dyn_cast<SubstNonTypeTemplateParmExpr>(Arg)) |
| 3614 | Arg = subst->getReplacement()->IgnoreImpCasts(); |
| 3615 | |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 3616 | // A pointer-to-member constant written &Class::member. |
| 3617 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3618 | if (UnOp->getOpcode() == UO_AddrOf) { |
Douglas Gregor | a2813ce | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 3619 | DRE = dyn_cast<DeclRefExpr>(UnOp->getSubExpr()); |
| 3620 | if (DRE && !DRE->getQualifier()) |
| 3621 | DRE = 0; |
| 3622 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3623 | } |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 3624 | // A constant of pointer-to-member type. |
| 3625 | else if ((DRE = dyn_cast<DeclRefExpr>(Arg))) { |
| 3626 | if (ValueDecl *VD = dyn_cast<ValueDecl>(DRE->getDecl())) { |
| 3627 | if (VD->getType()->isMemberPointerType()) { |
| 3628 | if (isa<NonTypeTemplateParmDecl>(VD) || |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3629 | (isa<VarDecl>(VD) && |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 3630 | Context.getCanonicalType(VD->getType()).isConstQualified())) { |
| 3631 | if (Arg->isTypeDependent() || Arg->isValueDependent()) |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 3632 | Converted = TemplateArgument(Arg); |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 3633 | else |
| 3634 | Converted = TemplateArgument(VD->getCanonicalDecl()); |
| 3635 | return Invalid; |
| 3636 | } |
| 3637 | } |
| 3638 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3639 | |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 3640 | DRE = 0; |
| 3641 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3642 | |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3643 | if (!DRE) |
| 3644 | return Diag(Arg->getSourceRange().getBegin(), |
| 3645 | diag::err_template_arg_not_pointer_to_member_form) |
| 3646 | << Arg->getSourceRange(); |
| 3647 | |
| 3648 | if (isa<FieldDecl>(DRE->getDecl()) || isa<CXXMethodDecl>(DRE->getDecl())) { |
| 3649 | assert((isa<FieldDecl>(DRE->getDecl()) || |
| 3650 | !cast<CXXMethodDecl>(DRE->getDecl())->isStatic()) && |
| 3651 | "Only non-static member pointers can make it here"); |
| 3652 | |
| 3653 | // Okay: this is the address of a non-static member, and therefore |
| 3654 | // a member pointer constant. |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 3655 | if (Arg->isTypeDependent() || Arg->isValueDependent()) |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 3656 | Converted = TemplateArgument(Arg); |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 3657 | else |
| 3658 | Converted = TemplateArgument(DRE->getDecl()->getCanonicalDecl()); |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3659 | return Invalid; |
| 3660 | } |
| 3661 | |
| 3662 | // 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] | 3663 | Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3664 | diag::err_template_arg_not_pointer_to_member_form) |
| 3665 | << Arg->getSourceRange(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3666 | Diag(DRE->getDecl()->getLocation(), |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3667 | diag::note_template_arg_refers_here); |
| 3668 | return true; |
| 3669 | } |
| 3670 | |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3671 | /// \brief Check a template argument against its corresponding |
| 3672 | /// non-type template parameter. |
| 3673 | /// |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 3674 | /// This routine implements the semantics of C++ [temp.arg.nontype]. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3675 | /// If an error occurred, it returns ExprError(); otherwise, it |
| 3676 | /// returns the converted template argument. \p |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 3677 | /// InstantiatedParamType is the type of the non-type template |
| 3678 | /// parameter after it has been instantiated. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3679 | ExprResult Sema::CheckTemplateArgument(NonTypeTemplateParmDecl *Param, |
| 3680 | QualType InstantiatedParamType, Expr *Arg, |
| 3681 | TemplateArgument &Converted, |
| 3682 | CheckTemplateArgumentKind CTAK) { |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 3683 | SourceLocation StartLoc = Arg->getSourceRange().getBegin(); |
| 3684 | |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3685 | // If either the parameter has a dependent type or the argument is |
| 3686 | // type-dependent, there's nothing we can check now. |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 3687 | if (InstantiatedParamType->isDependentType() || Arg->isTypeDependent()) { |
| 3688 | // FIXME: Produce a cloned, canonical expression? |
Douglas Gregor | 02cbbd2 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 3689 | Converted = TemplateArgument(Arg); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3690 | return Owned(Arg); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 3691 | } |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3692 | |
| 3693 | // C++ [temp.arg.nontype]p5: |
| 3694 | // The following conversions are performed on each expression used |
| 3695 | // as a non-type template-argument. If a non-type |
| 3696 | // template-argument cannot be converted to the type of the |
| 3697 | // corresponding template-parameter then the program is |
| 3698 | // ill-formed. |
| 3699 | // |
| 3700 | // -- for a non-type template-parameter of integral or |
| 3701 | // enumeration type, integral promotions (4.5) and integral |
| 3702 | // conversions (4.7) are applied. |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 3703 | QualType ParamType = InstantiatedParamType; |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 3704 | QualType ArgType = Arg->getType(); |
Douglas Gregor | 2ade35e | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 3705 | if (ParamType->isIntegralOrEnumerationType()) { |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3706 | // C++ [temp.arg.nontype]p1: |
| 3707 | // A template-argument for a non-type, non-template |
| 3708 | // template-parameter shall be one of: |
| 3709 | // |
| 3710 | // -- an integral constant-expression of integral or enumeration |
| 3711 | // type; or |
| 3712 | // -- the name of a non-type template-parameter; or |
| 3713 | SourceLocation NonConstantLoc; |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 3714 | llvm::APSInt Value; |
Douglas Gregor | 2ade35e | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 3715 | if (!ArgType->isIntegralOrEnumerationType()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3716 | Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3717 | diag::err_template_arg_not_integral_or_enumeral) |
| 3718 | << ArgType << Arg->getSourceRange(); |
| 3719 | Diag(Param->getLocation(), diag::note_template_param_here); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3720 | return ExprError(); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3721 | } else if (!Arg->isValueDependent() && |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 3722 | !Arg->isIntegerConstantExpr(Value, Context, &NonConstantLoc)) { |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3723 | Diag(NonConstantLoc, diag::err_template_arg_not_ice) |
| 3724 | << ArgType << Arg->getSourceRange(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3725 | return ExprError(); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3726 | } |
| 3727 | |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3728 | // From here on out, all we care about are the unqualified forms |
| 3729 | // of the parameter and argument types. |
| 3730 | ParamType = ParamType.getUnqualifiedType(); |
| 3731 | ArgType = ArgType.getUnqualifiedType(); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3732 | |
| 3733 | // Try to convert the argument to the parameter's type. |
Douglas Gregor | ff52439 | 2009-11-04 21:50:46 +0000 | [diff] [blame] | 3734 | if (Context.hasSameType(ParamType, ArgType)) { |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3735 | // Okay: no conversion necessary |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3736 | } else if (CTAK == CTAK_Deduced) { |
| 3737 | // C++ [temp.deduct.type]p17: |
| 3738 | // If, in the declaration of a function template with a non-type |
| 3739 | // template-parameter, the non-type template- parameter is used |
| 3740 | // in an expression in the function parameter-list and, if the |
| 3741 | // corresponding template-argument is deduced, the |
| 3742 | // template-argument type shall match the type of the |
| 3743 | // template-parameter exactly, except that a template-argument |
| 3744 | // deduced from an array bound may be of any integral type. |
| 3745 | Diag(StartLoc, diag::err_deduced_non_type_template_arg_type_mismatch) |
| 3746 | << ArgType << ParamType; |
| 3747 | Diag(Param->getLocation(), diag::note_template_param_here); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3748 | return ExprError(); |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3749 | } else if (ParamType->isBooleanType()) { |
| 3750 | // This is an integral-to-boolean conversion. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3751 | Arg = ImpCastExprToType(Arg, ParamType, CK_IntegralToBoolean).take(); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3752 | } else if (IsIntegralPromotion(Arg, ArgType, ParamType) || |
| 3753 | !ParamType->isEnumeralType()) { |
| 3754 | // This is an integral promotion or conversion. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3755 | Arg = ImpCastExprToType(Arg, ParamType, CK_IntegralCast).take(); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3756 | } else { |
| 3757 | // We can't perform this conversion. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3758 | Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3759 | diag::err_template_arg_not_convertible) |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 3760 | << Arg->getType() << InstantiatedParamType << Arg->getSourceRange(); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3761 | Diag(Param->getLocation(), diag::note_template_param_here); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3762 | return ExprError(); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3763 | } |
| 3764 | |
Douglas Gregor | c746937 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 3765 | // Add the value of this argument to the list of converted |
| 3766 | // arguments. We use the bitwidth and signedness of the template |
| 3767 | // parameter. |
| 3768 | if (Arg->isValueDependent()) { |
| 3769 | // The argument is value-dependent. Create a new |
| 3770 | // TemplateArgument with the converted expression. |
| 3771 | Converted = TemplateArgument(Arg); |
| 3772 | return Owned(Arg); |
| 3773 | } |
| 3774 | |
Douglas Gregor | f80a9d5 | 2009-03-14 00:20:21 +0000 | [diff] [blame] | 3775 | QualType IntegerType = Context.getCanonicalType(ParamType); |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3776 | if (const EnumType *Enum = IntegerType->getAs<EnumType>()) |
Douglas Gregor | 02cbbd2 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 3777 | IntegerType = Context.getCanonicalType(Enum->getDecl()->getIntegerType()); |
Douglas Gregor | f80a9d5 | 2009-03-14 00:20:21 +0000 | [diff] [blame] | 3778 | |
Douglas Gregor | c746937 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 3779 | if (ParamType->isBooleanType()) { |
| 3780 | // Value must be zero or one. |
| 3781 | Value = Value != 0; |
| 3782 | unsigned AllowedBits = Context.getTypeSize(IntegerType); |
| 3783 | if (Value.getBitWidth() != AllowedBits) |
| 3784 | Value = Value.extOrTrunc(AllowedBits); |
Douglas Gregor | 575a1c9 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 3785 | Value.setIsSigned(IntegerType->isSignedIntegerOrEnumerationType()); |
Douglas Gregor | c746937 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 3786 | } else { |
Douglas Gregor | 1a6e034 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 3787 | llvm::APSInt OldValue = Value; |
Douglas Gregor | c746937 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 3788 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3789 | // Coerce the template argument's value to the value it will have |
Douglas Gregor | 1a6e034 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 3790 | // based on the template parameter's type. |
Douglas Gregor | 0d4fd8e | 2010-03-26 00:39:40 +0000 | [diff] [blame] | 3791 | unsigned AllowedBits = Context.getTypeSize(IntegerType); |
Douglas Gregor | 0d4fd8e | 2010-03-26 00:39:40 +0000 | [diff] [blame] | 3792 | if (Value.getBitWidth() != AllowedBits) |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 3793 | Value = Value.extOrTrunc(AllowedBits); |
Douglas Gregor | 575a1c9 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 3794 | Value.setIsSigned(IntegerType->isSignedIntegerOrEnumerationType()); |
Douglas Gregor | c746937 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 3795 | |
Douglas Gregor | 1a6e034 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 3796 | // Complain if an unsigned parameter received a negative value. |
Douglas Gregor | 575a1c9 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 3797 | if (IntegerType->isUnsignedIntegerOrEnumerationType() |
Douglas Gregor | c746937 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 3798 | && (OldValue.isSigned() && OldValue.isNegative())) { |
Douglas Gregor | 1a6e034 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 3799 | Diag(Arg->getSourceRange().getBegin(), diag::warn_template_arg_negative) |
| 3800 | << OldValue.toString(10) << Value.toString(10) << Param->getType() |
| 3801 | << Arg->getSourceRange(); |
| 3802 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 3803 | } |
Douglas Gregor | c746937 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 3804 | |
Douglas Gregor | 1a6e034 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 3805 | // Complain if we overflowed the template parameter's type. |
| 3806 | unsigned RequiredBits; |
Douglas Gregor | 575a1c9 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 3807 | if (IntegerType->isUnsignedIntegerOrEnumerationType()) |
Douglas Gregor | 1a6e034 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 3808 | RequiredBits = OldValue.getActiveBits(); |
| 3809 | else if (OldValue.isUnsigned()) |
| 3810 | RequiredBits = OldValue.getActiveBits() + 1; |
| 3811 | else |
| 3812 | RequiredBits = OldValue.getMinSignedBits(); |
| 3813 | if (RequiredBits > AllowedBits) { |
| 3814 | Diag(Arg->getSourceRange().getBegin(), |
| 3815 | diag::warn_template_arg_too_large) |
| 3816 | << OldValue.toString(10) << Value.toString(10) << Param->getType() |
| 3817 | << Arg->getSourceRange(); |
| 3818 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 3819 | } |
Douglas Gregor | f80a9d5 | 2009-03-14 00:20:21 +0000 | [diff] [blame] | 3820 | } |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 3821 | |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 3822 | Converted = TemplateArgument(Value, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3823 | ParamType->isEnumeralType() ? ParamType |
Douglas Gregor | 02cbbd2 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 3824 | : IntegerType); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3825 | return Owned(Arg); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3826 | } |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 3827 | |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 3828 | DeclAccessPair FoundResult; // temporary for ResolveOverloadedFunction |
| 3829 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3830 | // C++0x [temp.arg.nontype]p5 bullets 2, 4 and 6 permit conversion |
| 3831 | // from a template argument of type std::nullptr_t to a non-type |
| 3832 | // template parameter of type pointer to object, pointer to |
| 3833 | // function, or pointer-to-member, respectively. |
Douglas Gregor | 84ee2ee | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 3834 | if (ArgType->isNullPtrType()) { |
| 3835 | if (ParamType->isPointerType() || ParamType->isMemberPointerType()) { |
| 3836 | Converted = TemplateArgument((NamedDecl *)0); |
| 3837 | return Owned(Arg); |
| 3838 | } |
| 3839 | |
| 3840 | if (ParamType->isNullPtrType()) { |
| 3841 | llvm::APSInt Zero(Context.getTypeSize(Context.NullPtrTy), true); |
| 3842 | Converted = TemplateArgument(Zero, Context.NullPtrTy); |
| 3843 | return Owned(Arg); |
| 3844 | } |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3845 | } |
| 3846 | |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 3847 | // Handle pointer-to-function, reference-to-function, and |
| 3848 | // pointer-to-member-function all in (roughly) the same way. |
| 3849 | if (// -- For a non-type template-parameter of type pointer to |
| 3850 | // function, only the function-to-pointer conversion (4.3) is |
| 3851 | // applied. If the template-argument represents a set of |
| 3852 | // overloaded functions (or a pointer to such), the matching |
| 3853 | // function is selected from the set (13.4). |
| 3854 | (ParamType->isPointerType() && |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3855 | ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType()) || |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 3856 | // -- For a non-type template-parameter of type reference to |
| 3857 | // function, no conversions apply. If the template-argument |
| 3858 | // represents a set of overloaded functions, the matching |
| 3859 | // function is selected from the set (13.4). |
| 3860 | (ParamType->isReferenceType() && |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3861 | ParamType->getAs<ReferenceType>()->getPointeeType()->isFunctionType()) || |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 3862 | // -- For a non-type template-parameter of type pointer to |
| 3863 | // member function, no conversions apply. If the |
| 3864 | // template-argument represents a set of overloaded member |
| 3865 | // functions, the matching member function is selected from |
| 3866 | // the set (13.4). |
| 3867 | (ParamType->isMemberPointerType() && |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3868 | ParamType->getAs<MemberPointerType>()->getPointeeType() |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 3869 | ->isFunctionType())) { |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3870 | |
Douglas Gregor | 1a8cf73 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 3871 | if (Arg->getType() == Context.OverloadTy) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3872 | if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg, ParamType, |
Douglas Gregor | 1a8cf73 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 3873 | true, |
| 3874 | FoundResult)) { |
| 3875 | if (DiagnoseUseOfDecl(Fn, Arg->getSourceRange().getBegin())) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3876 | return ExprError(); |
Douglas Gregor | 1a8cf73 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 3877 | |
| 3878 | Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn); |
| 3879 | ArgType = Arg->getType(); |
| 3880 | } else |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3881 | return ExprError(); |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 3882 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3883 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3884 | if (!ParamType->isMemberPointerType()) { |
| 3885 | if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param, |
| 3886 | ParamType, |
| 3887 | Arg, Converted)) |
| 3888 | return ExprError(); |
| 3889 | return Owned(Arg); |
| 3890 | } |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3891 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3892 | bool ObjCLifetimeConversion; |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 3893 | if (IsQualificationConversion(ArgType, ParamType.getNonReferenceType(), |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3894 | false, ObjCLifetimeConversion)) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3895 | Arg = ImpCastExprToType(Arg, ParamType, CK_NoOp, CastCategory(Arg)).take(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3896 | } else if (!Context.hasSameUnqualifiedType(ArgType, |
| 3897 | ParamType.getNonReferenceType())) { |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 3898 | // We can't perform this conversion. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3899 | Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 3900 | diag::err_template_arg_not_convertible) |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 3901 | << Arg->getType() << InstantiatedParamType << Arg->getSourceRange(); |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 3902 | Diag(Param->getLocation(), diag::note_template_param_here); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3903 | return ExprError(); |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 3904 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3905 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3906 | if (CheckTemplateArgumentPointerToMember(Arg, Converted)) |
| 3907 | return ExprError(); |
| 3908 | return Owned(Arg); |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 3909 | } |
| 3910 | |
Chris Lattner | fe90de7 | 2009-02-20 21:37:53 +0000 | [diff] [blame] | 3911 | if (ParamType->isPointerType()) { |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 3912 | // -- for a non-type template-parameter of type pointer to |
| 3913 | // object, qualification conversions (4.4) and the |
| 3914 | // array-to-pointer conversion (4.2) are applied. |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 3915 | // C++0x also allows a value of std::nullptr_t. |
Eli Friedman | 1357869 | 2010-08-05 02:49:48 +0000 | [diff] [blame] | 3916 | assert(ParamType->getPointeeType()->isIncompleteOrObjectType() && |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 3917 | "Only object pointers allowed here"); |
Douglas Gregor | f684e6e | 2009-02-11 00:44:29 +0000 | [diff] [blame] | 3918 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3919 | if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param, |
| 3920 | ParamType, |
| 3921 | Arg, Converted)) |
| 3922 | return ExprError(); |
| 3923 | return Owned(Arg); |
Douglas Gregor | f684e6e | 2009-02-11 00:44:29 +0000 | [diff] [blame] | 3924 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3925 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3926 | if (const ReferenceType *ParamRefType = ParamType->getAs<ReferenceType>()) { |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 3927 | // -- For a non-type template-parameter of type reference to |
| 3928 | // object, no conversions apply. The type referred to by the |
| 3929 | // reference may be more cv-qualified than the (otherwise |
| 3930 | // identical) type of the template-argument. The |
| 3931 | // template-parameter is bound directly to the |
| 3932 | // template-argument, which must be an lvalue. |
Eli Friedman | 1357869 | 2010-08-05 02:49:48 +0000 | [diff] [blame] | 3933 | assert(ParamRefType->getPointeeType()->isIncompleteOrObjectType() && |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 3934 | "Only object references allowed here"); |
Douglas Gregor | f684e6e | 2009-02-11 00:44:29 +0000 | [diff] [blame] | 3935 | |
Douglas Gregor | 1a8cf73 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 3936 | if (Arg->getType() == Context.OverloadTy) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3937 | if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg, |
| 3938 | ParamRefType->getPointeeType(), |
Douglas Gregor | 1a8cf73 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 3939 | true, |
| 3940 | FoundResult)) { |
| 3941 | if (DiagnoseUseOfDecl(Fn, Arg->getSourceRange().getBegin())) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3942 | return ExprError(); |
Douglas Gregor | 1a8cf73 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 3943 | |
| 3944 | Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn); |
| 3945 | ArgType = Arg->getType(); |
| 3946 | } else |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3947 | return ExprError(); |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 3948 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3949 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3950 | if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param, |
| 3951 | ParamType, |
| 3952 | Arg, Converted)) |
| 3953 | return ExprError(); |
| 3954 | return Owned(Arg); |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 3955 | } |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 3956 | |
| 3957 | // -- For a non-type template-parameter of type pointer to data |
| 3958 | // member, qualification conversions (4.4) are applied. |
| 3959 | assert(ParamType->isMemberPointerType() && "Only pointers to members remain"); |
| 3960 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3961 | bool ObjCLifetimeConversion; |
Douglas Gregor | 8e6563b | 2009-02-11 18:22:40 +0000 | [diff] [blame] | 3962 | if (Context.hasSameUnqualifiedType(ParamType, ArgType)) { |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 3963 | // Types match exactly: nothing more to do here. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3964 | } else if (IsQualificationConversion(ArgType, ParamType, false, |
| 3965 | ObjCLifetimeConversion)) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3966 | Arg = ImpCastExprToType(Arg, ParamType, CK_NoOp, CastCategory(Arg)).take(); |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 3967 | } else { |
| 3968 | // We can't perform this conversion. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3969 | Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 3970 | diag::err_template_arg_not_convertible) |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 3971 | << Arg->getType() << InstantiatedParamType << Arg->getSourceRange(); |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 3972 | Diag(Param->getLocation(), diag::note_template_param_here); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3973 | return ExprError(); |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 3974 | } |
| 3975 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3976 | if (CheckTemplateArgumentPointerToMember(Arg, Converted)) |
| 3977 | return ExprError(); |
| 3978 | return Owned(Arg); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3979 | } |
| 3980 | |
| 3981 | /// \brief Check a template argument against its corresponding |
| 3982 | /// template template parameter. |
| 3983 | /// |
| 3984 | /// This routine implements the semantics of C++ [temp.arg.template]. |
| 3985 | /// It returns true if an error occurred, and false otherwise. |
| 3986 | bool Sema::CheckTemplateArgument(TemplateTemplateParmDecl *Param, |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 3987 | const TemplateArgumentLoc &Arg) { |
| 3988 | TemplateName Name = Arg.getArgument().getAsTemplate(); |
| 3989 | TemplateDecl *Template = Name.getAsTemplateDecl(); |
| 3990 | if (!Template) { |
| 3991 | // Any dependent template name is fine. |
| 3992 | assert(Name.isDependent() && "Non-dependent template isn't a declaration?"); |
| 3993 | return false; |
| 3994 | } |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 3995 | |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3996 | // C++0x [temp.arg.template]p1: |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 3997 | // A template-argument for a template template-parameter shall be |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3998 | // the name of a class template or an alias template, expressed as an |
| 3999 | // id-expression. When the template-argument names a class template, only |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 4000 | // primary class templates are considered when matching the |
| 4001 | // template template argument with the corresponding parameter; |
| 4002 | // partial specializations are not considered even if their |
| 4003 | // parameter lists match that of the template template parameter. |
Douglas Gregor | ba1ecb5 | 2009-06-12 19:43:02 +0000 | [diff] [blame] | 4004 | // |
| 4005 | // Note that we also allow template template parameters here, which |
| 4006 | // will happen when we are dealing with, e.g., class template |
| 4007 | // partial specializations. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4008 | if (!isa<ClassTemplateDecl>(Template) && |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 4009 | !isa<TemplateTemplateParmDecl>(Template) && |
| 4010 | !isa<TypeAliasTemplateDecl>(Template)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4011 | assert(isa<FunctionTemplateDecl>(Template) && |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 4012 | "Only function templates are possible here"); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 4013 | Diag(Arg.getLocation(), diag::err_template_arg_not_class_template); |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 4014 | Diag(Template->getLocation(), diag::note_template_arg_refers_here_func) |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 4015 | << Template; |
| 4016 | } |
| 4017 | |
| 4018 | return !TemplateParameterListsAreEqual(Template->getTemplateParameters(), |
| 4019 | Param->getTemplateParameters(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4020 | true, |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 4021 | TPL_TemplateTemplateArgumentMatch, |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 4022 | Arg.getLocation()); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 4023 | } |
| 4024 | |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4025 | /// \brief Given a non-type template argument that refers to a |
| 4026 | /// declaration and the type of its corresponding non-type template |
| 4027 | /// parameter, produce an expression that properly refers to that |
| 4028 | /// declaration. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4029 | ExprResult |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4030 | Sema::BuildExpressionFromDeclTemplateArgument(const TemplateArgument &Arg, |
| 4031 | QualType ParamType, |
| 4032 | SourceLocation Loc) { |
| 4033 | assert(Arg.getKind() == TemplateArgument::Declaration && |
| 4034 | "Only declaration template arguments permitted here"); |
| 4035 | ValueDecl *VD = cast<ValueDecl>(Arg.getAsDecl()); |
| 4036 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4037 | if (VD->getDeclContext()->isRecord() && |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4038 | (isa<CXXMethodDecl>(VD) || isa<FieldDecl>(VD))) { |
| 4039 | // If the value is a class member, we might have a pointer-to-member. |
| 4040 | // Determine whether the non-type template template parameter is of |
| 4041 | // pointer-to-member type. If so, we need to build an appropriate |
| 4042 | // expression for a pointer-to-member, since a "normal" DeclRefExpr |
| 4043 | // would refer to the member itself. |
| 4044 | if (ParamType->isMemberPointerType()) { |
| 4045 | QualType ClassType |
| 4046 | = Context.getTypeDeclType(cast<RecordDecl>(VD->getDeclContext())); |
| 4047 | NestedNameSpecifier *Qualifier |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4048 | = NestedNameSpecifier::Create(Context, 0, false, |
| 4049 | ClassType.getTypePtr()); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4050 | CXXScopeSpec SS; |
Douglas Gregor | c34348a | 2011-02-24 17:54:50 +0000 | [diff] [blame] | 4051 | SS.MakeTrivial(Context, Qualifier, Loc); |
John McCall | dfa1edb | 2010-11-23 20:48:44 +0000 | [diff] [blame] | 4052 | |
| 4053 | // The actual value-ness of this is unimportant, but for |
| 4054 | // internal consistency's sake, references to instance methods |
| 4055 | // are r-values. |
| 4056 | ExprValueKind VK = VK_LValue; |
| 4057 | if (isa<CXXMethodDecl>(VD) && cast<CXXMethodDecl>(VD)->isInstance()) |
| 4058 | VK = VK_RValue; |
| 4059 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4060 | ExprResult RefExpr = BuildDeclRefExpr(VD, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4061 | VD->getType().getNonReferenceType(), |
John McCall | dfa1edb | 2010-11-23 20:48:44 +0000 | [diff] [blame] | 4062 | VK, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4063 | Loc, |
| 4064 | &SS); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4065 | if (RefExpr.isInvalid()) |
| 4066 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4067 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4068 | RefExpr = CreateBuiltinUnaryOp(Loc, UO_AddrOf, RefExpr.get()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4069 | |
Douglas Gregor | c0c8300 | 2010-04-30 21:46:38 +0000 | [diff] [blame] | 4070 | // We might need to perform a trailing qualification conversion, since |
| 4071 | // the element type on the parameter could be more qualified than the |
| 4072 | // element type in the expression we constructed. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4073 | bool ObjCLifetimeConversion; |
Douglas Gregor | c0c8300 | 2010-04-30 21:46:38 +0000 | [diff] [blame] | 4074 | if (IsQualificationConversion(((Expr*) RefExpr.get())->getType(), |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4075 | ParamType.getUnqualifiedType(), false, |
| 4076 | ObjCLifetimeConversion)) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4077 | RefExpr = ImpCastExprToType(RefExpr.take(), ParamType.getUnqualifiedType(), CK_NoOp); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4078 | |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4079 | assert(!RefExpr.isInvalid() && |
| 4080 | Context.hasSameType(((Expr*) RefExpr.get())->getType(), |
Douglas Gregor | c0c8300 | 2010-04-30 21:46:38 +0000 | [diff] [blame] | 4081 | ParamType.getUnqualifiedType())); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4082 | return move(RefExpr); |
| 4083 | } |
| 4084 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4085 | |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4086 | QualType T = VD->getType().getNonReferenceType(); |
| 4087 | if (ParamType->isPointerType()) { |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4088 | // When the non-type template parameter is a pointer, take the |
| 4089 | // address of the declaration. |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4090 | ExprResult RefExpr = BuildDeclRefExpr(VD, T, VK_LValue, Loc); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4091 | if (RefExpr.isInvalid()) |
| 4092 | return ExprError(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4093 | |
| 4094 | if (T->isFunctionType() || T->isArrayType()) { |
| 4095 | // Decay functions and arrays. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4096 | RefExpr = DefaultFunctionArrayConversion(RefExpr.take()); |
| 4097 | if (RefExpr.isInvalid()) |
| 4098 | return ExprError(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4099 | |
| 4100 | return move(RefExpr); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4101 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4102 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4103 | // Take the address of everything else |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4104 | return CreateBuiltinUnaryOp(Loc, UO_AddrOf, RefExpr.get()); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4105 | } |
| 4106 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4107 | ExprValueKind VK = VK_RValue; |
| 4108 | |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4109 | // If the non-type template parameter has reference type, qualify the |
| 4110 | // resulting declaration reference with the extra qualifiers on the |
| 4111 | // type that the reference refers to. |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4112 | if (const ReferenceType *TargetRef = ParamType->getAs<ReferenceType>()) { |
| 4113 | VK = VK_LValue; |
| 4114 | T = Context.getQualifiedType(T, |
| 4115 | TargetRef->getPointeeType().getQualifiers()); |
| 4116 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4117 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4118 | return BuildDeclRefExpr(VD, T, VK, Loc); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4119 | } |
| 4120 | |
| 4121 | /// \brief Construct a new expression that refers to the given |
| 4122 | /// integral template argument with the given source-location |
| 4123 | /// information. |
| 4124 | /// |
| 4125 | /// This routine takes care of the mapping from an integral template |
| 4126 | /// argument (which may have any integral type) to the appropriate |
| 4127 | /// literal value. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4128 | ExprResult |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4129 | Sema::BuildExpressionFromIntegralTemplateArgument(const TemplateArgument &Arg, |
| 4130 | SourceLocation Loc) { |
| 4131 | assert(Arg.getKind() == TemplateArgument::Integral && |
Douglas Gregor | d373119 | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 4132 | "Operation is only valid for integral template arguments"); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4133 | QualType T = Arg.getIntegralType(); |
| 4134 | if (T->isCharType() || T->isWideCharType()) |
| 4135 | return Owned(new (Context) CharacterLiteral( |
| 4136 | Arg.getAsIntegral()->getZExtValue(), |
Chris Lattner | 223de24 | 2011-04-25 20:37:58 +0000 | [diff] [blame] | 4137 | T->isWideCharType(), T, Loc)); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4138 | if (T->isBooleanType()) |
| 4139 | return Owned(new (Context) CXXBoolLiteralExpr( |
| 4140 | Arg.getAsIntegral()->getBoolValue(), |
Chris Lattner | 223de24 | 2011-04-25 20:37:58 +0000 | [diff] [blame] | 4141 | T, Loc)); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4142 | |
Douglas Gregor | 84ee2ee | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 4143 | if (T->isNullPtrType()) |
| 4144 | return Owned(new (Context) CXXNullPtrLiteralExpr(Context.NullPtrTy, Loc)); |
| 4145 | |
Chris Lattner | 223de24 | 2011-04-25 20:37:58 +0000 | [diff] [blame] | 4146 | // If this is an enum type that we're instantiating, we need to use an integer |
| 4147 | // type the same size as the enumerator. We don't want to build an |
| 4148 | // IntegerLiteral with enum type. |
Peter Collingbourne | fb7b363 | 2010-12-15 15:06:14 +0000 | [diff] [blame] | 4149 | QualType BT; |
| 4150 | if (const EnumType *ET = T->getAs<EnumType>()) |
Chris Lattner | 223de24 | 2011-04-25 20:37:58 +0000 | [diff] [blame] | 4151 | BT = ET->getDecl()->getIntegerType(); |
Peter Collingbourne | fb7b363 | 2010-12-15 15:06:14 +0000 | [diff] [blame] | 4152 | else |
| 4153 | BT = T; |
| 4154 | |
John McCall | 4e9272d | 2011-07-15 07:47:58 +0000 | [diff] [blame] | 4155 | Expr *E = IntegerLiteral::Create(Context, *Arg.getAsIntegral(), BT, Loc); |
| 4156 | if (T->isEnumeralType()) { |
| 4157 | // FIXME: This is a hack. We need a better way to handle substituted |
| 4158 | // non-type template parameters. |
| 4159 | E = CStyleCastExpr::Create(Context, T, VK_RValue, CK_IntegralCast, E, 0, |
| 4160 | Context.getTrivialTypeSourceInfo(T, Loc), |
| 4161 | Loc, Loc); |
| 4162 | } |
| 4163 | |
| 4164 | return Owned(E); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4165 | } |
| 4166 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4167 | /// \brief Match two template parameters within template parameter lists. |
| 4168 | static bool MatchTemplateParameterKind(Sema &S, NamedDecl *New, NamedDecl *Old, |
| 4169 | bool Complain, |
| 4170 | Sema::TemplateParameterListEqualKind Kind, |
| 4171 | SourceLocation TemplateArgLoc) { |
| 4172 | // Check the actual kind (type, non-type, template). |
| 4173 | if (Old->getKind() != New->getKind()) { |
| 4174 | if (Complain) { |
| 4175 | unsigned NextDiag = diag::err_template_param_different_kind; |
| 4176 | if (TemplateArgLoc.isValid()) { |
| 4177 | S.Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch); |
| 4178 | NextDiag = diag::note_template_param_different_kind; |
| 4179 | } |
| 4180 | S.Diag(New->getLocation(), NextDiag) |
| 4181 | << (Kind != Sema::TPL_TemplateMatch); |
| 4182 | S.Diag(Old->getLocation(), diag::note_template_prev_declaration) |
| 4183 | << (Kind != Sema::TPL_TemplateMatch); |
| 4184 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4185 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4186 | return false; |
| 4187 | } |
| 4188 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4189 | // Check that both are parameter packs are neither are parameter packs. |
| 4190 | // However, if we are matching a template template argument to a |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4191 | // template template parameter, the template template parameter can have |
| 4192 | // a parameter pack where the template template argument does not. |
| 4193 | if (Old->isTemplateParameterPack() != New->isTemplateParameterPack() && |
| 4194 | !(Kind == Sema::TPL_TemplateTemplateArgumentMatch && |
| 4195 | Old->isTemplateParameterPack())) { |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4196 | if (Complain) { |
| 4197 | unsigned NextDiag = diag::err_template_parameter_pack_non_pack; |
| 4198 | if (TemplateArgLoc.isValid()) { |
| 4199 | S.Diag(TemplateArgLoc, |
| 4200 | diag::err_template_arg_template_params_mismatch); |
| 4201 | NextDiag = diag::note_template_parameter_pack_non_pack; |
| 4202 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4203 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4204 | unsigned ParamKind = isa<TemplateTypeParmDecl>(New)? 0 |
| 4205 | : isa<NonTypeTemplateParmDecl>(New)? 1 |
| 4206 | : 2; |
| 4207 | S.Diag(New->getLocation(), NextDiag) |
| 4208 | << ParamKind << New->isParameterPack(); |
| 4209 | S.Diag(Old->getLocation(), diag::note_template_parameter_pack_here) |
| 4210 | << ParamKind << Old->isParameterPack(); |
| 4211 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4212 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4213 | return false; |
| 4214 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4215 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4216 | // For non-type template parameters, check the type of the parameter. |
| 4217 | if (NonTypeTemplateParmDecl *OldNTTP |
| 4218 | = dyn_cast<NonTypeTemplateParmDecl>(Old)) { |
| 4219 | NonTypeTemplateParmDecl *NewNTTP = cast<NonTypeTemplateParmDecl>(New); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4220 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4221 | // If we are matching a template template argument to a template |
| 4222 | // template parameter and one of the non-type template parameter types |
| 4223 | // is dependent, then we must wait until template instantiation time |
| 4224 | // to actually compare the arguments. |
| 4225 | if (Kind == Sema::TPL_TemplateTemplateArgumentMatch && |
| 4226 | (OldNTTP->getType()->isDependentType() || |
| 4227 | NewNTTP->getType()->isDependentType())) |
| 4228 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4229 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4230 | if (!S.Context.hasSameType(OldNTTP->getType(), NewNTTP->getType())) { |
| 4231 | if (Complain) { |
| 4232 | unsigned NextDiag = diag::err_template_nontype_parm_different_type; |
| 4233 | if (TemplateArgLoc.isValid()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4234 | S.Diag(TemplateArgLoc, |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4235 | diag::err_template_arg_template_params_mismatch); |
| 4236 | NextDiag = diag::note_template_nontype_parm_different_type; |
| 4237 | } |
| 4238 | S.Diag(NewNTTP->getLocation(), NextDiag) |
| 4239 | << NewNTTP->getType() |
| 4240 | << (Kind != Sema::TPL_TemplateMatch); |
| 4241 | S.Diag(OldNTTP->getLocation(), |
| 4242 | diag::note_template_nontype_parm_prev_declaration) |
| 4243 | << OldNTTP->getType(); |
| 4244 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4245 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4246 | return false; |
| 4247 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4248 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4249 | return true; |
| 4250 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4251 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4252 | // For template template parameters, check the template parameter types. |
| 4253 | // The template parameter lists of template template |
| 4254 | // parameters must agree. |
| 4255 | if (TemplateTemplateParmDecl *OldTTP |
| 4256 | = dyn_cast<TemplateTemplateParmDecl>(Old)) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4257 | TemplateTemplateParmDecl *NewTTP = cast<TemplateTemplateParmDecl>(New); |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4258 | return S.TemplateParameterListsAreEqual(NewTTP->getTemplateParameters(), |
| 4259 | OldTTP->getTemplateParameters(), |
| 4260 | Complain, |
| 4261 | (Kind == Sema::TPL_TemplateMatch |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4262 | ? Sema::TPL_TemplateTemplateParmMatch |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4263 | : Kind), |
| 4264 | TemplateArgLoc); |
| 4265 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4266 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4267 | return true; |
| 4268 | } |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4269 | |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4270 | /// \brief Diagnose a known arity mismatch when comparing template argument |
| 4271 | /// lists. |
| 4272 | static |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4273 | void DiagnoseTemplateParameterListArityMismatch(Sema &S, |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4274 | TemplateParameterList *New, |
| 4275 | TemplateParameterList *Old, |
| 4276 | Sema::TemplateParameterListEqualKind Kind, |
| 4277 | SourceLocation TemplateArgLoc) { |
| 4278 | unsigned NextDiag = diag::err_template_param_list_different_arity; |
| 4279 | if (TemplateArgLoc.isValid()) { |
| 4280 | S.Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch); |
| 4281 | NextDiag = diag::note_template_param_list_different_arity; |
| 4282 | } |
| 4283 | S.Diag(New->getTemplateLoc(), NextDiag) |
| 4284 | << (New->size() > Old->size()) |
| 4285 | << (Kind != Sema::TPL_TemplateMatch) |
| 4286 | << SourceRange(New->getTemplateLoc(), New->getRAngleLoc()); |
| 4287 | S.Diag(Old->getTemplateLoc(), diag::note_template_prev_declaration) |
| 4288 | << (Kind != Sema::TPL_TemplateMatch) |
| 4289 | << SourceRange(Old->getTemplateLoc(), Old->getRAngleLoc()); |
| 4290 | } |
| 4291 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4292 | /// \brief Determine whether the given template parameter lists are |
| 4293 | /// equivalent. |
| 4294 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4295 | /// \param New The new template parameter list, typically written in the |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4296 | /// source code as part of a new template declaration. |
| 4297 | /// |
| 4298 | /// \param Old The old template parameter list, typically found via |
| 4299 | /// name lookup of the template declared with this template parameter |
| 4300 | /// list. |
| 4301 | /// |
| 4302 | /// \param Complain If true, this routine will produce a diagnostic if |
| 4303 | /// the template parameter lists are not equivalent. |
| 4304 | /// |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 4305 | /// \param Kind describes how we are to match the template parameter lists. |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 4306 | /// |
| 4307 | /// \param TemplateArgLoc If this source location is valid, then we |
| 4308 | /// are actually checking the template parameter list of a template |
| 4309 | /// argument (New) against the template parameter list of its |
| 4310 | /// corresponding template template parameter (Old). We produce |
| 4311 | /// slightly different diagnostics in this scenario. |
| 4312 | /// |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4313 | /// \returns True if the template parameter lists are equal, false |
| 4314 | /// otherwise. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4315 | bool |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4316 | Sema::TemplateParameterListsAreEqual(TemplateParameterList *New, |
| 4317 | TemplateParameterList *Old, |
| 4318 | bool Complain, |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 4319 | TemplateParameterListEqualKind Kind, |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 4320 | SourceLocation TemplateArgLoc) { |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4321 | if (Old->size() != New->size() && Kind != TPL_TemplateTemplateArgumentMatch) { |
| 4322 | if (Complain) |
| 4323 | DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind, |
| 4324 | TemplateArgLoc); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4325 | |
| 4326 | return false; |
| 4327 | } |
| 4328 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4329 | // C++0x [temp.arg.template]p3: |
| 4330 | // A template-argument matches a template template-parameter (call it P) |
NAKAMURA Takumi | 0099530 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 4331 | // when each of the template parameters in the template-parameter-list of |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 4332 | // the template-argument's corresponding class template or alias template |
NAKAMURA Takumi | 0099530 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 4333 | // (call it A) matches the corresponding template parameter in the |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4334 | // template-parameter-list of P. [...] |
| 4335 | TemplateParameterList::iterator NewParm = New->begin(); |
| 4336 | TemplateParameterList::iterator NewParmEnd = New->end(); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4337 | for (TemplateParameterList::iterator OldParm = Old->begin(), |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4338 | OldParmEnd = Old->end(); |
| 4339 | OldParm != OldParmEnd; ++OldParm) { |
Douglas Gregor | c421f54 | 2011-01-13 18:47:47 +0000 | [diff] [blame] | 4340 | if (Kind != TPL_TemplateTemplateArgumentMatch || |
| 4341 | !(*OldParm)->isTemplateParameterPack()) { |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4342 | if (NewParm == NewParmEnd) { |
| 4343 | if (Complain) |
| 4344 | DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind, |
| 4345 | TemplateArgLoc); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4346 | |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4347 | return false; |
| 4348 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4349 | |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4350 | if (!MatchTemplateParameterKind(*this, *NewParm, *OldParm, Complain, |
| 4351 | Kind, TemplateArgLoc)) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4352 | return false; |
| 4353 | |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4354 | ++NewParm; |
| 4355 | continue; |
| 4356 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4357 | |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4358 | // C++0x [temp.arg.template]p3: |
NAKAMURA Takumi | 0099530 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 4359 | // [...] When P's template- parameter-list contains a template parameter |
| 4360 | // pack (14.5.3), the template parameter pack will match zero or more |
| 4361 | // template parameters or template parameter packs in the |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4362 | // template-parameter-list of A with the same type and form as the |
| 4363 | // template parameter pack in P (ignoring whether those template |
| 4364 | // parameters are template parameter packs). |
| 4365 | for (; NewParm != NewParmEnd; ++NewParm) { |
| 4366 | if (!MatchTemplateParameterKind(*this, *NewParm, *OldParm, Complain, |
| 4367 | Kind, TemplateArgLoc)) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4368 | return false; |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4369 | } |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4370 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4371 | |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4372 | // Make sure we exhausted all of the arguments. |
| 4373 | if (NewParm != NewParmEnd) { |
| 4374 | if (Complain) |
| 4375 | DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind, |
| 4376 | TemplateArgLoc); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4377 | |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4378 | return false; |
| 4379 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4380 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4381 | return true; |
| 4382 | } |
| 4383 | |
| 4384 | /// \brief Check whether a template can be declared within this scope. |
| 4385 | /// |
| 4386 | /// If the template declaration is valid in this scope, returns |
| 4387 | /// false. Otherwise, issues a diagnostic and returns true. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4388 | bool |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4389 | Sema::CheckTemplateDeclScope(Scope *S, TemplateParameterList *TemplateParams) { |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4390 | // Find the nearest enclosing declaration scope. |
| 4391 | while ((S->getFlags() & Scope::DeclScope) == 0 || |
| 4392 | (S->getFlags() & Scope::TemplateParamScope) != 0) |
| 4393 | S = S->getParent(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4394 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4395 | // C++ [temp]p2: |
| 4396 | // A template-declaration can appear only as a namespace scope or |
| 4397 | // class scope declaration. |
| 4398 | DeclContext *Ctx = static_cast<DeclContext *>(S->getEntity()); |
Eli Friedman | 1503f77 | 2009-07-31 01:43:05 +0000 | [diff] [blame] | 4399 | if (Ctx && isa<LinkageSpecDecl>(Ctx) && |
| 4400 | cast<LinkageSpecDecl>(Ctx)->getLanguage() != LinkageSpecDecl::lang_cxx) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4401 | return Diag(TemplateParams->getTemplateLoc(), diag::err_template_linkage) |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4402 | << TemplateParams->getSourceRange(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4403 | |
Eli Friedman | 1503f77 | 2009-07-31 01:43:05 +0000 | [diff] [blame] | 4404 | while (Ctx && isa<LinkageSpecDecl>(Ctx)) |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4405 | Ctx = Ctx->getParent(); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4406 | |
| 4407 | if (Ctx && (Ctx->isFileContext() || Ctx->isRecord())) |
| 4408 | return false; |
| 4409 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4410 | return Diag(TemplateParams->getTemplateLoc(), |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4411 | diag::err_template_outside_namespace_or_class_scope) |
| 4412 | << TemplateParams->getSourceRange(); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4413 | } |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4414 | |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4415 | /// \brief Determine what kind of template specialization the given declaration |
| 4416 | /// is. |
| 4417 | static TemplateSpecializationKind getTemplateSpecializationKind(NamedDecl *D) { |
| 4418 | if (!D) |
| 4419 | return TSK_Undeclared; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4420 | |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 4421 | if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) |
| 4422 | return Record->getTemplateSpecializationKind(); |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4423 | if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) |
| 4424 | return Function->getTemplateSpecializationKind(); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4425 | if (VarDecl *Var = dyn_cast<VarDecl>(D)) |
| 4426 | return Var->getTemplateSpecializationKind(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4427 | |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4428 | return TSK_Undeclared; |
| 4429 | } |
| 4430 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4431 | /// \brief Check whether a specialization is well-formed in the current |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4432 | /// context. |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4433 | /// |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4434 | /// This routine determines whether a template specialization can be declared |
| 4435 | /// in the current context (C++ [temp.expl.spec]p2). |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4436 | /// |
| 4437 | /// \param S the semantic analysis object for which this check is being |
| 4438 | /// performed. |
| 4439 | /// |
| 4440 | /// \param Specialized the entity being specialized or instantiated, which |
| 4441 | /// may be a kind of template (class template, function template, etc.) or |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4442 | /// a member of a class template (member function, static data member, |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4443 | /// member class). |
| 4444 | /// |
| 4445 | /// \param PrevDecl the previous declaration of this entity, if any. |
| 4446 | /// |
| 4447 | /// \param Loc the location of the explicit specialization or instantiation of |
| 4448 | /// this entity. |
| 4449 | /// |
| 4450 | /// \param IsPartialSpecialization whether this is a partial specialization of |
| 4451 | /// a class template. |
| 4452 | /// |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4453 | /// \returns true if there was an error that we cannot recover from, false |
| 4454 | /// otherwise. |
| 4455 | static bool CheckTemplateSpecializationScope(Sema &S, |
| 4456 | NamedDecl *Specialized, |
| 4457 | NamedDecl *PrevDecl, |
| 4458 | SourceLocation Loc, |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4459 | bool IsPartialSpecialization) { |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4460 | // Keep these "kind" numbers in sync with the %select statements in the |
| 4461 | // various diagnostics emitted by this routine. |
| 4462 | int EntityKind = 0; |
Ted Kremenek | fe62b06 | 2011-01-14 22:31:36 +0000 | [diff] [blame] | 4463 | if (isa<ClassTemplateDecl>(Specialized)) |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4464 | EntityKind = IsPartialSpecialization? 1 : 0; |
Ted Kremenek | fe62b06 | 2011-01-14 22:31:36 +0000 | [diff] [blame] | 4465 | else if (isa<FunctionTemplateDecl>(Specialized)) |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4466 | EntityKind = 2; |
Ted Kremenek | fe62b06 | 2011-01-14 22:31:36 +0000 | [diff] [blame] | 4467 | else if (isa<CXXMethodDecl>(Specialized)) |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4468 | EntityKind = 3; |
| 4469 | else if (isa<VarDecl>(Specialized)) |
| 4470 | EntityKind = 4; |
| 4471 | else if (isa<RecordDecl>(Specialized)) |
| 4472 | EntityKind = 5; |
| 4473 | else { |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4474 | S.Diag(Loc, diag::err_template_spec_unknown_kind); |
| 4475 | S.Diag(Specialized->getLocation(), diag::note_specialized_entity); |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4476 | return true; |
| 4477 | } |
| 4478 | |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4479 | // C++ [temp.expl.spec]p2: |
| 4480 | // An explicit specialization shall be declared in the namespace |
| 4481 | // of which the template is a member, or, for member templates, in |
| 4482 | // the namespace of which the enclosing class or enclosing class |
| 4483 | // template is a member. An explicit specialization of a member |
| 4484 | // function, member class or static data member of a class |
| 4485 | // template shall be declared in the namespace of which the class |
| 4486 | // template is a member. Such a declaration may also be a |
| 4487 | // definition. If the declaration is not a definition, the |
| 4488 | // specialization may be defined later in the name- space in which |
| 4489 | // the explicit specialization was declared, or in a namespace |
| 4490 | // that encloses the one in which the explicit specialization was |
| 4491 | // declared. |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 4492 | if (S.CurContext->getRedeclContext()->isFunctionOrMethod()) { |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4493 | S.Diag(Loc, diag::err_template_spec_decl_function_scope) |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4494 | << Specialized; |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4495 | return true; |
| 4496 | } |
Douglas Gregor | 7974c3b | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 4497 | |
Douglas Gregor | 0a40747 | 2009-10-07 17:30:37 +0000 | [diff] [blame] | 4498 | if (S.CurContext->isRecord() && !IsPartialSpecialization) { |
Francois Pichet | e1e96a6 | 2011-05-14 19:17:07 +0000 | [diff] [blame] | 4499 | S.Diag(Loc, diag::err_template_spec_decl_class_scope) |
| 4500 | << Specialized; |
| 4501 | return true; |
Douglas Gregor | 0a40747 | 2009-10-07 17:30:37 +0000 | [diff] [blame] | 4502 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4503 | |
Douglas Gregor | 7974c3b | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 4504 | // C++ [temp.class.spec]p6: |
| 4505 | // A class template partial specialization may be declared or redeclared |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4506 | // in any namespace scope in which its definition may be defined (14.5.1 |
| 4507 | // and 14.5.2). |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4508 | bool ComplainedAboutScope = false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4509 | DeclContext *SpecializedContext |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4510 | = Specialized->getDeclContext()->getEnclosingNamespaceContext(); |
Douglas Gregor | 7974c3b | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 4511 | DeclContext *DC = S.CurContext->getEnclosingNamespaceContext(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4512 | if ((!PrevDecl || |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4513 | getTemplateSpecializationKind(PrevDecl) == TSK_Undeclared || |
| 4514 | getTemplateSpecializationKind(PrevDecl) == TSK_ImplicitInstantiation)){ |
Douglas Gregor | 121dc9a | 2010-09-12 05:08:28 +0000 | [diff] [blame] | 4515 | // C++ [temp.exp.spec]p2: |
| 4516 | // An explicit specialization shall be declared in the namespace of which |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4517 | // the template is a member, or, for member templates, in the namespace |
Douglas Gregor | 121dc9a | 2010-09-12 05:08:28 +0000 | [diff] [blame] | 4518 | // of which the enclosing class or enclosing class template is a member. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4519 | // An explicit specialization of a member function, member class or |
| 4520 | // static data member of a class template shall be declared in the |
Douglas Gregor | 121dc9a | 2010-09-12 05:08:28 +0000 | [diff] [blame] | 4521 | // namespace of which the class template is a member. |
| 4522 | // |
| 4523 | // C++0x [temp.expl.spec]p2: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4524 | // An explicit specialization shall be declared in a namespace enclosing |
Douglas Gregor | 121dc9a | 2010-09-12 05:08:28 +0000 | [diff] [blame] | 4525 | // the specialized template. |
| 4526 | if (!DC->InEnclosingNamespaceSetOf(SpecializedContext) && |
| 4527 | !(S.getLangOptions().CPlusPlus0x && DC->Encloses(SpecializedContext))) { |
Douglas Gregor | a4d5de5 | 2010-09-12 05:24:55 +0000 | [diff] [blame] | 4528 | bool IsCPlusPlus0xExtension |
| 4529 | = !S.getLangOptions().CPlusPlus0x && DC->Encloses(SpecializedContext); |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4530 | if (isa<TranslationUnitDecl>(SpecializedContext)) |
Douglas Gregor | a4d5de5 | 2010-09-12 05:24:55 +0000 | [diff] [blame] | 4531 | S.Diag(Loc, IsCPlusPlus0xExtension |
| 4532 | ? diag::ext_template_spec_decl_out_of_scope_global |
| 4533 | : diag::err_template_spec_decl_out_of_scope_global) |
| 4534 | << EntityKind << Specialized; |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4535 | else if (isa<NamespaceDecl>(SpecializedContext)) |
Douglas Gregor | a4d5de5 | 2010-09-12 05:24:55 +0000 | [diff] [blame] | 4536 | S.Diag(Loc, IsCPlusPlus0xExtension |
| 4537 | ? diag::ext_template_spec_decl_out_of_scope |
| 4538 | : diag::err_template_spec_decl_out_of_scope) |
| 4539 | << EntityKind << Specialized |
| 4540 | << cast<NamedDecl>(SpecializedContext); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4541 | |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4542 | S.Diag(Specialized->getLocation(), diag::note_specialized_entity); |
| 4543 | ComplainedAboutScope = true; |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4544 | } |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4545 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4546 | |
| 4547 | // Make sure that this redeclaration (or definition) occurs in an enclosing |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4548 | // namespace. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4549 | // Note that HandleDeclarator() performs this check for explicit |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4550 | // specializations of function templates, static data members, and member |
| 4551 | // functions, so we skip the check here for those kinds of entities. |
| 4552 | // FIXME: HandleDeclarator's diagnostics aren't quite as good, though. |
Douglas Gregor | 7974c3b | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 4553 | // Should we refactor that check, so that it occurs later? |
| 4554 | if (!ComplainedAboutScope && !DC->Encloses(SpecializedContext) && |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4555 | !(isa<FunctionTemplateDecl>(Specialized) || isa<VarDecl>(Specialized) || |
| 4556 | isa<FunctionDecl>(Specialized))) { |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4557 | if (isa<TranslationUnitDecl>(SpecializedContext)) |
| 4558 | S.Diag(Loc, diag::err_template_spec_redecl_global_scope) |
| 4559 | << EntityKind << Specialized; |
| 4560 | else if (isa<NamespaceDecl>(SpecializedContext)) |
| 4561 | S.Diag(Loc, diag::err_template_spec_redecl_out_of_scope) |
| 4562 | << EntityKind << Specialized |
| 4563 | << cast<NamedDecl>(SpecializedContext); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4564 | |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4565 | S.Diag(Specialized->getLocation(), diag::note_specialized_entity); |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4566 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4567 | |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4568 | // FIXME: check for specialization-after-instantiation errors and such. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4569 | |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4570 | return false; |
| 4571 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4572 | |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4573 | /// \brief Subroutine of Sema::CheckClassTemplatePartialSpecializationArgs |
| 4574 | /// that checks non-type template partial specialization arguments. |
| 4575 | static bool CheckNonTypeClassTemplatePartialSpecializationArgs(Sema &S, |
| 4576 | NonTypeTemplateParmDecl *Param, |
| 4577 | const TemplateArgument *Args, |
| 4578 | unsigned NumArgs) { |
| 4579 | for (unsigned I = 0; I != NumArgs; ++I) { |
| 4580 | if (Args[I].getKind() == TemplateArgument::Pack) { |
| 4581 | if (CheckNonTypeClassTemplatePartialSpecializationArgs(S, Param, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4582 | Args[I].pack_begin(), |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4583 | Args[I].pack_size())) |
| 4584 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4585 | |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4586 | continue; |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4587 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4588 | |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4589 | Expr *ArgExpr = Args[I].getAsExpr(); |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 4590 | if (!ArgExpr) { |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4591 | continue; |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 4592 | } |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4593 | |
Douglas Gregor | 7a21fd4 | 2011-01-03 21:37:45 +0000 | [diff] [blame] | 4594 | // We can have a pack expansion of any of the bullets below. |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4595 | if (PackExpansionExpr *Expansion = dyn_cast<PackExpansionExpr>(ArgExpr)) |
| 4596 | ArgExpr = Expansion->getPattern(); |
Douglas Gregor | 54c53cc | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 4597 | |
| 4598 | // Strip off any implicit casts we added as part of type checking. |
| 4599 | while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(ArgExpr)) |
| 4600 | ArgExpr = ICE->getSubExpr(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4601 | |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4602 | // C++ [temp.class.spec]p8: |
| 4603 | // A non-type argument is non-specialized if it is the name of a |
| 4604 | // non-type parameter. All other non-type arguments are |
| 4605 | // specialized. |
| 4606 | // |
| 4607 | // Below, we check the two conditions that only apply to |
| 4608 | // specialized non-type arguments, so skip any non-specialized |
| 4609 | // arguments. |
| 4610 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ArgExpr)) |
Douglas Gregor | 54c53cc | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 4611 | if (isa<NonTypeTemplateParmDecl>(DRE->getDecl())) |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4612 | continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4613 | |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4614 | // C++ [temp.class.spec]p9: |
| 4615 | // Within the argument list of a class template partial |
| 4616 | // specialization, the following restrictions apply: |
| 4617 | // -- A partially specialized non-type argument expression |
| 4618 | // shall not involve a template parameter of the partial |
| 4619 | // specialization except when the argument expression is a |
| 4620 | // simple identifier. |
| 4621 | if (ArgExpr->isTypeDependent() || ArgExpr->isValueDependent()) { |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4622 | S.Diag(ArgExpr->getLocStart(), |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4623 | diag::err_dependent_non_type_arg_in_partial_spec) |
| 4624 | << ArgExpr->getSourceRange(); |
| 4625 | return true; |
| 4626 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4627 | |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4628 | // -- The type of a template parameter corresponding to a |
| 4629 | // specialized non-type argument shall not be dependent on a |
| 4630 | // parameter of the specialization. |
| 4631 | if (Param->getType()->isDependentType()) { |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4632 | S.Diag(ArgExpr->getLocStart(), |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4633 | diag::err_dependent_typed_non_type_arg_in_partial_spec) |
| 4634 | << Param->getType() |
| 4635 | << ArgExpr->getSourceRange(); |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4636 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4637 | return true; |
| 4638 | } |
| 4639 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4640 | |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4641 | return false; |
| 4642 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4643 | |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4644 | /// \brief Check the non-type template arguments of a class template |
| 4645 | /// partial specialization according to C++ [temp.class.spec]p9. |
| 4646 | /// |
| 4647 | /// \param TemplateParams the template parameters of the primary class |
| 4648 | /// template. |
| 4649 | /// |
| 4650 | /// \param TemplateArg the template arguments of the class template |
| 4651 | /// partial specialization. |
| 4652 | /// |
| 4653 | /// \returns true if there was an error, false otherwise. |
| 4654 | static bool CheckClassTemplatePartialSpecializationArgs(Sema &S, |
| 4655 | TemplateParameterList *TemplateParams, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4656 | SmallVectorImpl<TemplateArgument> &TemplateArgs) { |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4657 | const TemplateArgument *ArgList = TemplateArgs.data(); |
| 4658 | |
| 4659 | for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) { |
| 4660 | NonTypeTemplateParmDecl *Param |
| 4661 | = dyn_cast<NonTypeTemplateParmDecl>(TemplateParams->getParam(I)); |
| 4662 | if (!Param) |
| 4663 | continue; |
| 4664 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4665 | if (CheckNonTypeClassTemplatePartialSpecializationArgs(S, Param, |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4666 | &ArgList[I], 1)) |
| 4667 | return true; |
| 4668 | } |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4669 | |
| 4670 | return false; |
| 4671 | } |
| 4672 | |
Douglas Gregor | dc0a11c | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 4673 | /// \brief Retrieve the previous declaration of the given declaration. |
| 4674 | static NamedDecl *getPreviousDecl(NamedDecl *ND) { |
| 4675 | if (VarDecl *VD = dyn_cast<VarDecl>(ND)) |
| 4676 | return VD->getPreviousDeclaration(); |
| 4677 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) |
| 4678 | return FD->getPreviousDeclaration(); |
| 4679 | if (TagDecl *TD = dyn_cast<TagDecl>(ND)) |
| 4680 | return TD->getPreviousDeclaration(); |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 4681 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(ND)) |
Douglas Gregor | dc0a11c | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 4682 | return TD->getPreviousDeclaration(); |
| 4683 | if (FunctionTemplateDecl *FTD = dyn_cast<FunctionTemplateDecl>(ND)) |
| 4684 | return FTD->getPreviousDeclaration(); |
| 4685 | if (ClassTemplateDecl *CTD = dyn_cast<ClassTemplateDecl>(ND)) |
| 4686 | return CTD->getPreviousDeclaration(); |
| 4687 | return 0; |
| 4688 | } |
| 4689 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4690 | DeclResult |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 4691 | Sema::ActOnClassTemplateSpecialization(Scope *S, unsigned TagSpec, |
| 4692 | TagUseKind TUK, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4693 | SourceLocation KWLoc, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 4694 | CXXScopeSpec &SS, |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 4695 | TemplateTy TemplateD, |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4696 | SourceLocation TemplateNameLoc, |
| 4697 | SourceLocation LAngleLoc, |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 4698 | ASTTemplateArgsPtr TemplateArgsIn, |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4699 | SourceLocation RAngleLoc, |
| 4700 | AttributeList *Attr, |
| 4701 | MultiTemplateParamsArg TemplateParameterLists) { |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 4702 | assert(TUK != TUK_Reference && "References are not specializations"); |
John McCall | f1bbbb4 | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 4703 | |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 4704 | // NOTE: KWLoc is the location of the tag keyword. This will instead |
| 4705 | // store the location of the outermost template keyword in the declaration. |
| 4706 | SourceLocation TemplateKWLoc = TemplateParameterLists.size() > 0 |
| 4707 | ? TemplateParameterLists.get()[0]->getTemplateLoc() : SourceLocation(); |
| 4708 | |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4709 | // Find the class template we're specializing |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 4710 | TemplateName Name = TemplateD.getAsVal<TemplateName>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4711 | ClassTemplateDecl *ClassTemplate |
Douglas Gregor | 8b13c08 | 2009-11-12 00:46:20 +0000 | [diff] [blame] | 4712 | = dyn_cast_or_null<ClassTemplateDecl>(Name.getAsTemplateDecl()); |
| 4713 | |
| 4714 | if (!ClassTemplate) { |
| 4715 | Diag(TemplateNameLoc, diag::err_not_class_template_specialization) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4716 | << (Name.getAsTemplateDecl() && |
Douglas Gregor | 8b13c08 | 2009-11-12 00:46:20 +0000 | [diff] [blame] | 4717 | isa<TemplateTemplateParmDecl>(Name.getAsTemplateDecl())); |
| 4718 | return true; |
| 4719 | } |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4720 | |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 4721 | bool isExplicitSpecialization = false; |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 4722 | bool isPartialSpecialization = false; |
| 4723 | |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4724 | // Check the validity of the template headers that introduce this |
| 4725 | // template. |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 4726 | // FIXME: We probably shouldn't complain about these headers for |
| 4727 | // friend declarations. |
Douglas Gregor | 0167f3c | 2010-07-14 23:14:12 +0000 | [diff] [blame] | 4728 | bool Invalid = false; |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4729 | TemplateParameterList *TemplateParams |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 4730 | = MatchTemplateParametersToScopeSpecifier(TemplateNameLoc, |
| 4731 | TemplateNameLoc, |
| 4732 | SS, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4733 | (TemplateParameterList**)TemplateParameterLists.get(), |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 4734 | TemplateParameterLists.size(), |
John McCall | 77e8b11 | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 4735 | TUK == TUK_Friend, |
Douglas Gregor | 0167f3c | 2010-07-14 23:14:12 +0000 | [diff] [blame] | 4736 | isExplicitSpecialization, |
| 4737 | Invalid); |
| 4738 | if (Invalid) |
| 4739 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4740 | |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4741 | if (TemplateParams && TemplateParams->size() > 0) { |
| 4742 | isPartialSpecialization = true; |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4743 | |
Douglas Gregor | b0ee93c | 2010-12-21 08:14:57 +0000 | [diff] [blame] | 4744 | if (TUK == TUK_Friend) { |
| 4745 | Diag(KWLoc, diag::err_partial_specialization_friend) |
| 4746 | << SourceRange(LAngleLoc, RAngleLoc); |
| 4747 | return true; |
| 4748 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4749 | |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4750 | // C++ [temp.class.spec]p10: |
| 4751 | // The template parameter list of a specialization shall not |
| 4752 | // contain default template argument values. |
| 4753 | for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) { |
| 4754 | Decl *Param = TemplateParams->getParam(I); |
| 4755 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) { |
| 4756 | if (TTP->hasDefaultArgument()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4757 | Diag(TTP->getDefaultArgumentLoc(), |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4758 | diag::err_default_arg_in_partial_spec); |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 4759 | TTP->removeDefaultArgument(); |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4760 | } |
| 4761 | } else if (NonTypeTemplateParmDecl *NTTP |
| 4762 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
| 4763 | if (Expr *DefArg = NTTP->getDefaultArgument()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4764 | Diag(NTTP->getDefaultArgumentLoc(), |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4765 | diag::err_default_arg_in_partial_spec) |
| 4766 | << DefArg->getSourceRange(); |
Abramo Bagnara | d92f7a2 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 4767 | NTTP->removeDefaultArgument(); |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4768 | } |
| 4769 | } else { |
| 4770 | TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(Param); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 4771 | if (TTP->hasDefaultArgument()) { |
| 4772 | Diag(TTP->getDefaultArgument().getLocation(), |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4773 | diag::err_default_arg_in_partial_spec) |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 4774 | << TTP->getDefaultArgument().getSourceRange(); |
Abramo Bagnara | d92f7a2 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 4775 | TTP->removeDefaultArgument(); |
Douglas Gregor | ba1ecb5 | 2009-06-12 19:43:02 +0000 | [diff] [blame] | 4776 | } |
| 4777 | } |
| 4778 | } |
Douglas Gregor | a735b20 | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 4779 | } else if (TemplateParams) { |
| 4780 | if (TUK == TUK_Friend) |
| 4781 | Diag(KWLoc, diag::err_template_spec_friend) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 4782 | << FixItHint::CreateRemoval( |
Douglas Gregor | a735b20 | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 4783 | SourceRange(TemplateParams->getTemplateLoc(), |
| 4784 | TemplateParams->getRAngleLoc())) |
| 4785 | << SourceRange(LAngleLoc, RAngleLoc); |
| 4786 | else |
| 4787 | isExplicitSpecialization = true; |
| 4788 | } else if (TUK != TUK_Friend) { |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4789 | Diag(KWLoc, diag::err_template_spec_needs_header) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 4790 | << FixItHint::CreateInsertion(KWLoc, "template<> "); |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 4791 | isExplicitSpecialization = true; |
| 4792 | } |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4793 | |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4794 | // Check that the specialization uses the same tag kind as the |
| 4795 | // original template. |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 4796 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
| 4797 | assert(Kind != TTK_Enum && "Invalid enum tag in class template spec!"); |
Douglas Gregor | 501c5ce | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 4798 | if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(), |
Richard Trieu | bbf34c0 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 4799 | Kind, TUK == TUK_Definition, KWLoc, |
Douglas Gregor | 501c5ce | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 4800 | *ClassTemplate->getIdentifier())) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4801 | Diag(KWLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 4802 | << ClassTemplate |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 4803 | << FixItHint::CreateReplacement(KWLoc, |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 4804 | ClassTemplate->getTemplatedDecl()->getKindName()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4805 | Diag(ClassTemplate->getTemplatedDecl()->getLocation(), |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4806 | diag::note_previous_use); |
| 4807 | Kind = ClassTemplate->getTemplatedDecl()->getTagKind(); |
| 4808 | } |
| 4809 | |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 4810 | // Translate the parser's template argument list in our AST format. |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 4811 | TemplateArgumentListInfo TemplateArgs; |
| 4812 | TemplateArgs.setLAngleLoc(LAngleLoc); |
| 4813 | TemplateArgs.setRAngleLoc(RAngleLoc); |
Douglas Gregor | 314b97f | 2009-11-10 19:49:08 +0000 | [diff] [blame] | 4814 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 4815 | |
Douglas Gregor | 925910d | 2011-01-03 20:35:03 +0000 | [diff] [blame] | 4816 | // Check for unexpanded parameter packs in any of the template arguments. |
| 4817 | for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4818 | if (DiagnoseUnexpandedParameterPack(TemplateArgs[I], |
Douglas Gregor | 925910d | 2011-01-03 20:35:03 +0000 | [diff] [blame] | 4819 | UPPC_PartialSpecialization)) |
| 4820 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4821 | |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4822 | // Check that the template argument list is well-formed for this |
| 4823 | // template. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4824 | SmallVector<TemplateArgument, 4> Converted; |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 4825 | if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc, |
| 4826 | TemplateArgs, false, Converted)) |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 4827 | return true; |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4828 | |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 4829 | assert((Converted.size() == ClassTemplate->getTemplateParameters()->size()) && |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4830 | "Converted template argument list is too short!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4831 | |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 4832 | // Find the class template (partial) specialization declaration that |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4833 | // corresponds to these arguments. |
Douglas Gregor | ba1ecb5 | 2009-06-12 19:43:02 +0000 | [diff] [blame] | 4834 | if (isPartialSpecialization) { |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4835 | if (CheckClassTemplatePartialSpecializationArgs(*this, |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4836 | ClassTemplate->getTemplateParameters(), |
Douglas Gregor | b9c6631 | 2010-12-23 17:13:55 +0000 | [diff] [blame] | 4837 | Converted)) |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4838 | return true; |
| 4839 | |
Douglas Gregor | 561f812 | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 4840 | bool InstantiationDependent; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4841 | if (!Name.isDependent() && |
Douglas Gregor | de09096 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 4842 | !TemplateSpecializationType::anyDependentTemplateArguments( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4843 | TemplateArgs.getArgumentArray(), |
Douglas Gregor | 561f812 | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 4844 | TemplateArgs.size(), |
| 4845 | InstantiationDependent)) { |
Douglas Gregor | de09096 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 4846 | Diag(TemplateNameLoc, diag::err_partial_spec_fully_specialized) |
| 4847 | << ClassTemplate->getDeclName(); |
| 4848 | isPartialSpecialization = false; |
Douglas Gregor | de09096 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 4849 | } |
| 4850 | } |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 4851 | |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4852 | void *InsertPos = 0; |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 4853 | ClassTemplateSpecializationDecl *PrevDecl = 0; |
| 4854 | |
| 4855 | if (isPartialSpecialization) |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 4856 | // FIXME: Template parameter list matters, too |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 4857 | PrevDecl |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 4858 | = ClassTemplate->findPartialSpecialization(Converted.data(), |
| 4859 | Converted.size(), |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 4860 | InsertPos); |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 4861 | else |
| 4862 | PrevDecl |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 4863 | = ClassTemplate->findSpecialization(Converted.data(), |
| 4864 | Converted.size(), InsertPos); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4865 | |
| 4866 | ClassTemplateSpecializationDecl *Specialization = 0; |
| 4867 | |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4868 | // Check whether we can declare a class template specialization in |
| 4869 | // the current scope. |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 4870 | if (TUK != TUK_Friend && |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4871 | CheckTemplateSpecializationScope(*this, ClassTemplate, PrevDecl, |
| 4872 | TemplateNameLoc, |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4873 | isPartialSpecialization)) |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 4874 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4875 | |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 4876 | // The canonical type |
| 4877 | QualType CanonType; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4878 | if (PrevDecl && |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 4879 | (PrevDecl->getSpecializationKind() == TSK_Undeclared || |
Douglas Gregor | de09096 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 4880 | TUK == TUK_Friend)) { |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4881 | // Since the only prior class template specialization with these |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 4882 | // arguments was referenced but not declared, or we're only |
| 4883 | // referencing this specialization as a friend, reuse that |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 4884 | // declaration node as our own, updating its source location and |
| 4885 | // the list of outer template parameters to reflect our new declaration. |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4886 | Specialization = PrevDecl; |
Douglas Gregor | 6bc9f7e | 2009-02-25 22:18:32 +0000 | [diff] [blame] | 4887 | Specialization->setLocation(TemplateNameLoc); |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 4888 | if (TemplateParameterLists.size() > 0) { |
| 4889 | Specialization->setTemplateParameterListsInfo(Context, |
| 4890 | TemplateParameterLists.size(), |
| 4891 | (TemplateParameterList**) TemplateParameterLists.release()); |
| 4892 | } |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4893 | PrevDecl = 0; |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 4894 | CanonType = Context.getTypeDeclType(Specialization); |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 4895 | } else if (isPartialSpecialization) { |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 4896 | // Build the canonical type that describes the converted template |
| 4897 | // arguments of the class template partial specialization. |
Douglas Gregor | de09096 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 4898 | TemplateName CanonTemplate = Context.getCanonicalTemplateName(Name); |
| 4899 | CanonType = Context.getTemplateSpecializationType(CanonTemplate, |
Douglas Gregor | b9c6631 | 2010-12-23 17:13:55 +0000 | [diff] [blame] | 4900 | Converted.data(), |
| 4901 | Converted.size()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4902 | |
| 4903 | if (Context.hasSameType(CanonType, |
Douglas Gregor | b9c6631 | 2010-12-23 17:13:55 +0000 | [diff] [blame] | 4904 | ClassTemplate->getInjectedClassNameSpecialization())) { |
| 4905 | // C++ [temp.class.spec]p9b3: |
| 4906 | // |
| 4907 | // -- The argument list of the specialization shall not be identical |
| 4908 | // to the implicit argument list of the primary template. |
| 4909 | Diag(TemplateNameLoc, diag::err_partial_spec_args_match_primary_template) |
| 4910 | << (TUK == TUK_Definition) |
| 4911 | << FixItHint::CreateRemoval(SourceRange(LAngleLoc, RAngleLoc)); |
| 4912 | return CheckClassTemplate(S, TagSpec, TUK, KWLoc, SS, |
| 4913 | ClassTemplate->getIdentifier(), |
| 4914 | TemplateNameLoc, |
| 4915 | Attr, |
| 4916 | TemplateParams, |
Abramo Bagnara | c57c17d | 2011-03-10 13:28:31 +0000 | [diff] [blame] | 4917 | AS_none, |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 4918 | TemplateParameterLists.size() - 1, |
Abramo Bagnara | c57c17d | 2011-03-10 13:28:31 +0000 | [diff] [blame] | 4919 | (TemplateParameterList**) TemplateParameterLists.release()); |
Douglas Gregor | b9c6631 | 2010-12-23 17:13:55 +0000 | [diff] [blame] | 4920 | } |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 4921 | |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 4922 | // Create a new class template partial specialization declaration node. |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 4923 | ClassTemplatePartialSpecializationDecl *PrevPartial |
| 4924 | = cast_or_null<ClassTemplatePartialSpecializationDecl>(PrevDecl); |
Douglas Gregor | dc60c1e | 2010-04-30 05:56:50 +0000 | [diff] [blame] | 4925 | unsigned SequenceNumber = PrevPartial? PrevPartial->getSequenceNumber() |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 4926 | : ClassTemplate->getNextPartialSpecSequenceNumber(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4927 | ClassTemplatePartialSpecializationDecl *Partial |
Douglas Gregor | 13c8577 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 4928 | = ClassTemplatePartialSpecializationDecl::Create(Context, Kind, |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 4929 | ClassTemplate->getDeclContext(), |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 4930 | KWLoc, TemplateNameLoc, |
Anders Carlsson | 91fdf6f | 2009-06-05 04:06:48 +0000 | [diff] [blame] | 4931 | TemplateParams, |
| 4932 | ClassTemplate, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 4933 | Converted.data(), |
| 4934 | Converted.size(), |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 4935 | TemplateArgs, |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 4936 | CanonType, |
Douglas Gregor | dc60c1e | 2010-04-30 05:56:50 +0000 | [diff] [blame] | 4937 | PrevPartial, |
| 4938 | SequenceNumber); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 4939 | SetNestedNameSpecifier(Partial, SS); |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 4940 | if (TemplateParameterLists.size() > 1 && SS.isSet()) { |
Douglas Gregor | c722ea4 | 2010-06-15 17:44:38 +0000 | [diff] [blame] | 4941 | Partial->setTemplateParameterListsInfo(Context, |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 4942 | TemplateParameterLists.size() - 1, |
Abramo Bagnara | 9b93488 | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 4943 | (TemplateParameterList**) TemplateParameterLists.release()); |
| 4944 | } |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 4945 | |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 4946 | if (!PrevPartial) |
| 4947 | ClassTemplate->AddPartialSpecialization(Partial, InsertPos); |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 4948 | Specialization = Partial; |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4949 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4950 | // If we are providing an explicit specialization of a member class |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4951 | // template specialization, make a note of that. |
| 4952 | if (PrevPartial && PrevPartial->getInstantiatedFromMember()) |
| 4953 | PrevPartial->setMemberSpecialization(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4954 | |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4955 | // Check that all of the template parameters of the class template |
| 4956 | // partial specialization are deducible from the template |
| 4957 | // arguments. If not, this class template partial specialization |
| 4958 | // will never be used. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4959 | SmallVector<bool, 8> DeducibleParams; |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4960 | DeducibleParams.resize(TemplateParams->size()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4961 | MarkUsedTemplateParameters(Partial->getTemplateArgs(), true, |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4962 | TemplateParams->getDepth(), |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4963 | DeducibleParams); |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4964 | unsigned NumNonDeducible = 0; |
| 4965 | for (unsigned I = 0, N = DeducibleParams.size(); I != N; ++I) |
| 4966 | if (!DeducibleParams[I]) |
| 4967 | ++NumNonDeducible; |
| 4968 | |
| 4969 | if (NumNonDeducible) { |
| 4970 | Diag(TemplateNameLoc, diag::warn_partial_specs_not_deducible) |
| 4971 | << (NumNonDeducible > 1) |
| 4972 | << SourceRange(TemplateNameLoc, RAngleLoc); |
| 4973 | for (unsigned I = 0, N = DeducibleParams.size(); I != N; ++I) { |
| 4974 | if (!DeducibleParams[I]) { |
| 4975 | NamedDecl *Param = cast<NamedDecl>(TemplateParams->getParam(I)); |
| 4976 | if (Param->getDeclName()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4977 | Diag(Param->getLocation(), |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4978 | diag::note_partial_spec_unused_parameter) |
| 4979 | << Param->getDeclName(); |
| 4980 | else |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4981 | Diag(Param->getLocation(), |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4982 | diag::note_partial_spec_unused_parameter) |
Benjamin Kramer | 476d8b8 | 2010-08-11 14:47:12 +0000 | [diff] [blame] | 4983 | << "<anonymous>"; |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4984 | } |
| 4985 | } |
| 4986 | } |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4987 | } else { |
| 4988 | // Create a new class template specialization declaration node for |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 4989 | // this explicit specialization or friend declaration. |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4990 | Specialization |
Douglas Gregor | 13c8577 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 4991 | = ClassTemplateSpecializationDecl::Create(Context, Kind, |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4992 | ClassTemplate->getDeclContext(), |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 4993 | KWLoc, TemplateNameLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4994 | ClassTemplate, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 4995 | Converted.data(), |
| 4996 | Converted.size(), |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4997 | PrevDecl); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 4998 | SetNestedNameSpecifier(Specialization, SS); |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 4999 | if (TemplateParameterLists.size() > 0) { |
Douglas Gregor | c722ea4 | 2010-06-15 17:44:38 +0000 | [diff] [blame] | 5000 | Specialization->setTemplateParameterListsInfo(Context, |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 5001 | TemplateParameterLists.size(), |
Abramo Bagnara | 9b93488 | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 5002 | (TemplateParameterList**) TemplateParameterLists.release()); |
| 5003 | } |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5004 | |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 5005 | if (!PrevDecl) |
| 5006 | ClassTemplate->AddSpecialization(Specialization, InsertPos); |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 5007 | |
| 5008 | CanonType = Context.getTypeDeclType(Specialization); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5009 | } |
| 5010 | |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5011 | // C++ [temp.expl.spec]p6: |
| 5012 | // If a template, a member template or the member of a class template is |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5013 | // explicitly specialized then that specialization shall be declared |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5014 | // before the first use of that specialization that would cause an implicit |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5015 | // instantiation to take place, in every translation unit in which such a |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5016 | // use occurs; no diagnostic is required. |
| 5017 | if (PrevDecl && PrevDecl->getPointOfInstantiation().isValid()) { |
Douglas Gregor | dc0a11c | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 5018 | bool Okay = false; |
| 5019 | for (NamedDecl *Prev = PrevDecl; Prev; Prev = getPreviousDecl(Prev)) { |
| 5020 | // Is there any previous explicit specialization declaration? |
| 5021 | if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) { |
| 5022 | Okay = true; |
| 5023 | break; |
| 5024 | } |
| 5025 | } |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5026 | |
Douglas Gregor | dc0a11c | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 5027 | if (!Okay) { |
| 5028 | SourceRange Range(TemplateNameLoc, RAngleLoc); |
| 5029 | Diag(TemplateNameLoc, diag::err_specialization_after_instantiation) |
| 5030 | << Context.getTypeDeclType(Specialization) << Range; |
| 5031 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5032 | Diag(PrevDecl->getPointOfInstantiation(), |
Douglas Gregor | dc0a11c | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 5033 | diag::note_instantiation_required_here) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5034 | << (PrevDecl->getTemplateSpecializationKind() |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5035 | != TSK_ImplicitInstantiation); |
Douglas Gregor | dc0a11c | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 5036 | return true; |
| 5037 | } |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5038 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5039 | |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 5040 | // If this is not a friend, note that this is an explicit specialization. |
| 5041 | if (TUK != TUK_Friend) |
| 5042 | Specialization->setSpecializationKind(TSK_ExplicitSpecialization); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5043 | |
| 5044 | // Check that this isn't a redefinition of this specialization. |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 5045 | if (TUK == TUK_Definition) { |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 5046 | if (RecordDecl *Def = Specialization->getDefinition()) { |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5047 | SourceRange Range(TemplateNameLoc, RAngleLoc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5048 | Diag(TemplateNameLoc, diag::err_redefinition) |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 5049 | << Context.getTypeDeclType(Specialization) << Range; |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5050 | Diag(Def->getLocation(), diag::note_previous_definition); |
| 5051 | Specialization->setInvalidDecl(); |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 5052 | return true; |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5053 | } |
| 5054 | } |
| 5055 | |
John McCall | 7f1b987 | 2010-12-18 03:30:47 +0000 | [diff] [blame] | 5056 | if (Attr) |
| 5057 | ProcessDeclAttributeList(S, Specialization, Attr); |
| 5058 | |
Douglas Gregor | fc705b8 | 2009-02-26 22:19:44 +0000 | [diff] [blame] | 5059 | // Build the fully-sugared type for this class template |
| 5060 | // specialization as the user wrote in the specialization |
| 5061 | // itself. This means that we'll pretty-print the type retrieved |
| 5062 | // from the specialization's declaration the way that the user |
| 5063 | // actually wrote the specialization, rather than formatting the |
| 5064 | // name based on the "canonical" representation used to store the |
| 5065 | // template arguments in the specialization. |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 5066 | TypeSourceInfo *WrittenTy |
| 5067 | = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc, |
| 5068 | TemplateArgs, CanonType); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5069 | if (TUK != TUK_Friend) { |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 5070 | Specialization->setTypeAsWritten(WrittenTy); |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 5071 | Specialization->setTemplateKeywordLoc(TemplateKWLoc); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5072 | } |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 5073 | TemplateArgsIn.release(); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5074 | |
Douglas Gregor | 6bc9f7e | 2009-02-25 22:18:32 +0000 | [diff] [blame] | 5075 | // C++ [temp.expl.spec]p9: |
| 5076 | // A template explicit specialization is in the scope of the |
| 5077 | // namespace in which the template was defined. |
| 5078 | // |
| 5079 | // We actually implement this paragraph where we set the semantic |
| 5080 | // context (in the creation of the ClassTemplateSpecializationDecl), |
| 5081 | // but we also maintain the lexical context where the actual |
| 5082 | // definition occurs. |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5083 | Specialization->setLexicalDeclContext(CurContext); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5084 | |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5085 | // We may be starting the definition of this specialization. |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 5086 | if (TUK == TUK_Definition) |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5087 | Specialization->startDefinition(); |
| 5088 | |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 5089 | if (TUK == TUK_Friend) { |
| 5090 | FriendDecl *Friend = FriendDecl::Create(Context, CurContext, |
| 5091 | TemplateNameLoc, |
John McCall | 32f2fb5 | 2010-03-25 18:04:51 +0000 | [diff] [blame] | 5092 | WrittenTy, |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 5093 | /*FIXME:*/KWLoc); |
| 5094 | Friend->setAccess(AS_public); |
| 5095 | CurContext->addDecl(Friend); |
| 5096 | } else { |
| 5097 | // Add the specialization into its lexical context, so that it can |
| 5098 | // be seen when iterating through the list of declarations in that |
| 5099 | // context. However, specializations are not found by name lookup. |
| 5100 | CurContext->addDecl(Specialization); |
| 5101 | } |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5102 | return Specialization; |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5103 | } |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5104 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5105 | Decl *Sema::ActOnTemplateDeclarator(Scope *S, |
Douglas Gregor | e542c86 | 2009-06-23 23:11:28 +0000 | [diff] [blame] | 5106 | MultiTemplateParamsArg TemplateParameterLists, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5107 | Declarator &D) { |
Douglas Gregor | e542c86 | 2009-06-23 23:11:28 +0000 | [diff] [blame] | 5108 | return HandleDeclarator(S, D, move(TemplateParameterLists), false); |
| 5109 | } |
| 5110 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5111 | Decl *Sema::ActOnStartOfFunctionTemplateDef(Scope *FnBodyScope, |
Douglas Gregor | 52591bf | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 5112 | MultiTemplateParamsArg TemplateParameterLists, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5113 | Declarator &D) { |
Douglas Gregor | 52591bf | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 5114 | assert(getCurFunctionDecl() == 0 && "Function parsing confused"); |
Abramo Bagnara | 075f8f1 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 5115 | DeclaratorChunk::FunctionTypeInfo &FTI = D.getFunctionTypeInfo(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5116 | |
Douglas Gregor | 52591bf | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 5117 | if (FTI.hasPrototype) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5118 | // FIXME: Diagnose arguments without names in C. |
Douglas Gregor | 52591bf | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 5119 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5120 | |
Douglas Gregor | 52591bf | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 5121 | Scope *ParentScope = FnBodyScope->getParent(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5122 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5123 | Decl *DP = HandleDeclarator(ParentScope, D, |
| 5124 | move(TemplateParameterLists), |
| 5125 | /*IsFunctionDefinition=*/true); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5126 | if (FunctionTemplateDecl *FunctionTemplate |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5127 | = dyn_cast_or_null<FunctionTemplateDecl>(DP)) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5128 | return ActOnStartOfFunctionDef(FnBodyScope, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5129 | FunctionTemplate->getTemplatedDecl()); |
| 5130 | if (FunctionDecl *Function = dyn_cast_or_null<FunctionDecl>(DP)) |
| 5131 | return ActOnStartOfFunctionDef(FnBodyScope, Function); |
| 5132 | return 0; |
Douglas Gregor | 52591bf | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 5133 | } |
| 5134 | |
John McCall | 7504239 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 5135 | /// \brief Strips various properties off an implicit instantiation |
| 5136 | /// that has just been explicitly specialized. |
| 5137 | static void StripImplicitInstantiation(NamedDecl *D) { |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 5138 | D->dropAttrs(); |
John McCall | 7504239 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 5139 | |
| 5140 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 5141 | FD->setInlineSpecified(false); |
| 5142 | } |
| 5143 | } |
| 5144 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5145 | /// \brief Diagnose cases where we have an explicit template specialization |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5146 | /// before/after an explicit template instantiation, producing diagnostics |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5147 | /// for those cases where they are required and determining whether the |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5148 | /// new specialization/instantiation will have any effect. |
| 5149 | /// |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5150 | /// \param NewLoc the location of the new explicit specialization or |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5151 | /// instantiation. |
| 5152 | /// |
| 5153 | /// \param NewTSK the kind of the new explicit specialization or instantiation. |
| 5154 | /// |
| 5155 | /// \param PrevDecl the previous declaration of the entity. |
| 5156 | /// |
| 5157 | /// \param PrevTSK the kind of the old explicit specialization or instantiatin. |
| 5158 | /// |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5159 | /// \param PrevPointOfInstantiation if valid, indicates where the previus |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5160 | /// declaration was instantiated (either implicitly or explicitly). |
| 5161 | /// |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5162 | /// \param HasNoEffect will be set to true to indicate that the new |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5163 | /// specialization or instantiation has no effect and should be ignored. |
| 5164 | /// |
| 5165 | /// \returns true if there was an error that should prevent the introduction of |
| 5166 | /// the new declaration into the AST, false otherwise. |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 5167 | bool |
| 5168 | Sema::CheckSpecializationInstantiationRedecl(SourceLocation NewLoc, |
| 5169 | TemplateSpecializationKind NewTSK, |
| 5170 | NamedDecl *PrevDecl, |
| 5171 | TemplateSpecializationKind PrevTSK, |
| 5172 | SourceLocation PrevPointOfInstantiation, |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5173 | bool &HasNoEffect) { |
| 5174 | HasNoEffect = false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5175 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5176 | switch (NewTSK) { |
| 5177 | case TSK_Undeclared: |
| 5178 | case TSK_ImplicitInstantiation: |
| 5179 | assert(false && "Don't check implicit instantiations here"); |
| 5180 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5181 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5182 | case TSK_ExplicitSpecialization: |
| 5183 | switch (PrevTSK) { |
| 5184 | case TSK_Undeclared: |
| 5185 | case TSK_ExplicitSpecialization: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5186 | // Okay, we're just specializing something that is either already |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5187 | // explicitly specialized or has merely been mentioned without any |
| 5188 | // instantiation. |
| 5189 | return false; |
| 5190 | |
| 5191 | case TSK_ImplicitInstantiation: |
| 5192 | if (PrevPointOfInstantiation.isInvalid()) { |
| 5193 | // The declaration itself has not actually been instantiated, so it is |
| 5194 | // still okay to specialize it. |
John McCall | 7504239 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 5195 | StripImplicitInstantiation(PrevDecl); |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5196 | return false; |
| 5197 | } |
| 5198 | // Fall through |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5199 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5200 | case TSK_ExplicitInstantiationDeclaration: |
| 5201 | case TSK_ExplicitInstantiationDefinition: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5202 | assert((PrevTSK == TSK_ImplicitInstantiation || |
| 5203 | PrevPointOfInstantiation.isValid()) && |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5204 | "Explicit instantiation without point of instantiation?"); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5205 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5206 | // C++ [temp.expl.spec]p6: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5207 | // If a template, a member template or the member of a class template |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5208 | // is explicitly specialized then that specialization shall be declared |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5209 | // before the first use of that specialization that would cause an |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5210 | // implicit instantiation to take place, in every translation unit in |
| 5211 | // which such a use occurs; no diagnostic is required. |
Douglas Gregor | dc0a11c | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 5212 | for (NamedDecl *Prev = PrevDecl; Prev; Prev = getPreviousDecl(Prev)) { |
| 5213 | // Is there any previous explicit specialization declaration? |
| 5214 | if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) |
| 5215 | return false; |
| 5216 | } |
| 5217 | |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 5218 | Diag(NewLoc, diag::err_specialization_after_instantiation) |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5219 | << PrevDecl; |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 5220 | Diag(PrevPointOfInstantiation, diag::note_instantiation_required_here) |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5221 | << (PrevTSK != TSK_ImplicitInstantiation); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5222 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5223 | return true; |
| 5224 | } |
| 5225 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5226 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5227 | case TSK_ExplicitInstantiationDeclaration: |
| 5228 | switch (PrevTSK) { |
| 5229 | case TSK_ExplicitInstantiationDeclaration: |
| 5230 | // This explicit instantiation declaration is redundant (that's okay). |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5231 | HasNoEffect = true; |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5232 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5233 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5234 | case TSK_Undeclared: |
| 5235 | case TSK_ImplicitInstantiation: |
| 5236 | // We're explicitly instantiating something that may have already been |
| 5237 | // implicitly instantiated; that's fine. |
| 5238 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5239 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5240 | case TSK_ExplicitSpecialization: |
| 5241 | // C++0x [temp.explicit]p4: |
| 5242 | // For a given set of template parameters, if an explicit instantiation |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5243 | // of a template appears after a declaration of an explicit |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5244 | // specialization for that template, the explicit instantiation has no |
| 5245 | // effect. |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5246 | HasNoEffect = true; |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5247 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5248 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5249 | case TSK_ExplicitInstantiationDefinition: |
| 5250 | // C++0x [temp.explicit]p10: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5251 | // If an entity is the subject of both an explicit instantiation |
| 5252 | // declaration and an explicit instantiation definition in the same |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5253 | // translation unit, the definition shall follow the declaration. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5254 | Diag(NewLoc, |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 5255 | diag::err_explicit_instantiation_declaration_after_definition); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5256 | Diag(PrevPointOfInstantiation, |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 5257 | diag::note_explicit_instantiation_definition_here); |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5258 | assert(PrevPointOfInstantiation.isValid() && |
| 5259 | "Explicit instantiation without point of instantiation?"); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5260 | HasNoEffect = true; |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5261 | return false; |
| 5262 | } |
| 5263 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5264 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5265 | case TSK_ExplicitInstantiationDefinition: |
| 5266 | switch (PrevTSK) { |
| 5267 | case TSK_Undeclared: |
| 5268 | case TSK_ImplicitInstantiation: |
| 5269 | // We're explicitly instantiating something that may have already been |
| 5270 | // implicitly instantiated; that's fine. |
| 5271 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5272 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5273 | case TSK_ExplicitSpecialization: |
| 5274 | // C++ DR 259, C++0x [temp.explicit]p4: |
| 5275 | // For a given set of template parameters, if an explicit |
| 5276 | // instantiation of a template appears after a declaration of |
| 5277 | // an explicit specialization for that template, the explicit |
| 5278 | // instantiation has no effect. |
| 5279 | // |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5280 | // 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] | 5281 | // is not harmful to try to explicitly instantiate something that |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5282 | // has been explicitly specialized. |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 5283 | if (!getLangOptions().CPlusPlus0x) { |
| 5284 | Diag(NewLoc, diag::ext_explicit_instantiation_after_specialization) |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5285 | << PrevDecl; |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 5286 | Diag(PrevDecl->getLocation(), |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5287 | diag::note_previous_template_specialization); |
| 5288 | } |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5289 | HasNoEffect = true; |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5290 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5291 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5292 | case TSK_ExplicitInstantiationDeclaration: |
| 5293 | // We're explicity instantiating a definition for something for which we |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5294 | // were previously asked to suppress instantiations. That's fine. |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5295 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5296 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5297 | case TSK_ExplicitInstantiationDefinition: |
| 5298 | // C++0x [temp.spec]p5: |
| 5299 | // For a given template and a given set of template-arguments, |
| 5300 | // - an explicit instantiation definition shall appear at most once |
| 5301 | // in a program, |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 5302 | Diag(NewLoc, diag::err_explicit_instantiation_duplicate) |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5303 | << PrevDecl; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5304 | Diag(PrevPointOfInstantiation, |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 5305 | diag::note_previous_explicit_instantiation); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5306 | HasNoEffect = true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5307 | return false; |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5308 | } |
| 5309 | break; |
| 5310 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5311 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5312 | assert(false && "Missing specialization/instantiation case?"); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5313 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5314 | return false; |
| 5315 | } |
| 5316 | |
John McCall | af2094e | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 5317 | /// \brief Perform semantic analysis for the given dependent function |
| 5318 | /// template specialization. The only possible way to get a dependent |
| 5319 | /// function template specialization is with a friend declaration, |
| 5320 | /// like so: |
| 5321 | /// |
| 5322 | /// template <class T> void foo(T); |
| 5323 | /// template <class T> class A { |
| 5324 | /// friend void foo<>(T); |
| 5325 | /// }; |
| 5326 | /// |
| 5327 | /// There really isn't any useful analysis we can do here, so we |
| 5328 | /// just store the information. |
| 5329 | bool |
| 5330 | Sema::CheckDependentFunctionTemplateSpecialization(FunctionDecl *FD, |
| 5331 | const TemplateArgumentListInfo &ExplicitTemplateArgs, |
| 5332 | LookupResult &Previous) { |
| 5333 | // Remove anything from Previous that isn't a function template in |
| 5334 | // the correct context. |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 5335 | DeclContext *FDLookupContext = FD->getDeclContext()->getRedeclContext(); |
John McCall | af2094e | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 5336 | LookupResult::Filter F = Previous.makeFilter(); |
| 5337 | while (F.hasNext()) { |
| 5338 | NamedDecl *D = F.next()->getUnderlyingDecl(); |
| 5339 | if (!isa<FunctionTemplateDecl>(D) || |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 5340 | !FDLookupContext->InEnclosingNamespaceSetOf( |
| 5341 | D->getDeclContext()->getRedeclContext())) |
John McCall | af2094e | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 5342 | F.erase(); |
| 5343 | } |
| 5344 | F.done(); |
| 5345 | |
| 5346 | // Should this be diagnosed here? |
| 5347 | if (Previous.empty()) return true; |
| 5348 | |
| 5349 | FD->setDependentTemplateSpecialization(Context, Previous.asUnresolvedSet(), |
| 5350 | ExplicitTemplateArgs); |
| 5351 | return false; |
| 5352 | } |
| 5353 | |
Abramo Bagnara | e03db98 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 5354 | /// \brief Perform semantic analysis for the given function template |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5355 | /// specialization. |
| 5356 | /// |
Abramo Bagnara | e03db98 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 5357 | /// This routine performs all of the semantic analysis required for an |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5358 | /// explicit function template specialization. On successful completion, |
| 5359 | /// the function declaration \p FD will become a function template |
| 5360 | /// specialization. |
| 5361 | /// |
| 5362 | /// \param FD the function declaration, which will be updated to become a |
| 5363 | /// function template specialization. |
| 5364 | /// |
Abramo Bagnara | e03db98 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 5365 | /// \param ExplicitTemplateArgs the explicitly-provided template arguments, |
| 5366 | /// if any. Note that this may be valid info even when 0 arguments are |
| 5367 | /// explicitly provided as in, e.g., \c void sort<>(char*, char*); |
| 5368 | /// as it anyway contains info on the angle brackets locations. |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5369 | /// |
Francois Pichet | 59e7c56 | 2011-07-08 06:21:47 +0000 | [diff] [blame] | 5370 | /// \param Previous the set of declarations that may be specialized by |
Abramo Bagnara | e03db98 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 5371 | /// this function specialization. |
| 5372 | bool |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5373 | Sema::CheckFunctionTemplateSpecialization(FunctionDecl *FD, |
Douglas Gregor | 6771423 | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 5374 | TemplateArgumentListInfo *ExplicitTemplateArgs, |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5375 | LookupResult &Previous) { |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5376 | // The set of function template specializations that could match this |
| 5377 | // explicit function template specialization. |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5378 | UnresolvedSet<8> Candidates; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5379 | |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 5380 | DeclContext *FDLookupContext = FD->getDeclContext()->getRedeclContext(); |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5381 | for (LookupResult::iterator I = Previous.begin(), E = Previous.end(); |
| 5382 | I != E; ++I) { |
| 5383 | NamedDecl *Ovl = (*I)->getUnderlyingDecl(); |
| 5384 | if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Ovl)) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5385 | // Only consider templates found within the same semantic lookup scope as |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5386 | // FD. |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 5387 | if (!FDLookupContext->InEnclosingNamespaceSetOf( |
| 5388 | Ovl->getDeclContext()->getRedeclContext())) |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5389 | continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5390 | |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5391 | // C++ [temp.expl.spec]p11: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5392 | // A trailing template-argument can be left unspecified in the |
| 5393 | // template-id naming an explicit function template specialization |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5394 | // provided it can be deduced from the function argument type. |
| 5395 | // Perform template argument deduction to determine whether we may be |
| 5396 | // specializing this template. |
| 5397 | // FIXME: It is somewhat wasteful to build |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 5398 | TemplateDeductionInfo Info(Context, FD->getLocation()); |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5399 | FunctionDecl *Specialization = 0; |
| 5400 | if (TemplateDeductionResult TDK |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 5401 | = DeduceTemplateArguments(FunTmpl, ExplicitTemplateArgs, |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5402 | FD->getType(), |
| 5403 | Specialization, |
| 5404 | Info)) { |
| 5405 | // FIXME: Template argument deduction failed; record why it failed, so |
| 5406 | // that we can provide nifty diagnostics. |
| 5407 | (void)TDK; |
| 5408 | continue; |
| 5409 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5410 | |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5411 | // Record this candidate. |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5412 | Candidates.addDecl(Specialization, I.getAccess()); |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5413 | } |
| 5414 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5415 | |
Douglas Gregor | c5df30f | 2009-09-26 03:41:46 +0000 | [diff] [blame] | 5416 | // Find the most specialized function template. |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5417 | UnresolvedSetIterator Result |
| 5418 | = getMostSpecialized(Candidates.begin(), Candidates.end(), |
Douglas Gregor | 5c7bf42 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 5419 | TPOC_Other, 0, FD->getLocation(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5420 | PDiag(diag::err_function_template_spec_no_match) |
Douglas Gregor | c5df30f | 2009-09-26 03:41:46 +0000 | [diff] [blame] | 5421 | << FD->getDeclName(), |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 5422 | PDiag(diag::err_function_template_spec_ambiguous) |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 5423 | << FD->getDeclName() << (ExplicitTemplateArgs != 0), |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 5424 | PDiag(diag::note_function_template_spec_matched)); |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5425 | if (Result == Candidates.end()) |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5426 | return true; |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5427 | |
| 5428 | // Ignore access information; it doesn't figure into redeclaration checking. |
| 5429 | FunctionDecl *Specialization = cast<FunctionDecl>(*Result); |
Abramo Bagnara | abfb405 | 2011-03-04 17:20:30 +0000 | [diff] [blame] | 5430 | |
| 5431 | FunctionTemplateSpecializationInfo *SpecInfo |
| 5432 | = Specialization->getTemplateSpecializationInfo(); |
| 5433 | assert(SpecInfo && "Function template specialization info missing?"); |
Francois Pichet | 59e7c56 | 2011-07-08 06:21:47 +0000 | [diff] [blame] | 5434 | |
| 5435 | // Note: do not overwrite location info if previous template |
| 5436 | // specialization kind was explicit. |
| 5437 | TemplateSpecializationKind TSK = SpecInfo->getTemplateSpecializationKind(); |
| 5438 | if (TSK == TSK_Undeclared || TSK == TSK_ImplicitInstantiation) |
| 5439 | Specialization->setLocation(FD->getLocation()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5440 | |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5441 | // FIXME: Check if the prior specialization has a point of instantiation. |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5442 | // If so, we have run afoul of . |
John McCall | 7ad650f | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 5443 | |
| 5444 | // If this is a friend declaration, then we're not really declaring |
| 5445 | // an explicit specialization. |
| 5446 | bool isFriend = (FD->getFriendObjectKind() != Decl::FOK_None); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5447 | |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5448 | // Check the scope of this explicit specialization. |
John McCall | 7ad650f | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 5449 | if (!isFriend && |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5450 | CheckTemplateSpecializationScope(*this, |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5451 | Specialization->getPrimaryTemplate(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5452 | Specialization, FD->getLocation(), |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 5453 | false)) |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5454 | return true; |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5455 | |
| 5456 | // C++ [temp.expl.spec]p6: |
| 5457 | // If a template, a member template or the member of a class template is |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5458 | // explicitly specialized then that specialization shall be declared |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5459 | // before the first use of that specialization that would cause an implicit |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5460 | // instantiation to take place, in every translation unit in which such a |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5461 | // use occurs; no diagnostic is required. |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5462 | bool HasNoEffect = false; |
John McCall | 7ad650f | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 5463 | if (!isFriend && |
| 5464 | CheckSpecializationInstantiationRedecl(FD->getLocation(), |
John McCall | 7504239 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 5465 | TSK_ExplicitSpecialization, |
| 5466 | Specialization, |
| 5467 | SpecInfo->getTemplateSpecializationKind(), |
| 5468 | SpecInfo->getPointOfInstantiation(), |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5469 | HasNoEffect)) |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5470 | return true; |
Douglas Gregor | e885e18 | 2011-05-21 18:53:30 +0000 | [diff] [blame] | 5471 | |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5472 | // Mark the prior declaration as an explicit specialization, so that later |
| 5473 | // clients know that this is an explicit specialization. |
Argyrios Kyrtzidis | bbc6454 | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 5474 | if (!isFriend) { |
John McCall | 7ad650f | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 5475 | SpecInfo->setTemplateSpecializationKind(TSK_ExplicitSpecialization); |
Argyrios Kyrtzidis | bbc6454 | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 5476 | MarkUnusedFileScopedDecl(Specialization); |
| 5477 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5478 | |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5479 | // Turn the given function declaration into a function template |
| 5480 | // specialization, with the template arguments from the previous |
| 5481 | // specialization. |
Abramo Bagnara | e03db98 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 5482 | // Take copies of (semantic and syntactic) template argument lists. |
| 5483 | const TemplateArgumentList* TemplArgs = new (Context) |
| 5484 | TemplateArgumentList(Specialization->getTemplateSpecializationArgs()); |
| 5485 | const TemplateArgumentListInfo* TemplArgsAsWritten = ExplicitTemplateArgs |
| 5486 | ? new (Context) TemplateArgumentListInfo(*ExplicitTemplateArgs) : 0; |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 5487 | FD->setFunctionTemplateSpecialization(Specialization->getPrimaryTemplate(), |
Abramo Bagnara | e03db98 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 5488 | TemplArgs, /*InsertPos=*/0, |
| 5489 | SpecInfo->getTemplateSpecializationKind(), |
| 5490 | TemplArgsAsWritten); |
Douglas Gregor | e885e18 | 2011-05-21 18:53:30 +0000 | [diff] [blame] | 5491 | FD->setStorageClass(Specialization->getStorageClass()); |
| 5492 | |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5493 | // The "previous declaration" for this function template specialization is |
| 5494 | // the prior function template specialization. |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5495 | Previous.clear(); |
| 5496 | Previous.addDecl(Specialization); |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5497 | return false; |
| 5498 | } |
| 5499 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5500 | /// \brief Perform semantic analysis for the given non-template member |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5501 | /// specialization. |
| 5502 | /// |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5503 | /// This routine performs all of the semantic analysis required for an |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5504 | /// explicit member function specialization. On successful completion, |
| 5505 | /// the function declaration \p FD will become a member function |
| 5506 | /// specialization. |
| 5507 | /// |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5508 | /// \param Member the member declaration, which will be updated to become a |
| 5509 | /// specialization. |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5510 | /// |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5511 | /// \param Previous the set of declarations, one of which may be specialized |
| 5512 | /// by this function specialization; the set will be modified to contain the |
| 5513 | /// redeclared member. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5514 | bool |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5515 | Sema::CheckMemberSpecialization(NamedDecl *Member, LookupResult &Previous) { |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5516 | assert(!isa<TemplateDecl>(Member) && "Only for non-template members"); |
John McCall | 77e8b11 | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 5517 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5518 | // Try to find the member we are instantiating. |
| 5519 | NamedDecl *Instantiation = 0; |
| 5520 | NamedDecl *InstantiatedFrom = 0; |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5521 | MemberSpecializationInfo *MSInfo = 0; |
| 5522 | |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5523 | if (Previous.empty()) { |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5524 | // Nowhere to look anyway. |
| 5525 | } else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Member)) { |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5526 | for (LookupResult::iterator I = Previous.begin(), E = Previous.end(); |
| 5527 | I != E; ++I) { |
| 5528 | NamedDecl *D = (*I)->getUnderlyingDecl(); |
| 5529 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) { |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5530 | if (Context.hasSameType(Function->getType(), Method->getType())) { |
| 5531 | Instantiation = Method; |
| 5532 | InstantiatedFrom = Method->getInstantiatedFromMemberFunction(); |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5533 | MSInfo = Method->getMemberSpecializationInfo(); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5534 | break; |
| 5535 | } |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5536 | } |
| 5537 | } |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5538 | } else if (isa<VarDecl>(Member)) { |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5539 | VarDecl *PrevVar; |
| 5540 | if (Previous.isSingleResult() && |
| 5541 | (PrevVar = dyn_cast<VarDecl>(Previous.getFoundDecl()))) |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5542 | if (PrevVar->isStaticDataMember()) { |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5543 | Instantiation = PrevVar; |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5544 | InstantiatedFrom = PrevVar->getInstantiatedFromStaticDataMember(); |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5545 | MSInfo = PrevVar->getMemberSpecializationInfo(); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5546 | } |
| 5547 | } else if (isa<RecordDecl>(Member)) { |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5548 | CXXRecordDecl *PrevRecord; |
| 5549 | if (Previous.isSingleResult() && |
| 5550 | (PrevRecord = dyn_cast<CXXRecordDecl>(Previous.getFoundDecl()))) { |
| 5551 | Instantiation = PrevRecord; |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5552 | InstantiatedFrom = PrevRecord->getInstantiatedFromMemberClass(); |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5553 | MSInfo = PrevRecord->getMemberSpecializationInfo(); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5554 | } |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5555 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5556 | |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5557 | if (!Instantiation) { |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5558 | // There is no previous declaration that matches. Since member |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5559 | // specializations are always out-of-line, the caller will complain about |
| 5560 | // this mismatch later. |
| 5561 | return false; |
| 5562 | } |
John McCall | 77e8b11 | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 5563 | |
| 5564 | // If this is a friend, just bail out here before we start turning |
| 5565 | // things into explicit specializations. |
| 5566 | if (Member->getFriendObjectKind() != Decl::FOK_None) { |
| 5567 | // Preserve instantiation information. |
| 5568 | if (InstantiatedFrom && isa<CXXMethodDecl>(Member)) { |
| 5569 | cast<CXXMethodDecl>(Member)->setInstantiationOfMemberFunction( |
| 5570 | cast<CXXMethodDecl>(InstantiatedFrom), |
| 5571 | cast<CXXMethodDecl>(Instantiation)->getTemplateSpecializationKind()); |
| 5572 | } else if (InstantiatedFrom && isa<CXXRecordDecl>(Member)) { |
| 5573 | cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass( |
| 5574 | cast<CXXRecordDecl>(InstantiatedFrom), |
| 5575 | cast<CXXRecordDecl>(Instantiation)->getTemplateSpecializationKind()); |
| 5576 | } |
| 5577 | |
| 5578 | Previous.clear(); |
| 5579 | Previous.addDecl(Instantiation); |
| 5580 | return false; |
| 5581 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5582 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5583 | // Make sure that this is a specialization of a member. |
| 5584 | if (!InstantiatedFrom) { |
| 5585 | Diag(Member->getLocation(), diag::err_spec_member_not_instantiated) |
| 5586 | << Member; |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5587 | Diag(Instantiation->getLocation(), diag::note_specialized_decl); |
| 5588 | return true; |
| 5589 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5590 | |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5591 | // C++ [temp.expl.spec]p6: |
| 5592 | // If a template, a member template or the member of a class template is |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5593 | // explicitly specialized then that spe- cialization shall be declared |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5594 | // before the first use of that specialization that would cause an implicit |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5595 | // instantiation to take place, in every translation unit in which such a |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5596 | // use occurs; no diagnostic is required. |
| 5597 | assert(MSInfo && "Member specialization info missing?"); |
John McCall | 7504239 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 5598 | |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5599 | bool HasNoEffect = false; |
John McCall | 7504239 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 5600 | if (CheckSpecializationInstantiationRedecl(Member->getLocation(), |
| 5601 | TSK_ExplicitSpecialization, |
| 5602 | Instantiation, |
| 5603 | MSInfo->getTemplateSpecializationKind(), |
| 5604 | MSInfo->getPointOfInstantiation(), |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5605 | HasNoEffect)) |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5606 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5607 | |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5608 | // Check the scope of this explicit specialization. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5609 | if (CheckTemplateSpecializationScope(*this, |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5610 | InstantiatedFrom, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5611 | Instantiation, Member->getLocation(), |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 5612 | false)) |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5613 | return true; |
Douglas Gregor | 2db3232 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 5614 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5615 | // Note that this is an explicit instantiation of a member. |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 5616 | // the original declaration to note that it is an explicit specialization |
| 5617 | // (if it was previously an implicit instantiation). This latter step |
| 5618 | // makes bookkeeping easier. |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5619 | if (isa<FunctionDecl>(Member)) { |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 5620 | FunctionDecl *InstantiationFunction = cast<FunctionDecl>(Instantiation); |
| 5621 | if (InstantiationFunction->getTemplateSpecializationKind() == |
| 5622 | TSK_ImplicitInstantiation) { |
| 5623 | InstantiationFunction->setTemplateSpecializationKind( |
| 5624 | TSK_ExplicitSpecialization); |
| 5625 | InstantiationFunction->setLocation(Member->getLocation()); |
| 5626 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5627 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5628 | cast<FunctionDecl>(Member)->setInstantiationOfMemberFunction( |
| 5629 | cast<CXXMethodDecl>(InstantiatedFrom), |
| 5630 | TSK_ExplicitSpecialization); |
Argyrios Kyrtzidis | bbc6454 | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 5631 | MarkUnusedFileScopedDecl(InstantiationFunction); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5632 | } else if (isa<VarDecl>(Member)) { |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 5633 | VarDecl *InstantiationVar = cast<VarDecl>(Instantiation); |
| 5634 | if (InstantiationVar->getTemplateSpecializationKind() == |
| 5635 | TSK_ImplicitInstantiation) { |
| 5636 | InstantiationVar->setTemplateSpecializationKind( |
| 5637 | TSK_ExplicitSpecialization); |
| 5638 | InstantiationVar->setLocation(Member->getLocation()); |
| 5639 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5640 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5641 | Context.setInstantiatedFromStaticDataMember(cast<VarDecl>(Member), |
| 5642 | cast<VarDecl>(InstantiatedFrom), |
| 5643 | TSK_ExplicitSpecialization); |
Argyrios Kyrtzidis | bbc6454 | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 5644 | MarkUnusedFileScopedDecl(InstantiationVar); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5645 | } else { |
| 5646 | assert(isa<CXXRecordDecl>(Member) && "Only member classes remain"); |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 5647 | CXXRecordDecl *InstantiationClass = cast<CXXRecordDecl>(Instantiation); |
| 5648 | if (InstantiationClass->getTemplateSpecializationKind() == |
| 5649 | TSK_ImplicitInstantiation) { |
| 5650 | InstantiationClass->setTemplateSpecializationKind( |
| 5651 | TSK_ExplicitSpecialization); |
| 5652 | InstantiationClass->setLocation(Member->getLocation()); |
| 5653 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5654 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5655 | cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass( |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 5656 | cast<CXXRecordDecl>(InstantiatedFrom), |
| 5657 | TSK_ExplicitSpecialization); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5658 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5659 | |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5660 | // Save the caller the trouble of having to figure out which declaration |
| 5661 | // this specialization matches. |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5662 | Previous.clear(); |
| 5663 | Previous.addDecl(Instantiation); |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5664 | return false; |
| 5665 | } |
| 5666 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5667 | /// \brief Check the scope of an explicit instantiation. |
Douglas Gregor | 669eed8 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 5668 | /// |
| 5669 | /// \returns true if a serious error occurs, false otherwise. |
| 5670 | static bool CheckExplicitInstantiationScope(Sema &S, NamedDecl *D, |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5671 | SourceLocation InstLoc, |
| 5672 | bool WasQualifiedName) { |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 5673 | DeclContext *OrigContext= D->getDeclContext()->getEnclosingNamespaceContext(); |
| 5674 | DeclContext *CurContext = S.CurContext->getRedeclContext(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5675 | |
Douglas Gregor | 669eed8 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 5676 | if (CurContext->isRecord()) { |
| 5677 | S.Diag(InstLoc, diag::err_explicit_instantiation_in_class) |
| 5678 | << D; |
| 5679 | return true; |
| 5680 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5681 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5682 | // C++0x [temp.explicit]p2: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5683 | // An explicit instantiation shall appear in an enclosing namespace of its |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5684 | // template. |
| 5685 | // |
| 5686 | // This is DR275, which we do not retroactively apply to C++98/03. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5687 | if (S.getLangOptions().CPlusPlus0x && |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 5688 | !CurContext->Encloses(OrigContext)) { |
| 5689 | if (NamespaceDecl *NS = dyn_cast<NamespaceDecl>(OrigContext)) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5690 | S.Diag(InstLoc, |
| 5691 | S.getLangOptions().CPlusPlus0x? |
Douglas Gregor | 2166beb | 2010-05-11 17:39:34 +0000 | [diff] [blame] | 5692 | diag::err_explicit_instantiation_out_of_scope |
| 5693 | : diag::warn_explicit_instantiation_out_of_scope_0x) |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5694 | << D << NS; |
| 5695 | else |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5696 | S.Diag(InstLoc, |
Douglas Gregor | 2166beb | 2010-05-11 17:39:34 +0000 | [diff] [blame] | 5697 | S.getLangOptions().CPlusPlus0x? |
| 5698 | diag::err_explicit_instantiation_must_be_global |
| 5699 | : diag::warn_explicit_instantiation_out_of_scope_0x) |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5700 | << D; |
| 5701 | S.Diag(D->getLocation(), diag::note_explicit_instantiation_here); |
Douglas Gregor | 669eed8 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 5702 | return false; |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5703 | } |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 5704 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5705 | // C++0x [temp.explicit]p2: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5706 | // If the name declared in the explicit instantiation is an unqualified |
| 5707 | // name, the explicit instantiation shall appear in the namespace where |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5708 | // its template is declared or, if that namespace is inline (7.3.1), any |
| 5709 | // namespace from its enclosing namespace set. |
| 5710 | if (WasQualifiedName) |
Douglas Gregor | 669eed8 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 5711 | return false; |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 5712 | |
| 5713 | if (CurContext->InEnclosingNamespaceSetOf(OrigContext)) |
Douglas Gregor | 669eed8 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 5714 | return false; |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 5715 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5716 | S.Diag(InstLoc, |
Douglas Gregor | 2166beb | 2010-05-11 17:39:34 +0000 | [diff] [blame] | 5717 | S.getLangOptions().CPlusPlus0x? |
| 5718 | diag::err_explicit_instantiation_unqualified_wrong_namespace |
| 5719 | : diag::warn_explicit_instantiation_unqualified_wrong_namespace_0x) |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 5720 | << D << OrigContext; |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5721 | S.Diag(D->getLocation(), diag::note_explicit_instantiation_here); |
Douglas Gregor | 669eed8 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 5722 | return false; |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5723 | } |
| 5724 | |
| 5725 | /// \brief Determine whether the given scope specifier has a template-id in it. |
| 5726 | static bool ScopeSpecifierHasTemplateId(const CXXScopeSpec &SS) { |
| 5727 | if (!SS.isSet()) |
| 5728 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5729 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5730 | // C++0x [temp.explicit]p2: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5731 | // If the explicit instantiation is for a member function, a member class |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5732 | // or a static data member of a class template specialization, the name of |
| 5733 | // the class template specialization in the qualified-id for the member |
| 5734 | // name shall be a simple-template-id. |
| 5735 | // |
| 5736 | // C++98 has the same restriction, just worded differently. |
| 5737 | for (NestedNameSpecifier *NNS = (NestedNameSpecifier *)SS.getScopeRep(); |
| 5738 | NNS; NNS = NNS->getPrefix()) |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 5739 | if (const Type *T = NNS->getAsType()) |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5740 | if (isa<TemplateSpecializationType>(T)) |
| 5741 | return true; |
| 5742 | |
| 5743 | return false; |
| 5744 | } |
| 5745 | |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 5746 | // Explicit instantiation of a class template specialization |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5747 | DeclResult |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5748 | Sema::ActOnExplicitInstantiation(Scope *S, |
Douglas Gregor | 45f9655 | 2009-09-04 06:33:52 +0000 | [diff] [blame] | 5749 | SourceLocation ExternLoc, |
| 5750 | SourceLocation TemplateLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5751 | unsigned TagSpec, |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5752 | SourceLocation KWLoc, |
| 5753 | const CXXScopeSpec &SS, |
| 5754 | TemplateTy TemplateD, |
| 5755 | SourceLocation TemplateNameLoc, |
| 5756 | SourceLocation LAngleLoc, |
| 5757 | ASTTemplateArgsPtr TemplateArgsIn, |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5758 | SourceLocation RAngleLoc, |
| 5759 | AttributeList *Attr) { |
| 5760 | // Find the class template we're specializing |
| 5761 | TemplateName Name = TemplateD.getAsVal<TemplateName>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5762 | ClassTemplateDecl *ClassTemplate |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5763 | = cast<ClassTemplateDecl>(Name.getAsTemplateDecl()); |
| 5764 | |
| 5765 | // Check that the specialization uses the same tag kind as the |
| 5766 | // original template. |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 5767 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
| 5768 | assert(Kind != TTK_Enum && |
| 5769 | "Invalid enum tag in class template explicit instantiation!"); |
Douglas Gregor | 501c5ce | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 5770 | if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(), |
Richard Trieu | bbf34c0 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 5771 | Kind, /*isDefinition*/false, KWLoc, |
Douglas Gregor | 501c5ce | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 5772 | *ClassTemplate->getIdentifier())) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5773 | Diag(KWLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5774 | << ClassTemplate |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 5775 | << FixItHint::CreateReplacement(KWLoc, |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5776 | ClassTemplate->getTemplatedDecl()->getKindName()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5777 | Diag(ClassTemplate->getTemplatedDecl()->getLocation(), |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5778 | diag::note_previous_use); |
| 5779 | Kind = ClassTemplate->getTemplatedDecl()->getTagKind(); |
| 5780 | } |
| 5781 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5782 | // C++0x [temp.explicit]p2: |
| 5783 | // There are two forms of explicit instantiation: an explicit instantiation |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5784 | // definition and an explicit instantiation declaration. An explicit |
| 5785 | // instantiation declaration begins with the extern keyword. [...] |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5786 | TemplateSpecializationKind TSK |
| 5787 | = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition |
| 5788 | : TSK_ExplicitInstantiationDeclaration; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5789 | |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5790 | // Translate the parser's template argument list in our AST format. |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 5791 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
Douglas Gregor | 314b97f | 2009-11-10 19:49:08 +0000 | [diff] [blame] | 5792 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5793 | |
| 5794 | // Check that the template argument list is well-formed for this |
| 5795 | // template. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5796 | SmallVector<TemplateArgument, 4> Converted; |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 5797 | if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc, |
| 5798 | TemplateArgs, false, Converted)) |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5799 | return true; |
| 5800 | |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 5801 | assert((Converted.size() == ClassTemplate->getTemplateParameters()->size()) && |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5802 | "Converted template argument list is too short!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5803 | |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5804 | // Find the class template specialization declaration that |
| 5805 | // corresponds to these arguments. |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5806 | void *InsertPos = 0; |
| 5807 | ClassTemplateSpecializationDecl *PrevDecl |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 5808 | = ClassTemplate->findSpecialization(Converted.data(), |
| 5809 | Converted.size(), InsertPos); |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5810 | |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5811 | TemplateSpecializationKind PrevDecl_TSK |
| 5812 | = PrevDecl ? PrevDecl->getTemplateSpecializationKind() : TSK_Undeclared; |
| 5813 | |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5814 | // C++0x [temp.explicit]p2: |
| 5815 | // [...] An explicit instantiation shall appear in an enclosing |
| 5816 | // namespace of its template. [...] |
| 5817 | // |
| 5818 | // This is C++ DR 275. |
Douglas Gregor | 669eed8 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 5819 | if (CheckExplicitInstantiationScope(*this, ClassTemplate, TemplateNameLoc, |
| 5820 | SS.isSet())) |
| 5821 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5822 | |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5823 | ClassTemplateSpecializationDecl *Specialization = 0; |
| 5824 | |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5825 | bool HasNoEffect = false; |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5826 | if (PrevDecl) { |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 5827 | if (CheckSpecializationInstantiationRedecl(TemplateNameLoc, TSK, |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5828 | PrevDecl, PrevDecl_TSK, |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 5829 | PrevDecl->getPointOfInstantiation(), |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5830 | HasNoEffect)) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5831 | return PrevDecl; |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5832 | |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5833 | // Even though HasNoEffect == true means that this explicit instantiation |
| 5834 | // has no effect on semantics, we go on to put its syntax in the AST. |
| 5835 | |
| 5836 | if (PrevDecl_TSK == TSK_ImplicitInstantiation || |
| 5837 | PrevDecl_TSK == TSK_Undeclared) { |
Douglas Gregor | 52604ab | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 5838 | // Since the only prior class template specialization with these |
| 5839 | // arguments was referenced but not declared, reuse that |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5840 | // declaration node as our own, updating the source location |
| 5841 | // for the template name to reflect our new declaration. |
| 5842 | // (Other source locations will be updated later.) |
Douglas Gregor | 52604ab | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 5843 | Specialization = PrevDecl; |
| 5844 | Specialization->setLocation(TemplateNameLoc); |
| 5845 | PrevDecl = 0; |
| 5846 | } |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 5847 | } |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5848 | |
Douglas Gregor | 52604ab | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 5849 | if (!Specialization) { |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5850 | // Create a new class template specialization declaration node for |
| 5851 | // this explicit specialization. |
| 5852 | Specialization |
Douglas Gregor | 13c8577 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 5853 | = ClassTemplateSpecializationDecl::Create(Context, Kind, |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5854 | ClassTemplate->getDeclContext(), |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 5855 | KWLoc, TemplateNameLoc, |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5856 | ClassTemplate, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 5857 | Converted.data(), |
| 5858 | Converted.size(), |
| 5859 | PrevDecl); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 5860 | SetNestedNameSpecifier(Specialization, SS); |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5861 | |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 5862 | if (!HasNoEffect && !PrevDecl) { |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5863 | // Insert the new specialization. |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 5864 | ClassTemplate->AddSpecialization(Specialization, InsertPos); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5865 | } |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5866 | } |
| 5867 | |
| 5868 | // Build the fully-sugared type for this explicit instantiation as |
| 5869 | // the user wrote in the explicit instantiation itself. This means |
| 5870 | // that we'll pretty-print the type retrieved from the |
| 5871 | // specialization's declaration the way that the user actually wrote |
| 5872 | // the explicit instantiation, rather than formatting the name based |
| 5873 | // on the "canonical" representation used to store the template |
| 5874 | // arguments in the specialization. |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 5875 | TypeSourceInfo *WrittenTy |
| 5876 | = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc, |
| 5877 | TemplateArgs, |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5878 | Context.getTypeDeclType(Specialization)); |
| 5879 | Specialization->setTypeAsWritten(WrittenTy); |
| 5880 | TemplateArgsIn.release(); |
| 5881 | |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5882 | // Set source locations for keywords. |
| 5883 | Specialization->setExternLoc(ExternLoc); |
| 5884 | Specialization->setTemplateKeywordLoc(TemplateLoc); |
| 5885 | |
| 5886 | // Add the explicit instantiation into its lexical context. However, |
| 5887 | // since explicit instantiations are never found by name lookup, we |
| 5888 | // just put it into the declaration context directly. |
| 5889 | Specialization->setLexicalDeclContext(CurContext); |
| 5890 | CurContext->addDecl(Specialization); |
| 5891 | |
| 5892 | // Syntax is now OK, so return if it has no other effect on semantics. |
| 5893 | if (HasNoEffect) { |
| 5894 | // Set the template specialization kind. |
| 5895 | Specialization->setTemplateSpecializationKind(TSK); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5896 | return Specialization; |
Douglas Gregor | d78f598 | 2009-11-25 06:01:46 +0000 | [diff] [blame] | 5897 | } |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5898 | |
| 5899 | // C++ [temp.explicit]p3: |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5900 | // A definition of a class template or class member template |
| 5901 | // shall be in scope at the point of the explicit instantiation of |
| 5902 | // the class template or class member template. |
| 5903 | // |
| 5904 | // This check comes when we actually try to perform the |
| 5905 | // instantiation. |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 5906 | ClassTemplateSpecializationDecl *Def |
| 5907 | = cast_or_null<ClassTemplateSpecializationDecl>( |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 5908 | Specialization->getDefinition()); |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 5909 | if (!Def) |
Douglas Gregor | 972e6ce | 2009-10-27 06:26:26 +0000 | [diff] [blame] | 5910 | InstantiateClassTemplateSpecialization(TemplateNameLoc, Specialization, TSK); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5911 | else if (TSK == TSK_ExplicitInstantiationDefinition) { |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 5912 | MarkVTableUsed(TemplateNameLoc, Specialization, true); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5913 | Specialization->setPointOfInstantiation(Def->getPointOfInstantiation()); |
| 5914 | } |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 5915 | |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 5916 | // Instantiate the members of this class template specialization. |
| 5917 | Def = cast_or_null<ClassTemplateSpecializationDecl>( |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 5918 | Specialization->getDefinition()); |
Rafael Espindola | b0f65ca | 2010-03-22 23:12:48 +0000 | [diff] [blame] | 5919 | if (Def) { |
Rafael Espindola | f075b22 | 2010-03-23 19:55:22 +0000 | [diff] [blame] | 5920 | TemplateSpecializationKind Old_TSK = Def->getTemplateSpecializationKind(); |
| 5921 | |
| 5922 | // Fix a TSK_ExplicitInstantiationDeclaration followed by a |
| 5923 | // TSK_ExplicitInstantiationDefinition |
| 5924 | if (Old_TSK == TSK_ExplicitInstantiationDeclaration && |
| 5925 | TSK == TSK_ExplicitInstantiationDefinition) |
| 5926 | Def->setTemplateSpecializationKind(TSK); |
Rafael Espindola | b0f65ca | 2010-03-22 23:12:48 +0000 | [diff] [blame] | 5927 | |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 5928 | InstantiateClassTemplateSpecializationMembers(TemplateNameLoc, Def, TSK); |
Rafael Espindola | b0f65ca | 2010-03-22 23:12:48 +0000 | [diff] [blame] | 5929 | } |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5930 | |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5931 | // Set the template specialization kind. |
| 5932 | Specialization->setTemplateSpecializationKind(TSK); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5933 | return Specialization; |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5934 | } |
| 5935 | |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 5936 | // Explicit instantiation of a member class of a class template. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5937 | DeclResult |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5938 | Sema::ActOnExplicitInstantiation(Scope *S, |
Douglas Gregor | 45f9655 | 2009-09-04 06:33:52 +0000 | [diff] [blame] | 5939 | SourceLocation ExternLoc, |
| 5940 | SourceLocation TemplateLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5941 | unsigned TagSpec, |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 5942 | SourceLocation KWLoc, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 5943 | CXXScopeSpec &SS, |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 5944 | IdentifierInfo *Name, |
| 5945 | SourceLocation NameLoc, |
| 5946 | AttributeList *Attr) { |
| 5947 | |
Douglas Gregor | 402abb5 | 2009-05-28 23:31:59 +0000 | [diff] [blame] | 5948 | bool Owned = false; |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 5949 | bool IsDependent = false; |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5950 | Decl *TagD = ActOnTag(S, TagSpec, Sema::TUK_Reference, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5951 | KWLoc, SS, Name, NameLoc, Attr, AS_none, |
| 5952 | MultiTemplateParamsArg(*this, 0, 0), |
Abramo Bagnara | a88cefd | 2010-12-03 18:54:17 +0000 | [diff] [blame] | 5953 | Owned, IsDependent, false, false, |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 5954 | TypeResult()); |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 5955 | assert(!IsDependent && "explicit instantiation of dependent name not yet handled"); |
| 5956 | |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 5957 | if (!TagD) |
| 5958 | return true; |
| 5959 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5960 | TagDecl *Tag = cast<TagDecl>(TagD); |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 5961 | if (Tag->isEnum()) { |
| 5962 | Diag(TemplateLoc, diag::err_explicit_instantiation_enum) |
| 5963 | << Context.getTypeDeclType(Tag); |
| 5964 | return true; |
| 5965 | } |
| 5966 | |
Douglas Gregor | d0c8737 | 2009-05-27 17:30:49 +0000 | [diff] [blame] | 5967 | if (Tag->isInvalidDecl()) |
| 5968 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5969 | |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 5970 | CXXRecordDecl *Record = cast<CXXRecordDecl>(Tag); |
| 5971 | CXXRecordDecl *Pattern = Record->getInstantiatedFromMemberClass(); |
| 5972 | if (!Pattern) { |
| 5973 | Diag(TemplateLoc, diag::err_explicit_instantiation_nontemplate_type) |
| 5974 | << Context.getTypeDeclType(Record); |
| 5975 | Diag(Record->getLocation(), diag::note_nontemplate_decl_here); |
| 5976 | return true; |
| 5977 | } |
| 5978 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5979 | // C++0x [temp.explicit]p2: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5980 | // If the explicit instantiation is for a class or member class, the |
| 5981 | // elaborated-type-specifier in the declaration shall include a |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5982 | // simple-template-id. |
| 5983 | // |
| 5984 | // C++98 has the same restriction, just worded differently. |
| 5985 | if (!ScopeSpecifierHasTemplateId(SS)) |
Douglas Gregor | a2dd828 | 2010-06-16 16:26:47 +0000 | [diff] [blame] | 5986 | Diag(TemplateLoc, diag::ext_explicit_instantiation_without_qualified_id) |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5987 | << Record << SS.getRange(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5988 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5989 | // C++0x [temp.explicit]p2: |
| 5990 | // There are two forms of explicit instantiation: an explicit instantiation |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5991 | // definition and an explicit instantiation declaration. An explicit |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5992 | // instantiation declaration begins with the extern keyword. [...] |
Douglas Gregor | a74bbe2 | 2009-10-14 21:46:58 +0000 | [diff] [blame] | 5993 | TemplateSpecializationKind TSK |
| 5994 | = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition |
| 5995 | : TSK_ExplicitInstantiationDeclaration; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5996 | |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 5997 | // C++0x [temp.explicit]p2: |
| 5998 | // [...] An explicit instantiation shall appear in an enclosing |
| 5999 | // namespace of its template. [...] |
| 6000 | // |
| 6001 | // This is C++ DR 275. |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6002 | CheckExplicitInstantiationScope(*this, Record, NameLoc, true); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6003 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6004 | // Verify that it is okay to explicitly instantiate here. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6005 | CXXRecordDecl *PrevDecl |
Douglas Gregor | 583f33b | 2009-10-15 18:07:02 +0000 | [diff] [blame] | 6006 | = cast_or_null<CXXRecordDecl>(Record->getPreviousDeclaration()); |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 6007 | if (!PrevDecl && Record->getDefinition()) |
Douglas Gregor | 583f33b | 2009-10-15 18:07:02 +0000 | [diff] [blame] | 6008 | PrevDecl = Record; |
| 6009 | if (PrevDecl) { |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6010 | MemberSpecializationInfo *MSInfo = PrevDecl->getMemberSpecializationInfo(); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6011 | bool HasNoEffect = false; |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6012 | assert(MSInfo && "No member specialization information?"); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6013 | if (CheckSpecializationInstantiationRedecl(TemplateLoc, TSK, |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6014 | PrevDecl, |
| 6015 | MSInfo->getTemplateSpecializationKind(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6016 | MSInfo->getPointOfInstantiation(), |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6017 | HasNoEffect)) |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6018 | return true; |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6019 | if (HasNoEffect) |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6020 | return TagD; |
| 6021 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6022 | |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 6023 | CXXRecordDecl *RecordDef |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 6024 | = cast_or_null<CXXRecordDecl>(Record->getDefinition()); |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 6025 | if (!RecordDef) { |
Douglas Gregor | bf7643e | 2009-10-15 12:53:22 +0000 | [diff] [blame] | 6026 | // C++ [temp.explicit]p3: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6027 | // A definition of a member class of a class template shall be in scope |
Douglas Gregor | bf7643e | 2009-10-15 12:53:22 +0000 | [diff] [blame] | 6028 | // at the point of an explicit instantiation of the member class. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6029 | CXXRecordDecl *Def |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 6030 | = cast_or_null<CXXRecordDecl>(Pattern->getDefinition()); |
Douglas Gregor | bf7643e | 2009-10-15 12:53:22 +0000 | [diff] [blame] | 6031 | if (!Def) { |
Douglas Gregor | e2d3a3d | 2009-10-15 14:05:49 +0000 | [diff] [blame] | 6032 | Diag(TemplateLoc, diag::err_explicit_instantiation_undefined_member) |
| 6033 | << 0 << Record->getDeclName() << Record->getDeclContext(); |
Douglas Gregor | bf7643e | 2009-10-15 12:53:22 +0000 | [diff] [blame] | 6034 | Diag(Pattern->getLocation(), diag::note_forward_declaration) |
| 6035 | << Pattern; |
| 6036 | return true; |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 6037 | } else { |
| 6038 | if (InstantiateClass(NameLoc, Record, Def, |
| 6039 | getTemplateInstantiationArgs(Record), |
| 6040 | TSK)) |
| 6041 | return true; |
| 6042 | |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 6043 | RecordDef = cast_or_null<CXXRecordDecl>(Record->getDefinition()); |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 6044 | if (!RecordDef) |
| 6045 | return true; |
| 6046 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6047 | } |
| 6048 | |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 6049 | // Instantiate all of the members of the class. |
| 6050 | InstantiateClassMembers(NameLoc, RecordDef, |
| 6051 | getTemplateInstantiationArgs(Record), TSK); |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 6052 | |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 6053 | if (TSK == TSK_ExplicitInstantiationDefinition) |
| 6054 | MarkVTableUsed(NameLoc, RecordDef, true); |
| 6055 | |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 6056 | // FIXME: We don't have any representation for explicit instantiations of |
| 6057 | // member classes. Such a representation is not needed for compilation, but it |
| 6058 | // should be available for clients that want to see all of the declarations in |
| 6059 | // the source code. |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 6060 | return TagD; |
| 6061 | } |
| 6062 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 6063 | DeclResult Sema::ActOnExplicitInstantiation(Scope *S, |
| 6064 | SourceLocation ExternLoc, |
| 6065 | SourceLocation TemplateLoc, |
| 6066 | Declarator &D) { |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6067 | // Explicit instantiations always require a name. |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 6068 | // TODO: check if/when DNInfo should replace Name. |
| 6069 | DeclarationNameInfo NameInfo = GetNameForDeclarator(D); |
| 6070 | DeclarationName Name = NameInfo.getName(); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6071 | if (!Name) { |
| 6072 | if (!D.isInvalidType()) |
| 6073 | Diag(D.getDeclSpec().getSourceRange().getBegin(), |
| 6074 | diag::err_explicit_instantiation_requires_name) |
| 6075 | << D.getDeclSpec().getSourceRange() |
| 6076 | << D.getSourceRange(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6077 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6078 | return true; |
| 6079 | } |
| 6080 | |
| 6081 | // The scope passed in may not be a decl scope. Zip up the scope tree until |
| 6082 | // we find one that is. |
| 6083 | while ((S->getFlags() & Scope::DeclScope) == 0 || |
| 6084 | (S->getFlags() & Scope::TemplateParamScope) != 0) |
| 6085 | S = S->getParent(); |
| 6086 | |
| 6087 | // Determine the type of the declaration. |
John McCall | bf1a028 | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 6088 | TypeSourceInfo *T = GetTypeForDeclarator(D, S); |
| 6089 | QualType R = T->getType(); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6090 | if (R.isNull()) |
| 6091 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6092 | |
Douglas Gregor | e885e18 | 2011-05-21 18:53:30 +0000 | [diff] [blame] | 6093 | // C++ [dcl.stc]p1: |
| 6094 | // A storage-class-specifier shall not be specified in [...] an explicit |
| 6095 | // instantiation (14.7.2) directive. |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6096 | if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef) { |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6097 | Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_of_typedef) |
| 6098 | << Name; |
| 6099 | return true; |
Douglas Gregor | e885e18 | 2011-05-21 18:53:30 +0000 | [diff] [blame] | 6100 | } else if (D.getDeclSpec().getStorageClassSpec() |
| 6101 | != DeclSpec::SCS_unspecified) { |
| 6102 | // Complain about then remove the storage class specifier. |
| 6103 | Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_storage_class) |
| 6104 | << FixItHint::CreateRemoval(D.getDeclSpec().getStorageClassSpecLoc()); |
| 6105 | |
| 6106 | D.getMutableDeclSpec().ClearStorageClassSpecs(); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6107 | } |
| 6108 | |
Douglas Gregor | 663b5a0 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 6109 | // C++0x [temp.explicit]p1: |
| 6110 | // [...] An explicit instantiation of a function template shall not use the |
| 6111 | // inline or constexpr specifiers. |
| 6112 | // Presumably, this also applies to member functions of class templates as |
| 6113 | // well. |
| 6114 | if (D.getDeclSpec().isInlineSpecified() && getLangOptions().CPlusPlus0x) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6115 | Diag(D.getDeclSpec().getInlineSpecLoc(), |
Douglas Gregor | 663b5a0 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 6116 | diag::err_explicit_instantiation_inline) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 6117 | <<FixItHint::CreateRemoval(D.getDeclSpec().getInlineSpecLoc()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6118 | |
Douglas Gregor | 663b5a0 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 6119 | // FIXME: check for constexpr specifier. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6120 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6121 | // C++0x [temp.explicit]p2: |
| 6122 | // There are two forms of explicit instantiation: an explicit instantiation |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6123 | // definition and an explicit instantiation declaration. An explicit |
| 6124 | // instantiation declaration begins with the extern keyword. [...] |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6125 | TemplateSpecializationKind TSK |
| 6126 | = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition |
| 6127 | : TSK_ExplicitInstantiationDeclaration; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6128 | |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 6129 | LookupResult Previous(*this, NameInfo, LookupOrdinaryName); |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 6130 | LookupParsedName(Previous, S, &D.getCXXScopeSpec()); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6131 | |
| 6132 | if (!R->isFunctionType()) { |
| 6133 | // C++ [temp.explicit]p1: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6134 | // A [...] static data member of a class template can be explicitly |
| 6135 | // instantiated from the member definition associated with its class |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6136 | // template. |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 6137 | if (Previous.isAmbiguous()) |
| 6138 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6139 | |
John McCall | 1bcee0a | 2009-12-02 08:25:40 +0000 | [diff] [blame] | 6140 | VarDecl *Prev = Previous.getAsSingle<VarDecl>(); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6141 | if (!Prev || !Prev->isStaticDataMember()) { |
| 6142 | // We expect to see a data data member here. |
| 6143 | Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_not_known) |
| 6144 | << Name; |
| 6145 | for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end(); |
| 6146 | P != PEnd; ++P) |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 6147 | Diag((*P)->getLocation(), diag::note_explicit_instantiation_here); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6148 | return true; |
| 6149 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6150 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6151 | if (!Prev->getInstantiatedFromStaticDataMember()) { |
| 6152 | // FIXME: Check for explicit specialization? |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6153 | Diag(D.getIdentifierLoc(), |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6154 | diag::err_explicit_instantiation_data_member_not_instantiated) |
| 6155 | << Prev; |
| 6156 | Diag(Prev->getLocation(), diag::note_explicit_instantiation_here); |
| 6157 | // FIXME: Can we provide a note showing where this was declared? |
| 6158 | return true; |
| 6159 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6160 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6161 | // C++0x [temp.explicit]p2: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6162 | // If the explicit instantiation is for a member function, a member class |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6163 | // or a static data member of a class template specialization, the name of |
| 6164 | // the class template specialization in the qualified-id for the member |
| 6165 | // name shall be a simple-template-id. |
| 6166 | // |
| 6167 | // C++98 has the same restriction, just worded differently. |
| 6168 | if (!ScopeSpecifierHasTemplateId(D.getCXXScopeSpec())) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6169 | Diag(D.getIdentifierLoc(), |
Douglas Gregor | a2dd828 | 2010-06-16 16:26:47 +0000 | [diff] [blame] | 6170 | diag::ext_explicit_instantiation_without_qualified_id) |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6171 | << Prev << D.getCXXScopeSpec().getRange(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6172 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6173 | // Check the scope of this explicit instantiation. |
| 6174 | CheckExplicitInstantiationScope(*this, Prev, D.getIdentifierLoc(), true); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6175 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6176 | // Verify that it is okay to explicitly instantiate here. |
| 6177 | MemberSpecializationInfo *MSInfo = Prev->getMemberSpecializationInfo(); |
| 6178 | assert(MSInfo && "Missing static data member specialization info?"); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6179 | bool HasNoEffect = false; |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 6180 | if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK, Prev, |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6181 | MSInfo->getTemplateSpecializationKind(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6182 | MSInfo->getPointOfInstantiation(), |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6183 | HasNoEffect)) |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6184 | return true; |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6185 | if (HasNoEffect) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6186 | return (Decl*) 0; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6187 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6188 | // Instantiate static data member. |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 6189 | Prev->setTemplateSpecializationKind(TSK, D.getIdentifierLoc()); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6190 | if (TSK == TSK_ExplicitInstantiationDefinition) |
Chandler Carruth | 58e390e | 2010-08-25 08:27:02 +0000 | [diff] [blame] | 6191 | InstantiateStaticDataMemberDefinition(D.getIdentifierLoc(), Prev); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6192 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6193 | // FIXME: Create an ExplicitInstantiation node? |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6194 | return (Decl*) 0; |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6195 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6196 | |
| 6197 | // If the declarator is a template-id, translate the parser's template |
Douglas Gregor | 0b60d9e | 2009-09-25 23:53:26 +0000 | [diff] [blame] | 6198 | // argument list into our AST format. |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 6199 | bool HasExplicitTemplateArgs = false; |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 6200 | TemplateArgumentListInfo TemplateArgs; |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 6201 | if (D.getName().getKind() == UnqualifiedId::IK_TemplateId) { |
| 6202 | TemplateIdAnnotation *TemplateId = D.getName().TemplateId; |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 6203 | TemplateArgs.setLAngleLoc(TemplateId->LAngleLoc); |
| 6204 | TemplateArgs.setRAngleLoc(TemplateId->RAngleLoc); |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 6205 | ASTTemplateArgsPtr TemplateArgsPtr(*this, |
| 6206 | TemplateId->getTemplateArgs(), |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 6207 | TemplateId->NumArgs); |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 6208 | translateTemplateArguments(TemplateArgsPtr, TemplateArgs); |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 6209 | HasExplicitTemplateArgs = true; |
Douglas Gregor | b2f81cf | 2009-10-01 23:51:25 +0000 | [diff] [blame] | 6210 | TemplateArgsPtr.release(); |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 6211 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6212 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6213 | // C++ [temp.explicit]p1: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6214 | // A [...] function [...] can be explicitly instantiated from its template. |
| 6215 | // A member function [...] of a class template can be explicitly |
| 6216 | // instantiated from the member definition associated with its class |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6217 | // template. |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6218 | UnresolvedSet<8> Matches; |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6219 | for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end(); |
| 6220 | P != PEnd; ++P) { |
| 6221 | NamedDecl *Prev = *P; |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 6222 | if (!HasExplicitTemplateArgs) { |
| 6223 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Prev)) { |
| 6224 | if (Context.hasSameUnqualifiedType(Method->getType(), R)) { |
| 6225 | Matches.clear(); |
Douglas Gregor | 48026d2 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 6226 | |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6227 | Matches.addDecl(Method, P.getAccess()); |
Douglas Gregor | 48026d2 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 6228 | if (Method->getTemplateSpecializationKind() == TSK_Undeclared) |
| 6229 | break; |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 6230 | } |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6231 | } |
| 6232 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6233 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6234 | FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Prev); |
| 6235 | if (!FunTmpl) |
| 6236 | continue; |
| 6237 | |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 6238 | TemplateDeductionInfo Info(Context, D.getIdentifierLoc()); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6239 | FunctionDecl *Specialization = 0; |
| 6240 | if (TemplateDeductionResult TDK |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6241 | = DeduceTemplateArguments(FunTmpl, |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 6242 | (HasExplicitTemplateArgs ? &TemplateArgs : 0), |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6243 | R, Specialization, Info)) { |
| 6244 | // FIXME: Keep track of almost-matches? |
| 6245 | (void)TDK; |
| 6246 | continue; |
| 6247 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6248 | |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6249 | Matches.addDecl(Specialization, P.getAccess()); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6250 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6251 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6252 | // Find the most specialized function template specialization. |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6253 | UnresolvedSetIterator Result |
Douglas Gregor | 5c7bf42 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 6254 | = getMostSpecialized(Matches.begin(), Matches.end(), TPOC_Other, 0, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6255 | D.getIdentifierLoc(), |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 6256 | PDiag(diag::err_explicit_instantiation_not_known) << Name, |
| 6257 | PDiag(diag::err_explicit_instantiation_ambiguous) << Name, |
| 6258 | PDiag(diag::note_explicit_instantiation_candidate)); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6259 | |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6260 | if (Result == Matches.end()) |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6261 | return true; |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6262 | |
| 6263 | // Ignore access control bits, we don't need them for redeclaration checking. |
| 6264 | FunctionDecl *Specialization = cast<FunctionDecl>(*Result); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6265 | |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 6266 | if (Specialization->getTemplateSpecializationKind() == TSK_Undeclared) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6267 | Diag(D.getIdentifierLoc(), |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6268 | diag::err_explicit_instantiation_member_function_not_instantiated) |
| 6269 | << Specialization |
| 6270 | << (Specialization->getTemplateSpecializationKind() == |
| 6271 | TSK_ExplicitSpecialization); |
| 6272 | Diag(Specialization->getLocation(), diag::note_explicit_instantiation_here); |
| 6273 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6274 | } |
| 6275 | |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 6276 | FunctionDecl *PrevDecl = Specialization->getPreviousDeclaration(); |
Douglas Gregor | 583f33b | 2009-10-15 18:07:02 +0000 | [diff] [blame] | 6277 | if (!PrevDecl && Specialization->isThisDeclarationADefinition()) |
| 6278 | PrevDecl = Specialization; |
| 6279 | |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 6280 | if (PrevDecl) { |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6281 | bool HasNoEffect = false; |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 6282 | if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6283 | PrevDecl, |
| 6284 | PrevDecl->getTemplateSpecializationKind(), |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 6285 | PrevDecl->getPointOfInstantiation(), |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6286 | HasNoEffect)) |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 6287 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6288 | |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 6289 | // FIXME: We may still want to build some representation of this |
| 6290 | // explicit specialization. |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6291 | if (HasNoEffect) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6292 | return (Decl*) 0; |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 6293 | } |
Anders Carlsson | 26d6e9d | 2009-11-24 05:34:41 +0000 | [diff] [blame] | 6294 | |
| 6295 | Specialization->setTemplateSpecializationKind(TSK, D.getIdentifierLoc()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6296 | |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 6297 | if (TSK == TSK_ExplicitInstantiationDefinition) |
Chandler Carruth | 58e390e | 2010-08-25 08:27:02 +0000 | [diff] [blame] | 6298 | InstantiateFunctionDefinition(D.getIdentifierLoc(), Specialization); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6299 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6300 | // C++0x [temp.explicit]p2: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6301 | // If the explicit instantiation is for a member function, a member class |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6302 | // or a static data member of a class template specialization, the name of |
| 6303 | // the class template specialization in the qualified-id for the member |
| 6304 | // name shall be a simple-template-id. |
| 6305 | // |
| 6306 | // C++98 has the same restriction, just worded differently. |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 6307 | FunctionTemplateDecl *FunTmpl = Specialization->getPrimaryTemplate(); |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 6308 | if (D.getName().getKind() != UnqualifiedId::IK_TemplateId && !FunTmpl && |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6309 | D.getCXXScopeSpec().isSet() && |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6310 | !ScopeSpecifierHasTemplateId(D.getCXXScopeSpec())) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6311 | Diag(D.getIdentifierLoc(), |
Douglas Gregor | a2dd828 | 2010-06-16 16:26:47 +0000 | [diff] [blame] | 6312 | diag::ext_explicit_instantiation_without_qualified_id) |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6313 | << Specialization << D.getCXXScopeSpec().getRange(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6314 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6315 | CheckExplicitInstantiationScope(*this, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6316 | FunTmpl? (NamedDecl *)FunTmpl |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6317 | : Specialization->getInstantiatedFromMemberFunction(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6318 | D.getIdentifierLoc(), |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6319 | D.getCXXScopeSpec().isSet()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6320 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6321 | // FIXME: Create some kind of ExplicitInstantiationDecl here. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6322 | return (Decl*) 0; |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6323 | } |
| 6324 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 6325 | TypeResult |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 6326 | Sema::ActOnDependentTag(Scope *S, unsigned TagSpec, TagUseKind TUK, |
| 6327 | const CXXScopeSpec &SS, IdentifierInfo *Name, |
| 6328 | SourceLocation TagLoc, SourceLocation NameLoc) { |
| 6329 | // This has to hold, because SS is expected to be defined. |
| 6330 | assert(Name && "Expected a name in a dependent tag"); |
| 6331 | |
| 6332 | NestedNameSpecifier *NNS |
| 6333 | = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); |
| 6334 | if (!NNS) |
| 6335 | return true; |
| 6336 | |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 6337 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
Daniel Dunbar | 12c0ade | 2010-04-01 16:50:48 +0000 | [diff] [blame] | 6338 | |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 6339 | if (TUK == TUK_Declaration || TUK == TUK_Definition) { |
| 6340 | Diag(NameLoc, diag::err_dependent_tag_decl) |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 6341 | << (TUK == TUK_Definition) << Kind << SS.getRange(); |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 6342 | return true; |
| 6343 | } |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 6344 | |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 6345 | // Create the resulting type. |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 6346 | ElaboratedTypeKeyword Kwd = TypeWithKeyword::getKeywordForTagTypeKind(Kind); |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 6347 | QualType Result = Context.getDependentNameType(Kwd, NNS, Name); |
| 6348 | |
| 6349 | // Create type-source location information for this type. |
| 6350 | TypeLocBuilder TLB; |
| 6351 | DependentNameTypeLoc TL = TLB.push<DependentNameTypeLoc>(Result); |
| 6352 | TL.setKeywordLoc(TagLoc); |
| 6353 | TL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 6354 | TL.setNameLoc(NameLoc); |
| 6355 | return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result)); |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 6356 | } |
| 6357 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 6358 | TypeResult |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6359 | Sema::ActOnTypenameType(Scope *S, SourceLocation TypenameLoc, |
| 6360 | const CXXScopeSpec &SS, const IdentifierInfo &II, |
Douglas Gregor | 1a15dae | 2010-06-16 22:31:08 +0000 | [diff] [blame] | 6361 | SourceLocation IdLoc) { |
Douglas Gregor | e29425b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 6362 | if (SS.isInvalid()) |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6363 | return true; |
Douglas Gregor | e29425b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 6364 | |
Douglas Gregor | 1a15dae | 2010-06-16 22:31:08 +0000 | [diff] [blame] | 6365 | if (TypenameLoc.isValid() && S && !S->getTemplateParamParent() && |
| 6366 | !getLangOptions().CPlusPlus0x) |
| 6367 | Diag(TypenameLoc, diag::ext_typename_outside_of_template) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6368 | << FixItHint::CreateRemoval(TypenameLoc); |
| 6369 | |
Douglas Gregor | 2494dd0 | 2011-03-01 01:34:45 +0000 | [diff] [blame] | 6370 | NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context); |
Douglas Gregor | 9e87687 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 6371 | QualType T = CheckTypenameType(TypenameLoc.isValid()? ETK_Typename : ETK_None, |
| 6372 | TypenameLoc, QualifierLoc, II, IdLoc); |
Douglas Gregor | 31a19b6 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 6373 | if (T.isNull()) |
| 6374 | return true; |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 6375 | |
| 6376 | TypeSourceInfo *TSI = Context.CreateTypeSourceInfo(T); |
| 6377 | if (isa<DependentNameType>(T)) { |
| 6378 | DependentNameTypeLoc TL = cast<DependentNameTypeLoc>(TSI->getTypeLoc()); |
John McCall | 4e44983 | 2010-05-28 23:32:21 +0000 | [diff] [blame] | 6379 | TL.setKeywordLoc(TypenameLoc); |
Douglas Gregor | 2494dd0 | 2011-03-01 01:34:45 +0000 | [diff] [blame] | 6380 | TL.setQualifierLoc(QualifierLoc); |
John McCall | 4e44983 | 2010-05-28 23:32:21 +0000 | [diff] [blame] | 6381 | TL.setNameLoc(IdLoc); |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 6382 | } else { |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 6383 | ElaboratedTypeLoc TL = cast<ElaboratedTypeLoc>(TSI->getTypeLoc()); |
John McCall | 4e44983 | 2010-05-28 23:32:21 +0000 | [diff] [blame] | 6384 | TL.setKeywordLoc(TypenameLoc); |
Douglas Gregor | 9e87687 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 6385 | TL.setQualifierLoc(QualifierLoc); |
John McCall | 4e44983 | 2010-05-28 23:32:21 +0000 | [diff] [blame] | 6386 | cast<TypeSpecTypeLoc>(TL.getNamedTypeLoc()).setNameLoc(IdLoc); |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 6387 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6388 | |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 6389 | return CreateParsedType(T, TSI); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6390 | } |
| 6391 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 6392 | TypeResult |
Douglas Gregor | a02411e | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 6393 | Sema::ActOnTypenameType(Scope *S, SourceLocation TypenameLoc, |
| 6394 | const CXXScopeSpec &SS, |
| 6395 | SourceLocation TemplateLoc, |
| 6396 | TemplateTy TemplateIn, |
| 6397 | SourceLocation TemplateNameLoc, |
| 6398 | SourceLocation LAngleLoc, |
| 6399 | ASTTemplateArgsPtr TemplateArgsIn, |
| 6400 | SourceLocation RAngleLoc) { |
Douglas Gregor | 1a15dae | 2010-06-16 22:31:08 +0000 | [diff] [blame] | 6401 | if (TypenameLoc.isValid() && S && !S->getTemplateParamParent() && |
| 6402 | !getLangOptions().CPlusPlus0x) |
| 6403 | Diag(TypenameLoc, diag::ext_typename_outside_of_template) |
Douglas Gregor | a02411e | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 6404 | << FixItHint::CreateRemoval(TypenameLoc); |
| 6405 | |
| 6406 | // Translate the parser's template argument list in our AST format. |
| 6407 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
| 6408 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
| 6409 | |
| 6410 | TemplateName Template = TemplateIn.get(); |
Douglas Gregor | ef24c4b | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 6411 | if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) { |
| 6412 | // Construct a dependent template specialization type. |
| 6413 | assert(DTN && "dependent template has non-dependent name?"); |
| 6414 | assert(DTN->getQualifier() |
| 6415 | == static_cast<NestedNameSpecifier*>(SS.getScopeRep())); |
| 6416 | QualType T = Context.getDependentTemplateSpecializationType(ETK_Typename, |
| 6417 | DTN->getQualifier(), |
| 6418 | DTN->getIdentifier(), |
| 6419 | TemplateArgs); |
Douglas Gregor | a02411e | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 6420 | |
Douglas Gregor | ef24c4b | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 6421 | // Create source-location information for this type. |
John McCall | 4e44983 | 2010-05-28 23:32:21 +0000 | [diff] [blame] | 6422 | TypeLocBuilder Builder; |
Douglas Gregor | ef24c4b | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 6423 | DependentTemplateSpecializationTypeLoc SpecTL |
| 6424 | = Builder.push<DependentTemplateSpecializationTypeLoc>(T); |
Douglas Gregor | a02411e | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 6425 | SpecTL.setLAngleLoc(LAngleLoc); |
| 6426 | SpecTL.setRAngleLoc(RAngleLoc); |
Douglas Gregor | ef24c4b | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 6427 | SpecTL.setKeywordLoc(TypenameLoc); |
Douglas Gregor | 94fdffa | 2011-03-01 20:11:18 +0000 | [diff] [blame] | 6428 | SpecTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
Douglas Gregor | ef24c4b | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 6429 | SpecTL.setNameLoc(TemplateNameLoc); |
Douglas Gregor | a02411e | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 6430 | for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) |
| 6431 | SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo()); |
Douglas Gregor | ef24c4b | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 6432 | return CreateParsedType(T, Builder.getTypeSourceInfo(Context, T)); |
Douglas Gregor | 6946baf | 2009-09-02 13:05:45 +0000 | [diff] [blame] | 6433 | } |
Douglas Gregor | a02411e | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 6434 | |
Douglas Gregor | ef24c4b | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 6435 | QualType T = CheckTemplateIdType(Template, TemplateNameLoc, TemplateArgs); |
| 6436 | if (T.isNull()) |
| 6437 | return true; |
Douglas Gregor | a02411e | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 6438 | |
Douglas Gregor | ef24c4b | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 6439 | // Provide source-location information for the template specialization |
| 6440 | // type. |
Douglas Gregor | a02411e | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 6441 | TypeLocBuilder Builder; |
Douglas Gregor | ef24c4b | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 6442 | TemplateSpecializationTypeLoc SpecTL |
| 6443 | = Builder.push<TemplateSpecializationTypeLoc>(T); |
| 6444 | |
| 6445 | // FIXME: No place to set the location of the 'template' keyword! |
Douglas Gregor | a02411e | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 6446 | SpecTL.setLAngleLoc(LAngleLoc); |
| 6447 | SpecTL.setRAngleLoc(RAngleLoc); |
Douglas Gregor | ef24c4b | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 6448 | SpecTL.setTemplateNameLoc(TemplateNameLoc); |
Douglas Gregor | a02411e | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 6449 | for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) |
| 6450 | SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo()); |
| 6451 | |
Douglas Gregor | ef24c4b | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 6452 | T = Context.getElaboratedType(ETK_Typename, SS.getScopeRep(), T); |
| 6453 | ElaboratedTypeLoc TL = Builder.push<ElaboratedTypeLoc>(T); |
| 6454 | TL.setKeywordLoc(TypenameLoc); |
Douglas Gregor | 9e87687 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 6455 | TL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 6456 | |
Douglas Gregor | ef24c4b | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 6457 | TypeSourceInfo *TSI = Builder.getTypeSourceInfo(Context, T); |
| 6458 | return CreateParsedType(T, TSI); |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 6459 | } |
| 6460 | |
Douglas Gregor | a02411e | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 6461 | |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6462 | /// \brief Build the type that describes a C++ typename specifier, |
| 6463 | /// e.g., "typename T::type". |
| 6464 | QualType |
Douglas Gregor | e29425b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 6465 | Sema::CheckTypenameType(ElaboratedTypeKeyword Keyword, |
| 6466 | SourceLocation KeywordLoc, |
| 6467 | NestedNameSpecifierLoc QualifierLoc, |
| 6468 | const IdentifierInfo &II, |
Abramo Bagnara | e4da7a0 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 6469 | SourceLocation IILoc) { |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 6470 | CXXScopeSpec SS; |
Douglas Gregor | e29425b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 6471 | SS.Adopt(QualifierLoc); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6472 | |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 6473 | DeclContext *Ctx = computeDeclContext(SS); |
| 6474 | if (!Ctx) { |
| 6475 | // If the nested-name-specifier is dependent and couldn't be |
| 6476 | // resolved to a type, build a typename type. |
Douglas Gregor | e29425b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 6477 | assert(QualifierLoc.getNestedNameSpecifier()->isDependent()); |
| 6478 | return Context.getDependentNameType(Keyword, |
| 6479 | QualifierLoc.getNestedNameSpecifier(), |
| 6480 | &II); |
Douglas Gregor | 42af25f | 2009-05-11 19:58:34 +0000 | [diff] [blame] | 6481 | } |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6482 | |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 6483 | // If the nested-name-specifier refers to the current instantiation, |
| 6484 | // the "typename" keyword itself is superfluous. In C++03, the |
| 6485 | // program is actually ill-formed. However, DR 382 (in C++0x CD1) |
| 6486 | // allows such extraneous "typename" keywords, and we retroactively |
Douglas Gregor | 732281d | 2010-06-14 22:07:54 +0000 | [diff] [blame] | 6487 | // 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] | 6488 | |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 6489 | if (RequireCompleteDeclContext(SS, Ctx)) |
| 6490 | return QualType(); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6491 | |
| 6492 | DeclarationName Name(&II); |
Abramo Bagnara | e4da7a0 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 6493 | LookupResult Result(*this, Name, IILoc, LookupOrdinaryName); |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 6494 | LookupQualifiedName(Result, Ctx); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6495 | unsigned DiagID = 0; |
| 6496 | Decl *Referenced = 0; |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 6497 | switch (Result.getResultKind()) { |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6498 | case LookupResult::NotFound: |
Douglas Gregor | 3f09327 | 2009-10-13 21:16:44 +0000 | [diff] [blame] | 6499 | DiagID = diag::err_typename_nested_not_found; |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6500 | break; |
Douglas Gregor | d954504 | 2010-12-09 00:06:27 +0000 | [diff] [blame] | 6501 | |
| 6502 | case LookupResult::FoundUnresolvedValue: { |
| 6503 | // We found a using declaration that is a value. Most likely, the using |
| 6504 | // declaration itself is meant to have the 'typename' keyword. |
Douglas Gregor | e29425b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 6505 | SourceRange FullRange(KeywordLoc.isValid() ? KeywordLoc : SS.getBeginLoc(), |
Douglas Gregor | d954504 | 2010-12-09 00:06:27 +0000 | [diff] [blame] | 6506 | IILoc); |
| 6507 | Diag(IILoc, diag::err_typename_refers_to_using_value_decl) |
| 6508 | << Name << Ctx << FullRange; |
| 6509 | if (UnresolvedUsingValueDecl *Using |
| 6510 | = dyn_cast<UnresolvedUsingValueDecl>(Result.getRepresentativeDecl())){ |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 6511 | SourceLocation Loc = Using->getQualifierLoc().getBeginLoc(); |
Douglas Gregor | d954504 | 2010-12-09 00:06:27 +0000 | [diff] [blame] | 6512 | Diag(Loc, diag::note_using_value_decl_missing_typename) |
| 6513 | << FixItHint::CreateInsertion(Loc, "typename "); |
| 6514 | } |
| 6515 | } |
| 6516 | // Fall through to create a dependent typename type, from which we can recover |
| 6517 | // better. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6518 | |
Douglas Gregor | 7d3f576 | 2010-01-15 01:44:47 +0000 | [diff] [blame] | 6519 | case LookupResult::NotFoundInCurrentInstantiation: |
| 6520 | // Okay, it's a member of an unknown instantiation. |
Douglas Gregor | e29425b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 6521 | return Context.getDependentNameType(Keyword, |
| 6522 | QualifierLoc.getNestedNameSpecifier(), |
| 6523 | &II); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6524 | |
| 6525 | case LookupResult::Found: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6526 | if (TypeDecl *Type = dyn_cast<TypeDecl>(Result.getFoundDecl())) { |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 6527 | // We found a type. Build an ElaboratedType, since the |
| 6528 | // typename-specifier was just sugar. |
Douglas Gregor | e29425b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 6529 | return Context.getElaboratedType(ETK_Typename, |
| 6530 | QualifierLoc.getNestedNameSpecifier(), |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 6531 | Context.getTypeDeclType(Type)); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6532 | } |
| 6533 | |
| 6534 | DiagID = diag::err_typename_nested_not_type; |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 6535 | Referenced = Result.getFoundDecl(); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6536 | break; |
| 6537 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6538 | |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 6539 | llvm_unreachable("unresolved using decl in non-dependent context"); |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 6540 | return QualType(); |
| 6541 | |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6542 | case LookupResult::FoundOverloaded: |
| 6543 | DiagID = diag::err_typename_nested_not_type; |
| 6544 | Referenced = *Result.begin(); |
| 6545 | break; |
| 6546 | |
John McCall | 6e24726 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 6547 | case LookupResult::Ambiguous: |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6548 | return QualType(); |
| 6549 | } |
| 6550 | |
| 6551 | // If we get here, it's because name lookup did not find a |
| 6552 | // type. Emit an appropriate diagnostic and return an error. |
Douglas Gregor | e29425b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 6553 | SourceRange FullRange(KeywordLoc.isValid() ? KeywordLoc : SS.getBeginLoc(), |
Abramo Bagnara | e4da7a0 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 6554 | IILoc); |
| 6555 | Diag(IILoc, DiagID) << FullRange << Name << Ctx; |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6556 | if (Referenced) |
| 6557 | Diag(Referenced->getLocation(), diag::note_typename_refers_here) |
| 6558 | << Name; |
| 6559 | return QualType(); |
| 6560 | } |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6561 | |
| 6562 | namespace { |
| 6563 | // See Sema::RebuildTypeInCurrentInstantiation |
Benjamin Kramer | 85b4521 | 2009-11-28 19:45:26 +0000 | [diff] [blame] | 6564 | class CurrentInstantiationRebuilder |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6565 | : public TreeTransform<CurrentInstantiationRebuilder> { |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6566 | SourceLocation Loc; |
| 6567 | DeclarationName Entity; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6568 | |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6569 | public: |
Douglas Gregor | 895162d | 2010-04-30 18:55:50 +0000 | [diff] [blame] | 6570 | typedef TreeTransform<CurrentInstantiationRebuilder> inherited; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6571 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6572 | CurrentInstantiationRebuilder(Sema &SemaRef, |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6573 | SourceLocation Loc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6574 | DeclarationName Entity) |
| 6575 | : TreeTransform<CurrentInstantiationRebuilder>(SemaRef), |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6576 | Loc(Loc), Entity(Entity) { } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6577 | |
| 6578 | /// \brief Determine whether the given type \p T has already been |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6579 | /// transformed. |
| 6580 | /// |
| 6581 | /// For the purposes of type reconstruction, a type has already been |
| 6582 | /// transformed if it is NULL or if it is not dependent. |
| 6583 | bool AlreadyTransformed(QualType T) { |
| 6584 | return T.isNull() || !T->isDependentType(); |
| 6585 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6586 | |
| 6587 | /// \brief Returns the location of the entity whose type is being |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6588 | /// rebuilt. |
| 6589 | SourceLocation getBaseLocation() { return Loc; } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6590 | |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6591 | /// \brief Returns the name of the entity whose type is being rebuilt. |
| 6592 | DeclarationName getBaseEntity() { return Entity; } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6593 | |
Douglas Gregor | 972e6ce | 2009-10-27 06:26:26 +0000 | [diff] [blame] | 6594 | /// \brief Sets the "base" location and entity when that |
| 6595 | /// information is known based on another transformation. |
| 6596 | void setBase(SourceLocation Loc, DeclarationName Entity) { |
| 6597 | this->Loc = Loc; |
| 6598 | this->Entity = Entity; |
| 6599 | } |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6600 | }; |
| 6601 | } |
| 6602 | |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6603 | /// \brief Rebuilds a type within the context of the current instantiation. |
| 6604 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6605 | /// 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] | 6606 | /// a class template (or class template partial specialization) that was parsed |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6607 | /// and constructed before we entered the scope of the class template (or |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6608 | /// partial specialization thereof). This routine will rebuild that type now |
| 6609 | /// that we have entered the declarator's scope, which may produce different |
| 6610 | /// canonical types, e.g., |
| 6611 | /// |
| 6612 | /// \code |
| 6613 | /// template<typename T> |
| 6614 | /// struct X { |
| 6615 | /// typedef T* pointer; |
| 6616 | /// pointer data(); |
| 6617 | /// }; |
| 6618 | /// |
| 6619 | /// template<typename T> |
| 6620 | /// typename X<T>::pointer X<T>::data() { ... } |
| 6621 | /// \endcode |
| 6622 | /// |
Douglas Gregor | 4714c12 | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 6623 | /// 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] | 6624 | /// since we do not know that we can look into X<T> when we parsed the type. |
| 6625 | /// This function will rebuild the type, performing the lookup of "pointer" |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 6626 | /// 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] | 6627 | /// as the canonical type of T*, allowing the return types of the out-of-line |
| 6628 | /// definition and the declaration to match. |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 6629 | TypeSourceInfo *Sema::RebuildTypeInCurrentInstantiation(TypeSourceInfo *T, |
| 6630 | SourceLocation Loc, |
| 6631 | DeclarationName Name) { |
| 6632 | if (!T || !T->getType()->isDependentType()) |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6633 | return T; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6634 | |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6635 | CurrentInstantiationRebuilder Rebuilder(*this, Loc, Name); |
| 6636 | return Rebuilder.TransformType(T); |
Benjamin Kramer | 27ba2f0 | 2009-08-11 22:33:06 +0000 | [diff] [blame] | 6637 | } |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6638 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 6639 | ExprResult Sema::RebuildExprInCurrentInstantiation(Expr *E) { |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 6640 | CurrentInstantiationRebuilder Rebuilder(*this, E->getExprLoc(), |
| 6641 | DeclarationName()); |
| 6642 | return Rebuilder.TransformExpr(E); |
| 6643 | } |
| 6644 | |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 6645 | bool Sema::RebuildNestedNameSpecifierInCurrentInstantiation(CXXScopeSpec &SS) { |
Douglas Gregor | 7e38494 | 2011-02-25 16:07:42 +0000 | [diff] [blame] | 6646 | if (SS.isInvalid()) |
| 6647 | return true; |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 6648 | |
Douglas Gregor | 7e38494 | 2011-02-25 16:07:42 +0000 | [diff] [blame] | 6649 | NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context); |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 6650 | CurrentInstantiationRebuilder Rebuilder(*this, SS.getRange().getBegin(), |
| 6651 | DeclarationName()); |
Douglas Gregor | 7e38494 | 2011-02-25 16:07:42 +0000 | [diff] [blame] | 6652 | NestedNameSpecifierLoc Rebuilt |
| 6653 | = Rebuilder.TransformNestedNameSpecifierLoc(QualifierLoc); |
| 6654 | if (!Rebuilt) |
| 6655 | return true; |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 6656 | |
Douglas Gregor | 7e38494 | 2011-02-25 16:07:42 +0000 | [diff] [blame] | 6657 | SS.Adopt(Rebuilt); |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 6658 | return false; |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 6659 | } |
| 6660 | |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6661 | /// \brief Produces a formatted string that describes the binding of |
| 6662 | /// template parameters to template arguments. |
| 6663 | std::string |
| 6664 | Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params, |
| 6665 | const TemplateArgumentList &Args) { |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 6666 | return getTemplateArgumentBindingsText(Params, Args.data(), Args.size()); |
Douglas Gregor | 9148c3f | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 6667 | } |
| 6668 | |
| 6669 | std::string |
| 6670 | Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params, |
| 6671 | const TemplateArgument *Args, |
| 6672 | unsigned NumArgs) { |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 6673 | llvm::SmallString<128> Str; |
| 6674 | llvm::raw_svector_ostream Out(Str); |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6675 | |
Douglas Gregor | 9148c3f | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 6676 | if (!Params || Params->size() == 0 || NumArgs == 0) |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 6677 | return std::string(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6678 | |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6679 | for (unsigned I = 0, N = Params->size(); I != N; ++I) { |
Douglas Gregor | 9148c3f | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 6680 | if (I >= NumArgs) |
| 6681 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6682 | |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6683 | if (I == 0) |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 6684 | Out << "[with "; |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6685 | else |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 6686 | Out << ", "; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6687 | |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6688 | if (const IdentifierInfo *Id = Params->getParam(I)->getIdentifier()) { |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 6689 | Out << Id->getName(); |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6690 | } else { |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 6691 | Out << '$' << I; |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6692 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6693 | |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 6694 | Out << " = "; |
| 6695 | Args[I].print(Context.PrintingPolicy, Out); |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6696 | } |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 6697 | |
| 6698 | Out << ']'; |
| 6699 | return Out.str(); |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6700 | } |
Francois Pichet | 8387e2a | 2011-04-22 22:18:13 +0000 | [diff] [blame] | 6701 | |
| 6702 | void Sema::MarkAsLateParsedTemplate(FunctionDecl *FD, bool Flag) { |
| 6703 | if (!FD) |
| 6704 | return; |
| 6705 | FD->setLateTemplateParsed(Flag); |
| 6706 | } |
| 6707 | |
| 6708 | bool Sema::IsInsideALocalClassWithinATemplateFunction() { |
| 6709 | DeclContext *DC = CurContext; |
| 6710 | |
| 6711 | while (DC) { |
| 6712 | if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(CurContext)) { |
| 6713 | const FunctionDecl *FD = RD->isLocalClass(); |
| 6714 | return (FD && FD->getTemplatedKind() != FunctionDecl::TK_NonTemplate); |
| 6715 | } else if (DC->isTranslationUnit() || DC->isNamespace()) |
| 6716 | return false; |
| 6717 | |
| 6718 | DC = DC->getParent(); |
| 6719 | } |
| 6720 | return false; |
| 6721 | } |