Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 1 | //===------- SemaTemplate.cpp - Semantic Analysis for C++ Templates -------===/ |
Douglas Gregor | 5101c24 | 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 | fe1e110 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 7 | //===----------------------------------------------------------------------===/ |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 8 | // |
| 9 | // This file implements semantic analysis for C++ templates. |
Douglas Gregor | fe1e110 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 10 | //===----------------------------------------------------------------------===/ |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 11 | |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 12 | #include "TreeTransform.h" |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 13 | #include "clang/AST/ASTConsumer.h" |
Chandler Carruth | 5553d0d | 2014-01-07 11:51:46 +0000 | [diff] [blame] | 14 | #include "clang/AST/ASTContext.h" |
John McCall | bbbbe4e | 2010-03-11 07:50:04 +0000 | [diff] [blame] | 15 | #include "clang/AST/DeclFriend.h" |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 16 | #include "clang/AST/DeclTemplate.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 17 | #include "clang/AST/Expr.h" |
| 18 | #include "clang/AST/ExprCXX.h" |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 19 | #include "clang/AST/RecursiveASTVisitor.h" |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 20 | #include "clang/AST/TypeVisitor.h" |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 21 | #include "clang/Basic/LangOptions.h" |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 22 | #include "clang/Basic/PartialDiagnostic.h" |
David Majnemer | 763584d | 2014-02-06 10:59:19 +0000 | [diff] [blame] | 23 | #include "clang/Basic/TargetInfo.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 24 | #include "clang/Sema/DeclSpec.h" |
| 25 | #include "clang/Sema/Lookup.h" |
| 26 | #include "clang/Sema/ParsedTemplate.h" |
| 27 | #include "clang/Sema/Scope.h" |
| 28 | #include "clang/Sema/SemaInternal.h" |
| 29 | #include "clang/Sema/Template.h" |
| 30 | #include "clang/Sema/TemplateDeduction.h" |
Benjamin Kramer | e0513cb | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 31 | #include "llvm/ADT/SmallBitVector.h" |
Benjamin Kramer | 4903802 | 2012-02-04 13:45:25 +0000 | [diff] [blame] | 32 | #include "llvm/ADT/SmallString.h" |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 33 | #include "llvm/ADT/StringExtras.h" |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 34 | using namespace clang; |
John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 35 | using namespace sema; |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 36 | |
John McCall | 9b72f89 | 2010-11-10 02:40:36 +0000 | [diff] [blame] | 37 | // Exported for use by Parser. |
| 38 | SourceRange |
| 39 | clang::getTemplateParamsRange(TemplateParameterList const * const *Ps, |
| 40 | unsigned N) { |
| 41 | if (!N) return SourceRange(); |
| 42 | return SourceRange(Ps[0]->getTemplateLoc(), Ps[N-1]->getRAngleLoc()); |
| 43 | } |
| 44 | |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 45 | /// \brief Determine whether the declaration found is acceptable as the name |
| 46 | /// of a template and, if so, return that template declaration. Otherwise, |
| 47 | /// returns NULL. |
John McCall | e9cccd8 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 48 | static NamedDecl *isAcceptableTemplateName(ASTContext &Context, |
Douglas Gregor | 50a3cdd | 2012-03-10 23:52:41 +0000 | [diff] [blame] | 49 | NamedDecl *Orig, |
| 50 | bool AllowFunctionTemplates) { |
John McCall | e9cccd8 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 51 | NamedDecl *D = Orig->getUnderlyingDecl(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 52 | |
Douglas Gregor | 50a3cdd | 2012-03-10 23:52:41 +0000 | [diff] [blame] | 53 | if (isa<TemplateDecl>(D)) { |
| 54 | if (!AllowFunctionTemplates && isa<FunctionTemplateDecl>(D)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 55 | return nullptr; |
| 56 | |
John McCall | e9cccd8 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 57 | return Orig; |
Douglas Gregor | 50a3cdd | 2012-03-10 23:52:41 +0000 | [diff] [blame] | 58 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 59 | |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 60 | if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) { |
| 61 | // C++ [temp.local]p1: |
| 62 | // Like normal (non-template) classes, class templates have an |
| 63 | // injected-class-name (Clause 9). The injected-class-name |
| 64 | // can be used with or without a template-argument-list. When |
| 65 | // it is used without a template-argument-list, it is |
| 66 | // equivalent to the injected-class-name followed by the |
| 67 | // template-parameters of the class template enclosed in |
| 68 | // <>. When it is used with a template-argument-list, it |
| 69 | // refers to the specified class template specialization, |
| 70 | // which could be the current specialization or another |
| 71 | // specialization. |
| 72 | if (Record->isInjectedClassName()) { |
Douglas Gregor | 568a071 | 2009-10-14 17:30:58 +0000 | [diff] [blame] | 73 | Record = cast<CXXRecordDecl>(Record->getDeclContext()); |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 74 | if (Record->getDescribedClassTemplate()) |
| 75 | return Record->getDescribedClassTemplate(); |
| 76 | |
| 77 | if (ClassTemplateSpecializationDecl *Spec |
| 78 | = dyn_cast<ClassTemplateSpecializationDecl>(Record)) |
| 79 | return Spec->getSpecializedTemplate(); |
| 80 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 81 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 82 | return nullptr; |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 83 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 84 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 85 | return nullptr; |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 86 | } |
| 87 | |
Douglas Gregor | 50a3cdd | 2012-03-10 23:52:41 +0000 | [diff] [blame] | 88 | void Sema::FilterAcceptableTemplateNames(LookupResult &R, |
| 89 | bool AllowFunctionTemplates) { |
Douglas Gregor | 41f9030 | 2010-04-12 20:54:26 +0000 | [diff] [blame] | 90 | // The set of class templates we've already seen. |
| 91 | llvm::SmallPtrSet<ClassTemplateDecl *, 8> ClassTemplates; |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 92 | LookupResult::Filter filter = R.makeFilter(); |
| 93 | while (filter.hasNext()) { |
| 94 | NamedDecl *Orig = filter.next(); |
Douglas Gregor | 50a3cdd | 2012-03-10 23:52:41 +0000 | [diff] [blame] | 95 | NamedDecl *Repl = isAcceptableTemplateName(Context, Orig, |
| 96 | AllowFunctionTemplates); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 97 | if (!Repl) |
| 98 | filter.erase(); |
Douglas Gregor | 41f9030 | 2010-04-12 20:54:26 +0000 | [diff] [blame] | 99 | else if (Repl != Orig) { |
| 100 | |
| 101 | // C++ [temp.local]p3: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 102 | // A lookup that finds an injected-class-name (10.2) can result in an |
Douglas Gregor | 41f9030 | 2010-04-12 20:54:26 +0000 | [diff] [blame] | 103 | // ambiguity in certain cases (for example, if it is found in more than |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 104 | // one base class). If all of the injected-class-names that are found |
| 105 | // refer to specializations of the same class template, and if the name |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 106 | // is used as a template-name, the reference refers to the class |
| 107 | // template itself and not a specialization thereof, and is not |
Douglas Gregor | 41f9030 | 2010-04-12 20:54:26 +0000 | [diff] [blame] | 108 | // ambiguous. |
Douglas Gregor | 41f9030 | 2010-04-12 20:54:26 +0000 | [diff] [blame] | 109 | if (ClassTemplateDecl *ClassTmpl = dyn_cast<ClassTemplateDecl>(Repl)) |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 110 | if (!ClassTemplates.insert(ClassTmpl).second) { |
Douglas Gregor | 41f9030 | 2010-04-12 20:54:26 +0000 | [diff] [blame] | 111 | filter.erase(); |
| 112 | continue; |
| 113 | } |
John McCall | bd8062d | 2010-08-13 07:02:08 +0000 | [diff] [blame] | 114 | |
| 115 | // FIXME: we promote access to public here as a workaround to |
| 116 | // the fact that LookupResult doesn't let us remember that we |
| 117 | // found this template through a particular injected class name, |
| 118 | // which means we end up doing nasty things to the invariants. |
| 119 | // Pretending that access is public is *much* safer. |
| 120 | filter.replace(Repl, AS_public); |
Douglas Gregor | 41f9030 | 2010-04-12 20:54:26 +0000 | [diff] [blame] | 121 | } |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 122 | } |
| 123 | filter.done(); |
| 124 | } |
| 125 | |
Douglas Gregor | 50a3cdd | 2012-03-10 23:52:41 +0000 | [diff] [blame] | 126 | bool Sema::hasAnyAcceptableTemplateNames(LookupResult &R, |
| 127 | bool AllowFunctionTemplates) { |
Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 128 | for (LookupResult::iterator I = R.begin(), IEnd = R.end(); I != IEnd; ++I) |
Douglas Gregor | 50a3cdd | 2012-03-10 23:52:41 +0000 | [diff] [blame] | 129 | if (isAcceptableTemplateName(Context, *I, AllowFunctionTemplates)) |
Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 130 | return true; |
| 131 | |
Douglas Gregor | 8b02cd0 | 2011-04-27 04:48:22 +0000 | [diff] [blame] | 132 | return false; |
Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 133 | } |
| 134 | |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 135 | TemplateNameKind Sema::isTemplateName(Scope *S, |
Jeffrey Yasskin | c76498d | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 136 | CXXScopeSpec &SS, |
Abramo Bagnara | 7c5dee4 | 2010-08-06 12:11:11 +0000 | [diff] [blame] | 137 | bool hasTemplateKeyword, |
Douglas Gregor | 3cf8131 | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 138 | UnqualifiedId &Name, |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 139 | ParsedType ObjectTypePtr, |
Douglas Gregor | e861bac | 2009-08-25 22:51:20 +0000 | [diff] [blame] | 140 | bool EnteringContext, |
Douglas Gregor | 786123d | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 141 | TemplateTy &TemplateResult, |
| 142 | bool &MemberOfUnknownSpecialization) { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 143 | assert(getLangOpts().CPlusPlus && "No template names in C!"); |
Douglas Gregor | 411e5ac | 2010-01-11 23:29:10 +0000 | [diff] [blame] | 144 | |
Douglas Gregor | 3cf8131 | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 145 | DeclarationName TName; |
Douglas Gregor | 786123d | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 146 | MemberOfUnknownSpecialization = false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 147 | |
Douglas Gregor | 3cf8131 | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 148 | switch (Name.getKind()) { |
| 149 | case UnqualifiedId::IK_Identifier: |
| 150 | TName = DeclarationName(Name.Identifier); |
| 151 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 152 | |
Douglas Gregor | 3cf8131 | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 153 | case UnqualifiedId::IK_OperatorFunctionId: |
| 154 | TName = Context.DeclarationNames.getCXXOperatorName( |
| 155 | Name.OperatorFunctionId.Operator); |
| 156 | break; |
| 157 | |
Alexis Hunt | ed0530f | 2009-11-28 08:58:14 +0000 | [diff] [blame] | 158 | case UnqualifiedId::IK_LiteralOperatorId: |
Alexis Hunt | 3d221f2 | 2009-11-29 07:34:05 +0000 | [diff] [blame] | 159 | TName = Context.DeclarationNames.getCXXLiteralOperatorName(Name.Identifier); |
| 160 | break; |
Alexis Hunt | ed0530f | 2009-11-28 08:58:14 +0000 | [diff] [blame] | 161 | |
Douglas Gregor | 3cf8131 | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 162 | default: |
| 163 | return TNK_Non_template; |
| 164 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 165 | |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 166 | QualType ObjectType = ObjectTypePtr.get(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 167 | |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 168 | LookupResult R(*this, TName, Name.getLocStart(), LookupOrdinaryName); |
Douglas Gregor | 786123d | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 169 | LookupTemplateName(R, S, SS, ObjectType, EnteringContext, |
| 170 | MemberOfUnknownSpecialization); |
John McCall | fb3f9ba | 2010-08-28 20:17:00 +0000 | [diff] [blame] | 171 | if (R.empty()) return TNK_Non_template; |
| 172 | if (R.isAmbiguous()) { |
| 173 | // Suppress diagnostics; we'll redo this lookup later. |
John McCall | dcc7140 | 2010-08-13 02:23:42 +0000 | [diff] [blame] | 174 | R.suppressDiagnostics(); |
John McCall | fb3f9ba | 2010-08-28 20:17:00 +0000 | [diff] [blame] | 175 | |
| 176 | // FIXME: we might have ambiguous templates, in which case we |
| 177 | // should at least parse them properly! |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 178 | return TNK_Non_template; |
John McCall | dcc7140 | 2010-08-13 02:23:42 +0000 | [diff] [blame] | 179 | } |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 180 | |
John McCall | d28ae27 | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 181 | TemplateName Template; |
| 182 | TemplateNameKind TemplateKind; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 183 | |
John McCall | d28ae27 | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 184 | unsigned ResultCount = R.end() - R.begin(); |
| 185 | if (ResultCount > 1) { |
| 186 | // We assume that we'll preserve the qualifier from a function |
| 187 | // template name in other ways. |
| 188 | Template = Context.getOverloadedTemplateName(R.begin(), R.end()); |
| 189 | TemplateKind = TNK_Function_template; |
John McCall | dcc7140 | 2010-08-13 02:23:42 +0000 | [diff] [blame] | 190 | |
| 191 | // We'll do this lookup again later. |
| 192 | R.suppressDiagnostics(); |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 193 | } else { |
John McCall | d28ae27 | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 194 | TemplateDecl *TD = cast<TemplateDecl>((*R.begin())->getUnderlyingDecl()); |
| 195 | |
| 196 | if (SS.isSet() && !SS.isInvalid()) { |
Aaron Ballman | 4a97967 | 2014-01-03 13:56:08 +0000 | [diff] [blame] | 197 | NestedNameSpecifier *Qualifier = SS.getScopeRep(); |
Abramo Bagnara | 7c5dee4 | 2010-08-06 12:11:11 +0000 | [diff] [blame] | 198 | Template = Context.getQualifiedTemplateName(Qualifier, |
| 199 | hasTemplateKeyword, TD); |
John McCall | d28ae27 | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 200 | } else { |
| 201 | Template = TemplateName(TD); |
| 202 | } |
| 203 | |
John McCall | dcc7140 | 2010-08-13 02:23:42 +0000 | [diff] [blame] | 204 | if (isa<FunctionTemplateDecl>(TD)) { |
John McCall | d28ae27 | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 205 | TemplateKind = TNK_Function_template; |
John McCall | dcc7140 | 2010-08-13 02:23:42 +0000 | [diff] [blame] | 206 | |
| 207 | // We'll do this lookup again later. |
| 208 | R.suppressDiagnostics(); |
| 209 | } else { |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 210 | assert(isa<ClassTemplateDecl>(TD) || isa<TemplateTemplateParmDecl>(TD) || |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 211 | isa<TypeAliasTemplateDecl>(TD) || isa<VarTemplateDecl>(TD)); |
| 212 | TemplateKind = |
| 213 | isa<VarTemplateDecl>(TD) ? TNK_Var_template : TNK_Type_template; |
John McCall | d28ae27 | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 214 | } |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 215 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 216 | |
John McCall | d28ae27 | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 217 | TemplateResult = TemplateTy::make(Template); |
| 218 | return TemplateKind; |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 219 | } |
| 220 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 221 | bool Sema::DiagnoseUnknownTemplateName(const IdentifierInfo &II, |
Douglas Gregor | 18473f3 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 222 | SourceLocation IILoc, |
| 223 | Scope *S, |
| 224 | const CXXScopeSpec *SS, |
| 225 | TemplateTy &SuggestedTemplate, |
| 226 | TemplateNameKind &SuggestedKind) { |
| 227 | // We can't recover unless there's a dependent scope specifier preceding the |
| 228 | // template name. |
Douglas Gregor | 20c38a7 | 2010-05-21 23:43:39 +0000 | [diff] [blame] | 229 | // FIXME: Typo correction? |
Douglas Gregor | 18473f3 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 230 | if (!SS || !SS->isSet() || !isDependentScopeSpecifier(*SS) || |
| 231 | computeDeclContext(*SS)) |
| 232 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 233 | |
Douglas Gregor | 18473f3 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 234 | // The code is missing a 'template' keyword prior to the dependent template |
| 235 | // name. |
| 236 | NestedNameSpecifier *Qualifier = (NestedNameSpecifier*)SS->getScopeRep(); |
| 237 | Diag(IILoc, diag::err_template_kw_missing) |
| 238 | << Qualifier << II.getName() |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 239 | << FixItHint::CreateInsertion(IILoc, "template "); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 240 | SuggestedTemplate |
Douglas Gregor | 18473f3 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 241 | = TemplateTy::make(Context.getDependentTemplateName(Qualifier, &II)); |
| 242 | SuggestedKind = TNK_Dependent_template_name; |
| 243 | return true; |
| 244 | } |
| 245 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 246 | void Sema::LookupTemplateName(LookupResult &Found, |
Jeffrey Yasskin | c76498d | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 247 | Scope *S, CXXScopeSpec &SS, |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 248 | QualType ObjectType, |
Douglas Gregor | 786123d | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 249 | bool EnteringContext, |
| 250 | bool &MemberOfUnknownSpecialization) { |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 251 | // Determine where to perform name lookup |
Douglas Gregor | 786123d | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 252 | MemberOfUnknownSpecialization = false; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 253 | DeclContext *LookupCtx = nullptr; |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 254 | bool isDependent = false; |
| 255 | if (!ObjectType.isNull()) { |
| 256 | // This nested-name-specifier occurs in a member access expression, e.g., |
| 257 | // x->B::f, and we are looking into the type of the object. |
| 258 | assert(!SS.isSet() && "ObjectType and scope specifier cannot coexist"); |
| 259 | LookupCtx = computeDeclContext(ObjectType); |
| 260 | isDependent = ObjectType->isDependentType(); |
Richard Smith | 5ed7956 | 2013-06-07 20:03:01 +0000 | [diff] [blame] | 261 | assert((isDependent || !ObjectType->isIncompleteType() || |
| 262 | ObjectType->castAs<TagType>()->isBeingDefined()) && |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 263 | "Caller should have completed object type"); |
Douglas Gregor | bf3a826 | 2012-01-12 16:11:24 +0000 | [diff] [blame] | 264 | |
| 265 | // Template names cannot appear inside an Objective-C class or object type. |
| 266 | if (ObjectType->isObjCObjectOrInterfaceType()) { |
| 267 | Found.clear(); |
| 268 | return; |
| 269 | } |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 270 | } else if (SS.isSet()) { |
| 271 | // This nested-name-specifier occurs after another nested-name-specifier, |
| 272 | // so long into the context associated with the prior nested-name-specifier. |
| 273 | LookupCtx = computeDeclContext(SS, EnteringContext); |
| 274 | isDependent = isDependentScopeSpecifier(SS); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 275 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 276 | // The declaration context must be complete. |
John McCall | 0b66eb3 | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 277 | if (LookupCtx && RequireCompleteDeclContext(SS, LookupCtx)) |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 278 | return; |
| 279 | } |
| 280 | |
| 281 | bool ObjectTypeSearchedInScope = false; |
Douglas Gregor | 50a3cdd | 2012-03-10 23:52:41 +0000 | [diff] [blame] | 282 | bool AllowFunctionTemplatesInLookup = true; |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 283 | if (LookupCtx) { |
| 284 | // Perform "qualified" name lookup into the declaration context we |
| 285 | // computed, which is either the type of the base of a member access |
| 286 | // expression or the declaration context associated with a prior |
| 287 | // nested-name-specifier. |
| 288 | LookupQualifiedName(Found, LookupCtx); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 289 | if (!ObjectType.isNull() && Found.empty()) { |
| 290 | // C++ [basic.lookup.classref]p1: |
| 291 | // In a class member access expression (5.2.5), if the . or -> token is |
| 292 | // immediately followed by an identifier followed by a <, the |
| 293 | // identifier must be looked up to determine whether the < is the |
| 294 | // beginning of a template argument list (14.2) or a less-than operator. |
| 295 | // The identifier is first looked up in the class of the object |
| 296 | // expression. If the identifier is not found, it is then looked up in |
| 297 | // the context of the entire postfix-expression and shall name a class |
| 298 | // or function template. |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 299 | if (S) LookupName(Found, S); |
| 300 | ObjectTypeSearchedInScope = true; |
Douglas Gregor | 50a3cdd | 2012-03-10 23:52:41 +0000 | [diff] [blame] | 301 | AllowFunctionTemplatesInLookup = false; |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 302 | } |
Douglas Gregor | fc6c3e7 | 2010-07-16 16:54:17 +0000 | [diff] [blame] | 303 | } else if (isDependent && (!S || ObjectType.isNull())) { |
Douglas Gregor | c119dd5 | 2010-01-12 17:06:20 +0000 | [diff] [blame] | 304 | // We cannot look into a dependent object type or nested nme |
| 305 | // specifier. |
Douglas Gregor | 786123d | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 306 | MemberOfUnknownSpecialization = true; |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 307 | return; |
| 308 | } else { |
| 309 | // Perform unqualified name lookup in the current scope. |
| 310 | LookupName(Found, S); |
Douglas Gregor | 50a3cdd | 2012-03-10 23:52:41 +0000 | [diff] [blame] | 311 | |
| 312 | if (!ObjectType.isNull()) |
| 313 | AllowFunctionTemplatesInLookup = false; |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 314 | } |
| 315 | |
Douglas Gregor | c119dd5 | 2010-01-12 17:06:20 +0000 | [diff] [blame] | 316 | if (Found.empty() && !isDependent) { |
Douglas Gregor | ff18cc1 | 2009-12-31 08:11:17 +0000 | [diff] [blame] | 317 | // If we did not find any names, attempt to correct any typos. |
| 318 | DeclarationName Name = Found.getLookupName(); |
Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 319 | Found.clear(); |
Kaelyn Uhrain | 637b5b3 | 2012-01-13 23:10:36 +0000 | [diff] [blame] | 320 | // Simple filter callback that, for keywords, only accepts the C++ *_cast |
Kaelyn Takata | 89c881b | 2014-10-27 18:07:29 +0000 | [diff] [blame] | 321 | auto FilterCCC = llvm::make_unique<CorrectionCandidateCallback>(); |
| 322 | FilterCCC->WantTypeSpecifiers = false; |
| 323 | FilterCCC->WantExpressionKeywords = false; |
| 324 | FilterCCC->WantRemainingKeywords = false; |
| 325 | FilterCCC->WantCXXNamedCasts = true; |
| 326 | if (TypoCorrection Corrected = CorrectTypo( |
| 327 | Found.getLookupNameInfo(), Found.getLookupKind(), S, &SS, |
| 328 | std::move(FilterCCC), CTK_ErrorRecovery, LookupCtx)) { |
Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 329 | Found.setLookupName(Corrected.getCorrection()); |
| 330 | if (Corrected.getCorrectionDecl()) |
| 331 | Found.addDecl(Corrected.getCorrectionDecl()); |
Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 332 | FilterAcceptableTemplateNames(Found); |
John McCall | e9cccd8 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 333 | if (!Found.empty()) { |
Kaelyn Uhrain | 10413a4 | 2013-07-02 23:47:44 +0000 | [diff] [blame] | 334 | if (LookupCtx) { |
Richard Smith | f9b1510 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 335 | std::string CorrectedStr(Corrected.getAsString(getLangOpts())); |
| 336 | bool DroppedSpecifier = Corrected.WillReplaceSpecifier() && |
Kaelyn Uhrain | 10413a4 | 2013-07-02 23:47:44 +0000 | [diff] [blame] | 337 | Name.getAsString() == CorrectedStr; |
Richard Smith | f9b1510 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 338 | diagnoseTypo(Corrected, PDiag(diag::err_no_member_template_suggest) |
| 339 | << Name << LookupCtx << DroppedSpecifier |
| 340 | << SS.getRange()); |
Kaelyn Uhrain | 10413a4 | 2013-07-02 23:47:44 +0000 | [diff] [blame] | 341 | } else { |
Richard Smith | f9b1510 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 342 | diagnoseTypo(Corrected, PDiag(diag::err_no_template_suggest) << Name); |
Kaelyn Uhrain | 10413a4 | 2013-07-02 23:47:44 +0000 | [diff] [blame] | 343 | } |
John McCall | e9cccd8 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 344 | } |
Douglas Gregor | ff18cc1 | 2009-12-31 08:11:17 +0000 | [diff] [blame] | 345 | } else { |
Douglas Gregor | c048c52 | 2010-06-29 19:27:42 +0000 | [diff] [blame] | 346 | Found.setLookupName(Name); |
Douglas Gregor | ff18cc1 | 2009-12-31 08:11:17 +0000 | [diff] [blame] | 347 | } |
| 348 | } |
| 349 | |
Douglas Gregor | 50a3cdd | 2012-03-10 23:52:41 +0000 | [diff] [blame] | 350 | FilterAcceptableTemplateNames(Found, AllowFunctionTemplatesInLookup); |
Douglas Gregor | fc6c3e7 | 2010-07-16 16:54:17 +0000 | [diff] [blame] | 351 | if (Found.empty()) { |
| 352 | if (isDependent) |
| 353 | MemberOfUnknownSpecialization = true; |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 354 | return; |
Douglas Gregor | fc6c3e7 | 2010-07-16 16:54:17 +0000 | [diff] [blame] | 355 | } |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 356 | |
Douglas Gregor | 1b02e4a | 2012-05-01 20:23:02 +0000 | [diff] [blame] | 357 | if (S && !ObjectType.isNull() && !ObjectTypeSearchedInScope && |
Richard Smith | e7d67f2 | 2013-09-03 21:22:41 +0000 | [diff] [blame] | 358 | !getLangOpts().CPlusPlus11) { |
Douglas Gregor | 1b02e4a | 2012-05-01 20:23:02 +0000 | [diff] [blame] | 359 | // C++03 [basic.lookup.classref]p1: |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 360 | // [...] If the lookup in the class of the object expression finds a |
| 361 | // template, the name is also looked up in the context of the entire |
| 362 | // postfix-expression and [...] |
| 363 | // |
Douglas Gregor | 1b02e4a | 2012-05-01 20:23:02 +0000 | [diff] [blame] | 364 | // Note: C++11 does not perform this second lookup. |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 365 | LookupResult FoundOuter(*this, Found.getLookupName(), Found.getNameLoc(), |
| 366 | LookupOrdinaryName); |
| 367 | LookupName(FoundOuter, S); |
Douglas Gregor | 50a3cdd | 2012-03-10 23:52:41 +0000 | [diff] [blame] | 368 | FilterAcceptableTemplateNames(FoundOuter, /*AllowFunctionTemplates=*/false); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 369 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 370 | if (FoundOuter.empty()) { |
| 371 | // - if the name is not found, the name found in the class of the |
| 372 | // object expression is used, otherwise |
Douglas Gregor | de0a43f | 2011-08-10 21:59:45 +0000 | [diff] [blame] | 373 | } else if (!FoundOuter.getAsSingle<ClassTemplateDecl>() || |
| 374 | FoundOuter.isAmbiguous()) { |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 375 | // - if the name is found in the context of the entire |
| 376 | // postfix-expression and does not name a class template, the name |
| 377 | // found in the class of the object expression is used, otherwise |
Douglas Gregor | de0a43f | 2011-08-10 21:59:45 +0000 | [diff] [blame] | 378 | FoundOuter.clear(); |
John McCall | e9cccd8 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 379 | } else if (!Found.isSuppressingDiagnostics()) { |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 380 | // - if the name found is a class template, it must refer to the same |
| 381 | // entity as the one found in the class of the object expression, |
| 382 | // otherwise the program is ill-formed. |
| 383 | if (!Found.isSingleResult() || |
| 384 | Found.getFoundDecl()->getCanonicalDecl() |
| 385 | != FoundOuter.getFoundDecl()->getCanonicalDecl()) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 386 | Diag(Found.getNameLoc(), |
Jeffrey Yasskin | 2f96e9f | 2010-06-05 01:39:57 +0000 | [diff] [blame] | 387 | diag::ext_nested_name_member_ref_lookup_ambiguous) |
| 388 | << Found.getLookupName() |
| 389 | << ObjectType; |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 390 | Diag(Found.getRepresentativeDecl()->getLocation(), |
| 391 | diag::note_ambig_member_ref_object_type) |
| 392 | << ObjectType; |
| 393 | Diag(FoundOuter.getFoundDecl()->getLocation(), |
| 394 | diag::note_ambig_member_ref_scope); |
| 395 | |
| 396 | // Recover by taking the template that we found in the object |
| 397 | // expression's type. |
| 398 | } |
| 399 | } |
| 400 | } |
| 401 | } |
| 402 | |
John McCall | cd4b477 | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 403 | /// ActOnDependentIdExpression - Handle a dependent id-expression that |
| 404 | /// was just parsed. This is only possible with an explicit scope |
| 405 | /// specifier naming a dependent type. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 406 | ExprResult |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 407 | Sema::ActOnDependentIdExpression(const CXXScopeSpec &SS, |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 408 | SourceLocation TemplateKWLoc, |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 409 | const DeclarationNameInfo &NameInfo, |
John McCall | cd4b477 | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 410 | bool isAddressOfOperand, |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 411 | const TemplateArgumentListInfo *TemplateArgs) { |
John McCall | 87fe5d5 | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 412 | DeclContext *DC = getFunctionLevelDeclContext(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 413 | |
John McCall | cd4b477 | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 414 | if (!isAddressOfOperand && |
John McCall | 87fe5d5 | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 415 | isa<CXXMethodDecl>(DC) && |
| 416 | cast<CXXMethodDecl>(DC)->isInstance()) { |
| 417 | QualType ThisType = cast<CXXMethodDecl>(DC)->getThisType(Context); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 418 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 419 | // Since the 'this' expression is synthesized, we don't need to |
| 420 | // perform the double-lookup check. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 421 | NamedDecl *FirstQualifierInScope = nullptr; |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 422 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 423 | return CXXDependentScopeMemberExpr::Create( |
| 424 | Context, /*This*/ nullptr, ThisType, /*IsArrow*/ true, |
| 425 | /*Op*/ SourceLocation(), SS.getWithLocInContext(Context), TemplateKWLoc, |
| 426 | FirstQualifierInScope, NameInfo, TemplateArgs); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 427 | } |
| 428 | |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 429 | return BuildDependentDeclRefExpr(SS, TemplateKWLoc, NameInfo, TemplateArgs); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 430 | } |
| 431 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 432 | ExprResult |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 433 | Sema::BuildDependentDeclRefExpr(const CXXScopeSpec &SS, |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 434 | SourceLocation TemplateKWLoc, |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 435 | const DeclarationNameInfo &NameInfo, |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 436 | const TemplateArgumentListInfo *TemplateArgs) { |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 437 | return DependentScopeDeclRefExpr::Create( |
| 438 | Context, SS.getWithLocInContext(Context), TemplateKWLoc, NameInfo, |
| 439 | TemplateArgs); |
Douglas Gregor | 55ad91f | 2008-12-18 19:37:40 +0000 | [diff] [blame] | 440 | } |
| 441 | |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 442 | /// DiagnoseTemplateParameterShadow - Produce a diagnostic complaining |
| 443 | /// that the template parameter 'PrevDecl' is being shadowed by a new |
| 444 | /// declaration at location Loc. Returns true to indicate that this is |
| 445 | /// an error, and false otherwise. |
Douglas Gregor | f4ef4d2 | 2011-10-20 17:58:49 +0000 | [diff] [blame] | 446 | void Sema::DiagnoseTemplateParameterShadow(SourceLocation Loc, Decl *PrevDecl) { |
Douglas Gregor | 5daeee2 | 2008-12-08 18:40:42 +0000 | [diff] [blame] | 447 | assert(PrevDecl->isTemplateParameter() && "Not a template parameter"); |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 448 | |
| 449 | // Microsoft Visual C++ permits template parameters to be shadowed. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 450 | if (getLangOpts().MicrosoftExt) |
Douglas Gregor | f4ef4d2 | 2011-10-20 17:58:49 +0000 | [diff] [blame] | 451 | return; |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 452 | |
| 453 | // C++ [temp.local]p4: |
| 454 | // A template-parameter shall not be redeclared within its |
| 455 | // scope (including nested scopes). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 456 | Diag(Loc, diag::err_template_param_shadow) |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 457 | << cast<NamedDecl>(PrevDecl)->getDeclName(); |
| 458 | Diag(PrevDecl->getLocation(), diag::note_template_param_here); |
Douglas Gregor | f4ef4d2 | 2011-10-20 17:58:49 +0000 | [diff] [blame] | 459 | return; |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 460 | } |
| 461 | |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 462 | /// AdjustDeclIfTemplate - If the given decl happens to be a template, reset |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 463 | /// the parameter D to reference the templated declaration and return a pointer |
| 464 | /// to the template declaration. Otherwise, do nothing to D and return null. |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 465 | TemplateDecl *Sema::AdjustDeclIfTemplate(Decl *&D) { |
| 466 | if (TemplateDecl *Temp = dyn_cast_or_null<TemplateDecl>(D)) { |
| 467 | D = Temp->getTemplatedDecl(); |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 468 | return Temp; |
| 469 | } |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 470 | return nullptr; |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 471 | } |
| 472 | |
Douglas Gregor | eb29d18 | 2011-01-05 17:40:24 +0000 | [diff] [blame] | 473 | ParsedTemplateArgument ParsedTemplateArgument::getTemplatePackExpansion( |
| 474 | SourceLocation EllipsisLoc) const { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 475 | assert(Kind == Template && |
Douglas Gregor | eb29d18 | 2011-01-05 17:40:24 +0000 | [diff] [blame] | 476 | "Only template template arguments can be pack expansions here"); |
| 477 | assert(getAsTemplate().get().containsUnexpandedParameterPack() && |
| 478 | "Template template argument pack expansion without packs"); |
| 479 | ParsedTemplateArgument Result(*this); |
| 480 | Result.EllipsisLoc = EllipsisLoc; |
| 481 | return Result; |
| 482 | } |
| 483 | |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 484 | static TemplateArgumentLoc translateTemplateArgument(Sema &SemaRef, |
| 485 | const ParsedTemplateArgument &Arg) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 486 | |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 487 | switch (Arg.getKind()) { |
| 488 | case ParsedTemplateArgument::Type: { |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 489 | TypeSourceInfo *DI; |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 490 | QualType T = SemaRef.GetTypeFromParser(Arg.getAsType(), &DI); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 491 | if (!DI) |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 492 | DI = SemaRef.Context.getTrivialTypeSourceInfo(T, Arg.getLocation()); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 493 | return TemplateArgumentLoc(TemplateArgument(T), DI); |
| 494 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 495 | |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 496 | case ParsedTemplateArgument::NonType: { |
| 497 | Expr *E = static_cast<Expr *>(Arg.getAsExpr()); |
| 498 | return TemplateArgumentLoc(TemplateArgument(E), E); |
| 499 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 500 | |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 501 | case ParsedTemplateArgument::Template: { |
John McCall | 3e56fd4 | 2010-08-23 07:28:44 +0000 | [diff] [blame] | 502 | TemplateName Template = Arg.getAsTemplate().get(); |
Douglas Gregor | e1d60df | 2011-01-14 23:41:42 +0000 | [diff] [blame] | 503 | TemplateArgument TArg; |
| 504 | if (Arg.getEllipsisLoc().isValid()) |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 505 | TArg = TemplateArgument(Template, Optional<unsigned int>()); |
Douglas Gregor | e1d60df | 2011-01-14 23:41:42 +0000 | [diff] [blame] | 506 | else |
| 507 | TArg = Template; |
| 508 | return TemplateArgumentLoc(TArg, |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 509 | Arg.getScopeSpec().getWithLocInContext( |
| 510 | SemaRef.Context), |
Douglas Gregor | eb29d18 | 2011-01-05 17:40:24 +0000 | [diff] [blame] | 511 | Arg.getLocation(), |
| 512 | Arg.getEllipsisLoc()); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 513 | } |
| 514 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 515 | |
Jeffrey Yasskin | 1615d45 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 516 | llvm_unreachable("Unhandled parsed template argument"); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 517 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 518 | |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 519 | /// \brief Translates template arguments as provided by the parser |
| 520 | /// into template arguments used by semantic analysis. |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 521 | void Sema::translateTemplateArguments(const ASTTemplateArgsPtr &TemplateArgsIn, |
| 522 | TemplateArgumentListInfo &TemplateArgs) { |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 523 | for (unsigned I = 0, Last = TemplateArgsIn.size(); I != Last; ++I) |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 524 | TemplateArgs.addArgument(translateTemplateArgument(*this, |
| 525 | TemplateArgsIn[I])); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 526 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 527 | |
Richard Smith | b80d540 | 2013-06-25 22:21:36 +0000 | [diff] [blame] | 528 | static void maybeDiagnoseTemplateParameterShadow(Sema &SemaRef, Scope *S, |
| 529 | SourceLocation Loc, |
| 530 | IdentifierInfo *Name) { |
| 531 | NamedDecl *PrevDecl = SemaRef.LookupSingleName( |
| 532 | S, Name, Loc, Sema::LookupOrdinaryName, Sema::ForRedeclaration); |
| 533 | if (PrevDecl && PrevDecl->isTemplateParameter()) |
| 534 | SemaRef.DiagnoseTemplateParameterShadow(Loc, PrevDecl); |
| 535 | } |
| 536 | |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 537 | /// ActOnTypeParameter - Called when a C++ template type parameter |
| 538 | /// (e.g., "typename T") has been parsed. Typename specifies whether |
| 539 | /// the keyword "typename" was used to declare the type parameter |
| 540 | /// (otherwise, "class" was used), and KeyLoc is the location of the |
| 541 | /// "class" or "typename" keyword. ParamName is the name of the |
| 542 | /// parameter (NULL indicates an unnamed template parameter) and |
Chandler Carruth | 0883632 | 2011-05-01 00:51:33 +0000 | [diff] [blame] | 543 | /// ParamNameLoc is the location of the parameter name (if any). |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 544 | /// If the type parameter has a default argument, it will be added |
| 545 | /// later via ActOnTypeParameterDefault. |
Nikola Smiljanic | 69fdc9f | 2014-06-06 02:58:59 +0000 | [diff] [blame] | 546 | Decl *Sema::ActOnTypeParameter(Scope *S, bool Typename, |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 547 | SourceLocation EllipsisLoc, |
| 548 | SourceLocation KeyLoc, |
| 549 | IdentifierInfo *ParamName, |
| 550 | SourceLocation ParamNameLoc, |
| 551 | unsigned Depth, unsigned Position, |
| 552 | SourceLocation EqualLoc, |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 553 | ParsedType DefaultArg) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 554 | assert(S->isTemplateParamScope() && |
| 555 | "Template type parameter not in template parameter scope!"); |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 556 | bool Invalid = false; |
| 557 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 558 | SourceLocation Loc = ParamNameLoc; |
| 559 | if (!ParamName) |
| 560 | Loc = KeyLoc; |
| 561 | |
Nikola Smiljanic | 69fdc9f | 2014-06-06 02:58:59 +0000 | [diff] [blame] | 562 | bool IsParameterPack = EllipsisLoc.isValid(); |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 563 | TemplateTypeParmDecl *Param |
John McCall | f7b2fb5 | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 564 | = TemplateTypeParmDecl::Create(Context, Context.getTranslationUnitDecl(), |
Abramo Bagnara | b3185b0 | 2011-03-06 15:48:19 +0000 | [diff] [blame] | 565 | KeyLoc, Loc, Depth, Position, ParamName, |
Nikola Smiljanic | 69fdc9f | 2014-06-06 02:58:59 +0000 | [diff] [blame] | 566 | Typename, IsParameterPack); |
Douglas Gregor | fd7c225 | 2011-03-04 17:52:15 +0000 | [diff] [blame] | 567 | Param->setAccess(AS_public); |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 568 | if (Invalid) |
| 569 | Param->setInvalidDecl(); |
| 570 | |
| 571 | if (ParamName) { |
Richard Smith | b80d540 | 2013-06-25 22:21:36 +0000 | [diff] [blame] | 572 | maybeDiagnoseTemplateParameterShadow(*this, S, ParamNameLoc, ParamName); |
| 573 | |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 574 | // Add the template parameter into the current scope. |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 575 | S->AddDecl(Param); |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 576 | IdResolver.AddDecl(Param); |
| 577 | } |
| 578 | |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 579 | // C++0x [temp.param]p9: |
| 580 | // A default template-argument may be specified for any kind of |
| 581 | // template-parameter that is not a template parameter pack. |
Nikola Smiljanic | 69fdc9f | 2014-06-06 02:58:59 +0000 | [diff] [blame] | 582 | if (DefaultArg && IsParameterPack) { |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 583 | Diag(EqualLoc, diag::err_template_param_pack_default_arg); |
| 584 | DefaultArg = ParsedType(); |
| 585 | } |
| 586 | |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 587 | // Handle the default argument, if provided. |
| 588 | if (DefaultArg) { |
| 589 | TypeSourceInfo *DefaultTInfo; |
| 590 | GetTypeFromParser(DefaultArg, &DefaultTInfo); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 591 | |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 592 | assert(DefaultTInfo && "expected source information for type"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 593 | |
Douglas Gregor | 6ff1fbf | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 594 | // Check for unexpanded parameter packs. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 595 | if (DiagnoseUnexpandedParameterPack(Loc, DefaultTInfo, |
Douglas Gregor | 6ff1fbf | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 596 | UPPC_DefaultArgument)) |
| 597 | return Param; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 598 | |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 599 | // Check the template argument itself. |
| 600 | if (CheckTemplateArgument(Param, DefaultTInfo)) { |
| 601 | Param->setInvalidDecl(); |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 602 | return Param; |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 603 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 604 | |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 605 | Param->setDefaultArgument(DefaultTInfo, false); |
| 606 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 607 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 608 | return Param; |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 609 | } |
| 610 | |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 611 | /// \brief Check that the type of a non-type template parameter is |
| 612 | /// well-formed. |
| 613 | /// |
| 614 | /// \returns the (possibly-promoted) parameter type if valid; |
| 615 | /// otherwise, produces a diagnostic and returns a NULL type. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 616 | QualType |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 617 | Sema::CheckNonTypeTemplateParameterType(QualType T, SourceLocation Loc) { |
Douglas Gregor | a09387d | 2010-05-23 19:57:01 +0000 | [diff] [blame] | 618 | // We don't allow variably-modified types as the type of non-type template |
| 619 | // parameters. |
| 620 | if (T->isVariablyModifiedType()) { |
| 621 | Diag(Loc, diag::err_variably_modified_nontype_template_param) |
| 622 | << T; |
| 623 | return QualType(); |
| 624 | } |
| 625 | |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 626 | // C++ [temp.param]p4: |
| 627 | // |
| 628 | // A non-type template-parameter shall have one of the following |
| 629 | // (optionally cv-qualified) types: |
| 630 | // |
| 631 | // -- integral or enumeration type, |
Douglas Gregor | b90df60 | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 632 | if (T->isIntegralOrEnumerationType() || |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 633 | // -- pointer to object or pointer to function, |
Eli Friedman | a170cd6 | 2010-08-05 02:49:48 +0000 | [diff] [blame] | 634 | T->isPointerType() || |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 635 | // -- reference to object or reference to function, |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 636 | T->isReferenceType() || |
Douglas Gregor | 80af313 | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 637 | // -- pointer to member, |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 638 | T->isMemberPointerType() || |
Douglas Gregor | 80af313 | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 639 | // -- std::nullptr_t. |
| 640 | T->isNullPtrType() || |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 641 | // If T is a dependent type, we can't do the check now, so we |
| 642 | // assume that it is well-formed. |
Richard Smith | d0e1c95 | 2012-03-13 07:21:50 +0000 | [diff] [blame] | 643 | T->isDependentType()) { |
| 644 | // C++ [temp.param]p5: The top-level cv-qualifiers on the template-parameter |
| 645 | // are ignored when determining its type. |
| 646 | return T.getUnqualifiedType(); |
| 647 | } |
| 648 | |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 649 | // C++ [temp.param]p8: |
| 650 | // |
| 651 | // A non-type template-parameter of type "array of T" or |
| 652 | // "function returning T" is adjusted to be of type "pointer to |
| 653 | // T" or "pointer to function returning T", respectively. |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 654 | else if (T->isArrayType() || T->isFunctionType()) |
| 655 | return Context.getDecayedType(T); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 656 | |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 657 | Diag(Loc, diag::err_template_nontype_parm_bad_type) |
| 658 | << T; |
| 659 | |
| 660 | return QualType(); |
| 661 | } |
| 662 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 663 | Decl *Sema::ActOnNonTypeTemplateParameter(Scope *S, Declarator &D, |
| 664 | unsigned Depth, |
| 665 | unsigned Position, |
| 666 | SourceLocation EqualLoc, |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 667 | Expr *Default) { |
John McCall | 8cb7bdf | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 668 | TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S); |
| 669 | QualType T = TInfo->getType(); |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 670 | |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 671 | assert(S->isTemplateParamScope() && |
| 672 | "Non-type template parameter not in template parameter scope!"); |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 673 | bool Invalid = false; |
| 674 | |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 675 | T = CheckNonTypeTemplateParameterType(T, D.getIdentifierLoc()); |
| 676 | if (T.isNull()) { |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 677 | T = Context.IntTy; // Recover with an 'int' type. |
Douglas Gregor | ce0fc86f | 2009-03-09 16:46:39 +0000 | [diff] [blame] | 678 | Invalid = true; |
| 679 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 680 | |
Richard Smith | b80d540 | 2013-06-25 22:21:36 +0000 | [diff] [blame] | 681 | IdentifierInfo *ParamName = D.getIdentifier(); |
Douglas Gregor | da3cc0d | 2010-12-23 23:51:58 +0000 | [diff] [blame] | 682 | bool IsParameterPack = D.hasEllipsis(); |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 683 | NonTypeTemplateParmDecl *Param |
John McCall | f7b2fb5 | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 684 | = NonTypeTemplateParmDecl::Create(Context, Context.getTranslationUnitDecl(), |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 685 | D.getLocStart(), |
John McCall | f7b2fb5 | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 686 | D.getIdentifierLoc(), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 687 | Depth, Position, ParamName, T, |
Douglas Gregor | da3cc0d | 2010-12-23 23:51:58 +0000 | [diff] [blame] | 688 | IsParameterPack, TInfo); |
Douglas Gregor | fd7c225 | 2011-03-04 17:52:15 +0000 | [diff] [blame] | 689 | Param->setAccess(AS_public); |
Richard Smith | b80d540 | 2013-06-25 22:21:36 +0000 | [diff] [blame] | 690 | |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 691 | if (Invalid) |
| 692 | Param->setInvalidDecl(); |
| 693 | |
Richard Smith | b80d540 | 2013-06-25 22:21:36 +0000 | [diff] [blame] | 694 | if (ParamName) { |
| 695 | maybeDiagnoseTemplateParameterShadow(*this, S, D.getIdentifierLoc(), |
| 696 | ParamName); |
| 697 | |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 698 | // Add the template parameter into the current scope. |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 699 | S->AddDecl(Param); |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 700 | IdResolver.AddDecl(Param); |
| 701 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 702 | |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 703 | // C++0x [temp.param]p9: |
| 704 | // A default template-argument may be specified for any kind of |
| 705 | // template-parameter that is not a template parameter pack. |
| 706 | if (Default && IsParameterPack) { |
| 707 | Diag(EqualLoc, diag::err_template_param_pack_default_arg); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 708 | Default = nullptr; |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 709 | } |
| 710 | |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 711 | // Check the well-formedness of the default template argument, if provided. |
Douglas Gregor | da3cc0d | 2010-12-23 23:51:58 +0000 | [diff] [blame] | 712 | if (Default) { |
Douglas Gregor | 6ff1fbf | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 713 | // Check for unexpanded parameter packs. |
| 714 | if (DiagnoseUnexpandedParameterPack(Default, UPPC_DefaultArgument)) |
| 715 | return Param; |
| 716 | |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 717 | TemplateArgument Converted; |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 718 | ExprResult DefaultRes = |
| 719 | CheckTemplateArgument(Param, Param->getType(), Default, Converted); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 720 | if (DefaultRes.isInvalid()) { |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 721 | Param->setInvalidDecl(); |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 722 | return Param; |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 723 | } |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 724 | Default = DefaultRes.get(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 725 | |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 726 | Param->setDefaultArgument(Default, false); |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 727 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 728 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 729 | return Param; |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 730 | } |
Douglas Gregor | b9bd8a9 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 731 | |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 732 | /// ActOnTemplateTemplateParameter - Called when a C++ template template |
James Dennett | 2a4d13c | 2012-06-15 07:13:21 +0000 | [diff] [blame] | 733 | /// parameter (e.g. T in template <template \<typename> class T> class array) |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 734 | /// has been parsed. S is the current scope. |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 735 | Decl *Sema::ActOnTemplateTemplateParameter(Scope* S, |
| 736 | SourceLocation TmpLoc, |
Richard Trieu | 9becef6 | 2011-09-09 03:18:59 +0000 | [diff] [blame] | 737 | TemplateParameterList *Params, |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 738 | SourceLocation EllipsisLoc, |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 739 | IdentifierInfo *Name, |
| 740 | SourceLocation NameLoc, |
| 741 | unsigned Depth, |
| 742 | unsigned Position, |
| 743 | SourceLocation EqualLoc, |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 744 | ParsedTemplateArgument Default) { |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 745 | assert(S->isTemplateParamScope() && |
| 746 | "Template template parameter not in template parameter scope!"); |
| 747 | |
| 748 | // Construct the parameter object. |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 749 | bool IsParameterPack = EllipsisLoc.isValid(); |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 750 | TemplateTemplateParmDecl *Param = |
John McCall | f7b2fb5 | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 751 | TemplateTemplateParmDecl::Create(Context, Context.getTranslationUnitDecl(), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 752 | NameLoc.isInvalid()? TmpLoc : NameLoc, |
| 753 | Depth, Position, IsParameterPack, |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 754 | Name, Params); |
Douglas Gregor | fd7c225 | 2011-03-04 17:52:15 +0000 | [diff] [blame] | 755 | Param->setAccess(AS_public); |
| 756 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 757 | // If the template template parameter has a name, then link the identifier |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 758 | // into the scope and lookup mechanisms. |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 759 | if (Name) { |
Richard Smith | b80d540 | 2013-06-25 22:21:36 +0000 | [diff] [blame] | 760 | maybeDiagnoseTemplateParameterShadow(*this, S, NameLoc, Name); |
| 761 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 762 | S->AddDecl(Param); |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 763 | IdResolver.AddDecl(Param); |
| 764 | } |
| 765 | |
Douglas Gregor | 6ff1fbf | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 766 | if (Params->size() == 0) { |
| 767 | Diag(Param->getLocation(), diag::err_template_template_parm_no_parms) |
| 768 | << SourceRange(Params->getLAngleLoc(), Params->getRAngleLoc()); |
| 769 | Param->setInvalidDecl(); |
| 770 | } |
| 771 | |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 772 | // C++0x [temp.param]p9: |
| 773 | // A default template-argument may be specified for any kind of |
| 774 | // template-parameter that is not a template parameter pack. |
| 775 | if (IsParameterPack && !Default.isInvalid()) { |
| 776 | Diag(EqualLoc, diag::err_template_param_pack_default_arg); |
| 777 | Default = ParsedTemplateArgument(); |
| 778 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 779 | |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 780 | if (!Default.isInvalid()) { |
| 781 | // Check only that we have a template template argument. We don't want to |
| 782 | // try to check well-formedness now, because our template template parameter |
| 783 | // might have dependent types in its template parameters, which we wouldn't |
| 784 | // be able to match now. |
| 785 | // |
| 786 | // If none of the template template parameter's template arguments mention |
| 787 | // other template parameters, we could actually perform more checking here. |
| 788 | // However, it isn't worth doing. |
| 789 | TemplateArgumentLoc DefaultArg = translateTemplateArgument(*this, Default); |
| 790 | if (DefaultArg.getArgument().getAsTemplate().isNull()) { |
| 791 | Diag(DefaultArg.getLocation(), diag::err_template_arg_not_class_template) |
| 792 | << DefaultArg.getSourceRange(); |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 793 | return Param; |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 794 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 795 | |
Douglas Gregor | 6ff1fbf | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 796 | // Check for unexpanded parameter packs. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 797 | if (DiagnoseUnexpandedParameterPack(DefaultArg.getLocation(), |
Douglas Gregor | 6ff1fbf | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 798 | DefaultArg.getArgument().getAsTemplate(), |
| 799 | UPPC_DefaultArgument)) |
| 800 | return Param; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 801 | |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 802 | Param->setDefaultArgument(DefaultArg, false); |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 803 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 804 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 805 | return Param; |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 806 | } |
| 807 | |
Douglas Gregor | b9bd8a9 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 808 | /// ActOnTemplateParameterList - Builds a TemplateParameterList that |
| 809 | /// contains the template parameters in Params/NumParams. |
Richard Trieu | 9becef6 | 2011-09-09 03:18:59 +0000 | [diff] [blame] | 810 | TemplateParameterList * |
Douglas Gregor | b9bd8a9 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 811 | Sema::ActOnTemplateParameterList(unsigned Depth, |
| 812 | SourceLocation ExportLoc, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 813 | SourceLocation TemplateLoc, |
Douglas Gregor | b9bd8a9 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 814 | SourceLocation LAngleLoc, |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 815 | Decl **Params, unsigned NumParams, |
Douglas Gregor | b9bd8a9 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 816 | SourceLocation RAngleLoc) { |
| 817 | if (ExportLoc.isValid()) |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 818 | Diag(ExportLoc, diag::warn_template_export_unsupported); |
Douglas Gregor | b9bd8a9 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 819 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 820 | return TemplateParameterList::Create(Context, TemplateLoc, LAngleLoc, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 821 | (NamedDecl**)Params, NumParams, |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 822 | RAngleLoc); |
Douglas Gregor | b9bd8a9 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 823 | } |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 824 | |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 825 | static void SetNestedNameSpecifier(TagDecl *T, const CXXScopeSpec &SS) { |
| 826 | if (SS.isSet()) |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 827 | T->setQualifierInfo(SS.getWithLocInContext(T->getASTContext())); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 828 | } |
| 829 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 830 | DeclResult |
John McCall | 9bb74a5 | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 831 | Sema::CheckClassTemplate(Scope *S, unsigned TagSpec, TagUseKind TUK, |
Jeffrey Yasskin | c76498d | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 832 | SourceLocation KWLoc, CXXScopeSpec &SS, |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 833 | IdentifierInfo *Name, SourceLocation NameLoc, |
| 834 | AttributeList *Attr, |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 835 | TemplateParameterList *TemplateParams, |
Douglas Gregor | 2820e69 | 2011-09-09 19:05:14 +0000 | [diff] [blame] | 836 | AccessSpecifier AS, SourceLocation ModulePrivateLoc, |
Nikola Smiljanic | 4fc9153 | 2014-07-17 01:59:34 +0000 | [diff] [blame] | 837 | SourceLocation FriendLoc, |
Abramo Bagnara | 0adf29a | 2011-03-10 13:28:31 +0000 | [diff] [blame] | 838 | unsigned NumOuterTemplateParamLists, |
| 839 | TemplateParameterList** OuterTemplateParamLists) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 840 | assert(TemplateParams && TemplateParams->size() > 0 && |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 841 | "No template parameters"); |
John McCall | 9bb74a5 | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 842 | assert(TUK != TUK_Reference && "Can only declare or define class templates"); |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 843 | bool Invalid = false; |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 844 | |
| 845 | // Check that we can declare a template here. |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 846 | if (CheckTemplateDeclScope(S, TemplateParams)) |
Douglas Gregor | c08f489 | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 847 | return true; |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 848 | |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 849 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
| 850 | assert(Kind != TTK_Enum && "can't build template of enumerated type"); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 851 | |
| 852 | // There is no such thing as an unnamed class template. |
| 853 | if (!Name) { |
| 854 | Diag(KWLoc, diag::err_template_unnamed_class); |
Douglas Gregor | c08f489 | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 855 | return true; |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 856 | } |
| 857 | |
Richard Smith | 6483d22 | 2012-04-21 01:27:54 +0000 | [diff] [blame] | 858 | // Find any previous declaration with this name. For a friend with no |
| 859 | // scope explicitly specified, we only look for tag declarations (per |
| 860 | // C++11 [basic.lookup.elab]p2). |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 861 | DeclContext *SemanticContext; |
Richard Smith | 6483d22 | 2012-04-21 01:27:54 +0000 | [diff] [blame] | 862 | LookupResult Previous(*this, Name, NameLoc, |
| 863 | (SS.isEmpty() && TUK == TUK_Friend) |
| 864 | ? LookupTagName : LookupOrdinaryName, |
John McCall | 5cebab1 | 2009-11-18 07:57:50 +0000 | [diff] [blame] | 865 | ForRedeclaration); |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 866 | if (SS.isNotEmpty() && !SS.isInvalid()) { |
| 867 | SemanticContext = computeDeclContext(SS, true); |
| 868 | if (!SemanticContext) { |
Douglas Gregor | 67daacb | 2012-03-30 16:20:47 +0000 | [diff] [blame] | 869 | // FIXME: Horrible, horrible hack! We can't currently represent this |
| 870 | // in the AST, and historically we have just ignored such friend |
| 871 | // class templates, so don't complain here. |
Richard Smith | cd556eb | 2013-11-08 18:59:56 +0000 | [diff] [blame] | 872 | Diag(NameLoc, TUK == TUK_Friend |
| 873 | ? diag::warn_template_qualified_friend_ignored |
| 874 | : diag::err_template_qualified_declarator_no_match) |
Douglas Gregor | 67daacb | 2012-03-30 16:20:47 +0000 | [diff] [blame] | 875 | << SS.getScopeRep() << SS.getRange(); |
Richard Smith | cd556eb | 2013-11-08 18:59:56 +0000 | [diff] [blame] | 876 | return TUK != TUK_Friend; |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 877 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 878 | |
John McCall | 0b66eb3 | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 879 | if (RequireCompleteDeclContext(SS, SemanticContext)) |
| 880 | return true; |
| 881 | |
Douglas Gregor | 041b084 | 2011-10-14 15:31:12 +0000 | [diff] [blame] | 882 | // If we're adding a template to a dependent context, we may need to |
| 883 | // rebuilding some of the types used within the template parameter list, |
| 884 | // now that we know what the current instantiation is. |
| 885 | if (SemanticContext->isDependentContext()) { |
| 886 | ContextRAII SavedContext(*this, SemanticContext); |
| 887 | if (RebuildTemplateParamsInCurrentInstantiation(TemplateParams)) |
| 888 | Invalid = true; |
Douglas Gregor | b7d17dd | 2012-03-28 16:01:27 +0000 | [diff] [blame] | 889 | } else if (TUK != TUK_Friend && TUK != TUK_Reference) |
| 890 | diagnoseQualifiedDeclaration(SS, SemanticContext, Name, NameLoc); |
Richard Smith | 6483d22 | 2012-04-21 01:27:54 +0000 | [diff] [blame] | 891 | |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 892 | LookupQualifiedName(Previous, SemanticContext); |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 893 | } else { |
| 894 | SemanticContext = CurContext; |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 895 | LookupName(Previous, S); |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 896 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 897 | |
Douglas Gregor | ce40e2e | 2010-04-12 16:00:01 +0000 | [diff] [blame] | 898 | if (Previous.isAmbiguous()) |
| 899 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 900 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 901 | NamedDecl *PrevDecl = nullptr; |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 902 | if (Previous.begin() != Previous.end()) |
Douglas Gregor | ce40e2e | 2010-04-12 16:00:01 +0000 | [diff] [blame] | 903 | PrevDecl = (*Previous.begin())->getUnderlyingDecl(); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 904 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 905 | // If there is a previous declaration with the same name, check |
| 906 | // whether this is a valid redeclaration. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 907 | ClassTemplateDecl *PrevClassTemplate |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 908 | = dyn_cast_or_null<ClassTemplateDecl>(PrevDecl); |
Douglas Gregor | 7f34bae | 2009-10-09 21:11:42 +0000 | [diff] [blame] | 909 | |
| 910 | // We may have found the injected-class-name of a class template, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 911 | // class template partial specialization, or class template specialization. |
Douglas Gregor | 7f34bae | 2009-10-09 21:11:42 +0000 | [diff] [blame] | 912 | // In these cases, grab the template that is being defined or specialized. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 913 | if (!PrevClassTemplate && PrevDecl && isa<CXXRecordDecl>(PrevDecl) && |
Douglas Gregor | 7f34bae | 2009-10-09 21:11:42 +0000 | [diff] [blame] | 914 | cast<CXXRecordDecl>(PrevDecl)->isInjectedClassName()) { |
| 915 | PrevDecl = cast<CXXRecordDecl>(PrevDecl->getDeclContext()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 916 | PrevClassTemplate |
Douglas Gregor | 7f34bae | 2009-10-09 21:11:42 +0000 | [diff] [blame] | 917 | = cast<CXXRecordDecl>(PrevDecl)->getDescribedClassTemplate(); |
| 918 | if (!PrevClassTemplate && isa<ClassTemplateSpecializationDecl>(PrevDecl)) { |
| 919 | PrevClassTemplate |
| 920 | = cast<ClassTemplateSpecializationDecl>(PrevDecl) |
| 921 | ->getSpecializedTemplate(); |
| 922 | } |
| 923 | } |
| 924 | |
John McCall | d43784f | 2009-12-18 11:25:59 +0000 | [diff] [blame] | 925 | if (TUK == TUK_Friend) { |
John McCall | 90d3bb9 | 2009-12-17 23:21:11 +0000 | [diff] [blame] | 926 | // C++ [namespace.memdef]p3: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 927 | // [...] When looking for a prior declaration of a class or a function |
| 928 | // declared as a friend, and when the name of the friend class or |
John McCall | 90d3bb9 | 2009-12-17 23:21:11 +0000 | [diff] [blame] | 929 | // function is neither a qualified name nor a template-id, scopes outside |
| 930 | // the innermost enclosing namespace scope are not considered. |
Douglas Gregor | b74b103 | 2010-04-18 17:37:40 +0000 | [diff] [blame] | 931 | if (!SS.isSet()) { |
| 932 | DeclContext *OutermostContext = CurContext; |
| 933 | while (!OutermostContext->isFileContext()) |
| 934 | OutermostContext = OutermostContext->getLookupParent(); |
John McCall | d43784f | 2009-12-18 11:25:59 +0000 | [diff] [blame] | 935 | |
Richard Smith | 61e582f | 2012-04-20 07:12:26 +0000 | [diff] [blame] | 936 | if (PrevDecl && |
Douglas Gregor | b74b103 | 2010-04-18 17:37:40 +0000 | [diff] [blame] | 937 | (OutermostContext->Equals(PrevDecl->getDeclContext()) || |
| 938 | OutermostContext->Encloses(PrevDecl->getDeclContext()))) { |
| 939 | SemanticContext = PrevDecl->getDeclContext(); |
| 940 | } else { |
| 941 | // Declarations in outer scopes don't matter. However, the outermost |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 942 | // context we computed is the semantic context for our new |
Douglas Gregor | b74b103 | 2010-04-18 17:37:40 +0000 | [diff] [blame] | 943 | // declaration. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 944 | PrevDecl = PrevClassTemplate = nullptr; |
Douglas Gregor | b74b103 | 2010-04-18 17:37:40 +0000 | [diff] [blame] | 945 | SemanticContext = OutermostContext; |
Richard Smith | 6483d22 | 2012-04-21 01:27:54 +0000 | [diff] [blame] | 946 | |
| 947 | // Check that the chosen semantic context doesn't already contain a |
| 948 | // declaration of this name as a non-tag type. |
| 949 | LookupResult Previous(*this, Name, NameLoc, LookupOrdinaryName, |
| 950 | ForRedeclaration); |
| 951 | DeclContext *LookupContext = SemanticContext; |
| 952 | while (LookupContext->isTransparentContext()) |
| 953 | LookupContext = LookupContext->getLookupParent(); |
| 954 | LookupQualifiedName(Previous, LookupContext); |
| 955 | |
| 956 | if (Previous.isAmbiguous()) |
| 957 | return true; |
| 958 | |
| 959 | if (Previous.begin() != Previous.end()) |
| 960 | PrevDecl = (*Previous.begin())->getUnderlyingDecl(); |
Douglas Gregor | b74b103 | 2010-04-18 17:37:40 +0000 | [diff] [blame] | 961 | } |
John McCall | 90d3bb9 | 2009-12-17 23:21:11 +0000 | [diff] [blame] | 962 | } |
Richard Smith | 72bcaec | 2013-12-05 04:30:04 +0000 | [diff] [blame] | 963 | } else if (PrevDecl && |
| 964 | !isDeclInScope(PrevDecl, SemanticContext, S, SS.isValid())) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 965 | PrevDecl = PrevClassTemplate = nullptr; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 966 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 967 | if (PrevClassTemplate) { |
Richard Smith | e85e176 | 2012-04-22 02:13:50 +0000 | [diff] [blame] | 968 | // Ensure that the template parameter lists are compatible. Skip this check |
| 969 | // for a friend in a dependent context: the template parameter list itself |
| 970 | // could be dependent. |
| 971 | if (!(TUK == TUK_Friend && CurContext->isDependentContext()) && |
| 972 | !TemplateParameterListsAreEqual(TemplateParams, |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 973 | PrevClassTemplate->getTemplateParameters(), |
Douglas Gregor | 19ac2d6 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 974 | /*Complain=*/true, |
| 975 | TPL_TemplateMatch)) |
Douglas Gregor | c08f489 | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 976 | return true; |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 977 | |
| 978 | // C++ [temp.class]p4: |
| 979 | // In a redeclaration, partial specialization, explicit |
| 980 | // specialization or explicit instantiation of a class template, |
| 981 | // the class-key shall agree in kind with the original class |
| 982 | // template declaration (7.1.5.3). |
| 983 | RecordDecl *PrevRecordDecl = PrevClassTemplate->getTemplatedDecl(); |
Richard Trieu | caa33d3 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 984 | if (!isAcceptableTagRedeclaration(PrevRecordDecl, Kind, |
| 985 | TUK == TUK_Definition, KWLoc, *Name)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 986 | Diag(KWLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | 170512f | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 987 | << Name |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 988 | << FixItHint::CreateReplacement(KWLoc, PrevRecordDecl->getKindName()); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 989 | Diag(PrevRecordDecl->getLocation(), diag::note_previous_use); |
Douglas Gregor | 170512f | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 990 | Kind = PrevRecordDecl->getTagKind(); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 991 | } |
| 992 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 993 | // Check for redefinition of this class template. |
John McCall | 9bb74a5 | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 994 | if (TUK == TUK_Definition) { |
Douglas Gregor | 0a5a221 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 995 | if (TagDecl *Def = PrevRecordDecl->getDefinition()) { |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 996 | Diag(NameLoc, diag::err_redefinition) << Name; |
| 997 | Diag(Def->getLocation(), diag::note_previous_definition); |
| 998 | // FIXME: Would it make sense to try to "forget" the previous |
| 999 | // definition, as part of error recovery? |
Douglas Gregor | c08f489 | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 1000 | return true; |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1001 | } |
Douglas Gregor | ef15bdb | 2011-09-09 18:32:39 +0000 | [diff] [blame] | 1002 | } |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1003 | } else if (PrevDecl && PrevDecl->isTemplateParameter()) { |
| 1004 | // Maybe we will complain about the shadowed template parameter. |
| 1005 | DiagnoseTemplateParameterShadow(NameLoc, PrevDecl); |
| 1006 | // Just pretend that we didn't see the previous declaration. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1007 | PrevDecl = nullptr; |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1008 | } else if (PrevDecl) { |
| 1009 | // C++ [temp]p5: |
| 1010 | // A class template shall not have the same name as any other |
| 1011 | // template, class, function, object, enumeration, enumerator, |
| 1012 | // namespace, or type in the same scope (3.3), except as specified |
| 1013 | // in (14.5.4). |
| 1014 | Diag(NameLoc, diag::err_redefinition_different_kind) << Name; |
| 1015 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
Douglas Gregor | c08f489 | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 1016 | return true; |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1017 | } |
| 1018 | |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1019 | // Check the template parameter list of this declaration, possibly |
| 1020 | // merging in the template parameter list from the previous class |
Richard Smith | e85e176 | 2012-04-22 02:13:50 +0000 | [diff] [blame] | 1021 | // template declaration. Skip this check for a friend in a dependent |
| 1022 | // context, because the template parameter list might be dependent. |
| 1023 | if (!(TUK == TUK_Friend && CurContext->isDependentContext()) && |
David Majnemer | ba8f17a | 2013-06-25 22:08:55 +0000 | [diff] [blame] | 1024 | CheckTemplateParameterList( |
| 1025 | TemplateParams, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1026 | PrevClassTemplate ? PrevClassTemplate->getTemplateParameters() |
| 1027 | : nullptr, |
David Majnemer | ba8f17a | 2013-06-25 22:08:55 +0000 | [diff] [blame] | 1028 | (SS.isSet() && SemanticContext && SemanticContext->isRecord() && |
| 1029 | SemanticContext->isDependentContext()) |
| 1030 | ? TPC_ClassTemplateMember |
| 1031 | : TUK == TUK_Friend ? TPC_FriendClassTemplate |
| 1032 | : TPC_ClassTemplate)) |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1033 | Invalid = true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1034 | |
Douglas Gregor | ce40e2e | 2010-04-12 16:00:01 +0000 | [diff] [blame] | 1035 | if (SS.isSet()) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1036 | // If the name of the template was qualified, we must be defining the |
Douglas Gregor | ce40e2e | 2010-04-12 16:00:01 +0000 | [diff] [blame] | 1037 | // template out-of-line. |
Richard Smith | e85e176 | 2012-04-22 02:13:50 +0000 | [diff] [blame] | 1038 | if (!SS.isInvalid() && !Invalid && !PrevClassTemplate) { |
| 1039 | Diag(NameLoc, TUK == TUK_Friend ? diag::err_friend_decl_does_not_match |
Richard Smith | 114394f | 2013-08-09 04:35:01 +0000 | [diff] [blame] | 1040 | : diag::err_member_decl_does_not_match) |
| 1041 | << Name << SemanticContext << /*IsDefinition*/true << SS.getRange(); |
Douglas Gregor | fe0055e | 2011-11-01 21:35:16 +0000 | [diff] [blame] | 1042 | Invalid = true; |
| 1043 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1044 | } |
| 1045 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1046 | CXXRecordDecl *NewClass = |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 1047 | CXXRecordDecl::Create(Context, Kind, SemanticContext, KWLoc, NameLoc, Name, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1048 | PrevClassTemplate? |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1049 | PrevClassTemplate->getTemplatedDecl() : nullptr, |
Douglas Gregor | 1ec5e9f | 2009-05-15 19:11:46 +0000 | [diff] [blame] | 1050 | /*DelayTypeCreation=*/true); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 1051 | SetNestedNameSpecifier(NewClass, SS); |
Abramo Bagnara | 0adf29a | 2011-03-10 13:28:31 +0000 | [diff] [blame] | 1052 | if (NumOuterTemplateParamLists > 0) |
| 1053 | NewClass->setTemplateParameterListsInfo(Context, |
| 1054 | NumOuterTemplateParamLists, |
| 1055 | OuterTemplateParamLists); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1056 | |
Eli Friedman | edb6f5d | 2012-02-10 02:02:21 +0000 | [diff] [blame] | 1057 | // Add alignment attributes if necessary; these attributes are checked when |
| 1058 | // the ASTContext lays out the structure. |
Eli Friedman | 0415f3e1 | 2012-08-08 21:08:34 +0000 | [diff] [blame] | 1059 | if (TUK == TUK_Definition) { |
| 1060 | AddAlignmentAttributesForRecord(NewClass); |
| 1061 | AddMsStructLayoutForRecord(NewClass); |
| 1062 | } |
Eli Friedman | edb6f5d | 2012-02-10 02:02:21 +0000 | [diff] [blame] | 1063 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1064 | ClassTemplateDecl *NewTemplate |
| 1065 | = ClassTemplateDecl::Create(Context, SemanticContext, NameLoc, |
| 1066 | DeclarationName(Name), TemplateParams, |
Douglas Gregor | 90a1a65 | 2009-03-19 17:26:29 +0000 | [diff] [blame] | 1067 | NewClass, PrevClassTemplate); |
Douglas Gregor | 97f1f1c | 2009-03-26 00:10:35 +0000 | [diff] [blame] | 1068 | NewClass->setDescribedClassTemplate(NewTemplate); |
Douglas Gregor | ef15bdb | 2011-09-09 18:32:39 +0000 | [diff] [blame] | 1069 | |
Douglas Gregor | 21823bf | 2011-12-20 18:11:52 +0000 | [diff] [blame] | 1070 | if (ModulePrivateLoc.isValid()) |
Douglas Gregor | ef15bdb | 2011-09-09 18:32:39 +0000 | [diff] [blame] | 1071 | NewTemplate->setModulePrivate(); |
Douglas Gregor | 26701a4 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 1072 | |
Douglas Gregor | 1ec5e9f | 2009-05-15 19:11:46 +0000 | [diff] [blame] | 1073 | // Build the type for the class template declaration now. |
Douglas Gregor | 9961ce9 | 2010-07-08 18:37:38 +0000 | [diff] [blame] | 1074 | QualType T = NewTemplate->getInjectedClassNameSpecialization(); |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 1075 | T = Context.getInjectedClassNameType(NewClass, T); |
Douglas Gregor | 1ec5e9f | 2009-05-15 19:11:46 +0000 | [diff] [blame] | 1076 | assert(T->isDependentType() && "Class template type is not dependent?"); |
| 1077 | (void)T; |
| 1078 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1079 | // If we are providing an explicit specialization of a member that is a |
Douglas Gregor | cf91555 | 2009-10-13 16:30:37 +0000 | [diff] [blame] | 1080 | // class template, make a note of that. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1081 | if (PrevClassTemplate && |
Douglas Gregor | cf91555 | 2009-10-13 16:30:37 +0000 | [diff] [blame] | 1082 | PrevClassTemplate->getInstantiatedFromMemberTemplate()) |
| 1083 | PrevClassTemplate->setMemberSpecialization(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1084 | |
Anders Carlsson | 137108d | 2009-03-26 01:24:28 +0000 | [diff] [blame] | 1085 | // Set the access specifier. |
Douglas Gregor | 31feb33 | 2012-03-17 23:06:31 +0000 | [diff] [blame] | 1086 | if (!Invalid && TUK != TUK_Friend && NewTemplate->getDeclContext()->isRecord()) |
John McCall | 27b5c25 | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1087 | SetMemberAccessSpecifier(NewTemplate, PrevClassTemplate, AS); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1088 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1089 | // Set the lexical context of these templates |
| 1090 | NewClass->setLexicalDeclContext(CurContext); |
| 1091 | NewTemplate->setLexicalDeclContext(CurContext); |
| 1092 | |
John McCall | 9bb74a5 | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 1093 | if (TUK == TUK_Definition) |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1094 | NewClass->startDefinition(); |
| 1095 | |
| 1096 | if (Attr) |
Douglas Gregor | 758a869 | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 1097 | ProcessDeclAttributeList(S, NewClass, Attr); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1098 | |
Rafael Espindola | 0c6c405 | 2012-08-22 14:52:14 +0000 | [diff] [blame] | 1099 | if (PrevClassTemplate) |
| 1100 | mergeDeclAttributes(NewClass, PrevClassTemplate->getTemplatedDecl()); |
| 1101 | |
Rafael Espindola | 385c042 | 2012-07-13 18:04:45 +0000 | [diff] [blame] | 1102 | AddPushedVisibilityAttribute(NewClass); |
| 1103 | |
Richard Smith | 234ff47 | 2014-08-23 00:49:01 +0000 | [diff] [blame] | 1104 | if (TUK != TUK_Friend) { |
| 1105 | // Per C++ [basic.scope.temp]p2, skip the template parameter scopes. |
| 1106 | Scope *Outer = S; |
| 1107 | while ((Outer->getFlags() & Scope::TemplateParamScope) != 0) |
| 1108 | Outer = Outer->getParent(); |
| 1109 | PushOnScopeChains(NewTemplate, Outer); |
| 1110 | } else { |
Douglas Gregor | 3dad842 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 1111 | if (PrevClassTemplate && PrevClassTemplate->getAccess() != AS_none) { |
John McCall | 27b5c25 | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1112 | NewTemplate->setAccess(PrevClassTemplate->getAccess()); |
Douglas Gregor | 3dad842 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 1113 | NewClass->setAccess(PrevClassTemplate->getAccess()); |
| 1114 | } |
John McCall | 27b5c25 | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1115 | |
Richard Smith | 6401768 | 2013-07-17 23:53:16 +0000 | [diff] [blame] | 1116 | NewTemplate->setObjectOfFriendDecl(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1117 | |
John McCall | 27b5c25 | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1118 | // Friend templates are visible in fairly strange ways. |
| 1119 | if (!CurContext->isDependentContext()) { |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 1120 | DeclContext *DC = SemanticContext->getRedeclContext(); |
Richard Smith | 05afe5e | 2012-03-13 03:12:56 +0000 | [diff] [blame] | 1121 | DC->makeDeclVisibleInContext(NewTemplate); |
John McCall | 27b5c25 | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1122 | if (Scope *EnclosingScope = getScopeForDeclContext(S, DC)) |
| 1123 | PushOnScopeChains(NewTemplate, EnclosingScope, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1124 | /* AddToContext = */ false); |
John McCall | 27b5c25 | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1125 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1126 | |
Nikola Smiljanic | 4fc9153 | 2014-07-17 01:59:34 +0000 | [diff] [blame] | 1127 | FriendDecl *Friend = FriendDecl::Create( |
| 1128 | Context, CurContext, NewClass->getLocation(), NewTemplate, FriendLoc); |
Douglas Gregor | 3dad842 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 1129 | Friend->setAccess(AS_public); |
| 1130 | CurContext->addDecl(Friend); |
John McCall | 27b5c25 | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1131 | } |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1132 | |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1133 | if (Invalid) { |
| 1134 | NewTemplate->setInvalidDecl(); |
| 1135 | NewClass->setInvalidDecl(); |
| 1136 | } |
Rafael Espindola | eca5cd2 | 2012-07-13 01:19:08 +0000 | [diff] [blame] | 1137 | |
Dmitri Gribenko | 34df220 | 2012-07-31 22:37:06 +0000 | [diff] [blame] | 1138 | ActOnDocumentableDecl(NewTemplate); |
| 1139 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1140 | return NewTemplate; |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1141 | } |
| 1142 | |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1143 | /// \brief Diagnose the presence of a default template argument on a |
| 1144 | /// template parameter, which is ill-formed in certain contexts. |
| 1145 | /// |
| 1146 | /// \returns true if the default template argument should be dropped. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1147 | static bool DiagnoseDefaultTemplateArgument(Sema &S, |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1148 | Sema::TemplateParamListContext TPC, |
| 1149 | SourceLocation ParamLoc, |
| 1150 | SourceRange DefArgRange) { |
| 1151 | switch (TPC) { |
| 1152 | case Sema::TPC_ClassTemplate: |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1153 | case Sema::TPC_VarTemplate: |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 1154 | case Sema::TPC_TypeAliasTemplate: |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1155 | return false; |
| 1156 | |
| 1157 | case Sema::TPC_FunctionTemplate: |
Douglas Gregor | a99fb4c | 2011-02-04 04:20:44 +0000 | [diff] [blame] | 1158 | case Sema::TPC_FriendFunctionTemplateDefinition: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1159 | // C++ [temp.param]p9: |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1160 | // A default template-argument shall not be specified in a |
| 1161 | // function template declaration or a function template |
| 1162 | // definition [...] |
Douglas Gregor | a99fb4c | 2011-02-04 04:20:44 +0000 | [diff] [blame] | 1163 | // If a friend function template declaration specifies a default |
| 1164 | // template-argument, that declaration shall be a definition and shall be |
| 1165 | // the only declaration of the function template in the translation unit. |
| 1166 | // (C++98/03 doesn't have this wording; see DR226). |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 1167 | S.Diag(ParamLoc, S.getLangOpts().CPlusPlus11 ? |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 1168 | diag::warn_cxx98_compat_template_parameter_default_in_function_template |
| 1169 | : diag::ext_template_parameter_default_in_function_template) |
| 1170 | << DefArgRange; |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1171 | return false; |
| 1172 | |
| 1173 | case Sema::TPC_ClassTemplateMember: |
| 1174 | // C++0x [temp.param]p9: |
| 1175 | // A default template-argument shall not be specified in the |
| 1176 | // template-parameter-lists of the definition of a member of a |
| 1177 | // class template that appears outside of the member's class. |
| 1178 | S.Diag(ParamLoc, diag::err_template_parameter_default_template_member) |
| 1179 | << DefArgRange; |
| 1180 | return true; |
| 1181 | |
David Majnemer | ba8f17a | 2013-06-25 22:08:55 +0000 | [diff] [blame] | 1182 | case Sema::TPC_FriendClassTemplate: |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1183 | case Sema::TPC_FriendFunctionTemplate: |
| 1184 | // C++ [temp.param]p9: |
| 1185 | // A default template-argument shall not be specified in a |
| 1186 | // friend template declaration. |
| 1187 | S.Diag(ParamLoc, diag::err_template_parameter_default_friend_template) |
| 1188 | << DefArgRange; |
| 1189 | return true; |
| 1190 | |
| 1191 | // FIXME: C++0x [temp.param]p9 allows default template-arguments |
| 1192 | // for friend function templates if there is only a single |
| 1193 | // declaration (and it is a definition). Strange! |
| 1194 | } |
| 1195 | |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 1196 | llvm_unreachable("Invalid TemplateParamListContext!"); |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1197 | } |
| 1198 | |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1199 | /// \brief Check for unexpanded parameter packs within the template parameters |
| 1200 | /// of a template template parameter, recursively. |
Benjamin Kramer | 8aef596 | 2011-03-26 12:38:21 +0000 | [diff] [blame] | 1201 | static bool DiagnoseUnexpandedParameterPacks(Sema &S, |
| 1202 | TemplateTemplateParmDecl *TTP) { |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 1203 | // A template template parameter which is a parameter pack is also a pack |
| 1204 | // expansion. |
| 1205 | if (TTP->isParameterPack()) |
| 1206 | return false; |
| 1207 | |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1208 | TemplateParameterList *Params = TTP->getTemplateParameters(); |
| 1209 | for (unsigned I = 0, N = Params->size(); I != N; ++I) { |
| 1210 | NamedDecl *P = Params->getParam(I); |
| 1211 | if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(P)) { |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 1212 | if (!NTTP->isParameterPack() && |
| 1213 | S.DiagnoseUnexpandedParameterPack(NTTP->getLocation(), |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1214 | NTTP->getTypeSourceInfo(), |
| 1215 | Sema::UPPC_NonTypeTemplateParameterType)) |
| 1216 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1217 | |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1218 | continue; |
| 1219 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1220 | |
| 1221 | if (TemplateTemplateParmDecl *InnerTTP |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1222 | = dyn_cast<TemplateTemplateParmDecl>(P)) |
| 1223 | if (DiagnoseUnexpandedParameterPacks(S, InnerTTP)) |
| 1224 | return true; |
| 1225 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1226 | |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1227 | return false; |
| 1228 | } |
| 1229 | |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1230 | /// \brief Checks the validity of a template parameter list, possibly |
| 1231 | /// considering the template parameter list from a previous |
| 1232 | /// declaration. |
| 1233 | /// |
| 1234 | /// If an "old" template parameter list is provided, it must be |
| 1235 | /// equivalent (per TemplateParameterListsAreEqual) to the "new" |
| 1236 | /// template parameter list. |
| 1237 | /// |
| 1238 | /// \param NewParams Template parameter list for a new template |
| 1239 | /// declaration. This template parameter list will be updated with any |
| 1240 | /// default arguments that are carried through from the previous |
| 1241 | /// template parameter list. |
| 1242 | /// |
| 1243 | /// \param OldParams If provided, template parameter list from a |
| 1244 | /// previous declaration of the same template. Default template |
| 1245 | /// arguments will be merged from the old template parameter list to |
| 1246 | /// the new template parameter list. |
| 1247 | /// |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1248 | /// \param TPC Describes the context in which we are checking the given |
| 1249 | /// template parameter list. |
| 1250 | /// |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1251 | /// \returns true if an error occurred, false otherwise. |
| 1252 | bool Sema::CheckTemplateParameterList(TemplateParameterList *NewParams, |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1253 | TemplateParameterList *OldParams, |
| 1254 | TemplateParamListContext TPC) { |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1255 | bool Invalid = false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1256 | |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1257 | // C++ [temp.param]p10: |
| 1258 | // The set of default template-arguments available for use with a |
| 1259 | // template declaration or definition is obtained by merging the |
| 1260 | // default arguments from the definition (if in scope) and all |
| 1261 | // declarations in scope in the same way default function |
| 1262 | // arguments are (8.3.6). |
| 1263 | bool SawDefaultArgument = false; |
| 1264 | SourceLocation PreviousDefaultArgLoc; |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 1265 | |
Mike Stump | c89c8e3 | 2009-02-11 23:03:27 +0000 | [diff] [blame] | 1266 | // Dummy initialization to avoid warnings. |
Douglas Gregor | 5bd22da | 2009-02-11 20:46:19 +0000 | [diff] [blame] | 1267 | TemplateParameterList::iterator OldParam = NewParams->end(); |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1268 | if (OldParams) |
| 1269 | OldParam = OldParams->begin(); |
| 1270 | |
Douglas Gregor | 0693def | 2011-01-27 01:40:17 +0000 | [diff] [blame] | 1271 | bool RemoveDefaultArguments = false; |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1272 | for (TemplateParameterList::iterator NewParam = NewParams->begin(), |
| 1273 | NewParamEnd = NewParams->end(); |
| 1274 | NewParam != NewParamEnd; ++NewParam) { |
| 1275 | // Variables used to diagnose redundant default arguments |
| 1276 | bool RedundantDefaultArg = false; |
| 1277 | SourceLocation OldDefaultLoc; |
| 1278 | SourceLocation NewDefaultLoc; |
| 1279 | |
David Blaikie | 651c73c | 2011-10-19 05:19:50 +0000 | [diff] [blame] | 1280 | // Variable used to diagnose missing default arguments |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1281 | bool MissingDefaultArg = false; |
| 1282 | |
David Blaikie | 651c73c | 2011-10-19 05:19:50 +0000 | [diff] [blame] | 1283 | // Variable used to diagnose non-final parameter packs |
| 1284 | bool SawParameterPack = false; |
Anders Carlsson | 327865d | 2009-06-12 23:20:15 +0000 | [diff] [blame] | 1285 | |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1286 | if (TemplateTypeParmDecl *NewTypeParm |
| 1287 | = dyn_cast<TemplateTypeParmDecl>(*NewParam)) { |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1288 | // Check the presence of a default argument here. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1289 | if (NewTypeParm->hasDefaultArgument() && |
| 1290 | DiagnoseDefaultTemplateArgument(*this, TPC, |
| 1291 | NewTypeParm->getLocation(), |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1292 | NewTypeParm->getDefaultArgumentInfo()->getTypeLoc() |
Abramo Bagnara | 1108e7b | 2010-05-20 10:00:11 +0000 | [diff] [blame] | 1293 | .getSourceRange())) |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1294 | NewTypeParm->removeDefaultArgument(); |
| 1295 | |
| 1296 | // Merge default arguments for template type parameters. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1297 | TemplateTypeParmDecl *OldTypeParm |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1298 | = OldParams? cast<TemplateTypeParmDecl>(*OldParam) : nullptr; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1299 | |
Anders Carlsson | 327865d | 2009-06-12 23:20:15 +0000 | [diff] [blame] | 1300 | if (NewTypeParm->isParameterPack()) { |
| 1301 | assert(!NewTypeParm->hasDefaultArgument() && |
| 1302 | "Parameter packs can't have a default argument!"); |
| 1303 | SawParameterPack = true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1304 | } else if (OldTypeParm && OldTypeParm->hasDefaultArgument() && |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1305 | NewTypeParm->hasDefaultArgument()) { |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1306 | OldDefaultLoc = OldTypeParm->getDefaultArgumentLoc(); |
| 1307 | NewDefaultLoc = NewTypeParm->getDefaultArgumentLoc(); |
| 1308 | SawDefaultArgument = true; |
| 1309 | RedundantDefaultArg = true; |
| 1310 | PreviousDefaultArgLoc = NewDefaultLoc; |
| 1311 | } else if (OldTypeParm && OldTypeParm->hasDefaultArgument()) { |
| 1312 | // Merge the default argument from the old declaration to the |
| 1313 | // new declaration. |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1314 | NewTypeParm->setDefaultArgument(OldTypeParm->getDefaultArgumentInfo(), |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1315 | true); |
| 1316 | PreviousDefaultArgLoc = OldTypeParm->getDefaultArgumentLoc(); |
| 1317 | } else if (NewTypeParm->hasDefaultArgument()) { |
| 1318 | SawDefaultArgument = true; |
| 1319 | PreviousDefaultArgLoc = NewTypeParm->getDefaultArgumentLoc(); |
| 1320 | } else if (SawDefaultArgument) |
| 1321 | MissingDefaultArg = true; |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1322 | } else if (NonTypeTemplateParmDecl *NewNonTypeParm |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1323 | = dyn_cast<NonTypeTemplateParmDecl>(*NewParam)) { |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1324 | // Check for unexpanded parameter packs. |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 1325 | if (!NewNonTypeParm->isParameterPack() && |
| 1326 | DiagnoseUnexpandedParameterPack(NewNonTypeParm->getLocation(), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1327 | NewNonTypeParm->getTypeSourceInfo(), |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1328 | UPPC_NonTypeTemplateParameterType)) { |
| 1329 | Invalid = true; |
| 1330 | continue; |
| 1331 | } |
| 1332 | |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1333 | // Check the presence of a default argument here. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1334 | if (NewNonTypeParm->hasDefaultArgument() && |
| 1335 | DiagnoseDefaultTemplateArgument(*this, TPC, |
| 1336 | NewNonTypeParm->getLocation(), |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1337 | NewNonTypeParm->getDefaultArgument()->getSourceRange())) { |
Abramo Bagnara | 656e300 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 1338 | NewNonTypeParm->removeDefaultArgument(); |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1339 | } |
| 1340 | |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1341 | // Merge default arguments for non-type template parameters |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1342 | NonTypeTemplateParmDecl *OldNonTypeParm |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1343 | = OldParams? cast<NonTypeTemplateParmDecl>(*OldParam) : nullptr; |
Douglas Gregor | 0018cdc | 2011-01-05 16:19:19 +0000 | [diff] [blame] | 1344 | if (NewNonTypeParm->isParameterPack()) { |
| 1345 | assert(!NewNonTypeParm->hasDefaultArgument() && |
| 1346 | "Parameter packs can't have a default argument!"); |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 1347 | if (!NewNonTypeParm->isPackExpansion()) |
| 1348 | SawParameterPack = true; |
Douglas Gregor | 0018cdc | 2011-01-05 16:19:19 +0000 | [diff] [blame] | 1349 | } else if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument() && |
Richard Smith | 35828f1 | 2013-07-22 03:31:14 +0000 | [diff] [blame] | 1350 | NewNonTypeParm->hasDefaultArgument()) { |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1351 | OldDefaultLoc = OldNonTypeParm->getDefaultArgumentLoc(); |
| 1352 | NewDefaultLoc = NewNonTypeParm->getDefaultArgumentLoc(); |
| 1353 | SawDefaultArgument = true; |
| 1354 | RedundantDefaultArg = true; |
| 1355 | PreviousDefaultArgLoc = NewDefaultLoc; |
| 1356 | } else if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument()) { |
| 1357 | // Merge the default argument from the old declaration to the |
| 1358 | // new declaration. |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1359 | // FIXME: We need to create a new kind of "default argument" |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 1360 | // expression that points to a previous non-type template |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1361 | // parameter. |
| 1362 | NewNonTypeParm->setDefaultArgument( |
Abramo Bagnara | 656e300 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 1363 | OldNonTypeParm->getDefaultArgument(), |
| 1364 | /*Inherited=*/ true); |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1365 | PreviousDefaultArgLoc = OldNonTypeParm->getDefaultArgumentLoc(); |
| 1366 | } else if (NewNonTypeParm->hasDefaultArgument()) { |
| 1367 | SawDefaultArgument = true; |
| 1368 | PreviousDefaultArgLoc = NewNonTypeParm->getDefaultArgumentLoc(); |
| 1369 | } else if (SawDefaultArgument) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1370 | MissingDefaultArg = true; |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1371 | } else { |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1372 | TemplateTemplateParmDecl *NewTemplateParm |
| 1373 | = cast<TemplateTemplateParmDecl>(*NewParam); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1374 | |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1375 | // Check for unexpanded parameter packs, recursively. |
Douglas Gregor | 4a2a8f7 | 2011-10-25 03:44:56 +0000 | [diff] [blame] | 1376 | if (::DiagnoseUnexpandedParameterPacks(*this, NewTemplateParm)) { |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1377 | Invalid = true; |
| 1378 | continue; |
| 1379 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1380 | |
David Blaikie | 651c73c | 2011-10-19 05:19:50 +0000 | [diff] [blame] | 1381 | // Check the presence of a default argument here. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1382 | if (NewTemplateParm->hasDefaultArgument() && |
| 1383 | DiagnoseDefaultTemplateArgument(*this, TPC, |
| 1384 | NewTemplateParm->getLocation(), |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1385 | NewTemplateParm->getDefaultArgument().getSourceRange())) |
Abramo Bagnara | 656e300 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 1386 | NewTemplateParm->removeDefaultArgument(); |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1387 | |
| 1388 | // Merge default arguments for template template parameters |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1389 | TemplateTemplateParmDecl *OldTemplateParm |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1390 | = OldParams? cast<TemplateTemplateParmDecl>(*OldParam) : nullptr; |
Douglas Gregor | 0018cdc | 2011-01-05 16:19:19 +0000 | [diff] [blame] | 1391 | if (NewTemplateParm->isParameterPack()) { |
| 1392 | assert(!NewTemplateParm->hasDefaultArgument() && |
| 1393 | "Parameter packs can't have a default argument!"); |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 1394 | if (!NewTemplateParm->isPackExpansion()) |
| 1395 | SawParameterPack = true; |
Douglas Gregor | 0018cdc | 2011-01-05 16:19:19 +0000 | [diff] [blame] | 1396 | } else if (OldTemplateParm && OldTemplateParm->hasDefaultArgument() && |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1397 | NewTemplateParm->hasDefaultArgument()) { |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1398 | OldDefaultLoc = OldTemplateParm->getDefaultArgument().getLocation(); |
| 1399 | NewDefaultLoc = NewTemplateParm->getDefaultArgument().getLocation(); |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1400 | SawDefaultArgument = true; |
| 1401 | RedundantDefaultArg = true; |
| 1402 | PreviousDefaultArgLoc = NewDefaultLoc; |
| 1403 | } else if (OldTemplateParm && OldTemplateParm->hasDefaultArgument()) { |
| 1404 | // Merge the default argument from the old declaration to the |
| 1405 | // new declaration. |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 1406 | // FIXME: We need to create a new kind of "default argument" expression |
| 1407 | // that points to a previous template template parameter. |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1408 | NewTemplateParm->setDefaultArgument( |
Abramo Bagnara | 656e300 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 1409 | OldTemplateParm->getDefaultArgument(), |
| 1410 | /*Inherited=*/ true); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1411 | PreviousDefaultArgLoc |
| 1412 | = OldTemplateParm->getDefaultArgument().getLocation(); |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1413 | } else if (NewTemplateParm->hasDefaultArgument()) { |
| 1414 | SawDefaultArgument = true; |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1415 | PreviousDefaultArgLoc |
| 1416 | = NewTemplateParm->getDefaultArgument().getLocation(); |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1417 | } else if (SawDefaultArgument) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1418 | MissingDefaultArg = true; |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1419 | } |
| 1420 | |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 1421 | // C++11 [temp.param]p11: |
David Blaikie | 651c73c | 2011-10-19 05:19:50 +0000 | [diff] [blame] | 1422 | // If a template parameter of a primary class template or alias template |
| 1423 | // is a template parameter pack, it shall be the last template parameter. |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 1424 | if (SawParameterPack && (NewParam + 1) != NewParamEnd && |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1425 | (TPC == TPC_ClassTemplate || TPC == TPC_VarTemplate || |
| 1426 | TPC == TPC_TypeAliasTemplate)) { |
David Blaikie | 651c73c | 2011-10-19 05:19:50 +0000 | [diff] [blame] | 1427 | Diag((*NewParam)->getLocation(), |
| 1428 | diag::err_template_param_pack_must_be_last_template_parameter); |
| 1429 | Invalid = true; |
| 1430 | } |
| 1431 | |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1432 | if (RedundantDefaultArg) { |
| 1433 | // C++ [temp.param]p12: |
| 1434 | // A template-parameter shall not be given default arguments |
| 1435 | // by two different declarations in the same scope. |
| 1436 | Diag(NewDefaultLoc, diag::err_template_param_default_arg_redefinition); |
| 1437 | Diag(OldDefaultLoc, diag::note_template_param_prev_default_arg); |
| 1438 | Invalid = true; |
Douglas Gregor | 8b481d8 | 2011-02-04 03:57:22 +0000 | [diff] [blame] | 1439 | } else if (MissingDefaultArg && TPC != TPC_FunctionTemplate) { |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1440 | // C++ [temp.param]p11: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1441 | // If a template-parameter of a class template has a default |
| 1442 | // template-argument, each subsequent template-parameter shall either |
Douglas Gregor | 7dba51f | 2011-01-05 16:21:17 +0000 | [diff] [blame] | 1443 | // have a default template-argument supplied or be a template parameter |
| 1444 | // pack. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1445 | Diag((*NewParam)->getLocation(), |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1446 | diag::err_template_param_default_arg_missing); |
| 1447 | Diag(PreviousDefaultArgLoc, diag::note_template_param_prev_default_arg); |
| 1448 | Invalid = true; |
Douglas Gregor | 0693def | 2011-01-27 01:40:17 +0000 | [diff] [blame] | 1449 | RemoveDefaultArguments = true; |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1450 | } |
| 1451 | |
| 1452 | // If we have an old template parameter list that we're merging |
| 1453 | // in, move on to the next parameter. |
| 1454 | if (OldParams) |
| 1455 | ++OldParam; |
| 1456 | } |
| 1457 | |
Douglas Gregor | 0693def | 2011-01-27 01:40:17 +0000 | [diff] [blame] | 1458 | // We were missing some default arguments at the end of the list, so remove |
| 1459 | // all of the default arguments. |
| 1460 | if (RemoveDefaultArguments) { |
| 1461 | for (TemplateParameterList::iterator NewParam = NewParams->begin(), |
| 1462 | NewParamEnd = NewParams->end(); |
| 1463 | NewParam != NewParamEnd; ++NewParam) { |
| 1464 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*NewParam)) |
| 1465 | TTP->removeDefaultArgument(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1466 | else if (NonTypeTemplateParmDecl *NTTP |
Douglas Gregor | 0693def | 2011-01-27 01:40:17 +0000 | [diff] [blame] | 1467 | = dyn_cast<NonTypeTemplateParmDecl>(*NewParam)) |
| 1468 | NTTP->removeDefaultArgument(); |
| 1469 | else |
| 1470 | cast<TemplateTemplateParmDecl>(*NewParam)->removeDefaultArgument(); |
| 1471 | } |
| 1472 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1473 | |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1474 | return Invalid; |
| 1475 | } |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 1476 | |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1477 | namespace { |
| 1478 | |
| 1479 | /// A class which looks for a use of a certain level of template |
| 1480 | /// parameter. |
| 1481 | struct DependencyChecker : RecursiveASTVisitor<DependencyChecker> { |
| 1482 | typedef RecursiveASTVisitor<DependencyChecker> super; |
| 1483 | |
| 1484 | unsigned Depth; |
| 1485 | bool Match; |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 1486 | SourceLocation MatchLoc; |
| 1487 | |
| 1488 | DependencyChecker(unsigned Depth) : Depth(Depth), Match(false) {} |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1489 | |
| 1490 | DependencyChecker(TemplateParameterList *Params) : Match(false) { |
| 1491 | NamedDecl *ND = Params->getParam(0); |
| 1492 | if (TemplateTypeParmDecl *PD = dyn_cast<TemplateTypeParmDecl>(ND)) { |
| 1493 | Depth = PD->getDepth(); |
| 1494 | } else if (NonTypeTemplateParmDecl *PD = |
| 1495 | dyn_cast<NonTypeTemplateParmDecl>(ND)) { |
| 1496 | Depth = PD->getDepth(); |
| 1497 | } else { |
| 1498 | Depth = cast<TemplateTemplateParmDecl>(ND)->getDepth(); |
| 1499 | } |
| 1500 | } |
| 1501 | |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 1502 | bool Matches(unsigned ParmDepth, SourceLocation Loc = SourceLocation()) { |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1503 | if (ParmDepth >= Depth) { |
| 1504 | Match = true; |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 1505 | MatchLoc = Loc; |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1506 | return true; |
| 1507 | } |
| 1508 | return false; |
| 1509 | } |
| 1510 | |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 1511 | bool VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) { |
| 1512 | return !Matches(TL.getTypePtr()->getDepth(), TL.getNameLoc()); |
| 1513 | } |
| 1514 | |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1515 | bool VisitTemplateTypeParmType(const TemplateTypeParmType *T) { |
| 1516 | return !Matches(T->getDepth()); |
| 1517 | } |
| 1518 | |
| 1519 | bool TraverseTemplateName(TemplateName N) { |
| 1520 | if (TemplateTemplateParmDecl *PD = |
| 1521 | dyn_cast_or_null<TemplateTemplateParmDecl>(N.getAsTemplateDecl())) |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 1522 | if (Matches(PD->getDepth())) |
| 1523 | return false; |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1524 | return super::TraverseTemplateName(N); |
| 1525 | } |
| 1526 | |
| 1527 | bool VisitDeclRefExpr(DeclRefExpr *E) { |
| 1528 | if (NonTypeTemplateParmDecl *PD = |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 1529 | dyn_cast<NonTypeTemplateParmDecl>(E->getDecl())) |
| 1530 | if (Matches(PD->getDepth(), E->getExprLoc())) |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1531 | return false; |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1532 | return super::VisitDeclRefExpr(E); |
| 1533 | } |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 1534 | |
| 1535 | bool VisitSubstTemplateTypeParmType(const SubstTemplateTypeParmType *T) { |
| 1536 | return TraverseType(T->getReplacementType()); |
| 1537 | } |
| 1538 | |
| 1539 | bool |
| 1540 | VisitSubstTemplateTypeParmPackType(const SubstTemplateTypeParmPackType *T) { |
| 1541 | return TraverseTemplateArgument(T->getArgumentPack()); |
| 1542 | } |
| 1543 | |
Douglas Gregor | a6a7e3c | 2011-05-13 00:34:01 +0000 | [diff] [blame] | 1544 | bool TraverseInjectedClassNameType(const InjectedClassNameType *T) { |
| 1545 | return TraverseType(T->getInjectedSpecializationType()); |
| 1546 | } |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1547 | }; |
| 1548 | } |
| 1549 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1550 | /// Determines whether a given type depends on the given parameter |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1551 | /// list. |
| 1552 | static bool |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1553 | DependsOnTemplateParameters(QualType T, TemplateParameterList *Params) { |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1554 | DependencyChecker Checker(Params); |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1555 | Checker.TraverseType(T); |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1556 | return Checker.Match; |
| 1557 | } |
| 1558 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1559 | // Find the source range corresponding to the named type in the given |
| 1560 | // nested-name-specifier, if any. |
| 1561 | static SourceRange getRangeOfTypeInNestedNameSpecifier(ASTContext &Context, |
| 1562 | QualType T, |
| 1563 | const CXXScopeSpec &SS) { |
| 1564 | NestedNameSpecifierLoc NNSLoc(SS.getScopeRep(), SS.location_data()); |
| 1565 | while (NestedNameSpecifier *NNS = NNSLoc.getNestedNameSpecifier()) { |
| 1566 | if (const Type *CurType = NNS->getAsType()) { |
| 1567 | if (Context.hasSameUnqualifiedType(T, QualType(CurType, 0))) |
| 1568 | return NNSLoc.getTypeLoc().getSourceRange(); |
| 1569 | } else |
| 1570 | break; |
| 1571 | |
| 1572 | NNSLoc = NNSLoc.getPrefix(); |
| 1573 | } |
| 1574 | |
| 1575 | return SourceRange(); |
| 1576 | } |
| 1577 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1578 | /// \brief Match the given template parameter lists to the given scope |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1579 | /// specifier, returning the template parameter list that applies to the |
| 1580 | /// name. |
| 1581 | /// |
| 1582 | /// \param DeclStartLoc the start of the declaration that has a scope |
| 1583 | /// specifier or a template parameter list. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1584 | /// |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1585 | /// \param DeclLoc The location of the declaration itself. |
| 1586 | /// |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1587 | /// \param SS the scope specifier that will be matched to the given template |
| 1588 | /// parameter lists. This scope specifier precedes a qualified name that is |
| 1589 | /// being declared. |
| 1590 | /// |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 1591 | /// \param TemplateId The template-id following the scope specifier, if there |
| 1592 | /// is one. Used to check for a missing 'template<>'. |
| 1593 | /// |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1594 | /// \param ParamLists the template parameter lists, from the outermost to the |
| 1595 | /// innermost template parameter lists. |
| 1596 | /// |
John McCall | e820e5e | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 1597 | /// \param IsFriend Whether to apply the slightly different rules for |
| 1598 | /// matching template parameters to scope specifiers in friend |
| 1599 | /// declarations. |
| 1600 | /// |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 1601 | /// \param IsExplicitSpecialization will be set true if the entity being |
| 1602 | /// declared is an explicit specialization, false otherwise. |
| 1603 | /// |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1604 | /// \returns the template parameter list, if any, that corresponds to the |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1605 | /// name that is preceded by the scope specifier @p SS. This template |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 1606 | /// parameter list may have template parameters (if we're declaring a |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1607 | /// template) or may have no template parameters (if we're declaring a |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 1608 | /// template specialization), or may be NULL (if what we're declaring isn't |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1609 | /// itself a template). |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 1610 | TemplateParameterList *Sema::MatchTemplateParametersToScopeSpecifier( |
| 1611 | SourceLocation DeclStartLoc, SourceLocation DeclLoc, const CXXScopeSpec &SS, |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 1612 | TemplateIdAnnotation *TemplateId, |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 1613 | ArrayRef<TemplateParameterList *> ParamLists, bool IsFriend, |
| 1614 | bool &IsExplicitSpecialization, bool &Invalid) { |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 1615 | IsExplicitSpecialization = false; |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1616 | Invalid = false; |
| 1617 | |
| 1618 | // The sequence of nested types to which we will match up the template |
| 1619 | // parameter lists. We first build this list by starting with the type named |
| 1620 | // by the nested-name-specifier and walking out until we run out of types. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1621 | SmallVector<QualType, 4> NestedTypes; |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1622 | QualType T; |
Douglas Gregor | 9d07dfa | 2011-05-15 17:27:27 +0000 | [diff] [blame] | 1623 | if (SS.getScopeRep()) { |
| 1624 | if (CXXRecordDecl *Record |
| 1625 | = dyn_cast_or_null<CXXRecordDecl>(computeDeclContext(SS, true))) |
| 1626 | T = Context.getTypeDeclType(Record); |
| 1627 | else |
| 1628 | T = QualType(SS.getScopeRep()->getAsType(), 0); |
| 1629 | } |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1630 | |
| 1631 | // If we found an explicit specialization that prevents us from needing |
| 1632 | // 'template<>' headers, this will be set to the location of that |
| 1633 | // explicit specialization. |
| 1634 | SourceLocation ExplicitSpecLoc; |
| 1635 | |
| 1636 | while (!T.isNull()) { |
| 1637 | NestedTypes.push_back(T); |
| 1638 | |
| 1639 | // Retrieve the parent of a record type. |
| 1640 | if (CXXRecordDecl *Record = T->getAsCXXRecordDecl()) { |
| 1641 | // If this type is an explicit specialization, we're done. |
| 1642 | if (ClassTemplateSpecializationDecl *Spec |
| 1643 | = dyn_cast<ClassTemplateSpecializationDecl>(Record)) { |
| 1644 | if (!isa<ClassTemplatePartialSpecializationDecl>(Spec) && |
| 1645 | Spec->getSpecializationKind() == TSK_ExplicitSpecialization) { |
| 1646 | ExplicitSpecLoc = Spec->getLocation(); |
| 1647 | break; |
Douglas Gregor | 6591149 | 2009-11-23 12:11:45 +0000 | [diff] [blame] | 1648 | } |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1649 | } else if (Record->getTemplateSpecializationKind() |
| 1650 | == TSK_ExplicitSpecialization) { |
| 1651 | ExplicitSpecLoc = Record->getLocation(); |
John McCall | e820e5e | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 1652 | break; |
| 1653 | } |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1654 | |
| 1655 | if (TypeDecl *Parent = dyn_cast<TypeDecl>(Record->getParent())) |
| 1656 | T = Context.getTypeDeclType(Parent); |
| 1657 | else |
| 1658 | T = QualType(); |
| 1659 | continue; |
| 1660 | } |
| 1661 | |
| 1662 | if (const TemplateSpecializationType *TST |
| 1663 | = T->getAs<TemplateSpecializationType>()) { |
| 1664 | if (TemplateDecl *Template = TST->getTemplateName().getAsTemplateDecl()) { |
| 1665 | if (TypeDecl *Parent = dyn_cast<TypeDecl>(Template->getDeclContext())) |
| 1666 | T = Context.getTypeDeclType(Parent); |
| 1667 | else |
| 1668 | T = QualType(); |
| 1669 | continue; |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1670 | } |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1671 | } |
| 1672 | |
| 1673 | // Look one step prior in a dependent template specialization type. |
| 1674 | if (const DependentTemplateSpecializationType *DependentTST |
| 1675 | = T->getAs<DependentTemplateSpecializationType>()) { |
| 1676 | if (NestedNameSpecifier *NNS = DependentTST->getQualifier()) |
| 1677 | T = QualType(NNS->getAsType(), 0); |
| 1678 | else |
| 1679 | T = QualType(); |
| 1680 | continue; |
| 1681 | } |
| 1682 | |
| 1683 | // Look one step prior in a dependent name type. |
| 1684 | if (const DependentNameType *DependentName = T->getAs<DependentNameType>()){ |
| 1685 | if (NestedNameSpecifier *NNS = DependentName->getQualifier()) |
| 1686 | T = QualType(NNS->getAsType(), 0); |
| 1687 | else |
| 1688 | T = QualType(); |
| 1689 | continue; |
| 1690 | } |
| 1691 | |
| 1692 | // Retrieve the parent of an enumeration type. |
| 1693 | if (const EnumType *EnumT = T->getAs<EnumType>()) { |
| 1694 | // FIXME: Forward-declared enums require a TSK_ExplicitSpecialization |
| 1695 | // check here. |
| 1696 | EnumDecl *Enum = EnumT->getDecl(); |
| 1697 | |
| 1698 | // Get to the parent type. |
| 1699 | if (TypeDecl *Parent = dyn_cast<TypeDecl>(Enum->getParent())) |
| 1700 | T = Context.getTypeDeclType(Parent); |
| 1701 | else |
| 1702 | T = QualType(); |
| 1703 | continue; |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1704 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1705 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1706 | T = QualType(); |
| 1707 | } |
| 1708 | // Reverse the nested types list, since we want to traverse from the outermost |
| 1709 | // to the innermost while checking template-parameter-lists. |
| 1710 | std::reverse(NestedTypes.begin(), NestedTypes.end()); |
Douglas Gregor | 1530138 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 1711 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1712 | // C++0x [temp.expl.spec]p17: |
| 1713 | // A member or a member template may be nested within many |
| 1714 | // enclosing class templates. In an explicit specialization for |
| 1715 | // such a member, the member declaration shall be preceded by a |
| 1716 | // template<> for each enclosing class template that is |
| 1717 | // explicitly specialized. |
Douglas Gregor | 522d5eb | 2011-06-06 15:22:55 +0000 | [diff] [blame] | 1718 | bool SawNonEmptyTemplateParameterList = false; |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 1719 | |
NAKAMURA Takumi | de4077a | 2014-04-17 08:57:09 +0000 | [diff] [blame] | 1720 | auto CheckExplicitSpecialization = [&](SourceRange Range, bool Recovery) { |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 1721 | if (SawNonEmptyTemplateParameterList) { |
| 1722 | Diag(DeclLoc, diag::err_specialize_member_of_template) |
| 1723 | << !Recovery << Range; |
| 1724 | Invalid = true; |
| 1725 | IsExplicitSpecialization = false; |
| 1726 | return true; |
| 1727 | } |
| 1728 | |
| 1729 | return false; |
| 1730 | }; |
| 1731 | |
| 1732 | auto DiagnoseMissingExplicitSpecialization = [&] (SourceRange Range) { |
| 1733 | // Check that we can have an explicit specialization here. |
| 1734 | if (CheckExplicitSpecialization(Range, true)) |
| 1735 | return true; |
| 1736 | |
| 1737 | // We don't have a template header, but we should. |
| 1738 | SourceLocation ExpectedTemplateLoc; |
| 1739 | if (!ParamLists.empty()) |
| 1740 | ExpectedTemplateLoc = ParamLists[0]->getTemplateLoc(); |
| 1741 | else |
| 1742 | ExpectedTemplateLoc = DeclStartLoc; |
| 1743 | |
| 1744 | Diag(DeclLoc, diag::err_template_spec_needs_header) |
| 1745 | << Range |
| 1746 | << FixItHint::CreateInsertion(ExpectedTemplateLoc, "template<> "); |
| 1747 | return false; |
| 1748 | }; |
| 1749 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1750 | unsigned ParamIdx = 0; |
| 1751 | for (unsigned TypeIdx = 0, NumTypes = NestedTypes.size(); TypeIdx != NumTypes; |
| 1752 | ++TypeIdx) { |
| 1753 | T = NestedTypes[TypeIdx]; |
| 1754 | |
| 1755 | // Whether we expect a 'template<>' header. |
| 1756 | bool NeedEmptyTemplateHeader = false; |
| 1757 | |
| 1758 | // Whether we expect a template header with parameters. |
| 1759 | bool NeedNonemptyTemplateHeader = false; |
| 1760 | |
| 1761 | // For a dependent type, the set of template parameters that we |
| 1762 | // expect to see. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1763 | TemplateParameterList *ExpectedTemplateParams = nullptr; |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1764 | |
Douglas Gregor | 373af9b | 2011-05-11 23:26:17 +0000 | [diff] [blame] | 1765 | // C++0x [temp.expl.spec]p15: |
| 1766 | // A member or a member template may be nested within many enclosing |
| 1767 | // class templates. In an explicit specialization for such a member, the |
| 1768 | // member declaration shall be preceded by a template<> for each |
| 1769 | // enclosing class template that is explicitly specialized. |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1770 | if (CXXRecordDecl *Record = T->getAsCXXRecordDecl()) { |
| 1771 | if (ClassTemplatePartialSpecializationDecl *Partial |
| 1772 | = dyn_cast<ClassTemplatePartialSpecializationDecl>(Record)) { |
| 1773 | ExpectedTemplateParams = Partial->getTemplateParameters(); |
| 1774 | NeedNonemptyTemplateHeader = true; |
| 1775 | } else if (Record->isDependentType()) { |
| 1776 | if (Record->getDescribedClassTemplate()) { |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 1777 | ExpectedTemplateParams = Record->getDescribedClassTemplate() |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1778 | ->getTemplateParameters(); |
| 1779 | NeedNonemptyTemplateHeader = true; |
| 1780 | } |
| 1781 | } else if (ClassTemplateSpecializationDecl *Spec |
| 1782 | = dyn_cast<ClassTemplateSpecializationDecl>(Record)) { |
| 1783 | // C++0x [temp.expl.spec]p4: |
| 1784 | // Members of an explicitly specialized class template are defined |
| 1785 | // in the same manner as members of normal classes, and not using |
| 1786 | // the template<> syntax. |
| 1787 | if (Spec->getSpecializationKind() != TSK_ExplicitSpecialization) |
| 1788 | NeedEmptyTemplateHeader = true; |
| 1789 | else |
Douglas Gregor | b32e825 | 2011-06-01 22:37:07 +0000 | [diff] [blame] | 1790 | continue; |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1791 | } else if (Record->getTemplateSpecializationKind()) { |
| 1792 | if (Record->getTemplateSpecializationKind() |
Douglas Gregor | 373af9b | 2011-05-11 23:26:17 +0000 | [diff] [blame] | 1793 | != TSK_ExplicitSpecialization && |
| 1794 | TypeIdx == NumTypes - 1) |
| 1795 | IsExplicitSpecialization = true; |
| 1796 | |
| 1797 | continue; |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1798 | } |
| 1799 | } else if (const TemplateSpecializationType *TST |
| 1800 | = T->getAs<TemplateSpecializationType>()) { |
Nico Weber | 2890061 | 2015-01-30 02:35:21 +0000 | [diff] [blame] | 1801 | if (TemplateDecl *Template = TST->getTemplateName().getAsTemplateDecl()) { |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1802 | ExpectedTemplateParams = Template->getTemplateParameters(); |
| 1803 | NeedNonemptyTemplateHeader = true; |
| 1804 | } |
| 1805 | } else if (T->getAs<DependentTemplateSpecializationType>()) { |
| 1806 | // FIXME: We actually could/should check the template arguments here |
| 1807 | // against the corresponding template parameter list. |
| 1808 | NeedNonemptyTemplateHeader = false; |
| 1809 | } |
| 1810 | |
Douglas Gregor | 522d5eb | 2011-06-06 15:22:55 +0000 | [diff] [blame] | 1811 | // C++ [temp.expl.spec]p16: |
| 1812 | // In an explicit specialization declaration for a member of a class |
| 1813 | // template or a member template that ap- pears in namespace scope, the |
| 1814 | // member template and some of its enclosing class templates may remain |
| 1815 | // unspecialized, except that the declaration shall not explicitly |
| 1816 | // specialize a class member template if its en- closing class templates |
| 1817 | // are not explicitly specialized as well. |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 1818 | if (ParamIdx < ParamLists.size()) { |
Douglas Gregor | 522d5eb | 2011-06-06 15:22:55 +0000 | [diff] [blame] | 1819 | if (ParamLists[ParamIdx]->size() == 0) { |
NAKAMURA Takumi | de4077a | 2014-04-17 08:57:09 +0000 | [diff] [blame] | 1820 | if (CheckExplicitSpecialization(ParamLists[ParamIdx]->getSourceRange(), |
| 1821 | false)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1822 | return nullptr; |
Douglas Gregor | 522d5eb | 2011-06-06 15:22:55 +0000 | [diff] [blame] | 1823 | } else |
| 1824 | SawNonEmptyTemplateParameterList = true; |
| 1825 | } |
| 1826 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1827 | if (NeedEmptyTemplateHeader) { |
| 1828 | // If we're on the last of the types, and we need a 'template<>' header |
| 1829 | // here, then it's an explicit specialization. |
| 1830 | if (TypeIdx == NumTypes - 1) |
| 1831 | IsExplicitSpecialization = true; |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 1832 | |
| 1833 | if (ParamIdx < ParamLists.size()) { |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1834 | if (ParamLists[ParamIdx]->size() > 0) { |
| 1835 | // The header has template parameters when it shouldn't. Complain. |
| 1836 | Diag(ParamLists[ParamIdx]->getTemplateLoc(), |
| 1837 | diag::err_template_param_list_matches_nontemplate) |
| 1838 | << T |
| 1839 | << SourceRange(ParamLists[ParamIdx]->getLAngleLoc(), |
| 1840 | ParamLists[ParamIdx]->getRAngleLoc()) |
| 1841 | << getRangeOfTypeInNestedNameSpecifier(Context, T, SS); |
| 1842 | Invalid = true; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1843 | return nullptr; |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1844 | } |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 1845 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1846 | // Consume this template header. |
| 1847 | ++ParamIdx; |
| 1848 | continue; |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1849 | } |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 1850 | |
| 1851 | if (!IsFriend) |
| 1852 | if (DiagnoseMissingExplicitSpecialization( |
| 1853 | getRangeOfTypeInNestedNameSpecifier(Context, T, SS))) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1854 | return nullptr; |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 1855 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1856 | continue; |
| 1857 | } |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 1858 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1859 | if (NeedNonemptyTemplateHeader) { |
| 1860 | // In friend declarations we can have template-ids which don't |
| 1861 | // depend on the corresponding template parameter lists. But |
| 1862 | // assume that empty parameter lists are supposed to match this |
| 1863 | // template-id. |
| 1864 | if (IsFriend && T->isDependentType()) { |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 1865 | if (ParamIdx < ParamLists.size() && |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1866 | DependsOnTemplateParameters(T, ParamLists[ParamIdx])) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1867 | ExpectedTemplateParams = nullptr; |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1868 | else |
| 1869 | continue; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1870 | } |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1871 | |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 1872 | if (ParamIdx < ParamLists.size()) { |
| 1873 | // Check the template parameter list, if we can. |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1874 | if (ExpectedTemplateParams && |
| 1875 | !TemplateParameterListsAreEqual(ParamLists[ParamIdx], |
| 1876 | ExpectedTemplateParams, |
| 1877 | true, TPL_TemplateMatch)) |
| 1878 | Invalid = true; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1879 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1880 | if (!Invalid && |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1881 | CheckTemplateParameterList(ParamLists[ParamIdx], nullptr, |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1882 | TPC_ClassTemplateMember)) |
| 1883 | Invalid = true; |
| 1884 | |
| 1885 | ++ParamIdx; |
| 1886 | continue; |
| 1887 | } |
| 1888 | |
| 1889 | Diag(DeclLoc, diag::err_template_spec_needs_template_parameters) |
| 1890 | << T |
| 1891 | << getRangeOfTypeInNestedNameSpecifier(Context, T, SS); |
| 1892 | Invalid = true; |
| 1893 | continue; |
| 1894 | } |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1895 | } |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 1896 | |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1897 | // If there were at least as many template-ids as there were template |
| 1898 | // parameter lists, then there are no template parameter lists remaining for |
| 1899 | // the declaration itself. |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 1900 | if (ParamIdx >= ParamLists.size()) { |
| 1901 | if (TemplateId && !IsFriend) { |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 1902 | // We don't have a template header for the declaration itself, but we |
| 1903 | // should. |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 1904 | IsExplicitSpecialization = true; |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 1905 | DiagnoseMissingExplicitSpecialization(SourceRange(TemplateId->LAngleLoc, |
| 1906 | TemplateId->RAngleLoc)); |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 1907 | |
| 1908 | // Fabricate an empty template parameter list for the invented header. |
| 1909 | return TemplateParameterList::Create(Context, SourceLocation(), |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1910 | SourceLocation(), nullptr, 0, |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 1911 | SourceLocation()); |
| 1912 | } |
| 1913 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1914 | return nullptr; |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 1915 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1916 | |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1917 | // If there were too many template parameter lists, complain about that now. |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 1918 | if (ParamIdx < ParamLists.size() - 1) { |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1919 | bool HasAnyExplicitSpecHeader = false; |
| 1920 | bool AllExplicitSpecHeaders = true; |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 1921 | for (unsigned I = ParamIdx, E = ParamLists.size() - 1; I != E; ++I) { |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1922 | if (ParamLists[I]->size() == 0) |
| 1923 | HasAnyExplicitSpecHeader = true; |
| 1924 | else |
| 1925 | AllExplicitSpecHeaders = false; |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1926 | } |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 1927 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1928 | Diag(ParamLists[ParamIdx]->getTemplateLoc(), |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 1929 | AllExplicitSpecHeaders ? diag::warn_template_spec_extra_headers |
| 1930 | : diag::err_template_spec_extra_headers) |
| 1931 | << SourceRange(ParamLists[ParamIdx]->getTemplateLoc(), |
| 1932 | ParamLists[ParamLists.size() - 2]->getRAngleLoc()); |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1933 | |
| 1934 | // If there was a specialization somewhere, such that 'template<>' is |
| 1935 | // not required, and there were any 'template<>' headers, note where the |
| 1936 | // specialization occurred. |
| 1937 | if (ExplicitSpecLoc.isValid() && HasAnyExplicitSpecHeader) |
| 1938 | Diag(ExplicitSpecLoc, |
| 1939 | diag::note_explicit_template_spec_does_not_need_header) |
| 1940 | << NestedTypes.back(); |
| 1941 | |
| 1942 | // We have a template parameter list with no corresponding scope, which |
| 1943 | // means that the resulting template declaration can't be instantiated |
| 1944 | // properly (we'll end up with dependent nodes when we shouldn't). |
| 1945 | if (!AllExplicitSpecHeaders) |
| 1946 | Invalid = true; |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1947 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1948 | |
Douglas Gregor | 522d5eb | 2011-06-06 15:22:55 +0000 | [diff] [blame] | 1949 | // C++ [temp.expl.spec]p16: |
| 1950 | // In an explicit specialization declaration for a member of a class |
| 1951 | // template or a member template that ap- pears in namespace scope, the |
| 1952 | // member template and some of its enclosing class templates may remain |
| 1953 | // unspecialized, except that the declaration shall not explicitly |
| 1954 | // specialize a class member template if its en- closing class templates |
| 1955 | // are not explicitly specialized as well. |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 1956 | if (ParamLists.back()->size() == 0 && |
NAKAMURA Takumi | de4077a | 2014-04-17 08:57:09 +0000 | [diff] [blame] | 1957 | CheckExplicitSpecialization(ParamLists[ParamIdx]->getSourceRange(), |
| 1958 | false)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1959 | return nullptr; |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 1960 | |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1961 | // Return the last template parameter list, which corresponds to the |
| 1962 | // entity being declared. |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 1963 | return ParamLists.back(); |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1964 | } |
| 1965 | |
Douglas Gregor | 8b6070b | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 1966 | void Sema::NoteAllFoundTemplates(TemplateName Name) { |
| 1967 | if (TemplateDecl *Template = Name.getAsTemplateDecl()) { |
| 1968 | Diag(Template->getLocation(), diag::note_template_declared_here) |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1969 | << (isa<FunctionTemplateDecl>(Template) |
| 1970 | ? 0 |
| 1971 | : isa<ClassTemplateDecl>(Template) |
| 1972 | ? 1 |
| 1973 | : isa<VarTemplateDecl>(Template) |
| 1974 | ? 2 |
| 1975 | : isa<TypeAliasTemplateDecl>(Template) ? 3 : 4) |
| 1976 | << Template->getDeclName(); |
Douglas Gregor | 8b6070b | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 1977 | return; |
| 1978 | } |
| 1979 | |
| 1980 | if (OverloadedTemplateStorage *OST = Name.getAsOverloadedTemplate()) { |
| 1981 | for (OverloadedTemplateStorage::iterator I = OST->begin(), |
| 1982 | IEnd = OST->end(); |
| 1983 | I != IEnd; ++I) |
| 1984 | Diag((*I)->getLocation(), diag::note_template_declared_here) |
| 1985 | << 0 << (*I)->getDeclName(); |
| 1986 | |
| 1987 | return; |
| 1988 | } |
| 1989 | } |
| 1990 | |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1991 | QualType Sema::CheckTemplateIdType(TemplateName Name, |
| 1992 | SourceLocation TemplateLoc, |
Douglas Gregor | 739b107a | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 1993 | TemplateArgumentListInfo &TemplateArgs) { |
John McCall | d9dfe3a | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 1994 | DependentTemplateName *DTN |
| 1995 | = Name.getUnderlying().getAsDependentTemplateName(); |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 1996 | if (DTN && DTN->isIdentifier()) |
| 1997 | // When building a template-id where the template-name is dependent, |
| 1998 | // assume the template is a type template. Either our assumption is |
| 1999 | // correct, or the code is ill-formed and will be diagnosed when the |
| 2000 | // dependent name is substituted. |
| 2001 | return Context.getDependentTemplateSpecializationType(ETK_None, |
| 2002 | DTN->getQualifier(), |
| 2003 | DTN->getIdentifier(), |
| 2004 | TemplateArgs); |
| 2005 | |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 2006 | TemplateDecl *Template = Name.getAsTemplateDecl(); |
Richard Smith | 8f65806 | 2013-12-04 00:56:29 +0000 | [diff] [blame] | 2007 | if (!Template || isa<FunctionTemplateDecl>(Template) || |
| 2008 | isa<VarTemplateDecl>(Template)) { |
Douglas Gregor | 8b6070b | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 2009 | // We might have a substituted template template parameter pack. If so, |
| 2010 | // build a template specialization type for it. |
| 2011 | if (Name.getAsSubstTemplateTemplateParmPack()) |
| 2012 | return Context.getTemplateSpecializationType(Name, TemplateArgs); |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2013 | |
Douglas Gregor | 8b6070b | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 2014 | Diag(TemplateLoc, diag::err_template_id_not_a_type) |
| 2015 | << Name; |
| 2016 | NoteAllFoundTemplates(Name); |
| 2017 | return QualType(); |
Douglas Gregor | b67535d | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 2018 | } |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 2019 | |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2020 | // Check that the template argument list is well-formed for this |
| 2021 | // template. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2022 | SmallVector<TemplateArgument, 4> Converted; |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2023 | if (CheckTemplateArgumentList(Template, TemplateLoc, TemplateArgs, |
Richard Smith | 83b11aa | 2014-01-09 02:22:22 +0000 | [diff] [blame] | 2024 | false, Converted)) |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2025 | return QualType(); |
| 2026 | |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2027 | QualType CanonType; |
| 2028 | |
Douglas Gregor | 678d76c | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 2029 | bool InstantiationDependent = false; |
Richard Smith | 83b11aa | 2014-01-09 02:22:22 +0000 | [diff] [blame] | 2030 | if (TypeAliasTemplateDecl *AliasTemplate = |
| 2031 | dyn_cast<TypeAliasTemplateDecl>(Template)) { |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2032 | // Find the canonical type for this type alias template specialization. |
| 2033 | TypeAliasDecl *Pattern = AliasTemplate->getTemplatedDecl(); |
| 2034 | if (Pattern->isInvalidDecl()) |
| 2035 | return QualType(); |
| 2036 | |
| 2037 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
| 2038 | Converted.data(), Converted.size()); |
| 2039 | |
| 2040 | // Only substitute for the innermost template argument list. |
| 2041 | MultiLevelTemplateArgumentList TemplateArgLists; |
Richard Smith | 0c4a34b | 2011-05-14 15:04:18 +0000 | [diff] [blame] | 2042 | TemplateArgLists.addOuterTemplateArguments(&TemplateArgs); |
Richard Smith | 5e96d83 | 2011-05-12 00:06:17 +0000 | [diff] [blame] | 2043 | unsigned Depth = AliasTemplate->getTemplateParameters()->getDepth(); |
| 2044 | for (unsigned I = 0; I < Depth; ++I) |
Richard Smith | 841d8b2 | 2013-05-17 03:04:50 +0000 | [diff] [blame] | 2045 | TemplateArgLists.addOuterTemplateArguments(None); |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2046 | |
Richard Smith | 802c4b7 | 2012-08-23 06:16:52 +0000 | [diff] [blame] | 2047 | LocalInstantiationScope Scope(*this); |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2048 | InstantiatingTemplate Inst(*this, TemplateLoc, Template); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 2049 | if (Inst.isInvalid()) |
Richard Smith | 8a874c9 | 2012-07-08 02:38:24 +0000 | [diff] [blame] | 2050 | return QualType(); |
Richard Smith | 802c4b7 | 2012-08-23 06:16:52 +0000 | [diff] [blame] | 2051 | |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2052 | CanonType = SubstType(Pattern->getUnderlyingType(), |
| 2053 | TemplateArgLists, AliasTemplate->getLocation(), |
| 2054 | AliasTemplate->getDeclName()); |
| 2055 | if (CanonType.isNull()) |
| 2056 | return QualType(); |
| 2057 | } else if (Name.isDependent() || |
| 2058 | TemplateSpecializationType::anyDependentTemplateArguments( |
Douglas Gregor | 678d76c | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 2059 | TemplateArgs, InstantiationDependent)) { |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2060 | // This class template specialization is a dependent |
| 2061 | // type. Therefore, its canonical type is another class template |
| 2062 | // specialization type that contains all of the converted |
| 2063 | // arguments in canonical form. This ensures that, e.g., A<T> and |
| 2064 | // A<T, T> have identical types when A is declared as: |
| 2065 | // |
| 2066 | // template<typename T, typename U = T> struct A; |
Douglas Gregor | 6bc5058 | 2009-05-07 06:41:52 +0000 | [diff] [blame] | 2067 | TemplateName CanonName = Context.getCanonicalTemplateName(Name); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2068 | CanonType = Context.getTemplateSpecializationType(CanonName, |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2069 | Converted.data(), |
| 2070 | Converted.size()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2071 | |
Douglas Gregor | a8e02e7 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2072 | // FIXME: CanonType is not actually the canonical type, and unfortunately |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2073 | // it is a TemplateSpecializationType that we will never use again. |
Douglas Gregor | a8e02e7 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2074 | // In the future, we need to teach getTemplateSpecializationType to only |
| 2075 | // build the canonical type and return that to us. |
| 2076 | CanonType = Context.getCanonicalType(CanonType); |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 2077 | |
| 2078 | // This might work out to be a current instantiation, in which |
| 2079 | // case the canonical type needs to be the InjectedClassNameType. |
| 2080 | // |
| 2081 | // TODO: in theory this could be a simple hashtable lookup; most |
| 2082 | // changes to CurContext don't change the set of current |
| 2083 | // instantiations. |
| 2084 | if (isa<ClassTemplateDecl>(Template)) { |
| 2085 | for (DeclContext *Ctx = CurContext; Ctx; Ctx = Ctx->getLookupParent()) { |
| 2086 | // If we get out to a namespace, we're done. |
| 2087 | if (Ctx->isFileContext()) break; |
| 2088 | |
| 2089 | // If this isn't a record, keep looking. |
| 2090 | CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Ctx); |
| 2091 | if (!Record) continue; |
| 2092 | |
| 2093 | // Look for one of the two cases with InjectedClassNameTypes |
| 2094 | // and check whether it's the same template. |
| 2095 | if (!isa<ClassTemplatePartialSpecializationDecl>(Record) && |
| 2096 | !Record->getDescribedClassTemplate()) |
| 2097 | continue; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2098 | |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 2099 | // Fetch the injected class name type and check whether its |
| 2100 | // injected type is equal to the type we just built. |
| 2101 | QualType ICNT = Context.getTypeDeclType(Record); |
| 2102 | QualType Injected = cast<InjectedClassNameType>(ICNT) |
| 2103 | ->getInjectedSpecializationType(); |
| 2104 | |
| 2105 | if (CanonType != Injected->getCanonicalTypeInternal()) |
| 2106 | continue; |
| 2107 | |
| 2108 | // If so, the canonical type of this TST is the injected |
| 2109 | // class name type of the record we just found. |
| 2110 | assert(ICNT.isCanonical()); |
| 2111 | CanonType = ICNT; |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 2112 | break; |
| 2113 | } |
| 2114 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2115 | } else if (ClassTemplateDecl *ClassTemplate |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 2116 | = dyn_cast<ClassTemplateDecl>(Template)) { |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2117 | // Find the class template specialization declaration that |
| 2118 | // corresponds to these arguments. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2119 | void *InsertPos = nullptr; |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2120 | ClassTemplateSpecializationDecl *Decl |
Craig Topper | 7e0daca | 2014-06-26 04:58:53 +0000 | [diff] [blame] | 2121 | = ClassTemplate->findSpecialization(Converted, InsertPos); |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2122 | if (!Decl) { |
| 2123 | // This is the first time we have referenced this class template |
| 2124 | // specialization. Create the canonical declaration and add it to |
| 2125 | // the set of specializations. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2126 | Decl = ClassTemplateSpecializationDecl::Create(Context, |
Douglas Gregor | e902956 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 2127 | ClassTemplate->getTemplatedDecl()->getTagKind(), |
| 2128 | ClassTemplate->getDeclContext(), |
Abramo Bagnara | fd3a455 | 2011-10-03 20:34:03 +0000 | [diff] [blame] | 2129 | ClassTemplate->getTemplatedDecl()->getLocStart(), |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 2130 | ClassTemplate->getLocation(), |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2131 | ClassTemplate, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2132 | Converted.data(), |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2133 | Converted.size(), nullptr); |
Argyrios Kyrtzidis | 47470f2 | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 2134 | ClassTemplate->AddSpecialization(Decl, InsertPos); |
Abramo Bagnara | 02b9553 | 2012-09-05 09:05:18 +0000 | [diff] [blame] | 2135 | if (ClassTemplate->isOutOfLine()) |
| 2136 | Decl->setLexicalDeclContext(ClassTemplate->getLexicalDeclContext()); |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2137 | } |
| 2138 | |
Chandler Carruth | 2acfb22 | 2013-09-27 22:14:40 +0000 | [diff] [blame] | 2139 | // Diagnose uses of this specialization. |
| 2140 | (void)DiagnoseUseOfDecl(Decl, TemplateLoc); |
| 2141 | |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2142 | CanonType = Context.getTypeDeclType(Decl); |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 2143 | assert(isa<RecordType>(CanonType) && |
| 2144 | "type of non-dependent specialization is not a RecordType"); |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2145 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2146 | |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2147 | // Build the fully-sugared type for this class template |
| 2148 | // specialization, which refers back to the class template |
| 2149 | // specialization we created or found. |
John McCall | 30576cd | 2010-06-13 09:25:03 +0000 | [diff] [blame] | 2150 | return Context.getTemplateSpecializationType(Name, TemplateArgs, CanonType); |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2151 | } |
| 2152 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2153 | TypeResult |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2154 | Sema::ActOnTemplateIdType(CXXScopeSpec &SS, SourceLocation TemplateKWLoc, |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2155 | TemplateTy TemplateD, SourceLocation TemplateLoc, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2156 | SourceLocation LAngleLoc, |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 2157 | ASTTemplateArgsPtr TemplateArgsIn, |
Abramo Bagnara | 4244b43 | 2012-01-27 08:46:19 +0000 | [diff] [blame] | 2158 | SourceLocation RAngleLoc, |
| 2159 | bool IsCtorOrDtorName) { |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2160 | if (SS.isInvalid()) |
| 2161 | return true; |
| 2162 | |
Serge Pavlov | 9ddb76e | 2013-08-27 13:15:56 +0000 | [diff] [blame] | 2163 | TemplateName Template = TemplateD.get(); |
Douglas Gregor | 8bf4205 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 2164 | |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2165 | // Translate the parser's template argument list in our AST format. |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2166 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
Douglas Gregor | b53edfb | 2009-11-10 19:49:08 +0000 | [diff] [blame] | 2167 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2168 | |
Douglas Gregor | 5a06472 | 2011-02-28 17:23:35 +0000 | [diff] [blame] | 2169 | if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) { |
Abramo Bagnara | 4244b43 | 2012-01-27 08:46:19 +0000 | [diff] [blame] | 2170 | QualType T |
| 2171 | = Context.getDependentTemplateSpecializationType(ETK_None, |
| 2172 | DTN->getQualifier(), |
| 2173 | DTN->getIdentifier(), |
| 2174 | TemplateArgs); |
| 2175 | // Build type-source information. |
Douglas Gregor | 5a06472 | 2011-02-28 17:23:35 +0000 | [diff] [blame] | 2176 | TypeLocBuilder TLB; |
| 2177 | DependentTemplateSpecializationTypeLoc SpecTL |
| 2178 | = TLB.push<DependentTemplateSpecializationTypeLoc>(T); |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2179 | SpecTL.setElaboratedKeywordLoc(SourceLocation()); |
| 2180 | SpecTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
Abramo Bagnara | e0a70b2 | 2012-02-06 22:45:07 +0000 | [diff] [blame] | 2181 | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2182 | SpecTL.setTemplateNameLoc(TemplateLoc); |
Douglas Gregor | 5a06472 | 2011-02-28 17:23:35 +0000 | [diff] [blame] | 2183 | SpecTL.setLAngleLoc(LAngleLoc); |
| 2184 | SpecTL.setRAngleLoc(RAngleLoc); |
Douglas Gregor | 5a06472 | 2011-02-28 17:23:35 +0000 | [diff] [blame] | 2185 | for (unsigned I = 0, N = SpecTL.getNumArgs(); I != N; ++I) |
| 2186 | SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo()); |
| 2187 | return CreateParsedType(T, TLB.getTypeSourceInfo(Context, T)); |
| 2188 | } |
| 2189 | |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2190 | QualType Result = CheckTemplateIdType(Template, TemplateLoc, TemplateArgs); |
Douglas Gregor | fe3d7d0 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 2191 | |
| 2192 | if (Result.isNull()) |
| 2193 | return true; |
| 2194 | |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2195 | // Build type-source information. |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2196 | TypeLocBuilder TLB; |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2197 | TemplateSpecializationTypeLoc SpecTL |
| 2198 | = TLB.push<TemplateSpecializationTypeLoc>(Result); |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2199 | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2200 | SpecTL.setTemplateNameLoc(TemplateLoc); |
| 2201 | SpecTL.setLAngleLoc(LAngleLoc); |
| 2202 | SpecTL.setRAngleLoc(RAngleLoc); |
| 2203 | for (unsigned i = 0, e = SpecTL.getNumArgs(); i != e; ++i) |
| 2204 | SpecTL.setArgLocInfo(i, TemplateArgs[i].getLocInfo()); |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2205 | |
Abramo Bagnara | 4244b43 | 2012-01-27 08:46:19 +0000 | [diff] [blame] | 2206 | // NOTE: avoid constructing an ElaboratedTypeLoc if this is a |
| 2207 | // constructor or destructor name (in such a case, the scope specifier |
| 2208 | // will be attached to the enclosing Decl or Expr node). |
| 2209 | if (SS.isNotEmpty() && !IsCtorOrDtorName) { |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2210 | // Create an elaborated-type-specifier containing the nested-name-specifier. |
| 2211 | Result = Context.getElaboratedType(ETK_None, SS.getScopeRep(), Result); |
| 2212 | ElaboratedTypeLoc ElabTL = TLB.push<ElaboratedTypeLoc>(Result); |
Abramo Bagnara | 9033e2b | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 2213 | ElabTL.setElaboratedKeywordLoc(SourceLocation()); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2214 | ElabTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 2215 | } |
| 2216 | |
| 2217 | return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result)); |
John McCall | d8fe9af | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 2218 | } |
John McCall | 06f6fe8d | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 2219 | |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2220 | TypeResult Sema::ActOnTagTemplateIdType(TagUseKind TUK, |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2221 | TypeSpecifierType TagSpec, |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2222 | SourceLocation TagLoc, |
| 2223 | CXXScopeSpec &SS, |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2224 | SourceLocation TemplateKWLoc, |
| 2225 | TemplateTy TemplateD, |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2226 | SourceLocation TemplateLoc, |
| 2227 | SourceLocation LAngleLoc, |
| 2228 | ASTTemplateArgsPtr TemplateArgsIn, |
| 2229 | SourceLocation RAngleLoc) { |
Serge Pavlov | 9ddb76e | 2013-08-27 13:15:56 +0000 | [diff] [blame] | 2230 | TemplateName Template = TemplateD.get(); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2231 | |
| 2232 | // Translate the parser's template argument list in our AST format. |
| 2233 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
| 2234 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
| 2235 | |
| 2236 | // Determine the tag kind |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2237 | TagTypeKind TagKind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2238 | ElaboratedTypeKeyword Keyword |
| 2239 | = TypeWithKeyword::getKeywordForTagTypeKind(TagKind); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2240 | |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2241 | if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) { |
| 2242 | QualType T = Context.getDependentTemplateSpecializationType(Keyword, |
| 2243 | DTN->getQualifier(), |
| 2244 | DTN->getIdentifier(), |
| 2245 | TemplateArgs); |
| 2246 | |
| 2247 | // Build type-source information. |
| 2248 | TypeLocBuilder TLB; |
| 2249 | DependentTemplateSpecializationTypeLoc SpecTL |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2250 | = TLB.push<DependentTemplateSpecializationTypeLoc>(T); |
| 2251 | SpecTL.setElaboratedKeywordLoc(TagLoc); |
| 2252 | SpecTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
Abramo Bagnara | e0a70b2 | 2012-02-06 22:45:07 +0000 | [diff] [blame] | 2253 | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2254 | SpecTL.setTemplateNameLoc(TemplateLoc); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2255 | SpecTL.setLAngleLoc(LAngleLoc); |
| 2256 | SpecTL.setRAngleLoc(RAngleLoc); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2257 | for (unsigned I = 0, N = SpecTL.getNumArgs(); I != N; ++I) |
| 2258 | SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo()); |
| 2259 | return CreateParsedType(T, TLB.getTypeSourceInfo(Context, T)); |
| 2260 | } |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2261 | |
| 2262 | if (TypeAliasTemplateDecl *TAT = |
| 2263 | dyn_cast_or_null<TypeAliasTemplateDecl>(Template.getAsTemplateDecl())) { |
| 2264 | // C++0x [dcl.type.elab]p2: |
| 2265 | // If the identifier resolves to a typedef-name or the simple-template-id |
| 2266 | // resolves to an alias template specialization, the |
| 2267 | // elaborated-type-specifier is ill-formed. |
| 2268 | Diag(TemplateLoc, diag::err_tag_reference_non_tag) << 4; |
| 2269 | Diag(TAT->getLocation(), diag::note_declared_at); |
| 2270 | } |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2271 | |
| 2272 | QualType Result = CheckTemplateIdType(Template, TemplateLoc, TemplateArgs); |
| 2273 | if (Result.isNull()) |
Matt Beaumont-Gay | 045bde4 | 2011-08-25 23:22:24 +0000 | [diff] [blame] | 2274 | return TypeResult(true); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2275 | |
| 2276 | // Check the tag kind |
| 2277 | if (const RecordType *RT = Result->getAs<RecordType>()) { |
John McCall | d8fe9af | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 2278 | RecordDecl *D = RT->getDecl(); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2279 | |
John McCall | d8fe9af | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 2280 | IdentifierInfo *Id = D->getIdentifier(); |
| 2281 | assert(Id && "templated class must have an identifier"); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2282 | |
Richard Trieu | caa33d3 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 2283 | if (!isAcceptableTagRedeclaration(D, TagKind, TUK == TUK_Definition, |
| 2284 | TagLoc, *Id)) { |
John McCall | d8fe9af | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 2285 | Diag(TagLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2286 | << Result |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 2287 | << FixItHint::CreateReplacement(SourceRange(TagLoc), D->getKindName()); |
John McCall | 7f41d98 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 2288 | Diag(D->getLocation(), diag::note_previous_use); |
John McCall | 06f6fe8d | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 2289 | } |
| 2290 | } |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2291 | |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2292 | // Provide source-location information for the template specialization. |
| 2293 | TypeLocBuilder TLB; |
| 2294 | TemplateSpecializationTypeLoc SpecTL |
| 2295 | = TLB.push<TemplateSpecializationTypeLoc>(Result); |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2296 | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2297 | SpecTL.setTemplateNameLoc(TemplateLoc); |
| 2298 | SpecTL.setLAngleLoc(LAngleLoc); |
| 2299 | SpecTL.setRAngleLoc(RAngleLoc); |
| 2300 | for (unsigned i = 0, e = SpecTL.getNumArgs(); i != e; ++i) |
| 2301 | SpecTL.setArgLocInfo(i, TemplateArgs[i].getLocInfo()); |
John McCall | 06f6fe8d | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 2302 | |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2303 | // Construct an elaborated type containing the nested-name-specifier (if any) |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2304 | // and tag keyword. |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2305 | Result = Context.getElaboratedType(Keyword, SS.getScopeRep(), Result); |
| 2306 | ElaboratedTypeLoc ElabTL = TLB.push<ElaboratedTypeLoc>(Result); |
Abramo Bagnara | 9033e2b | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 2307 | ElabTL.setElaboratedKeywordLoc(TagLoc); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2308 | ElabTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 2309 | return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result)); |
Douglas Gregor | 8bf4205 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 2310 | } |
| 2311 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2312 | static bool CheckTemplatePartialSpecializationArgs( |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 2313 | Sema &S, SourceLocation NameLoc, TemplateParameterList *TemplateParams, |
| 2314 | unsigned ExplicitArgs, SmallVectorImpl<TemplateArgument> &TemplateArgs); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2315 | |
| 2316 | static bool CheckTemplateSpecializationScope(Sema &S, NamedDecl *Specialized, |
| 2317 | NamedDecl *PrevDecl, |
| 2318 | SourceLocation Loc, |
| 2319 | bool IsPartialSpecialization); |
| 2320 | |
| 2321 | static TemplateSpecializationKind getTemplateSpecializationKind(Decl *D); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2322 | |
Richard Smith | 300e0c3 | 2013-09-24 04:49:23 +0000 | [diff] [blame] | 2323 | static bool isTemplateArgumentTemplateParameter( |
| 2324 | const TemplateArgument &Arg, unsigned Depth, unsigned Index) { |
| 2325 | switch (Arg.getKind()) { |
| 2326 | case TemplateArgument::Null: |
| 2327 | case TemplateArgument::NullPtr: |
| 2328 | case TemplateArgument::Integral: |
| 2329 | case TemplateArgument::Declaration: |
| 2330 | case TemplateArgument::Pack: |
| 2331 | case TemplateArgument::TemplateExpansion: |
| 2332 | return false; |
| 2333 | |
| 2334 | case TemplateArgument::Type: { |
| 2335 | QualType Type = Arg.getAsType(); |
| 2336 | const TemplateTypeParmType *TPT = |
| 2337 | Arg.getAsType()->getAs<TemplateTypeParmType>(); |
| 2338 | return TPT && !Type.hasQualifiers() && |
| 2339 | TPT->getDepth() == Depth && TPT->getIndex() == Index; |
| 2340 | } |
| 2341 | |
| 2342 | case TemplateArgument::Expression: { |
| 2343 | DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Arg.getAsExpr()); |
| 2344 | if (!DRE || !DRE->getDecl()) |
| 2345 | return false; |
| 2346 | const NonTypeTemplateParmDecl *NTTP = |
| 2347 | dyn_cast<NonTypeTemplateParmDecl>(DRE->getDecl()); |
| 2348 | return NTTP && NTTP->getDepth() == Depth && NTTP->getIndex() == Index; |
| 2349 | } |
| 2350 | |
| 2351 | case TemplateArgument::Template: |
| 2352 | const TemplateTemplateParmDecl *TTP = |
| 2353 | dyn_cast_or_null<TemplateTemplateParmDecl>( |
| 2354 | Arg.getAsTemplateOrTemplatePattern().getAsTemplateDecl()); |
| 2355 | return TTP && TTP->getDepth() == Depth && TTP->getIndex() == Index; |
| 2356 | } |
| 2357 | llvm_unreachable("unexpected kind of template argument"); |
| 2358 | } |
| 2359 | |
| 2360 | static bool isSameAsPrimaryTemplate(TemplateParameterList *Params, |
| 2361 | ArrayRef<TemplateArgument> Args) { |
| 2362 | if (Params->size() != Args.size()) |
| 2363 | return false; |
| 2364 | |
| 2365 | unsigned Depth = Params->getDepth(); |
| 2366 | |
| 2367 | for (unsigned I = 0, N = Args.size(); I != N; ++I) { |
| 2368 | TemplateArgument Arg = Args[I]; |
| 2369 | |
| 2370 | // If the parameter is a pack expansion, the argument must be a pack |
| 2371 | // whose only element is a pack expansion. |
| 2372 | if (Params->getParam(I)->isParameterPack()) { |
| 2373 | if (Arg.getKind() != TemplateArgument::Pack || Arg.pack_size() != 1 || |
| 2374 | !Arg.pack_begin()->isPackExpansion()) |
| 2375 | return false; |
| 2376 | Arg = Arg.pack_begin()->getPackExpansionPattern(); |
| 2377 | } |
| 2378 | |
| 2379 | if (!isTemplateArgumentTemplateParameter(Arg, Depth, I)) |
| 2380 | return false; |
| 2381 | } |
| 2382 | |
| 2383 | return true; |
| 2384 | } |
| 2385 | |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 2386 | /// Convert the parser's template argument list representation into our form. |
| 2387 | static TemplateArgumentListInfo |
| 2388 | makeTemplateArgumentListInfo(Sema &S, TemplateIdAnnotation &TemplateId) { |
| 2389 | TemplateArgumentListInfo TemplateArgs(TemplateId.LAngleLoc, |
| 2390 | TemplateId.RAngleLoc); |
| 2391 | ASTTemplateArgsPtr TemplateArgsPtr(TemplateId.getTemplateArgs(), |
| 2392 | TemplateId.NumArgs); |
| 2393 | S.translateTemplateArguments(TemplateArgsPtr, TemplateArgs); |
| 2394 | return TemplateArgs; |
| 2395 | } |
| 2396 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2397 | DeclResult Sema::ActOnVarTemplateSpecialization( |
Richard Smith | beef345 | 2014-01-16 23:39:20 +0000 | [diff] [blame] | 2398 | Scope *S, Declarator &D, TypeSourceInfo *DI, SourceLocation TemplateKWLoc, |
Craig Topper | c79e5e3 | 2014-10-31 06:57:13 +0000 | [diff] [blame] | 2399 | TemplateParameterList *TemplateParams, StorageClass SC, |
Richard Smith | beef345 | 2014-01-16 23:39:20 +0000 | [diff] [blame] | 2400 | bool IsPartialSpecialization) { |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2401 | // D must be variable template id. |
| 2402 | assert(D.getName().getKind() == UnqualifiedId::IK_TemplateId && |
| 2403 | "Variable template specialization is declared with a template it."); |
| 2404 | |
| 2405 | TemplateIdAnnotation *TemplateId = D.getName().TemplateId; |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 2406 | TemplateArgumentListInfo TemplateArgs = |
| 2407 | makeTemplateArgumentListInfo(*this, *TemplateId); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2408 | SourceLocation TemplateNameLoc = D.getIdentifierLoc(); |
| 2409 | SourceLocation LAngleLoc = TemplateId->LAngleLoc; |
| 2410 | SourceLocation RAngleLoc = TemplateId->RAngleLoc; |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 2411 | |
Richard Smith | beef345 | 2014-01-16 23:39:20 +0000 | [diff] [blame] | 2412 | TemplateName Name = TemplateId->Template.get(); |
| 2413 | |
| 2414 | // The template-id must name a variable template. |
| 2415 | VarTemplateDecl *VarTemplate = |
Karthik Bhat | 967c13d | 2014-05-08 13:16:20 +0000 | [diff] [blame] | 2416 | dyn_cast_or_null<VarTemplateDecl>(Name.getAsTemplateDecl()); |
| 2417 | if (!VarTemplate) { |
| 2418 | NamedDecl *FnTemplate; |
| 2419 | if (auto *OTS = Name.getAsOverloadedTemplate()) |
| 2420 | FnTemplate = *OTS->begin(); |
| 2421 | else |
| 2422 | FnTemplate = dyn_cast_or_null<FunctionTemplateDecl>(Name.getAsTemplateDecl()); |
| 2423 | if (FnTemplate) |
| 2424 | return Diag(D.getIdentifierLoc(), diag::err_var_spec_no_template_but_method) |
| 2425 | << FnTemplate->getDeclName(); |
Richard Smith | beef345 | 2014-01-16 23:39:20 +0000 | [diff] [blame] | 2426 | return Diag(D.getIdentifierLoc(), diag::err_var_spec_no_template) |
| 2427 | << IsPartialSpecialization; |
Karthik Bhat | 967c13d | 2014-05-08 13:16:20 +0000 | [diff] [blame] | 2428 | } |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2429 | |
| 2430 | // Check for unexpanded parameter packs in any of the template arguments. |
| 2431 | for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) |
| 2432 | if (DiagnoseUnexpandedParameterPack(TemplateArgs[I], |
| 2433 | UPPC_PartialSpecialization)) |
| 2434 | return true; |
| 2435 | |
| 2436 | // Check that the template argument list is well-formed for this |
| 2437 | // template. |
| 2438 | SmallVector<TemplateArgument, 4> Converted; |
| 2439 | if (CheckTemplateArgumentList(VarTemplate, TemplateNameLoc, TemplateArgs, |
| 2440 | false, Converted)) |
| 2441 | return true; |
| 2442 | |
| 2443 | // Check that the type of this variable template specialization |
| 2444 | // matches the expected type. |
| 2445 | TypeSourceInfo *ExpectedDI; |
| 2446 | { |
| 2447 | // Do substitution on the type of the declaration |
| 2448 | TemplateArgumentList TemplateArgList(TemplateArgumentList::OnStack, |
| 2449 | Converted.data(), Converted.size()); |
| 2450 | InstantiatingTemplate Inst(*this, TemplateKWLoc, VarTemplate); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 2451 | if (Inst.isInvalid()) |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2452 | return true; |
| 2453 | VarDecl *Templated = VarTemplate->getTemplatedDecl(); |
| 2454 | ExpectedDI = |
| 2455 | SubstType(Templated->getTypeSourceInfo(), |
| 2456 | MultiLevelTemplateArgumentList(TemplateArgList), |
| 2457 | Templated->getTypeSpecStartLoc(), Templated->getDeclName()); |
| 2458 | } |
| 2459 | if (!ExpectedDI) |
| 2460 | return true; |
| 2461 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2462 | // Find the variable template (partial) specialization declaration that |
| 2463 | // corresponds to these arguments. |
| 2464 | if (IsPartialSpecialization) { |
| 2465 | if (CheckTemplatePartialSpecializationArgs( |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 2466 | *this, TemplateNameLoc, VarTemplate->getTemplateParameters(), |
| 2467 | TemplateArgs.size(), Converted)) |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2468 | return true; |
| 2469 | |
| 2470 | bool InstantiationDependent; |
| 2471 | if (!Name.isDependent() && |
| 2472 | !TemplateSpecializationType::anyDependentTemplateArguments( |
| 2473 | TemplateArgs.getArgumentArray(), TemplateArgs.size(), |
| 2474 | InstantiationDependent)) { |
| 2475 | Diag(TemplateNameLoc, diag::err_partial_spec_fully_specialized) |
| 2476 | << VarTemplate->getDeclName(); |
| 2477 | IsPartialSpecialization = false; |
| 2478 | } |
Richard Smith | 300e0c3 | 2013-09-24 04:49:23 +0000 | [diff] [blame] | 2479 | |
| 2480 | if (isSameAsPrimaryTemplate(VarTemplate->getTemplateParameters(), |
| 2481 | Converted)) { |
| 2482 | // C++ [temp.class.spec]p9b3: |
| 2483 | // |
| 2484 | // -- The argument list of the specialization shall not be identical |
| 2485 | // to the implicit argument list of the primary template. |
| 2486 | Diag(TemplateNameLoc, diag::err_partial_spec_args_match_primary_template) |
| 2487 | << /*variable template*/ 1 |
| 2488 | << /*is definition*/(SC != SC_Extern && !CurContext->isRecord()) |
| 2489 | << FixItHint::CreateRemoval(SourceRange(LAngleLoc, RAngleLoc)); |
| 2490 | // FIXME: Recover from this by treating the declaration as a redeclaration |
| 2491 | // of the primary template. |
| 2492 | return true; |
| 2493 | } |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2494 | } |
| 2495 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2496 | void *InsertPos = nullptr; |
| 2497 | VarTemplateSpecializationDecl *PrevDecl = nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2498 | |
| 2499 | if (IsPartialSpecialization) |
| 2500 | // FIXME: Template parameter list matters too |
Craig Topper | 7e0daca | 2014-06-26 04:58:53 +0000 | [diff] [blame] | 2501 | PrevDecl = VarTemplate->findPartialSpecialization(Converted, InsertPos); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2502 | else |
Craig Topper | 7e0daca | 2014-06-26 04:58:53 +0000 | [diff] [blame] | 2503 | PrevDecl = VarTemplate->findSpecialization(Converted, InsertPos); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2504 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2505 | VarTemplateSpecializationDecl *Specialization = nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2506 | |
| 2507 | // Check whether we can declare a variable template specialization in |
| 2508 | // the current scope. |
| 2509 | if (CheckTemplateSpecializationScope(*this, VarTemplate, PrevDecl, |
| 2510 | TemplateNameLoc, |
| 2511 | IsPartialSpecialization)) |
| 2512 | return true; |
| 2513 | |
| 2514 | if (PrevDecl && PrevDecl->getSpecializationKind() == TSK_Undeclared) { |
| 2515 | // Since the only prior variable template specialization with these |
| 2516 | // arguments was referenced but not declared, reuse that |
| 2517 | // declaration node as our own, updating its source location and |
| 2518 | // the list of outer template parameters to reflect our new declaration. |
| 2519 | Specialization = PrevDecl; |
| 2520 | Specialization->setLocation(TemplateNameLoc); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2521 | PrevDecl = nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2522 | } else if (IsPartialSpecialization) { |
| 2523 | // Create a new class template partial specialization declaration node. |
| 2524 | VarTemplatePartialSpecializationDecl *PrevPartial = |
| 2525 | cast_or_null<VarTemplatePartialSpecializationDecl>(PrevDecl); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2526 | VarTemplatePartialSpecializationDecl *Partial = |
| 2527 | VarTemplatePartialSpecializationDecl::Create( |
| 2528 | Context, VarTemplate->getDeclContext(), TemplateKWLoc, |
| 2529 | TemplateNameLoc, TemplateParams, VarTemplate, DI->getType(), DI, SC, |
Richard Smith | b2f61b4 | 2013-08-22 23:27:37 +0000 | [diff] [blame] | 2530 | Converted.data(), Converted.size(), TemplateArgs); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2531 | |
| 2532 | if (!PrevPartial) |
| 2533 | VarTemplate->AddPartialSpecialization(Partial, InsertPos); |
| 2534 | Specialization = Partial; |
| 2535 | |
| 2536 | // If we are providing an explicit specialization of a member variable |
| 2537 | // template specialization, make a note of that. |
| 2538 | if (PrevPartial && PrevPartial->getInstantiatedFromMember()) |
Larisse Voufo | 4cda461 | 2013-08-22 00:28:27 +0000 | [diff] [blame] | 2539 | PrevPartial->setMemberSpecialization(); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2540 | |
| 2541 | // Check that all of the template parameters of the variable template |
| 2542 | // partial specialization are deducible from the template |
| 2543 | // arguments. If not, this variable template partial specialization |
| 2544 | // will never be used. |
| 2545 | llvm::SmallBitVector DeducibleParams(TemplateParams->size()); |
| 2546 | MarkUsedTemplateParameters(Partial->getTemplateArgs(), true, |
| 2547 | TemplateParams->getDepth(), DeducibleParams); |
| 2548 | |
| 2549 | if (!DeducibleParams.all()) { |
| 2550 | unsigned NumNonDeducible = |
| 2551 | DeducibleParams.size() - DeducibleParams.count(); |
| 2552 | Diag(TemplateNameLoc, diag::warn_partial_specs_not_deducible) |
Richard Smith | 300e0c3 | 2013-09-24 04:49:23 +0000 | [diff] [blame] | 2553 | << /*variable template*/ 1 << (NumNonDeducible > 1) |
| 2554 | << SourceRange(TemplateNameLoc, RAngleLoc); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2555 | for (unsigned I = 0, N = DeducibleParams.size(); I != N; ++I) { |
| 2556 | if (!DeducibleParams[I]) { |
| 2557 | NamedDecl *Param = cast<NamedDecl>(TemplateParams->getParam(I)); |
| 2558 | if (Param->getDeclName()) |
| 2559 | Diag(Param->getLocation(), diag::note_partial_spec_unused_parameter) |
| 2560 | << Param->getDeclName(); |
| 2561 | else |
| 2562 | Diag(Param->getLocation(), diag::note_partial_spec_unused_parameter) |
David Blaikie | abe1a39 | 2014-04-02 05:58:29 +0000 | [diff] [blame] | 2563 | << "(anonymous)"; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2564 | } |
| 2565 | } |
| 2566 | } |
| 2567 | } else { |
| 2568 | // Create a new class template specialization declaration node for |
| 2569 | // this explicit specialization or friend declaration. |
| 2570 | Specialization = VarTemplateSpecializationDecl::Create( |
| 2571 | Context, VarTemplate->getDeclContext(), TemplateKWLoc, TemplateNameLoc, |
| 2572 | VarTemplate, DI->getType(), DI, SC, Converted.data(), Converted.size()); |
| 2573 | Specialization->setTemplateArgsInfo(TemplateArgs); |
| 2574 | |
| 2575 | if (!PrevDecl) |
| 2576 | VarTemplate->AddSpecialization(Specialization, InsertPos); |
| 2577 | } |
| 2578 | |
| 2579 | // C++ [temp.expl.spec]p6: |
| 2580 | // If a template, a member template or the member of a class template is |
| 2581 | // explicitly specialized then that specialization shall be declared |
| 2582 | // before the first use of that specialization that would cause an implicit |
| 2583 | // instantiation to take place, in every translation unit in which such a |
| 2584 | // use occurs; no diagnostic is required. |
| 2585 | if (PrevDecl && PrevDecl->getPointOfInstantiation().isValid()) { |
| 2586 | bool Okay = false; |
| 2587 | for (Decl *Prev = PrevDecl; Prev; Prev = Prev->getPreviousDecl()) { |
| 2588 | // Is there any previous explicit specialization declaration? |
| 2589 | if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) { |
| 2590 | Okay = true; |
| 2591 | break; |
| 2592 | } |
| 2593 | } |
| 2594 | |
| 2595 | if (!Okay) { |
| 2596 | SourceRange Range(TemplateNameLoc, RAngleLoc); |
| 2597 | Diag(TemplateNameLoc, diag::err_specialization_after_instantiation) |
| 2598 | << Name << Range; |
| 2599 | |
| 2600 | Diag(PrevDecl->getPointOfInstantiation(), |
| 2601 | diag::note_instantiation_required_here) |
| 2602 | << (PrevDecl->getTemplateSpecializationKind() != |
| 2603 | TSK_ImplicitInstantiation); |
| 2604 | return true; |
| 2605 | } |
| 2606 | } |
| 2607 | |
| 2608 | Specialization->setTemplateKeywordLoc(TemplateKWLoc); |
| 2609 | Specialization->setLexicalDeclContext(CurContext); |
| 2610 | |
| 2611 | // Add the specialization into its lexical context, so that it can |
| 2612 | // be seen when iterating through the list of declarations in that |
| 2613 | // context. However, specializations are not found by name lookup. |
| 2614 | CurContext->addDecl(Specialization); |
| 2615 | |
| 2616 | // Note that this is an explicit specialization. |
| 2617 | Specialization->setSpecializationKind(TSK_ExplicitSpecialization); |
| 2618 | |
| 2619 | if (PrevDecl) { |
| 2620 | // Check that this isn't a redefinition of this specialization, |
| 2621 | // merging with previous declarations. |
| 2622 | LookupResult PrevSpec(*this, GetNameForDeclarator(D), LookupOrdinaryName, |
| 2623 | ForRedeclaration); |
| 2624 | PrevSpec.addDecl(PrevDecl); |
| 2625 | D.setRedeclaration(CheckVariableDeclaration(Specialization, PrevSpec)); |
Larisse Voufo | 4cda461 | 2013-08-22 00:28:27 +0000 | [diff] [blame] | 2626 | } else if (Specialization->isStaticDataMember() && |
| 2627 | Specialization->isOutOfLine()) { |
| 2628 | Specialization->setAccess(VarTemplate->getAccess()); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2629 | } |
| 2630 | |
| 2631 | // Link instantiations of static data members back to the template from |
| 2632 | // which they were instantiated. |
| 2633 | if (Specialization->isStaticDataMember()) |
| 2634 | Specialization->setInstantiationOfStaticDataMember( |
| 2635 | VarTemplate->getTemplatedDecl(), |
| 2636 | Specialization->getSpecializationKind()); |
| 2637 | |
| 2638 | return Specialization; |
| 2639 | } |
| 2640 | |
| 2641 | namespace { |
| 2642 | /// \brief A partial specialization whose template arguments have matched |
| 2643 | /// a given template-id. |
| 2644 | struct PartialSpecMatchResult { |
| 2645 | VarTemplatePartialSpecializationDecl *Partial; |
| 2646 | TemplateArgumentList *Args; |
| 2647 | }; |
| 2648 | } |
| 2649 | |
| 2650 | DeclResult |
| 2651 | Sema::CheckVarTemplateId(VarTemplateDecl *Template, SourceLocation TemplateLoc, |
| 2652 | SourceLocation TemplateNameLoc, |
| 2653 | const TemplateArgumentListInfo &TemplateArgs) { |
| 2654 | assert(Template && "A variable template id without template?"); |
| 2655 | |
| 2656 | // Check that the template argument list is well-formed for this template. |
| 2657 | SmallVector<TemplateArgument, 4> Converted; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2658 | if (CheckTemplateArgumentList( |
| 2659 | Template, TemplateNameLoc, |
| 2660 | const_cast<TemplateArgumentListInfo &>(TemplateArgs), false, |
Richard Smith | 83b11aa | 2014-01-09 02:22:22 +0000 | [diff] [blame] | 2661 | Converted)) |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2662 | return true; |
| 2663 | |
| 2664 | // Find the variable template specialization declaration that |
| 2665 | // corresponds to these arguments. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2666 | void *InsertPos = nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2667 | if (VarTemplateSpecializationDecl *Spec = Template->findSpecialization( |
Craig Topper | 7e0daca | 2014-06-26 04:58:53 +0000 | [diff] [blame] | 2668 | Converted, InsertPos)) |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2669 | // If we already have a variable template specialization, return it. |
| 2670 | return Spec; |
| 2671 | |
| 2672 | // This is the first time we have referenced this variable template |
| 2673 | // specialization. Create the canonical declaration and add it to |
| 2674 | // the set of specializations, based on the closest partial specialization |
| 2675 | // that it represents. That is, |
| 2676 | VarDecl *InstantiationPattern = Template->getTemplatedDecl(); |
| 2677 | TemplateArgumentList TemplateArgList(TemplateArgumentList::OnStack, |
| 2678 | Converted.data(), Converted.size()); |
| 2679 | TemplateArgumentList *InstantiationArgs = &TemplateArgList; |
| 2680 | bool AmbiguousPartialSpec = false; |
| 2681 | typedef PartialSpecMatchResult MatchResult; |
| 2682 | SmallVector<MatchResult, 4> Matched; |
| 2683 | SourceLocation PointOfInstantiation = TemplateNameLoc; |
| 2684 | TemplateSpecCandidateSet FailedCandidates(PointOfInstantiation); |
| 2685 | |
| 2686 | // 1. Attempt to find the closest partial specialization that this |
| 2687 | // specializes, if any. |
| 2688 | // If any of the template arguments is dependent, then this is probably |
| 2689 | // a placeholder for an incomplete declarative context; which must be |
| 2690 | // complete by instantiation time. Thus, do not search through the partial |
| 2691 | // specializations yet. |
Larisse Voufo | 3061638 | 2013-08-23 22:21:36 +0000 | [diff] [blame] | 2692 | // TODO: Unify with InstantiateClassTemplateSpecialization()? |
| 2693 | // Perhaps better after unification of DeduceTemplateArguments() and |
| 2694 | // getMoreSpecializedPartialSpecialization(). |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2695 | bool InstantiationDependent = false; |
| 2696 | if (!TemplateSpecializationType::anyDependentTemplateArguments( |
| 2697 | TemplateArgs, InstantiationDependent)) { |
| 2698 | |
| 2699 | SmallVector<VarTemplatePartialSpecializationDecl *, 4> PartialSpecs; |
| 2700 | Template->getPartialSpecializations(PartialSpecs); |
| 2701 | |
| 2702 | for (unsigned I = 0, N = PartialSpecs.size(); I != N; ++I) { |
| 2703 | VarTemplatePartialSpecializationDecl *Partial = PartialSpecs[I]; |
| 2704 | TemplateDeductionInfo Info(FailedCandidates.getLocation()); |
| 2705 | |
| 2706 | if (TemplateDeductionResult Result = |
| 2707 | DeduceTemplateArguments(Partial, TemplateArgList, Info)) { |
| 2708 | // Store the failed-deduction information for use in diagnostics, later. |
Larisse Voufo | 3061638 | 2013-08-23 22:21:36 +0000 | [diff] [blame] | 2709 | // TODO: Actually use the failed-deduction info? |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2710 | FailedCandidates.addCandidate() |
| 2711 | .set(Partial, MakeDeductionFailureInfo(Context, Result, Info)); |
| 2712 | (void)Result; |
| 2713 | } else { |
| 2714 | Matched.push_back(PartialSpecMatchResult()); |
| 2715 | Matched.back().Partial = Partial; |
| 2716 | Matched.back().Args = Info.take(); |
| 2717 | } |
| 2718 | } |
| 2719 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2720 | if (Matched.size() >= 1) { |
| 2721 | SmallVector<MatchResult, 4>::iterator Best = Matched.begin(); |
| 2722 | if (Matched.size() == 1) { |
| 2723 | // -- If exactly one matching specialization is found, the |
| 2724 | // instantiation is generated from that specialization. |
| 2725 | // We don't need to do anything for this. |
| 2726 | } else { |
| 2727 | // -- If more than one matching specialization is found, the |
| 2728 | // partial order rules (14.5.4.2) are used to determine |
| 2729 | // whether one of the specializations is more specialized |
| 2730 | // than the others. If none of the specializations is more |
| 2731 | // specialized than all of the other matching |
| 2732 | // specializations, then the use of the variable template is |
| 2733 | // ambiguous and the program is ill-formed. |
| 2734 | for (SmallVector<MatchResult, 4>::iterator P = Best + 1, |
| 2735 | PEnd = Matched.end(); |
| 2736 | P != PEnd; ++P) { |
| 2737 | if (getMoreSpecializedPartialSpecialization(P->Partial, Best->Partial, |
| 2738 | PointOfInstantiation) == |
| 2739 | P->Partial) |
| 2740 | Best = P; |
| 2741 | } |
| 2742 | |
| 2743 | // Determine if the best partial specialization is more specialized than |
| 2744 | // the others. |
| 2745 | for (SmallVector<MatchResult, 4>::iterator P = Matched.begin(), |
| 2746 | PEnd = Matched.end(); |
| 2747 | P != PEnd; ++P) { |
| 2748 | if (P != Best && getMoreSpecializedPartialSpecialization( |
| 2749 | P->Partial, Best->Partial, |
| 2750 | PointOfInstantiation) != Best->Partial) { |
| 2751 | AmbiguousPartialSpec = true; |
| 2752 | break; |
| 2753 | } |
| 2754 | } |
| 2755 | } |
| 2756 | |
| 2757 | // Instantiate using the best variable template partial specialization. |
| 2758 | InstantiationPattern = Best->Partial; |
| 2759 | InstantiationArgs = Best->Args; |
| 2760 | } else { |
| 2761 | // -- If no match is found, the instantiation is generated |
| 2762 | // from the primary template. |
| 2763 | // InstantiationPattern = Template->getTemplatedDecl(); |
| 2764 | } |
| 2765 | } |
| 2766 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2767 | // 2. Create the canonical declaration. |
| 2768 | // Note that we do not instantiate the variable just yet, since |
| 2769 | // instantiation is handled in DoMarkVarDeclReferenced(). |
| 2770 | // FIXME: LateAttrs et al.? |
| 2771 | VarTemplateSpecializationDecl *Decl = BuildVarTemplateInstantiation( |
| 2772 | Template, InstantiationPattern, *InstantiationArgs, TemplateArgs, |
| 2773 | Converted, TemplateNameLoc, InsertPos /*, LateAttrs, StartingScope*/); |
| 2774 | if (!Decl) |
| 2775 | return true; |
| 2776 | |
| 2777 | if (AmbiguousPartialSpec) { |
| 2778 | // Partial ordering did not produce a clear winner. Complain. |
| 2779 | Decl->setInvalidDecl(); |
| 2780 | Diag(PointOfInstantiation, diag::err_partial_spec_ordering_ambiguous) |
| 2781 | << Decl; |
| 2782 | |
| 2783 | // Print the matching partial specializations. |
| 2784 | for (SmallVector<MatchResult, 4>::iterator P = Matched.begin(), |
| 2785 | PEnd = Matched.end(); |
| 2786 | P != PEnd; ++P) |
| 2787 | Diag(P->Partial->getLocation(), diag::note_partial_spec_match) |
| 2788 | << getTemplateArgumentBindingsText( |
| 2789 | P->Partial->getTemplateParameters(), *P->Args); |
| 2790 | return true; |
| 2791 | } |
| 2792 | |
| 2793 | if (VarTemplatePartialSpecializationDecl *D = |
| 2794 | dyn_cast<VarTemplatePartialSpecializationDecl>(InstantiationPattern)) |
| 2795 | Decl->setInstantiationOf(D, InstantiationArgs); |
| 2796 | |
| 2797 | assert(Decl && "No variable template specialization?"); |
| 2798 | return Decl; |
| 2799 | } |
| 2800 | |
| 2801 | ExprResult |
| 2802 | Sema::CheckVarTemplateId(const CXXScopeSpec &SS, |
| 2803 | const DeclarationNameInfo &NameInfo, |
| 2804 | VarTemplateDecl *Template, SourceLocation TemplateLoc, |
| 2805 | const TemplateArgumentListInfo *TemplateArgs) { |
| 2806 | |
| 2807 | DeclResult Decl = CheckVarTemplateId(Template, TemplateLoc, NameInfo.getLoc(), |
| 2808 | *TemplateArgs); |
| 2809 | if (Decl.isInvalid()) |
| 2810 | return ExprError(); |
| 2811 | |
| 2812 | VarDecl *Var = cast<VarDecl>(Decl.get()); |
| 2813 | if (!Var->getTemplateSpecializationKind()) |
| 2814 | Var->setTemplateSpecializationKind(TSK_ImplicitInstantiation, |
| 2815 | NameInfo.getLoc()); |
| 2816 | |
| 2817 | // Build an ordinary singleton decl ref. |
| 2818 | return BuildDeclarationNameExpr(SS, NameInfo, Var, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2819 | /*FoundD=*/nullptr, TemplateArgs); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2820 | } |
| 2821 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2822 | ExprResult Sema::BuildTemplateIdExpr(const CXXScopeSpec &SS, |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 2823 | SourceLocation TemplateKWLoc, |
Douglas Gregor | 0da1d43 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 2824 | LookupResult &R, |
| 2825 | bool RequiresADL, |
Abramo Bagnara | 65f7c3d | 2012-02-06 14:31:00 +0000 | [diff] [blame] | 2826 | const TemplateArgumentListInfo *TemplateArgs) { |
Douglas Gregor | a727cb9 | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 2827 | // FIXME: Can we do any checking at this point? I guess we could check the |
| 2828 | // template arguments that we have against the template name, if the template |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2829 | // name refers to a single template. That's not a terribly common case, |
Douglas Gregor | a727cb9 | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 2830 | // though. |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 2831 | // foo<int> could identify a single function unambiguously |
| 2832 | // This approach does NOT work, since f<int>(1); |
| 2833 | // gets resolved prior to resorting to overload resolution |
| 2834 | // i.e., template<class T> void f(double); |
| 2835 | // vs template<class T, class U> void f(U); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2836 | |
| 2837 | // These should be filtered out by our callers. |
| 2838 | assert(!R.empty() && "empty lookup results when building templateid"); |
| 2839 | assert(!R.isAmbiguous() && "ambiguous lookup when building templateid"); |
| 2840 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2841 | // In C++1y, check variable template ids. |
Richard Smith | d7d11ef | 2014-02-03 20:09:56 +0000 | [diff] [blame] | 2842 | bool InstantiationDependent; |
| 2843 | if (R.getAsSingle<VarTemplateDecl>() && |
| 2844 | !TemplateSpecializationType::anyDependentTemplateArguments( |
| 2845 | *TemplateArgs, InstantiationDependent)) { |
| 2846 | return CheckVarTemplateId(SS, R.getLookupNameInfo(), |
| 2847 | R.getAsSingle<VarTemplateDecl>(), |
| 2848 | TemplateKWLoc, TemplateArgs); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2849 | } |
| 2850 | |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 2851 | // We don't want lookup warnings at this point. |
| 2852 | R.suppressDiagnostics(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2853 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2854 | UnresolvedLookupExpr *ULE |
Douglas Gregor | a6e053e | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 2855 | = UnresolvedLookupExpr::Create(Context, R.getNamingClass(), |
Douglas Gregor | 0da1d43 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 2856 | SS.getWithLocInContext(Context), |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 2857 | TemplateKWLoc, |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2858 | R.getLookupNameInfo(), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2859 | RequiresADL, TemplateArgs, |
Douglas Gregor | 30a4f4c | 2010-05-23 18:57:34 +0000 | [diff] [blame] | 2860 | R.begin(), R.end()); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2861 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 2862 | return ULE; |
Douglas Gregor | a727cb9 | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 2863 | } |
| 2864 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2865 | // We actually only call this from template instantiation. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2866 | ExprResult |
Jeffrey Yasskin | c76498d | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 2867 | Sema::BuildQualifiedTemplateIdExpr(CXXScopeSpec &SS, |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 2868 | SourceLocation TemplateKWLoc, |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2869 | const DeclarationNameInfo &NameInfo, |
Abramo Bagnara | 65f7c3d | 2012-02-06 14:31:00 +0000 | [diff] [blame] | 2870 | const TemplateArgumentListInfo *TemplateArgs) { |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2871 | |
Abramo Bagnara | 65f7c3d | 2012-02-06 14:31:00 +0000 | [diff] [blame] | 2872 | assert(TemplateArgs || TemplateKWLoc.isValid()); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2873 | DeclContext *DC; |
| 2874 | if (!(DC = computeDeclContext(SS, false)) || |
| 2875 | DC->isDependentContext() || |
John McCall | 0b66eb3 | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 2876 | RequireCompleteDeclContext(SS, DC)) |
Reid Kleckner | 034531d | 2014-12-18 18:17:42 +0000 | [diff] [blame] | 2877 | return BuildDependentDeclRefExpr(SS, TemplateKWLoc, NameInfo, TemplateArgs); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2878 | |
Douglas Gregor | 786123d | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 2879 | bool MemberOfUnknownSpecialization; |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2880 | LookupResult R(*this, NameInfo, LookupOrdinaryName); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2881 | LookupTemplateName(R, (Scope*)nullptr, SS, QualType(), /*Entering*/ false, |
Douglas Gregor | 786123d | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 2882 | MemberOfUnknownSpecialization); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2883 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2884 | if (R.isAmbiguous()) |
| 2885 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2886 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2887 | if (R.empty()) { |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2888 | Diag(NameInfo.getLoc(), diag::err_template_kw_refers_to_non_template) |
| 2889 | << NameInfo.getName() << SS.getRange(); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2890 | return ExprError(); |
| 2891 | } |
| 2892 | |
| 2893 | if (ClassTemplateDecl *Temp = R.getAsSingle<ClassTemplateDecl>()) { |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2894 | Diag(NameInfo.getLoc(), diag::err_template_kw_refers_to_class_template) |
Aaron Ballman | 4a97967 | 2014-01-03 13:56:08 +0000 | [diff] [blame] | 2895 | << SS.getScopeRep() |
Reid Kleckner | 32506ed | 2014-06-12 23:03:48 +0000 | [diff] [blame] | 2896 | << NameInfo.getName().getAsString() << SS.getRange(); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2897 | Diag(Temp->getLocation(), diag::note_referenced_class_template); |
| 2898 | return ExprError(); |
| 2899 | } |
| 2900 | |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 2901 | return BuildTemplateIdExpr(SS, TemplateKWLoc, R, /*ADL*/ false, TemplateArgs); |
Douglas Gregor | a727cb9 | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 2902 | } |
| 2903 | |
Douglas Gregor | b67535d | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 2904 | /// \brief Form a dependent template name. |
| 2905 | /// |
| 2906 | /// This action forms a dependent template name given the template |
| 2907 | /// name and its (presumably dependent) scope specifier. For |
| 2908 | /// example, given "MetaFun::template apply", the scope specifier \p |
| 2909 | /// SS will be "MetaFun::", \p TemplateKWLoc contains the location |
| 2910 | /// of the "template" keyword, and "apply" is the \p Name. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2911 | TemplateNameKind Sema::ActOnDependentTemplateName(Scope *S, |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2912 | CXXScopeSpec &SS, |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 2913 | SourceLocation TemplateKWLoc, |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2914 | UnqualifiedId &Name, |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2915 | ParsedType ObjectType, |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2916 | bool EnteringContext, |
| 2917 | TemplateTy &Result) { |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 2918 | if (TemplateKWLoc.isValid() && S && !S->getTemplateParamParent()) |
| 2919 | Diag(TemplateKWLoc, |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 2920 | getLangOpts().CPlusPlus11 ? |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 2921 | diag::warn_cxx98_compat_template_outside_of_template : |
| 2922 | diag::ext_template_outside_of_template) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2923 | << FixItHint::CreateRemoval(TemplateKWLoc); |
| 2924 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2925 | DeclContext *LookupCtx = nullptr; |
Douglas Gregor | 9abe237 | 2010-01-19 16:01:07 +0000 | [diff] [blame] | 2926 | if (SS.isSet()) |
| 2927 | LookupCtx = computeDeclContext(SS, EnteringContext); |
| 2928 | if (!LookupCtx && ObjectType) |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2929 | LookupCtx = computeDeclContext(ObjectType.get()); |
Douglas Gregor | 9abe237 | 2010-01-19 16:01:07 +0000 | [diff] [blame] | 2930 | if (LookupCtx) { |
Douglas Gregor | b67535d | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 2931 | // C++0x [temp.names]p5: |
| 2932 | // If a name prefixed by the keyword template is not the name of |
| 2933 | // a template, the program is ill-formed. [Note: the keyword |
| 2934 | // template may not be applied to non-template members of class |
| 2935 | // templates. -end note ] [ Note: as is the case with the |
| 2936 | // typename prefix, the template prefix is allowed in cases |
| 2937 | // where it is not strictly necessary; i.e., when the |
| 2938 | // nested-name-specifier or the expression on the left of the -> |
| 2939 | // or . is not dependent on a template-parameter, or the use |
| 2940 | // does not appear in the scope of a template. -end note] |
| 2941 | // |
| 2942 | // Note: C++03 was more strict here, because it banned the use of |
| 2943 | // the "template" keyword prior to a template-name that was not a |
| 2944 | // dependent name. C++ DR468 relaxed this requirement (the |
| 2945 | // "template" keyword is now permitted). We follow the C++0x |
Douglas Gregor | c9d2682 | 2010-06-14 22:07:54 +0000 | [diff] [blame] | 2946 | // rules, even in C++03 mode with a warning, retroactively applying the DR. |
Douglas Gregor | 786123d | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 2947 | bool MemberOfUnknownSpecialization; |
Richard Smith | af41696 | 2012-11-15 00:31:27 +0000 | [diff] [blame] | 2948 | TemplateNameKind TNK = isTemplateName(S, SS, TemplateKWLoc.isValid(), Name, |
Abramo Bagnara | 7c5dee4 | 2010-08-06 12:11:11 +0000 | [diff] [blame] | 2949 | ObjectType, EnteringContext, Result, |
Douglas Gregor | 786123d | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 2950 | MemberOfUnknownSpecialization); |
Douglas Gregor | 9abe237 | 2010-01-19 16:01:07 +0000 | [diff] [blame] | 2951 | if (TNK == TNK_Non_template && LookupCtx->isDependentContext() && |
| 2952 | isa<CXXRecordDecl>(LookupCtx) && |
Douglas Gregor | 5ecbb1b | 2011-03-11 23:27:41 +0000 | [diff] [blame] | 2953 | (!cast<CXXRecordDecl>(LookupCtx)->hasDefinition() || |
| 2954 | cast<CXXRecordDecl>(LookupCtx)->hasAnyDependentBases())) { |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2955 | // This is a dependent template. Handle it below. |
Douglas Gregor | d2e6a45 | 2010-01-14 17:47:39 +0000 | [diff] [blame] | 2956 | } else if (TNK == TNK_Non_template) { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 2957 | Diag(Name.getLocStart(), |
Douglas Gregor | 3cf8131 | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 2958 | diag::err_template_kw_refers_to_non_template) |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2959 | << GetNameFromUnqualifiedId(Name).getName() |
Douglas Gregor | b22ee88 | 2010-05-05 05:58:24 +0000 | [diff] [blame] | 2960 | << Name.getSourceRange() |
| 2961 | << TemplateKWLoc; |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2962 | return TNK_Non_template; |
Douglas Gregor | d2e6a45 | 2010-01-14 17:47:39 +0000 | [diff] [blame] | 2963 | } else { |
| 2964 | // We found something; return it. |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2965 | return TNK; |
Douglas Gregor | b67535d | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 2966 | } |
Douglas Gregor | b67535d | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 2967 | } |
| 2968 | |
Aaron Ballman | 4a97967 | 2014-01-03 13:56:08 +0000 | [diff] [blame] | 2969 | NestedNameSpecifier *Qualifier = SS.getScopeRep(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2970 | |
Douglas Gregor | 3cf8131 | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 2971 | switch (Name.getKind()) { |
| 2972 | case UnqualifiedId::IK_Identifier: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2973 | Result = TemplateTy::make(Context.getDependentTemplateName(Qualifier, |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2974 | Name.Identifier)); |
| 2975 | return TNK_Dependent_template_name; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2976 | |
Douglas Gregor | 71395fa | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 2977 | case UnqualifiedId::IK_OperatorFunctionId: |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2978 | Result = TemplateTy::make(Context.getDependentTemplateName(Qualifier, |
Douglas Gregor | 71395fa | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 2979 | Name.OperatorFunctionId.Operator)); |
Richard Smith | 72bfbd8 | 2013-12-04 00:28:23 +0000 | [diff] [blame] | 2980 | return TNK_Function_template; |
Alexis Hunt | ed0530f | 2009-11-28 08:58:14 +0000 | [diff] [blame] | 2981 | |
| 2982 | case UnqualifiedId::IK_LiteralOperatorId: |
Richard Smith | d091dc1 | 2013-12-05 00:58:33 +0000 | [diff] [blame] | 2983 | llvm_unreachable("literal operator id cannot have a dependent scope"); |
Alexis Hunt | ed0530f | 2009-11-28 08:58:14 +0000 | [diff] [blame] | 2984 | |
Douglas Gregor | 3cf8131 | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 2985 | default: |
| 2986 | break; |
| 2987 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2988 | |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 2989 | Diag(Name.getLocStart(), |
Douglas Gregor | 3cf8131 | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 2990 | diag::err_template_kw_refers_to_non_template) |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2991 | << GetNameFromUnqualifiedId(Name).getName() |
Douglas Gregor | b22ee88 | 2010-05-05 05:58:24 +0000 | [diff] [blame] | 2992 | << Name.getSourceRange() |
| 2993 | << TemplateKWLoc; |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2994 | return TNK_Non_template; |
Douglas Gregor | b67535d | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 2995 | } |
| 2996 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2997 | bool Sema::CheckTemplateTypeArgument(TemplateTypeParmDecl *Param, |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 2998 | TemplateArgumentLoc &AL, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2999 | SmallVectorImpl<TemplateArgument> &Converted) { |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 3000 | const TemplateArgument &Arg = AL.getArgument(); |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 3001 | QualType ArgType; |
| 3002 | TypeSourceInfo *TSI = nullptr; |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 3003 | |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 3004 | // Check template type parameter. |
Jeffrey Yasskin | 823015d | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 3005 | switch(Arg.getKind()) { |
| 3006 | case TemplateArgument::Type: |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 3007 | // C++ [temp.arg.type]p1: |
| 3008 | // A template-argument for a template-parameter which is a |
| 3009 | // type shall be a type-id. |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 3010 | ArgType = Arg.getAsType(); |
| 3011 | TSI = AL.getTypeSourceInfo(); |
Jeffrey Yasskin | 823015d | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 3012 | break; |
| 3013 | case TemplateArgument::Template: { |
| 3014 | // We have a template type parameter but the template argument |
| 3015 | // is a template without any arguments. |
| 3016 | SourceRange SR = AL.getSourceRange(); |
| 3017 | TemplateName Name = Arg.getAsTemplate(); |
| 3018 | Diag(SR.getBegin(), diag::err_template_missing_args) |
| 3019 | << Name << SR; |
| 3020 | if (TemplateDecl *Decl = Name.getAsTemplateDecl()) |
| 3021 | Diag(Decl->getLocation(), diag::note_template_decl_here); |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 3022 | |
Jeffrey Yasskin | 823015d | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 3023 | return true; |
| 3024 | } |
Kaelyn Uhrain | 864d0b0 | 2012-05-18 23:42:49 +0000 | [diff] [blame] | 3025 | case TemplateArgument::Expression: { |
| 3026 | // We have a template type parameter but the template argument is an |
| 3027 | // expression; see if maybe it is missing the "typename" keyword. |
| 3028 | CXXScopeSpec SS; |
| 3029 | DeclarationNameInfo NameInfo; |
| 3030 | |
| 3031 | if (DeclRefExpr *ArgExpr = dyn_cast<DeclRefExpr>(Arg.getAsExpr())) { |
| 3032 | SS.Adopt(ArgExpr->getQualifierLoc()); |
| 3033 | NameInfo = ArgExpr->getNameInfo(); |
| 3034 | } else if (DependentScopeDeclRefExpr *ArgExpr = |
| 3035 | dyn_cast<DependentScopeDeclRefExpr>(Arg.getAsExpr())) { |
| 3036 | SS.Adopt(ArgExpr->getQualifierLoc()); |
| 3037 | NameInfo = ArgExpr->getNameInfo(); |
| 3038 | } else if (CXXDependentScopeMemberExpr *ArgExpr = |
| 3039 | dyn_cast<CXXDependentScopeMemberExpr>(Arg.getAsExpr())) { |
Kaelyn Uhrain | 055e947 | 2012-06-08 01:07:26 +0000 | [diff] [blame] | 3040 | if (ArgExpr->isImplicitAccess()) { |
| 3041 | SS.Adopt(ArgExpr->getQualifierLoc()); |
| 3042 | NameInfo = ArgExpr->getMemberNameInfo(); |
| 3043 | } |
Kaelyn Uhrain | 864d0b0 | 2012-05-18 23:42:49 +0000 | [diff] [blame] | 3044 | } |
| 3045 | |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 3046 | if (auto *II = NameInfo.getName().getAsIdentifierInfo()) { |
Kaelyn Uhrain | 864d0b0 | 2012-05-18 23:42:49 +0000 | [diff] [blame] | 3047 | LookupResult Result(*this, NameInfo, LookupOrdinaryName); |
| 3048 | LookupParsedName(Result, CurScope, &SS); |
| 3049 | |
Kaelyn Uhrain | 055e947 | 2012-06-08 01:07:26 +0000 | [diff] [blame] | 3050 | if (Result.getAsSingle<TypeDecl>() || |
| 3051 | Result.getResultKind() == |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 3052 | LookupResult::NotFoundInCurrentInstantiation) { |
| 3053 | // Suggest that the user add 'typename' before the NNS. |
Kaelyn Uhrain | 864d0b0 | 2012-05-18 23:42:49 +0000 | [diff] [blame] | 3054 | SourceLocation Loc = AL.getSourceRange().getBegin(); |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 3055 | Diag(Loc, getLangOpts().MSVCCompat |
| 3056 | ? diag::ext_ms_template_type_arg_missing_typename |
| 3057 | : diag::err_template_arg_must_be_type_suggest) |
| 3058 | << FixItHint::CreateInsertion(Loc, "typename "); |
Kaelyn Uhrain | 864d0b0 | 2012-05-18 23:42:49 +0000 | [diff] [blame] | 3059 | Diag(Param->getLocation(), diag::note_template_param_here); |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 3060 | |
| 3061 | // Recover by synthesizing a type using the location information that we |
| 3062 | // already have. |
| 3063 | ArgType = |
| 3064 | Context.getDependentNameType(ETK_Typename, SS.getScopeRep(), II); |
| 3065 | TypeLocBuilder TLB; |
| 3066 | DependentNameTypeLoc TL = TLB.push<DependentNameTypeLoc>(ArgType); |
| 3067 | TL.setElaboratedKeywordLoc(SourceLocation(/*synthesized*/)); |
| 3068 | TL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 3069 | TL.setNameLoc(NameInfo.getLoc()); |
| 3070 | TSI = TLB.getTypeSourceInfo(Context, ArgType); |
| 3071 | |
| 3072 | // Overwrite our input TemplateArgumentLoc so that we can recover |
| 3073 | // properly. |
| 3074 | AL = TemplateArgumentLoc(TemplateArgument(ArgType), |
| 3075 | TemplateArgumentLocInfo(TSI)); |
| 3076 | |
| 3077 | break; |
Kaelyn Uhrain | 864d0b0 | 2012-05-18 23:42:49 +0000 | [diff] [blame] | 3078 | } |
| 3079 | } |
| 3080 | // fallthrough |
| 3081 | } |
Jeffrey Yasskin | 823015d | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 3082 | default: { |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 3083 | // We have a template type parameter but the template argument |
| 3084 | // is not a type. |
John McCall | 0d07eb3 | 2009-10-29 18:45:58 +0000 | [diff] [blame] | 3085 | SourceRange SR = AL.getSourceRange(); |
| 3086 | Diag(SR.getBegin(), diag::err_template_arg_must_be_type) << SR; |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 3087 | Diag(Param->getLocation(), diag::note_template_param_here); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3088 | |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 3089 | return true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3090 | } |
Jeffrey Yasskin | 823015d | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 3091 | } |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 3092 | |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 3093 | if (CheckTemplateArgument(Param, TSI)) |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 3094 | return true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3095 | |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 3096 | // Add the converted template type argument. |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 3097 | ArgType = Context.getCanonicalType(ArgType); |
Douglas Gregor | e46db90 | 2011-06-17 22:11:49 +0000 | [diff] [blame] | 3098 | |
| 3099 | // Objective-C ARC: |
| 3100 | // If an explicitly-specified template argument type is a lifetime type |
| 3101 | // with no lifetime qualifier, the __strong lifetime qualifier is inferred. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3102 | if (getLangOpts().ObjCAutoRefCount && |
Douglas Gregor | e46db90 | 2011-06-17 22:11:49 +0000 | [diff] [blame] | 3103 | ArgType->isObjCLifetimeType() && |
| 3104 | !ArgType.getObjCLifetime()) { |
| 3105 | Qualifiers Qs; |
| 3106 | Qs.setObjCLifetime(Qualifiers::OCL_Strong); |
| 3107 | ArgType = Context.getQualifiedType(ArgType, Qs); |
| 3108 | } |
| 3109 | |
| 3110 | Converted.push_back(TemplateArgument(ArgType)); |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 3111 | return false; |
| 3112 | } |
| 3113 | |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3114 | /// \brief Substitute template arguments into the default template argument for |
| 3115 | /// the given template type parameter. |
| 3116 | /// |
| 3117 | /// \param SemaRef the semantic analysis object for which we are performing |
| 3118 | /// the substitution. |
| 3119 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3120 | /// \param Template the template that we are synthesizing template arguments |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3121 | /// for. |
| 3122 | /// |
| 3123 | /// \param TemplateLoc the location of the template name that started the |
| 3124 | /// template-id we are checking. |
| 3125 | /// |
| 3126 | /// \param RAngleLoc the location of the right angle bracket ('>') that |
| 3127 | /// terminates the template-id. |
| 3128 | /// |
| 3129 | /// \param Param the template template parameter whose default we are |
| 3130 | /// substituting into. |
| 3131 | /// |
| 3132 | /// \param Converted the list of template arguments provided for template |
| 3133 | /// parameters that precede \p Param in the template parameter list. |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3134 | /// \returns the substituted template argument, or NULL if an error occurred. |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3135 | static TypeSourceInfo * |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3136 | SubstDefaultTemplateArgument(Sema &SemaRef, |
| 3137 | TemplateDecl *Template, |
| 3138 | SourceLocation TemplateLoc, |
| 3139 | SourceLocation RAngleLoc, |
| 3140 | TemplateTypeParmDecl *Param, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3141 | SmallVectorImpl<TemplateArgument> &Converted) { |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3142 | TypeSourceInfo *ArgType = Param->getDefaultArgumentInfo(); |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3143 | |
| 3144 | // If the argument type is dependent, instantiate it now based |
| 3145 | // on the previously-computed template arguments. |
| 3146 | if (ArgType->getType()->isDependentType()) { |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3147 | Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc, |
Richard Smith | 8093465 | 2012-07-16 01:09:10 +0000 | [diff] [blame] | 3148 | Template, Converted, |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3149 | SourceRange(TemplateLoc, RAngleLoc)); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 3150 | if (Inst.isInvalid()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3151 | return nullptr; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3152 | |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 3153 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
| 3154 | Converted.data(), Converted.size()); |
| 3155 | |
| 3156 | // Only substitute for the innermost template argument list. |
| 3157 | MultiLevelTemplateArgumentList TemplateArgLists; |
| 3158 | TemplateArgLists.addOuterTemplateArguments(&TemplateArgs); |
| 3159 | for (unsigned i = 0, e = Param->getDepth(); i != e; ++i) |
| 3160 | TemplateArgLists.addOuterTemplateArguments(None); |
| 3161 | |
Argyrios Kyrtzidis | 6fe744c | 2012-04-25 18:39:17 +0000 | [diff] [blame] | 3162 | Sema::ContextRAII SavedContext(SemaRef, Template->getDeclContext()); |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 3163 | ArgType = |
| 3164 | SemaRef.SubstType(ArgType, TemplateArgLists, |
| 3165 | Param->getDefaultArgumentLoc(), Param->getDeclName()); |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3166 | } |
| 3167 | |
| 3168 | return ArgType; |
| 3169 | } |
| 3170 | |
| 3171 | /// \brief Substitute template arguments into the default template argument for |
| 3172 | /// the given non-type template parameter. |
| 3173 | /// |
| 3174 | /// \param SemaRef the semantic analysis object for which we are performing |
| 3175 | /// the substitution. |
| 3176 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3177 | /// \param Template the template that we are synthesizing template arguments |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3178 | /// for. |
| 3179 | /// |
| 3180 | /// \param TemplateLoc the location of the template name that started the |
| 3181 | /// template-id we are checking. |
| 3182 | /// |
| 3183 | /// \param RAngleLoc the location of the right angle bracket ('>') that |
| 3184 | /// terminates the template-id. |
| 3185 | /// |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 3186 | /// \param Param the non-type template parameter whose default we are |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3187 | /// substituting into. |
| 3188 | /// |
| 3189 | /// \param Converted the list of template arguments provided for template |
| 3190 | /// parameters that precede \p Param in the template parameter list. |
| 3191 | /// |
| 3192 | /// \returns the substituted template argument, or NULL if an error occurred. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3193 | static ExprResult |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3194 | SubstDefaultTemplateArgument(Sema &SemaRef, |
| 3195 | TemplateDecl *Template, |
| 3196 | SourceLocation TemplateLoc, |
| 3197 | SourceLocation RAngleLoc, |
| 3198 | NonTypeTemplateParmDecl *Param, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3199 | SmallVectorImpl<TemplateArgument> &Converted) { |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3200 | Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc, |
Richard Smith | 8093465 | 2012-07-16 01:09:10 +0000 | [diff] [blame] | 3201 | Template, Converted, |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3202 | SourceRange(TemplateLoc, RAngleLoc)); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 3203 | if (Inst.isInvalid()) |
Richard Smith | 8a874c9 | 2012-07-08 02:38:24 +0000 | [diff] [blame] | 3204 | return ExprError(); |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3205 | |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 3206 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
| 3207 | Converted.data(), Converted.size()); |
| 3208 | |
| 3209 | // Only substitute for the innermost template argument list. |
| 3210 | MultiLevelTemplateArgumentList TemplateArgLists; |
| 3211 | TemplateArgLists.addOuterTemplateArguments(&TemplateArgs); |
| 3212 | for (unsigned i = 0, e = Param->getDepth(); i != e; ++i) |
| 3213 | TemplateArgLists.addOuterTemplateArguments(None); |
| 3214 | |
Argyrios Kyrtzidis | 6fe744c | 2012-04-25 18:39:17 +0000 | [diff] [blame] | 3215 | Sema::ContextRAII SavedContext(SemaRef, Template->getDeclContext()); |
Eli Friedman | c25372b | 2012-04-26 22:43:24 +0000 | [diff] [blame] | 3216 | EnterExpressionEvaluationContext Unevaluated(SemaRef, Sema::Unevaluated); |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 3217 | return SemaRef.SubstExpr(Param->getDefaultArgument(), TemplateArgLists); |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3218 | } |
| 3219 | |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 3220 | /// \brief Substitute template arguments into the default template argument for |
| 3221 | /// the given template template parameter. |
| 3222 | /// |
| 3223 | /// \param SemaRef the semantic analysis object for which we are performing |
| 3224 | /// the substitution. |
| 3225 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3226 | /// \param Template the template that we are synthesizing template arguments |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 3227 | /// for. |
| 3228 | /// |
| 3229 | /// \param TemplateLoc the location of the template name that started the |
| 3230 | /// template-id we are checking. |
| 3231 | /// |
| 3232 | /// \param RAngleLoc the location of the right angle bracket ('>') that |
| 3233 | /// terminates the template-id. |
| 3234 | /// |
| 3235 | /// \param Param the template template parameter whose default we are |
| 3236 | /// substituting into. |
| 3237 | /// |
| 3238 | /// \param Converted the list of template arguments provided for template |
| 3239 | /// parameters that precede \p Param in the template parameter list. |
| 3240 | /// |
Douglas Gregor | df846d1 | 2011-03-02 18:46:51 +0000 | [diff] [blame] | 3241 | /// \param QualifierLoc Will be set to the nested-name-specifier (with |
| 3242 | /// source-location information) that precedes the template name. |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 3243 | /// |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 3244 | /// \returns the substituted template argument, or NULL if an error occurred. |
| 3245 | static TemplateName |
| 3246 | SubstDefaultTemplateArgument(Sema &SemaRef, |
| 3247 | TemplateDecl *Template, |
| 3248 | SourceLocation TemplateLoc, |
| 3249 | SourceLocation RAngleLoc, |
| 3250 | TemplateTemplateParmDecl *Param, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3251 | SmallVectorImpl<TemplateArgument> &Converted, |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 3252 | NestedNameSpecifierLoc &QualifierLoc) { |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 3253 | Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc, Template, Converted, |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 3254 | SourceRange(TemplateLoc, RAngleLoc)); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 3255 | if (Inst.isInvalid()) |
Richard Smith | 8a874c9 | 2012-07-08 02:38:24 +0000 | [diff] [blame] | 3256 | return TemplateName(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3257 | |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 3258 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
| 3259 | Converted.data(), Converted.size()); |
| 3260 | |
| 3261 | // Only substitute for the innermost template argument list. |
| 3262 | MultiLevelTemplateArgumentList TemplateArgLists; |
| 3263 | TemplateArgLists.addOuterTemplateArguments(&TemplateArgs); |
| 3264 | for (unsigned i = 0, e = Param->getDepth(); i != e; ++i) |
| 3265 | TemplateArgLists.addOuterTemplateArguments(None); |
| 3266 | |
Argyrios Kyrtzidis | 6fe744c | 2012-04-25 18:39:17 +0000 | [diff] [blame] | 3267 | Sema::ContextRAII SavedContext(SemaRef, Template->getDeclContext()); |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 3268 | // Substitute into the nested-name-specifier first, |
Douglas Gregor | df846d1 | 2011-03-02 18:46:51 +0000 | [diff] [blame] | 3269 | QualifierLoc = Param->getDefaultArgument().getTemplateQualifierLoc(); |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 3270 | if (QualifierLoc) { |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 3271 | QualifierLoc = |
| 3272 | SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc, TemplateArgLists); |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 3273 | if (!QualifierLoc) |
| 3274 | return TemplateName(); |
| 3275 | } |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 3276 | |
| 3277 | return SemaRef.SubstTemplateName( |
| 3278 | QualifierLoc, |
| 3279 | Param->getDefaultArgument().getArgument().getAsTemplate(), |
| 3280 | Param->getDefaultArgument().getTemplateNameLoc(), |
| 3281 | TemplateArgLists); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 3282 | } |
| 3283 | |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 3284 | /// \brief If the given template parameter has a default template |
| 3285 | /// argument, substitute into that default template argument and |
| 3286 | /// return the corresponding template argument. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3287 | TemplateArgumentLoc |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 3288 | Sema::SubstDefaultTemplateArgumentIfAvailable(TemplateDecl *Template, |
| 3289 | SourceLocation TemplateLoc, |
| 3290 | SourceLocation RAngleLoc, |
| 3291 | Decl *Param, |
Richard Smith | c87b938 | 2013-07-04 01:01:24 +0000 | [diff] [blame] | 3292 | SmallVectorImpl<TemplateArgument> |
| 3293 | &Converted, |
| 3294 | bool &HasDefaultArg) { |
| 3295 | HasDefaultArg = false; |
| 3296 | |
| 3297 | if (TemplateTypeParmDecl *TypeParm = dyn_cast<TemplateTypeParmDecl>(Param)) { |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 3298 | if (!TypeParm->hasDefaultArgument()) |
| 3299 | return TemplateArgumentLoc(); |
| 3300 | |
Richard Smith | c87b938 | 2013-07-04 01:01:24 +0000 | [diff] [blame] | 3301 | HasDefaultArg = true; |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3302 | TypeSourceInfo *DI = SubstDefaultTemplateArgument(*this, Template, |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 3303 | TemplateLoc, |
| 3304 | RAngleLoc, |
| 3305 | TypeParm, |
| 3306 | Converted); |
| 3307 | if (DI) |
| 3308 | return TemplateArgumentLoc(TemplateArgument(DI->getType()), DI); |
| 3309 | |
| 3310 | return TemplateArgumentLoc(); |
| 3311 | } |
| 3312 | |
| 3313 | if (NonTypeTemplateParmDecl *NonTypeParm |
| 3314 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
| 3315 | if (!NonTypeParm->hasDefaultArgument()) |
| 3316 | return TemplateArgumentLoc(); |
| 3317 | |
Richard Smith | c87b938 | 2013-07-04 01:01:24 +0000 | [diff] [blame] | 3318 | HasDefaultArg = true; |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3319 | ExprResult Arg = SubstDefaultTemplateArgument(*this, Template, |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 3320 | TemplateLoc, |
| 3321 | RAngleLoc, |
| 3322 | NonTypeParm, |
| 3323 | Converted); |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 3324 | if (Arg.isInvalid()) |
| 3325 | return TemplateArgumentLoc(); |
| 3326 | |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3327 | Expr *ArgE = Arg.getAs<Expr>(); |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 3328 | return TemplateArgumentLoc(TemplateArgument(ArgE), ArgE); |
| 3329 | } |
| 3330 | |
| 3331 | TemplateTemplateParmDecl *TempTempParm |
| 3332 | = cast<TemplateTemplateParmDecl>(Param); |
| 3333 | if (!TempTempParm->hasDefaultArgument()) |
| 3334 | return TemplateArgumentLoc(); |
| 3335 | |
Richard Smith | c87b938 | 2013-07-04 01:01:24 +0000 | [diff] [blame] | 3336 | HasDefaultArg = true; |
Douglas Gregor | df846d1 | 2011-03-02 18:46:51 +0000 | [diff] [blame] | 3337 | NestedNameSpecifierLoc QualifierLoc; |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 3338 | TemplateName TName = SubstDefaultTemplateArgument(*this, Template, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3339 | TemplateLoc, |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 3340 | RAngleLoc, |
| 3341 | TempTempParm, |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 3342 | Converted, |
| 3343 | QualifierLoc); |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 3344 | if (TName.isNull()) |
| 3345 | return TemplateArgumentLoc(); |
| 3346 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3347 | return TemplateArgumentLoc(TemplateArgument(TName), |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 3348 | TempTempParm->getDefaultArgument().getTemplateQualifierLoc(), |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 3349 | TempTempParm->getDefaultArgument().getTemplateNameLoc()); |
| 3350 | } |
| 3351 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3352 | /// \brief Check that the given template argument corresponds to the given |
| 3353 | /// template parameter. |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 3354 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3355 | /// \param Param The template parameter against which the argument will be |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 3356 | /// checked. |
| 3357 | /// |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 3358 | /// \param Arg The template argument, which may be updated due to conversions. |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 3359 | /// |
| 3360 | /// \param Template The template in which the template argument resides. |
| 3361 | /// |
| 3362 | /// \param TemplateLoc The location of the template name for the template |
| 3363 | /// whose argument list we're matching. |
| 3364 | /// |
| 3365 | /// \param RAngleLoc The location of the right angle bracket ('>') that closes |
| 3366 | /// the template argument list. |
| 3367 | /// |
| 3368 | /// \param ArgumentPackIndex The index into the argument pack where this |
| 3369 | /// argument will be placed. Only valid if the parameter is a parameter pack. |
| 3370 | /// |
| 3371 | /// \param Converted The checked, converted argument will be added to the |
| 3372 | /// end of this small vector. |
| 3373 | /// |
| 3374 | /// \param CTAK Describes how we arrived at this particular template argument: |
| 3375 | /// explicitly written, deduced, etc. |
| 3376 | /// |
| 3377 | /// \returns true on error, false otherwise. |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3378 | bool Sema::CheckTemplateArgument(NamedDecl *Param, |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 3379 | TemplateArgumentLoc &Arg, |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 3380 | NamedDecl *Template, |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3381 | SourceLocation TemplateLoc, |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3382 | SourceLocation RAngleLoc, |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 3383 | unsigned ArgumentPackIndex, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3384 | SmallVectorImpl<TemplateArgument> &Converted, |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3385 | CheckTemplateArgumentKind CTAK) { |
Douglas Gregor | eebed72 | 2009-11-11 19:41:09 +0000 | [diff] [blame] | 3386 | // Check template type parameters. |
| 3387 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3388 | return CheckTemplateTypeArgument(TTP, Arg, Converted); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3389 | |
Douglas Gregor | eebed72 | 2009-11-11 19:41:09 +0000 | [diff] [blame] | 3390 | // Check non-type template parameters. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3391 | if (NonTypeTemplateParmDecl *NTTP =dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3392 | // Do substitution on the type of the non-type template parameter |
Peter Collingbourne | 0168763 | 2010-12-10 17:08:53 +0000 | [diff] [blame] | 3393 | // with the template arguments we've seen thus far. But if the |
| 3394 | // template has a dependent context then we cannot substitute yet. |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3395 | QualType NTTPType = NTTP->getType(); |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 3396 | if (NTTP->isParameterPack() && NTTP->isExpandedParameterPack()) |
| 3397 | NTTPType = NTTP->getExpansionType(ArgumentPackIndex); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3398 | |
Peter Collingbourne | 0168763 | 2010-12-10 17:08:53 +0000 | [diff] [blame] | 3399 | if (NTTPType->isDependentType() && |
| 3400 | !isa<TemplateTemplateParmDecl>(Template) && |
| 3401 | !Template->getDeclContext()->isDependentContext()) { |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3402 | // Do substitution on the type of the non-type template parameter. |
| 3403 | InstantiatingTemplate Inst(*this, TemplateLoc, Template, |
Richard Smith | 8093465 | 2012-07-16 01:09:10 +0000 | [diff] [blame] | 3404 | NTTP, Converted, |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3405 | SourceRange(TemplateLoc, RAngleLoc)); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 3406 | if (Inst.isInvalid()) |
Richard Smith | 8a874c9 | 2012-07-08 02:38:24 +0000 | [diff] [blame] | 3407 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3408 | |
| 3409 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 3410 | Converted.data(), Converted.size()); |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3411 | NTTPType = SubstType(NTTPType, |
| 3412 | MultiLevelTemplateArgumentList(TemplateArgs), |
| 3413 | NTTP->getLocation(), |
| 3414 | NTTP->getDeclName()); |
| 3415 | // If that worked, check the non-type template parameter type |
| 3416 | // for validity. |
| 3417 | if (!NTTPType.isNull()) |
| 3418 | NTTPType = CheckNonTypeTemplateParameterType(NTTPType, |
| 3419 | NTTP->getLocation()); |
| 3420 | if (NTTPType.isNull()) |
| 3421 | return true; |
| 3422 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3423 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3424 | switch (Arg.getArgument().getKind()) { |
| 3425 | case TemplateArgument::Null: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3426 | llvm_unreachable("Should never see a NULL template argument here"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3427 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3428 | case TemplateArgument::Expression: { |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3429 | TemplateArgument Result; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3430 | ExprResult Res = |
| 3431 | CheckTemplateArgument(NTTP, NTTPType, Arg.getArgument().getAsExpr(), |
| 3432 | Result, CTAK); |
| 3433 | if (Res.isInvalid()) |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3434 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3435 | |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 3436 | // If the resulting expression is new, then use it in place of the |
| 3437 | // old expression in the template argument. |
| 3438 | if (Res.get() != Arg.getArgument().getAsExpr()) { |
| 3439 | TemplateArgument TA(Res.get()); |
| 3440 | Arg = TemplateArgumentLoc(TA, Res.get()); |
| 3441 | } |
| 3442 | |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 3443 | Converted.push_back(Result); |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3444 | break; |
| 3445 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3446 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3447 | case TemplateArgument::Declaration: |
| 3448 | case TemplateArgument::Integral: |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 3449 | case TemplateArgument::NullPtr: |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3450 | // We've already checked this template argument, so just copy |
| 3451 | // it to the list of converted arguments. |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 3452 | Converted.push_back(Arg.getArgument()); |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3453 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3454 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3455 | case TemplateArgument::Template: |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 3456 | case TemplateArgument::TemplateExpansion: |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3457 | // We were given a template template argument. It may not be ill-formed; |
| 3458 | // see below. |
| 3459 | if (DependentTemplateName *DTN |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 3460 | = Arg.getArgument().getAsTemplateOrTemplatePattern() |
| 3461 | .getAsDependentTemplateName()) { |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3462 | // We have a template argument such as \c T::template X, which we |
| 3463 | // parsed as a template template argument. However, since we now |
| 3464 | // know that we need a non-type template argument, convert this |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3465 | // template name into an expression. |
| 3466 | |
| 3467 | DeclarationNameInfo NameInfo(DTN->getIdentifier(), |
| 3468 | Arg.getTemplateNameLoc()); |
| 3469 | |
Douglas Gregor | 3a43fd6 | 2011-02-25 20:49:16 +0000 | [diff] [blame] | 3470 | CXXScopeSpec SS; |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 3471 | SS.Adopt(Arg.getTemplateQualifierLoc()); |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 3472 | // FIXME: the template-template arg was a DependentTemplateName, |
| 3473 | // so it was provided with a template keyword. However, its source |
| 3474 | // location is not stored in the template argument structure. |
| 3475 | SourceLocation TemplateKWLoc; |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 3476 | ExprResult E = DependentScopeDeclRefExpr::Create( |
| 3477 | Context, SS.getWithLocInContext(Context), TemplateKWLoc, NameInfo, |
| 3478 | nullptr); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3479 | |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 3480 | // If we parsed the template argument as a pack expansion, create a |
| 3481 | // pack expansion expression. |
| 3482 | if (Arg.getArgument().getKind() == TemplateArgument::TemplateExpansion){ |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3483 | E = ActOnPackExpansion(E.get(), Arg.getTemplateEllipsisLoc()); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3484 | if (E.isInvalid()) |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 3485 | return true; |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 3486 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3487 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3488 | TemplateArgument Result; |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3489 | E = CheckTemplateArgument(NTTP, NTTPType, E.get(), Result); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3490 | if (E.isInvalid()) |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3491 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3492 | |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 3493 | Converted.push_back(Result); |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3494 | break; |
| 3495 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3496 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3497 | // We have a template argument that actually does refer to a class |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3498 | // template, alias template, or template template parameter, and |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3499 | // therefore cannot be a non-type template argument. |
| 3500 | Diag(Arg.getLocation(), diag::err_template_arg_must_be_expr) |
| 3501 | << Arg.getSourceRange(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3502 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3503 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 3504 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3505 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3506 | case TemplateArgument::Type: { |
| 3507 | // We have a non-type template parameter but the template |
| 3508 | // argument is a type. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3509 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3510 | // C++ [temp.arg]p2: |
| 3511 | // In a template-argument, an ambiguity between a type-id and |
| 3512 | // an expression is resolved to a type-id, regardless of the |
| 3513 | // form of the corresponding template-parameter. |
| 3514 | // |
| 3515 | // We warn specifically about this case, since it can be rather |
| 3516 | // confusing for users. |
| 3517 | QualType T = Arg.getArgument().getAsType(); |
| 3518 | SourceRange SR = Arg.getSourceRange(); |
| 3519 | if (T->isFunctionType()) |
| 3520 | Diag(SR.getBegin(), diag::err_template_arg_nontype_ambig) << SR << T; |
| 3521 | else |
| 3522 | Diag(SR.getBegin(), diag::err_template_arg_must_be_expr) << SR; |
| 3523 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 3524 | return true; |
| 3525 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3526 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3527 | case TemplateArgument::Pack: |
Jeffrey Yasskin | 1615d45 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 3528 | llvm_unreachable("Caller must expand template argument packs"); |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3529 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3530 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3531 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3532 | } |
| 3533 | |
| 3534 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3535 | // Check template template parameters. |
| 3536 | TemplateTemplateParmDecl *TempParm = cast<TemplateTemplateParmDecl>(Param); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3537 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3538 | // Substitute into the template parameter list of the template |
| 3539 | // template parameter, since previously-supplied template arguments |
| 3540 | // may appear within the template template parameter. |
| 3541 | { |
| 3542 | // Set up a template instantiation context. |
| 3543 | LocalInstantiationScope Scope(*this); |
| 3544 | InstantiatingTemplate Inst(*this, TemplateLoc, Template, |
Richard Smith | 8093465 | 2012-07-16 01:09:10 +0000 | [diff] [blame] | 3545 | TempParm, Converted, |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3546 | SourceRange(TemplateLoc, RAngleLoc)); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 3547 | if (Inst.isInvalid()) |
Richard Smith | 8a874c9 | 2012-07-08 02:38:24 +0000 | [diff] [blame] | 3548 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3549 | |
| 3550 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 3551 | Converted.data(), Converted.size()); |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3552 | TempParm = cast_or_null<TemplateTemplateParmDecl>( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3553 | SubstDecl(TempParm, CurContext, |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3554 | MultiLevelTemplateArgumentList(TemplateArgs))); |
| 3555 | if (!TempParm) |
| 3556 | return true; |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3557 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3558 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3559 | switch (Arg.getArgument().getKind()) { |
| 3560 | case TemplateArgument::Null: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3561 | llvm_unreachable("Should never see a NULL template argument here"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3562 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3563 | case TemplateArgument::Template: |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 3564 | case TemplateArgument::TemplateExpansion: |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3565 | if (CheckTemplateArgument(TempParm, Arg, ArgumentPackIndex)) |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3566 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3567 | |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 3568 | Converted.push_back(Arg.getArgument()); |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3569 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3570 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3571 | case TemplateArgument::Expression: |
| 3572 | case TemplateArgument::Type: |
| 3573 | // We have a template template parameter but the template |
| 3574 | // argument does not refer to a template. |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3575 | Diag(Arg.getLocation(), diag::err_template_arg_must_be_template) |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3576 | << getLangOpts().CPlusPlus11; |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3577 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3578 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3579 | case TemplateArgument::Declaration: |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3580 | llvm_unreachable("Declaration argument with template template parameter"); |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3581 | case TemplateArgument::Integral: |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3582 | llvm_unreachable("Integral argument with template template parameter"); |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 3583 | case TemplateArgument::NullPtr: |
| 3584 | llvm_unreachable("Null pointer argument with template template parameter"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3585 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3586 | case TemplateArgument::Pack: |
Jeffrey Yasskin | 1615d45 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 3587 | llvm_unreachable("Caller must expand template argument packs"); |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3588 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3589 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3590 | return false; |
| 3591 | } |
| 3592 | |
Douglas Gregor | 8e07261 | 2012-02-03 07:34:46 +0000 | [diff] [blame] | 3593 | /// \brief Diagnose an arity mismatch in the |
| 3594 | static bool diagnoseArityMismatch(Sema &S, TemplateDecl *Template, |
| 3595 | SourceLocation TemplateLoc, |
| 3596 | TemplateArgumentListInfo &TemplateArgs) { |
| 3597 | TemplateParameterList *Params = Template->getTemplateParameters(); |
| 3598 | unsigned NumParams = Params->size(); |
| 3599 | unsigned NumArgs = TemplateArgs.size(); |
| 3600 | |
| 3601 | SourceRange Range; |
| 3602 | if (NumArgs > NumParams) |
| 3603 | Range = SourceRange(TemplateArgs[NumParams].getLocation(), |
| 3604 | TemplateArgs.getRAngleLoc()); |
| 3605 | S.Diag(TemplateLoc, diag::err_template_arg_list_different_arity) |
| 3606 | << (NumArgs > NumParams) |
| 3607 | << (isa<ClassTemplateDecl>(Template)? 0 : |
| 3608 | isa<FunctionTemplateDecl>(Template)? 1 : |
| 3609 | isa<TemplateTemplateParmDecl>(Template)? 2 : 3) |
| 3610 | << Template << Range; |
| 3611 | S.Diag(Template->getLocation(), diag::note_template_decl_here) |
| 3612 | << Params->getSourceRange(); |
| 3613 | return true; |
| 3614 | } |
| 3615 | |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3616 | /// \brief Check whether the template parameter is a pack expansion, and if so, |
| 3617 | /// determine the number of parameters produced by that expansion. For instance: |
| 3618 | /// |
| 3619 | /// \code |
| 3620 | /// template<typename ...Ts> struct A { |
| 3621 | /// template<Ts ...NTs, template<Ts> class ...TTs, typename ...Us> struct B; |
| 3622 | /// }; |
| 3623 | /// \endcode |
| 3624 | /// |
| 3625 | /// In \c A<int,int>::B, \c NTs and \c TTs have expanded pack size 2, and \c Us |
| 3626 | /// is not a pack expansion, so returns an empty Optional. |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 3627 | static Optional<unsigned> getExpandedPackSize(NamedDecl *Param) { |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3628 | if (NonTypeTemplateParmDecl *NTTP |
| 3629 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
| 3630 | if (NTTP->isExpandedParameterPack()) |
| 3631 | return NTTP->getNumExpansionTypes(); |
| 3632 | } |
| 3633 | |
| 3634 | if (TemplateTemplateParmDecl *TTP |
| 3635 | = dyn_cast<TemplateTemplateParmDecl>(Param)) { |
| 3636 | if (TTP->isExpandedParameterPack()) |
| 3637 | return TTP->getNumExpansionTemplateParameters(); |
| 3638 | } |
| 3639 | |
David Blaikie | 7a30dc5 | 2013-02-21 01:47:18 +0000 | [diff] [blame] | 3640 | return None; |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3641 | } |
| 3642 | |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3643 | /// \brief Check that the given template argument list is well-formed |
| 3644 | /// for specializing the given template. |
| 3645 | bool Sema::CheckTemplateArgumentList(TemplateDecl *Template, |
| 3646 | SourceLocation TemplateLoc, |
Douglas Gregor | 739b107a | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 3647 | TemplateArgumentListInfo &TemplateArgs, |
Douglas Gregor | e3f1f35 | 2009-07-01 00:28:38 +0000 | [diff] [blame] | 3648 | bool PartialTemplateArgs, |
Richard Smith | 83b11aa | 2014-01-09 02:22:22 +0000 | [diff] [blame] | 3649 | SmallVectorImpl<TemplateArgument> &Converted) { |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 3650 | // Make a copy of the template arguments for processing. Only make the |
| 3651 | // changes at the end when successful in matching the arguments to the |
| 3652 | // template. |
| 3653 | TemplateArgumentListInfo NewArgs = TemplateArgs; |
| 3654 | |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3655 | TemplateParameterList *Params = Template->getTemplateParameters(); |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3656 | |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 3657 | SourceLocation RAngleLoc = NewArgs.getRAngleLoc(); |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 3658 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3659 | // C++ [temp.arg]p1: |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3660 | // [...] The type and form of each template-argument specified in |
| 3661 | // a template-id shall match the type and form specified for the |
| 3662 | // corresponding parameter declared by the template in its |
| 3663 | // template-parameter-list. |
Douglas Gregor | 739b107a | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 3664 | bool isTemplateTemplateParameter = isa<TemplateTemplateParmDecl>(Template); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3665 | SmallVector<TemplateArgument, 2> ArgumentPack; |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 3666 | unsigned ArgIdx = 0, NumArgs = NewArgs.size(); |
Douglas Gregor | f143cd5 | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 3667 | LocalInstantiationScope InstScope(*this, true); |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3668 | for (TemplateParameterList::iterator Param = Params->begin(), |
| 3669 | ParamEnd = Params->end(); |
| 3670 | Param != ParamEnd; /* increment in loop */) { |
| 3671 | // If we have an expanded parameter pack, make sure we don't have too |
| 3672 | // many arguments. |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 3673 | if (Optional<unsigned> Expansions = getExpandedPackSize(*Param)) { |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3674 | if (*Expansions == ArgumentPack.size()) { |
| 3675 | // We're done with this parameter pack. Pack up its arguments and add |
| 3676 | // them to the list. |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 3677 | Converted.push_back( |
| 3678 | TemplateArgument::CreatePackCopy(Context, |
| 3679 | ArgumentPack.data(), |
| 3680 | ArgumentPack.size())); |
| 3681 | ArgumentPack.clear(); |
| 3682 | |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3683 | // This argument is assigned to the next parameter. |
| 3684 | ++Param; |
| 3685 | continue; |
| 3686 | } else if (ArgIdx == NumArgs && !PartialTemplateArgs) { |
| 3687 | // Not enough arguments for this parameter pack. |
| 3688 | Diag(TemplateLoc, diag::err_template_arg_list_different_arity) |
| 3689 | << false |
| 3690 | << (isa<ClassTemplateDecl>(Template)? 0 : |
| 3691 | isa<FunctionTemplateDecl>(Template)? 1 : |
| 3692 | isa<TemplateTemplateParmDecl>(Template)? 2 : 3) |
| 3693 | << Template; |
| 3694 | Diag(Template->getLocation(), diag::note_template_decl_here) |
| 3695 | << Params->getSourceRange(); |
| 3696 | return true; |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 3697 | } |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3698 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3699 | |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3700 | if (ArgIdx < NumArgs) { |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3701 | // Check the template argument we were given. |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 3702 | if (CheckTemplateArgument(*Param, NewArgs[ArgIdx], Template, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3703 | TemplateLoc, RAngleLoc, |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 3704 | ArgumentPack.size(), Converted)) |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3705 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3706 | |
Richard Smith | 96d71c3 | 2014-11-12 23:38:38 +0000 | [diff] [blame] | 3707 | bool PackExpansionIntoNonPack = |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 3708 | NewArgs[ArgIdx].getArgument().isPackExpansion() && |
Richard Smith | 96d71c3 | 2014-11-12 23:38:38 +0000 | [diff] [blame] | 3709 | (!(*Param)->isTemplateParameterPack() || getExpandedPackSize(*Param)); |
| 3710 | if (PackExpansionIntoNonPack && isa<TypeAliasTemplateDecl>(Template)) { |
Richard Smith | 83b11aa | 2014-01-09 02:22:22 +0000 | [diff] [blame] | 3711 | // Core issue 1430: we have a pack expansion as an argument to an |
Richard Smith | 96d71c3 | 2014-11-12 23:38:38 +0000 | [diff] [blame] | 3712 | // alias template, and it's not part of a parameter pack. This |
Richard Smith | 83b11aa | 2014-01-09 02:22:22 +0000 | [diff] [blame] | 3713 | // can't be canonicalized, so reject it now. |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 3714 | Diag(NewArgs[ArgIdx].getLocation(), |
Richard Smith | 83b11aa | 2014-01-09 02:22:22 +0000 | [diff] [blame] | 3715 | diag::err_alias_template_expansion_into_fixed_list) |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 3716 | << NewArgs[ArgIdx].getSourceRange(); |
Richard Smith | 83b11aa | 2014-01-09 02:22:22 +0000 | [diff] [blame] | 3717 | Diag((*Param)->getLocation(), diag::note_template_param_here); |
| 3718 | return true; |
| 3719 | } |
| 3720 | |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3721 | // We're now done with this argument. |
| 3722 | ++ArgIdx; |
| 3723 | |
Douglas Gregor | 9abeaf5 | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 3724 | if ((*Param)->isTemplateParameterPack()) { |
| 3725 | // The template parameter was a template parameter pack, so take the |
| 3726 | // deduced argument and place it on the argument pack. Note that we |
| 3727 | // stay on the same template parameter so that we can deduce more |
| 3728 | // arguments. |
Robert Wilhelm | 25284cc | 2013-08-23 16:11:15 +0000 | [diff] [blame] | 3729 | ArgumentPack.push_back(Converted.pop_back_val()); |
Douglas Gregor | 9abeaf5 | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 3730 | } else { |
| 3731 | // Move to the next template parameter. |
| 3732 | ++Param; |
| 3733 | } |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3734 | |
Richard Smith | 96d71c3 | 2014-11-12 23:38:38 +0000 | [diff] [blame] | 3735 | // If we just saw a pack expansion into a non-pack, then directly convert |
| 3736 | // the remaining arguments, because we don't know what parameters they'll |
| 3737 | // match up with. |
| 3738 | if (PackExpansionIntoNonPack) { |
| 3739 | if (!ArgumentPack.empty()) { |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3740 | // If we were part way through filling in an expanded parameter pack, |
| 3741 | // fall back to just producing individual arguments. |
| 3742 | Converted.insert(Converted.end(), |
| 3743 | ArgumentPack.begin(), ArgumentPack.end()); |
| 3744 | ArgumentPack.clear(); |
| 3745 | } |
| 3746 | |
| 3747 | while (ArgIdx < NumArgs) { |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 3748 | Converted.push_back(NewArgs[ArgIdx].getArgument()); |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3749 | ++ArgIdx; |
| 3750 | } |
| 3751 | |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3752 | return false; |
Douglas Gregor | 8e07261 | 2012-02-03 07:34:46 +0000 | [diff] [blame] | 3753 | } |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3754 | |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3755 | continue; |
Douglas Gregor | 264ec4f | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 3756 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3757 | |
Douglas Gregor | 2f157c9 | 2011-06-03 02:59:40 +0000 | [diff] [blame] | 3758 | // If we're checking a partial template argument list, we're done. |
| 3759 | if (PartialTemplateArgs) { |
| 3760 | if ((*Param)->isTemplateParameterPack() && !ArgumentPack.empty()) |
| 3761 | Converted.push_back(TemplateArgument::CreatePackCopy(Context, |
| 3762 | ArgumentPack.data(), |
| 3763 | ArgumentPack.size())); |
| 3764 | |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3765 | return false; |
Douglas Gregor | 2f157c9 | 2011-06-03 02:59:40 +0000 | [diff] [blame] | 3766 | } |
| 3767 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3768 | // If we have a template parameter pack with no more corresponding |
Douglas Gregor | 9abeaf5 | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 3769 | // arguments, just break out now and we'll fill in the argument pack below. |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3770 | if ((*Param)->isTemplateParameterPack()) { |
| 3771 | assert(!getExpandedPackSize(*Param) && |
| 3772 | "Should have dealt with this already"); |
| 3773 | |
| 3774 | // A non-expanded parameter pack before the end of the parameter list |
| 3775 | // only occurs for an ill-formed template parameter list, unless we've |
| 3776 | // got a partial argument list for a function template, so just bail out. |
| 3777 | if (Param + 1 != ParamEnd) |
| 3778 | return true; |
| 3779 | |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 3780 | Converted.push_back(TemplateArgument::CreatePackCopy(Context, |
| 3781 | ArgumentPack.data(), |
| 3782 | ArgumentPack.size())); |
| 3783 | ArgumentPack.clear(); |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3784 | |
| 3785 | ++Param; |
| 3786 | continue; |
| 3787 | } |
| 3788 | |
Douglas Gregor | 8e07261 | 2012-02-03 07:34:46 +0000 | [diff] [blame] | 3789 | // Check whether we have a default argument. |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3790 | TemplateArgumentLoc Arg; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3791 | |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3792 | // Retrieve the default template argument from the template |
| 3793 | // parameter. For each kind of template parameter, we substitute the |
| 3794 | // template arguments provided thus far and any "outer" template arguments |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3795 | // (when the template parameter was part of a nested template) into |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3796 | // the default argument. |
| 3797 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*Param)) { |
Douglas Gregor | 8e07261 | 2012-02-03 07:34:46 +0000 | [diff] [blame] | 3798 | if (!TTP->hasDefaultArgument()) |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 3799 | return diagnoseArityMismatch(*this, Template, TemplateLoc, NewArgs); |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3800 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3801 | TypeSourceInfo *ArgType = SubstDefaultTemplateArgument(*this, |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3802 | Template, |
| 3803 | TemplateLoc, |
| 3804 | RAngleLoc, |
| 3805 | TTP, |
| 3806 | Converted); |
| 3807 | if (!ArgType) |
| 3808 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3809 | |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3810 | Arg = TemplateArgumentLoc(TemplateArgument(ArgType->getType()), |
| 3811 | ArgType); |
| 3812 | } else if (NonTypeTemplateParmDecl *NTTP |
| 3813 | = dyn_cast<NonTypeTemplateParmDecl>(*Param)) { |
Douglas Gregor | 8e07261 | 2012-02-03 07:34:46 +0000 | [diff] [blame] | 3814 | if (!NTTP->hasDefaultArgument()) |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 3815 | return diagnoseArityMismatch(*this, Template, TemplateLoc, NewArgs); |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3816 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3817 | ExprResult E = SubstDefaultTemplateArgument(*this, Template, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3818 | TemplateLoc, |
| 3819 | RAngleLoc, |
| 3820 | NTTP, |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3821 | Converted); |
| 3822 | if (E.isInvalid()) |
| 3823 | return true; |
| 3824 | |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3825 | Expr *Ex = E.getAs<Expr>(); |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3826 | Arg = TemplateArgumentLoc(TemplateArgument(Ex), Ex); |
| 3827 | } else { |
| 3828 | TemplateTemplateParmDecl *TempParm |
| 3829 | = cast<TemplateTemplateParmDecl>(*Param); |
| 3830 | |
Douglas Gregor | 8e07261 | 2012-02-03 07:34:46 +0000 | [diff] [blame] | 3831 | if (!TempParm->hasDefaultArgument()) |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 3832 | return diagnoseArityMismatch(*this, Template, TemplateLoc, NewArgs); |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3833 | |
Douglas Gregor | df846d1 | 2011-03-02 18:46:51 +0000 | [diff] [blame] | 3834 | NestedNameSpecifierLoc QualifierLoc; |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3835 | TemplateName Name = SubstDefaultTemplateArgument(*this, Template, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3836 | TemplateLoc, |
| 3837 | RAngleLoc, |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3838 | TempParm, |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 3839 | Converted, |
| 3840 | QualifierLoc); |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3841 | if (Name.isNull()) |
| 3842 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3843 | |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 3844 | Arg = TemplateArgumentLoc(TemplateArgument(Name), QualifierLoc, |
| 3845 | TempParm->getDefaultArgument().getTemplateNameLoc()); |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3846 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3847 | |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3848 | // Introduce an instantiation record that describes where we are using |
| 3849 | // the default template argument. |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 3850 | InstantiatingTemplate Inst(*this, RAngleLoc, Template, *Param, Converted, |
| 3851 | SourceRange(TemplateLoc, RAngleLoc)); |
| 3852 | if (Inst.isInvalid()) |
Richard Smith | 8a874c9 | 2012-07-08 02:38:24 +0000 | [diff] [blame] | 3853 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3854 | |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3855 | // Check the default template argument. |
Douglas Gregor | eebed72 | 2009-11-11 19:41:09 +0000 | [diff] [blame] | 3856 | if (CheckTemplateArgument(*Param, Arg, Template, TemplateLoc, |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 3857 | RAngleLoc, 0, Converted)) |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3858 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3859 | |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 3860 | // Core issue 150 (assumed resolution): if this is a template template |
| 3861 | // parameter, keep track of the default template arguments from the |
Douglas Gregor | 739b107a | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 3862 | // template definition. |
| 3863 | if (isTemplateTemplateParameter) |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 3864 | NewArgs.addArgument(Arg); |
| 3865 | |
Douglas Gregor | 9abeaf5 | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 3866 | // Move to the next template parameter and argument. |
| 3867 | ++Param; |
| 3868 | ++ArgIdx; |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3869 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3870 | |
Richard Smith | 07f7991 | 2014-06-06 16:00:50 +0000 | [diff] [blame] | 3871 | // If we're performing a partial argument substitution, allow any trailing |
| 3872 | // pack expansions; they might be empty. This can happen even if |
| 3873 | // PartialTemplateArgs is false (the list of arguments is complete but |
| 3874 | // still dependent). |
| 3875 | if (ArgIdx < NumArgs && CurrentInstantiationScope && |
| 3876 | CurrentInstantiationScope->getPartiallySubstitutedPack()) { |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 3877 | while (ArgIdx < NumArgs && NewArgs[ArgIdx].getArgument().isPackExpansion()) |
| 3878 | Converted.push_back(NewArgs[ArgIdx++].getArgument()); |
Richard Smith | 07f7991 | 2014-06-06 16:00:50 +0000 | [diff] [blame] | 3879 | } |
| 3880 | |
Douglas Gregor | 8e07261 | 2012-02-03 07:34:46 +0000 | [diff] [blame] | 3881 | // If we have any leftover arguments, then there were too many arguments. |
| 3882 | // Complain and fail. |
| 3883 | if (ArgIdx < NumArgs) |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 3884 | return diagnoseArityMismatch(*this, Template, TemplateLoc, NewArgs); |
| 3885 | |
| 3886 | // No problems found with the new argument list, propagate changes back |
| 3887 | // to caller. |
| 3888 | TemplateArgs = NewArgs; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3889 | |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3890 | return false; |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3891 | } |
| 3892 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3893 | namespace { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3894 | class UnnamedLocalNoLinkageFinder |
| 3895 | : public TypeVisitor<UnnamedLocalNoLinkageFinder, bool> |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3896 | { |
| 3897 | Sema &S; |
| 3898 | SourceRange SR; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3899 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3900 | typedef TypeVisitor<UnnamedLocalNoLinkageFinder, bool> inherited; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3901 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3902 | public: |
| 3903 | UnnamedLocalNoLinkageFinder(Sema &S, SourceRange SR) : S(S), SR(SR) { } |
| 3904 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3905 | bool Visit(QualType T) { |
| 3906 | return inherited::Visit(T.getTypePtr()); |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3907 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3908 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3909 | #define TYPE(Class, Parent) \ |
| 3910 | bool Visit##Class##Type(const Class##Type *); |
| 3911 | #define ABSTRACT_TYPE(Class, Parent) \ |
| 3912 | bool Visit##Class##Type(const Class##Type *) { return false; } |
| 3913 | #define NON_CANONICAL_TYPE(Class, Parent) \ |
| 3914 | bool Visit##Class##Type(const Class##Type *) { return false; } |
| 3915 | #include "clang/AST/TypeNodes.def" |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3916 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3917 | bool VisitTagDecl(const TagDecl *Tag); |
| 3918 | bool VisitNestedNameSpecifier(NestedNameSpecifier *NNS); |
| 3919 | }; |
| 3920 | } |
| 3921 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3922 | bool UnnamedLocalNoLinkageFinder::VisitBuiltinType(const BuiltinType*) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3923 | return false; |
| 3924 | } |
| 3925 | |
| 3926 | bool UnnamedLocalNoLinkageFinder::VisitComplexType(const ComplexType* T) { |
| 3927 | return Visit(T->getElementType()); |
| 3928 | } |
| 3929 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3930 | bool UnnamedLocalNoLinkageFinder::VisitPointerType(const PointerType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3931 | return Visit(T->getPointeeType()); |
| 3932 | } |
| 3933 | |
| 3934 | bool UnnamedLocalNoLinkageFinder::VisitBlockPointerType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3935 | const BlockPointerType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3936 | return Visit(T->getPointeeType()); |
| 3937 | } |
| 3938 | |
| 3939 | bool UnnamedLocalNoLinkageFinder::VisitLValueReferenceType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3940 | const LValueReferenceType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3941 | return Visit(T->getPointeeType()); |
| 3942 | } |
| 3943 | |
| 3944 | bool UnnamedLocalNoLinkageFinder::VisitRValueReferenceType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3945 | const RValueReferenceType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3946 | return Visit(T->getPointeeType()); |
| 3947 | } |
| 3948 | |
| 3949 | bool UnnamedLocalNoLinkageFinder::VisitMemberPointerType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3950 | const MemberPointerType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3951 | return Visit(T->getPointeeType()) || Visit(QualType(T->getClass(), 0)); |
| 3952 | } |
| 3953 | |
| 3954 | bool UnnamedLocalNoLinkageFinder::VisitConstantArrayType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3955 | const ConstantArrayType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3956 | return Visit(T->getElementType()); |
| 3957 | } |
| 3958 | |
| 3959 | bool UnnamedLocalNoLinkageFinder::VisitIncompleteArrayType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3960 | const IncompleteArrayType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3961 | return Visit(T->getElementType()); |
| 3962 | } |
| 3963 | |
| 3964 | bool UnnamedLocalNoLinkageFinder::VisitVariableArrayType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3965 | const VariableArrayType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3966 | return Visit(T->getElementType()); |
| 3967 | } |
| 3968 | |
| 3969 | bool UnnamedLocalNoLinkageFinder::VisitDependentSizedArrayType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3970 | const DependentSizedArrayType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3971 | return Visit(T->getElementType()); |
| 3972 | } |
| 3973 | |
| 3974 | bool UnnamedLocalNoLinkageFinder::VisitDependentSizedExtVectorType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3975 | const DependentSizedExtVectorType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3976 | return Visit(T->getElementType()); |
| 3977 | } |
| 3978 | |
| 3979 | bool UnnamedLocalNoLinkageFinder::VisitVectorType(const VectorType* T) { |
| 3980 | return Visit(T->getElementType()); |
| 3981 | } |
| 3982 | |
| 3983 | bool UnnamedLocalNoLinkageFinder::VisitExtVectorType(const ExtVectorType* T) { |
| 3984 | return Visit(T->getElementType()); |
| 3985 | } |
| 3986 | |
| 3987 | bool UnnamedLocalNoLinkageFinder::VisitFunctionProtoType( |
| 3988 | const FunctionProtoType* T) { |
Aaron Ballman | 40bd0aa | 2014-03-17 15:23:01 +0000 | [diff] [blame] | 3989 | for (const auto &A : T->param_types()) { |
| 3990 | if (Visit(A)) |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3991 | return true; |
| 3992 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3993 | |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3994 | return Visit(T->getReturnType()); |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3995 | } |
| 3996 | |
| 3997 | bool UnnamedLocalNoLinkageFinder::VisitFunctionNoProtoType( |
| 3998 | const FunctionNoProtoType* T) { |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3999 | return Visit(T->getReturnType()); |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4000 | } |
| 4001 | |
| 4002 | bool UnnamedLocalNoLinkageFinder::VisitUnresolvedUsingType( |
| 4003 | const UnresolvedUsingType*) { |
| 4004 | return false; |
| 4005 | } |
| 4006 | |
| 4007 | bool UnnamedLocalNoLinkageFinder::VisitTypeOfExprType(const TypeOfExprType*) { |
| 4008 | return false; |
| 4009 | } |
| 4010 | |
| 4011 | bool UnnamedLocalNoLinkageFinder::VisitTypeOfType(const TypeOfType* T) { |
| 4012 | return Visit(T->getUnderlyingType()); |
| 4013 | } |
| 4014 | |
| 4015 | bool UnnamedLocalNoLinkageFinder::VisitDecltypeType(const DecltypeType*) { |
| 4016 | return false; |
| 4017 | } |
| 4018 | |
Alexis Hunt | e852b10 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 4019 | bool UnnamedLocalNoLinkageFinder::VisitUnaryTransformType( |
| 4020 | const UnaryTransformType*) { |
| 4021 | return false; |
| 4022 | } |
| 4023 | |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 4024 | bool UnnamedLocalNoLinkageFinder::VisitAutoType(const AutoType *T) { |
| 4025 | return Visit(T->getDeducedType()); |
| 4026 | } |
| 4027 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4028 | bool UnnamedLocalNoLinkageFinder::VisitRecordType(const RecordType* T) { |
| 4029 | return VisitTagDecl(T->getDecl()); |
| 4030 | } |
| 4031 | |
| 4032 | bool UnnamedLocalNoLinkageFinder::VisitEnumType(const EnumType* T) { |
| 4033 | return VisitTagDecl(T->getDecl()); |
| 4034 | } |
| 4035 | |
| 4036 | bool UnnamedLocalNoLinkageFinder::VisitTemplateTypeParmType( |
| 4037 | const TemplateTypeParmType*) { |
| 4038 | return false; |
| 4039 | } |
| 4040 | |
Douglas Gregor | ada4b79 | 2011-01-14 02:55:32 +0000 | [diff] [blame] | 4041 | bool UnnamedLocalNoLinkageFinder::VisitSubstTemplateTypeParmPackType( |
| 4042 | const SubstTemplateTypeParmPackType *) { |
| 4043 | return false; |
| 4044 | } |
| 4045 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4046 | bool UnnamedLocalNoLinkageFinder::VisitTemplateSpecializationType( |
| 4047 | const TemplateSpecializationType*) { |
| 4048 | return false; |
| 4049 | } |
| 4050 | |
| 4051 | bool UnnamedLocalNoLinkageFinder::VisitInjectedClassNameType( |
| 4052 | const InjectedClassNameType* T) { |
| 4053 | return VisitTagDecl(T->getDecl()); |
| 4054 | } |
| 4055 | |
| 4056 | bool UnnamedLocalNoLinkageFinder::VisitDependentNameType( |
| 4057 | const DependentNameType* T) { |
| 4058 | return VisitNestedNameSpecifier(T->getQualifier()); |
| 4059 | } |
| 4060 | |
| 4061 | bool UnnamedLocalNoLinkageFinder::VisitDependentTemplateSpecializationType( |
| 4062 | const DependentTemplateSpecializationType* T) { |
| 4063 | return VisitNestedNameSpecifier(T->getQualifier()); |
| 4064 | } |
| 4065 | |
Douglas Gregor | d2fa766 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 4066 | bool UnnamedLocalNoLinkageFinder::VisitPackExpansionType( |
| 4067 | const PackExpansionType* T) { |
| 4068 | return Visit(T->getPattern()); |
| 4069 | } |
| 4070 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4071 | bool UnnamedLocalNoLinkageFinder::VisitObjCObjectType(const ObjCObjectType *) { |
| 4072 | return false; |
| 4073 | } |
| 4074 | |
| 4075 | bool UnnamedLocalNoLinkageFinder::VisitObjCInterfaceType( |
| 4076 | const ObjCInterfaceType *) { |
| 4077 | return false; |
| 4078 | } |
| 4079 | |
| 4080 | bool UnnamedLocalNoLinkageFinder::VisitObjCObjectPointerType( |
| 4081 | const ObjCObjectPointerType *) { |
| 4082 | return false; |
| 4083 | } |
| 4084 | |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 4085 | bool UnnamedLocalNoLinkageFinder::VisitAtomicType(const AtomicType* T) { |
| 4086 | return Visit(T->getValueType()); |
| 4087 | } |
| 4088 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4089 | bool UnnamedLocalNoLinkageFinder::VisitTagDecl(const TagDecl *Tag) { |
| 4090 | if (Tag->getDeclContext()->isFunctionOrMethod()) { |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 4091 | S.Diag(SR.getBegin(), |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4092 | S.getLangOpts().CPlusPlus11 ? |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 4093 | diag::warn_cxx98_compat_template_arg_local_type : |
| 4094 | diag::ext_template_arg_local_type) |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4095 | << S.Context.getTypeDeclType(Tag) << SR; |
| 4096 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4097 | } |
| 4098 | |
John McCall | 5ea9577 | 2013-03-09 00:54:27 +0000 | [diff] [blame] | 4099 | if (!Tag->hasNameForLinkage()) { |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 4100 | S.Diag(SR.getBegin(), |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4101 | S.getLangOpts().CPlusPlus11 ? |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 4102 | diag::warn_cxx98_compat_template_arg_unnamed_type : |
| 4103 | diag::ext_template_arg_unnamed_type) << SR; |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4104 | S.Diag(Tag->getLocation(), diag::note_template_unnamed_type_here); |
| 4105 | return true; |
| 4106 | } |
| 4107 | |
| 4108 | return false; |
| 4109 | } |
| 4110 | |
| 4111 | bool UnnamedLocalNoLinkageFinder::VisitNestedNameSpecifier( |
| 4112 | NestedNameSpecifier *NNS) { |
| 4113 | if (NNS->getPrefix() && VisitNestedNameSpecifier(NNS->getPrefix())) |
| 4114 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4115 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4116 | switch (NNS->getKind()) { |
| 4117 | case NestedNameSpecifier::Identifier: |
| 4118 | case NestedNameSpecifier::Namespace: |
Douglas Gregor | 7b26ff9 | 2011-02-24 02:36:08 +0000 | [diff] [blame] | 4119 | case NestedNameSpecifier::NamespaceAlias: |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4120 | case NestedNameSpecifier::Global: |
Nikola Smiljanic | 6786024 | 2014-09-26 00:28:20 +0000 | [diff] [blame] | 4121 | case NestedNameSpecifier::Super: |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4122 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4123 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4124 | case NestedNameSpecifier::TypeSpec: |
| 4125 | case NestedNameSpecifier::TypeSpecWithTemplate: |
| 4126 | return Visit(QualType(NNS->getAsType(), 0)); |
| 4127 | } |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 4128 | llvm_unreachable("Invalid NestedNameSpecifier::Kind!"); |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4129 | } |
| 4130 | |
| 4131 | |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 4132 | /// \brief Check a template argument against its corresponding |
| 4133 | /// template type parameter. |
| 4134 | /// |
| 4135 | /// This routine implements the semantics of C++ [temp.arg.type]. It |
| 4136 | /// returns true if an error occurred, and false otherwise. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4137 | bool Sema::CheckTemplateArgument(TemplateTypeParmDecl *Param, |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 4138 | TypeSourceInfo *ArgInfo) { |
| 4139 | assert(ArgInfo && "invalid TypeSourceInfo"); |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 4140 | QualType Arg = ArgInfo->getType(); |
Douglas Gregor | 959d5a0 | 2010-05-22 16:17:30 +0000 | [diff] [blame] | 4141 | SourceRange SR = ArgInfo->getTypeLoc().getSourceRange(); |
Chandler Carruth | 9bb67f4 | 2010-09-03 21:12:34 +0000 | [diff] [blame] | 4142 | |
| 4143 | if (Arg->isVariablyModifiedType()) { |
| 4144 | return Diag(SR.getBegin(), diag::err_variably_modified_template_arg) << Arg; |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 4145 | } else if (Context.hasSameUnqualifiedType(Arg, Context.OverloadTy)) { |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 4146 | return Diag(SR.getBegin(), diag::err_template_arg_overload_type) << SR; |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 4147 | } |
| 4148 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4149 | // C++03 [temp.arg.type]p2: |
| 4150 | // A local type, a type with no linkage, an unnamed type or a type |
| 4151 | // compounded from any of these types shall not be used as a |
| 4152 | // template-argument for a template type-parameter. |
| 4153 | // |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 4154 | // C++11 allows these, and even in C++03 we allow them as an extension with |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4155 | // a warning. |
Alp Toker | d4a3f0e | 2014-06-15 23:30:39 +0000 | [diff] [blame] | 4156 | bool NeedsCheck; |
| 4157 | if (LangOpts.CPlusPlus11) |
| 4158 | NeedsCheck = |
| 4159 | !Diags.isIgnored(diag::warn_cxx98_compat_template_arg_unnamed_type, |
| 4160 | SR.getBegin()) || |
| 4161 | !Diags.isIgnored(diag::warn_cxx98_compat_template_arg_local_type, |
| 4162 | SR.getBegin()); |
| 4163 | else |
| 4164 | NeedsCheck = Arg->hasUnnamedOrLocalType(); |
| 4165 | |
| 4166 | if (NeedsCheck) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4167 | UnnamedLocalNoLinkageFinder Finder(*this, SR); |
| 4168 | (void)Finder.Visit(Context.getCanonicalType(Arg)); |
| 4169 | } |
| 4170 | |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 4171 | return false; |
| 4172 | } |
| 4173 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4174 | enum NullPointerValueKind { |
| 4175 | NPV_NotNullPointer, |
| 4176 | NPV_NullPointer, |
| 4177 | NPV_Error |
| 4178 | }; |
| 4179 | |
| 4180 | /// \brief Determine whether the given template argument is a null pointer |
| 4181 | /// value of the appropriate type. |
| 4182 | static NullPointerValueKind |
| 4183 | isNullPointerValueTemplateArgument(Sema &S, NonTypeTemplateParmDecl *Param, |
| 4184 | QualType ParamType, Expr *Arg) { |
| 4185 | if (Arg->isValueDependent() || Arg->isTypeDependent()) |
| 4186 | return NPV_NotNullPointer; |
| 4187 | |
David Majnemer | 5c734ad | 2014-08-14 00:49:23 +0000 | [diff] [blame] | 4188 | if (!S.getLangOpts().CPlusPlus11) |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4189 | return NPV_NotNullPointer; |
| 4190 | |
| 4191 | // Determine whether we have a constant expression. |
Douglas Gregor | 350880c | 2012-04-10 19:03:30 +0000 | [diff] [blame] | 4192 | ExprResult ArgRV = S.DefaultFunctionArrayConversion(Arg); |
| 4193 | if (ArgRV.isInvalid()) |
| 4194 | return NPV_Error; |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 4195 | Arg = ArgRV.get(); |
Douglas Gregor | 350880c | 2012-04-10 19:03:30 +0000 | [diff] [blame] | 4196 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4197 | Expr::EvalResult EvalResult; |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 4198 | SmallVector<PartialDiagnosticAt, 8> Notes; |
Douglas Gregor | 350880c | 2012-04-10 19:03:30 +0000 | [diff] [blame] | 4199 | EvalResult.Diag = &Notes; |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4200 | if (!Arg->EvaluateAsRValue(EvalResult, S.Context) || |
Douglas Gregor | 350880c | 2012-04-10 19:03:30 +0000 | [diff] [blame] | 4201 | EvalResult.HasSideEffects) { |
| 4202 | SourceLocation DiagLoc = Arg->getExprLoc(); |
| 4203 | |
| 4204 | // If our only note is the usual "invalid subexpression" note, just point |
| 4205 | // the caret at its location rather than producing an essentially |
| 4206 | // redundant note. |
| 4207 | if (Notes.size() == 1 && Notes[0].second.getDiagID() == |
| 4208 | diag::note_invalid_subexpr_in_const_expr) { |
| 4209 | DiagLoc = Notes[0].first; |
| 4210 | Notes.clear(); |
| 4211 | } |
| 4212 | |
| 4213 | S.Diag(DiagLoc, diag::err_template_arg_not_address_constant) |
| 4214 | << Arg->getType() << Arg->getSourceRange(); |
| 4215 | for (unsigned I = 0, N = Notes.size(); I != N; ++I) |
| 4216 | S.Diag(Notes[I].first, Notes[I].second); |
| 4217 | |
| 4218 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4219 | return NPV_Error; |
| 4220 | } |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4221 | |
| 4222 | // C++11 [temp.arg.nontype]p1: |
| 4223 | // - an address constant expression of type std::nullptr_t |
| 4224 | if (Arg->getType()->isNullPtrType()) |
| 4225 | return NPV_NullPointer; |
| 4226 | |
| 4227 | // - a constant expression that evaluates to a null pointer value (4.10); or |
| 4228 | // - a constant expression that evaluates to a null member pointer value |
| 4229 | // (4.11); or |
| 4230 | if ((EvalResult.Val.isLValue() && !EvalResult.Val.getLValueBase()) || |
| 4231 | (EvalResult.Val.isMemberPointer() && |
| 4232 | !EvalResult.Val.getMemberPointerDecl())) { |
| 4233 | // If our expression has an appropriate type, we've succeeded. |
| 4234 | bool ObjCLifetimeConversion; |
| 4235 | if (S.Context.hasSameUnqualifiedType(Arg->getType(), ParamType) || |
| 4236 | S.IsQualificationConversion(Arg->getType(), ParamType, false, |
| 4237 | ObjCLifetimeConversion)) |
| 4238 | return NPV_NullPointer; |
| 4239 | |
| 4240 | // The types didn't match, but we know we got a null pointer; complain, |
| 4241 | // then recover as if the types were correct. |
| 4242 | S.Diag(Arg->getExprLoc(), diag::err_template_arg_wrongtype_null_constant) |
| 4243 | << Arg->getType() << ParamType << Arg->getSourceRange(); |
| 4244 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4245 | return NPV_NullPointer; |
| 4246 | } |
| 4247 | |
| 4248 | // If we don't have a null pointer value, but we do have a NULL pointer |
| 4249 | // constant, suggest a cast to the appropriate type. |
| 4250 | if (Arg->isNullPointerConstant(S.Context, Expr::NPC_NeverValueDependent)) { |
| 4251 | std::string Code = "static_cast<" + ParamType.getAsString() + ">("; |
| 4252 | S.Diag(Arg->getExprLoc(), diag::err_template_arg_untyped_null_constant) |
Alp Toker | b6cc592 | 2014-05-03 03:45:55 +0000 | [diff] [blame] | 4253 | << ParamType << FixItHint::CreateInsertion(Arg->getLocStart(), Code) |
| 4254 | << FixItHint::CreateInsertion(S.getLocForEndOfToken(Arg->getLocEnd()), |
| 4255 | ")"); |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4256 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4257 | return NPV_NullPointer; |
| 4258 | } |
| 4259 | |
| 4260 | // FIXME: If we ever want to support general, address-constant expressions |
| 4261 | // as non-type template arguments, we should return the ExprResult here to |
| 4262 | // be interpreted by the caller. |
| 4263 | return NPV_NotNullPointer; |
| 4264 | } |
| 4265 | |
David Majnemer | 61c39a1 | 2013-08-23 05:39:39 +0000 | [diff] [blame] | 4266 | /// \brief Checks whether the given template argument is compatible with its |
| 4267 | /// template parameter. |
| 4268 | static bool CheckTemplateArgumentIsCompatibleWithParameter( |
| 4269 | Sema &S, NonTypeTemplateParmDecl *Param, QualType ParamType, Expr *ArgIn, |
| 4270 | Expr *Arg, QualType ArgType) { |
| 4271 | bool ObjCLifetimeConversion; |
| 4272 | if (ParamType->isPointerType() && |
| 4273 | !ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType() && |
| 4274 | S.IsQualificationConversion(ArgType, ParamType, false, |
| 4275 | ObjCLifetimeConversion)) { |
| 4276 | // For pointer-to-object types, qualification conversions are |
| 4277 | // permitted. |
| 4278 | } else { |
| 4279 | if (const ReferenceType *ParamRef = ParamType->getAs<ReferenceType>()) { |
| 4280 | if (!ParamRef->getPointeeType()->isFunctionType()) { |
| 4281 | // C++ [temp.arg.nontype]p5b3: |
| 4282 | // For a non-type template-parameter of type reference to |
| 4283 | // object, no conversions apply. The type referred to by the |
| 4284 | // reference may be more cv-qualified than the (otherwise |
| 4285 | // identical) type of the template- argument. The |
| 4286 | // template-parameter is bound directly to the |
| 4287 | // template-argument, which shall be an lvalue. |
| 4288 | |
| 4289 | // FIXME: Other qualifiers? |
| 4290 | unsigned ParamQuals = ParamRef->getPointeeType().getCVRQualifiers(); |
| 4291 | unsigned ArgQuals = ArgType.getCVRQualifiers(); |
| 4292 | |
| 4293 | if ((ParamQuals | ArgQuals) != ParamQuals) { |
| 4294 | S.Diag(Arg->getLocStart(), |
| 4295 | diag::err_template_arg_ref_bind_ignores_quals) |
| 4296 | << ParamType << Arg->getType() << Arg->getSourceRange(); |
| 4297 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4298 | return true; |
| 4299 | } |
| 4300 | } |
| 4301 | } |
| 4302 | |
| 4303 | // At this point, the template argument refers to an object or |
| 4304 | // function with external linkage. We now need to check whether the |
| 4305 | // argument and parameter types are compatible. |
| 4306 | if (!S.Context.hasSameUnqualifiedType(ArgType, |
| 4307 | ParamType.getNonReferenceType())) { |
| 4308 | // We can't perform this conversion or binding. |
| 4309 | if (ParamType->isReferenceType()) |
| 4310 | S.Diag(Arg->getLocStart(), diag::err_template_arg_no_ref_bind) |
| 4311 | << ParamType << ArgIn->getType() << Arg->getSourceRange(); |
| 4312 | else |
| 4313 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_convertible) |
| 4314 | << ArgIn->getType() << ParamType << Arg->getSourceRange(); |
| 4315 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4316 | return true; |
| 4317 | } |
| 4318 | } |
| 4319 | |
| 4320 | return false; |
| 4321 | } |
| 4322 | |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4323 | /// \brief Checks whether the given template argument is the address |
| 4324 | /// of an object or function according to C++ [temp.arg.nontype]p1. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4325 | static bool |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4326 | CheckTemplateArgumentAddressOfObjectOrFunction(Sema &S, |
| 4327 | NonTypeTemplateParmDecl *Param, |
| 4328 | QualType ParamType, |
| 4329 | Expr *ArgIn, |
| 4330 | TemplateArgument &Converted) { |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4331 | bool Invalid = false; |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4332 | Expr *Arg = ArgIn; |
| 4333 | QualType ArgType = Arg->getType(); |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4334 | |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4335 | bool AddressTaken = false; |
| 4336 | SourceLocation AddrOpLoc; |
David Majnemer | 61c39a1 | 2013-08-23 05:39:39 +0000 | [diff] [blame] | 4337 | if (S.getLangOpts().MicrosoftExt) { |
| 4338 | // Microsoft Visual C++ strips all casts, allows an arbitrary number of |
| 4339 | // dereference and address-of operators. |
| 4340 | Arg = Arg->IgnoreParenCasts(); |
| 4341 | |
| 4342 | bool ExtWarnMSTemplateArg = false; |
| 4343 | UnaryOperatorKind FirstOpKind; |
| 4344 | SourceLocation FirstOpLoc; |
| 4345 | while (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) { |
| 4346 | UnaryOperatorKind UnOpKind = UnOp->getOpcode(); |
| 4347 | if (UnOpKind == UO_Deref) |
| 4348 | ExtWarnMSTemplateArg = true; |
| 4349 | if (UnOpKind == UO_AddrOf || UnOpKind == UO_Deref) { |
| 4350 | Arg = UnOp->getSubExpr()->IgnoreParenCasts(); |
| 4351 | if (!AddrOpLoc.isValid()) { |
| 4352 | FirstOpKind = UnOpKind; |
| 4353 | FirstOpLoc = UnOp->getOperatorLoc(); |
| 4354 | } |
| 4355 | } else |
| 4356 | break; |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4357 | } |
David Majnemer | 61c39a1 | 2013-08-23 05:39:39 +0000 | [diff] [blame] | 4358 | if (FirstOpLoc.isValid()) { |
| 4359 | if (ExtWarnMSTemplateArg) |
| 4360 | S.Diag(ArgIn->getLocStart(), diag::ext_ms_deref_template_argument) |
| 4361 | << ArgIn->getSourceRange(); |
John McCall | 7c454bb | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 4362 | |
David Majnemer | 61c39a1 | 2013-08-23 05:39:39 +0000 | [diff] [blame] | 4363 | if (FirstOpKind == UO_AddrOf) |
| 4364 | AddressTaken = true; |
| 4365 | else if (Arg->getType()->isPointerType()) { |
| 4366 | // We cannot let pointers get dereferenced here, that is obviously not a |
| 4367 | // constant expression. |
| 4368 | assert(FirstOpKind == UO_Deref); |
| 4369 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_decl_ref) |
| 4370 | << Arg->getSourceRange(); |
| 4371 | } |
| 4372 | } |
| 4373 | } else { |
| 4374 | // See through any implicit casts we added to fix the type. |
| 4375 | Arg = Arg->IgnoreImpCasts(); |
John McCall | 7c454bb | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 4376 | |
David Majnemer | 61c39a1 | 2013-08-23 05:39:39 +0000 | [diff] [blame] | 4377 | // C++ [temp.arg.nontype]p1: |
| 4378 | // |
| 4379 | // A template-argument for a non-type, non-template |
| 4380 | // template-parameter shall be one of: [...] |
| 4381 | // |
| 4382 | // -- the address of an object or function with external |
| 4383 | // linkage, including function templates and function |
| 4384 | // template-ids but excluding non-static class members, |
| 4385 | // expressed as & id-expression where the & is optional if |
| 4386 | // the name refers to a function or array, or if the |
| 4387 | // corresponding template-parameter is a reference; or |
| 4388 | |
| 4389 | // In C++98/03 mode, give an extension warning on any extra parentheses. |
| 4390 | // See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#773 |
| 4391 | bool ExtraParens = false; |
| 4392 | while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) { |
| 4393 | if (!Invalid && !ExtraParens) { |
| 4394 | S.Diag(Arg->getLocStart(), |
| 4395 | S.getLangOpts().CPlusPlus11 |
| 4396 | ? diag::warn_cxx98_compat_template_arg_extra_parens |
| 4397 | : diag::ext_template_arg_extra_parens) |
| 4398 | << Arg->getSourceRange(); |
| 4399 | ExtraParens = true; |
| 4400 | } |
| 4401 | |
| 4402 | Arg = Parens->getSubExpr(); |
| 4403 | } |
| 4404 | |
| 4405 | while (SubstNonTypeTemplateParmExpr *subst = |
| 4406 | dyn_cast<SubstNonTypeTemplateParmExpr>(Arg)) |
| 4407 | Arg = subst->getReplacement()->IgnoreImpCasts(); |
| 4408 | |
| 4409 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) { |
| 4410 | if (UnOp->getOpcode() == UO_AddrOf) { |
| 4411 | Arg = UnOp->getSubExpr(); |
| 4412 | AddressTaken = true; |
| 4413 | AddrOpLoc = UnOp->getOperatorLoc(); |
| 4414 | } |
| 4415 | } |
| 4416 | |
| 4417 | while (SubstNonTypeTemplateParmExpr *subst = |
| 4418 | dyn_cast<SubstNonTypeTemplateParmExpr>(Arg)) |
| 4419 | Arg = subst->getReplacement()->IgnoreImpCasts(); |
| 4420 | } |
John McCall | 7c454bb | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 4421 | |
David Majnemer | 07910d6 | 2014-06-26 07:48:46 +0000 | [diff] [blame] | 4422 | DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Arg); |
| 4423 | ValueDecl *Entity = DRE ? DRE->getDecl() : nullptr; |
| 4424 | |
| 4425 | // If our parameter has pointer type, check for a null template value. |
| 4426 | if (ParamType->isPointerType() || ParamType->isNullPtrType()) { |
| 4427 | NullPointerValueKind NPV; |
| 4428 | // dllimport'd entities aren't constant but are available inside of template |
| 4429 | // arguments. |
| 4430 | if (Entity && Entity->hasAttr<DLLImportAttr>()) |
| 4431 | NPV = NPV_NotNullPointer; |
| 4432 | else |
| 4433 | NPV = isNullPointerValueTemplateArgument(S, Param, ParamType, ArgIn); |
| 4434 | switch (NPV) { |
| 4435 | case NPV_NullPointer: |
| 4436 | S.Diag(Arg->getExprLoc(), diag::warn_cxx98_compat_template_arg_null); |
Richard Smith | 78bb36f | 2014-07-24 02:27:39 +0000 | [diff] [blame] | 4437 | Converted = TemplateArgument(S.Context.getCanonicalType(ParamType), |
| 4438 | /*isNullPtr=*/true); |
David Majnemer | 07910d6 | 2014-06-26 07:48:46 +0000 | [diff] [blame] | 4439 | return false; |
| 4440 | |
| 4441 | case NPV_Error: |
| 4442 | return true; |
| 4443 | |
| 4444 | case NPV_NotNullPointer: |
| 4445 | break; |
| 4446 | } |
| 4447 | } |
| 4448 | |
Chandler Carruth | 724a8a1 | 2010-01-31 10:01:20 +0000 | [diff] [blame] | 4449 | // Stop checking the precise nature of the argument if it is value dependent, |
| 4450 | // it should be checked when instantiated. |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4451 | if (Arg->isValueDependent()) { |
John McCall | c3007a2 | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 4452 | Converted = TemplateArgument(ArgIn); |
Chandler Carruth | 724a8a1 | 2010-01-31 10:01:20 +0000 | [diff] [blame] | 4453 | return false; |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4454 | } |
David Majnemer | 61c39a1 | 2013-08-23 05:39:39 +0000 | [diff] [blame] | 4455 | |
| 4456 | if (isa<CXXUuidofExpr>(Arg)) { |
| 4457 | if (CheckTemplateArgumentIsCompatibleWithParameter(S, Param, ParamType, |
| 4458 | ArgIn, Arg, ArgType)) |
| 4459 | return true; |
| 4460 | |
| 4461 | Converted = TemplateArgument(ArgIn); |
| 4462 | return false; |
| 4463 | } |
| 4464 | |
Douglas Gregor | 31f55dc | 2012-04-06 22:40:38 +0000 | [diff] [blame] | 4465 | if (!DRE) { |
| 4466 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_decl_ref) |
| 4467 | << Arg->getSourceRange(); |
| 4468 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4469 | return true; |
| 4470 | } |
Chandler Carruth | 724a8a1 | 2010-01-31 10:01:20 +0000 | [diff] [blame] | 4471 | |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4472 | // Cannot refer to non-static data members |
David Majnemer | 6bedcfa | 2013-10-26 06:12:44 +0000 | [diff] [blame] | 4473 | if (isa<FieldDecl>(Entity) || isa<IndirectFieldDecl>(Entity)) { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4474 | S.Diag(Arg->getLocStart(), diag::err_template_arg_field) |
David Majnemer | 6bedcfa | 2013-10-26 06:12:44 +0000 | [diff] [blame] | 4475 | << Entity << Arg->getSourceRange(); |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4476 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4477 | return true; |
| 4478 | } |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4479 | |
| 4480 | // Cannot refer to non-static member functions |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 4481 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Entity)) { |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4482 | if (!Method->isStatic()) { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4483 | S.Diag(Arg->getLocStart(), diag::err_template_arg_method) |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4484 | << Method << Arg->getSourceRange(); |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4485 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4486 | return true; |
| 4487 | } |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 4488 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4489 | |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 4490 | FunctionDecl *Func = dyn_cast<FunctionDecl>(Entity); |
| 4491 | VarDecl *Var = dyn_cast<VarDecl>(Entity); |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4492 | |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 4493 | // A non-type template argument must refer to an object or function. |
| 4494 | if (!Func && !Var) { |
| 4495 | // We found something, but we don't know specifically what it is. |
| 4496 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_object_or_func) |
| 4497 | << Arg->getSourceRange(); |
| 4498 | S.Diag(DRE->getDecl()->getLocation(), diag::note_template_arg_refers_here); |
| 4499 | return true; |
| 4500 | } |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4501 | |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 4502 | // Address / reference template args must have external linkage in C++98. |
Rafael Espindola | 3ae0005 | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 4503 | if (Entity->getFormalLinkage() == InternalLinkage) { |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4504 | S.Diag(Arg->getLocStart(), S.getLangOpts().CPlusPlus11 ? |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 4505 | diag::warn_cxx98_compat_template_arg_object_internal : |
| 4506 | diag::ext_template_arg_object_internal) |
| 4507 | << !Func << Entity << Arg->getSourceRange(); |
| 4508 | S.Diag(Entity->getLocation(), diag::note_template_arg_internal_object) |
| 4509 | << !Func; |
Rafael Espindola | 3ae0005 | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 4510 | } else if (!Entity->hasLinkage()) { |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 4511 | S.Diag(Arg->getLocStart(), diag::err_template_arg_object_no_linkage) |
| 4512 | << !Func << Entity << Arg->getSourceRange(); |
| 4513 | S.Diag(Entity->getLocation(), diag::note_template_arg_internal_object) |
| 4514 | << !Func; |
| 4515 | return true; |
| 4516 | } |
| 4517 | |
| 4518 | if (Func) { |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4519 | // If the template parameter has pointer type, the function decays. |
| 4520 | if (ParamType->isPointerType() && !AddressTaken) |
| 4521 | ArgType = S.Context.getPointerType(Func->getType()); |
| 4522 | else if (AddressTaken && ParamType->isReferenceType()) { |
| 4523 | // If we originally had an address-of operator, but the |
| 4524 | // parameter has reference type, complain and (if things look |
| 4525 | // like they will work) drop the address-of operator. |
| 4526 | if (!S.Context.hasSameUnqualifiedType(Func->getType(), |
| 4527 | ParamType.getNonReferenceType())) { |
| 4528 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 4529 | << ParamType; |
| 4530 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4531 | return true; |
| 4532 | } |
| 4533 | |
| 4534 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 4535 | << ParamType |
| 4536 | << FixItHint::CreateRemoval(AddrOpLoc); |
| 4537 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4538 | |
| 4539 | ArgType = Func->getType(); |
| 4540 | } |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 4541 | } else { |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4542 | // A value of reference type is not an object. |
| 4543 | if (Var->getType()->isReferenceType()) { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4544 | S.Diag(Arg->getLocStart(), |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4545 | diag::err_template_arg_reference_var) |
| 4546 | << Var->getType() << Arg->getSourceRange(); |
| 4547 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4548 | return true; |
| 4549 | } |
| 4550 | |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 4551 | // A template argument must have static storage duration. |
Richard Smith | fd3834f | 2013-04-13 02:43:54 +0000 | [diff] [blame] | 4552 | if (Var->getTLSKind()) { |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 4553 | S.Diag(Arg->getLocStart(), diag::err_template_arg_thread_local) |
| 4554 | << Arg->getSourceRange(); |
| 4555 | S.Diag(Var->getLocation(), diag::note_template_arg_refers_here); |
| 4556 | return true; |
| 4557 | } |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4558 | |
| 4559 | // If the template parameter has pointer type, we must have taken |
| 4560 | // the address of this object. |
| 4561 | if (ParamType->isReferenceType()) { |
| 4562 | if (AddressTaken) { |
| 4563 | // If we originally had an address-of operator, but the |
| 4564 | // parameter has reference type, complain and (if things look |
| 4565 | // like they will work) drop the address-of operator. |
| 4566 | if (!S.Context.hasSameUnqualifiedType(Var->getType(), |
| 4567 | ParamType.getNonReferenceType())) { |
| 4568 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 4569 | << ParamType; |
| 4570 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4571 | return true; |
| 4572 | } |
| 4573 | |
| 4574 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 4575 | << ParamType |
| 4576 | << FixItHint::CreateRemoval(AddrOpLoc); |
| 4577 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4578 | |
| 4579 | ArgType = Var->getType(); |
| 4580 | } |
| 4581 | } else if (!AddressTaken && ParamType->isPointerType()) { |
| 4582 | if (Var->getType()->isArrayType()) { |
| 4583 | // Array-to-pointer decay. |
| 4584 | ArgType = S.Context.getArrayDecayedType(Var->getType()); |
| 4585 | } else { |
| 4586 | // If the template parameter has pointer type but the address of |
| 4587 | // this object was not taken, complain and (possibly) recover by |
| 4588 | // taking the address of the entity. |
| 4589 | ArgType = S.Context.getPointerType(Var->getType()); |
| 4590 | if (!S.Context.hasSameUnqualifiedType(ArgType, ParamType)) { |
| 4591 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_address_of) |
| 4592 | << ParamType; |
| 4593 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4594 | return true; |
| 4595 | } |
| 4596 | |
| 4597 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_address_of) |
| 4598 | << ParamType |
| 4599 | << FixItHint::CreateInsertion(Arg->getLocStart(), "&"); |
| 4600 | |
| 4601 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4602 | } |
| 4603 | } |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4604 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4605 | |
David Majnemer | 61c39a1 | 2013-08-23 05:39:39 +0000 | [diff] [blame] | 4606 | if (CheckTemplateArgumentIsCompatibleWithParameter(S, Param, ParamType, ArgIn, |
| 4607 | Arg, ArgType)) |
| 4608 | return true; |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4609 | |
| 4610 | // Create the template argument. |
David Blaikie | 0f62c8d | 2014-10-16 04:21:25 +0000 | [diff] [blame] | 4611 | Converted = |
| 4612 | TemplateArgument(cast<ValueDecl>(Entity->getCanonicalDecl()), ParamType); |
Nick Lewycky | 45b5052 | 2013-02-02 00:25:55 +0000 | [diff] [blame] | 4613 | S.MarkAnyDeclReferenced(Arg->getLocStart(), Entity, false); |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4614 | return false; |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4615 | } |
| 4616 | |
| 4617 | /// \brief Checks whether the given template argument is a pointer to |
| 4618 | /// member constant according to C++ [temp.arg.nontype]p1. |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4619 | static bool CheckTemplateArgumentPointerToMember(Sema &S, |
| 4620 | NonTypeTemplateParmDecl *Param, |
| 4621 | QualType ParamType, |
| 4622 | Expr *&ResultArg, |
| 4623 | TemplateArgument &Converted) { |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4624 | bool Invalid = false; |
| 4625 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4626 | // Check for a null pointer value. |
| 4627 | Expr *Arg = ResultArg; |
| 4628 | switch (isNullPointerValueTemplateArgument(S, Param, ParamType, Arg)) { |
| 4629 | case NPV_Error: |
| 4630 | return true; |
| 4631 | case NPV_NullPointer: |
Richard Smith | bc8c5b5 | 2012-04-26 01:51:03 +0000 | [diff] [blame] | 4632 | S.Diag(Arg->getExprLoc(), diag::warn_cxx98_compat_template_arg_null); |
Richard Smith | 78bb36f | 2014-07-24 02:27:39 +0000 | [diff] [blame] | 4633 | Converted = TemplateArgument(S.Context.getCanonicalType(ParamType), |
| 4634 | /*isNullPtr*/true); |
David Majnemer | 763584d | 2014-02-06 10:59:19 +0000 | [diff] [blame] | 4635 | if (S.Context.getTargetInfo().getCXXABI().isMicrosoft()) |
| 4636 | S.RequireCompleteType(Arg->getExprLoc(), ParamType, 0); |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4637 | return false; |
| 4638 | case NPV_NotNullPointer: |
| 4639 | break; |
| 4640 | } |
| 4641 | |
| 4642 | bool ObjCLifetimeConversion; |
| 4643 | if (S.IsQualificationConversion(Arg->getType(), |
| 4644 | ParamType.getNonReferenceType(), |
| 4645 | false, ObjCLifetimeConversion)) { |
| 4646 | Arg = S.ImpCastExprToType(Arg, ParamType, CK_NoOp, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 4647 | Arg->getValueKind()).get(); |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4648 | ResultArg = Arg; |
| 4649 | } else if (!S.Context.hasSameUnqualifiedType(Arg->getType(), |
| 4650 | ParamType.getNonReferenceType())) { |
| 4651 | // We can't perform this conversion. |
| 4652 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_convertible) |
| 4653 | << Arg->getType() << ParamType << Arg->getSourceRange(); |
| 4654 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4655 | return true; |
| 4656 | } |
| 4657 | |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4658 | // See through any implicit casts we added to fix the type. |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 4659 | while (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(Arg)) |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4660 | Arg = Cast->getSubExpr(); |
| 4661 | |
| 4662 | // C++ [temp.arg.nontype]p1: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4663 | // |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4664 | // A template-argument for a non-type, non-template |
| 4665 | // template-parameter shall be one of: [...] |
| 4666 | // |
| 4667 | // -- a pointer to member expressed as described in 5.3.1. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4668 | DeclRefExpr *DRE = nullptr; |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4669 | |
Abramo Bagnara | 6a0c409 | 2010-09-13 06:06:58 +0000 | [diff] [blame] | 4670 | // In C++98/03 mode, give an extension warning on any extra parentheses. |
| 4671 | // See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#773 |
| 4672 | bool ExtraParens = false; |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4673 | while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) { |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 4674 | if (!Invalid && !ExtraParens) { |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4675 | S.Diag(Arg->getLocStart(), |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4676 | S.getLangOpts().CPlusPlus11 ? |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4677 | diag::warn_cxx98_compat_template_arg_extra_parens : |
| 4678 | diag::ext_template_arg_extra_parens) |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4679 | << Arg->getSourceRange(); |
Abramo Bagnara | 6a0c409 | 2010-09-13 06:06:58 +0000 | [diff] [blame] | 4680 | ExtraParens = true; |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4681 | } |
| 4682 | |
| 4683 | Arg = Parens->getSubExpr(); |
| 4684 | } |
| 4685 | |
John McCall | 7c454bb | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 4686 | while (SubstNonTypeTemplateParmExpr *subst = |
| 4687 | dyn_cast<SubstNonTypeTemplateParmExpr>(Arg)) |
| 4688 | Arg = subst->getReplacement()->IgnoreImpCasts(); |
| 4689 | |
Douglas Gregor | 49ba3ca | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 4690 | // A pointer-to-member constant written &Class::member. |
| 4691 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4692 | if (UnOp->getOpcode() == UO_AddrOf) { |
Douglas Gregor | 4bd90e5 | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 4693 | DRE = dyn_cast<DeclRefExpr>(UnOp->getSubExpr()); |
| 4694 | if (DRE && !DRE->getQualifier()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4695 | DRE = nullptr; |
Douglas Gregor | 4bd90e5 | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 4696 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4697 | } |
Douglas Gregor | 49ba3ca | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 4698 | // A constant of pointer-to-member type. |
| 4699 | else if ((DRE = dyn_cast<DeclRefExpr>(Arg))) { |
| 4700 | if (ValueDecl *VD = dyn_cast<ValueDecl>(DRE->getDecl())) { |
| 4701 | if (VD->getType()->isMemberPointerType()) { |
David Majnemer | cd053cd | 2013-12-10 00:40:58 +0000 | [diff] [blame] | 4702 | if (isa<NonTypeTemplateParmDecl>(VD)) { |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 4703 | if (Arg->isTypeDependent() || Arg->isValueDependent()) { |
John McCall | c3007a2 | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 4704 | Converted = TemplateArgument(Arg); |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 4705 | } else { |
| 4706 | VD = cast<ValueDecl>(VD->getCanonicalDecl()); |
David Blaikie | 0f62c8d | 2014-10-16 04:21:25 +0000 | [diff] [blame] | 4707 | Converted = TemplateArgument(VD, ParamType); |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 4708 | } |
Douglas Gregor | 49ba3ca | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 4709 | return Invalid; |
| 4710 | } |
| 4711 | } |
| 4712 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4713 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4714 | DRE = nullptr; |
Douglas Gregor | 49ba3ca | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 4715 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4716 | |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4717 | if (!DRE) |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4718 | return S.Diag(Arg->getLocStart(), |
| 4719 | diag::err_template_arg_not_pointer_to_member_form) |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4720 | << Arg->getSourceRange(); |
| 4721 | |
David Majnemer | 3ac84e6 | 2013-10-22 21:56:38 +0000 | [diff] [blame] | 4722 | if (isa<FieldDecl>(DRE->getDecl()) || |
| 4723 | isa<IndirectFieldDecl>(DRE->getDecl()) || |
| 4724 | isa<CXXMethodDecl>(DRE->getDecl())) { |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4725 | assert((isa<FieldDecl>(DRE->getDecl()) || |
David Majnemer | 3ac84e6 | 2013-10-22 21:56:38 +0000 | [diff] [blame] | 4726 | isa<IndirectFieldDecl>(DRE->getDecl()) || |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4727 | !cast<CXXMethodDecl>(DRE->getDecl())->isStatic()) && |
| 4728 | "Only non-static member pointers can make it here"); |
| 4729 | |
| 4730 | // Okay: this is the address of a non-static member, and therefore |
| 4731 | // a member pointer constant. |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 4732 | if (Arg->isTypeDependent() || Arg->isValueDependent()) { |
John McCall | c3007a2 | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 4733 | Converted = TemplateArgument(Arg); |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 4734 | } else { |
| 4735 | ValueDecl *D = cast<ValueDecl>(DRE->getDecl()->getCanonicalDecl()); |
David Blaikie | 0f62c8d | 2014-10-16 04:21:25 +0000 | [diff] [blame] | 4736 | Converted = TemplateArgument(D, ParamType); |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 4737 | } |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4738 | return Invalid; |
| 4739 | } |
| 4740 | |
| 4741 | // We found something else, but we don't know specifically what it is. |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4742 | S.Diag(Arg->getLocStart(), |
| 4743 | diag::err_template_arg_not_pointer_to_member_form) |
| 4744 | << Arg->getSourceRange(); |
| 4745 | S.Diag(DRE->getDecl()->getLocation(), diag::note_template_arg_refers_here); |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4746 | return true; |
| 4747 | } |
| 4748 | |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 4749 | /// \brief Check a template argument against its corresponding |
| 4750 | /// non-type template parameter. |
| 4751 | /// |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 4752 | /// This routine implements the semantics of C++ [temp.arg.nontype]. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4753 | /// If an error occurred, it returns ExprError(); otherwise, it |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 4754 | /// returns the converted template argument. \p ParamType is the |
| 4755 | /// type of the non-type template parameter after it has been instantiated. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4756 | ExprResult Sema::CheckTemplateArgument(NonTypeTemplateParmDecl *Param, |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 4757 | QualType ParamType, Expr *Arg, |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4758 | TemplateArgument &Converted, |
| 4759 | CheckTemplateArgumentKind CTAK) { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4760 | SourceLocation StartLoc = Arg->getLocStart(); |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 4761 | |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 4762 | // If either the parameter has a dependent type or the argument is |
| 4763 | // type-dependent, there's nothing we can check now. |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 4764 | if (ParamType->isDependentType() || Arg->isTypeDependent()) { |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 4765 | // FIXME: Produce a cloned, canonical expression? |
Douglas Gregor | 74eba0b | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 4766 | Converted = TemplateArgument(Arg); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 4767 | return Arg; |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 4768 | } |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 4769 | |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 4770 | // We should have already dropped all cv-qualifiers by now. |
| 4771 | assert(!ParamType.hasQualifiers() && |
| 4772 | "non-type template parameter type cannot be qualified"); |
| 4773 | |
| 4774 | if (CTAK == CTAK_Deduced && |
| 4775 | !Context.hasSameUnqualifiedType(ParamType, Arg->getType())) { |
| 4776 | // C++ [temp.deduct.type]p17: |
| 4777 | // If, in the declaration of a function template with a non-type |
| 4778 | // template-parameter, the non-type template-parameter is used |
| 4779 | // in an expression in the function parameter-list and, if the |
| 4780 | // corresponding template-argument is deduced, the |
| 4781 | // template-argument type shall match the type of the |
| 4782 | // template-parameter exactly, except that a template-argument |
| 4783 | // deduced from an array bound may be of any integral type. |
| 4784 | Diag(StartLoc, diag::err_deduced_non_type_template_arg_type_mismatch) |
| 4785 | << Arg->getType().getUnqualifiedType() |
| 4786 | << ParamType.getUnqualifiedType(); |
| 4787 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 4788 | return ExprError(); |
| 4789 | } |
| 4790 | |
Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 4791 | if (getLangOpts().CPlusPlus1z) { |
| 4792 | // FIXME: We can do some limited checking for a value-dependent but not |
| 4793 | // type-dependent argument. |
| 4794 | if (Arg->isValueDependent()) { |
| 4795 | Converted = TemplateArgument(Arg); |
| 4796 | return Arg; |
| 4797 | } |
| 4798 | |
| 4799 | // C++1z [temp.arg.nontype]p1: |
| 4800 | // A template-argument for a non-type template parameter shall be |
| 4801 | // a converted constant expression of the type of the template-parameter. |
| 4802 | APValue Value; |
| 4803 | ExprResult ArgResult = CheckConvertedConstantExpression( |
| 4804 | Arg, ParamType, Value, CCEK_TemplateArg); |
| 4805 | if (ArgResult.isInvalid()) |
| 4806 | return ExprError(); |
| 4807 | |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 4808 | QualType CanonParamType = Context.getCanonicalType(ParamType); |
| 4809 | |
Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 4810 | // Convert the APValue to a TemplateArgument. |
| 4811 | switch (Value.getKind()) { |
| 4812 | case APValue::Uninitialized: |
| 4813 | assert(ParamType->isNullPtrType()); |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 4814 | Converted = TemplateArgument(CanonParamType, /*isNullPtr*/true); |
Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 4815 | break; |
| 4816 | case APValue::Int: |
| 4817 | assert(ParamType->isIntegralOrEnumerationType()); |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 4818 | Converted = TemplateArgument(Context, Value.getInt(), CanonParamType); |
Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 4819 | break; |
| 4820 | case APValue::MemberPointer: { |
| 4821 | assert(ParamType->isMemberPointerType()); |
| 4822 | |
| 4823 | // FIXME: We need TemplateArgument representation and mangling for these. |
| 4824 | if (!Value.getMemberPointerPath().empty()) { |
| 4825 | Diag(Arg->getLocStart(), |
| 4826 | diag::err_template_arg_member_ptr_base_derived_not_supported) |
| 4827 | << Value.getMemberPointerDecl() << ParamType |
| 4828 | << Arg->getSourceRange(); |
| 4829 | return ExprError(); |
| 4830 | } |
| 4831 | |
| 4832 | auto *VD = const_cast<ValueDecl*>(Value.getMemberPointerDecl()); |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 4833 | Converted = VD ? TemplateArgument(VD, CanonParamType) |
| 4834 | : TemplateArgument(CanonParamType, /*isNullPtr*/true); |
Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 4835 | break; |
| 4836 | } |
| 4837 | case APValue::LValue: { |
| 4838 | // For a non-type template-parameter of pointer or reference type, |
| 4839 | // the value of the constant expression shall not refer to |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 4840 | assert(ParamType->isPointerType() || ParamType->isReferenceType() || |
| 4841 | ParamType->isNullPtrType()); |
Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 4842 | // -- a temporary object |
| 4843 | // -- a string literal |
| 4844 | // -- the result of a typeid expression, or |
| 4845 | // -- a predefind __func__ variable |
| 4846 | if (auto *E = Value.getLValueBase().dyn_cast<const Expr*>()) { |
| 4847 | if (isa<CXXUuidofExpr>(E)) { |
| 4848 | Converted = TemplateArgument(const_cast<Expr*>(E)); |
| 4849 | break; |
| 4850 | } |
| 4851 | Diag(Arg->getLocStart(), diag::err_template_arg_not_decl_ref) |
| 4852 | << Arg->getSourceRange(); |
| 4853 | return ExprError(); |
| 4854 | } |
| 4855 | auto *VD = const_cast<ValueDecl *>( |
| 4856 | Value.getLValueBase().dyn_cast<const ValueDecl *>()); |
| 4857 | // -- a subobject |
| 4858 | if (Value.hasLValuePath() && Value.getLValuePath().size() == 1 && |
| 4859 | VD && VD->getType()->isArrayType() && |
| 4860 | Value.getLValuePath()[0].ArrayIndex == 0 && |
| 4861 | !Value.isLValueOnePastTheEnd() && ParamType->isPointerType()) { |
| 4862 | // Per defect report (no number yet): |
| 4863 | // ... other than a pointer to the first element of a complete array |
| 4864 | // object. |
| 4865 | } else if (!Value.hasLValuePath() || Value.getLValuePath().size() || |
| 4866 | Value.isLValueOnePastTheEnd()) { |
| 4867 | Diag(StartLoc, diag::err_non_type_template_arg_subobject) |
| 4868 | << Value.getAsString(Context, ParamType); |
| 4869 | return ExprError(); |
| 4870 | } |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 4871 | assert((VD || !ParamType->isReferenceType()) && |
Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 4872 | "null reference should not be a constant expression"); |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 4873 | assert((!VD || !ParamType->isNullPtrType()) && |
| 4874 | "non-null value of type nullptr_t?"); |
| 4875 | Converted = VD ? TemplateArgument(VD, CanonParamType) |
| 4876 | : TemplateArgument(CanonParamType, /*isNullPtr*/true); |
Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 4877 | break; |
| 4878 | } |
| 4879 | case APValue::AddrLabelDiff: |
| 4880 | return Diag(StartLoc, diag::err_non_type_template_arg_addr_label_diff); |
| 4881 | case APValue::Float: |
| 4882 | case APValue::ComplexInt: |
| 4883 | case APValue::ComplexFloat: |
| 4884 | case APValue::Vector: |
| 4885 | case APValue::Array: |
| 4886 | case APValue::Struct: |
| 4887 | case APValue::Union: |
| 4888 | llvm_unreachable("invalid kind for template argument"); |
| 4889 | } |
| 4890 | |
| 4891 | return ArgResult.get(); |
| 4892 | } |
| 4893 | |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 4894 | // C++ [temp.arg.nontype]p5: |
| 4895 | // The following conversions are performed on each expression used |
| 4896 | // as a non-type template-argument. If a non-type |
| 4897 | // template-argument cannot be converted to the type of the |
| 4898 | // corresponding template-parameter then the program is |
| 4899 | // ill-formed. |
Douglas Gregor | b90df60 | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 4900 | if (ParamType->isIntegralOrEnumerationType()) { |
Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 4901 | // C++11: |
| 4902 | // -- for a non-type template-parameter of integral or |
| 4903 | // enumeration type, conversions permitted in a converted |
| 4904 | // constant expression are applied. |
| 4905 | // |
| 4906 | // C++98: |
| 4907 | // -- for a non-type template-parameter of integral or |
| 4908 | // enumeration type, integral promotions (4.5) and integral |
| 4909 | // conversions (4.7) are applied. |
| 4910 | |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4911 | if (getLangOpts().CPlusPlus11) { |
Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 4912 | // We can't check arbitrary value-dependent arguments. |
| 4913 | // FIXME: If there's no viable conversion to the template parameter type, |
| 4914 | // we should be able to diagnose that prior to instantiation. |
| 4915 | if (Arg->isValueDependent()) { |
| 4916 | Converted = TemplateArgument(Arg); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 4917 | return Arg; |
Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 4918 | } |
| 4919 | |
| 4920 | // C++ [temp.arg.nontype]p1: |
| 4921 | // A template-argument for a non-type, non-template template-parameter |
| 4922 | // shall be one of: |
| 4923 | // |
| 4924 | // -- for a non-type template-parameter of integral or enumeration |
| 4925 | // type, a converted constant expression of the type of the |
| 4926 | // template-parameter; or |
| 4927 | llvm::APSInt Value; |
| 4928 | ExprResult ArgResult = |
| 4929 | CheckConvertedConstantExpression(Arg, ParamType, Value, |
| 4930 | CCEK_TemplateArg); |
| 4931 | if (ArgResult.isInvalid()) |
| 4932 | return ExprError(); |
| 4933 | |
| 4934 | // Widen the argument value to sizeof(parameter type). This is almost |
| 4935 | // always a no-op, except when the parameter type is bool. In |
| 4936 | // that case, this may extend the argument from 1 bit to 8 bits. |
| 4937 | QualType IntegerType = ParamType; |
| 4938 | if (const EnumType *Enum = IntegerType->getAs<EnumType>()) |
| 4939 | IntegerType = Enum->getDecl()->getIntegerType(); |
| 4940 | Value = Value.extOrTrunc(Context.getTypeSize(IntegerType)); |
| 4941 | |
Benjamin Kramer | 6003ad5 | 2012-06-07 15:09:51 +0000 | [diff] [blame] | 4942 | Converted = TemplateArgument(Context, Value, |
| 4943 | Context.getCanonicalType(ParamType)); |
Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 4944 | return ArgResult; |
| 4945 | } |
| 4946 | |
Richard Smith | 08b12f1 | 2011-10-27 22:11:44 +0000 | [diff] [blame] | 4947 | ExprResult ArgResult = DefaultLvalueConversion(Arg); |
| 4948 | if (ArgResult.isInvalid()) |
| 4949 | return ExprError(); |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 4950 | Arg = ArgResult.get(); |
Richard Smith | 08b12f1 | 2011-10-27 22:11:44 +0000 | [diff] [blame] | 4951 | |
| 4952 | QualType ArgType = Arg->getType(); |
| 4953 | |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 4954 | // C++ [temp.arg.nontype]p1: |
| 4955 | // A template-argument for a non-type, non-template |
| 4956 | // template-parameter shall be one of: |
| 4957 | // |
| 4958 | // -- an integral constant-expression of integral or enumeration |
| 4959 | // type; or |
| 4960 | // -- the name of a non-type template-parameter; or |
| 4961 | SourceLocation NonConstantLoc; |
Douglas Gregor | 264ec4f | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 4962 | llvm::APSInt Value; |
Douglas Gregor | b90df60 | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 4963 | if (!ArgType->isIntegralOrEnumerationType()) { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4964 | Diag(Arg->getLocStart(), |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 4965 | diag::err_template_arg_not_integral_or_enumeral) |
| 4966 | << ArgType << Arg->getSourceRange(); |
| 4967 | Diag(Param->getLocation(), diag::note_template_param_here); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4968 | return ExprError(); |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 4969 | } else if (!Arg->isValueDependent()) { |
Douglas Gregor | e2b3744 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 4970 | class TmplArgICEDiagnoser : public VerifyICEDiagnoser { |
| 4971 | QualType T; |
| 4972 | |
| 4973 | public: |
| 4974 | TmplArgICEDiagnoser(QualType T) : T(T) { } |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 4975 | |
| 4976 | void diagnoseNotICE(Sema &S, SourceLocation Loc, |
| 4977 | SourceRange SR) override { |
Douglas Gregor | e2b3744 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 4978 | S.Diag(Loc, diag::err_template_arg_not_ice) << T << SR; |
| 4979 | } |
| 4980 | } Diagnoser(ArgType); |
| 4981 | |
| 4982 | Arg = VerifyIntegerConstantExpression(Arg, &Value, Diagnoser, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 4983 | false).get(); |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 4984 | if (!Arg) |
| 4985 | return ExprError(); |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 4986 | } |
| 4987 | |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 4988 | // From here on out, all we care about is the unqualified form |
| 4989 | // of the argument type. |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4990 | ArgType = ArgType.getUnqualifiedType(); |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 4991 | |
| 4992 | // Try to convert the argument to the parameter's type. |
Douglas Gregor | 4d0c38a | 2009-11-04 21:50:46 +0000 | [diff] [blame] | 4993 | if (Context.hasSameType(ParamType, ArgType)) { |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 4994 | // Okay: no conversion necessary |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4995 | } else if (ParamType->isBooleanType()) { |
| 4996 | // This is an integral-to-boolean conversion. |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 4997 | Arg = ImpCastExprToType(Arg, ParamType, CK_IntegralToBoolean).get(); |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 4998 | } else if (IsIntegralPromotion(Arg, ArgType, ParamType) || |
| 4999 | !ParamType->isEnumeralType()) { |
| 5000 | // This is an integral promotion or conversion. |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 5001 | Arg = ImpCastExprToType(Arg, ParamType, CK_IntegralCast).get(); |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 5002 | } else { |
| 5003 | // We can't perform this conversion. |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 5004 | Diag(Arg->getLocStart(), |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 5005 | diag::err_template_arg_not_convertible) |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 5006 | << Arg->getType() << ParamType << Arg->getSourceRange(); |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 5007 | Diag(Param->getLocation(), diag::note_template_param_here); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5008 | return ExprError(); |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 5009 | } |
| 5010 | |
Douglas Gregor | b4f4d51 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 5011 | // Add the value of this argument to the list of converted |
| 5012 | // arguments. We use the bitwidth and signedness of the template |
| 5013 | // parameter. |
| 5014 | if (Arg->isValueDependent()) { |
| 5015 | // The argument is value-dependent. Create a new |
| 5016 | // TemplateArgument with the converted expression. |
| 5017 | Converted = TemplateArgument(Arg); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5018 | return Arg; |
Douglas Gregor | b4f4d51 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 5019 | } |
| 5020 | |
Douglas Gregor | 52aba87 | 2009-03-14 00:20:21 +0000 | [diff] [blame] | 5021 | QualType IntegerType = Context.getCanonicalType(ParamType); |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5022 | if (const EnumType *Enum = IntegerType->getAs<EnumType>()) |
Douglas Gregor | 74eba0b | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 5023 | IntegerType = Context.getCanonicalType(Enum->getDecl()->getIntegerType()); |
Douglas Gregor | 52aba87 | 2009-03-14 00:20:21 +0000 | [diff] [blame] | 5024 | |
Douglas Gregor | b4f4d51 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 5025 | if (ParamType->isBooleanType()) { |
| 5026 | // Value must be zero or one. |
| 5027 | Value = Value != 0; |
| 5028 | unsigned AllowedBits = Context.getTypeSize(IntegerType); |
| 5029 | if (Value.getBitWidth() != AllowedBits) |
| 5030 | Value = Value.extOrTrunc(AllowedBits); |
Douglas Gregor | 6ab2fa8 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 5031 | Value.setIsSigned(IntegerType->isSignedIntegerOrEnumerationType()); |
Douglas Gregor | b4f4d51 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 5032 | } else { |
Douglas Gregor | bb3d786 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 5033 | llvm::APSInt OldValue = Value; |
Douglas Gregor | b4f4d51 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 5034 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5035 | // Coerce the template argument's value to the value it will have |
Douglas Gregor | bb3d786 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 5036 | // based on the template parameter's type. |
Douglas Gregor | a14cb9f | 2010-03-26 00:39:40 +0000 | [diff] [blame] | 5037 | unsigned AllowedBits = Context.getTypeSize(IntegerType); |
Douglas Gregor | a14cb9f | 2010-03-26 00:39:40 +0000 | [diff] [blame] | 5038 | if (Value.getBitWidth() != AllowedBits) |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 5039 | Value = Value.extOrTrunc(AllowedBits); |
Douglas Gregor | 6ab2fa8 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 5040 | Value.setIsSigned(IntegerType->isSignedIntegerOrEnumerationType()); |
Douglas Gregor | b4f4d51 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 5041 | |
Douglas Gregor | bb3d786 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 5042 | // Complain if an unsigned parameter received a negative value. |
Douglas Gregor | 6ab2fa8 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 5043 | if (IntegerType->isUnsignedIntegerOrEnumerationType() |
Douglas Gregor | b4f4d51 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 5044 | && (OldValue.isSigned() && OldValue.isNegative())) { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 5045 | Diag(Arg->getLocStart(), diag::warn_template_arg_negative) |
Douglas Gregor | bb3d786 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 5046 | << OldValue.toString(10) << Value.toString(10) << Param->getType() |
| 5047 | << Arg->getSourceRange(); |
| 5048 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 5049 | } |
Douglas Gregor | b4f4d51 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 5050 | |
Douglas Gregor | bb3d786 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 5051 | // Complain if we overflowed the template parameter's type. |
| 5052 | unsigned RequiredBits; |
Douglas Gregor | 6ab2fa8 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 5053 | if (IntegerType->isUnsignedIntegerOrEnumerationType()) |
Douglas Gregor | bb3d786 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 5054 | RequiredBits = OldValue.getActiveBits(); |
| 5055 | else if (OldValue.isUnsigned()) |
| 5056 | RequiredBits = OldValue.getActiveBits() + 1; |
| 5057 | else |
| 5058 | RequiredBits = OldValue.getMinSignedBits(); |
| 5059 | if (RequiredBits > AllowedBits) { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 5060 | Diag(Arg->getLocStart(), |
Douglas Gregor | bb3d786 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 5061 | diag::warn_template_arg_too_large) |
| 5062 | << OldValue.toString(10) << Value.toString(10) << Param->getType() |
| 5063 | << Arg->getSourceRange(); |
| 5064 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 5065 | } |
Douglas Gregor | 52aba87 | 2009-03-14 00:20:21 +0000 | [diff] [blame] | 5066 | } |
Douglas Gregor | 264ec4f | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 5067 | |
Benjamin Kramer | 6003ad5 | 2012-06-07 15:09:51 +0000 | [diff] [blame] | 5068 | Converted = TemplateArgument(Context, Value, |
Douglas Gregor | 3d63a9e | 2011-08-09 01:55:14 +0000 | [diff] [blame] | 5069 | ParamType->isEnumeralType() |
| 5070 | ? Context.getCanonicalType(ParamType) |
| 5071 | : IntegerType); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5072 | return Arg; |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 5073 | } |
Douglas Gregor | 3a7796b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 5074 | |
Richard Smith | 08b12f1 | 2011-10-27 22:11:44 +0000 | [diff] [blame] | 5075 | QualType ArgType = Arg->getType(); |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 5076 | DeclAccessPair FoundResult; // temporary for ResolveOverloadedFunction |
| 5077 | |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 5078 | // Handle pointer-to-function, reference-to-function, and |
| 5079 | // pointer-to-member-function all in (roughly) the same way. |
| 5080 | if (// -- For a non-type template-parameter of type pointer to |
| 5081 | // function, only the function-to-pointer conversion (4.3) is |
| 5082 | // applied. If the template-argument represents a set of |
| 5083 | // overloaded functions (or a pointer to such), the matching |
| 5084 | // function is selected from the set (13.4). |
| 5085 | (ParamType->isPointerType() && |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5086 | ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType()) || |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 5087 | // -- For a non-type template-parameter of type reference to |
| 5088 | // function, no conversions apply. If the template-argument |
| 5089 | // represents a set of overloaded functions, the matching |
| 5090 | // function is selected from the set (13.4). |
| 5091 | (ParamType->isReferenceType() && |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5092 | ParamType->getAs<ReferenceType>()->getPointeeType()->isFunctionType()) || |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 5093 | // -- For a non-type template-parameter of type pointer to |
| 5094 | // member function, no conversions apply. If the |
| 5095 | // template-argument represents a set of overloaded member |
| 5096 | // functions, the matching member function is selected from |
| 5097 | // the set (13.4). |
| 5098 | (ParamType->isMemberPointerType() && |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5099 | ParamType->getAs<MemberPointerType>()->getPointeeType() |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 5100 | ->isFunctionType())) { |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 5101 | |
Douglas Gregor | 064fdb2 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 5102 | if (Arg->getType() == Context.OverloadTy) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5103 | if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg, ParamType, |
Douglas Gregor | 064fdb2 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 5104 | true, |
| 5105 | FoundResult)) { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 5106 | if (DiagnoseUseOfDecl(Fn, Arg->getLocStart())) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5107 | return ExprError(); |
Douglas Gregor | 064fdb2 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 5108 | |
| 5109 | Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn); |
| 5110 | ArgType = Arg->getType(); |
| 5111 | } else |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5112 | return ExprError(); |
Douglas Gregor | 3a7796b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 5113 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5114 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5115 | if (!ParamType->isMemberPointerType()) { |
| 5116 | if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param, |
| 5117 | ParamType, |
| 5118 | Arg, Converted)) |
| 5119 | return ExprError(); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5120 | return Arg; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5121 | } |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 5122 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 5123 | if (CheckTemplateArgumentPointerToMember(*this, Param, ParamType, Arg, |
| 5124 | Converted)) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5125 | return ExprError(); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5126 | return Arg; |
Douglas Gregor | 3a7796b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 5127 | } |
| 5128 | |
Chris Lattner | 696197c | 2009-02-20 21:37:53 +0000 | [diff] [blame] | 5129 | if (ParamType->isPointerType()) { |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 5130 | // -- for a non-type template-parameter of type pointer to |
| 5131 | // object, qualification conversions (4.4) and the |
| 5132 | // array-to-pointer conversion (4.2) are applied. |
Sebastian Redl | 576fd42 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 5133 | // C++0x also allows a value of std::nullptr_t. |
Eli Friedman | a170cd6 | 2010-08-05 02:49:48 +0000 | [diff] [blame] | 5134 | assert(ParamType->getPointeeType()->isIncompleteOrObjectType() && |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 5135 | "Only object pointers allowed here"); |
Douglas Gregor | a9faa44 | 2009-02-11 00:44:29 +0000 | [diff] [blame] | 5136 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5137 | if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param, |
| 5138 | ParamType, |
| 5139 | Arg, Converted)) |
| 5140 | return ExprError(); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5141 | return Arg; |
Douglas Gregor | a9faa44 | 2009-02-11 00:44:29 +0000 | [diff] [blame] | 5142 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5143 | |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5144 | if (const ReferenceType *ParamRefType = ParamType->getAs<ReferenceType>()) { |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 5145 | // -- For a non-type template-parameter of type reference to |
| 5146 | // object, no conversions apply. The type referred to by the |
| 5147 | // reference may be more cv-qualified than the (otherwise |
| 5148 | // identical) type of the template-argument. The |
| 5149 | // template-parameter is bound directly to the |
| 5150 | // template-argument, which must be an lvalue. |
Eli Friedman | a170cd6 | 2010-08-05 02:49:48 +0000 | [diff] [blame] | 5151 | assert(ParamRefType->getPointeeType()->isIncompleteOrObjectType() && |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 5152 | "Only object references allowed here"); |
Douglas Gregor | a9faa44 | 2009-02-11 00:44:29 +0000 | [diff] [blame] | 5153 | |
Douglas Gregor | 064fdb2 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 5154 | if (Arg->getType() == Context.OverloadTy) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5155 | if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg, |
| 5156 | ParamRefType->getPointeeType(), |
Douglas Gregor | 064fdb2 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 5157 | true, |
| 5158 | FoundResult)) { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 5159 | if (DiagnoseUseOfDecl(Fn, Arg->getLocStart())) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5160 | return ExprError(); |
Douglas Gregor | 064fdb2 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 5161 | |
| 5162 | Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn); |
| 5163 | ArgType = Arg->getType(); |
| 5164 | } else |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5165 | return ExprError(); |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 5166 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5167 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5168 | if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param, |
| 5169 | ParamType, |
| 5170 | Arg, Converted)) |
| 5171 | return ExprError(); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5172 | return Arg; |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 5173 | } |
Douglas Gregor | 0e55853 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 5174 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 5175 | // Deal with parameters of type std::nullptr_t. |
| 5176 | if (ParamType->isNullPtrType()) { |
| 5177 | if (Arg->isTypeDependent() || Arg->isValueDependent()) { |
| 5178 | Converted = TemplateArgument(Arg); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5179 | return Arg; |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 5180 | } |
| 5181 | |
| 5182 | switch (isNullPointerValueTemplateArgument(*this, Param, ParamType, Arg)) { |
| 5183 | case NPV_NotNullPointer: |
| 5184 | Diag(Arg->getExprLoc(), diag::err_template_arg_not_convertible) |
| 5185 | << Arg->getType() << ParamType; |
| 5186 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 5187 | return ExprError(); |
| 5188 | |
| 5189 | case NPV_Error: |
| 5190 | return ExprError(); |
| 5191 | |
| 5192 | case NPV_NullPointer: |
Richard Smith | bc8c5b5 | 2012-04-26 01:51:03 +0000 | [diff] [blame] | 5193 | Diag(Arg->getExprLoc(), diag::warn_cxx98_compat_template_arg_null); |
Richard Smith | 78bb36f | 2014-07-24 02:27:39 +0000 | [diff] [blame] | 5194 | Converted = TemplateArgument(Context.getCanonicalType(ParamType), |
| 5195 | /*isNullPtr*/true); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5196 | return Arg; |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 5197 | } |
| 5198 | } |
| 5199 | |
Douglas Gregor | 0e55853 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 5200 | // -- For a non-type template-parameter of type pointer to data |
| 5201 | // member, qualification conversions (4.4) are applied. |
| 5202 | assert(ParamType->isMemberPointerType() && "Only pointers to members remain"); |
| 5203 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 5204 | if (CheckTemplateArgumentPointerToMember(*this, Param, ParamType, Arg, |
| 5205 | Converted)) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5206 | return ExprError(); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5207 | return Arg; |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 5208 | } |
| 5209 | |
| 5210 | /// \brief Check a template argument against its corresponding |
| 5211 | /// template template parameter. |
| 5212 | /// |
| 5213 | /// This routine implements the semantics of C++ [temp.arg.template]. |
| 5214 | /// It returns true if an error occurred, and false otherwise. |
| 5215 | bool Sema::CheckTemplateArgument(TemplateTemplateParmDecl *Param, |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 5216 | TemplateArgumentLoc &Arg, |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 5217 | unsigned ArgumentPackIndex) { |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 5218 | TemplateName Name = Arg.getArgument().getAsTemplateOrTemplatePattern(); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 5219 | TemplateDecl *Template = Name.getAsTemplateDecl(); |
| 5220 | if (!Template) { |
| 5221 | // Any dependent template name is fine. |
| 5222 | assert(Name.isDependent() && "Non-dependent template isn't a declaration?"); |
| 5223 | return false; |
| 5224 | } |
Douglas Gregor | 85e0f66 | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 5225 | |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 5226 | // C++0x [temp.arg.template]p1: |
Douglas Gregor | 85e0f66 | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 5227 | // A template-argument for a template template-parameter shall be |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 5228 | // the name of a class template or an alias template, expressed as an |
| 5229 | // id-expression. When the template-argument names a class template, only |
Douglas Gregor | 85e0f66 | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 5230 | // primary class templates are considered when matching the |
| 5231 | // template template argument with the corresponding parameter; |
| 5232 | // partial specializations are not considered even if their |
| 5233 | // parameter lists match that of the template template parameter. |
Douglas Gregor | d522205 | 2009-06-12 19:43:02 +0000 | [diff] [blame] | 5234 | // |
| 5235 | // Note that we also allow template template parameters here, which |
| 5236 | // will happen when we are dealing with, e.g., class template |
| 5237 | // partial specializations. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5238 | if (!isa<ClassTemplateDecl>(Template) && |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 5239 | !isa<TemplateTemplateParmDecl>(Template) && |
| 5240 | !isa<TypeAliasTemplateDecl>(Template)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5241 | assert(isa<FunctionTemplateDecl>(Template) && |
Douglas Gregor | 85e0f66 | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 5242 | "Only function templates are possible here"); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 5243 | Diag(Arg.getLocation(), diag::err_template_arg_not_class_template); |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 5244 | Diag(Template->getLocation(), diag::note_template_arg_refers_here_func) |
Douglas Gregor | 85e0f66 | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 5245 | << Template; |
| 5246 | } |
| 5247 | |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 5248 | TemplateParameterList *Params = Param->getTemplateParameters(); |
| 5249 | if (Param->isExpandedParameterPack()) |
| 5250 | Params = Param->getExpansionTemplateParameters(ArgumentPackIndex); |
| 5251 | |
Douglas Gregor | 85e0f66 | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 5252 | return !TemplateParameterListsAreEqual(Template->getTemplateParameters(), |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 5253 | Params, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5254 | true, |
Douglas Gregor | 19ac2d6 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 5255 | TPL_TemplateTemplateArgumentMatch, |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 5256 | Arg.getLocation()); |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 5257 | } |
| 5258 | |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5259 | /// \brief Given a non-type template argument that refers to a |
| 5260 | /// declaration and the type of its corresponding non-type template |
| 5261 | /// parameter, produce an expression that properly refers to that |
| 5262 | /// declaration. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5263 | ExprResult |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5264 | Sema::BuildExpressionFromDeclTemplateArgument(const TemplateArgument &Arg, |
| 5265 | QualType ParamType, |
| 5266 | SourceLocation Loc) { |
David Blaikie | dc601e3 | 2013-02-27 22:10:40 +0000 | [diff] [blame] | 5267 | // C++ [temp.param]p8: |
| 5268 | // |
| 5269 | // A non-type template-parameter of type "array of T" or |
| 5270 | // "function returning T" is adjusted to be of type "pointer to |
| 5271 | // T" or "pointer to function returning T", respectively. |
| 5272 | if (ParamType->isArrayType()) |
| 5273 | ParamType = Context.getArrayDecayedType(ParamType); |
| 5274 | else if (ParamType->isFunctionType()) |
| 5275 | ParamType = Context.getPointerType(ParamType); |
| 5276 | |
Douglas Gregor | 31f55dc | 2012-04-06 22:40:38 +0000 | [diff] [blame] | 5277 | // For a NULL non-type template argument, return nullptr casted to the |
| 5278 | // parameter's type. |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 5279 | if (Arg.getKind() == TemplateArgument::NullPtr) { |
Douglas Gregor | 31f55dc | 2012-04-06 22:40:38 +0000 | [diff] [blame] | 5280 | return ImpCastExprToType( |
| 5281 | new (Context) CXXNullPtrLiteralExpr(Context.NullPtrTy, Loc), |
| 5282 | ParamType, |
| 5283 | ParamType->getAs<MemberPointerType>() |
| 5284 | ? CK_NullToMemberPointer |
| 5285 | : CK_NullToPointer); |
| 5286 | } |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 5287 | assert(Arg.getKind() == TemplateArgument::Declaration && |
| 5288 | "Only declaration template arguments permitted here"); |
| 5289 | |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5290 | ValueDecl *VD = cast<ValueDecl>(Arg.getAsDecl()); |
| 5291 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5292 | if (VD->getDeclContext()->isRecord() && |
David Majnemer | 3ae0bfa | 2013-10-26 05:02:13 +0000 | [diff] [blame] | 5293 | (isa<CXXMethodDecl>(VD) || isa<FieldDecl>(VD) || |
| 5294 | isa<IndirectFieldDecl>(VD))) { |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5295 | // If the value is a class member, we might have a pointer-to-member. |
| 5296 | // Determine whether the non-type template template parameter is of |
| 5297 | // pointer-to-member type. If so, we need to build an appropriate |
| 5298 | // expression for a pointer-to-member, since a "normal" DeclRefExpr |
| 5299 | // would refer to the member itself. |
| 5300 | if (ParamType->isMemberPointerType()) { |
| 5301 | QualType ClassType |
| 5302 | = Context.getTypeDeclType(cast<RecordDecl>(VD->getDeclContext())); |
| 5303 | NestedNameSpecifier *Qualifier |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5304 | = NestedNameSpecifier::Create(Context, nullptr, false, |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5305 | ClassType.getTypePtr()); |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5306 | CXXScopeSpec SS; |
Douglas Gregor | 869ad45 | 2011-02-24 17:54:50 +0000 | [diff] [blame] | 5307 | SS.MakeTrivial(Context, Qualifier, Loc); |
John McCall | feb624a | 2010-11-23 20:48:44 +0000 | [diff] [blame] | 5308 | |
| 5309 | // The actual value-ness of this is unimportant, but for |
| 5310 | // internal consistency's sake, references to instance methods |
| 5311 | // are r-values. |
| 5312 | ExprValueKind VK = VK_LValue; |
| 5313 | if (isa<CXXMethodDecl>(VD) && cast<CXXMethodDecl>(VD)->isInstance()) |
| 5314 | VK = VK_RValue; |
| 5315 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5316 | ExprResult RefExpr = BuildDeclRefExpr(VD, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5317 | VD->getType().getNonReferenceType(), |
John McCall | feb624a | 2010-11-23 20:48:44 +0000 | [diff] [blame] | 5318 | VK, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5319 | Loc, |
| 5320 | &SS); |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5321 | if (RefExpr.isInvalid()) |
| 5322 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5323 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5324 | RefExpr = CreateBuiltinUnaryOp(Loc, UO_AddrOf, RefExpr.get()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5325 | |
Douglas Gregor | fabf95d | 2010-04-30 21:46:38 +0000 | [diff] [blame] | 5326 | // We might need to perform a trailing qualification conversion, since |
| 5327 | // the element type on the parameter could be more qualified than the |
| 5328 | // element type in the expression we constructed. |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5329 | bool ObjCLifetimeConversion; |
Douglas Gregor | fabf95d | 2010-04-30 21:46:38 +0000 | [diff] [blame] | 5330 | if (IsQualificationConversion(((Expr*) RefExpr.get())->getType(), |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5331 | ParamType.getUnqualifiedType(), false, |
| 5332 | ObjCLifetimeConversion)) |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 5333 | RefExpr = ImpCastExprToType(RefExpr.get(), ParamType.getUnqualifiedType(), CK_NoOp); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5334 | |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5335 | assert(!RefExpr.isInvalid() && |
| 5336 | Context.hasSameType(((Expr*) RefExpr.get())->getType(), |
Douglas Gregor | fabf95d | 2010-04-30 21:46:38 +0000 | [diff] [blame] | 5337 | ParamType.getUnqualifiedType())); |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5338 | return RefExpr; |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5339 | } |
| 5340 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5341 | |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5342 | QualType T = VD->getType().getNonReferenceType(); |
Douglas Gregor | effe2a1 | 2013-01-16 00:52:15 +0000 | [diff] [blame] | 5343 | |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5344 | if (ParamType->isPointerType()) { |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 5345 | // When the non-type template parameter is a pointer, take the |
| 5346 | // address of the declaration. |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5347 | ExprResult RefExpr = BuildDeclRefExpr(VD, T, VK_LValue, Loc); |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5348 | if (RefExpr.isInvalid()) |
| 5349 | return ExprError(); |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 5350 | |
| 5351 | if (T->isFunctionType() || T->isArrayType()) { |
| 5352 | // Decay functions and arrays. |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 5353 | RefExpr = DefaultFunctionArrayConversion(RefExpr.get()); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5354 | if (RefExpr.isInvalid()) |
| 5355 | return ExprError(); |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 5356 | |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5357 | return RefExpr; |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5358 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5359 | |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 5360 | // Take the address of everything else |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5361 | return CreateBuiltinUnaryOp(Loc, UO_AddrOf, RefExpr.get()); |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5362 | } |
| 5363 | |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5364 | ExprValueKind VK = VK_RValue; |
| 5365 | |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5366 | // If the non-type template parameter has reference type, qualify the |
| 5367 | // resulting declaration reference with the extra qualifiers on the |
| 5368 | // type that the reference refers to. |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5369 | if (const ReferenceType *TargetRef = ParamType->getAs<ReferenceType>()) { |
| 5370 | VK = VK_LValue; |
| 5371 | T = Context.getQualifiedType(T, |
| 5372 | TargetRef->getPointeeType().getQualifiers()); |
Douglas Gregor | effe2a1 | 2013-01-16 00:52:15 +0000 | [diff] [blame] | 5373 | } else if (isa<FunctionDecl>(VD)) { |
| 5374 | // References to functions are always lvalues. |
| 5375 | VK = VK_LValue; |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5376 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5377 | |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5378 | return BuildDeclRefExpr(VD, T, VK, Loc); |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5379 | } |
| 5380 | |
| 5381 | /// \brief Construct a new expression that refers to the given |
| 5382 | /// integral template argument with the given source-location |
| 5383 | /// information. |
| 5384 | /// |
| 5385 | /// This routine takes care of the mapping from an integral template |
| 5386 | /// argument (which may have any integral type) to the appropriate |
| 5387 | /// literal value. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5388 | ExprResult |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5389 | Sema::BuildExpressionFromIntegralTemplateArgument(const TemplateArgument &Arg, |
| 5390 | SourceLocation Loc) { |
| 5391 | assert(Arg.getKind() == TemplateArgument::Integral && |
Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 5392 | "Operation is only valid for integral template arguments"); |
Benjamin Kramer | 3d3ddce | 2012-11-21 17:42:47 +0000 | [diff] [blame] | 5393 | QualType OrigT = Arg.getIntegralType(); |
| 5394 | |
| 5395 | // If this is an enum type that we're instantiating, we need to use an integer |
| 5396 | // type the same size as the enumerator. We don't want to build an |
| 5397 | // IntegerLiteral with enum type. The integer type of an enum type can be of |
| 5398 | // any integral type with C++11 enum classes, make sure we create the right |
| 5399 | // type of literal for it. |
| 5400 | QualType T = OrigT; |
| 5401 | if (const EnumType *ET = OrigT->getAs<EnumType>()) |
| 5402 | T = ET->getDecl()->getIntegerType(); |
| 5403 | |
| 5404 | Expr *E; |
Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 5405 | if (T->isAnyCharacterType()) { |
| 5406 | CharacterLiteral::CharacterKind Kind; |
| 5407 | if (T->isWideCharType()) |
| 5408 | Kind = CharacterLiteral::Wide; |
| 5409 | else if (T->isChar16Type()) |
| 5410 | Kind = CharacterLiteral::UTF16; |
| 5411 | else if (T->isChar32Type()) |
| 5412 | Kind = CharacterLiteral::UTF32; |
| 5413 | else |
| 5414 | Kind = CharacterLiteral::Ascii; |
| 5415 | |
Benjamin Kramer | 3d3ddce | 2012-11-21 17:42:47 +0000 | [diff] [blame] | 5416 | E = new (Context) CharacterLiteral(Arg.getAsIntegral().getZExtValue(), |
| 5417 | Kind, T, Loc); |
| 5418 | } else if (T->isBooleanType()) { |
| 5419 | E = new (Context) CXXBoolLiteralExpr(Arg.getAsIntegral().getBoolValue(), |
| 5420 | T, Loc); |
| 5421 | } else if (T->isNullPtrType()) { |
| 5422 | E = new (Context) CXXNullPtrLiteralExpr(Context.NullPtrTy, Loc); |
| 5423 | } else { |
| 5424 | E = IntegerLiteral::Create(Context, Arg.getAsIntegral(), T, Loc); |
Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 5425 | } |
| 5426 | |
Benjamin Kramer | 3d3ddce | 2012-11-21 17:42:47 +0000 | [diff] [blame] | 5427 | if (OrigT->isEnumeralType()) { |
John McCall | 6730e4d | 2011-07-15 07:47:58 +0000 | [diff] [blame] | 5428 | // FIXME: This is a hack. We need a better way to handle substituted |
| 5429 | // non-type template parameters. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5430 | E = CStyleCastExpr::Create(Context, OrigT, VK_RValue, CK_IntegralCast, E, |
| 5431 | nullptr, |
Benjamin Kramer | 3d3ddce | 2012-11-21 17:42:47 +0000 | [diff] [blame] | 5432 | Context.getTrivialTypeSourceInfo(OrigT, Loc), |
John McCall | 6730e4d | 2011-07-15 07:47:58 +0000 | [diff] [blame] | 5433 | Loc, Loc); |
| 5434 | } |
| 5435 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5436 | return E; |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5437 | } |
| 5438 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5439 | /// \brief Match two template parameters within template parameter lists. |
| 5440 | static bool MatchTemplateParameterKind(Sema &S, NamedDecl *New, NamedDecl *Old, |
| 5441 | bool Complain, |
| 5442 | Sema::TemplateParameterListEqualKind Kind, |
| 5443 | SourceLocation TemplateArgLoc) { |
| 5444 | // Check the actual kind (type, non-type, template). |
| 5445 | if (Old->getKind() != New->getKind()) { |
| 5446 | if (Complain) { |
| 5447 | unsigned NextDiag = diag::err_template_param_different_kind; |
| 5448 | if (TemplateArgLoc.isValid()) { |
| 5449 | S.Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch); |
| 5450 | NextDiag = diag::note_template_param_different_kind; |
| 5451 | } |
| 5452 | S.Diag(New->getLocation(), NextDiag) |
| 5453 | << (Kind != Sema::TPL_TemplateMatch); |
| 5454 | S.Diag(Old->getLocation(), diag::note_template_prev_declaration) |
| 5455 | << (Kind != Sema::TPL_TemplateMatch); |
| 5456 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5457 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5458 | return false; |
| 5459 | } |
| 5460 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5461 | // Check that both are parameter packs are neither are parameter packs. |
| 5462 | // However, if we are matching a template template argument to a |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 5463 | // template template parameter, the template template parameter can have |
| 5464 | // a parameter pack where the template template argument does not. |
| 5465 | if (Old->isTemplateParameterPack() != New->isTemplateParameterPack() && |
| 5466 | !(Kind == Sema::TPL_TemplateTemplateArgumentMatch && |
| 5467 | Old->isTemplateParameterPack())) { |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5468 | if (Complain) { |
| 5469 | unsigned NextDiag = diag::err_template_parameter_pack_non_pack; |
| 5470 | if (TemplateArgLoc.isValid()) { |
| 5471 | S.Diag(TemplateArgLoc, |
| 5472 | diag::err_template_arg_template_params_mismatch); |
| 5473 | NextDiag = diag::note_template_parameter_pack_non_pack; |
| 5474 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5475 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5476 | unsigned ParamKind = isa<TemplateTypeParmDecl>(New)? 0 |
| 5477 | : isa<NonTypeTemplateParmDecl>(New)? 1 |
| 5478 | : 2; |
| 5479 | S.Diag(New->getLocation(), NextDiag) |
| 5480 | << ParamKind << New->isParameterPack(); |
| 5481 | S.Diag(Old->getLocation(), diag::note_template_parameter_pack_here) |
| 5482 | << ParamKind << Old->isParameterPack(); |
| 5483 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5484 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5485 | return false; |
| 5486 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5487 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5488 | // For non-type template parameters, check the type of the parameter. |
| 5489 | if (NonTypeTemplateParmDecl *OldNTTP |
| 5490 | = dyn_cast<NonTypeTemplateParmDecl>(Old)) { |
| 5491 | NonTypeTemplateParmDecl *NewNTTP = cast<NonTypeTemplateParmDecl>(New); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5492 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5493 | // If we are matching a template template argument to a template |
| 5494 | // template parameter and one of the non-type template parameter types |
| 5495 | // is dependent, then we must wait until template instantiation time |
| 5496 | // to actually compare the arguments. |
| 5497 | if (Kind == Sema::TPL_TemplateTemplateArgumentMatch && |
| 5498 | (OldNTTP->getType()->isDependentType() || |
| 5499 | NewNTTP->getType()->isDependentType())) |
| 5500 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5501 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5502 | if (!S.Context.hasSameType(OldNTTP->getType(), NewNTTP->getType())) { |
| 5503 | if (Complain) { |
| 5504 | unsigned NextDiag = diag::err_template_nontype_parm_different_type; |
| 5505 | if (TemplateArgLoc.isValid()) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5506 | S.Diag(TemplateArgLoc, |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5507 | diag::err_template_arg_template_params_mismatch); |
| 5508 | NextDiag = diag::note_template_nontype_parm_different_type; |
| 5509 | } |
| 5510 | S.Diag(NewNTTP->getLocation(), NextDiag) |
| 5511 | << NewNTTP->getType() |
| 5512 | << (Kind != Sema::TPL_TemplateMatch); |
| 5513 | S.Diag(OldNTTP->getLocation(), |
| 5514 | diag::note_template_nontype_parm_prev_declaration) |
| 5515 | << OldNTTP->getType(); |
| 5516 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5517 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5518 | return false; |
| 5519 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5520 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5521 | return true; |
| 5522 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5523 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5524 | // For template template parameters, check the template parameter types. |
| 5525 | // The template parameter lists of template template |
| 5526 | // parameters must agree. |
| 5527 | if (TemplateTemplateParmDecl *OldTTP |
| 5528 | = dyn_cast<TemplateTemplateParmDecl>(Old)) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5529 | TemplateTemplateParmDecl *NewTTP = cast<TemplateTemplateParmDecl>(New); |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5530 | return S.TemplateParameterListsAreEqual(NewTTP->getTemplateParameters(), |
| 5531 | OldTTP->getTemplateParameters(), |
| 5532 | Complain, |
| 5533 | (Kind == Sema::TPL_TemplateMatch |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5534 | ? Sema::TPL_TemplateTemplateParmMatch |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5535 | : Kind), |
| 5536 | TemplateArgLoc); |
| 5537 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5538 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5539 | return true; |
| 5540 | } |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5541 | |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 5542 | /// \brief Diagnose a known arity mismatch when comparing template argument |
| 5543 | /// lists. |
| 5544 | static |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5545 | void DiagnoseTemplateParameterListArityMismatch(Sema &S, |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 5546 | TemplateParameterList *New, |
| 5547 | TemplateParameterList *Old, |
| 5548 | Sema::TemplateParameterListEqualKind Kind, |
| 5549 | SourceLocation TemplateArgLoc) { |
| 5550 | unsigned NextDiag = diag::err_template_param_list_different_arity; |
| 5551 | if (TemplateArgLoc.isValid()) { |
| 5552 | S.Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch); |
| 5553 | NextDiag = diag::note_template_param_list_different_arity; |
| 5554 | } |
| 5555 | S.Diag(New->getTemplateLoc(), NextDiag) |
| 5556 | << (New->size() > Old->size()) |
| 5557 | << (Kind != Sema::TPL_TemplateMatch) |
| 5558 | << SourceRange(New->getTemplateLoc(), New->getRAngleLoc()); |
| 5559 | S.Diag(Old->getTemplateLoc(), diag::note_template_prev_declaration) |
| 5560 | << (Kind != Sema::TPL_TemplateMatch) |
| 5561 | << SourceRange(Old->getTemplateLoc(), Old->getRAngleLoc()); |
| 5562 | } |
| 5563 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 5564 | /// \brief Determine whether the given template parameter lists are |
| 5565 | /// equivalent. |
| 5566 | /// |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5567 | /// \param New The new template parameter list, typically written in the |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 5568 | /// source code as part of a new template declaration. |
| 5569 | /// |
| 5570 | /// \param Old The old template parameter list, typically found via |
| 5571 | /// name lookup of the template declared with this template parameter |
| 5572 | /// list. |
| 5573 | /// |
| 5574 | /// \param Complain If true, this routine will produce a diagnostic if |
| 5575 | /// the template parameter lists are not equivalent. |
| 5576 | /// |
Douglas Gregor | 19ac2d6 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 5577 | /// \param Kind describes how we are to match the template parameter lists. |
Douglas Gregor | 85e0f66 | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 5578 | /// |
| 5579 | /// \param TemplateArgLoc If this source location is valid, then we |
| 5580 | /// are actually checking the template parameter list of a template |
| 5581 | /// argument (New) against the template parameter list of its |
| 5582 | /// corresponding template template parameter (Old). We produce |
| 5583 | /// slightly different diagnostics in this scenario. |
| 5584 | /// |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 5585 | /// \returns True if the template parameter lists are equal, false |
| 5586 | /// otherwise. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5587 | bool |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 5588 | Sema::TemplateParameterListsAreEqual(TemplateParameterList *New, |
| 5589 | TemplateParameterList *Old, |
| 5590 | bool Complain, |
Douglas Gregor | 19ac2d6 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 5591 | TemplateParameterListEqualKind Kind, |
Douglas Gregor | 85e0f66 | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 5592 | SourceLocation TemplateArgLoc) { |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 5593 | if (Old->size() != New->size() && Kind != TPL_TemplateTemplateArgumentMatch) { |
| 5594 | if (Complain) |
| 5595 | DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind, |
| 5596 | TemplateArgLoc); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 5597 | |
| 5598 | return false; |
| 5599 | } |
| 5600 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5601 | // C++0x [temp.arg.template]p3: |
| 5602 | // A template-argument matches a template template-parameter (call it P) |
NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 5603 | // when each of the template parameters in the template-parameter-list of |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 5604 | // the template-argument's corresponding class template or alias template |
NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 5605 | // (call it A) matches the corresponding template parameter in the |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 5606 | // template-parameter-list of P. [...] |
| 5607 | TemplateParameterList::iterator NewParm = New->begin(); |
| 5608 | TemplateParameterList::iterator NewParmEnd = New->end(); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 5609 | for (TemplateParameterList::iterator OldParm = Old->begin(), |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 5610 | OldParmEnd = Old->end(); |
| 5611 | OldParm != OldParmEnd; ++OldParm) { |
Douglas Gregor | 018778a | 2011-01-13 18:47:47 +0000 | [diff] [blame] | 5612 | if (Kind != TPL_TemplateTemplateArgumentMatch || |
| 5613 | !(*OldParm)->isTemplateParameterPack()) { |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 5614 | if (NewParm == NewParmEnd) { |
| 5615 | if (Complain) |
| 5616 | DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind, |
| 5617 | TemplateArgLoc); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5618 | |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 5619 | return false; |
| 5620 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5621 | |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 5622 | if (!MatchTemplateParameterKind(*this, *NewParm, *OldParm, Complain, |
| 5623 | Kind, TemplateArgLoc)) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5624 | return false; |
| 5625 | |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 5626 | ++NewParm; |
| 5627 | continue; |
| 5628 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5629 | |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 5630 | // C++0x [temp.arg.template]p3: |
NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 5631 | // [...] When P's template- parameter-list contains a template parameter |
| 5632 | // pack (14.5.3), the template parameter pack will match zero or more |
| 5633 | // template parameters or template parameter packs in the |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 5634 | // template-parameter-list of A with the same type and form as the |
| 5635 | // template parameter pack in P (ignoring whether those template |
| 5636 | // parameters are template parameter packs). |
| 5637 | for (; NewParm != NewParmEnd; ++NewParm) { |
| 5638 | if (!MatchTemplateParameterKind(*this, *NewParm, *OldParm, Complain, |
| 5639 | Kind, TemplateArgLoc)) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5640 | return false; |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 5641 | } |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 5642 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5643 | |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 5644 | // Make sure we exhausted all of the arguments. |
| 5645 | if (NewParm != NewParmEnd) { |
| 5646 | if (Complain) |
| 5647 | DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind, |
| 5648 | TemplateArgLoc); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5649 | |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 5650 | return false; |
| 5651 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5652 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 5653 | return true; |
| 5654 | } |
| 5655 | |
| 5656 | /// \brief Check whether a template can be declared within this scope. |
| 5657 | /// |
| 5658 | /// If the template declaration is valid in this scope, returns |
| 5659 | /// false. Otherwise, issues a diagnostic and returns true. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5660 | bool |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 5661 | Sema::CheckTemplateDeclScope(Scope *S, TemplateParameterList *TemplateParams) { |
Douglas Gregor | dd847ba | 2011-11-03 16:37:14 +0000 | [diff] [blame] | 5662 | if (!S) |
| 5663 | return false; |
| 5664 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 5665 | // Find the nearest enclosing declaration scope. |
| 5666 | while ((S->getFlags() & Scope::DeclScope) == 0 || |
| 5667 | (S->getFlags() & Scope::TemplateParamScope) != 0) |
| 5668 | S = S->getParent(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5669 | |
Benjamin Kramer | 35e6fee | 2014-02-02 16:35:43 +0000 | [diff] [blame] | 5670 | // C++ [temp]p4: |
| 5671 | // A template [...] shall not have C linkage. |
Ted Kremenek | c37877d | 2013-10-08 17:08:03 +0000 | [diff] [blame] | 5672 | DeclContext *Ctx = S->getEntity(); |
Benjamin Kramer | 35e6fee | 2014-02-02 16:35:43 +0000 | [diff] [blame] | 5673 | if (Ctx && Ctx->isExternCContext()) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5674 | return Diag(TemplateParams->getTemplateLoc(), diag::err_template_linkage) |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 5675 | << TemplateParams->getSourceRange(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5676 | |
Eli Friedman | dfbd0c4 | 2009-07-31 01:43:05 +0000 | [diff] [blame] | 5677 | while (Ctx && isa<LinkageSpecDecl>(Ctx)) |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 5678 | Ctx = Ctx->getParent(); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 5679 | |
Benjamin Kramer | 35e6fee | 2014-02-02 16:35:43 +0000 | [diff] [blame] | 5680 | // C++ [temp]p2: |
| 5681 | // A template-declaration can appear only as a namespace scope or |
| 5682 | // class scope declaration. |
David Majnemer | 766e259 | 2013-10-22 04:14:18 +0000 | [diff] [blame] | 5683 | if (Ctx) { |
| 5684 | if (Ctx->isFileContext()) |
| 5685 | return false; |
| 5686 | if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(Ctx)) { |
| 5687 | // C++ [temp.mem]p2: |
| 5688 | // A local class shall not have member templates. |
| 5689 | if (RD->isLocalClass()) |
| 5690 | return Diag(TemplateParams->getTemplateLoc(), |
| 5691 | diag::err_template_inside_local_class) |
| 5692 | << TemplateParams->getSourceRange(); |
| 5693 | else |
| 5694 | return false; |
| 5695 | } |
| 5696 | } |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 5697 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5698 | return Diag(TemplateParams->getTemplateLoc(), |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 5699 | diag::err_template_outside_namespace_or_class_scope) |
| 5700 | << TemplateParams->getSourceRange(); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 5701 | } |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5702 | |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5703 | /// \brief Determine what kind of template specialization the given declaration |
| 5704 | /// is. |
Douglas Gregor | 0bc8a21 | 2012-01-14 15:55:47 +0000 | [diff] [blame] | 5705 | static TemplateSpecializationKind getTemplateSpecializationKind(Decl *D) { |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5706 | if (!D) |
| 5707 | return TSK_Undeclared; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5708 | |
Douglas Gregor | bbe8f46 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 5709 | if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) |
| 5710 | return Record->getTemplateSpecializationKind(); |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5711 | if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) |
| 5712 | return Function->getTemplateSpecializationKind(); |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5713 | if (VarDecl *Var = dyn_cast<VarDecl>(D)) |
| 5714 | return Var->getTemplateSpecializationKind(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5715 | |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5716 | return TSK_Undeclared; |
| 5717 | } |
| 5718 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5719 | /// \brief Check whether a specialization is well-formed in the current |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 5720 | /// context. |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 5721 | /// |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 5722 | /// This routine determines whether a template specialization can be declared |
| 5723 | /// in the current context (C++ [temp.expl.spec]p2). |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5724 | /// |
| 5725 | /// \param S the semantic analysis object for which this check is being |
| 5726 | /// performed. |
| 5727 | /// |
| 5728 | /// \param Specialized the entity being specialized or instantiated, which |
| 5729 | /// may be a kind of template (class template, function template, etc.) or |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5730 | /// a member of a class template (member function, static data member, |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5731 | /// member class). |
| 5732 | /// |
| 5733 | /// \param PrevDecl the previous declaration of this entity, if any. |
| 5734 | /// |
| 5735 | /// \param Loc the location of the explicit specialization or instantiation of |
| 5736 | /// this entity. |
| 5737 | /// |
| 5738 | /// \param IsPartialSpecialization whether this is a partial specialization of |
| 5739 | /// a class template. |
| 5740 | /// |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5741 | /// \returns true if there was an error that we cannot recover from, false |
| 5742 | /// otherwise. |
| 5743 | static bool CheckTemplateSpecializationScope(Sema &S, |
| 5744 | NamedDecl *Specialized, |
| 5745 | NamedDecl *PrevDecl, |
| 5746 | SourceLocation Loc, |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 5747 | bool IsPartialSpecialization) { |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5748 | // Keep these "kind" numbers in sync with the %select statements in the |
| 5749 | // various diagnostics emitted by this routine. |
| 5750 | int EntityKind = 0; |
Ted Kremenek | 7f1f3f6 | 2011-01-14 22:31:36 +0000 | [diff] [blame] | 5751 | if (isa<ClassTemplateDecl>(Specialized)) |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5752 | EntityKind = IsPartialSpecialization? 1 : 0; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5753 | else if (isa<VarTemplateDecl>(Specialized)) |
| 5754 | EntityKind = IsPartialSpecialization ? 3 : 2; |
Ted Kremenek | 7f1f3f6 | 2011-01-14 22:31:36 +0000 | [diff] [blame] | 5755 | else if (isa<FunctionTemplateDecl>(Specialized)) |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5756 | EntityKind = 4; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5757 | else if (isa<CXXMethodDecl>(Specialized)) |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5758 | EntityKind = 5; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5759 | else if (isa<VarDecl>(Specialized)) |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 5760 | EntityKind = 6; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5761 | else if (isa<RecordDecl>(Specialized)) |
| 5762 | EntityKind = 7; |
| 5763 | else if (isa<EnumDecl>(Specialized) && S.getLangOpts().CPlusPlus11) |
| 5764 | EntityKind = 8; |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5765 | else { |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 5766 | S.Diag(Loc, diag::err_template_spec_unknown_kind) |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5767 | << S.getLangOpts().CPlusPlus11; |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 5768 | S.Diag(Specialized->getLocation(), diag::note_specialized_entity); |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5769 | return true; |
| 5770 | } |
| 5771 | |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 5772 | // C++ [temp.expl.spec]p2: |
| 5773 | // An explicit specialization shall be declared in the namespace |
| 5774 | // of which the template is a member, or, for member templates, in |
| 5775 | // the namespace of which the enclosing class or enclosing class |
| 5776 | // template is a member. An explicit specialization of a member |
| 5777 | // function, member class or static data member of a class |
| 5778 | // template shall be declared in the namespace of which the class |
| 5779 | // template is a member. Such a declaration may also be a |
| 5780 | // definition. If the declaration is not a definition, the |
| 5781 | // specialization may be defined later in the name- space in which |
| 5782 | // the explicit specialization was declared, or in a namespace |
| 5783 | // that encloses the one in which the explicit specialization was |
| 5784 | // declared. |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 5785 | if (S.CurContext->getRedeclContext()->isFunctionOrMethod()) { |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5786 | S.Diag(Loc, diag::err_template_spec_decl_function_scope) |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 5787 | << Specialized; |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 5788 | return true; |
| 5789 | } |
Douglas Gregor | e4b0516 | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 5790 | |
Douglas Gregor | 40fb744 | 2009-10-07 17:30:37 +0000 | [diff] [blame] | 5791 | if (S.CurContext->isRecord() && !IsPartialSpecialization) { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5792 | if (S.getLangOpts().MicrosoftExt) { |
Francois Pichet | 00c7e6c | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 5793 | // Do not warn for class scope explicit specialization during |
| 5794 | // instantiation, warning was already emitted during pattern |
| 5795 | // semantic analysis. |
| 5796 | if (!S.ActiveTemplateInstantiations.size()) |
| 5797 | S.Diag(Loc, diag::ext_function_specialization_in_class) |
| 5798 | << Specialized; |
| 5799 | } else { |
| 5800 | S.Diag(Loc, diag::err_template_spec_decl_class_scope) |
| 5801 | << Specialized; |
| 5802 | return true; |
| 5803 | } |
Douglas Gregor | 40fb744 | 2009-10-07 17:30:37 +0000 | [diff] [blame] | 5804 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5805 | |
Douglas Gregor | 44e5a0a | 2011-10-20 16:41:18 +0000 | [diff] [blame] | 5806 | if (S.CurContext->isRecord() && |
| 5807 | !S.CurContext->Equals(Specialized->getDeclContext())) { |
| 5808 | // Make sure that we're specializing in the right record context. |
| 5809 | // Otherwise, things can go horribly wrong. |
| 5810 | S.Diag(Loc, diag::err_template_spec_decl_class_scope) |
| 5811 | << Specialized; |
| 5812 | return true; |
| 5813 | } |
| 5814 | |
Douglas Gregor | e4b0516 | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 5815 | // C++ [temp.class.spec]p6: |
| 5816 | // A class template partial specialization may be declared or redeclared |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5817 | // in any namespace scope in which its definition may be defined (14.5.1 |
| 5818 | // and 14.5.2). |
Richard Smith | a98f8fc | 2013-12-07 05:09:50 +0000 | [diff] [blame] | 5819 | DeclContext *SpecializedContext |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5820 | = Specialized->getDeclContext()->getEnclosingNamespaceContext(); |
Douglas Gregor | e4b0516 | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 5821 | DeclContext *DC = S.CurContext->getEnclosingNamespaceContext(); |
Richard Smith | a98f8fc | 2013-12-07 05:09:50 +0000 | [diff] [blame] | 5822 | |
| 5823 | // Make sure that this redeclaration (or definition) occurs in an enclosing |
| 5824 | // namespace. |
| 5825 | // Note that HandleDeclarator() performs this check for explicit |
| 5826 | // specializations of function templates, static data members, and member |
| 5827 | // functions, so we skip the check here for those kinds of entities. |
| 5828 | // FIXME: HandleDeclarator's diagnostics aren't quite as good, though. |
| 5829 | // Should we refactor that check, so that it occurs later? |
| 5830 | if (!DC->Encloses(SpecializedContext) && |
| 5831 | !(isa<FunctionTemplateDecl>(Specialized) || |
| 5832 | isa<FunctionDecl>(Specialized) || |
| 5833 | isa<VarTemplateDecl>(Specialized) || |
| 5834 | isa<VarDecl>(Specialized))) { |
| 5835 | if (isa<TranslationUnitDecl>(SpecializedContext)) |
| 5836 | S.Diag(Loc, diag::err_template_spec_redecl_global_scope) |
| 5837 | << EntityKind << Specialized; |
Alexey Bataev | 0068cb2 | 2015-03-20 07:21:46 +0000 | [diff] [blame] | 5838 | else if (isa<NamespaceDecl>(SpecializedContext)) { |
| 5839 | int Diag = diag::err_template_spec_redecl_out_of_scope; |
| 5840 | if (S.getLangOpts().MicrosoftExt) |
| 5841 | Diag = diag::ext_ms_template_spec_redecl_out_of_scope; |
| 5842 | S.Diag(Loc, Diag) << EntityKind << Specialized |
| 5843 | << cast<NamedDecl>(SpecializedContext); |
| 5844 | } else |
Richard Smith | a98f8fc | 2013-12-07 05:09:50 +0000 | [diff] [blame] | 5845 | llvm_unreachable("unexpected namespace context for specialization"); |
| 5846 | |
| 5847 | S.Diag(Specialized->getLocation(), diag::note_specialized_entity); |
| 5848 | } else if ((!PrevDecl || |
| 5849 | getTemplateSpecializationKind(PrevDecl) == TSK_Undeclared || |
| 5850 | getTemplateSpecializationKind(PrevDecl) == |
| 5851 | TSK_ImplicitInstantiation)) { |
Douglas Gregor | b1aab43 | 2010-09-12 05:08:28 +0000 | [diff] [blame] | 5852 | // C++ [temp.exp.spec]p2: |
| 5853 | // An explicit specialization shall be declared in the namespace of which |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5854 | // the template is a member, or, for member templates, in the namespace |
Douglas Gregor | b1aab43 | 2010-09-12 05:08:28 +0000 | [diff] [blame] | 5855 | // of which the enclosing class or enclosing class template is a member. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5856 | // An explicit specialization of a member function, member class or |
| 5857 | // static data member of a class template shall be declared in the |
Douglas Gregor | b1aab43 | 2010-09-12 05:08:28 +0000 | [diff] [blame] | 5858 | // namespace of which the class template is a member. |
| 5859 | // |
Richard Smith | a98f8fc | 2013-12-07 05:09:50 +0000 | [diff] [blame] | 5860 | // C++11 [temp.expl.spec]p2: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5861 | // An explicit specialization shall be declared in a namespace enclosing |
Douglas Gregor | b1aab43 | 2010-09-12 05:08:28 +0000 | [diff] [blame] | 5862 | // the specialized template. |
Richard Smith | a98f8fc | 2013-12-07 05:09:50 +0000 | [diff] [blame] | 5863 | // C++11 [temp.explicit]p3: |
| 5864 | // An explicit instantiation shall appear in an enclosing namespace of its |
| 5865 | // template. |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 5866 | if (!DC->InEnclosingNamespaceSetOf(SpecializedContext)) { |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5867 | bool IsCPlusPlus11Extension = DC->Encloses(SpecializedContext); |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 5868 | if (isa<TranslationUnitDecl>(SpecializedContext)) { |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5869 | assert(!IsCPlusPlus11Extension && |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 5870 | "DC encloses TU but isn't in enclosing namespace set"); |
| 5871 | S.Diag(Loc, diag::err_template_spec_decl_out_of_scope_global) |
Douglas Gregor | 8ce6315 | 2010-09-12 05:24:55 +0000 | [diff] [blame] | 5872 | << EntityKind << Specialized; |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 5873 | } else if (isa<NamespaceDecl>(SpecializedContext)) { |
| 5874 | int Diag; |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5875 | if (!IsCPlusPlus11Extension) |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 5876 | Diag = diag::err_template_spec_decl_out_of_scope; |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5877 | else if (!S.getLangOpts().CPlusPlus11) |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 5878 | Diag = diag::ext_template_spec_decl_out_of_scope; |
| 5879 | else |
| 5880 | Diag = diag::warn_cxx98_compat_template_spec_decl_out_of_scope; |
| 5881 | S.Diag(Loc, Diag) |
| 5882 | << EntityKind << Specialized << cast<NamedDecl>(SpecializedContext); |
| 5883 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5884 | |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 5885 | S.Diag(Specialized->getLocation(), diag::note_specialized_entity); |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 5886 | } |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 5887 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5888 | |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 5889 | return false; |
| 5890 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5891 | |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 5892 | static SourceRange findTemplateParameter(unsigned Depth, Expr *E) { |
| 5893 | if (!E->isInstantiationDependent()) |
| 5894 | return SourceLocation(); |
| 5895 | DependencyChecker Checker(Depth); |
| 5896 | Checker.TraverseStmt(E); |
| 5897 | if (Checker.Match && Checker.MatchLoc.isInvalid()) |
| 5898 | return E->getSourceRange(); |
| 5899 | return Checker.MatchLoc; |
| 5900 | } |
| 5901 | |
| 5902 | static SourceRange findTemplateParameter(unsigned Depth, TypeLoc TL) { |
| 5903 | if (!TL.getType()->isDependentType()) |
| 5904 | return SourceLocation(); |
| 5905 | DependencyChecker Checker(Depth); |
| 5906 | Checker.TraverseTypeLoc(TL); |
| 5907 | if (Checker.Match && Checker.MatchLoc.isInvalid()) |
| 5908 | return TL.getSourceRange(); |
| 5909 | return Checker.MatchLoc; |
| 5910 | } |
| 5911 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5912 | /// \brief Subroutine of Sema::CheckTemplatePartialSpecializationArgs |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 5913 | /// that checks non-type template partial specialization arguments. |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5914 | static bool CheckNonTypeTemplatePartialSpecializationArgs( |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 5915 | Sema &S, SourceLocation TemplateNameLoc, NonTypeTemplateParmDecl *Param, |
| 5916 | const TemplateArgument *Args, unsigned NumArgs, bool IsDefaultArgument) { |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 5917 | for (unsigned I = 0; I != NumArgs; ++I) { |
| 5918 | if (Args[I].getKind() == TemplateArgument::Pack) { |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5919 | if (CheckNonTypeTemplatePartialSpecializationArgs( |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 5920 | S, TemplateNameLoc, Param, Args[I].pack_begin(), |
| 5921 | Args[I].pack_size(), IsDefaultArgument)) |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 5922 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5923 | |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 5924 | continue; |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 5925 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5926 | |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 5927 | if (Args[I].getKind() != TemplateArgument::Expression) |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 5928 | continue; |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 5929 | |
| 5930 | Expr *ArgExpr = Args[I].getAsExpr(); |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 5931 | |
Douglas Gregor | 98318c2 | 2011-01-03 21:37:45 +0000 | [diff] [blame] | 5932 | // We can have a pack expansion of any of the bullets below. |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 5933 | if (PackExpansionExpr *Expansion = dyn_cast<PackExpansionExpr>(ArgExpr)) |
| 5934 | ArgExpr = Expansion->getPattern(); |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 5935 | |
| 5936 | // Strip off any implicit casts we added as part of type checking. |
| 5937 | while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(ArgExpr)) |
| 5938 | ArgExpr = ICE->getSubExpr(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5939 | |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 5940 | // C++ [temp.class.spec]p8: |
| 5941 | // A non-type argument is non-specialized if it is the name of a |
| 5942 | // non-type parameter. All other non-type arguments are |
| 5943 | // specialized. |
| 5944 | // |
| 5945 | // Below, we check the two conditions that only apply to |
| 5946 | // specialized non-type arguments, so skip any non-specialized |
| 5947 | // arguments. |
| 5948 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ArgExpr)) |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 5949 | if (isa<NonTypeTemplateParmDecl>(DRE->getDecl())) |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 5950 | continue; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5951 | |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 5952 | // C++ [temp.class.spec]p9: |
| 5953 | // Within the argument list of a class template partial |
| 5954 | // specialization, the following restrictions apply: |
| 5955 | // -- A partially specialized non-type argument expression |
| 5956 | // shall not involve a template parameter of the partial |
| 5957 | // specialization except when the argument expression is a |
| 5958 | // simple identifier. |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 5959 | SourceRange ParamUseRange = |
| 5960 | findTemplateParameter(Param->getDepth(), ArgExpr); |
| 5961 | if (ParamUseRange.isValid()) { |
| 5962 | if (IsDefaultArgument) { |
| 5963 | S.Diag(TemplateNameLoc, |
| 5964 | diag::err_dependent_non_type_arg_in_partial_spec); |
| 5965 | S.Diag(ParamUseRange.getBegin(), |
| 5966 | diag::note_dependent_non_type_default_arg_in_partial_spec) |
| 5967 | << ParamUseRange; |
| 5968 | } else { |
| 5969 | S.Diag(ParamUseRange.getBegin(), |
| 5970 | diag::err_dependent_non_type_arg_in_partial_spec) |
| 5971 | << ParamUseRange; |
| 5972 | } |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 5973 | return true; |
| 5974 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5975 | |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 5976 | // -- The type of a template parameter corresponding to a |
| 5977 | // specialized non-type argument shall not be dependent on a |
| 5978 | // parameter of the specialization. |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 5979 | // |
| 5980 | // FIXME: We need to delay this check until instantiation in some cases: |
| 5981 | // |
| 5982 | // template<template<typename> class X> struct A { |
| 5983 | // template<typename T, X<T> N> struct B; |
| 5984 | // template<typename T> struct B<T, 0>; |
| 5985 | // }; |
| 5986 | // template<typename> using X = int; |
| 5987 | // A<X>::B<int, 0> b; |
| 5988 | ParamUseRange = findTemplateParameter( |
| 5989 | Param->getDepth(), Param->getTypeSourceInfo()->getTypeLoc()); |
| 5990 | if (ParamUseRange.isValid()) { |
| 5991 | S.Diag(IsDefaultArgument ? TemplateNameLoc : ArgExpr->getLocStart(), |
| 5992 | diag::err_dependent_typed_non_type_arg_in_partial_spec) |
| 5993 | << Param->getType() << ParamUseRange; |
| 5994 | S.Diag(Param->getLocation(), diag::note_template_param_here) |
| 5995 | << (IsDefaultArgument ? ParamUseRange : SourceRange()); |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 5996 | return true; |
| 5997 | } |
| 5998 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5999 | |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 6000 | return false; |
| 6001 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6002 | |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 6003 | /// \brief Check the non-type template arguments of a class template |
| 6004 | /// partial specialization according to C++ [temp.class.spec]p9. |
| 6005 | /// |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 6006 | /// \param TemplateNameLoc the location of the template name. |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 6007 | /// \param TemplateParams the template parameters of the primary class |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 6008 | /// template. |
| 6009 | /// \param NumExplicit the number of explicitly-specified template arguments. |
James Dennett | 634962f | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 6010 | /// \param TemplateArgs the template arguments of the class template |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 6011 | /// partial specialization. |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 6012 | /// |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 6013 | /// \returns \c true if there was an error, \c false otherwise. |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 6014 | static bool CheckTemplatePartialSpecializationArgs( |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 6015 | Sema &S, SourceLocation TemplateNameLoc, |
| 6016 | TemplateParameterList *TemplateParams, unsigned NumExplicit, |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 6017 | SmallVectorImpl<TemplateArgument> &TemplateArgs) { |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 6018 | const TemplateArgument *ArgList = TemplateArgs.data(); |
| 6019 | |
| 6020 | for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) { |
| 6021 | NonTypeTemplateParmDecl *Param |
| 6022 | = dyn_cast<NonTypeTemplateParmDecl>(TemplateParams->getParam(I)); |
| 6023 | if (!Param) |
| 6024 | continue; |
| 6025 | |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 6026 | if (CheckNonTypeTemplatePartialSpecializationArgs( |
| 6027 | S, TemplateNameLoc, Param, &ArgList[I], 1, I >= NumExplicit)) |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 6028 | return true; |
| 6029 | } |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 6030 | |
| 6031 | return false; |
| 6032 | } |
| 6033 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6034 | DeclResult |
John McCall | 9bb74a5 | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 6035 | Sema::ActOnClassTemplateSpecialization(Scope *S, unsigned TagSpec, |
| 6036 | TagUseKind TUK, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6037 | SourceLocation KWLoc, |
Douglas Gregor | 3c7cd6a | 2011-09-09 20:53:38 +0000 | [diff] [blame] | 6038 | SourceLocation ModulePrivateLoc, |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 6039 | TemplateIdAnnotation &TemplateId, |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6040 | AttributeList *Attr, |
| 6041 | MultiTemplateParamsArg TemplateParameterLists) { |
Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 6042 | assert(TUK != TUK_Reference && "References are not specializations"); |
John McCall | 06f6fe8d | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 6043 | |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 6044 | CXXScopeSpec &SS = TemplateId.SS; |
| 6045 | |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 6046 | // NOTE: KWLoc is the location of the tag keyword. This will instead |
| 6047 | // store the location of the outermost template keyword in the declaration. |
| 6048 | SourceLocation TemplateKWLoc = TemplateParameterLists.size() > 0 |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 6049 | ? TemplateParameterLists[0]->getTemplateLoc() : KWLoc; |
| 6050 | SourceLocation TemplateNameLoc = TemplateId.TemplateNameLoc; |
| 6051 | SourceLocation LAngleLoc = TemplateId.LAngleLoc; |
| 6052 | SourceLocation RAngleLoc = TemplateId.RAngleLoc; |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 6053 | |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6054 | // Find the class template we're specializing |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 6055 | TemplateName Name = TemplateId.Template.get(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6056 | ClassTemplateDecl *ClassTemplate |
Douglas Gregor | dd6c035 | 2009-11-12 00:46:20 +0000 | [diff] [blame] | 6057 | = dyn_cast_or_null<ClassTemplateDecl>(Name.getAsTemplateDecl()); |
| 6058 | |
| 6059 | if (!ClassTemplate) { |
| 6060 | Diag(TemplateNameLoc, diag::err_not_class_template_specialization) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6061 | << (Name.getAsTemplateDecl() && |
Douglas Gregor | dd6c035 | 2009-11-12 00:46:20 +0000 | [diff] [blame] | 6062 | isa<TemplateTemplateParmDecl>(Name.getAsTemplateDecl())); |
| 6063 | return true; |
| 6064 | } |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6065 | |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 6066 | bool isExplicitSpecialization = false; |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 6067 | bool isPartialSpecialization = false; |
| 6068 | |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 6069 | // Check the validity of the template headers that introduce this |
| 6070 | // template. |
Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 6071 | // FIXME: We probably shouldn't complain about these headers for |
| 6072 | // friend declarations. |
Douglas Gregor | 5f0e252 | 2010-07-14 23:14:12 +0000 | [diff] [blame] | 6073 | bool Invalid = false; |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 6074 | TemplateParameterList *TemplateParams = |
| 6075 | MatchTemplateParametersToScopeSpecifier( |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 6076 | KWLoc, TemplateNameLoc, SS, &TemplateId, |
| 6077 | TemplateParameterLists, TUK == TUK_Friend, isExplicitSpecialization, |
| 6078 | Invalid); |
Douglas Gregor | 5f0e252 | 2010-07-14 23:14:12 +0000 | [diff] [blame] | 6079 | if (Invalid) |
| 6080 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6081 | |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 6082 | if (TemplateParams && TemplateParams->size() > 0) { |
| 6083 | isPartialSpecialization = true; |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 6084 | |
Douglas Gregor | ec9518b | 2010-12-21 08:14:57 +0000 | [diff] [blame] | 6085 | if (TUK == TUK_Friend) { |
| 6086 | Diag(KWLoc, diag::err_partial_specialization_friend) |
| 6087 | << SourceRange(LAngleLoc, RAngleLoc); |
| 6088 | return true; |
| 6089 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6090 | |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 6091 | // C++ [temp.class.spec]p10: |
| 6092 | // The template parameter list of a specialization shall not |
| 6093 | // contain default template argument values. |
| 6094 | for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) { |
| 6095 | Decl *Param = TemplateParams->getParam(I); |
| 6096 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) { |
| 6097 | if (TTP->hasDefaultArgument()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6098 | Diag(TTP->getDefaultArgumentLoc(), |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 6099 | diag::err_default_arg_in_partial_spec); |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 6100 | TTP->removeDefaultArgument(); |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 6101 | } |
| 6102 | } else if (NonTypeTemplateParmDecl *NTTP |
| 6103 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
| 6104 | if (Expr *DefArg = NTTP->getDefaultArgument()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6105 | Diag(NTTP->getDefaultArgumentLoc(), |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 6106 | diag::err_default_arg_in_partial_spec) |
| 6107 | << DefArg->getSourceRange(); |
Abramo Bagnara | 656e300 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 6108 | NTTP->removeDefaultArgument(); |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 6109 | } |
| 6110 | } else { |
| 6111 | TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(Param); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 6112 | if (TTP->hasDefaultArgument()) { |
| 6113 | Diag(TTP->getDefaultArgument().getLocation(), |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 6114 | diag::err_default_arg_in_partial_spec) |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 6115 | << TTP->getDefaultArgument().getSourceRange(); |
Abramo Bagnara | 656e300 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 6116 | TTP->removeDefaultArgument(); |
Douglas Gregor | d522205 | 2009-06-12 19:43:02 +0000 | [diff] [blame] | 6117 | } |
| 6118 | } |
| 6119 | } |
Douglas Gregor | 3a88c1d | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 6120 | } else if (TemplateParams) { |
| 6121 | if (TUK == TUK_Friend) |
| 6122 | Diag(KWLoc, diag::err_template_spec_friend) |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 6123 | << FixItHint::CreateRemoval( |
Douglas Gregor | 3a88c1d | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 6124 | SourceRange(TemplateParams->getTemplateLoc(), |
| 6125 | TemplateParams->getRAngleLoc())) |
| 6126 | << SourceRange(LAngleLoc, RAngleLoc); |
| 6127 | else |
| 6128 | isExplicitSpecialization = true; |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 6129 | } else { |
| 6130 | assert(TUK == TUK_Friend && "should have a 'template<>' for this decl"); |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 6131 | } |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 6132 | |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6133 | // Check that the specialization uses the same tag kind as the |
| 6134 | // original template. |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 6135 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
| 6136 | assert(Kind != TTK_Enum && "Invalid enum tag in class template spec!"); |
Douglas Gregor | d9034f0 | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 6137 | if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(), |
Richard Trieu | caa33d3 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 6138 | Kind, TUK == TUK_Definition, KWLoc, |
Douglas Gregor | d9034f0 | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 6139 | *ClassTemplate->getIdentifier())) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6140 | Diag(KWLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | 170512f | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 6141 | << ClassTemplate |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 6142 | << FixItHint::CreateReplacement(KWLoc, |
Douglas Gregor | 170512f | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 6143 | ClassTemplate->getTemplatedDecl()->getKindName()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6144 | Diag(ClassTemplate->getTemplatedDecl()->getLocation(), |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6145 | diag::note_previous_use); |
| 6146 | Kind = ClassTemplate->getTemplatedDecl()->getTagKind(); |
| 6147 | } |
| 6148 | |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 6149 | // Translate the parser's template argument list in our AST format. |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 6150 | TemplateArgumentListInfo TemplateArgs = |
| 6151 | makeTemplateArgumentListInfo(*this, TemplateId); |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 6152 | |
Douglas Gregor | 1440693 | 2011-01-03 20:35:03 +0000 | [diff] [blame] | 6153 | // Check for unexpanded parameter packs in any of the template arguments. |
| 6154 | for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6155 | if (DiagnoseUnexpandedParameterPack(TemplateArgs[I], |
Douglas Gregor | 1440693 | 2011-01-03 20:35:03 +0000 | [diff] [blame] | 6156 | UPPC_PartialSpecialization)) |
| 6157 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6158 | |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6159 | // Check that the template argument list is well-formed for this |
| 6160 | // template. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6161 | SmallVector<TemplateArgument, 4> Converted; |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 6162 | if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc, |
| 6163 | TemplateArgs, false, Converted)) |
Douglas Gregor | c08f489 | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 6164 | return true; |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6165 | |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 6166 | // Find the class template (partial) specialization declaration that |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6167 | // corresponds to these arguments. |
Douglas Gregor | d522205 | 2009-06-12 19:43:02 +0000 | [diff] [blame] | 6168 | if (isPartialSpecialization) { |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 6169 | if (CheckTemplatePartialSpecializationArgs( |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 6170 | *this, TemplateNameLoc, ClassTemplate->getTemplateParameters(), |
| 6171 | TemplateArgs.size(), Converted)) |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 6172 | return true; |
| 6173 | |
Douglas Gregor | 678d76c | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 6174 | bool InstantiationDependent; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6175 | if (!Name.isDependent() && |
Douglas Gregor | 92354b6 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 6176 | !TemplateSpecializationType::anyDependentTemplateArguments( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6177 | TemplateArgs.getArgumentArray(), |
Douglas Gregor | 678d76c | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 6178 | TemplateArgs.size(), |
| 6179 | InstantiationDependent)) { |
Douglas Gregor | 92354b6 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 6180 | Diag(TemplateNameLoc, diag::err_partial_spec_fully_specialized) |
| 6181 | << ClassTemplate->getDeclName(); |
| 6182 | isPartialSpecialization = false; |
Douglas Gregor | 92354b6 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 6183 | } |
| 6184 | } |
Argyrios Kyrtzidis | 47470f2 | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 6185 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6186 | void *InsertPos = nullptr; |
| 6187 | ClassTemplateSpecializationDecl *PrevDecl = nullptr; |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 6188 | |
| 6189 | if (isPartialSpecialization) |
Argyrios Kyrtzidis | 47470f2 | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 6190 | // FIXME: Template parameter list matters, too |
Craig Topper | 7e0daca | 2014-06-26 04:58:53 +0000 | [diff] [blame] | 6191 | PrevDecl = ClassTemplate->findPartialSpecialization(Converted, InsertPos); |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 6192 | else |
Craig Topper | 7e0daca | 2014-06-26 04:58:53 +0000 | [diff] [blame] | 6193 | PrevDecl = ClassTemplate->findSpecialization(Converted, InsertPos); |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6194 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6195 | ClassTemplateSpecializationDecl *Specialization = nullptr; |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6196 | |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 6197 | // Check whether we can declare a class template specialization in |
| 6198 | // the current scope. |
Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 6199 | if (TUK != TUK_Friend && |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6200 | CheckTemplateSpecializationScope(*this, ClassTemplate, PrevDecl, |
| 6201 | TemplateNameLoc, |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 6202 | isPartialSpecialization)) |
Douglas Gregor | c08f489 | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 6203 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6204 | |
Douglas Gregor | 1530138 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 6205 | // The canonical type |
| 6206 | QualType CanonType; |
Richard Smith | 871cd4c | 2014-05-23 21:00:28 +0000 | [diff] [blame] | 6207 | if (isPartialSpecialization) { |
Douglas Gregor | 1530138 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 6208 | // Build the canonical type that describes the converted template |
| 6209 | // arguments of the class template partial specialization. |
Douglas Gregor | 92354b6 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 6210 | TemplateName CanonTemplate = Context.getCanonicalTemplateName(Name); |
| 6211 | CanonType = Context.getTemplateSpecializationType(CanonTemplate, |
Douglas Gregor | dd7ec463 | 2010-12-23 17:13:55 +0000 | [diff] [blame] | 6212 | Converted.data(), |
| 6213 | Converted.size()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6214 | |
| 6215 | if (Context.hasSameType(CanonType, |
Douglas Gregor | dd7ec463 | 2010-12-23 17:13:55 +0000 | [diff] [blame] | 6216 | ClassTemplate->getInjectedClassNameSpecialization())) { |
| 6217 | // C++ [temp.class.spec]p9b3: |
| 6218 | // |
| 6219 | // -- The argument list of the specialization shall not be identical |
| 6220 | // to the implicit argument list of the primary template. |
| 6221 | Diag(TemplateNameLoc, diag::err_partial_spec_args_match_primary_template) |
Richard Smith | 300e0c3 | 2013-09-24 04:49:23 +0000 | [diff] [blame] | 6222 | << /*class template*/0 << (TUK == TUK_Definition) |
Douglas Gregor | 26701a4 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 6223 | << FixItHint::CreateRemoval(SourceRange(LAngleLoc, RAngleLoc)); |
Douglas Gregor | dd7ec463 | 2010-12-23 17:13:55 +0000 | [diff] [blame] | 6224 | return CheckClassTemplate(S, TagSpec, TUK, KWLoc, SS, |
| 6225 | ClassTemplate->getIdentifier(), |
| 6226 | TemplateNameLoc, |
| 6227 | Attr, |
| 6228 | TemplateParams, |
Douglas Gregor | 2820e69 | 2011-09-09 19:05:14 +0000 | [diff] [blame] | 6229 | AS_none, /*ModulePrivateLoc=*/SourceLocation(), |
Nikola Smiljanic | 4fc9153 | 2014-07-17 01:59:34 +0000 | [diff] [blame] | 6230 | /*FriendLoc*/SourceLocation(), |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 6231 | TemplateParameterLists.size() - 1, |
Benjamin Kramer | cc4c49d | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 6232 | TemplateParameterLists.data()); |
Douglas Gregor | dd7ec463 | 2010-12-23 17:13:55 +0000 | [diff] [blame] | 6233 | } |
Douglas Gregor | 1530138 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 6234 | |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 6235 | // Create a new class template partial specialization declaration node. |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 6236 | ClassTemplatePartialSpecializationDecl *PrevPartial |
| 6237 | = cast_or_null<ClassTemplatePartialSpecializationDecl>(PrevDecl); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6238 | ClassTemplatePartialSpecializationDecl *Partial |
Douglas Gregor | e902956 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 6239 | = ClassTemplatePartialSpecializationDecl::Create(Context, Kind, |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 6240 | ClassTemplate->getDeclContext(), |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 6241 | KWLoc, TemplateNameLoc, |
Anders Carlsson | 1b28c3e | 2009-06-05 04:06:48 +0000 | [diff] [blame] | 6242 | TemplateParams, |
| 6243 | ClassTemplate, |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 6244 | Converted.data(), |
| 6245 | Converted.size(), |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 6246 | TemplateArgs, |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 6247 | CanonType, |
Richard Smith | b2f61b4 | 2013-08-22 23:27:37 +0000 | [diff] [blame] | 6248 | PrevPartial); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 6249 | SetNestedNameSpecifier(Partial, SS); |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 6250 | if (TemplateParameterLists.size() > 1 && SS.isSet()) { |
Douglas Gregor | 20527e2 | 2010-06-15 17:44:38 +0000 | [diff] [blame] | 6251 | Partial->setTemplateParameterListsInfo(Context, |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 6252 | TemplateParameterLists.size() - 1, |
Benjamin Kramer | cc4c49d | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 6253 | TemplateParameterLists.data()); |
Abramo Bagnara | da41d0c | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 6254 | } |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 6255 | |
Argyrios Kyrtzidis | 47470f2 | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 6256 | if (!PrevPartial) |
| 6257 | ClassTemplate->AddPartialSpecialization(Partial, InsertPos); |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 6258 | Specialization = Partial; |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 6259 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6260 | // If we are providing an explicit specialization of a member class |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 6261 | // template specialization, make a note of that. |
| 6262 | if (PrevPartial && PrevPartial->getInstantiatedFromMember()) |
| 6263 | PrevPartial->setMemberSpecialization(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6264 | |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 6265 | // Check that all of the template parameters of the class template |
| 6266 | // partial specialization are deducible from the template |
| 6267 | // arguments. If not, this class template partial specialization |
| 6268 | // will never be used. |
Benjamin Kramer | e0513cb | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 6269 | llvm::SmallBitVector DeducibleParams(TemplateParams->size()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6270 | MarkUsedTemplateParameters(Partial->getTemplateArgs(), true, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 6271 | TemplateParams->getDepth(), |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 6272 | DeducibleParams); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 6273 | |
Benjamin Kramer | e0513cb | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 6274 | if (!DeducibleParams.all()) { |
| 6275 | unsigned NumNonDeducible = DeducibleParams.size()-DeducibleParams.count(); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 6276 | Diag(TemplateNameLoc, diag::warn_partial_specs_not_deducible) |
Richard Smith | 300e0c3 | 2013-09-24 04:49:23 +0000 | [diff] [blame] | 6277 | << /*class template*/0 << (NumNonDeducible > 1) |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 6278 | << SourceRange(TemplateNameLoc, RAngleLoc); |
| 6279 | for (unsigned I = 0, N = DeducibleParams.size(); I != N; ++I) { |
| 6280 | if (!DeducibleParams[I]) { |
| 6281 | NamedDecl *Param = cast<NamedDecl>(TemplateParams->getParam(I)); |
| 6282 | if (Param->getDeclName()) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6283 | Diag(Param->getLocation(), |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 6284 | diag::note_partial_spec_unused_parameter) |
| 6285 | << Param->getDeclName(); |
| 6286 | else |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6287 | Diag(Param->getLocation(), |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 6288 | diag::note_partial_spec_unused_parameter) |
David Blaikie | abe1a39 | 2014-04-02 05:58:29 +0000 | [diff] [blame] | 6289 | << "(anonymous)"; |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 6290 | } |
| 6291 | } |
| 6292 | } |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6293 | } else { |
| 6294 | // Create a new class template specialization declaration node for |
Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 6295 | // this explicit specialization or friend declaration. |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6296 | Specialization |
Douglas Gregor | e902956 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 6297 | = ClassTemplateSpecializationDecl::Create(Context, Kind, |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6298 | ClassTemplate->getDeclContext(), |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 6299 | KWLoc, TemplateNameLoc, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6300 | ClassTemplate, |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 6301 | Converted.data(), |
| 6302 | Converted.size(), |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6303 | PrevDecl); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 6304 | SetNestedNameSpecifier(Specialization, SS); |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 6305 | if (TemplateParameterLists.size() > 0) { |
Douglas Gregor | 20527e2 | 2010-06-15 17:44:38 +0000 | [diff] [blame] | 6306 | Specialization->setTemplateParameterListsInfo(Context, |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 6307 | TemplateParameterLists.size(), |
Benjamin Kramer | cc4c49d | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 6308 | TemplateParameterLists.data()); |
Abramo Bagnara | da41d0c | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 6309 | } |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6310 | |
Argyrios Kyrtzidis | 47470f2 | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 6311 | if (!PrevDecl) |
| 6312 | ClassTemplate->AddSpecialization(Specialization, InsertPos); |
Douglas Gregor | 1530138 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 6313 | |
| 6314 | CanonType = Context.getTypeDeclType(Specialization); |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6315 | } |
| 6316 | |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6317 | // C++ [temp.expl.spec]p6: |
| 6318 | // If a template, a member template or the member of a class template is |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6319 | // explicitly specialized then that specialization shall be declared |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6320 | // before the first use of that specialization that would cause an implicit |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6321 | // instantiation to take place, in every translation unit in which such a |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6322 | // use occurs; no diagnostic is required. |
| 6323 | if (PrevDecl && PrevDecl->getPointOfInstantiation().isValid()) { |
Douglas Gregor | c854c66 | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 6324 | bool Okay = false; |
Douglas Gregor | 0bc8a21 | 2012-01-14 15:55:47 +0000 | [diff] [blame] | 6325 | for (Decl *Prev = PrevDecl; Prev; Prev = Prev->getPreviousDecl()) { |
Douglas Gregor | c854c66 | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 6326 | // Is there any previous explicit specialization declaration? |
| 6327 | if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) { |
| 6328 | Okay = true; |
| 6329 | break; |
| 6330 | } |
| 6331 | } |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6332 | |
Douglas Gregor | c854c66 | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 6333 | if (!Okay) { |
| 6334 | SourceRange Range(TemplateNameLoc, RAngleLoc); |
| 6335 | Diag(TemplateNameLoc, diag::err_specialization_after_instantiation) |
| 6336 | << Context.getTypeDeclType(Specialization) << Range; |
| 6337 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6338 | Diag(PrevDecl->getPointOfInstantiation(), |
Douglas Gregor | c854c66 | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 6339 | diag::note_instantiation_required_here) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6340 | << (PrevDecl->getTemplateSpecializationKind() |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6341 | != TSK_ImplicitInstantiation); |
Douglas Gregor | c854c66 | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 6342 | return true; |
| 6343 | } |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6344 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6345 | |
Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 6346 | // If this is not a friend, note that this is an explicit specialization. |
| 6347 | if (TUK != TUK_Friend) |
| 6348 | Specialization->setSpecializationKind(TSK_ExplicitSpecialization); |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6349 | |
| 6350 | // Check that this isn't a redefinition of this specialization. |
John McCall | 9bb74a5 | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 6351 | if (TUK == TUK_Definition) { |
Douglas Gregor | 0a5a221 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 6352 | if (RecordDecl *Def = Specialization->getDefinition()) { |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6353 | SourceRange Range(TemplateNameLoc, RAngleLoc); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6354 | Diag(TemplateNameLoc, diag::err_redefinition) |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 6355 | << Context.getTypeDeclType(Specialization) << Range; |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6356 | Diag(Def->getLocation(), diag::note_previous_definition); |
| 6357 | Specialization->setInvalidDecl(); |
Douglas Gregor | c08f489 | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 6358 | return true; |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6359 | } |
| 6360 | } |
| 6361 | |
John McCall | 659a337 | 2010-12-18 03:30:47 +0000 | [diff] [blame] | 6362 | if (Attr) |
| 6363 | ProcessDeclAttributeList(S, Specialization, Attr); |
| 6364 | |
Richard Smith | 034b94a | 2012-08-17 03:20:55 +0000 | [diff] [blame] | 6365 | // Add alignment attributes if necessary; these attributes are checked when |
| 6366 | // the ASTContext lays out the structure. |
| 6367 | if (TUK == TUK_Definition) { |
| 6368 | AddAlignmentAttributesForRecord(Specialization); |
| 6369 | AddMsStructLayoutForRecord(Specialization); |
| 6370 | } |
| 6371 | |
Douglas Gregor | 3c7cd6a | 2011-09-09 20:53:38 +0000 | [diff] [blame] | 6372 | if (ModulePrivateLoc.isValid()) |
| 6373 | Diag(Specialization->getLocation(), diag::err_module_private_specialization) |
| 6374 | << (isPartialSpecialization? 1 : 0) |
| 6375 | << FixItHint::CreateRemoval(ModulePrivateLoc); |
| 6376 | |
Douglas Gregor | d56a91e | 2009-02-26 22:19:44 +0000 | [diff] [blame] | 6377 | // Build the fully-sugared type for this class template |
| 6378 | // specialization as the user wrote in the specialization |
| 6379 | // itself. This means that we'll pretty-print the type retrieved |
| 6380 | // from the specialization's declaration the way that the user |
| 6381 | // actually wrote the specialization, rather than formatting the |
| 6382 | // name based on the "canonical" representation used to store the |
| 6383 | // template arguments in the specialization. |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 6384 | TypeSourceInfo *WrittenTy |
| 6385 | = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc, |
| 6386 | TemplateArgs, CanonType); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6387 | if (TUK != TUK_Friend) { |
Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 6388 | Specialization->setTypeAsWritten(WrittenTy); |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 6389 | Specialization->setTemplateKeywordLoc(TemplateKWLoc); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6390 | } |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6391 | |
Douglas Gregor | 1e249f8 | 2009-02-25 22:18:32 +0000 | [diff] [blame] | 6392 | // C++ [temp.expl.spec]p9: |
| 6393 | // A template explicit specialization is in the scope of the |
| 6394 | // namespace in which the template was defined. |
| 6395 | // |
| 6396 | // We actually implement this paragraph where we set the semantic |
| 6397 | // context (in the creation of the ClassTemplateSpecializationDecl), |
| 6398 | // but we also maintain the lexical context where the actual |
| 6399 | // definition occurs. |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6400 | Specialization->setLexicalDeclContext(CurContext); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6401 | |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6402 | // We may be starting the definition of this specialization. |
John McCall | 9bb74a5 | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 6403 | if (TUK == TUK_Definition) |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6404 | Specialization->startDefinition(); |
| 6405 | |
Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 6406 | if (TUK == TUK_Friend) { |
| 6407 | FriendDecl *Friend = FriendDecl::Create(Context, CurContext, |
| 6408 | TemplateNameLoc, |
John McCall | 15ad096 | 2010-03-25 18:04:51 +0000 | [diff] [blame] | 6409 | WrittenTy, |
Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 6410 | /*FIXME:*/KWLoc); |
| 6411 | Friend->setAccess(AS_public); |
| 6412 | CurContext->addDecl(Friend); |
| 6413 | } else { |
| 6414 | // Add the specialization into its lexical context, so that it can |
| 6415 | // be seen when iterating through the list of declarations in that |
| 6416 | // context. However, specializations are not found by name lookup. |
| 6417 | CurContext->addDecl(Specialization); |
| 6418 | } |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6419 | return Specialization; |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6420 | } |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6421 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6422 | Decl *Sema::ActOnTemplateDeclarator(Scope *S, |
Douglas Gregor | b52fabb | 2009-06-23 23:11:28 +0000 | [diff] [blame] | 6423 | MultiTemplateParamsArg TemplateParameterLists, |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6424 | Declarator &D) { |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 6425 | Decl *NewDecl = HandleDeclarator(S, D, TemplateParameterLists); |
Dmitri Gribenko | 34df220 | 2012-07-31 22:37:06 +0000 | [diff] [blame] | 6426 | ActOnDocumentableDecl(NewDecl); |
| 6427 | return NewDecl; |
Douglas Gregor | b52fabb | 2009-06-23 23:11:28 +0000 | [diff] [blame] | 6428 | } |
| 6429 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6430 | Decl *Sema::ActOnStartOfFunctionTemplateDef(Scope *FnBodyScope, |
Douglas Gregor | 17a7c12 | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 6431 | MultiTemplateParamsArg TemplateParameterLists, |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6432 | Declarator &D) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6433 | assert(getCurFunctionDecl() == nullptr && "Function parsing confused"); |
Abramo Bagnara | 924a8f3 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 6434 | DeclaratorChunk::FunctionTypeInfo &FTI = D.getFunctionTypeInfo(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6435 | |
Douglas Gregor | 17a7c12 | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 6436 | if (FTI.hasPrototype) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6437 | // FIXME: Diagnose arguments without names in C. |
Douglas Gregor | 17a7c12 | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 6438 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6439 | |
Douglas Gregor | 17a7c12 | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 6440 | Scope *ParentScope = FnBodyScope->getParent(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6441 | |
Douglas Gregor | 5d1b4e3 | 2011-11-07 20:56:01 +0000 | [diff] [blame] | 6442 | D.setFunctionDefinitionKind(FDK_Definition); |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6443 | Decl *DP = HandleDeclarator(ParentScope, D, |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 6444 | TemplateParameterLists); |
Argyrios Kyrtzidis | 6fada2d | 2012-12-14 06:53:58 +0000 | [diff] [blame] | 6445 | return ActOnStartOfFunctionDef(FnBodyScope, DP); |
Douglas Gregor | 17a7c12 | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 6446 | } |
| 6447 | |
John McCall | 4f7ced6 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 6448 | /// \brief Strips various properties off an implicit instantiation |
| 6449 | /// that has just been explicitly specialized. |
| 6450 | static void StripImplicitInstantiation(NamedDecl *D) { |
Nico Weber | e497438 | 2014-12-19 23:52:45 +0000 | [diff] [blame] | 6451 | D->dropAttr<DLLImportAttr>(); |
| 6452 | D->dropAttr<DLLExportAttr>(); |
John McCall | 4f7ced6 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 6453 | |
Nico Weber | e497438 | 2014-12-19 23:52:45 +0000 | [diff] [blame] | 6454 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) |
John McCall | 4f7ced6 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 6455 | FD->setInlineSpecified(false); |
John McCall | 4f7ced6 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 6456 | } |
| 6457 | |
Nico Weber | a8f80b3 | 2012-01-09 19:52:25 +0000 | [diff] [blame] | 6458 | /// \brief Compute the diagnostic location for an explicit instantiation |
| 6459 | // declaration or definition. |
| 6460 | static SourceLocation DiagLocForExplicitInstantiation( |
Douglas Gregor | 0bc8a21 | 2012-01-14 15:55:47 +0000 | [diff] [blame] | 6461 | NamedDecl* D, SourceLocation PointOfInstantiation) { |
Nico Weber | a8f80b3 | 2012-01-09 19:52:25 +0000 | [diff] [blame] | 6462 | // Explicit instantiations following a specialization have no effect and |
| 6463 | // hence no PointOfInstantiation. In that case, walk decl backwards |
| 6464 | // until a valid name loc is found. |
| 6465 | SourceLocation PrevDiagLoc = PointOfInstantiation; |
Douglas Gregor | 0bc8a21 | 2012-01-14 15:55:47 +0000 | [diff] [blame] | 6466 | for (Decl *Prev = D; Prev && !PrevDiagLoc.isValid(); |
| 6467 | Prev = Prev->getPreviousDecl()) { |
Nico Weber | a8f80b3 | 2012-01-09 19:52:25 +0000 | [diff] [blame] | 6468 | PrevDiagLoc = Prev->getLocation(); |
| 6469 | } |
| 6470 | assert(PrevDiagLoc.isValid() && |
| 6471 | "Explicit instantiation without point of instantiation?"); |
| 6472 | return PrevDiagLoc; |
| 6473 | } |
| 6474 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6475 | /// \brief Diagnose cases where we have an explicit template specialization |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6476 | /// before/after an explicit template instantiation, producing diagnostics |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6477 | /// for those cases where they are required and determining whether the |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6478 | /// new specialization/instantiation will have any effect. |
| 6479 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6480 | /// \param NewLoc the location of the new explicit specialization or |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6481 | /// instantiation. |
| 6482 | /// |
| 6483 | /// \param NewTSK the kind of the new explicit specialization or instantiation. |
| 6484 | /// |
| 6485 | /// \param PrevDecl the previous declaration of the entity. |
| 6486 | /// |
| 6487 | /// \param PrevTSK the kind of the old explicit specialization or instantiatin. |
| 6488 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6489 | /// \param PrevPointOfInstantiation if valid, indicates where the previus |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6490 | /// declaration was instantiated (either implicitly or explicitly). |
| 6491 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6492 | /// \param HasNoEffect will be set to true to indicate that the new |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6493 | /// specialization or instantiation has no effect and should be ignored. |
| 6494 | /// |
| 6495 | /// \returns true if there was an error that should prevent the introduction of |
| 6496 | /// the new declaration into the AST, false otherwise. |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 6497 | bool |
| 6498 | Sema::CheckSpecializationInstantiationRedecl(SourceLocation NewLoc, |
| 6499 | TemplateSpecializationKind NewTSK, |
| 6500 | NamedDecl *PrevDecl, |
| 6501 | TemplateSpecializationKind PrevTSK, |
| 6502 | SourceLocation PrevPointOfInstantiation, |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6503 | bool &HasNoEffect) { |
| 6504 | HasNoEffect = false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6505 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6506 | switch (NewTSK) { |
| 6507 | case TSK_Undeclared: |
| 6508 | case TSK_ImplicitInstantiation: |
David Majnemer | 192d179 | 2013-11-27 08:20:38 +0000 | [diff] [blame] | 6509 | assert( |
| 6510 | (PrevTSK == TSK_Undeclared || PrevTSK == TSK_ImplicitInstantiation) && |
| 6511 | "previous declaration must be implicit!"); |
| 6512 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6513 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6514 | case TSK_ExplicitSpecialization: |
| 6515 | switch (PrevTSK) { |
| 6516 | case TSK_Undeclared: |
| 6517 | case TSK_ExplicitSpecialization: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6518 | // Okay, we're just specializing something that is either already |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6519 | // explicitly specialized or has merely been mentioned without any |
| 6520 | // instantiation. |
| 6521 | return false; |
| 6522 | |
| 6523 | case TSK_ImplicitInstantiation: |
| 6524 | if (PrevPointOfInstantiation.isInvalid()) { |
| 6525 | // The declaration itself has not actually been instantiated, so it is |
| 6526 | // still okay to specialize it. |
John McCall | 4f7ced6 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 6527 | StripImplicitInstantiation(PrevDecl); |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6528 | return false; |
| 6529 | } |
| 6530 | // Fall through |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6531 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6532 | case TSK_ExplicitInstantiationDeclaration: |
| 6533 | case TSK_ExplicitInstantiationDefinition: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6534 | assert((PrevTSK == TSK_ImplicitInstantiation || |
| 6535 | PrevPointOfInstantiation.isValid()) && |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6536 | "Explicit instantiation without point of instantiation?"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6537 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6538 | // C++ [temp.expl.spec]p6: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6539 | // If a template, a member template or the member of a class template |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6540 | // is explicitly specialized then that specialization shall be declared |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6541 | // before the first use of that specialization that would cause an |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6542 | // implicit instantiation to take place, in every translation unit in |
| 6543 | // which such a use occurs; no diagnostic is required. |
Douglas Gregor | 0bc8a21 | 2012-01-14 15:55:47 +0000 | [diff] [blame] | 6544 | for (Decl *Prev = PrevDecl; Prev; Prev = Prev->getPreviousDecl()) { |
Douglas Gregor | c854c66 | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 6545 | // Is there any previous explicit specialization declaration? |
| 6546 | if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) |
| 6547 | return false; |
| 6548 | } |
| 6549 | |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 6550 | Diag(NewLoc, diag::err_specialization_after_instantiation) |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6551 | << PrevDecl; |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 6552 | Diag(PrevPointOfInstantiation, diag::note_instantiation_required_here) |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6553 | << (PrevTSK != TSK_ImplicitInstantiation); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6554 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6555 | return true; |
| 6556 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6557 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6558 | case TSK_ExplicitInstantiationDeclaration: |
| 6559 | switch (PrevTSK) { |
| 6560 | case TSK_ExplicitInstantiationDeclaration: |
| 6561 | // This explicit instantiation declaration is redundant (that's okay). |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6562 | HasNoEffect = true; |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6563 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6564 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6565 | case TSK_Undeclared: |
| 6566 | case TSK_ImplicitInstantiation: |
| 6567 | // We're explicitly instantiating something that may have already been |
| 6568 | // implicitly instantiated; that's fine. |
| 6569 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6570 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6571 | case TSK_ExplicitSpecialization: |
| 6572 | // C++0x [temp.explicit]p4: |
| 6573 | // For a given set of template parameters, if an explicit instantiation |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6574 | // of a template appears after a declaration of an explicit |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6575 | // specialization for that template, the explicit instantiation has no |
| 6576 | // effect. |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6577 | HasNoEffect = true; |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6578 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6579 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6580 | case TSK_ExplicitInstantiationDefinition: |
| 6581 | // C++0x [temp.explicit]p10: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6582 | // If an entity is the subject of both an explicit instantiation |
| 6583 | // declaration and an explicit instantiation definition in the same |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6584 | // translation unit, the definition shall follow the declaration. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6585 | Diag(NewLoc, |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 6586 | diag::err_explicit_instantiation_declaration_after_definition); |
Nico Weber | d3bdadf | 2011-12-23 20:58:04 +0000 | [diff] [blame] | 6587 | |
| 6588 | // Explicit instantiations following a specialization have no effect and |
| 6589 | // hence no PrevPointOfInstantiation. In that case, walk decl backwards |
| 6590 | // until a valid name loc is found. |
Nico Weber | a8f80b3 | 2012-01-09 19:52:25 +0000 | [diff] [blame] | 6591 | Diag(DiagLocForExplicitInstantiation(PrevDecl, PrevPointOfInstantiation), |
| 6592 | diag::note_explicit_instantiation_definition_here); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6593 | HasNoEffect = true; |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6594 | return false; |
| 6595 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6596 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6597 | case TSK_ExplicitInstantiationDefinition: |
| 6598 | switch (PrevTSK) { |
| 6599 | case TSK_Undeclared: |
| 6600 | case TSK_ImplicitInstantiation: |
| 6601 | // We're explicitly instantiating something that may have already been |
| 6602 | // implicitly instantiated; that's fine. |
| 6603 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6604 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6605 | case TSK_ExplicitSpecialization: |
| 6606 | // C++ DR 259, C++0x [temp.explicit]p4: |
| 6607 | // For a given set of template parameters, if an explicit |
| 6608 | // instantiation of a template appears after a declaration of |
| 6609 | // an explicit specialization for that template, the explicit |
| 6610 | // instantiation has no effect. |
| 6611 | // |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6612 | // In C++98/03 mode, we only give an extension warning here, because it |
Douglas Gregor | 06aa5041 | 2010-04-09 21:02:29 +0000 | [diff] [blame] | 6613 | // is not harmful to try to explicitly instantiate something that |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6614 | // has been explicitly specialized. |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 6615 | Diag(NewLoc, getLangOpts().CPlusPlus11 ? |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 6616 | diag::warn_cxx98_compat_explicit_instantiation_after_specialization : |
| 6617 | diag::ext_explicit_instantiation_after_specialization) |
| 6618 | << PrevDecl; |
| 6619 | Diag(PrevDecl->getLocation(), |
| 6620 | diag::note_previous_template_specialization); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6621 | HasNoEffect = true; |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6622 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6623 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6624 | case TSK_ExplicitInstantiationDeclaration: |
| 6625 | // We're explicity instantiating a definition for something for which we |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6626 | // were previously asked to suppress instantiations. That's fine. |
Nico Weber | d3bdadf | 2011-12-23 20:58:04 +0000 | [diff] [blame] | 6627 | |
| 6628 | // C++0x [temp.explicit]p4: |
| 6629 | // For a given set of template parameters, if an explicit instantiation |
| 6630 | // of a template appears after a declaration of an explicit |
| 6631 | // specialization for that template, the explicit instantiation has no |
| 6632 | // effect. |
Douglas Gregor | 0bc8a21 | 2012-01-14 15:55:47 +0000 | [diff] [blame] | 6633 | for (Decl *Prev = PrevDecl; Prev; Prev = Prev->getPreviousDecl()) { |
Nico Weber | d3bdadf | 2011-12-23 20:58:04 +0000 | [diff] [blame] | 6634 | // Is there any previous explicit specialization declaration? |
| 6635 | if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) { |
| 6636 | HasNoEffect = true; |
| 6637 | break; |
| 6638 | } |
| 6639 | } |
| 6640 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6641 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6642 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6643 | case TSK_ExplicitInstantiationDefinition: |
| 6644 | // C++0x [temp.spec]p5: |
| 6645 | // For a given template and a given set of template-arguments, |
| 6646 | // - an explicit instantiation definition shall appear at most once |
| 6647 | // in a program, |
Will Wilson | eadcdbb | 2014-05-09 09:52:13 +0000 | [diff] [blame] | 6648 | |
| 6649 | // MSVCCompat: MSVC silently ignores duplicate explicit instantiations. |
| 6650 | Diag(NewLoc, (getLangOpts().MSVCCompat) |
Richard Smith | 1b98ccc | 2014-07-19 01:39:17 +0000 | [diff] [blame] | 6651 | ? diag::ext_explicit_instantiation_duplicate |
Will Wilson | eadcdbb | 2014-05-09 09:52:13 +0000 | [diff] [blame] | 6652 | : diag::err_explicit_instantiation_duplicate) |
| 6653 | << PrevDecl; |
Nico Weber | a8f80b3 | 2012-01-09 19:52:25 +0000 | [diff] [blame] | 6654 | Diag(DiagLocForExplicitInstantiation(PrevDecl, PrevPointOfInstantiation), |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 6655 | diag::note_previous_explicit_instantiation); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6656 | HasNoEffect = true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6657 | return false; |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6658 | } |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6659 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6660 | |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 6661 | llvm_unreachable("Missing specialization/instantiation case?"); |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6662 | } |
| 6663 | |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 6664 | /// \brief Perform semantic analysis for the given dependent function |
James Dennett | f14a6e5 | 2012-06-15 22:23:43 +0000 | [diff] [blame] | 6665 | /// template specialization. |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 6666 | /// |
James Dennett | f14a6e5 | 2012-06-15 22:23:43 +0000 | [diff] [blame] | 6667 | /// The only possible way to get a dependent function template specialization |
| 6668 | /// is with a friend declaration, like so: |
| 6669 | /// |
| 6670 | /// \code |
| 6671 | /// template \<class T> void foo(T); |
| 6672 | /// template \<class T> class A { |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 6673 | /// friend void foo<>(T); |
| 6674 | /// }; |
James Dennett | f14a6e5 | 2012-06-15 22:23:43 +0000 | [diff] [blame] | 6675 | /// \endcode |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 6676 | /// |
| 6677 | /// There really isn't any useful analysis we can do here, so we |
| 6678 | /// just store the information. |
| 6679 | bool |
| 6680 | Sema::CheckDependentFunctionTemplateSpecialization(FunctionDecl *FD, |
| 6681 | const TemplateArgumentListInfo &ExplicitTemplateArgs, |
| 6682 | LookupResult &Previous) { |
| 6683 | // Remove anything from Previous that isn't a function template in |
| 6684 | // the correct context. |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 6685 | DeclContext *FDLookupContext = FD->getDeclContext()->getRedeclContext(); |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 6686 | LookupResult::Filter F = Previous.makeFilter(); |
| 6687 | while (F.hasNext()) { |
| 6688 | NamedDecl *D = F.next()->getUnderlyingDecl(); |
| 6689 | if (!isa<FunctionTemplateDecl>(D) || |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 6690 | !FDLookupContext->InEnclosingNamespaceSetOf( |
| 6691 | D->getDeclContext()->getRedeclContext())) |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 6692 | F.erase(); |
| 6693 | } |
| 6694 | F.done(); |
| 6695 | |
| 6696 | // Should this be diagnosed here? |
| 6697 | if (Previous.empty()) return true; |
| 6698 | |
| 6699 | FD->setDependentTemplateSpecialization(Context, Previous.asUnresolvedSet(), |
| 6700 | ExplicitTemplateArgs); |
| 6701 | return false; |
| 6702 | } |
| 6703 | |
Abramo Bagnara | 02ccd28 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 6704 | /// \brief Perform semantic analysis for the given function template |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6705 | /// specialization. |
| 6706 | /// |
Abramo Bagnara | 02ccd28 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 6707 | /// This routine performs all of the semantic analysis required for an |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6708 | /// explicit function template specialization. On successful completion, |
| 6709 | /// the function declaration \p FD will become a function template |
| 6710 | /// specialization. |
| 6711 | /// |
| 6712 | /// \param FD the function declaration, which will be updated to become a |
| 6713 | /// function template specialization. |
| 6714 | /// |
Abramo Bagnara | 02ccd28 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 6715 | /// \param ExplicitTemplateArgs the explicitly-provided template arguments, |
| 6716 | /// if any. Note that this may be valid info even when 0 arguments are |
| 6717 | /// explicitly provided as in, e.g., \c void sort<>(char*, char*); |
| 6718 | /// as it anyway contains info on the angle brackets locations. |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6719 | /// |
Francois Pichet | 3a44e43 | 2011-07-08 06:21:47 +0000 | [diff] [blame] | 6720 | /// \param Previous the set of declarations that may be specialized by |
Abramo Bagnara | 02ccd28 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 6721 | /// this function specialization. |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 6722 | bool Sema::CheckFunctionTemplateSpecialization( |
| 6723 | FunctionDecl *FD, TemplateArgumentListInfo *ExplicitTemplateArgs, |
| 6724 | LookupResult &Previous) { |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6725 | // The set of function template specializations that could match this |
| 6726 | // explicit function template specialization. |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6727 | UnresolvedSet<8> Candidates; |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 6728 | TemplateSpecCandidateSet FailedCandidates(FD->getLocation()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6729 | |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 6730 | DeclContext *FDLookupContext = FD->getDeclContext()->getRedeclContext(); |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 6731 | for (LookupResult::iterator I = Previous.begin(), E = Previous.end(); |
| 6732 | I != E; ++I) { |
| 6733 | NamedDecl *Ovl = (*I)->getUnderlyingDecl(); |
| 6734 | if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Ovl)) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6735 | // Only consider templates found within the same semantic lookup scope as |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6736 | // FD. |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 6737 | if (!FDLookupContext->InEnclosingNamespaceSetOf( |
| 6738 | Ovl->getDeclContext()->getRedeclContext())) |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6739 | continue; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6740 | |
Richard Smith | 574f4f6 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 6741 | // When matching a constexpr member function template specialization |
| 6742 | // against the primary template, we don't yet know whether the |
| 6743 | // specialization has an implicit 'const' (because we don't know whether |
| 6744 | // it will be a static member function until we know which template it |
| 6745 | // specializes), so adjust it now assuming it specializes this template. |
| 6746 | QualType FT = FD->getType(); |
| 6747 | if (FD->isConstexpr()) { |
Rafael Espindola | 92045bc | 2013-11-19 21:07:04 +0000 | [diff] [blame] | 6748 | CXXMethodDecl *OldMD = |
| 6749 | dyn_cast<CXXMethodDecl>(FunTmpl->getTemplatedDecl()); |
Richard Smith | 574f4f6 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 6750 | if (OldMD && OldMD->isConst()) { |
Rafael Espindola | 92045bc | 2013-11-19 21:07:04 +0000 | [diff] [blame] | 6751 | const FunctionProtoType *FPT = FT->castAs<FunctionProtoType>(); |
Richard Smith | 574f4f6 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 6752 | FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo(); |
| 6753 | EPI.TypeQuals |= Qualifiers::Const; |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 6754 | FT = Context.getFunctionType(FPT->getReturnType(), |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 6755 | FPT->getParamTypes(), EPI); |
Richard Smith | 574f4f6 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 6756 | } |
| 6757 | } |
| 6758 | |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6759 | // C++ [temp.expl.spec]p11: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6760 | // A trailing template-argument can be left unspecified in the |
| 6761 | // template-id naming an explicit function template specialization |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6762 | // provided it can be deduced from the function argument type. |
| 6763 | // Perform template argument deduction to determine whether we may be |
| 6764 | // specializing this template. |
| 6765 | // FIXME: It is somewhat wasteful to build |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 6766 | TemplateDeductionInfo Info(FailedCandidates.getLocation()); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6767 | FunctionDecl *Specialization = nullptr; |
Richard Smith | 3298368 | 2013-12-14 03:18:05 +0000 | [diff] [blame] | 6768 | if (TemplateDeductionResult TDK = DeduceTemplateArguments( |
| 6769 | cast<FunctionTemplateDecl>(FunTmpl->getFirstDecl()), |
| 6770 | ExplicitTemplateArgs, FT, Specialization, Info)) { |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 6771 | // Template argument deduction failed; record why it failed, so |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6772 | // that we can provide nifty diagnostics. |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 6773 | FailedCandidates.addCandidate() |
| 6774 | .set(FunTmpl->getTemplatedDecl(), |
| 6775 | MakeDeductionFailureInfo(Context, TDK, Info)); |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6776 | (void)TDK; |
| 6777 | continue; |
| 6778 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6779 | |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6780 | // Record this candidate. |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6781 | Candidates.addDecl(Specialization, I.getAccess()); |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6782 | } |
| 6783 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6784 | |
Douglas Gregor | 5de279c | 2009-09-26 03:41:46 +0000 | [diff] [blame] | 6785 | // Find the most specialized function template. |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 6786 | UnresolvedSetIterator Result = getMostSpecialized( |
Richard Smith | 35e1da2 | 2013-09-10 22:59:25 +0000 | [diff] [blame] | 6787 | Candidates.begin(), Candidates.end(), FailedCandidates, |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 6788 | FD->getLocation(), |
| 6789 | PDiag(diag::err_function_template_spec_no_match) << FD->getDeclName(), |
| 6790 | PDiag(diag::err_function_template_spec_ambiguous) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6791 | << FD->getDeclName() << (ExplicitTemplateArgs != nullptr), |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 6792 | PDiag(diag::note_function_template_spec_matched)); |
| 6793 | |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6794 | if (Result == Candidates.end()) |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6795 | return true; |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6796 | |
| 6797 | // Ignore access information; it doesn't figure into redeclaration checking. |
| 6798 | FunctionDecl *Specialization = cast<FunctionDecl>(*Result); |
Abramo Bagnara | b9893d6 | 2011-03-04 17:20:30 +0000 | [diff] [blame] | 6799 | |
| 6800 | FunctionTemplateSpecializationInfo *SpecInfo |
| 6801 | = Specialization->getTemplateSpecializationInfo(); |
| 6802 | assert(SpecInfo && "Function template specialization info missing?"); |
Francois Pichet | 3a44e43 | 2011-07-08 06:21:47 +0000 | [diff] [blame] | 6803 | |
| 6804 | // Note: do not overwrite location info if previous template |
| 6805 | // specialization kind was explicit. |
| 6806 | TemplateSpecializationKind TSK = SpecInfo->getTemplateSpecializationKind(); |
Richard Smith | 5b8b3db | 2012-02-20 23:28:05 +0000 | [diff] [blame] | 6807 | if (TSK == TSK_Undeclared || TSK == TSK_ImplicitInstantiation) { |
Francois Pichet | 3a44e43 | 2011-07-08 06:21:47 +0000 | [diff] [blame] | 6808 | Specialization->setLocation(FD->getLocation()); |
Richard Smith | 5b8b3db | 2012-02-20 23:28:05 +0000 | [diff] [blame] | 6809 | // C++11 [dcl.constexpr]p1: An explicit specialization of a constexpr |
| 6810 | // function can differ from the template declaration with respect to |
| 6811 | // the constexpr specifier. |
| 6812 | Specialization->setConstexpr(FD->isConstexpr()); |
| 6813 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6814 | |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6815 | // FIXME: Check if the prior specialization has a point of instantiation. |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6816 | // If so, we have run afoul of . |
John McCall | 816d75b | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 6817 | |
| 6818 | // If this is a friend declaration, then we're not really declaring |
| 6819 | // an explicit specialization. |
| 6820 | bool isFriend = (FD->getFriendObjectKind() != Decl::FOK_None); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6821 | |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 6822 | // Check the scope of this explicit specialization. |
John McCall | 816d75b | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 6823 | if (!isFriend && |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6824 | CheckTemplateSpecializationScope(*this, |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 6825 | Specialization->getPrimaryTemplate(), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6826 | Specialization, FD->getLocation(), |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 6827 | false)) |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 6828 | return true; |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6829 | |
| 6830 | // C++ [temp.expl.spec]p6: |
| 6831 | // If a template, a member template or the member of a class template is |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6832 | // explicitly specialized then that specialization shall be declared |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6833 | // before the first use of that specialization that would cause an implicit |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6834 | // instantiation to take place, in every translation unit in which such a |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6835 | // use occurs; no diagnostic is required. |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6836 | bool HasNoEffect = false; |
John McCall | 816d75b | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 6837 | if (!isFriend && |
| 6838 | CheckSpecializationInstantiationRedecl(FD->getLocation(), |
John McCall | 4f7ced6 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 6839 | TSK_ExplicitSpecialization, |
| 6840 | Specialization, |
| 6841 | SpecInfo->getTemplateSpecializationKind(), |
| 6842 | SpecInfo->getPointOfInstantiation(), |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6843 | HasNoEffect)) |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6844 | return true; |
Douglas Gregor | 781ba6e | 2011-05-21 18:53:30 +0000 | [diff] [blame] | 6845 | |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6846 | // Mark the prior declaration as an explicit specialization, so that later |
| 6847 | // clients know that this is an explicit specialization. |
Argyrios Kyrtzidis | 1b30d9c | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 6848 | if (!isFriend) { |
John McCall | 816d75b | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 6849 | SpecInfo->setTemplateSpecializationKind(TSK_ExplicitSpecialization); |
Argyrios Kyrtzidis | 1b30d9c | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 6850 | MarkUnusedFileScopedDecl(Specialization); |
| 6851 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6852 | |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6853 | // Turn the given function declaration into a function template |
| 6854 | // specialization, with the template arguments from the previous |
| 6855 | // specialization. |
Abramo Bagnara | 02ccd28 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 6856 | // Take copies of (semantic and syntactic) template argument lists. |
| 6857 | const TemplateArgumentList* TemplArgs = new (Context) |
| 6858 | TemplateArgumentList(Specialization->getTemplateSpecializationArgs()); |
Douglas Gregor | d505812 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 6859 | FD->setFunctionTemplateSpecialization(Specialization->getPrimaryTemplate(), |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6860 | TemplArgs, /*InsertPos=*/nullptr, |
Abramo Bagnara | 02ccd28 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 6861 | SpecInfo->getTemplateSpecializationKind(), |
Argyrios Kyrtzidis | e9a2443 | 2011-09-22 20:07:09 +0000 | [diff] [blame] | 6862 | ExplicitTemplateArgs); |
Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 6863 | |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6864 | // The "previous declaration" for this function template specialization is |
| 6865 | // the prior function template specialization. |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 6866 | Previous.clear(); |
| 6867 | Previous.addDecl(Specialization); |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6868 | return false; |
| 6869 | } |
| 6870 | |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 6871 | /// \brief Perform semantic analysis for the given non-template member |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 6872 | /// specialization. |
| 6873 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6874 | /// This routine performs all of the semantic analysis required for an |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 6875 | /// explicit member function specialization. On successful completion, |
| 6876 | /// the function declaration \p FD will become a member function |
| 6877 | /// specialization. |
| 6878 | /// |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 6879 | /// \param Member the member declaration, which will be updated to become a |
| 6880 | /// specialization. |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 6881 | /// |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 6882 | /// \param Previous the set of declarations, one of which may be specialized |
| 6883 | /// by this function specialization; the set will be modified to contain the |
| 6884 | /// redeclared member. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6885 | bool |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 6886 | Sema::CheckMemberSpecialization(NamedDecl *Member, LookupResult &Previous) { |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 6887 | assert(!isa<TemplateDecl>(Member) && "Only for non-template members"); |
John McCall | e820e5e | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 6888 | |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 6889 | // Try to find the member we are instantiating. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6890 | NamedDecl *Instantiation = nullptr; |
| 6891 | NamedDecl *InstantiatedFrom = nullptr; |
| 6892 | MemberSpecializationInfo *MSInfo = nullptr; |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6893 | |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 6894 | if (Previous.empty()) { |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 6895 | // Nowhere to look anyway. |
| 6896 | } else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Member)) { |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 6897 | for (LookupResult::iterator I = Previous.begin(), E = Previous.end(); |
| 6898 | I != E; ++I) { |
| 6899 | NamedDecl *D = (*I)->getUnderlyingDecl(); |
| 6900 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) { |
Rafael Espindola | 6674722 | 2013-12-10 00:59:31 +0000 | [diff] [blame] | 6901 | QualType Adjusted = Function->getType(); |
| 6902 | if (!hasExplicitCallingConv(Adjusted)) |
| 6903 | Adjusted = adjustCCAndNoReturn(Adjusted, Method->getType()); |
| 6904 | if (Context.hasSameType(Adjusted, Method->getType())) { |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 6905 | Instantiation = Method; |
| 6906 | InstantiatedFrom = Method->getInstantiatedFromMemberFunction(); |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6907 | MSInfo = Method->getMemberSpecializationInfo(); |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 6908 | break; |
| 6909 | } |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 6910 | } |
| 6911 | } |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 6912 | } else if (isa<VarDecl>(Member)) { |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 6913 | VarDecl *PrevVar; |
| 6914 | if (Previous.isSingleResult() && |
| 6915 | (PrevVar = dyn_cast<VarDecl>(Previous.getFoundDecl()))) |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 6916 | if (PrevVar->isStaticDataMember()) { |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 6917 | Instantiation = PrevVar; |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 6918 | InstantiatedFrom = PrevVar->getInstantiatedFromStaticDataMember(); |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6919 | MSInfo = PrevVar->getMemberSpecializationInfo(); |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 6920 | } |
| 6921 | } else if (isa<RecordDecl>(Member)) { |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 6922 | CXXRecordDecl *PrevRecord; |
| 6923 | if (Previous.isSingleResult() && |
| 6924 | (PrevRecord = dyn_cast<CXXRecordDecl>(Previous.getFoundDecl()))) { |
| 6925 | Instantiation = PrevRecord; |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 6926 | InstantiatedFrom = PrevRecord->getInstantiatedFromMemberClass(); |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6927 | MSInfo = PrevRecord->getMemberSpecializationInfo(); |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 6928 | } |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 6929 | } else if (isa<EnumDecl>(Member)) { |
| 6930 | EnumDecl *PrevEnum; |
| 6931 | if (Previous.isSingleResult() && |
| 6932 | (PrevEnum = dyn_cast<EnumDecl>(Previous.getFoundDecl()))) { |
| 6933 | Instantiation = PrevEnum; |
| 6934 | InstantiatedFrom = PrevEnum->getInstantiatedFromMemberEnum(); |
| 6935 | MSInfo = PrevEnum->getMemberSpecializationInfo(); |
| 6936 | } |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 6937 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6938 | |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 6939 | if (!Instantiation) { |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 6940 | // There is no previous declaration that matches. Since member |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 6941 | // specializations are always out-of-line, the caller will complain about |
| 6942 | // this mismatch later. |
| 6943 | return false; |
| 6944 | } |
John McCall | e820e5e | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 6945 | |
| 6946 | // If this is a friend, just bail out here before we start turning |
| 6947 | // things into explicit specializations. |
| 6948 | if (Member->getFriendObjectKind() != Decl::FOK_None) { |
| 6949 | // Preserve instantiation information. |
| 6950 | if (InstantiatedFrom && isa<CXXMethodDecl>(Member)) { |
| 6951 | cast<CXXMethodDecl>(Member)->setInstantiationOfMemberFunction( |
| 6952 | cast<CXXMethodDecl>(InstantiatedFrom), |
| 6953 | cast<CXXMethodDecl>(Instantiation)->getTemplateSpecializationKind()); |
| 6954 | } else if (InstantiatedFrom && isa<CXXRecordDecl>(Member)) { |
| 6955 | cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass( |
| 6956 | cast<CXXRecordDecl>(InstantiatedFrom), |
| 6957 | cast<CXXRecordDecl>(Instantiation)->getTemplateSpecializationKind()); |
| 6958 | } |
| 6959 | |
| 6960 | Previous.clear(); |
| 6961 | Previous.addDecl(Instantiation); |
| 6962 | return false; |
| 6963 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6964 | |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 6965 | // Make sure that this is a specialization of a member. |
| 6966 | if (!InstantiatedFrom) { |
| 6967 | Diag(Member->getLocation(), diag::err_spec_member_not_instantiated) |
| 6968 | << Member; |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 6969 | Diag(Instantiation->getLocation(), diag::note_specialized_decl); |
| 6970 | return true; |
| 6971 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6972 | |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6973 | // C++ [temp.expl.spec]p6: |
| 6974 | // If a template, a member template or the member of a class template is |
Nico Weber | d3bdadf | 2011-12-23 20:58:04 +0000 | [diff] [blame] | 6975 | // explicitly specialized then that specialization shall be declared |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6976 | // before the first use of that specialization that would cause an implicit |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6977 | // instantiation to take place, in every translation unit in which such a |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6978 | // use occurs; no diagnostic is required. |
| 6979 | assert(MSInfo && "Member specialization info missing?"); |
John McCall | 4f7ced6 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 6980 | |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6981 | bool HasNoEffect = false; |
John McCall | 4f7ced6 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 6982 | if (CheckSpecializationInstantiationRedecl(Member->getLocation(), |
| 6983 | TSK_ExplicitSpecialization, |
| 6984 | Instantiation, |
| 6985 | MSInfo->getTemplateSpecializationKind(), |
| 6986 | MSInfo->getPointOfInstantiation(), |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6987 | HasNoEffect)) |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6988 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6989 | |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 6990 | // Check the scope of this explicit specialization. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6991 | if (CheckTemplateSpecializationScope(*this, |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 6992 | InstantiatedFrom, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6993 | Instantiation, Member->getLocation(), |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 6994 | false)) |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 6995 | return true; |
Douglas Gregor | d801b06 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 6996 | |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 6997 | // Note that this is an explicit instantiation of a member. |
Douglas Gregor | bbe8f46 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 6998 | // the original declaration to note that it is an explicit specialization |
| 6999 | // (if it was previously an implicit instantiation). This latter step |
| 7000 | // makes bookkeeping easier. |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7001 | if (isa<FunctionDecl>(Member)) { |
Douglas Gregor | bbe8f46 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 7002 | FunctionDecl *InstantiationFunction = cast<FunctionDecl>(Instantiation); |
| 7003 | if (InstantiationFunction->getTemplateSpecializationKind() == |
| 7004 | TSK_ImplicitInstantiation) { |
| 7005 | InstantiationFunction->setTemplateSpecializationKind( |
| 7006 | TSK_ExplicitSpecialization); |
| 7007 | InstantiationFunction->setLocation(Member->getLocation()); |
| 7008 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7009 | |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7010 | cast<FunctionDecl>(Member)->setInstantiationOfMemberFunction( |
| 7011 | cast<CXXMethodDecl>(InstantiatedFrom), |
| 7012 | TSK_ExplicitSpecialization); |
Argyrios Kyrtzidis | 1b30d9c | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 7013 | MarkUnusedFileScopedDecl(InstantiationFunction); |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7014 | } else if (isa<VarDecl>(Member)) { |
Douglas Gregor | bbe8f46 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 7015 | VarDecl *InstantiationVar = cast<VarDecl>(Instantiation); |
| 7016 | if (InstantiationVar->getTemplateSpecializationKind() == |
| 7017 | TSK_ImplicitInstantiation) { |
| 7018 | InstantiationVar->setTemplateSpecializationKind( |
| 7019 | TSK_ExplicitSpecialization); |
| 7020 | InstantiationVar->setLocation(Member->getLocation()); |
| 7021 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7022 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 7023 | cast<VarDecl>(Member)->setInstantiationOfStaticDataMember( |
| 7024 | cast<VarDecl>(InstantiatedFrom), TSK_ExplicitSpecialization); |
Argyrios Kyrtzidis | 1b30d9c | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 7025 | MarkUnusedFileScopedDecl(InstantiationVar); |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 7026 | } else if (isa<CXXRecordDecl>(Member)) { |
Douglas Gregor | bbe8f46 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 7027 | CXXRecordDecl *InstantiationClass = cast<CXXRecordDecl>(Instantiation); |
| 7028 | if (InstantiationClass->getTemplateSpecializationKind() == |
| 7029 | TSK_ImplicitInstantiation) { |
| 7030 | InstantiationClass->setTemplateSpecializationKind( |
| 7031 | TSK_ExplicitSpecialization); |
| 7032 | InstantiationClass->setLocation(Member->getLocation()); |
| 7033 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7034 | |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7035 | cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass( |
Douglas Gregor | bbe8f46 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 7036 | cast<CXXRecordDecl>(InstantiatedFrom), |
| 7037 | TSK_ExplicitSpecialization); |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 7038 | } else { |
| 7039 | assert(isa<EnumDecl>(Member) && "Only member enums remain"); |
| 7040 | EnumDecl *InstantiationEnum = cast<EnumDecl>(Instantiation); |
| 7041 | if (InstantiationEnum->getTemplateSpecializationKind() == |
| 7042 | TSK_ImplicitInstantiation) { |
| 7043 | InstantiationEnum->setTemplateSpecializationKind( |
| 7044 | TSK_ExplicitSpecialization); |
| 7045 | InstantiationEnum->setLocation(Member->getLocation()); |
| 7046 | } |
| 7047 | |
| 7048 | cast<EnumDecl>(Member)->setInstantiationOfMemberEnum( |
| 7049 | cast<EnumDecl>(InstantiatedFrom), TSK_ExplicitSpecialization); |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7050 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7051 | |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 7052 | // Save the caller the trouble of having to figure out which declaration |
| 7053 | // this specialization matches. |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 7054 | Previous.clear(); |
| 7055 | Previous.addDecl(Instantiation); |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 7056 | return false; |
| 7057 | } |
| 7058 | |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7059 | /// \brief Check the scope of an explicit instantiation. |
Douglas Gregor | 6cc1df5 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 7060 | /// |
| 7061 | /// \returns true if a serious error occurs, false otherwise. |
| 7062 | static bool CheckExplicitInstantiationScope(Sema &S, NamedDecl *D, |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7063 | SourceLocation InstLoc, |
| 7064 | bool WasQualifiedName) { |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 7065 | DeclContext *OrigContext= D->getDeclContext()->getEnclosingNamespaceContext(); |
| 7066 | DeclContext *CurContext = S.CurContext->getRedeclContext(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7067 | |
Douglas Gregor | 6cc1df5 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 7068 | if (CurContext->isRecord()) { |
| 7069 | S.Diag(InstLoc, diag::err_explicit_instantiation_in_class) |
| 7070 | << D; |
| 7071 | return true; |
| 7072 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7073 | |
Richard Smith | 050d261 | 2011-10-18 02:28:33 +0000 | [diff] [blame] | 7074 | // C++11 [temp.explicit]p3: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7075 | // An explicit instantiation shall appear in an enclosing namespace of its |
Richard Smith | 050d261 | 2011-10-18 02:28:33 +0000 | [diff] [blame] | 7076 | // template. If the name declared in the explicit instantiation is an |
| 7077 | // unqualified name, the explicit instantiation shall appear in the |
| 7078 | // namespace where its template is declared or, if that namespace is inline |
| 7079 | // (7.3.1), any namespace from its enclosing namespace set. |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7080 | // |
| 7081 | // This is DR275, which we do not retroactively apply to C++98/03. |
Richard Smith | 050d261 | 2011-10-18 02:28:33 +0000 | [diff] [blame] | 7082 | if (WasQualifiedName) { |
| 7083 | if (CurContext->Encloses(OrigContext)) |
| 7084 | return false; |
| 7085 | } else { |
| 7086 | if (CurContext->InEnclosingNamespaceSetOf(OrigContext)) |
| 7087 | return false; |
| 7088 | } |
| 7089 | |
| 7090 | if (NamespaceDecl *NS = dyn_cast<NamespaceDecl>(OrigContext)) { |
| 7091 | if (WasQualifiedName) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7092 | S.Diag(InstLoc, |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 7093 | S.getLangOpts().CPlusPlus11? |
Richard Smith | 050d261 | 2011-10-18 02:28:33 +0000 | [diff] [blame] | 7094 | diag::err_explicit_instantiation_out_of_scope : |
| 7095 | diag::warn_explicit_instantiation_out_of_scope_0x) |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7096 | << D << NS; |
| 7097 | else |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7098 | S.Diag(InstLoc, |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 7099 | S.getLangOpts().CPlusPlus11? |
Richard Smith | 050d261 | 2011-10-18 02:28:33 +0000 | [diff] [blame] | 7100 | diag::err_explicit_instantiation_unqualified_wrong_namespace : |
| 7101 | diag::warn_explicit_instantiation_unqualified_wrong_namespace_0x) |
| 7102 | << D << NS; |
| 7103 | } else |
| 7104 | S.Diag(InstLoc, |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 7105 | S.getLangOpts().CPlusPlus11? |
Richard Smith | 050d261 | 2011-10-18 02:28:33 +0000 | [diff] [blame] | 7106 | diag::err_explicit_instantiation_must_be_global : |
| 7107 | diag::warn_explicit_instantiation_must_be_global_0x) |
| 7108 | << D; |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7109 | S.Diag(D->getLocation(), diag::note_explicit_instantiation_here); |
Douglas Gregor | 6cc1df5 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 7110 | return false; |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7111 | } |
| 7112 | |
| 7113 | /// \brief Determine whether the given scope specifier has a template-id in it. |
| 7114 | static bool ScopeSpecifierHasTemplateId(const CXXScopeSpec &SS) { |
| 7115 | if (!SS.isSet()) |
| 7116 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7117 | |
Richard Smith | 050d261 | 2011-10-18 02:28:33 +0000 | [diff] [blame] | 7118 | // C++11 [temp.explicit]p3: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7119 | // If the explicit instantiation is for a member function, a member class |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7120 | // or a static data member of a class template specialization, the name of |
| 7121 | // the class template specialization in the qualified-id for the member |
| 7122 | // name shall be a simple-template-id. |
| 7123 | // |
| 7124 | // C++98 has the same restriction, just worded differently. |
Aaron Ballman | 4a97967 | 2014-01-03 13:56:08 +0000 | [diff] [blame] | 7125 | for (NestedNameSpecifier *NNS = SS.getScopeRep(); NNS; |
| 7126 | NNS = NNS->getPrefix()) |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 7127 | if (const Type *T = NNS->getAsType()) |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7128 | if (isa<TemplateSpecializationType>(T)) |
| 7129 | return true; |
| 7130 | |
| 7131 | return false; |
| 7132 | } |
| 7133 | |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 7134 | // Explicit instantiation of a class template specialization |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 7135 | DeclResult |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7136 | Sema::ActOnExplicitInstantiation(Scope *S, |
Douglas Gregor | 43e7517 | 2009-09-04 06:33:52 +0000 | [diff] [blame] | 7137 | SourceLocation ExternLoc, |
| 7138 | SourceLocation TemplateLoc, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7139 | unsigned TagSpec, |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7140 | SourceLocation KWLoc, |
| 7141 | const CXXScopeSpec &SS, |
| 7142 | TemplateTy TemplateD, |
| 7143 | SourceLocation TemplateNameLoc, |
| 7144 | SourceLocation LAngleLoc, |
| 7145 | ASTTemplateArgsPtr TemplateArgsIn, |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7146 | SourceLocation RAngleLoc, |
| 7147 | AttributeList *Attr) { |
| 7148 | // Find the class template we're specializing |
Serge Pavlov | 9ddb76e | 2013-08-27 13:15:56 +0000 | [diff] [blame] | 7149 | TemplateName Name = TemplateD.get(); |
Richard Smith | 392497b | 2013-06-22 22:03:31 +0000 | [diff] [blame] | 7150 | TemplateDecl *TD = Name.getAsTemplateDecl(); |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7151 | // Check that the specialization uses the same tag kind as the |
| 7152 | // original template. |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 7153 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
| 7154 | assert(Kind != TTK_Enum && |
| 7155 | "Invalid enum tag in class template explicit instantiation!"); |
Richard Smith | 392497b | 2013-06-22 22:03:31 +0000 | [diff] [blame] | 7156 | |
| 7157 | if (isa<TypeAliasTemplateDecl>(TD)) { |
| 7158 | Diag(KWLoc, diag::err_tag_reference_non_tag) << Kind; |
| 7159 | Diag(TD->getTemplatedDecl()->getLocation(), |
| 7160 | diag::note_previous_use); |
| 7161 | return true; |
| 7162 | } |
| 7163 | |
| 7164 | ClassTemplateDecl *ClassTemplate = cast<ClassTemplateDecl>(TD); |
| 7165 | |
Douglas Gregor | d9034f0 | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 7166 | if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(), |
Richard Trieu | caa33d3 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 7167 | Kind, /*isDefinition*/false, KWLoc, |
Douglas Gregor | d9034f0 | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 7168 | *ClassTemplate->getIdentifier())) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7169 | Diag(KWLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7170 | << ClassTemplate |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 7171 | << FixItHint::CreateReplacement(KWLoc, |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7172 | ClassTemplate->getTemplatedDecl()->getKindName()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7173 | Diag(ClassTemplate->getTemplatedDecl()->getLocation(), |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7174 | diag::note_previous_use); |
| 7175 | Kind = ClassTemplate->getTemplatedDecl()->getTagKind(); |
| 7176 | } |
| 7177 | |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7178 | // C++0x [temp.explicit]p2: |
| 7179 | // There are two forms of explicit instantiation: an explicit instantiation |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7180 | // definition and an explicit instantiation declaration. An explicit |
| 7181 | // instantiation declaration begins with the extern keyword. [...] |
Hans Wennborg | fd76d91 | 2015-01-15 21:18:30 +0000 | [diff] [blame] | 7182 | TemplateSpecializationKind TSK = ExternLoc.isInvalid() |
| 7183 | ? TSK_ExplicitInstantiationDefinition |
| 7184 | : TSK_ExplicitInstantiationDeclaration; |
| 7185 | |
| 7186 | if (TSK == TSK_ExplicitInstantiationDeclaration) { |
| 7187 | // Check for dllexport class template instantiation declarations. |
| 7188 | for (AttributeList *A = Attr; A; A = A->getNext()) { |
| 7189 | if (A->getKind() == AttributeList::AT_DLLExport) { |
| 7190 | Diag(ExternLoc, |
| 7191 | diag::warn_attribute_dllexport_explicit_instantiation_decl); |
| 7192 | Diag(A->getLoc(), diag::note_attribute); |
| 7193 | break; |
| 7194 | } |
| 7195 | } |
| 7196 | |
| 7197 | if (auto *A = ClassTemplate->getTemplatedDecl()->getAttr<DLLExportAttr>()) { |
| 7198 | Diag(ExternLoc, |
| 7199 | diag::warn_attribute_dllexport_explicit_instantiation_decl); |
| 7200 | Diag(A->getLocation(), diag::note_attribute); |
| 7201 | } |
| 7202 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7203 | |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7204 | // Translate the parser's template argument list in our AST format. |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 7205 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
Douglas Gregor | b53edfb | 2009-11-10 19:49:08 +0000 | [diff] [blame] | 7206 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7207 | |
| 7208 | // Check that the template argument list is well-formed for this |
| 7209 | // template. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 7210 | SmallVector<TemplateArgument, 4> Converted; |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 7211 | if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc, |
| 7212 | TemplateArgs, false, Converted)) |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7213 | return true; |
| 7214 | |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7215 | // Find the class template specialization declaration that |
| 7216 | // corresponds to these arguments. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7217 | void *InsertPos = nullptr; |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7218 | ClassTemplateSpecializationDecl *PrevDecl |
Craig Topper | 7e0daca | 2014-06-26 04:58:53 +0000 | [diff] [blame] | 7219 | = ClassTemplate->findSpecialization(Converted, InsertPos); |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7220 | |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7221 | TemplateSpecializationKind PrevDecl_TSK |
| 7222 | = PrevDecl ? PrevDecl->getTemplateSpecializationKind() : TSK_Undeclared; |
| 7223 | |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 7224 | // C++0x [temp.explicit]p2: |
| 7225 | // [...] An explicit instantiation shall appear in an enclosing |
| 7226 | // namespace of its template. [...] |
| 7227 | // |
| 7228 | // This is C++ DR 275. |
Douglas Gregor | 6cc1df5 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 7229 | if (CheckExplicitInstantiationScope(*this, ClassTemplate, TemplateNameLoc, |
| 7230 | SS.isSet())) |
| 7231 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7232 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7233 | ClassTemplateSpecializationDecl *Specialization = nullptr; |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7234 | |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7235 | bool HasNoEffect = false; |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7236 | if (PrevDecl) { |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 7237 | if (CheckSpecializationInstantiationRedecl(TemplateNameLoc, TSK, |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7238 | PrevDecl, PrevDecl_TSK, |
Douglas Gregor | 12e49d3 | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 7239 | PrevDecl->getPointOfInstantiation(), |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7240 | HasNoEffect)) |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7241 | return PrevDecl; |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7242 | |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7243 | // Even though HasNoEffect == true means that this explicit instantiation |
| 7244 | // has no effect on semantics, we go on to put its syntax in the AST. |
| 7245 | |
| 7246 | if (PrevDecl_TSK == TSK_ImplicitInstantiation || |
| 7247 | PrevDecl_TSK == TSK_Undeclared) { |
Douglas Gregor | 4aa04b1 | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 7248 | // Since the only prior class template specialization with these |
| 7249 | // arguments was referenced but not declared, reuse that |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7250 | // declaration node as our own, updating the source location |
| 7251 | // for the template name to reflect our new declaration. |
| 7252 | // (Other source locations will be updated later.) |
Douglas Gregor | 4aa04b1 | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 7253 | Specialization = PrevDecl; |
| 7254 | Specialization->setLocation(TemplateNameLoc); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7255 | PrevDecl = nullptr; |
Douglas Gregor | 4aa04b1 | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 7256 | } |
Douglas Gregor | 12e49d3 | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 7257 | } |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7258 | |
Douglas Gregor | 4aa04b1 | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 7259 | if (!Specialization) { |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7260 | // Create a new class template specialization declaration node for |
| 7261 | // this explicit specialization. |
| 7262 | Specialization |
Douglas Gregor | e902956 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 7263 | = ClassTemplateSpecializationDecl::Create(Context, Kind, |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7264 | ClassTemplate->getDeclContext(), |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 7265 | KWLoc, TemplateNameLoc, |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7266 | ClassTemplate, |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 7267 | Converted.data(), |
| 7268 | Converted.size(), |
| 7269 | PrevDecl); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 7270 | SetNestedNameSpecifier(Specialization, SS); |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7271 | |
Argyrios Kyrtzidis | 47470f2 | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 7272 | if (!HasNoEffect && !PrevDecl) { |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7273 | // Insert the new specialization. |
Argyrios Kyrtzidis | 47470f2 | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 7274 | ClassTemplate->AddSpecialization(Specialization, InsertPos); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7275 | } |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7276 | } |
| 7277 | |
| 7278 | // Build the fully-sugared type for this explicit instantiation as |
| 7279 | // the user wrote in the explicit instantiation itself. This means |
| 7280 | // that we'll pretty-print the type retrieved from the |
| 7281 | // specialization's declaration the way that the user actually wrote |
| 7282 | // the explicit instantiation, rather than formatting the name based |
| 7283 | // on the "canonical" representation used to store the template |
| 7284 | // arguments in the specialization. |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 7285 | TypeSourceInfo *WrittenTy |
| 7286 | = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc, |
| 7287 | TemplateArgs, |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7288 | Context.getTypeDeclType(Specialization)); |
| 7289 | Specialization->setTypeAsWritten(WrittenTy); |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7290 | |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7291 | // Set source locations for keywords. |
| 7292 | Specialization->setExternLoc(ExternLoc); |
| 7293 | Specialization->setTemplateKeywordLoc(TemplateLoc); |
Argyrios Kyrtzidis | 40bcfd7 | 2013-04-22 23:23:42 +0000 | [diff] [blame] | 7294 | Specialization->setRBraceLoc(SourceLocation()); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7295 | |
Rafael Espindola | 0b06207 | 2012-01-03 06:04:21 +0000 | [diff] [blame] | 7296 | if (Attr) |
| 7297 | ProcessDeclAttributeList(S, Specialization, Attr); |
| 7298 | |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7299 | // Add the explicit instantiation into its lexical context. However, |
| 7300 | // since explicit instantiations are never found by name lookup, we |
| 7301 | // just put it into the declaration context directly. |
| 7302 | Specialization->setLexicalDeclContext(CurContext); |
| 7303 | CurContext->addDecl(Specialization); |
| 7304 | |
| 7305 | // Syntax is now OK, so return if it has no other effect on semantics. |
| 7306 | if (HasNoEffect) { |
| 7307 | // Set the template specialization kind. |
| 7308 | Specialization->setTemplateSpecializationKind(TSK); |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7309 | return Specialization; |
Douglas Gregor | 0681a35 | 2009-11-25 06:01:46 +0000 | [diff] [blame] | 7310 | } |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7311 | |
| 7312 | // C++ [temp.explicit]p3: |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7313 | // A definition of a class template or class member template |
| 7314 | // shall be in scope at the point of the explicit instantiation of |
| 7315 | // the class template or class member template. |
| 7316 | // |
| 7317 | // This check comes when we actually try to perform the |
| 7318 | // instantiation. |
Douglas Gregor | 12e49d3 | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 7319 | ClassTemplateSpecializationDecl *Def |
| 7320 | = cast_or_null<ClassTemplateSpecializationDecl>( |
Douglas Gregor | 0a5a221 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 7321 | Specialization->getDefinition()); |
Douglas Gregor | 12e49d3 | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 7322 | if (!Def) |
Douglas Gregor | ef6ab41 | 2009-10-27 06:26:26 +0000 | [diff] [blame] | 7323 | InstantiateClassTemplateSpecialization(TemplateNameLoc, Specialization, TSK); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7324 | else if (TSK == TSK_ExplicitInstantiationDefinition) { |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 7325 | MarkVTableUsed(TemplateNameLoc, Specialization, true); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7326 | Specialization->setPointOfInstantiation(Def->getPointOfInstantiation()); |
| 7327 | } |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 7328 | |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 7329 | // Instantiate the members of this class template specialization. |
| 7330 | Def = cast_or_null<ClassTemplateSpecializationDecl>( |
Douglas Gregor | 0a5a221 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 7331 | Specialization->getDefinition()); |
Rafael Espindola | 8d04f06 | 2010-03-22 23:12:48 +0000 | [diff] [blame] | 7332 | if (Def) { |
Rafael Espindola | fa1708fd | 2010-03-23 19:55:22 +0000 | [diff] [blame] | 7333 | TemplateSpecializationKind Old_TSK = Def->getTemplateSpecializationKind(); |
| 7334 | |
| 7335 | // Fix a TSK_ExplicitInstantiationDeclaration followed by a |
| 7336 | // TSK_ExplicitInstantiationDefinition |
| 7337 | if (Old_TSK == TSK_ExplicitInstantiationDeclaration && |
| 7338 | TSK == TSK_ExplicitInstantiationDefinition) |
Richard Smith | eb36ddf | 2014-04-24 22:45:46 +0000 | [diff] [blame] | 7339 | // FIXME: Need to notify the ASTMutationListener that we did this. |
Rafael Espindola | fa1708fd | 2010-03-23 19:55:22 +0000 | [diff] [blame] | 7340 | Def->setTemplateSpecializationKind(TSK); |
Rafael Espindola | 8d04f06 | 2010-03-22 23:12:48 +0000 | [diff] [blame] | 7341 | |
Douglas Gregor | 12e49d3 | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 7342 | InstantiateClassTemplateSpecializationMembers(TemplateNameLoc, Def, TSK); |
Rafael Espindola | 8d04f06 | 2010-03-22 23:12:48 +0000 | [diff] [blame] | 7343 | } |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7344 | |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7345 | // Set the template specialization kind. |
| 7346 | Specialization->setTemplateSpecializationKind(TSK); |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7347 | return Specialization; |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7348 | } |
| 7349 | |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 7350 | // Explicit instantiation of a member class of a class template. |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7351 | DeclResult |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7352 | Sema::ActOnExplicitInstantiation(Scope *S, |
Douglas Gregor | 43e7517 | 2009-09-04 06:33:52 +0000 | [diff] [blame] | 7353 | SourceLocation ExternLoc, |
| 7354 | SourceLocation TemplateLoc, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7355 | unsigned TagSpec, |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 7356 | SourceLocation KWLoc, |
Jeffrey Yasskin | c76498d | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 7357 | CXXScopeSpec &SS, |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 7358 | IdentifierInfo *Name, |
| 7359 | SourceLocation NameLoc, |
| 7360 | AttributeList *Attr) { |
| 7361 | |
Douglas Gregor | d6ab874 | 2009-05-28 23:31:59 +0000 | [diff] [blame] | 7362 | bool Owned = false; |
John McCall | 7f41d98 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 7363 | bool IsDependent = false; |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 7364 | Decl *TagD = ActOnTag(S, TagSpec, Sema::TUK_Reference, |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7365 | KWLoc, SS, Name, NameLoc, Attr, AS_none, |
Douglas Gregor | 2820e69 | 2011-09-09 19:05:14 +0000 | [diff] [blame] | 7366 | /*ModulePrivateLoc=*/SourceLocation(), |
Benjamin Kramer | cc4c49d | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 7367 | MultiTemplateParamsArg(), Owned, IsDependent, |
Richard Smith | 649c7b06 | 2014-01-08 00:56:48 +0000 | [diff] [blame] | 7368 | SourceLocation(), false, TypeResult(), |
| 7369 | /*IsTypeSpecifier*/false); |
John McCall | 7f41d98 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 7370 | assert(!IsDependent && "explicit instantiation of dependent name not yet handled"); |
| 7371 | |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 7372 | if (!TagD) |
| 7373 | return true; |
| 7374 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7375 | TagDecl *Tag = cast<TagDecl>(TagD); |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 7376 | assert(!Tag->isEnum() && "shouldn't see enumerations here"); |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 7377 | |
Douglas Gregor | b8006faf | 2009-05-27 17:30:49 +0000 | [diff] [blame] | 7378 | if (Tag->isInvalidDecl()) |
| 7379 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7380 | |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 7381 | CXXRecordDecl *Record = cast<CXXRecordDecl>(Tag); |
| 7382 | CXXRecordDecl *Pattern = Record->getInstantiatedFromMemberClass(); |
| 7383 | if (!Pattern) { |
| 7384 | Diag(TemplateLoc, diag::err_explicit_instantiation_nontemplate_type) |
| 7385 | << Context.getTypeDeclType(Record); |
| 7386 | Diag(Record->getLocation(), diag::note_nontemplate_decl_here); |
| 7387 | return true; |
| 7388 | } |
| 7389 | |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7390 | // C++0x [temp.explicit]p2: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7391 | // If the explicit instantiation is for a class or member class, the |
| 7392 | // elaborated-type-specifier in the declaration shall include a |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7393 | // simple-template-id. |
| 7394 | // |
| 7395 | // C++98 has the same restriction, just worded differently. |
| 7396 | if (!ScopeSpecifierHasTemplateId(SS)) |
Douglas Gregor | 010815a | 2010-06-16 16:26:47 +0000 | [diff] [blame] | 7397 | Diag(TemplateLoc, diag::ext_explicit_instantiation_without_qualified_id) |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7398 | << Record << SS.getRange(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7399 | |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7400 | // C++0x [temp.explicit]p2: |
| 7401 | // There are two forms of explicit instantiation: an explicit instantiation |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7402 | // definition and an explicit instantiation declaration. An explicit |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7403 | // instantiation declaration begins with the extern keyword. [...] |
Douglas Gregor | 5d85197 | 2009-10-14 21:46:58 +0000 | [diff] [blame] | 7404 | TemplateSpecializationKind TSK |
| 7405 | = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition |
| 7406 | : TSK_ExplicitInstantiationDeclaration; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7407 | |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 7408 | // C++0x [temp.explicit]p2: |
| 7409 | // [...] An explicit instantiation shall appear in an enclosing |
| 7410 | // namespace of its template. [...] |
| 7411 | // |
| 7412 | // This is C++ DR 275. |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7413 | CheckExplicitInstantiationScope(*this, Record, NameLoc, true); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7414 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7415 | // Verify that it is okay to explicitly instantiate here. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7416 | CXXRecordDecl *PrevDecl |
Douglas Gregor | ec9fd13 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 7417 | = cast_or_null<CXXRecordDecl>(Record->getPreviousDecl()); |
Douglas Gregor | 0a5a221 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 7418 | if (!PrevDecl && Record->getDefinition()) |
Douglas Gregor | 8f003d0 | 2009-10-15 18:07:02 +0000 | [diff] [blame] | 7419 | PrevDecl = Record; |
| 7420 | if (PrevDecl) { |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7421 | MemberSpecializationInfo *MSInfo = PrevDecl->getMemberSpecializationInfo(); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7422 | bool HasNoEffect = false; |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7423 | assert(MSInfo && "No member specialization information?"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7424 | if (CheckSpecializationInstantiationRedecl(TemplateLoc, TSK, |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7425 | PrevDecl, |
| 7426 | MSInfo->getTemplateSpecializationKind(), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7427 | MSInfo->getPointOfInstantiation(), |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7428 | HasNoEffect)) |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7429 | return true; |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7430 | if (HasNoEffect) |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7431 | return TagD; |
| 7432 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7433 | |
Douglas Gregor | 12e49d3 | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 7434 | CXXRecordDecl *RecordDef |
Douglas Gregor | 0a5a221 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 7435 | = cast_or_null<CXXRecordDecl>(Record->getDefinition()); |
Douglas Gregor | 12e49d3 | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 7436 | if (!RecordDef) { |
Douglas Gregor | 68edf13 | 2009-10-15 12:53:22 +0000 | [diff] [blame] | 7437 | // C++ [temp.explicit]p3: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7438 | // A definition of a member class of a class template shall be in scope |
Douglas Gregor | 68edf13 | 2009-10-15 12:53:22 +0000 | [diff] [blame] | 7439 | // at the point of an explicit instantiation of the member class. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7440 | CXXRecordDecl *Def |
Douglas Gregor | 0a5a221 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 7441 | = cast_or_null<CXXRecordDecl>(Pattern->getDefinition()); |
Douglas Gregor | 68edf13 | 2009-10-15 12:53:22 +0000 | [diff] [blame] | 7442 | if (!Def) { |
Douglas Gregor | a8b89d2 | 2009-10-15 14:05:49 +0000 | [diff] [blame] | 7443 | Diag(TemplateLoc, diag::err_explicit_instantiation_undefined_member) |
| 7444 | << 0 << Record->getDeclName() << Record->getDeclContext(); |
Douglas Gregor | 68edf13 | 2009-10-15 12:53:22 +0000 | [diff] [blame] | 7445 | Diag(Pattern->getLocation(), diag::note_forward_declaration) |
| 7446 | << Pattern; |
| 7447 | return true; |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 7448 | } else { |
| 7449 | if (InstantiateClass(NameLoc, Record, Def, |
| 7450 | getTemplateInstantiationArgs(Record), |
| 7451 | TSK)) |
| 7452 | return true; |
| 7453 | |
Douglas Gregor | 0a5a221 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 7454 | RecordDef = cast_or_null<CXXRecordDecl>(Record->getDefinition()); |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 7455 | if (!RecordDef) |
| 7456 | return true; |
| 7457 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7458 | } |
| 7459 | |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 7460 | // Instantiate all of the members of the class. |
| 7461 | InstantiateClassMembers(NameLoc, RecordDef, |
| 7462 | getTemplateInstantiationArgs(Record), TSK); |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 7463 | |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 7464 | if (TSK == TSK_ExplicitInstantiationDefinition) |
| 7465 | MarkVTableUsed(NameLoc, RecordDef, true); |
| 7466 | |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 7467 | // FIXME: We don't have any representation for explicit instantiations of |
| 7468 | // member classes. Such a representation is not needed for compilation, but it |
| 7469 | // should be available for clients that want to see all of the declarations in |
| 7470 | // the source code. |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 7471 | return TagD; |
| 7472 | } |
| 7473 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 7474 | DeclResult Sema::ActOnExplicitInstantiation(Scope *S, |
| 7475 | SourceLocation ExternLoc, |
| 7476 | SourceLocation TemplateLoc, |
| 7477 | Declarator &D) { |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7478 | // Explicit instantiations always require a name. |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 7479 | // TODO: check if/when DNInfo should replace Name. |
| 7480 | DeclarationNameInfo NameInfo = GetNameForDeclarator(D); |
| 7481 | DeclarationName Name = NameInfo.getName(); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7482 | if (!Name) { |
| 7483 | if (!D.isInvalidType()) |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 7484 | Diag(D.getDeclSpec().getLocStart(), |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7485 | diag::err_explicit_instantiation_requires_name) |
| 7486 | << D.getDeclSpec().getSourceRange() |
| 7487 | << D.getSourceRange(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7488 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7489 | return true; |
| 7490 | } |
| 7491 | |
| 7492 | // The scope passed in may not be a decl scope. Zip up the scope tree until |
| 7493 | // we find one that is. |
| 7494 | while ((S->getFlags() & Scope::DeclScope) == 0 || |
| 7495 | (S->getFlags() & Scope::TemplateParamScope) != 0) |
| 7496 | S = S->getParent(); |
| 7497 | |
| 7498 | // Determine the type of the declaration. |
John McCall | 8cb7bdf | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 7499 | TypeSourceInfo *T = GetTypeForDeclarator(D, S); |
| 7500 | QualType R = T->getType(); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7501 | if (R.isNull()) |
| 7502 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7503 | |
Douglas Gregor | 781ba6e | 2011-05-21 18:53:30 +0000 | [diff] [blame] | 7504 | // C++ [dcl.stc]p1: |
| 7505 | // A storage-class-specifier shall not be specified in [...] an explicit |
| 7506 | // instantiation (14.7.2) directive. |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7507 | if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef) { |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7508 | Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_of_typedef) |
| 7509 | << Name; |
| 7510 | return true; |
Douglas Gregor | 781ba6e | 2011-05-21 18:53:30 +0000 | [diff] [blame] | 7511 | } else if (D.getDeclSpec().getStorageClassSpec() |
| 7512 | != DeclSpec::SCS_unspecified) { |
| 7513 | // Complain about then remove the storage class specifier. |
| 7514 | Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_storage_class) |
| 7515 | << FixItHint::CreateRemoval(D.getDeclSpec().getStorageClassSpecLoc()); |
| 7516 | |
| 7517 | D.getMutableDeclSpec().ClearStorageClassSpecs(); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7518 | } |
| 7519 | |
Douglas Gregor | 3c74d41 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 7520 | // C++0x [temp.explicit]p1: |
| 7521 | // [...] An explicit instantiation of a function template shall not use the |
| 7522 | // inline or constexpr specifiers. |
| 7523 | // Presumably, this also applies to member functions of class templates as |
| 7524 | // well. |
Richard Smith | 83c1929 | 2011-10-18 03:44:03 +0000 | [diff] [blame] | 7525 | if (D.getDeclSpec().isInlineSpecified()) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7526 | Diag(D.getDeclSpec().getInlineSpecLoc(), |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 7527 | getLangOpts().CPlusPlus11 ? |
Richard Smith | 83c1929 | 2011-10-18 03:44:03 +0000 | [diff] [blame] | 7528 | diag::err_explicit_instantiation_inline : |
| 7529 | diag::warn_explicit_instantiation_inline_0x) |
Richard Smith | 465841e | 2011-10-14 19:58:02 +0000 | [diff] [blame] | 7530 | << FixItHint::CreateRemoval(D.getDeclSpec().getInlineSpecLoc()); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 7531 | if (D.getDeclSpec().isConstexprSpecified() && R->isFunctionType()) |
Richard Smith | 465841e | 2011-10-14 19:58:02 +0000 | [diff] [blame] | 7532 | // FIXME: Add a fix-it to remove the 'constexpr' and add a 'const' if one is |
| 7533 | // not already specified. |
| 7534 | Diag(D.getDeclSpec().getConstexprSpecLoc(), |
| 7535 | diag::err_explicit_instantiation_constexpr); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7536 | |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7537 | // C++0x [temp.explicit]p2: |
| 7538 | // There are two forms of explicit instantiation: an explicit instantiation |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7539 | // definition and an explicit instantiation declaration. An explicit |
| 7540 | // instantiation declaration begins with the extern keyword. [...] |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7541 | TemplateSpecializationKind TSK |
| 7542 | = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition |
| 7543 | : TSK_ExplicitInstantiationDeclaration; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7544 | |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 7545 | LookupResult Previous(*this, NameInfo, LookupOrdinaryName); |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 7546 | LookupParsedName(Previous, S, &D.getCXXScopeSpec()); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7547 | |
| 7548 | if (!R->isFunctionType()) { |
| 7549 | // C++ [temp.explicit]p1: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7550 | // A [...] static data member of a class template can be explicitly |
| 7551 | // instantiated from the member definition associated with its class |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7552 | // template. |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 7553 | // C++1y [temp.explicit]p1: |
| 7554 | // A [...] variable [...] template specialization can be explicitly |
| 7555 | // instantiated from its template. |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 7556 | if (Previous.isAmbiguous()) |
| 7557 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7558 | |
John McCall | 67c0087 | 2009-12-02 08:25:40 +0000 | [diff] [blame] | 7559 | VarDecl *Prev = Previous.getAsSingle<VarDecl>(); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 7560 | VarTemplateDecl *PrevTemplate = Previous.getAsSingle<VarTemplateDecl>(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7561 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 7562 | if (!PrevTemplate) { |
| 7563 | if (!Prev || !Prev->isStaticDataMember()) { |
| 7564 | // We expect to see a data data member here. |
| 7565 | Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_not_known) |
| 7566 | << Name; |
| 7567 | for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end(); |
| 7568 | P != PEnd; ++P) |
| 7569 | Diag((*P)->getLocation(), diag::note_explicit_instantiation_here); |
| 7570 | return true; |
| 7571 | } |
| 7572 | |
| 7573 | if (!Prev->getInstantiatedFromStaticDataMember()) { |
| 7574 | // FIXME: Check for explicit specialization? |
| 7575 | Diag(D.getIdentifierLoc(), |
| 7576 | diag::err_explicit_instantiation_data_member_not_instantiated) |
| 7577 | << Prev; |
| 7578 | Diag(Prev->getLocation(), diag::note_explicit_instantiation_here); |
| 7579 | // FIXME: Can we provide a note showing where this was declared? |
| 7580 | return true; |
| 7581 | } |
| 7582 | } else { |
| 7583 | // Explicitly instantiate a variable template. |
| 7584 | |
| 7585 | // C++1y [dcl.spec.auto]p6: |
| 7586 | // ... A program that uses auto or decltype(auto) in a context not |
| 7587 | // explicitly allowed in this section is ill-formed. |
| 7588 | // |
| 7589 | // This includes auto-typed variable template instantiations. |
| 7590 | if (R->isUndeducedType()) { |
| 7591 | Diag(T->getTypeLoc().getLocStart(), |
| 7592 | diag::err_auto_not_allowed_var_inst); |
| 7593 | return true; |
| 7594 | } |
| 7595 | |
Richard Smith | ef985ac | 2013-09-18 02:10:12 +0000 | [diff] [blame] | 7596 | if (D.getName().getKind() != UnqualifiedId::IK_TemplateId) { |
| 7597 | // C++1y [temp.explicit]p3: |
| 7598 | // If the explicit instantiation is for a variable, the unqualified-id |
| 7599 | // in the declaration shall be a template-id. |
| 7600 | Diag(D.getIdentifierLoc(), |
| 7601 | diag::err_explicit_instantiation_without_template_id) |
| 7602 | << PrevTemplate; |
| 7603 | Diag(PrevTemplate->getLocation(), |
| 7604 | diag::note_explicit_instantiation_here); |
| 7605 | return true; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 7606 | } |
| 7607 | |
Richard Smith | ef985ac | 2013-09-18 02:10:12 +0000 | [diff] [blame] | 7608 | // Translate the parser's template argument list into our AST format. |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 7609 | TemplateArgumentListInfo TemplateArgs = |
| 7610 | makeTemplateArgumentListInfo(*this, *D.getName().TemplateId); |
Richard Smith | ef985ac | 2013-09-18 02:10:12 +0000 | [diff] [blame] | 7611 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 7612 | DeclResult Res = CheckVarTemplateId(PrevTemplate, TemplateLoc, |
| 7613 | D.getIdentifierLoc(), TemplateArgs); |
| 7614 | if (Res.isInvalid()) |
| 7615 | return true; |
| 7616 | |
| 7617 | // Ignore access control bits, we don't need them for redeclaration |
| 7618 | // checking. |
| 7619 | Prev = cast<VarDecl>(Res.get()); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7620 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7621 | |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7622 | // C++0x [temp.explicit]p2: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7623 | // If the explicit instantiation is for a member function, a member class |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7624 | // or a static data member of a class template specialization, the name of |
| 7625 | // the class template specialization in the qualified-id for the member |
| 7626 | // name shall be a simple-template-id. |
| 7627 | // |
| 7628 | // C++98 has the same restriction, just worded differently. |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 7629 | // |
Richard Smith | 5977d87 | 2013-09-18 21:55:14 +0000 | [diff] [blame] | 7630 | // This does not apply to variable template specializations, where the |
| 7631 | // template-id is in the unqualified-id instead. |
| 7632 | if (!ScopeSpecifierHasTemplateId(D.getCXXScopeSpec()) && !PrevTemplate) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7633 | Diag(D.getIdentifierLoc(), |
Douglas Gregor | 010815a | 2010-06-16 16:26:47 +0000 | [diff] [blame] | 7634 | diag::ext_explicit_instantiation_without_qualified_id) |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7635 | << Prev << D.getCXXScopeSpec().getRange(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7636 | |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7637 | // Check the scope of this explicit instantiation. |
| 7638 | CheckExplicitInstantiationScope(*this, Prev, D.getIdentifierLoc(), true); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7639 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7640 | // Verify that it is okay to explicitly instantiate here. |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 7641 | TemplateSpecializationKind PrevTSK = Prev->getTemplateSpecializationKind(); |
| 7642 | SourceLocation POI = Prev->getPointOfInstantiation(); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7643 | bool HasNoEffect = false; |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 7644 | if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK, Prev, |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 7645 | PrevTSK, POI, HasNoEffect)) |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7646 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7647 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 7648 | if (!HasNoEffect) { |
| 7649 | // Instantiate static data member or variable template. |
| 7650 | |
| 7651 | Prev->setTemplateSpecializationKind(TSK, D.getIdentifierLoc()); |
| 7652 | if (PrevTemplate) { |
| 7653 | // Merge attributes. |
| 7654 | if (AttributeList *Attr = D.getDeclSpec().getAttributes().getList()) |
| 7655 | ProcessDeclAttributeList(S, Prev, Attr); |
| 7656 | } |
| 7657 | if (TSK == TSK_ExplicitInstantiationDefinition) |
| 7658 | InstantiateVariableDefinition(D.getIdentifierLoc(), Prev); |
| 7659 | } |
| 7660 | |
| 7661 | // Check the new variable specialization against the parsed input. |
| 7662 | if (PrevTemplate && Prev && !Context.hasSameType(Prev->getType(), R)) { |
| 7663 | Diag(T->getTypeLoc().getLocStart(), |
| 7664 | diag::err_invalid_var_template_spec_type) |
| 7665 | << 0 << PrevTemplate << R << Prev->getType(); |
| 7666 | Diag(PrevTemplate->getLocation(), diag::note_template_declared_here) |
| 7667 | << 2 << PrevTemplate->getDeclName(); |
| 7668 | return true; |
| 7669 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7670 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7671 | // FIXME: Create an ExplicitInstantiation node? |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7672 | return (Decl*) nullptr; |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7673 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7674 | |
| 7675 | // If the declarator is a template-id, translate the parser's template |
Douglas Gregor | 0e876e0 | 2009-09-25 23:53:26 +0000 | [diff] [blame] | 7676 | // argument list into our AST format. |
Douglas Gregor | d90fd52 | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 7677 | bool HasExplicitTemplateArgs = false; |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 7678 | TemplateArgumentListInfo TemplateArgs; |
Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 7679 | if (D.getName().getKind() == UnqualifiedId::IK_TemplateId) { |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 7680 | TemplateArgs = makeTemplateArgumentListInfo(*this, *D.getName().TemplateId); |
Douglas Gregor | d90fd52 | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 7681 | HasExplicitTemplateArgs = true; |
| 7682 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7683 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7684 | // C++ [temp.explicit]p1: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7685 | // A [...] function [...] can be explicitly instantiated from its template. |
| 7686 | // A member function [...] of a class template can be explicitly |
| 7687 | // instantiated from the member definition associated with its class |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7688 | // template. |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 7689 | UnresolvedSet<8> Matches; |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 7690 | TemplateSpecCandidateSet FailedCandidates(D.getIdentifierLoc()); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7691 | for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end(); |
| 7692 | P != PEnd; ++P) { |
| 7693 | NamedDecl *Prev = *P; |
Douglas Gregor | d90fd52 | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 7694 | if (!HasExplicitTemplateArgs) { |
| 7695 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Prev)) { |
Rafael Espindola | 6edca7d | 2013-12-01 16:54:29 +0000 | [diff] [blame] | 7696 | QualType Adjusted = adjustCCAndNoReturn(R, Method->getType()); |
| 7697 | if (Context.hasSameUnqualifiedType(Method->getType(), Adjusted)) { |
Douglas Gregor | d90fd52 | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 7698 | Matches.clear(); |
Douglas Gregor | ea0a0a9 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 7699 | |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 7700 | Matches.addDecl(Method, P.getAccess()); |
Douglas Gregor | ea0a0a9 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 7701 | if (Method->getTemplateSpecializationKind() == TSK_Undeclared) |
| 7702 | break; |
Douglas Gregor | d90fd52 | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 7703 | } |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7704 | } |
| 7705 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7706 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7707 | FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Prev); |
| 7708 | if (!FunTmpl) |
| 7709 | continue; |
| 7710 | |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 7711 | TemplateDeductionInfo Info(FailedCandidates.getLocation()); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7712 | FunctionDecl *Specialization = nullptr; |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7713 | if (TemplateDeductionResult TDK |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7714 | = DeduceTemplateArguments(FunTmpl, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7715 | (HasExplicitTemplateArgs ? &TemplateArgs |
| 7716 | : nullptr), |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7717 | R, Specialization, Info)) { |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 7718 | // Keep track of almost-matches. |
| 7719 | FailedCandidates.addCandidate() |
| 7720 | .set(FunTmpl->getTemplatedDecl(), |
| 7721 | MakeDeductionFailureInfo(Context, TDK, Info)); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7722 | (void)TDK; |
| 7723 | continue; |
| 7724 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7725 | |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 7726 | Matches.addDecl(Specialization, P.getAccess()); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7727 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7728 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7729 | // Find the most specialized function template specialization. |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 7730 | UnresolvedSetIterator Result = getMostSpecialized( |
Richard Smith | 35e1da2 | 2013-09-10 22:59:25 +0000 | [diff] [blame] | 7731 | Matches.begin(), Matches.end(), FailedCandidates, |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 7732 | D.getIdentifierLoc(), |
| 7733 | PDiag(diag::err_explicit_instantiation_not_known) << Name, |
| 7734 | PDiag(diag::err_explicit_instantiation_ambiguous) << Name, |
| 7735 | PDiag(diag::note_explicit_instantiation_candidate)); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7736 | |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 7737 | if (Result == Matches.end()) |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7738 | return true; |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 7739 | |
| 7740 | // Ignore access control bits, we don't need them for redeclaration checking. |
| 7741 | FunctionDecl *Specialization = cast<FunctionDecl>(*Result); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7742 | |
Alexey Bataev | 7398391 | 2014-11-06 10:10:50 +0000 | [diff] [blame] | 7743 | // C++11 [except.spec]p4 |
| 7744 | // In an explicit instantiation an exception-specification may be specified, |
| 7745 | // but is not required. |
| 7746 | // If an exception-specification is specified in an explicit instantiation |
| 7747 | // directive, it shall be compatible with the exception-specifications of |
| 7748 | // other declarations of that function. |
| 7749 | if (auto *FPT = R->getAs<FunctionProtoType>()) |
| 7750 | if (FPT->hasExceptionSpec()) { |
| 7751 | unsigned DiagID = |
| 7752 | diag::err_mismatched_exception_spec_explicit_instantiation; |
| 7753 | if (getLangOpts().MicrosoftExt) |
| 7754 | DiagID = diag::ext_mismatched_exception_spec_explicit_instantiation; |
| 7755 | bool Result = CheckEquivalentExceptionSpec( |
| 7756 | PDiag(DiagID) << Specialization->getType(), |
| 7757 | PDiag(diag::note_explicit_instantiation_here), |
| 7758 | Specialization->getType()->getAs<FunctionProtoType>(), |
| 7759 | Specialization->getLocation(), FPT, D.getLocStart()); |
| 7760 | // In Microsoft mode, mismatching exception specifications just cause a |
| 7761 | // warning. |
| 7762 | if (!getLangOpts().MicrosoftExt && Result) |
| 7763 | return true; |
| 7764 | } |
| 7765 | |
Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 7766 | if (Specialization->getTemplateSpecializationKind() == TSK_Undeclared) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7767 | Diag(D.getIdentifierLoc(), |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7768 | diag::err_explicit_instantiation_member_function_not_instantiated) |
| 7769 | << Specialization |
| 7770 | << (Specialization->getTemplateSpecializationKind() == |
| 7771 | TSK_ExplicitSpecialization); |
| 7772 | Diag(Specialization->getLocation(), diag::note_explicit_instantiation_here); |
| 7773 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7774 | } |
| 7775 | |
Douglas Gregor | ec9fd13 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 7776 | FunctionDecl *PrevDecl = Specialization->getPreviousDecl(); |
Douglas Gregor | 8f003d0 | 2009-10-15 18:07:02 +0000 | [diff] [blame] | 7777 | if (!PrevDecl && Specialization->isThisDeclarationADefinition()) |
| 7778 | PrevDecl = Specialization; |
| 7779 | |
Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 7780 | if (PrevDecl) { |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7781 | bool HasNoEffect = false; |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 7782 | if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7783 | PrevDecl, |
| 7784 | PrevDecl->getTemplateSpecializationKind(), |
Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 7785 | PrevDecl->getPointOfInstantiation(), |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7786 | HasNoEffect)) |
Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 7787 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7788 | |
Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 7789 | // FIXME: We may still want to build some representation of this |
| 7790 | // explicit specialization. |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7791 | if (HasNoEffect) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7792 | return (Decl*) nullptr; |
Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 7793 | } |
Anders Carlsson | 65e6d13 | 2009-11-24 05:34:41 +0000 | [diff] [blame] | 7794 | |
| 7795 | Specialization->setTemplateSpecializationKind(TSK, D.getIdentifierLoc()); |
Rafael Espindola | 2aa7acf | 2012-01-04 05:40:59 +0000 | [diff] [blame] | 7796 | AttributeList *Attr = D.getDeclSpec().getAttributes().getList(); |
| 7797 | if (Attr) |
| 7798 | ProcessDeclAttributeList(S, Specialization, Attr); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7799 | |
Richard Smith | eb36ddf | 2014-04-24 22:45:46 +0000 | [diff] [blame] | 7800 | if (Specialization->isDefined()) { |
| 7801 | // Let the ASTConsumer know that this function has been explicitly |
| 7802 | // instantiated now, and its linkage might have changed. |
| 7803 | Consumer.HandleTopLevelDecl(DeclGroupRef(Specialization)); |
| 7804 | } else if (TSK == TSK_ExplicitInstantiationDefinition) |
Chandler Carruth | cfe41db | 2010-08-25 08:27:02 +0000 | [diff] [blame] | 7805 | InstantiateFunctionDefinition(D.getIdentifierLoc(), Specialization); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7806 | |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7807 | // C++0x [temp.explicit]p2: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7808 | // If the explicit instantiation is for a member function, a member class |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7809 | // or a static data member of a class template specialization, the name of |
| 7810 | // the class template specialization in the qualified-id for the member |
| 7811 | // name shall be a simple-template-id. |
| 7812 | // |
| 7813 | // C++98 has the same restriction, just worded differently. |
Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 7814 | FunctionTemplateDecl *FunTmpl = Specialization->getPrimaryTemplate(); |
Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 7815 | if (D.getName().getKind() != UnqualifiedId::IK_TemplateId && !FunTmpl && |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7816 | D.getCXXScopeSpec().isSet() && |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7817 | !ScopeSpecifierHasTemplateId(D.getCXXScopeSpec())) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7818 | Diag(D.getIdentifierLoc(), |
Douglas Gregor | 010815a | 2010-06-16 16:26:47 +0000 | [diff] [blame] | 7819 | diag::ext_explicit_instantiation_without_qualified_id) |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7820 | << Specialization << D.getCXXScopeSpec().getRange(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7821 | |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7822 | CheckExplicitInstantiationScope(*this, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7823 | FunTmpl? (NamedDecl *)FunTmpl |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7824 | : Specialization->getInstantiatedFromMemberFunction(), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7825 | D.getIdentifierLoc(), |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7826 | D.getCXXScopeSpec().isSet()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7827 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7828 | // FIXME: Create some kind of ExplicitInstantiationDecl here. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7829 | return (Decl*) nullptr; |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7830 | } |
| 7831 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 7832 | TypeResult |
John McCall | 7f41d98 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 7833 | Sema::ActOnDependentTag(Scope *S, unsigned TagSpec, TagUseKind TUK, |
| 7834 | const CXXScopeSpec &SS, IdentifierInfo *Name, |
| 7835 | SourceLocation TagLoc, SourceLocation NameLoc) { |
| 7836 | // This has to hold, because SS is expected to be defined. |
| 7837 | assert(Name && "Expected a name in a dependent tag"); |
| 7838 | |
Aaron Ballman | 4a97967 | 2014-01-03 13:56:08 +0000 | [diff] [blame] | 7839 | NestedNameSpecifier *NNS = SS.getScopeRep(); |
John McCall | 7f41d98 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 7840 | if (!NNS) |
| 7841 | return true; |
| 7842 | |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 7843 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
Daniel Dunbar | f4b37e1 | 2010-04-01 16:50:48 +0000 | [diff] [blame] | 7844 | |
Douglas Gregor | ba41d01 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 7845 | if (TUK == TUK_Declaration || TUK == TUK_Definition) { |
| 7846 | Diag(NameLoc, diag::err_dependent_tag_decl) |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 7847 | << (TUK == TUK_Definition) << Kind << SS.getRange(); |
Douglas Gregor | ba41d01 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 7848 | return true; |
| 7849 | } |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 7850 | |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 7851 | // Create the resulting type. |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 7852 | ElaboratedTypeKeyword Kwd = TypeWithKeyword::getKeywordForTagTypeKind(Kind); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 7853 | QualType Result = Context.getDependentNameType(Kwd, NNS, Name); |
| 7854 | |
| 7855 | // Create type-source location information for this type. |
| 7856 | TypeLocBuilder TLB; |
| 7857 | DependentNameTypeLoc TL = TLB.push<DependentNameTypeLoc>(Result); |
Abramo Bagnara | 9033e2b | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 7858 | TL.setElaboratedKeywordLoc(TagLoc); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 7859 | TL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 7860 | TL.setNameLoc(NameLoc); |
| 7861 | return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result)); |
John McCall | 7f41d98 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 7862 | } |
| 7863 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 7864 | TypeResult |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7865 | Sema::ActOnTypenameType(Scope *S, SourceLocation TypenameLoc, |
| 7866 | const CXXScopeSpec &SS, const IdentifierInfo &II, |
Douglas Gregor | f7d7771 | 2010-06-16 22:31:08 +0000 | [diff] [blame] | 7867 | SourceLocation IdLoc) { |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 7868 | if (SS.isInvalid()) |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 7869 | return true; |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 7870 | |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 7871 | if (TypenameLoc.isValid() && S && !S->getTemplateParamParent()) |
| 7872 | Diag(TypenameLoc, |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 7873 | getLangOpts().CPlusPlus11 ? |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 7874 | diag::warn_cxx98_compat_typename_outside_of_template : |
| 7875 | diag::ext_typename_outside_of_template) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7876 | << FixItHint::CreateRemoval(TypenameLoc); |
| 7877 | |
Douglas Gregor | 3d0da5f | 2011-03-01 01:34:45 +0000 | [diff] [blame] | 7878 | NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context); |
Douglas Gregor | 844cb50 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 7879 | QualType T = CheckTypenameType(TypenameLoc.isValid()? ETK_Typename : ETK_None, |
| 7880 | TypenameLoc, QualifierLoc, II, IdLoc); |
Douglas Gregor | fe3d7d0 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 7881 | if (T.isNull()) |
| 7882 | return true; |
John McCall | 99b2fe5 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 7883 | |
| 7884 | TypeSourceInfo *TSI = Context.CreateTypeSourceInfo(T); |
| 7885 | if (isa<DependentNameType>(T)) { |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 7886 | DependentNameTypeLoc TL = TSI->getTypeLoc().castAs<DependentNameTypeLoc>(); |
Abramo Bagnara | 9033e2b | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 7887 | TL.setElaboratedKeywordLoc(TypenameLoc); |
Douglas Gregor | 3d0da5f | 2011-03-01 01:34:45 +0000 | [diff] [blame] | 7888 | TL.setQualifierLoc(QualifierLoc); |
John McCall | f7bcc81 | 2010-05-28 23:32:21 +0000 | [diff] [blame] | 7889 | TL.setNameLoc(IdLoc); |
John McCall | 99b2fe5 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 7890 | } else { |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 7891 | ElaboratedTypeLoc TL = TSI->getTypeLoc().castAs<ElaboratedTypeLoc>(); |
Abramo Bagnara | 9033e2b | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 7892 | TL.setElaboratedKeywordLoc(TypenameLoc); |
Douglas Gregor | 844cb50 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 7893 | TL.setQualifierLoc(QualifierLoc); |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 7894 | TL.getNamedTypeLoc().castAs<TypeSpecTypeLoc>().setNameLoc(IdLoc); |
John McCall | 99b2fe5 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 7895 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7896 | |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 7897 | return CreateParsedType(T, TSI); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 7898 | } |
| 7899 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 7900 | TypeResult |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 7901 | Sema::ActOnTypenameType(Scope *S, |
| 7902 | SourceLocation TypenameLoc, |
| 7903 | const CXXScopeSpec &SS, |
| 7904 | SourceLocation TemplateKWLoc, |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 7905 | TemplateTy TemplateIn, |
| 7906 | SourceLocation TemplateNameLoc, |
| 7907 | SourceLocation LAngleLoc, |
| 7908 | ASTTemplateArgsPtr TemplateArgsIn, |
| 7909 | SourceLocation RAngleLoc) { |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 7910 | if (TypenameLoc.isValid() && S && !S->getTemplateParamParent()) |
| 7911 | Diag(TypenameLoc, |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 7912 | getLangOpts().CPlusPlus11 ? |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 7913 | diag::warn_cxx98_compat_typename_outside_of_template : |
| 7914 | diag::ext_typename_outside_of_template) |
| 7915 | << FixItHint::CreateRemoval(TypenameLoc); |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 7916 | |
| 7917 | // Translate the parser's template argument list in our AST format. |
| 7918 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
| 7919 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
| 7920 | |
| 7921 | TemplateName Template = TemplateIn.get(); |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 7922 | if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) { |
| 7923 | // Construct a dependent template specialization type. |
| 7924 | assert(DTN && "dependent template has non-dependent name?"); |
Aaron Ballman | 4a97967 | 2014-01-03 13:56:08 +0000 | [diff] [blame] | 7925 | assert(DTN->getQualifier() == SS.getScopeRep()); |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 7926 | QualType T = Context.getDependentTemplateSpecializationType(ETK_Typename, |
| 7927 | DTN->getQualifier(), |
| 7928 | DTN->getIdentifier(), |
| 7929 | TemplateArgs); |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 7930 | |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 7931 | // Create source-location information for this type. |
John McCall | f7bcc81 | 2010-05-28 23:32:21 +0000 | [diff] [blame] | 7932 | TypeLocBuilder Builder; |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 7933 | DependentTemplateSpecializationTypeLoc SpecTL |
| 7934 | = Builder.push<DependentTemplateSpecializationTypeLoc>(T); |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 7935 | SpecTL.setElaboratedKeywordLoc(TypenameLoc); |
| 7936 | SpecTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
Abramo Bagnara | e0a70b2 | 2012-02-06 22:45:07 +0000 | [diff] [blame] | 7937 | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 7938 | SpecTL.setTemplateNameLoc(TemplateNameLoc); |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 7939 | SpecTL.setLAngleLoc(LAngleLoc); |
| 7940 | SpecTL.setRAngleLoc(RAngleLoc); |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 7941 | for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) |
| 7942 | SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo()); |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 7943 | return CreateParsedType(T, Builder.getTypeSourceInfo(Context, T)); |
Douglas Gregor | 12bbfe1 | 2009-09-02 13:05:45 +0000 | [diff] [blame] | 7944 | } |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 7945 | |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 7946 | QualType T = CheckTemplateIdType(Template, TemplateNameLoc, TemplateArgs); |
| 7947 | if (T.isNull()) |
| 7948 | return true; |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 7949 | |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 7950 | // Provide source-location information for the template specialization type. |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 7951 | TypeLocBuilder Builder; |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 7952 | TemplateSpecializationTypeLoc SpecTL |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 7953 | = Builder.push<TemplateSpecializationTypeLoc>(T); |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 7954 | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); |
| 7955 | SpecTL.setTemplateNameLoc(TemplateNameLoc); |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 7956 | SpecTL.setLAngleLoc(LAngleLoc); |
| 7957 | SpecTL.setRAngleLoc(RAngleLoc); |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 7958 | for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) |
| 7959 | SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo()); |
| 7960 | |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 7961 | T = Context.getElaboratedType(ETK_Typename, SS.getScopeRep(), T); |
| 7962 | ElaboratedTypeLoc TL = Builder.push<ElaboratedTypeLoc>(T); |
Abramo Bagnara | 9033e2b | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 7963 | TL.setElaboratedKeywordLoc(TypenameLoc); |
Douglas Gregor | 844cb50 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 7964 | TL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 7965 | |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 7966 | TypeSourceInfo *TSI = Builder.getTypeSourceInfo(Context, T); |
| 7967 | return CreateParsedType(T, TSI); |
Douglas Gregor | dce2b62 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 7968 | } |
| 7969 | |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 7970 | |
Richard Smith | 6f8d2c6 | 2012-05-09 05:17:00 +0000 | [diff] [blame] | 7971 | /// Determine whether this failed name lookup should be treated as being |
| 7972 | /// disabled by a usage of std::enable_if. |
| 7973 | static bool isEnableIf(NestedNameSpecifierLoc NNS, const IdentifierInfo &II, |
| 7974 | SourceRange &CondRange) { |
| 7975 | // We must be looking for a ::type... |
| 7976 | if (!II.isStr("type")) |
| 7977 | return false; |
| 7978 | |
| 7979 | // ... within an explicitly-written template specialization... |
| 7980 | if (!NNS || !NNS.getNestedNameSpecifier()->getAsType()) |
| 7981 | return false; |
| 7982 | TypeLoc EnableIfTy = NNS.getTypeLoc(); |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 7983 | TemplateSpecializationTypeLoc EnableIfTSTLoc = |
| 7984 | EnableIfTy.getAs<TemplateSpecializationTypeLoc>(); |
| 7985 | if (!EnableIfTSTLoc || EnableIfTSTLoc.getNumArgs() == 0) |
Richard Smith | 6f8d2c6 | 2012-05-09 05:17:00 +0000 | [diff] [blame] | 7986 | return false; |
| 7987 | const TemplateSpecializationType *EnableIfTST = |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 7988 | cast<TemplateSpecializationType>(EnableIfTSTLoc.getTypePtr()); |
Richard Smith | 6f8d2c6 | 2012-05-09 05:17:00 +0000 | [diff] [blame] | 7989 | |
| 7990 | // ... which names a complete class template declaration... |
| 7991 | const TemplateDecl *EnableIfDecl = |
| 7992 | EnableIfTST->getTemplateName().getAsTemplateDecl(); |
| 7993 | if (!EnableIfDecl || EnableIfTST->isIncompleteType()) |
| 7994 | return false; |
| 7995 | |
| 7996 | // ... called "enable_if". |
| 7997 | const IdentifierInfo *EnableIfII = |
| 7998 | EnableIfDecl->getDeclName().getAsIdentifierInfo(); |
| 7999 | if (!EnableIfII || !EnableIfII->isStr("enable_if")) |
| 8000 | return false; |
| 8001 | |
| 8002 | // Assume the first template argument is the condition. |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 8003 | CondRange = EnableIfTSTLoc.getArgLoc(0).getSourceRange(); |
Richard Smith | 6f8d2c6 | 2012-05-09 05:17:00 +0000 | [diff] [blame] | 8004 | return true; |
| 8005 | } |
| 8006 | |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 8007 | /// \brief Build the type that describes a C++ typename specifier, |
| 8008 | /// e.g., "typename T::type". |
| 8009 | QualType |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 8010 | Sema::CheckTypenameType(ElaboratedTypeKeyword Keyword, |
| 8011 | SourceLocation KeywordLoc, |
| 8012 | NestedNameSpecifierLoc QualifierLoc, |
| 8013 | const IdentifierInfo &II, |
Abramo Bagnara | d754848 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 8014 | SourceLocation IILoc) { |
John McCall | 0b66eb3 | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 8015 | CXXScopeSpec SS; |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 8016 | SS.Adopt(QualifierLoc); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 8017 | |
John McCall | 0b66eb3 | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 8018 | DeclContext *Ctx = computeDeclContext(SS); |
| 8019 | if (!Ctx) { |
| 8020 | // If the nested-name-specifier is dependent and couldn't be |
| 8021 | // resolved to a type, build a typename type. |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 8022 | assert(QualifierLoc.getNestedNameSpecifier()->isDependent()); |
| 8023 | return Context.getDependentNameType(Keyword, |
| 8024 | QualifierLoc.getNestedNameSpecifier(), |
| 8025 | &II); |
Douglas Gregor | c9f9b86 | 2009-05-11 19:58:34 +0000 | [diff] [blame] | 8026 | } |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 8027 | |
John McCall | 0b66eb3 | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 8028 | // If the nested-name-specifier refers to the current instantiation, |
| 8029 | // the "typename" keyword itself is superfluous. In C++03, the |
| 8030 | // program is actually ill-formed. However, DR 382 (in C++0x CD1) |
| 8031 | // allows such extraneous "typename" keywords, and we retroactively |
Douglas Gregor | c9d2682 | 2010-06-14 22:07:54 +0000 | [diff] [blame] | 8032 | // apply this DR to C++03 code with only a warning. In any case we continue. |
Douglas Gregor | c9f9b86 | 2009-05-11 19:58:34 +0000 | [diff] [blame] | 8033 | |
John McCall | 0b66eb3 | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 8034 | if (RequireCompleteDeclContext(SS, Ctx)) |
| 8035 | return QualType(); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 8036 | |
| 8037 | DeclarationName Name(&II); |
Abramo Bagnara | d754848 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 8038 | LookupResult Result(*this, Name, IILoc, LookupOrdinaryName); |
Nikola Smiljanic | fce370e | 2014-12-01 23:15:01 +0000 | [diff] [blame] | 8039 | LookupQualifiedName(Result, Ctx, SS); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 8040 | unsigned DiagID = 0; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 8041 | Decl *Referenced = nullptr; |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 8042 | switch (Result.getResultKind()) { |
Richard Smith | 6f8d2c6 | 2012-05-09 05:17:00 +0000 | [diff] [blame] | 8043 | case LookupResult::NotFound: { |
| 8044 | // If we're looking up 'type' within a template named 'enable_if', produce |
| 8045 | // a more specific diagnostic. |
| 8046 | SourceRange CondRange; |
| 8047 | if (isEnableIf(QualifierLoc, II, CondRange)) { |
| 8048 | Diag(CondRange.getBegin(), diag::err_typename_nested_not_found_enable_if) |
| 8049 | << Ctx << CondRange; |
| 8050 | return QualType(); |
| 8051 | } |
| 8052 | |
Douglas Gregor | e40876a | 2009-10-13 21:16:44 +0000 | [diff] [blame] | 8053 | DiagID = diag::err_typename_nested_not_found; |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 8054 | break; |
Richard Smith | 6f8d2c6 | 2012-05-09 05:17:00 +0000 | [diff] [blame] | 8055 | } |
Douglas Gregor | aed2efb | 2010-12-09 00:06:27 +0000 | [diff] [blame] | 8056 | |
| 8057 | case LookupResult::FoundUnresolvedValue: { |
| 8058 | // We found a using declaration that is a value. Most likely, the using |
| 8059 | // declaration itself is meant to have the 'typename' keyword. |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 8060 | SourceRange FullRange(KeywordLoc.isValid() ? KeywordLoc : SS.getBeginLoc(), |
Douglas Gregor | aed2efb | 2010-12-09 00:06:27 +0000 | [diff] [blame] | 8061 | IILoc); |
| 8062 | Diag(IILoc, diag::err_typename_refers_to_using_value_decl) |
| 8063 | << Name << Ctx << FullRange; |
| 8064 | if (UnresolvedUsingValueDecl *Using |
| 8065 | = dyn_cast<UnresolvedUsingValueDecl>(Result.getRepresentativeDecl())){ |
Douglas Gregor | a9d87bc | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 8066 | SourceLocation Loc = Using->getQualifierLoc().getBeginLoc(); |
Douglas Gregor | aed2efb | 2010-12-09 00:06:27 +0000 | [diff] [blame] | 8067 | Diag(Loc, diag::note_using_value_decl_missing_typename) |
| 8068 | << FixItHint::CreateInsertion(Loc, "typename "); |
| 8069 | } |
| 8070 | } |
| 8071 | // Fall through to create a dependent typename type, from which we can recover |
| 8072 | // better. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8073 | |
Douglas Gregor | d0d2ee0 | 2010-01-15 01:44:47 +0000 | [diff] [blame] | 8074 | case LookupResult::NotFoundInCurrentInstantiation: |
| 8075 | // Okay, it's a member of an unknown instantiation. |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 8076 | return Context.getDependentNameType(Keyword, |
| 8077 | QualifierLoc.getNestedNameSpecifier(), |
| 8078 | &II); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 8079 | |
| 8080 | case LookupResult::Found: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8081 | if (TypeDecl *Type = dyn_cast<TypeDecl>(Result.getFoundDecl())) { |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 8082 | // We found a type. Build an ElaboratedType, since the |
| 8083 | // typename-specifier was just sugar. |
Nico Weber | 7288943 | 2014-09-06 01:25:55 +0000 | [diff] [blame] | 8084 | MarkAnyDeclReferenced(Type->getLocation(), Type, /*OdrUse=*/false); |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 8085 | return Context.getElaboratedType(ETK_Typename, |
| 8086 | QualifierLoc.getNestedNameSpecifier(), |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 8087 | Context.getTypeDeclType(Type)); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 8088 | } |
| 8089 | |
| 8090 | DiagID = diag::err_typename_nested_not_type; |
John McCall | 9f3059a | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 8091 | Referenced = Result.getFoundDecl(); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 8092 | break; |
| 8093 | |
| 8094 | case LookupResult::FoundOverloaded: |
| 8095 | DiagID = diag::err_typename_nested_not_type; |
| 8096 | Referenced = *Result.begin(); |
| 8097 | break; |
| 8098 | |
John McCall | 6538c93 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 8099 | case LookupResult::Ambiguous: |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 8100 | return QualType(); |
| 8101 | } |
| 8102 | |
| 8103 | // If we get here, it's because name lookup did not find a |
| 8104 | // type. Emit an appropriate diagnostic and return an error. |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 8105 | SourceRange FullRange(KeywordLoc.isValid() ? KeywordLoc : SS.getBeginLoc(), |
Abramo Bagnara | d754848 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 8106 | IILoc); |
| 8107 | Diag(IILoc, DiagID) << FullRange << Name << Ctx; |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 8108 | if (Referenced) |
| 8109 | Diag(Referenced->getLocation(), diag::note_typename_refers_here) |
| 8110 | << Name; |
| 8111 | return QualType(); |
| 8112 | } |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8113 | |
| 8114 | namespace { |
| 8115 | // See Sema::RebuildTypeInCurrentInstantiation |
Benjamin Kramer | 337e3a5 | 2009-11-28 19:45:26 +0000 | [diff] [blame] | 8116 | class CurrentInstantiationRebuilder |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8117 | : public TreeTransform<CurrentInstantiationRebuilder> { |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8118 | SourceLocation Loc; |
| 8119 | DeclarationName Entity; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8120 | |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8121 | public: |
Douglas Gregor | 14cf752 | 2010-04-30 18:55:50 +0000 | [diff] [blame] | 8122 | typedef TreeTransform<CurrentInstantiationRebuilder> inherited; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8123 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8124 | CurrentInstantiationRebuilder(Sema &SemaRef, |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8125 | SourceLocation Loc, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8126 | DeclarationName Entity) |
| 8127 | : TreeTransform<CurrentInstantiationRebuilder>(SemaRef), |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8128 | Loc(Loc), Entity(Entity) { } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8129 | |
| 8130 | /// \brief Determine whether the given type \p T has already been |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8131 | /// transformed. |
| 8132 | /// |
| 8133 | /// For the purposes of type reconstruction, a type has already been |
| 8134 | /// transformed if it is NULL or if it is not dependent. |
| 8135 | bool AlreadyTransformed(QualType T) { |
| 8136 | return T.isNull() || !T->isDependentType(); |
| 8137 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8138 | |
| 8139 | /// \brief Returns the location of the entity whose type is being |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8140 | /// rebuilt. |
| 8141 | SourceLocation getBaseLocation() { return Loc; } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8142 | |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8143 | /// \brief Returns the name of the entity whose type is being rebuilt. |
| 8144 | DeclarationName getBaseEntity() { return Entity; } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8145 | |
Douglas Gregor | ef6ab41 | 2009-10-27 06:26:26 +0000 | [diff] [blame] | 8146 | /// \brief Sets the "base" location and entity when that |
| 8147 | /// information is known based on another transformation. |
| 8148 | void setBase(SourceLocation Loc, DeclarationName Entity) { |
| 8149 | this->Loc = Loc; |
| 8150 | this->Entity = Entity; |
| 8151 | } |
Douglas Gregor | 0c46b2b | 2012-02-13 22:00:16 +0000 | [diff] [blame] | 8152 | |
| 8153 | ExprResult TransformLambdaExpr(LambdaExpr *E) { |
| 8154 | // Lambdas never need to be transformed. |
| 8155 | return E; |
| 8156 | } |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8157 | }; |
| 8158 | } |
| 8159 | |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8160 | /// \brief Rebuilds a type within the context of the current instantiation. |
| 8161 | /// |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8162 | /// The type \p T is part of the type of an out-of-line member definition of |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8163 | /// a class template (or class template partial specialization) that was parsed |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8164 | /// and constructed before we entered the scope of the class template (or |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8165 | /// partial specialization thereof). This routine will rebuild that type now |
| 8166 | /// that we have entered the declarator's scope, which may produce different |
| 8167 | /// canonical types, e.g., |
| 8168 | /// |
| 8169 | /// \code |
| 8170 | /// template<typename T> |
| 8171 | /// struct X { |
| 8172 | /// typedef T* pointer; |
| 8173 | /// pointer data(); |
| 8174 | /// }; |
| 8175 | /// |
| 8176 | /// template<typename T> |
| 8177 | /// typename X<T>::pointer X<T>::data() { ... } |
| 8178 | /// \endcode |
| 8179 | /// |
Douglas Gregor | c1d2d8a | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 8180 | /// Here, the type "typename X<T>::pointer" will be created as a DependentNameType, |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8181 | /// since we do not know that we can look into X<T> when we parsed the type. |
| 8182 | /// This function will rebuild the type, performing the lookup of "pointer" |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 8183 | /// in X<T> and returning an ElaboratedType whose canonical type is the same |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8184 | /// as the canonical type of T*, allowing the return types of the out-of-line |
| 8185 | /// definition and the declaration to match. |
John McCall | 99b2fe5 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 8186 | TypeSourceInfo *Sema::RebuildTypeInCurrentInstantiation(TypeSourceInfo *T, |
| 8187 | SourceLocation Loc, |
| 8188 | DeclarationName Name) { |
| 8189 | if (!T || !T->getType()->isDependentType()) |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8190 | return T; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8191 | |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8192 | CurrentInstantiationRebuilder Rebuilder(*this, Loc, Name); |
| 8193 | return Rebuilder.TransformType(T); |
Benjamin Kramer | 854d7de | 2009-08-11 22:33:06 +0000 | [diff] [blame] | 8194 | } |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 8195 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8196 | ExprResult Sema::RebuildExprInCurrentInstantiation(Expr *E) { |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 8197 | CurrentInstantiationRebuilder Rebuilder(*this, E->getExprLoc(), |
| 8198 | DeclarationName()); |
| 8199 | return Rebuilder.TransformExpr(E); |
| 8200 | } |
| 8201 | |
John McCall | 99b2fe5 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 8202 | bool Sema::RebuildNestedNameSpecifierInCurrentInstantiation(CXXScopeSpec &SS) { |
Douglas Gregor | 1017641 | 2011-02-25 16:07:42 +0000 | [diff] [blame] | 8203 | if (SS.isInvalid()) |
| 8204 | return true; |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 8205 | |
Douglas Gregor | 1017641 | 2011-02-25 16:07:42 +0000 | [diff] [blame] | 8206 | NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context); |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 8207 | CurrentInstantiationRebuilder Rebuilder(*this, SS.getRange().getBegin(), |
| 8208 | DeclarationName()); |
Douglas Gregor | 1017641 | 2011-02-25 16:07:42 +0000 | [diff] [blame] | 8209 | NestedNameSpecifierLoc Rebuilt |
| 8210 | = Rebuilder.TransformNestedNameSpecifierLoc(QualifierLoc); |
| 8211 | if (!Rebuilt) |
| 8212 | return true; |
John McCall | 99b2fe5 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 8213 | |
Douglas Gregor | 1017641 | 2011-02-25 16:07:42 +0000 | [diff] [blame] | 8214 | SS.Adopt(Rebuilt); |
John McCall | 99b2fe5 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 8215 | return false; |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 8216 | } |
| 8217 | |
Douglas Gregor | 041b084 | 2011-10-14 15:31:12 +0000 | [diff] [blame] | 8218 | /// \brief Rebuild the template parameters now that we know we're in a current |
| 8219 | /// instantiation. |
| 8220 | bool Sema::RebuildTemplateParamsInCurrentInstantiation( |
| 8221 | TemplateParameterList *Params) { |
| 8222 | for (unsigned I = 0, N = Params->size(); I != N; ++I) { |
| 8223 | Decl *Param = Params->getParam(I); |
| 8224 | |
| 8225 | // There is nothing to rebuild in a type parameter. |
| 8226 | if (isa<TemplateTypeParmDecl>(Param)) |
| 8227 | continue; |
| 8228 | |
| 8229 | // Rebuild the template parameter list of a template template parameter. |
| 8230 | if (TemplateTemplateParmDecl *TTP |
| 8231 | = dyn_cast<TemplateTemplateParmDecl>(Param)) { |
| 8232 | if (RebuildTemplateParamsInCurrentInstantiation( |
| 8233 | TTP->getTemplateParameters())) |
| 8234 | return true; |
| 8235 | |
| 8236 | continue; |
| 8237 | } |
| 8238 | |
| 8239 | // Rebuild the type of a non-type template parameter. |
| 8240 | NonTypeTemplateParmDecl *NTTP = cast<NonTypeTemplateParmDecl>(Param); |
| 8241 | TypeSourceInfo *NewTSI |
| 8242 | = RebuildTypeInCurrentInstantiation(NTTP->getTypeSourceInfo(), |
| 8243 | NTTP->getLocation(), |
| 8244 | NTTP->getDeclName()); |
| 8245 | if (!NewTSI) |
| 8246 | return true; |
| 8247 | |
| 8248 | if (NewTSI != NTTP->getTypeSourceInfo()) { |
| 8249 | NTTP->setTypeSourceInfo(NewTSI); |
| 8250 | NTTP->setType(NewTSI->getType()); |
| 8251 | } |
| 8252 | } |
| 8253 | |
| 8254 | return false; |
| 8255 | } |
| 8256 | |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 8257 | /// \brief Produces a formatted string that describes the binding of |
| 8258 | /// template parameters to template arguments. |
| 8259 | std::string |
| 8260 | Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params, |
| 8261 | const TemplateArgumentList &Args) { |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 8262 | return getTemplateArgumentBindingsText(Params, Args.data(), Args.size()); |
Douglas Gregor | e62e6a0 | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 8263 | } |
| 8264 | |
| 8265 | std::string |
| 8266 | Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params, |
| 8267 | const TemplateArgument *Args, |
| 8268 | unsigned NumArgs) { |
Dylan Noblesmith | 2c1dd27 | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 8269 | SmallString<128> Str; |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 8270 | llvm::raw_svector_ostream Out(Str); |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 8271 | |
Douglas Gregor | e62e6a0 | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 8272 | if (!Params || Params->size() == 0 || NumArgs == 0) |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 8273 | return std::string(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8274 | |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 8275 | for (unsigned I = 0, N = Params->size(); I != N; ++I) { |
Douglas Gregor | e62e6a0 | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 8276 | if (I >= NumArgs) |
| 8277 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8278 | |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 8279 | if (I == 0) |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 8280 | Out << "[with "; |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 8281 | else |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 8282 | Out << ", "; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8283 | |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 8284 | if (const IdentifierInfo *Id = Params->getParam(I)->getIdentifier()) { |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 8285 | Out << Id->getName(); |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 8286 | } else { |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 8287 | Out << '$' << I; |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 8288 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8289 | |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 8290 | Out << " = "; |
Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 8291 | Args[I].print(getPrintingPolicy(), Out); |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 8292 | } |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 8293 | |
| 8294 | Out << ']'; |
| 8295 | return Out.str(); |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 8296 | } |
Francois Pichet | 1c229c0 | 2011-04-22 22:18:13 +0000 | [diff] [blame] | 8297 | |
Richard Smith | e40f2ba | 2013-08-07 21:41:30 +0000 | [diff] [blame] | 8298 | void Sema::MarkAsLateParsedTemplate(FunctionDecl *FD, Decl *FnD, |
| 8299 | CachedTokens &Toks) { |
Francois Pichet | 1c229c0 | 2011-04-22 22:18:13 +0000 | [diff] [blame] | 8300 | if (!FD) |
| 8301 | return; |
Richard Smith | e40f2ba | 2013-08-07 21:41:30 +0000 | [diff] [blame] | 8302 | |
| 8303 | LateParsedTemplate *LPT = new LateParsedTemplate; |
| 8304 | |
| 8305 | // Take tokens to avoid allocations |
| 8306 | LPT->Toks.swap(Toks); |
| 8307 | LPT->D = FnD; |
Chandler Carruth | 52cee4d | 2015-03-26 09:08:15 +0000 | [diff] [blame^] | 8308 | LateParsedTemplateMap.insert(std::make_pair(FD, LPT)); |
Richard Smith | e40f2ba | 2013-08-07 21:41:30 +0000 | [diff] [blame] | 8309 | |
| 8310 | FD->setLateTemplateParsed(true); |
| 8311 | } |
| 8312 | |
| 8313 | void Sema::UnmarkAsLateParsedTemplate(FunctionDecl *FD) { |
| 8314 | if (!FD) |
| 8315 | return; |
| 8316 | FD->setLateTemplateParsed(false); |
| 8317 | } |
Francois Pichet | 1c229c0 | 2011-04-22 22:18:13 +0000 | [diff] [blame] | 8318 | |
| 8319 | bool Sema::IsInsideALocalClassWithinATemplateFunction() { |
| 8320 | DeclContext *DC = CurContext; |
| 8321 | |
| 8322 | while (DC) { |
| 8323 | if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(CurContext)) { |
| 8324 | const FunctionDecl *FD = RD->isLocalClass(); |
| 8325 | return (FD && FD->getTemplatedKind() != FunctionDecl::TK_NonTemplate); |
| 8326 | } else if (DC->isTranslationUnit() || DC->isNamespace()) |
| 8327 | return false; |
| 8328 | |
| 8329 | DC = DC->getParent(); |
| 8330 | } |
| 8331 | return false; |
| 8332 | } |